xref: /openbmc/linux/net/wireless/nl80211.c (revision 2ae1beb3)
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->channels[n_channels];
9683 	request->n_ssids = n_ssids;
9684 	if (ie_len) {
9685 		if (n_ssids)
9686 			request->ie = (void *)(request->ssids + n_ssids);
9687 		else
9688 			request->ie = (void *)(request->channels + n_channels);
9689 	}
9690 
9691 	if (n_match_sets) {
9692 		if (request->ie)
9693 			request->match_sets = (void *)(request->ie + ie_len);
9694 		else if (n_ssids)
9695 			request->match_sets =
9696 				(void *)(request->ssids + n_ssids);
9697 		else
9698 			request->match_sets =
9699 				(void *)(request->channels + n_channels);
9700 	}
9701 	request->n_match_sets = n_match_sets;
9702 
9703 	if (n_match_sets)
9704 		request->scan_plans = (void *)(request->match_sets +
9705 					       n_match_sets);
9706 	else if (request->ie)
9707 		request->scan_plans = (void *)(request->ie + ie_len);
9708 	else if (n_ssids)
9709 		request->scan_plans = (void *)(request->ssids + n_ssids);
9710 	else
9711 		request->scan_plans = (void *)(request->channels + n_channels);
9712 
9713 	request->n_scan_plans = n_plans;
9714 
9715 	i = 0;
9716 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9717 		/* user specified, bail out if channel not found */
9718 		nla_for_each_nested(attr,
9719 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
9720 				    tmp) {
9721 			struct ieee80211_channel *chan;
9722 
9723 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
9724 
9725 			if (!chan) {
9726 				err = -EINVAL;
9727 				goto out_free;
9728 			}
9729 
9730 			/* ignore disabled channels */
9731 			if (chan->flags & IEEE80211_CHAN_DISABLED)
9732 				continue;
9733 
9734 			request->channels[i] = chan;
9735 			i++;
9736 		}
9737 	} else {
9738 		/* all channels */
9739 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
9740 			int j;
9741 
9742 			if (!wiphy->bands[band])
9743 				continue;
9744 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9745 				struct ieee80211_channel *chan;
9746 
9747 				chan = &wiphy->bands[band]->channels[j];
9748 
9749 				if (chan->flags & IEEE80211_CHAN_DISABLED)
9750 					continue;
9751 
9752 				request->channels[i] = chan;
9753 				i++;
9754 			}
9755 		}
9756 	}
9757 
9758 	if (!i) {
9759 		err = -EINVAL;
9760 		goto out_free;
9761 	}
9762 
9763 	request->n_channels = i;
9764 
9765 	i = 0;
9766 	if (n_ssids) {
9767 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9768 				    tmp) {
9769 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9770 				err = -EINVAL;
9771 				goto out_free;
9772 			}
9773 			request->ssids[i].ssid_len = nla_len(attr);
9774 			memcpy(request->ssids[i].ssid, nla_data(attr),
9775 			       nla_len(attr));
9776 			i++;
9777 		}
9778 	}
9779 
9780 	i = 0;
9781 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9782 		nla_for_each_nested(attr,
9783 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9784 				    tmp) {
9785 			struct nlattr *ssid, *bssid, *rssi;
9786 
9787 			err = nla_parse_nested_deprecated(tb,
9788 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9789 							  attr,
9790 							  nl80211_match_policy,
9791 							  NULL);
9792 			if (err)
9793 				goto out_free;
9794 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
9795 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
9796 
9797 			if (!ssid && !bssid) {
9798 				i++;
9799 				continue;
9800 			}
9801 
9802 			if (WARN_ON(i >= n_match_sets)) {
9803 				/* this indicates a programming error,
9804 				 * the loop above should have verified
9805 				 * things properly
9806 				 */
9807 				err = -EINVAL;
9808 				goto out_free;
9809 			}
9810 
9811 			if (ssid) {
9812 				memcpy(request->match_sets[i].ssid.ssid,
9813 				       nla_data(ssid), nla_len(ssid));
9814 				request->match_sets[i].ssid.ssid_len =
9815 					nla_len(ssid);
9816 			}
9817 			if (bssid)
9818 				memcpy(request->match_sets[i].bssid,
9819 				       nla_data(bssid), ETH_ALEN);
9820 
9821 			/* special attribute - old implementation w/a */
9822 			request->match_sets[i].rssi_thold = default_match_rssi;
9823 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9824 			if (rssi)
9825 				request->match_sets[i].rssi_thold =
9826 					nla_get_s32(rssi);
9827 
9828 			/* Parse per band RSSI attribute */
9829 			err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
9830 				&request->match_sets[i],
9831 				tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
9832 				request->match_sets[i].rssi_thold);
9833 			if (err)
9834 				goto out_free;
9835 
9836 			i++;
9837 		}
9838 
9839 		/* there was no other matchset, so the RSSI one is alone */
9840 		if (i == 0 && n_match_sets)
9841 			request->match_sets[0].rssi_thold = default_match_rssi;
9842 
9843 		request->min_rssi_thold = INT_MAX;
9844 		for (i = 0; i < n_match_sets; i++)
9845 			request->min_rssi_thold =
9846 				min(request->match_sets[i].rssi_thold,
9847 				    request->min_rssi_thold);
9848 	} else {
9849 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
9850 	}
9851 
9852 	if (ie_len) {
9853 		request->ie_len = ie_len;
9854 		memcpy((void *)request->ie,
9855 		       nla_data(attrs[NL80211_ATTR_IE]),
9856 		       request->ie_len);
9857 	}
9858 
9859 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
9860 	if (err)
9861 		goto out_free;
9862 
9863 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
9864 		request->delay =
9865 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
9866 
9867 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
9868 		request->relative_rssi = nla_get_s8(
9869 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
9870 		request->relative_rssi_set = true;
9871 	}
9872 
9873 	if (request->relative_rssi_set &&
9874 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
9875 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
9876 
9877 		rssi_adjust = nla_data(
9878 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
9879 		request->rssi_adjust.band = rssi_adjust->band;
9880 		request->rssi_adjust.delta = rssi_adjust->delta;
9881 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
9882 			err = -EINVAL;
9883 			goto out_free;
9884 		}
9885 	}
9886 
9887 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
9888 	if (err)
9889 		goto out_free;
9890 
9891 	request->scan_start = jiffies;
9892 
9893 	return request;
9894 
9895 out_free:
9896 	kfree(request);
9897 	return ERR_PTR(err);
9898 }
9899 
9900 static int nl80211_start_sched_scan(struct sk_buff *skb,
9901 				    struct genl_info *info)
9902 {
9903 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9904 	struct net_device *dev = info->user_ptr[1];
9905 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9906 	struct cfg80211_sched_scan_request *sched_scan_req;
9907 	bool want_multi;
9908 	int err;
9909 
9910 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
9911 		return -EOPNOTSUPP;
9912 
9913 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
9914 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
9915 	if (err)
9916 		return err;
9917 
9918 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
9919 						  info->attrs,
9920 						  rdev->wiphy.max_match_sets);
9921 
9922 	err = PTR_ERR_OR_ZERO(sched_scan_req);
9923 	if (err)
9924 		goto out_err;
9925 
9926 	/* leave request id zero for legacy request
9927 	 * or if driver does not support multi-scheduled scan
9928 	 */
9929 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1)
9930 		sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
9931 
9932 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
9933 	if (err)
9934 		goto out_free;
9935 
9936 	sched_scan_req->dev = dev;
9937 	sched_scan_req->wiphy = &rdev->wiphy;
9938 
9939 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9940 		sched_scan_req->owner_nlportid = info->snd_portid;
9941 
9942 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
9943 
9944 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
9945 	return 0;
9946 
9947 out_free:
9948 	kfree(sched_scan_req);
9949 out_err:
9950 	return err;
9951 }
9952 
9953 static int nl80211_stop_sched_scan(struct sk_buff *skb,
9954 				   struct genl_info *info)
9955 {
9956 	struct cfg80211_sched_scan_request *req;
9957 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9958 	u64 cookie;
9959 
9960 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
9961 		return -EOPNOTSUPP;
9962 
9963 	if (info->attrs[NL80211_ATTR_COOKIE]) {
9964 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9965 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
9966 	}
9967 
9968 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
9969 				     struct cfg80211_sched_scan_request,
9970 				     list);
9971 	if (!req || req->reqid ||
9972 	    (req->owner_nlportid &&
9973 	     req->owner_nlportid != info->snd_portid))
9974 		return -ENOENT;
9975 
9976 	return cfg80211_stop_sched_scan_req(rdev, req, false);
9977 }
9978 
9979 static int nl80211_start_radar_detection(struct sk_buff *skb,
9980 					 struct genl_info *info)
9981 {
9982 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9983 	struct net_device *dev = info->user_ptr[1];
9984 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9985 	struct wiphy *wiphy = wdev->wiphy;
9986 	struct cfg80211_chan_def chandef;
9987 	enum nl80211_dfs_regions dfs_region;
9988 	unsigned int cac_time_ms;
9989 	int err = -EINVAL;
9990 
9991 	flush_delayed_work(&rdev->dfs_update_channels_wk);
9992 
9993 	wiphy_lock(wiphy);
9994 
9995 	dfs_region = reg_get_dfs_region(wiphy);
9996 	if (dfs_region == NL80211_DFS_UNSET)
9997 		goto unlock;
9998 
9999 	err = nl80211_parse_chandef(rdev, info, &chandef);
10000 	if (err)
10001 		goto unlock;
10002 
10003 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
10004 	if (err < 0)
10005 		goto unlock;
10006 
10007 	if (err == 0) {
10008 		err = -EINVAL;
10009 		goto unlock;
10010 	}
10011 
10012 	if (!cfg80211_chandef_dfs_usable(wiphy, &chandef)) {
10013 		err = -EINVAL;
10014 		goto unlock;
10015 	}
10016 
10017 	if (nla_get_flag(info->attrs[NL80211_ATTR_RADAR_BACKGROUND])) {
10018 		err = cfg80211_start_background_radar_detection(rdev, wdev,
10019 								&chandef);
10020 		goto unlock;
10021 	}
10022 
10023 	if (netif_carrier_ok(dev)) {
10024 		err = -EBUSY;
10025 		goto unlock;
10026 	}
10027 
10028 	if (wdev->cac_started) {
10029 		err = -EBUSY;
10030 		goto unlock;
10031 	}
10032 
10033 	/* CAC start is offloaded to HW and can't be started manually */
10034 	if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD)) {
10035 		err = -EOPNOTSUPP;
10036 		goto unlock;
10037 	}
10038 
10039 	if (!rdev->ops->start_radar_detection) {
10040 		err = -EOPNOTSUPP;
10041 		goto unlock;
10042 	}
10043 
10044 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
10045 	if (WARN_ON(!cac_time_ms))
10046 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
10047 
10048 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
10049 	if (!err) {
10050 		wdev->links[0].ap.chandef = chandef;
10051 		wdev->cac_started = true;
10052 		wdev->cac_start_time = jiffies;
10053 		wdev->cac_time_ms = cac_time_ms;
10054 	}
10055 unlock:
10056 	wiphy_unlock(wiphy);
10057 
10058 	return err;
10059 }
10060 
10061 static int nl80211_notify_radar_detection(struct sk_buff *skb,
10062 					  struct genl_info *info)
10063 {
10064 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10065 	struct net_device *dev = info->user_ptr[1];
10066 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10067 	struct wiphy *wiphy = wdev->wiphy;
10068 	struct cfg80211_chan_def chandef;
10069 	enum nl80211_dfs_regions dfs_region;
10070 	int err;
10071 
10072 	dfs_region = reg_get_dfs_region(wiphy);
10073 	if (dfs_region == NL80211_DFS_UNSET) {
10074 		GENL_SET_ERR_MSG(info,
10075 				 "DFS Region is not set. Unexpected Radar indication");
10076 		return -EINVAL;
10077 	}
10078 
10079 	err = nl80211_parse_chandef(rdev, info, &chandef);
10080 	if (err) {
10081 		GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
10082 		return err;
10083 	}
10084 
10085 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
10086 	if (err < 0) {
10087 		GENL_SET_ERR_MSG(info, "chandef is invalid");
10088 		return err;
10089 	}
10090 
10091 	if (err == 0) {
10092 		GENL_SET_ERR_MSG(info,
10093 				 "Unexpected Radar indication for chandef/iftype");
10094 		return -EINVAL;
10095 	}
10096 
10097 	/* Do not process this notification if radar is already detected
10098 	 * by kernel on this channel, and return success.
10099 	 */
10100 	if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
10101 		return 0;
10102 
10103 	cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
10104 
10105 	cfg80211_sched_dfs_chan_update(rdev);
10106 
10107 	rdev->radar_chandef = chandef;
10108 
10109 	/* Propagate this notification to other radios as well */
10110 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
10111 
10112 	return 0;
10113 }
10114 
10115 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
10116 {
10117 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10118 	unsigned int link_id = nl80211_link_id(info->attrs);
10119 	struct net_device *dev = info->user_ptr[1];
10120 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10121 	struct cfg80211_csa_settings params;
10122 	struct nlattr **csa_attrs = NULL;
10123 	int err;
10124 	bool need_new_beacon = false;
10125 	bool need_handle_dfs_flag = true;
10126 	int len, i;
10127 	u32 cs_count;
10128 
10129 	if (!rdev->ops->channel_switch ||
10130 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
10131 		return -EOPNOTSUPP;
10132 
10133 	switch (dev->ieee80211_ptr->iftype) {
10134 	case NL80211_IFTYPE_AP:
10135 	case NL80211_IFTYPE_P2P_GO:
10136 		need_new_beacon = true;
10137 		/* For all modes except AP the handle_dfs flag needs to be
10138 		 * supplied to tell the kernel that userspace will handle radar
10139 		 * events when they happen. Otherwise a switch to a channel
10140 		 * requiring DFS will be rejected.
10141 		 */
10142 		need_handle_dfs_flag = false;
10143 
10144 		/* useless if AP is not running */
10145 		if (!wdev->links[link_id].ap.beacon_interval)
10146 			return -ENOTCONN;
10147 		break;
10148 	case NL80211_IFTYPE_ADHOC:
10149 		if (!wdev->u.ibss.ssid_len)
10150 			return -ENOTCONN;
10151 		break;
10152 	case NL80211_IFTYPE_MESH_POINT:
10153 		if (!wdev->u.mesh.id_len)
10154 			return -ENOTCONN;
10155 		break;
10156 	default:
10157 		return -EOPNOTSUPP;
10158 	}
10159 
10160 	memset(&params, 0, sizeof(params));
10161 	params.beacon_csa.ftm_responder = -1;
10162 
10163 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
10164 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
10165 		return -EINVAL;
10166 
10167 	/* only important for AP, IBSS and mesh create IEs internally */
10168 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
10169 		return -EINVAL;
10170 
10171 	/* Even though the attribute is u32, the specification says
10172 	 * u8, so let's make sure we don't overflow.
10173 	 */
10174 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
10175 	if (cs_count > 255)
10176 		return -EINVAL;
10177 
10178 	params.count = cs_count;
10179 
10180 	if (!need_new_beacon)
10181 		goto skip_beacons;
10182 
10183 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after,
10184 				   info->extack);
10185 	if (err)
10186 		goto free;
10187 
10188 	csa_attrs = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*csa_attrs),
10189 			    GFP_KERNEL);
10190 	if (!csa_attrs) {
10191 		err = -ENOMEM;
10192 		goto free;
10193 	}
10194 
10195 	err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
10196 					  info->attrs[NL80211_ATTR_CSA_IES],
10197 					  nl80211_policy, info->extack);
10198 	if (err)
10199 		goto free;
10200 
10201 	err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa,
10202 				   info->extack);
10203 	if (err)
10204 		goto free;
10205 
10206 	if (!csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
10207 		err = -EINVAL;
10208 		goto free;
10209 	}
10210 
10211 	len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
10212 	if (!len || (len % sizeof(u16))) {
10213 		err = -EINVAL;
10214 		goto free;
10215 	}
10216 
10217 	params.n_counter_offsets_beacon = len / sizeof(u16);
10218 	if (rdev->wiphy.max_num_csa_counters &&
10219 	    (params.n_counter_offsets_beacon >
10220 	     rdev->wiphy.max_num_csa_counters)) {
10221 		err = -EINVAL;
10222 		goto free;
10223 	}
10224 
10225 	params.counter_offsets_beacon =
10226 		nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
10227 
10228 	/* sanity checks - counters should fit and be the same */
10229 	for (i = 0; i < params.n_counter_offsets_beacon; i++) {
10230 		u16 offset = params.counter_offsets_beacon[i];
10231 
10232 		if (offset >= params.beacon_csa.tail_len) {
10233 			err = -EINVAL;
10234 			goto free;
10235 		}
10236 
10237 		if (params.beacon_csa.tail[offset] != params.count) {
10238 			err = -EINVAL;
10239 			goto free;
10240 		}
10241 	}
10242 
10243 	if (csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
10244 		len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
10245 		if (!len || (len % sizeof(u16))) {
10246 			err = -EINVAL;
10247 			goto free;
10248 		}
10249 
10250 		params.n_counter_offsets_presp = len / sizeof(u16);
10251 		if (rdev->wiphy.max_num_csa_counters &&
10252 		    (params.n_counter_offsets_presp >
10253 		     rdev->wiphy.max_num_csa_counters)) {
10254 			err = -EINVAL;
10255 			goto free;
10256 		}
10257 
10258 		params.counter_offsets_presp =
10259 			nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
10260 
10261 		/* sanity checks - counters should fit and be the same */
10262 		for (i = 0; i < params.n_counter_offsets_presp; i++) {
10263 			u16 offset = params.counter_offsets_presp[i];
10264 
10265 			if (offset >= params.beacon_csa.probe_resp_len) {
10266 				err = -EINVAL;
10267 				goto free;
10268 			}
10269 
10270 			if (params.beacon_csa.probe_resp[offset] !=
10271 			    params.count) {
10272 				err = -EINVAL;
10273 				goto free;
10274 			}
10275 		}
10276 	}
10277 
10278 skip_beacons:
10279 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
10280 	if (err)
10281 		goto free;
10282 
10283 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
10284 					   wdev->iftype)) {
10285 		err = -EINVAL;
10286 		goto free;
10287 	}
10288 
10289 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
10290 					    &params.chandef,
10291 					    wdev->iftype);
10292 	if (err < 0)
10293 		goto free;
10294 
10295 	if (err > 0) {
10296 		params.radar_required = true;
10297 		if (need_handle_dfs_flag &&
10298 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
10299 			err = -EINVAL;
10300 			goto free;
10301 		}
10302 	}
10303 
10304 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
10305 		params.block_tx = true;
10306 
10307 	if (info->attrs[NL80211_ATTR_PUNCT_BITMAP]) {
10308 		err = nl80211_parse_punct_bitmap(rdev, info,
10309 						 &params.chandef,
10310 						 &params.punct_bitmap);
10311 		if (err)
10312 			goto free;
10313 	}
10314 
10315 	wdev_lock(wdev);
10316 	err = rdev_channel_switch(rdev, dev, &params);
10317 	wdev_unlock(wdev);
10318 
10319 free:
10320 	kfree(params.beacon_after.mbssid_ies);
10321 	kfree(params.beacon_csa.mbssid_ies);
10322 	kfree(params.beacon_after.rnr_ies);
10323 	kfree(params.beacon_csa.rnr_ies);
10324 	kfree(csa_attrs);
10325 	return err;
10326 }
10327 
10328 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
10329 			    u32 seq, int flags,
10330 			    struct cfg80211_registered_device *rdev,
10331 			    struct wireless_dev *wdev,
10332 			    struct cfg80211_internal_bss *intbss)
10333 {
10334 	struct cfg80211_bss *res = &intbss->pub;
10335 	const struct cfg80211_bss_ies *ies;
10336 	unsigned int link_id;
10337 	void *hdr;
10338 	struct nlattr *bss;
10339 
10340 	ASSERT_WDEV_LOCK(wdev);
10341 
10342 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
10343 			     NL80211_CMD_NEW_SCAN_RESULTS);
10344 	if (!hdr)
10345 		return -1;
10346 
10347 	genl_dump_check_consistent(cb, hdr);
10348 
10349 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
10350 		goto nla_put_failure;
10351 	if (wdev->netdev &&
10352 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
10353 		goto nla_put_failure;
10354 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
10355 			      NL80211_ATTR_PAD))
10356 		goto nla_put_failure;
10357 
10358 	bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
10359 	if (!bss)
10360 		goto nla_put_failure;
10361 	if ((!is_zero_ether_addr(res->bssid) &&
10362 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
10363 		goto nla_put_failure;
10364 
10365 	rcu_read_lock();
10366 	/* indicate whether we have probe response data or not */
10367 	if (rcu_access_pointer(res->proberesp_ies) &&
10368 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
10369 		goto fail_unlock_rcu;
10370 
10371 	/* this pointer prefers to be pointed to probe response data
10372 	 * but is always valid
10373 	 */
10374 	ies = rcu_dereference(res->ies);
10375 	if (ies) {
10376 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
10377 				      NL80211_BSS_PAD))
10378 			goto fail_unlock_rcu;
10379 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
10380 					ies->len, ies->data))
10381 			goto fail_unlock_rcu;
10382 	}
10383 
10384 	/* and this pointer is always (unless driver didn't know) beacon data */
10385 	ies = rcu_dereference(res->beacon_ies);
10386 	if (ies && ies->from_beacon) {
10387 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
10388 				      NL80211_BSS_PAD))
10389 			goto fail_unlock_rcu;
10390 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
10391 					ies->len, ies->data))
10392 			goto fail_unlock_rcu;
10393 	}
10394 	rcu_read_unlock();
10395 
10396 	if (res->beacon_interval &&
10397 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
10398 		goto nla_put_failure;
10399 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
10400 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
10401 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY_OFFSET,
10402 			res->channel->freq_offset) ||
10403 	    nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
10404 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
10405 			jiffies_to_msecs(jiffies - intbss->ts)))
10406 		goto nla_put_failure;
10407 
10408 	if (intbss->parent_tsf &&
10409 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
10410 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
10411 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
10412 		     intbss->parent_bssid)))
10413 		goto nla_put_failure;
10414 
10415 	if (intbss->ts_boottime &&
10416 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
10417 			      intbss->ts_boottime, NL80211_BSS_PAD))
10418 		goto nla_put_failure;
10419 
10420 	if (!nl80211_put_signal(msg, intbss->pub.chains,
10421 				intbss->pub.chain_signal,
10422 				NL80211_BSS_CHAIN_SIGNAL))
10423 		goto nla_put_failure;
10424 
10425 	switch (rdev->wiphy.signal_type) {
10426 	case CFG80211_SIGNAL_TYPE_MBM:
10427 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
10428 			goto nla_put_failure;
10429 		break;
10430 	case CFG80211_SIGNAL_TYPE_UNSPEC:
10431 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
10432 			goto nla_put_failure;
10433 		break;
10434 	default:
10435 		break;
10436 	}
10437 
10438 	switch (wdev->iftype) {
10439 	case NL80211_IFTYPE_P2P_CLIENT:
10440 	case NL80211_IFTYPE_STATION:
10441 		for_each_valid_link(wdev, link_id) {
10442 			if (intbss == wdev->links[link_id].client.current_bss &&
10443 			    (nla_put_u32(msg, NL80211_BSS_STATUS,
10444 					 NL80211_BSS_STATUS_ASSOCIATED) ||
10445 			     (wdev->valid_links &&
10446 			      (nla_put_u8(msg, NL80211_BSS_MLO_LINK_ID,
10447 					  link_id) ||
10448 			       nla_put(msg, NL80211_BSS_MLD_ADDR, ETH_ALEN,
10449 				       wdev->u.client.connected_addr)))))
10450 				goto nla_put_failure;
10451 		}
10452 		break;
10453 	case NL80211_IFTYPE_ADHOC:
10454 		if (intbss == wdev->u.ibss.current_bss &&
10455 		    nla_put_u32(msg, NL80211_BSS_STATUS,
10456 				NL80211_BSS_STATUS_IBSS_JOINED))
10457 			goto nla_put_failure;
10458 		break;
10459 	default:
10460 		break;
10461 	}
10462 
10463 	nla_nest_end(msg, bss);
10464 
10465 	genlmsg_end(msg, hdr);
10466 	return 0;
10467 
10468  fail_unlock_rcu:
10469 	rcu_read_unlock();
10470  nla_put_failure:
10471 	genlmsg_cancel(msg, hdr);
10472 	return -EMSGSIZE;
10473 }
10474 
10475 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
10476 {
10477 	struct cfg80211_registered_device *rdev;
10478 	struct cfg80211_internal_bss *scan;
10479 	struct wireless_dev *wdev;
10480 	int start = cb->args[2], idx = 0;
10481 	int err;
10482 
10483 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
10484 	if (err)
10485 		return err;
10486 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10487 	__acquire(&rdev->wiphy.mtx);
10488 
10489 	wdev_lock(wdev);
10490 	spin_lock_bh(&rdev->bss_lock);
10491 
10492 	/*
10493 	 * dump_scan will be called multiple times to break up the scan results
10494 	 * into multiple messages.  It is unlikely that any more bss-es will be
10495 	 * expired after the first call, so only call only call this on the
10496 	 * first dump_scan invocation.
10497 	 */
10498 	if (start == 0)
10499 		cfg80211_bss_expire(rdev);
10500 
10501 	cb->seq = rdev->bss_generation;
10502 
10503 	list_for_each_entry(scan, &rdev->bss_list, list) {
10504 		if (++idx <= start)
10505 			continue;
10506 		if (nl80211_send_bss(skb, cb,
10507 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10508 				rdev, wdev, scan) < 0) {
10509 			idx--;
10510 			break;
10511 		}
10512 	}
10513 
10514 	spin_unlock_bh(&rdev->bss_lock);
10515 	wdev_unlock(wdev);
10516 
10517 	cb->args[2] = idx;
10518 	wiphy_unlock(&rdev->wiphy);
10519 
10520 	return skb->len;
10521 }
10522 
10523 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
10524 			       int flags, struct net_device *dev,
10525 			       bool allow_radio_stats,
10526 			       struct survey_info *survey)
10527 {
10528 	void *hdr;
10529 	struct nlattr *infoattr;
10530 
10531 	/* skip radio stats if userspace didn't request them */
10532 	if (!survey->channel && !allow_radio_stats)
10533 		return 0;
10534 
10535 	hdr = nl80211hdr_put(msg, portid, seq, flags,
10536 			     NL80211_CMD_NEW_SURVEY_RESULTS);
10537 	if (!hdr)
10538 		return -ENOMEM;
10539 
10540 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
10541 		goto nla_put_failure;
10542 
10543 	infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
10544 	if (!infoattr)
10545 		goto nla_put_failure;
10546 
10547 	if (survey->channel &&
10548 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
10549 			survey->channel->center_freq))
10550 		goto nla_put_failure;
10551 
10552 	if (survey->channel && survey->channel->freq_offset &&
10553 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY_OFFSET,
10554 			survey->channel->freq_offset))
10555 		goto nla_put_failure;
10556 
10557 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
10558 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
10559 		goto nla_put_failure;
10560 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
10561 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
10562 		goto nla_put_failure;
10563 	if ((survey->filled & SURVEY_INFO_TIME) &&
10564 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
10565 			survey->time, NL80211_SURVEY_INFO_PAD))
10566 		goto nla_put_failure;
10567 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
10568 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
10569 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
10570 		goto nla_put_failure;
10571 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
10572 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
10573 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
10574 		goto nla_put_failure;
10575 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
10576 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
10577 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
10578 		goto nla_put_failure;
10579 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
10580 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
10581 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
10582 		goto nla_put_failure;
10583 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
10584 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
10585 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
10586 		goto nla_put_failure;
10587 	if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
10588 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
10589 			      survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
10590 		goto nla_put_failure;
10591 
10592 	nla_nest_end(msg, infoattr);
10593 
10594 	genlmsg_end(msg, hdr);
10595 	return 0;
10596 
10597  nla_put_failure:
10598 	genlmsg_cancel(msg, hdr);
10599 	return -EMSGSIZE;
10600 }
10601 
10602 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
10603 {
10604 	struct nlattr **attrbuf;
10605 	struct survey_info survey;
10606 	struct cfg80211_registered_device *rdev;
10607 	struct wireless_dev *wdev;
10608 	int survey_idx = cb->args[2];
10609 	int res;
10610 	bool radio_stats;
10611 
10612 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
10613 	if (!attrbuf)
10614 		return -ENOMEM;
10615 
10616 	res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, attrbuf);
10617 	if (res) {
10618 		kfree(attrbuf);
10619 		return res;
10620 	}
10621 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10622 	__acquire(&rdev->wiphy.mtx);
10623 
10624 	/* prepare_wdev_dump parsed the attributes */
10625 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
10626 
10627 	if (!wdev->netdev) {
10628 		res = -EINVAL;
10629 		goto out_err;
10630 	}
10631 
10632 	if (!rdev->ops->dump_survey) {
10633 		res = -EOPNOTSUPP;
10634 		goto out_err;
10635 	}
10636 
10637 	while (1) {
10638 		wdev_lock(wdev);
10639 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
10640 		wdev_unlock(wdev);
10641 		if (res == -ENOENT)
10642 			break;
10643 		if (res)
10644 			goto out_err;
10645 
10646 		/* don't send disabled channels, but do send non-channel data */
10647 		if (survey.channel &&
10648 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
10649 			survey_idx++;
10650 			continue;
10651 		}
10652 
10653 		if (nl80211_send_survey(skb,
10654 				NETLINK_CB(cb->skb).portid,
10655 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10656 				wdev->netdev, radio_stats, &survey) < 0)
10657 			goto out;
10658 		survey_idx++;
10659 	}
10660 
10661  out:
10662 	cb->args[2] = survey_idx;
10663 	res = skb->len;
10664  out_err:
10665 	kfree(attrbuf);
10666 	wiphy_unlock(&rdev->wiphy);
10667 	return res;
10668 }
10669 
10670 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
10671 {
10672 	return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
10673 				  NL80211_WPA_VERSION_2 |
10674 				  NL80211_WPA_VERSION_3));
10675 }
10676 
10677 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
10678 {
10679 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10680 	struct net_device *dev = info->user_ptr[1];
10681 	struct ieee80211_channel *chan;
10682 	const u8 *bssid, *ssid;
10683 	int err, ssid_len;
10684 	enum nl80211_auth_type auth_type;
10685 	struct key_parse key;
10686 	bool local_state_change;
10687 	struct cfg80211_auth_request req = {};
10688 	u32 freq;
10689 
10690 	if (!info->attrs[NL80211_ATTR_MAC])
10691 		return -EINVAL;
10692 
10693 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
10694 		return -EINVAL;
10695 
10696 	if (!info->attrs[NL80211_ATTR_SSID])
10697 		return -EINVAL;
10698 
10699 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10700 		return -EINVAL;
10701 
10702 	err = nl80211_parse_key(info, &key);
10703 	if (err)
10704 		return err;
10705 
10706 	if (key.idx >= 0) {
10707 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
10708 			return -EINVAL;
10709 		if (!key.p.key || !key.p.key_len)
10710 			return -EINVAL;
10711 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
10712 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
10713 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
10714 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
10715 			return -EINVAL;
10716 		if (key.idx > 3)
10717 			return -EINVAL;
10718 	} else {
10719 		key.p.key_len = 0;
10720 		key.p.key = NULL;
10721 	}
10722 
10723 	if (key.idx >= 0) {
10724 		int i;
10725 		bool ok = false;
10726 
10727 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
10728 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
10729 				ok = true;
10730 				break;
10731 			}
10732 		}
10733 		if (!ok)
10734 			return -EINVAL;
10735 	}
10736 
10737 	if (!rdev->ops->auth)
10738 		return -EOPNOTSUPP;
10739 
10740 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10741 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10742 		return -EOPNOTSUPP;
10743 
10744 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10745 	freq = MHZ_TO_KHZ(nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
10746 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10747 		freq +=
10748 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10749 
10750 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10751 	if (!chan)
10752 		return -EINVAL;
10753 
10754 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10755 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10756 
10757 	if (info->attrs[NL80211_ATTR_IE]) {
10758 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10759 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10760 	}
10761 
10762 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10763 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
10764 		return -EINVAL;
10765 
10766 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
10767 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
10768 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
10769 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
10770 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
10771 		return -EINVAL;
10772 
10773 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
10774 		if (auth_type != NL80211_AUTHTYPE_SAE &&
10775 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
10776 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
10777 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
10778 			return -EINVAL;
10779 		req.auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
10780 		req.auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
10781 	}
10782 
10783 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
10784 
10785 	/*
10786 	 * Since we no longer track auth state, ignore
10787 	 * requests to only change local state.
10788 	 */
10789 	if (local_state_change)
10790 		return 0;
10791 
10792 	req.auth_type = auth_type;
10793 	req.key = key.p.key;
10794 	req.key_len = key.p.key_len;
10795 	req.key_idx = key.idx;
10796 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
10797 	if (req.link_id >= 0) {
10798 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
10799 			return -EINVAL;
10800 		if (!info->attrs[NL80211_ATTR_MLD_ADDR])
10801 			return -EINVAL;
10802 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
10803 		if (!is_valid_ether_addr(req.ap_mld_addr))
10804 			return -EINVAL;
10805 	}
10806 
10807 	req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
10808 				   IEEE80211_BSS_TYPE_ESS,
10809 				   IEEE80211_PRIVACY_ANY);
10810 	if (!req.bss)
10811 		return -ENOENT;
10812 
10813 	wdev_lock(dev->ieee80211_ptr);
10814 	err = cfg80211_mlme_auth(rdev, dev, &req);
10815 	wdev_unlock(dev->ieee80211_ptr);
10816 
10817 	cfg80211_put_bss(&rdev->wiphy, req.bss);
10818 
10819 	return err;
10820 }
10821 
10822 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
10823 				     struct genl_info *info)
10824 {
10825 	if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10826 		GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
10827 		return -EINVAL;
10828 	}
10829 
10830 	if (!rdev->ops->tx_control_port ||
10831 	    !wiphy_ext_feature_isset(&rdev->wiphy,
10832 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
10833 		return -EOPNOTSUPP;
10834 
10835 	return 0;
10836 }
10837 
10838 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
10839 				   struct genl_info *info,
10840 				   struct cfg80211_crypto_settings *settings,
10841 				   int cipher_limit)
10842 {
10843 	memset(settings, 0, sizeof(*settings));
10844 
10845 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
10846 
10847 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
10848 		u16 proto;
10849 
10850 		proto = nla_get_u16(
10851 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
10852 		settings->control_port_ethertype = cpu_to_be16(proto);
10853 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
10854 		    proto != ETH_P_PAE)
10855 			return -EINVAL;
10856 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
10857 			settings->control_port_no_encrypt = true;
10858 	} else
10859 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
10860 
10861 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
10862 		int r = validate_pae_over_nl80211(rdev, info);
10863 
10864 		if (r < 0)
10865 			return r;
10866 
10867 		settings->control_port_over_nl80211 = true;
10868 
10869 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH])
10870 			settings->control_port_no_preauth = true;
10871 	}
10872 
10873 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
10874 		void *data;
10875 		int len, i;
10876 
10877 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10878 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10879 		settings->n_ciphers_pairwise = len / sizeof(u32);
10880 
10881 		if (len % sizeof(u32))
10882 			return -EINVAL;
10883 
10884 		if (settings->n_ciphers_pairwise > cipher_limit)
10885 			return -EINVAL;
10886 
10887 		memcpy(settings->ciphers_pairwise, data, len);
10888 
10889 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
10890 			if (!cfg80211_supported_cipher_suite(
10891 					&rdev->wiphy,
10892 					settings->ciphers_pairwise[i]))
10893 				return -EINVAL;
10894 	}
10895 
10896 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
10897 		settings->cipher_group =
10898 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
10899 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
10900 						     settings->cipher_group))
10901 			return -EINVAL;
10902 	}
10903 
10904 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
10905 		settings->wpa_versions =
10906 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
10907 		if (!nl80211_valid_wpa_versions(settings->wpa_versions))
10908 			return -EINVAL;
10909 	}
10910 
10911 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
10912 		void *data;
10913 		int len;
10914 
10915 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
10916 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
10917 		settings->n_akm_suites = len / sizeof(u32);
10918 
10919 		if (len % sizeof(u32))
10920 			return -EINVAL;
10921 
10922 		if (settings->n_akm_suites > rdev->wiphy.max_num_akm_suites)
10923 			return -EINVAL;
10924 
10925 		memcpy(settings->akm_suites, data, len);
10926 	}
10927 
10928 	if (info->attrs[NL80211_ATTR_PMK]) {
10929 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
10930 			return -EINVAL;
10931 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10932 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK) &&
10933 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10934 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_AP_PSK))
10935 			return -EINVAL;
10936 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10937 	}
10938 
10939 	if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
10940 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10941 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
10942 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10943 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP))
10944 			return -EINVAL;
10945 		settings->sae_pwd =
10946 			nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10947 		settings->sae_pwd_len =
10948 			nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10949 	}
10950 
10951 	if (info->attrs[NL80211_ATTR_SAE_PWE])
10952 		settings->sae_pwe =
10953 			nla_get_u8(info->attrs[NL80211_ATTR_SAE_PWE]);
10954 	else
10955 		settings->sae_pwe = NL80211_SAE_PWE_UNSPECIFIED;
10956 
10957 	return 0;
10958 }
10959 
10960 static struct cfg80211_bss *nl80211_assoc_bss(struct cfg80211_registered_device *rdev,
10961 					      const u8 *ssid, int ssid_len,
10962 					      struct nlattr **attrs)
10963 {
10964 	struct ieee80211_channel *chan;
10965 	struct cfg80211_bss *bss;
10966 	const u8 *bssid;
10967 	u32 freq;
10968 
10969 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_WIPHY_FREQ])
10970 		return ERR_PTR(-EINVAL);
10971 
10972 	bssid = nla_data(attrs[NL80211_ATTR_MAC]);
10973 
10974 	freq = MHZ_TO_KHZ(nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ]));
10975 	if (attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10976 		freq += nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10977 
10978 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10979 	if (!chan)
10980 		return ERR_PTR(-EINVAL);
10981 
10982 	bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid,
10983 			       ssid, ssid_len,
10984 			       IEEE80211_BSS_TYPE_ESS,
10985 			       IEEE80211_PRIVACY_ANY);
10986 	if (!bss)
10987 		return ERR_PTR(-ENOENT);
10988 
10989 	return bss;
10990 }
10991 
10992 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
10993 {
10994 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10995 	struct net_device *dev = info->user_ptr[1];
10996 	struct cfg80211_assoc_request req = {};
10997 	struct nlattr **attrs = NULL;
10998 	const u8 *ap_addr, *ssid;
10999 	unsigned int link_id;
11000 	int err, ssid_len;
11001 
11002 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11003 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11004 		return -EPERM;
11005 
11006 	if (!info->attrs[NL80211_ATTR_SSID])
11007 		return -EINVAL;
11008 
11009 	if (!rdev->ops->assoc)
11010 		return -EOPNOTSUPP;
11011 
11012 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11013 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11014 		return -EOPNOTSUPP;
11015 
11016 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11017 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11018 
11019 	if (info->attrs[NL80211_ATTR_IE]) {
11020 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11021 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11022 
11023 		if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
11024 					   req.ie, req.ie_len)) {
11025 			GENL_SET_ERR_MSG(info,
11026 					 "non-inheritance makes no sense");
11027 			return -EINVAL;
11028 		}
11029 	}
11030 
11031 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
11032 		enum nl80211_mfp mfp =
11033 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
11034 		if (mfp == NL80211_MFP_REQUIRED)
11035 			req.use_mfp = true;
11036 		else if (mfp != NL80211_MFP_NO)
11037 			return -EINVAL;
11038 	}
11039 
11040 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
11041 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
11042 
11043 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
11044 		req.flags |= ASSOC_REQ_DISABLE_HT;
11045 
11046 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11047 		memcpy(&req.ht_capa_mask,
11048 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11049 		       sizeof(req.ht_capa_mask));
11050 
11051 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11052 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11053 			return -EINVAL;
11054 		memcpy(&req.ht_capa,
11055 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11056 		       sizeof(req.ht_capa));
11057 	}
11058 
11059 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
11060 		req.flags |= ASSOC_REQ_DISABLE_VHT;
11061 
11062 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
11063 		req.flags |= ASSOC_REQ_DISABLE_HE;
11064 
11065 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
11066 		req.flags |= ASSOC_REQ_DISABLE_EHT;
11067 
11068 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11069 		memcpy(&req.vht_capa_mask,
11070 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
11071 		       sizeof(req.vht_capa_mask));
11072 
11073 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
11074 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11075 			return -EINVAL;
11076 		memcpy(&req.vht_capa,
11077 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
11078 		       sizeof(req.vht_capa));
11079 	}
11080 
11081 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
11082 		if (!((rdev->wiphy.features &
11083 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
11084 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
11085 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11086 					     NL80211_EXT_FEATURE_RRM))
11087 			return -EINVAL;
11088 		req.flags |= ASSOC_REQ_USE_RRM;
11089 	}
11090 
11091 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
11092 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
11093 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
11094 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
11095 			return -EINVAL;
11096 		req.fils_nonces =
11097 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
11098 	}
11099 
11100 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]) {
11101 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY])
11102 			return -EINVAL;
11103 		memcpy(&req.s1g_capa_mask,
11104 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]),
11105 		       sizeof(req.s1g_capa_mask));
11106 	}
11107 
11108 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY]) {
11109 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK])
11110 			return -EINVAL;
11111 		memcpy(&req.s1g_capa,
11112 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY]),
11113 		       sizeof(req.s1g_capa));
11114 	}
11115 
11116 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
11117 
11118 	if (info->attrs[NL80211_ATTR_MLO_LINKS]) {
11119 		unsigned int attrsize = NUM_NL80211_ATTR * sizeof(*attrs);
11120 		struct nlattr *link;
11121 		int rem = 0;
11122 
11123 		if (req.link_id < 0)
11124 			return -EINVAL;
11125 
11126 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
11127 			return -EINVAL;
11128 
11129 		if (info->attrs[NL80211_ATTR_MAC] ||
11130 		    info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
11131 		    !info->attrs[NL80211_ATTR_MLD_ADDR])
11132 			return -EINVAL;
11133 
11134 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
11135 		ap_addr = req.ap_mld_addr;
11136 
11137 		attrs = kzalloc(attrsize, GFP_KERNEL);
11138 		if (!attrs)
11139 			return -ENOMEM;
11140 
11141 		nla_for_each_nested(link,
11142 				    info->attrs[NL80211_ATTR_MLO_LINKS],
11143 				    rem) {
11144 			memset(attrs, 0, attrsize);
11145 
11146 			nla_parse_nested(attrs, NL80211_ATTR_MAX,
11147 					 link, NULL, NULL);
11148 
11149 			if (!attrs[NL80211_ATTR_MLO_LINK_ID]) {
11150 				err = -EINVAL;
11151 				goto free;
11152 			}
11153 
11154 			link_id = nla_get_u8(attrs[NL80211_ATTR_MLO_LINK_ID]);
11155 			/* cannot use the same link ID again */
11156 			if (req.links[link_id].bss) {
11157 				err = -EINVAL;
11158 				goto free;
11159 			}
11160 			req.links[link_id].bss =
11161 				nl80211_assoc_bss(rdev, ssid, ssid_len, attrs);
11162 			if (IS_ERR(req.links[link_id].bss)) {
11163 				err = PTR_ERR(req.links[link_id].bss);
11164 				req.links[link_id].bss = NULL;
11165 				goto free;
11166 			}
11167 
11168 			if (attrs[NL80211_ATTR_IE]) {
11169 				req.links[link_id].elems =
11170 					nla_data(attrs[NL80211_ATTR_IE]);
11171 				req.links[link_id].elems_len =
11172 					nla_len(attrs[NL80211_ATTR_IE]);
11173 
11174 				if (cfg80211_find_elem(WLAN_EID_FRAGMENT,
11175 						       req.links[link_id].elems,
11176 						       req.links[link_id].elems_len)) {
11177 					GENL_SET_ERR_MSG(info,
11178 							 "cannot deal with fragmentation");
11179 					err = -EINVAL;
11180 					goto free;
11181 				}
11182 
11183 				if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
11184 							   req.links[link_id].elems,
11185 							   req.links[link_id].elems_len)) {
11186 					GENL_SET_ERR_MSG(info,
11187 							 "cannot deal with non-inheritance");
11188 					err = -EINVAL;
11189 					goto free;
11190 				}
11191 			}
11192 
11193 			req.links[link_id].disabled =
11194 				nla_get_flag(attrs[NL80211_ATTR_MLO_LINK_DISABLED]);
11195 		}
11196 
11197 		if (!req.links[req.link_id].bss) {
11198 			err = -EINVAL;
11199 			goto free;
11200 		}
11201 
11202 		if (req.links[req.link_id].elems_len) {
11203 			GENL_SET_ERR_MSG(info,
11204 					 "cannot have per-link elems on assoc link");
11205 			err = -EINVAL;
11206 			goto free;
11207 		}
11208 
11209 		if (req.links[req.link_id].disabled) {
11210 			GENL_SET_ERR_MSG(info,
11211 					 "cannot have assoc link disabled");
11212 			err = -EINVAL;
11213 			goto free;
11214 		}
11215 
11216 		kfree(attrs);
11217 		attrs = NULL;
11218 	} else {
11219 		if (req.link_id >= 0)
11220 			return -EINVAL;
11221 
11222 		req.bss = nl80211_assoc_bss(rdev, ssid, ssid_len, info->attrs);
11223 		if (IS_ERR(req.bss))
11224 			return PTR_ERR(req.bss);
11225 		ap_addr = req.bss->bssid;
11226 	}
11227 
11228 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
11229 	if (!err) {
11230 		wdev_lock(dev->ieee80211_ptr);
11231 
11232 		err = cfg80211_mlme_assoc(rdev, dev, &req);
11233 
11234 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
11235 			dev->ieee80211_ptr->conn_owner_nlportid =
11236 				info->snd_portid;
11237 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
11238 			       ap_addr, ETH_ALEN);
11239 		}
11240 
11241 		wdev_unlock(dev->ieee80211_ptr);
11242 	}
11243 
11244 free:
11245 	for (link_id = 0; link_id < ARRAY_SIZE(req.links); link_id++)
11246 		cfg80211_put_bss(&rdev->wiphy, req.links[link_id].bss);
11247 	cfg80211_put_bss(&rdev->wiphy, req.bss);
11248 	kfree(attrs);
11249 
11250 	return err;
11251 }
11252 
11253 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
11254 {
11255 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11256 	struct net_device *dev = info->user_ptr[1];
11257 	const u8 *ie = NULL, *bssid;
11258 	int ie_len = 0, err;
11259 	u16 reason_code;
11260 	bool local_state_change;
11261 
11262 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11263 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11264 		return -EPERM;
11265 
11266 	if (!info->attrs[NL80211_ATTR_MAC])
11267 		return -EINVAL;
11268 
11269 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11270 		return -EINVAL;
11271 
11272 	if (!rdev->ops->deauth)
11273 		return -EOPNOTSUPP;
11274 
11275 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11276 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11277 		return -EOPNOTSUPP;
11278 
11279 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11280 
11281 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11282 	if (reason_code == 0) {
11283 		/* Reason Code 0 is reserved */
11284 		return -EINVAL;
11285 	}
11286 
11287 	if (info->attrs[NL80211_ATTR_IE]) {
11288 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11289 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11290 	}
11291 
11292 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11293 
11294 	wdev_lock(dev->ieee80211_ptr);
11295 	err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
11296 				   local_state_change);
11297 	wdev_unlock(dev->ieee80211_ptr);
11298 	return err;
11299 }
11300 
11301 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
11302 {
11303 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11304 	struct net_device *dev = info->user_ptr[1];
11305 	const u8 *ie = NULL, *bssid;
11306 	int ie_len = 0, err;
11307 	u16 reason_code;
11308 	bool local_state_change;
11309 
11310 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11311 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11312 		return -EPERM;
11313 
11314 	if (!info->attrs[NL80211_ATTR_MAC])
11315 		return -EINVAL;
11316 
11317 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11318 		return -EINVAL;
11319 
11320 	if (!rdev->ops->disassoc)
11321 		return -EOPNOTSUPP;
11322 
11323 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11324 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11325 		return -EOPNOTSUPP;
11326 
11327 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11328 
11329 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11330 	if (reason_code == 0) {
11331 		/* Reason Code 0 is reserved */
11332 		return -EINVAL;
11333 	}
11334 
11335 	if (info->attrs[NL80211_ATTR_IE]) {
11336 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11337 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11338 	}
11339 
11340 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11341 
11342 	wdev_lock(dev->ieee80211_ptr);
11343 	err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
11344 				     local_state_change);
11345 	wdev_unlock(dev->ieee80211_ptr);
11346 	return err;
11347 }
11348 
11349 static bool
11350 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
11351 			 int mcast_rate[NUM_NL80211_BANDS],
11352 			 int rateval)
11353 {
11354 	struct wiphy *wiphy = &rdev->wiphy;
11355 	bool found = false;
11356 	int band, i;
11357 
11358 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
11359 		struct ieee80211_supported_band *sband;
11360 
11361 		sband = wiphy->bands[band];
11362 		if (!sband)
11363 			continue;
11364 
11365 		for (i = 0; i < sband->n_bitrates; i++) {
11366 			if (sband->bitrates[i].bitrate == rateval) {
11367 				mcast_rate[band] = i + 1;
11368 				found = true;
11369 				break;
11370 			}
11371 		}
11372 	}
11373 
11374 	return found;
11375 }
11376 
11377 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
11378 {
11379 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11380 	struct net_device *dev = info->user_ptr[1];
11381 	struct cfg80211_ibss_params ibss;
11382 	struct wiphy *wiphy;
11383 	struct cfg80211_cached_keys *connkeys = NULL;
11384 	int err;
11385 
11386 	memset(&ibss, 0, sizeof(ibss));
11387 
11388 	if (!info->attrs[NL80211_ATTR_SSID] ||
11389 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11390 		return -EINVAL;
11391 
11392 	ibss.beacon_interval = 100;
11393 
11394 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
11395 		ibss.beacon_interval =
11396 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11397 
11398 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
11399 					   ibss.beacon_interval);
11400 	if (err)
11401 		return err;
11402 
11403 	if (!rdev->ops->join_ibss)
11404 		return -EOPNOTSUPP;
11405 
11406 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11407 		return -EOPNOTSUPP;
11408 
11409 	wiphy = &rdev->wiphy;
11410 
11411 	if (info->attrs[NL80211_ATTR_MAC]) {
11412 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11413 
11414 		if (!is_valid_ether_addr(ibss.bssid))
11415 			return -EINVAL;
11416 	}
11417 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11418 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11419 
11420 	if (info->attrs[NL80211_ATTR_IE]) {
11421 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11422 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11423 	}
11424 
11425 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
11426 	if (err)
11427 		return err;
11428 
11429 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
11430 				     NL80211_IFTYPE_ADHOC))
11431 		return -EINVAL;
11432 
11433 	switch (ibss.chandef.width) {
11434 	case NL80211_CHAN_WIDTH_5:
11435 	case NL80211_CHAN_WIDTH_10:
11436 	case NL80211_CHAN_WIDTH_20_NOHT:
11437 		break;
11438 	case NL80211_CHAN_WIDTH_20:
11439 	case NL80211_CHAN_WIDTH_40:
11440 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11441 			return -EINVAL;
11442 		break;
11443 	case NL80211_CHAN_WIDTH_80:
11444 	case NL80211_CHAN_WIDTH_80P80:
11445 	case NL80211_CHAN_WIDTH_160:
11446 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11447 			return -EINVAL;
11448 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
11449 					     NL80211_EXT_FEATURE_VHT_IBSS))
11450 			return -EINVAL;
11451 		break;
11452 	case NL80211_CHAN_WIDTH_320:
11453 		return -EINVAL;
11454 	default:
11455 		return -EINVAL;
11456 	}
11457 
11458 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
11459 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
11460 
11461 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11462 		u8 *rates =
11463 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11464 		int n_rates =
11465 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11466 		struct ieee80211_supported_band *sband =
11467 			wiphy->bands[ibss.chandef.chan->band];
11468 
11469 		err = ieee80211_get_ratemask(sband, rates, n_rates,
11470 					     &ibss.basic_rates);
11471 		if (err)
11472 			return err;
11473 	}
11474 
11475 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11476 		memcpy(&ibss.ht_capa_mask,
11477 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11478 		       sizeof(ibss.ht_capa_mask));
11479 
11480 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11481 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11482 			return -EINVAL;
11483 		memcpy(&ibss.ht_capa,
11484 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11485 		       sizeof(ibss.ht_capa));
11486 	}
11487 
11488 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11489 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
11490 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11491 		return -EINVAL;
11492 
11493 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11494 		bool no_ht = false;
11495 
11496 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
11497 		if (IS_ERR(connkeys))
11498 			return PTR_ERR(connkeys);
11499 
11500 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
11501 		    no_ht) {
11502 			kfree_sensitive(connkeys);
11503 			return -EINVAL;
11504 		}
11505 	}
11506 
11507 	ibss.control_port =
11508 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
11509 
11510 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11511 		int r = validate_pae_over_nl80211(rdev, info);
11512 
11513 		if (r < 0) {
11514 			kfree_sensitive(connkeys);
11515 			return r;
11516 		}
11517 
11518 		ibss.control_port_over_nl80211 = true;
11519 	}
11520 
11521 	ibss.userspace_handles_dfs =
11522 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11523 
11524 	wdev_lock(dev->ieee80211_ptr);
11525 	err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
11526 	if (err)
11527 		kfree_sensitive(connkeys);
11528 	else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
11529 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11530 	wdev_unlock(dev->ieee80211_ptr);
11531 
11532 	return err;
11533 }
11534 
11535 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
11536 {
11537 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11538 	struct net_device *dev = info->user_ptr[1];
11539 
11540 	if (!rdev->ops->leave_ibss)
11541 		return -EOPNOTSUPP;
11542 
11543 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11544 		return -EOPNOTSUPP;
11545 
11546 	return cfg80211_leave_ibss(rdev, dev, false);
11547 }
11548 
11549 static int nl80211_set_mcast_rate(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 	int mcast_rate[NUM_NL80211_BANDS];
11554 	u32 nla_rate;
11555 
11556 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
11557 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
11558 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
11559 		return -EOPNOTSUPP;
11560 
11561 	if (!rdev->ops->set_mcast_rate)
11562 		return -EOPNOTSUPP;
11563 
11564 	memset(mcast_rate, 0, sizeof(mcast_rate));
11565 
11566 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
11567 		return -EINVAL;
11568 
11569 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
11570 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
11571 		return -EINVAL;
11572 
11573 	return rdev_set_mcast_rate(rdev, dev, mcast_rate);
11574 }
11575 
11576 static struct sk_buff *
11577 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
11578 			    struct wireless_dev *wdev, int approxlen,
11579 			    u32 portid, u32 seq, enum nl80211_commands cmd,
11580 			    enum nl80211_attrs attr,
11581 			    const struct nl80211_vendor_cmd_info *info,
11582 			    gfp_t gfp)
11583 {
11584 	struct sk_buff *skb;
11585 	void *hdr;
11586 	struct nlattr *data;
11587 
11588 	skb = nlmsg_new(approxlen + 100, gfp);
11589 	if (!skb)
11590 		return NULL;
11591 
11592 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
11593 	if (!hdr) {
11594 		kfree_skb(skb);
11595 		return NULL;
11596 	}
11597 
11598 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11599 		goto nla_put_failure;
11600 
11601 	if (info) {
11602 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
11603 				info->vendor_id))
11604 			goto nla_put_failure;
11605 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
11606 				info->subcmd))
11607 			goto nla_put_failure;
11608 	}
11609 
11610 	if (wdev) {
11611 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
11612 				      wdev_id(wdev), NL80211_ATTR_PAD))
11613 			goto nla_put_failure;
11614 		if (wdev->netdev &&
11615 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
11616 				wdev->netdev->ifindex))
11617 			goto nla_put_failure;
11618 	}
11619 
11620 	data = nla_nest_start_noflag(skb, attr);
11621 	if (!data)
11622 		goto nla_put_failure;
11623 
11624 	((void **)skb->cb)[0] = rdev;
11625 	((void **)skb->cb)[1] = hdr;
11626 	((void **)skb->cb)[2] = data;
11627 
11628 	return skb;
11629 
11630  nla_put_failure:
11631 	kfree_skb(skb);
11632 	return NULL;
11633 }
11634 
11635 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
11636 					   struct wireless_dev *wdev,
11637 					   enum nl80211_commands cmd,
11638 					   enum nl80211_attrs attr,
11639 					   unsigned int portid,
11640 					   int vendor_event_idx,
11641 					   int approxlen, gfp_t gfp)
11642 {
11643 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11644 	const struct nl80211_vendor_cmd_info *info;
11645 
11646 	switch (cmd) {
11647 	case NL80211_CMD_TESTMODE:
11648 		if (WARN_ON(vendor_event_idx != -1))
11649 			return NULL;
11650 		info = NULL;
11651 		break;
11652 	case NL80211_CMD_VENDOR:
11653 		if (WARN_ON(vendor_event_idx < 0 ||
11654 			    vendor_event_idx >= wiphy->n_vendor_events))
11655 			return NULL;
11656 		info = &wiphy->vendor_events[vendor_event_idx];
11657 		break;
11658 	default:
11659 		WARN_ON(1);
11660 		return NULL;
11661 	}
11662 
11663 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
11664 					   cmd, attr, info, gfp);
11665 }
11666 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
11667 
11668 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
11669 {
11670 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
11671 	void *hdr = ((void **)skb->cb)[1];
11672 	struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
11673 	struct nlattr *data = ((void **)skb->cb)[2];
11674 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
11675 
11676 	/* clear CB data for netlink core to own from now on */
11677 	memset(skb->cb, 0, sizeof(skb->cb));
11678 
11679 	nla_nest_end(skb, data);
11680 	genlmsg_end(skb, hdr);
11681 
11682 	if (nlhdr->nlmsg_pid) {
11683 		genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
11684 				nlhdr->nlmsg_pid);
11685 	} else {
11686 		if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
11687 			mcgrp = NL80211_MCGRP_VENDOR;
11688 
11689 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11690 					skb, 0, mcgrp, gfp);
11691 	}
11692 }
11693 EXPORT_SYMBOL(__cfg80211_send_event_skb);
11694 
11695 #ifdef CONFIG_NL80211_TESTMODE
11696 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
11697 {
11698 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11699 	struct wireless_dev *wdev;
11700 	int err;
11701 
11702 	lockdep_assert_held(&rdev->wiphy.mtx);
11703 
11704 	wdev = __cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
11705 					  info->attrs);
11706 
11707 	if (!rdev->ops->testmode_cmd)
11708 		return -EOPNOTSUPP;
11709 
11710 	if (IS_ERR(wdev)) {
11711 		err = PTR_ERR(wdev);
11712 		if (err != -EINVAL)
11713 			return err;
11714 		wdev = NULL;
11715 	} else if (wdev->wiphy != &rdev->wiphy) {
11716 		return -EINVAL;
11717 	}
11718 
11719 	if (!info->attrs[NL80211_ATTR_TESTDATA])
11720 		return -EINVAL;
11721 
11722 	rdev->cur_cmd_info = info;
11723 	err = rdev_testmode_cmd(rdev, wdev,
11724 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
11725 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
11726 	rdev->cur_cmd_info = NULL;
11727 
11728 	return err;
11729 }
11730 
11731 static int nl80211_testmode_dump(struct sk_buff *skb,
11732 				 struct netlink_callback *cb)
11733 {
11734 	struct cfg80211_registered_device *rdev;
11735 	struct nlattr **attrbuf = NULL;
11736 	int err;
11737 	long phy_idx;
11738 	void *data = NULL;
11739 	int data_len = 0;
11740 
11741 	rtnl_lock();
11742 
11743 	if (cb->args[0]) {
11744 		/*
11745 		 * 0 is a valid index, but not valid for args[0],
11746 		 * so we need to offset by 1.
11747 		 */
11748 		phy_idx = cb->args[0] - 1;
11749 
11750 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
11751 		if (!rdev) {
11752 			err = -ENOENT;
11753 			goto out_err;
11754 		}
11755 	} else {
11756 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
11757 				  GFP_KERNEL);
11758 		if (!attrbuf) {
11759 			err = -ENOMEM;
11760 			goto out_err;
11761 		}
11762 
11763 		err = nlmsg_parse_deprecated(cb->nlh,
11764 					     GENL_HDRLEN + nl80211_fam.hdrsize,
11765 					     attrbuf, nl80211_fam.maxattr,
11766 					     nl80211_policy, NULL);
11767 		if (err)
11768 			goto out_err;
11769 
11770 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
11771 		if (IS_ERR(rdev)) {
11772 			err = PTR_ERR(rdev);
11773 			goto out_err;
11774 		}
11775 		phy_idx = rdev->wiphy_idx;
11776 
11777 		if (attrbuf[NL80211_ATTR_TESTDATA])
11778 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
11779 	}
11780 
11781 	if (cb->args[1]) {
11782 		data = nla_data((void *)cb->args[1]);
11783 		data_len = nla_len((void *)cb->args[1]);
11784 	}
11785 
11786 	if (!rdev->ops->testmode_dump) {
11787 		err = -EOPNOTSUPP;
11788 		goto out_err;
11789 	}
11790 
11791 	while (1) {
11792 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
11793 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
11794 					   NL80211_CMD_TESTMODE);
11795 		struct nlattr *tmdata;
11796 
11797 		if (!hdr)
11798 			break;
11799 
11800 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
11801 			genlmsg_cancel(skb, hdr);
11802 			break;
11803 		}
11804 
11805 		tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
11806 		if (!tmdata) {
11807 			genlmsg_cancel(skb, hdr);
11808 			break;
11809 		}
11810 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
11811 		nla_nest_end(skb, tmdata);
11812 
11813 		if (err == -ENOBUFS || err == -ENOENT) {
11814 			genlmsg_cancel(skb, hdr);
11815 			break;
11816 		} else if (err) {
11817 			genlmsg_cancel(skb, hdr);
11818 			goto out_err;
11819 		}
11820 
11821 		genlmsg_end(skb, hdr);
11822 	}
11823 
11824 	err = skb->len;
11825 	/* see above */
11826 	cb->args[0] = phy_idx + 1;
11827  out_err:
11828 	kfree(attrbuf);
11829 	rtnl_unlock();
11830 	return err;
11831 }
11832 #endif
11833 
11834 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
11835 {
11836 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11837 	struct net_device *dev = info->user_ptr[1];
11838 	struct cfg80211_connect_params connect;
11839 	struct wiphy *wiphy;
11840 	struct cfg80211_cached_keys *connkeys = NULL;
11841 	u32 freq = 0;
11842 	int err;
11843 
11844 	memset(&connect, 0, sizeof(connect));
11845 
11846 	if (!info->attrs[NL80211_ATTR_SSID] ||
11847 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11848 		return -EINVAL;
11849 
11850 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
11851 		connect.auth_type =
11852 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
11853 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
11854 					     NL80211_CMD_CONNECT))
11855 			return -EINVAL;
11856 	} else
11857 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
11858 
11859 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
11860 
11861 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
11862 	    !wiphy_ext_feature_isset(&rdev->wiphy,
11863 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
11864 		return -EINVAL;
11865 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
11866 
11867 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
11868 				      NL80211_MAX_NR_CIPHER_SUITES);
11869 	if (err)
11870 		return err;
11871 
11872 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11873 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11874 		return -EOPNOTSUPP;
11875 
11876 	wiphy = &rdev->wiphy;
11877 
11878 	connect.bg_scan_period = -1;
11879 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
11880 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
11881 		connect.bg_scan_period =
11882 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
11883 	}
11884 
11885 	if (info->attrs[NL80211_ATTR_MAC])
11886 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11887 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
11888 		connect.bssid_hint =
11889 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
11890 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11891 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11892 
11893 	if (info->attrs[NL80211_ATTR_IE]) {
11894 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11895 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11896 	}
11897 
11898 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
11899 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
11900 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
11901 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11902 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
11903 			return -EOPNOTSUPP;
11904 	} else {
11905 		connect.mfp = NL80211_MFP_NO;
11906 	}
11907 
11908 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
11909 		connect.prev_bssid =
11910 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
11911 
11912 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ])
11913 		freq = MHZ_TO_KHZ(nla_get_u32(
11914 					info->attrs[NL80211_ATTR_WIPHY_FREQ]));
11915 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
11916 		freq +=
11917 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
11918 
11919 	if (freq) {
11920 		connect.channel = nl80211_get_valid_chan(wiphy, freq);
11921 		if (!connect.channel)
11922 			return -EINVAL;
11923 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
11924 		freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
11925 		freq = MHZ_TO_KHZ(freq);
11926 		connect.channel_hint = nl80211_get_valid_chan(wiphy, freq);
11927 		if (!connect.channel_hint)
11928 			return -EINVAL;
11929 	}
11930 
11931 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
11932 		connect.edmg.channels =
11933 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
11934 
11935 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
11936 			connect.edmg.bw_config =
11937 				nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
11938 	}
11939 
11940 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11941 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
11942 		if (IS_ERR(connkeys))
11943 			return PTR_ERR(connkeys);
11944 	}
11945 
11946 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
11947 		connect.flags |= ASSOC_REQ_DISABLE_HT;
11948 
11949 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11950 		memcpy(&connect.ht_capa_mask,
11951 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11952 		       sizeof(connect.ht_capa_mask));
11953 
11954 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11955 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
11956 			kfree_sensitive(connkeys);
11957 			return -EINVAL;
11958 		}
11959 		memcpy(&connect.ht_capa,
11960 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11961 		       sizeof(connect.ht_capa));
11962 	}
11963 
11964 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
11965 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
11966 
11967 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
11968 		connect.flags |= ASSOC_REQ_DISABLE_HE;
11969 
11970 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
11971 		connect.flags |= ASSOC_REQ_DISABLE_EHT;
11972 
11973 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11974 		memcpy(&connect.vht_capa_mask,
11975 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
11976 		       sizeof(connect.vht_capa_mask));
11977 
11978 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
11979 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
11980 			kfree_sensitive(connkeys);
11981 			return -EINVAL;
11982 		}
11983 		memcpy(&connect.vht_capa,
11984 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
11985 		       sizeof(connect.vht_capa));
11986 	}
11987 
11988 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
11989 		if (!((rdev->wiphy.features &
11990 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
11991 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
11992 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11993 					     NL80211_EXT_FEATURE_RRM)) {
11994 			kfree_sensitive(connkeys);
11995 			return -EINVAL;
11996 		}
11997 		connect.flags |= ASSOC_REQ_USE_RRM;
11998 	}
11999 
12000 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
12001 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
12002 		kfree_sensitive(connkeys);
12003 		return -EOPNOTSUPP;
12004 	}
12005 
12006 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
12007 		/* bss selection makes no sense if bssid is set */
12008 		if (connect.bssid) {
12009 			kfree_sensitive(connkeys);
12010 			return -EINVAL;
12011 		}
12012 
12013 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
12014 				       wiphy, &connect.bss_select);
12015 		if (err) {
12016 			kfree_sensitive(connkeys);
12017 			return err;
12018 		}
12019 	}
12020 
12021 	if (wiphy_ext_feature_isset(&rdev->wiphy,
12022 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
12023 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
12024 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
12025 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
12026 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12027 		connect.fils_erp_username =
12028 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12029 		connect.fils_erp_username_len =
12030 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12031 		connect.fils_erp_realm =
12032 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12033 		connect.fils_erp_realm_len =
12034 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12035 		connect.fils_erp_next_seq_num =
12036 			nla_get_u16(
12037 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
12038 		connect.fils_erp_rrk =
12039 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12040 		connect.fils_erp_rrk_len =
12041 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12042 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
12043 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
12044 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
12045 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12046 		kfree_sensitive(connkeys);
12047 		return -EINVAL;
12048 	}
12049 
12050 	if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
12051 		if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
12052 			kfree_sensitive(connkeys);
12053 			GENL_SET_ERR_MSG(info,
12054 					 "external auth requires connection ownership");
12055 			return -EINVAL;
12056 		}
12057 		connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
12058 	}
12059 
12060 	if (nla_get_flag(info->attrs[NL80211_ATTR_MLO_SUPPORT]))
12061 		connect.flags |= CONNECT_REQ_MLO_SUPPORT;
12062 
12063 	wdev_lock(dev->ieee80211_ptr);
12064 
12065 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
12066 			       connect.prev_bssid);
12067 	if (err)
12068 		kfree_sensitive(connkeys);
12069 
12070 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
12071 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
12072 		if (connect.bssid)
12073 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
12074 			       connect.bssid, ETH_ALEN);
12075 		else
12076 			eth_zero_addr(dev->ieee80211_ptr->disconnect_bssid);
12077 	}
12078 
12079 	wdev_unlock(dev->ieee80211_ptr);
12080 
12081 	return err;
12082 }
12083 
12084 static int nl80211_update_connect_params(struct sk_buff *skb,
12085 					 struct genl_info *info)
12086 {
12087 	struct cfg80211_connect_params connect = {};
12088 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12089 	struct net_device *dev = info->user_ptr[1];
12090 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12091 	bool fils_sk_offload;
12092 	u32 auth_type;
12093 	u32 changed = 0;
12094 	int ret;
12095 
12096 	if (!rdev->ops->update_connect_params)
12097 		return -EOPNOTSUPP;
12098 
12099 	if (info->attrs[NL80211_ATTR_IE]) {
12100 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
12101 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
12102 		changed |= UPDATE_ASSOC_IES;
12103 	}
12104 
12105 	fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
12106 						  NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
12107 
12108 	/*
12109 	 * when driver supports fils-sk offload all attributes must be
12110 	 * provided. So the else covers "fils-sk-not-all" and
12111 	 * "no-fils-sk-any".
12112 	 */
12113 	if (fils_sk_offload &&
12114 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
12115 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
12116 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
12117 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12118 		connect.fils_erp_username =
12119 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12120 		connect.fils_erp_username_len =
12121 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12122 		connect.fils_erp_realm =
12123 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12124 		connect.fils_erp_realm_len =
12125 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12126 		connect.fils_erp_next_seq_num =
12127 			nla_get_u16(
12128 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
12129 		connect.fils_erp_rrk =
12130 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12131 		connect.fils_erp_rrk_len =
12132 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12133 		changed |= UPDATE_FILS_ERP_INFO;
12134 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
12135 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
12136 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
12137 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12138 		return -EINVAL;
12139 	}
12140 
12141 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
12142 		auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
12143 		if (!nl80211_valid_auth_type(rdev, auth_type,
12144 					     NL80211_CMD_CONNECT))
12145 			return -EINVAL;
12146 
12147 		if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
12148 		    fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
12149 			return -EINVAL;
12150 
12151 		connect.auth_type = auth_type;
12152 		changed |= UPDATE_AUTH_TYPE;
12153 	}
12154 
12155 	wdev_lock(dev->ieee80211_ptr);
12156 	if (!wdev->connected)
12157 		ret = -ENOLINK;
12158 	else
12159 		ret = rdev_update_connect_params(rdev, dev, &connect, changed);
12160 	wdev_unlock(dev->ieee80211_ptr);
12161 
12162 	return ret;
12163 }
12164 
12165 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
12166 {
12167 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12168 	struct net_device *dev = info->user_ptr[1];
12169 	u16 reason;
12170 	int ret;
12171 
12172 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
12173 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
12174 		return -EPERM;
12175 
12176 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
12177 		reason = WLAN_REASON_DEAUTH_LEAVING;
12178 	else
12179 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
12180 
12181 	if (reason == 0)
12182 		return -EINVAL;
12183 
12184 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12185 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
12186 		return -EOPNOTSUPP;
12187 
12188 	wdev_lock(dev->ieee80211_ptr);
12189 	ret = cfg80211_disconnect(rdev, dev, reason, true);
12190 	wdev_unlock(dev->ieee80211_ptr);
12191 	return ret;
12192 }
12193 
12194 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
12195 {
12196 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12197 	struct net *net;
12198 	int err;
12199 
12200 	if (info->attrs[NL80211_ATTR_PID]) {
12201 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
12202 
12203 		net = get_net_ns_by_pid(pid);
12204 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
12205 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
12206 
12207 		net = get_net_ns_by_fd(fd);
12208 	} else {
12209 		return -EINVAL;
12210 	}
12211 
12212 	if (IS_ERR(net))
12213 		return PTR_ERR(net);
12214 
12215 	err = 0;
12216 
12217 	/* check if anything to do */
12218 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
12219 		err = cfg80211_switch_netns(rdev, net);
12220 
12221 	put_net(net);
12222 	return err;
12223 }
12224 
12225 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
12226 {
12227 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12228 	int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
12229 			struct cfg80211_pmksa *pmksa) = NULL;
12230 	struct net_device *dev = info->user_ptr[1];
12231 	struct cfg80211_pmksa pmksa;
12232 
12233 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
12234 
12235 	if (!info->attrs[NL80211_ATTR_PMKID])
12236 		return -EINVAL;
12237 
12238 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
12239 
12240 	if (info->attrs[NL80211_ATTR_MAC]) {
12241 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
12242 	} else if (info->attrs[NL80211_ATTR_SSID] &&
12243 		   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
12244 		   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
12245 		    info->attrs[NL80211_ATTR_PMK])) {
12246 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
12247 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
12248 		pmksa.cache_id =
12249 			nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
12250 	} else {
12251 		return -EINVAL;
12252 	}
12253 	if (info->attrs[NL80211_ATTR_PMK]) {
12254 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
12255 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
12256 	}
12257 
12258 	if (info->attrs[NL80211_ATTR_PMK_LIFETIME])
12259 		pmksa.pmk_lifetime =
12260 			nla_get_u32(info->attrs[NL80211_ATTR_PMK_LIFETIME]);
12261 
12262 	if (info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD])
12263 		pmksa.pmk_reauth_threshold =
12264 			nla_get_u8(
12265 				info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD]);
12266 
12267 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12268 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
12269 	    !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
12270 	      wiphy_ext_feature_isset(&rdev->wiphy,
12271 				      NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
12272 		return -EOPNOTSUPP;
12273 
12274 	switch (info->genlhdr->cmd) {
12275 	case NL80211_CMD_SET_PMKSA:
12276 		rdev_ops = rdev->ops->set_pmksa;
12277 		break;
12278 	case NL80211_CMD_DEL_PMKSA:
12279 		rdev_ops = rdev->ops->del_pmksa;
12280 		break;
12281 	default:
12282 		WARN_ON(1);
12283 		break;
12284 	}
12285 
12286 	if (!rdev_ops)
12287 		return -EOPNOTSUPP;
12288 
12289 	return rdev_ops(&rdev->wiphy, dev, &pmksa);
12290 }
12291 
12292 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
12293 {
12294 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12295 	struct net_device *dev = info->user_ptr[1];
12296 
12297 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12298 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
12299 		return -EOPNOTSUPP;
12300 
12301 	if (!rdev->ops->flush_pmksa)
12302 		return -EOPNOTSUPP;
12303 
12304 	return rdev_flush_pmksa(rdev, dev);
12305 }
12306 
12307 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
12308 {
12309 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12310 	struct net_device *dev = info->user_ptr[1];
12311 	u8 action_code, dialog_token;
12312 	u32 peer_capability = 0;
12313 	u16 status_code;
12314 	u8 *peer;
12315 	int link_id;
12316 	bool initiator;
12317 
12318 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12319 	    !rdev->ops->tdls_mgmt)
12320 		return -EOPNOTSUPP;
12321 
12322 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
12323 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
12324 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
12325 	    !info->attrs[NL80211_ATTR_IE] ||
12326 	    !info->attrs[NL80211_ATTR_MAC])
12327 		return -EINVAL;
12328 
12329 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12330 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
12331 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
12332 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
12333 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
12334 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
12335 		peer_capability =
12336 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
12337 	link_id = nl80211_link_id_or_invalid(info->attrs);
12338 
12339 	return rdev_tdls_mgmt(rdev, dev, peer, link_id, action_code,
12340 			      dialog_token, status_code, peer_capability,
12341 			      initiator,
12342 			      nla_data(info->attrs[NL80211_ATTR_IE]),
12343 			      nla_len(info->attrs[NL80211_ATTR_IE]));
12344 }
12345 
12346 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
12347 {
12348 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12349 	struct net_device *dev = info->user_ptr[1];
12350 	enum nl80211_tdls_operation operation;
12351 	u8 *peer;
12352 
12353 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12354 	    !rdev->ops->tdls_oper)
12355 		return -EOPNOTSUPP;
12356 
12357 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
12358 	    !info->attrs[NL80211_ATTR_MAC])
12359 		return -EINVAL;
12360 
12361 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
12362 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12363 
12364 	return rdev_tdls_oper(rdev, dev, peer, operation);
12365 }
12366 
12367 static int nl80211_remain_on_channel(struct sk_buff *skb,
12368 				     struct genl_info *info)
12369 {
12370 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12371 	unsigned int link_id = nl80211_link_id(info->attrs);
12372 	struct wireless_dev *wdev = info->user_ptr[1];
12373 	struct cfg80211_chan_def chandef;
12374 	struct sk_buff *msg;
12375 	void *hdr;
12376 	u64 cookie;
12377 	u32 duration;
12378 	int err;
12379 
12380 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
12381 	    !info->attrs[NL80211_ATTR_DURATION])
12382 		return -EINVAL;
12383 
12384 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12385 
12386 	if (!rdev->ops->remain_on_channel ||
12387 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
12388 		return -EOPNOTSUPP;
12389 
12390 	/*
12391 	 * We should be on that channel for at least a minimum amount of
12392 	 * time (10ms) but no longer than the driver supports.
12393 	 */
12394 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12395 	    duration > rdev->wiphy.max_remain_on_channel_duration)
12396 		return -EINVAL;
12397 
12398 	err = nl80211_parse_chandef(rdev, info, &chandef);
12399 	if (err)
12400 		return err;
12401 
12402 	wdev_lock(wdev);
12403 	if (!cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12404 		const struct cfg80211_chan_def *oper_chandef, *compat_chandef;
12405 
12406 		oper_chandef = wdev_chandef(wdev, link_id);
12407 
12408 		if (WARN_ON(!oper_chandef)) {
12409 			/* cannot happen since we must beacon to get here */
12410 			WARN_ON(1);
12411 			wdev_unlock(wdev);
12412 			return -EBUSY;
12413 		}
12414 
12415 		/* note: returns first one if identical chandefs */
12416 		compat_chandef = cfg80211_chandef_compatible(&chandef,
12417 							     oper_chandef);
12418 
12419 		if (compat_chandef != &chandef) {
12420 			wdev_unlock(wdev);
12421 			return -EBUSY;
12422 		}
12423 	}
12424 	wdev_unlock(wdev);
12425 
12426 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12427 	if (!msg)
12428 		return -ENOMEM;
12429 
12430 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12431 			     NL80211_CMD_REMAIN_ON_CHANNEL);
12432 	if (!hdr) {
12433 		err = -ENOBUFS;
12434 		goto free_msg;
12435 	}
12436 
12437 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
12438 				     duration, &cookie);
12439 
12440 	if (err)
12441 		goto free_msg;
12442 
12443 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12444 			      NL80211_ATTR_PAD))
12445 		goto nla_put_failure;
12446 
12447 	genlmsg_end(msg, hdr);
12448 
12449 	return genlmsg_reply(msg, info);
12450 
12451  nla_put_failure:
12452 	err = -ENOBUFS;
12453  free_msg:
12454 	nlmsg_free(msg);
12455 	return err;
12456 }
12457 
12458 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
12459 					    struct genl_info *info)
12460 {
12461 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12462 	struct wireless_dev *wdev = info->user_ptr[1];
12463 	u64 cookie;
12464 
12465 	if (!info->attrs[NL80211_ATTR_COOKIE])
12466 		return -EINVAL;
12467 
12468 	if (!rdev->ops->cancel_remain_on_channel)
12469 		return -EOPNOTSUPP;
12470 
12471 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12472 
12473 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
12474 }
12475 
12476 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
12477 				       struct genl_info *info)
12478 {
12479 	struct cfg80211_bitrate_mask mask;
12480 	unsigned int link_id = nl80211_link_id(info->attrs);
12481 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12482 	struct net_device *dev = info->user_ptr[1];
12483 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12484 	int err;
12485 
12486 	if (!rdev->ops->set_bitrate_mask)
12487 		return -EOPNOTSUPP;
12488 
12489 	wdev_lock(wdev);
12490 	err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
12491 					    NL80211_ATTR_TX_RATES, &mask,
12492 					    dev, true, link_id);
12493 	if (err)
12494 		goto out;
12495 
12496 	err = rdev_set_bitrate_mask(rdev, dev, link_id, NULL, &mask);
12497 out:
12498 	wdev_unlock(wdev);
12499 	return err;
12500 }
12501 
12502 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
12503 {
12504 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12505 	struct wireless_dev *wdev = info->user_ptr[1];
12506 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
12507 
12508 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
12509 		return -EINVAL;
12510 
12511 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
12512 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
12513 
12514 	switch (wdev->iftype) {
12515 	case NL80211_IFTYPE_STATION:
12516 	case NL80211_IFTYPE_ADHOC:
12517 	case NL80211_IFTYPE_P2P_CLIENT:
12518 	case NL80211_IFTYPE_AP:
12519 	case NL80211_IFTYPE_AP_VLAN:
12520 	case NL80211_IFTYPE_MESH_POINT:
12521 	case NL80211_IFTYPE_P2P_GO:
12522 	case NL80211_IFTYPE_P2P_DEVICE:
12523 		break;
12524 	case NL80211_IFTYPE_NAN:
12525 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12526 					     NL80211_EXT_FEATURE_SECURE_NAN))
12527 			return -EOPNOTSUPP;
12528 		break;
12529 	default:
12530 		return -EOPNOTSUPP;
12531 	}
12532 
12533 	/* not much point in registering if we can't reply */
12534 	if (!rdev->ops->mgmt_tx)
12535 		return -EOPNOTSUPP;
12536 
12537 	if (info->attrs[NL80211_ATTR_RECEIVE_MULTICAST] &&
12538 	    !wiphy_ext_feature_isset(&rdev->wiphy,
12539 				     NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS)) {
12540 		GENL_SET_ERR_MSG(info,
12541 				 "multicast RX registrations are not supported");
12542 		return -EOPNOTSUPP;
12543 	}
12544 
12545 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
12546 					   nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12547 					   nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12548 					   info->attrs[NL80211_ATTR_RECEIVE_MULTICAST],
12549 					   info->extack);
12550 }
12551 
12552 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
12553 {
12554 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12555 	struct wireless_dev *wdev = info->user_ptr[1];
12556 	struct cfg80211_chan_def chandef;
12557 	int err;
12558 	void *hdr = NULL;
12559 	u64 cookie;
12560 	struct sk_buff *msg = NULL;
12561 	struct cfg80211_mgmt_tx_params params = {
12562 		.dont_wait_for_ack =
12563 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
12564 	};
12565 
12566 	if (!info->attrs[NL80211_ATTR_FRAME])
12567 		return -EINVAL;
12568 
12569 	if (!rdev->ops->mgmt_tx)
12570 		return -EOPNOTSUPP;
12571 
12572 	switch (wdev->iftype) {
12573 	case NL80211_IFTYPE_P2P_DEVICE:
12574 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
12575 			return -EINVAL;
12576 		break;
12577 	case NL80211_IFTYPE_STATION:
12578 	case NL80211_IFTYPE_ADHOC:
12579 	case NL80211_IFTYPE_P2P_CLIENT:
12580 	case NL80211_IFTYPE_AP:
12581 	case NL80211_IFTYPE_AP_VLAN:
12582 	case NL80211_IFTYPE_MESH_POINT:
12583 	case NL80211_IFTYPE_P2P_GO:
12584 		break;
12585 	case NL80211_IFTYPE_NAN:
12586 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12587 					     NL80211_EXT_FEATURE_SECURE_NAN))
12588 			return -EOPNOTSUPP;
12589 		break;
12590 	default:
12591 		return -EOPNOTSUPP;
12592 	}
12593 
12594 	if (info->attrs[NL80211_ATTR_DURATION]) {
12595 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12596 			return -EINVAL;
12597 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12598 
12599 		/*
12600 		 * We should wait on the channel for at least a minimum amount
12601 		 * of time (10ms) but no longer than the driver supports.
12602 		 */
12603 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12604 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
12605 			return -EINVAL;
12606 	}
12607 
12608 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
12609 
12610 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12611 		return -EINVAL;
12612 
12613 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
12614 
12615 	/* get the channel if any has been specified, otherwise pass NULL to
12616 	 * the driver. The latter will use the current one
12617 	 */
12618 	chandef.chan = NULL;
12619 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
12620 		err = nl80211_parse_chandef(rdev, info, &chandef);
12621 		if (err)
12622 			return err;
12623 	}
12624 
12625 	if (!chandef.chan && params.offchan)
12626 		return -EINVAL;
12627 
12628 	wdev_lock(wdev);
12629 	if (params.offchan &&
12630 	    !cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12631 		wdev_unlock(wdev);
12632 		return -EBUSY;
12633 	}
12634 
12635 	params.link_id = nl80211_link_id_or_invalid(info->attrs);
12636 	/*
12637 	 * This now races due to the unlock, but we cannot check
12638 	 * the valid links for the _station_ anyway, so that's up
12639 	 * to the driver.
12640 	 */
12641 	if (params.link_id >= 0 &&
12642 	    !(wdev->valid_links & BIT(params.link_id))) {
12643 		wdev_unlock(wdev);
12644 		return -EINVAL;
12645 	}
12646 	wdev_unlock(wdev);
12647 
12648 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
12649 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
12650 
12651 	if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
12652 		int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12653 		int i;
12654 
12655 		if (len % sizeof(u16))
12656 			return -EINVAL;
12657 
12658 		params.n_csa_offsets = len / sizeof(u16);
12659 		params.csa_offsets =
12660 			nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12661 
12662 		/* check that all the offsets fit the frame */
12663 		for (i = 0; i < params.n_csa_offsets; i++) {
12664 			if (params.csa_offsets[i] >= params.len)
12665 				return -EINVAL;
12666 		}
12667 	}
12668 
12669 	if (!params.dont_wait_for_ack) {
12670 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12671 		if (!msg)
12672 			return -ENOMEM;
12673 
12674 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12675 				     NL80211_CMD_FRAME);
12676 		if (!hdr) {
12677 			err = -ENOBUFS;
12678 			goto free_msg;
12679 		}
12680 	}
12681 
12682 	params.chan = chandef.chan;
12683 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
12684 	if (err)
12685 		goto free_msg;
12686 
12687 	if (msg) {
12688 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12689 				      NL80211_ATTR_PAD))
12690 			goto nla_put_failure;
12691 
12692 		genlmsg_end(msg, hdr);
12693 		return genlmsg_reply(msg, info);
12694 	}
12695 
12696 	return 0;
12697 
12698  nla_put_failure:
12699 	err = -ENOBUFS;
12700  free_msg:
12701 	nlmsg_free(msg);
12702 	return err;
12703 }
12704 
12705 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
12706 {
12707 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12708 	struct wireless_dev *wdev = info->user_ptr[1];
12709 	u64 cookie;
12710 
12711 	if (!info->attrs[NL80211_ATTR_COOKIE])
12712 		return -EINVAL;
12713 
12714 	if (!rdev->ops->mgmt_tx_cancel_wait)
12715 		return -EOPNOTSUPP;
12716 
12717 	switch (wdev->iftype) {
12718 	case NL80211_IFTYPE_STATION:
12719 	case NL80211_IFTYPE_ADHOC:
12720 	case NL80211_IFTYPE_P2P_CLIENT:
12721 	case NL80211_IFTYPE_AP:
12722 	case NL80211_IFTYPE_AP_VLAN:
12723 	case NL80211_IFTYPE_P2P_GO:
12724 	case NL80211_IFTYPE_P2P_DEVICE:
12725 		break;
12726 	case NL80211_IFTYPE_NAN:
12727 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12728 					     NL80211_EXT_FEATURE_SECURE_NAN))
12729 			return -EOPNOTSUPP;
12730 		break;
12731 	default:
12732 		return -EOPNOTSUPP;
12733 	}
12734 
12735 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12736 
12737 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
12738 }
12739 
12740 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
12741 {
12742 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12743 	struct wireless_dev *wdev;
12744 	struct net_device *dev = info->user_ptr[1];
12745 	u8 ps_state;
12746 	bool state;
12747 	int err;
12748 
12749 	if (!info->attrs[NL80211_ATTR_PS_STATE])
12750 		return -EINVAL;
12751 
12752 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
12753 
12754 	wdev = dev->ieee80211_ptr;
12755 
12756 	if (!rdev->ops->set_power_mgmt)
12757 		return -EOPNOTSUPP;
12758 
12759 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
12760 
12761 	if (state == wdev->ps)
12762 		return 0;
12763 
12764 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
12765 	if (!err)
12766 		wdev->ps = state;
12767 	return err;
12768 }
12769 
12770 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
12771 {
12772 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12773 	enum nl80211_ps_state ps_state;
12774 	struct wireless_dev *wdev;
12775 	struct net_device *dev = info->user_ptr[1];
12776 	struct sk_buff *msg;
12777 	void *hdr;
12778 	int err;
12779 
12780 	wdev = dev->ieee80211_ptr;
12781 
12782 	if (!rdev->ops->set_power_mgmt)
12783 		return -EOPNOTSUPP;
12784 
12785 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12786 	if (!msg)
12787 		return -ENOMEM;
12788 
12789 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12790 			     NL80211_CMD_GET_POWER_SAVE);
12791 	if (!hdr) {
12792 		err = -ENOBUFS;
12793 		goto free_msg;
12794 	}
12795 
12796 	if (wdev->ps)
12797 		ps_state = NL80211_PS_ENABLED;
12798 	else
12799 		ps_state = NL80211_PS_DISABLED;
12800 
12801 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
12802 		goto nla_put_failure;
12803 
12804 	genlmsg_end(msg, hdr);
12805 	return genlmsg_reply(msg, info);
12806 
12807  nla_put_failure:
12808 	err = -ENOBUFS;
12809  free_msg:
12810 	nlmsg_free(msg);
12811 	return err;
12812 }
12813 
12814 static const struct nla_policy
12815 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
12816 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
12817 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
12818 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
12819 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
12820 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
12821 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
12822 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
12823 };
12824 
12825 static int nl80211_set_cqm_txe(struct genl_info *info,
12826 			       u32 rate, u32 pkts, u32 intvl)
12827 {
12828 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12829 	struct net_device *dev = info->user_ptr[1];
12830 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12831 
12832 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
12833 		return -EINVAL;
12834 
12835 	if (!rdev->ops->set_cqm_txe_config)
12836 		return -EOPNOTSUPP;
12837 
12838 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12839 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12840 		return -EOPNOTSUPP;
12841 
12842 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
12843 }
12844 
12845 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
12846 				    struct net_device *dev,
12847 				    struct cfg80211_cqm_config *cqm_config)
12848 {
12849 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12850 	s32 last, low, high;
12851 	u32 hyst;
12852 	int i, n, low_index;
12853 	int err;
12854 
12855 	/*
12856 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
12857 	 * event has been received yet, we should receive an event after a
12858 	 * connection is established and enough beacons received to calculate
12859 	 * the average.
12860 	 */
12861 	if (!cqm_config->last_rssi_event_value &&
12862 	    wdev->links[0].client.current_bss &&
12863 	    rdev->ops->get_station) {
12864 		struct station_info sinfo = {};
12865 		u8 *mac_addr;
12866 
12867 		mac_addr = wdev->links[0].client.current_bss->pub.bssid;
12868 
12869 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
12870 		if (err)
12871 			return err;
12872 
12873 		cfg80211_sinfo_release_content(&sinfo);
12874 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
12875 			cqm_config->last_rssi_event_value =
12876 				(s8) sinfo.rx_beacon_signal_avg;
12877 	}
12878 
12879 	last = cqm_config->last_rssi_event_value;
12880 	hyst = cqm_config->rssi_hyst;
12881 	n = cqm_config->n_rssi_thresholds;
12882 
12883 	for (i = 0; i < n; i++) {
12884 		i = array_index_nospec(i, n);
12885 		if (last < cqm_config->rssi_thresholds[i])
12886 			break;
12887 	}
12888 
12889 	low_index = i - 1;
12890 	if (low_index >= 0) {
12891 		low_index = array_index_nospec(low_index, n);
12892 		low = cqm_config->rssi_thresholds[low_index] - hyst;
12893 	} else {
12894 		low = S32_MIN;
12895 	}
12896 	if (i < n) {
12897 		i = array_index_nospec(i, n);
12898 		high = cqm_config->rssi_thresholds[i] + hyst - 1;
12899 	} else {
12900 		high = S32_MAX;
12901 	}
12902 
12903 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
12904 }
12905 
12906 static int nl80211_set_cqm_rssi(struct genl_info *info,
12907 				const s32 *thresholds, int n_thresholds,
12908 				u32 hysteresis)
12909 {
12910 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12911 	struct cfg80211_cqm_config *cqm_config = NULL, *old;
12912 	struct net_device *dev = info->user_ptr[1];
12913 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12914 	int i, err;
12915 	s32 prev = S32_MIN;
12916 
12917 	/* Check all values negative and sorted */
12918 	for (i = 0; i < n_thresholds; i++) {
12919 		if (thresholds[i] > 0 || thresholds[i] <= prev)
12920 			return -EINVAL;
12921 
12922 		prev = thresholds[i];
12923 	}
12924 
12925 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12926 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12927 		return -EOPNOTSUPP;
12928 
12929 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
12930 		n_thresholds = 0;
12931 
12932 	wdev_lock(wdev);
12933 	old = rcu_dereference_protected(wdev->cqm_config,
12934 					lockdep_is_held(&wdev->mtx));
12935 
12936 	/* if already disabled just succeed */
12937 	if (!n_thresholds && !old) {
12938 		err = 0;
12939 		goto unlock;
12940 	}
12941 
12942 	if (n_thresholds > 1) {
12943 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
12944 					     NL80211_EXT_FEATURE_CQM_RSSI_LIST) ||
12945 		    !rdev->ops->set_cqm_rssi_range_config) {
12946 			err = -EOPNOTSUPP;
12947 			goto unlock;
12948 		}
12949 	} else {
12950 		if (!rdev->ops->set_cqm_rssi_config) {
12951 			err = -EOPNOTSUPP;
12952 			goto unlock;
12953 		}
12954 	}
12955 
12956 	if (n_thresholds) {
12957 		cqm_config = kzalloc(struct_size(cqm_config, rssi_thresholds,
12958 						 n_thresholds),
12959 				     GFP_KERNEL);
12960 		if (!cqm_config) {
12961 			err = -ENOMEM;
12962 			goto unlock;
12963 		}
12964 
12965 		cqm_config->rssi_hyst = hysteresis;
12966 		cqm_config->n_rssi_thresholds = n_thresholds;
12967 		memcpy(cqm_config->rssi_thresholds, thresholds,
12968 		       flex_array_size(cqm_config, rssi_thresholds,
12969 				       n_thresholds));
12970 		cqm_config->use_range_api = n_thresholds > 1 ||
12971 					    !rdev->ops->set_cqm_rssi_config;
12972 
12973 		rcu_assign_pointer(wdev->cqm_config, cqm_config);
12974 
12975 		if (cqm_config->use_range_api)
12976 			err = cfg80211_cqm_rssi_update(rdev, dev, cqm_config);
12977 		else
12978 			err = rdev_set_cqm_rssi_config(rdev, dev,
12979 						       thresholds[0],
12980 						       hysteresis);
12981 	} else {
12982 		RCU_INIT_POINTER(wdev->cqm_config, NULL);
12983 		/* if enabled as range also disable via range */
12984 		if (old->use_range_api)
12985 			err = rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
12986 		else
12987 			err = rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
12988 	}
12989 
12990 	if (err) {
12991 		rcu_assign_pointer(wdev->cqm_config, old);
12992 		kfree_rcu(cqm_config, rcu_head);
12993 	} else {
12994 		kfree_rcu(old, rcu_head);
12995 	}
12996 unlock:
12997 	wdev_unlock(wdev);
12998 
12999 	return err;
13000 }
13001 
13002 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
13003 {
13004 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
13005 	struct nlattr *cqm;
13006 	int err;
13007 
13008 	cqm = info->attrs[NL80211_ATTR_CQM];
13009 	if (!cqm)
13010 		return -EINVAL;
13011 
13012 	err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
13013 					  nl80211_attr_cqm_policy,
13014 					  info->extack);
13015 	if (err)
13016 		return err;
13017 
13018 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
13019 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
13020 		const s32 *thresholds =
13021 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
13022 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
13023 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
13024 
13025 		if (len % 4)
13026 			return -EINVAL;
13027 
13028 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
13029 					    hysteresis);
13030 	}
13031 
13032 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
13033 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
13034 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
13035 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
13036 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
13037 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
13038 
13039 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
13040 	}
13041 
13042 	return -EINVAL;
13043 }
13044 
13045 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
13046 {
13047 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13048 	struct net_device *dev = info->user_ptr[1];
13049 	struct ocb_setup setup = {};
13050 	int err;
13051 
13052 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
13053 	if (err)
13054 		return err;
13055 
13056 	return cfg80211_join_ocb(rdev, dev, &setup);
13057 }
13058 
13059 static int nl80211_leave_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 
13064 	return cfg80211_leave_ocb(rdev, dev);
13065 }
13066 
13067 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
13068 {
13069 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13070 	struct net_device *dev = info->user_ptr[1];
13071 	struct mesh_config cfg;
13072 	struct mesh_setup setup;
13073 	int err;
13074 
13075 	/* start with default */
13076 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
13077 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
13078 
13079 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
13080 		/* and parse parameters if given */
13081 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
13082 		if (err)
13083 			return err;
13084 	}
13085 
13086 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
13087 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
13088 		return -EINVAL;
13089 
13090 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
13091 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
13092 
13093 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
13094 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
13095 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
13096 			return -EINVAL;
13097 
13098 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
13099 		setup.beacon_interval =
13100 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
13101 
13102 		err = cfg80211_validate_beacon_int(rdev,
13103 						   NL80211_IFTYPE_MESH_POINT,
13104 						   setup.beacon_interval);
13105 		if (err)
13106 			return err;
13107 	}
13108 
13109 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
13110 		setup.dtim_period =
13111 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
13112 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
13113 			return -EINVAL;
13114 	}
13115 
13116 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
13117 		/* parse additional setup parameters if given */
13118 		err = nl80211_parse_mesh_setup(info, &setup);
13119 		if (err)
13120 			return err;
13121 	}
13122 
13123 	if (setup.user_mpm)
13124 		cfg.auto_open_plinks = false;
13125 
13126 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
13127 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
13128 		if (err)
13129 			return err;
13130 	} else {
13131 		/* __cfg80211_join_mesh() will sort it out */
13132 		setup.chandef.chan = NULL;
13133 	}
13134 
13135 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
13136 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
13137 		int n_rates =
13138 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
13139 		struct ieee80211_supported_band *sband;
13140 
13141 		if (!setup.chandef.chan)
13142 			return -EINVAL;
13143 
13144 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
13145 
13146 		err = ieee80211_get_ratemask(sband, rates, n_rates,
13147 					     &setup.basic_rates);
13148 		if (err)
13149 			return err;
13150 	}
13151 
13152 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
13153 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
13154 						    NL80211_ATTR_TX_RATES,
13155 						    &setup.beacon_rate,
13156 						    dev, false, 0);
13157 		if (err)
13158 			return err;
13159 
13160 		if (!setup.chandef.chan)
13161 			return -EINVAL;
13162 
13163 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
13164 					      &setup.beacon_rate);
13165 		if (err)
13166 			return err;
13167 	}
13168 
13169 	setup.userspace_handles_dfs =
13170 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
13171 
13172 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
13173 		int r = validate_pae_over_nl80211(rdev, info);
13174 
13175 		if (r < 0)
13176 			return r;
13177 
13178 		setup.control_port_over_nl80211 = true;
13179 	}
13180 
13181 	wdev_lock(dev->ieee80211_ptr);
13182 	err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
13183 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
13184 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
13185 	wdev_unlock(dev->ieee80211_ptr);
13186 
13187 	return err;
13188 }
13189 
13190 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
13191 {
13192 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13193 	struct net_device *dev = info->user_ptr[1];
13194 
13195 	return cfg80211_leave_mesh(rdev, dev);
13196 }
13197 
13198 #ifdef CONFIG_PM
13199 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
13200 					struct cfg80211_registered_device *rdev)
13201 {
13202 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
13203 	struct nlattr *nl_pats, *nl_pat;
13204 	int i, pat_len;
13205 
13206 	if (!wowlan->n_patterns)
13207 		return 0;
13208 
13209 	nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
13210 	if (!nl_pats)
13211 		return -ENOBUFS;
13212 
13213 	for (i = 0; i < wowlan->n_patterns; i++) {
13214 		nl_pat = nla_nest_start_noflag(msg, i + 1);
13215 		if (!nl_pat)
13216 			return -ENOBUFS;
13217 		pat_len = wowlan->patterns[i].pattern_len;
13218 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
13219 			    wowlan->patterns[i].mask) ||
13220 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
13221 			    wowlan->patterns[i].pattern) ||
13222 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
13223 				wowlan->patterns[i].pkt_offset))
13224 			return -ENOBUFS;
13225 		nla_nest_end(msg, nl_pat);
13226 	}
13227 	nla_nest_end(msg, nl_pats);
13228 
13229 	return 0;
13230 }
13231 
13232 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
13233 				   struct cfg80211_wowlan_tcp *tcp)
13234 {
13235 	struct nlattr *nl_tcp;
13236 
13237 	if (!tcp)
13238 		return 0;
13239 
13240 	nl_tcp = nla_nest_start_noflag(msg,
13241 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
13242 	if (!nl_tcp)
13243 		return -ENOBUFS;
13244 
13245 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
13246 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
13247 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
13248 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
13249 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
13250 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
13251 		    tcp->payload_len, tcp->payload) ||
13252 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
13253 			tcp->data_interval) ||
13254 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
13255 		    tcp->wake_len, tcp->wake_data) ||
13256 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
13257 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
13258 		return -ENOBUFS;
13259 
13260 	if (tcp->payload_seq.len &&
13261 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
13262 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
13263 		return -ENOBUFS;
13264 
13265 	if (tcp->payload_tok.len &&
13266 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
13267 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
13268 		    &tcp->payload_tok))
13269 		return -ENOBUFS;
13270 
13271 	nla_nest_end(msg, nl_tcp);
13272 
13273 	return 0;
13274 }
13275 
13276 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
13277 				  struct cfg80211_sched_scan_request *req)
13278 {
13279 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
13280 	int i;
13281 
13282 	if (!req)
13283 		return 0;
13284 
13285 	nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
13286 	if (!nd)
13287 		return -ENOBUFS;
13288 
13289 	if (req->n_scan_plans == 1 &&
13290 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
13291 			req->scan_plans[0].interval * 1000))
13292 		return -ENOBUFS;
13293 
13294 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
13295 		return -ENOBUFS;
13296 
13297 	if (req->relative_rssi_set) {
13298 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
13299 
13300 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
13301 			       req->relative_rssi))
13302 			return -ENOBUFS;
13303 
13304 		rssi_adjust.band = req->rssi_adjust.band;
13305 		rssi_adjust.delta = req->rssi_adjust.delta;
13306 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
13307 			    sizeof(rssi_adjust), &rssi_adjust))
13308 			return -ENOBUFS;
13309 	}
13310 
13311 	freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
13312 	if (!freqs)
13313 		return -ENOBUFS;
13314 
13315 	for (i = 0; i < req->n_channels; i++) {
13316 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
13317 			return -ENOBUFS;
13318 	}
13319 
13320 	nla_nest_end(msg, freqs);
13321 
13322 	if (req->n_match_sets) {
13323 		matches = nla_nest_start_noflag(msg,
13324 						NL80211_ATTR_SCHED_SCAN_MATCH);
13325 		if (!matches)
13326 			return -ENOBUFS;
13327 
13328 		for (i = 0; i < req->n_match_sets; i++) {
13329 			match = nla_nest_start_noflag(msg, i);
13330 			if (!match)
13331 				return -ENOBUFS;
13332 
13333 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
13334 				    req->match_sets[i].ssid.ssid_len,
13335 				    req->match_sets[i].ssid.ssid))
13336 				return -ENOBUFS;
13337 			nla_nest_end(msg, match);
13338 		}
13339 		nla_nest_end(msg, matches);
13340 	}
13341 
13342 	scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
13343 	if (!scan_plans)
13344 		return -ENOBUFS;
13345 
13346 	for (i = 0; i < req->n_scan_plans; i++) {
13347 		scan_plan = nla_nest_start_noflag(msg, i + 1);
13348 		if (!scan_plan)
13349 			return -ENOBUFS;
13350 
13351 		if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
13352 				req->scan_plans[i].interval) ||
13353 		    (req->scan_plans[i].iterations &&
13354 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
13355 				 req->scan_plans[i].iterations)))
13356 			return -ENOBUFS;
13357 		nla_nest_end(msg, scan_plan);
13358 	}
13359 	nla_nest_end(msg, scan_plans);
13360 
13361 	nla_nest_end(msg, nd);
13362 
13363 	return 0;
13364 }
13365 
13366 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
13367 {
13368 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13369 	struct sk_buff *msg;
13370 	void *hdr;
13371 	u32 size = NLMSG_DEFAULT_SIZE;
13372 
13373 	if (!rdev->wiphy.wowlan)
13374 		return -EOPNOTSUPP;
13375 
13376 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
13377 		/* adjust size to have room for all the data */
13378 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
13379 			rdev->wiphy.wowlan_config->tcp->payload_len +
13380 			rdev->wiphy.wowlan_config->tcp->wake_len +
13381 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
13382 	}
13383 
13384 	msg = nlmsg_new(size, GFP_KERNEL);
13385 	if (!msg)
13386 		return -ENOMEM;
13387 
13388 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13389 			     NL80211_CMD_GET_WOWLAN);
13390 	if (!hdr)
13391 		goto nla_put_failure;
13392 
13393 	if (rdev->wiphy.wowlan_config) {
13394 		struct nlattr *nl_wowlan;
13395 
13396 		nl_wowlan = nla_nest_start_noflag(msg,
13397 						  NL80211_ATTR_WOWLAN_TRIGGERS);
13398 		if (!nl_wowlan)
13399 			goto nla_put_failure;
13400 
13401 		if ((rdev->wiphy.wowlan_config->any &&
13402 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
13403 		    (rdev->wiphy.wowlan_config->disconnect &&
13404 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
13405 		    (rdev->wiphy.wowlan_config->magic_pkt &&
13406 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
13407 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
13408 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
13409 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
13410 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
13411 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
13412 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
13413 		    (rdev->wiphy.wowlan_config->rfkill_release &&
13414 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
13415 			goto nla_put_failure;
13416 
13417 		if (nl80211_send_wowlan_patterns(msg, rdev))
13418 			goto nla_put_failure;
13419 
13420 		if (nl80211_send_wowlan_tcp(msg,
13421 					    rdev->wiphy.wowlan_config->tcp))
13422 			goto nla_put_failure;
13423 
13424 		if (nl80211_send_wowlan_nd(
13425 			    msg,
13426 			    rdev->wiphy.wowlan_config->nd_config))
13427 			goto nla_put_failure;
13428 
13429 		nla_nest_end(msg, nl_wowlan);
13430 	}
13431 
13432 	genlmsg_end(msg, hdr);
13433 	return genlmsg_reply(msg, info);
13434 
13435 nla_put_failure:
13436 	nlmsg_free(msg);
13437 	return -ENOBUFS;
13438 }
13439 
13440 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
13441 				    struct nlattr *attr,
13442 				    struct cfg80211_wowlan *trig)
13443 {
13444 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
13445 	struct cfg80211_wowlan_tcp *cfg;
13446 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
13447 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
13448 	u32 size;
13449 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
13450 	int err, port;
13451 
13452 	if (!rdev->wiphy.wowlan->tcp)
13453 		return -EINVAL;
13454 
13455 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
13456 					  nl80211_wowlan_tcp_policy, NULL);
13457 	if (err)
13458 		return err;
13459 
13460 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
13461 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
13462 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
13463 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
13464 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
13465 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
13466 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
13467 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
13468 		return -EINVAL;
13469 
13470 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
13471 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
13472 		return -EINVAL;
13473 
13474 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
13475 			rdev->wiphy.wowlan->tcp->data_interval_max ||
13476 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
13477 		return -EINVAL;
13478 
13479 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
13480 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
13481 		return -EINVAL;
13482 
13483 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
13484 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
13485 		return -EINVAL;
13486 
13487 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
13488 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13489 
13490 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13491 		tokens_size = tokln - sizeof(*tok);
13492 
13493 		if (!tok->len || tokens_size % tok->len)
13494 			return -EINVAL;
13495 		if (!rdev->wiphy.wowlan->tcp->tok)
13496 			return -EINVAL;
13497 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
13498 			return -EINVAL;
13499 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
13500 			return -EINVAL;
13501 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
13502 			return -EINVAL;
13503 		if (tok->offset + tok->len > data_size)
13504 			return -EINVAL;
13505 	}
13506 
13507 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
13508 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
13509 		if (!rdev->wiphy.wowlan->tcp->seq)
13510 			return -EINVAL;
13511 		if (seq->len == 0 || seq->len > 4)
13512 			return -EINVAL;
13513 		if (seq->len + seq->offset > data_size)
13514 			return -EINVAL;
13515 	}
13516 
13517 	size = sizeof(*cfg);
13518 	size += data_size;
13519 	size += wake_size + wake_mask_size;
13520 	size += tokens_size;
13521 
13522 	cfg = kzalloc(size, GFP_KERNEL);
13523 	if (!cfg)
13524 		return -ENOMEM;
13525 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
13526 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
13527 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
13528 	       ETH_ALEN);
13529 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
13530 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
13531 	else
13532 		port = 0;
13533 #ifdef CONFIG_INET
13534 	/* allocate a socket and port for it and use it */
13535 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
13536 			    IPPROTO_TCP, &cfg->sock, 1);
13537 	if (err) {
13538 		kfree(cfg);
13539 		return err;
13540 	}
13541 	if (inet_csk_get_port(cfg->sock->sk, port)) {
13542 		sock_release(cfg->sock);
13543 		kfree(cfg);
13544 		return -EADDRINUSE;
13545 	}
13546 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
13547 #else
13548 	if (!port) {
13549 		kfree(cfg);
13550 		return -EINVAL;
13551 	}
13552 	cfg->src_port = port;
13553 #endif
13554 
13555 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
13556 	cfg->payload_len = data_size;
13557 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
13558 	memcpy((void *)cfg->payload,
13559 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
13560 	       data_size);
13561 	if (seq)
13562 		cfg->payload_seq = *seq;
13563 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
13564 	cfg->wake_len = wake_size;
13565 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
13566 	memcpy((void *)cfg->wake_data,
13567 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
13568 	       wake_size);
13569 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
13570 			 data_size + wake_size;
13571 	memcpy((void *)cfg->wake_mask,
13572 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
13573 	       wake_mask_size);
13574 	if (tok) {
13575 		cfg->tokens_size = tokens_size;
13576 		cfg->payload_tok = *tok;
13577 		memcpy(cfg->payload_tok.token_stream, tok->token_stream,
13578 		       tokens_size);
13579 	}
13580 
13581 	trig->tcp = cfg;
13582 
13583 	return 0;
13584 }
13585 
13586 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
13587 				   const struct wiphy_wowlan_support *wowlan,
13588 				   struct nlattr *attr,
13589 				   struct cfg80211_wowlan *trig)
13590 {
13591 	struct nlattr **tb;
13592 	int err;
13593 
13594 	tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
13595 	if (!tb)
13596 		return -ENOMEM;
13597 
13598 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
13599 		err = -EOPNOTSUPP;
13600 		goto out;
13601 	}
13602 
13603 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
13604 					  nl80211_policy, NULL);
13605 	if (err)
13606 		goto out;
13607 
13608 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
13609 						   wowlan->max_nd_match_sets);
13610 	err = PTR_ERR_OR_ZERO(trig->nd_config);
13611 	if (err)
13612 		trig->nd_config = NULL;
13613 
13614 out:
13615 	kfree(tb);
13616 	return err;
13617 }
13618 
13619 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
13620 {
13621 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13622 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
13623 	struct cfg80211_wowlan new_triggers = {};
13624 	struct cfg80211_wowlan *ntrig;
13625 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
13626 	int err, i;
13627 	bool prev_enabled = rdev->wiphy.wowlan_config;
13628 	bool regular = false;
13629 
13630 	if (!wowlan)
13631 		return -EOPNOTSUPP;
13632 
13633 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
13634 		cfg80211_rdev_free_wowlan(rdev);
13635 		rdev->wiphy.wowlan_config = NULL;
13636 		goto set_wakeup;
13637 	}
13638 
13639 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
13640 					  info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
13641 					  nl80211_wowlan_policy, info->extack);
13642 	if (err)
13643 		return err;
13644 
13645 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
13646 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
13647 			return -EINVAL;
13648 		new_triggers.any = true;
13649 	}
13650 
13651 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
13652 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
13653 			return -EINVAL;
13654 		new_triggers.disconnect = true;
13655 		regular = true;
13656 	}
13657 
13658 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
13659 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
13660 			return -EINVAL;
13661 		new_triggers.magic_pkt = true;
13662 		regular = true;
13663 	}
13664 
13665 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
13666 		return -EINVAL;
13667 
13668 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
13669 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
13670 			return -EINVAL;
13671 		new_triggers.gtk_rekey_failure = true;
13672 		regular = true;
13673 	}
13674 
13675 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
13676 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
13677 			return -EINVAL;
13678 		new_triggers.eap_identity_req = true;
13679 		regular = true;
13680 	}
13681 
13682 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
13683 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
13684 			return -EINVAL;
13685 		new_triggers.four_way_handshake = true;
13686 		regular = true;
13687 	}
13688 
13689 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
13690 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
13691 			return -EINVAL;
13692 		new_triggers.rfkill_release = true;
13693 		regular = true;
13694 	}
13695 
13696 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
13697 		struct nlattr *pat;
13698 		int n_patterns = 0;
13699 		int rem, pat_len, mask_len, pkt_offset;
13700 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13701 
13702 		regular = true;
13703 
13704 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13705 				    rem)
13706 			n_patterns++;
13707 		if (n_patterns > wowlan->n_patterns)
13708 			return -EINVAL;
13709 
13710 		new_triggers.patterns = kcalloc(n_patterns,
13711 						sizeof(new_triggers.patterns[0]),
13712 						GFP_KERNEL);
13713 		if (!new_triggers.patterns)
13714 			return -ENOMEM;
13715 
13716 		new_triggers.n_patterns = n_patterns;
13717 		i = 0;
13718 
13719 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13720 				    rem) {
13721 			u8 *mask_pat;
13722 
13723 			err = nla_parse_nested_deprecated(pat_tb,
13724 							  MAX_NL80211_PKTPAT,
13725 							  pat,
13726 							  nl80211_packet_pattern_policy,
13727 							  info->extack);
13728 			if (err)
13729 				goto error;
13730 
13731 			err = -EINVAL;
13732 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
13733 			    !pat_tb[NL80211_PKTPAT_PATTERN])
13734 				goto error;
13735 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13736 			mask_len = DIV_ROUND_UP(pat_len, 8);
13737 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13738 				goto error;
13739 			if (pat_len > wowlan->pattern_max_len ||
13740 			    pat_len < wowlan->pattern_min_len)
13741 				goto error;
13742 
13743 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
13744 				pkt_offset = 0;
13745 			else
13746 				pkt_offset = nla_get_u32(
13747 					pat_tb[NL80211_PKTPAT_OFFSET]);
13748 			if (pkt_offset > wowlan->max_pkt_offset)
13749 				goto error;
13750 			new_triggers.patterns[i].pkt_offset = pkt_offset;
13751 
13752 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13753 			if (!mask_pat) {
13754 				err = -ENOMEM;
13755 				goto error;
13756 			}
13757 			new_triggers.patterns[i].mask = mask_pat;
13758 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13759 			       mask_len);
13760 			mask_pat += mask_len;
13761 			new_triggers.patterns[i].pattern = mask_pat;
13762 			new_triggers.patterns[i].pattern_len = pat_len;
13763 			memcpy(mask_pat,
13764 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13765 			       pat_len);
13766 			i++;
13767 		}
13768 	}
13769 
13770 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
13771 		regular = true;
13772 		err = nl80211_parse_wowlan_tcp(
13773 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
13774 			&new_triggers);
13775 		if (err)
13776 			goto error;
13777 	}
13778 
13779 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
13780 		regular = true;
13781 		err = nl80211_parse_wowlan_nd(
13782 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
13783 			&new_triggers);
13784 		if (err)
13785 			goto error;
13786 	}
13787 
13788 	/* The 'any' trigger means the device continues operating more or less
13789 	 * as in its normal operation mode and wakes up the host on most of the
13790 	 * normal interrupts (like packet RX, ...)
13791 	 * It therefore makes little sense to combine with the more constrained
13792 	 * wakeup trigger modes.
13793 	 */
13794 	if (new_triggers.any && regular) {
13795 		err = -EINVAL;
13796 		goto error;
13797 	}
13798 
13799 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
13800 	if (!ntrig) {
13801 		err = -ENOMEM;
13802 		goto error;
13803 	}
13804 	cfg80211_rdev_free_wowlan(rdev);
13805 	rdev->wiphy.wowlan_config = ntrig;
13806 
13807  set_wakeup:
13808 	if (rdev->ops->set_wakeup &&
13809 	    prev_enabled != !!rdev->wiphy.wowlan_config)
13810 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
13811 
13812 	return 0;
13813  error:
13814 	for (i = 0; i < new_triggers.n_patterns; i++)
13815 		kfree(new_triggers.patterns[i].mask);
13816 	kfree(new_triggers.patterns);
13817 	if (new_triggers.tcp && new_triggers.tcp->sock)
13818 		sock_release(new_triggers.tcp->sock);
13819 	kfree(new_triggers.tcp);
13820 	kfree(new_triggers.nd_config);
13821 	return err;
13822 }
13823 #endif
13824 
13825 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
13826 				       struct cfg80211_registered_device *rdev)
13827 {
13828 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
13829 	int i, j, pat_len;
13830 	struct cfg80211_coalesce_rules *rule;
13831 
13832 	if (!rdev->coalesce->n_rules)
13833 		return 0;
13834 
13835 	nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
13836 	if (!nl_rules)
13837 		return -ENOBUFS;
13838 
13839 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
13840 		nl_rule = nla_nest_start_noflag(msg, i + 1);
13841 		if (!nl_rule)
13842 			return -ENOBUFS;
13843 
13844 		rule = &rdev->coalesce->rules[i];
13845 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
13846 				rule->delay))
13847 			return -ENOBUFS;
13848 
13849 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
13850 				rule->condition))
13851 			return -ENOBUFS;
13852 
13853 		nl_pats = nla_nest_start_noflag(msg,
13854 						NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
13855 		if (!nl_pats)
13856 			return -ENOBUFS;
13857 
13858 		for (j = 0; j < rule->n_patterns; j++) {
13859 			nl_pat = nla_nest_start_noflag(msg, j + 1);
13860 			if (!nl_pat)
13861 				return -ENOBUFS;
13862 			pat_len = rule->patterns[j].pattern_len;
13863 			if (nla_put(msg, NL80211_PKTPAT_MASK,
13864 				    DIV_ROUND_UP(pat_len, 8),
13865 				    rule->patterns[j].mask) ||
13866 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
13867 				    rule->patterns[j].pattern) ||
13868 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
13869 					rule->patterns[j].pkt_offset))
13870 				return -ENOBUFS;
13871 			nla_nest_end(msg, nl_pat);
13872 		}
13873 		nla_nest_end(msg, nl_pats);
13874 		nla_nest_end(msg, nl_rule);
13875 	}
13876 	nla_nest_end(msg, nl_rules);
13877 
13878 	return 0;
13879 }
13880 
13881 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
13882 {
13883 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13884 	struct sk_buff *msg;
13885 	void *hdr;
13886 
13887 	if (!rdev->wiphy.coalesce)
13888 		return -EOPNOTSUPP;
13889 
13890 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13891 	if (!msg)
13892 		return -ENOMEM;
13893 
13894 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13895 			     NL80211_CMD_GET_COALESCE);
13896 	if (!hdr)
13897 		goto nla_put_failure;
13898 
13899 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
13900 		goto nla_put_failure;
13901 
13902 	genlmsg_end(msg, hdr);
13903 	return genlmsg_reply(msg, info);
13904 
13905 nla_put_failure:
13906 	nlmsg_free(msg);
13907 	return -ENOBUFS;
13908 }
13909 
13910 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
13911 {
13912 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
13913 	int i, j;
13914 	struct cfg80211_coalesce_rules *rule;
13915 
13916 	if (!coalesce)
13917 		return;
13918 
13919 	for (i = 0; i < coalesce->n_rules; i++) {
13920 		rule = &coalesce->rules[i];
13921 		for (j = 0; j < rule->n_patterns; j++)
13922 			kfree(rule->patterns[j].mask);
13923 		kfree(rule->patterns);
13924 	}
13925 	kfree(coalesce->rules);
13926 	kfree(coalesce);
13927 	rdev->coalesce = NULL;
13928 }
13929 
13930 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
13931 				       struct nlattr *rule,
13932 				       struct cfg80211_coalesce_rules *new_rule)
13933 {
13934 	int err, i;
13935 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13936 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
13937 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
13938 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13939 
13940 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
13941 					  rule, nl80211_coalesce_policy, NULL);
13942 	if (err)
13943 		return err;
13944 
13945 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
13946 		new_rule->delay =
13947 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
13948 	if (new_rule->delay > coalesce->max_delay)
13949 		return -EINVAL;
13950 
13951 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
13952 		new_rule->condition =
13953 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
13954 
13955 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
13956 		return -EINVAL;
13957 
13958 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13959 			    rem)
13960 		n_patterns++;
13961 	if (n_patterns > coalesce->n_patterns)
13962 		return -EINVAL;
13963 
13964 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
13965 				     GFP_KERNEL);
13966 	if (!new_rule->patterns)
13967 		return -ENOMEM;
13968 
13969 	new_rule->n_patterns = n_patterns;
13970 	i = 0;
13971 
13972 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13973 			    rem) {
13974 		u8 *mask_pat;
13975 
13976 		err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
13977 						  pat,
13978 						  nl80211_packet_pattern_policy,
13979 						  NULL);
13980 		if (err)
13981 			return err;
13982 
13983 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
13984 		    !pat_tb[NL80211_PKTPAT_PATTERN])
13985 			return -EINVAL;
13986 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13987 		mask_len = DIV_ROUND_UP(pat_len, 8);
13988 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13989 			return -EINVAL;
13990 		if (pat_len > coalesce->pattern_max_len ||
13991 		    pat_len < coalesce->pattern_min_len)
13992 			return -EINVAL;
13993 
13994 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
13995 			pkt_offset = 0;
13996 		else
13997 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
13998 		if (pkt_offset > coalesce->max_pkt_offset)
13999 			return -EINVAL;
14000 		new_rule->patterns[i].pkt_offset = pkt_offset;
14001 
14002 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
14003 		if (!mask_pat)
14004 			return -ENOMEM;
14005 
14006 		new_rule->patterns[i].mask = mask_pat;
14007 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
14008 		       mask_len);
14009 
14010 		mask_pat += mask_len;
14011 		new_rule->patterns[i].pattern = mask_pat;
14012 		new_rule->patterns[i].pattern_len = pat_len;
14013 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
14014 		       pat_len);
14015 		i++;
14016 	}
14017 
14018 	return 0;
14019 }
14020 
14021 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
14022 {
14023 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14024 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
14025 	struct cfg80211_coalesce new_coalesce = {};
14026 	struct cfg80211_coalesce *n_coalesce;
14027 	int err, rem_rule, n_rules = 0, i, j;
14028 	struct nlattr *rule;
14029 	struct cfg80211_coalesce_rules *tmp_rule;
14030 
14031 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
14032 		return -EOPNOTSUPP;
14033 
14034 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
14035 		cfg80211_rdev_free_coalesce(rdev);
14036 		rdev_set_coalesce(rdev, NULL);
14037 		return 0;
14038 	}
14039 
14040 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
14041 			    rem_rule)
14042 		n_rules++;
14043 	if (n_rules > coalesce->n_rules)
14044 		return -EINVAL;
14045 
14046 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
14047 				     GFP_KERNEL);
14048 	if (!new_coalesce.rules)
14049 		return -ENOMEM;
14050 
14051 	new_coalesce.n_rules = n_rules;
14052 	i = 0;
14053 
14054 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
14055 			    rem_rule) {
14056 		err = nl80211_parse_coalesce_rule(rdev, rule,
14057 						  &new_coalesce.rules[i]);
14058 		if (err)
14059 			goto error;
14060 
14061 		i++;
14062 	}
14063 
14064 	err = rdev_set_coalesce(rdev, &new_coalesce);
14065 	if (err)
14066 		goto error;
14067 
14068 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
14069 	if (!n_coalesce) {
14070 		err = -ENOMEM;
14071 		goto error;
14072 	}
14073 	cfg80211_rdev_free_coalesce(rdev);
14074 	rdev->coalesce = n_coalesce;
14075 
14076 	return 0;
14077 error:
14078 	for (i = 0; i < new_coalesce.n_rules; i++) {
14079 		tmp_rule = &new_coalesce.rules[i];
14080 		if (!tmp_rule)
14081 			continue;
14082 		for (j = 0; j < tmp_rule->n_patterns; j++)
14083 			kfree(tmp_rule->patterns[j].mask);
14084 		kfree(tmp_rule->patterns);
14085 	}
14086 	kfree(new_coalesce.rules);
14087 
14088 	return err;
14089 }
14090 
14091 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
14092 {
14093 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14094 	struct net_device *dev = info->user_ptr[1];
14095 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14096 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
14097 	struct cfg80211_gtk_rekey_data rekey_data = {};
14098 	int err;
14099 
14100 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
14101 		return -EINVAL;
14102 
14103 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
14104 					  info->attrs[NL80211_ATTR_REKEY_DATA],
14105 					  nl80211_rekey_policy, info->extack);
14106 	if (err)
14107 		return err;
14108 
14109 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
14110 	    !tb[NL80211_REKEY_DATA_KCK])
14111 		return -EINVAL;
14112 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN &&
14113 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
14114 	      nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KEK_EXT_LEN))
14115 		return -ERANGE;
14116 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN &&
14117 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
14118 	      nla_len(tb[NL80211_REKEY_DATA_KCK]) == NL80211_KCK_EXT_LEN) &&
14119 	     !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KCK_32 &&
14120 	       nla_len(tb[NL80211_REKEY_DATA_KCK]) == NL80211_KCK_EXT_LEN_32))
14121 		return -ERANGE;
14122 
14123 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
14124 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
14125 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
14126 	rekey_data.kek_len = nla_len(tb[NL80211_REKEY_DATA_KEK]);
14127 	rekey_data.kck_len = nla_len(tb[NL80211_REKEY_DATA_KCK]);
14128 	if (tb[NL80211_REKEY_DATA_AKM])
14129 		rekey_data.akm = nla_get_u32(tb[NL80211_REKEY_DATA_AKM]);
14130 
14131 	wdev_lock(wdev);
14132 	if (!wdev->connected) {
14133 		err = -ENOTCONN;
14134 		goto out;
14135 	}
14136 
14137 	if (!rdev->ops->set_rekey_data) {
14138 		err = -EOPNOTSUPP;
14139 		goto out;
14140 	}
14141 
14142 	err = rdev_set_rekey_data(rdev, dev, &rekey_data);
14143  out:
14144 	wdev_unlock(wdev);
14145 	return err;
14146 }
14147 
14148 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
14149 					     struct genl_info *info)
14150 {
14151 	struct net_device *dev = info->user_ptr[1];
14152 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14153 
14154 	if (wdev->iftype != NL80211_IFTYPE_AP &&
14155 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
14156 		return -EINVAL;
14157 
14158 	if (wdev->ap_unexpected_nlportid)
14159 		return -EBUSY;
14160 
14161 	wdev->ap_unexpected_nlportid = info->snd_portid;
14162 	return 0;
14163 }
14164 
14165 static int nl80211_probe_client(struct sk_buff *skb,
14166 				struct genl_info *info)
14167 {
14168 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14169 	struct net_device *dev = info->user_ptr[1];
14170 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14171 	struct sk_buff *msg;
14172 	void *hdr;
14173 	const u8 *addr;
14174 	u64 cookie;
14175 	int err;
14176 
14177 	if (wdev->iftype != NL80211_IFTYPE_AP &&
14178 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
14179 		return -EOPNOTSUPP;
14180 
14181 	if (!info->attrs[NL80211_ATTR_MAC])
14182 		return -EINVAL;
14183 
14184 	if (!rdev->ops->probe_client)
14185 		return -EOPNOTSUPP;
14186 
14187 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14188 	if (!msg)
14189 		return -ENOMEM;
14190 
14191 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14192 			     NL80211_CMD_PROBE_CLIENT);
14193 	if (!hdr) {
14194 		err = -ENOBUFS;
14195 		goto free_msg;
14196 	}
14197 
14198 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
14199 
14200 	err = rdev_probe_client(rdev, dev, addr, &cookie);
14201 	if (err)
14202 		goto free_msg;
14203 
14204 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14205 			      NL80211_ATTR_PAD))
14206 		goto nla_put_failure;
14207 
14208 	genlmsg_end(msg, hdr);
14209 
14210 	return genlmsg_reply(msg, info);
14211 
14212  nla_put_failure:
14213 	err = -ENOBUFS;
14214  free_msg:
14215 	nlmsg_free(msg);
14216 	return err;
14217 }
14218 
14219 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
14220 {
14221 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14222 	struct cfg80211_beacon_registration *reg, *nreg;
14223 	int rv;
14224 
14225 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
14226 		return -EOPNOTSUPP;
14227 
14228 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
14229 	if (!nreg)
14230 		return -ENOMEM;
14231 
14232 	/* First, check if already registered. */
14233 	spin_lock_bh(&rdev->beacon_registrations_lock);
14234 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
14235 		if (reg->nlportid == info->snd_portid) {
14236 			rv = -EALREADY;
14237 			goto out_err;
14238 		}
14239 	}
14240 	/* Add it to the list */
14241 	nreg->nlportid = info->snd_portid;
14242 	list_add(&nreg->list, &rdev->beacon_registrations);
14243 
14244 	spin_unlock_bh(&rdev->beacon_registrations_lock);
14245 
14246 	return 0;
14247 out_err:
14248 	spin_unlock_bh(&rdev->beacon_registrations_lock);
14249 	kfree(nreg);
14250 	return rv;
14251 }
14252 
14253 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
14254 {
14255 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14256 	struct wireless_dev *wdev = info->user_ptr[1];
14257 	int err;
14258 
14259 	if (!rdev->ops->start_p2p_device)
14260 		return -EOPNOTSUPP;
14261 
14262 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
14263 		return -EOPNOTSUPP;
14264 
14265 	if (wdev_running(wdev))
14266 		return 0;
14267 
14268 	if (rfkill_blocked(rdev->wiphy.rfkill))
14269 		return -ERFKILL;
14270 
14271 	err = rdev_start_p2p_device(rdev, wdev);
14272 	if (err)
14273 		return err;
14274 
14275 	wdev->is_running = true;
14276 	rdev->opencount++;
14277 
14278 	return 0;
14279 }
14280 
14281 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
14282 {
14283 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14284 	struct wireless_dev *wdev = info->user_ptr[1];
14285 
14286 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
14287 		return -EOPNOTSUPP;
14288 
14289 	if (!rdev->ops->stop_p2p_device)
14290 		return -EOPNOTSUPP;
14291 
14292 	cfg80211_stop_p2p_device(rdev, wdev);
14293 
14294 	return 0;
14295 }
14296 
14297 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
14298 {
14299 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14300 	struct wireless_dev *wdev = info->user_ptr[1];
14301 	struct cfg80211_nan_conf conf = {};
14302 	int err;
14303 
14304 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14305 		return -EOPNOTSUPP;
14306 
14307 	if (wdev_running(wdev))
14308 		return -EEXIST;
14309 
14310 	if (rfkill_blocked(rdev->wiphy.rfkill))
14311 		return -ERFKILL;
14312 
14313 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
14314 		return -EINVAL;
14315 
14316 	conf.master_pref =
14317 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14318 
14319 	if (info->attrs[NL80211_ATTR_BANDS]) {
14320 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14321 
14322 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14323 			return -EOPNOTSUPP;
14324 
14325 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14326 			return -EINVAL;
14327 
14328 		conf.bands = bands;
14329 	}
14330 
14331 	err = rdev_start_nan(rdev, wdev, &conf);
14332 	if (err)
14333 		return err;
14334 
14335 	wdev->is_running = true;
14336 	rdev->opencount++;
14337 
14338 	return 0;
14339 }
14340 
14341 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
14342 {
14343 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14344 	struct wireless_dev *wdev = info->user_ptr[1];
14345 
14346 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14347 		return -EOPNOTSUPP;
14348 
14349 	cfg80211_stop_nan(rdev, wdev);
14350 
14351 	return 0;
14352 }
14353 
14354 static int validate_nan_filter(struct nlattr *filter_attr)
14355 {
14356 	struct nlattr *attr;
14357 	int len = 0, n_entries = 0, rem;
14358 
14359 	nla_for_each_nested(attr, filter_attr, rem) {
14360 		len += nla_len(attr);
14361 		n_entries++;
14362 	}
14363 
14364 	if (len >= U8_MAX)
14365 		return -EINVAL;
14366 
14367 	return n_entries;
14368 }
14369 
14370 static int handle_nan_filter(struct nlattr *attr_filter,
14371 			     struct cfg80211_nan_func *func,
14372 			     bool tx)
14373 {
14374 	struct nlattr *attr;
14375 	int n_entries, rem, i;
14376 	struct cfg80211_nan_func_filter *filter;
14377 
14378 	n_entries = validate_nan_filter(attr_filter);
14379 	if (n_entries < 0)
14380 		return n_entries;
14381 
14382 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
14383 
14384 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
14385 	if (!filter)
14386 		return -ENOMEM;
14387 
14388 	i = 0;
14389 	nla_for_each_nested(attr, attr_filter, rem) {
14390 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
14391 		if (!filter[i].filter)
14392 			goto err;
14393 
14394 		filter[i].len = nla_len(attr);
14395 		i++;
14396 	}
14397 	if (tx) {
14398 		func->num_tx_filters = n_entries;
14399 		func->tx_filters = filter;
14400 	} else {
14401 		func->num_rx_filters = n_entries;
14402 		func->rx_filters = filter;
14403 	}
14404 
14405 	return 0;
14406 
14407 err:
14408 	i = 0;
14409 	nla_for_each_nested(attr, attr_filter, rem) {
14410 		kfree(filter[i].filter);
14411 		i++;
14412 	}
14413 	kfree(filter);
14414 	return -ENOMEM;
14415 }
14416 
14417 static int nl80211_nan_add_func(struct sk_buff *skb,
14418 				struct genl_info *info)
14419 {
14420 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14421 	struct wireless_dev *wdev = info->user_ptr[1];
14422 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
14423 	struct cfg80211_nan_func *func;
14424 	struct sk_buff *msg = NULL;
14425 	void *hdr = NULL;
14426 	int err = 0;
14427 
14428 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14429 		return -EOPNOTSUPP;
14430 
14431 	if (!wdev_running(wdev))
14432 		return -ENOTCONN;
14433 
14434 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
14435 		return -EINVAL;
14436 
14437 	err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
14438 					  info->attrs[NL80211_ATTR_NAN_FUNC],
14439 					  nl80211_nan_func_policy,
14440 					  info->extack);
14441 	if (err)
14442 		return err;
14443 
14444 	func = kzalloc(sizeof(*func), GFP_KERNEL);
14445 	if (!func)
14446 		return -ENOMEM;
14447 
14448 	func->cookie = cfg80211_assign_cookie(rdev);
14449 
14450 	if (!tb[NL80211_NAN_FUNC_TYPE]) {
14451 		err = -EINVAL;
14452 		goto out;
14453 	}
14454 
14455 
14456 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
14457 
14458 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
14459 		err = -EINVAL;
14460 		goto out;
14461 	}
14462 
14463 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
14464 	       sizeof(func->service_id));
14465 
14466 	func->close_range =
14467 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
14468 
14469 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
14470 		func->serv_spec_info_len =
14471 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
14472 		func->serv_spec_info =
14473 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
14474 				func->serv_spec_info_len,
14475 				GFP_KERNEL);
14476 		if (!func->serv_spec_info) {
14477 			err = -ENOMEM;
14478 			goto out;
14479 		}
14480 	}
14481 
14482 	if (tb[NL80211_NAN_FUNC_TTL])
14483 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
14484 
14485 	switch (func->type) {
14486 	case NL80211_NAN_FUNC_PUBLISH:
14487 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
14488 			err = -EINVAL;
14489 			goto out;
14490 		}
14491 
14492 		func->publish_type =
14493 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
14494 		func->publish_bcast =
14495 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
14496 
14497 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
14498 			func->publish_bcast) {
14499 			err = -EINVAL;
14500 			goto out;
14501 		}
14502 		break;
14503 	case NL80211_NAN_FUNC_SUBSCRIBE:
14504 		func->subscribe_active =
14505 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
14506 		break;
14507 	case NL80211_NAN_FUNC_FOLLOW_UP:
14508 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
14509 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
14510 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
14511 			err = -EINVAL;
14512 			goto out;
14513 		}
14514 
14515 		func->followup_id =
14516 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
14517 		func->followup_reqid =
14518 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
14519 		memcpy(func->followup_dest.addr,
14520 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
14521 		       sizeof(func->followup_dest.addr));
14522 		if (func->ttl) {
14523 			err = -EINVAL;
14524 			goto out;
14525 		}
14526 		break;
14527 	default:
14528 		err = -EINVAL;
14529 		goto out;
14530 	}
14531 
14532 	if (tb[NL80211_NAN_FUNC_SRF]) {
14533 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
14534 
14535 		err = nla_parse_nested_deprecated(srf_tb,
14536 						  NL80211_NAN_SRF_ATTR_MAX,
14537 						  tb[NL80211_NAN_FUNC_SRF],
14538 						  nl80211_nan_srf_policy,
14539 						  info->extack);
14540 		if (err)
14541 			goto out;
14542 
14543 		func->srf_include =
14544 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
14545 
14546 		if (srf_tb[NL80211_NAN_SRF_BF]) {
14547 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
14548 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
14549 				err = -EINVAL;
14550 				goto out;
14551 			}
14552 
14553 			func->srf_bf_len =
14554 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
14555 			func->srf_bf =
14556 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
14557 					func->srf_bf_len, GFP_KERNEL);
14558 			if (!func->srf_bf) {
14559 				err = -ENOMEM;
14560 				goto out;
14561 			}
14562 
14563 			func->srf_bf_idx =
14564 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
14565 		} else {
14566 			struct nlattr *attr, *mac_attr =
14567 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
14568 			int n_entries, rem, i = 0;
14569 
14570 			if (!mac_attr) {
14571 				err = -EINVAL;
14572 				goto out;
14573 			}
14574 
14575 			n_entries = validate_acl_mac_addrs(mac_attr);
14576 			if (n_entries <= 0) {
14577 				err = -EINVAL;
14578 				goto out;
14579 			}
14580 
14581 			func->srf_num_macs = n_entries;
14582 			func->srf_macs =
14583 				kcalloc(n_entries, sizeof(*func->srf_macs),
14584 					GFP_KERNEL);
14585 			if (!func->srf_macs) {
14586 				err = -ENOMEM;
14587 				goto out;
14588 			}
14589 
14590 			nla_for_each_nested(attr, mac_attr, rem)
14591 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
14592 				       sizeof(*func->srf_macs));
14593 		}
14594 	}
14595 
14596 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
14597 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
14598 					func, true);
14599 		if (err)
14600 			goto out;
14601 	}
14602 
14603 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
14604 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
14605 					func, false);
14606 		if (err)
14607 			goto out;
14608 	}
14609 
14610 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14611 	if (!msg) {
14612 		err = -ENOMEM;
14613 		goto out;
14614 	}
14615 
14616 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14617 			     NL80211_CMD_ADD_NAN_FUNCTION);
14618 	/* This can't really happen - we just allocated 4KB */
14619 	if (WARN_ON(!hdr)) {
14620 		err = -ENOMEM;
14621 		goto out;
14622 	}
14623 
14624 	err = rdev_add_nan_func(rdev, wdev, func);
14625 out:
14626 	if (err < 0) {
14627 		cfg80211_free_nan_func(func);
14628 		nlmsg_free(msg);
14629 		return err;
14630 	}
14631 
14632 	/* propagate the instance id and cookie to userspace  */
14633 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
14634 			      NL80211_ATTR_PAD))
14635 		goto nla_put_failure;
14636 
14637 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14638 	if (!func_attr)
14639 		goto nla_put_failure;
14640 
14641 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
14642 		       func->instance_id))
14643 		goto nla_put_failure;
14644 
14645 	nla_nest_end(msg, func_attr);
14646 
14647 	genlmsg_end(msg, hdr);
14648 	return genlmsg_reply(msg, info);
14649 
14650 nla_put_failure:
14651 	nlmsg_free(msg);
14652 	return -ENOBUFS;
14653 }
14654 
14655 static int nl80211_nan_del_func(struct sk_buff *skb,
14656 			       struct genl_info *info)
14657 {
14658 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14659 	struct wireless_dev *wdev = info->user_ptr[1];
14660 	u64 cookie;
14661 
14662 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14663 		return -EOPNOTSUPP;
14664 
14665 	if (!wdev_running(wdev))
14666 		return -ENOTCONN;
14667 
14668 	if (!info->attrs[NL80211_ATTR_COOKIE])
14669 		return -EINVAL;
14670 
14671 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
14672 
14673 	rdev_del_nan_func(rdev, wdev, cookie);
14674 
14675 	return 0;
14676 }
14677 
14678 static int nl80211_nan_change_config(struct sk_buff *skb,
14679 				     struct genl_info *info)
14680 {
14681 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14682 	struct wireless_dev *wdev = info->user_ptr[1];
14683 	struct cfg80211_nan_conf conf = {};
14684 	u32 changed = 0;
14685 
14686 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14687 		return -EOPNOTSUPP;
14688 
14689 	if (!wdev_running(wdev))
14690 		return -ENOTCONN;
14691 
14692 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
14693 		conf.master_pref =
14694 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14695 		if (conf.master_pref <= 1 || conf.master_pref == 255)
14696 			return -EINVAL;
14697 
14698 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
14699 	}
14700 
14701 	if (info->attrs[NL80211_ATTR_BANDS]) {
14702 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14703 
14704 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14705 			return -EOPNOTSUPP;
14706 
14707 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14708 			return -EINVAL;
14709 
14710 		conf.bands = bands;
14711 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
14712 	}
14713 
14714 	if (!changed)
14715 		return -EINVAL;
14716 
14717 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
14718 }
14719 
14720 void cfg80211_nan_match(struct wireless_dev *wdev,
14721 			struct cfg80211_nan_match_params *match, gfp_t gfp)
14722 {
14723 	struct wiphy *wiphy = wdev->wiphy;
14724 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14725 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
14726 	struct sk_buff *msg;
14727 	void *hdr;
14728 
14729 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
14730 		return;
14731 
14732 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14733 	if (!msg)
14734 		return;
14735 
14736 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
14737 	if (!hdr) {
14738 		nlmsg_free(msg);
14739 		return;
14740 	}
14741 
14742 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14743 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14744 					 wdev->netdev->ifindex)) ||
14745 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14746 			      NL80211_ATTR_PAD))
14747 		goto nla_put_failure;
14748 
14749 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
14750 			      NL80211_ATTR_PAD) ||
14751 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
14752 		goto nla_put_failure;
14753 
14754 	match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
14755 	if (!match_attr)
14756 		goto nla_put_failure;
14757 
14758 	local_func_attr = nla_nest_start_noflag(msg,
14759 						NL80211_NAN_MATCH_FUNC_LOCAL);
14760 	if (!local_func_attr)
14761 		goto nla_put_failure;
14762 
14763 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
14764 		goto nla_put_failure;
14765 
14766 	nla_nest_end(msg, local_func_attr);
14767 
14768 	peer_func_attr = nla_nest_start_noflag(msg,
14769 					       NL80211_NAN_MATCH_FUNC_PEER);
14770 	if (!peer_func_attr)
14771 		goto nla_put_failure;
14772 
14773 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
14774 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
14775 		goto nla_put_failure;
14776 
14777 	if (match->info && match->info_len &&
14778 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
14779 		    match->info))
14780 		goto nla_put_failure;
14781 
14782 	nla_nest_end(msg, peer_func_attr);
14783 	nla_nest_end(msg, match_attr);
14784 	genlmsg_end(msg, hdr);
14785 
14786 	if (!wdev->owner_nlportid)
14787 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14788 					msg, 0, NL80211_MCGRP_NAN, gfp);
14789 	else
14790 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14791 				wdev->owner_nlportid);
14792 
14793 	return;
14794 
14795 nla_put_failure:
14796 	nlmsg_free(msg);
14797 }
14798 EXPORT_SYMBOL(cfg80211_nan_match);
14799 
14800 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
14801 				  u8 inst_id,
14802 				  enum nl80211_nan_func_term_reason reason,
14803 				  u64 cookie, gfp_t gfp)
14804 {
14805 	struct wiphy *wiphy = wdev->wiphy;
14806 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14807 	struct sk_buff *msg;
14808 	struct nlattr *func_attr;
14809 	void *hdr;
14810 
14811 	if (WARN_ON(!inst_id))
14812 		return;
14813 
14814 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14815 	if (!msg)
14816 		return;
14817 
14818 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
14819 	if (!hdr) {
14820 		nlmsg_free(msg);
14821 		return;
14822 	}
14823 
14824 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14825 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14826 					 wdev->netdev->ifindex)) ||
14827 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14828 			      NL80211_ATTR_PAD))
14829 		goto nla_put_failure;
14830 
14831 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14832 			      NL80211_ATTR_PAD))
14833 		goto nla_put_failure;
14834 
14835 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14836 	if (!func_attr)
14837 		goto nla_put_failure;
14838 
14839 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
14840 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
14841 		goto nla_put_failure;
14842 
14843 	nla_nest_end(msg, func_attr);
14844 	genlmsg_end(msg, hdr);
14845 
14846 	if (!wdev->owner_nlportid)
14847 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14848 					msg, 0, NL80211_MCGRP_NAN, gfp);
14849 	else
14850 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14851 				wdev->owner_nlportid);
14852 
14853 	return;
14854 
14855 nla_put_failure:
14856 	nlmsg_free(msg);
14857 }
14858 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
14859 
14860 static int nl80211_get_protocol_features(struct sk_buff *skb,
14861 					 struct genl_info *info)
14862 {
14863 	void *hdr;
14864 	struct sk_buff *msg;
14865 
14866 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14867 	if (!msg)
14868 		return -ENOMEM;
14869 
14870 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14871 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
14872 	if (!hdr)
14873 		goto nla_put_failure;
14874 
14875 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
14876 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
14877 		goto nla_put_failure;
14878 
14879 	genlmsg_end(msg, hdr);
14880 	return genlmsg_reply(msg, info);
14881 
14882  nla_put_failure:
14883 	kfree_skb(msg);
14884 	return -ENOBUFS;
14885 }
14886 
14887 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
14888 {
14889 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14890 	struct cfg80211_update_ft_ies_params ft_params;
14891 	struct net_device *dev = info->user_ptr[1];
14892 
14893 	if (!rdev->ops->update_ft_ies)
14894 		return -EOPNOTSUPP;
14895 
14896 	if (!info->attrs[NL80211_ATTR_MDID] ||
14897 	    !info->attrs[NL80211_ATTR_IE])
14898 		return -EINVAL;
14899 
14900 	memset(&ft_params, 0, sizeof(ft_params));
14901 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
14902 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
14903 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
14904 
14905 	return rdev_update_ft_ies(rdev, dev, &ft_params);
14906 }
14907 
14908 static int nl80211_crit_protocol_start(struct sk_buff *skb,
14909 				       struct genl_info *info)
14910 {
14911 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14912 	struct wireless_dev *wdev = info->user_ptr[1];
14913 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
14914 	u16 duration;
14915 	int ret;
14916 
14917 	if (!rdev->ops->crit_proto_start)
14918 		return -EOPNOTSUPP;
14919 
14920 	if (WARN_ON(!rdev->ops->crit_proto_stop))
14921 		return -EINVAL;
14922 
14923 	if (rdev->crit_proto_nlportid)
14924 		return -EBUSY;
14925 
14926 	/* determine protocol if provided */
14927 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
14928 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
14929 
14930 	if (proto >= NUM_NL80211_CRIT_PROTO)
14931 		return -EINVAL;
14932 
14933 	/* timeout must be provided */
14934 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
14935 		return -EINVAL;
14936 
14937 	duration =
14938 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
14939 
14940 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
14941 	if (!ret)
14942 		rdev->crit_proto_nlportid = info->snd_portid;
14943 
14944 	return ret;
14945 }
14946 
14947 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
14948 				      struct genl_info *info)
14949 {
14950 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14951 	struct wireless_dev *wdev = info->user_ptr[1];
14952 
14953 	if (!rdev->ops->crit_proto_stop)
14954 		return -EOPNOTSUPP;
14955 
14956 	if (rdev->crit_proto_nlportid) {
14957 		rdev->crit_proto_nlportid = 0;
14958 		rdev_crit_proto_stop(rdev, wdev);
14959 	}
14960 	return 0;
14961 }
14962 
14963 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
14964 				       struct nlattr *attr,
14965 				       struct netlink_ext_ack *extack)
14966 {
14967 	if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
14968 		if (attr->nla_type & NLA_F_NESTED) {
14969 			NL_SET_ERR_MSG_ATTR(extack, attr,
14970 					    "unexpected nested data");
14971 			return -EINVAL;
14972 		}
14973 
14974 		return 0;
14975 	}
14976 
14977 	if (!(attr->nla_type & NLA_F_NESTED)) {
14978 		NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
14979 		return -EINVAL;
14980 	}
14981 
14982 	return nla_validate_nested(attr, vcmd->maxattr, vcmd->policy, extack);
14983 }
14984 
14985 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
14986 {
14987 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14988 	struct wireless_dev *wdev =
14989 		__cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
14990 					   info->attrs);
14991 	int i, err;
14992 	u32 vid, subcmd;
14993 
14994 	if (!rdev->wiphy.vendor_commands)
14995 		return -EOPNOTSUPP;
14996 
14997 	if (IS_ERR(wdev)) {
14998 		err = PTR_ERR(wdev);
14999 		if (err != -EINVAL)
15000 			return err;
15001 		wdev = NULL;
15002 	} else if (wdev->wiphy != &rdev->wiphy) {
15003 		return -EINVAL;
15004 	}
15005 
15006 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
15007 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
15008 		return -EINVAL;
15009 
15010 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
15011 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
15012 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
15013 		const struct wiphy_vendor_command *vcmd;
15014 		void *data = NULL;
15015 		int len = 0;
15016 
15017 		vcmd = &rdev->wiphy.vendor_commands[i];
15018 
15019 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
15020 			continue;
15021 
15022 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
15023 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
15024 			if (!wdev)
15025 				return -EINVAL;
15026 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
15027 			    !wdev->netdev)
15028 				return -EINVAL;
15029 
15030 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
15031 				if (!wdev_running(wdev))
15032 					return -ENETDOWN;
15033 			}
15034 		} else {
15035 			wdev = NULL;
15036 		}
15037 
15038 		if (!vcmd->doit)
15039 			return -EOPNOTSUPP;
15040 
15041 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
15042 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
15043 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
15044 
15045 			err = nl80211_vendor_check_policy(vcmd,
15046 					info->attrs[NL80211_ATTR_VENDOR_DATA],
15047 					info->extack);
15048 			if (err)
15049 				return err;
15050 		}
15051 
15052 		rdev->cur_cmd_info = info;
15053 		err = vcmd->doit(&rdev->wiphy, wdev, data, len);
15054 		rdev->cur_cmd_info = NULL;
15055 		return err;
15056 	}
15057 
15058 	return -EOPNOTSUPP;
15059 }
15060 
15061 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
15062 				       struct netlink_callback *cb,
15063 				       struct cfg80211_registered_device **rdev,
15064 				       struct wireless_dev **wdev)
15065 {
15066 	struct nlattr **attrbuf;
15067 	u32 vid, subcmd;
15068 	unsigned int i;
15069 	int vcmd_idx = -1;
15070 	int err;
15071 	void *data = NULL;
15072 	unsigned int data_len = 0;
15073 
15074 	if (cb->args[0]) {
15075 		/* subtract the 1 again here */
15076 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
15077 		struct wireless_dev *tmp;
15078 
15079 		if (!wiphy)
15080 			return -ENODEV;
15081 		*rdev = wiphy_to_rdev(wiphy);
15082 		*wdev = NULL;
15083 
15084 		if (cb->args[1]) {
15085 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
15086 				if (tmp->identifier == cb->args[1] - 1) {
15087 					*wdev = tmp;
15088 					break;
15089 				}
15090 			}
15091 		}
15092 
15093 		/* keep rtnl locked in successful case */
15094 		return 0;
15095 	}
15096 
15097 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
15098 	if (!attrbuf)
15099 		return -ENOMEM;
15100 
15101 	err = nlmsg_parse_deprecated(cb->nlh,
15102 				     GENL_HDRLEN + nl80211_fam.hdrsize,
15103 				     attrbuf, nl80211_fam.maxattr,
15104 				     nl80211_policy, NULL);
15105 	if (err)
15106 		goto out;
15107 
15108 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
15109 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
15110 		err = -EINVAL;
15111 		goto out;
15112 	}
15113 
15114 	*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(skb->sk), attrbuf);
15115 	if (IS_ERR(*wdev))
15116 		*wdev = NULL;
15117 
15118 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
15119 	if (IS_ERR(*rdev)) {
15120 		err = PTR_ERR(*rdev);
15121 		goto out;
15122 	}
15123 
15124 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
15125 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
15126 
15127 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
15128 		const struct wiphy_vendor_command *vcmd;
15129 
15130 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
15131 
15132 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
15133 			continue;
15134 
15135 		if (!vcmd->dumpit) {
15136 			err = -EOPNOTSUPP;
15137 			goto out;
15138 		}
15139 
15140 		vcmd_idx = i;
15141 		break;
15142 	}
15143 
15144 	if (vcmd_idx < 0) {
15145 		err = -EOPNOTSUPP;
15146 		goto out;
15147 	}
15148 
15149 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
15150 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
15151 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
15152 
15153 		err = nl80211_vendor_check_policy(
15154 				&(*rdev)->wiphy.vendor_commands[vcmd_idx],
15155 				attrbuf[NL80211_ATTR_VENDOR_DATA],
15156 				cb->extack);
15157 		if (err)
15158 			goto out;
15159 	}
15160 
15161 	/* 0 is the first index - add 1 to parse only once */
15162 	cb->args[0] = (*rdev)->wiphy_idx + 1;
15163 	/* add 1 to know if it was NULL */
15164 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
15165 	cb->args[2] = vcmd_idx;
15166 	cb->args[3] = (unsigned long)data;
15167 	cb->args[4] = data_len;
15168 
15169 	/* keep rtnl locked in successful case */
15170 	err = 0;
15171 out:
15172 	kfree(attrbuf);
15173 	return err;
15174 }
15175 
15176 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
15177 				   struct netlink_callback *cb)
15178 {
15179 	struct cfg80211_registered_device *rdev;
15180 	struct wireless_dev *wdev;
15181 	unsigned int vcmd_idx;
15182 	const struct wiphy_vendor_command *vcmd;
15183 	void *data;
15184 	int data_len;
15185 	int err;
15186 	struct nlattr *vendor_data;
15187 
15188 	rtnl_lock();
15189 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
15190 	if (err)
15191 		goto out;
15192 
15193 	vcmd_idx = cb->args[2];
15194 	data = (void *)cb->args[3];
15195 	data_len = cb->args[4];
15196 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
15197 
15198 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
15199 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
15200 		if (!wdev) {
15201 			err = -EINVAL;
15202 			goto out;
15203 		}
15204 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
15205 		    !wdev->netdev) {
15206 			err = -EINVAL;
15207 			goto out;
15208 		}
15209 
15210 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
15211 			if (!wdev_running(wdev)) {
15212 				err = -ENETDOWN;
15213 				goto out;
15214 			}
15215 		}
15216 	}
15217 
15218 	while (1) {
15219 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
15220 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
15221 					   NL80211_CMD_VENDOR);
15222 		if (!hdr)
15223 			break;
15224 
15225 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15226 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
15227 					       wdev_id(wdev),
15228 					       NL80211_ATTR_PAD))) {
15229 			genlmsg_cancel(skb, hdr);
15230 			break;
15231 		}
15232 
15233 		vendor_data = nla_nest_start_noflag(skb,
15234 						    NL80211_ATTR_VENDOR_DATA);
15235 		if (!vendor_data) {
15236 			genlmsg_cancel(skb, hdr);
15237 			break;
15238 		}
15239 
15240 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
15241 				   (unsigned long *)&cb->args[5]);
15242 		nla_nest_end(skb, vendor_data);
15243 
15244 		if (err == -ENOBUFS || err == -ENOENT) {
15245 			genlmsg_cancel(skb, hdr);
15246 			break;
15247 		} else if (err <= 0) {
15248 			genlmsg_cancel(skb, hdr);
15249 			goto out;
15250 		}
15251 
15252 		genlmsg_end(skb, hdr);
15253 	}
15254 
15255 	err = skb->len;
15256  out:
15257 	rtnl_unlock();
15258 	return err;
15259 }
15260 
15261 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
15262 					   enum nl80211_commands cmd,
15263 					   enum nl80211_attrs attr,
15264 					   int approxlen)
15265 {
15266 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15267 
15268 	if (WARN_ON(!rdev->cur_cmd_info))
15269 		return NULL;
15270 
15271 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
15272 					   rdev->cur_cmd_info->snd_portid,
15273 					   rdev->cur_cmd_info->snd_seq,
15274 					   cmd, attr, NULL, GFP_KERNEL);
15275 }
15276 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
15277 
15278 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
15279 {
15280 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
15281 	void *hdr = ((void **)skb->cb)[1];
15282 	struct nlattr *data = ((void **)skb->cb)[2];
15283 
15284 	/* clear CB data for netlink core to own from now on */
15285 	memset(skb->cb, 0, sizeof(skb->cb));
15286 
15287 	if (WARN_ON(!rdev->cur_cmd_info)) {
15288 		kfree_skb(skb);
15289 		return -EINVAL;
15290 	}
15291 
15292 	nla_nest_end(skb, data);
15293 	genlmsg_end(skb, hdr);
15294 	return genlmsg_reply(skb, rdev->cur_cmd_info);
15295 }
15296 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
15297 
15298 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
15299 {
15300 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15301 
15302 	if (WARN_ON(!rdev->cur_cmd_info))
15303 		return 0;
15304 
15305 	return rdev->cur_cmd_info->snd_portid;
15306 }
15307 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
15308 
15309 static int nl80211_set_qos_map(struct sk_buff *skb,
15310 			       struct genl_info *info)
15311 {
15312 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15313 	struct cfg80211_qos_map *qos_map = NULL;
15314 	struct net_device *dev = info->user_ptr[1];
15315 	u8 *pos, len, num_des, des_len, des;
15316 	int ret;
15317 
15318 	if (!rdev->ops->set_qos_map)
15319 		return -EOPNOTSUPP;
15320 
15321 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
15322 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
15323 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
15324 
15325 		if (len % 2)
15326 			return -EINVAL;
15327 
15328 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
15329 		if (!qos_map)
15330 			return -ENOMEM;
15331 
15332 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
15333 		if (num_des) {
15334 			des_len = num_des *
15335 				sizeof(struct cfg80211_dscp_exception);
15336 			memcpy(qos_map->dscp_exception, pos, des_len);
15337 			qos_map->num_des = num_des;
15338 			for (des = 0; des < num_des; des++) {
15339 				if (qos_map->dscp_exception[des].up > 7) {
15340 					kfree(qos_map);
15341 					return -EINVAL;
15342 				}
15343 			}
15344 			pos += des_len;
15345 		}
15346 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
15347 	}
15348 
15349 	wdev_lock(dev->ieee80211_ptr);
15350 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
15351 	if (!ret)
15352 		ret = rdev_set_qos_map(rdev, dev, qos_map);
15353 	wdev_unlock(dev->ieee80211_ptr);
15354 
15355 	kfree(qos_map);
15356 	return ret;
15357 }
15358 
15359 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
15360 {
15361 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15362 	struct net_device *dev = info->user_ptr[1];
15363 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15364 	const u8 *peer;
15365 	u8 tsid, up;
15366 	u16 admitted_time = 0;
15367 	int err;
15368 
15369 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
15370 		return -EOPNOTSUPP;
15371 
15372 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
15373 	    !info->attrs[NL80211_ATTR_USER_PRIO])
15374 		return -EINVAL;
15375 
15376 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15377 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
15378 
15379 	/* WMM uses TIDs 0-7 even for TSPEC */
15380 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
15381 		/* TODO: handle 802.11 TSPEC/admission control
15382 		 * need more attributes for that (e.g. BA session requirement);
15383 		 * change the WMM adminssion test above to allow both then
15384 		 */
15385 		return -EINVAL;
15386 	}
15387 
15388 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15389 
15390 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
15391 		admitted_time =
15392 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
15393 		if (!admitted_time)
15394 			return -EINVAL;
15395 	}
15396 
15397 	wdev_lock(wdev);
15398 	switch (wdev->iftype) {
15399 	case NL80211_IFTYPE_STATION:
15400 	case NL80211_IFTYPE_P2P_CLIENT:
15401 		if (wdev->connected)
15402 			break;
15403 		err = -ENOTCONN;
15404 		goto out;
15405 	default:
15406 		err = -EOPNOTSUPP;
15407 		goto out;
15408 	}
15409 
15410 	err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
15411 
15412  out:
15413 	wdev_unlock(wdev);
15414 	return err;
15415 }
15416 
15417 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
15418 {
15419 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15420 	struct net_device *dev = info->user_ptr[1];
15421 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15422 	const u8 *peer;
15423 	u8 tsid;
15424 	int err;
15425 
15426 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
15427 		return -EINVAL;
15428 
15429 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15430 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15431 
15432 	wdev_lock(wdev);
15433 	err = rdev_del_tx_ts(rdev, dev, tsid, peer);
15434 	wdev_unlock(wdev);
15435 
15436 	return err;
15437 }
15438 
15439 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
15440 				       struct genl_info *info)
15441 {
15442 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15443 	struct net_device *dev = info->user_ptr[1];
15444 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15445 	struct cfg80211_chan_def chandef = {};
15446 	const u8 *addr;
15447 	u8 oper_class;
15448 	int err;
15449 
15450 	if (!rdev->ops->tdls_channel_switch ||
15451 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15452 		return -EOPNOTSUPP;
15453 
15454 	switch (dev->ieee80211_ptr->iftype) {
15455 	case NL80211_IFTYPE_STATION:
15456 	case NL80211_IFTYPE_P2P_CLIENT:
15457 		break;
15458 	default:
15459 		return -EOPNOTSUPP;
15460 	}
15461 
15462 	if (!info->attrs[NL80211_ATTR_MAC] ||
15463 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
15464 		return -EINVAL;
15465 
15466 	err = nl80211_parse_chandef(rdev, info, &chandef);
15467 	if (err)
15468 		return err;
15469 
15470 	/*
15471 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
15472 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
15473 	 * specification is not defined for them.
15474 	 */
15475 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
15476 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
15477 	    chandef.width != NL80211_CHAN_WIDTH_20)
15478 		return -EINVAL;
15479 
15480 	/* we will be active on the TDLS link */
15481 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
15482 					   wdev->iftype))
15483 		return -EINVAL;
15484 
15485 	/* don't allow switching to DFS channels */
15486 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
15487 		return -EINVAL;
15488 
15489 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15490 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
15491 
15492 	wdev_lock(wdev);
15493 	err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
15494 	wdev_unlock(wdev);
15495 
15496 	return err;
15497 }
15498 
15499 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
15500 					      struct genl_info *info)
15501 {
15502 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15503 	struct net_device *dev = info->user_ptr[1];
15504 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15505 	const u8 *addr;
15506 
15507 	if (!rdev->ops->tdls_channel_switch ||
15508 	    !rdev->ops->tdls_cancel_channel_switch ||
15509 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15510 		return -EOPNOTSUPP;
15511 
15512 	switch (dev->ieee80211_ptr->iftype) {
15513 	case NL80211_IFTYPE_STATION:
15514 	case NL80211_IFTYPE_P2P_CLIENT:
15515 		break;
15516 	default:
15517 		return -EOPNOTSUPP;
15518 	}
15519 
15520 	if (!info->attrs[NL80211_ATTR_MAC])
15521 		return -EINVAL;
15522 
15523 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15524 
15525 	wdev_lock(wdev);
15526 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
15527 	wdev_unlock(wdev);
15528 
15529 	return 0;
15530 }
15531 
15532 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
15533 					    struct genl_info *info)
15534 {
15535 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15536 	struct net_device *dev = info->user_ptr[1];
15537 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15538 	const struct nlattr *nla;
15539 	bool enabled;
15540 
15541 	if (!rdev->ops->set_multicast_to_unicast)
15542 		return -EOPNOTSUPP;
15543 
15544 	if (wdev->iftype != NL80211_IFTYPE_AP &&
15545 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
15546 		return -EOPNOTSUPP;
15547 
15548 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
15549 	enabled = nla_get_flag(nla);
15550 
15551 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
15552 }
15553 
15554 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
15555 {
15556 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15557 	struct net_device *dev = info->user_ptr[1];
15558 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15559 	struct cfg80211_pmk_conf pmk_conf = {};
15560 	int ret;
15561 
15562 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15563 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15564 		return -EOPNOTSUPP;
15565 
15566 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15567 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15568 		return -EOPNOTSUPP;
15569 
15570 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
15571 		return -EINVAL;
15572 
15573 	wdev_lock(wdev);
15574 	if (!wdev->connected) {
15575 		ret = -ENOTCONN;
15576 		goto out;
15577 	}
15578 
15579 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15580 	if (memcmp(pmk_conf.aa, wdev->u.client.connected_addr, ETH_ALEN)) {
15581 		ret = -EINVAL;
15582 		goto out;
15583 	}
15584 
15585 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
15586 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
15587 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
15588 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
15589 		ret = -EINVAL;
15590 		goto out;
15591 	}
15592 
15593 	if (info->attrs[NL80211_ATTR_PMKR0_NAME])
15594 		pmk_conf.pmk_r0_name =
15595 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
15596 
15597 	ret = rdev_set_pmk(rdev, dev, &pmk_conf);
15598 out:
15599 	wdev_unlock(wdev);
15600 	return ret;
15601 }
15602 
15603 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
15604 {
15605 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15606 	struct net_device *dev = info->user_ptr[1];
15607 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15608 	const u8 *aa;
15609 	int ret;
15610 
15611 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15612 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15613 		return -EOPNOTSUPP;
15614 
15615 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15616 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15617 		return -EOPNOTSUPP;
15618 
15619 	if (!info->attrs[NL80211_ATTR_MAC])
15620 		return -EINVAL;
15621 
15622 	wdev_lock(wdev);
15623 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15624 	ret = rdev_del_pmk(rdev, dev, aa);
15625 	wdev_unlock(wdev);
15626 
15627 	return ret;
15628 }
15629 
15630 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
15631 {
15632 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15633 	struct net_device *dev = info->user_ptr[1];
15634 	struct cfg80211_external_auth_params params;
15635 
15636 	if (!rdev->ops->external_auth)
15637 		return -EOPNOTSUPP;
15638 
15639 	if (!info->attrs[NL80211_ATTR_SSID] &&
15640 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
15641 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
15642 		return -EINVAL;
15643 
15644 	if (!info->attrs[NL80211_ATTR_BSSID])
15645 		return -EINVAL;
15646 
15647 	if (!info->attrs[NL80211_ATTR_STATUS_CODE])
15648 		return -EINVAL;
15649 
15650 	memset(&params, 0, sizeof(params));
15651 
15652 	if (info->attrs[NL80211_ATTR_SSID]) {
15653 		params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
15654 		if (params.ssid.ssid_len == 0)
15655 			return -EINVAL;
15656 		memcpy(params.ssid.ssid,
15657 		       nla_data(info->attrs[NL80211_ATTR_SSID]),
15658 		       params.ssid.ssid_len);
15659 	}
15660 
15661 	memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
15662 	       ETH_ALEN);
15663 
15664 	params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15665 
15666 	if (info->attrs[NL80211_ATTR_PMKID])
15667 		params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
15668 
15669 	return rdev_external_auth(rdev, dev, &params);
15670 }
15671 
15672 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
15673 {
15674 	bool dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
15675 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15676 	struct net_device *dev = info->user_ptr[1];
15677 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15678 	const u8 *buf;
15679 	size_t len;
15680 	u8 *dest;
15681 	u16 proto;
15682 	bool noencrypt;
15683 	u64 cookie = 0;
15684 	int link_id;
15685 	int err;
15686 
15687 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15688 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
15689 		return -EOPNOTSUPP;
15690 
15691 	if (!rdev->ops->tx_control_port)
15692 		return -EOPNOTSUPP;
15693 
15694 	if (!info->attrs[NL80211_ATTR_FRAME] ||
15695 	    !info->attrs[NL80211_ATTR_MAC] ||
15696 	    !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
15697 		GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
15698 		return -EINVAL;
15699 	}
15700 
15701 	wdev_lock(wdev);
15702 
15703 	switch (wdev->iftype) {
15704 	case NL80211_IFTYPE_AP:
15705 	case NL80211_IFTYPE_P2P_GO:
15706 	case NL80211_IFTYPE_MESH_POINT:
15707 		break;
15708 	case NL80211_IFTYPE_ADHOC:
15709 		if (wdev->u.ibss.current_bss)
15710 			break;
15711 		err = -ENOTCONN;
15712 		goto out;
15713 	case NL80211_IFTYPE_STATION:
15714 	case NL80211_IFTYPE_P2P_CLIENT:
15715 		if (wdev->connected)
15716 			break;
15717 		err = -ENOTCONN;
15718 		goto out;
15719 	default:
15720 		err = -EOPNOTSUPP;
15721 		goto out;
15722 	}
15723 
15724 	wdev_unlock(wdev);
15725 
15726 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15727 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15728 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15729 	proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
15730 	noencrypt =
15731 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
15732 
15733 	link_id = nl80211_link_id_or_invalid(info->attrs);
15734 
15735 	err = rdev_tx_control_port(rdev, dev, buf, len,
15736 				   dest, cpu_to_be16(proto), noencrypt, link_id,
15737 				   dont_wait_for_ack ? NULL : &cookie);
15738 	if (!err && !dont_wait_for_ack)
15739 		nl_set_extack_cookie_u64(info->extack, cookie);
15740 	return err;
15741  out:
15742 	wdev_unlock(wdev);
15743 	return err;
15744 }
15745 
15746 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
15747 					   struct genl_info *info)
15748 {
15749 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15750 	struct net_device *dev = info->user_ptr[1];
15751 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15752 	struct cfg80211_ftm_responder_stats ftm_stats = {};
15753 	unsigned int link_id = nl80211_link_id(info->attrs);
15754 	struct sk_buff *msg;
15755 	void *hdr;
15756 	struct nlattr *ftm_stats_attr;
15757 	int err;
15758 
15759 	if (wdev->iftype != NL80211_IFTYPE_AP ||
15760 	    !wdev->links[link_id].ap.beacon_interval)
15761 		return -EOPNOTSUPP;
15762 
15763 	err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
15764 	if (err)
15765 		return err;
15766 
15767 	if (!ftm_stats.filled)
15768 		return -ENODATA;
15769 
15770 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15771 	if (!msg)
15772 		return -ENOMEM;
15773 
15774 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
15775 			     NL80211_CMD_GET_FTM_RESPONDER_STATS);
15776 	if (!hdr)
15777 		goto nla_put_failure;
15778 
15779 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15780 		goto nla_put_failure;
15781 
15782 	ftm_stats_attr = nla_nest_start_noflag(msg,
15783 					       NL80211_ATTR_FTM_RESPONDER_STATS);
15784 	if (!ftm_stats_attr)
15785 		goto nla_put_failure;
15786 
15787 #define SET_FTM(field, name, type)					 \
15788 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15789 	    nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,		 \
15790 			     ftm_stats.field))				 \
15791 		goto nla_put_failure; } while (0)
15792 #define SET_FTM_U64(field, name)					 \
15793 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15794 	    nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,		 \
15795 			      ftm_stats.field, NL80211_FTM_STATS_PAD))	 \
15796 		goto nla_put_failure; } while (0)
15797 
15798 	SET_FTM(success_num, SUCCESS_NUM, u32);
15799 	SET_FTM(partial_num, PARTIAL_NUM, u32);
15800 	SET_FTM(failed_num, FAILED_NUM, u32);
15801 	SET_FTM(asap_num, ASAP_NUM, u32);
15802 	SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
15803 	SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
15804 	SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
15805 	SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
15806 	SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
15807 #undef SET_FTM
15808 
15809 	nla_nest_end(msg, ftm_stats_attr);
15810 
15811 	genlmsg_end(msg, hdr);
15812 	return genlmsg_reply(msg, info);
15813 
15814 nla_put_failure:
15815 	nlmsg_free(msg);
15816 	return -ENOBUFS;
15817 }
15818 
15819 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
15820 {
15821 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15822 	struct cfg80211_update_owe_info owe_info;
15823 	struct net_device *dev = info->user_ptr[1];
15824 
15825 	if (!rdev->ops->update_owe_info)
15826 		return -EOPNOTSUPP;
15827 
15828 	if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
15829 	    !info->attrs[NL80211_ATTR_MAC])
15830 		return -EINVAL;
15831 
15832 	memset(&owe_info, 0, sizeof(owe_info));
15833 	owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15834 	nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
15835 
15836 	if (info->attrs[NL80211_ATTR_IE]) {
15837 		owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
15838 		owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
15839 	}
15840 
15841 	return rdev_update_owe_info(rdev, dev, &owe_info);
15842 }
15843 
15844 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
15845 {
15846 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15847 	struct net_device *dev = info->user_ptr[1];
15848 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15849 	struct station_info sinfo = {};
15850 	const u8 *buf;
15851 	size_t len;
15852 	u8 *dest;
15853 	int err;
15854 
15855 	if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
15856 		return -EOPNOTSUPP;
15857 
15858 	if (!info->attrs[NL80211_ATTR_MAC] ||
15859 	    !info->attrs[NL80211_ATTR_FRAME]) {
15860 		GENL_SET_ERR_MSG(info, "Frame or MAC missing");
15861 		return -EINVAL;
15862 	}
15863 
15864 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
15865 		return -EOPNOTSUPP;
15866 
15867 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15868 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15869 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15870 
15871 	if (len < sizeof(struct ethhdr))
15872 		return -EINVAL;
15873 
15874 	if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
15875 	    !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
15876 		return -EINVAL;
15877 
15878 	err = rdev_get_station(rdev, dev, dest, &sinfo);
15879 	if (err)
15880 		return err;
15881 
15882 	cfg80211_sinfo_release_content(&sinfo);
15883 
15884 	return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
15885 }
15886 
15887 static int parse_tid_conf(struct cfg80211_registered_device *rdev,
15888 			  struct nlattr *attrs[], struct net_device *dev,
15889 			  struct cfg80211_tid_cfg *tid_conf,
15890 			  struct genl_info *info, const u8 *peer,
15891 			  unsigned int link_id)
15892 {
15893 	struct netlink_ext_ack *extack = info->extack;
15894 	u64 mask;
15895 	int err;
15896 
15897 	if (!attrs[NL80211_TID_CONFIG_ATTR_TIDS])
15898 		return -EINVAL;
15899 
15900 	tid_conf->config_override =
15901 			nla_get_flag(attrs[NL80211_TID_CONFIG_ATTR_OVERRIDE]);
15902 	tid_conf->tids = nla_get_u16(attrs[NL80211_TID_CONFIG_ATTR_TIDS]);
15903 
15904 	if (tid_conf->config_override) {
15905 		if (rdev->ops->reset_tid_config) {
15906 			err = rdev_reset_tid_config(rdev, dev, peer,
15907 						    tid_conf->tids);
15908 			if (err)
15909 				return err;
15910 		} else {
15911 			return -EINVAL;
15912 		}
15913 	}
15914 
15915 	if (attrs[NL80211_TID_CONFIG_ATTR_NOACK]) {
15916 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_NOACK);
15917 		tid_conf->noack =
15918 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_NOACK]);
15919 	}
15920 
15921 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]) {
15922 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT);
15923 		tid_conf->retry_short =
15924 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]);
15925 
15926 		if (tid_conf->retry_short > rdev->wiphy.max_data_retry_count)
15927 			return -EINVAL;
15928 	}
15929 
15930 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]) {
15931 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
15932 		tid_conf->retry_long =
15933 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]);
15934 
15935 		if (tid_conf->retry_long > rdev->wiphy.max_data_retry_count)
15936 			return -EINVAL;
15937 	}
15938 
15939 	if (attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]) {
15940 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
15941 		tid_conf->ampdu =
15942 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]);
15943 	}
15944 
15945 	if (attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]) {
15946 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
15947 		tid_conf->rtscts =
15948 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]);
15949 	}
15950 
15951 	if (attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]) {
15952 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
15953 		tid_conf->amsdu =
15954 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]);
15955 	}
15956 
15957 	if (attrs[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE]) {
15958 		u32 idx = NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE, attr;
15959 
15960 		tid_conf->txrate_type = nla_get_u8(attrs[idx]);
15961 
15962 		if (tid_conf->txrate_type != NL80211_TX_RATE_AUTOMATIC) {
15963 			attr = NL80211_TID_CONFIG_ATTR_TX_RATE;
15964 			err = nl80211_parse_tx_bitrate_mask(info, attrs, attr,
15965 						    &tid_conf->txrate_mask, dev,
15966 						    true, link_id);
15967 			if (err)
15968 				return err;
15969 
15970 			tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE);
15971 		}
15972 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
15973 	}
15974 
15975 	if (peer)
15976 		mask = rdev->wiphy.tid_config_support.peer;
15977 	else
15978 		mask = rdev->wiphy.tid_config_support.vif;
15979 
15980 	if (tid_conf->mask & ~mask) {
15981 		NL_SET_ERR_MSG(extack, "unsupported TID configuration");
15982 		return -ENOTSUPP;
15983 	}
15984 
15985 	return 0;
15986 }
15987 
15988 static int nl80211_set_tid_config(struct sk_buff *skb,
15989 				  struct genl_info *info)
15990 {
15991 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15992 	struct nlattr *attrs[NL80211_TID_CONFIG_ATTR_MAX + 1];
15993 	unsigned int link_id = nl80211_link_id(info->attrs);
15994 	struct net_device *dev = info->user_ptr[1];
15995 	struct cfg80211_tid_config *tid_config;
15996 	struct nlattr *tid;
15997 	int conf_idx = 0, rem_conf;
15998 	int ret = -EINVAL;
15999 	u32 num_conf = 0;
16000 
16001 	if (!info->attrs[NL80211_ATTR_TID_CONFIG])
16002 		return -EINVAL;
16003 
16004 	if (!rdev->ops->set_tid_config)
16005 		return -EOPNOTSUPP;
16006 
16007 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
16008 			    rem_conf)
16009 		num_conf++;
16010 
16011 	tid_config = kzalloc(struct_size(tid_config, tid_conf, num_conf),
16012 			     GFP_KERNEL);
16013 	if (!tid_config)
16014 		return -ENOMEM;
16015 
16016 	tid_config->n_tid_conf = num_conf;
16017 
16018 	if (info->attrs[NL80211_ATTR_MAC])
16019 		tid_config->peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
16020 
16021 	wdev_lock(dev->ieee80211_ptr);
16022 
16023 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
16024 			    rem_conf) {
16025 		ret = nla_parse_nested(attrs, NL80211_TID_CONFIG_ATTR_MAX,
16026 				       tid, NULL, NULL);
16027 
16028 		if (ret)
16029 			goto bad_tid_conf;
16030 
16031 		ret = parse_tid_conf(rdev, attrs, dev,
16032 				     &tid_config->tid_conf[conf_idx],
16033 				     info, tid_config->peer, link_id);
16034 		if (ret)
16035 			goto bad_tid_conf;
16036 
16037 		conf_idx++;
16038 	}
16039 
16040 	ret = rdev_set_tid_config(rdev, dev, tid_config);
16041 
16042 bad_tid_conf:
16043 	kfree(tid_config);
16044 	wdev_unlock(dev->ieee80211_ptr);
16045 	return ret;
16046 }
16047 
16048 static int nl80211_color_change(struct sk_buff *skb, struct genl_info *info)
16049 {
16050 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16051 	struct cfg80211_color_change_settings params = {};
16052 	struct net_device *dev = info->user_ptr[1];
16053 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16054 	struct nlattr **tb;
16055 	u16 offset;
16056 	int err;
16057 
16058 	if (!rdev->ops->color_change)
16059 		return -EOPNOTSUPP;
16060 
16061 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
16062 				     NL80211_EXT_FEATURE_BSS_COLOR))
16063 		return -EOPNOTSUPP;
16064 
16065 	if (wdev->iftype != NL80211_IFTYPE_AP)
16066 		return -EOPNOTSUPP;
16067 
16068 	if (!info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT] ||
16069 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR] ||
16070 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS])
16071 		return -EINVAL;
16072 
16073 	params.count = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT]);
16074 	params.color = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR]);
16075 
16076 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_next,
16077 				   info->extack);
16078 	if (err)
16079 		return err;
16080 
16081 	tb = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*tb), GFP_KERNEL);
16082 	if (!tb)
16083 		return -ENOMEM;
16084 
16085 	err = nla_parse_nested(tb, NL80211_ATTR_MAX,
16086 			       info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS],
16087 			       nl80211_policy, info->extack);
16088 	if (err)
16089 		goto out;
16090 
16091 	err = nl80211_parse_beacon(rdev, tb, &params.beacon_color_change,
16092 				   info->extack);
16093 	if (err)
16094 		goto out;
16095 
16096 	if (!tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
16097 		err = -EINVAL;
16098 		goto out;
16099 	}
16100 
16101 	if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) != sizeof(u16)) {
16102 		err = -EINVAL;
16103 		goto out;
16104 	}
16105 
16106 	offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
16107 	if (offset >= params.beacon_color_change.tail_len) {
16108 		err = -EINVAL;
16109 		goto out;
16110 	}
16111 
16112 	if (params.beacon_color_change.tail[offset] != params.count) {
16113 		err = -EINVAL;
16114 		goto out;
16115 	}
16116 
16117 	params.counter_offset_beacon = offset;
16118 
16119 	if (tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
16120 		if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) !=
16121 		    sizeof(u16)) {
16122 			err = -EINVAL;
16123 			goto out;
16124 		}
16125 
16126 		offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
16127 		if (offset >= params.beacon_color_change.probe_resp_len) {
16128 			err = -EINVAL;
16129 			goto out;
16130 		}
16131 
16132 		if (params.beacon_color_change.probe_resp[offset] !=
16133 		    params.count) {
16134 			err = -EINVAL;
16135 			goto out;
16136 		}
16137 
16138 		params.counter_offset_presp = offset;
16139 	}
16140 
16141 	wdev_lock(wdev);
16142 	err = rdev_color_change(rdev, dev, &params);
16143 	wdev_unlock(wdev);
16144 
16145 out:
16146 	kfree(params.beacon_next.mbssid_ies);
16147 	kfree(params.beacon_color_change.mbssid_ies);
16148 	kfree(params.beacon_next.rnr_ies);
16149 	kfree(params.beacon_color_change.rnr_ies);
16150 	kfree(tb);
16151 	return err;
16152 }
16153 
16154 static int nl80211_set_fils_aad(struct sk_buff *skb,
16155 				struct genl_info *info)
16156 {
16157 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16158 	struct net_device *dev = info->user_ptr[1];
16159 	struct cfg80211_fils_aad fils_aad = {};
16160 	u8 *nonces;
16161 
16162 	if (!info->attrs[NL80211_ATTR_MAC] ||
16163 	    !info->attrs[NL80211_ATTR_FILS_KEK] ||
16164 	    !info->attrs[NL80211_ATTR_FILS_NONCES])
16165 		return -EINVAL;
16166 
16167 	fils_aad.macaddr = nla_data(info->attrs[NL80211_ATTR_MAC]);
16168 	fils_aad.kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
16169 	fils_aad.kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
16170 	nonces = nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
16171 	fils_aad.snonce = nonces;
16172 	fils_aad.anonce = nonces + FILS_NONCE_LEN;
16173 
16174 	return rdev_set_fils_aad(rdev, dev, &fils_aad);
16175 }
16176 
16177 static int nl80211_add_link(struct sk_buff *skb, struct genl_info *info)
16178 {
16179 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16180 	unsigned int link_id = nl80211_link_id(info->attrs);
16181 	struct net_device *dev = info->user_ptr[1];
16182 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16183 	int ret;
16184 
16185 	if (!(wdev->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
16186 		return -EINVAL;
16187 
16188 	switch (wdev->iftype) {
16189 	case NL80211_IFTYPE_AP:
16190 		break;
16191 	default:
16192 		return -EINVAL;
16193 	}
16194 
16195 	if (!info->attrs[NL80211_ATTR_MAC] ||
16196 	    !is_valid_ether_addr(nla_data(info->attrs[NL80211_ATTR_MAC])))
16197 		return -EINVAL;
16198 
16199 	wdev_lock(wdev);
16200 	wdev->valid_links |= BIT(link_id);
16201 	ether_addr_copy(wdev->links[link_id].addr,
16202 			nla_data(info->attrs[NL80211_ATTR_MAC]));
16203 
16204 	ret = rdev_add_intf_link(rdev, wdev, link_id);
16205 	if (ret) {
16206 		wdev->valid_links &= ~BIT(link_id);
16207 		eth_zero_addr(wdev->links[link_id].addr);
16208 	}
16209 	wdev_unlock(wdev);
16210 
16211 	return ret;
16212 }
16213 
16214 static int nl80211_remove_link(struct sk_buff *skb, struct genl_info *info)
16215 {
16216 	unsigned int link_id = nl80211_link_id(info->attrs);
16217 	struct net_device *dev = info->user_ptr[1];
16218 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16219 
16220 	/* cannot remove if there's no link */
16221 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
16222 		return -EINVAL;
16223 
16224 	switch (wdev->iftype) {
16225 	case NL80211_IFTYPE_AP:
16226 		break;
16227 	default:
16228 		return -EINVAL;
16229 	}
16230 
16231 	wdev_lock(wdev);
16232 	cfg80211_remove_link(wdev, link_id);
16233 	wdev_unlock(wdev);
16234 
16235 	return 0;
16236 }
16237 
16238 static int
16239 nl80211_add_mod_link_station(struct sk_buff *skb, struct genl_info *info,
16240 			     bool add)
16241 {
16242 	struct link_station_parameters params = {};
16243 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16244 	struct net_device *dev = info->user_ptr[1];
16245 	int err;
16246 
16247 	if ((add && !rdev->ops->add_link_station) ||
16248 	    (!add && !rdev->ops->mod_link_station))
16249 		return -EOPNOTSUPP;
16250 
16251 	if (add && !info->attrs[NL80211_ATTR_MAC])
16252 		return -EINVAL;
16253 
16254 	if (!info->attrs[NL80211_ATTR_MLD_ADDR])
16255 		return -EINVAL;
16256 
16257 	if (add && !info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
16258 		return -EINVAL;
16259 
16260 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
16261 
16262 	if (info->attrs[NL80211_ATTR_MAC]) {
16263 		params.link_mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
16264 		if (!is_valid_ether_addr(params.link_mac))
16265 			return -EINVAL;
16266 	}
16267 
16268 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
16269 		return -EINVAL;
16270 
16271 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
16272 
16273 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
16274 		params.supported_rates =
16275 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
16276 		params.supported_rates_len =
16277 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
16278 	}
16279 
16280 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
16281 		params.ht_capa =
16282 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
16283 
16284 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
16285 		params.vht_capa =
16286 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
16287 
16288 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
16289 		params.he_capa =
16290 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
16291 		params.he_capa_len =
16292 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
16293 
16294 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
16295 			params.eht_capa =
16296 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
16297 			params.eht_capa_len =
16298 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
16299 
16300 			if (!ieee80211_eht_capa_size_ok((const u8 *)params.he_capa,
16301 							(const u8 *)params.eht_capa,
16302 							params.eht_capa_len,
16303 							false))
16304 				return -EINVAL;
16305 		}
16306 	}
16307 
16308 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
16309 		params.he_6ghz_capa =
16310 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
16311 
16312 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
16313 		params.opmode_notif_used = true;
16314 		params.opmode_notif =
16315 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
16316 	}
16317 
16318 	err = nl80211_parse_sta_txpower_setting(info, &params.txpwr,
16319 						&params.txpwr_set);
16320 	if (err)
16321 		return err;
16322 
16323 	wdev_lock(dev->ieee80211_ptr);
16324 	if (add)
16325 		err = rdev_add_link_station(rdev, dev, &params);
16326 	else
16327 		err = rdev_mod_link_station(rdev, dev, &params);
16328 	wdev_unlock(dev->ieee80211_ptr);
16329 
16330 	return err;
16331 }
16332 
16333 static int
16334 nl80211_add_link_station(struct sk_buff *skb, struct genl_info *info)
16335 {
16336 	return nl80211_add_mod_link_station(skb, info, true);
16337 }
16338 
16339 static int
16340 nl80211_modify_link_station(struct sk_buff *skb, struct genl_info *info)
16341 {
16342 	return nl80211_add_mod_link_station(skb, info, false);
16343 }
16344 
16345 static int
16346 nl80211_remove_link_station(struct sk_buff *skb, struct genl_info *info)
16347 {
16348 	struct link_station_del_parameters params = {};
16349 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16350 	struct net_device *dev = info->user_ptr[1];
16351 	int ret;
16352 
16353 	if (!rdev->ops->del_link_station)
16354 		return -EOPNOTSUPP;
16355 
16356 	if (!info->attrs[NL80211_ATTR_MLD_ADDR] ||
16357 	    !info->attrs[NL80211_ATTR_MLO_LINK_ID])
16358 		return -EINVAL;
16359 
16360 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
16361 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
16362 
16363 	wdev_lock(dev->ieee80211_ptr);
16364 	ret = rdev_del_link_station(rdev, dev, &params);
16365 	wdev_unlock(dev->ieee80211_ptr);
16366 
16367 	return ret;
16368 }
16369 
16370 static int nl80211_set_hw_timestamp(struct sk_buff *skb,
16371 				    struct genl_info *info)
16372 {
16373 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16374 	struct net_device *dev = info->user_ptr[1];
16375 	struct cfg80211_set_hw_timestamp hwts = {};
16376 
16377 	if (!rdev->wiphy.hw_timestamp_max_peers)
16378 		return -EOPNOTSUPP;
16379 
16380 	if (!info->attrs[NL80211_ATTR_MAC] &&
16381 	    rdev->wiphy.hw_timestamp_max_peers != CFG80211_HW_TIMESTAMP_ALL_PEERS)
16382 		return -EOPNOTSUPP;
16383 
16384 	if (info->attrs[NL80211_ATTR_MAC])
16385 		hwts.macaddr = nla_data(info->attrs[NL80211_ATTR_MAC]);
16386 
16387 	hwts.enable =
16388 		nla_get_flag(info->attrs[NL80211_ATTR_HW_TIMESTAMP_ENABLED]);
16389 
16390 	return rdev_set_hw_timestamp(rdev, dev, &hwts);
16391 }
16392 
16393 #define NL80211_FLAG_NEED_WIPHY		0x01
16394 #define NL80211_FLAG_NEED_NETDEV	0x02
16395 #define NL80211_FLAG_NEED_RTNL		0x04
16396 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
16397 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
16398 					 NL80211_FLAG_CHECK_NETDEV_UP)
16399 #define NL80211_FLAG_NEED_WDEV		0x10
16400 /* If a netdev is associated, it must be UP, P2P must be started */
16401 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
16402 					 NL80211_FLAG_CHECK_NETDEV_UP)
16403 #define NL80211_FLAG_CLEAR_SKB		0x20
16404 #define NL80211_FLAG_NO_WIPHY_MTX	0x40
16405 #define NL80211_FLAG_MLO_VALID_LINK_ID	0x80
16406 #define NL80211_FLAG_MLO_UNSUPPORTED	0x100
16407 
16408 #define INTERNAL_FLAG_SELECTORS(__sel)			\
16409 	SELECTOR(__sel, NONE, 0) /* must be first */	\
16410 	SELECTOR(__sel, WIPHY,				\
16411 		 NL80211_FLAG_NEED_WIPHY)		\
16412 	SELECTOR(__sel, WDEV,				\
16413 		 NL80211_FLAG_NEED_WDEV)		\
16414 	SELECTOR(__sel, NETDEV,				\
16415 		 NL80211_FLAG_NEED_NETDEV)		\
16416 	SELECTOR(__sel, NETDEV_LINK,			\
16417 		 NL80211_FLAG_NEED_NETDEV |		\
16418 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16419 	SELECTOR(__sel, NETDEV_NO_MLO,			\
16420 		 NL80211_FLAG_NEED_NETDEV |		\
16421 		 NL80211_FLAG_MLO_UNSUPPORTED)	\
16422 	SELECTOR(__sel, WIPHY_RTNL,			\
16423 		 NL80211_FLAG_NEED_WIPHY |		\
16424 		 NL80211_FLAG_NEED_RTNL)		\
16425 	SELECTOR(__sel, WIPHY_RTNL_NOMTX,		\
16426 		 NL80211_FLAG_NEED_WIPHY |		\
16427 		 NL80211_FLAG_NEED_RTNL |		\
16428 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16429 	SELECTOR(__sel, WDEV_RTNL,			\
16430 		 NL80211_FLAG_NEED_WDEV |		\
16431 		 NL80211_FLAG_NEED_RTNL)		\
16432 	SELECTOR(__sel, NETDEV_RTNL,			\
16433 		 NL80211_FLAG_NEED_NETDEV |		\
16434 		 NL80211_FLAG_NEED_RTNL)		\
16435 	SELECTOR(__sel, NETDEV_UP,			\
16436 		 NL80211_FLAG_NEED_NETDEV_UP)		\
16437 	SELECTOR(__sel, NETDEV_UP_LINK,			\
16438 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16439 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16440 	SELECTOR(__sel, NETDEV_UP_NO_MLO,		\
16441 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16442 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16443 	SELECTOR(__sel, NETDEV_UP_NO_MLO_CLEAR,		\
16444 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16445 		 NL80211_FLAG_CLEAR_SKB |		\
16446 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16447 	SELECTOR(__sel, NETDEV_UP_NOTMX,		\
16448 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16449 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16450 	SELECTOR(__sel, NETDEV_UP_NOTMX_NOMLO,		\
16451 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16452 		 NL80211_FLAG_NO_WIPHY_MTX |		\
16453 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16454 	SELECTOR(__sel, NETDEV_UP_CLEAR,		\
16455 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16456 		 NL80211_FLAG_CLEAR_SKB)		\
16457 	SELECTOR(__sel, WDEV_UP,			\
16458 		 NL80211_FLAG_NEED_WDEV_UP)		\
16459 	SELECTOR(__sel, WDEV_UP_LINK,			\
16460 		 NL80211_FLAG_NEED_WDEV_UP |		\
16461 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16462 	SELECTOR(__sel, WDEV_UP_RTNL,			\
16463 		 NL80211_FLAG_NEED_WDEV_UP |		\
16464 		 NL80211_FLAG_NEED_RTNL)		\
16465 	SELECTOR(__sel, WIPHY_CLEAR,			\
16466 		 NL80211_FLAG_NEED_WIPHY |		\
16467 		 NL80211_FLAG_CLEAR_SKB)
16468 
16469 enum nl80211_internal_flags_selector {
16470 #define SELECTOR(_, name, value)	NL80211_IFL_SEL_##name,
16471 	INTERNAL_FLAG_SELECTORS(_)
16472 #undef SELECTOR
16473 };
16474 
16475 static u32 nl80211_internal_flags[] = {
16476 #define SELECTOR(_, name, value)	[NL80211_IFL_SEL_##name] = value,
16477 	INTERNAL_FLAG_SELECTORS(_)
16478 #undef SELECTOR
16479 };
16480 
16481 static int nl80211_pre_doit(const struct genl_split_ops *ops,
16482 			    struct sk_buff *skb,
16483 			    struct genl_info *info)
16484 {
16485 	struct cfg80211_registered_device *rdev = NULL;
16486 	struct wireless_dev *wdev = NULL;
16487 	struct net_device *dev = NULL;
16488 	u32 internal_flags;
16489 	int err;
16490 
16491 	if (WARN_ON(ops->internal_flags >= ARRAY_SIZE(nl80211_internal_flags)))
16492 		return -EINVAL;
16493 
16494 	internal_flags = nl80211_internal_flags[ops->internal_flags];
16495 
16496 	rtnl_lock();
16497 	if (internal_flags & NL80211_FLAG_NEED_WIPHY) {
16498 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
16499 		if (IS_ERR(rdev)) {
16500 			err = PTR_ERR(rdev);
16501 			goto out_unlock;
16502 		}
16503 		info->user_ptr[0] = rdev;
16504 	} else if (internal_flags & NL80211_FLAG_NEED_NETDEV ||
16505 		   internal_flags & NL80211_FLAG_NEED_WDEV) {
16506 		wdev = __cfg80211_wdev_from_attrs(NULL, genl_info_net(info),
16507 						  info->attrs);
16508 		if (IS_ERR(wdev)) {
16509 			err = PTR_ERR(wdev);
16510 			goto out_unlock;
16511 		}
16512 
16513 		dev = wdev->netdev;
16514 		dev_hold(dev);
16515 		rdev = wiphy_to_rdev(wdev->wiphy);
16516 
16517 		if (internal_flags & NL80211_FLAG_NEED_NETDEV) {
16518 			if (!dev) {
16519 				err = -EINVAL;
16520 				goto out_unlock;
16521 			}
16522 
16523 			info->user_ptr[1] = dev;
16524 		} else {
16525 			info->user_ptr[1] = wdev;
16526 		}
16527 
16528 		if (internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
16529 		    !wdev_running(wdev)) {
16530 			err = -ENETDOWN;
16531 			goto out_unlock;
16532 		}
16533 
16534 		info->user_ptr[0] = rdev;
16535 	}
16536 
16537 	if (internal_flags & NL80211_FLAG_MLO_VALID_LINK_ID) {
16538 		struct nlattr *link_id = info->attrs[NL80211_ATTR_MLO_LINK_ID];
16539 
16540 		if (!wdev) {
16541 			err = -EINVAL;
16542 			goto out_unlock;
16543 		}
16544 
16545 		/* MLO -> require valid link ID */
16546 		if (wdev->valid_links &&
16547 		    (!link_id ||
16548 		     !(wdev->valid_links & BIT(nla_get_u8(link_id))))) {
16549 			err = -EINVAL;
16550 			goto out_unlock;
16551 		}
16552 
16553 		/* non-MLO -> no link ID attribute accepted */
16554 		if (!wdev->valid_links && link_id) {
16555 			err = -EINVAL;
16556 			goto out_unlock;
16557 		}
16558 	}
16559 
16560 	if (internal_flags & NL80211_FLAG_MLO_UNSUPPORTED) {
16561 		if (info->attrs[NL80211_ATTR_MLO_LINK_ID] ||
16562 		    (wdev && wdev->valid_links)) {
16563 			err = -EINVAL;
16564 			goto out_unlock;
16565 		}
16566 	}
16567 
16568 	if (rdev && !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16569 		wiphy_lock(&rdev->wiphy);
16570 		/* we keep the mutex locked until post_doit */
16571 		__release(&rdev->wiphy.mtx);
16572 	}
16573 	if (!(internal_flags & NL80211_FLAG_NEED_RTNL))
16574 		rtnl_unlock();
16575 
16576 	return 0;
16577 out_unlock:
16578 	rtnl_unlock();
16579 	dev_put(dev);
16580 	return err;
16581 }
16582 
16583 static void nl80211_post_doit(const struct genl_split_ops *ops,
16584 			      struct sk_buff *skb,
16585 			      struct genl_info *info)
16586 {
16587 	u32 internal_flags = nl80211_internal_flags[ops->internal_flags];
16588 
16589 	if (info->user_ptr[1]) {
16590 		if (internal_flags & NL80211_FLAG_NEED_WDEV) {
16591 			struct wireless_dev *wdev = info->user_ptr[1];
16592 
16593 			dev_put(wdev->netdev);
16594 		} else {
16595 			dev_put(info->user_ptr[1]);
16596 		}
16597 	}
16598 
16599 	if (info->user_ptr[0] &&
16600 	    !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16601 		struct cfg80211_registered_device *rdev = info->user_ptr[0];
16602 
16603 		/* we kept the mutex locked since pre_doit */
16604 		__acquire(&rdev->wiphy.mtx);
16605 		wiphy_unlock(&rdev->wiphy);
16606 	}
16607 
16608 	if (internal_flags & NL80211_FLAG_NEED_RTNL)
16609 		rtnl_unlock();
16610 
16611 	/* If needed, clear the netlink message payload from the SKB
16612 	 * as it might contain key data that shouldn't stick around on
16613 	 * the heap after the SKB is freed. The netlink message header
16614 	 * is still needed for further processing, so leave it intact.
16615 	 */
16616 	if (internal_flags & NL80211_FLAG_CLEAR_SKB) {
16617 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
16618 
16619 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
16620 	}
16621 }
16622 
16623 static int nl80211_set_sar_sub_specs(struct cfg80211_registered_device *rdev,
16624 				     struct cfg80211_sar_specs *sar_specs,
16625 				     struct nlattr *spec[], int index)
16626 {
16627 	u32 range_index, i;
16628 
16629 	if (!sar_specs || !spec)
16630 		return -EINVAL;
16631 
16632 	if (!spec[NL80211_SAR_ATTR_SPECS_POWER] ||
16633 	    !spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX])
16634 		return -EINVAL;
16635 
16636 	range_index = nla_get_u32(spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX]);
16637 
16638 	/* check if range_index exceeds num_freq_ranges */
16639 	if (range_index >= rdev->wiphy.sar_capa->num_freq_ranges)
16640 		return -EINVAL;
16641 
16642 	/* check if range_index duplicates */
16643 	for (i = 0; i < index; i++) {
16644 		if (sar_specs->sub_specs[i].freq_range_index == range_index)
16645 			return -EINVAL;
16646 	}
16647 
16648 	sar_specs->sub_specs[index].power =
16649 		nla_get_s32(spec[NL80211_SAR_ATTR_SPECS_POWER]);
16650 
16651 	sar_specs->sub_specs[index].freq_range_index = range_index;
16652 
16653 	return 0;
16654 }
16655 
16656 static int nl80211_set_sar_specs(struct sk_buff *skb, struct genl_info *info)
16657 {
16658 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16659 	struct nlattr *spec[NL80211_SAR_ATTR_SPECS_MAX + 1];
16660 	struct nlattr *tb[NL80211_SAR_ATTR_MAX + 1];
16661 	struct cfg80211_sar_specs *sar_spec;
16662 	enum nl80211_sar_type type;
16663 	struct nlattr *spec_list;
16664 	u32 specs;
16665 	int rem, err;
16666 
16667 	if (!rdev->wiphy.sar_capa || !rdev->ops->set_sar_specs)
16668 		return -EOPNOTSUPP;
16669 
16670 	if (!info->attrs[NL80211_ATTR_SAR_SPEC])
16671 		return -EINVAL;
16672 
16673 	nla_parse_nested(tb, NL80211_SAR_ATTR_MAX,
16674 			 info->attrs[NL80211_ATTR_SAR_SPEC],
16675 			 NULL, NULL);
16676 
16677 	if (!tb[NL80211_SAR_ATTR_TYPE] || !tb[NL80211_SAR_ATTR_SPECS])
16678 		return -EINVAL;
16679 
16680 	type = nla_get_u32(tb[NL80211_SAR_ATTR_TYPE]);
16681 	if (type != rdev->wiphy.sar_capa->type)
16682 		return -EINVAL;
16683 
16684 	specs = 0;
16685 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem)
16686 		specs++;
16687 
16688 	if (specs > rdev->wiphy.sar_capa->num_freq_ranges)
16689 		return -EINVAL;
16690 
16691 	sar_spec = kzalloc(struct_size(sar_spec, sub_specs, specs), GFP_KERNEL);
16692 	if (!sar_spec)
16693 		return -ENOMEM;
16694 
16695 	sar_spec->type = type;
16696 	specs = 0;
16697 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem) {
16698 		nla_parse_nested(spec, NL80211_SAR_ATTR_SPECS_MAX,
16699 				 spec_list, NULL, NULL);
16700 
16701 		switch (type) {
16702 		case NL80211_SAR_TYPE_POWER:
16703 			if (nl80211_set_sar_sub_specs(rdev, sar_spec,
16704 						      spec, specs)) {
16705 				err = -EINVAL;
16706 				goto error;
16707 			}
16708 			break;
16709 		default:
16710 			err = -EINVAL;
16711 			goto error;
16712 		}
16713 		specs++;
16714 	}
16715 
16716 	sar_spec->num_sub_specs = specs;
16717 
16718 	rdev->cur_cmd_info = info;
16719 	err = rdev_set_sar_specs(rdev, sar_spec);
16720 	rdev->cur_cmd_info = NULL;
16721 error:
16722 	kfree(sar_spec);
16723 	return err;
16724 }
16725 
16726 #define SELECTOR(__sel, name, value) \
16727 	((__sel) == (value)) ? NL80211_IFL_SEL_##name :
16728 int __missing_selector(void);
16729 #define IFLAGS(__val) INTERNAL_FLAG_SELECTORS(__val) __missing_selector()
16730 
16731 static const struct genl_ops nl80211_ops[] = {
16732 	{
16733 		.cmd = NL80211_CMD_GET_WIPHY,
16734 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16735 		.doit = nl80211_get_wiphy,
16736 		.dumpit = nl80211_dump_wiphy,
16737 		.done = nl80211_dump_wiphy_done,
16738 		/* can be retrieved by unprivileged users */
16739 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16740 	},
16741 };
16742 
16743 static const struct genl_small_ops nl80211_small_ops[] = {
16744 	{
16745 		.cmd = NL80211_CMD_SET_WIPHY,
16746 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16747 		.doit = nl80211_set_wiphy,
16748 		.flags = GENL_UNS_ADMIN_PERM,
16749 	},
16750 	{
16751 		.cmd = NL80211_CMD_GET_INTERFACE,
16752 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16753 		.doit = nl80211_get_interface,
16754 		.dumpit = nl80211_dump_interface,
16755 		/* can be retrieved by unprivileged users */
16756 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
16757 	},
16758 	{
16759 		.cmd = NL80211_CMD_SET_INTERFACE,
16760 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16761 		.doit = nl80211_set_interface,
16762 		.flags = GENL_UNS_ADMIN_PERM,
16763 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16764 					 NL80211_FLAG_NEED_RTNL),
16765 	},
16766 	{
16767 		.cmd = NL80211_CMD_NEW_INTERFACE,
16768 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16769 		.doit = nl80211_new_interface,
16770 		.flags = GENL_UNS_ADMIN_PERM,
16771 		.internal_flags =
16772 			IFLAGS(NL80211_FLAG_NEED_WIPHY |
16773 			       NL80211_FLAG_NEED_RTNL |
16774 			       /* we take the wiphy mutex later ourselves */
16775 			       NL80211_FLAG_NO_WIPHY_MTX),
16776 	},
16777 	{
16778 		.cmd = NL80211_CMD_DEL_INTERFACE,
16779 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16780 		.doit = nl80211_del_interface,
16781 		.flags = GENL_UNS_ADMIN_PERM,
16782 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
16783 					 NL80211_FLAG_NEED_RTNL),
16784 	},
16785 	{
16786 		.cmd = NL80211_CMD_GET_KEY,
16787 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16788 		.doit = nl80211_get_key,
16789 		.flags = GENL_UNS_ADMIN_PERM,
16790 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16791 	},
16792 	{
16793 		.cmd = NL80211_CMD_SET_KEY,
16794 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16795 		.doit = nl80211_set_key,
16796 		.flags = GENL_UNS_ADMIN_PERM,
16797 		/* cannot use NL80211_FLAG_MLO_VALID_LINK_ID, depends on key */
16798 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16799 					 NL80211_FLAG_CLEAR_SKB),
16800 	},
16801 	{
16802 		.cmd = NL80211_CMD_NEW_KEY,
16803 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16804 		.doit = nl80211_new_key,
16805 		.flags = GENL_UNS_ADMIN_PERM,
16806 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16807 					 NL80211_FLAG_CLEAR_SKB),
16808 	},
16809 	{
16810 		.cmd = NL80211_CMD_DEL_KEY,
16811 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16812 		.doit = nl80211_del_key,
16813 		.flags = GENL_UNS_ADMIN_PERM,
16814 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16815 	},
16816 	{
16817 		.cmd = NL80211_CMD_SET_BEACON,
16818 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16819 		.flags = GENL_UNS_ADMIN_PERM,
16820 		.doit = nl80211_set_beacon,
16821 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16822 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16823 	},
16824 	{
16825 		.cmd = NL80211_CMD_START_AP,
16826 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16827 		.flags = GENL_UNS_ADMIN_PERM,
16828 		.doit = nl80211_start_ap,
16829 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16830 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16831 	},
16832 	{
16833 		.cmd = NL80211_CMD_STOP_AP,
16834 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16835 		.flags = GENL_UNS_ADMIN_PERM,
16836 		.doit = nl80211_stop_ap,
16837 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16838 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16839 	},
16840 	{
16841 		.cmd = NL80211_CMD_GET_STATION,
16842 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16843 		.doit = nl80211_get_station,
16844 		.dumpit = nl80211_dump_station,
16845 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16846 	},
16847 	{
16848 		.cmd = NL80211_CMD_SET_STATION,
16849 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16850 		.doit = nl80211_set_station,
16851 		.flags = GENL_UNS_ADMIN_PERM,
16852 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16853 	},
16854 	{
16855 		.cmd = NL80211_CMD_NEW_STATION,
16856 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16857 		.doit = nl80211_new_station,
16858 		.flags = GENL_UNS_ADMIN_PERM,
16859 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16860 	},
16861 	{
16862 		.cmd = NL80211_CMD_DEL_STATION,
16863 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16864 		.doit = nl80211_del_station,
16865 		.flags = GENL_UNS_ADMIN_PERM,
16866 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16867 	},
16868 	{
16869 		.cmd = NL80211_CMD_GET_MPATH,
16870 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16871 		.doit = nl80211_get_mpath,
16872 		.dumpit = nl80211_dump_mpath,
16873 		.flags = GENL_UNS_ADMIN_PERM,
16874 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16875 	},
16876 	{
16877 		.cmd = NL80211_CMD_GET_MPP,
16878 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16879 		.doit = nl80211_get_mpp,
16880 		.dumpit = nl80211_dump_mpp,
16881 		.flags = GENL_UNS_ADMIN_PERM,
16882 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16883 	},
16884 	{
16885 		.cmd = NL80211_CMD_SET_MPATH,
16886 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16887 		.doit = nl80211_set_mpath,
16888 		.flags = GENL_UNS_ADMIN_PERM,
16889 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16890 	},
16891 	{
16892 		.cmd = NL80211_CMD_NEW_MPATH,
16893 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16894 		.doit = nl80211_new_mpath,
16895 		.flags = GENL_UNS_ADMIN_PERM,
16896 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16897 	},
16898 	{
16899 		.cmd = NL80211_CMD_DEL_MPATH,
16900 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16901 		.doit = nl80211_del_mpath,
16902 		.flags = GENL_UNS_ADMIN_PERM,
16903 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16904 	},
16905 	{
16906 		.cmd = NL80211_CMD_SET_BSS,
16907 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16908 		.doit = nl80211_set_bss,
16909 		.flags = GENL_UNS_ADMIN_PERM,
16910 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16911 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16912 	},
16913 	{
16914 		.cmd = NL80211_CMD_GET_REG,
16915 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16916 		.doit = nl80211_get_reg_do,
16917 		.dumpit = nl80211_get_reg_dump,
16918 		/* can be retrieved by unprivileged users */
16919 	},
16920 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
16921 	{
16922 		.cmd = NL80211_CMD_SET_REG,
16923 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16924 		.doit = nl80211_set_reg,
16925 		.flags = GENL_ADMIN_PERM,
16926 	},
16927 #endif
16928 	{
16929 		.cmd = NL80211_CMD_REQ_SET_REG,
16930 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16931 		.doit = nl80211_req_set_reg,
16932 		.flags = GENL_ADMIN_PERM,
16933 	},
16934 	{
16935 		.cmd = NL80211_CMD_RELOAD_REGDB,
16936 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16937 		.doit = nl80211_reload_regdb,
16938 		.flags = GENL_ADMIN_PERM,
16939 	},
16940 	{
16941 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
16942 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16943 		.doit = nl80211_get_mesh_config,
16944 		/* can be retrieved by unprivileged users */
16945 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16946 	},
16947 	{
16948 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
16949 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16950 		.doit = nl80211_update_mesh_config,
16951 		.flags = GENL_UNS_ADMIN_PERM,
16952 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16953 	},
16954 	{
16955 		.cmd = NL80211_CMD_TRIGGER_SCAN,
16956 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16957 		.doit = nl80211_trigger_scan,
16958 		.flags = GENL_UNS_ADMIN_PERM,
16959 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16960 	},
16961 	{
16962 		.cmd = NL80211_CMD_ABORT_SCAN,
16963 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16964 		.doit = nl80211_abort_scan,
16965 		.flags = GENL_UNS_ADMIN_PERM,
16966 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16967 	},
16968 	{
16969 		.cmd = NL80211_CMD_GET_SCAN,
16970 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16971 		.dumpit = nl80211_dump_scan,
16972 	},
16973 	{
16974 		.cmd = NL80211_CMD_START_SCHED_SCAN,
16975 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16976 		.doit = nl80211_start_sched_scan,
16977 		.flags = GENL_UNS_ADMIN_PERM,
16978 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16979 	},
16980 	{
16981 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
16982 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16983 		.doit = nl80211_stop_sched_scan,
16984 		.flags = GENL_UNS_ADMIN_PERM,
16985 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16986 	},
16987 	{
16988 		.cmd = NL80211_CMD_AUTHENTICATE,
16989 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16990 		.doit = nl80211_authenticate,
16991 		.flags = GENL_UNS_ADMIN_PERM,
16992 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16993 					 NL80211_FLAG_CLEAR_SKB),
16994 	},
16995 	{
16996 		.cmd = NL80211_CMD_ASSOCIATE,
16997 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16998 		.doit = nl80211_associate,
16999 		.flags = GENL_UNS_ADMIN_PERM,
17000 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17001 					 NL80211_FLAG_CLEAR_SKB),
17002 	},
17003 	{
17004 		.cmd = NL80211_CMD_DEAUTHENTICATE,
17005 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17006 		.doit = nl80211_deauthenticate,
17007 		.flags = GENL_UNS_ADMIN_PERM,
17008 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17009 	},
17010 	{
17011 		.cmd = NL80211_CMD_DISASSOCIATE,
17012 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17013 		.doit = nl80211_disassociate,
17014 		.flags = GENL_UNS_ADMIN_PERM,
17015 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17016 	},
17017 	{
17018 		.cmd = NL80211_CMD_JOIN_IBSS,
17019 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17020 		.doit = nl80211_join_ibss,
17021 		.flags = GENL_UNS_ADMIN_PERM,
17022 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17023 	},
17024 	{
17025 		.cmd = NL80211_CMD_LEAVE_IBSS,
17026 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17027 		.doit = nl80211_leave_ibss,
17028 		.flags = GENL_UNS_ADMIN_PERM,
17029 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17030 	},
17031 #ifdef CONFIG_NL80211_TESTMODE
17032 	{
17033 		.cmd = NL80211_CMD_TESTMODE,
17034 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17035 		.doit = nl80211_testmode_do,
17036 		.dumpit = nl80211_testmode_dump,
17037 		.flags = GENL_UNS_ADMIN_PERM,
17038 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17039 	},
17040 #endif
17041 	{
17042 		.cmd = NL80211_CMD_CONNECT,
17043 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17044 		.doit = nl80211_connect,
17045 		.flags = GENL_UNS_ADMIN_PERM,
17046 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17047 					 NL80211_FLAG_CLEAR_SKB),
17048 	},
17049 	{
17050 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
17051 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17052 		.doit = nl80211_update_connect_params,
17053 		.flags = GENL_ADMIN_PERM,
17054 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17055 					 NL80211_FLAG_CLEAR_SKB),
17056 	},
17057 	{
17058 		.cmd = NL80211_CMD_DISCONNECT,
17059 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17060 		.doit = nl80211_disconnect,
17061 		.flags = GENL_UNS_ADMIN_PERM,
17062 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17063 	},
17064 	{
17065 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
17066 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17067 		.doit = nl80211_wiphy_netns,
17068 		.flags = GENL_UNS_ADMIN_PERM,
17069 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17070 					 NL80211_FLAG_NEED_RTNL |
17071 					 NL80211_FLAG_NO_WIPHY_MTX),
17072 	},
17073 	{
17074 		.cmd = NL80211_CMD_GET_SURVEY,
17075 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17076 		.dumpit = nl80211_dump_survey,
17077 	},
17078 	{
17079 		.cmd = NL80211_CMD_SET_PMKSA,
17080 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17081 		.doit = nl80211_setdel_pmksa,
17082 		.flags = GENL_UNS_ADMIN_PERM,
17083 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17084 					 NL80211_FLAG_CLEAR_SKB),
17085 	},
17086 	{
17087 		.cmd = NL80211_CMD_DEL_PMKSA,
17088 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17089 		.doit = nl80211_setdel_pmksa,
17090 		.flags = GENL_UNS_ADMIN_PERM,
17091 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17092 	},
17093 	{
17094 		.cmd = NL80211_CMD_FLUSH_PMKSA,
17095 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17096 		.doit = nl80211_flush_pmksa,
17097 		.flags = GENL_UNS_ADMIN_PERM,
17098 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17099 	},
17100 	{
17101 		.cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
17102 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17103 		.doit = nl80211_remain_on_channel,
17104 		.flags = GENL_UNS_ADMIN_PERM,
17105 		/* FIXME: requiring a link ID here is probably not good */
17106 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17107 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17108 	},
17109 	{
17110 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
17111 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17112 		.doit = nl80211_cancel_remain_on_channel,
17113 		.flags = GENL_UNS_ADMIN_PERM,
17114 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17115 	},
17116 	{
17117 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
17118 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17119 		.doit = nl80211_set_tx_bitrate_mask,
17120 		.flags = GENL_UNS_ADMIN_PERM,
17121 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17122 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17123 	},
17124 	{
17125 		.cmd = NL80211_CMD_REGISTER_FRAME,
17126 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17127 		.doit = nl80211_register_mgmt,
17128 		.flags = GENL_UNS_ADMIN_PERM,
17129 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
17130 	},
17131 	{
17132 		.cmd = NL80211_CMD_FRAME,
17133 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17134 		.doit = nl80211_tx_mgmt,
17135 		.flags = GENL_UNS_ADMIN_PERM,
17136 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17137 	},
17138 	{
17139 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
17140 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17141 		.doit = nl80211_tx_mgmt_cancel_wait,
17142 		.flags = GENL_UNS_ADMIN_PERM,
17143 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17144 	},
17145 	{
17146 		.cmd = NL80211_CMD_SET_POWER_SAVE,
17147 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17148 		.doit = nl80211_set_power_save,
17149 		.flags = GENL_UNS_ADMIN_PERM,
17150 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17151 	},
17152 	{
17153 		.cmd = NL80211_CMD_GET_POWER_SAVE,
17154 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17155 		.doit = nl80211_get_power_save,
17156 		/* can be retrieved by unprivileged users */
17157 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17158 	},
17159 	{
17160 		.cmd = NL80211_CMD_SET_CQM,
17161 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17162 		.doit = nl80211_set_cqm,
17163 		.flags = GENL_UNS_ADMIN_PERM,
17164 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17165 	},
17166 	{
17167 		.cmd = NL80211_CMD_SET_CHANNEL,
17168 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17169 		.doit = nl80211_set_channel,
17170 		.flags = GENL_UNS_ADMIN_PERM,
17171 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17172 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17173 	},
17174 	{
17175 		.cmd = NL80211_CMD_JOIN_MESH,
17176 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17177 		.doit = nl80211_join_mesh,
17178 		.flags = GENL_UNS_ADMIN_PERM,
17179 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17180 	},
17181 	{
17182 		.cmd = NL80211_CMD_LEAVE_MESH,
17183 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17184 		.doit = nl80211_leave_mesh,
17185 		.flags = GENL_UNS_ADMIN_PERM,
17186 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17187 	},
17188 	{
17189 		.cmd = NL80211_CMD_JOIN_OCB,
17190 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17191 		.doit = nl80211_join_ocb,
17192 		.flags = GENL_UNS_ADMIN_PERM,
17193 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17194 	},
17195 	{
17196 		.cmd = NL80211_CMD_LEAVE_OCB,
17197 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17198 		.doit = nl80211_leave_ocb,
17199 		.flags = GENL_UNS_ADMIN_PERM,
17200 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17201 	},
17202 #ifdef CONFIG_PM
17203 	{
17204 		.cmd = NL80211_CMD_GET_WOWLAN,
17205 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17206 		.doit = nl80211_get_wowlan,
17207 		/* can be retrieved by unprivileged users */
17208 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17209 	},
17210 	{
17211 		.cmd = NL80211_CMD_SET_WOWLAN,
17212 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17213 		.doit = nl80211_set_wowlan,
17214 		.flags = GENL_UNS_ADMIN_PERM,
17215 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17216 	},
17217 #endif
17218 	{
17219 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
17220 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17221 		.doit = nl80211_set_rekey_data,
17222 		.flags = GENL_UNS_ADMIN_PERM,
17223 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17224 					 NL80211_FLAG_CLEAR_SKB),
17225 	},
17226 	{
17227 		.cmd = NL80211_CMD_TDLS_MGMT,
17228 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17229 		.doit = nl80211_tdls_mgmt,
17230 		.flags = GENL_UNS_ADMIN_PERM,
17231 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17232 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17233 	},
17234 	{
17235 		.cmd = NL80211_CMD_TDLS_OPER,
17236 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17237 		.doit = nl80211_tdls_oper,
17238 		.flags = GENL_UNS_ADMIN_PERM,
17239 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17240 	},
17241 	{
17242 		.cmd = NL80211_CMD_UNEXPECTED_FRAME,
17243 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17244 		.doit = nl80211_register_unexpected_frame,
17245 		.flags = GENL_UNS_ADMIN_PERM,
17246 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17247 	},
17248 	{
17249 		.cmd = NL80211_CMD_PROBE_CLIENT,
17250 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17251 		.doit = nl80211_probe_client,
17252 		.flags = GENL_UNS_ADMIN_PERM,
17253 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17254 	},
17255 	{
17256 		.cmd = NL80211_CMD_REGISTER_BEACONS,
17257 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17258 		.doit = nl80211_register_beacons,
17259 		.flags = GENL_UNS_ADMIN_PERM,
17260 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17261 	},
17262 	{
17263 		.cmd = NL80211_CMD_SET_NOACK_MAP,
17264 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17265 		.doit = nl80211_set_noack_map,
17266 		.flags = GENL_UNS_ADMIN_PERM,
17267 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17268 	},
17269 	{
17270 		.cmd = NL80211_CMD_START_P2P_DEVICE,
17271 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17272 		.doit = nl80211_start_p2p_device,
17273 		.flags = GENL_UNS_ADMIN_PERM,
17274 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
17275 					 NL80211_FLAG_NEED_RTNL),
17276 	},
17277 	{
17278 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
17279 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17280 		.doit = nl80211_stop_p2p_device,
17281 		.flags = GENL_UNS_ADMIN_PERM,
17282 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17283 					 NL80211_FLAG_NEED_RTNL),
17284 	},
17285 	{
17286 		.cmd = NL80211_CMD_START_NAN,
17287 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17288 		.doit = nl80211_start_nan,
17289 		.flags = GENL_ADMIN_PERM,
17290 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
17291 					 NL80211_FLAG_NEED_RTNL),
17292 	},
17293 	{
17294 		.cmd = NL80211_CMD_STOP_NAN,
17295 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17296 		.doit = nl80211_stop_nan,
17297 		.flags = GENL_ADMIN_PERM,
17298 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17299 					 NL80211_FLAG_NEED_RTNL),
17300 	},
17301 	{
17302 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
17303 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17304 		.doit = nl80211_nan_add_func,
17305 		.flags = GENL_ADMIN_PERM,
17306 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17307 	},
17308 	{
17309 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
17310 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17311 		.doit = nl80211_nan_del_func,
17312 		.flags = GENL_ADMIN_PERM,
17313 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17314 	},
17315 	{
17316 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
17317 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17318 		.doit = nl80211_nan_change_config,
17319 		.flags = GENL_ADMIN_PERM,
17320 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17321 	},
17322 	{
17323 		.cmd = NL80211_CMD_SET_MCAST_RATE,
17324 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17325 		.doit = nl80211_set_mcast_rate,
17326 		.flags = GENL_UNS_ADMIN_PERM,
17327 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17328 	},
17329 	{
17330 		.cmd = NL80211_CMD_SET_MAC_ACL,
17331 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17332 		.doit = nl80211_set_mac_acl,
17333 		.flags = GENL_UNS_ADMIN_PERM,
17334 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17335 					 NL80211_FLAG_MLO_UNSUPPORTED),
17336 	},
17337 	{
17338 		.cmd = NL80211_CMD_RADAR_DETECT,
17339 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17340 		.doit = nl80211_start_radar_detection,
17341 		.flags = GENL_UNS_ADMIN_PERM,
17342 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17343 					 NL80211_FLAG_NO_WIPHY_MTX |
17344 					 NL80211_FLAG_MLO_UNSUPPORTED),
17345 	},
17346 	{
17347 		.cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
17348 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17349 		.doit = nl80211_get_protocol_features,
17350 	},
17351 	{
17352 		.cmd = NL80211_CMD_UPDATE_FT_IES,
17353 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17354 		.doit = nl80211_update_ft_ies,
17355 		.flags = GENL_UNS_ADMIN_PERM,
17356 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17357 	},
17358 	{
17359 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
17360 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17361 		.doit = nl80211_crit_protocol_start,
17362 		.flags = GENL_UNS_ADMIN_PERM,
17363 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17364 	},
17365 	{
17366 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
17367 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17368 		.doit = nl80211_crit_protocol_stop,
17369 		.flags = GENL_UNS_ADMIN_PERM,
17370 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17371 	},
17372 	{
17373 		.cmd = NL80211_CMD_GET_COALESCE,
17374 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17375 		.doit = nl80211_get_coalesce,
17376 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17377 	},
17378 	{
17379 		.cmd = NL80211_CMD_SET_COALESCE,
17380 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17381 		.doit = nl80211_set_coalesce,
17382 		.flags = GENL_UNS_ADMIN_PERM,
17383 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17384 	},
17385 	{
17386 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
17387 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17388 		.doit = nl80211_channel_switch,
17389 		.flags = GENL_UNS_ADMIN_PERM,
17390 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17391 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17392 	},
17393 	{
17394 		.cmd = NL80211_CMD_VENDOR,
17395 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17396 		.doit = nl80211_vendor_cmd,
17397 		.dumpit = nl80211_vendor_cmd_dump,
17398 		.flags = GENL_UNS_ADMIN_PERM,
17399 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17400 					 NL80211_FLAG_CLEAR_SKB),
17401 	},
17402 	{
17403 		.cmd = NL80211_CMD_SET_QOS_MAP,
17404 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17405 		.doit = nl80211_set_qos_map,
17406 		.flags = GENL_UNS_ADMIN_PERM,
17407 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17408 	},
17409 	{
17410 		.cmd = NL80211_CMD_ADD_TX_TS,
17411 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17412 		.doit = nl80211_add_tx_ts,
17413 		.flags = GENL_UNS_ADMIN_PERM,
17414 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17415 					 NL80211_FLAG_MLO_UNSUPPORTED),
17416 	},
17417 	{
17418 		.cmd = NL80211_CMD_DEL_TX_TS,
17419 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17420 		.doit = nl80211_del_tx_ts,
17421 		.flags = GENL_UNS_ADMIN_PERM,
17422 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17423 	},
17424 	{
17425 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
17426 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17427 		.doit = nl80211_tdls_channel_switch,
17428 		.flags = GENL_UNS_ADMIN_PERM,
17429 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17430 	},
17431 	{
17432 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
17433 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17434 		.doit = nl80211_tdls_cancel_channel_switch,
17435 		.flags = GENL_UNS_ADMIN_PERM,
17436 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17437 	},
17438 	{
17439 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
17440 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17441 		.doit = nl80211_set_multicast_to_unicast,
17442 		.flags = GENL_UNS_ADMIN_PERM,
17443 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17444 	},
17445 	{
17446 		.cmd = NL80211_CMD_SET_PMK,
17447 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17448 		.doit = nl80211_set_pmk,
17449 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17450 					 NL80211_FLAG_CLEAR_SKB),
17451 	},
17452 	{
17453 		.cmd = NL80211_CMD_DEL_PMK,
17454 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17455 		.doit = nl80211_del_pmk,
17456 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17457 	},
17458 	{
17459 		.cmd = NL80211_CMD_EXTERNAL_AUTH,
17460 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17461 		.doit = nl80211_external_auth,
17462 		.flags = GENL_ADMIN_PERM,
17463 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17464 	},
17465 	{
17466 		.cmd = NL80211_CMD_CONTROL_PORT_FRAME,
17467 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17468 		.doit = nl80211_tx_control_port,
17469 		.flags = GENL_UNS_ADMIN_PERM,
17470 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17471 	},
17472 	{
17473 		.cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
17474 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17475 		.doit = nl80211_get_ftm_responder_stats,
17476 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17477 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17478 	},
17479 	{
17480 		.cmd = NL80211_CMD_PEER_MEASUREMENT_START,
17481 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17482 		.doit = nl80211_pmsr_start,
17483 		.flags = GENL_UNS_ADMIN_PERM,
17484 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17485 	},
17486 	{
17487 		.cmd = NL80211_CMD_NOTIFY_RADAR,
17488 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17489 		.doit = nl80211_notify_radar_detection,
17490 		.flags = GENL_UNS_ADMIN_PERM,
17491 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17492 	},
17493 	{
17494 		.cmd = NL80211_CMD_UPDATE_OWE_INFO,
17495 		.doit = nl80211_update_owe_info,
17496 		.flags = GENL_ADMIN_PERM,
17497 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17498 	},
17499 	{
17500 		.cmd = NL80211_CMD_PROBE_MESH_LINK,
17501 		.doit = nl80211_probe_mesh_link,
17502 		.flags = GENL_UNS_ADMIN_PERM,
17503 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17504 	},
17505 	{
17506 		.cmd = NL80211_CMD_SET_TID_CONFIG,
17507 		.doit = nl80211_set_tid_config,
17508 		.flags = GENL_UNS_ADMIN_PERM,
17509 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17510 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17511 	},
17512 	{
17513 		.cmd = NL80211_CMD_SET_SAR_SPECS,
17514 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17515 		.doit = nl80211_set_sar_specs,
17516 		.flags = GENL_UNS_ADMIN_PERM,
17517 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17518 					 NL80211_FLAG_NEED_RTNL),
17519 	},
17520 	{
17521 		.cmd = NL80211_CMD_COLOR_CHANGE_REQUEST,
17522 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17523 		.doit = nl80211_color_change,
17524 		.flags = GENL_UNS_ADMIN_PERM,
17525 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17526 	},
17527 	{
17528 		.cmd = NL80211_CMD_SET_FILS_AAD,
17529 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17530 		.doit = nl80211_set_fils_aad,
17531 		.flags = GENL_UNS_ADMIN_PERM,
17532 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17533 	},
17534 	{
17535 		.cmd = NL80211_CMD_ADD_LINK,
17536 		.doit = nl80211_add_link,
17537 		.flags = GENL_UNS_ADMIN_PERM,
17538 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17539 	},
17540 	{
17541 		.cmd = NL80211_CMD_REMOVE_LINK,
17542 		.doit = nl80211_remove_link,
17543 		.flags = GENL_UNS_ADMIN_PERM,
17544 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17545 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17546 	},
17547 	{
17548 		.cmd = NL80211_CMD_ADD_LINK_STA,
17549 		.doit = nl80211_add_link_station,
17550 		.flags = GENL_UNS_ADMIN_PERM,
17551 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17552 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17553 	},
17554 	{
17555 		.cmd = NL80211_CMD_MODIFY_LINK_STA,
17556 		.doit = nl80211_modify_link_station,
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_REMOVE_LINK_STA,
17563 		.doit = nl80211_remove_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_SET_HW_TIMESTAMP,
17570 		.doit = nl80211_set_hw_timestamp,
17571 		.flags = GENL_UNS_ADMIN_PERM,
17572 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17573 	},
17574 };
17575 
17576 static struct genl_family nl80211_fam __ro_after_init = {
17577 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
17578 	.hdrsize = 0,			/* no private header */
17579 	.version = 1,			/* no particular meaning now */
17580 	.maxattr = NL80211_ATTR_MAX,
17581 	.policy = nl80211_policy,
17582 	.netnsok = true,
17583 	.pre_doit = nl80211_pre_doit,
17584 	.post_doit = nl80211_post_doit,
17585 	.module = THIS_MODULE,
17586 	.ops = nl80211_ops,
17587 	.n_ops = ARRAY_SIZE(nl80211_ops),
17588 	.small_ops = nl80211_small_ops,
17589 	.n_small_ops = ARRAY_SIZE(nl80211_small_ops),
17590 	.resv_start_op = NL80211_CMD_REMOVE_LINK_STA + 1,
17591 	.mcgrps = nl80211_mcgrps,
17592 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
17593 	.parallel_ops = true,
17594 };
17595 
17596 /* notification functions */
17597 
17598 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
17599 			  enum nl80211_commands cmd)
17600 {
17601 	struct sk_buff *msg;
17602 	struct nl80211_dump_wiphy_state state = {};
17603 
17604 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
17605 		cmd != NL80211_CMD_DEL_WIPHY);
17606 
17607 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17608 	if (!msg)
17609 		return;
17610 
17611 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
17612 		nlmsg_free(msg);
17613 		return;
17614 	}
17615 
17616 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17617 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17618 }
17619 
17620 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
17621 				struct wireless_dev *wdev,
17622 				enum nl80211_commands cmd)
17623 {
17624 	struct sk_buff *msg;
17625 
17626 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17627 	if (!msg)
17628 		return;
17629 
17630 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
17631 		nlmsg_free(msg);
17632 		return;
17633 	}
17634 
17635 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17636 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17637 }
17638 
17639 static int nl80211_add_scan_req(struct sk_buff *msg,
17640 				struct cfg80211_registered_device *rdev)
17641 {
17642 	struct cfg80211_scan_request *req = rdev->scan_req;
17643 	struct nlattr *nest;
17644 	int i;
17645 	struct cfg80211_scan_info *info;
17646 
17647 	if (WARN_ON(!req))
17648 		return 0;
17649 
17650 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
17651 	if (!nest)
17652 		goto nla_put_failure;
17653 	for (i = 0; i < req->n_ssids; i++) {
17654 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
17655 			goto nla_put_failure;
17656 	}
17657 	nla_nest_end(msg, nest);
17658 
17659 	if (req->flags & NL80211_SCAN_FLAG_FREQ_KHZ) {
17660 		nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQ_KHZ);
17661 		if (!nest)
17662 			goto nla_put_failure;
17663 		for (i = 0; i < req->n_channels; i++) {
17664 			if (nla_put_u32(msg, i,
17665 				   ieee80211_channel_to_khz(req->channels[i])))
17666 				goto nla_put_failure;
17667 		}
17668 		nla_nest_end(msg, nest);
17669 	} else {
17670 		nest = nla_nest_start_noflag(msg,
17671 					     NL80211_ATTR_SCAN_FREQUENCIES);
17672 		if (!nest)
17673 			goto nla_put_failure;
17674 		for (i = 0; i < req->n_channels; i++) {
17675 			if (nla_put_u32(msg, i, req->channels[i]->center_freq))
17676 				goto nla_put_failure;
17677 		}
17678 		nla_nest_end(msg, nest);
17679 	}
17680 
17681 	if (req->ie &&
17682 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
17683 		goto nla_put_failure;
17684 
17685 	if (req->flags &&
17686 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
17687 		goto nla_put_failure;
17688 
17689 	info = rdev->int_scan_req ? &rdev->int_scan_req->info :
17690 		&rdev->scan_req->info;
17691 	if (info->scan_start_tsf &&
17692 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
17693 			       info->scan_start_tsf, NL80211_BSS_PAD) ||
17694 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
17695 		     info->tsf_bssid)))
17696 		goto nla_put_failure;
17697 
17698 	return 0;
17699  nla_put_failure:
17700 	return -ENOBUFS;
17701 }
17702 
17703 static int nl80211_prep_scan_msg(struct sk_buff *msg,
17704 				 struct cfg80211_registered_device *rdev,
17705 				 struct wireless_dev *wdev,
17706 				 u32 portid, u32 seq, int flags,
17707 				 u32 cmd)
17708 {
17709 	void *hdr;
17710 
17711 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
17712 	if (!hdr)
17713 		return -1;
17714 
17715 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17716 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
17717 					 wdev->netdev->ifindex)) ||
17718 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17719 			      NL80211_ATTR_PAD))
17720 		goto nla_put_failure;
17721 
17722 	/* ignore errors and send incomplete event anyway */
17723 	nl80211_add_scan_req(msg, rdev);
17724 
17725 	genlmsg_end(msg, hdr);
17726 	return 0;
17727 
17728  nla_put_failure:
17729 	genlmsg_cancel(msg, hdr);
17730 	return -EMSGSIZE;
17731 }
17732 
17733 static int
17734 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
17735 			    struct cfg80211_sched_scan_request *req, u32 cmd)
17736 {
17737 	void *hdr;
17738 
17739 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17740 	if (!hdr)
17741 		return -1;
17742 
17743 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
17744 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
17745 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
17746 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
17747 			      NL80211_ATTR_PAD))
17748 		goto nla_put_failure;
17749 
17750 	genlmsg_end(msg, hdr);
17751 	return 0;
17752 
17753  nla_put_failure:
17754 	genlmsg_cancel(msg, hdr);
17755 	return -EMSGSIZE;
17756 }
17757 
17758 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
17759 			     struct wireless_dev *wdev)
17760 {
17761 	struct sk_buff *msg;
17762 
17763 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17764 	if (!msg)
17765 		return;
17766 
17767 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17768 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
17769 		nlmsg_free(msg);
17770 		return;
17771 	}
17772 
17773 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17774 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17775 }
17776 
17777 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
17778 				       struct wireless_dev *wdev, bool aborted)
17779 {
17780 	struct sk_buff *msg;
17781 
17782 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17783 	if (!msg)
17784 		return NULL;
17785 
17786 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17787 				  aborted ? NL80211_CMD_SCAN_ABORTED :
17788 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
17789 		nlmsg_free(msg);
17790 		return NULL;
17791 	}
17792 
17793 	return msg;
17794 }
17795 
17796 /* send message created by nl80211_build_scan_msg() */
17797 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
17798 			   struct sk_buff *msg)
17799 {
17800 	if (!msg)
17801 		return;
17802 
17803 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17804 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17805 }
17806 
17807 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
17808 {
17809 	struct sk_buff *msg;
17810 
17811 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17812 	if (!msg)
17813 		return;
17814 
17815 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
17816 		nlmsg_free(msg);
17817 		return;
17818 	}
17819 
17820 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
17821 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17822 }
17823 
17824 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
17825 					  struct regulatory_request *request)
17826 {
17827 	/* Userspace can always count this one always being set */
17828 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
17829 		goto nla_put_failure;
17830 
17831 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
17832 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17833 			       NL80211_REGDOM_TYPE_WORLD))
17834 			goto nla_put_failure;
17835 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
17836 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17837 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
17838 			goto nla_put_failure;
17839 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
17840 		   request->intersect) {
17841 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17842 			       NL80211_REGDOM_TYPE_INTERSECTION))
17843 			goto nla_put_failure;
17844 	} else {
17845 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17846 			       NL80211_REGDOM_TYPE_COUNTRY) ||
17847 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
17848 				   request->alpha2))
17849 			goto nla_put_failure;
17850 	}
17851 
17852 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
17853 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
17854 
17855 		if (wiphy &&
17856 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
17857 			goto nla_put_failure;
17858 
17859 		if (wiphy &&
17860 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
17861 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
17862 			goto nla_put_failure;
17863 	}
17864 
17865 	return true;
17866 
17867 nla_put_failure:
17868 	return false;
17869 }
17870 
17871 /*
17872  * This can happen on global regulatory changes or device specific settings
17873  * based on custom regulatory domains.
17874  */
17875 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
17876 				     struct regulatory_request *request)
17877 {
17878 	struct sk_buff *msg;
17879 	void *hdr;
17880 
17881 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17882 	if (!msg)
17883 		return;
17884 
17885 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
17886 	if (!hdr)
17887 		goto nla_put_failure;
17888 
17889 	if (!nl80211_reg_change_event_fill(msg, request))
17890 		goto nla_put_failure;
17891 
17892 	genlmsg_end(msg, hdr);
17893 
17894 	rcu_read_lock();
17895 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
17896 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
17897 	rcu_read_unlock();
17898 
17899 	return;
17900 
17901 nla_put_failure:
17902 	nlmsg_free(msg);
17903 }
17904 
17905 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
17906 				    struct net_device *netdev,
17907 				    const u8 *buf, size_t len,
17908 				    enum nl80211_commands cmd, gfp_t gfp,
17909 				    int uapsd_queues, const u8 *req_ies,
17910 				    size_t req_ies_len, bool reconnect)
17911 {
17912 	struct sk_buff *msg;
17913 	void *hdr;
17914 
17915 	msg = nlmsg_new(100 + len + req_ies_len, gfp);
17916 	if (!msg)
17917 		return;
17918 
17919 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17920 	if (!hdr) {
17921 		nlmsg_free(msg);
17922 		return;
17923 	}
17924 
17925 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17926 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17927 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
17928 	    (req_ies &&
17929 	     nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
17930 		goto nla_put_failure;
17931 
17932 	if (reconnect && nla_put_flag(msg, NL80211_ATTR_RECONNECT_REQUESTED))
17933 		goto nla_put_failure;
17934 
17935 	if (uapsd_queues >= 0) {
17936 		struct nlattr *nla_wmm =
17937 			nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
17938 		if (!nla_wmm)
17939 			goto nla_put_failure;
17940 
17941 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
17942 			       uapsd_queues))
17943 			goto nla_put_failure;
17944 
17945 		nla_nest_end(msg, nla_wmm);
17946 	}
17947 
17948 	genlmsg_end(msg, hdr);
17949 
17950 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17951 				NL80211_MCGRP_MLME, gfp);
17952 	return;
17953 
17954  nla_put_failure:
17955 	nlmsg_free(msg);
17956 }
17957 
17958 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
17959 			  struct net_device *netdev, const u8 *buf,
17960 			  size_t len, gfp_t gfp)
17961 {
17962 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17963 				NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0,
17964 				false);
17965 }
17966 
17967 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
17968 			   struct net_device *netdev,
17969 			   struct cfg80211_rx_assoc_resp *data)
17970 {
17971 	nl80211_send_mlme_event(rdev, netdev, data->buf, data->len,
17972 				NL80211_CMD_ASSOCIATE, GFP_KERNEL,
17973 				data->uapsd_queues,
17974 				data->req_ies, data->req_ies_len, false);
17975 }
17976 
17977 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
17978 			 struct net_device *netdev, const u8 *buf,
17979 			 size_t len, bool reconnect, gfp_t gfp)
17980 {
17981 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17982 				NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0,
17983 				reconnect);
17984 }
17985 
17986 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
17987 			   struct net_device *netdev, const u8 *buf,
17988 			   size_t len, bool reconnect, gfp_t gfp)
17989 {
17990 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17991 				NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0,
17992 				reconnect);
17993 }
17994 
17995 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
17996 				  size_t len)
17997 {
17998 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17999 	struct wiphy *wiphy = wdev->wiphy;
18000 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18001 	const struct ieee80211_mgmt *mgmt = (void *)buf;
18002 	u32 cmd;
18003 
18004 	if (WARN_ON(len < 2))
18005 		return;
18006 
18007 	if (ieee80211_is_deauth(mgmt->frame_control)) {
18008 		cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
18009 	} else if (ieee80211_is_disassoc(mgmt->frame_control)) {
18010 		cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
18011 	} else if (ieee80211_is_beacon(mgmt->frame_control)) {
18012 		if (wdev->unprot_beacon_reported &&
18013 		    elapsed_jiffies_msecs(wdev->unprot_beacon_reported) < 10000)
18014 			return;
18015 		cmd = NL80211_CMD_UNPROT_BEACON;
18016 		wdev->unprot_beacon_reported = jiffies;
18017 	} else {
18018 		return;
18019 	}
18020 
18021 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
18022 	nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
18023 				NULL, 0, false);
18024 }
18025 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
18026 
18027 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
18028 				      struct net_device *netdev, int cmd,
18029 				      const u8 *addr, gfp_t gfp)
18030 {
18031 	struct sk_buff *msg;
18032 	void *hdr;
18033 
18034 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18035 	if (!msg)
18036 		return;
18037 
18038 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18039 	if (!hdr) {
18040 		nlmsg_free(msg);
18041 		return;
18042 	}
18043 
18044 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18045 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18046 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
18047 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
18048 		goto nla_put_failure;
18049 
18050 	genlmsg_end(msg, hdr);
18051 
18052 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18053 				NL80211_MCGRP_MLME, gfp);
18054 	return;
18055 
18056  nla_put_failure:
18057 	nlmsg_free(msg);
18058 }
18059 
18060 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
18061 			       struct net_device *netdev, const u8 *addr,
18062 			       gfp_t gfp)
18063 {
18064 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
18065 				  addr, gfp);
18066 }
18067 
18068 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
18069 				struct net_device *netdev, const u8 *addr,
18070 				gfp_t gfp)
18071 {
18072 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
18073 				  addr, gfp);
18074 }
18075 
18076 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
18077 				 struct net_device *netdev,
18078 				 struct cfg80211_connect_resp_params *cr,
18079 				 gfp_t gfp)
18080 {
18081 	struct sk_buff *msg;
18082 	void *hdr;
18083 	unsigned int link;
18084 	size_t link_info_size = 0;
18085 	const u8 *connected_addr = cr->valid_links ?
18086 				   cr->ap_mld_addr : cr->links[0].bssid;
18087 
18088 	if (cr->valid_links) {
18089 		for_each_valid_link(cr, link) {
18090 			/* Nested attribute header */
18091 			link_info_size += NLA_HDRLEN;
18092 			/* Link ID */
18093 			link_info_size += nla_total_size(sizeof(u8));
18094 			link_info_size += cr->links[link].addr ?
18095 					  nla_total_size(ETH_ALEN) : 0;
18096 			link_info_size += (cr->links[link].bssid ||
18097 					   cr->links[link].bss) ?
18098 					  nla_total_size(ETH_ALEN) : 0;
18099 			link_info_size += nla_total_size(sizeof(u16));
18100 		}
18101 	}
18102 
18103 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
18104 			cr->fils.kek_len + cr->fils.pmk_len +
18105 			(cr->fils.pmkid ? WLAN_PMKID_LEN : 0) + link_info_size,
18106 			gfp);
18107 	if (!msg)
18108 		return;
18109 
18110 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
18111 	if (!hdr) {
18112 		nlmsg_free(msg);
18113 		return;
18114 	}
18115 
18116 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18117 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18118 	    (connected_addr &&
18119 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr)) ||
18120 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
18121 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
18122 			cr->status) ||
18123 	    (cr->status < 0 &&
18124 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
18125 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
18126 			  cr->timeout_reason))) ||
18127 	    (cr->req_ie &&
18128 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
18129 	    (cr->resp_ie &&
18130 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
18131 		     cr->resp_ie)) ||
18132 	    (cr->fils.update_erp_next_seq_num &&
18133 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
18134 			 cr->fils.erp_next_seq_num)) ||
18135 	    (cr->status == WLAN_STATUS_SUCCESS &&
18136 	     ((cr->fils.kek &&
18137 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
18138 		       cr->fils.kek)) ||
18139 	      (cr->fils.pmk &&
18140 	       nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
18141 	      (cr->fils.pmkid &&
18142 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
18143 		goto nla_put_failure;
18144 
18145 	if (cr->valid_links) {
18146 		int i = 1;
18147 		struct nlattr *nested;
18148 
18149 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18150 		if (!nested)
18151 			goto nla_put_failure;
18152 
18153 		for_each_valid_link(cr, link) {
18154 			struct nlattr *nested_mlo_links;
18155 			const u8 *bssid = cr->links[link].bss ?
18156 					  cr->links[link].bss->bssid :
18157 					  cr->links[link].bssid;
18158 
18159 			nested_mlo_links = nla_nest_start(msg, i);
18160 			if (!nested_mlo_links)
18161 				goto nla_put_failure;
18162 
18163 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
18164 			    (bssid &&
18165 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
18166 			    (cr->links[link].addr &&
18167 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
18168 				     cr->links[link].addr)) ||
18169 			    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
18170 					cr->links[link].status))
18171 				goto nla_put_failure;
18172 
18173 			nla_nest_end(msg, nested_mlo_links);
18174 			i++;
18175 		}
18176 		nla_nest_end(msg, nested);
18177 	}
18178 
18179 	genlmsg_end(msg, hdr);
18180 
18181 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18182 				NL80211_MCGRP_MLME, gfp);
18183 	return;
18184 
18185  nla_put_failure:
18186 	nlmsg_free(msg);
18187 }
18188 
18189 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
18190 			 struct net_device *netdev,
18191 			 struct cfg80211_roam_info *info, gfp_t gfp)
18192 {
18193 	struct sk_buff *msg;
18194 	void *hdr;
18195 	size_t link_info_size = 0;
18196 	unsigned int link;
18197 	const u8 *connected_addr = info->ap_mld_addr ?
18198 				   info->ap_mld_addr :
18199 				   (info->links[0].bss ?
18200 				    info->links[0].bss->bssid :
18201 				    info->links[0].bssid);
18202 
18203 	if (info->valid_links) {
18204 		for_each_valid_link(info, link) {
18205 			/* Nested attribute header */
18206 			link_info_size += NLA_HDRLEN;
18207 			/* Link ID */
18208 			link_info_size += nla_total_size(sizeof(u8));
18209 			link_info_size += info->links[link].addr ?
18210 					  nla_total_size(ETH_ALEN) : 0;
18211 			link_info_size += (info->links[link].bssid ||
18212 					   info->links[link].bss) ?
18213 					  nla_total_size(ETH_ALEN) : 0;
18214 		}
18215 	}
18216 
18217 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
18218 			info->fils.kek_len + info->fils.pmk_len +
18219 			(info->fils.pmkid ? WLAN_PMKID_LEN : 0) +
18220 			link_info_size, gfp);
18221 	if (!msg)
18222 		return;
18223 
18224 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
18225 	if (!hdr) {
18226 		nlmsg_free(msg);
18227 		return;
18228 	}
18229 
18230 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18231 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18232 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr) ||
18233 	    (info->req_ie &&
18234 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
18235 		     info->req_ie)) ||
18236 	    (info->resp_ie &&
18237 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
18238 		     info->resp_ie)) ||
18239 	    (info->fils.update_erp_next_seq_num &&
18240 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
18241 			 info->fils.erp_next_seq_num)) ||
18242 	    (info->fils.kek &&
18243 	     nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
18244 		     info->fils.kek)) ||
18245 	    (info->fils.pmk &&
18246 	     nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
18247 	    (info->fils.pmkid &&
18248 	     nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
18249 		goto nla_put_failure;
18250 
18251 	if (info->valid_links) {
18252 		int i = 1;
18253 		struct nlattr *nested;
18254 
18255 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18256 		if (!nested)
18257 			goto nla_put_failure;
18258 
18259 		for_each_valid_link(info, link) {
18260 			struct nlattr *nested_mlo_links;
18261 			const u8 *bssid = info->links[link].bss ?
18262 					  info->links[link].bss->bssid :
18263 					  info->links[link].bssid;
18264 
18265 			nested_mlo_links = nla_nest_start(msg, i);
18266 			if (!nested_mlo_links)
18267 				goto nla_put_failure;
18268 
18269 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
18270 			    (bssid &&
18271 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
18272 			    (info->links[link].addr &&
18273 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
18274 				     info->links[link].addr)))
18275 				goto nla_put_failure;
18276 
18277 			nla_nest_end(msg, nested_mlo_links);
18278 			i++;
18279 		}
18280 		nla_nest_end(msg, nested);
18281 	}
18282 
18283 	genlmsg_end(msg, hdr);
18284 
18285 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18286 				NL80211_MCGRP_MLME, gfp);
18287 	return;
18288 
18289  nla_put_failure:
18290 	nlmsg_free(msg);
18291 }
18292 
18293 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
18294 				  struct net_device *netdev, const u8 *bssid,
18295 				  const u8 *td_bitmap, u8 td_bitmap_len)
18296 {
18297 	struct sk_buff *msg;
18298 	void *hdr;
18299 
18300 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18301 	if (!msg)
18302 		return;
18303 
18304 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
18305 	if (!hdr) {
18306 		nlmsg_free(msg);
18307 		return;
18308 	}
18309 
18310 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18311 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18312 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18313 		goto nla_put_failure;
18314 
18315 	if ((td_bitmap_len > 0) && td_bitmap)
18316 		if (nla_put(msg, NL80211_ATTR_TD_BITMAP,
18317 			    td_bitmap_len, td_bitmap))
18318 			goto nla_put_failure;
18319 
18320 	genlmsg_end(msg, hdr);
18321 
18322 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18323 				NL80211_MCGRP_MLME, GFP_KERNEL);
18324 	return;
18325 
18326  nla_put_failure:
18327 	nlmsg_free(msg);
18328 }
18329 
18330 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
18331 			       struct net_device *netdev, u16 reason,
18332 			       const u8 *ie, size_t ie_len, bool from_ap)
18333 {
18334 	struct sk_buff *msg;
18335 	void *hdr;
18336 
18337 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
18338 	if (!msg)
18339 		return;
18340 
18341 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
18342 	if (!hdr) {
18343 		nlmsg_free(msg);
18344 		return;
18345 	}
18346 
18347 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18348 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18349 	    (reason &&
18350 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
18351 	    (from_ap &&
18352 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
18353 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
18354 		goto nla_put_failure;
18355 
18356 	genlmsg_end(msg, hdr);
18357 
18358 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18359 				NL80211_MCGRP_MLME, GFP_KERNEL);
18360 	return;
18361 
18362  nla_put_failure:
18363 	nlmsg_free(msg);
18364 }
18365 
18366 void cfg80211_links_removed(struct net_device *dev, u16 link_mask)
18367 {
18368 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18369 	struct wiphy *wiphy = wdev->wiphy;
18370 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18371 	struct sk_buff *msg;
18372 	struct nlattr *links;
18373 	void *hdr;
18374 
18375 	ASSERT_WDEV_LOCK(wdev);
18376 	trace_cfg80211_links_removed(dev, link_mask);
18377 
18378 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
18379 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
18380 		return;
18381 
18382 	if (WARN_ON(!wdev->valid_links || !link_mask ||
18383 		    (wdev->valid_links & link_mask) != link_mask ||
18384 		    wdev->valid_links == link_mask))
18385 		return;
18386 
18387 	cfg80211_wdev_release_link_bsses(wdev, link_mask);
18388 	wdev->valid_links &= ~link_mask;
18389 
18390 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18391 	if (!msg)
18392 		return;
18393 
18394 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_LINKS_REMOVED);
18395 	if (!hdr) {
18396 		nlmsg_free(msg);
18397 		return;
18398 	}
18399 
18400 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18401 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
18402 		goto nla_put_failure;
18403 
18404 	links = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18405 	if (!links)
18406 		goto nla_put_failure;
18407 
18408 	while (link_mask) {
18409 		struct nlattr *link;
18410 		int link_id = __ffs(link_mask);
18411 
18412 		link = nla_nest_start(msg, link_id + 1);
18413 		if (!link)
18414 			goto nla_put_failure;
18415 
18416 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
18417 			goto nla_put_failure;
18418 
18419 		nla_nest_end(msg, link);
18420 		link_mask &= ~(1 << link_id);
18421 	}
18422 
18423 	nla_nest_end(msg, links);
18424 
18425 	genlmsg_end(msg, hdr);
18426 
18427 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18428 				NL80211_MCGRP_MLME, GFP_KERNEL);
18429 	return;
18430 
18431  nla_put_failure:
18432 	nlmsg_free(msg);
18433 }
18434 EXPORT_SYMBOL(cfg80211_links_removed);
18435 
18436 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
18437 			     struct net_device *netdev, const u8 *bssid,
18438 			     gfp_t gfp)
18439 {
18440 	struct sk_buff *msg;
18441 	void *hdr;
18442 
18443 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18444 	if (!msg)
18445 		return;
18446 
18447 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
18448 	if (!hdr) {
18449 		nlmsg_free(msg);
18450 		return;
18451 	}
18452 
18453 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18454 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18455 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18456 		goto nla_put_failure;
18457 
18458 	genlmsg_end(msg, hdr);
18459 
18460 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18461 				NL80211_MCGRP_MLME, gfp);
18462 	return;
18463 
18464  nla_put_failure:
18465 	nlmsg_free(msg);
18466 }
18467 
18468 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
18469 					const u8 *ie, u8 ie_len,
18470 					int sig_dbm, gfp_t gfp)
18471 {
18472 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18473 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18474 	struct sk_buff *msg;
18475 	void *hdr;
18476 
18477 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
18478 		return;
18479 
18480 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
18481 
18482 	msg = nlmsg_new(100 + ie_len, gfp);
18483 	if (!msg)
18484 		return;
18485 
18486 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
18487 	if (!hdr) {
18488 		nlmsg_free(msg);
18489 		return;
18490 	}
18491 
18492 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18493 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18494 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
18495 	    (ie_len && ie &&
18496 	     nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
18497 	    (sig_dbm &&
18498 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
18499 		goto nla_put_failure;
18500 
18501 	genlmsg_end(msg, hdr);
18502 
18503 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18504 				NL80211_MCGRP_MLME, gfp);
18505 	return;
18506 
18507  nla_put_failure:
18508 	nlmsg_free(msg);
18509 }
18510 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
18511 
18512 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
18513 				 struct net_device *netdev, const u8 *addr,
18514 				 enum nl80211_key_type key_type, int key_id,
18515 				 const u8 *tsc, gfp_t gfp)
18516 {
18517 	struct sk_buff *msg;
18518 	void *hdr;
18519 
18520 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18521 	if (!msg)
18522 		return;
18523 
18524 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
18525 	if (!hdr) {
18526 		nlmsg_free(msg);
18527 		return;
18528 	}
18529 
18530 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18531 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18532 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
18533 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
18534 	    (key_id != -1 &&
18535 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
18536 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
18537 		goto nla_put_failure;
18538 
18539 	genlmsg_end(msg, hdr);
18540 
18541 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18542 				NL80211_MCGRP_MLME, gfp);
18543 	return;
18544 
18545  nla_put_failure:
18546 	nlmsg_free(msg);
18547 }
18548 
18549 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
18550 				    struct ieee80211_channel *channel_before,
18551 				    struct ieee80211_channel *channel_after)
18552 {
18553 	struct sk_buff *msg;
18554 	void *hdr;
18555 	struct nlattr *nl_freq;
18556 
18557 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
18558 	if (!msg)
18559 		return;
18560 
18561 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
18562 	if (!hdr) {
18563 		nlmsg_free(msg);
18564 		return;
18565 	}
18566 
18567 	/*
18568 	 * Since we are applying the beacon hint to a wiphy we know its
18569 	 * wiphy_idx is valid
18570 	 */
18571 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
18572 		goto nla_put_failure;
18573 
18574 	/* Before */
18575 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
18576 	if (!nl_freq)
18577 		goto nla_put_failure;
18578 
18579 	if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
18580 		goto nla_put_failure;
18581 	nla_nest_end(msg, nl_freq);
18582 
18583 	/* After */
18584 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
18585 	if (!nl_freq)
18586 		goto nla_put_failure;
18587 
18588 	if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
18589 		goto nla_put_failure;
18590 	nla_nest_end(msg, nl_freq);
18591 
18592 	genlmsg_end(msg, hdr);
18593 
18594 	rcu_read_lock();
18595 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
18596 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
18597 	rcu_read_unlock();
18598 
18599 	return;
18600 
18601 nla_put_failure:
18602 	nlmsg_free(msg);
18603 }
18604 
18605 static void nl80211_send_remain_on_chan_event(
18606 	int cmd, struct cfg80211_registered_device *rdev,
18607 	struct wireless_dev *wdev, u64 cookie,
18608 	struct ieee80211_channel *chan,
18609 	unsigned int duration, gfp_t gfp)
18610 {
18611 	struct sk_buff *msg;
18612 	void *hdr;
18613 
18614 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18615 	if (!msg)
18616 		return;
18617 
18618 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18619 	if (!hdr) {
18620 		nlmsg_free(msg);
18621 		return;
18622 	}
18623 
18624 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18625 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18626 					 wdev->netdev->ifindex)) ||
18627 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18628 			      NL80211_ATTR_PAD) ||
18629 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
18630 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
18631 			NL80211_CHAN_NO_HT) ||
18632 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
18633 			      NL80211_ATTR_PAD))
18634 		goto nla_put_failure;
18635 
18636 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
18637 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
18638 		goto nla_put_failure;
18639 
18640 	genlmsg_end(msg, hdr);
18641 
18642 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18643 				NL80211_MCGRP_MLME, gfp);
18644 	return;
18645 
18646  nla_put_failure:
18647 	nlmsg_free(msg);
18648 }
18649 
18650 void cfg80211_assoc_comeback(struct net_device *netdev,
18651 			     const u8 *ap_addr, u32 timeout)
18652 {
18653 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
18654 	struct wiphy *wiphy = wdev->wiphy;
18655 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18656 	struct sk_buff *msg;
18657 	void *hdr;
18658 
18659 	trace_cfg80211_assoc_comeback(wdev, ap_addr, timeout);
18660 
18661 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18662 	if (!msg)
18663 		return;
18664 
18665 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ASSOC_COMEBACK);
18666 	if (!hdr) {
18667 		nlmsg_free(msg);
18668 		return;
18669 	}
18670 
18671 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18672 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18673 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ap_addr) ||
18674 	    nla_put_u32(msg, NL80211_ATTR_TIMEOUT, timeout))
18675 		goto nla_put_failure;
18676 
18677 	genlmsg_end(msg, hdr);
18678 
18679 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18680 				NL80211_MCGRP_MLME, GFP_KERNEL);
18681 	return;
18682 
18683  nla_put_failure:
18684 	nlmsg_free(msg);
18685 }
18686 EXPORT_SYMBOL(cfg80211_assoc_comeback);
18687 
18688 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
18689 			       struct ieee80211_channel *chan,
18690 			       unsigned int duration, gfp_t gfp)
18691 {
18692 	struct wiphy *wiphy = wdev->wiphy;
18693 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18694 
18695 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
18696 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
18697 					  rdev, wdev, cookie, chan,
18698 					  duration, gfp);
18699 }
18700 EXPORT_SYMBOL(cfg80211_ready_on_channel);
18701 
18702 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
18703 					struct ieee80211_channel *chan,
18704 					gfp_t gfp)
18705 {
18706 	struct wiphy *wiphy = wdev->wiphy;
18707 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18708 
18709 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
18710 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
18711 					  rdev, wdev, cookie, chan, 0, gfp);
18712 }
18713 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
18714 
18715 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
18716 					struct ieee80211_channel *chan,
18717 					gfp_t gfp)
18718 {
18719 	struct wiphy *wiphy = wdev->wiphy;
18720 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18721 
18722 	trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
18723 	nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
18724 					  rdev, wdev, cookie, chan, 0, gfp);
18725 }
18726 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
18727 
18728 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
18729 		      struct station_info *sinfo, gfp_t gfp)
18730 {
18731 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18732 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18733 	struct sk_buff *msg;
18734 
18735 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
18736 
18737 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18738 	if (!msg)
18739 		return;
18740 
18741 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
18742 				 rdev, dev, mac_addr, sinfo) < 0) {
18743 		nlmsg_free(msg);
18744 		return;
18745 	}
18746 
18747 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18748 				NL80211_MCGRP_MLME, gfp);
18749 }
18750 EXPORT_SYMBOL(cfg80211_new_sta);
18751 
18752 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
18753 			    struct station_info *sinfo, gfp_t gfp)
18754 {
18755 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18756 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18757 	struct sk_buff *msg;
18758 	struct station_info empty_sinfo = {};
18759 
18760 	if (!sinfo)
18761 		sinfo = &empty_sinfo;
18762 
18763 	trace_cfg80211_del_sta(dev, mac_addr);
18764 
18765 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18766 	if (!msg) {
18767 		cfg80211_sinfo_release_content(sinfo);
18768 		return;
18769 	}
18770 
18771 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
18772 				 rdev, dev, mac_addr, sinfo) < 0) {
18773 		nlmsg_free(msg);
18774 		return;
18775 	}
18776 
18777 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18778 				NL80211_MCGRP_MLME, gfp);
18779 }
18780 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
18781 
18782 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
18783 			  enum nl80211_connect_failed_reason reason,
18784 			  gfp_t gfp)
18785 {
18786 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18787 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18788 	struct sk_buff *msg;
18789 	void *hdr;
18790 
18791 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
18792 	if (!msg)
18793 		return;
18794 
18795 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
18796 	if (!hdr) {
18797 		nlmsg_free(msg);
18798 		return;
18799 	}
18800 
18801 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18802 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
18803 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
18804 		goto nla_put_failure;
18805 
18806 	genlmsg_end(msg, hdr);
18807 
18808 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18809 				NL80211_MCGRP_MLME, gfp);
18810 	return;
18811 
18812  nla_put_failure:
18813 	nlmsg_free(msg);
18814 }
18815 EXPORT_SYMBOL(cfg80211_conn_failed);
18816 
18817 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
18818 				       const u8 *addr, gfp_t gfp)
18819 {
18820 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18821 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18822 	struct sk_buff *msg;
18823 	void *hdr;
18824 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
18825 
18826 	if (!nlportid)
18827 		return false;
18828 
18829 	msg = nlmsg_new(100, gfp);
18830 	if (!msg)
18831 		return true;
18832 
18833 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18834 	if (!hdr) {
18835 		nlmsg_free(msg);
18836 		return true;
18837 	}
18838 
18839 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18840 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18841 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
18842 		goto nla_put_failure;
18843 
18844 	genlmsg_end(msg, hdr);
18845 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18846 	return true;
18847 
18848  nla_put_failure:
18849 	nlmsg_free(msg);
18850 	return true;
18851 }
18852 
18853 bool cfg80211_rx_spurious_frame(struct net_device *dev,
18854 				const u8 *addr, gfp_t gfp)
18855 {
18856 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18857 	bool ret;
18858 
18859 	trace_cfg80211_rx_spurious_frame(dev, addr);
18860 
18861 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18862 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
18863 		trace_cfg80211_return_bool(false);
18864 		return false;
18865 	}
18866 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
18867 					 addr, gfp);
18868 	trace_cfg80211_return_bool(ret);
18869 	return ret;
18870 }
18871 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
18872 
18873 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
18874 					const u8 *addr, gfp_t gfp)
18875 {
18876 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18877 	bool ret;
18878 
18879 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
18880 
18881 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18882 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
18883 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
18884 		trace_cfg80211_return_bool(false);
18885 		return false;
18886 	}
18887 	ret = __nl80211_unexpected_frame(dev,
18888 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
18889 					 addr, gfp);
18890 	trace_cfg80211_return_bool(ret);
18891 	return ret;
18892 }
18893 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
18894 
18895 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
18896 		      struct wireless_dev *wdev, u32 nlportid,
18897 		      struct cfg80211_rx_info *info, gfp_t gfp)
18898 {
18899 	struct net_device *netdev = wdev->netdev;
18900 	struct sk_buff *msg;
18901 	void *hdr;
18902 
18903 	msg = nlmsg_new(100 + info->len, gfp);
18904 	if (!msg)
18905 		return -ENOMEM;
18906 
18907 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
18908 	if (!hdr) {
18909 		nlmsg_free(msg);
18910 		return -ENOMEM;
18911 	}
18912 
18913 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18914 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18915 					netdev->ifindex)) ||
18916 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18917 			      NL80211_ATTR_PAD) ||
18918 	    (info->have_link_id &&
18919 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, info->link_id)) ||
18920 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, KHZ_TO_MHZ(info->freq)) ||
18921 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET, info->freq % 1000) ||
18922 	    (info->sig_dbm &&
18923 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, info->sig_dbm)) ||
18924 	    nla_put(msg, NL80211_ATTR_FRAME, info->len, info->buf) ||
18925 	    (info->flags &&
18926 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, info->flags)) ||
18927 	    (info->rx_tstamp && nla_put_u64_64bit(msg,
18928 						  NL80211_ATTR_RX_HW_TIMESTAMP,
18929 						  info->rx_tstamp,
18930 						  NL80211_ATTR_PAD)) ||
18931 	    (info->ack_tstamp && nla_put_u64_64bit(msg,
18932 						   NL80211_ATTR_TX_HW_TIMESTAMP,
18933 						   info->ack_tstamp,
18934 						   NL80211_ATTR_PAD)))
18935 		goto nla_put_failure;
18936 
18937 	genlmsg_end(msg, hdr);
18938 
18939 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18940 
18941  nla_put_failure:
18942 	nlmsg_free(msg);
18943 	return -ENOBUFS;
18944 }
18945 
18946 static void nl80211_frame_tx_status(struct wireless_dev *wdev,
18947 				    struct cfg80211_tx_status *status,
18948 				    gfp_t gfp, enum nl80211_commands command)
18949 {
18950 	struct wiphy *wiphy = wdev->wiphy;
18951 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18952 	struct net_device *netdev = wdev->netdev;
18953 	struct sk_buff *msg;
18954 	void *hdr;
18955 
18956 	if (command == NL80211_CMD_FRAME_TX_STATUS)
18957 		trace_cfg80211_mgmt_tx_status(wdev, status->cookie,
18958 					      status->ack);
18959 	else
18960 		trace_cfg80211_control_port_tx_status(wdev, status->cookie,
18961 						      status->ack);
18962 
18963 	msg = nlmsg_new(100 + status->len, gfp);
18964 	if (!msg)
18965 		return;
18966 
18967 	hdr = nl80211hdr_put(msg, 0, 0, 0, command);
18968 	if (!hdr) {
18969 		nlmsg_free(msg);
18970 		return;
18971 	}
18972 
18973 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18974 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18975 				   netdev->ifindex)) ||
18976 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18977 			      NL80211_ATTR_PAD) ||
18978 	    nla_put(msg, NL80211_ATTR_FRAME, status->len, status->buf) ||
18979 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, status->cookie,
18980 			      NL80211_ATTR_PAD) ||
18981 	    (status->ack && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
18982 	    (status->tx_tstamp &&
18983 	     nla_put_u64_64bit(msg, NL80211_ATTR_TX_HW_TIMESTAMP,
18984 			       status->tx_tstamp, NL80211_ATTR_PAD)) ||
18985 	    (status->ack_tstamp &&
18986 	     nla_put_u64_64bit(msg, NL80211_ATTR_RX_HW_TIMESTAMP,
18987 			       status->ack_tstamp, NL80211_ATTR_PAD)))
18988 		goto nla_put_failure;
18989 
18990 	genlmsg_end(msg, hdr);
18991 
18992 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18993 				NL80211_MCGRP_MLME, gfp);
18994 	return;
18995 
18996 nla_put_failure:
18997 	nlmsg_free(msg);
18998 }
18999 
19000 void cfg80211_control_port_tx_status(struct wireless_dev *wdev, u64 cookie,
19001 				     const u8 *buf, size_t len, bool ack,
19002 				     gfp_t gfp)
19003 {
19004 	struct cfg80211_tx_status status = {
19005 		.cookie = cookie,
19006 		.buf = buf,
19007 		.len = len,
19008 		.ack = ack
19009 	};
19010 
19011 	nl80211_frame_tx_status(wdev, &status, gfp,
19012 				NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS);
19013 }
19014 EXPORT_SYMBOL(cfg80211_control_port_tx_status);
19015 
19016 void cfg80211_mgmt_tx_status_ext(struct wireless_dev *wdev,
19017 				 struct cfg80211_tx_status *status, gfp_t gfp)
19018 {
19019 	nl80211_frame_tx_status(wdev, status, gfp, NL80211_CMD_FRAME_TX_STATUS);
19020 }
19021 EXPORT_SYMBOL(cfg80211_mgmt_tx_status_ext);
19022 
19023 static int __nl80211_rx_control_port(struct net_device *dev,
19024 				     struct sk_buff *skb,
19025 				     bool unencrypted,
19026 				     int link_id,
19027 				     gfp_t gfp)
19028 {
19029 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19030 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19031 	struct ethhdr *ehdr = eth_hdr(skb);
19032 	const u8 *addr = ehdr->h_source;
19033 	u16 proto = be16_to_cpu(skb->protocol);
19034 	struct sk_buff *msg;
19035 	void *hdr;
19036 	struct nlattr *frame;
19037 
19038 	u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
19039 
19040 	if (!nlportid)
19041 		return -ENOENT;
19042 
19043 	msg = nlmsg_new(100 + skb->len, gfp);
19044 	if (!msg)
19045 		return -ENOMEM;
19046 
19047 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
19048 	if (!hdr) {
19049 		nlmsg_free(msg);
19050 		return -ENOBUFS;
19051 	}
19052 
19053 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19054 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19055 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19056 			      NL80211_ATTR_PAD) ||
19057 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
19058 	    nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
19059 	    (link_id >= 0 &&
19060 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
19061 	    (unencrypted && nla_put_flag(msg,
19062 					 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
19063 		goto nla_put_failure;
19064 
19065 	frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
19066 	if (!frame)
19067 		goto nla_put_failure;
19068 
19069 	skb_copy_bits(skb, 0, nla_data(frame), skb->len);
19070 	genlmsg_end(msg, hdr);
19071 
19072 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
19073 
19074  nla_put_failure:
19075 	nlmsg_free(msg);
19076 	return -ENOBUFS;
19077 }
19078 
19079 bool cfg80211_rx_control_port(struct net_device *dev, struct sk_buff *skb,
19080 			      bool unencrypted, int link_id)
19081 {
19082 	int ret;
19083 
19084 	trace_cfg80211_rx_control_port(dev, skb, unencrypted, link_id);
19085 	ret = __nl80211_rx_control_port(dev, skb, unencrypted, link_id,
19086 					GFP_ATOMIC);
19087 	trace_cfg80211_return_bool(ret == 0);
19088 	return ret == 0;
19089 }
19090 EXPORT_SYMBOL(cfg80211_rx_control_port);
19091 
19092 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
19093 					    const char *mac, gfp_t gfp)
19094 {
19095 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19096 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19097 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19098 	void **cb;
19099 
19100 	if (!msg)
19101 		return NULL;
19102 
19103 	cb = (void **)msg->cb;
19104 
19105 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
19106 	if (!cb[0]) {
19107 		nlmsg_free(msg);
19108 		return NULL;
19109 	}
19110 
19111 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19112 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19113 		goto nla_put_failure;
19114 
19115 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
19116 		goto nla_put_failure;
19117 
19118 	cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
19119 	if (!cb[1])
19120 		goto nla_put_failure;
19121 
19122 	cb[2] = rdev;
19123 
19124 	return msg;
19125  nla_put_failure:
19126 	nlmsg_free(msg);
19127 	return NULL;
19128 }
19129 
19130 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
19131 {
19132 	void **cb = (void **)msg->cb;
19133 	struct cfg80211_registered_device *rdev = cb[2];
19134 
19135 	nla_nest_end(msg, cb[1]);
19136 	genlmsg_end(msg, cb[0]);
19137 
19138 	memset(msg->cb, 0, sizeof(msg->cb));
19139 
19140 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19141 				NL80211_MCGRP_MLME, gfp);
19142 }
19143 
19144 void cfg80211_cqm_rssi_notify(struct net_device *dev,
19145 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
19146 			      s32 rssi_level, gfp_t gfp)
19147 {
19148 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19149 	struct cfg80211_cqm_config *cqm_config;
19150 
19151 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
19152 
19153 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
19154 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
19155 		return;
19156 
19157 	rcu_read_lock();
19158 	cqm_config = rcu_dereference(wdev->cqm_config);
19159 	if (cqm_config) {
19160 		cqm_config->last_rssi_event_value = rssi_level;
19161 		cqm_config->last_rssi_event_type = rssi_event;
19162 		wiphy_work_queue(wdev->wiphy, &wdev->cqm_rssi_work);
19163 	}
19164 	rcu_read_unlock();
19165 }
19166 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
19167 
19168 void cfg80211_cqm_rssi_notify_work(struct wiphy *wiphy, struct wiphy_work *work)
19169 {
19170 	struct wireless_dev *wdev = container_of(work, struct wireless_dev,
19171 						 cqm_rssi_work);
19172 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19173 	enum nl80211_cqm_rssi_threshold_event rssi_event;
19174 	struct cfg80211_cqm_config *cqm_config;
19175 	struct sk_buff *msg;
19176 	s32 rssi_level;
19177 
19178 	wdev_lock(wdev);
19179 	cqm_config = rcu_dereference_protected(wdev->cqm_config,
19180 					       lockdep_is_held(&wdev->mtx));
19181 	if (!cqm_config)
19182 		goto unlock;
19183 
19184 	if (cqm_config->use_range_api)
19185 		cfg80211_cqm_rssi_update(rdev, wdev->netdev, cqm_config);
19186 
19187 	rssi_level = cqm_config->last_rssi_event_value;
19188 	rssi_event = cqm_config->last_rssi_event_type;
19189 
19190 	msg = cfg80211_prepare_cqm(wdev->netdev, NULL, GFP_KERNEL);
19191 	if (!msg)
19192 		goto unlock;
19193 
19194 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
19195 			rssi_event))
19196 		goto nla_put_failure;
19197 
19198 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
19199 				      rssi_level))
19200 		goto nla_put_failure;
19201 
19202 	cfg80211_send_cqm(msg, GFP_KERNEL);
19203 
19204 	goto unlock;
19205 
19206  nla_put_failure:
19207 	nlmsg_free(msg);
19208  unlock:
19209 	wdev_unlock(wdev);
19210 }
19211 
19212 void cfg80211_cqm_txe_notify(struct net_device *dev,
19213 			     const u8 *peer, u32 num_packets,
19214 			     u32 rate, u32 intvl, gfp_t gfp)
19215 {
19216 	struct sk_buff *msg;
19217 
19218 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
19219 	if (!msg)
19220 		return;
19221 
19222 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
19223 		goto nla_put_failure;
19224 
19225 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
19226 		goto nla_put_failure;
19227 
19228 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
19229 		goto nla_put_failure;
19230 
19231 	cfg80211_send_cqm(msg, gfp);
19232 	return;
19233 
19234  nla_put_failure:
19235 	nlmsg_free(msg);
19236 }
19237 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
19238 
19239 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
19240 				 const u8 *peer, u32 num_packets, gfp_t gfp)
19241 {
19242 	struct sk_buff *msg;
19243 
19244 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
19245 
19246 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
19247 	if (!msg)
19248 		return;
19249 
19250 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
19251 		goto nla_put_failure;
19252 
19253 	cfg80211_send_cqm(msg, gfp);
19254 	return;
19255 
19256  nla_put_failure:
19257 	nlmsg_free(msg);
19258 }
19259 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
19260 
19261 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
19262 {
19263 	struct sk_buff *msg;
19264 
19265 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
19266 	if (!msg)
19267 		return;
19268 
19269 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
19270 		goto nla_put_failure;
19271 
19272 	cfg80211_send_cqm(msg, gfp);
19273 	return;
19274 
19275  nla_put_failure:
19276 	nlmsg_free(msg);
19277 }
19278 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
19279 
19280 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
19281 				     struct net_device *netdev, const u8 *bssid,
19282 				     const u8 *replay_ctr, gfp_t gfp)
19283 {
19284 	struct sk_buff *msg;
19285 	struct nlattr *rekey_attr;
19286 	void *hdr;
19287 
19288 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19289 	if (!msg)
19290 		return;
19291 
19292 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
19293 	if (!hdr) {
19294 		nlmsg_free(msg);
19295 		return;
19296 	}
19297 
19298 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19299 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19300 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
19301 		goto nla_put_failure;
19302 
19303 	rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
19304 	if (!rekey_attr)
19305 		goto nla_put_failure;
19306 
19307 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
19308 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
19309 		goto nla_put_failure;
19310 
19311 	nla_nest_end(msg, rekey_attr);
19312 
19313 	genlmsg_end(msg, hdr);
19314 
19315 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19316 				NL80211_MCGRP_MLME, gfp);
19317 	return;
19318 
19319  nla_put_failure:
19320 	nlmsg_free(msg);
19321 }
19322 
19323 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
19324 			       const u8 *replay_ctr, gfp_t gfp)
19325 {
19326 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19327 	struct wiphy *wiphy = wdev->wiphy;
19328 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19329 
19330 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
19331 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
19332 }
19333 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
19334 
19335 static void
19336 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
19337 			       struct net_device *netdev, int index,
19338 			       const u8 *bssid, bool preauth, gfp_t gfp)
19339 {
19340 	struct sk_buff *msg;
19341 	struct nlattr *attr;
19342 	void *hdr;
19343 
19344 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19345 	if (!msg)
19346 		return;
19347 
19348 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
19349 	if (!hdr) {
19350 		nlmsg_free(msg);
19351 		return;
19352 	}
19353 
19354 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19355 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
19356 		goto nla_put_failure;
19357 
19358 	attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
19359 	if (!attr)
19360 		goto nla_put_failure;
19361 
19362 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
19363 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
19364 	    (preauth &&
19365 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
19366 		goto nla_put_failure;
19367 
19368 	nla_nest_end(msg, attr);
19369 
19370 	genlmsg_end(msg, hdr);
19371 
19372 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19373 				NL80211_MCGRP_MLME, gfp);
19374 	return;
19375 
19376  nla_put_failure:
19377 	nlmsg_free(msg);
19378 }
19379 
19380 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
19381 				     const u8 *bssid, bool preauth, gfp_t gfp)
19382 {
19383 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19384 	struct wiphy *wiphy = wdev->wiphy;
19385 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19386 
19387 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
19388 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
19389 }
19390 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
19391 
19392 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
19393 				     struct net_device *netdev,
19394 				     unsigned int link_id,
19395 				     struct cfg80211_chan_def *chandef,
19396 				     gfp_t gfp,
19397 				     enum nl80211_commands notif,
19398 				     u8 count, bool quiet, u16 punct_bitmap)
19399 {
19400 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
19401 	struct sk_buff *msg;
19402 	void *hdr;
19403 
19404 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19405 	if (!msg)
19406 		return;
19407 
19408 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
19409 	if (!hdr) {
19410 		nlmsg_free(msg);
19411 		return;
19412 	}
19413 
19414 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
19415 		goto nla_put_failure;
19416 
19417 	if (wdev->valid_links &&
19418 	    nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
19419 		goto nla_put_failure;
19420 
19421 	if (nl80211_send_chandef(msg, chandef))
19422 		goto nla_put_failure;
19423 
19424 	if (notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) {
19425 		if (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count))
19426 			goto nla_put_failure;
19427 		if (quiet &&
19428 		    nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX))
19429 			goto nla_put_failure;
19430 	}
19431 
19432 	if (nla_put_u32(msg, NL80211_ATTR_PUNCT_BITMAP, punct_bitmap))
19433 		goto nla_put_failure;
19434 
19435 	genlmsg_end(msg, hdr);
19436 
19437 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19438 				NL80211_MCGRP_MLME, gfp);
19439 	return;
19440 
19441  nla_put_failure:
19442 	nlmsg_free(msg);
19443 }
19444 
19445 void cfg80211_ch_switch_notify(struct net_device *dev,
19446 			       struct cfg80211_chan_def *chandef,
19447 			       unsigned int link_id, u16 punct_bitmap)
19448 {
19449 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19450 	struct wiphy *wiphy = wdev->wiphy;
19451 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19452 
19453 	ASSERT_WDEV_LOCK(wdev);
19454 	WARN_INVALID_LINK_ID(wdev, link_id);
19455 
19456 	trace_cfg80211_ch_switch_notify(dev, chandef, link_id, punct_bitmap);
19457 
19458 	switch (wdev->iftype) {
19459 	case NL80211_IFTYPE_STATION:
19460 	case NL80211_IFTYPE_P2P_CLIENT:
19461 		if (!WARN_ON(!wdev->links[link_id].client.current_bss))
19462 			cfg80211_update_assoc_bss_entry(wdev, link_id,
19463 							chandef->chan);
19464 		break;
19465 	case NL80211_IFTYPE_MESH_POINT:
19466 		wdev->u.mesh.chandef = *chandef;
19467 		wdev->u.mesh.preset_chandef = *chandef;
19468 		break;
19469 	case NL80211_IFTYPE_AP:
19470 	case NL80211_IFTYPE_P2P_GO:
19471 		wdev->links[link_id].ap.chandef = *chandef;
19472 		break;
19473 	case NL80211_IFTYPE_ADHOC:
19474 		wdev->u.ibss.chandef = *chandef;
19475 		break;
19476 	default:
19477 		WARN_ON(1);
19478 		break;
19479 	}
19480 
19481 	cfg80211_sched_dfs_chan_update(rdev);
19482 
19483 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19484 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0, false,
19485 				 punct_bitmap);
19486 }
19487 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
19488 
19489 void cfg80211_ch_switch_started_notify(struct net_device *dev,
19490 				       struct cfg80211_chan_def *chandef,
19491 				       unsigned int link_id, u8 count,
19492 				       bool quiet, u16 punct_bitmap)
19493 {
19494 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19495 	struct wiphy *wiphy = wdev->wiphy;
19496 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19497 
19498 	ASSERT_WDEV_LOCK(wdev);
19499 	WARN_INVALID_LINK_ID(wdev, link_id);
19500 
19501 	trace_cfg80211_ch_switch_started_notify(dev, chandef, link_id,
19502 						punct_bitmap);
19503 
19504 
19505 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19506 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY,
19507 				 count, quiet, punct_bitmap);
19508 }
19509 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
19510 
19511 int cfg80211_bss_color_notify(struct net_device *dev,
19512 			      enum nl80211_commands cmd, u8 count,
19513 			      u64 color_bitmap)
19514 {
19515 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19516 	struct wiphy *wiphy = wdev->wiphy;
19517 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19518 	struct sk_buff *msg;
19519 	void *hdr;
19520 
19521 	ASSERT_WDEV_LOCK(wdev);
19522 
19523 	trace_cfg80211_bss_color_notify(dev, cmd, count, color_bitmap);
19524 
19525 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
19526 	if (!msg)
19527 		return -ENOMEM;
19528 
19529 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
19530 	if (!hdr)
19531 		goto nla_put_failure;
19532 
19533 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19534 		goto nla_put_failure;
19535 
19536 	if (cmd == NL80211_CMD_COLOR_CHANGE_STARTED &&
19537 	    nla_put_u32(msg, NL80211_ATTR_COLOR_CHANGE_COUNT, count))
19538 		goto nla_put_failure;
19539 
19540 	if (cmd == NL80211_CMD_OBSS_COLOR_COLLISION &&
19541 	    nla_put_u64_64bit(msg, NL80211_ATTR_OBSS_COLOR_BITMAP,
19542 			      color_bitmap, NL80211_ATTR_PAD))
19543 		goto nla_put_failure;
19544 
19545 	genlmsg_end(msg, hdr);
19546 
19547 	return genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
19548 				       msg, 0, NL80211_MCGRP_MLME, GFP_KERNEL);
19549 
19550 nla_put_failure:
19551 	nlmsg_free(msg);
19552 	return -EINVAL;
19553 }
19554 EXPORT_SYMBOL(cfg80211_bss_color_notify);
19555 
19556 void
19557 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
19558 		     const struct cfg80211_chan_def *chandef,
19559 		     enum nl80211_radar_event event,
19560 		     struct net_device *netdev, gfp_t gfp)
19561 {
19562 	struct sk_buff *msg;
19563 	void *hdr;
19564 
19565 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19566 	if (!msg)
19567 		return;
19568 
19569 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
19570 	if (!hdr) {
19571 		nlmsg_free(msg);
19572 		return;
19573 	}
19574 
19575 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19576 		goto nla_put_failure;
19577 
19578 	/* NOP and radar events don't need a netdev parameter */
19579 	if (netdev) {
19580 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
19581 
19582 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19583 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19584 				      NL80211_ATTR_PAD))
19585 			goto nla_put_failure;
19586 	}
19587 
19588 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
19589 		goto nla_put_failure;
19590 
19591 	if (nl80211_send_chandef(msg, chandef))
19592 		goto nla_put_failure;
19593 
19594 	genlmsg_end(msg, hdr);
19595 
19596 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19597 				NL80211_MCGRP_MLME, gfp);
19598 	return;
19599 
19600  nla_put_failure:
19601 	nlmsg_free(msg);
19602 }
19603 
19604 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
19605 				       struct sta_opmode_info *sta_opmode,
19606 				       gfp_t gfp)
19607 {
19608 	struct sk_buff *msg;
19609 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19610 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19611 	void *hdr;
19612 
19613 	if (WARN_ON(!mac))
19614 		return;
19615 
19616 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19617 	if (!msg)
19618 		return;
19619 
19620 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
19621 	if (!hdr) {
19622 		nlmsg_free(msg);
19623 		return;
19624 	}
19625 
19626 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19627 		goto nla_put_failure;
19628 
19629 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19630 		goto nla_put_failure;
19631 
19632 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
19633 		goto nla_put_failure;
19634 
19635 	if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
19636 	    nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
19637 		goto nla_put_failure;
19638 
19639 	if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
19640 	    nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
19641 		goto nla_put_failure;
19642 
19643 	if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
19644 	    nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
19645 		goto nla_put_failure;
19646 
19647 	genlmsg_end(msg, hdr);
19648 
19649 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19650 				NL80211_MCGRP_MLME, gfp);
19651 
19652 	return;
19653 
19654 nla_put_failure:
19655 	nlmsg_free(msg);
19656 }
19657 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
19658 
19659 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
19660 			   u64 cookie, bool acked, s32 ack_signal,
19661 			   bool is_valid_ack_signal, gfp_t gfp)
19662 {
19663 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19664 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19665 	struct sk_buff *msg;
19666 	void *hdr;
19667 
19668 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
19669 
19670 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19671 
19672 	if (!msg)
19673 		return;
19674 
19675 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
19676 	if (!hdr) {
19677 		nlmsg_free(msg);
19678 		return;
19679 	}
19680 
19681 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19682 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19683 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
19684 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
19685 			      NL80211_ATTR_PAD) ||
19686 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
19687 	    (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
19688 						ack_signal)))
19689 		goto nla_put_failure;
19690 
19691 	genlmsg_end(msg, hdr);
19692 
19693 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19694 				NL80211_MCGRP_MLME, gfp);
19695 	return;
19696 
19697  nla_put_failure:
19698 	nlmsg_free(msg);
19699 }
19700 EXPORT_SYMBOL(cfg80211_probe_status);
19701 
19702 void cfg80211_report_obss_beacon_khz(struct wiphy *wiphy, const u8 *frame,
19703 				     size_t len, int freq, int sig_dbm)
19704 {
19705 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19706 	struct sk_buff *msg;
19707 	void *hdr;
19708 	struct cfg80211_beacon_registration *reg;
19709 
19710 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
19711 
19712 	spin_lock_bh(&rdev->beacon_registrations_lock);
19713 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
19714 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
19715 		if (!msg) {
19716 			spin_unlock_bh(&rdev->beacon_registrations_lock);
19717 			return;
19718 		}
19719 
19720 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
19721 		if (!hdr)
19722 			goto nla_put_failure;
19723 
19724 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19725 		    (freq &&
19726 		     (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
19727 				  KHZ_TO_MHZ(freq)) ||
19728 		      nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
19729 				  freq % 1000))) ||
19730 		    (sig_dbm &&
19731 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
19732 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
19733 			goto nla_put_failure;
19734 
19735 		genlmsg_end(msg, hdr);
19736 
19737 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
19738 	}
19739 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19740 	return;
19741 
19742  nla_put_failure:
19743 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19744 	nlmsg_free(msg);
19745 }
19746 EXPORT_SYMBOL(cfg80211_report_obss_beacon_khz);
19747 
19748 #ifdef CONFIG_PM
19749 static int cfg80211_net_detect_results(struct sk_buff *msg,
19750 				       struct cfg80211_wowlan_wakeup *wakeup)
19751 {
19752 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
19753 	struct nlattr *nl_results, *nl_match, *nl_freqs;
19754 	int i, j;
19755 
19756 	nl_results = nla_nest_start_noflag(msg,
19757 					   NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
19758 	if (!nl_results)
19759 		return -EMSGSIZE;
19760 
19761 	for (i = 0; i < nd->n_matches; i++) {
19762 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
19763 
19764 		nl_match = nla_nest_start_noflag(msg, i);
19765 		if (!nl_match)
19766 			break;
19767 
19768 		/* The SSID attribute is optional in nl80211, but for
19769 		 * simplicity reasons it's always present in the
19770 		 * cfg80211 structure.  If a driver can't pass the
19771 		 * SSID, that needs to be changed.  A zero length SSID
19772 		 * is still a valid SSID (wildcard), so it cannot be
19773 		 * used for this purpose.
19774 		 */
19775 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
19776 			    match->ssid.ssid)) {
19777 			nla_nest_cancel(msg, nl_match);
19778 			goto out;
19779 		}
19780 
19781 		if (match->n_channels) {
19782 			nl_freqs = nla_nest_start_noflag(msg,
19783 							 NL80211_ATTR_SCAN_FREQUENCIES);
19784 			if (!nl_freqs) {
19785 				nla_nest_cancel(msg, nl_match);
19786 				goto out;
19787 			}
19788 
19789 			for (j = 0; j < match->n_channels; j++) {
19790 				if (nla_put_u32(msg, j, match->channels[j])) {
19791 					nla_nest_cancel(msg, nl_freqs);
19792 					nla_nest_cancel(msg, nl_match);
19793 					goto out;
19794 				}
19795 			}
19796 
19797 			nla_nest_end(msg, nl_freqs);
19798 		}
19799 
19800 		nla_nest_end(msg, nl_match);
19801 	}
19802 
19803 out:
19804 	nla_nest_end(msg, nl_results);
19805 	return 0;
19806 }
19807 
19808 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
19809 				   struct cfg80211_wowlan_wakeup *wakeup,
19810 				   gfp_t gfp)
19811 {
19812 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19813 	struct sk_buff *msg;
19814 	void *hdr;
19815 	int size = 200;
19816 
19817 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
19818 
19819 	if (wakeup)
19820 		size += wakeup->packet_present_len;
19821 
19822 	msg = nlmsg_new(size, gfp);
19823 	if (!msg)
19824 		return;
19825 
19826 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
19827 	if (!hdr)
19828 		goto free_msg;
19829 
19830 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19831 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19832 			      NL80211_ATTR_PAD))
19833 		goto free_msg;
19834 
19835 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
19836 					wdev->netdev->ifindex))
19837 		goto free_msg;
19838 
19839 	if (wakeup) {
19840 		struct nlattr *reasons;
19841 
19842 		reasons = nla_nest_start_noflag(msg,
19843 						NL80211_ATTR_WOWLAN_TRIGGERS);
19844 		if (!reasons)
19845 			goto free_msg;
19846 
19847 		if (wakeup->disconnect &&
19848 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
19849 			goto free_msg;
19850 		if (wakeup->magic_pkt &&
19851 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
19852 			goto free_msg;
19853 		if (wakeup->gtk_rekey_failure &&
19854 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
19855 			goto free_msg;
19856 		if (wakeup->eap_identity_req &&
19857 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
19858 			goto free_msg;
19859 		if (wakeup->four_way_handshake &&
19860 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
19861 			goto free_msg;
19862 		if (wakeup->rfkill_release &&
19863 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
19864 			goto free_msg;
19865 
19866 		if (wakeup->pattern_idx >= 0 &&
19867 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
19868 				wakeup->pattern_idx))
19869 			goto free_msg;
19870 
19871 		if (wakeup->tcp_match &&
19872 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
19873 			goto free_msg;
19874 
19875 		if (wakeup->tcp_connlost &&
19876 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
19877 			goto free_msg;
19878 
19879 		if (wakeup->tcp_nomoretokens &&
19880 		    nla_put_flag(msg,
19881 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
19882 			goto free_msg;
19883 
19884 		if (wakeup->packet) {
19885 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
19886 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
19887 
19888 			if (!wakeup->packet_80211) {
19889 				pkt_attr =
19890 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
19891 				len_attr =
19892 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
19893 			}
19894 
19895 			if (wakeup->packet_len &&
19896 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
19897 				goto free_msg;
19898 
19899 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
19900 				    wakeup->packet))
19901 				goto free_msg;
19902 		}
19903 
19904 		if (wakeup->net_detect &&
19905 		    cfg80211_net_detect_results(msg, wakeup))
19906 				goto free_msg;
19907 
19908 		nla_nest_end(msg, reasons);
19909 	}
19910 
19911 	genlmsg_end(msg, hdr);
19912 
19913 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19914 				NL80211_MCGRP_MLME, gfp);
19915 	return;
19916 
19917  free_msg:
19918 	nlmsg_free(msg);
19919 }
19920 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
19921 #endif
19922 
19923 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
19924 				enum nl80211_tdls_operation oper,
19925 				u16 reason_code, gfp_t gfp)
19926 {
19927 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19928 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19929 	struct sk_buff *msg;
19930 	void *hdr;
19931 
19932 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
19933 					 reason_code);
19934 
19935 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19936 	if (!msg)
19937 		return;
19938 
19939 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
19940 	if (!hdr) {
19941 		nlmsg_free(msg);
19942 		return;
19943 	}
19944 
19945 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19946 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19947 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
19948 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
19949 	    (reason_code > 0 &&
19950 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
19951 		goto nla_put_failure;
19952 
19953 	genlmsg_end(msg, hdr);
19954 
19955 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19956 				NL80211_MCGRP_MLME, gfp);
19957 	return;
19958 
19959  nla_put_failure:
19960 	nlmsg_free(msg);
19961 }
19962 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
19963 
19964 static int nl80211_netlink_notify(struct notifier_block * nb,
19965 				  unsigned long state,
19966 				  void *_notify)
19967 {
19968 	struct netlink_notify *notify = _notify;
19969 	struct cfg80211_registered_device *rdev;
19970 	struct wireless_dev *wdev;
19971 	struct cfg80211_beacon_registration *reg, *tmp;
19972 
19973 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
19974 		return NOTIFY_DONE;
19975 
19976 	rcu_read_lock();
19977 
19978 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
19979 		struct cfg80211_sched_scan_request *sched_scan_req;
19980 
19981 		list_for_each_entry_rcu(sched_scan_req,
19982 					&rdev->sched_scan_req_list,
19983 					list) {
19984 			if (sched_scan_req->owner_nlportid == notify->portid) {
19985 				sched_scan_req->nl_owner_dead = true;
19986 				wiphy_work_queue(&rdev->wiphy,
19987 						 &rdev->sched_scan_stop_wk);
19988 			}
19989 		}
19990 
19991 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
19992 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
19993 
19994 			if (wdev->owner_nlportid == notify->portid) {
19995 				wdev->nl_owner_dead = true;
19996 				schedule_work(&rdev->destroy_work);
19997 			} else if (wdev->conn_owner_nlportid == notify->portid) {
19998 				schedule_work(&wdev->disconnect_wk);
19999 			}
20000 
20001 			cfg80211_release_pmsr(wdev, notify->portid);
20002 		}
20003 
20004 		spin_lock_bh(&rdev->beacon_registrations_lock);
20005 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
20006 					 list) {
20007 			if (reg->nlportid == notify->portid) {
20008 				list_del(&reg->list);
20009 				kfree(reg);
20010 				break;
20011 			}
20012 		}
20013 		spin_unlock_bh(&rdev->beacon_registrations_lock);
20014 	}
20015 
20016 	rcu_read_unlock();
20017 
20018 	/*
20019 	 * It is possible that the user space process that is controlling the
20020 	 * indoor setting disappeared, so notify the regulatory core.
20021 	 */
20022 	regulatory_netlink_notify(notify->portid);
20023 	return NOTIFY_OK;
20024 }
20025 
20026 static struct notifier_block nl80211_netlink_notifier = {
20027 	.notifier_call = nl80211_netlink_notify,
20028 };
20029 
20030 void cfg80211_ft_event(struct net_device *netdev,
20031 		       struct cfg80211_ft_event_params *ft_event)
20032 {
20033 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
20034 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
20035 	struct sk_buff *msg;
20036 	void *hdr;
20037 
20038 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
20039 
20040 	if (!ft_event->target_ap)
20041 		return;
20042 
20043 	msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
20044 			GFP_KERNEL);
20045 	if (!msg)
20046 		return;
20047 
20048 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
20049 	if (!hdr)
20050 		goto out;
20051 
20052 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20053 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
20054 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
20055 		goto out;
20056 
20057 	if (ft_event->ies &&
20058 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
20059 		goto out;
20060 	if (ft_event->ric_ies &&
20061 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
20062 		    ft_event->ric_ies))
20063 		goto out;
20064 
20065 	genlmsg_end(msg, hdr);
20066 
20067 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
20068 				NL80211_MCGRP_MLME, GFP_KERNEL);
20069 	return;
20070  out:
20071 	nlmsg_free(msg);
20072 }
20073 EXPORT_SYMBOL(cfg80211_ft_event);
20074 
20075 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
20076 {
20077 	struct cfg80211_registered_device *rdev;
20078 	struct sk_buff *msg;
20079 	void *hdr;
20080 	u32 nlportid;
20081 
20082 	rdev = wiphy_to_rdev(wdev->wiphy);
20083 	if (!rdev->crit_proto_nlportid)
20084 		return;
20085 
20086 	nlportid = rdev->crit_proto_nlportid;
20087 	rdev->crit_proto_nlportid = 0;
20088 
20089 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
20090 	if (!msg)
20091 		return;
20092 
20093 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
20094 	if (!hdr)
20095 		goto nla_put_failure;
20096 
20097 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20098 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
20099 			      NL80211_ATTR_PAD))
20100 		goto nla_put_failure;
20101 
20102 	genlmsg_end(msg, hdr);
20103 
20104 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
20105 	return;
20106 
20107  nla_put_failure:
20108 	nlmsg_free(msg);
20109 }
20110 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
20111 
20112 void nl80211_send_ap_stopped(struct wireless_dev *wdev, unsigned int link_id)
20113 {
20114 	struct wiphy *wiphy = wdev->wiphy;
20115 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
20116 	struct sk_buff *msg;
20117 	void *hdr;
20118 
20119 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
20120 	if (!msg)
20121 		return;
20122 
20123 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
20124 	if (!hdr)
20125 		goto out;
20126 
20127 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20128 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
20129 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
20130 			      NL80211_ATTR_PAD) ||
20131 	    (wdev->valid_links &&
20132 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)))
20133 		goto out;
20134 
20135 	genlmsg_end(msg, hdr);
20136 
20137 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
20138 				NL80211_MCGRP_MLME, GFP_KERNEL);
20139 	return;
20140  out:
20141 	nlmsg_free(msg);
20142 }
20143 
20144 int cfg80211_external_auth_request(struct net_device *dev,
20145 				   struct cfg80211_external_auth_params *params,
20146 				   gfp_t gfp)
20147 {
20148 	struct wireless_dev *wdev = dev->ieee80211_ptr;
20149 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
20150 	struct sk_buff *msg;
20151 	void *hdr;
20152 
20153 	if (!wdev->conn_owner_nlportid)
20154 		return -EINVAL;
20155 
20156 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
20157 	if (!msg)
20158 		return -ENOMEM;
20159 
20160 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
20161 	if (!hdr)
20162 		goto nla_put_failure;
20163 
20164 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20165 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
20166 	    nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
20167 	    nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
20168 			params->action) ||
20169 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
20170 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
20171 		    params->ssid.ssid) ||
20172 	    (!is_zero_ether_addr(params->mld_addr) &&
20173 	     nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN, params->mld_addr)))
20174 		goto nla_put_failure;
20175 
20176 	genlmsg_end(msg, hdr);
20177 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
20178 			wdev->conn_owner_nlportid);
20179 	return 0;
20180 
20181  nla_put_failure:
20182 	nlmsg_free(msg);
20183 	return -ENOBUFS;
20184 }
20185 EXPORT_SYMBOL(cfg80211_external_auth_request);
20186 
20187 void cfg80211_update_owe_info_event(struct net_device *netdev,
20188 				    struct cfg80211_update_owe_info *owe_info,
20189 				    gfp_t gfp)
20190 {
20191 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
20192 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
20193 	struct sk_buff *msg;
20194 	void *hdr;
20195 
20196 	trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
20197 
20198 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
20199 	if (!msg)
20200 		return;
20201 
20202 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
20203 	if (!hdr)
20204 		goto nla_put_failure;
20205 
20206 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20207 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
20208 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
20209 		goto nla_put_failure;
20210 
20211 	if (!owe_info->ie_len ||
20212 	    nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
20213 		goto nla_put_failure;
20214 
20215 	if (owe_info->assoc_link_id != -1) {
20216 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
20217 			       owe_info->assoc_link_id))
20218 			goto nla_put_failure;
20219 
20220 		if (!is_zero_ether_addr(owe_info->peer_mld_addr) &&
20221 		    nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
20222 			    owe_info->peer_mld_addr))
20223 			goto nla_put_failure;
20224 	}
20225 
20226 	genlmsg_end(msg, hdr);
20227 
20228 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
20229 				NL80211_MCGRP_MLME, gfp);
20230 	return;
20231 
20232 nla_put_failure:
20233 	genlmsg_cancel(msg, hdr);
20234 	nlmsg_free(msg);
20235 }
20236 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
20237 
20238 /* initialisation/exit functions */
20239 
20240 int __init nl80211_init(void)
20241 {
20242 	int err;
20243 
20244 	err = genl_register_family(&nl80211_fam);
20245 	if (err)
20246 		return err;
20247 
20248 	err = netlink_register_notifier(&nl80211_netlink_notifier);
20249 	if (err)
20250 		goto err_out;
20251 
20252 	return 0;
20253  err_out:
20254 	genl_unregister_family(&nl80211_fam);
20255 	return err;
20256 }
20257 
20258 void nl80211_exit(void)
20259 {
20260 	netlink_unregister_notifier(&nl80211_netlink_notifier);
20261 	genl_unregister_family(&nl80211_fam);
20262 }
20263