xref: /linux/net/wireless/nl80211.c (revision 1f20a5769446a1acae67ac9e63d07a594829a789)
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-2024 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 	for_each_rdev(rdev) {
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 const struct netlink_range_validation nl80211_punct_bitmap_range = {
467 	.min = 0,
468 	.max = 0xffff,
469 };
470 
471 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
472 	[0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
473 	[NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
474 	[NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
475 				      .len = 20-1 },
476 	[NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
477 
478 	[NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
479 	[NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
480 	[NL80211_ATTR_WIPHY_EDMG_CHANNELS] = NLA_POLICY_RANGE(NLA_U8,
481 						NL80211_EDMG_CHANNELS_MIN,
482 						NL80211_EDMG_CHANNELS_MAX),
483 	[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG] = NLA_POLICY_RANGE(NLA_U8,
484 						NL80211_EDMG_BW_CONFIG_MIN,
485 						NL80211_EDMG_BW_CONFIG_MAX),
486 
487 	[NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
488 	[NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
489 	[NL80211_ATTR_CENTER_FREQ1_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
490 	[NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
491 
492 	[NL80211_ATTR_WIPHY_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
493 	[NL80211_ATTR_WIPHY_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
494 	[NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
495 	[NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
496 	[NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
497 	[NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
498 
499 	[NL80211_ATTR_IFTYPE] = NLA_POLICY_MAX(NLA_U32, NL80211_IFTYPE_MAX),
500 	[NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
501 	[NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
502 
503 	[NL80211_ATTR_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
504 	[NL80211_ATTR_PREV_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
505 
506 	[NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
507 	[NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
508 				    .len = WLAN_MAX_KEY_LEN },
509 	[NL80211_ATTR_KEY_IDX] = NLA_POLICY_MAX(NLA_U8, 7),
510 	[NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
511 	[NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
512 	[NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
513 	[NL80211_ATTR_KEY_TYPE] =
514 		NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES),
515 
516 	[NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
517 	[NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
518 	[NL80211_ATTR_BEACON_HEAD] =
519 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_beacon_head,
520 				       IEEE80211_MAX_DATA_LEN),
521 	[NL80211_ATTR_BEACON_TAIL] =
522 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
523 				       IEEE80211_MAX_DATA_LEN),
524 	[NL80211_ATTR_STA_AID] =
525 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
526 	[NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
527 	[NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
528 	[NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
529 					       .len = NL80211_MAX_SUPP_RATES },
530 	[NL80211_ATTR_STA_PLINK_ACTION] =
531 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_ACTIONS - 1),
532 	[NL80211_ATTR_STA_TX_POWER_SETTING] =
533 		NLA_POLICY_RANGE(NLA_U8,
534 				 NL80211_TX_POWER_AUTOMATIC,
535 				 NL80211_TX_POWER_FIXED),
536 	[NL80211_ATTR_STA_TX_POWER] = { .type = NLA_S16 },
537 	[NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
538 	[NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
539 	[NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
540 				   .len = IEEE80211_MAX_MESH_ID_LEN },
541 	[NL80211_ATTR_MPATH_NEXT_HOP] = NLA_POLICY_ETH_ADDR_COMPAT,
542 
543 	/* allow 3 for NUL-termination, we used to declare this NLA_STRING */
544 	[NL80211_ATTR_REG_ALPHA2] = NLA_POLICY_RANGE(NLA_BINARY, 2, 3),
545 	[NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
546 
547 	[NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
548 	[NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
549 	[NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
550 	[NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
551 					   .len = NL80211_MAX_SUPP_RATES },
552 	[NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
553 
554 	[NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
555 	[NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
556 
557 	[NL80211_ATTR_HT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_HT_CAPABILITY_LEN),
558 
559 	[NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
560 	[NL80211_ATTR_IE] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
561 						   validate_ie_attr,
562 						   IEEE80211_MAX_DATA_LEN),
563 	[NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
564 	[NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
565 
566 	[NL80211_ATTR_SSID] = { .type = NLA_BINARY,
567 				.len = IEEE80211_MAX_SSID_LEN },
568 	[NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
569 	[NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
570 	[NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
571 	[NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
572 	[NL80211_ATTR_USE_MFP] = NLA_POLICY_RANGE(NLA_U32,
573 						  NL80211_MFP_NO,
574 						  NL80211_MFP_OPTIONAL),
575 	[NL80211_ATTR_STA_FLAGS2] =
576 		NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_sta_flag_update)),
577 	[NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
578 	[NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
579 	[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
580 	[NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
581 	[NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
582 	[NL80211_ATTR_STATUS_CODE] = { .type = NLA_U16 },
583 	[NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
584 	[NL80211_ATTR_WPA_VERSIONS] =
585 		NLA_POLICY_RANGE(NLA_U32, 0,
586 				 NL80211_WPA_VERSION_1 |
587 				 NL80211_WPA_VERSION_2 |
588 				 NL80211_WPA_VERSION_3),
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] = { .type = NLA_U32 },
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 	[NL80211_ATTR_BSS_DUMP_INCLUDE_USE_DATA] = { .type = NLA_FLAG },
826 	[NL80211_ATTR_MLO_TTLM_DLINK] = NLA_POLICY_EXACT_LEN(sizeof(u16) * 8),
827 	[NL80211_ATTR_MLO_TTLM_ULINK] = NLA_POLICY_EXACT_LEN(sizeof(u16) * 8),
828 	[NL80211_ATTR_ASSOC_SPP_AMSDU] = { .type = NLA_FLAG },
829 };
830 
831 /* policy for the key attributes */
832 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
833 	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
834 	[NL80211_KEY_IDX] = { .type = NLA_U8 },
835 	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
836 	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
837 	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
838 	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
839 	[NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
840 	[NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
841 	[NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
842 };
843 
844 /* policy for the key default flags */
845 static const struct nla_policy
846 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
847 	[NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
848 	[NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
849 };
850 
851 #ifdef CONFIG_PM
852 /* policy for WoWLAN attributes */
853 static const struct nla_policy
854 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
855 	[NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
856 	[NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
857 	[NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
858 	[NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
859 	[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
860 	[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
861 	[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
862 	[NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
863 	[NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
864 	[NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
865 };
866 
867 static const struct nla_policy
868 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
869 	[NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
870 	[NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
871 	[NL80211_WOWLAN_TCP_DST_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
872 	[NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
873 	[NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
874 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
875 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
876 		.len = sizeof(struct nl80211_wowlan_tcp_data_seq)
877 	},
878 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
879 		.len = sizeof(struct nl80211_wowlan_tcp_data_token)
880 	},
881 	[NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
882 	[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
883 	[NL80211_WOWLAN_TCP_WAKE_MASK] = NLA_POLICY_MIN_LEN(1),
884 };
885 #endif /* CONFIG_PM */
886 
887 /* policy for coalesce rule attributes */
888 static const struct nla_policy
889 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
890 	[NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
891 	[NL80211_ATTR_COALESCE_RULE_CONDITION] =
892 		NLA_POLICY_RANGE(NLA_U32,
893 				 NL80211_COALESCE_CONDITION_MATCH,
894 				 NL80211_COALESCE_CONDITION_NO_MATCH),
895 	[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
896 };
897 
898 /* policy for GTK rekey offload attributes */
899 static const struct nla_policy
900 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
901 	[NL80211_REKEY_DATA_KEK] = {
902 		.type = NLA_BINARY,
903 		.len = NL80211_KEK_EXT_LEN
904 	},
905 	[NL80211_REKEY_DATA_KCK] = {
906 		.type = NLA_BINARY,
907 		.len = NL80211_KCK_EXT_LEN_32
908 	},
909 	[NL80211_REKEY_DATA_REPLAY_CTR] = NLA_POLICY_EXACT_LEN(NL80211_REPLAY_CTR_LEN),
910 	[NL80211_REKEY_DATA_AKM] = { .type = NLA_U32 },
911 };
912 
913 static const struct nla_policy
914 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
915 	[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
916 						 .len = IEEE80211_MAX_SSID_LEN },
917 	[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
918 	[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
919 };
920 
921 static const struct nla_policy
922 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
923 	[NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
924 	[NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
925 };
926 
927 static const struct nla_policy
928 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
929 	[NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
930 	[NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
931 	[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
932 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
933 	},
934 };
935 
936 /* policy for NAN function attributes */
937 static const struct nla_policy
938 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
939 	[NL80211_NAN_FUNC_TYPE] =
940 		NLA_POLICY_MAX(NLA_U8, NL80211_NAN_FUNC_MAX_TYPE),
941 	[NL80211_NAN_FUNC_SERVICE_ID] = {
942 				    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
943 	[NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
944 	[NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
945 	[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
946 	[NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
947 	[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
948 	[NL80211_NAN_FUNC_FOLLOW_UP_DEST] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
949 	[NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
950 	[NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
951 	[NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
952 			.len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
953 	[NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
954 	[NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
955 	[NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
956 	[NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
957 	[NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
958 };
959 
960 /* policy for Service Response Filter attributes */
961 static const struct nla_policy
962 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
963 	[NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
964 	[NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
965 				 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
966 	[NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
967 	[NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
968 };
969 
970 /* policy for packet pattern attributes */
971 static const struct nla_policy
972 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
973 	[NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
974 	[NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
975 	[NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
976 };
977 
978 static int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
979 				     struct cfg80211_registered_device **rdev,
980 				     struct wireless_dev **wdev,
981 				     struct nlattr **attrbuf)
982 {
983 	int err;
984 
985 	if (!cb->args[0]) {
986 		struct nlattr **attrbuf_free = NULL;
987 
988 		if (!attrbuf) {
989 			attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
990 					  GFP_KERNEL);
991 			if (!attrbuf)
992 				return -ENOMEM;
993 			attrbuf_free = attrbuf;
994 		}
995 
996 		err = nlmsg_parse_deprecated(cb->nlh,
997 					     GENL_HDRLEN + nl80211_fam.hdrsize,
998 					     attrbuf, nl80211_fam.maxattr,
999 					     nl80211_policy, NULL);
1000 		if (err) {
1001 			kfree(attrbuf_free);
1002 			return err;
1003 		}
1004 
1005 		rtnl_lock();
1006 		*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(cb->skb->sk),
1007 						   attrbuf);
1008 		kfree(attrbuf_free);
1009 		if (IS_ERR(*wdev)) {
1010 			rtnl_unlock();
1011 			return PTR_ERR(*wdev);
1012 		}
1013 		*rdev = wiphy_to_rdev((*wdev)->wiphy);
1014 		mutex_lock(&(*rdev)->wiphy.mtx);
1015 		rtnl_unlock();
1016 		/* 0 is the first index - add 1 to parse only once */
1017 		cb->args[0] = (*rdev)->wiphy_idx + 1;
1018 		cb->args[1] = (*wdev)->identifier;
1019 	} else {
1020 		/* subtract the 1 again here */
1021 		struct wiphy *wiphy;
1022 		struct wireless_dev *tmp;
1023 
1024 		rtnl_lock();
1025 		wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
1026 		if (!wiphy) {
1027 			rtnl_unlock();
1028 			return -ENODEV;
1029 		}
1030 		*rdev = wiphy_to_rdev(wiphy);
1031 		*wdev = NULL;
1032 
1033 		list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
1034 			if (tmp->identifier == cb->args[1]) {
1035 				*wdev = tmp;
1036 				break;
1037 			}
1038 		}
1039 
1040 		if (!*wdev) {
1041 			rtnl_unlock();
1042 			return -ENODEV;
1043 		}
1044 		mutex_lock(&(*rdev)->wiphy.mtx);
1045 		rtnl_unlock();
1046 	}
1047 
1048 	return 0;
1049 }
1050 
1051 /* message building helper */
1052 void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1053 		     int flags, u8 cmd)
1054 {
1055 	/* since there is no private header just add the generic one */
1056 	return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
1057 }
1058 
1059 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
1060 				     const struct ieee80211_reg_rule *rule)
1061 {
1062 	int j;
1063 	struct nlattr *nl_wmm_rules =
1064 		nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
1065 
1066 	if (!nl_wmm_rules)
1067 		goto nla_put_failure;
1068 
1069 	for (j = 0; j < IEEE80211_NUM_ACS; j++) {
1070 		struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
1071 
1072 		if (!nl_wmm_rule)
1073 			goto nla_put_failure;
1074 
1075 		if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
1076 				rule->wmm_rule.client[j].cw_min) ||
1077 		    nla_put_u16(msg, NL80211_WMMR_CW_MAX,
1078 				rule->wmm_rule.client[j].cw_max) ||
1079 		    nla_put_u8(msg, NL80211_WMMR_AIFSN,
1080 			       rule->wmm_rule.client[j].aifsn) ||
1081 		    nla_put_u16(msg, NL80211_WMMR_TXOP,
1082 			        rule->wmm_rule.client[j].cot))
1083 			goto nla_put_failure;
1084 
1085 		nla_nest_end(msg, nl_wmm_rule);
1086 	}
1087 	nla_nest_end(msg, nl_wmm_rules);
1088 
1089 	return 0;
1090 
1091 nla_put_failure:
1092 	return -ENOBUFS;
1093 }
1094 
1095 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
1096 				   struct ieee80211_channel *chan,
1097 				   bool large)
1098 {
1099 	/* Some channels must be completely excluded from the
1100 	 * list to protect old user-space tools from breaking
1101 	 */
1102 	if (!large && chan->flags &
1103 	    (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
1104 		return 0;
1105 	if (!large && chan->freq_offset)
1106 		return 0;
1107 
1108 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
1109 			chan->center_freq))
1110 		goto nla_put_failure;
1111 
1112 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_OFFSET, chan->freq_offset))
1113 		goto nla_put_failure;
1114 
1115 	if ((chan->flags & IEEE80211_CHAN_PSD) &&
1116 	    nla_put_s8(msg, NL80211_FREQUENCY_ATTR_PSD, chan->psd))
1117 		goto nla_put_failure;
1118 
1119 	if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
1120 	    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
1121 		goto nla_put_failure;
1122 	if (chan->flags & IEEE80211_CHAN_NO_IR) {
1123 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
1124 			goto nla_put_failure;
1125 		if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
1126 			goto nla_put_failure;
1127 	}
1128 	if (chan->flags & IEEE80211_CHAN_RADAR) {
1129 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
1130 			goto nla_put_failure;
1131 		if (large) {
1132 			u32 time;
1133 
1134 			time = elapsed_jiffies_msecs(chan->dfs_state_entered);
1135 
1136 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
1137 					chan->dfs_state))
1138 				goto nla_put_failure;
1139 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
1140 					time))
1141 				goto nla_put_failure;
1142 			if (nla_put_u32(msg,
1143 					NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
1144 					chan->dfs_cac_ms))
1145 				goto nla_put_failure;
1146 		}
1147 	}
1148 
1149 	if (large) {
1150 		if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
1151 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
1152 			goto nla_put_failure;
1153 		if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
1154 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
1155 			goto nla_put_failure;
1156 		if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
1157 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
1158 			goto nla_put_failure;
1159 		if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
1160 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
1161 			goto nla_put_failure;
1162 		if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
1163 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
1164 			goto nla_put_failure;
1165 		if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
1166 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
1167 			goto nla_put_failure;
1168 		if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
1169 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
1170 			goto nla_put_failure;
1171 		if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
1172 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
1173 			goto nla_put_failure;
1174 		if ((chan->flags & IEEE80211_CHAN_NO_HE) &&
1175 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HE))
1176 			goto nla_put_failure;
1177 		if ((chan->flags & IEEE80211_CHAN_1MHZ) &&
1178 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_1MHZ))
1179 			goto nla_put_failure;
1180 		if ((chan->flags & IEEE80211_CHAN_2MHZ) &&
1181 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_2MHZ))
1182 			goto nla_put_failure;
1183 		if ((chan->flags & IEEE80211_CHAN_4MHZ) &&
1184 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_4MHZ))
1185 			goto nla_put_failure;
1186 		if ((chan->flags & IEEE80211_CHAN_8MHZ) &&
1187 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_8MHZ))
1188 			goto nla_put_failure;
1189 		if ((chan->flags & IEEE80211_CHAN_16MHZ) &&
1190 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_16MHZ))
1191 			goto nla_put_failure;
1192 		if ((chan->flags & IEEE80211_CHAN_NO_320MHZ) &&
1193 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_320MHZ))
1194 			goto nla_put_failure;
1195 		if ((chan->flags & IEEE80211_CHAN_NO_EHT) &&
1196 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_EHT))
1197 			goto nla_put_failure;
1198 		if ((chan->flags & IEEE80211_CHAN_DFS_CONCURRENT) &&
1199 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DFS_CONCURRENT))
1200 			goto nla_put_failure;
1201 		if ((chan->flags & IEEE80211_CHAN_NO_6GHZ_VLP_CLIENT) &&
1202 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_6GHZ_VLP_CLIENT))
1203 			goto nla_put_failure;
1204 		if ((chan->flags & IEEE80211_CHAN_NO_6GHZ_AFC_CLIENT) &&
1205 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_6GHZ_AFC_CLIENT))
1206 			goto nla_put_failure;
1207 	}
1208 
1209 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
1210 			DBM_TO_MBM(chan->max_power)))
1211 		goto nla_put_failure;
1212 
1213 	if (large) {
1214 		const struct ieee80211_reg_rule *rule =
1215 			freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1216 
1217 		if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
1218 			if (nl80211_msg_put_wmm_rules(msg, rule))
1219 				goto nla_put_failure;
1220 		}
1221 	}
1222 
1223 	return 0;
1224 
1225  nla_put_failure:
1226 	return -ENOBUFS;
1227 }
1228 
1229 static bool nl80211_put_txq_stats(struct sk_buff *msg,
1230 				  struct cfg80211_txq_stats *txqstats,
1231 				  int attrtype)
1232 {
1233 	struct nlattr *txqattr;
1234 
1235 #define PUT_TXQVAL_U32(attr, memb) do {					  \
1236 	if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&	  \
1237 	    nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
1238 		return false;						  \
1239 	} while (0)
1240 
1241 	txqattr = nla_nest_start_noflag(msg, attrtype);
1242 	if (!txqattr)
1243 		return false;
1244 
1245 	PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
1246 	PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
1247 	PUT_TXQVAL_U32(FLOWS, flows);
1248 	PUT_TXQVAL_U32(DROPS, drops);
1249 	PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
1250 	PUT_TXQVAL_U32(OVERLIMIT, overlimit);
1251 	PUT_TXQVAL_U32(OVERMEMORY, overmemory);
1252 	PUT_TXQVAL_U32(COLLISIONS, collisions);
1253 	PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
1254 	PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
1255 	PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
1256 	nla_nest_end(msg, txqattr);
1257 
1258 #undef PUT_TXQVAL_U32
1259 	return true;
1260 }
1261 
1262 /* netlink command implementations */
1263 
1264 /**
1265  * nl80211_link_id - return link ID
1266  * @attrs: attributes to look at
1267  *
1268  * Returns: the link ID or 0 if not given
1269  *
1270  * Note this function doesn't do any validation of the link
1271  * ID validity wrt. links that were actually added, so it must
1272  * be called only from ops with %NL80211_FLAG_MLO_VALID_LINK_ID
1273  * or if additional validation is done.
1274  */
1275 static unsigned int nl80211_link_id(struct nlattr **attrs)
1276 {
1277 	struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1278 
1279 	if (!linkid)
1280 		return 0;
1281 
1282 	return nla_get_u8(linkid);
1283 }
1284 
1285 static int nl80211_link_id_or_invalid(struct nlattr **attrs)
1286 {
1287 	struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1288 
1289 	if (!linkid)
1290 		return -1;
1291 
1292 	return nla_get_u8(linkid);
1293 }
1294 
1295 struct key_parse {
1296 	struct key_params p;
1297 	int idx;
1298 	int type;
1299 	bool def, defmgmt, defbeacon;
1300 	bool def_uni, def_multi;
1301 };
1302 
1303 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
1304 				 struct key_parse *k)
1305 {
1306 	struct nlattr *tb[NL80211_KEY_MAX + 1];
1307 	int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
1308 					      nl80211_key_policy,
1309 					      info->extack);
1310 	if (err)
1311 		return err;
1312 
1313 	k->def = !!tb[NL80211_KEY_DEFAULT];
1314 	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1315 	k->defbeacon = !!tb[NL80211_KEY_DEFAULT_BEACON];
1316 
1317 	if (k->def) {
1318 		k->def_uni = true;
1319 		k->def_multi = true;
1320 	}
1321 	if (k->defmgmt || k->defbeacon)
1322 		k->def_multi = true;
1323 
1324 	if (tb[NL80211_KEY_IDX])
1325 		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1326 
1327 	if (tb[NL80211_KEY_DATA]) {
1328 		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1329 		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1330 	}
1331 
1332 	if (tb[NL80211_KEY_SEQ]) {
1333 		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1334 		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1335 	}
1336 
1337 	if (tb[NL80211_KEY_CIPHER])
1338 		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1339 
1340 	if (tb[NL80211_KEY_TYPE])
1341 		k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1342 
1343 	if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1344 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1345 
1346 		err = nla_parse_nested_deprecated(kdt,
1347 						  NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1348 						  tb[NL80211_KEY_DEFAULT_TYPES],
1349 						  nl80211_key_default_policy,
1350 						  info->extack);
1351 		if (err)
1352 			return err;
1353 
1354 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1355 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1356 	}
1357 
1358 	if (tb[NL80211_KEY_MODE])
1359 		k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1360 
1361 	return 0;
1362 }
1363 
1364 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1365 {
1366 	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1367 		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1368 		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1369 	}
1370 
1371 	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1372 		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1373 		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1374 	}
1375 
1376 	if (info->attrs[NL80211_ATTR_KEY_IDX])
1377 		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1378 
1379 	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1380 		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1381 
1382 	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1383 	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1384 
1385 	if (k->def) {
1386 		k->def_uni = true;
1387 		k->def_multi = true;
1388 	}
1389 	if (k->defmgmt)
1390 		k->def_multi = true;
1391 
1392 	if (info->attrs[NL80211_ATTR_KEY_TYPE])
1393 		k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1394 
1395 	if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1396 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1397 		int err = nla_parse_nested_deprecated(kdt,
1398 						      NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1399 						      info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1400 						      nl80211_key_default_policy,
1401 						      info->extack);
1402 		if (err)
1403 			return err;
1404 
1405 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1406 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1407 	}
1408 
1409 	return 0;
1410 }
1411 
1412 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1413 {
1414 	int err;
1415 
1416 	memset(k, 0, sizeof(*k));
1417 	k->idx = -1;
1418 	k->type = -1;
1419 
1420 	if (info->attrs[NL80211_ATTR_KEY])
1421 		err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1422 	else
1423 		err = nl80211_parse_key_old(info, k);
1424 
1425 	if (err)
1426 		return err;
1427 
1428 	if ((k->def ? 1 : 0) + (k->defmgmt ? 1 : 0) +
1429 	    (k->defbeacon ? 1 : 0) > 1) {
1430 		GENL_SET_ERR_MSG(info,
1431 				 "key with multiple default flags is invalid");
1432 		return -EINVAL;
1433 	}
1434 
1435 	if (k->defmgmt || k->defbeacon) {
1436 		if (k->def_uni || !k->def_multi) {
1437 			GENL_SET_ERR_MSG(info,
1438 					 "defmgmt/defbeacon key must be mcast");
1439 			return -EINVAL;
1440 		}
1441 	}
1442 
1443 	if (k->idx != -1) {
1444 		if (k->defmgmt) {
1445 			if (k->idx < 4 || k->idx > 5) {
1446 				GENL_SET_ERR_MSG(info,
1447 						 "defmgmt key idx not 4 or 5");
1448 				return -EINVAL;
1449 			}
1450 		} else if (k->defbeacon) {
1451 			if (k->idx < 6 || k->idx > 7) {
1452 				GENL_SET_ERR_MSG(info,
1453 						 "defbeacon key idx not 6 or 7");
1454 				return -EINVAL;
1455 			}
1456 		} else if (k->def) {
1457 			if (k->idx < 0 || k->idx > 3) {
1458 				GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1459 				return -EINVAL;
1460 			}
1461 		} else {
1462 			if (k->idx < 0 || k->idx > 7) {
1463 				GENL_SET_ERR_MSG(info, "key idx not 0-7");
1464 				return -EINVAL;
1465 			}
1466 		}
1467 	}
1468 
1469 	return 0;
1470 }
1471 
1472 static struct cfg80211_cached_keys *
1473 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1474 		       struct genl_info *info, bool *no_ht)
1475 {
1476 	struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1477 	struct key_parse parse;
1478 	struct nlattr *key;
1479 	struct cfg80211_cached_keys *result;
1480 	int rem, err, def = 0;
1481 	bool have_key = false;
1482 
1483 	nla_for_each_nested(key, keys, rem) {
1484 		have_key = true;
1485 		break;
1486 	}
1487 
1488 	if (!have_key)
1489 		return NULL;
1490 
1491 	result = kzalloc(sizeof(*result), GFP_KERNEL);
1492 	if (!result)
1493 		return ERR_PTR(-ENOMEM);
1494 
1495 	result->def = -1;
1496 
1497 	nla_for_each_nested(key, keys, rem) {
1498 		memset(&parse, 0, sizeof(parse));
1499 		parse.idx = -1;
1500 
1501 		err = nl80211_parse_key_new(info, key, &parse);
1502 		if (err)
1503 			goto error;
1504 		err = -EINVAL;
1505 		if (!parse.p.key)
1506 			goto error;
1507 		if (parse.idx < 0 || parse.idx > 3) {
1508 			GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1509 			goto error;
1510 		}
1511 		if (parse.def) {
1512 			if (def) {
1513 				GENL_SET_ERR_MSG(info,
1514 						 "only one key can be default");
1515 				goto error;
1516 			}
1517 			def = 1;
1518 			result->def = parse.idx;
1519 			if (!parse.def_uni || !parse.def_multi)
1520 				goto error;
1521 		} else if (parse.defmgmt)
1522 			goto error;
1523 		err = cfg80211_validate_key_settings(rdev, &parse.p,
1524 						     parse.idx, false, NULL);
1525 		if (err)
1526 			goto error;
1527 		if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1528 		    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1529 			GENL_SET_ERR_MSG(info, "connect key must be WEP");
1530 			err = -EINVAL;
1531 			goto error;
1532 		}
1533 		result->params[parse.idx].cipher = parse.p.cipher;
1534 		result->params[parse.idx].key_len = parse.p.key_len;
1535 		result->params[parse.idx].key = result->data[parse.idx];
1536 		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1537 
1538 		/* must be WEP key if we got here */
1539 		if (no_ht)
1540 			*no_ht = true;
1541 	}
1542 
1543 	if (result->def < 0) {
1544 		err = -EINVAL;
1545 		GENL_SET_ERR_MSG(info, "need a default/TX key");
1546 		goto error;
1547 	}
1548 
1549 	return result;
1550  error:
1551 	kfree(result);
1552 	return ERR_PTR(err);
1553 }
1554 
1555 static int nl80211_key_allowed(struct wireless_dev *wdev)
1556 {
1557 	lockdep_assert_wiphy(wdev->wiphy);
1558 
1559 	switch (wdev->iftype) {
1560 	case NL80211_IFTYPE_AP:
1561 	case NL80211_IFTYPE_AP_VLAN:
1562 	case NL80211_IFTYPE_P2P_GO:
1563 	case NL80211_IFTYPE_MESH_POINT:
1564 		break;
1565 	case NL80211_IFTYPE_ADHOC:
1566 		if (wdev->u.ibss.current_bss)
1567 			return 0;
1568 		return -ENOLINK;
1569 	case NL80211_IFTYPE_STATION:
1570 	case NL80211_IFTYPE_P2P_CLIENT:
1571 		if (wdev->connected)
1572 			return 0;
1573 		return -ENOLINK;
1574 	case NL80211_IFTYPE_NAN:
1575 		if (wiphy_ext_feature_isset(wdev->wiphy,
1576 					    NL80211_EXT_FEATURE_SECURE_NAN))
1577 			return 0;
1578 		return -EINVAL;
1579 	case NL80211_IFTYPE_UNSPECIFIED:
1580 	case NL80211_IFTYPE_OCB:
1581 	case NL80211_IFTYPE_MONITOR:
1582 	case NL80211_IFTYPE_P2P_DEVICE:
1583 	case NL80211_IFTYPE_WDS:
1584 	case NUM_NL80211_IFTYPES:
1585 		return -EINVAL;
1586 	}
1587 
1588 	return 0;
1589 }
1590 
1591 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1592 							u32 freq)
1593 {
1594 	struct ieee80211_channel *chan;
1595 
1596 	chan = ieee80211_get_channel_khz(wiphy, freq);
1597 	if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1598 		return NULL;
1599 	return chan;
1600 }
1601 
1602 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1603 {
1604 	struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1605 	int i;
1606 
1607 	if (!nl_modes)
1608 		goto nla_put_failure;
1609 
1610 	i = 0;
1611 	while (ifmodes) {
1612 		if ((ifmodes & 1) && nla_put_flag(msg, i))
1613 			goto nla_put_failure;
1614 		ifmodes >>= 1;
1615 		i++;
1616 	}
1617 
1618 	nla_nest_end(msg, nl_modes);
1619 	return 0;
1620 
1621 nla_put_failure:
1622 	return -ENOBUFS;
1623 }
1624 
1625 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1626 					  struct sk_buff *msg,
1627 					  bool large)
1628 {
1629 	struct nlattr *nl_combis;
1630 	int i, j;
1631 
1632 	nl_combis = nla_nest_start_noflag(msg,
1633 					  NL80211_ATTR_INTERFACE_COMBINATIONS);
1634 	if (!nl_combis)
1635 		goto nla_put_failure;
1636 
1637 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1638 		const struct ieee80211_iface_combination *c;
1639 		struct nlattr *nl_combi, *nl_limits;
1640 
1641 		c = &wiphy->iface_combinations[i];
1642 
1643 		nl_combi = nla_nest_start_noflag(msg, i + 1);
1644 		if (!nl_combi)
1645 			goto nla_put_failure;
1646 
1647 		nl_limits = nla_nest_start_noflag(msg,
1648 						  NL80211_IFACE_COMB_LIMITS);
1649 		if (!nl_limits)
1650 			goto nla_put_failure;
1651 
1652 		for (j = 0; j < c->n_limits; j++) {
1653 			struct nlattr *nl_limit;
1654 
1655 			nl_limit = nla_nest_start_noflag(msg, j + 1);
1656 			if (!nl_limit)
1657 				goto nla_put_failure;
1658 			if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1659 					c->limits[j].max))
1660 				goto nla_put_failure;
1661 			if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1662 						c->limits[j].types))
1663 				goto nla_put_failure;
1664 			nla_nest_end(msg, nl_limit);
1665 		}
1666 
1667 		nla_nest_end(msg, nl_limits);
1668 
1669 		if (c->beacon_int_infra_match &&
1670 		    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1671 			goto nla_put_failure;
1672 		if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1673 				c->num_different_channels) ||
1674 		    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1675 				c->max_interfaces))
1676 			goto nla_put_failure;
1677 		if (large &&
1678 		    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1679 				c->radar_detect_widths) ||
1680 		     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1681 				c->radar_detect_regions)))
1682 			goto nla_put_failure;
1683 		if (c->beacon_int_min_gcd &&
1684 		    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1685 				c->beacon_int_min_gcd))
1686 			goto nla_put_failure;
1687 
1688 		nla_nest_end(msg, nl_combi);
1689 	}
1690 
1691 	nla_nest_end(msg, nl_combis);
1692 
1693 	return 0;
1694 nla_put_failure:
1695 	return -ENOBUFS;
1696 }
1697 
1698 #ifdef CONFIG_PM
1699 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1700 					struct sk_buff *msg)
1701 {
1702 	const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1703 	struct nlattr *nl_tcp;
1704 
1705 	if (!tcp)
1706 		return 0;
1707 
1708 	nl_tcp = nla_nest_start_noflag(msg,
1709 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1710 	if (!nl_tcp)
1711 		return -ENOBUFS;
1712 
1713 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1714 			tcp->data_payload_max))
1715 		return -ENOBUFS;
1716 
1717 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1718 			tcp->data_payload_max))
1719 		return -ENOBUFS;
1720 
1721 	if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1722 		return -ENOBUFS;
1723 
1724 	if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1725 				sizeof(*tcp->tok), tcp->tok))
1726 		return -ENOBUFS;
1727 
1728 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1729 			tcp->data_interval_max))
1730 		return -ENOBUFS;
1731 
1732 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1733 			tcp->wake_payload_max))
1734 		return -ENOBUFS;
1735 
1736 	nla_nest_end(msg, nl_tcp);
1737 	return 0;
1738 }
1739 
1740 static int nl80211_send_wowlan(struct sk_buff *msg,
1741 			       struct cfg80211_registered_device *rdev,
1742 			       bool large)
1743 {
1744 	struct nlattr *nl_wowlan;
1745 
1746 	if (!rdev->wiphy.wowlan)
1747 		return 0;
1748 
1749 	nl_wowlan = nla_nest_start_noflag(msg,
1750 					  NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1751 	if (!nl_wowlan)
1752 		return -ENOBUFS;
1753 
1754 	if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1755 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1756 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1757 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1758 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1759 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1760 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1761 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1762 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1763 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1764 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1765 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1766 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1767 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1768 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1769 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1770 		return -ENOBUFS;
1771 
1772 	if (rdev->wiphy.wowlan->n_patterns) {
1773 		struct nl80211_pattern_support pat = {
1774 			.max_patterns = rdev->wiphy.wowlan->n_patterns,
1775 			.min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1776 			.max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1777 			.max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1778 		};
1779 
1780 		if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1781 			    sizeof(pat), &pat))
1782 			return -ENOBUFS;
1783 	}
1784 
1785 	if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1786 	    nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1787 			rdev->wiphy.wowlan->max_nd_match_sets))
1788 		return -ENOBUFS;
1789 
1790 	if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1791 		return -ENOBUFS;
1792 
1793 	nla_nest_end(msg, nl_wowlan);
1794 
1795 	return 0;
1796 }
1797 #endif
1798 
1799 static int nl80211_send_coalesce(struct sk_buff *msg,
1800 				 struct cfg80211_registered_device *rdev)
1801 {
1802 	struct nl80211_coalesce_rule_support rule;
1803 
1804 	if (!rdev->wiphy.coalesce)
1805 		return 0;
1806 
1807 	rule.max_rules = rdev->wiphy.coalesce->n_rules;
1808 	rule.max_delay = rdev->wiphy.coalesce->max_delay;
1809 	rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1810 	rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1811 	rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1812 	rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1813 
1814 	if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1815 		return -ENOBUFS;
1816 
1817 	return 0;
1818 }
1819 
1820 static int
1821 nl80211_send_iftype_data(struct sk_buff *msg,
1822 			 const struct ieee80211_supported_band *sband,
1823 			 const struct ieee80211_sband_iftype_data *iftdata)
1824 {
1825 	const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1826 	const struct ieee80211_sta_eht_cap *eht_cap = &iftdata->eht_cap;
1827 
1828 	if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1829 				iftdata->types_mask))
1830 		return -ENOBUFS;
1831 
1832 	if (he_cap->has_he) {
1833 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1834 			    sizeof(he_cap->he_cap_elem.mac_cap_info),
1835 			    he_cap->he_cap_elem.mac_cap_info) ||
1836 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1837 			    sizeof(he_cap->he_cap_elem.phy_cap_info),
1838 			    he_cap->he_cap_elem.phy_cap_info) ||
1839 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1840 			    sizeof(he_cap->he_mcs_nss_supp),
1841 			    &he_cap->he_mcs_nss_supp) ||
1842 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1843 			    sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1844 			return -ENOBUFS;
1845 	}
1846 
1847 	if (eht_cap->has_eht && he_cap->has_he) {
1848 		u8 mcs_nss_size, ppe_thresh_size;
1849 		u16 ppe_thres_hdr;
1850 		bool is_ap;
1851 
1852 		is_ap = iftdata->types_mask & BIT(NL80211_IFTYPE_AP) ||
1853 			iftdata->types_mask & BIT(NL80211_IFTYPE_P2P_GO);
1854 
1855 		mcs_nss_size =
1856 			ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
1857 						   &eht_cap->eht_cap_elem,
1858 						   is_ap);
1859 
1860 		ppe_thres_hdr = get_unaligned_le16(&eht_cap->eht_ppe_thres[0]);
1861 		ppe_thresh_size =
1862 			ieee80211_eht_ppe_size(ppe_thres_hdr,
1863 					       eht_cap->eht_cap_elem.phy_cap_info);
1864 
1865 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MAC,
1866 			    sizeof(eht_cap->eht_cap_elem.mac_cap_info),
1867 			    eht_cap->eht_cap_elem.mac_cap_info) ||
1868 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY,
1869 			    sizeof(eht_cap->eht_cap_elem.phy_cap_info),
1870 			    eht_cap->eht_cap_elem.phy_cap_info) ||
1871 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MCS_SET,
1872 			    mcs_nss_size, &eht_cap->eht_mcs_nss_supp) ||
1873 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PPE,
1874 			    ppe_thresh_size, eht_cap->eht_ppe_thres))
1875 			return -ENOBUFS;
1876 	}
1877 
1878 	if (sband->band == NL80211_BAND_6GHZ &&
1879 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_6GHZ_CAPA,
1880 		    sizeof(iftdata->he_6ghz_capa),
1881 		    &iftdata->he_6ghz_capa))
1882 		return -ENOBUFS;
1883 
1884 	if (iftdata->vendor_elems.data && iftdata->vendor_elems.len &&
1885 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_VENDOR_ELEMS,
1886 		    iftdata->vendor_elems.len, iftdata->vendor_elems.data))
1887 		return -ENOBUFS;
1888 
1889 	return 0;
1890 }
1891 
1892 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1893 				      struct ieee80211_supported_band *sband,
1894 				      bool large)
1895 {
1896 	struct nlattr *nl_rates, *nl_rate;
1897 	struct ieee80211_rate *rate;
1898 	int i;
1899 
1900 	/* add HT info */
1901 	if (sband->ht_cap.ht_supported &&
1902 	    (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1903 		     sizeof(sband->ht_cap.mcs),
1904 		     &sband->ht_cap.mcs) ||
1905 	     nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1906 			 sband->ht_cap.cap) ||
1907 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1908 			sband->ht_cap.ampdu_factor) ||
1909 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1910 			sband->ht_cap.ampdu_density)))
1911 		return -ENOBUFS;
1912 
1913 	/* add VHT info */
1914 	if (sband->vht_cap.vht_supported &&
1915 	    (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1916 		     sizeof(sband->vht_cap.vht_mcs),
1917 		     &sband->vht_cap.vht_mcs) ||
1918 	     nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1919 			 sband->vht_cap.cap)))
1920 		return -ENOBUFS;
1921 
1922 	if (large && sband->n_iftype_data) {
1923 		struct nlattr *nl_iftype_data =
1924 			nla_nest_start_noflag(msg,
1925 					      NL80211_BAND_ATTR_IFTYPE_DATA);
1926 		const struct ieee80211_sband_iftype_data *iftd;
1927 		int err;
1928 
1929 		if (!nl_iftype_data)
1930 			return -ENOBUFS;
1931 
1932 		for_each_sband_iftype_data(sband, i, iftd) {
1933 			struct nlattr *iftdata;
1934 
1935 			iftdata = nla_nest_start_noflag(msg, i + 1);
1936 			if (!iftdata)
1937 				return -ENOBUFS;
1938 
1939 			err = nl80211_send_iftype_data(msg, sband, iftd);
1940 			if (err)
1941 				return err;
1942 
1943 			nla_nest_end(msg, iftdata);
1944 		}
1945 
1946 		nla_nest_end(msg, nl_iftype_data);
1947 	}
1948 
1949 	/* add EDMG info */
1950 	if (large && sband->edmg_cap.channels &&
1951 	    (nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_CHANNELS,
1952 		       sband->edmg_cap.channels) ||
1953 	    nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_BW_CONFIG,
1954 		       sband->edmg_cap.bw_config)))
1955 
1956 		return -ENOBUFS;
1957 
1958 	/* add bitrates */
1959 	nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1960 	if (!nl_rates)
1961 		return -ENOBUFS;
1962 
1963 	for (i = 0; i < sband->n_bitrates; i++) {
1964 		nl_rate = nla_nest_start_noflag(msg, i);
1965 		if (!nl_rate)
1966 			return -ENOBUFS;
1967 
1968 		rate = &sband->bitrates[i];
1969 		if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1970 				rate->bitrate))
1971 			return -ENOBUFS;
1972 		if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1973 		    nla_put_flag(msg,
1974 				 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1975 			return -ENOBUFS;
1976 
1977 		nla_nest_end(msg, nl_rate);
1978 	}
1979 
1980 	nla_nest_end(msg, nl_rates);
1981 
1982 	/* S1G capabilities */
1983 	if (sband->band == NL80211_BAND_S1GHZ && sband->s1g_cap.s1g &&
1984 	    (nla_put(msg, NL80211_BAND_ATTR_S1G_CAPA,
1985 		     sizeof(sband->s1g_cap.cap),
1986 		     sband->s1g_cap.cap) ||
1987 	     nla_put(msg, NL80211_BAND_ATTR_S1G_MCS_NSS_SET,
1988 		     sizeof(sband->s1g_cap.nss_mcs),
1989 		     sband->s1g_cap.nss_mcs)))
1990 		return -ENOBUFS;
1991 
1992 	return 0;
1993 }
1994 
1995 static int
1996 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1997 			 const struct ieee80211_txrx_stypes *mgmt_stypes)
1998 {
1999 	u16 stypes;
2000 	struct nlattr *nl_ftypes, *nl_ifs;
2001 	enum nl80211_iftype ift;
2002 	int i;
2003 
2004 	if (!mgmt_stypes)
2005 		return 0;
2006 
2007 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
2008 	if (!nl_ifs)
2009 		return -ENOBUFS;
2010 
2011 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
2012 		nl_ftypes = nla_nest_start_noflag(msg, ift);
2013 		if (!nl_ftypes)
2014 			return -ENOBUFS;
2015 		i = 0;
2016 		stypes = mgmt_stypes[ift].tx;
2017 		while (stypes) {
2018 			if ((stypes & 1) &&
2019 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
2020 					(i << 4) | IEEE80211_FTYPE_MGMT))
2021 				return -ENOBUFS;
2022 			stypes >>= 1;
2023 			i++;
2024 		}
2025 		nla_nest_end(msg, nl_ftypes);
2026 	}
2027 
2028 	nla_nest_end(msg, nl_ifs);
2029 
2030 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
2031 	if (!nl_ifs)
2032 		return -ENOBUFS;
2033 
2034 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
2035 		nl_ftypes = nla_nest_start_noflag(msg, ift);
2036 		if (!nl_ftypes)
2037 			return -ENOBUFS;
2038 		i = 0;
2039 		stypes = mgmt_stypes[ift].rx;
2040 		while (stypes) {
2041 			if ((stypes & 1) &&
2042 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
2043 					(i << 4) | IEEE80211_FTYPE_MGMT))
2044 				return -ENOBUFS;
2045 			stypes >>= 1;
2046 			i++;
2047 		}
2048 		nla_nest_end(msg, nl_ftypes);
2049 	}
2050 	nla_nest_end(msg, nl_ifs);
2051 
2052 	return 0;
2053 }
2054 
2055 #define CMD(op, n)							\
2056 	 do {								\
2057 		if (rdev->ops->op) {					\
2058 			i++;						\
2059 			if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) 	\
2060 				goto nla_put_failure;			\
2061 		}							\
2062 	} while (0)
2063 
2064 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
2065 					struct sk_buff *msg)
2066 {
2067 	int i = 0;
2068 
2069 	/*
2070 	 * do *NOT* add anything into this function, new things need to be
2071 	 * advertised only to new versions of userspace that can deal with
2072 	 * the split (and they can't possibly care about new features...
2073 	 */
2074 	CMD(add_virtual_intf, NEW_INTERFACE);
2075 	CMD(change_virtual_intf, SET_INTERFACE);
2076 	CMD(add_key, NEW_KEY);
2077 	CMD(start_ap, START_AP);
2078 	CMD(add_station, NEW_STATION);
2079 	CMD(add_mpath, NEW_MPATH);
2080 	CMD(update_mesh_config, SET_MESH_CONFIG);
2081 	CMD(change_bss, SET_BSS);
2082 	CMD(auth, AUTHENTICATE);
2083 	CMD(assoc, ASSOCIATE);
2084 	CMD(deauth, DEAUTHENTICATE);
2085 	CMD(disassoc, DISASSOCIATE);
2086 	CMD(join_ibss, JOIN_IBSS);
2087 	CMD(join_mesh, JOIN_MESH);
2088 	CMD(set_pmksa, SET_PMKSA);
2089 	CMD(del_pmksa, DEL_PMKSA);
2090 	CMD(flush_pmksa, FLUSH_PMKSA);
2091 	if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
2092 		CMD(remain_on_channel, REMAIN_ON_CHANNEL);
2093 	CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
2094 	CMD(mgmt_tx, FRAME);
2095 	CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
2096 	if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
2097 		i++;
2098 		if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
2099 			goto nla_put_failure;
2100 	}
2101 	if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
2102 	    rdev->ops->join_mesh) {
2103 		i++;
2104 		if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
2105 			goto nla_put_failure;
2106 	}
2107 	if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2108 		CMD(tdls_mgmt, TDLS_MGMT);
2109 		CMD(tdls_oper, TDLS_OPER);
2110 	}
2111 	if (rdev->wiphy.max_sched_scan_reqs)
2112 		CMD(sched_scan_start, START_SCHED_SCAN);
2113 	CMD(probe_client, PROBE_CLIENT);
2114 	CMD(set_noack_map, SET_NOACK_MAP);
2115 	if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
2116 		i++;
2117 		if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
2118 			goto nla_put_failure;
2119 	}
2120 	CMD(start_p2p_device, START_P2P_DEVICE);
2121 	CMD(set_mcast_rate, SET_MCAST_RATE);
2122 #ifdef CONFIG_NL80211_TESTMODE
2123 	CMD(testmode_cmd, TESTMODE);
2124 #endif
2125 
2126 	if (rdev->ops->connect || rdev->ops->auth) {
2127 		i++;
2128 		if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
2129 			goto nla_put_failure;
2130 	}
2131 
2132 	if (rdev->ops->disconnect || rdev->ops->deauth) {
2133 		i++;
2134 		if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
2135 			goto nla_put_failure;
2136 	}
2137 
2138 	return i;
2139  nla_put_failure:
2140 	return -ENOBUFS;
2141 }
2142 
2143 static int
2144 nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
2145 			   struct sk_buff *msg)
2146 {
2147 	struct nlattr *ftm;
2148 
2149 	if (!cap->ftm.supported)
2150 		return 0;
2151 
2152 	ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
2153 	if (!ftm)
2154 		return -ENOBUFS;
2155 
2156 	if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
2157 		return -ENOBUFS;
2158 	if (cap->ftm.non_asap &&
2159 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
2160 		return -ENOBUFS;
2161 	if (cap->ftm.request_lci &&
2162 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
2163 		return -ENOBUFS;
2164 	if (cap->ftm.request_civicloc &&
2165 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
2166 		return -ENOBUFS;
2167 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
2168 			cap->ftm.preambles))
2169 		return -ENOBUFS;
2170 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
2171 			cap->ftm.bandwidths))
2172 		return -ENOBUFS;
2173 	if (cap->ftm.max_bursts_exponent >= 0 &&
2174 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
2175 			cap->ftm.max_bursts_exponent))
2176 		return -ENOBUFS;
2177 	if (cap->ftm.max_ftms_per_burst &&
2178 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
2179 			cap->ftm.max_ftms_per_burst))
2180 		return -ENOBUFS;
2181 	if (cap->ftm.trigger_based &&
2182 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_TRIGGER_BASED))
2183 		return -ENOBUFS;
2184 	if (cap->ftm.non_trigger_based &&
2185 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_TRIGGER_BASED))
2186 		return -ENOBUFS;
2187 
2188 	nla_nest_end(msg, ftm);
2189 	return 0;
2190 }
2191 
2192 static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
2193 				  struct sk_buff *msg)
2194 {
2195 	const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
2196 	struct nlattr *pmsr, *caps;
2197 
2198 	if (!cap)
2199 		return 0;
2200 
2201 	/*
2202 	 * we don't need to clean up anything here since the caller
2203 	 * will genlmsg_cancel() if we fail
2204 	 */
2205 
2206 	pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
2207 	if (!pmsr)
2208 		return -ENOBUFS;
2209 
2210 	if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
2211 		return -ENOBUFS;
2212 
2213 	if (cap->report_ap_tsf &&
2214 	    nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
2215 		return -ENOBUFS;
2216 
2217 	if (cap->randomize_mac_addr &&
2218 	    nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
2219 		return -ENOBUFS;
2220 
2221 	caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
2222 	if (!caps)
2223 		return -ENOBUFS;
2224 
2225 	if (nl80211_send_pmsr_ftm_capa(cap, msg))
2226 		return -ENOBUFS;
2227 
2228 	nla_nest_end(msg, caps);
2229 	nla_nest_end(msg, pmsr);
2230 
2231 	return 0;
2232 }
2233 
2234 static int
2235 nl80211_put_iftype_akm_suites(struct cfg80211_registered_device *rdev,
2236 			      struct sk_buff *msg)
2237 {
2238 	int i;
2239 	struct nlattr *nested, *nested_akms;
2240 	const struct wiphy_iftype_akm_suites *iftype_akms;
2241 
2242 	if (!rdev->wiphy.num_iftype_akm_suites ||
2243 	    !rdev->wiphy.iftype_akm_suites)
2244 		return 0;
2245 
2246 	nested = nla_nest_start(msg, NL80211_ATTR_IFTYPE_AKM_SUITES);
2247 	if (!nested)
2248 		return -ENOBUFS;
2249 
2250 	for (i = 0; i < rdev->wiphy.num_iftype_akm_suites; i++) {
2251 		nested_akms = nla_nest_start(msg, i + 1);
2252 		if (!nested_akms)
2253 			return -ENOBUFS;
2254 
2255 		iftype_akms = &rdev->wiphy.iftype_akm_suites[i];
2256 
2257 		if (nl80211_put_iftypes(msg, NL80211_IFTYPE_AKM_ATTR_IFTYPES,
2258 					iftype_akms->iftypes_mask))
2259 			return -ENOBUFS;
2260 
2261 		if (nla_put(msg, NL80211_IFTYPE_AKM_ATTR_SUITES,
2262 			    sizeof(u32) * iftype_akms->n_akm_suites,
2263 			    iftype_akms->akm_suites)) {
2264 			return -ENOBUFS;
2265 		}
2266 		nla_nest_end(msg, nested_akms);
2267 	}
2268 
2269 	nla_nest_end(msg, nested);
2270 
2271 	return 0;
2272 }
2273 
2274 static int
2275 nl80211_put_tid_config_support(struct cfg80211_registered_device *rdev,
2276 			       struct sk_buff *msg)
2277 {
2278 	struct nlattr *supp;
2279 
2280 	if (!rdev->wiphy.tid_config_support.vif &&
2281 	    !rdev->wiphy.tid_config_support.peer)
2282 		return 0;
2283 
2284 	supp = nla_nest_start(msg, NL80211_ATTR_TID_CONFIG);
2285 	if (!supp)
2286 		return -ENOSPC;
2287 
2288 	if (rdev->wiphy.tid_config_support.vif &&
2289 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_VIF_SUPP,
2290 			      rdev->wiphy.tid_config_support.vif,
2291 			      NL80211_TID_CONFIG_ATTR_PAD))
2292 		goto fail;
2293 
2294 	if (rdev->wiphy.tid_config_support.peer &&
2295 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_PEER_SUPP,
2296 			      rdev->wiphy.tid_config_support.peer,
2297 			      NL80211_TID_CONFIG_ATTR_PAD))
2298 		goto fail;
2299 
2300 	/* for now we just use the same value ... makes more sense */
2301 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_SHORT,
2302 		       rdev->wiphy.tid_config_support.max_retry))
2303 		goto fail;
2304 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_LONG,
2305 		       rdev->wiphy.tid_config_support.max_retry))
2306 		goto fail;
2307 
2308 	nla_nest_end(msg, supp);
2309 
2310 	return 0;
2311 fail:
2312 	nla_nest_cancel(msg, supp);
2313 	return -ENOBUFS;
2314 }
2315 
2316 static int
2317 nl80211_put_sar_specs(struct cfg80211_registered_device *rdev,
2318 		      struct sk_buff *msg)
2319 {
2320 	struct nlattr *sar_capa, *specs, *sub_freq_range;
2321 	u8 num_freq_ranges;
2322 	int i;
2323 
2324 	if (!rdev->wiphy.sar_capa)
2325 		return 0;
2326 
2327 	num_freq_ranges = rdev->wiphy.sar_capa->num_freq_ranges;
2328 
2329 	sar_capa = nla_nest_start(msg, NL80211_ATTR_SAR_SPEC);
2330 	if (!sar_capa)
2331 		return -ENOSPC;
2332 
2333 	if (nla_put_u32(msg, NL80211_SAR_ATTR_TYPE, rdev->wiphy.sar_capa->type))
2334 		goto fail;
2335 
2336 	specs = nla_nest_start(msg, NL80211_SAR_ATTR_SPECS);
2337 	if (!specs)
2338 		goto fail;
2339 
2340 	/* report supported freq_ranges */
2341 	for (i = 0; i < num_freq_ranges; i++) {
2342 		sub_freq_range = nla_nest_start(msg, i + 1);
2343 		if (!sub_freq_range)
2344 			goto fail;
2345 
2346 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_START_FREQ,
2347 				rdev->wiphy.sar_capa->freq_ranges[i].start_freq))
2348 			goto fail;
2349 
2350 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_END_FREQ,
2351 				rdev->wiphy.sar_capa->freq_ranges[i].end_freq))
2352 			goto fail;
2353 
2354 		nla_nest_end(msg, sub_freq_range);
2355 	}
2356 
2357 	nla_nest_end(msg, specs);
2358 	nla_nest_end(msg, sar_capa);
2359 
2360 	return 0;
2361 fail:
2362 	nla_nest_cancel(msg, sar_capa);
2363 	return -ENOBUFS;
2364 }
2365 
2366 static int nl80211_put_mbssid_support(struct wiphy *wiphy, struct sk_buff *msg)
2367 {
2368 	struct nlattr *config;
2369 
2370 	if (!wiphy->mbssid_max_interfaces)
2371 		return 0;
2372 
2373 	config = nla_nest_start(msg, NL80211_ATTR_MBSSID_CONFIG);
2374 	if (!config)
2375 		return -ENOBUFS;
2376 
2377 	if (nla_put_u8(msg, NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES,
2378 		       wiphy->mbssid_max_interfaces))
2379 		goto fail;
2380 
2381 	if (wiphy->ema_max_profile_periodicity &&
2382 	    nla_put_u8(msg,
2383 		       NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY,
2384 		       wiphy->ema_max_profile_periodicity))
2385 		goto fail;
2386 
2387 	nla_nest_end(msg, config);
2388 	return 0;
2389 
2390 fail:
2391 	nla_nest_cancel(msg, config);
2392 	return -ENOBUFS;
2393 }
2394 
2395 struct nl80211_dump_wiphy_state {
2396 	s64 filter_wiphy;
2397 	long start;
2398 	long split_start, band_start, chan_start, capa_start;
2399 	bool split;
2400 };
2401 
2402 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
2403 			      enum nl80211_commands cmd,
2404 			      struct sk_buff *msg, u32 portid, u32 seq,
2405 			      int flags, struct nl80211_dump_wiphy_state *state)
2406 {
2407 	void *hdr;
2408 	struct nlattr *nl_bands, *nl_band;
2409 	struct nlattr *nl_freqs, *nl_freq;
2410 	struct nlattr *nl_cmds;
2411 	enum nl80211_band band;
2412 	struct ieee80211_channel *chan;
2413 	int i;
2414 	const struct ieee80211_txrx_stypes *mgmt_stypes =
2415 				rdev->wiphy.mgmt_stypes;
2416 	u32 features;
2417 
2418 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2419 	if (!hdr)
2420 		return -ENOBUFS;
2421 
2422 	if (WARN_ON(!state))
2423 		return -EINVAL;
2424 
2425 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2426 	    nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
2427 			   wiphy_name(&rdev->wiphy)) ||
2428 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
2429 			cfg80211_rdev_list_generation))
2430 		goto nla_put_failure;
2431 
2432 	if (cmd != NL80211_CMD_NEW_WIPHY)
2433 		goto finish;
2434 
2435 	switch (state->split_start) {
2436 	case 0:
2437 		if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
2438 			       rdev->wiphy.retry_short) ||
2439 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
2440 			       rdev->wiphy.retry_long) ||
2441 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
2442 				rdev->wiphy.frag_threshold) ||
2443 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
2444 				rdev->wiphy.rts_threshold) ||
2445 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
2446 			       rdev->wiphy.coverage_class) ||
2447 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
2448 			       rdev->wiphy.max_scan_ssids) ||
2449 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
2450 			       rdev->wiphy.max_sched_scan_ssids) ||
2451 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
2452 				rdev->wiphy.max_scan_ie_len) ||
2453 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
2454 				rdev->wiphy.max_sched_scan_ie_len) ||
2455 		    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
2456 			       rdev->wiphy.max_match_sets))
2457 			goto nla_put_failure;
2458 
2459 		if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
2460 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
2461 			goto nla_put_failure;
2462 		if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
2463 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
2464 			goto nla_put_failure;
2465 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2466 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
2467 			goto nla_put_failure;
2468 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
2469 		    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
2470 			goto nla_put_failure;
2471 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2472 		    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
2473 			goto nla_put_failure;
2474 		if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
2475 		    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
2476 			goto nla_put_failure;
2477 		state->split_start++;
2478 		if (state->split)
2479 			break;
2480 		fallthrough;
2481 	case 1:
2482 		if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
2483 			    sizeof(u32) * rdev->wiphy.n_cipher_suites,
2484 			    rdev->wiphy.cipher_suites))
2485 			goto nla_put_failure;
2486 
2487 		if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
2488 			       rdev->wiphy.max_num_pmkids))
2489 			goto nla_put_failure;
2490 
2491 		if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
2492 		    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
2493 			goto nla_put_failure;
2494 
2495 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
2496 				rdev->wiphy.available_antennas_tx) ||
2497 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
2498 				rdev->wiphy.available_antennas_rx))
2499 			goto nla_put_failure;
2500 
2501 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
2502 		    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
2503 				rdev->wiphy.probe_resp_offload))
2504 			goto nla_put_failure;
2505 
2506 		if ((rdev->wiphy.available_antennas_tx ||
2507 		     rdev->wiphy.available_antennas_rx) &&
2508 		    rdev->ops->get_antenna) {
2509 			u32 tx_ant = 0, rx_ant = 0;
2510 			int res;
2511 
2512 			res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
2513 			if (!res) {
2514 				if (nla_put_u32(msg,
2515 						NL80211_ATTR_WIPHY_ANTENNA_TX,
2516 						tx_ant) ||
2517 				    nla_put_u32(msg,
2518 						NL80211_ATTR_WIPHY_ANTENNA_RX,
2519 						rx_ant))
2520 					goto nla_put_failure;
2521 			}
2522 		}
2523 
2524 		state->split_start++;
2525 		if (state->split)
2526 			break;
2527 		fallthrough;
2528 	case 2:
2529 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
2530 					rdev->wiphy.interface_modes))
2531 				goto nla_put_failure;
2532 		state->split_start++;
2533 		if (state->split)
2534 			break;
2535 		fallthrough;
2536 	case 3:
2537 		nl_bands = nla_nest_start_noflag(msg,
2538 						 NL80211_ATTR_WIPHY_BANDS);
2539 		if (!nl_bands)
2540 			goto nla_put_failure;
2541 
2542 		for (band = state->band_start;
2543 		     band < (state->split ?
2544 				NUM_NL80211_BANDS :
2545 				NL80211_BAND_60GHZ + 1);
2546 		     band++) {
2547 			struct ieee80211_supported_band *sband;
2548 
2549 			/* omit higher bands for ancient software */
2550 			if (band > NL80211_BAND_5GHZ && !state->split)
2551 				break;
2552 
2553 			sband = rdev->wiphy.bands[band];
2554 
2555 			if (!sband)
2556 				continue;
2557 
2558 			nl_band = nla_nest_start_noflag(msg, band);
2559 			if (!nl_band)
2560 				goto nla_put_failure;
2561 
2562 			switch (state->chan_start) {
2563 			case 0:
2564 				if (nl80211_send_band_rateinfo(msg, sband,
2565 							       state->split))
2566 					goto nla_put_failure;
2567 				state->chan_start++;
2568 				if (state->split)
2569 					break;
2570 				fallthrough;
2571 			default:
2572 				/* add frequencies */
2573 				nl_freqs = nla_nest_start_noflag(msg,
2574 								 NL80211_BAND_ATTR_FREQS);
2575 				if (!nl_freqs)
2576 					goto nla_put_failure;
2577 
2578 				for (i = state->chan_start - 1;
2579 				     i < sband->n_channels;
2580 				     i++) {
2581 					nl_freq = nla_nest_start_noflag(msg,
2582 									i);
2583 					if (!nl_freq)
2584 						goto nla_put_failure;
2585 
2586 					chan = &sband->channels[i];
2587 
2588 					if (nl80211_msg_put_channel(
2589 							msg, &rdev->wiphy, chan,
2590 							state->split))
2591 						goto nla_put_failure;
2592 
2593 					nla_nest_end(msg, nl_freq);
2594 					if (state->split)
2595 						break;
2596 				}
2597 				if (i < sband->n_channels)
2598 					state->chan_start = i + 2;
2599 				else
2600 					state->chan_start = 0;
2601 				nla_nest_end(msg, nl_freqs);
2602 			}
2603 
2604 			nla_nest_end(msg, nl_band);
2605 
2606 			if (state->split) {
2607 				/* start again here */
2608 				if (state->chan_start)
2609 					band--;
2610 				break;
2611 			}
2612 		}
2613 		nla_nest_end(msg, nl_bands);
2614 
2615 		if (band < NUM_NL80211_BANDS)
2616 			state->band_start = band + 1;
2617 		else
2618 			state->band_start = 0;
2619 
2620 		/* if bands & channels are done, continue outside */
2621 		if (state->band_start == 0 && state->chan_start == 0)
2622 			state->split_start++;
2623 		if (state->split)
2624 			break;
2625 		fallthrough;
2626 	case 4:
2627 		nl_cmds = nla_nest_start_noflag(msg,
2628 						NL80211_ATTR_SUPPORTED_COMMANDS);
2629 		if (!nl_cmds)
2630 			goto nla_put_failure;
2631 
2632 		i = nl80211_add_commands_unsplit(rdev, msg);
2633 		if (i < 0)
2634 			goto nla_put_failure;
2635 		if (state->split) {
2636 			CMD(crit_proto_start, CRIT_PROTOCOL_START);
2637 			CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2638 			if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2639 				CMD(channel_switch, CHANNEL_SWITCH);
2640 			CMD(set_qos_map, SET_QOS_MAP);
2641 			if (rdev->wiphy.features &
2642 					NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2643 				CMD(add_tx_ts, ADD_TX_TS);
2644 			CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2645 			CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2646 			CMD(update_ft_ies, UPDATE_FT_IES);
2647 			if (rdev->wiphy.sar_capa)
2648 				CMD(set_sar_specs, SET_SAR_SPECS);
2649 		}
2650 #undef CMD
2651 
2652 		nla_nest_end(msg, nl_cmds);
2653 		state->split_start++;
2654 		if (state->split)
2655 			break;
2656 		fallthrough;
2657 	case 5:
2658 		if (rdev->ops->remain_on_channel &&
2659 		    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2660 		    nla_put_u32(msg,
2661 				NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2662 				rdev->wiphy.max_remain_on_channel_duration))
2663 			goto nla_put_failure;
2664 
2665 		if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2666 		    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2667 			goto nla_put_failure;
2668 
2669 		state->split_start++;
2670 		if (state->split)
2671 			break;
2672 		fallthrough;
2673 	case 6:
2674 #ifdef CONFIG_PM
2675 		if (nl80211_send_wowlan(msg, rdev, state->split))
2676 			goto nla_put_failure;
2677 		state->split_start++;
2678 		if (state->split)
2679 			break;
2680 #else
2681 		state->split_start++;
2682 #endif
2683 		fallthrough;
2684 	case 7:
2685 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2686 					rdev->wiphy.software_iftypes))
2687 			goto nla_put_failure;
2688 
2689 		if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2690 						   state->split))
2691 			goto nla_put_failure;
2692 
2693 		state->split_start++;
2694 		if (state->split)
2695 			break;
2696 		fallthrough;
2697 	case 8:
2698 		if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2699 		    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2700 				rdev->wiphy.ap_sme_capa))
2701 			goto nla_put_failure;
2702 
2703 		features = rdev->wiphy.features;
2704 		/*
2705 		 * We can only add the per-channel limit information if the
2706 		 * dump is split, otherwise it makes it too big. Therefore
2707 		 * only advertise it in that case.
2708 		 */
2709 		if (state->split)
2710 			features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2711 		if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2712 			goto nla_put_failure;
2713 
2714 		if (rdev->wiphy.ht_capa_mod_mask &&
2715 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2716 			    sizeof(*rdev->wiphy.ht_capa_mod_mask),
2717 			    rdev->wiphy.ht_capa_mod_mask))
2718 			goto nla_put_failure;
2719 
2720 		if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2721 		    rdev->wiphy.max_acl_mac_addrs &&
2722 		    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2723 				rdev->wiphy.max_acl_mac_addrs))
2724 			goto nla_put_failure;
2725 
2726 		/*
2727 		 * Any information below this point is only available to
2728 		 * applications that can deal with it being split. This
2729 		 * helps ensure that newly added capabilities don't break
2730 		 * older tools by overrunning their buffers.
2731 		 *
2732 		 * We still increment split_start so that in the split
2733 		 * case we'll continue with more data in the next round,
2734 		 * but break unconditionally so unsplit data stops here.
2735 		 */
2736 		if (state->split)
2737 			state->split_start++;
2738 		else
2739 			state->split_start = 0;
2740 		break;
2741 	case 9:
2742 		if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2743 			goto nla_put_failure;
2744 
2745 		if (nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
2746 				rdev->wiphy.max_sched_scan_plans) ||
2747 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
2748 				rdev->wiphy.max_sched_scan_plan_interval) ||
2749 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
2750 				rdev->wiphy.max_sched_scan_plan_iterations))
2751 			goto nla_put_failure;
2752 
2753 		if (rdev->wiphy.extended_capabilities &&
2754 		    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2755 			     rdev->wiphy.extended_capabilities_len,
2756 			     rdev->wiphy.extended_capabilities) ||
2757 		     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2758 			     rdev->wiphy.extended_capabilities_len,
2759 			     rdev->wiphy.extended_capabilities_mask)))
2760 			goto nla_put_failure;
2761 
2762 		if (rdev->wiphy.vht_capa_mod_mask &&
2763 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2764 			    sizeof(*rdev->wiphy.vht_capa_mod_mask),
2765 			    rdev->wiphy.vht_capa_mod_mask))
2766 			goto nla_put_failure;
2767 
2768 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2769 			    rdev->wiphy.perm_addr))
2770 			goto nla_put_failure;
2771 
2772 		if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2773 		    nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2774 			    rdev->wiphy.addr_mask))
2775 			goto nla_put_failure;
2776 
2777 		if (rdev->wiphy.n_addresses > 1) {
2778 			void *attr;
2779 
2780 			attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2781 			if (!attr)
2782 				goto nla_put_failure;
2783 
2784 			for (i = 0; i < rdev->wiphy.n_addresses; i++)
2785 				if (nla_put(msg, i + 1, ETH_ALEN,
2786 					    rdev->wiphy.addresses[i].addr))
2787 					goto nla_put_failure;
2788 
2789 			nla_nest_end(msg, attr);
2790 		}
2791 
2792 		state->split_start++;
2793 		break;
2794 	case 10:
2795 		if (nl80211_send_coalesce(msg, rdev))
2796 			goto nla_put_failure;
2797 
2798 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2799 		    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2800 		     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2801 			goto nla_put_failure;
2802 
2803 		if (rdev->wiphy.max_ap_assoc_sta &&
2804 		    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2805 				rdev->wiphy.max_ap_assoc_sta))
2806 			goto nla_put_failure;
2807 
2808 		state->split_start++;
2809 		break;
2810 	case 11:
2811 		if (rdev->wiphy.n_vendor_commands) {
2812 			const struct nl80211_vendor_cmd_info *info;
2813 			struct nlattr *nested;
2814 
2815 			nested = nla_nest_start_noflag(msg,
2816 						       NL80211_ATTR_VENDOR_DATA);
2817 			if (!nested)
2818 				goto nla_put_failure;
2819 
2820 			for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2821 				info = &rdev->wiphy.vendor_commands[i].info;
2822 				if (nla_put(msg, i + 1, sizeof(*info), info))
2823 					goto nla_put_failure;
2824 			}
2825 			nla_nest_end(msg, nested);
2826 		}
2827 
2828 		if (rdev->wiphy.n_vendor_events) {
2829 			const struct nl80211_vendor_cmd_info *info;
2830 			struct nlattr *nested;
2831 
2832 			nested = nla_nest_start_noflag(msg,
2833 						       NL80211_ATTR_VENDOR_EVENTS);
2834 			if (!nested)
2835 				goto nla_put_failure;
2836 
2837 			for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2838 				info = &rdev->wiphy.vendor_events[i];
2839 				if (nla_put(msg, i + 1, sizeof(*info), info))
2840 					goto nla_put_failure;
2841 			}
2842 			nla_nest_end(msg, nested);
2843 		}
2844 		state->split_start++;
2845 		break;
2846 	case 12:
2847 		if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2848 		    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2849 			       rdev->wiphy.max_num_csa_counters))
2850 			goto nla_put_failure;
2851 
2852 		if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2853 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2854 			goto nla_put_failure;
2855 
2856 		if (rdev->wiphy.max_sched_scan_reqs &&
2857 		    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2858 				rdev->wiphy.max_sched_scan_reqs))
2859 			goto nla_put_failure;
2860 
2861 		if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2862 			    sizeof(rdev->wiphy.ext_features),
2863 			    rdev->wiphy.ext_features))
2864 			goto nla_put_failure;
2865 
2866 		if (rdev->wiphy.bss_select_support) {
2867 			struct nlattr *nested;
2868 			u32 bss_select_support = rdev->wiphy.bss_select_support;
2869 
2870 			nested = nla_nest_start_noflag(msg,
2871 						       NL80211_ATTR_BSS_SELECT);
2872 			if (!nested)
2873 				goto nla_put_failure;
2874 
2875 			i = 0;
2876 			while (bss_select_support) {
2877 				if ((bss_select_support & 1) &&
2878 				    nla_put_flag(msg, i))
2879 					goto nla_put_failure;
2880 				i++;
2881 				bss_select_support >>= 1;
2882 			}
2883 			nla_nest_end(msg, nested);
2884 		}
2885 
2886 		state->split_start++;
2887 		break;
2888 	case 13:
2889 		if (rdev->wiphy.num_iftype_ext_capab &&
2890 		    rdev->wiphy.iftype_ext_capab) {
2891 			struct nlattr *nested_ext_capab, *nested;
2892 
2893 			nested = nla_nest_start_noflag(msg,
2894 						       NL80211_ATTR_IFTYPE_EXT_CAPA);
2895 			if (!nested)
2896 				goto nla_put_failure;
2897 
2898 			for (i = state->capa_start;
2899 			     i < rdev->wiphy.num_iftype_ext_capab; i++) {
2900 				const struct wiphy_iftype_ext_capab *capab;
2901 
2902 				capab = &rdev->wiphy.iftype_ext_capab[i];
2903 
2904 				nested_ext_capab = nla_nest_start_noflag(msg,
2905 									 i);
2906 				if (!nested_ext_capab ||
2907 				    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2908 						capab->iftype) ||
2909 				    nla_put(msg, NL80211_ATTR_EXT_CAPA,
2910 					    capab->extended_capabilities_len,
2911 					    capab->extended_capabilities) ||
2912 				    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2913 					    capab->extended_capabilities_len,
2914 					    capab->extended_capabilities_mask))
2915 					goto nla_put_failure;
2916 
2917 				if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO &&
2918 				    (nla_put_u16(msg,
2919 						 NL80211_ATTR_EML_CAPABILITY,
2920 						 capab->eml_capabilities) ||
2921 				     nla_put_u16(msg,
2922 						 NL80211_ATTR_MLD_CAPA_AND_OPS,
2923 						 capab->mld_capa_and_ops)))
2924 					goto nla_put_failure;
2925 
2926 				nla_nest_end(msg, nested_ext_capab);
2927 				if (state->split)
2928 					break;
2929 			}
2930 			nla_nest_end(msg, nested);
2931 			if (i < rdev->wiphy.num_iftype_ext_capab) {
2932 				state->capa_start = i + 1;
2933 				break;
2934 			}
2935 		}
2936 
2937 		if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2938 				rdev->wiphy.nan_supported_bands))
2939 			goto nla_put_failure;
2940 
2941 		if (wiphy_ext_feature_isset(&rdev->wiphy,
2942 					    NL80211_EXT_FEATURE_TXQS)) {
2943 			struct cfg80211_txq_stats txqstats = {};
2944 			int res;
2945 
2946 			res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2947 			if (!res &&
2948 			    !nl80211_put_txq_stats(msg, &txqstats,
2949 						   NL80211_ATTR_TXQ_STATS))
2950 				goto nla_put_failure;
2951 
2952 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2953 					rdev->wiphy.txq_limit))
2954 				goto nla_put_failure;
2955 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2956 					rdev->wiphy.txq_memory_limit))
2957 				goto nla_put_failure;
2958 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2959 					rdev->wiphy.txq_quantum))
2960 				goto nla_put_failure;
2961 		}
2962 
2963 		state->split_start++;
2964 		break;
2965 	case 14:
2966 		if (nl80211_send_pmsr_capa(rdev, msg))
2967 			goto nla_put_failure;
2968 
2969 		state->split_start++;
2970 		break;
2971 	case 15:
2972 		if (rdev->wiphy.akm_suites &&
2973 		    nla_put(msg, NL80211_ATTR_AKM_SUITES,
2974 			    sizeof(u32) * rdev->wiphy.n_akm_suites,
2975 			    rdev->wiphy.akm_suites))
2976 			goto nla_put_failure;
2977 
2978 		if (nl80211_put_iftype_akm_suites(rdev, msg))
2979 			goto nla_put_failure;
2980 
2981 		if (nl80211_put_tid_config_support(rdev, msg))
2982 			goto nla_put_failure;
2983 		state->split_start++;
2984 		break;
2985 	case 16:
2986 		if (nl80211_put_sar_specs(rdev, msg))
2987 			goto nla_put_failure;
2988 
2989 		if (nl80211_put_mbssid_support(&rdev->wiphy, msg))
2990 			goto nla_put_failure;
2991 
2992 		if (nla_put_u16(msg, NL80211_ATTR_MAX_NUM_AKM_SUITES,
2993 				rdev->wiphy.max_num_akm_suites))
2994 			goto nla_put_failure;
2995 
2996 		if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO)
2997 			nla_put_flag(msg, NL80211_ATTR_MLO_SUPPORT);
2998 
2999 		if (rdev->wiphy.hw_timestamp_max_peers &&
3000 		    nla_put_u16(msg, NL80211_ATTR_MAX_HW_TIMESTAMP_PEERS,
3001 				rdev->wiphy.hw_timestamp_max_peers))
3002 			goto nla_put_failure;
3003 
3004 		/* done */
3005 		state->split_start = 0;
3006 		break;
3007 	}
3008  finish:
3009 	genlmsg_end(msg, hdr);
3010 	return 0;
3011 
3012  nla_put_failure:
3013 	genlmsg_cancel(msg, hdr);
3014 	return -EMSGSIZE;
3015 }
3016 
3017 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
3018 				    struct netlink_callback *cb,
3019 				    struct nl80211_dump_wiphy_state *state)
3020 {
3021 	struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
3022 	int ret;
3023 
3024 	if (!tb)
3025 		return -ENOMEM;
3026 
3027 	ret = nlmsg_parse_deprecated(cb->nlh,
3028 				     GENL_HDRLEN + nl80211_fam.hdrsize,
3029 				     tb, nl80211_fam.maxattr,
3030 				     nl80211_policy, NULL);
3031 	/* ignore parse errors for backward compatibility */
3032 	if (ret) {
3033 		ret = 0;
3034 		goto out;
3035 	}
3036 
3037 	state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
3038 	if (tb[NL80211_ATTR_WIPHY])
3039 		state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
3040 	if (tb[NL80211_ATTR_WDEV])
3041 		state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
3042 	if (tb[NL80211_ATTR_IFINDEX]) {
3043 		struct net_device *netdev;
3044 		struct cfg80211_registered_device *rdev;
3045 		int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3046 
3047 		netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
3048 		if (!netdev) {
3049 			ret = -ENODEV;
3050 			goto out;
3051 		}
3052 		if (netdev->ieee80211_ptr) {
3053 			rdev = wiphy_to_rdev(
3054 				netdev->ieee80211_ptr->wiphy);
3055 			state->filter_wiphy = rdev->wiphy_idx;
3056 		}
3057 	}
3058 
3059 	ret = 0;
3060 out:
3061 	kfree(tb);
3062 	return ret;
3063 }
3064 
3065 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
3066 {
3067 	int idx = 0, ret;
3068 	struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
3069 	struct cfg80211_registered_device *rdev;
3070 
3071 	rtnl_lock();
3072 	if (!state) {
3073 		state = kzalloc(sizeof(*state), GFP_KERNEL);
3074 		if (!state) {
3075 			rtnl_unlock();
3076 			return -ENOMEM;
3077 		}
3078 		state->filter_wiphy = -1;
3079 		ret = nl80211_dump_wiphy_parse(skb, cb, state);
3080 		if (ret) {
3081 			kfree(state);
3082 			rtnl_unlock();
3083 			return ret;
3084 		}
3085 		cb->args[0] = (long)state;
3086 	}
3087 
3088 	for_each_rdev(rdev) {
3089 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3090 			continue;
3091 		if (++idx <= state->start)
3092 			continue;
3093 		if (state->filter_wiphy != -1 &&
3094 		    state->filter_wiphy != rdev->wiphy_idx)
3095 			continue;
3096 		wiphy_lock(&rdev->wiphy);
3097 		/* attempt to fit multiple wiphy data chunks into the skb */
3098 		do {
3099 			ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
3100 						 skb,
3101 						 NETLINK_CB(cb->skb).portid,
3102 						 cb->nlh->nlmsg_seq,
3103 						 NLM_F_MULTI, state);
3104 			if (ret < 0) {
3105 				/*
3106 				 * If sending the wiphy data didn't fit (ENOBUFS
3107 				 * or EMSGSIZE returned), this SKB is still
3108 				 * empty (so it's not too big because another
3109 				 * wiphy dataset is already in the skb) and
3110 				 * we've not tried to adjust the dump allocation
3111 				 * yet ... then adjust the alloc size to be
3112 				 * bigger, and return 1 but with the empty skb.
3113 				 * This results in an empty message being RX'ed
3114 				 * in userspace, but that is ignored.
3115 				 *
3116 				 * We can then retry with the larger buffer.
3117 				 */
3118 				if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
3119 				    !skb->len && !state->split &&
3120 				    cb->min_dump_alloc < 4096) {
3121 					cb->min_dump_alloc = 4096;
3122 					state->split_start = 0;
3123 					wiphy_unlock(&rdev->wiphy);
3124 					rtnl_unlock();
3125 					return 1;
3126 				}
3127 				idx--;
3128 				break;
3129 			}
3130 		} while (state->split_start > 0);
3131 		wiphy_unlock(&rdev->wiphy);
3132 		break;
3133 	}
3134 	rtnl_unlock();
3135 
3136 	state->start = idx;
3137 
3138 	return skb->len;
3139 }
3140 
3141 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
3142 {
3143 	kfree((void *)cb->args[0]);
3144 	return 0;
3145 }
3146 
3147 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
3148 {
3149 	struct sk_buff *msg;
3150 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3151 	struct nl80211_dump_wiphy_state state = {};
3152 
3153 	msg = nlmsg_new(4096, GFP_KERNEL);
3154 	if (!msg)
3155 		return -ENOMEM;
3156 
3157 	if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
3158 			       info->snd_portid, info->snd_seq, 0,
3159 			       &state) < 0) {
3160 		nlmsg_free(msg);
3161 		return -ENOBUFS;
3162 	}
3163 
3164 	return genlmsg_reply(msg, info);
3165 }
3166 
3167 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
3168 	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
3169 	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
3170 	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
3171 	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
3172 	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
3173 };
3174 
3175 static int parse_txq_params(struct nlattr *tb[],
3176 			    struct ieee80211_txq_params *txq_params)
3177 {
3178 	u8 ac;
3179 
3180 	if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
3181 	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
3182 	    !tb[NL80211_TXQ_ATTR_AIFS])
3183 		return -EINVAL;
3184 
3185 	ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
3186 	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
3187 	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
3188 	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
3189 	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
3190 
3191 	if (ac >= NL80211_NUM_ACS)
3192 		return -EINVAL;
3193 	txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
3194 	return 0;
3195 }
3196 
3197 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
3198 {
3199 	/*
3200 	 * You can only set the channel explicitly for some interfaces,
3201 	 * most have their channel managed via their respective
3202 	 * "establish a connection" command (connect, join, ...)
3203 	 *
3204 	 * For AP/GO and mesh mode, the channel can be set with the
3205 	 * channel userspace API, but is only stored and passed to the
3206 	 * low-level driver when the AP starts or the mesh is joined.
3207 	 * This is for backward compatibility, userspace can also give
3208 	 * the channel in the start-ap or join-mesh commands instead.
3209 	 *
3210 	 * Monitors are special as they are normally slaved to
3211 	 * whatever else is going on, so they have their own special
3212 	 * operation to set the monitor channel if possible.
3213 	 */
3214 	return !wdev ||
3215 		wdev->iftype == NL80211_IFTYPE_AP ||
3216 		wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
3217 		wdev->iftype == NL80211_IFTYPE_MONITOR ||
3218 		wdev->iftype == NL80211_IFTYPE_P2P_GO;
3219 }
3220 
3221 static int _nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
3222 				  struct genl_info *info, bool monitor,
3223 				  struct cfg80211_chan_def *chandef)
3224 {
3225 	struct netlink_ext_ack *extack = info->extack;
3226 	struct nlattr **attrs = info->attrs;
3227 	u32 control_freq;
3228 
3229 	if (!attrs[NL80211_ATTR_WIPHY_FREQ]) {
3230 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
3231 				    "Frequency is missing");
3232 		return -EINVAL;
3233 	}
3234 
3235 	control_freq = MHZ_TO_KHZ(
3236 			nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3237 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
3238 		control_freq +=
3239 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
3240 
3241 	memset(chandef, 0, sizeof(*chandef));
3242 	chandef->chan = ieee80211_get_channel_khz(&rdev->wiphy, control_freq);
3243 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
3244 	chandef->center_freq1 = KHZ_TO_MHZ(control_freq);
3245 	chandef->freq1_offset = control_freq % 1000;
3246 	chandef->center_freq2 = 0;
3247 
3248 	if (!chandef->chan) {
3249 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
3250 				    "Unknown channel");
3251 		return -EINVAL;
3252 	}
3253 
3254 	if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
3255 		enum nl80211_channel_type chantype;
3256 
3257 		chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
3258 
3259 		switch (chantype) {
3260 		case NL80211_CHAN_NO_HT:
3261 		case NL80211_CHAN_HT20:
3262 		case NL80211_CHAN_HT40PLUS:
3263 		case NL80211_CHAN_HT40MINUS:
3264 			cfg80211_chandef_create(chandef, chandef->chan,
3265 						chantype);
3266 			/* user input for center_freq is incorrect */
3267 			if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
3268 			    chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
3269 				NL_SET_ERR_MSG_ATTR(extack,
3270 						    attrs[NL80211_ATTR_CENTER_FREQ1],
3271 						    "bad center frequency 1");
3272 				return -EINVAL;
3273 			}
3274 			/* center_freq2 must be zero */
3275 			if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
3276 			    nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
3277 				NL_SET_ERR_MSG_ATTR(extack,
3278 						    attrs[NL80211_ATTR_CENTER_FREQ2],
3279 						    "center frequency 2 can't be used");
3280 				return -EINVAL;
3281 			}
3282 			break;
3283 		default:
3284 			NL_SET_ERR_MSG_ATTR(extack,
3285 					    attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3286 					    "invalid channel type");
3287 			return -EINVAL;
3288 		}
3289 	} else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
3290 		chandef->width =
3291 			nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
3292 		if (chandef->chan->band == NL80211_BAND_S1GHZ) {
3293 			/* User input error for channel width doesn't match channel  */
3294 			if (chandef->width != ieee80211_s1g_channel_width(chandef->chan)) {
3295 				NL_SET_ERR_MSG_ATTR(extack,
3296 						    attrs[NL80211_ATTR_CHANNEL_WIDTH],
3297 						    "bad channel width");
3298 				return -EINVAL;
3299 			}
3300 		}
3301 		if (attrs[NL80211_ATTR_CENTER_FREQ1]) {
3302 			chandef->center_freq1 =
3303 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
3304 			if (attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET])
3305 				chandef->freq1_offset = nla_get_u32(
3306 				      attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET]);
3307 			else
3308 				chandef->freq1_offset = 0;
3309 		}
3310 		if (attrs[NL80211_ATTR_CENTER_FREQ2])
3311 			chandef->center_freq2 =
3312 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
3313 	}
3314 
3315 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
3316 		chandef->edmg.channels =
3317 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
3318 
3319 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
3320 			chandef->edmg.bw_config =
3321 		     nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
3322 	} else {
3323 		chandef->edmg.bw_config = 0;
3324 		chandef->edmg.channels = 0;
3325 	}
3326 
3327 	if (info->attrs[NL80211_ATTR_PUNCT_BITMAP]) {
3328 		chandef->punctured =
3329 			nla_get_u32(info->attrs[NL80211_ATTR_PUNCT_BITMAP]);
3330 
3331 		if (chandef->punctured &&
3332 		    !wiphy_ext_feature_isset(&rdev->wiphy,
3333 					     NL80211_EXT_FEATURE_PUNCT)) {
3334 			NL_SET_ERR_MSG(extack,
3335 				       "driver doesn't support puncturing");
3336 			return -EINVAL;
3337 		}
3338 	}
3339 
3340 	if (!cfg80211_chandef_valid(chandef)) {
3341 		NL_SET_ERR_MSG(extack, "invalid channel definition");
3342 		return -EINVAL;
3343 	}
3344 
3345 	if (!_cfg80211_chandef_usable(&rdev->wiphy, chandef,
3346 				      IEEE80211_CHAN_DISABLED,
3347 				      monitor)) {
3348 		NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
3349 		return -EINVAL;
3350 	}
3351 
3352 	if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
3353 	     chandef->width == NL80211_CHAN_WIDTH_10) &&
3354 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
3355 		NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
3356 		return -EINVAL;
3357 	}
3358 
3359 	return 0;
3360 }
3361 
3362 int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
3363 			  struct genl_info *info,
3364 			  struct cfg80211_chan_def *chandef)
3365 {
3366 	return _nl80211_parse_chandef(rdev, info, false, chandef);
3367 }
3368 
3369 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
3370 				 struct net_device *dev,
3371 				 struct genl_info *info,
3372 				 int _link_id)
3373 {
3374 	struct cfg80211_chan_def chandef;
3375 	int result;
3376 	enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
3377 	struct wireless_dev *wdev = NULL;
3378 	int link_id = _link_id;
3379 
3380 	if (dev)
3381 		wdev = dev->ieee80211_ptr;
3382 	if (!nl80211_can_set_dev_channel(wdev))
3383 		return -EOPNOTSUPP;
3384 	if (wdev)
3385 		iftype = wdev->iftype;
3386 
3387 	if (link_id < 0) {
3388 		if (wdev && wdev->valid_links)
3389 			return -EINVAL;
3390 		link_id = 0;
3391 	}
3392 
3393 	result = _nl80211_parse_chandef(rdev, info,
3394 					iftype == NL80211_IFTYPE_MONITOR,
3395 					&chandef);
3396 	if (result)
3397 		return result;
3398 
3399 	switch (iftype) {
3400 	case NL80211_IFTYPE_AP:
3401 	case NL80211_IFTYPE_P2P_GO:
3402 		if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
3403 						   iftype))
3404 			return -EINVAL;
3405 		if (wdev->links[link_id].ap.beacon_interval) {
3406 			struct ieee80211_channel *cur_chan;
3407 
3408 			if (!dev || !rdev->ops->set_ap_chanwidth ||
3409 			    !(rdev->wiphy.features &
3410 			      NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE))
3411 				return -EBUSY;
3412 
3413 			/* Only allow dynamic channel width changes */
3414 			cur_chan = wdev->links[link_id].ap.chandef.chan;
3415 			if (chandef.chan != cur_chan)
3416 				return -EBUSY;
3417 
3418 			result = rdev_set_ap_chanwidth(rdev, dev, link_id,
3419 						       &chandef);
3420 			if (result)
3421 				return result;
3422 			wdev->links[link_id].ap.chandef = chandef;
3423 		} else {
3424 			wdev->u.ap.preset_chandef = chandef;
3425 		}
3426 		return 0;
3427 	case NL80211_IFTYPE_MESH_POINT:
3428 		return cfg80211_set_mesh_channel(rdev, wdev, &chandef);
3429 	case NL80211_IFTYPE_MONITOR:
3430 		return cfg80211_set_monitor_channel(rdev, &chandef);
3431 	default:
3432 		break;
3433 	}
3434 
3435 	return -EINVAL;
3436 }
3437 
3438 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
3439 {
3440 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3441 	int link_id = nl80211_link_id_or_invalid(info->attrs);
3442 	struct net_device *netdev = info->user_ptr[1];
3443 
3444 	return __nl80211_set_channel(rdev, netdev, info, link_id);
3445 }
3446 
3447 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
3448 {
3449 	struct cfg80211_registered_device *rdev = NULL;
3450 	struct net_device *netdev = NULL;
3451 	struct wireless_dev *wdev;
3452 	int result = 0, rem_txq_params = 0;
3453 	struct nlattr *nl_txq_params;
3454 	u32 changed;
3455 	u8 retry_short = 0, retry_long = 0;
3456 	u32 frag_threshold = 0, rts_threshold = 0;
3457 	u8 coverage_class = 0;
3458 	u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
3459 
3460 	rtnl_lock();
3461 	/*
3462 	 * Try to find the wiphy and netdev. Normally this
3463 	 * function shouldn't need the netdev, but this is
3464 	 * done for backward compatibility -- previously
3465 	 * setting the channel was done per wiphy, but now
3466 	 * it is per netdev. Previous userland like hostapd
3467 	 * also passed a netdev to set_wiphy, so that it is
3468 	 * possible to let that go to the right netdev!
3469 	 */
3470 
3471 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
3472 		int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
3473 
3474 		netdev = __dev_get_by_index(genl_info_net(info), ifindex);
3475 		if (netdev && netdev->ieee80211_ptr)
3476 			rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
3477 		else
3478 			netdev = NULL;
3479 	}
3480 
3481 	if (!netdev) {
3482 		rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
3483 						  info->attrs);
3484 		if (IS_ERR(rdev)) {
3485 			rtnl_unlock();
3486 			return PTR_ERR(rdev);
3487 		}
3488 		wdev = NULL;
3489 		netdev = NULL;
3490 		result = 0;
3491 	} else
3492 		wdev = netdev->ieee80211_ptr;
3493 
3494 	wiphy_lock(&rdev->wiphy);
3495 
3496 	/*
3497 	 * end workaround code, by now the rdev is available
3498 	 * and locked, and wdev may or may not be NULL.
3499 	 */
3500 
3501 	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
3502 		result = cfg80211_dev_rename(
3503 			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
3504 	rtnl_unlock();
3505 
3506 	if (result)
3507 		goto out;
3508 
3509 	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
3510 		struct ieee80211_txq_params txq_params;
3511 		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
3512 
3513 		if (!rdev->ops->set_txq_params) {
3514 			result = -EOPNOTSUPP;
3515 			goto out;
3516 		}
3517 
3518 		if (!netdev) {
3519 			result = -EINVAL;
3520 			goto out;
3521 		}
3522 
3523 		if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3524 		    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3525 			result = -EINVAL;
3526 			goto out;
3527 		}
3528 
3529 		if (!netif_running(netdev)) {
3530 			result = -ENETDOWN;
3531 			goto out;
3532 		}
3533 
3534 		nla_for_each_nested(nl_txq_params,
3535 				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
3536 				    rem_txq_params) {
3537 			result = nla_parse_nested_deprecated(tb,
3538 							     NL80211_TXQ_ATTR_MAX,
3539 							     nl_txq_params,
3540 							     txq_params_policy,
3541 							     info->extack);
3542 			if (result)
3543 				goto out;
3544 			result = parse_txq_params(tb, &txq_params);
3545 			if (result)
3546 				goto out;
3547 
3548 			txq_params.link_id =
3549 				nl80211_link_id_or_invalid(info->attrs);
3550 
3551 			if (txq_params.link_id >= 0 &&
3552 			    !(netdev->ieee80211_ptr->valid_links &
3553 			      BIT(txq_params.link_id)))
3554 				result = -ENOLINK;
3555 			else if (txq_params.link_id >= 0 &&
3556 				 !netdev->ieee80211_ptr->valid_links)
3557 				result = -EINVAL;
3558 			else
3559 				result = rdev_set_txq_params(rdev, netdev,
3560 							     &txq_params);
3561 			if (result)
3562 				goto out;
3563 		}
3564 	}
3565 
3566 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3567 		int link_id = nl80211_link_id_or_invalid(info->attrs);
3568 
3569 		if (wdev) {
3570 			result = __nl80211_set_channel(
3571 				rdev,
3572 				nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
3573 				info, link_id);
3574 		} else {
3575 			result = __nl80211_set_channel(rdev, netdev, info, link_id);
3576 		}
3577 
3578 		if (result)
3579 			goto out;
3580 	}
3581 
3582 	if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
3583 		struct wireless_dev *txp_wdev = wdev;
3584 		enum nl80211_tx_power_setting type;
3585 		int idx, mbm = 0;
3586 
3587 		if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
3588 			txp_wdev = NULL;
3589 
3590 		if (!rdev->ops->set_tx_power) {
3591 			result = -EOPNOTSUPP;
3592 			goto out;
3593 		}
3594 
3595 		idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
3596 		type = nla_get_u32(info->attrs[idx]);
3597 
3598 		if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
3599 		    (type != NL80211_TX_POWER_AUTOMATIC)) {
3600 			result = -EINVAL;
3601 			goto out;
3602 		}
3603 
3604 		if (type != NL80211_TX_POWER_AUTOMATIC) {
3605 			idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
3606 			mbm = nla_get_u32(info->attrs[idx]);
3607 		}
3608 
3609 		result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
3610 		if (result)
3611 			goto out;
3612 	}
3613 
3614 	if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
3615 	    info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
3616 		u32 tx_ant, rx_ant;
3617 
3618 		if ((!rdev->wiphy.available_antennas_tx &&
3619 		     !rdev->wiphy.available_antennas_rx) ||
3620 		    !rdev->ops->set_antenna) {
3621 			result = -EOPNOTSUPP;
3622 			goto out;
3623 		}
3624 
3625 		tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
3626 		rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
3627 
3628 		/* reject antenna configurations which don't match the
3629 		 * available antenna masks, except for the "all" mask */
3630 		if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
3631 		    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
3632 			result = -EINVAL;
3633 			goto out;
3634 		}
3635 
3636 		tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
3637 		rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
3638 
3639 		result = rdev_set_antenna(rdev, tx_ant, rx_ant);
3640 		if (result)
3641 			goto out;
3642 	}
3643 
3644 	changed = 0;
3645 
3646 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
3647 		retry_short = nla_get_u8(
3648 			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
3649 
3650 		changed |= WIPHY_PARAM_RETRY_SHORT;
3651 	}
3652 
3653 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
3654 		retry_long = nla_get_u8(
3655 			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
3656 
3657 		changed |= WIPHY_PARAM_RETRY_LONG;
3658 	}
3659 
3660 	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
3661 		frag_threshold = nla_get_u32(
3662 			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
3663 		if (frag_threshold < 256) {
3664 			result = -EINVAL;
3665 			goto out;
3666 		}
3667 
3668 		if (frag_threshold != (u32) -1) {
3669 			/*
3670 			 * Fragments (apart from the last one) are required to
3671 			 * have even length. Make the fragmentation code
3672 			 * simpler by stripping LSB should someone try to use
3673 			 * odd threshold value.
3674 			 */
3675 			frag_threshold &= ~0x1;
3676 		}
3677 		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
3678 	}
3679 
3680 	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
3681 		rts_threshold = nla_get_u32(
3682 			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
3683 		changed |= WIPHY_PARAM_RTS_THRESHOLD;
3684 	}
3685 
3686 	if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
3687 		if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3688 			result = -EINVAL;
3689 			goto out;
3690 		}
3691 
3692 		coverage_class = nla_get_u8(
3693 			info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
3694 		changed |= WIPHY_PARAM_COVERAGE_CLASS;
3695 	}
3696 
3697 	if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3698 		if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION)) {
3699 			result = -EOPNOTSUPP;
3700 			goto out;
3701 		}
3702 
3703 		changed |= WIPHY_PARAM_DYN_ACK;
3704 	}
3705 
3706 	if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3707 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3708 					     NL80211_EXT_FEATURE_TXQS)) {
3709 			result = -EOPNOTSUPP;
3710 			goto out;
3711 		}
3712 		txq_limit = nla_get_u32(
3713 			info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3714 		changed |= WIPHY_PARAM_TXQ_LIMIT;
3715 	}
3716 
3717 	if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3718 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3719 					     NL80211_EXT_FEATURE_TXQS)) {
3720 			result = -EOPNOTSUPP;
3721 			goto out;
3722 		}
3723 		txq_memory_limit = nla_get_u32(
3724 			info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3725 		changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3726 	}
3727 
3728 	if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3729 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3730 					     NL80211_EXT_FEATURE_TXQS)) {
3731 			result = -EOPNOTSUPP;
3732 			goto out;
3733 		}
3734 		txq_quantum = nla_get_u32(
3735 			info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3736 		changed |= WIPHY_PARAM_TXQ_QUANTUM;
3737 	}
3738 
3739 	if (changed) {
3740 		u8 old_retry_short, old_retry_long;
3741 		u32 old_frag_threshold, old_rts_threshold;
3742 		u8 old_coverage_class;
3743 		u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3744 
3745 		if (!rdev->ops->set_wiphy_params) {
3746 			result = -EOPNOTSUPP;
3747 			goto out;
3748 		}
3749 
3750 		old_retry_short = rdev->wiphy.retry_short;
3751 		old_retry_long = rdev->wiphy.retry_long;
3752 		old_frag_threshold = rdev->wiphy.frag_threshold;
3753 		old_rts_threshold = rdev->wiphy.rts_threshold;
3754 		old_coverage_class = rdev->wiphy.coverage_class;
3755 		old_txq_limit = rdev->wiphy.txq_limit;
3756 		old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3757 		old_txq_quantum = rdev->wiphy.txq_quantum;
3758 
3759 		if (changed & WIPHY_PARAM_RETRY_SHORT)
3760 			rdev->wiphy.retry_short = retry_short;
3761 		if (changed & WIPHY_PARAM_RETRY_LONG)
3762 			rdev->wiphy.retry_long = retry_long;
3763 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3764 			rdev->wiphy.frag_threshold = frag_threshold;
3765 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3766 			rdev->wiphy.rts_threshold = rts_threshold;
3767 		if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3768 			rdev->wiphy.coverage_class = coverage_class;
3769 		if (changed & WIPHY_PARAM_TXQ_LIMIT)
3770 			rdev->wiphy.txq_limit = txq_limit;
3771 		if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3772 			rdev->wiphy.txq_memory_limit = txq_memory_limit;
3773 		if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3774 			rdev->wiphy.txq_quantum = txq_quantum;
3775 
3776 		result = rdev_set_wiphy_params(rdev, changed);
3777 		if (result) {
3778 			rdev->wiphy.retry_short = old_retry_short;
3779 			rdev->wiphy.retry_long = old_retry_long;
3780 			rdev->wiphy.frag_threshold = old_frag_threshold;
3781 			rdev->wiphy.rts_threshold = old_rts_threshold;
3782 			rdev->wiphy.coverage_class = old_coverage_class;
3783 			rdev->wiphy.txq_limit = old_txq_limit;
3784 			rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3785 			rdev->wiphy.txq_quantum = old_txq_quantum;
3786 			goto out;
3787 		}
3788 	}
3789 
3790 	result = 0;
3791 
3792 out:
3793 	wiphy_unlock(&rdev->wiphy);
3794 	return result;
3795 }
3796 
3797 int nl80211_send_chandef(struct sk_buff *msg, const struct cfg80211_chan_def *chandef)
3798 {
3799 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3800 		return -EINVAL;
3801 
3802 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3803 			chandef->chan->center_freq))
3804 		return -ENOBUFS;
3805 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
3806 			chandef->chan->freq_offset))
3807 		return -ENOBUFS;
3808 	switch (chandef->width) {
3809 	case NL80211_CHAN_WIDTH_20_NOHT:
3810 	case NL80211_CHAN_WIDTH_20:
3811 	case NL80211_CHAN_WIDTH_40:
3812 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3813 				cfg80211_get_chandef_type(chandef)))
3814 			return -ENOBUFS;
3815 		break;
3816 	default:
3817 		break;
3818 	}
3819 	if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3820 		return -ENOBUFS;
3821 	if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3822 		return -ENOBUFS;
3823 	if (chandef->center_freq2 &&
3824 	    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3825 		return -ENOBUFS;
3826 	if (chandef->punctured &&
3827 	    nla_put_u32(msg, NL80211_ATTR_PUNCT_BITMAP, chandef->punctured))
3828 		return -ENOBUFS;
3829 
3830 	return 0;
3831 }
3832 EXPORT_SYMBOL(nl80211_send_chandef);
3833 
3834 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3835 			      struct cfg80211_registered_device *rdev,
3836 			      struct wireless_dev *wdev,
3837 			      enum nl80211_commands cmd)
3838 {
3839 	struct net_device *dev = wdev->netdev;
3840 	void *hdr;
3841 
3842 	lockdep_assert_wiphy(&rdev->wiphy);
3843 
3844 	WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3845 		cmd != NL80211_CMD_DEL_INTERFACE &&
3846 		cmd != NL80211_CMD_SET_INTERFACE);
3847 
3848 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3849 	if (!hdr)
3850 		return -1;
3851 
3852 	if (dev &&
3853 	    (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3854 	     nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3855 		goto nla_put_failure;
3856 
3857 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3858 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3859 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3860 			      NL80211_ATTR_PAD) ||
3861 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3862 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
3863 			rdev->devlist_generation ^
3864 			(cfg80211_rdev_list_generation << 2)) ||
3865 	    nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3866 		goto nla_put_failure;
3867 
3868 	if (rdev->ops->get_channel && !wdev->valid_links) {
3869 		struct cfg80211_chan_def chandef = {};
3870 		int ret;
3871 
3872 		ret = rdev_get_channel(rdev, wdev, 0, &chandef);
3873 		if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3874 			goto nla_put_failure;
3875 	}
3876 
3877 	if (rdev->ops->get_tx_power) {
3878 		int dbm, ret;
3879 
3880 		ret = rdev_get_tx_power(rdev, wdev, &dbm);
3881 		if (ret == 0 &&
3882 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3883 				DBM_TO_MBM(dbm)))
3884 			goto nla_put_failure;
3885 	}
3886 
3887 	switch (wdev->iftype) {
3888 	case NL80211_IFTYPE_AP:
3889 	case NL80211_IFTYPE_P2P_GO:
3890 		if (wdev->u.ap.ssid_len &&
3891 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.ap.ssid_len,
3892 			    wdev->u.ap.ssid))
3893 			goto nla_put_failure;
3894 		break;
3895 	case NL80211_IFTYPE_STATION:
3896 	case NL80211_IFTYPE_P2P_CLIENT:
3897 		if (wdev->u.client.ssid_len &&
3898 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.client.ssid_len,
3899 			    wdev->u.client.ssid))
3900 			goto nla_put_failure;
3901 		break;
3902 	case NL80211_IFTYPE_ADHOC:
3903 		if (wdev->u.ibss.ssid_len &&
3904 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.ibss.ssid_len,
3905 			    wdev->u.ibss.ssid))
3906 			goto nla_put_failure;
3907 		break;
3908 	default:
3909 		/* nothing */
3910 		break;
3911 	}
3912 
3913 	if (rdev->ops->get_txq_stats) {
3914 		struct cfg80211_txq_stats txqstats = {};
3915 		int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3916 
3917 		if (ret == 0 &&
3918 		    !nl80211_put_txq_stats(msg, &txqstats,
3919 					   NL80211_ATTR_TXQ_STATS))
3920 			goto nla_put_failure;
3921 	}
3922 
3923 	if (wdev->valid_links) {
3924 		unsigned int link_id;
3925 		struct nlattr *links = nla_nest_start(msg,
3926 						      NL80211_ATTR_MLO_LINKS);
3927 
3928 		if (!links)
3929 			goto nla_put_failure;
3930 
3931 		for_each_valid_link(wdev, link_id) {
3932 			struct nlattr *link = nla_nest_start(msg, link_id + 1);
3933 			struct cfg80211_chan_def chandef = {};
3934 			int ret;
3935 
3936 			if (!link)
3937 				goto nla_put_failure;
3938 
3939 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
3940 				goto nla_put_failure;
3941 			if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
3942 				    wdev->links[link_id].addr))
3943 				goto nla_put_failure;
3944 
3945 			ret = rdev_get_channel(rdev, wdev, link_id, &chandef);
3946 			if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3947 				goto nla_put_failure;
3948 
3949 			nla_nest_end(msg, link);
3950 		}
3951 
3952 		nla_nest_end(msg, links);
3953 	}
3954 
3955 	genlmsg_end(msg, hdr);
3956 	return 0;
3957 
3958  nla_put_failure:
3959 	genlmsg_cancel(msg, hdr);
3960 	return -EMSGSIZE;
3961 }
3962 
3963 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3964 {
3965 	int wp_idx = 0;
3966 	int if_idx = 0;
3967 	int wp_start = cb->args[0];
3968 	int if_start = cb->args[1];
3969 	int filter_wiphy = -1;
3970 	struct cfg80211_registered_device *rdev;
3971 	struct wireless_dev *wdev;
3972 	int ret;
3973 
3974 	rtnl_lock();
3975 	if (!cb->args[2]) {
3976 		struct nl80211_dump_wiphy_state state = {
3977 			.filter_wiphy = -1,
3978 		};
3979 
3980 		ret = nl80211_dump_wiphy_parse(skb, cb, &state);
3981 		if (ret)
3982 			goto out_unlock;
3983 
3984 		filter_wiphy = state.filter_wiphy;
3985 
3986 		/*
3987 		 * if filtering, set cb->args[2] to +1 since 0 is the default
3988 		 * value needed to determine that parsing is necessary.
3989 		 */
3990 		if (filter_wiphy >= 0)
3991 			cb->args[2] = filter_wiphy + 1;
3992 		else
3993 			cb->args[2] = -1;
3994 	} else if (cb->args[2] > 0) {
3995 		filter_wiphy = cb->args[2] - 1;
3996 	}
3997 
3998 	for_each_rdev(rdev) {
3999 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
4000 			continue;
4001 		if (wp_idx < wp_start) {
4002 			wp_idx++;
4003 			continue;
4004 		}
4005 
4006 		if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
4007 			continue;
4008 
4009 		if_idx = 0;
4010 
4011 		wiphy_lock(&rdev->wiphy);
4012 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
4013 			if (if_idx < if_start) {
4014 				if_idx++;
4015 				continue;
4016 			}
4017 			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
4018 					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
4019 					       rdev, wdev,
4020 					       NL80211_CMD_NEW_INTERFACE) < 0) {
4021 				wiphy_unlock(&rdev->wiphy);
4022 				goto out;
4023 			}
4024 			if_idx++;
4025 		}
4026 		wiphy_unlock(&rdev->wiphy);
4027 
4028 		if_start = 0;
4029 		wp_idx++;
4030 	}
4031  out:
4032 	cb->args[0] = wp_idx;
4033 	cb->args[1] = if_idx;
4034 
4035 	ret = skb->len;
4036  out_unlock:
4037 	rtnl_unlock();
4038 
4039 	return ret;
4040 }
4041 
4042 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
4043 {
4044 	struct sk_buff *msg;
4045 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4046 	struct wireless_dev *wdev = info->user_ptr[1];
4047 
4048 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4049 	if (!msg)
4050 		return -ENOMEM;
4051 
4052 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
4053 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
4054 		nlmsg_free(msg);
4055 		return -ENOBUFS;
4056 	}
4057 
4058 	return genlmsg_reply(msg, info);
4059 }
4060 
4061 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
4062 	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
4063 	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
4064 	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
4065 	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
4066 	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
4067 	[NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
4068 };
4069 
4070 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
4071 {
4072 	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
4073 	int flag;
4074 
4075 	*mntrflags = 0;
4076 
4077 	if (!nla)
4078 		return -EINVAL;
4079 
4080 	if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
4081 		return -EINVAL;
4082 
4083 	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
4084 		if (flags[flag])
4085 			*mntrflags |= (1<<flag);
4086 
4087 	*mntrflags |= MONITOR_FLAG_CHANGED;
4088 
4089 	return 0;
4090 }
4091 
4092 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
4093 				     enum nl80211_iftype type,
4094 				     struct genl_info *info,
4095 				     struct vif_params *params)
4096 {
4097 	bool change = false;
4098 	int err;
4099 
4100 	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
4101 		if (type != NL80211_IFTYPE_MONITOR)
4102 			return -EINVAL;
4103 
4104 		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
4105 					  &params->flags);
4106 		if (err)
4107 			return err;
4108 
4109 		change = true;
4110 	}
4111 
4112 	if (params->flags & MONITOR_FLAG_ACTIVE &&
4113 	    !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
4114 		return -EOPNOTSUPP;
4115 
4116 	if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
4117 		const u8 *mumimo_groups;
4118 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4119 
4120 		if (type != NL80211_IFTYPE_MONITOR)
4121 			return -EINVAL;
4122 
4123 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4124 			return -EOPNOTSUPP;
4125 
4126 		mumimo_groups =
4127 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
4128 
4129 		/* bits 0 and 63 are reserved and must be zero */
4130 		if ((mumimo_groups[0] & BIT(0)) ||
4131 		    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
4132 			return -EINVAL;
4133 
4134 		params->vht_mumimo_groups = mumimo_groups;
4135 		change = true;
4136 	}
4137 
4138 	if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
4139 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4140 
4141 		if (type != NL80211_IFTYPE_MONITOR)
4142 			return -EINVAL;
4143 
4144 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4145 			return -EOPNOTSUPP;
4146 
4147 		params->vht_mumimo_follow_addr =
4148 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
4149 		change = true;
4150 	}
4151 
4152 	return change ? 1 : 0;
4153 }
4154 
4155 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
4156 			       struct net_device *netdev, u8 use_4addr,
4157 			       enum nl80211_iftype iftype)
4158 {
4159 	if (!use_4addr) {
4160 		if (netdev && netif_is_bridge_port(netdev))
4161 			return -EBUSY;
4162 		return 0;
4163 	}
4164 
4165 	switch (iftype) {
4166 	case NL80211_IFTYPE_AP_VLAN:
4167 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
4168 			return 0;
4169 		break;
4170 	case NL80211_IFTYPE_STATION:
4171 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
4172 			return 0;
4173 		break;
4174 	default:
4175 		break;
4176 	}
4177 
4178 	return -EOPNOTSUPP;
4179 }
4180 
4181 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
4182 {
4183 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4184 	struct vif_params params;
4185 	int err;
4186 	enum nl80211_iftype otype, ntype;
4187 	struct net_device *dev = info->user_ptr[1];
4188 	bool change = false;
4189 
4190 	memset(&params, 0, sizeof(params));
4191 
4192 	otype = ntype = dev->ieee80211_ptr->iftype;
4193 
4194 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
4195 		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4196 		if (otype != ntype)
4197 			change = true;
4198 	}
4199 
4200 	if (info->attrs[NL80211_ATTR_MESH_ID]) {
4201 		struct wireless_dev *wdev = dev->ieee80211_ptr;
4202 
4203 		if (ntype != NL80211_IFTYPE_MESH_POINT)
4204 			return -EINVAL;
4205 		if (otype != NL80211_IFTYPE_MESH_POINT)
4206 			return -EINVAL;
4207 		if (netif_running(dev))
4208 			return -EBUSY;
4209 
4210 		wdev->u.mesh.id_up_len =
4211 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4212 		memcpy(wdev->u.mesh.id,
4213 		       nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4214 		       wdev->u.mesh.id_up_len);
4215 	}
4216 
4217 	if (info->attrs[NL80211_ATTR_4ADDR]) {
4218 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4219 		change = true;
4220 		err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
4221 		if (err)
4222 			return err;
4223 	} else {
4224 		params.use_4addr = -1;
4225 	}
4226 
4227 	err = nl80211_parse_mon_options(rdev, ntype, info, &params);
4228 	if (err < 0)
4229 		return err;
4230 	if (err > 0)
4231 		change = true;
4232 
4233 	if (change)
4234 		err = cfg80211_change_iface(rdev, dev, ntype, &params);
4235 	else
4236 		err = 0;
4237 
4238 	if (!err && params.use_4addr != -1)
4239 		dev->ieee80211_ptr->use_4addr = params.use_4addr;
4240 
4241 	if (change && !err) {
4242 		struct wireless_dev *wdev = dev->ieee80211_ptr;
4243 
4244 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
4245 	}
4246 
4247 	return err;
4248 }
4249 
4250 static int _nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4251 {
4252 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4253 	struct vif_params params;
4254 	struct wireless_dev *wdev;
4255 	struct sk_buff *msg;
4256 	int err;
4257 	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
4258 
4259 	memset(&params, 0, sizeof(params));
4260 
4261 	if (!info->attrs[NL80211_ATTR_IFNAME])
4262 		return -EINVAL;
4263 
4264 	if (info->attrs[NL80211_ATTR_IFTYPE])
4265 		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4266 
4267 	if (!rdev->ops->add_virtual_intf)
4268 		return -EOPNOTSUPP;
4269 
4270 	if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
4271 	     rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
4272 	    info->attrs[NL80211_ATTR_MAC]) {
4273 		nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
4274 			   ETH_ALEN);
4275 		if (!is_valid_ether_addr(params.macaddr))
4276 			return -EADDRNOTAVAIL;
4277 	}
4278 
4279 	if (info->attrs[NL80211_ATTR_4ADDR]) {
4280 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4281 		err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
4282 		if (err)
4283 			return err;
4284 	}
4285 
4286 	if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
4287 		return -EOPNOTSUPP;
4288 
4289 	err = nl80211_parse_mon_options(rdev, type, info, &params);
4290 	if (err < 0)
4291 		return err;
4292 
4293 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4294 	if (!msg)
4295 		return -ENOMEM;
4296 
4297 	wdev = rdev_add_virtual_intf(rdev,
4298 				nla_data(info->attrs[NL80211_ATTR_IFNAME]),
4299 				NET_NAME_USER, type, &params);
4300 	if (WARN_ON(!wdev)) {
4301 		nlmsg_free(msg);
4302 		return -EPROTO;
4303 	} else if (IS_ERR(wdev)) {
4304 		nlmsg_free(msg);
4305 		return PTR_ERR(wdev);
4306 	}
4307 
4308 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
4309 		wdev->owner_nlportid = info->snd_portid;
4310 
4311 	switch (type) {
4312 	case NL80211_IFTYPE_MESH_POINT:
4313 		if (!info->attrs[NL80211_ATTR_MESH_ID])
4314 			break;
4315 		wdev->u.mesh.id_up_len =
4316 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4317 		memcpy(wdev->u.mesh.id,
4318 		       nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4319 		       wdev->u.mesh.id_up_len);
4320 		break;
4321 	case NL80211_IFTYPE_NAN:
4322 	case NL80211_IFTYPE_P2P_DEVICE:
4323 		/*
4324 		 * P2P Device and NAN do not have a netdev, so don't go
4325 		 * through the netdev notifier and must be added here
4326 		 */
4327 		cfg80211_init_wdev(wdev);
4328 		cfg80211_register_wdev(rdev, wdev);
4329 		break;
4330 	default:
4331 		break;
4332 	}
4333 
4334 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
4335 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
4336 		nlmsg_free(msg);
4337 		return -ENOBUFS;
4338 	}
4339 
4340 	return genlmsg_reply(msg, info);
4341 }
4342 
4343 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4344 {
4345 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4346 	int ret;
4347 
4348 	/* to avoid failing a new interface creation due to pending removal */
4349 	cfg80211_destroy_ifaces(rdev);
4350 
4351 	wiphy_lock(&rdev->wiphy);
4352 	ret = _nl80211_new_interface(skb, info);
4353 	wiphy_unlock(&rdev->wiphy);
4354 
4355 	return ret;
4356 }
4357 
4358 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
4359 {
4360 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4361 	struct wireless_dev *wdev = info->user_ptr[1];
4362 
4363 	if (!rdev->ops->del_virtual_intf)
4364 		return -EOPNOTSUPP;
4365 
4366 	/*
4367 	 * We hold RTNL, so this is safe, without RTNL opencount cannot
4368 	 * reach 0, and thus the rdev cannot be deleted.
4369 	 *
4370 	 * We need to do it for the dev_close(), since that will call
4371 	 * the netdev notifiers, and we need to acquire the mutex there
4372 	 * but don't know if we get there from here or from some other
4373 	 * place (e.g. "ip link set ... down").
4374 	 */
4375 	mutex_unlock(&rdev->wiphy.mtx);
4376 
4377 	/*
4378 	 * If we remove a wireless device without a netdev then clear
4379 	 * user_ptr[1] so that nl80211_post_doit won't dereference it
4380 	 * to check if it needs to do dev_put(). Otherwise it crashes
4381 	 * since the wdev has been freed, unlike with a netdev where
4382 	 * we need the dev_put() for the netdev to really be freed.
4383 	 */
4384 	if (!wdev->netdev)
4385 		info->user_ptr[1] = NULL;
4386 	else
4387 		dev_close(wdev->netdev);
4388 
4389 	mutex_lock(&rdev->wiphy.mtx);
4390 
4391 	return cfg80211_remove_virtual_intf(rdev, wdev);
4392 }
4393 
4394 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
4395 {
4396 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4397 	struct net_device *dev = info->user_ptr[1];
4398 	u16 noack_map;
4399 
4400 	if (!info->attrs[NL80211_ATTR_NOACK_MAP])
4401 		return -EINVAL;
4402 
4403 	if (!rdev->ops->set_noack_map)
4404 		return -EOPNOTSUPP;
4405 
4406 	noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
4407 
4408 	return rdev_set_noack_map(rdev, dev, noack_map);
4409 }
4410 
4411 static int nl80211_validate_key_link_id(struct genl_info *info,
4412 					struct wireless_dev *wdev,
4413 					int link_id, bool pairwise)
4414 {
4415 	if (pairwise) {
4416 		if (link_id != -1) {
4417 			GENL_SET_ERR_MSG(info,
4418 					 "link ID not allowed for pairwise key");
4419 			return -EINVAL;
4420 		}
4421 
4422 		return 0;
4423 	}
4424 
4425 	if (wdev->valid_links) {
4426 		if (link_id == -1) {
4427 			GENL_SET_ERR_MSG(info,
4428 					 "link ID must for MLO group key");
4429 			return -EINVAL;
4430 		}
4431 		if (!(wdev->valid_links & BIT(link_id))) {
4432 			GENL_SET_ERR_MSG(info, "invalid link ID for MLO group key");
4433 			return -EINVAL;
4434 		}
4435 	} else if (link_id != -1) {
4436 		GENL_SET_ERR_MSG(info, "link ID not allowed for non-MLO group key");
4437 		return -EINVAL;
4438 	}
4439 
4440 	return 0;
4441 }
4442 
4443 struct get_key_cookie {
4444 	struct sk_buff *msg;
4445 	int error;
4446 	int idx;
4447 };
4448 
4449 static void get_key_callback(void *c, struct key_params *params)
4450 {
4451 	struct nlattr *key;
4452 	struct get_key_cookie *cookie = c;
4453 
4454 	if ((params->key &&
4455 	     nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
4456 		     params->key_len, params->key)) ||
4457 	    (params->seq &&
4458 	     nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
4459 		     params->seq_len, params->seq)) ||
4460 	    (params->cipher &&
4461 	     nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
4462 			 params->cipher)))
4463 		goto nla_put_failure;
4464 
4465 	key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
4466 	if (!key)
4467 		goto nla_put_failure;
4468 
4469 	if ((params->key &&
4470 	     nla_put(cookie->msg, NL80211_KEY_DATA,
4471 		     params->key_len, params->key)) ||
4472 	    (params->seq &&
4473 	     nla_put(cookie->msg, NL80211_KEY_SEQ,
4474 		     params->seq_len, params->seq)) ||
4475 	    (params->cipher &&
4476 	     nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
4477 			 params->cipher)))
4478 		goto nla_put_failure;
4479 
4480 	if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
4481 		goto nla_put_failure;
4482 
4483 	nla_nest_end(cookie->msg, key);
4484 
4485 	return;
4486  nla_put_failure:
4487 	cookie->error = 1;
4488 }
4489 
4490 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
4491 {
4492 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4493 	int err;
4494 	struct net_device *dev = info->user_ptr[1];
4495 	u8 key_idx = 0;
4496 	const u8 *mac_addr = NULL;
4497 	bool pairwise;
4498 	struct get_key_cookie cookie = {
4499 		.error = 0,
4500 	};
4501 	void *hdr;
4502 	struct sk_buff *msg;
4503 	bool bigtk_support = false;
4504 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4505 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4506 
4507 	if (wiphy_ext_feature_isset(&rdev->wiphy,
4508 				    NL80211_EXT_FEATURE_BEACON_PROTECTION))
4509 		bigtk_support = true;
4510 
4511 	if ((wdev->iftype == NL80211_IFTYPE_STATION ||
4512 	     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
4513 	    wiphy_ext_feature_isset(&rdev->wiphy,
4514 				    NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
4515 		bigtk_support = true;
4516 
4517 	if (info->attrs[NL80211_ATTR_KEY_IDX]) {
4518 		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
4519 
4520 		if (key_idx >= 6 && key_idx <= 7 && !bigtk_support) {
4521 			GENL_SET_ERR_MSG(info, "BIGTK not supported");
4522 			return -EINVAL;
4523 		}
4524 	}
4525 
4526 	if (info->attrs[NL80211_ATTR_MAC])
4527 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4528 
4529 	pairwise = !!mac_addr;
4530 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
4531 		u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
4532 
4533 		if (kt != NL80211_KEYTYPE_GROUP &&
4534 		    kt != NL80211_KEYTYPE_PAIRWISE)
4535 			return -EINVAL;
4536 		pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
4537 	}
4538 
4539 	if (!rdev->ops->get_key)
4540 		return -EOPNOTSUPP;
4541 
4542 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4543 		return -ENOENT;
4544 
4545 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4546 	if (!msg)
4547 		return -ENOMEM;
4548 
4549 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
4550 			     NL80211_CMD_NEW_KEY);
4551 	if (!hdr)
4552 		goto nla_put_failure;
4553 
4554 	cookie.msg = msg;
4555 	cookie.idx = key_idx;
4556 
4557 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4558 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
4559 		goto nla_put_failure;
4560 	if (mac_addr &&
4561 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
4562 		goto nla_put_failure;
4563 
4564 	err = nl80211_validate_key_link_id(info, wdev, link_id, pairwise);
4565 	if (err)
4566 		goto free_msg;
4567 
4568 	err = rdev_get_key(rdev, dev, link_id, key_idx, pairwise, mac_addr,
4569 			   &cookie, get_key_callback);
4570 
4571 	if (err)
4572 		goto free_msg;
4573 
4574 	if (cookie.error)
4575 		goto nla_put_failure;
4576 
4577 	genlmsg_end(msg, hdr);
4578 	return genlmsg_reply(msg, info);
4579 
4580  nla_put_failure:
4581 	err = -ENOBUFS;
4582  free_msg:
4583 	nlmsg_free(msg);
4584 	return err;
4585 }
4586 
4587 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
4588 {
4589 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4590 	struct key_parse key;
4591 	int err;
4592 	struct net_device *dev = info->user_ptr[1];
4593 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4594 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4595 
4596 	err = nl80211_parse_key(info, &key);
4597 	if (err)
4598 		return err;
4599 
4600 	if (key.idx < 0)
4601 		return -EINVAL;
4602 
4603 	/* Only support setting default key and
4604 	 * Extended Key ID action NL80211_KEY_SET_TX.
4605 	 */
4606 	if (!key.def && !key.defmgmt && !key.defbeacon &&
4607 	    !(key.p.mode == NL80211_KEY_SET_TX))
4608 		return -EINVAL;
4609 
4610 	if (key.def) {
4611 		if (!rdev->ops->set_default_key)
4612 			return -EOPNOTSUPP;
4613 
4614 		err = nl80211_key_allowed(wdev);
4615 		if (err)
4616 			return err;
4617 
4618 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4619 		if (err)
4620 			return err;
4621 
4622 		err = rdev_set_default_key(rdev, dev, link_id, key.idx,
4623 					   key.def_uni, key.def_multi);
4624 
4625 		if (err)
4626 			return err;
4627 
4628 #ifdef CONFIG_CFG80211_WEXT
4629 		wdev->wext.default_key = key.idx;
4630 #endif
4631 		return 0;
4632 	} else if (key.defmgmt) {
4633 		if (key.def_uni || !key.def_multi)
4634 			return -EINVAL;
4635 
4636 		if (!rdev->ops->set_default_mgmt_key)
4637 			return -EOPNOTSUPP;
4638 
4639 		err = nl80211_key_allowed(wdev);
4640 		if (err)
4641 			return err;
4642 
4643 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4644 		if (err)
4645 			return err;
4646 
4647 		err = rdev_set_default_mgmt_key(rdev, dev, link_id, key.idx);
4648 		if (err)
4649 			return err;
4650 
4651 #ifdef CONFIG_CFG80211_WEXT
4652 		wdev->wext.default_mgmt_key = key.idx;
4653 #endif
4654 		return 0;
4655 	} else if (key.defbeacon) {
4656 		if (key.def_uni || !key.def_multi)
4657 			return -EINVAL;
4658 
4659 		if (!rdev->ops->set_default_beacon_key)
4660 			return -EOPNOTSUPP;
4661 
4662 		err = nl80211_key_allowed(wdev);
4663 		if (err)
4664 			return err;
4665 
4666 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4667 		if (err)
4668 			return err;
4669 
4670 		return rdev_set_default_beacon_key(rdev, dev, link_id, key.idx);
4671 	} else if (key.p.mode == NL80211_KEY_SET_TX &&
4672 		   wiphy_ext_feature_isset(&rdev->wiphy,
4673 					   NL80211_EXT_FEATURE_EXT_KEY_ID)) {
4674 		u8 *mac_addr = NULL;
4675 
4676 		if (info->attrs[NL80211_ATTR_MAC])
4677 			mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4678 
4679 		if (!mac_addr || key.idx < 0 || key.idx > 1)
4680 			return -EINVAL;
4681 
4682 		err = nl80211_validate_key_link_id(info, wdev, link_id, true);
4683 		if (err)
4684 			return err;
4685 
4686 		return rdev_add_key(rdev, dev, link_id, key.idx,
4687 				    NL80211_KEYTYPE_PAIRWISE,
4688 				    mac_addr, &key.p);
4689 	}
4690 
4691 	return -EINVAL;
4692 }
4693 
4694 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
4695 {
4696 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4697 	int err;
4698 	struct net_device *dev = info->user_ptr[1];
4699 	struct key_parse key;
4700 	const u8 *mac_addr = NULL;
4701 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4702 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4703 
4704 	err = nl80211_parse_key(info, &key);
4705 	if (err)
4706 		return err;
4707 
4708 	if (!key.p.key) {
4709 		GENL_SET_ERR_MSG(info, "no key");
4710 		return -EINVAL;
4711 	}
4712 
4713 	if (info->attrs[NL80211_ATTR_MAC])
4714 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4715 
4716 	if (key.type == -1) {
4717 		if (mac_addr)
4718 			key.type = NL80211_KEYTYPE_PAIRWISE;
4719 		else
4720 			key.type = NL80211_KEYTYPE_GROUP;
4721 	}
4722 
4723 	/* for now */
4724 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4725 	    key.type != NL80211_KEYTYPE_GROUP) {
4726 		GENL_SET_ERR_MSG(info, "key type not pairwise or group");
4727 		return -EINVAL;
4728 	}
4729 
4730 	if (key.type == NL80211_KEYTYPE_GROUP &&
4731 	    info->attrs[NL80211_ATTR_VLAN_ID])
4732 		key.p.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
4733 
4734 	if (!rdev->ops->add_key)
4735 		return -EOPNOTSUPP;
4736 
4737 	if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
4738 					   key.type == NL80211_KEYTYPE_PAIRWISE,
4739 					   mac_addr)) {
4740 		GENL_SET_ERR_MSG(info, "key setting validation failed");
4741 		return -EINVAL;
4742 	}
4743 
4744 	err = nl80211_key_allowed(wdev);
4745 	if (err)
4746 		GENL_SET_ERR_MSG(info, "key not allowed");
4747 
4748 	if (!err)
4749 		err = nl80211_validate_key_link_id(info, wdev, link_id,
4750 				key.type == NL80211_KEYTYPE_PAIRWISE);
4751 
4752 	if (!err) {
4753 		err = rdev_add_key(rdev, dev, link_id, key.idx,
4754 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4755 				    mac_addr, &key.p);
4756 		if (err)
4757 			GENL_SET_ERR_MSG(info, "key addition failed");
4758 	}
4759 
4760 	return err;
4761 }
4762 
4763 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
4764 {
4765 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4766 	int err;
4767 	struct net_device *dev = info->user_ptr[1];
4768 	u8 *mac_addr = NULL;
4769 	struct key_parse key;
4770 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4771 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4772 
4773 	err = nl80211_parse_key(info, &key);
4774 	if (err)
4775 		return err;
4776 
4777 	if (info->attrs[NL80211_ATTR_MAC])
4778 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4779 
4780 	if (key.type == -1) {
4781 		if (mac_addr)
4782 			key.type = NL80211_KEYTYPE_PAIRWISE;
4783 		else
4784 			key.type = NL80211_KEYTYPE_GROUP;
4785 	}
4786 
4787 	/* for now */
4788 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4789 	    key.type != NL80211_KEYTYPE_GROUP)
4790 		return -EINVAL;
4791 
4792 	if (!cfg80211_valid_key_idx(rdev, key.idx,
4793 				    key.type == NL80211_KEYTYPE_PAIRWISE))
4794 		return -EINVAL;
4795 
4796 	if (!rdev->ops->del_key)
4797 		return -EOPNOTSUPP;
4798 
4799 	err = nl80211_key_allowed(wdev);
4800 
4801 	if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
4802 	    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4803 		err = -ENOENT;
4804 
4805 	if (!err)
4806 		err = nl80211_validate_key_link_id(info, wdev, link_id,
4807 				key.type == NL80211_KEYTYPE_PAIRWISE);
4808 
4809 	if (!err)
4810 		err = rdev_del_key(rdev, dev, link_id, key.idx,
4811 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4812 				   mac_addr);
4813 
4814 #ifdef CONFIG_CFG80211_WEXT
4815 	if (!err) {
4816 		if (key.idx == wdev->wext.default_key)
4817 			wdev->wext.default_key = -1;
4818 		else if (key.idx == wdev->wext.default_mgmt_key)
4819 			wdev->wext.default_mgmt_key = -1;
4820 	}
4821 #endif
4822 
4823 	return err;
4824 }
4825 
4826 /* This function returns an error or the number of nested attributes */
4827 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
4828 {
4829 	struct nlattr *attr;
4830 	int n_entries = 0, tmp;
4831 
4832 	nla_for_each_nested(attr, nl_attr, tmp) {
4833 		if (nla_len(attr) != ETH_ALEN)
4834 			return -EINVAL;
4835 
4836 		n_entries++;
4837 	}
4838 
4839 	return n_entries;
4840 }
4841 
4842 /*
4843  * This function parses ACL information and allocates memory for ACL data.
4844  * On successful return, the calling function is responsible to free the
4845  * ACL buffer returned by this function.
4846  */
4847 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
4848 						struct genl_info *info)
4849 {
4850 	enum nl80211_acl_policy acl_policy;
4851 	struct nlattr *attr;
4852 	struct cfg80211_acl_data *acl;
4853 	int i = 0, n_entries, tmp;
4854 
4855 	if (!wiphy->max_acl_mac_addrs)
4856 		return ERR_PTR(-EOPNOTSUPP);
4857 
4858 	if (!info->attrs[NL80211_ATTR_ACL_POLICY])
4859 		return ERR_PTR(-EINVAL);
4860 
4861 	acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
4862 	if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
4863 	    acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
4864 		return ERR_PTR(-EINVAL);
4865 
4866 	if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
4867 		return ERR_PTR(-EINVAL);
4868 
4869 	n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
4870 	if (n_entries < 0)
4871 		return ERR_PTR(n_entries);
4872 
4873 	if (n_entries > wiphy->max_acl_mac_addrs)
4874 		return ERR_PTR(-EOPNOTSUPP);
4875 
4876 	acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4877 	if (!acl)
4878 		return ERR_PTR(-ENOMEM);
4879 	acl->n_acl_entries = n_entries;
4880 
4881 	nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4882 		memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4883 		i++;
4884 	}
4885 	acl->acl_policy = acl_policy;
4886 
4887 	return acl;
4888 }
4889 
4890 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4891 {
4892 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4893 	struct net_device *dev = info->user_ptr[1];
4894 	struct cfg80211_acl_data *acl;
4895 	int err;
4896 
4897 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4898 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4899 		return -EOPNOTSUPP;
4900 
4901 	if (!dev->ieee80211_ptr->links[0].ap.beacon_interval)
4902 		return -EINVAL;
4903 
4904 	acl = parse_acl_data(&rdev->wiphy, info);
4905 	if (IS_ERR(acl))
4906 		return PTR_ERR(acl);
4907 
4908 	err = rdev_set_mac_acl(rdev, dev, acl);
4909 
4910 	kfree(acl);
4911 
4912 	return err;
4913 }
4914 
4915 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4916 			   u8 *rates, u8 rates_len)
4917 {
4918 	u8 i;
4919 	u32 mask = 0;
4920 
4921 	for (i = 0; i < rates_len; i++) {
4922 		int rate = (rates[i] & 0x7f) * 5;
4923 		int ridx;
4924 
4925 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4926 			struct ieee80211_rate *srate =
4927 				&sband->bitrates[ridx];
4928 			if (rate == srate->bitrate) {
4929 				mask |= 1 << ridx;
4930 				break;
4931 			}
4932 		}
4933 		if (ridx == sband->n_bitrates)
4934 			return 0; /* rate not found */
4935 	}
4936 
4937 	return mask;
4938 }
4939 
4940 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4941 			       u8 *rates, u8 rates_len,
4942 			       u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4943 {
4944 	u8 i;
4945 
4946 	memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4947 
4948 	for (i = 0; i < rates_len; i++) {
4949 		int ridx, rbit;
4950 
4951 		ridx = rates[i] / 8;
4952 		rbit = BIT(rates[i] % 8);
4953 
4954 		/* check validity */
4955 		if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4956 			return false;
4957 
4958 		/* check availability */
4959 		ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
4960 		if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
4961 			mcs[ridx] |= rbit;
4962 		else
4963 			return false;
4964 	}
4965 
4966 	return true;
4967 }
4968 
4969 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
4970 {
4971 	u16 mcs_mask = 0;
4972 
4973 	switch (vht_mcs_map) {
4974 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
4975 		break;
4976 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
4977 		mcs_mask = 0x00FF;
4978 		break;
4979 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
4980 		mcs_mask = 0x01FF;
4981 		break;
4982 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
4983 		mcs_mask = 0x03FF;
4984 		break;
4985 	default:
4986 		break;
4987 	}
4988 
4989 	return mcs_mask;
4990 }
4991 
4992 static void vht_build_mcs_mask(u16 vht_mcs_map,
4993 			       u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
4994 {
4995 	u8 nss;
4996 
4997 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
4998 		vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
4999 		vht_mcs_map >>= 2;
5000 	}
5001 }
5002 
5003 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
5004 			     struct nl80211_txrate_vht *txrate,
5005 			     u16 mcs[NL80211_VHT_NSS_MAX])
5006 {
5007 	u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5008 	u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
5009 	u8 i;
5010 
5011 	if (!sband->vht_cap.vht_supported)
5012 		return false;
5013 
5014 	memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
5015 
5016 	/* Build vht_mcs_mask from VHT capabilities */
5017 	vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
5018 
5019 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5020 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
5021 			mcs[i] = txrate->mcs[i];
5022 		else
5023 			return false;
5024 	}
5025 
5026 	return true;
5027 }
5028 
5029 static u16 he_mcs_map_to_mcs_mask(u8 he_mcs_map)
5030 {
5031 	switch (he_mcs_map) {
5032 	case IEEE80211_HE_MCS_NOT_SUPPORTED:
5033 		return 0;
5034 	case IEEE80211_HE_MCS_SUPPORT_0_7:
5035 		return 0x00FF;
5036 	case IEEE80211_HE_MCS_SUPPORT_0_9:
5037 		return 0x03FF;
5038 	case IEEE80211_HE_MCS_SUPPORT_0_11:
5039 		return 0xFFF;
5040 	default:
5041 		break;
5042 	}
5043 	return 0;
5044 }
5045 
5046 static void he_build_mcs_mask(u16 he_mcs_map,
5047 			      u16 he_mcs_mask[NL80211_HE_NSS_MAX])
5048 {
5049 	u8 nss;
5050 
5051 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
5052 		he_mcs_mask[nss] = he_mcs_map_to_mcs_mask(he_mcs_map & 0x03);
5053 		he_mcs_map >>= 2;
5054 	}
5055 }
5056 
5057 static u16 he_get_txmcsmap(struct genl_info *info, unsigned int link_id,
5058 			   const struct ieee80211_sta_he_cap *he_cap)
5059 {
5060 	struct net_device *dev = info->user_ptr[1];
5061 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5062 	struct cfg80211_chan_def *chandef;
5063 	__le16 tx_mcs;
5064 
5065 	chandef = wdev_chandef(wdev, link_id);
5066 	if (!chandef) {
5067 		/*
5068 		 * This is probably broken, but we never maintained
5069 		 * a chandef in these cases, so it always was.
5070 		 */
5071 		return le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
5072 	}
5073 
5074 	switch (chandef->width) {
5075 	case NL80211_CHAN_WIDTH_80P80:
5076 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80p80;
5077 		break;
5078 	case NL80211_CHAN_WIDTH_160:
5079 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_160;
5080 		break;
5081 	default:
5082 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80;
5083 		break;
5084 	}
5085 
5086 	return le16_to_cpu(tx_mcs);
5087 }
5088 
5089 static bool he_set_mcs_mask(struct genl_info *info,
5090 			    struct wireless_dev *wdev,
5091 			    struct ieee80211_supported_band *sband,
5092 			    struct nl80211_txrate_he *txrate,
5093 			    u16 mcs[NL80211_HE_NSS_MAX],
5094 			    unsigned int link_id)
5095 {
5096 	const struct ieee80211_sta_he_cap *he_cap;
5097 	u16 tx_mcs_mask[NL80211_HE_NSS_MAX] = {};
5098 	u16 tx_mcs_map = 0;
5099 	u8 i;
5100 
5101 	he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5102 	if (!he_cap)
5103 		return false;
5104 
5105 	memset(mcs, 0, sizeof(u16) * NL80211_HE_NSS_MAX);
5106 
5107 	tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5108 
5109 	/* Build he_mcs_mask from HE capabilities */
5110 	he_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
5111 
5112 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5113 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
5114 			mcs[i] = txrate->mcs[i];
5115 		else
5116 			return false;
5117 	}
5118 
5119 	return true;
5120 }
5121 
5122 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
5123 					 struct nlattr *attrs[],
5124 					 enum nl80211_attrs attr,
5125 					 struct cfg80211_bitrate_mask *mask,
5126 					 struct net_device *dev,
5127 					 bool default_all_enabled,
5128 					 unsigned int link_id)
5129 {
5130 	struct nlattr *tb[NL80211_TXRATE_MAX + 1];
5131 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5132 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5133 	int rem, i;
5134 	struct nlattr *tx_rates;
5135 	struct ieee80211_supported_band *sband;
5136 	u16 vht_tx_mcs_map, he_tx_mcs_map;
5137 
5138 	memset(mask, 0, sizeof(*mask));
5139 	/* Default to all rates enabled */
5140 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
5141 		const struct ieee80211_sta_he_cap *he_cap;
5142 
5143 		if (!default_all_enabled)
5144 			break;
5145 
5146 		sband = rdev->wiphy.bands[i];
5147 
5148 		if (!sband)
5149 			continue;
5150 
5151 		mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
5152 		memcpy(mask->control[i].ht_mcs,
5153 		       sband->ht_cap.mcs.rx_mask,
5154 		       sizeof(mask->control[i].ht_mcs));
5155 
5156 		if (sband->vht_cap.vht_supported) {
5157 			vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5158 			vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
5159 		}
5160 
5161 		he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5162 		if (!he_cap)
5163 			continue;
5164 
5165 		he_tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5166 		he_build_mcs_mask(he_tx_mcs_map, mask->control[i].he_mcs);
5167 
5168 		mask->control[i].he_gi = 0xFF;
5169 		mask->control[i].he_ltf = 0xFF;
5170 	}
5171 
5172 	/* if no rates are given set it back to the defaults */
5173 	if (!attrs[attr])
5174 		goto out;
5175 
5176 	/* The nested attribute uses enum nl80211_band as the index. This maps
5177 	 * directly to the enum nl80211_band values used in cfg80211.
5178 	 */
5179 	BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
5180 	nla_for_each_nested(tx_rates, attrs[attr], rem) {
5181 		enum nl80211_band band = nla_type(tx_rates);
5182 		int err;
5183 
5184 		if (band < 0 || band >= NUM_NL80211_BANDS)
5185 			return -EINVAL;
5186 		sband = rdev->wiphy.bands[band];
5187 		if (sband == NULL)
5188 			return -EINVAL;
5189 		err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
5190 						  tx_rates,
5191 						  nl80211_txattr_policy,
5192 						  info->extack);
5193 		if (err)
5194 			return err;
5195 		if (tb[NL80211_TXRATE_LEGACY]) {
5196 			mask->control[band].legacy = rateset_to_mask(
5197 				sband,
5198 				nla_data(tb[NL80211_TXRATE_LEGACY]),
5199 				nla_len(tb[NL80211_TXRATE_LEGACY]));
5200 			if ((mask->control[band].legacy == 0) &&
5201 			    nla_len(tb[NL80211_TXRATE_LEGACY]))
5202 				return -EINVAL;
5203 		}
5204 		if (tb[NL80211_TXRATE_HT]) {
5205 			if (!ht_rateset_to_mask(
5206 					sband,
5207 					nla_data(tb[NL80211_TXRATE_HT]),
5208 					nla_len(tb[NL80211_TXRATE_HT]),
5209 					mask->control[band].ht_mcs))
5210 				return -EINVAL;
5211 		}
5212 
5213 		if (tb[NL80211_TXRATE_VHT]) {
5214 			if (!vht_set_mcs_mask(
5215 					sband,
5216 					nla_data(tb[NL80211_TXRATE_VHT]),
5217 					mask->control[band].vht_mcs))
5218 				return -EINVAL;
5219 		}
5220 
5221 		if (tb[NL80211_TXRATE_GI]) {
5222 			mask->control[band].gi =
5223 				nla_get_u8(tb[NL80211_TXRATE_GI]);
5224 			if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
5225 				return -EINVAL;
5226 		}
5227 		if (tb[NL80211_TXRATE_HE] &&
5228 		    !he_set_mcs_mask(info, wdev, sband,
5229 				     nla_data(tb[NL80211_TXRATE_HE]),
5230 				     mask->control[band].he_mcs,
5231 				     link_id))
5232 			return -EINVAL;
5233 
5234 		if (tb[NL80211_TXRATE_HE_GI])
5235 			mask->control[band].he_gi =
5236 				nla_get_u8(tb[NL80211_TXRATE_HE_GI]);
5237 		if (tb[NL80211_TXRATE_HE_LTF])
5238 			mask->control[band].he_ltf =
5239 				nla_get_u8(tb[NL80211_TXRATE_HE_LTF]);
5240 
5241 		if (mask->control[band].legacy == 0) {
5242 			/* don't allow empty legacy rates if HT, VHT or HE
5243 			 * are not even supported.
5244 			 */
5245 			if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
5246 			      rdev->wiphy.bands[band]->vht_cap.vht_supported ||
5247 			      ieee80211_get_he_iftype_cap(sband, wdev->iftype)))
5248 				return -EINVAL;
5249 
5250 			for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5251 				if (mask->control[band].ht_mcs[i])
5252 					goto out;
5253 
5254 			for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5255 				if (mask->control[band].vht_mcs[i])
5256 					goto out;
5257 
5258 			for (i = 0; i < NL80211_HE_NSS_MAX; i++)
5259 				if (mask->control[band].he_mcs[i])
5260 					goto out;
5261 
5262 			/* legacy and mcs rates may not be both empty */
5263 			return -EINVAL;
5264 		}
5265 	}
5266 
5267 out:
5268 	return 0;
5269 }
5270 
5271 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
5272 				   enum nl80211_band band,
5273 				   struct cfg80211_bitrate_mask *beacon_rate)
5274 {
5275 	u32 count_ht, count_vht, count_he, i;
5276 	u32 rate = beacon_rate->control[band].legacy;
5277 
5278 	/* Allow only one rate */
5279 	if (hweight32(rate) > 1)
5280 		return -EINVAL;
5281 
5282 	count_ht = 0;
5283 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
5284 		if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
5285 			return -EINVAL;
5286 		} else if (beacon_rate->control[band].ht_mcs[i]) {
5287 			count_ht++;
5288 			if (count_ht > 1)
5289 				return -EINVAL;
5290 		}
5291 		if (count_ht && rate)
5292 			return -EINVAL;
5293 	}
5294 
5295 	count_vht = 0;
5296 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5297 		if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
5298 			return -EINVAL;
5299 		} else if (beacon_rate->control[band].vht_mcs[i]) {
5300 			count_vht++;
5301 			if (count_vht > 1)
5302 				return -EINVAL;
5303 		}
5304 		if (count_vht && rate)
5305 			return -EINVAL;
5306 	}
5307 
5308 	count_he = 0;
5309 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5310 		if (hweight16(beacon_rate->control[band].he_mcs[i]) > 1) {
5311 			return -EINVAL;
5312 		} else if (beacon_rate->control[band].he_mcs[i]) {
5313 			count_he++;
5314 			if (count_he > 1)
5315 				return -EINVAL;
5316 		}
5317 		if (count_he && rate)
5318 			return -EINVAL;
5319 	}
5320 
5321 	if ((count_ht && count_vht && count_he) ||
5322 	    (!rate && !count_ht && !count_vht && !count_he))
5323 		return -EINVAL;
5324 
5325 	if (rate &&
5326 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5327 				     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
5328 		return -EINVAL;
5329 	if (count_ht &&
5330 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5331 				     NL80211_EXT_FEATURE_BEACON_RATE_HT))
5332 		return -EINVAL;
5333 	if (count_vht &&
5334 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5335 				     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
5336 		return -EINVAL;
5337 	if (count_he &&
5338 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5339 				     NL80211_EXT_FEATURE_BEACON_RATE_HE))
5340 		return -EINVAL;
5341 
5342 	return 0;
5343 }
5344 
5345 static int nl80211_parse_mbssid_config(struct wiphy *wiphy,
5346 				       struct net_device *dev,
5347 				       struct nlattr *attrs,
5348 				       struct cfg80211_mbssid_config *config,
5349 				       u8 num_elems)
5350 {
5351 	struct nlattr *tb[NL80211_MBSSID_CONFIG_ATTR_MAX + 1];
5352 
5353 	if (!wiphy->mbssid_max_interfaces)
5354 		return -EOPNOTSUPP;
5355 
5356 	if (nla_parse_nested(tb, NL80211_MBSSID_CONFIG_ATTR_MAX, attrs, NULL,
5357 			     NULL) ||
5358 	    !tb[NL80211_MBSSID_CONFIG_ATTR_INDEX])
5359 		return -EINVAL;
5360 
5361 	config->ema = nla_get_flag(tb[NL80211_MBSSID_CONFIG_ATTR_EMA]);
5362 	if (config->ema) {
5363 		if (!wiphy->ema_max_profile_periodicity)
5364 			return -EOPNOTSUPP;
5365 
5366 		if (num_elems > wiphy->ema_max_profile_periodicity)
5367 			return -EINVAL;
5368 	}
5369 
5370 	config->index = nla_get_u8(tb[NL80211_MBSSID_CONFIG_ATTR_INDEX]);
5371 	if (config->index >= wiphy->mbssid_max_interfaces ||
5372 	    (!config->index && !num_elems))
5373 		return -EINVAL;
5374 
5375 	if (tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]) {
5376 		u32 tx_ifindex =
5377 			nla_get_u32(tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]);
5378 
5379 		if ((!config->index && tx_ifindex != dev->ifindex) ||
5380 		    (config->index && tx_ifindex == dev->ifindex))
5381 			return -EINVAL;
5382 
5383 		if (tx_ifindex != dev->ifindex) {
5384 			struct net_device *tx_netdev =
5385 				dev_get_by_index(wiphy_net(wiphy), tx_ifindex);
5386 
5387 			if (!tx_netdev || !tx_netdev->ieee80211_ptr ||
5388 			    tx_netdev->ieee80211_ptr->wiphy != wiphy ||
5389 			    tx_netdev->ieee80211_ptr->iftype !=
5390 							NL80211_IFTYPE_AP) {
5391 				dev_put(tx_netdev);
5392 				return -EINVAL;
5393 			}
5394 
5395 			config->tx_wdev = tx_netdev->ieee80211_ptr;
5396 		} else {
5397 			config->tx_wdev = dev->ieee80211_ptr;
5398 		}
5399 	} else if (!config->index) {
5400 		config->tx_wdev = dev->ieee80211_ptr;
5401 	} else {
5402 		return -EINVAL;
5403 	}
5404 
5405 	return 0;
5406 }
5407 
5408 static struct cfg80211_mbssid_elems *
5409 nl80211_parse_mbssid_elems(struct wiphy *wiphy, struct nlattr *attrs)
5410 {
5411 	struct nlattr *nl_elems;
5412 	struct cfg80211_mbssid_elems *elems;
5413 	int rem_elems;
5414 	u8 i = 0, num_elems = 0;
5415 
5416 	if (!wiphy->mbssid_max_interfaces)
5417 		return ERR_PTR(-EINVAL);
5418 
5419 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5420 		if (num_elems >= 255)
5421 			return ERR_PTR(-EINVAL);
5422 		num_elems++;
5423 	}
5424 
5425 	elems = kzalloc(struct_size(elems, elem, num_elems), GFP_KERNEL);
5426 	if (!elems)
5427 		return ERR_PTR(-ENOMEM);
5428 	elems->cnt = num_elems;
5429 
5430 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5431 		elems->elem[i].data = nla_data(nl_elems);
5432 		elems->elem[i].len = nla_len(nl_elems);
5433 		i++;
5434 	}
5435 	return elems;
5436 }
5437 
5438 static struct cfg80211_rnr_elems *
5439 nl80211_parse_rnr_elems(struct wiphy *wiphy, struct nlattr *attrs,
5440 			struct netlink_ext_ack *extack)
5441 {
5442 	struct nlattr *nl_elems;
5443 	struct cfg80211_rnr_elems *elems;
5444 	int rem_elems;
5445 	u8 i = 0, num_elems = 0;
5446 
5447 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5448 		int ret;
5449 
5450 		ret = validate_ie_attr(nl_elems, extack);
5451 		if (ret)
5452 			return ERR_PTR(ret);
5453 
5454 		num_elems++;
5455 	}
5456 
5457 	elems = kzalloc(struct_size(elems, elem, num_elems), GFP_KERNEL);
5458 	if (!elems)
5459 		return ERR_PTR(-ENOMEM);
5460 	elems->cnt = num_elems;
5461 
5462 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5463 		elems->elem[i].data = nla_data(nl_elems);
5464 		elems->elem[i].len = nla_len(nl_elems);
5465 		i++;
5466 	}
5467 	return elems;
5468 }
5469 
5470 static int nl80211_parse_he_bss_color(struct nlattr *attrs,
5471 				      struct cfg80211_he_bss_color *he_bss_color)
5472 {
5473 	struct nlattr *tb[NL80211_HE_BSS_COLOR_ATTR_MAX + 1];
5474 	int err;
5475 
5476 	err = nla_parse_nested(tb, NL80211_HE_BSS_COLOR_ATTR_MAX, attrs,
5477 			       he_bss_color_policy, NULL);
5478 	if (err)
5479 		return err;
5480 
5481 	if (!tb[NL80211_HE_BSS_COLOR_ATTR_COLOR])
5482 		return -EINVAL;
5483 
5484 	he_bss_color->color =
5485 		nla_get_u8(tb[NL80211_HE_BSS_COLOR_ATTR_COLOR]);
5486 	he_bss_color->enabled =
5487 		!nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_DISABLED]);
5488 	he_bss_color->partial =
5489 		nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_PARTIAL]);
5490 
5491 	return 0;
5492 }
5493 
5494 static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
5495 				struct nlattr *attrs[],
5496 				struct cfg80211_beacon_data *bcn,
5497 				struct netlink_ext_ack *extack)
5498 {
5499 	bool haveinfo = false;
5500 	int err;
5501 
5502 	memset(bcn, 0, sizeof(*bcn));
5503 
5504 	bcn->link_id = nl80211_link_id(attrs);
5505 
5506 	if (attrs[NL80211_ATTR_BEACON_HEAD]) {
5507 		bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
5508 		bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
5509 		if (!bcn->head_len)
5510 			return -EINVAL;
5511 		haveinfo = true;
5512 	}
5513 
5514 	if (attrs[NL80211_ATTR_BEACON_TAIL]) {
5515 		bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
5516 		bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
5517 		haveinfo = true;
5518 	}
5519 
5520 	if (!haveinfo)
5521 		return -EINVAL;
5522 
5523 	if (attrs[NL80211_ATTR_IE]) {
5524 		bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
5525 		bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
5526 	}
5527 
5528 	if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
5529 		bcn->proberesp_ies =
5530 			nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5531 		bcn->proberesp_ies_len =
5532 			nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5533 	}
5534 
5535 	if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
5536 		bcn->assocresp_ies =
5537 			nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5538 		bcn->assocresp_ies_len =
5539 			nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5540 	}
5541 
5542 	if (attrs[NL80211_ATTR_PROBE_RESP]) {
5543 		bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
5544 		bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
5545 	}
5546 
5547 	if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
5548 		struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
5549 
5550 		err = nla_parse_nested_deprecated(tb,
5551 						  NL80211_FTM_RESP_ATTR_MAX,
5552 						  attrs[NL80211_ATTR_FTM_RESPONDER],
5553 						  NULL, NULL);
5554 		if (err)
5555 			return err;
5556 
5557 		if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
5558 		    wiphy_ext_feature_isset(&rdev->wiphy,
5559 					    NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
5560 			bcn->ftm_responder = 1;
5561 		else
5562 			return -EOPNOTSUPP;
5563 
5564 		if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
5565 			bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
5566 			bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
5567 		}
5568 
5569 		if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
5570 			bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5571 			bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5572 		}
5573 	} else {
5574 		bcn->ftm_responder = -1;
5575 	}
5576 
5577 	if (attrs[NL80211_ATTR_HE_BSS_COLOR]) {
5578 		err = nl80211_parse_he_bss_color(attrs[NL80211_ATTR_HE_BSS_COLOR],
5579 						 &bcn->he_bss_color);
5580 		if (err)
5581 			return err;
5582 		bcn->he_bss_color_valid = true;
5583 	}
5584 
5585 	if (attrs[NL80211_ATTR_MBSSID_ELEMS]) {
5586 		struct cfg80211_mbssid_elems *mbssid =
5587 			nl80211_parse_mbssid_elems(&rdev->wiphy,
5588 						   attrs[NL80211_ATTR_MBSSID_ELEMS]);
5589 
5590 		if (IS_ERR(mbssid))
5591 			return PTR_ERR(mbssid);
5592 
5593 		bcn->mbssid_ies = mbssid;
5594 
5595 		if (bcn->mbssid_ies && attrs[NL80211_ATTR_EMA_RNR_ELEMS]) {
5596 			struct cfg80211_rnr_elems *rnr =
5597 				nl80211_parse_rnr_elems(&rdev->wiphy,
5598 							attrs[NL80211_ATTR_EMA_RNR_ELEMS],
5599 							extack);
5600 
5601 			if (IS_ERR(rnr))
5602 				return PTR_ERR(rnr);
5603 
5604 			if (rnr && rnr->cnt < bcn->mbssid_ies->cnt)
5605 				return -EINVAL;
5606 
5607 			bcn->rnr_ies = rnr;
5608 		}
5609 	}
5610 
5611 	return 0;
5612 }
5613 
5614 static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
5615 				    struct ieee80211_he_obss_pd *he_obss_pd)
5616 {
5617 	struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
5618 	int err;
5619 
5620 	err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
5621 			       he_obss_pd_policy, NULL);
5622 	if (err)
5623 		return err;
5624 
5625 	if (!tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL])
5626 		return -EINVAL;
5627 
5628 	he_obss_pd->sr_ctrl = nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL]);
5629 
5630 	if (tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET])
5631 		he_obss_pd->min_offset =
5632 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
5633 	if (tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
5634 		he_obss_pd->max_offset =
5635 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
5636 	if (tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET])
5637 		he_obss_pd->non_srg_max_offset =
5638 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET]);
5639 
5640 	if (he_obss_pd->min_offset > he_obss_pd->max_offset)
5641 		return -EINVAL;
5642 
5643 	if (tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP])
5644 		memcpy(he_obss_pd->bss_color_bitmap,
5645 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP]),
5646 		       sizeof(he_obss_pd->bss_color_bitmap));
5647 
5648 	if (tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP])
5649 		memcpy(he_obss_pd->partial_bssid_bitmap,
5650 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP]),
5651 		       sizeof(he_obss_pd->partial_bssid_bitmap));
5652 
5653 	he_obss_pd->enable = true;
5654 
5655 	return 0;
5656 }
5657 
5658 static int nl80211_parse_fils_discovery(struct cfg80211_registered_device *rdev,
5659 					struct nlattr *attrs,
5660 					struct cfg80211_fils_discovery *fd)
5661 {
5662 	struct nlattr *tb[NL80211_FILS_DISCOVERY_ATTR_MAX + 1];
5663 	int ret;
5664 
5665 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5666 				     NL80211_EXT_FEATURE_FILS_DISCOVERY))
5667 		return -EINVAL;
5668 
5669 	ret = nla_parse_nested(tb, NL80211_FILS_DISCOVERY_ATTR_MAX, attrs,
5670 			       NULL, NULL);
5671 	if (ret)
5672 		return ret;
5673 
5674 	if (!tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] &&
5675 	    !tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] &&
5676 	    !tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]) {
5677 		fd->update = true;
5678 		return 0;
5679 	}
5680 
5681 	if (!tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] ||
5682 	    !tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] ||
5683 	    !tb[NL80211_FILS_DISCOVERY_ATTR_TMPL])
5684 		return -EINVAL;
5685 
5686 	fd->tmpl_len = nla_len(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5687 	fd->tmpl = nla_data(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5688 	fd->min_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN]);
5689 	fd->max_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX]);
5690 	fd->update = true;
5691 	return 0;
5692 }
5693 
5694 static int
5695 nl80211_parse_unsol_bcast_probe_resp(struct cfg80211_registered_device *rdev,
5696 				     struct nlattr *attrs,
5697 				     struct cfg80211_unsol_bcast_probe_resp *presp)
5698 {
5699 	struct nlattr *tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1];
5700 	int ret;
5701 
5702 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5703 				     NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP))
5704 		return -EINVAL;
5705 
5706 	ret = nla_parse_nested(tb, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX,
5707 			       attrs, NULL, NULL);
5708 	if (ret)
5709 		return ret;
5710 
5711 	if (!tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] &&
5712 	    !tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]) {
5713 		presp->update = true;
5714 		return 0;
5715 	}
5716 
5717 	if (!tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] ||
5718 	    !tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL])
5719 		return -EINVAL;
5720 
5721 	presp->tmpl = nla_data(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5722 	presp->tmpl_len = nla_len(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5723 	presp->interval = nla_get_u32(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT]);
5724 	presp->update = true;
5725 	return 0;
5726 }
5727 
5728 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
5729 					    const struct element *rates)
5730 {
5731 	int i;
5732 
5733 	if (!rates)
5734 		return;
5735 
5736 	for (i = 0; i < rates->datalen; i++) {
5737 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
5738 			params->ht_required = true;
5739 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
5740 			params->vht_required = true;
5741 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HE_PHY)
5742 			params->he_required = true;
5743 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_SAE_H2E)
5744 			params->sae_h2e_required = true;
5745 	}
5746 }
5747 
5748 /*
5749  * Since the nl80211 API didn't include, from the beginning, attributes about
5750  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
5751  * benefit of drivers that rebuild IEs in the firmware.
5752  */
5753 static int nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
5754 {
5755 	const struct cfg80211_beacon_data *bcn = &params->beacon;
5756 	size_t ies_len = bcn->tail_len;
5757 	const u8 *ies = bcn->tail;
5758 	const struct element *rates;
5759 	const struct element *cap;
5760 
5761 	rates = cfg80211_find_elem(WLAN_EID_SUPP_RATES, ies, ies_len);
5762 	nl80211_check_ap_rate_selectors(params, rates);
5763 
5764 	rates = cfg80211_find_elem(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
5765 	nl80211_check_ap_rate_selectors(params, rates);
5766 
5767 	cap = cfg80211_find_elem(WLAN_EID_HT_CAPABILITY, ies, ies_len);
5768 	if (cap && cap->datalen >= sizeof(*params->ht_cap))
5769 		params->ht_cap = (void *)cap->data;
5770 	cap = cfg80211_find_elem(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
5771 	if (cap && cap->datalen >= sizeof(*params->vht_cap))
5772 		params->vht_cap = (void *)cap->data;
5773 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
5774 	if (cap && cap->datalen >= sizeof(*params->he_cap) + 1)
5775 		params->he_cap = (void *)(cap->data + 1);
5776 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ies, ies_len);
5777 	if (cap && cap->datalen >= sizeof(*params->he_oper) + 1)
5778 		params->he_oper = (void *)(cap->data + 1);
5779 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_CAPABILITY, ies, ies_len);
5780 	if (cap) {
5781 		if (!cap->datalen)
5782 			return -EINVAL;
5783 		params->eht_cap = (void *)(cap->data + 1);
5784 		if (!ieee80211_eht_capa_size_ok((const u8 *)params->he_cap,
5785 						(const u8 *)params->eht_cap,
5786 						cap->datalen - 1, true))
5787 			return -EINVAL;
5788 	}
5789 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_OPERATION, ies, ies_len);
5790 	if (cap) {
5791 		if (!cap->datalen)
5792 			return -EINVAL;
5793 		params->eht_oper = (void *)(cap->data + 1);
5794 		if (!ieee80211_eht_oper_size_ok((const u8 *)params->eht_oper,
5795 						cap->datalen - 1))
5796 			return -EINVAL;
5797 	}
5798 	return 0;
5799 }
5800 
5801 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
5802 				   struct cfg80211_ap_settings *params)
5803 {
5804 	struct wireless_dev *wdev;
5805 
5806 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
5807 		if (wdev->iftype != NL80211_IFTYPE_AP &&
5808 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)
5809 			continue;
5810 
5811 		if (!wdev->u.ap.preset_chandef.chan)
5812 			continue;
5813 
5814 		params->chandef = wdev->u.ap.preset_chandef;
5815 		return true;
5816 	}
5817 
5818 	return false;
5819 }
5820 
5821 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
5822 				    enum nl80211_auth_type auth_type,
5823 				    enum nl80211_commands cmd)
5824 {
5825 	if (auth_type > NL80211_AUTHTYPE_MAX)
5826 		return false;
5827 
5828 	switch (cmd) {
5829 	case NL80211_CMD_AUTHENTICATE:
5830 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5831 		    auth_type == NL80211_AUTHTYPE_SAE)
5832 			return false;
5833 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5834 					     NL80211_EXT_FEATURE_FILS_STA) &&
5835 		    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5836 		     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5837 		     auth_type == NL80211_AUTHTYPE_FILS_PK))
5838 			return false;
5839 		return true;
5840 	case NL80211_CMD_CONNECT:
5841 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5842 		    !wiphy_ext_feature_isset(&rdev->wiphy,
5843 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
5844 		    auth_type == NL80211_AUTHTYPE_SAE)
5845 			return false;
5846 
5847 		/* FILS with SK PFS or PK not supported yet */
5848 		if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5849 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5850 			return false;
5851 		if (!wiphy_ext_feature_isset(
5852 			    &rdev->wiphy,
5853 			    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
5854 		    auth_type == NL80211_AUTHTYPE_FILS_SK)
5855 			return false;
5856 		return true;
5857 	case NL80211_CMD_START_AP:
5858 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5859 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP) &&
5860 		    auth_type == NL80211_AUTHTYPE_SAE)
5861 			return false;
5862 		/* FILS not supported yet */
5863 		if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5864 		    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5865 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5866 			return false;
5867 		return true;
5868 	default:
5869 		return false;
5870 	}
5871 }
5872 
5873 static void nl80211_send_ap_started(struct wireless_dev *wdev,
5874 				    unsigned int link_id)
5875 {
5876 	struct wiphy *wiphy = wdev->wiphy;
5877 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
5878 	struct sk_buff *msg;
5879 	void *hdr;
5880 
5881 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5882 	if (!msg)
5883 		return;
5884 
5885 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_START_AP);
5886 	if (!hdr)
5887 		goto out;
5888 
5889 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
5890 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
5891 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
5892 			      NL80211_ATTR_PAD) ||
5893 	    (wdev->u.ap.ssid_len &&
5894 	     nla_put(msg, NL80211_ATTR_SSID, wdev->u.ap.ssid_len,
5895 		     wdev->u.ap.ssid)) ||
5896 	    (wdev->valid_links &&
5897 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)))
5898 		goto out;
5899 
5900 	genlmsg_end(msg, hdr);
5901 
5902 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
5903 				NL80211_MCGRP_MLME, GFP_KERNEL);
5904 	return;
5905 out:
5906 	nlmsg_free(msg);
5907 }
5908 
5909 static int nl80211_validate_ap_phy_operation(struct cfg80211_ap_settings *params)
5910 {
5911 	struct ieee80211_channel *channel = params->chandef.chan;
5912 
5913 	if ((params->he_cap ||  params->he_oper) &&
5914 	    (channel->flags & IEEE80211_CHAN_NO_HE))
5915 		return -EOPNOTSUPP;
5916 
5917 	if ((params->eht_cap || params->eht_oper) &&
5918 	    (channel->flags & IEEE80211_CHAN_NO_EHT))
5919 		return -EOPNOTSUPP;
5920 
5921 	return 0;
5922 }
5923 
5924 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
5925 {
5926 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5927 	unsigned int link_id = nl80211_link_id(info->attrs);
5928 	struct net_device *dev = info->user_ptr[1];
5929 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5930 	struct cfg80211_ap_settings *params;
5931 	int err;
5932 
5933 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5934 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5935 		return -EOPNOTSUPP;
5936 
5937 	if (!rdev->ops->start_ap)
5938 		return -EOPNOTSUPP;
5939 
5940 	if (wdev->links[link_id].ap.beacon_interval)
5941 		return -EALREADY;
5942 
5943 	/* these are required for START_AP */
5944 	if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
5945 	    !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
5946 	    !info->attrs[NL80211_ATTR_BEACON_HEAD])
5947 		return -EINVAL;
5948 
5949 	params = kzalloc(sizeof(*params), GFP_KERNEL);
5950 	if (!params)
5951 		return -ENOMEM;
5952 
5953 	err = nl80211_parse_beacon(rdev, info->attrs, &params->beacon,
5954 				   info->extack);
5955 	if (err)
5956 		goto out;
5957 
5958 	params->beacon_interval =
5959 		nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5960 	params->dtim_period =
5961 		nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
5962 
5963 	err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
5964 					   params->beacon_interval);
5965 	if (err)
5966 		goto out;
5967 
5968 	/*
5969 	 * In theory, some of these attributes should be required here
5970 	 * but since they were not used when the command was originally
5971 	 * added, keep them optional for old user space programs to let
5972 	 * them continue to work with drivers that do not need the
5973 	 * additional information -- drivers must check!
5974 	 */
5975 	if (info->attrs[NL80211_ATTR_SSID]) {
5976 		params->ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5977 		params->ssid_len =
5978 			nla_len(info->attrs[NL80211_ATTR_SSID]);
5979 		if (params->ssid_len == 0) {
5980 			err = -EINVAL;
5981 			goto out;
5982 		}
5983 
5984 		if (wdev->u.ap.ssid_len &&
5985 		    (wdev->u.ap.ssid_len != params->ssid_len ||
5986 		     memcmp(wdev->u.ap.ssid, params->ssid, params->ssid_len))) {
5987 			/* require identical SSID for MLO */
5988 			err = -EINVAL;
5989 			goto out;
5990 		}
5991 	} else if (wdev->valid_links) {
5992 		/* require SSID for MLO */
5993 		err = -EINVAL;
5994 		goto out;
5995 	}
5996 
5997 	if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
5998 		params->hidden_ssid = nla_get_u32(
5999 			info->attrs[NL80211_ATTR_HIDDEN_SSID]);
6000 
6001 	params->privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
6002 
6003 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6004 		params->auth_type = nla_get_u32(
6005 			info->attrs[NL80211_ATTR_AUTH_TYPE]);
6006 		if (!nl80211_valid_auth_type(rdev, params->auth_type,
6007 					     NL80211_CMD_START_AP)) {
6008 			err = -EINVAL;
6009 			goto out;
6010 		}
6011 	} else
6012 		params->auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6013 
6014 	err = nl80211_crypto_settings(rdev, info, &params->crypto,
6015 				      NL80211_MAX_NR_CIPHER_SUITES);
6016 	if (err)
6017 		goto out;
6018 
6019 	if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
6020 		if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER)) {
6021 			err = -EOPNOTSUPP;
6022 			goto out;
6023 		}
6024 		params->inactivity_timeout = nla_get_u16(
6025 			info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
6026 	}
6027 
6028 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
6029 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
6030 			err = -EINVAL;
6031 			goto out;
6032 		}
6033 		params->p2p_ctwindow =
6034 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
6035 		if (params->p2p_ctwindow != 0 &&
6036 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN)) {
6037 			err = -EINVAL;
6038 			goto out;
6039 		}
6040 	}
6041 
6042 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
6043 		u8 tmp;
6044 
6045 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
6046 			err = -EINVAL;
6047 			goto out;
6048 		}
6049 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
6050 		params->p2p_opp_ps = tmp;
6051 		if (params->p2p_opp_ps != 0 &&
6052 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS)) {
6053 			err = -EINVAL;
6054 			goto out;
6055 		}
6056 	}
6057 
6058 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6059 		err = nl80211_parse_chandef(rdev, info, &params->chandef);
6060 		if (err)
6061 			goto out;
6062 	} else if (wdev->valid_links) {
6063 		/* with MLD need to specify the channel configuration */
6064 		err = -EINVAL;
6065 		goto out;
6066 	} else if (wdev->u.ap.preset_chandef.chan) {
6067 		params->chandef = wdev->u.ap.preset_chandef;
6068 	} else if (!nl80211_get_ap_channel(rdev, params)) {
6069 		err = -EINVAL;
6070 		goto out;
6071 	}
6072 
6073 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params->chandef,
6074 					   wdev->iftype)) {
6075 		err = -EINVAL;
6076 		goto out;
6077 	}
6078 
6079 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
6080 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
6081 						    NL80211_ATTR_TX_RATES,
6082 						    &params->beacon_rate,
6083 						    dev, false, link_id);
6084 		if (err)
6085 			goto out;
6086 
6087 		err = validate_beacon_tx_rate(rdev, params->chandef.chan->band,
6088 					      &params->beacon_rate);
6089 		if (err)
6090 			goto out;
6091 	}
6092 
6093 	if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
6094 		params->smps_mode =
6095 			nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
6096 		switch (params->smps_mode) {
6097 		case NL80211_SMPS_OFF:
6098 			break;
6099 		case NL80211_SMPS_STATIC:
6100 			if (!(rdev->wiphy.features &
6101 			      NL80211_FEATURE_STATIC_SMPS)) {
6102 				err = -EINVAL;
6103 				goto out;
6104 			}
6105 			break;
6106 		case NL80211_SMPS_DYNAMIC:
6107 			if (!(rdev->wiphy.features &
6108 			      NL80211_FEATURE_DYNAMIC_SMPS)) {
6109 				err = -EINVAL;
6110 				goto out;
6111 			}
6112 			break;
6113 		default:
6114 			err = -EINVAL;
6115 			goto out;
6116 		}
6117 	} else {
6118 		params->smps_mode = NL80211_SMPS_OFF;
6119 	}
6120 
6121 	params->pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
6122 	if (params->pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
6123 		err = -EOPNOTSUPP;
6124 		goto out;
6125 	}
6126 
6127 	if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
6128 		params->acl = parse_acl_data(&rdev->wiphy, info);
6129 		if (IS_ERR(params->acl)) {
6130 			err = PTR_ERR(params->acl);
6131 			params->acl = NULL;
6132 			goto out;
6133 		}
6134 	}
6135 
6136 	params->twt_responder =
6137 		    nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
6138 
6139 	if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
6140 		err = nl80211_parse_he_obss_pd(
6141 					info->attrs[NL80211_ATTR_HE_OBSS_PD],
6142 					&params->he_obss_pd);
6143 		if (err)
6144 			goto out;
6145 	}
6146 
6147 	if (info->attrs[NL80211_ATTR_FILS_DISCOVERY]) {
6148 		err = nl80211_parse_fils_discovery(rdev,
6149 						   info->attrs[NL80211_ATTR_FILS_DISCOVERY],
6150 						   &params->fils_discovery);
6151 		if (err)
6152 			goto out;
6153 	}
6154 
6155 	if (info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP]) {
6156 		err = nl80211_parse_unsol_bcast_probe_resp(
6157 			rdev, info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP],
6158 			&params->unsol_bcast_probe_resp);
6159 		if (err)
6160 			goto out;
6161 	}
6162 
6163 	if (info->attrs[NL80211_ATTR_MBSSID_CONFIG]) {
6164 		err = nl80211_parse_mbssid_config(&rdev->wiphy, dev,
6165 						  info->attrs[NL80211_ATTR_MBSSID_CONFIG],
6166 						  &params->mbssid_config,
6167 						  params->beacon.mbssid_ies ?
6168 							params->beacon.mbssid_ies->cnt :
6169 							0);
6170 		if (err)
6171 			goto out;
6172 	}
6173 
6174 	if (!params->mbssid_config.ema && params->beacon.rnr_ies) {
6175 		err = -EINVAL;
6176 		goto out;
6177 	}
6178 
6179 	err = nl80211_calculate_ap_params(params);
6180 	if (err)
6181 		goto out;
6182 
6183 	err = nl80211_validate_ap_phy_operation(params);
6184 	if (err)
6185 		goto out;
6186 
6187 	if (info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS])
6188 		params->flags = nla_get_u32(
6189 			info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS]);
6190 	else if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
6191 		params->flags |= NL80211_AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
6192 
6193 	if (wdev->conn_owner_nlportid &&
6194 	    info->attrs[NL80211_ATTR_SOCKET_OWNER] &&
6195 	    wdev->conn_owner_nlportid != info->snd_portid) {
6196 		err = -EINVAL;
6197 		goto out;
6198 	}
6199 
6200 	/* FIXME: validate MLO/link-id against driver capabilities */
6201 
6202 	err = rdev_start_ap(rdev, dev, params);
6203 	if (!err) {
6204 		wdev->links[link_id].ap.beacon_interval = params->beacon_interval;
6205 		wdev->links[link_id].ap.chandef = params->chandef;
6206 		wdev->u.ap.ssid_len = params->ssid_len;
6207 		memcpy(wdev->u.ap.ssid, params->ssid,
6208 		       params->ssid_len);
6209 
6210 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
6211 			wdev->conn_owner_nlportid = info->snd_portid;
6212 
6213 		nl80211_send_ap_started(wdev, link_id);
6214 	}
6215 out:
6216 	kfree(params->acl);
6217 	kfree(params->beacon.mbssid_ies);
6218 	if (params->mbssid_config.tx_wdev &&
6219 	    params->mbssid_config.tx_wdev->netdev &&
6220 	    params->mbssid_config.tx_wdev->netdev != dev)
6221 		dev_put(params->mbssid_config.tx_wdev->netdev);
6222 	kfree(params->beacon.rnr_ies);
6223 	kfree(params);
6224 
6225 	return err;
6226 }
6227 
6228 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
6229 {
6230 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6231 	unsigned int link_id = nl80211_link_id(info->attrs);
6232 	struct net_device *dev = info->user_ptr[1];
6233 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6234 	struct cfg80211_ap_update *params;
6235 	struct nlattr *attr;
6236 	int err;
6237 
6238 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6239 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6240 		return -EOPNOTSUPP;
6241 
6242 	if (!rdev->ops->change_beacon)
6243 		return -EOPNOTSUPP;
6244 
6245 	if (!wdev->links[link_id].ap.beacon_interval)
6246 		return -EINVAL;
6247 
6248 	params = kzalloc(sizeof(*params), GFP_KERNEL);
6249 	if (!params)
6250 		return -ENOMEM;
6251 
6252 	err = nl80211_parse_beacon(rdev, info->attrs, &params->beacon,
6253 				   info->extack);
6254 	if (err)
6255 		goto out;
6256 
6257 	attr = info->attrs[NL80211_ATTR_FILS_DISCOVERY];
6258 	if (attr) {
6259 		err = nl80211_parse_fils_discovery(rdev, attr,
6260 						   &params->fils_discovery);
6261 		if (err)
6262 			goto out;
6263 	}
6264 
6265 	attr = info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP];
6266 	if (attr) {
6267 		err = nl80211_parse_unsol_bcast_probe_resp(rdev, attr,
6268 							   &params->unsol_bcast_probe_resp);
6269 		if (err)
6270 			goto out;
6271 	}
6272 
6273 	err = rdev_change_beacon(rdev, dev, params);
6274 
6275 out:
6276 	kfree(params->beacon.mbssid_ies);
6277 	kfree(params->beacon.rnr_ies);
6278 	kfree(params);
6279 	return err;
6280 }
6281 
6282 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
6283 {
6284 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6285 	unsigned int link_id = nl80211_link_id(info->attrs);
6286 	struct net_device *dev = info->user_ptr[1];
6287 
6288 	return cfg80211_stop_ap(rdev, dev, link_id, false);
6289 }
6290 
6291 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
6292 	[NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
6293 	[NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
6294 	[NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
6295 	[NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
6296 	[NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
6297 	[NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
6298 };
6299 
6300 static int parse_station_flags(struct genl_info *info,
6301 			       enum nl80211_iftype iftype,
6302 			       struct station_parameters *params)
6303 {
6304 	struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
6305 	struct nlattr *nla;
6306 	int flag;
6307 
6308 	/*
6309 	 * Try parsing the new attribute first so userspace
6310 	 * can specify both for older kernels.
6311 	 */
6312 	nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
6313 	if (nla) {
6314 		struct nl80211_sta_flag_update *sta_flags;
6315 
6316 		sta_flags = nla_data(nla);
6317 		params->sta_flags_mask = sta_flags->mask;
6318 		params->sta_flags_set = sta_flags->set;
6319 		params->sta_flags_set &= params->sta_flags_mask;
6320 		if ((params->sta_flags_mask |
6321 		     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
6322 			return -EINVAL;
6323 		return 0;
6324 	}
6325 
6326 	/* if present, parse the old attribute */
6327 
6328 	nla = info->attrs[NL80211_ATTR_STA_FLAGS];
6329 	if (!nla)
6330 		return 0;
6331 
6332 	if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
6333 		return -EINVAL;
6334 
6335 	/*
6336 	 * Only allow certain flags for interface types so that
6337 	 * other attributes are silently ignored. Remember that
6338 	 * this is backward compatibility code with old userspace
6339 	 * and shouldn't be hit in other cases anyway.
6340 	 */
6341 	switch (iftype) {
6342 	case NL80211_IFTYPE_AP:
6343 	case NL80211_IFTYPE_AP_VLAN:
6344 	case NL80211_IFTYPE_P2P_GO:
6345 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6346 					 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6347 					 BIT(NL80211_STA_FLAG_WME) |
6348 					 BIT(NL80211_STA_FLAG_MFP);
6349 		break;
6350 	case NL80211_IFTYPE_P2P_CLIENT:
6351 	case NL80211_IFTYPE_STATION:
6352 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6353 					 BIT(NL80211_STA_FLAG_TDLS_PEER);
6354 		break;
6355 	case NL80211_IFTYPE_MESH_POINT:
6356 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6357 					 BIT(NL80211_STA_FLAG_MFP) |
6358 					 BIT(NL80211_STA_FLAG_AUTHORIZED);
6359 		break;
6360 	default:
6361 		return -EINVAL;
6362 	}
6363 
6364 	for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
6365 		if (flags[flag]) {
6366 			params->sta_flags_set |= (1<<flag);
6367 
6368 			/* no longer support new API additions in old API */
6369 			if (flag > NL80211_STA_FLAG_MAX_OLD_API)
6370 				return -EINVAL;
6371 		}
6372 	}
6373 
6374 	return 0;
6375 }
6376 
6377 bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
6378 {
6379 	struct nlattr *rate;
6380 	u32 bitrate;
6381 	u16 bitrate_compat;
6382 	enum nl80211_rate_info rate_flg;
6383 
6384 	rate = nla_nest_start_noflag(msg, attr);
6385 	if (!rate)
6386 		return false;
6387 
6388 	/* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
6389 	bitrate = cfg80211_calculate_bitrate(info);
6390 	/* report 16-bit bitrate only if we can */
6391 	bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
6392 	if (bitrate > 0 &&
6393 	    nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
6394 		return false;
6395 	if (bitrate_compat > 0 &&
6396 	    nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
6397 		return false;
6398 
6399 	switch (info->bw) {
6400 	case RATE_INFO_BW_1:
6401 		rate_flg = NL80211_RATE_INFO_1_MHZ_WIDTH;
6402 		break;
6403 	case RATE_INFO_BW_2:
6404 		rate_flg = NL80211_RATE_INFO_2_MHZ_WIDTH;
6405 		break;
6406 	case RATE_INFO_BW_4:
6407 		rate_flg = NL80211_RATE_INFO_4_MHZ_WIDTH;
6408 		break;
6409 	case RATE_INFO_BW_5:
6410 		rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
6411 		break;
6412 	case RATE_INFO_BW_8:
6413 		rate_flg = NL80211_RATE_INFO_8_MHZ_WIDTH;
6414 		break;
6415 	case RATE_INFO_BW_10:
6416 		rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
6417 		break;
6418 	case RATE_INFO_BW_16:
6419 		rate_flg = NL80211_RATE_INFO_16_MHZ_WIDTH;
6420 		break;
6421 	default:
6422 		WARN_ON(1);
6423 		fallthrough;
6424 	case RATE_INFO_BW_20:
6425 		rate_flg = 0;
6426 		break;
6427 	case RATE_INFO_BW_40:
6428 		rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
6429 		break;
6430 	case RATE_INFO_BW_80:
6431 		rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
6432 		break;
6433 	case RATE_INFO_BW_160:
6434 		rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
6435 		break;
6436 	case RATE_INFO_BW_HE_RU:
6437 		rate_flg = 0;
6438 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
6439 		break;
6440 	case RATE_INFO_BW_320:
6441 		rate_flg = NL80211_RATE_INFO_320_MHZ_WIDTH;
6442 		break;
6443 	case RATE_INFO_BW_EHT_RU:
6444 		rate_flg = 0;
6445 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_EHT_MCS));
6446 		break;
6447 	}
6448 
6449 	if (rate_flg && nla_put_flag(msg, rate_flg))
6450 		return false;
6451 
6452 	if (info->flags & RATE_INFO_FLAGS_MCS) {
6453 		if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
6454 			return false;
6455 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6456 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6457 			return false;
6458 	} else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
6459 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
6460 			return false;
6461 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
6462 			return false;
6463 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6464 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6465 			return false;
6466 	} else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
6467 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
6468 			return false;
6469 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
6470 			return false;
6471 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
6472 			return false;
6473 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
6474 			return false;
6475 		if (info->bw == RATE_INFO_BW_HE_RU &&
6476 		    nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
6477 			       info->he_ru_alloc))
6478 			return false;
6479 	} else if (info->flags & RATE_INFO_FLAGS_S1G_MCS) {
6480 		if (nla_put_u8(msg, NL80211_RATE_INFO_S1G_MCS, info->mcs))
6481 			return false;
6482 		if (nla_put_u8(msg, NL80211_RATE_INFO_S1G_NSS, info->nss))
6483 			return false;
6484 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6485 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6486 			return false;
6487 	} else if (info->flags & RATE_INFO_FLAGS_EHT_MCS) {
6488 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_MCS, info->mcs))
6489 			return false;
6490 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_NSS, info->nss))
6491 			return false;
6492 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_GI, info->eht_gi))
6493 			return false;
6494 		if (info->bw == RATE_INFO_BW_EHT_RU &&
6495 		    nla_put_u8(msg, NL80211_RATE_INFO_EHT_RU_ALLOC,
6496 			       info->eht_ru_alloc))
6497 			return false;
6498 	}
6499 
6500 	nla_nest_end(msg, rate);
6501 	return true;
6502 }
6503 
6504 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
6505 			       int id)
6506 {
6507 	void *attr;
6508 	int i = 0;
6509 
6510 	if (!mask)
6511 		return true;
6512 
6513 	attr = nla_nest_start_noflag(msg, id);
6514 	if (!attr)
6515 		return false;
6516 
6517 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
6518 		if (!(mask & BIT(i)))
6519 			continue;
6520 
6521 		if (nla_put_u8(msg, i, signal[i]))
6522 			return false;
6523 	}
6524 
6525 	nla_nest_end(msg, attr);
6526 
6527 	return true;
6528 }
6529 
6530 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
6531 				u32 seq, int flags,
6532 				struct cfg80211_registered_device *rdev,
6533 				struct net_device *dev,
6534 				const u8 *mac_addr, struct station_info *sinfo)
6535 {
6536 	void *hdr;
6537 	struct nlattr *sinfoattr, *bss_param;
6538 
6539 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
6540 	if (!hdr) {
6541 		cfg80211_sinfo_release_content(sinfo);
6542 		return -1;
6543 	}
6544 
6545 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6546 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
6547 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
6548 		goto nla_put_failure;
6549 
6550 	sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
6551 	if (!sinfoattr)
6552 		goto nla_put_failure;
6553 
6554 #define PUT_SINFO(attr, memb, type) do {				\
6555 	BUILD_BUG_ON(sizeof(type) == sizeof(u64));			\
6556 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
6557 	    nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,		\
6558 			     sinfo->memb))				\
6559 		goto nla_put_failure;					\
6560 	} while (0)
6561 #define PUT_SINFO_U64(attr, memb) do {					\
6562 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
6563 	    nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,		\
6564 			      sinfo->memb, NL80211_STA_INFO_PAD))	\
6565 		goto nla_put_failure;					\
6566 	} while (0)
6567 
6568 	PUT_SINFO(CONNECTED_TIME, connected_time, u32);
6569 	PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
6570 	PUT_SINFO_U64(ASSOC_AT_BOOTTIME, assoc_at);
6571 
6572 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
6573 			     BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
6574 	    nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
6575 			(u32)sinfo->rx_bytes))
6576 		goto nla_put_failure;
6577 
6578 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
6579 			     BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
6580 	    nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
6581 			(u32)sinfo->tx_bytes))
6582 		goto nla_put_failure;
6583 
6584 	PUT_SINFO_U64(RX_BYTES64, rx_bytes);
6585 	PUT_SINFO_U64(TX_BYTES64, tx_bytes);
6586 	PUT_SINFO(LLID, llid, u16);
6587 	PUT_SINFO(PLID, plid, u16);
6588 	PUT_SINFO(PLINK_STATE, plink_state, u8);
6589 	PUT_SINFO_U64(RX_DURATION, rx_duration);
6590 	PUT_SINFO_U64(TX_DURATION, tx_duration);
6591 
6592 	if (wiphy_ext_feature_isset(&rdev->wiphy,
6593 				    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
6594 		PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
6595 
6596 	switch (rdev->wiphy.signal_type) {
6597 	case CFG80211_SIGNAL_TYPE_MBM:
6598 		PUT_SINFO(SIGNAL, signal, u8);
6599 		PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
6600 		break;
6601 	default:
6602 		break;
6603 	}
6604 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
6605 		if (!nl80211_put_signal(msg, sinfo->chains,
6606 					sinfo->chain_signal,
6607 					NL80211_STA_INFO_CHAIN_SIGNAL))
6608 			goto nla_put_failure;
6609 	}
6610 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
6611 		if (!nl80211_put_signal(msg, sinfo->chains,
6612 					sinfo->chain_signal_avg,
6613 					NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
6614 			goto nla_put_failure;
6615 	}
6616 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
6617 		if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
6618 					  NL80211_STA_INFO_TX_BITRATE))
6619 			goto nla_put_failure;
6620 	}
6621 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
6622 		if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
6623 					  NL80211_STA_INFO_RX_BITRATE))
6624 			goto nla_put_failure;
6625 	}
6626 
6627 	PUT_SINFO(RX_PACKETS, rx_packets, u32);
6628 	PUT_SINFO(TX_PACKETS, tx_packets, u32);
6629 	PUT_SINFO(TX_RETRIES, tx_retries, u32);
6630 	PUT_SINFO(TX_FAILED, tx_failed, u32);
6631 	PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
6632 	PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
6633 	PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
6634 	PUT_SINFO(LOCAL_PM, local_pm, u32);
6635 	PUT_SINFO(PEER_PM, peer_pm, u32);
6636 	PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
6637 	PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
6638 	PUT_SINFO(CONNECTED_TO_AS, connected_to_as, u8);
6639 
6640 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
6641 		bss_param = nla_nest_start_noflag(msg,
6642 						  NL80211_STA_INFO_BSS_PARAM);
6643 		if (!bss_param)
6644 			goto nla_put_failure;
6645 
6646 		if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
6647 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
6648 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
6649 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
6650 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
6651 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
6652 		    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
6653 			       sinfo->bss_param.dtim_period) ||
6654 		    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
6655 				sinfo->bss_param.beacon_interval))
6656 			goto nla_put_failure;
6657 
6658 		nla_nest_end(msg, bss_param);
6659 	}
6660 	if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
6661 	    nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
6662 		    sizeof(struct nl80211_sta_flag_update),
6663 		    &sinfo->sta_flags))
6664 		goto nla_put_failure;
6665 
6666 	PUT_SINFO_U64(T_OFFSET, t_offset);
6667 	PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
6668 	PUT_SINFO_U64(BEACON_RX, rx_beacon);
6669 	PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
6670 	PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
6671 	PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
6672 	if (wiphy_ext_feature_isset(&rdev->wiphy,
6673 				    NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
6674 		PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
6675 		PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
6676 	}
6677 
6678 #undef PUT_SINFO
6679 #undef PUT_SINFO_U64
6680 
6681 	if (sinfo->pertid) {
6682 		struct nlattr *tidsattr;
6683 		int tid;
6684 
6685 		tidsattr = nla_nest_start_noflag(msg,
6686 						 NL80211_STA_INFO_TID_STATS);
6687 		if (!tidsattr)
6688 			goto nla_put_failure;
6689 
6690 		for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
6691 			struct cfg80211_tid_stats *tidstats;
6692 			struct nlattr *tidattr;
6693 
6694 			tidstats = &sinfo->pertid[tid];
6695 
6696 			if (!tidstats->filled)
6697 				continue;
6698 
6699 			tidattr = nla_nest_start_noflag(msg, tid + 1);
6700 			if (!tidattr)
6701 				goto nla_put_failure;
6702 
6703 #define PUT_TIDVAL_U64(attr, memb) do {					\
6704 	if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&	\
6705 	    nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,		\
6706 			      tidstats->memb, NL80211_TID_STATS_PAD))	\
6707 		goto nla_put_failure;					\
6708 	} while (0)
6709 
6710 			PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
6711 			PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
6712 			PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
6713 			PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
6714 
6715 #undef PUT_TIDVAL_U64
6716 			if ((tidstats->filled &
6717 			     BIT(NL80211_TID_STATS_TXQ_STATS)) &&
6718 			    !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
6719 						   NL80211_TID_STATS_TXQ_STATS))
6720 				goto nla_put_failure;
6721 
6722 			nla_nest_end(msg, tidattr);
6723 		}
6724 
6725 		nla_nest_end(msg, tidsattr);
6726 	}
6727 
6728 	nla_nest_end(msg, sinfoattr);
6729 
6730 	if (sinfo->assoc_req_ies_len &&
6731 	    nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
6732 		    sinfo->assoc_req_ies))
6733 		goto nla_put_failure;
6734 
6735 	if (sinfo->assoc_resp_ies_len &&
6736 	    nla_put(msg, NL80211_ATTR_RESP_IE, sinfo->assoc_resp_ies_len,
6737 		    sinfo->assoc_resp_ies))
6738 		goto nla_put_failure;
6739 
6740 	if (sinfo->mlo_params_valid) {
6741 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
6742 			       sinfo->assoc_link_id))
6743 			goto nla_put_failure;
6744 
6745 		if (!is_zero_ether_addr(sinfo->mld_addr) &&
6746 		    nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
6747 			    sinfo->mld_addr))
6748 			goto nla_put_failure;
6749 	}
6750 
6751 	cfg80211_sinfo_release_content(sinfo);
6752 	genlmsg_end(msg, hdr);
6753 	return 0;
6754 
6755  nla_put_failure:
6756 	cfg80211_sinfo_release_content(sinfo);
6757 	genlmsg_cancel(msg, hdr);
6758 	return -EMSGSIZE;
6759 }
6760 
6761 static int nl80211_dump_station(struct sk_buff *skb,
6762 				struct netlink_callback *cb)
6763 {
6764 	struct station_info sinfo;
6765 	struct cfg80211_registered_device *rdev;
6766 	struct wireless_dev *wdev;
6767 	u8 mac_addr[ETH_ALEN];
6768 	int sta_idx = cb->args[2];
6769 	int err;
6770 
6771 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
6772 	if (err)
6773 		return err;
6774 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
6775 	__acquire(&rdev->wiphy.mtx);
6776 
6777 	if (!wdev->netdev) {
6778 		err = -EINVAL;
6779 		goto out_err;
6780 	}
6781 
6782 	if (!rdev->ops->dump_station) {
6783 		err = -EOPNOTSUPP;
6784 		goto out_err;
6785 	}
6786 
6787 	while (1) {
6788 		memset(&sinfo, 0, sizeof(sinfo));
6789 		err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
6790 					mac_addr, &sinfo);
6791 		if (err == -ENOENT)
6792 			break;
6793 		if (err)
6794 			goto out_err;
6795 
6796 		if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
6797 				NETLINK_CB(cb->skb).portid,
6798 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
6799 				rdev, wdev->netdev, mac_addr,
6800 				&sinfo) < 0)
6801 			goto out;
6802 
6803 		sta_idx++;
6804 	}
6805 
6806  out:
6807 	cb->args[2] = sta_idx;
6808 	err = skb->len;
6809  out_err:
6810 	wiphy_unlock(&rdev->wiphy);
6811 
6812 	return err;
6813 }
6814 
6815 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
6816 {
6817 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6818 	struct net_device *dev = info->user_ptr[1];
6819 	struct station_info sinfo;
6820 	struct sk_buff *msg;
6821 	u8 *mac_addr = NULL;
6822 	int err;
6823 
6824 	memset(&sinfo, 0, sizeof(sinfo));
6825 
6826 	if (!info->attrs[NL80211_ATTR_MAC])
6827 		return -EINVAL;
6828 
6829 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6830 
6831 	if (!rdev->ops->get_station)
6832 		return -EOPNOTSUPP;
6833 
6834 	err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
6835 	if (err)
6836 		return err;
6837 
6838 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6839 	if (!msg) {
6840 		cfg80211_sinfo_release_content(&sinfo);
6841 		return -ENOMEM;
6842 	}
6843 
6844 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
6845 				 info->snd_portid, info->snd_seq, 0,
6846 				 rdev, dev, mac_addr, &sinfo) < 0) {
6847 		nlmsg_free(msg);
6848 		return -ENOBUFS;
6849 	}
6850 
6851 	return genlmsg_reply(msg, info);
6852 }
6853 
6854 int cfg80211_check_station_change(struct wiphy *wiphy,
6855 				  struct station_parameters *params,
6856 				  enum cfg80211_station_type statype)
6857 {
6858 	if (params->listen_interval != -1 &&
6859 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6860 		return -EINVAL;
6861 
6862 	if (params->support_p2p_ps != -1 &&
6863 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6864 		return -EINVAL;
6865 
6866 	if (params->aid &&
6867 	    !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
6868 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6869 		return -EINVAL;
6870 
6871 	/* When you run into this, adjust the code below for the new flag */
6872 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 8);
6873 
6874 	switch (statype) {
6875 	case CFG80211_STA_MESH_PEER_KERNEL:
6876 	case CFG80211_STA_MESH_PEER_USER:
6877 		/*
6878 		 * No ignoring the TDLS flag here -- the userspace mesh
6879 		 * code doesn't have the bug of including TDLS in the
6880 		 * mask everywhere.
6881 		 */
6882 		if (params->sta_flags_mask &
6883 				~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6884 				  BIT(NL80211_STA_FLAG_MFP) |
6885 				  BIT(NL80211_STA_FLAG_AUTHORIZED)))
6886 			return -EINVAL;
6887 		break;
6888 	case CFG80211_STA_TDLS_PEER_SETUP:
6889 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6890 		if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6891 			return -EINVAL;
6892 		/* ignore since it can't change */
6893 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6894 		break;
6895 	default:
6896 		/* disallow mesh-specific things */
6897 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
6898 			return -EINVAL;
6899 		if (params->local_pm)
6900 			return -EINVAL;
6901 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6902 			return -EINVAL;
6903 	}
6904 
6905 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6906 	    statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
6907 		/* TDLS can't be set, ... */
6908 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
6909 			return -EINVAL;
6910 		/*
6911 		 * ... but don't bother the driver with it. This works around
6912 		 * a hostapd/wpa_supplicant issue -- it always includes the
6913 		 * TLDS_PEER flag in the mask even for AP mode.
6914 		 */
6915 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6916 	}
6917 
6918 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6919 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6920 		/* reject other things that can't change */
6921 		if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
6922 			return -EINVAL;
6923 		if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
6924 			return -EINVAL;
6925 		if (params->link_sta_params.supported_rates)
6926 			return -EINVAL;
6927 		if (params->ext_capab || params->link_sta_params.ht_capa ||
6928 		    params->link_sta_params.vht_capa ||
6929 		    params->link_sta_params.he_capa ||
6930 		    params->link_sta_params.eht_capa)
6931 			return -EINVAL;
6932 		if (params->sta_flags_mask & BIT(NL80211_STA_FLAG_SPP_AMSDU))
6933 			return -EINVAL;
6934 	}
6935 
6936 	if (statype != CFG80211_STA_AP_CLIENT &&
6937 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6938 		if (params->vlan)
6939 			return -EINVAL;
6940 	}
6941 
6942 	switch (statype) {
6943 	case CFG80211_STA_AP_MLME_CLIENT:
6944 		/* Use this only for authorizing/unauthorizing a station */
6945 		if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
6946 			return -EOPNOTSUPP;
6947 		break;
6948 	case CFG80211_STA_AP_CLIENT:
6949 	case CFG80211_STA_AP_CLIENT_UNASSOC:
6950 		/* accept only the listed bits */
6951 		if (params->sta_flags_mask &
6952 				~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6953 				  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6954 				  BIT(NL80211_STA_FLAG_ASSOCIATED) |
6955 				  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6956 				  BIT(NL80211_STA_FLAG_WME) |
6957 				  BIT(NL80211_STA_FLAG_MFP) |
6958 				  BIT(NL80211_STA_FLAG_SPP_AMSDU)))
6959 			return -EINVAL;
6960 
6961 		/* but authenticated/associated only if driver handles it */
6962 		if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
6963 		    params->sta_flags_mask &
6964 				(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6965 				 BIT(NL80211_STA_FLAG_ASSOCIATED)))
6966 			return -EINVAL;
6967 		break;
6968 	case CFG80211_STA_IBSS:
6969 	case CFG80211_STA_AP_STA:
6970 		/* reject any changes other than AUTHORIZED */
6971 		if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
6972 			return -EINVAL;
6973 		break;
6974 	case CFG80211_STA_TDLS_PEER_SETUP:
6975 		/* reject any changes other than AUTHORIZED or WME */
6976 		if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6977 					       BIT(NL80211_STA_FLAG_WME)))
6978 			return -EINVAL;
6979 		/* force (at least) rates when authorizing */
6980 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
6981 		    !params->link_sta_params.supported_rates)
6982 			return -EINVAL;
6983 		break;
6984 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6985 		/* reject any changes */
6986 		return -EINVAL;
6987 	case CFG80211_STA_MESH_PEER_KERNEL:
6988 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6989 			return -EINVAL;
6990 		break;
6991 	case CFG80211_STA_MESH_PEER_USER:
6992 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
6993 		    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
6994 			return -EINVAL;
6995 		break;
6996 	}
6997 
6998 	/*
6999 	 * Older kernel versions ignored this attribute entirely, so don't
7000 	 * reject attempts to update it but mark it as unused instead so the
7001 	 * driver won't look at the data.
7002 	 */
7003 	if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
7004 	    statype != CFG80211_STA_TDLS_PEER_SETUP)
7005 		params->link_sta_params.opmode_notif_used = false;
7006 
7007 	return 0;
7008 }
7009 EXPORT_SYMBOL(cfg80211_check_station_change);
7010 
7011 /*
7012  * Get vlan interface making sure it is running and on the right wiphy.
7013  */
7014 static struct net_device *get_vlan(struct genl_info *info,
7015 				   struct cfg80211_registered_device *rdev)
7016 {
7017 	struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
7018 	struct net_device *v;
7019 	int ret;
7020 
7021 	if (!vlanattr)
7022 		return NULL;
7023 
7024 	v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
7025 	if (!v)
7026 		return ERR_PTR(-ENODEV);
7027 
7028 	if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
7029 		ret = -EINVAL;
7030 		goto error;
7031 	}
7032 
7033 	if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
7034 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
7035 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
7036 		ret = -EINVAL;
7037 		goto error;
7038 	}
7039 
7040 	if (!netif_running(v)) {
7041 		ret = -ENETDOWN;
7042 		goto error;
7043 	}
7044 
7045 	return v;
7046  error:
7047 	dev_put(v);
7048 	return ERR_PTR(ret);
7049 }
7050 
7051 static int nl80211_parse_sta_wme(struct genl_info *info,
7052 				 struct station_parameters *params)
7053 {
7054 	struct nlattr *tb[NL80211_STA_WME_MAX + 1];
7055 	struct nlattr *nla;
7056 	int err;
7057 
7058 	/* parse WME attributes if present */
7059 	if (!info->attrs[NL80211_ATTR_STA_WME])
7060 		return 0;
7061 
7062 	nla = info->attrs[NL80211_ATTR_STA_WME];
7063 	err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
7064 					  nl80211_sta_wme_policy,
7065 					  info->extack);
7066 	if (err)
7067 		return err;
7068 
7069 	if (tb[NL80211_STA_WME_UAPSD_QUEUES])
7070 		params->uapsd_queues = nla_get_u8(
7071 			tb[NL80211_STA_WME_UAPSD_QUEUES]);
7072 	if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
7073 		return -EINVAL;
7074 
7075 	if (tb[NL80211_STA_WME_MAX_SP])
7076 		params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
7077 
7078 	if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
7079 		return -EINVAL;
7080 
7081 	params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
7082 
7083 	return 0;
7084 }
7085 
7086 static int nl80211_parse_sta_channel_info(struct genl_info *info,
7087 				      struct station_parameters *params)
7088 {
7089 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
7090 		params->supported_channels =
7091 		     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
7092 		params->supported_channels_len =
7093 		     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
7094 		/*
7095 		 * Need to include at least one (first channel, number of
7096 		 * channels) tuple for each subband (checked in policy),
7097 		 * and must have proper tuples for the rest of the data as well.
7098 		 */
7099 		if (params->supported_channels_len % 2)
7100 			return -EINVAL;
7101 	}
7102 
7103 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
7104 		params->supported_oper_classes =
7105 		 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
7106 		params->supported_oper_classes_len =
7107 		  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
7108 	}
7109 	return 0;
7110 }
7111 
7112 static int nl80211_set_station_tdls(struct genl_info *info,
7113 				    struct station_parameters *params)
7114 {
7115 	int err;
7116 	/* Dummy STA entry gets updated once the peer capabilities are known */
7117 	if (info->attrs[NL80211_ATTR_PEER_AID])
7118 		params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
7119 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
7120 		params->link_sta_params.ht_capa =
7121 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
7122 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
7123 		params->link_sta_params.vht_capa =
7124 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
7125 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
7126 		params->link_sta_params.he_capa =
7127 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7128 		params->link_sta_params.he_capa_len =
7129 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7130 
7131 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
7132 			params->link_sta_params.eht_capa =
7133 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7134 			params->link_sta_params.eht_capa_len =
7135 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7136 
7137 			if (!ieee80211_eht_capa_size_ok((const u8 *)params->link_sta_params.he_capa,
7138 							(const u8 *)params->link_sta_params.eht_capa,
7139 							params->link_sta_params.eht_capa_len,
7140 							false))
7141 				return -EINVAL;
7142 		}
7143 	}
7144 
7145 	err = nl80211_parse_sta_channel_info(info, params);
7146 	if (err)
7147 		return err;
7148 
7149 	return nl80211_parse_sta_wme(info, params);
7150 }
7151 
7152 static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
7153 					     struct sta_txpwr *txpwr,
7154 					     bool *txpwr_set)
7155 {
7156 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7157 	int idx;
7158 
7159 	if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
7160 		if (!rdev->ops->set_tx_power ||
7161 		    !wiphy_ext_feature_isset(&rdev->wiphy,
7162 					 NL80211_EXT_FEATURE_STA_TX_PWR))
7163 			return -EOPNOTSUPP;
7164 
7165 		idx = NL80211_ATTR_STA_TX_POWER_SETTING;
7166 		txpwr->type = nla_get_u8(info->attrs[idx]);
7167 
7168 		if (txpwr->type == NL80211_TX_POWER_LIMITED) {
7169 			idx = NL80211_ATTR_STA_TX_POWER;
7170 
7171 			if (info->attrs[idx])
7172 				txpwr->power = nla_get_s16(info->attrs[idx]);
7173 			else
7174 				return -EINVAL;
7175 		}
7176 
7177 		*txpwr_set = true;
7178 	} else {
7179 		*txpwr_set = false;
7180 	}
7181 
7182 	return 0;
7183 }
7184 
7185 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
7186 {
7187 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7188 	struct net_device *dev = info->user_ptr[1];
7189 	struct station_parameters params;
7190 	u8 *mac_addr;
7191 	int err;
7192 
7193 	memset(&params, 0, sizeof(params));
7194 
7195 	if (!rdev->ops->change_station)
7196 		return -EOPNOTSUPP;
7197 
7198 	/*
7199 	 * AID and listen_interval properties can be set only for unassociated
7200 	 * station. Include these parameters here and will check them in
7201 	 * cfg80211_check_station_change().
7202 	 */
7203 	if (info->attrs[NL80211_ATTR_STA_AID])
7204 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
7205 
7206 	if (info->attrs[NL80211_ATTR_VLAN_ID])
7207 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
7208 
7209 	if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
7210 		params.listen_interval =
7211 		     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
7212 	else
7213 		params.listen_interval = -1;
7214 
7215 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
7216 		params.support_p2p_ps =
7217 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
7218 	else
7219 		params.support_p2p_ps = -1;
7220 
7221 	if (!info->attrs[NL80211_ATTR_MAC])
7222 		return -EINVAL;
7223 
7224 	params.link_sta_params.link_id =
7225 		nl80211_link_id_or_invalid(info->attrs);
7226 
7227 	if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7228 		/* If MLD_ADDR attribute is set then this is an MLD station
7229 		 * and the MLD_ADDR attribute holds the MLD address and the
7230 		 * MAC attribute holds for the LINK address.
7231 		 * In that case, the link_id is also expected to be valid.
7232 		 */
7233 		if (params.link_sta_params.link_id < 0)
7234 			return -EINVAL;
7235 
7236 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7237 		params.link_sta_params.mld_mac = mac_addr;
7238 		params.link_sta_params.link_mac =
7239 			nla_data(info->attrs[NL80211_ATTR_MAC]);
7240 		if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7241 			return -EINVAL;
7242 	} else {
7243 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7244 	}
7245 
7246 
7247 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
7248 		params.link_sta_params.supported_rates =
7249 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7250 		params.link_sta_params.supported_rates_len =
7251 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7252 	}
7253 
7254 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7255 		params.capability =
7256 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7257 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7258 	}
7259 
7260 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7261 		params.ext_capab =
7262 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7263 		params.ext_capab_len =
7264 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7265 	}
7266 
7267 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7268 		return -EINVAL;
7269 
7270 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7271 		params.plink_action =
7272 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7273 
7274 	if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
7275 		params.plink_state =
7276 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
7277 		if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
7278 			params.peer_aid = nla_get_u16(
7279 				info->attrs[NL80211_ATTR_MESH_PEER_AID]);
7280 		params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
7281 	}
7282 
7283 	if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
7284 		params.local_pm = nla_get_u32(
7285 			info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
7286 
7287 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7288 		params.link_sta_params.opmode_notif_used = true;
7289 		params.link_sta_params.opmode_notif =
7290 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7291 	}
7292 
7293 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7294 		params.link_sta_params.he_6ghz_capa =
7295 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7296 
7297 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7298 		params.airtime_weight =
7299 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7300 
7301 	if (params.airtime_weight &&
7302 	    !wiphy_ext_feature_isset(&rdev->wiphy,
7303 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7304 		return -EOPNOTSUPP;
7305 
7306 	err = nl80211_parse_sta_txpower_setting(info,
7307 						&params.link_sta_params.txpwr,
7308 						&params.link_sta_params.txpwr_set);
7309 	if (err)
7310 		return err;
7311 
7312 	/* Include parameters for TDLS peer (will check later) */
7313 	err = nl80211_set_station_tdls(info, &params);
7314 	if (err)
7315 		return err;
7316 
7317 	params.vlan = get_vlan(info, rdev);
7318 	if (IS_ERR(params.vlan))
7319 		return PTR_ERR(params.vlan);
7320 
7321 	switch (dev->ieee80211_ptr->iftype) {
7322 	case NL80211_IFTYPE_AP:
7323 	case NL80211_IFTYPE_AP_VLAN:
7324 	case NL80211_IFTYPE_P2P_GO:
7325 	case NL80211_IFTYPE_P2P_CLIENT:
7326 	case NL80211_IFTYPE_STATION:
7327 	case NL80211_IFTYPE_ADHOC:
7328 	case NL80211_IFTYPE_MESH_POINT:
7329 		break;
7330 	default:
7331 		err = -EOPNOTSUPP;
7332 		goto out_put_vlan;
7333 	}
7334 
7335 	/* driver will call cfg80211_check_station_change() */
7336 	err = rdev_change_station(rdev, dev, mac_addr, &params);
7337 
7338  out_put_vlan:
7339 	dev_put(params.vlan);
7340 
7341 	return err;
7342 }
7343 
7344 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
7345 {
7346 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7347 	int err;
7348 	struct net_device *dev = info->user_ptr[1];
7349 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7350 	struct station_parameters params;
7351 	u8 *mac_addr = NULL;
7352 	u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
7353 			 BIT(NL80211_STA_FLAG_ASSOCIATED);
7354 
7355 	memset(&params, 0, sizeof(params));
7356 
7357 	if (!rdev->ops->add_station)
7358 		return -EOPNOTSUPP;
7359 
7360 	if (!info->attrs[NL80211_ATTR_MAC])
7361 		return -EINVAL;
7362 
7363 	if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
7364 		return -EINVAL;
7365 
7366 	if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
7367 		return -EINVAL;
7368 
7369 	if (!info->attrs[NL80211_ATTR_STA_AID] &&
7370 	    !info->attrs[NL80211_ATTR_PEER_AID])
7371 		return -EINVAL;
7372 
7373 	params.link_sta_params.link_id =
7374 		nl80211_link_id_or_invalid(info->attrs);
7375 
7376 	if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7377 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7378 		params.link_sta_params.mld_mac = mac_addr;
7379 		params.link_sta_params.link_mac =
7380 			nla_data(info->attrs[NL80211_ATTR_MAC]);
7381 		if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7382 			return -EINVAL;
7383 	} else {
7384 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7385 	}
7386 
7387 	params.link_sta_params.supported_rates =
7388 		nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7389 	params.link_sta_params.supported_rates_len =
7390 		nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7391 	params.listen_interval =
7392 		nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
7393 
7394 	if (info->attrs[NL80211_ATTR_VLAN_ID])
7395 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
7396 
7397 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
7398 		params.support_p2p_ps =
7399 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
7400 	} else {
7401 		/*
7402 		 * if not specified, assume it's supported for P2P GO interface,
7403 		 * and is NOT supported for AP interface
7404 		 */
7405 		params.support_p2p_ps =
7406 			dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
7407 	}
7408 
7409 	if (info->attrs[NL80211_ATTR_PEER_AID])
7410 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
7411 	else
7412 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
7413 
7414 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7415 		params.capability =
7416 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7417 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7418 	}
7419 
7420 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7421 		params.ext_capab =
7422 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7423 		params.ext_capab_len =
7424 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7425 	}
7426 
7427 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
7428 		params.link_sta_params.ht_capa =
7429 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
7430 
7431 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
7432 		params.link_sta_params.vht_capa =
7433 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
7434 
7435 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
7436 		params.link_sta_params.he_capa =
7437 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7438 		params.link_sta_params.he_capa_len =
7439 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7440 
7441 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
7442 			params.link_sta_params.eht_capa =
7443 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7444 			params.link_sta_params.eht_capa_len =
7445 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7446 
7447 			if (!ieee80211_eht_capa_size_ok((const u8 *)params.link_sta_params.he_capa,
7448 							(const u8 *)params.link_sta_params.eht_capa,
7449 							params.link_sta_params.eht_capa_len,
7450 							false))
7451 				return -EINVAL;
7452 		}
7453 	}
7454 
7455 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7456 		params.link_sta_params.he_6ghz_capa =
7457 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7458 
7459 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7460 		params.link_sta_params.opmode_notif_used = true;
7461 		params.link_sta_params.opmode_notif =
7462 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7463 	}
7464 
7465 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7466 		params.plink_action =
7467 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7468 
7469 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7470 		params.airtime_weight =
7471 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7472 
7473 	if (params.airtime_weight &&
7474 	    !wiphy_ext_feature_isset(&rdev->wiphy,
7475 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7476 		return -EOPNOTSUPP;
7477 
7478 	err = nl80211_parse_sta_txpower_setting(info,
7479 						&params.link_sta_params.txpwr,
7480 						&params.link_sta_params.txpwr_set);
7481 	if (err)
7482 		return err;
7483 
7484 	err = nl80211_parse_sta_channel_info(info, &params);
7485 	if (err)
7486 		return err;
7487 
7488 	err = nl80211_parse_sta_wme(info, &params);
7489 	if (err)
7490 		return err;
7491 
7492 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7493 		return -EINVAL;
7494 
7495 	/* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
7496 	 * as userspace might just pass through the capabilities from the IEs
7497 	 * directly, rather than enforcing this restriction and returning an
7498 	 * error in this case.
7499 	 */
7500 	if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
7501 		params.link_sta_params.ht_capa = NULL;
7502 		params.link_sta_params.vht_capa = NULL;
7503 
7504 		/* HE and EHT require WME */
7505 		if (params.link_sta_params.he_capa_len ||
7506 		    params.link_sta_params.he_6ghz_capa ||
7507 		    params.link_sta_params.eht_capa_len)
7508 			return -EINVAL;
7509 	}
7510 
7511 	/* Ensure that HT/VHT capabilities are not set for 6 GHz HE STA */
7512 	if (params.link_sta_params.he_6ghz_capa &&
7513 	    (params.link_sta_params.ht_capa || params.link_sta_params.vht_capa))
7514 		return -EINVAL;
7515 
7516 	/* When you run into this, adjust the code below for the new flag */
7517 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 8);
7518 
7519 	switch (dev->ieee80211_ptr->iftype) {
7520 	case NL80211_IFTYPE_AP:
7521 	case NL80211_IFTYPE_AP_VLAN:
7522 	case NL80211_IFTYPE_P2P_GO:
7523 		/* ignore WME attributes if iface/sta is not capable */
7524 		if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
7525 		    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
7526 			params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7527 
7528 		/* TDLS peers cannot be added */
7529 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7530 		    info->attrs[NL80211_ATTR_PEER_AID])
7531 			return -EINVAL;
7532 		/* but don't bother the driver with it */
7533 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
7534 
7535 		/* allow authenticated/associated only if driver handles it */
7536 		if (!(rdev->wiphy.features &
7537 				NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
7538 		    params.sta_flags_mask & auth_assoc)
7539 			return -EINVAL;
7540 
7541 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
7542 					     NL80211_EXT_FEATURE_SPP_AMSDU_SUPPORT) &&
7543 		    params.sta_flags_mask & BIT(NL80211_STA_FLAG_SPP_AMSDU))
7544 			return -EINVAL;
7545 
7546 		/* Older userspace, or userspace wanting to be compatible with
7547 		 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
7548 		 * and assoc flags in the mask, but assumes the station will be
7549 		 * added as associated anyway since this was the required driver
7550 		 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
7551 		 * introduced.
7552 		 * In order to not bother drivers with this quirk in the API
7553 		 * set the flags in both the mask and set for new stations in
7554 		 * this case.
7555 		 */
7556 		if (!(params.sta_flags_mask & auth_assoc)) {
7557 			params.sta_flags_mask |= auth_assoc;
7558 			params.sta_flags_set |= auth_assoc;
7559 		}
7560 
7561 		/* must be last in here for error handling */
7562 		params.vlan = get_vlan(info, rdev);
7563 		if (IS_ERR(params.vlan))
7564 			return PTR_ERR(params.vlan);
7565 		break;
7566 	case NL80211_IFTYPE_MESH_POINT:
7567 		/* ignore uAPSD data */
7568 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7569 
7570 		/* associated is disallowed */
7571 		if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
7572 			return -EINVAL;
7573 		/* TDLS peers cannot be added */
7574 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7575 		    info->attrs[NL80211_ATTR_PEER_AID])
7576 			return -EINVAL;
7577 		break;
7578 	case NL80211_IFTYPE_STATION:
7579 	case NL80211_IFTYPE_P2P_CLIENT:
7580 		/* ignore uAPSD data */
7581 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7582 
7583 		/* these are disallowed */
7584 		if (params.sta_flags_mask &
7585 				(BIT(NL80211_STA_FLAG_ASSOCIATED) |
7586 				 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
7587 			return -EINVAL;
7588 		/* Only TDLS peers can be added */
7589 		if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
7590 			return -EINVAL;
7591 		/* Can only add if TDLS ... */
7592 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
7593 			return -EOPNOTSUPP;
7594 		/* ... with external setup is supported */
7595 		if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
7596 			return -EOPNOTSUPP;
7597 		/*
7598 		 * Older wpa_supplicant versions always mark the TDLS peer
7599 		 * as authorized, but it shouldn't yet be.
7600 		 */
7601 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
7602 		break;
7603 	default:
7604 		return -EOPNOTSUPP;
7605 	}
7606 
7607 	/* be aware of params.vlan when changing code here */
7608 
7609 	if (wdev->valid_links) {
7610 		if (params.link_sta_params.link_id < 0) {
7611 			err = -EINVAL;
7612 			goto out;
7613 		}
7614 		if (!(wdev->valid_links & BIT(params.link_sta_params.link_id))) {
7615 			err = -ENOLINK;
7616 			goto out;
7617 		}
7618 	} else {
7619 		if (params.link_sta_params.link_id >= 0) {
7620 			err = -EINVAL;
7621 			goto out;
7622 		}
7623 	}
7624 	err = rdev_add_station(rdev, dev, mac_addr, &params);
7625 out:
7626 	dev_put(params.vlan);
7627 	return err;
7628 }
7629 
7630 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
7631 {
7632 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7633 	struct net_device *dev = info->user_ptr[1];
7634 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7635 	struct station_del_parameters params;
7636 	int link_id = nl80211_link_id_or_invalid(info->attrs);
7637 
7638 	memset(&params, 0, sizeof(params));
7639 
7640 	if (info->attrs[NL80211_ATTR_MAC])
7641 		params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
7642 
7643 	switch (wdev->iftype) {
7644 	case NL80211_IFTYPE_AP:
7645 	case NL80211_IFTYPE_AP_VLAN:
7646 	case NL80211_IFTYPE_MESH_POINT:
7647 	case NL80211_IFTYPE_P2P_GO:
7648 		/* always accept these */
7649 		break;
7650 	case NL80211_IFTYPE_ADHOC:
7651 		/* conditionally accept */
7652 		if (wiphy_ext_feature_isset(&rdev->wiphy,
7653 					    NL80211_EXT_FEATURE_DEL_IBSS_STA))
7654 			break;
7655 		return -EINVAL;
7656 	default:
7657 		return -EINVAL;
7658 	}
7659 
7660 	if (!rdev->ops->del_station)
7661 		return -EOPNOTSUPP;
7662 
7663 	if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
7664 		params.subtype =
7665 			nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
7666 		if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
7667 		    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
7668 			return -EINVAL;
7669 	} else {
7670 		/* Default to Deauthentication frame */
7671 		params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
7672 	}
7673 
7674 	if (info->attrs[NL80211_ATTR_REASON_CODE]) {
7675 		params.reason_code =
7676 			nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7677 		if (params.reason_code == 0)
7678 			return -EINVAL; /* 0 is reserved */
7679 	} else {
7680 		/* Default to reason code 2 */
7681 		params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
7682 	}
7683 
7684 	/* Link ID not expected in case of non-ML operation */
7685 	if (!wdev->valid_links && link_id != -1)
7686 		return -EINVAL;
7687 
7688 	/* If given, a valid link ID should be passed during MLO */
7689 	if (wdev->valid_links && link_id >= 0 &&
7690 	    !(wdev->valid_links & BIT(link_id)))
7691 		return -EINVAL;
7692 
7693 	params.link_id = link_id;
7694 
7695 	return rdev_del_station(rdev, dev, &params);
7696 }
7697 
7698 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
7699 				int flags, struct net_device *dev,
7700 				u8 *dst, u8 *next_hop,
7701 				struct mpath_info *pinfo)
7702 {
7703 	void *hdr;
7704 	struct nlattr *pinfoattr;
7705 
7706 	hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
7707 	if (!hdr)
7708 		return -1;
7709 
7710 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7711 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7712 	    nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
7713 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
7714 		goto nla_put_failure;
7715 
7716 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
7717 	if (!pinfoattr)
7718 		goto nla_put_failure;
7719 	if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
7720 	    nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
7721 			pinfo->frame_qlen))
7722 		goto nla_put_failure;
7723 	if (((pinfo->filled & MPATH_INFO_SN) &&
7724 	     nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
7725 	    ((pinfo->filled & MPATH_INFO_METRIC) &&
7726 	     nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
7727 			 pinfo->metric)) ||
7728 	    ((pinfo->filled & MPATH_INFO_EXPTIME) &&
7729 	     nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
7730 			 pinfo->exptime)) ||
7731 	    ((pinfo->filled & MPATH_INFO_FLAGS) &&
7732 	     nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
7733 			pinfo->flags)) ||
7734 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
7735 	     nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
7736 			 pinfo->discovery_timeout)) ||
7737 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
7738 	     nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
7739 			pinfo->discovery_retries)) ||
7740 	    ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
7741 	     nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
7742 			pinfo->hop_count)) ||
7743 	    ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
7744 	     nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
7745 			 pinfo->path_change_count)))
7746 		goto nla_put_failure;
7747 
7748 	nla_nest_end(msg, pinfoattr);
7749 
7750 	genlmsg_end(msg, hdr);
7751 	return 0;
7752 
7753  nla_put_failure:
7754 	genlmsg_cancel(msg, hdr);
7755 	return -EMSGSIZE;
7756 }
7757 
7758 static int nl80211_dump_mpath(struct sk_buff *skb,
7759 			      struct netlink_callback *cb)
7760 {
7761 	struct mpath_info pinfo;
7762 	struct cfg80211_registered_device *rdev;
7763 	struct wireless_dev *wdev;
7764 	u8 dst[ETH_ALEN];
7765 	u8 next_hop[ETH_ALEN];
7766 	int path_idx = cb->args[2];
7767 	int err;
7768 
7769 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7770 	if (err)
7771 		return err;
7772 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
7773 	__acquire(&rdev->wiphy.mtx);
7774 
7775 	if (!rdev->ops->dump_mpath) {
7776 		err = -EOPNOTSUPP;
7777 		goto out_err;
7778 	}
7779 
7780 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7781 		err = -EOPNOTSUPP;
7782 		goto out_err;
7783 	}
7784 
7785 	while (1) {
7786 		err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
7787 				      next_hop, &pinfo);
7788 		if (err == -ENOENT)
7789 			break;
7790 		if (err)
7791 			goto out_err;
7792 
7793 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7794 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
7795 				       wdev->netdev, dst, next_hop,
7796 				       &pinfo) < 0)
7797 			goto out;
7798 
7799 		path_idx++;
7800 	}
7801 
7802  out:
7803 	cb->args[2] = path_idx;
7804 	err = skb->len;
7805  out_err:
7806 	wiphy_unlock(&rdev->wiphy);
7807 	return err;
7808 }
7809 
7810 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
7811 {
7812 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7813 	int err;
7814 	struct net_device *dev = info->user_ptr[1];
7815 	struct mpath_info pinfo;
7816 	struct sk_buff *msg;
7817 	u8 *dst = NULL;
7818 	u8 next_hop[ETH_ALEN];
7819 
7820 	memset(&pinfo, 0, sizeof(pinfo));
7821 
7822 	if (!info->attrs[NL80211_ATTR_MAC])
7823 		return -EINVAL;
7824 
7825 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7826 
7827 	if (!rdev->ops->get_mpath)
7828 		return -EOPNOTSUPP;
7829 
7830 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7831 		return -EOPNOTSUPP;
7832 
7833 	err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
7834 	if (err)
7835 		return err;
7836 
7837 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7838 	if (!msg)
7839 		return -ENOMEM;
7840 
7841 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7842 				 dev, dst, next_hop, &pinfo) < 0) {
7843 		nlmsg_free(msg);
7844 		return -ENOBUFS;
7845 	}
7846 
7847 	return genlmsg_reply(msg, info);
7848 }
7849 
7850 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
7851 {
7852 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7853 	struct net_device *dev = info->user_ptr[1];
7854 	u8 *dst = NULL;
7855 	u8 *next_hop = NULL;
7856 
7857 	if (!info->attrs[NL80211_ATTR_MAC])
7858 		return -EINVAL;
7859 
7860 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7861 		return -EINVAL;
7862 
7863 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7864 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7865 
7866 	if (!rdev->ops->change_mpath)
7867 		return -EOPNOTSUPP;
7868 
7869 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7870 		return -EOPNOTSUPP;
7871 
7872 	return rdev_change_mpath(rdev, dev, dst, next_hop);
7873 }
7874 
7875 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
7876 {
7877 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7878 	struct net_device *dev = info->user_ptr[1];
7879 	u8 *dst = NULL;
7880 	u8 *next_hop = NULL;
7881 
7882 	if (!info->attrs[NL80211_ATTR_MAC])
7883 		return -EINVAL;
7884 
7885 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7886 		return -EINVAL;
7887 
7888 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7889 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7890 
7891 	if (!rdev->ops->add_mpath)
7892 		return -EOPNOTSUPP;
7893 
7894 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7895 		return -EOPNOTSUPP;
7896 
7897 	return rdev_add_mpath(rdev, dev, dst, next_hop);
7898 }
7899 
7900 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
7901 {
7902 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7903 	struct net_device *dev = info->user_ptr[1];
7904 	u8 *dst = NULL;
7905 
7906 	if (info->attrs[NL80211_ATTR_MAC])
7907 		dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7908 
7909 	if (!rdev->ops->del_mpath)
7910 		return -EOPNOTSUPP;
7911 
7912 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7913 		return -EOPNOTSUPP;
7914 
7915 	return rdev_del_mpath(rdev, dev, dst);
7916 }
7917 
7918 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
7919 {
7920 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7921 	int err;
7922 	struct net_device *dev = info->user_ptr[1];
7923 	struct mpath_info pinfo;
7924 	struct sk_buff *msg;
7925 	u8 *dst = NULL;
7926 	u8 mpp[ETH_ALEN];
7927 
7928 	memset(&pinfo, 0, sizeof(pinfo));
7929 
7930 	if (!info->attrs[NL80211_ATTR_MAC])
7931 		return -EINVAL;
7932 
7933 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7934 
7935 	if (!rdev->ops->get_mpp)
7936 		return -EOPNOTSUPP;
7937 
7938 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7939 		return -EOPNOTSUPP;
7940 
7941 	err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
7942 	if (err)
7943 		return err;
7944 
7945 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7946 	if (!msg)
7947 		return -ENOMEM;
7948 
7949 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7950 			       dev, dst, mpp, &pinfo) < 0) {
7951 		nlmsg_free(msg);
7952 		return -ENOBUFS;
7953 	}
7954 
7955 	return genlmsg_reply(msg, info);
7956 }
7957 
7958 static int nl80211_dump_mpp(struct sk_buff *skb,
7959 			    struct netlink_callback *cb)
7960 {
7961 	struct mpath_info pinfo;
7962 	struct cfg80211_registered_device *rdev;
7963 	struct wireless_dev *wdev;
7964 	u8 dst[ETH_ALEN];
7965 	u8 mpp[ETH_ALEN];
7966 	int path_idx = cb->args[2];
7967 	int err;
7968 
7969 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7970 	if (err)
7971 		return err;
7972 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
7973 	__acquire(&rdev->wiphy.mtx);
7974 
7975 	if (!rdev->ops->dump_mpp) {
7976 		err = -EOPNOTSUPP;
7977 		goto out_err;
7978 	}
7979 
7980 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7981 		err = -EOPNOTSUPP;
7982 		goto out_err;
7983 	}
7984 
7985 	while (1) {
7986 		err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
7987 				    mpp, &pinfo);
7988 		if (err == -ENOENT)
7989 			break;
7990 		if (err)
7991 			goto out_err;
7992 
7993 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7994 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
7995 				       wdev->netdev, dst, mpp,
7996 				       &pinfo) < 0)
7997 			goto out;
7998 
7999 		path_idx++;
8000 	}
8001 
8002  out:
8003 	cb->args[2] = path_idx;
8004 	err = skb->len;
8005  out_err:
8006 	wiphy_unlock(&rdev->wiphy);
8007 	return err;
8008 }
8009 
8010 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
8011 {
8012 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8013 	struct net_device *dev = info->user_ptr[1];
8014 	struct bss_parameters params;
8015 
8016 	memset(&params, 0, sizeof(params));
8017 	params.link_id = nl80211_link_id_or_invalid(info->attrs);
8018 	/* default to not changing parameters */
8019 	params.use_cts_prot = -1;
8020 	params.use_short_preamble = -1;
8021 	params.use_short_slot_time = -1;
8022 	params.ap_isolate = -1;
8023 	params.ht_opmode = -1;
8024 	params.p2p_ctwindow = -1;
8025 	params.p2p_opp_ps = -1;
8026 
8027 	if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
8028 		params.use_cts_prot =
8029 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
8030 	if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
8031 		params.use_short_preamble =
8032 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
8033 	if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
8034 		params.use_short_slot_time =
8035 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
8036 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8037 		params.basic_rates =
8038 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8039 		params.basic_rates_len =
8040 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8041 	}
8042 	if (info->attrs[NL80211_ATTR_AP_ISOLATE])
8043 		params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
8044 	if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
8045 		params.ht_opmode =
8046 			nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
8047 
8048 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
8049 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
8050 			return -EINVAL;
8051 		params.p2p_ctwindow =
8052 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
8053 		if (params.p2p_ctwindow != 0 &&
8054 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
8055 			return -EINVAL;
8056 	}
8057 
8058 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
8059 		u8 tmp;
8060 
8061 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
8062 			return -EINVAL;
8063 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
8064 		params.p2p_opp_ps = tmp;
8065 		if (params.p2p_opp_ps &&
8066 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
8067 			return -EINVAL;
8068 	}
8069 
8070 	if (!rdev->ops->change_bss)
8071 		return -EOPNOTSUPP;
8072 
8073 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
8074 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
8075 		return -EOPNOTSUPP;
8076 
8077 	return rdev_change_bss(rdev, dev, &params);
8078 }
8079 
8080 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
8081 {
8082 	char *data = NULL;
8083 	bool is_indoor;
8084 	enum nl80211_user_reg_hint_type user_reg_hint_type;
8085 	u32 owner_nlportid;
8086 
8087 	/*
8088 	 * You should only get this when cfg80211 hasn't yet initialized
8089 	 * completely when built-in to the kernel right between the time
8090 	 * window between nl80211_init() and regulatory_init(), if that is
8091 	 * even possible.
8092 	 */
8093 	if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
8094 		return -EINPROGRESS;
8095 
8096 	if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
8097 		user_reg_hint_type =
8098 		  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
8099 	else
8100 		user_reg_hint_type = NL80211_USER_REG_HINT_USER;
8101 
8102 	switch (user_reg_hint_type) {
8103 	case NL80211_USER_REG_HINT_USER:
8104 	case NL80211_USER_REG_HINT_CELL_BASE:
8105 		if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
8106 			return -EINVAL;
8107 
8108 		data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
8109 		return regulatory_hint_user(data, user_reg_hint_type);
8110 	case NL80211_USER_REG_HINT_INDOOR:
8111 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
8112 			owner_nlportid = info->snd_portid;
8113 			is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
8114 		} else {
8115 			owner_nlportid = 0;
8116 			is_indoor = true;
8117 		}
8118 
8119 		return regulatory_hint_indoor(is_indoor, owner_nlportid);
8120 	default:
8121 		return -EINVAL;
8122 	}
8123 }
8124 
8125 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
8126 {
8127 	return reg_reload_regdb();
8128 }
8129 
8130 static int nl80211_get_mesh_config(struct sk_buff *skb,
8131 				   struct genl_info *info)
8132 {
8133 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8134 	struct net_device *dev = info->user_ptr[1];
8135 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8136 	struct mesh_config cur_params;
8137 	int err = 0;
8138 	void *hdr;
8139 	struct nlattr *pinfoattr;
8140 	struct sk_buff *msg;
8141 
8142 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
8143 		return -EOPNOTSUPP;
8144 
8145 	if (!rdev->ops->get_mesh_config)
8146 		return -EOPNOTSUPP;
8147 
8148 	/* If not connected, get default parameters */
8149 	if (!wdev->u.mesh.id_len)
8150 		memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
8151 	else
8152 		err = rdev_get_mesh_config(rdev, dev, &cur_params);
8153 
8154 	if (err)
8155 		return err;
8156 
8157 	/* Draw up a netlink message to send back */
8158 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8159 	if (!msg)
8160 		return -ENOMEM;
8161 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8162 			     NL80211_CMD_GET_MESH_CONFIG);
8163 	if (!hdr)
8164 		goto out;
8165 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
8166 	if (!pinfoattr)
8167 		goto nla_put_failure;
8168 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8169 	    nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
8170 			cur_params.dot11MeshRetryTimeout) ||
8171 	    nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
8172 			cur_params.dot11MeshConfirmTimeout) ||
8173 	    nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
8174 			cur_params.dot11MeshHoldingTimeout) ||
8175 	    nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
8176 			cur_params.dot11MeshMaxPeerLinks) ||
8177 	    nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
8178 		       cur_params.dot11MeshMaxRetries) ||
8179 	    nla_put_u8(msg, NL80211_MESHCONF_TTL,
8180 		       cur_params.dot11MeshTTL) ||
8181 	    nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
8182 		       cur_params.element_ttl) ||
8183 	    nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8184 		       cur_params.auto_open_plinks) ||
8185 	    nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
8186 			cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
8187 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
8188 		       cur_params.dot11MeshHWMPmaxPREQretries) ||
8189 	    nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
8190 			cur_params.path_refresh_time) ||
8191 	    nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
8192 			cur_params.min_discovery_timeout) ||
8193 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
8194 			cur_params.dot11MeshHWMPactivePathTimeout) ||
8195 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
8196 			cur_params.dot11MeshHWMPpreqMinInterval) ||
8197 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
8198 			cur_params.dot11MeshHWMPperrMinInterval) ||
8199 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
8200 			cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
8201 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
8202 		       cur_params.dot11MeshHWMPRootMode) ||
8203 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
8204 			cur_params.dot11MeshHWMPRannInterval) ||
8205 	    nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
8206 		       cur_params.dot11MeshGateAnnouncementProtocol) ||
8207 	    nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
8208 		       cur_params.dot11MeshForwarding) ||
8209 	    nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
8210 			cur_params.rssi_threshold) ||
8211 	    nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
8212 			cur_params.ht_opmode) ||
8213 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
8214 			cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
8215 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
8216 			cur_params.dot11MeshHWMProotInterval) ||
8217 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
8218 			cur_params.dot11MeshHWMPconfirmationInterval) ||
8219 	    nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
8220 			cur_params.power_mode) ||
8221 	    nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
8222 			cur_params.dot11MeshAwakeWindowDuration) ||
8223 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
8224 			cur_params.plink_timeout) ||
8225 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
8226 		       cur_params.dot11MeshConnectedToMeshGate) ||
8227 	    nla_put_u8(msg, NL80211_MESHCONF_NOLEARN,
8228 		       cur_params.dot11MeshNolearn) ||
8229 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_AS,
8230 		       cur_params.dot11MeshConnectedToAuthServer))
8231 		goto nla_put_failure;
8232 	nla_nest_end(msg, pinfoattr);
8233 	genlmsg_end(msg, hdr);
8234 	return genlmsg_reply(msg, info);
8235 
8236  nla_put_failure:
8237  out:
8238 	nlmsg_free(msg);
8239 	return -ENOBUFS;
8240 }
8241 
8242 static const struct nla_policy
8243 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
8244 	[NL80211_MESHCONF_RETRY_TIMEOUT] =
8245 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8246 	[NL80211_MESHCONF_CONFIRM_TIMEOUT] =
8247 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8248 	[NL80211_MESHCONF_HOLDING_TIMEOUT] =
8249 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8250 	[NL80211_MESHCONF_MAX_PEER_LINKS] =
8251 		NLA_POLICY_RANGE(NLA_U16, 0, 255),
8252 	[NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
8253 	[NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8254 	[NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8255 	[NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
8256 	[NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
8257 		NLA_POLICY_RANGE(NLA_U32, 1, 255),
8258 	[NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
8259 	[NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
8260 	[NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
8261 	[NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
8262 	[NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
8263 		NLA_POLICY_MIN(NLA_U16, 1),
8264 	[NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
8265 		NLA_POLICY_MIN(NLA_U16, 1),
8266 	[NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
8267 		NLA_POLICY_MIN(NLA_U16, 1),
8268 	[NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
8269 	[NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
8270 		NLA_POLICY_MIN(NLA_U16, 1),
8271 	[NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
8272 	[NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
8273 	[NL80211_MESHCONF_RSSI_THRESHOLD] =
8274 		NLA_POLICY_RANGE(NLA_S32, -255, 0),
8275 	[NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
8276 	[NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
8277 	[NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
8278 		NLA_POLICY_MIN(NLA_U16, 1),
8279 	[NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
8280 		NLA_POLICY_MIN(NLA_U16, 1),
8281 	[NL80211_MESHCONF_POWER_MODE] =
8282 		NLA_POLICY_RANGE(NLA_U32,
8283 				 NL80211_MESH_POWER_ACTIVE,
8284 				 NL80211_MESH_POWER_MAX),
8285 	[NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
8286 	[NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
8287 	[NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8288 	[NL80211_MESHCONF_NOLEARN] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8289 	[NL80211_MESHCONF_CONNECTED_TO_AS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8290 };
8291 
8292 static const struct nla_policy
8293 	nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
8294 	[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
8295 	[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
8296 	[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
8297 	[NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
8298 	[NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
8299 	[NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
8300 	[NL80211_MESH_SETUP_IE] =
8301 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
8302 				       IEEE80211_MAX_DATA_LEN),
8303 	[NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
8304 };
8305 
8306 static int nl80211_parse_mesh_config(struct genl_info *info,
8307 				     struct mesh_config *cfg,
8308 				     u32 *mask_out)
8309 {
8310 	struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
8311 	u32 mask = 0;
8312 	u16 ht_opmode;
8313 
8314 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)	\
8315 do {									\
8316 	if (tb[attr]) {							\
8317 		cfg->param = fn(tb[attr]);				\
8318 		mask |= BIT((attr) - 1);				\
8319 	}								\
8320 } while (0)
8321 
8322 	if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
8323 		return -EINVAL;
8324 	if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
8325 		return -EINVAL;
8326 
8327 	/* This makes sure that there aren't more than 32 mesh config
8328 	 * parameters (otherwise our bitfield scheme would not work.) */
8329 	BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
8330 
8331 	/* Fill in the params struct */
8332 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
8333 				  NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
8334 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
8335 				  NL80211_MESHCONF_CONFIRM_TIMEOUT,
8336 				  nla_get_u16);
8337 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
8338 				  NL80211_MESHCONF_HOLDING_TIMEOUT,
8339 				  nla_get_u16);
8340 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
8341 				  NL80211_MESHCONF_MAX_PEER_LINKS,
8342 				  nla_get_u16);
8343 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
8344 				  NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
8345 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
8346 				  NL80211_MESHCONF_TTL, nla_get_u8);
8347 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
8348 				  NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
8349 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
8350 				  NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8351 				  nla_get_u8);
8352 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
8353 				  mask,
8354 				  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
8355 				  nla_get_u32);
8356 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
8357 				  NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
8358 				  nla_get_u8);
8359 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
8360 				  NL80211_MESHCONF_PATH_REFRESH_TIME,
8361 				  nla_get_u32);
8362 	if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
8363 	    (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
8364 		return -EINVAL;
8365 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
8366 				  NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
8367 				  nla_get_u16);
8368 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
8369 				  mask,
8370 				  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
8371 				  nla_get_u32);
8372 	if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
8373 	    (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
8374 	     cfg->dot11MeshHWMPactivePathTimeout > 65535))
8375 		return -EINVAL;
8376 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
8377 				  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
8378 				  nla_get_u16);
8379 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
8380 				  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
8381 				  nla_get_u16);
8382 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8383 				  dot11MeshHWMPnetDiameterTraversalTime, mask,
8384 				  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
8385 				  nla_get_u16);
8386 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
8387 				  NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
8388 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
8389 				  NL80211_MESHCONF_HWMP_RANN_INTERVAL,
8390 				  nla_get_u16);
8391 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
8392 				  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
8393 				  nla_get_u8);
8394 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
8395 				  NL80211_MESHCONF_FORWARDING, nla_get_u8);
8396 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
8397 				  NL80211_MESHCONF_RSSI_THRESHOLD,
8398 				  nla_get_s32);
8399 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
8400 				  NL80211_MESHCONF_CONNECTED_TO_GATE,
8401 				  nla_get_u8);
8402 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToAuthServer, mask,
8403 				  NL80211_MESHCONF_CONNECTED_TO_AS,
8404 				  nla_get_u8);
8405 	/*
8406 	 * Check HT operation mode based on
8407 	 * IEEE 802.11-2016 9.4.2.57 HT Operation element.
8408 	 */
8409 	if (tb[NL80211_MESHCONF_HT_OPMODE]) {
8410 		ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
8411 
8412 		if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
8413 				  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
8414 				  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
8415 			return -EINVAL;
8416 
8417 		/* NON_HT_STA bit is reserved, but some programs set it */
8418 		ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
8419 
8420 		cfg->ht_opmode = ht_opmode;
8421 		mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
8422 	}
8423 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8424 				  dot11MeshHWMPactivePathToRootTimeout, mask,
8425 				  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
8426 				  nla_get_u32);
8427 	if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
8428 	    (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
8429 	     cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
8430 		return -EINVAL;
8431 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
8432 				  NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
8433 				  nla_get_u16);
8434 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
8435 				  mask,
8436 				  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
8437 				  nla_get_u16);
8438 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
8439 				  NL80211_MESHCONF_POWER_MODE, nla_get_u32);
8440 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
8441 				  NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
8442 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
8443 				  NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
8444 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNolearn, mask,
8445 				  NL80211_MESHCONF_NOLEARN, nla_get_u8);
8446 	if (mask_out)
8447 		*mask_out = mask;
8448 
8449 	return 0;
8450 
8451 #undef FILL_IN_MESH_PARAM_IF_SET
8452 }
8453 
8454 static int nl80211_parse_mesh_setup(struct genl_info *info,
8455 				     struct mesh_setup *setup)
8456 {
8457 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8458 	struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
8459 
8460 	if (!info->attrs[NL80211_ATTR_MESH_SETUP])
8461 		return -EINVAL;
8462 	if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
8463 		return -EINVAL;
8464 
8465 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
8466 		setup->sync_method =
8467 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
8468 		 IEEE80211_SYNC_METHOD_VENDOR :
8469 		 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
8470 
8471 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
8472 		setup->path_sel_proto =
8473 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
8474 		 IEEE80211_PATH_PROTOCOL_VENDOR :
8475 		 IEEE80211_PATH_PROTOCOL_HWMP;
8476 
8477 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
8478 		setup->path_metric =
8479 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
8480 		 IEEE80211_PATH_METRIC_VENDOR :
8481 		 IEEE80211_PATH_METRIC_AIRTIME;
8482 
8483 	if (tb[NL80211_MESH_SETUP_IE]) {
8484 		struct nlattr *ieattr =
8485 			tb[NL80211_MESH_SETUP_IE];
8486 		setup->ie = nla_data(ieattr);
8487 		setup->ie_len = nla_len(ieattr);
8488 	}
8489 	if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
8490 	    !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
8491 		return -EINVAL;
8492 	setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
8493 	setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
8494 	setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
8495 	if (setup->is_secure)
8496 		setup->user_mpm = true;
8497 
8498 	if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
8499 		if (!setup->user_mpm)
8500 			return -EINVAL;
8501 		setup->auth_id =
8502 			nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
8503 	}
8504 
8505 	return 0;
8506 }
8507 
8508 static int nl80211_update_mesh_config(struct sk_buff *skb,
8509 				      struct genl_info *info)
8510 {
8511 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8512 	struct net_device *dev = info->user_ptr[1];
8513 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8514 	struct mesh_config cfg = {};
8515 	u32 mask;
8516 	int err;
8517 
8518 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
8519 		return -EOPNOTSUPP;
8520 
8521 	if (!rdev->ops->update_mesh_config)
8522 		return -EOPNOTSUPP;
8523 
8524 	err = nl80211_parse_mesh_config(info, &cfg, &mask);
8525 	if (err)
8526 		return err;
8527 
8528 	if (!wdev->u.mesh.id_len)
8529 		err = -ENOLINK;
8530 
8531 	if (!err)
8532 		err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
8533 
8534 	return err;
8535 }
8536 
8537 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
8538 			      struct sk_buff *msg)
8539 {
8540 	struct nlattr *nl_reg_rules;
8541 	unsigned int i;
8542 
8543 	if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
8544 	    (regdom->dfs_region &&
8545 	     nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
8546 		goto nla_put_failure;
8547 
8548 	nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
8549 	if (!nl_reg_rules)
8550 		goto nla_put_failure;
8551 
8552 	for (i = 0; i < regdom->n_reg_rules; i++) {
8553 		struct nlattr *nl_reg_rule;
8554 		const struct ieee80211_reg_rule *reg_rule;
8555 		const struct ieee80211_freq_range *freq_range;
8556 		const struct ieee80211_power_rule *power_rule;
8557 		unsigned int max_bandwidth_khz;
8558 
8559 		reg_rule = &regdom->reg_rules[i];
8560 		freq_range = &reg_rule->freq_range;
8561 		power_rule = &reg_rule->power_rule;
8562 
8563 		nl_reg_rule = nla_nest_start_noflag(msg, i);
8564 		if (!nl_reg_rule)
8565 			goto nla_put_failure;
8566 
8567 		max_bandwidth_khz = freq_range->max_bandwidth_khz;
8568 		if (!max_bandwidth_khz)
8569 			max_bandwidth_khz = reg_get_max_bandwidth(regdom,
8570 								  reg_rule);
8571 
8572 		if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
8573 				reg_rule->flags) ||
8574 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
8575 				freq_range->start_freq_khz) ||
8576 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
8577 				freq_range->end_freq_khz) ||
8578 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
8579 				max_bandwidth_khz) ||
8580 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
8581 				power_rule->max_antenna_gain) ||
8582 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
8583 				power_rule->max_eirp) ||
8584 		    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
8585 				reg_rule->dfs_cac_ms))
8586 			goto nla_put_failure;
8587 
8588 		if ((reg_rule->flags & NL80211_RRF_PSD) &&
8589 		    nla_put_s8(msg, NL80211_ATTR_POWER_RULE_PSD,
8590 			       reg_rule->psd))
8591 			goto nla_put_failure;
8592 
8593 		nla_nest_end(msg, nl_reg_rule);
8594 	}
8595 
8596 	nla_nest_end(msg, nl_reg_rules);
8597 	return 0;
8598 
8599 nla_put_failure:
8600 	return -EMSGSIZE;
8601 }
8602 
8603 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
8604 {
8605 	const struct ieee80211_regdomain *regdom = NULL;
8606 	struct cfg80211_registered_device *rdev;
8607 	struct wiphy *wiphy = NULL;
8608 	struct sk_buff *msg;
8609 	int err = -EMSGSIZE;
8610 	void *hdr;
8611 
8612 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8613 	if (!msg)
8614 		return -ENOBUFS;
8615 
8616 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8617 			     NL80211_CMD_GET_REG);
8618 	if (!hdr)
8619 		goto put_failure;
8620 
8621 	rtnl_lock();
8622 
8623 	if (info->attrs[NL80211_ATTR_WIPHY]) {
8624 		bool self_managed;
8625 
8626 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
8627 		if (IS_ERR(rdev)) {
8628 			err = PTR_ERR(rdev);
8629 			goto nla_put_failure;
8630 		}
8631 
8632 		wiphy = &rdev->wiphy;
8633 		self_managed = wiphy->regulatory_flags &
8634 			       REGULATORY_WIPHY_SELF_MANAGED;
8635 
8636 		rcu_read_lock();
8637 
8638 		regdom = get_wiphy_regdom(wiphy);
8639 
8640 		/* a self-managed-reg device must have a private regdom */
8641 		if (WARN_ON(!regdom && self_managed)) {
8642 			err = -EINVAL;
8643 			goto nla_put_failure_rcu;
8644 		}
8645 
8646 		if (regdom &&
8647 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8648 			goto nla_put_failure_rcu;
8649 	} else {
8650 		rcu_read_lock();
8651 	}
8652 
8653 	if (!wiphy && reg_last_request_cell_base() &&
8654 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8655 			NL80211_USER_REG_HINT_CELL_BASE))
8656 		goto nla_put_failure_rcu;
8657 
8658 	if (!regdom)
8659 		regdom = rcu_dereference(cfg80211_regdomain);
8660 
8661 	if (nl80211_put_regdom(regdom, msg))
8662 		goto nla_put_failure_rcu;
8663 
8664 	rcu_read_unlock();
8665 
8666 	genlmsg_end(msg, hdr);
8667 	rtnl_unlock();
8668 	return genlmsg_reply(msg, info);
8669 
8670 nla_put_failure_rcu:
8671 	rcu_read_unlock();
8672 nla_put_failure:
8673 	rtnl_unlock();
8674 put_failure:
8675 	nlmsg_free(msg);
8676 	return err;
8677 }
8678 
8679 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
8680 			       u32 seq, int flags, struct wiphy *wiphy,
8681 			       const struct ieee80211_regdomain *regdom)
8682 {
8683 	void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
8684 				   NL80211_CMD_GET_REG);
8685 
8686 	if (!hdr)
8687 		return -1;
8688 
8689 	genl_dump_check_consistent(cb, hdr);
8690 
8691 	if (nl80211_put_regdom(regdom, msg))
8692 		goto nla_put_failure;
8693 
8694 	if (!wiphy && reg_last_request_cell_base() &&
8695 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8696 			NL80211_USER_REG_HINT_CELL_BASE))
8697 		goto nla_put_failure;
8698 
8699 	if (wiphy &&
8700 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8701 		goto nla_put_failure;
8702 
8703 	if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
8704 	    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
8705 		goto nla_put_failure;
8706 
8707 	genlmsg_end(msg, hdr);
8708 	return 0;
8709 
8710 nla_put_failure:
8711 	genlmsg_cancel(msg, hdr);
8712 	return -EMSGSIZE;
8713 }
8714 
8715 static int nl80211_get_reg_dump(struct sk_buff *skb,
8716 				struct netlink_callback *cb)
8717 {
8718 	const struct ieee80211_regdomain *regdom = NULL;
8719 	struct cfg80211_registered_device *rdev;
8720 	int err, reg_idx, start = cb->args[2];
8721 
8722 	rcu_read_lock();
8723 
8724 	if (cfg80211_regdomain && start == 0) {
8725 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8726 					  NLM_F_MULTI, NULL,
8727 					  rcu_dereference(cfg80211_regdomain));
8728 		if (err < 0)
8729 			goto out_err;
8730 	}
8731 
8732 	/* the global regdom is idx 0 */
8733 	reg_idx = 1;
8734 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
8735 		regdom = get_wiphy_regdom(&rdev->wiphy);
8736 		if (!regdom)
8737 			continue;
8738 
8739 		if (++reg_idx <= start)
8740 			continue;
8741 
8742 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8743 					  NLM_F_MULTI, &rdev->wiphy, regdom);
8744 		if (err < 0) {
8745 			reg_idx--;
8746 			break;
8747 		}
8748 	}
8749 
8750 	cb->args[2] = reg_idx;
8751 	err = skb->len;
8752 out_err:
8753 	rcu_read_unlock();
8754 	return err;
8755 }
8756 
8757 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
8758 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
8759 	[NL80211_ATTR_REG_RULE_FLAGS]		= { .type = NLA_U32 },
8760 	[NL80211_ATTR_FREQ_RANGE_START]		= { .type = NLA_U32 },
8761 	[NL80211_ATTR_FREQ_RANGE_END]		= { .type = NLA_U32 },
8762 	[NL80211_ATTR_FREQ_RANGE_MAX_BW]	= { .type = NLA_U32 },
8763 	[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]	= { .type = NLA_U32 },
8764 	[NL80211_ATTR_POWER_RULE_MAX_EIRP]	= { .type = NLA_U32 },
8765 	[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
8766 };
8767 
8768 static int parse_reg_rule(struct nlattr *tb[],
8769 	struct ieee80211_reg_rule *reg_rule)
8770 {
8771 	struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
8772 	struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
8773 
8774 	if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
8775 		return -EINVAL;
8776 	if (!tb[NL80211_ATTR_FREQ_RANGE_START])
8777 		return -EINVAL;
8778 	if (!tb[NL80211_ATTR_FREQ_RANGE_END])
8779 		return -EINVAL;
8780 	if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
8781 		return -EINVAL;
8782 	if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
8783 		return -EINVAL;
8784 
8785 	reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
8786 
8787 	freq_range->start_freq_khz =
8788 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
8789 	freq_range->end_freq_khz =
8790 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
8791 	freq_range->max_bandwidth_khz =
8792 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
8793 
8794 	power_rule->max_eirp =
8795 		nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
8796 
8797 	if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
8798 		power_rule->max_antenna_gain =
8799 			nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
8800 
8801 	if (tb[NL80211_ATTR_DFS_CAC_TIME])
8802 		reg_rule->dfs_cac_ms =
8803 			nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
8804 
8805 	return 0;
8806 }
8807 
8808 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
8809 {
8810 	struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
8811 	struct nlattr *nl_reg_rule;
8812 	char *alpha2;
8813 	int rem_reg_rules, r;
8814 	u32 num_rules = 0, rule_idx = 0;
8815 	enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
8816 	struct ieee80211_regdomain *rd;
8817 
8818 	if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
8819 		return -EINVAL;
8820 
8821 	if (!info->attrs[NL80211_ATTR_REG_RULES])
8822 		return -EINVAL;
8823 
8824 	alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
8825 
8826 	if (info->attrs[NL80211_ATTR_DFS_REGION])
8827 		dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
8828 
8829 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8830 			    rem_reg_rules) {
8831 		num_rules++;
8832 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
8833 			return -EINVAL;
8834 	}
8835 
8836 	rtnl_lock();
8837 	if (!reg_is_valid_request(alpha2)) {
8838 		r = -EINVAL;
8839 		goto out;
8840 	}
8841 
8842 	rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
8843 	if (!rd) {
8844 		r = -ENOMEM;
8845 		goto out;
8846 	}
8847 
8848 	rd->n_reg_rules = num_rules;
8849 	rd->alpha2[0] = alpha2[0];
8850 	rd->alpha2[1] = alpha2[1];
8851 
8852 	/*
8853 	 * Disable DFS master mode if the DFS region was
8854 	 * not supported or known on this kernel.
8855 	 */
8856 	if (reg_supported_dfs_region(dfs_region))
8857 		rd->dfs_region = dfs_region;
8858 
8859 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8860 			    rem_reg_rules) {
8861 		r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
8862 						nl_reg_rule, reg_rule_policy,
8863 						info->extack);
8864 		if (r)
8865 			goto bad_reg;
8866 		r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
8867 		if (r)
8868 			goto bad_reg;
8869 
8870 		rule_idx++;
8871 
8872 		if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
8873 			r = -EINVAL;
8874 			goto bad_reg;
8875 		}
8876 	}
8877 
8878 	r = set_regdom(rd, REGD_SOURCE_CRDA);
8879 	/* set_regdom takes ownership of rd */
8880 	rd = NULL;
8881  bad_reg:
8882 	kfree(rd);
8883  out:
8884 	rtnl_unlock();
8885 	return r;
8886 }
8887 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
8888 
8889 static int validate_scan_freqs(struct nlattr *freqs)
8890 {
8891 	struct nlattr *attr1, *attr2;
8892 	int n_channels = 0, tmp1, tmp2;
8893 
8894 	nla_for_each_nested(attr1, freqs, tmp1)
8895 		if (nla_len(attr1) != sizeof(u32))
8896 			return 0;
8897 
8898 	nla_for_each_nested(attr1, freqs, tmp1) {
8899 		n_channels++;
8900 		/*
8901 		 * Some hardware has a limited channel list for
8902 		 * scanning, and it is pretty much nonsensical
8903 		 * to scan for a channel twice, so disallow that
8904 		 * and don't require drivers to check that the
8905 		 * channel list they get isn't longer than what
8906 		 * they can scan, as long as they can scan all
8907 		 * the channels they registered at once.
8908 		 */
8909 		nla_for_each_nested(attr2, freqs, tmp2)
8910 			if (attr1 != attr2 &&
8911 			    nla_get_u32(attr1) == nla_get_u32(attr2))
8912 				return 0;
8913 	}
8914 
8915 	return n_channels;
8916 }
8917 
8918 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
8919 {
8920 	return b < NUM_NL80211_BANDS && wiphy->bands[b];
8921 }
8922 
8923 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
8924 			    struct cfg80211_bss_selection *bss_select)
8925 {
8926 	struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
8927 	struct nlattr *nest;
8928 	int err;
8929 	bool found = false;
8930 	int i;
8931 
8932 	/* only process one nested attribute */
8933 	nest = nla_data(nla);
8934 	if (!nla_ok(nest, nla_len(nest)))
8935 		return -EINVAL;
8936 
8937 	err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
8938 					  nest, nl80211_bss_select_policy,
8939 					  NULL);
8940 	if (err)
8941 		return err;
8942 
8943 	/* only one attribute may be given */
8944 	for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
8945 		if (attr[i]) {
8946 			if (found)
8947 				return -EINVAL;
8948 			found = true;
8949 		}
8950 	}
8951 
8952 	bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
8953 
8954 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
8955 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
8956 
8957 	if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
8958 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
8959 		bss_select->param.band_pref =
8960 			nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
8961 		if (!is_band_valid(wiphy, bss_select->param.band_pref))
8962 			return -EINVAL;
8963 	}
8964 
8965 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
8966 		struct nl80211_bss_select_rssi_adjust *adj_param;
8967 
8968 		adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
8969 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
8970 		bss_select->param.adjust.band = adj_param->band;
8971 		bss_select->param.adjust.delta = adj_param->delta;
8972 		if (!is_band_valid(wiphy, bss_select->param.adjust.band))
8973 			return -EINVAL;
8974 	}
8975 
8976 	/* user-space did not provide behaviour attribute */
8977 	if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
8978 		return -EINVAL;
8979 
8980 	if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
8981 		return -EINVAL;
8982 
8983 	return 0;
8984 }
8985 
8986 int nl80211_parse_random_mac(struct nlattr **attrs,
8987 			     u8 *mac_addr, u8 *mac_addr_mask)
8988 {
8989 	int i;
8990 
8991 	if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
8992 		eth_zero_addr(mac_addr);
8993 		eth_zero_addr(mac_addr_mask);
8994 		mac_addr[0] = 0x2;
8995 		mac_addr_mask[0] = 0x3;
8996 
8997 		return 0;
8998 	}
8999 
9000 	/* need both or none */
9001 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
9002 		return -EINVAL;
9003 
9004 	memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
9005 	memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
9006 
9007 	/* don't allow or configure an mcast address */
9008 	if (!is_multicast_ether_addr(mac_addr_mask) ||
9009 	    is_multicast_ether_addr(mac_addr))
9010 		return -EINVAL;
9011 
9012 	/*
9013 	 * allow users to pass a MAC address that has bits set outside
9014 	 * of the mask, but don't bother drivers with having to deal
9015 	 * with such bits
9016 	 */
9017 	for (i = 0; i < ETH_ALEN; i++)
9018 		mac_addr[i] &= mac_addr_mask[i];
9019 
9020 	return 0;
9021 }
9022 
9023 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev,
9024 					      struct ieee80211_channel *chan)
9025 {
9026 	unsigned int link_id;
9027 	bool all_ok = true;
9028 
9029 	lockdep_assert_wiphy(wdev->wiphy);
9030 
9031 	if (!cfg80211_beaconing_iface_active(wdev))
9032 		return true;
9033 
9034 	/*
9035 	 * FIXME: check if we have a free HW resource/link for chan
9036 	 *
9037 	 * This, as well as the FIXME below, requires knowing the link
9038 	 * capabilities of the hardware.
9039 	 */
9040 
9041 	/* we cannot leave radar channels */
9042 	for_each_valid_link(wdev, link_id) {
9043 		struct cfg80211_chan_def *chandef;
9044 
9045 		chandef = wdev_chandef(wdev, link_id);
9046 		if (!chandef || !chandef->chan)
9047 			continue;
9048 
9049 		/*
9050 		 * FIXME: don't require all_ok, but rather check only the
9051 		 *	  correct HW resource/link onto which 'chan' falls,
9052 		 *	  as only that link leaves the channel for doing
9053 		 *	  the off-channel operation.
9054 		 */
9055 
9056 		if (chandef->chan->flags & IEEE80211_CHAN_RADAR)
9057 			all_ok = false;
9058 	}
9059 
9060 	if (all_ok)
9061 		return true;
9062 
9063 	return regulatory_pre_cac_allowed(wdev->wiphy);
9064 }
9065 
9066 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
9067 				    enum nl80211_ext_feature_index feat)
9068 {
9069 	if (!(flags & flag))
9070 		return true;
9071 	if (wiphy_ext_feature_isset(wiphy, feat))
9072 		return true;
9073 	return false;
9074 }
9075 
9076 static int
9077 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
9078 			 void *request, struct nlattr **attrs,
9079 			 bool is_sched_scan)
9080 {
9081 	u8 *mac_addr, *mac_addr_mask;
9082 	u32 *flags;
9083 	enum nl80211_feature_flags randomness_flag;
9084 
9085 	if (!attrs[NL80211_ATTR_SCAN_FLAGS])
9086 		return 0;
9087 
9088 	if (is_sched_scan) {
9089 		struct cfg80211_sched_scan_request *req = request;
9090 
9091 		randomness_flag = wdev ?
9092 				  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
9093 				  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
9094 		flags = &req->flags;
9095 		mac_addr = req->mac_addr;
9096 		mac_addr_mask = req->mac_addr_mask;
9097 	} else {
9098 		struct cfg80211_scan_request *req = request;
9099 
9100 		randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
9101 		flags = &req->flags;
9102 		mac_addr = req->mac_addr;
9103 		mac_addr_mask = req->mac_addr_mask;
9104 	}
9105 
9106 	*flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
9107 
9108 	if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
9109 	     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
9110 	    !nl80211_check_scan_feat(wiphy, *flags,
9111 				     NL80211_SCAN_FLAG_LOW_SPAN,
9112 				     NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
9113 	    !nl80211_check_scan_feat(wiphy, *flags,
9114 				     NL80211_SCAN_FLAG_LOW_POWER,
9115 				     NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
9116 	    !nl80211_check_scan_feat(wiphy, *flags,
9117 				     NL80211_SCAN_FLAG_HIGH_ACCURACY,
9118 				     NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
9119 	    !nl80211_check_scan_feat(wiphy, *flags,
9120 				     NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
9121 				     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
9122 	    !nl80211_check_scan_feat(wiphy, *flags,
9123 				     NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
9124 				     NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
9125 	    !nl80211_check_scan_feat(wiphy, *flags,
9126 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
9127 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
9128 	    !nl80211_check_scan_feat(wiphy, *flags,
9129 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
9130 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
9131 	    !nl80211_check_scan_feat(wiphy, *flags,
9132 				     NL80211_SCAN_FLAG_RANDOM_SN,
9133 				     NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
9134 	    !nl80211_check_scan_feat(wiphy, *flags,
9135 				     NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
9136 				     NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
9137 		return -EOPNOTSUPP;
9138 
9139 	if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
9140 		int err;
9141 
9142 		if (!(wiphy->features & randomness_flag) ||
9143 		    (wdev && wdev->connected))
9144 			return -EOPNOTSUPP;
9145 
9146 		err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
9147 		if (err)
9148 			return err;
9149 	}
9150 
9151 	return 0;
9152 }
9153 
9154 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
9155 {
9156 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9157 	struct wireless_dev *wdev = info->user_ptr[1];
9158 	struct cfg80211_scan_request *request;
9159 	struct nlattr *scan_freqs = NULL;
9160 	bool scan_freqs_khz = false;
9161 	struct nlattr *attr;
9162 	struct wiphy *wiphy;
9163 	int err, tmp, n_ssids = 0, n_channels, i;
9164 	size_t ie_len, size;
9165 
9166 	wiphy = &rdev->wiphy;
9167 
9168 	if (wdev->iftype == NL80211_IFTYPE_NAN)
9169 		return -EOPNOTSUPP;
9170 
9171 	if (!rdev->ops->scan)
9172 		return -EOPNOTSUPP;
9173 
9174 	if (rdev->scan_req || rdev->scan_msg)
9175 		return -EBUSY;
9176 
9177 	if (info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ]) {
9178 		if (!wiphy_ext_feature_isset(wiphy,
9179 					     NL80211_EXT_FEATURE_SCAN_FREQ_KHZ))
9180 			return -EOPNOTSUPP;
9181 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ];
9182 		scan_freqs_khz = true;
9183 	} else if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES])
9184 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQUENCIES];
9185 
9186 	if (scan_freqs) {
9187 		n_channels = validate_scan_freqs(scan_freqs);
9188 		if (!n_channels)
9189 			return -EINVAL;
9190 	} else {
9191 		n_channels = ieee80211_get_num_supported_channels(wiphy);
9192 	}
9193 
9194 	if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
9195 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
9196 			n_ssids++;
9197 
9198 	if (n_ssids > wiphy->max_scan_ssids)
9199 		return -EINVAL;
9200 
9201 	if (info->attrs[NL80211_ATTR_IE])
9202 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9203 	else
9204 		ie_len = 0;
9205 
9206 	if (ie_len > wiphy->max_scan_ie_len)
9207 		return -EINVAL;
9208 
9209 	size = struct_size(request, channels, n_channels);
9210 	size = size_add(size, array_size(sizeof(*request->ssids), n_ssids));
9211 	size = size_add(size, ie_len);
9212 	request = kzalloc(size, GFP_KERNEL);
9213 	if (!request)
9214 		return -ENOMEM;
9215 
9216 	if (n_ssids)
9217 		request->ssids = (void *)&request->channels[n_channels];
9218 	request->n_ssids = n_ssids;
9219 	if (ie_len) {
9220 		if (n_ssids)
9221 			request->ie = (void *)(request->ssids + n_ssids);
9222 		else
9223 			request->ie = (void *)(request->channels + n_channels);
9224 	}
9225 
9226 	i = 0;
9227 	if (scan_freqs) {
9228 		/* user specified, bail out if channel not found */
9229 		nla_for_each_nested(attr, scan_freqs, tmp) {
9230 			struct ieee80211_channel *chan;
9231 			int freq = nla_get_u32(attr);
9232 
9233 			if (!scan_freqs_khz)
9234 				freq = MHZ_TO_KHZ(freq);
9235 
9236 			chan = ieee80211_get_channel_khz(wiphy, freq);
9237 			if (!chan) {
9238 				err = -EINVAL;
9239 				goto out_free;
9240 			}
9241 
9242 			/* ignore disabled channels */
9243 			if (chan->flags & IEEE80211_CHAN_DISABLED)
9244 				continue;
9245 
9246 			request->channels[i] = chan;
9247 			i++;
9248 		}
9249 	} else {
9250 		enum nl80211_band band;
9251 
9252 		/* all channels */
9253 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
9254 			int j;
9255 
9256 			if (!wiphy->bands[band])
9257 				continue;
9258 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9259 				struct ieee80211_channel *chan;
9260 
9261 				chan = &wiphy->bands[band]->channels[j];
9262 
9263 				if (chan->flags & IEEE80211_CHAN_DISABLED)
9264 					continue;
9265 
9266 				request->channels[i] = chan;
9267 				i++;
9268 			}
9269 		}
9270 	}
9271 
9272 	if (!i) {
9273 		err = -EINVAL;
9274 		goto out_free;
9275 	}
9276 
9277 	request->n_channels = i;
9278 
9279 	for (i = 0; i < request->n_channels; i++) {
9280 		struct ieee80211_channel *chan = request->channels[i];
9281 
9282 		/* if we can go off-channel to the target channel we're good */
9283 		if (cfg80211_off_channel_oper_allowed(wdev, chan))
9284 			continue;
9285 
9286 		if (!cfg80211_wdev_on_sub_chan(wdev, chan, true)) {
9287 			err = -EBUSY;
9288 			goto out_free;
9289 		}
9290 	}
9291 
9292 	i = 0;
9293 	if (n_ssids) {
9294 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
9295 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9296 				err = -EINVAL;
9297 				goto out_free;
9298 			}
9299 			request->ssids[i].ssid_len = nla_len(attr);
9300 			memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
9301 			i++;
9302 		}
9303 	}
9304 
9305 	if (info->attrs[NL80211_ATTR_IE]) {
9306 		request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9307 		memcpy((void *)request->ie,
9308 		       nla_data(info->attrs[NL80211_ATTR_IE]),
9309 		       request->ie_len);
9310 	}
9311 
9312 	for (i = 0; i < NUM_NL80211_BANDS; i++)
9313 		if (wiphy->bands[i])
9314 			request->rates[i] =
9315 				(1 << wiphy->bands[i]->n_bitrates) - 1;
9316 
9317 	if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
9318 		nla_for_each_nested(attr,
9319 				    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
9320 				    tmp) {
9321 			enum nl80211_band band = nla_type(attr);
9322 
9323 			if (band < 0 || band >= NUM_NL80211_BANDS) {
9324 				err = -EINVAL;
9325 				goto out_free;
9326 			}
9327 
9328 			if (!wiphy->bands[band])
9329 				continue;
9330 
9331 			err = ieee80211_get_ratemask(wiphy->bands[band],
9332 						     nla_data(attr),
9333 						     nla_len(attr),
9334 						     &request->rates[band]);
9335 			if (err)
9336 				goto out_free;
9337 		}
9338 	}
9339 
9340 	if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
9341 		request->duration =
9342 			nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
9343 		request->duration_mandatory =
9344 			nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
9345 	}
9346 
9347 	err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
9348 				       false);
9349 	if (err)
9350 		goto out_free;
9351 
9352 	request->no_cck =
9353 		nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
9354 
9355 	/* Initial implementation used NL80211_ATTR_MAC to set the specific
9356 	 * BSSID to scan for. This was problematic because that same attribute
9357 	 * was already used for another purpose (local random MAC address). The
9358 	 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
9359 	 * compatibility with older userspace components, also use the
9360 	 * NL80211_ATTR_MAC value here if it can be determined to be used for
9361 	 * the specific BSSID use case instead of the random MAC address
9362 	 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
9363 	 */
9364 	if (info->attrs[NL80211_ATTR_BSSID])
9365 		memcpy(request->bssid,
9366 		       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
9367 	else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
9368 		 info->attrs[NL80211_ATTR_MAC])
9369 		memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
9370 		       ETH_ALEN);
9371 	else
9372 		eth_broadcast_addr(request->bssid);
9373 
9374 	request->tsf_report_link_id = nl80211_link_id_or_invalid(info->attrs);
9375 	request->wdev = wdev;
9376 	request->wiphy = &rdev->wiphy;
9377 	request->scan_start = jiffies;
9378 
9379 	rdev->scan_req = request;
9380 	err = cfg80211_scan(rdev);
9381 
9382 	if (err)
9383 		goto out_free;
9384 
9385 	nl80211_send_scan_start(rdev, wdev);
9386 	dev_hold(wdev->netdev);
9387 
9388 	return 0;
9389 
9390  out_free:
9391 	rdev->scan_req = NULL;
9392 	kfree(request);
9393 
9394 	return err;
9395 }
9396 
9397 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
9398 {
9399 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9400 	struct wireless_dev *wdev = info->user_ptr[1];
9401 
9402 	if (!rdev->ops->abort_scan)
9403 		return -EOPNOTSUPP;
9404 
9405 	if (rdev->scan_msg)
9406 		return 0;
9407 
9408 	if (!rdev->scan_req)
9409 		return -ENOENT;
9410 
9411 	rdev_abort_scan(rdev, wdev);
9412 	return 0;
9413 }
9414 
9415 static int
9416 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
9417 			       struct cfg80211_sched_scan_request *request,
9418 			       struct nlattr **attrs)
9419 {
9420 	int tmp, err, i = 0;
9421 	struct nlattr *attr;
9422 
9423 	if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9424 		u32 interval;
9425 
9426 		/*
9427 		 * If scan plans are not specified,
9428 		 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
9429 		 * case one scan plan will be set with the specified scan
9430 		 * interval and infinite number of iterations.
9431 		 */
9432 		interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
9433 		if (!interval)
9434 			return -EINVAL;
9435 
9436 		request->scan_plans[0].interval =
9437 			DIV_ROUND_UP(interval, MSEC_PER_SEC);
9438 		if (!request->scan_plans[0].interval)
9439 			return -EINVAL;
9440 
9441 		if (request->scan_plans[0].interval >
9442 		    wiphy->max_sched_scan_plan_interval)
9443 			request->scan_plans[0].interval =
9444 				wiphy->max_sched_scan_plan_interval;
9445 
9446 		return 0;
9447 	}
9448 
9449 	nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
9450 		struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
9451 
9452 		if (WARN_ON(i >= n_plans))
9453 			return -EINVAL;
9454 
9455 		err = nla_parse_nested_deprecated(plan,
9456 						  NL80211_SCHED_SCAN_PLAN_MAX,
9457 						  attr, nl80211_plan_policy,
9458 						  NULL);
9459 		if (err)
9460 			return err;
9461 
9462 		if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
9463 			return -EINVAL;
9464 
9465 		request->scan_plans[i].interval =
9466 			nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
9467 		if (!request->scan_plans[i].interval ||
9468 		    request->scan_plans[i].interval >
9469 		    wiphy->max_sched_scan_plan_interval)
9470 			return -EINVAL;
9471 
9472 		if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
9473 			request->scan_plans[i].iterations =
9474 				nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
9475 			if (!request->scan_plans[i].iterations ||
9476 			    (request->scan_plans[i].iterations >
9477 			     wiphy->max_sched_scan_plan_iterations))
9478 				return -EINVAL;
9479 		} else if (i < n_plans - 1) {
9480 			/*
9481 			 * All scan plans but the last one must specify
9482 			 * a finite number of iterations
9483 			 */
9484 			return -EINVAL;
9485 		}
9486 
9487 		i++;
9488 	}
9489 
9490 	/*
9491 	 * The last scan plan must not specify the number of
9492 	 * iterations, it is supposed to run infinitely
9493 	 */
9494 	if (request->scan_plans[n_plans - 1].iterations)
9495 		return  -EINVAL;
9496 
9497 	return 0;
9498 }
9499 
9500 static struct cfg80211_sched_scan_request *
9501 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
9502 			 struct nlattr **attrs, int max_match_sets)
9503 {
9504 	struct cfg80211_sched_scan_request *request;
9505 	struct nlattr *attr;
9506 	int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
9507 	enum nl80211_band band;
9508 	size_t ie_len, size;
9509 	struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
9510 	s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
9511 
9512 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9513 		n_channels = validate_scan_freqs(
9514 				attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
9515 		if (!n_channels)
9516 			return ERR_PTR(-EINVAL);
9517 	} else {
9518 		n_channels = ieee80211_get_num_supported_channels(wiphy);
9519 	}
9520 
9521 	if (attrs[NL80211_ATTR_SCAN_SSIDS])
9522 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9523 				    tmp)
9524 			n_ssids++;
9525 
9526 	if (n_ssids > wiphy->max_sched_scan_ssids)
9527 		return ERR_PTR(-EINVAL);
9528 
9529 	/*
9530 	 * First, count the number of 'real' matchsets. Due to an issue with
9531 	 * the old implementation, matchsets containing only the RSSI attribute
9532 	 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
9533 	 * RSSI for all matchsets, rather than their own matchset for reporting
9534 	 * all APs with a strong RSSI. This is needed to be compatible with
9535 	 * older userspace that treated a matchset with only the RSSI as the
9536 	 * global RSSI for all other matchsets - if there are other matchsets.
9537 	 */
9538 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9539 		nla_for_each_nested(attr,
9540 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9541 				    tmp) {
9542 			struct nlattr *rssi;
9543 
9544 			err = nla_parse_nested_deprecated(tb,
9545 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9546 							  attr,
9547 							  nl80211_match_policy,
9548 							  NULL);
9549 			if (err)
9550 				return ERR_PTR(err);
9551 
9552 			/* SSID and BSSID are mutually exclusive */
9553 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
9554 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
9555 				return ERR_PTR(-EINVAL);
9556 
9557 			/* add other standalone attributes here */
9558 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
9559 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
9560 				n_match_sets++;
9561 				continue;
9562 			}
9563 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9564 			if (rssi)
9565 				default_match_rssi = nla_get_s32(rssi);
9566 		}
9567 	}
9568 
9569 	/* However, if there's no other matchset, add the RSSI one */
9570 	if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
9571 		n_match_sets = 1;
9572 
9573 	if (n_match_sets > max_match_sets)
9574 		return ERR_PTR(-EINVAL);
9575 
9576 	if (attrs[NL80211_ATTR_IE])
9577 		ie_len = nla_len(attrs[NL80211_ATTR_IE]);
9578 	else
9579 		ie_len = 0;
9580 
9581 	if (ie_len > wiphy->max_sched_scan_ie_len)
9582 		return ERR_PTR(-EINVAL);
9583 
9584 	if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9585 		/*
9586 		 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
9587 		 * each scan plan already specifies its own interval
9588 		 */
9589 		if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9590 			return ERR_PTR(-EINVAL);
9591 
9592 		nla_for_each_nested(attr,
9593 				    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
9594 			n_plans++;
9595 	} else {
9596 		/*
9597 		 * The scan interval attribute is kept for backward
9598 		 * compatibility. If no scan plans are specified and sched scan
9599 		 * interval is specified, one scan plan will be set with this
9600 		 * scan interval and infinite number of iterations.
9601 		 */
9602 		if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9603 			return ERR_PTR(-EINVAL);
9604 
9605 		n_plans = 1;
9606 	}
9607 
9608 	if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
9609 		return ERR_PTR(-EINVAL);
9610 
9611 	if (!wiphy_ext_feature_isset(
9612 		    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
9613 	    (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
9614 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
9615 		return ERR_PTR(-EINVAL);
9616 
9617 	size = struct_size(request, channels, n_channels);
9618 	size = size_add(size, array_size(sizeof(*request->ssids), n_ssids));
9619 	size = size_add(size, array_size(sizeof(*request->match_sets),
9620 					 n_match_sets));
9621 	size = size_add(size, array_size(sizeof(*request->scan_plans),
9622 					 n_plans));
9623 	size = size_add(size, ie_len);
9624 	request = kzalloc(size, GFP_KERNEL);
9625 	if (!request)
9626 		return ERR_PTR(-ENOMEM);
9627 
9628 	if (n_ssids)
9629 		request->ssids = (void *)&request->channels[n_channels];
9630 	request->n_ssids = n_ssids;
9631 	if (ie_len) {
9632 		if (n_ssids)
9633 			request->ie = (void *)(request->ssids + n_ssids);
9634 		else
9635 			request->ie = (void *)(request->channels + n_channels);
9636 	}
9637 
9638 	if (n_match_sets) {
9639 		if (request->ie)
9640 			request->match_sets = (void *)(request->ie + ie_len);
9641 		else if (n_ssids)
9642 			request->match_sets =
9643 				(void *)(request->ssids + n_ssids);
9644 		else
9645 			request->match_sets =
9646 				(void *)(request->channels + n_channels);
9647 	}
9648 	request->n_match_sets = n_match_sets;
9649 
9650 	if (n_match_sets)
9651 		request->scan_plans = (void *)(request->match_sets +
9652 					       n_match_sets);
9653 	else if (request->ie)
9654 		request->scan_plans = (void *)(request->ie + ie_len);
9655 	else if (n_ssids)
9656 		request->scan_plans = (void *)(request->ssids + n_ssids);
9657 	else
9658 		request->scan_plans = (void *)(request->channels + n_channels);
9659 
9660 	request->n_scan_plans = n_plans;
9661 
9662 	i = 0;
9663 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9664 		/* user specified, bail out if channel not found */
9665 		nla_for_each_nested(attr,
9666 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
9667 				    tmp) {
9668 			struct ieee80211_channel *chan;
9669 
9670 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
9671 
9672 			if (!chan) {
9673 				err = -EINVAL;
9674 				goto out_free;
9675 			}
9676 
9677 			/* ignore disabled channels */
9678 			if (chan->flags & IEEE80211_CHAN_DISABLED)
9679 				continue;
9680 
9681 			request->channels[i] = chan;
9682 			i++;
9683 		}
9684 	} else {
9685 		/* all channels */
9686 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
9687 			int j;
9688 
9689 			if (!wiphy->bands[band])
9690 				continue;
9691 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9692 				struct ieee80211_channel *chan;
9693 
9694 				chan = &wiphy->bands[band]->channels[j];
9695 
9696 				if (chan->flags & IEEE80211_CHAN_DISABLED)
9697 					continue;
9698 
9699 				request->channels[i] = chan;
9700 				i++;
9701 			}
9702 		}
9703 	}
9704 
9705 	if (!i) {
9706 		err = -EINVAL;
9707 		goto out_free;
9708 	}
9709 
9710 	request->n_channels = i;
9711 
9712 	i = 0;
9713 	if (n_ssids) {
9714 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9715 				    tmp) {
9716 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9717 				err = -EINVAL;
9718 				goto out_free;
9719 			}
9720 			request->ssids[i].ssid_len = nla_len(attr);
9721 			memcpy(request->ssids[i].ssid, nla_data(attr),
9722 			       nla_len(attr));
9723 			i++;
9724 		}
9725 	}
9726 
9727 	i = 0;
9728 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9729 		nla_for_each_nested(attr,
9730 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9731 				    tmp) {
9732 			struct nlattr *ssid, *bssid, *rssi;
9733 
9734 			err = nla_parse_nested_deprecated(tb,
9735 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9736 							  attr,
9737 							  nl80211_match_policy,
9738 							  NULL);
9739 			if (err)
9740 				goto out_free;
9741 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
9742 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
9743 
9744 			if (!ssid && !bssid) {
9745 				i++;
9746 				continue;
9747 			}
9748 
9749 			if (WARN_ON(i >= n_match_sets)) {
9750 				/* this indicates a programming error,
9751 				 * the loop above should have verified
9752 				 * things properly
9753 				 */
9754 				err = -EINVAL;
9755 				goto out_free;
9756 			}
9757 
9758 			if (ssid) {
9759 				memcpy(request->match_sets[i].ssid.ssid,
9760 				       nla_data(ssid), nla_len(ssid));
9761 				request->match_sets[i].ssid.ssid_len =
9762 					nla_len(ssid);
9763 			}
9764 			if (bssid)
9765 				memcpy(request->match_sets[i].bssid,
9766 				       nla_data(bssid), ETH_ALEN);
9767 
9768 			/* special attribute - old implementation w/a */
9769 			request->match_sets[i].rssi_thold = default_match_rssi;
9770 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9771 			if (rssi)
9772 				request->match_sets[i].rssi_thold =
9773 					nla_get_s32(rssi);
9774 			i++;
9775 		}
9776 
9777 		/* there was no other matchset, so the RSSI one is alone */
9778 		if (i == 0 && n_match_sets)
9779 			request->match_sets[0].rssi_thold = default_match_rssi;
9780 
9781 		request->min_rssi_thold = INT_MAX;
9782 		for (i = 0; i < n_match_sets; i++)
9783 			request->min_rssi_thold =
9784 				min(request->match_sets[i].rssi_thold,
9785 				    request->min_rssi_thold);
9786 	} else {
9787 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
9788 	}
9789 
9790 	if (ie_len) {
9791 		request->ie_len = ie_len;
9792 		memcpy((void *)request->ie,
9793 		       nla_data(attrs[NL80211_ATTR_IE]),
9794 		       request->ie_len);
9795 	}
9796 
9797 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
9798 	if (err)
9799 		goto out_free;
9800 
9801 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
9802 		request->delay =
9803 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
9804 
9805 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
9806 		request->relative_rssi = nla_get_s8(
9807 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
9808 		request->relative_rssi_set = true;
9809 	}
9810 
9811 	if (request->relative_rssi_set &&
9812 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
9813 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
9814 
9815 		rssi_adjust = nla_data(
9816 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
9817 		request->rssi_adjust.band = rssi_adjust->band;
9818 		request->rssi_adjust.delta = rssi_adjust->delta;
9819 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
9820 			err = -EINVAL;
9821 			goto out_free;
9822 		}
9823 	}
9824 
9825 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
9826 	if (err)
9827 		goto out_free;
9828 
9829 	request->scan_start = jiffies;
9830 
9831 	return request;
9832 
9833 out_free:
9834 	kfree(request);
9835 	return ERR_PTR(err);
9836 }
9837 
9838 static int nl80211_start_sched_scan(struct sk_buff *skb,
9839 				    struct genl_info *info)
9840 {
9841 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9842 	struct net_device *dev = info->user_ptr[1];
9843 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9844 	struct cfg80211_sched_scan_request *sched_scan_req;
9845 	bool want_multi;
9846 	int err;
9847 
9848 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
9849 		return -EOPNOTSUPP;
9850 
9851 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
9852 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
9853 	if (err)
9854 		return err;
9855 
9856 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
9857 						  info->attrs,
9858 						  rdev->wiphy.max_match_sets);
9859 
9860 	err = PTR_ERR_OR_ZERO(sched_scan_req);
9861 	if (err)
9862 		goto out_err;
9863 
9864 	/* leave request id zero for legacy request
9865 	 * or if driver does not support multi-scheduled scan
9866 	 */
9867 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1)
9868 		sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
9869 
9870 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
9871 	if (err)
9872 		goto out_free;
9873 
9874 	sched_scan_req->dev = dev;
9875 	sched_scan_req->wiphy = &rdev->wiphy;
9876 
9877 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9878 		sched_scan_req->owner_nlportid = info->snd_portid;
9879 
9880 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
9881 
9882 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
9883 	return 0;
9884 
9885 out_free:
9886 	kfree(sched_scan_req);
9887 out_err:
9888 	return err;
9889 }
9890 
9891 static int nl80211_stop_sched_scan(struct sk_buff *skb,
9892 				   struct genl_info *info)
9893 {
9894 	struct cfg80211_sched_scan_request *req;
9895 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9896 	u64 cookie;
9897 
9898 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
9899 		return -EOPNOTSUPP;
9900 
9901 	if (info->attrs[NL80211_ATTR_COOKIE]) {
9902 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9903 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
9904 	}
9905 
9906 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
9907 				     struct cfg80211_sched_scan_request,
9908 				     list);
9909 	if (!req || req->reqid ||
9910 	    (req->owner_nlportid &&
9911 	     req->owner_nlportid != info->snd_portid))
9912 		return -ENOENT;
9913 
9914 	return cfg80211_stop_sched_scan_req(rdev, req, false);
9915 }
9916 
9917 static int nl80211_start_radar_detection(struct sk_buff *skb,
9918 					 struct genl_info *info)
9919 {
9920 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9921 	struct net_device *dev = info->user_ptr[1];
9922 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9923 	struct wiphy *wiphy = wdev->wiphy;
9924 	struct cfg80211_chan_def chandef;
9925 	enum nl80211_dfs_regions dfs_region;
9926 	unsigned int cac_time_ms;
9927 	int err = -EINVAL;
9928 
9929 	flush_delayed_work(&rdev->dfs_update_channels_wk);
9930 
9931 	wiphy_lock(wiphy);
9932 
9933 	dfs_region = reg_get_dfs_region(wiphy);
9934 	if (dfs_region == NL80211_DFS_UNSET)
9935 		goto unlock;
9936 
9937 	err = nl80211_parse_chandef(rdev, info, &chandef);
9938 	if (err)
9939 		goto unlock;
9940 
9941 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
9942 	if (err < 0)
9943 		goto unlock;
9944 
9945 	if (err == 0) {
9946 		err = -EINVAL;
9947 		goto unlock;
9948 	}
9949 
9950 	if (!cfg80211_chandef_dfs_usable(wiphy, &chandef)) {
9951 		err = -EINVAL;
9952 		goto unlock;
9953 	}
9954 
9955 	if (nla_get_flag(info->attrs[NL80211_ATTR_RADAR_BACKGROUND])) {
9956 		err = cfg80211_start_background_radar_detection(rdev, wdev,
9957 								&chandef);
9958 		goto unlock;
9959 	}
9960 
9961 	if (netif_carrier_ok(dev)) {
9962 		err = -EBUSY;
9963 		goto unlock;
9964 	}
9965 
9966 	if (wdev->cac_started) {
9967 		err = -EBUSY;
9968 		goto unlock;
9969 	}
9970 
9971 	/* CAC start is offloaded to HW and can't be started manually */
9972 	if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD)) {
9973 		err = -EOPNOTSUPP;
9974 		goto unlock;
9975 	}
9976 
9977 	if (!rdev->ops->start_radar_detection) {
9978 		err = -EOPNOTSUPP;
9979 		goto unlock;
9980 	}
9981 
9982 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
9983 	if (WARN_ON(!cac_time_ms))
9984 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
9985 
9986 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
9987 	if (!err) {
9988 		wdev->links[0].ap.chandef = chandef;
9989 		wdev->cac_started = true;
9990 		wdev->cac_start_time = jiffies;
9991 		wdev->cac_time_ms = cac_time_ms;
9992 	}
9993 unlock:
9994 	wiphy_unlock(wiphy);
9995 
9996 	return err;
9997 }
9998 
9999 static int nl80211_notify_radar_detection(struct sk_buff *skb,
10000 					  struct genl_info *info)
10001 {
10002 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10003 	struct net_device *dev = info->user_ptr[1];
10004 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10005 	struct wiphy *wiphy = wdev->wiphy;
10006 	struct cfg80211_chan_def chandef;
10007 	enum nl80211_dfs_regions dfs_region;
10008 	int err;
10009 
10010 	dfs_region = reg_get_dfs_region(wiphy);
10011 	if (dfs_region == NL80211_DFS_UNSET) {
10012 		GENL_SET_ERR_MSG(info,
10013 				 "DFS Region is not set. Unexpected Radar indication");
10014 		return -EINVAL;
10015 	}
10016 
10017 	err = nl80211_parse_chandef(rdev, info, &chandef);
10018 	if (err) {
10019 		GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
10020 		return err;
10021 	}
10022 
10023 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
10024 	if (err < 0) {
10025 		GENL_SET_ERR_MSG(info, "chandef is invalid");
10026 		return err;
10027 	}
10028 
10029 	if (err == 0) {
10030 		GENL_SET_ERR_MSG(info,
10031 				 "Unexpected Radar indication for chandef/iftype");
10032 		return -EINVAL;
10033 	}
10034 
10035 	/* Do not process this notification if radar is already detected
10036 	 * by kernel on this channel, and return success.
10037 	 */
10038 	if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
10039 		return 0;
10040 
10041 	cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
10042 
10043 	cfg80211_sched_dfs_chan_update(rdev);
10044 
10045 	rdev->radar_chandef = chandef;
10046 
10047 	/* Propagate this notification to other radios as well */
10048 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
10049 
10050 	return 0;
10051 }
10052 
10053 static int nl80211_parse_counter_offsets(struct cfg80211_registered_device *rdev,
10054 					 const u8 *data, size_t datalen,
10055 					 int first_count, struct nlattr *attr,
10056 					 const u16 **offsets, unsigned int *n_offsets)
10057 {
10058 	int i;
10059 
10060 	*n_offsets = 0;
10061 
10062 	if (!attr)
10063 		return 0;
10064 
10065 	if (!nla_len(attr) || (nla_len(attr) % sizeof(u16)))
10066 		return -EINVAL;
10067 
10068 	*n_offsets = nla_len(attr) / sizeof(u16);
10069 	if (rdev->wiphy.max_num_csa_counters &&
10070 	    (*n_offsets > rdev->wiphy.max_num_csa_counters))
10071 		return -EINVAL;
10072 
10073 	*offsets = nla_data(attr);
10074 
10075 	/* sanity checks - counters should fit and be the same */
10076 	for (i = 0; i < *n_offsets; i++) {
10077 		u16 offset = (*offsets)[i];
10078 
10079 		if (offset >= datalen)
10080 			return -EINVAL;
10081 
10082 		if (first_count != -1 && data[offset] != first_count)
10083 			return -EINVAL;
10084 	}
10085 
10086 	return 0;
10087 }
10088 
10089 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
10090 {
10091 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10092 	unsigned int link_id = nl80211_link_id(info->attrs);
10093 	struct net_device *dev = info->user_ptr[1];
10094 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10095 	struct cfg80211_csa_settings params;
10096 	struct nlattr **csa_attrs = NULL;
10097 	int err;
10098 	bool need_new_beacon = false;
10099 	bool need_handle_dfs_flag = true;
10100 	u32 cs_count;
10101 
10102 	if (!rdev->ops->channel_switch ||
10103 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
10104 		return -EOPNOTSUPP;
10105 
10106 	switch (dev->ieee80211_ptr->iftype) {
10107 	case NL80211_IFTYPE_AP:
10108 	case NL80211_IFTYPE_P2P_GO:
10109 		need_new_beacon = true;
10110 		/* For all modes except AP the handle_dfs flag needs to be
10111 		 * supplied to tell the kernel that userspace will handle radar
10112 		 * events when they happen. Otherwise a switch to a channel
10113 		 * requiring DFS will be rejected.
10114 		 */
10115 		need_handle_dfs_flag = false;
10116 
10117 		/* useless if AP is not running */
10118 		if (!wdev->links[link_id].ap.beacon_interval)
10119 			return -ENOTCONN;
10120 		break;
10121 	case NL80211_IFTYPE_ADHOC:
10122 		if (!wdev->u.ibss.ssid_len)
10123 			return -ENOTCONN;
10124 		break;
10125 	case NL80211_IFTYPE_MESH_POINT:
10126 		if (!wdev->u.mesh.id_len)
10127 			return -ENOTCONN;
10128 		break;
10129 	default:
10130 		return -EOPNOTSUPP;
10131 	}
10132 
10133 	memset(&params, 0, sizeof(params));
10134 	params.beacon_csa.ftm_responder = -1;
10135 
10136 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
10137 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
10138 		return -EINVAL;
10139 
10140 	/* only important for AP, IBSS and mesh create IEs internally */
10141 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
10142 		return -EINVAL;
10143 
10144 	/* Even though the attribute is u32, the specification says
10145 	 * u8, so let's make sure we don't overflow.
10146 	 */
10147 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
10148 	if (cs_count > 255)
10149 		return -EINVAL;
10150 
10151 	params.count = cs_count;
10152 
10153 	if (!need_new_beacon)
10154 		goto skip_beacons;
10155 
10156 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after,
10157 				   info->extack);
10158 	if (err)
10159 		goto free;
10160 
10161 	csa_attrs = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*csa_attrs),
10162 			    GFP_KERNEL);
10163 	if (!csa_attrs) {
10164 		err = -ENOMEM;
10165 		goto free;
10166 	}
10167 
10168 	err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
10169 					  info->attrs[NL80211_ATTR_CSA_IES],
10170 					  nl80211_policy, info->extack);
10171 	if (err)
10172 		goto free;
10173 
10174 	err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa,
10175 				   info->extack);
10176 	if (err)
10177 		goto free;
10178 
10179 	if (!csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
10180 		err = -EINVAL;
10181 		goto free;
10182 	}
10183 
10184 	err = nl80211_parse_counter_offsets(rdev, params.beacon_csa.tail,
10185 					    params.beacon_csa.tail_len,
10186 					    params.count,
10187 					    csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON],
10188 					    &params.counter_offsets_beacon,
10189 					    &params.n_counter_offsets_beacon);
10190 	if (err)
10191 		goto free;
10192 
10193 	err = nl80211_parse_counter_offsets(rdev, params.beacon_csa.probe_resp,
10194 					    params.beacon_csa.probe_resp_len,
10195 					    params.count,
10196 					    csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP],
10197 					    &params.counter_offsets_presp,
10198 					    &params.n_counter_offsets_presp);
10199 	if (err)
10200 		goto free;
10201 
10202 skip_beacons:
10203 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
10204 	if (err)
10205 		goto free;
10206 
10207 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
10208 					   wdev->iftype)) {
10209 		err = -EINVAL;
10210 		goto free;
10211 	}
10212 
10213 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
10214 					    &params.chandef,
10215 					    wdev->iftype);
10216 	if (err < 0)
10217 		goto free;
10218 
10219 	if (err > 0) {
10220 		params.radar_required = true;
10221 		if (need_handle_dfs_flag &&
10222 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
10223 			err = -EINVAL;
10224 			goto free;
10225 		}
10226 	}
10227 
10228 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
10229 		params.block_tx = true;
10230 
10231 	params.link_id = link_id;
10232 	err = rdev_channel_switch(rdev, dev, &params);
10233 
10234 free:
10235 	kfree(params.beacon_after.mbssid_ies);
10236 	kfree(params.beacon_csa.mbssid_ies);
10237 	kfree(params.beacon_after.rnr_ies);
10238 	kfree(params.beacon_csa.rnr_ies);
10239 	kfree(csa_attrs);
10240 	return err;
10241 }
10242 
10243 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
10244 			    u32 seq, int flags,
10245 			    struct cfg80211_registered_device *rdev,
10246 			    struct wireless_dev *wdev,
10247 			    struct cfg80211_internal_bss *intbss)
10248 {
10249 	struct cfg80211_bss *res = &intbss->pub;
10250 	const struct cfg80211_bss_ies *ies;
10251 	unsigned int link_id;
10252 	void *hdr;
10253 	struct nlattr *bss;
10254 
10255 	lockdep_assert_wiphy(wdev->wiphy);
10256 
10257 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
10258 			     NL80211_CMD_NEW_SCAN_RESULTS);
10259 	if (!hdr)
10260 		return -1;
10261 
10262 	genl_dump_check_consistent(cb, hdr);
10263 
10264 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
10265 		goto nla_put_failure;
10266 	if (wdev->netdev &&
10267 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
10268 		goto nla_put_failure;
10269 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
10270 			      NL80211_ATTR_PAD))
10271 		goto nla_put_failure;
10272 
10273 	bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
10274 	if (!bss)
10275 		goto nla_put_failure;
10276 	if ((!is_zero_ether_addr(res->bssid) &&
10277 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
10278 		goto nla_put_failure;
10279 
10280 	rcu_read_lock();
10281 	/* indicate whether we have probe response data or not */
10282 	if (rcu_access_pointer(res->proberesp_ies) &&
10283 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
10284 		goto fail_unlock_rcu;
10285 
10286 	/* this pointer prefers to be pointed to probe response data
10287 	 * but is always valid
10288 	 */
10289 	ies = rcu_dereference(res->ies);
10290 	if (ies) {
10291 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
10292 				      NL80211_BSS_PAD))
10293 			goto fail_unlock_rcu;
10294 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
10295 					ies->len, ies->data))
10296 			goto fail_unlock_rcu;
10297 	}
10298 
10299 	/* and this pointer is always (unless driver didn't know) beacon data */
10300 	ies = rcu_dereference(res->beacon_ies);
10301 	if (ies && ies->from_beacon) {
10302 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
10303 				      NL80211_BSS_PAD))
10304 			goto fail_unlock_rcu;
10305 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
10306 					ies->len, ies->data))
10307 			goto fail_unlock_rcu;
10308 	}
10309 	rcu_read_unlock();
10310 
10311 	if (res->beacon_interval &&
10312 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
10313 		goto nla_put_failure;
10314 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
10315 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
10316 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY_OFFSET,
10317 			res->channel->freq_offset) ||
10318 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
10319 			jiffies_to_msecs(jiffies - intbss->ts)))
10320 		goto nla_put_failure;
10321 
10322 	if (intbss->parent_tsf &&
10323 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
10324 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
10325 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
10326 		     intbss->parent_bssid)))
10327 		goto nla_put_failure;
10328 
10329 	if (intbss->ts_boottime &&
10330 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
10331 			      intbss->ts_boottime, NL80211_BSS_PAD))
10332 		goto nla_put_failure;
10333 
10334 	if (!nl80211_put_signal(msg, intbss->pub.chains,
10335 				intbss->pub.chain_signal,
10336 				NL80211_BSS_CHAIN_SIGNAL))
10337 		goto nla_put_failure;
10338 
10339 	switch (rdev->wiphy.signal_type) {
10340 	case CFG80211_SIGNAL_TYPE_MBM:
10341 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
10342 			goto nla_put_failure;
10343 		break;
10344 	case CFG80211_SIGNAL_TYPE_UNSPEC:
10345 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
10346 			goto nla_put_failure;
10347 		break;
10348 	default:
10349 		break;
10350 	}
10351 
10352 	switch (wdev->iftype) {
10353 	case NL80211_IFTYPE_P2P_CLIENT:
10354 	case NL80211_IFTYPE_STATION:
10355 		for_each_valid_link(wdev, link_id) {
10356 			if (intbss == wdev->links[link_id].client.current_bss &&
10357 			    (nla_put_u32(msg, NL80211_BSS_STATUS,
10358 					 NL80211_BSS_STATUS_ASSOCIATED) ||
10359 			     (wdev->valid_links &&
10360 			      (nla_put_u8(msg, NL80211_BSS_MLO_LINK_ID,
10361 					  link_id) ||
10362 			       nla_put(msg, NL80211_BSS_MLD_ADDR, ETH_ALEN,
10363 				       wdev->u.client.connected_addr)))))
10364 				goto nla_put_failure;
10365 		}
10366 		break;
10367 	case NL80211_IFTYPE_ADHOC:
10368 		if (intbss == wdev->u.ibss.current_bss &&
10369 		    nla_put_u32(msg, NL80211_BSS_STATUS,
10370 				NL80211_BSS_STATUS_IBSS_JOINED))
10371 			goto nla_put_failure;
10372 		break;
10373 	default:
10374 		break;
10375 	}
10376 
10377 	if (nla_put_u32(msg, NL80211_BSS_USE_FOR, res->use_for))
10378 		goto nla_put_failure;
10379 
10380 	if (res->cannot_use_reasons &&
10381 	    nla_put_u64_64bit(msg, NL80211_BSS_CANNOT_USE_REASONS,
10382 			      res->cannot_use_reasons,
10383 			      NL80211_BSS_PAD))
10384 		goto nla_put_failure;
10385 
10386 	nla_nest_end(msg, bss);
10387 
10388 	genlmsg_end(msg, hdr);
10389 	return 0;
10390 
10391  fail_unlock_rcu:
10392 	rcu_read_unlock();
10393  nla_put_failure:
10394 	genlmsg_cancel(msg, hdr);
10395 	return -EMSGSIZE;
10396 }
10397 
10398 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
10399 {
10400 	struct cfg80211_registered_device *rdev;
10401 	struct cfg80211_internal_bss *scan;
10402 	struct wireless_dev *wdev;
10403 	struct nlattr **attrbuf;
10404 	int start = cb->args[2], idx = 0;
10405 	bool dump_include_use_data;
10406 	int err;
10407 
10408 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
10409 	if (!attrbuf)
10410 		return -ENOMEM;
10411 
10412 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, attrbuf);
10413 	if (err) {
10414 		kfree(attrbuf);
10415 		return err;
10416 	}
10417 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10418 	__acquire(&rdev->wiphy.mtx);
10419 
10420 	dump_include_use_data =
10421 		attrbuf[NL80211_ATTR_BSS_DUMP_INCLUDE_USE_DATA];
10422 	kfree(attrbuf);
10423 
10424 	spin_lock_bh(&rdev->bss_lock);
10425 
10426 	/*
10427 	 * dump_scan will be called multiple times to break up the scan results
10428 	 * into multiple messages.  It is unlikely that any more bss-es will be
10429 	 * expired after the first call, so only call only call this on the
10430 	 * first dump_scan invocation.
10431 	 */
10432 	if (start == 0)
10433 		cfg80211_bss_expire(rdev);
10434 
10435 	cb->seq = rdev->bss_generation;
10436 
10437 	list_for_each_entry(scan, &rdev->bss_list, list) {
10438 		if (++idx <= start)
10439 			continue;
10440 		if (!dump_include_use_data &&
10441 		    !(scan->pub.use_for & NL80211_BSS_USE_FOR_NORMAL))
10442 			continue;
10443 		if (nl80211_send_bss(skb, cb,
10444 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10445 				rdev, wdev, scan) < 0) {
10446 			idx--;
10447 			break;
10448 		}
10449 	}
10450 
10451 	spin_unlock_bh(&rdev->bss_lock);
10452 
10453 	cb->args[2] = idx;
10454 	wiphy_unlock(&rdev->wiphy);
10455 
10456 	return skb->len;
10457 }
10458 
10459 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
10460 			       int flags, struct net_device *dev,
10461 			       bool allow_radio_stats,
10462 			       struct survey_info *survey)
10463 {
10464 	void *hdr;
10465 	struct nlattr *infoattr;
10466 
10467 	/* skip radio stats if userspace didn't request them */
10468 	if (!survey->channel && !allow_radio_stats)
10469 		return 0;
10470 
10471 	hdr = nl80211hdr_put(msg, portid, seq, flags,
10472 			     NL80211_CMD_NEW_SURVEY_RESULTS);
10473 	if (!hdr)
10474 		return -ENOMEM;
10475 
10476 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
10477 		goto nla_put_failure;
10478 
10479 	infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
10480 	if (!infoattr)
10481 		goto nla_put_failure;
10482 
10483 	if (survey->channel &&
10484 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
10485 			survey->channel->center_freq))
10486 		goto nla_put_failure;
10487 
10488 	if (survey->channel && survey->channel->freq_offset &&
10489 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY_OFFSET,
10490 			survey->channel->freq_offset))
10491 		goto nla_put_failure;
10492 
10493 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
10494 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
10495 		goto nla_put_failure;
10496 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
10497 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
10498 		goto nla_put_failure;
10499 	if ((survey->filled & SURVEY_INFO_TIME) &&
10500 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
10501 			survey->time, NL80211_SURVEY_INFO_PAD))
10502 		goto nla_put_failure;
10503 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
10504 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
10505 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
10506 		goto nla_put_failure;
10507 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
10508 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
10509 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
10510 		goto nla_put_failure;
10511 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
10512 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
10513 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
10514 		goto nla_put_failure;
10515 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
10516 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
10517 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
10518 		goto nla_put_failure;
10519 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
10520 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
10521 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
10522 		goto nla_put_failure;
10523 	if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
10524 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
10525 			      survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
10526 		goto nla_put_failure;
10527 
10528 	nla_nest_end(msg, infoattr);
10529 
10530 	genlmsg_end(msg, hdr);
10531 	return 0;
10532 
10533  nla_put_failure:
10534 	genlmsg_cancel(msg, hdr);
10535 	return -EMSGSIZE;
10536 }
10537 
10538 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
10539 {
10540 	struct nlattr **attrbuf;
10541 	struct survey_info survey;
10542 	struct cfg80211_registered_device *rdev;
10543 	struct wireless_dev *wdev;
10544 	int survey_idx = cb->args[2];
10545 	int res;
10546 	bool radio_stats;
10547 
10548 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
10549 	if (!attrbuf)
10550 		return -ENOMEM;
10551 
10552 	res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, attrbuf);
10553 	if (res) {
10554 		kfree(attrbuf);
10555 		return res;
10556 	}
10557 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10558 	__acquire(&rdev->wiphy.mtx);
10559 
10560 	/* prepare_wdev_dump parsed the attributes */
10561 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
10562 
10563 	if (!wdev->netdev) {
10564 		res = -EINVAL;
10565 		goto out_err;
10566 	}
10567 
10568 	if (!rdev->ops->dump_survey) {
10569 		res = -EOPNOTSUPP;
10570 		goto out_err;
10571 	}
10572 
10573 	while (1) {
10574 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
10575 		if (res == -ENOENT)
10576 			break;
10577 		if (res)
10578 			goto out_err;
10579 
10580 		/* don't send disabled channels, but do send non-channel data */
10581 		if (survey.channel &&
10582 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
10583 			survey_idx++;
10584 			continue;
10585 		}
10586 
10587 		if (nl80211_send_survey(skb,
10588 				NETLINK_CB(cb->skb).portid,
10589 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10590 				wdev->netdev, radio_stats, &survey) < 0)
10591 			goto out;
10592 		survey_idx++;
10593 	}
10594 
10595  out:
10596 	cb->args[2] = survey_idx;
10597 	res = skb->len;
10598  out_err:
10599 	kfree(attrbuf);
10600 	wiphy_unlock(&rdev->wiphy);
10601 	return res;
10602 }
10603 
10604 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
10605 {
10606 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10607 	struct net_device *dev = info->user_ptr[1];
10608 	struct ieee80211_channel *chan;
10609 	const u8 *bssid, *ssid;
10610 	int err, ssid_len;
10611 	enum nl80211_auth_type auth_type;
10612 	struct key_parse key;
10613 	bool local_state_change;
10614 	struct cfg80211_auth_request req = {};
10615 	u32 freq;
10616 
10617 	if (!info->attrs[NL80211_ATTR_MAC])
10618 		return -EINVAL;
10619 
10620 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
10621 		return -EINVAL;
10622 
10623 	if (!info->attrs[NL80211_ATTR_SSID])
10624 		return -EINVAL;
10625 
10626 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10627 		return -EINVAL;
10628 
10629 	err = nl80211_parse_key(info, &key);
10630 	if (err)
10631 		return err;
10632 
10633 	if (key.idx >= 0) {
10634 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
10635 			return -EINVAL;
10636 		if (!key.p.key || !key.p.key_len)
10637 			return -EINVAL;
10638 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
10639 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
10640 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
10641 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
10642 			return -EINVAL;
10643 		if (key.idx > 3)
10644 			return -EINVAL;
10645 	} else {
10646 		key.p.key_len = 0;
10647 		key.p.key = NULL;
10648 	}
10649 
10650 	if (key.idx >= 0) {
10651 		int i;
10652 		bool ok = false;
10653 
10654 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
10655 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
10656 				ok = true;
10657 				break;
10658 			}
10659 		}
10660 		if (!ok)
10661 			return -EINVAL;
10662 	}
10663 
10664 	if (!rdev->ops->auth)
10665 		return -EOPNOTSUPP;
10666 
10667 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10668 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10669 		return -EOPNOTSUPP;
10670 
10671 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10672 	freq = MHZ_TO_KHZ(nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
10673 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10674 		freq +=
10675 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10676 
10677 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10678 	if (!chan)
10679 		return -EINVAL;
10680 
10681 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10682 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10683 
10684 	if (info->attrs[NL80211_ATTR_IE]) {
10685 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10686 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10687 	}
10688 
10689 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10690 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
10691 		return -EINVAL;
10692 
10693 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
10694 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
10695 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
10696 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
10697 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
10698 		return -EINVAL;
10699 
10700 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
10701 		if (auth_type != NL80211_AUTHTYPE_SAE &&
10702 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
10703 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
10704 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
10705 			return -EINVAL;
10706 		req.auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
10707 		req.auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
10708 	}
10709 
10710 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
10711 
10712 	/*
10713 	 * Since we no longer track auth state, ignore
10714 	 * requests to only change local state.
10715 	 */
10716 	if (local_state_change)
10717 		return 0;
10718 
10719 	req.auth_type = auth_type;
10720 	req.key = key.p.key;
10721 	req.key_len = key.p.key_len;
10722 	req.key_idx = key.idx;
10723 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
10724 	if (req.link_id >= 0) {
10725 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
10726 			return -EINVAL;
10727 		if (!info->attrs[NL80211_ATTR_MLD_ADDR])
10728 			return -EINVAL;
10729 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
10730 		if (!is_valid_ether_addr(req.ap_mld_addr))
10731 			return -EINVAL;
10732 	}
10733 
10734 	req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
10735 				   IEEE80211_BSS_TYPE_ESS,
10736 				   IEEE80211_PRIVACY_ANY);
10737 	if (!req.bss)
10738 		return -ENOENT;
10739 
10740 	err = cfg80211_mlme_auth(rdev, dev, &req);
10741 
10742 	cfg80211_put_bss(&rdev->wiphy, req.bss);
10743 
10744 	return err;
10745 }
10746 
10747 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
10748 				     struct genl_info *info)
10749 {
10750 	if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10751 		GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
10752 		return -EINVAL;
10753 	}
10754 
10755 	if (!rdev->ops->tx_control_port ||
10756 	    !wiphy_ext_feature_isset(&rdev->wiphy,
10757 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
10758 		return -EOPNOTSUPP;
10759 
10760 	return 0;
10761 }
10762 
10763 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
10764 				   struct genl_info *info,
10765 				   struct cfg80211_crypto_settings *settings,
10766 				   int cipher_limit)
10767 {
10768 	memset(settings, 0, sizeof(*settings));
10769 
10770 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
10771 
10772 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
10773 		u16 proto;
10774 
10775 		proto = nla_get_u16(
10776 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
10777 		settings->control_port_ethertype = cpu_to_be16(proto);
10778 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
10779 		    proto != ETH_P_PAE)
10780 			return -EINVAL;
10781 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
10782 			settings->control_port_no_encrypt = true;
10783 	} else
10784 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
10785 
10786 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
10787 		int r = validate_pae_over_nl80211(rdev, info);
10788 
10789 		if (r < 0)
10790 			return r;
10791 
10792 		settings->control_port_over_nl80211 = true;
10793 
10794 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH])
10795 			settings->control_port_no_preauth = true;
10796 	}
10797 
10798 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
10799 		void *data;
10800 		int len, i;
10801 
10802 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10803 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10804 		settings->n_ciphers_pairwise = len / sizeof(u32);
10805 
10806 		if (len % sizeof(u32))
10807 			return -EINVAL;
10808 
10809 		if (settings->n_ciphers_pairwise > cipher_limit)
10810 			return -EINVAL;
10811 
10812 		memcpy(settings->ciphers_pairwise, data, len);
10813 
10814 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
10815 			if (!cfg80211_supported_cipher_suite(
10816 					&rdev->wiphy,
10817 					settings->ciphers_pairwise[i]))
10818 				return -EINVAL;
10819 	}
10820 
10821 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
10822 		settings->cipher_group =
10823 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
10824 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
10825 						     settings->cipher_group))
10826 			return -EINVAL;
10827 	}
10828 
10829 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS])
10830 		settings->wpa_versions =
10831 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
10832 
10833 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
10834 		void *data;
10835 		int len;
10836 
10837 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
10838 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
10839 		settings->n_akm_suites = len / sizeof(u32);
10840 
10841 		if (len % sizeof(u32))
10842 			return -EINVAL;
10843 
10844 		if (settings->n_akm_suites > rdev->wiphy.max_num_akm_suites)
10845 			return -EINVAL;
10846 
10847 		memcpy(settings->akm_suites, data, len);
10848 	}
10849 
10850 	if (info->attrs[NL80211_ATTR_PMK]) {
10851 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
10852 			return -EINVAL;
10853 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10854 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK) &&
10855 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10856 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_AP_PSK))
10857 			return -EINVAL;
10858 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10859 	}
10860 
10861 	if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
10862 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10863 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
10864 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10865 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP))
10866 			return -EINVAL;
10867 		settings->sae_pwd =
10868 			nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10869 		settings->sae_pwd_len =
10870 			nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10871 	}
10872 
10873 	if (info->attrs[NL80211_ATTR_SAE_PWE])
10874 		settings->sae_pwe =
10875 			nla_get_u8(info->attrs[NL80211_ATTR_SAE_PWE]);
10876 	else
10877 		settings->sae_pwe = NL80211_SAE_PWE_UNSPECIFIED;
10878 
10879 	return 0;
10880 }
10881 
10882 static struct cfg80211_bss *nl80211_assoc_bss(struct cfg80211_registered_device *rdev,
10883 					      const u8 *ssid, int ssid_len,
10884 					      struct nlattr **attrs,
10885 					      int assoc_link_id, int link_id)
10886 {
10887 	struct ieee80211_channel *chan;
10888 	struct cfg80211_bss *bss;
10889 	const u8 *bssid;
10890 	u32 freq, use_for = 0;
10891 
10892 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_WIPHY_FREQ])
10893 		return ERR_PTR(-EINVAL);
10894 
10895 	bssid = nla_data(attrs[NL80211_ATTR_MAC]);
10896 
10897 	freq = MHZ_TO_KHZ(nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ]));
10898 	if (attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10899 		freq += nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10900 
10901 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10902 	if (!chan)
10903 		return ERR_PTR(-EINVAL);
10904 
10905 	if (assoc_link_id >= 0)
10906 		use_for = NL80211_BSS_USE_FOR_MLD_LINK;
10907 	if (assoc_link_id == link_id)
10908 		use_for |= NL80211_BSS_USE_FOR_NORMAL;
10909 
10910 	bss = __cfg80211_get_bss(&rdev->wiphy, chan, bssid,
10911 				 ssid, ssid_len,
10912 				 IEEE80211_BSS_TYPE_ESS,
10913 				 IEEE80211_PRIVACY_ANY,
10914 				 use_for);
10915 	if (!bss)
10916 		return ERR_PTR(-ENOENT);
10917 
10918 	return bss;
10919 }
10920 
10921 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
10922 {
10923 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10924 	struct net_device *dev = info->user_ptr[1];
10925 	struct cfg80211_assoc_request req = {};
10926 	struct nlattr **attrs = NULL;
10927 	const u8 *ap_addr, *ssid;
10928 	unsigned int link_id;
10929 	int err, ssid_len;
10930 
10931 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
10932 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10933 		return -EPERM;
10934 
10935 	if (!info->attrs[NL80211_ATTR_SSID])
10936 		return -EINVAL;
10937 
10938 	if (!rdev->ops->assoc)
10939 		return -EOPNOTSUPP;
10940 
10941 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10942 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10943 		return -EOPNOTSUPP;
10944 
10945 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10946 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10947 
10948 	if (info->attrs[NL80211_ATTR_IE]) {
10949 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10950 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10951 
10952 		if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
10953 					   req.ie, req.ie_len)) {
10954 			NL_SET_ERR_MSG_ATTR(info->extack,
10955 					    info->attrs[NL80211_ATTR_IE],
10956 					    "non-inheritance makes no sense");
10957 			return -EINVAL;
10958 		}
10959 	}
10960 
10961 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
10962 		enum nl80211_mfp mfp =
10963 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
10964 		if (mfp == NL80211_MFP_REQUIRED)
10965 			req.use_mfp = true;
10966 		else if (mfp != NL80211_MFP_NO)
10967 			return -EINVAL;
10968 	}
10969 
10970 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
10971 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
10972 
10973 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
10974 		req.flags |= ASSOC_REQ_DISABLE_HT;
10975 
10976 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10977 		memcpy(&req.ht_capa_mask,
10978 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
10979 		       sizeof(req.ht_capa_mask));
10980 
10981 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
10982 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10983 			return -EINVAL;
10984 		memcpy(&req.ht_capa,
10985 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
10986 		       sizeof(req.ht_capa));
10987 	}
10988 
10989 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
10990 		req.flags |= ASSOC_REQ_DISABLE_VHT;
10991 
10992 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
10993 		req.flags |= ASSOC_REQ_DISABLE_HE;
10994 
10995 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
10996 		req.flags |= ASSOC_REQ_DISABLE_EHT;
10997 
10998 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10999 		memcpy(&req.vht_capa_mask,
11000 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
11001 		       sizeof(req.vht_capa_mask));
11002 
11003 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
11004 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11005 			return -EINVAL;
11006 		memcpy(&req.vht_capa,
11007 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
11008 		       sizeof(req.vht_capa));
11009 	}
11010 
11011 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
11012 		if (!((rdev->wiphy.features &
11013 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
11014 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
11015 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11016 					     NL80211_EXT_FEATURE_RRM))
11017 			return -EINVAL;
11018 		req.flags |= ASSOC_REQ_USE_RRM;
11019 	}
11020 
11021 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
11022 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
11023 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
11024 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
11025 			return -EINVAL;
11026 		req.fils_nonces =
11027 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
11028 	}
11029 
11030 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]) {
11031 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY])
11032 			return -EINVAL;
11033 		memcpy(&req.s1g_capa_mask,
11034 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]),
11035 		       sizeof(req.s1g_capa_mask));
11036 	}
11037 
11038 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY]) {
11039 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK])
11040 			return -EINVAL;
11041 		memcpy(&req.s1g_capa,
11042 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY]),
11043 		       sizeof(req.s1g_capa));
11044 	}
11045 
11046 	if (nla_get_flag(info->attrs[NL80211_ATTR_ASSOC_SPP_AMSDU])) {
11047 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
11048 					     NL80211_EXT_FEATURE_SPP_AMSDU_SUPPORT)) {
11049 			GENL_SET_ERR_MSG(info, "SPP A-MSDUs not supported");
11050 			return -EINVAL;
11051 		}
11052 		req.flags |= ASSOC_REQ_SPP_AMSDU;
11053 	}
11054 
11055 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
11056 
11057 	if (info->attrs[NL80211_ATTR_MLO_LINKS]) {
11058 		unsigned int attrsize = NUM_NL80211_ATTR * sizeof(*attrs);
11059 		struct nlattr *link;
11060 		int rem = 0;
11061 
11062 		if (req.link_id < 0)
11063 			return -EINVAL;
11064 
11065 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
11066 			return -EINVAL;
11067 
11068 		if (info->attrs[NL80211_ATTR_MAC] ||
11069 		    info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
11070 		    !info->attrs[NL80211_ATTR_MLD_ADDR])
11071 			return -EINVAL;
11072 
11073 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
11074 		ap_addr = req.ap_mld_addr;
11075 
11076 		attrs = kzalloc(attrsize, GFP_KERNEL);
11077 		if (!attrs)
11078 			return -ENOMEM;
11079 
11080 		nla_for_each_nested(link,
11081 				    info->attrs[NL80211_ATTR_MLO_LINKS],
11082 				    rem) {
11083 			memset(attrs, 0, attrsize);
11084 
11085 			nla_parse_nested(attrs, NL80211_ATTR_MAX,
11086 					 link, NULL, NULL);
11087 
11088 			if (!attrs[NL80211_ATTR_MLO_LINK_ID]) {
11089 				err = -EINVAL;
11090 				NL_SET_BAD_ATTR(info->extack, link);
11091 				goto free;
11092 			}
11093 
11094 			link_id = nla_get_u8(attrs[NL80211_ATTR_MLO_LINK_ID]);
11095 			/* cannot use the same link ID again */
11096 			if (req.links[link_id].bss) {
11097 				err = -EINVAL;
11098 				NL_SET_BAD_ATTR(info->extack, link);
11099 				goto free;
11100 			}
11101 			req.links[link_id].bss =
11102 				nl80211_assoc_bss(rdev, ssid, ssid_len, attrs,
11103 						  req.link_id, link_id);
11104 			if (IS_ERR(req.links[link_id].bss)) {
11105 				err = PTR_ERR(req.links[link_id].bss);
11106 				req.links[link_id].bss = NULL;
11107 				NL_SET_ERR_MSG_ATTR(info->extack,
11108 						    link, "Error fetching BSS for link");
11109 				goto free;
11110 			}
11111 
11112 			if (attrs[NL80211_ATTR_IE]) {
11113 				req.links[link_id].elems =
11114 					nla_data(attrs[NL80211_ATTR_IE]);
11115 				req.links[link_id].elems_len =
11116 					nla_len(attrs[NL80211_ATTR_IE]);
11117 
11118 				if (cfg80211_find_elem(WLAN_EID_FRAGMENT,
11119 						       req.links[link_id].elems,
11120 						       req.links[link_id].elems_len)) {
11121 					NL_SET_ERR_MSG_ATTR(info->extack,
11122 							    attrs[NL80211_ATTR_IE],
11123 							    "cannot deal with fragmentation");
11124 					err = -EINVAL;
11125 					goto free;
11126 				}
11127 
11128 				if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
11129 							   req.links[link_id].elems,
11130 							   req.links[link_id].elems_len)) {
11131 					NL_SET_ERR_MSG_ATTR(info->extack,
11132 							    attrs[NL80211_ATTR_IE],
11133 							    "cannot deal with non-inheritance");
11134 					err = -EINVAL;
11135 					goto free;
11136 				}
11137 			}
11138 
11139 			req.links[link_id].disabled =
11140 				nla_get_flag(attrs[NL80211_ATTR_MLO_LINK_DISABLED]);
11141 		}
11142 
11143 		if (!req.links[req.link_id].bss) {
11144 			err = -EINVAL;
11145 			goto free;
11146 		}
11147 
11148 		if (req.links[req.link_id].elems_len) {
11149 			GENL_SET_ERR_MSG(info,
11150 					 "cannot have per-link elems on assoc link");
11151 			err = -EINVAL;
11152 			goto free;
11153 		}
11154 
11155 		if (req.links[req.link_id].disabled) {
11156 			GENL_SET_ERR_MSG(info,
11157 					 "cannot have assoc link disabled");
11158 			err = -EINVAL;
11159 			goto free;
11160 		}
11161 
11162 		kfree(attrs);
11163 		attrs = NULL;
11164 	} else {
11165 		if (req.link_id >= 0)
11166 			return -EINVAL;
11167 
11168 		req.bss = nl80211_assoc_bss(rdev, ssid, ssid_len, info->attrs,
11169 					    -1, -1);
11170 		if (IS_ERR(req.bss))
11171 			return PTR_ERR(req.bss);
11172 		ap_addr = req.bss->bssid;
11173 	}
11174 
11175 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
11176 	if (!err) {
11177 		struct nlattr *link;
11178 		int rem = 0;
11179 
11180 		err = cfg80211_mlme_assoc(rdev, dev, &req,
11181 					  info->extack);
11182 
11183 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
11184 			dev->ieee80211_ptr->conn_owner_nlportid =
11185 				info->snd_portid;
11186 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
11187 			       ap_addr, ETH_ALEN);
11188 		}
11189 
11190 		/* Report error from first problematic link */
11191 		if (info->attrs[NL80211_ATTR_MLO_LINKS]) {
11192 			nla_for_each_nested(link,
11193 					    info->attrs[NL80211_ATTR_MLO_LINKS],
11194 					    rem) {
11195 				struct nlattr *link_id_attr =
11196 					nla_find_nested(link, NL80211_ATTR_MLO_LINK_ID);
11197 
11198 				if (!link_id_attr)
11199 					continue;
11200 
11201 				link_id = nla_get_u8(link_id_attr);
11202 
11203 				if (link_id == req.link_id)
11204 					continue;
11205 
11206 				if (!req.links[link_id].error ||
11207 				    WARN_ON(req.links[link_id].error > 0))
11208 					continue;
11209 
11210 				WARN_ON(err >= 0);
11211 
11212 				NL_SET_BAD_ATTR(info->extack, link);
11213 				err = req.links[link_id].error;
11214 				break;
11215 			}
11216 		}
11217 	}
11218 
11219 free:
11220 	for (link_id = 0; link_id < ARRAY_SIZE(req.links); link_id++)
11221 		cfg80211_put_bss(&rdev->wiphy, req.links[link_id].bss);
11222 	cfg80211_put_bss(&rdev->wiphy, req.bss);
11223 	kfree(attrs);
11224 
11225 	return err;
11226 }
11227 
11228 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
11229 {
11230 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11231 	struct net_device *dev = info->user_ptr[1];
11232 	const u8 *ie = NULL, *bssid;
11233 	int ie_len = 0;
11234 	u16 reason_code;
11235 	bool local_state_change;
11236 
11237 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11238 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11239 		return -EPERM;
11240 
11241 	if (!info->attrs[NL80211_ATTR_MAC])
11242 		return -EINVAL;
11243 
11244 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11245 		return -EINVAL;
11246 
11247 	if (!rdev->ops->deauth)
11248 		return -EOPNOTSUPP;
11249 
11250 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11251 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11252 		return -EOPNOTSUPP;
11253 
11254 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11255 
11256 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11257 	if (reason_code == 0) {
11258 		/* Reason Code 0 is reserved */
11259 		return -EINVAL;
11260 	}
11261 
11262 	if (info->attrs[NL80211_ATTR_IE]) {
11263 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11264 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11265 	}
11266 
11267 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11268 
11269 	return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
11270 				    local_state_change);
11271 }
11272 
11273 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
11274 {
11275 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11276 	struct net_device *dev = info->user_ptr[1];
11277 	const u8 *ie = NULL, *bssid;
11278 	int ie_len = 0;
11279 	u16 reason_code;
11280 	bool local_state_change;
11281 
11282 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11283 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11284 		return -EPERM;
11285 
11286 	if (!info->attrs[NL80211_ATTR_MAC])
11287 		return -EINVAL;
11288 
11289 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11290 		return -EINVAL;
11291 
11292 	if (!rdev->ops->disassoc)
11293 		return -EOPNOTSUPP;
11294 
11295 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11296 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11297 		return -EOPNOTSUPP;
11298 
11299 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11300 
11301 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11302 	if (reason_code == 0) {
11303 		/* Reason Code 0 is reserved */
11304 		return -EINVAL;
11305 	}
11306 
11307 	if (info->attrs[NL80211_ATTR_IE]) {
11308 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11309 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11310 	}
11311 
11312 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11313 
11314 	return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
11315 				      local_state_change);
11316 }
11317 
11318 static bool
11319 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
11320 			 int mcast_rate[NUM_NL80211_BANDS],
11321 			 int rateval)
11322 {
11323 	struct wiphy *wiphy = &rdev->wiphy;
11324 	bool found = false;
11325 	int band, i;
11326 
11327 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
11328 		struct ieee80211_supported_band *sband;
11329 
11330 		sband = wiphy->bands[band];
11331 		if (!sband)
11332 			continue;
11333 
11334 		for (i = 0; i < sband->n_bitrates; i++) {
11335 			if (sband->bitrates[i].bitrate == rateval) {
11336 				mcast_rate[band] = i + 1;
11337 				found = true;
11338 				break;
11339 			}
11340 		}
11341 	}
11342 
11343 	return found;
11344 }
11345 
11346 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
11347 {
11348 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11349 	struct net_device *dev = info->user_ptr[1];
11350 	struct cfg80211_ibss_params ibss;
11351 	struct wiphy *wiphy;
11352 	struct cfg80211_cached_keys *connkeys = NULL;
11353 	int err;
11354 
11355 	memset(&ibss, 0, sizeof(ibss));
11356 
11357 	if (!info->attrs[NL80211_ATTR_SSID] ||
11358 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11359 		return -EINVAL;
11360 
11361 	ibss.beacon_interval = 100;
11362 
11363 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
11364 		ibss.beacon_interval =
11365 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11366 
11367 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
11368 					   ibss.beacon_interval);
11369 	if (err)
11370 		return err;
11371 
11372 	if (!rdev->ops->join_ibss)
11373 		return -EOPNOTSUPP;
11374 
11375 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11376 		return -EOPNOTSUPP;
11377 
11378 	wiphy = &rdev->wiphy;
11379 
11380 	if (info->attrs[NL80211_ATTR_MAC]) {
11381 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11382 
11383 		if (!is_valid_ether_addr(ibss.bssid))
11384 			return -EINVAL;
11385 	}
11386 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11387 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11388 
11389 	if (info->attrs[NL80211_ATTR_IE]) {
11390 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11391 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11392 	}
11393 
11394 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
11395 	if (err)
11396 		return err;
11397 
11398 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
11399 				     NL80211_IFTYPE_ADHOC))
11400 		return -EINVAL;
11401 
11402 	switch (ibss.chandef.width) {
11403 	case NL80211_CHAN_WIDTH_5:
11404 	case NL80211_CHAN_WIDTH_10:
11405 	case NL80211_CHAN_WIDTH_20_NOHT:
11406 		break;
11407 	case NL80211_CHAN_WIDTH_20:
11408 	case NL80211_CHAN_WIDTH_40:
11409 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11410 			return -EINVAL;
11411 		break;
11412 	case NL80211_CHAN_WIDTH_80:
11413 	case NL80211_CHAN_WIDTH_80P80:
11414 	case NL80211_CHAN_WIDTH_160:
11415 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11416 			return -EINVAL;
11417 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
11418 					     NL80211_EXT_FEATURE_VHT_IBSS))
11419 			return -EINVAL;
11420 		break;
11421 	case NL80211_CHAN_WIDTH_320:
11422 		return -EINVAL;
11423 	default:
11424 		return -EINVAL;
11425 	}
11426 
11427 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
11428 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
11429 
11430 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11431 		u8 *rates =
11432 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11433 		int n_rates =
11434 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11435 		struct ieee80211_supported_band *sband =
11436 			wiphy->bands[ibss.chandef.chan->band];
11437 
11438 		err = ieee80211_get_ratemask(sband, rates, n_rates,
11439 					     &ibss.basic_rates);
11440 		if (err)
11441 			return err;
11442 	}
11443 
11444 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11445 		memcpy(&ibss.ht_capa_mask,
11446 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11447 		       sizeof(ibss.ht_capa_mask));
11448 
11449 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11450 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11451 			return -EINVAL;
11452 		memcpy(&ibss.ht_capa,
11453 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11454 		       sizeof(ibss.ht_capa));
11455 	}
11456 
11457 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11458 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
11459 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11460 		return -EINVAL;
11461 
11462 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11463 		bool no_ht = false;
11464 
11465 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
11466 		if (IS_ERR(connkeys))
11467 			return PTR_ERR(connkeys);
11468 
11469 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
11470 		    no_ht) {
11471 			kfree_sensitive(connkeys);
11472 			return -EINVAL;
11473 		}
11474 	}
11475 
11476 	ibss.control_port =
11477 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
11478 
11479 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11480 		int r = validate_pae_over_nl80211(rdev, info);
11481 
11482 		if (r < 0) {
11483 			kfree_sensitive(connkeys);
11484 			return r;
11485 		}
11486 
11487 		ibss.control_port_over_nl80211 = true;
11488 	}
11489 
11490 	ibss.userspace_handles_dfs =
11491 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11492 
11493 	err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
11494 	if (err)
11495 		kfree_sensitive(connkeys);
11496 	else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
11497 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11498 
11499 	return err;
11500 }
11501 
11502 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
11503 {
11504 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11505 	struct net_device *dev = info->user_ptr[1];
11506 
11507 	if (!rdev->ops->leave_ibss)
11508 		return -EOPNOTSUPP;
11509 
11510 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11511 		return -EOPNOTSUPP;
11512 
11513 	return cfg80211_leave_ibss(rdev, dev, false);
11514 }
11515 
11516 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
11517 {
11518 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11519 	struct net_device *dev = info->user_ptr[1];
11520 	int mcast_rate[NUM_NL80211_BANDS];
11521 	u32 nla_rate;
11522 
11523 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
11524 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
11525 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
11526 		return -EOPNOTSUPP;
11527 
11528 	if (!rdev->ops->set_mcast_rate)
11529 		return -EOPNOTSUPP;
11530 
11531 	memset(mcast_rate, 0, sizeof(mcast_rate));
11532 
11533 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
11534 		return -EINVAL;
11535 
11536 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
11537 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
11538 		return -EINVAL;
11539 
11540 	return rdev_set_mcast_rate(rdev, dev, mcast_rate);
11541 }
11542 
11543 static struct sk_buff *
11544 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
11545 			    struct wireless_dev *wdev, int approxlen,
11546 			    u32 portid, u32 seq, enum nl80211_commands cmd,
11547 			    enum nl80211_attrs attr,
11548 			    const struct nl80211_vendor_cmd_info *info,
11549 			    gfp_t gfp)
11550 {
11551 	struct sk_buff *skb;
11552 	void *hdr;
11553 	struct nlattr *data;
11554 
11555 	skb = nlmsg_new(approxlen + 100, gfp);
11556 	if (!skb)
11557 		return NULL;
11558 
11559 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
11560 	if (!hdr) {
11561 		kfree_skb(skb);
11562 		return NULL;
11563 	}
11564 
11565 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11566 		goto nla_put_failure;
11567 
11568 	if (info) {
11569 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
11570 				info->vendor_id))
11571 			goto nla_put_failure;
11572 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
11573 				info->subcmd))
11574 			goto nla_put_failure;
11575 	}
11576 
11577 	if (wdev) {
11578 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
11579 				      wdev_id(wdev), NL80211_ATTR_PAD))
11580 			goto nla_put_failure;
11581 		if (wdev->netdev &&
11582 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
11583 				wdev->netdev->ifindex))
11584 			goto nla_put_failure;
11585 	}
11586 
11587 	data = nla_nest_start_noflag(skb, attr);
11588 	if (!data)
11589 		goto nla_put_failure;
11590 
11591 	((void **)skb->cb)[0] = rdev;
11592 	((void **)skb->cb)[1] = hdr;
11593 	((void **)skb->cb)[2] = data;
11594 
11595 	return skb;
11596 
11597  nla_put_failure:
11598 	kfree_skb(skb);
11599 	return NULL;
11600 }
11601 
11602 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
11603 					   struct wireless_dev *wdev,
11604 					   enum nl80211_commands cmd,
11605 					   enum nl80211_attrs attr,
11606 					   unsigned int portid,
11607 					   int vendor_event_idx,
11608 					   int approxlen, gfp_t gfp)
11609 {
11610 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11611 	const struct nl80211_vendor_cmd_info *info;
11612 
11613 	switch (cmd) {
11614 	case NL80211_CMD_TESTMODE:
11615 		if (WARN_ON(vendor_event_idx != -1))
11616 			return NULL;
11617 		info = NULL;
11618 		break;
11619 	case NL80211_CMD_VENDOR:
11620 		if (WARN_ON(vendor_event_idx < 0 ||
11621 			    vendor_event_idx >= wiphy->n_vendor_events))
11622 			return NULL;
11623 		info = &wiphy->vendor_events[vendor_event_idx];
11624 		break;
11625 	default:
11626 		WARN_ON(1);
11627 		return NULL;
11628 	}
11629 
11630 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
11631 					   cmd, attr, info, gfp);
11632 }
11633 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
11634 
11635 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
11636 {
11637 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
11638 	void *hdr = ((void **)skb->cb)[1];
11639 	struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
11640 	struct nlattr *data = ((void **)skb->cb)[2];
11641 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
11642 
11643 	/* clear CB data for netlink core to own from now on */
11644 	memset(skb->cb, 0, sizeof(skb->cb));
11645 
11646 	nla_nest_end(skb, data);
11647 	genlmsg_end(skb, hdr);
11648 
11649 	if (nlhdr->nlmsg_pid) {
11650 		genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
11651 				nlhdr->nlmsg_pid);
11652 	} else {
11653 		if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
11654 			mcgrp = NL80211_MCGRP_VENDOR;
11655 
11656 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11657 					skb, 0, mcgrp, gfp);
11658 	}
11659 }
11660 EXPORT_SYMBOL(__cfg80211_send_event_skb);
11661 
11662 #ifdef CONFIG_NL80211_TESTMODE
11663 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
11664 {
11665 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11666 	struct wireless_dev *wdev;
11667 	int err;
11668 
11669 	lockdep_assert_held(&rdev->wiphy.mtx);
11670 
11671 	wdev = __cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
11672 					  info->attrs);
11673 
11674 	if (!rdev->ops->testmode_cmd)
11675 		return -EOPNOTSUPP;
11676 
11677 	if (IS_ERR(wdev)) {
11678 		err = PTR_ERR(wdev);
11679 		if (err != -EINVAL)
11680 			return err;
11681 		wdev = NULL;
11682 	} else if (wdev->wiphy != &rdev->wiphy) {
11683 		return -EINVAL;
11684 	}
11685 
11686 	if (!info->attrs[NL80211_ATTR_TESTDATA])
11687 		return -EINVAL;
11688 
11689 	rdev->cur_cmd_info = info;
11690 	err = rdev_testmode_cmd(rdev, wdev,
11691 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
11692 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
11693 	rdev->cur_cmd_info = NULL;
11694 
11695 	return err;
11696 }
11697 
11698 static int nl80211_testmode_dump(struct sk_buff *skb,
11699 				 struct netlink_callback *cb)
11700 {
11701 	struct cfg80211_registered_device *rdev;
11702 	struct nlattr **attrbuf = NULL;
11703 	int err;
11704 	long phy_idx;
11705 	void *data = NULL;
11706 	int data_len = 0;
11707 
11708 	rtnl_lock();
11709 
11710 	if (cb->args[0]) {
11711 		/*
11712 		 * 0 is a valid index, but not valid for args[0],
11713 		 * so we need to offset by 1.
11714 		 */
11715 		phy_idx = cb->args[0] - 1;
11716 
11717 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
11718 		if (!rdev) {
11719 			err = -ENOENT;
11720 			goto out_err;
11721 		}
11722 	} else {
11723 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
11724 				  GFP_KERNEL);
11725 		if (!attrbuf) {
11726 			err = -ENOMEM;
11727 			goto out_err;
11728 		}
11729 
11730 		err = nlmsg_parse_deprecated(cb->nlh,
11731 					     GENL_HDRLEN + nl80211_fam.hdrsize,
11732 					     attrbuf, nl80211_fam.maxattr,
11733 					     nl80211_policy, NULL);
11734 		if (err)
11735 			goto out_err;
11736 
11737 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
11738 		if (IS_ERR(rdev)) {
11739 			err = PTR_ERR(rdev);
11740 			goto out_err;
11741 		}
11742 		phy_idx = rdev->wiphy_idx;
11743 
11744 		if (attrbuf[NL80211_ATTR_TESTDATA])
11745 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
11746 	}
11747 
11748 	if (cb->args[1]) {
11749 		data = nla_data((void *)cb->args[1]);
11750 		data_len = nla_len((void *)cb->args[1]);
11751 	}
11752 
11753 	if (!rdev->ops->testmode_dump) {
11754 		err = -EOPNOTSUPP;
11755 		goto out_err;
11756 	}
11757 
11758 	while (1) {
11759 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
11760 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
11761 					   NL80211_CMD_TESTMODE);
11762 		struct nlattr *tmdata;
11763 
11764 		if (!hdr)
11765 			break;
11766 
11767 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
11768 			genlmsg_cancel(skb, hdr);
11769 			break;
11770 		}
11771 
11772 		tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
11773 		if (!tmdata) {
11774 			genlmsg_cancel(skb, hdr);
11775 			break;
11776 		}
11777 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
11778 		nla_nest_end(skb, tmdata);
11779 
11780 		if (err == -ENOBUFS || err == -ENOENT) {
11781 			genlmsg_cancel(skb, hdr);
11782 			break;
11783 		} else if (err) {
11784 			genlmsg_cancel(skb, hdr);
11785 			goto out_err;
11786 		}
11787 
11788 		genlmsg_end(skb, hdr);
11789 	}
11790 
11791 	err = skb->len;
11792 	/* see above */
11793 	cb->args[0] = phy_idx + 1;
11794  out_err:
11795 	kfree(attrbuf);
11796 	rtnl_unlock();
11797 	return err;
11798 }
11799 #endif
11800 
11801 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
11802 {
11803 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11804 	struct net_device *dev = info->user_ptr[1];
11805 	struct cfg80211_connect_params connect;
11806 	struct wiphy *wiphy;
11807 	struct cfg80211_cached_keys *connkeys = NULL;
11808 	u32 freq = 0;
11809 	int err;
11810 
11811 	memset(&connect, 0, sizeof(connect));
11812 
11813 	if (!info->attrs[NL80211_ATTR_SSID] ||
11814 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11815 		return -EINVAL;
11816 
11817 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
11818 		connect.auth_type =
11819 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
11820 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
11821 					     NL80211_CMD_CONNECT))
11822 			return -EINVAL;
11823 	} else
11824 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
11825 
11826 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
11827 
11828 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
11829 	    !wiphy_ext_feature_isset(&rdev->wiphy,
11830 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
11831 		return -EINVAL;
11832 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
11833 
11834 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
11835 				      NL80211_MAX_NR_CIPHER_SUITES);
11836 	if (err)
11837 		return err;
11838 
11839 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11840 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11841 		return -EOPNOTSUPP;
11842 
11843 	wiphy = &rdev->wiphy;
11844 
11845 	connect.bg_scan_period = -1;
11846 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
11847 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
11848 		connect.bg_scan_period =
11849 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
11850 	}
11851 
11852 	if (info->attrs[NL80211_ATTR_MAC])
11853 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11854 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
11855 		connect.bssid_hint =
11856 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
11857 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11858 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11859 
11860 	if (info->attrs[NL80211_ATTR_IE]) {
11861 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11862 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11863 	}
11864 
11865 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
11866 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
11867 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
11868 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11869 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
11870 			return -EOPNOTSUPP;
11871 	} else {
11872 		connect.mfp = NL80211_MFP_NO;
11873 	}
11874 
11875 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
11876 		connect.prev_bssid =
11877 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
11878 
11879 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ])
11880 		freq = MHZ_TO_KHZ(nla_get_u32(
11881 					info->attrs[NL80211_ATTR_WIPHY_FREQ]));
11882 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
11883 		freq +=
11884 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
11885 
11886 	if (freq) {
11887 		connect.channel = nl80211_get_valid_chan(wiphy, freq);
11888 		if (!connect.channel)
11889 			return -EINVAL;
11890 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
11891 		freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
11892 		freq = MHZ_TO_KHZ(freq);
11893 		connect.channel_hint = nl80211_get_valid_chan(wiphy, freq);
11894 		if (!connect.channel_hint)
11895 			return -EINVAL;
11896 	}
11897 
11898 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
11899 		connect.edmg.channels =
11900 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
11901 
11902 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
11903 			connect.edmg.bw_config =
11904 				nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
11905 	}
11906 
11907 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11908 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
11909 		if (IS_ERR(connkeys))
11910 			return PTR_ERR(connkeys);
11911 	}
11912 
11913 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
11914 		connect.flags |= ASSOC_REQ_DISABLE_HT;
11915 
11916 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11917 		memcpy(&connect.ht_capa_mask,
11918 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11919 		       sizeof(connect.ht_capa_mask));
11920 
11921 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11922 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
11923 			kfree_sensitive(connkeys);
11924 			return -EINVAL;
11925 		}
11926 		memcpy(&connect.ht_capa,
11927 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11928 		       sizeof(connect.ht_capa));
11929 	}
11930 
11931 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
11932 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
11933 
11934 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
11935 		connect.flags |= ASSOC_REQ_DISABLE_HE;
11936 
11937 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
11938 		connect.flags |= ASSOC_REQ_DISABLE_EHT;
11939 
11940 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11941 		memcpy(&connect.vht_capa_mask,
11942 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
11943 		       sizeof(connect.vht_capa_mask));
11944 
11945 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
11946 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
11947 			kfree_sensitive(connkeys);
11948 			return -EINVAL;
11949 		}
11950 		memcpy(&connect.vht_capa,
11951 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
11952 		       sizeof(connect.vht_capa));
11953 	}
11954 
11955 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
11956 		if (!((rdev->wiphy.features &
11957 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
11958 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
11959 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11960 					     NL80211_EXT_FEATURE_RRM)) {
11961 			kfree_sensitive(connkeys);
11962 			return -EINVAL;
11963 		}
11964 		connect.flags |= ASSOC_REQ_USE_RRM;
11965 	}
11966 
11967 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
11968 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
11969 		kfree_sensitive(connkeys);
11970 		return -EOPNOTSUPP;
11971 	}
11972 
11973 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
11974 		/* bss selection makes no sense if bssid is set */
11975 		if (connect.bssid) {
11976 			kfree_sensitive(connkeys);
11977 			return -EINVAL;
11978 		}
11979 
11980 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
11981 				       wiphy, &connect.bss_select);
11982 		if (err) {
11983 			kfree_sensitive(connkeys);
11984 			return err;
11985 		}
11986 	}
11987 
11988 	if (wiphy_ext_feature_isset(&rdev->wiphy,
11989 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
11990 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
11991 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
11992 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
11993 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11994 		connect.fils_erp_username =
11995 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11996 		connect.fils_erp_username_len =
11997 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11998 		connect.fils_erp_realm =
11999 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12000 		connect.fils_erp_realm_len =
12001 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12002 		connect.fils_erp_next_seq_num =
12003 			nla_get_u16(
12004 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
12005 		connect.fils_erp_rrk =
12006 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12007 		connect.fils_erp_rrk_len =
12008 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12009 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
12010 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
12011 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
12012 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12013 		kfree_sensitive(connkeys);
12014 		return -EINVAL;
12015 	}
12016 
12017 	if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
12018 		if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
12019 			kfree_sensitive(connkeys);
12020 			GENL_SET_ERR_MSG(info,
12021 					 "external auth requires connection ownership");
12022 			return -EINVAL;
12023 		}
12024 		connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
12025 	}
12026 
12027 	if (nla_get_flag(info->attrs[NL80211_ATTR_MLO_SUPPORT]))
12028 		connect.flags |= CONNECT_REQ_MLO_SUPPORT;
12029 
12030 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
12031 			       connect.prev_bssid);
12032 	if (err)
12033 		kfree_sensitive(connkeys);
12034 
12035 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
12036 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
12037 		if (connect.bssid)
12038 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
12039 			       connect.bssid, ETH_ALEN);
12040 		else
12041 			eth_zero_addr(dev->ieee80211_ptr->disconnect_bssid);
12042 	}
12043 
12044 	return err;
12045 }
12046 
12047 static int nl80211_update_connect_params(struct sk_buff *skb,
12048 					 struct genl_info *info)
12049 {
12050 	struct cfg80211_connect_params connect = {};
12051 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12052 	struct net_device *dev = info->user_ptr[1];
12053 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12054 	bool fils_sk_offload;
12055 	u32 auth_type;
12056 	u32 changed = 0;
12057 
12058 	if (!rdev->ops->update_connect_params)
12059 		return -EOPNOTSUPP;
12060 
12061 	if (info->attrs[NL80211_ATTR_IE]) {
12062 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
12063 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
12064 		changed |= UPDATE_ASSOC_IES;
12065 	}
12066 
12067 	fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
12068 						  NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
12069 
12070 	/*
12071 	 * when driver supports fils-sk offload all attributes must be
12072 	 * provided. So the else covers "fils-sk-not-all" and
12073 	 * "no-fils-sk-any".
12074 	 */
12075 	if (fils_sk_offload &&
12076 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
12077 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
12078 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
12079 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12080 		connect.fils_erp_username =
12081 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12082 		connect.fils_erp_username_len =
12083 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12084 		connect.fils_erp_realm =
12085 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12086 		connect.fils_erp_realm_len =
12087 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12088 		connect.fils_erp_next_seq_num =
12089 			nla_get_u16(
12090 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
12091 		connect.fils_erp_rrk =
12092 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12093 		connect.fils_erp_rrk_len =
12094 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12095 		changed |= UPDATE_FILS_ERP_INFO;
12096 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
12097 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
12098 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
12099 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12100 		return -EINVAL;
12101 	}
12102 
12103 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
12104 		auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
12105 		if (!nl80211_valid_auth_type(rdev, auth_type,
12106 					     NL80211_CMD_CONNECT))
12107 			return -EINVAL;
12108 
12109 		if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
12110 		    fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
12111 			return -EINVAL;
12112 
12113 		connect.auth_type = auth_type;
12114 		changed |= UPDATE_AUTH_TYPE;
12115 	}
12116 
12117 	if (!wdev->connected)
12118 		return -ENOLINK;
12119 
12120 	return rdev_update_connect_params(rdev, dev, &connect, changed);
12121 }
12122 
12123 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
12124 {
12125 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12126 	struct net_device *dev = info->user_ptr[1];
12127 	u16 reason;
12128 
12129 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
12130 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
12131 		return -EPERM;
12132 
12133 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
12134 		reason = WLAN_REASON_DEAUTH_LEAVING;
12135 	else
12136 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
12137 
12138 	if (reason == 0)
12139 		return -EINVAL;
12140 
12141 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12142 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
12143 		return -EOPNOTSUPP;
12144 
12145 	return cfg80211_disconnect(rdev, dev, reason, true);
12146 }
12147 
12148 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
12149 {
12150 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12151 	struct net *net;
12152 	int err;
12153 
12154 	if (info->attrs[NL80211_ATTR_PID]) {
12155 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
12156 
12157 		net = get_net_ns_by_pid(pid);
12158 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
12159 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
12160 
12161 		net = get_net_ns_by_fd(fd);
12162 	} else {
12163 		return -EINVAL;
12164 	}
12165 
12166 	if (IS_ERR(net))
12167 		return PTR_ERR(net);
12168 
12169 	err = 0;
12170 
12171 	/* check if anything to do */
12172 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
12173 		err = cfg80211_switch_netns(rdev, net);
12174 
12175 	put_net(net);
12176 	return err;
12177 }
12178 
12179 static int nl80211_set_pmksa(struct sk_buff *skb, struct genl_info *info)
12180 {
12181 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12182 	struct net_device *dev = info->user_ptr[1];
12183 	struct cfg80211_pmksa pmksa;
12184 	bool ap_pmksa_caching_support = false;
12185 
12186 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
12187 
12188 	ap_pmksa_caching_support = wiphy_ext_feature_isset(&rdev->wiphy,
12189 		NL80211_EXT_FEATURE_AP_PMKSA_CACHING);
12190 
12191 	if (!info->attrs[NL80211_ATTR_PMKID])
12192 		return -EINVAL;
12193 
12194 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
12195 
12196 	if (info->attrs[NL80211_ATTR_MAC]) {
12197 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
12198 	} else if (info->attrs[NL80211_ATTR_SSID] &&
12199 	           info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
12200 	           info->attrs[NL80211_ATTR_PMK]) {
12201 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
12202 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
12203 		pmksa.cache_id = nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
12204 	} else {
12205 		return -EINVAL;
12206 	}
12207 
12208 	if (info->attrs[NL80211_ATTR_PMK]) {
12209 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
12210 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
12211 	}
12212 
12213 	if (info->attrs[NL80211_ATTR_PMK_LIFETIME])
12214 		pmksa.pmk_lifetime =
12215 			nla_get_u32(info->attrs[NL80211_ATTR_PMK_LIFETIME]);
12216 
12217 	if (info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD])
12218 		pmksa.pmk_reauth_threshold =
12219 			nla_get_u8(info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD]);
12220 
12221 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12222 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
12223 	    !((dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP ||
12224 	       dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO) &&
12225 	       ap_pmksa_caching_support))
12226 		return -EOPNOTSUPP;
12227 
12228 	if (!rdev->ops->set_pmksa)
12229 		return -EOPNOTSUPP;
12230 
12231 	return rdev_set_pmksa(rdev, dev, &pmksa);
12232 }
12233 
12234 static int nl80211_del_pmksa(struct sk_buff *skb, struct genl_info *info)
12235 {
12236 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12237 	struct net_device *dev = info->user_ptr[1];
12238 	struct cfg80211_pmksa pmksa;
12239 	bool sae_offload_support = false;
12240 	bool owe_offload_support = false;
12241 	bool ap_pmksa_caching_support = false;
12242 
12243 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
12244 
12245 	sae_offload_support = wiphy_ext_feature_isset(&rdev->wiphy,
12246 		NL80211_EXT_FEATURE_SAE_OFFLOAD);
12247 	owe_offload_support = wiphy_ext_feature_isset(&rdev->wiphy,
12248 		NL80211_EXT_FEATURE_OWE_OFFLOAD);
12249 	ap_pmksa_caching_support = wiphy_ext_feature_isset(&rdev->wiphy,
12250 		NL80211_EXT_FEATURE_AP_PMKSA_CACHING);
12251 
12252 	if (info->attrs[NL80211_ATTR_PMKID])
12253 		pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
12254 
12255 	if (info->attrs[NL80211_ATTR_MAC]) {
12256 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
12257 	} else if (info->attrs[NL80211_ATTR_SSID]) {
12258 		/* SSID based pmksa flush suppported only for FILS,
12259 		 * OWE/SAE OFFLOAD cases
12260 		 */
12261 		if (info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
12262 		    info->attrs[NL80211_ATTR_PMK]) {
12263 			pmksa.cache_id = nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
12264 		} else if (!sae_offload_support && !owe_offload_support) {
12265 			return -EINVAL;
12266 		}
12267 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
12268 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
12269 	} else {
12270 		return -EINVAL;
12271 	}
12272 
12273 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12274 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
12275 	    !((dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP ||
12276 	       dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO) &&
12277 	       ap_pmksa_caching_support))
12278 		return -EOPNOTSUPP;
12279 
12280 	if (!rdev->ops->del_pmksa)
12281 		return -EOPNOTSUPP;
12282 
12283 	return rdev_del_pmksa(rdev, dev, &pmksa);
12284 }
12285 
12286 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
12287 {
12288 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12289 	struct net_device *dev = info->user_ptr[1];
12290 
12291 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12292 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
12293 		return -EOPNOTSUPP;
12294 
12295 	if (!rdev->ops->flush_pmksa)
12296 		return -EOPNOTSUPP;
12297 
12298 	return rdev_flush_pmksa(rdev, dev);
12299 }
12300 
12301 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
12302 {
12303 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12304 	struct net_device *dev = info->user_ptr[1];
12305 	u8 action_code, dialog_token;
12306 	u32 peer_capability = 0;
12307 	u16 status_code;
12308 	u8 *peer;
12309 	int link_id;
12310 	bool initiator;
12311 
12312 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12313 	    !rdev->ops->tdls_mgmt)
12314 		return -EOPNOTSUPP;
12315 
12316 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
12317 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
12318 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
12319 	    !info->attrs[NL80211_ATTR_IE] ||
12320 	    !info->attrs[NL80211_ATTR_MAC])
12321 		return -EINVAL;
12322 
12323 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12324 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
12325 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
12326 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
12327 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
12328 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
12329 		peer_capability =
12330 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
12331 	link_id = nl80211_link_id_or_invalid(info->attrs);
12332 
12333 	return rdev_tdls_mgmt(rdev, dev, peer, link_id, action_code,
12334 			      dialog_token, status_code, peer_capability,
12335 			      initiator,
12336 			      nla_data(info->attrs[NL80211_ATTR_IE]),
12337 			      nla_len(info->attrs[NL80211_ATTR_IE]));
12338 }
12339 
12340 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
12341 {
12342 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12343 	struct net_device *dev = info->user_ptr[1];
12344 	enum nl80211_tdls_operation operation;
12345 	u8 *peer;
12346 
12347 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12348 	    !rdev->ops->tdls_oper)
12349 		return -EOPNOTSUPP;
12350 
12351 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
12352 	    !info->attrs[NL80211_ATTR_MAC])
12353 		return -EINVAL;
12354 
12355 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
12356 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12357 
12358 	return rdev_tdls_oper(rdev, dev, peer, operation);
12359 }
12360 
12361 static int nl80211_remain_on_channel(struct sk_buff *skb,
12362 				     struct genl_info *info)
12363 {
12364 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12365 	unsigned int link_id = nl80211_link_id(info->attrs);
12366 	struct wireless_dev *wdev = info->user_ptr[1];
12367 	struct cfg80211_chan_def chandef;
12368 	struct sk_buff *msg;
12369 	void *hdr;
12370 	u64 cookie;
12371 	u32 duration;
12372 	int err;
12373 
12374 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
12375 	    !info->attrs[NL80211_ATTR_DURATION])
12376 		return -EINVAL;
12377 
12378 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12379 
12380 	if (!rdev->ops->remain_on_channel ||
12381 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
12382 		return -EOPNOTSUPP;
12383 
12384 	/*
12385 	 * We should be on that channel for at least a minimum amount of
12386 	 * time (10ms) but no longer than the driver supports.
12387 	 */
12388 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12389 	    duration > rdev->wiphy.max_remain_on_channel_duration)
12390 		return -EINVAL;
12391 
12392 	err = nl80211_parse_chandef(rdev, info, &chandef);
12393 	if (err)
12394 		return err;
12395 
12396 	if (!cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12397 		const struct cfg80211_chan_def *oper_chandef, *compat_chandef;
12398 
12399 		oper_chandef = wdev_chandef(wdev, link_id);
12400 
12401 		if (WARN_ON(!oper_chandef)) {
12402 			/* cannot happen since we must beacon to get here */
12403 			WARN_ON(1);
12404 			return -EBUSY;
12405 		}
12406 
12407 		/* note: returns first one if identical chandefs */
12408 		compat_chandef = cfg80211_chandef_compatible(&chandef,
12409 							     oper_chandef);
12410 
12411 		if (compat_chandef != &chandef)
12412 			return -EBUSY;
12413 	}
12414 
12415 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12416 	if (!msg)
12417 		return -ENOMEM;
12418 
12419 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12420 			     NL80211_CMD_REMAIN_ON_CHANNEL);
12421 	if (!hdr) {
12422 		err = -ENOBUFS;
12423 		goto free_msg;
12424 	}
12425 
12426 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
12427 				     duration, &cookie);
12428 
12429 	if (err)
12430 		goto free_msg;
12431 
12432 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12433 			      NL80211_ATTR_PAD))
12434 		goto nla_put_failure;
12435 
12436 	genlmsg_end(msg, hdr);
12437 
12438 	return genlmsg_reply(msg, info);
12439 
12440  nla_put_failure:
12441 	err = -ENOBUFS;
12442  free_msg:
12443 	nlmsg_free(msg);
12444 	return err;
12445 }
12446 
12447 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
12448 					    struct genl_info *info)
12449 {
12450 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12451 	struct wireless_dev *wdev = info->user_ptr[1];
12452 	u64 cookie;
12453 
12454 	if (!info->attrs[NL80211_ATTR_COOKIE])
12455 		return -EINVAL;
12456 
12457 	if (!rdev->ops->cancel_remain_on_channel)
12458 		return -EOPNOTSUPP;
12459 
12460 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12461 
12462 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
12463 }
12464 
12465 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
12466 				       struct genl_info *info)
12467 {
12468 	struct cfg80211_bitrate_mask mask;
12469 	unsigned int link_id = nl80211_link_id(info->attrs);
12470 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12471 	struct net_device *dev = info->user_ptr[1];
12472 	int err;
12473 
12474 	if (!rdev->ops->set_bitrate_mask)
12475 		return -EOPNOTSUPP;
12476 
12477 	err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
12478 					    NL80211_ATTR_TX_RATES, &mask,
12479 					    dev, true, link_id);
12480 	if (err)
12481 		return err;
12482 
12483 	return rdev_set_bitrate_mask(rdev, dev, link_id, NULL, &mask);
12484 }
12485 
12486 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
12487 {
12488 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12489 	struct wireless_dev *wdev = info->user_ptr[1];
12490 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
12491 
12492 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
12493 		return -EINVAL;
12494 
12495 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
12496 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
12497 
12498 	switch (wdev->iftype) {
12499 	case NL80211_IFTYPE_STATION:
12500 	case NL80211_IFTYPE_ADHOC:
12501 	case NL80211_IFTYPE_P2P_CLIENT:
12502 	case NL80211_IFTYPE_AP:
12503 	case NL80211_IFTYPE_AP_VLAN:
12504 	case NL80211_IFTYPE_MESH_POINT:
12505 	case NL80211_IFTYPE_P2P_GO:
12506 	case NL80211_IFTYPE_P2P_DEVICE:
12507 		break;
12508 	case NL80211_IFTYPE_NAN:
12509 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12510 					     NL80211_EXT_FEATURE_SECURE_NAN))
12511 			return -EOPNOTSUPP;
12512 		break;
12513 	default:
12514 		return -EOPNOTSUPP;
12515 	}
12516 
12517 	/* not much point in registering if we can't reply */
12518 	if (!rdev->ops->mgmt_tx)
12519 		return -EOPNOTSUPP;
12520 
12521 	if (info->attrs[NL80211_ATTR_RECEIVE_MULTICAST] &&
12522 	    !wiphy_ext_feature_isset(&rdev->wiphy,
12523 				     NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS)) {
12524 		GENL_SET_ERR_MSG(info,
12525 				 "multicast RX registrations are not supported");
12526 		return -EOPNOTSUPP;
12527 	}
12528 
12529 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
12530 					   nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12531 					   nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12532 					   info->attrs[NL80211_ATTR_RECEIVE_MULTICAST],
12533 					   info->extack);
12534 }
12535 
12536 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
12537 {
12538 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12539 	struct wireless_dev *wdev = info->user_ptr[1];
12540 	struct cfg80211_chan_def chandef;
12541 	int err;
12542 	void *hdr = NULL;
12543 	u64 cookie;
12544 	struct sk_buff *msg = NULL;
12545 	struct cfg80211_mgmt_tx_params params = {
12546 		.dont_wait_for_ack =
12547 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
12548 	};
12549 
12550 	if (!info->attrs[NL80211_ATTR_FRAME])
12551 		return -EINVAL;
12552 
12553 	if (!rdev->ops->mgmt_tx)
12554 		return -EOPNOTSUPP;
12555 
12556 	switch (wdev->iftype) {
12557 	case NL80211_IFTYPE_P2P_DEVICE:
12558 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
12559 			return -EINVAL;
12560 		break;
12561 	case NL80211_IFTYPE_STATION:
12562 	case NL80211_IFTYPE_ADHOC:
12563 	case NL80211_IFTYPE_P2P_CLIENT:
12564 	case NL80211_IFTYPE_AP:
12565 	case NL80211_IFTYPE_AP_VLAN:
12566 	case NL80211_IFTYPE_MESH_POINT:
12567 	case NL80211_IFTYPE_P2P_GO:
12568 		break;
12569 	case NL80211_IFTYPE_NAN:
12570 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12571 					     NL80211_EXT_FEATURE_SECURE_NAN))
12572 			return -EOPNOTSUPP;
12573 		break;
12574 	default:
12575 		return -EOPNOTSUPP;
12576 	}
12577 
12578 	if (info->attrs[NL80211_ATTR_DURATION]) {
12579 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12580 			return -EINVAL;
12581 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12582 
12583 		/*
12584 		 * We should wait on the channel for at least a minimum amount
12585 		 * of time (10ms) but no longer than the driver supports.
12586 		 */
12587 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12588 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
12589 			return -EINVAL;
12590 	}
12591 
12592 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
12593 
12594 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12595 		return -EINVAL;
12596 
12597 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
12598 
12599 	/* get the channel if any has been specified, otherwise pass NULL to
12600 	 * the driver. The latter will use the current one
12601 	 */
12602 	chandef.chan = NULL;
12603 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
12604 		err = nl80211_parse_chandef(rdev, info, &chandef);
12605 		if (err)
12606 			return err;
12607 	}
12608 
12609 	if (!chandef.chan && params.offchan)
12610 		return -EINVAL;
12611 
12612 	if (params.offchan &&
12613 	    !cfg80211_off_channel_oper_allowed(wdev, chandef.chan))
12614 		return -EBUSY;
12615 
12616 	params.link_id = nl80211_link_id_or_invalid(info->attrs);
12617 	/*
12618 	 * This now races due to the unlock, but we cannot check
12619 	 * the valid links for the _station_ anyway, so that's up
12620 	 * to the driver.
12621 	 */
12622 	if (params.link_id >= 0 &&
12623 	    !(wdev->valid_links & BIT(params.link_id)))
12624 		return -EINVAL;
12625 
12626 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
12627 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
12628 
12629 	err = nl80211_parse_counter_offsets(rdev, NULL, params.len, -1,
12630 					    info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX],
12631 					    &params.csa_offsets,
12632 					    &params.n_csa_offsets);
12633 	if (err)
12634 		return err;
12635 
12636 	if (!params.dont_wait_for_ack) {
12637 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12638 		if (!msg)
12639 			return -ENOMEM;
12640 
12641 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12642 				     NL80211_CMD_FRAME);
12643 		if (!hdr) {
12644 			err = -ENOBUFS;
12645 			goto free_msg;
12646 		}
12647 	}
12648 
12649 	params.chan = chandef.chan;
12650 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
12651 	if (err)
12652 		goto free_msg;
12653 
12654 	if (msg) {
12655 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12656 				      NL80211_ATTR_PAD))
12657 			goto nla_put_failure;
12658 
12659 		genlmsg_end(msg, hdr);
12660 		return genlmsg_reply(msg, info);
12661 	}
12662 
12663 	return 0;
12664 
12665  nla_put_failure:
12666 	err = -ENOBUFS;
12667  free_msg:
12668 	nlmsg_free(msg);
12669 	return err;
12670 }
12671 
12672 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
12673 {
12674 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12675 	struct wireless_dev *wdev = info->user_ptr[1];
12676 	u64 cookie;
12677 
12678 	if (!info->attrs[NL80211_ATTR_COOKIE])
12679 		return -EINVAL;
12680 
12681 	if (!rdev->ops->mgmt_tx_cancel_wait)
12682 		return -EOPNOTSUPP;
12683 
12684 	switch (wdev->iftype) {
12685 	case NL80211_IFTYPE_STATION:
12686 	case NL80211_IFTYPE_ADHOC:
12687 	case NL80211_IFTYPE_P2P_CLIENT:
12688 	case NL80211_IFTYPE_AP:
12689 	case NL80211_IFTYPE_AP_VLAN:
12690 	case NL80211_IFTYPE_P2P_GO:
12691 	case NL80211_IFTYPE_P2P_DEVICE:
12692 		break;
12693 	case NL80211_IFTYPE_NAN:
12694 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12695 					     NL80211_EXT_FEATURE_SECURE_NAN))
12696 			return -EOPNOTSUPP;
12697 		break;
12698 	default:
12699 		return -EOPNOTSUPP;
12700 	}
12701 
12702 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12703 
12704 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
12705 }
12706 
12707 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
12708 {
12709 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12710 	struct wireless_dev *wdev;
12711 	struct net_device *dev = info->user_ptr[1];
12712 	u8 ps_state;
12713 	bool state;
12714 	int err;
12715 
12716 	if (!info->attrs[NL80211_ATTR_PS_STATE])
12717 		return -EINVAL;
12718 
12719 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
12720 
12721 	wdev = dev->ieee80211_ptr;
12722 
12723 	if (!rdev->ops->set_power_mgmt)
12724 		return -EOPNOTSUPP;
12725 
12726 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
12727 
12728 	if (state == wdev->ps)
12729 		return 0;
12730 
12731 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
12732 	if (!err)
12733 		wdev->ps = state;
12734 	return err;
12735 }
12736 
12737 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
12738 {
12739 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12740 	enum nl80211_ps_state ps_state;
12741 	struct wireless_dev *wdev;
12742 	struct net_device *dev = info->user_ptr[1];
12743 	struct sk_buff *msg;
12744 	void *hdr;
12745 	int err;
12746 
12747 	wdev = dev->ieee80211_ptr;
12748 
12749 	if (!rdev->ops->set_power_mgmt)
12750 		return -EOPNOTSUPP;
12751 
12752 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12753 	if (!msg)
12754 		return -ENOMEM;
12755 
12756 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12757 			     NL80211_CMD_GET_POWER_SAVE);
12758 	if (!hdr) {
12759 		err = -ENOBUFS;
12760 		goto free_msg;
12761 	}
12762 
12763 	if (wdev->ps)
12764 		ps_state = NL80211_PS_ENABLED;
12765 	else
12766 		ps_state = NL80211_PS_DISABLED;
12767 
12768 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
12769 		goto nla_put_failure;
12770 
12771 	genlmsg_end(msg, hdr);
12772 	return genlmsg_reply(msg, info);
12773 
12774  nla_put_failure:
12775 	err = -ENOBUFS;
12776  free_msg:
12777 	nlmsg_free(msg);
12778 	return err;
12779 }
12780 
12781 static const struct nla_policy
12782 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
12783 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
12784 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
12785 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
12786 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
12787 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
12788 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
12789 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
12790 };
12791 
12792 static int nl80211_set_cqm_txe(struct genl_info *info,
12793 			       u32 rate, u32 pkts, u32 intvl)
12794 {
12795 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12796 	struct net_device *dev = info->user_ptr[1];
12797 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12798 
12799 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
12800 		return -EINVAL;
12801 
12802 	if (!rdev->ops->set_cqm_txe_config)
12803 		return -EOPNOTSUPP;
12804 
12805 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12806 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12807 		return -EOPNOTSUPP;
12808 
12809 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
12810 }
12811 
12812 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
12813 				    struct net_device *dev,
12814 				    struct cfg80211_cqm_config *cqm_config)
12815 {
12816 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12817 	s32 last, low, high;
12818 	u32 hyst;
12819 	int i, n, low_index;
12820 	int err;
12821 
12822 	/*
12823 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
12824 	 * event has been received yet, we should receive an event after a
12825 	 * connection is established and enough beacons received to calculate
12826 	 * the average.
12827 	 */
12828 	if (!cqm_config->last_rssi_event_value &&
12829 	    wdev->links[0].client.current_bss &&
12830 	    rdev->ops->get_station) {
12831 		struct station_info sinfo = {};
12832 		u8 *mac_addr;
12833 
12834 		mac_addr = wdev->links[0].client.current_bss->pub.bssid;
12835 
12836 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
12837 		if (err)
12838 			return err;
12839 
12840 		cfg80211_sinfo_release_content(&sinfo);
12841 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
12842 			cqm_config->last_rssi_event_value =
12843 				(s8) sinfo.rx_beacon_signal_avg;
12844 	}
12845 
12846 	last = cqm_config->last_rssi_event_value;
12847 	hyst = cqm_config->rssi_hyst;
12848 	n = cqm_config->n_rssi_thresholds;
12849 
12850 	for (i = 0; i < n; i++) {
12851 		i = array_index_nospec(i, n);
12852 		if (last < cqm_config->rssi_thresholds[i])
12853 			break;
12854 	}
12855 
12856 	low_index = i - 1;
12857 	if (low_index >= 0) {
12858 		low_index = array_index_nospec(low_index, n);
12859 		low = cqm_config->rssi_thresholds[low_index] - hyst;
12860 	} else {
12861 		low = S32_MIN;
12862 	}
12863 	if (i < n) {
12864 		i = array_index_nospec(i, n);
12865 		high = cqm_config->rssi_thresholds[i] + hyst - 1;
12866 	} else {
12867 		high = S32_MAX;
12868 	}
12869 
12870 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
12871 }
12872 
12873 static int nl80211_set_cqm_rssi(struct genl_info *info,
12874 				const s32 *thresholds, int n_thresholds,
12875 				u32 hysteresis)
12876 {
12877 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12878 	struct cfg80211_cqm_config *cqm_config = NULL, *old;
12879 	struct net_device *dev = info->user_ptr[1];
12880 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12881 	s32 prev = S32_MIN;
12882 	int i, err;
12883 
12884 	/* Check all values negative and sorted */
12885 	for (i = 0; i < n_thresholds; i++) {
12886 		if (thresholds[i] > 0 || thresholds[i] <= prev)
12887 			return -EINVAL;
12888 
12889 		prev = thresholds[i];
12890 	}
12891 
12892 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12893 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12894 		return -EOPNOTSUPP;
12895 
12896 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
12897 		n_thresholds = 0;
12898 
12899 	old = wiphy_dereference(wdev->wiphy, wdev->cqm_config);
12900 
12901 	/* if already disabled just succeed */
12902 	if (!n_thresholds && !old)
12903 		return 0;
12904 
12905 	if (n_thresholds > 1) {
12906 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
12907 					     NL80211_EXT_FEATURE_CQM_RSSI_LIST) ||
12908 		    !rdev->ops->set_cqm_rssi_range_config)
12909 			return -EOPNOTSUPP;
12910 	} else {
12911 		if (!rdev->ops->set_cqm_rssi_config)
12912 			return -EOPNOTSUPP;
12913 	}
12914 
12915 	if (n_thresholds) {
12916 		cqm_config = kzalloc(struct_size(cqm_config, rssi_thresholds,
12917 						 n_thresholds),
12918 				     GFP_KERNEL);
12919 		if (!cqm_config)
12920 			return -ENOMEM;
12921 
12922 		cqm_config->rssi_hyst = hysteresis;
12923 		cqm_config->n_rssi_thresholds = n_thresholds;
12924 		memcpy(cqm_config->rssi_thresholds, thresholds,
12925 		       flex_array_size(cqm_config, rssi_thresholds,
12926 				       n_thresholds));
12927 		cqm_config->use_range_api = n_thresholds > 1 ||
12928 					    !rdev->ops->set_cqm_rssi_config;
12929 
12930 		rcu_assign_pointer(wdev->cqm_config, cqm_config);
12931 
12932 		if (cqm_config->use_range_api)
12933 			err = cfg80211_cqm_rssi_update(rdev, dev, cqm_config);
12934 		else
12935 			err = rdev_set_cqm_rssi_config(rdev, dev,
12936 						       thresholds[0],
12937 						       hysteresis);
12938 	} else {
12939 		RCU_INIT_POINTER(wdev->cqm_config, NULL);
12940 		/* if enabled as range also disable via range */
12941 		if (old->use_range_api)
12942 			err = rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
12943 		else
12944 			err = rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
12945 	}
12946 
12947 	if (err) {
12948 		rcu_assign_pointer(wdev->cqm_config, old);
12949 		kfree_rcu(cqm_config, rcu_head);
12950 	} else {
12951 		kfree_rcu(old, rcu_head);
12952 	}
12953 
12954 	return err;
12955 }
12956 
12957 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
12958 {
12959 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
12960 	struct nlattr *cqm;
12961 	int err;
12962 
12963 	cqm = info->attrs[NL80211_ATTR_CQM];
12964 	if (!cqm)
12965 		return -EINVAL;
12966 
12967 	err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
12968 					  nl80211_attr_cqm_policy,
12969 					  info->extack);
12970 	if (err)
12971 		return err;
12972 
12973 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
12974 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
12975 		const s32 *thresholds =
12976 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
12977 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
12978 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
12979 
12980 		if (len % 4)
12981 			return -EINVAL;
12982 
12983 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
12984 					    hysteresis);
12985 	}
12986 
12987 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
12988 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
12989 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
12990 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
12991 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
12992 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
12993 
12994 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
12995 	}
12996 
12997 	return -EINVAL;
12998 }
12999 
13000 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
13001 {
13002 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13003 	struct net_device *dev = info->user_ptr[1];
13004 	struct ocb_setup setup = {};
13005 	int err;
13006 
13007 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
13008 	if (err)
13009 		return err;
13010 
13011 	return cfg80211_join_ocb(rdev, dev, &setup);
13012 }
13013 
13014 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
13015 {
13016 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13017 	struct net_device *dev = info->user_ptr[1];
13018 
13019 	return cfg80211_leave_ocb(rdev, dev);
13020 }
13021 
13022 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
13023 {
13024 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13025 	struct net_device *dev = info->user_ptr[1];
13026 	struct mesh_config cfg;
13027 	struct mesh_setup setup;
13028 	int err;
13029 
13030 	/* start with default */
13031 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
13032 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
13033 
13034 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
13035 		/* and parse parameters if given */
13036 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
13037 		if (err)
13038 			return err;
13039 	}
13040 
13041 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
13042 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
13043 		return -EINVAL;
13044 
13045 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
13046 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
13047 
13048 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
13049 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
13050 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
13051 			return -EINVAL;
13052 
13053 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
13054 		setup.beacon_interval =
13055 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
13056 
13057 		err = cfg80211_validate_beacon_int(rdev,
13058 						   NL80211_IFTYPE_MESH_POINT,
13059 						   setup.beacon_interval);
13060 		if (err)
13061 			return err;
13062 	}
13063 
13064 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
13065 		setup.dtim_period =
13066 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
13067 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
13068 			return -EINVAL;
13069 	}
13070 
13071 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
13072 		/* parse additional setup parameters if given */
13073 		err = nl80211_parse_mesh_setup(info, &setup);
13074 		if (err)
13075 			return err;
13076 	}
13077 
13078 	if (setup.user_mpm)
13079 		cfg.auto_open_plinks = false;
13080 
13081 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
13082 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
13083 		if (err)
13084 			return err;
13085 	} else {
13086 		/* __cfg80211_join_mesh() will sort it out */
13087 		setup.chandef.chan = NULL;
13088 	}
13089 
13090 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
13091 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
13092 		int n_rates =
13093 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
13094 		struct ieee80211_supported_band *sband;
13095 
13096 		if (!setup.chandef.chan)
13097 			return -EINVAL;
13098 
13099 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
13100 
13101 		err = ieee80211_get_ratemask(sband, rates, n_rates,
13102 					     &setup.basic_rates);
13103 		if (err)
13104 			return err;
13105 	}
13106 
13107 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
13108 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
13109 						    NL80211_ATTR_TX_RATES,
13110 						    &setup.beacon_rate,
13111 						    dev, false, 0);
13112 		if (err)
13113 			return err;
13114 
13115 		if (!setup.chandef.chan)
13116 			return -EINVAL;
13117 
13118 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
13119 					      &setup.beacon_rate);
13120 		if (err)
13121 			return err;
13122 	}
13123 
13124 	setup.userspace_handles_dfs =
13125 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
13126 
13127 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
13128 		int r = validate_pae_over_nl80211(rdev, info);
13129 
13130 		if (r < 0)
13131 			return r;
13132 
13133 		setup.control_port_over_nl80211 = true;
13134 	}
13135 
13136 	err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
13137 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
13138 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
13139 
13140 	return err;
13141 }
13142 
13143 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
13144 {
13145 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13146 	struct net_device *dev = info->user_ptr[1];
13147 
13148 	return cfg80211_leave_mesh(rdev, dev);
13149 }
13150 
13151 #ifdef CONFIG_PM
13152 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
13153 					struct cfg80211_registered_device *rdev)
13154 {
13155 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
13156 	struct nlattr *nl_pats, *nl_pat;
13157 	int i, pat_len;
13158 
13159 	if (!wowlan->n_patterns)
13160 		return 0;
13161 
13162 	nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
13163 	if (!nl_pats)
13164 		return -ENOBUFS;
13165 
13166 	for (i = 0; i < wowlan->n_patterns; i++) {
13167 		nl_pat = nla_nest_start_noflag(msg, i + 1);
13168 		if (!nl_pat)
13169 			return -ENOBUFS;
13170 		pat_len = wowlan->patterns[i].pattern_len;
13171 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
13172 			    wowlan->patterns[i].mask) ||
13173 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
13174 			    wowlan->patterns[i].pattern) ||
13175 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
13176 				wowlan->patterns[i].pkt_offset))
13177 			return -ENOBUFS;
13178 		nla_nest_end(msg, nl_pat);
13179 	}
13180 	nla_nest_end(msg, nl_pats);
13181 
13182 	return 0;
13183 }
13184 
13185 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
13186 				   struct cfg80211_wowlan_tcp *tcp)
13187 {
13188 	struct nlattr *nl_tcp;
13189 
13190 	if (!tcp)
13191 		return 0;
13192 
13193 	nl_tcp = nla_nest_start_noflag(msg,
13194 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
13195 	if (!nl_tcp)
13196 		return -ENOBUFS;
13197 
13198 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
13199 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
13200 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
13201 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
13202 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
13203 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
13204 		    tcp->payload_len, tcp->payload) ||
13205 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
13206 			tcp->data_interval) ||
13207 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
13208 		    tcp->wake_len, tcp->wake_data) ||
13209 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
13210 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
13211 		return -ENOBUFS;
13212 
13213 	if (tcp->payload_seq.len &&
13214 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
13215 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
13216 		return -ENOBUFS;
13217 
13218 	if (tcp->payload_tok.len &&
13219 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
13220 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
13221 		    &tcp->payload_tok))
13222 		return -ENOBUFS;
13223 
13224 	nla_nest_end(msg, nl_tcp);
13225 
13226 	return 0;
13227 }
13228 
13229 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
13230 				  struct cfg80211_sched_scan_request *req)
13231 {
13232 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
13233 	int i;
13234 
13235 	if (!req)
13236 		return 0;
13237 
13238 	nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
13239 	if (!nd)
13240 		return -ENOBUFS;
13241 
13242 	if (req->n_scan_plans == 1 &&
13243 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
13244 			req->scan_plans[0].interval * 1000))
13245 		return -ENOBUFS;
13246 
13247 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
13248 		return -ENOBUFS;
13249 
13250 	if (req->relative_rssi_set) {
13251 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
13252 
13253 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
13254 			       req->relative_rssi))
13255 			return -ENOBUFS;
13256 
13257 		rssi_adjust.band = req->rssi_adjust.band;
13258 		rssi_adjust.delta = req->rssi_adjust.delta;
13259 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
13260 			    sizeof(rssi_adjust), &rssi_adjust))
13261 			return -ENOBUFS;
13262 	}
13263 
13264 	freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
13265 	if (!freqs)
13266 		return -ENOBUFS;
13267 
13268 	for (i = 0; i < req->n_channels; i++) {
13269 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
13270 			return -ENOBUFS;
13271 	}
13272 
13273 	nla_nest_end(msg, freqs);
13274 
13275 	if (req->n_match_sets) {
13276 		matches = nla_nest_start_noflag(msg,
13277 						NL80211_ATTR_SCHED_SCAN_MATCH);
13278 		if (!matches)
13279 			return -ENOBUFS;
13280 
13281 		for (i = 0; i < req->n_match_sets; i++) {
13282 			match = nla_nest_start_noflag(msg, i);
13283 			if (!match)
13284 				return -ENOBUFS;
13285 
13286 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
13287 				    req->match_sets[i].ssid.ssid_len,
13288 				    req->match_sets[i].ssid.ssid))
13289 				return -ENOBUFS;
13290 			nla_nest_end(msg, match);
13291 		}
13292 		nla_nest_end(msg, matches);
13293 	}
13294 
13295 	scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
13296 	if (!scan_plans)
13297 		return -ENOBUFS;
13298 
13299 	for (i = 0; i < req->n_scan_plans; i++) {
13300 		scan_plan = nla_nest_start_noflag(msg, i + 1);
13301 		if (!scan_plan)
13302 			return -ENOBUFS;
13303 
13304 		if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
13305 				req->scan_plans[i].interval) ||
13306 		    (req->scan_plans[i].iterations &&
13307 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
13308 				 req->scan_plans[i].iterations)))
13309 			return -ENOBUFS;
13310 		nla_nest_end(msg, scan_plan);
13311 	}
13312 	nla_nest_end(msg, scan_plans);
13313 
13314 	nla_nest_end(msg, nd);
13315 
13316 	return 0;
13317 }
13318 
13319 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
13320 {
13321 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13322 	struct sk_buff *msg;
13323 	void *hdr;
13324 	u32 size = NLMSG_DEFAULT_SIZE;
13325 
13326 	if (!rdev->wiphy.wowlan)
13327 		return -EOPNOTSUPP;
13328 
13329 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
13330 		/* adjust size to have room for all the data */
13331 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
13332 			rdev->wiphy.wowlan_config->tcp->payload_len +
13333 			rdev->wiphy.wowlan_config->tcp->wake_len +
13334 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
13335 	}
13336 
13337 	msg = nlmsg_new(size, GFP_KERNEL);
13338 	if (!msg)
13339 		return -ENOMEM;
13340 
13341 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13342 			     NL80211_CMD_GET_WOWLAN);
13343 	if (!hdr)
13344 		goto nla_put_failure;
13345 
13346 	if (rdev->wiphy.wowlan_config) {
13347 		struct nlattr *nl_wowlan;
13348 
13349 		nl_wowlan = nla_nest_start_noflag(msg,
13350 						  NL80211_ATTR_WOWLAN_TRIGGERS);
13351 		if (!nl_wowlan)
13352 			goto nla_put_failure;
13353 
13354 		if ((rdev->wiphy.wowlan_config->any &&
13355 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
13356 		    (rdev->wiphy.wowlan_config->disconnect &&
13357 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
13358 		    (rdev->wiphy.wowlan_config->magic_pkt &&
13359 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
13360 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
13361 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
13362 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
13363 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
13364 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
13365 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
13366 		    (rdev->wiphy.wowlan_config->rfkill_release &&
13367 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
13368 			goto nla_put_failure;
13369 
13370 		if (nl80211_send_wowlan_patterns(msg, rdev))
13371 			goto nla_put_failure;
13372 
13373 		if (nl80211_send_wowlan_tcp(msg,
13374 					    rdev->wiphy.wowlan_config->tcp))
13375 			goto nla_put_failure;
13376 
13377 		if (nl80211_send_wowlan_nd(
13378 			    msg,
13379 			    rdev->wiphy.wowlan_config->nd_config))
13380 			goto nla_put_failure;
13381 
13382 		nla_nest_end(msg, nl_wowlan);
13383 	}
13384 
13385 	genlmsg_end(msg, hdr);
13386 	return genlmsg_reply(msg, info);
13387 
13388 nla_put_failure:
13389 	nlmsg_free(msg);
13390 	return -ENOBUFS;
13391 }
13392 
13393 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
13394 				    struct nlattr *attr,
13395 				    struct cfg80211_wowlan *trig)
13396 {
13397 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
13398 	struct cfg80211_wowlan_tcp *cfg;
13399 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
13400 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
13401 	u32 size;
13402 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
13403 	int err, port;
13404 
13405 	if (!rdev->wiphy.wowlan->tcp)
13406 		return -EINVAL;
13407 
13408 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
13409 					  nl80211_wowlan_tcp_policy, NULL);
13410 	if (err)
13411 		return err;
13412 
13413 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
13414 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
13415 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
13416 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
13417 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
13418 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
13419 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
13420 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
13421 		return -EINVAL;
13422 
13423 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
13424 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
13425 		return -EINVAL;
13426 
13427 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
13428 			rdev->wiphy.wowlan->tcp->data_interval_max ||
13429 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
13430 		return -EINVAL;
13431 
13432 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
13433 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
13434 		return -EINVAL;
13435 
13436 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
13437 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
13438 		return -EINVAL;
13439 
13440 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
13441 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13442 
13443 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13444 		tokens_size = tokln - sizeof(*tok);
13445 
13446 		if (!tok->len || tokens_size % tok->len)
13447 			return -EINVAL;
13448 		if (!rdev->wiphy.wowlan->tcp->tok)
13449 			return -EINVAL;
13450 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
13451 			return -EINVAL;
13452 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
13453 			return -EINVAL;
13454 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
13455 			return -EINVAL;
13456 		if (tok->offset + tok->len > data_size)
13457 			return -EINVAL;
13458 	}
13459 
13460 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
13461 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
13462 		if (!rdev->wiphy.wowlan->tcp->seq)
13463 			return -EINVAL;
13464 		if (seq->len == 0 || seq->len > 4)
13465 			return -EINVAL;
13466 		if (seq->len + seq->offset > data_size)
13467 			return -EINVAL;
13468 	}
13469 
13470 	size = sizeof(*cfg);
13471 	size += data_size;
13472 	size += wake_size + wake_mask_size;
13473 	size += tokens_size;
13474 
13475 	cfg = kzalloc(size, GFP_KERNEL);
13476 	if (!cfg)
13477 		return -ENOMEM;
13478 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
13479 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
13480 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
13481 	       ETH_ALEN);
13482 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
13483 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
13484 	else
13485 		port = 0;
13486 #ifdef CONFIG_INET
13487 	/* allocate a socket and port for it and use it */
13488 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
13489 			    IPPROTO_TCP, &cfg->sock, 1);
13490 	if (err) {
13491 		kfree(cfg);
13492 		return err;
13493 	}
13494 	if (inet_csk_get_port(cfg->sock->sk, port)) {
13495 		sock_release(cfg->sock);
13496 		kfree(cfg);
13497 		return -EADDRINUSE;
13498 	}
13499 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
13500 #else
13501 	if (!port) {
13502 		kfree(cfg);
13503 		return -EINVAL;
13504 	}
13505 	cfg->src_port = port;
13506 #endif
13507 
13508 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
13509 	cfg->payload_len = data_size;
13510 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
13511 	memcpy((void *)cfg->payload,
13512 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
13513 	       data_size);
13514 	if (seq)
13515 		cfg->payload_seq = *seq;
13516 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
13517 	cfg->wake_len = wake_size;
13518 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
13519 	memcpy((void *)cfg->wake_data,
13520 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
13521 	       wake_size);
13522 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
13523 			 data_size + wake_size;
13524 	memcpy((void *)cfg->wake_mask,
13525 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
13526 	       wake_mask_size);
13527 	if (tok) {
13528 		cfg->tokens_size = tokens_size;
13529 		cfg->payload_tok = *tok;
13530 		memcpy(cfg->payload_tok.token_stream, tok->token_stream,
13531 		       tokens_size);
13532 	}
13533 
13534 	trig->tcp = cfg;
13535 
13536 	return 0;
13537 }
13538 
13539 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
13540 				   const struct wiphy_wowlan_support *wowlan,
13541 				   struct nlattr *attr,
13542 				   struct cfg80211_wowlan *trig)
13543 {
13544 	struct nlattr **tb;
13545 	int err;
13546 
13547 	tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
13548 	if (!tb)
13549 		return -ENOMEM;
13550 
13551 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
13552 		err = -EOPNOTSUPP;
13553 		goto out;
13554 	}
13555 
13556 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
13557 					  nl80211_policy, NULL);
13558 	if (err)
13559 		goto out;
13560 
13561 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
13562 						   wowlan->max_nd_match_sets);
13563 	err = PTR_ERR_OR_ZERO(trig->nd_config);
13564 	if (err)
13565 		trig->nd_config = NULL;
13566 
13567 out:
13568 	kfree(tb);
13569 	return err;
13570 }
13571 
13572 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
13573 {
13574 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13575 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
13576 	struct cfg80211_wowlan new_triggers = {};
13577 	struct cfg80211_wowlan *ntrig;
13578 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
13579 	int err, i;
13580 	bool prev_enabled = rdev->wiphy.wowlan_config;
13581 	bool regular = false;
13582 
13583 	if (!wowlan)
13584 		return -EOPNOTSUPP;
13585 
13586 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
13587 		cfg80211_rdev_free_wowlan(rdev);
13588 		rdev->wiphy.wowlan_config = NULL;
13589 		goto set_wakeup;
13590 	}
13591 
13592 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
13593 					  info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
13594 					  nl80211_wowlan_policy, info->extack);
13595 	if (err)
13596 		return err;
13597 
13598 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
13599 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
13600 			return -EINVAL;
13601 		new_triggers.any = true;
13602 	}
13603 
13604 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
13605 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
13606 			return -EINVAL;
13607 		new_triggers.disconnect = true;
13608 		regular = true;
13609 	}
13610 
13611 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
13612 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
13613 			return -EINVAL;
13614 		new_triggers.magic_pkt = true;
13615 		regular = true;
13616 	}
13617 
13618 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
13619 		return -EINVAL;
13620 
13621 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
13622 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
13623 			return -EINVAL;
13624 		new_triggers.gtk_rekey_failure = true;
13625 		regular = true;
13626 	}
13627 
13628 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
13629 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
13630 			return -EINVAL;
13631 		new_triggers.eap_identity_req = true;
13632 		regular = true;
13633 	}
13634 
13635 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
13636 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
13637 			return -EINVAL;
13638 		new_triggers.four_way_handshake = true;
13639 		regular = true;
13640 	}
13641 
13642 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
13643 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
13644 			return -EINVAL;
13645 		new_triggers.rfkill_release = true;
13646 		regular = true;
13647 	}
13648 
13649 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
13650 		struct nlattr *pat;
13651 		int n_patterns = 0;
13652 		int rem, pat_len, mask_len, pkt_offset;
13653 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13654 
13655 		regular = true;
13656 
13657 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13658 				    rem)
13659 			n_patterns++;
13660 		if (n_patterns > wowlan->n_patterns)
13661 			return -EINVAL;
13662 
13663 		new_triggers.patterns = kcalloc(n_patterns,
13664 						sizeof(new_triggers.patterns[0]),
13665 						GFP_KERNEL);
13666 		if (!new_triggers.patterns)
13667 			return -ENOMEM;
13668 
13669 		new_triggers.n_patterns = n_patterns;
13670 		i = 0;
13671 
13672 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13673 				    rem) {
13674 			u8 *mask_pat;
13675 
13676 			err = nla_parse_nested_deprecated(pat_tb,
13677 							  MAX_NL80211_PKTPAT,
13678 							  pat,
13679 							  nl80211_packet_pattern_policy,
13680 							  info->extack);
13681 			if (err)
13682 				goto error;
13683 
13684 			err = -EINVAL;
13685 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
13686 			    !pat_tb[NL80211_PKTPAT_PATTERN])
13687 				goto error;
13688 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13689 			mask_len = DIV_ROUND_UP(pat_len, 8);
13690 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13691 				goto error;
13692 			if (pat_len > wowlan->pattern_max_len ||
13693 			    pat_len < wowlan->pattern_min_len)
13694 				goto error;
13695 
13696 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
13697 				pkt_offset = 0;
13698 			else
13699 				pkt_offset = nla_get_u32(
13700 					pat_tb[NL80211_PKTPAT_OFFSET]);
13701 			if (pkt_offset > wowlan->max_pkt_offset)
13702 				goto error;
13703 			new_triggers.patterns[i].pkt_offset = pkt_offset;
13704 
13705 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13706 			if (!mask_pat) {
13707 				err = -ENOMEM;
13708 				goto error;
13709 			}
13710 			new_triggers.patterns[i].mask = mask_pat;
13711 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13712 			       mask_len);
13713 			mask_pat += mask_len;
13714 			new_triggers.patterns[i].pattern = mask_pat;
13715 			new_triggers.patterns[i].pattern_len = pat_len;
13716 			memcpy(mask_pat,
13717 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13718 			       pat_len);
13719 			i++;
13720 		}
13721 	}
13722 
13723 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
13724 		regular = true;
13725 		err = nl80211_parse_wowlan_tcp(
13726 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
13727 			&new_triggers);
13728 		if (err)
13729 			goto error;
13730 	}
13731 
13732 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
13733 		regular = true;
13734 		err = nl80211_parse_wowlan_nd(
13735 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
13736 			&new_triggers);
13737 		if (err)
13738 			goto error;
13739 	}
13740 
13741 	/* The 'any' trigger means the device continues operating more or less
13742 	 * as in its normal operation mode and wakes up the host on most of the
13743 	 * normal interrupts (like packet RX, ...)
13744 	 * It therefore makes little sense to combine with the more constrained
13745 	 * wakeup trigger modes.
13746 	 */
13747 	if (new_triggers.any && regular) {
13748 		err = -EINVAL;
13749 		goto error;
13750 	}
13751 
13752 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
13753 	if (!ntrig) {
13754 		err = -ENOMEM;
13755 		goto error;
13756 	}
13757 	cfg80211_rdev_free_wowlan(rdev);
13758 	rdev->wiphy.wowlan_config = ntrig;
13759 
13760  set_wakeup:
13761 	if (rdev->ops->set_wakeup &&
13762 	    prev_enabled != !!rdev->wiphy.wowlan_config)
13763 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
13764 
13765 	return 0;
13766  error:
13767 	for (i = 0; i < new_triggers.n_patterns; i++)
13768 		kfree(new_triggers.patterns[i].mask);
13769 	kfree(new_triggers.patterns);
13770 	if (new_triggers.tcp && new_triggers.tcp->sock)
13771 		sock_release(new_triggers.tcp->sock);
13772 	kfree(new_triggers.tcp);
13773 	kfree(new_triggers.nd_config);
13774 	return err;
13775 }
13776 #endif
13777 
13778 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
13779 				       struct cfg80211_registered_device *rdev)
13780 {
13781 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
13782 	int i, j, pat_len;
13783 	struct cfg80211_coalesce_rules *rule;
13784 
13785 	if (!rdev->coalesce->n_rules)
13786 		return 0;
13787 
13788 	nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
13789 	if (!nl_rules)
13790 		return -ENOBUFS;
13791 
13792 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
13793 		nl_rule = nla_nest_start_noflag(msg, i + 1);
13794 		if (!nl_rule)
13795 			return -ENOBUFS;
13796 
13797 		rule = &rdev->coalesce->rules[i];
13798 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
13799 				rule->delay))
13800 			return -ENOBUFS;
13801 
13802 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
13803 				rule->condition))
13804 			return -ENOBUFS;
13805 
13806 		nl_pats = nla_nest_start_noflag(msg,
13807 						NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
13808 		if (!nl_pats)
13809 			return -ENOBUFS;
13810 
13811 		for (j = 0; j < rule->n_patterns; j++) {
13812 			nl_pat = nla_nest_start_noflag(msg, j + 1);
13813 			if (!nl_pat)
13814 				return -ENOBUFS;
13815 			pat_len = rule->patterns[j].pattern_len;
13816 			if (nla_put(msg, NL80211_PKTPAT_MASK,
13817 				    DIV_ROUND_UP(pat_len, 8),
13818 				    rule->patterns[j].mask) ||
13819 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
13820 				    rule->patterns[j].pattern) ||
13821 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
13822 					rule->patterns[j].pkt_offset))
13823 				return -ENOBUFS;
13824 			nla_nest_end(msg, nl_pat);
13825 		}
13826 		nla_nest_end(msg, nl_pats);
13827 		nla_nest_end(msg, nl_rule);
13828 	}
13829 	nla_nest_end(msg, nl_rules);
13830 
13831 	return 0;
13832 }
13833 
13834 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
13835 {
13836 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13837 	struct sk_buff *msg;
13838 	void *hdr;
13839 
13840 	if (!rdev->wiphy.coalesce)
13841 		return -EOPNOTSUPP;
13842 
13843 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13844 	if (!msg)
13845 		return -ENOMEM;
13846 
13847 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13848 			     NL80211_CMD_GET_COALESCE);
13849 	if (!hdr)
13850 		goto nla_put_failure;
13851 
13852 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
13853 		goto nla_put_failure;
13854 
13855 	genlmsg_end(msg, hdr);
13856 	return genlmsg_reply(msg, info);
13857 
13858 nla_put_failure:
13859 	nlmsg_free(msg);
13860 	return -ENOBUFS;
13861 }
13862 
13863 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
13864 {
13865 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
13866 	int i, j;
13867 	struct cfg80211_coalesce_rules *rule;
13868 
13869 	if (!coalesce)
13870 		return;
13871 
13872 	for (i = 0; i < coalesce->n_rules; i++) {
13873 		rule = &coalesce->rules[i];
13874 		for (j = 0; j < rule->n_patterns; j++)
13875 			kfree(rule->patterns[j].mask);
13876 		kfree(rule->patterns);
13877 	}
13878 	kfree(coalesce->rules);
13879 	kfree(coalesce);
13880 	rdev->coalesce = NULL;
13881 }
13882 
13883 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
13884 				       struct nlattr *rule,
13885 				       struct cfg80211_coalesce_rules *new_rule)
13886 {
13887 	int err, i;
13888 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13889 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
13890 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
13891 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13892 
13893 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
13894 					  rule, nl80211_coalesce_policy, NULL);
13895 	if (err)
13896 		return err;
13897 
13898 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
13899 		new_rule->delay =
13900 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
13901 	if (new_rule->delay > coalesce->max_delay)
13902 		return -EINVAL;
13903 
13904 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
13905 		new_rule->condition =
13906 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
13907 
13908 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
13909 		return -EINVAL;
13910 
13911 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13912 			    rem)
13913 		n_patterns++;
13914 	if (n_patterns > coalesce->n_patterns)
13915 		return -EINVAL;
13916 
13917 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
13918 				     GFP_KERNEL);
13919 	if (!new_rule->patterns)
13920 		return -ENOMEM;
13921 
13922 	new_rule->n_patterns = n_patterns;
13923 	i = 0;
13924 
13925 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13926 			    rem) {
13927 		u8 *mask_pat;
13928 
13929 		err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
13930 						  pat,
13931 						  nl80211_packet_pattern_policy,
13932 						  NULL);
13933 		if (err)
13934 			return err;
13935 
13936 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
13937 		    !pat_tb[NL80211_PKTPAT_PATTERN])
13938 			return -EINVAL;
13939 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13940 		mask_len = DIV_ROUND_UP(pat_len, 8);
13941 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13942 			return -EINVAL;
13943 		if (pat_len > coalesce->pattern_max_len ||
13944 		    pat_len < coalesce->pattern_min_len)
13945 			return -EINVAL;
13946 
13947 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
13948 			pkt_offset = 0;
13949 		else
13950 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
13951 		if (pkt_offset > coalesce->max_pkt_offset)
13952 			return -EINVAL;
13953 		new_rule->patterns[i].pkt_offset = pkt_offset;
13954 
13955 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13956 		if (!mask_pat)
13957 			return -ENOMEM;
13958 
13959 		new_rule->patterns[i].mask = mask_pat;
13960 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13961 		       mask_len);
13962 
13963 		mask_pat += mask_len;
13964 		new_rule->patterns[i].pattern = mask_pat;
13965 		new_rule->patterns[i].pattern_len = pat_len;
13966 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13967 		       pat_len);
13968 		i++;
13969 	}
13970 
13971 	return 0;
13972 }
13973 
13974 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
13975 {
13976 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13977 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13978 	struct cfg80211_coalesce new_coalesce = {};
13979 	struct cfg80211_coalesce *n_coalesce;
13980 	int err, rem_rule, n_rules = 0, i, j;
13981 	struct nlattr *rule;
13982 	struct cfg80211_coalesce_rules *tmp_rule;
13983 
13984 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
13985 		return -EOPNOTSUPP;
13986 
13987 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
13988 		cfg80211_rdev_free_coalesce(rdev);
13989 		rdev_set_coalesce(rdev, NULL);
13990 		return 0;
13991 	}
13992 
13993 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
13994 			    rem_rule)
13995 		n_rules++;
13996 	if (n_rules > coalesce->n_rules)
13997 		return -EINVAL;
13998 
13999 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
14000 				     GFP_KERNEL);
14001 	if (!new_coalesce.rules)
14002 		return -ENOMEM;
14003 
14004 	new_coalesce.n_rules = n_rules;
14005 	i = 0;
14006 
14007 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
14008 			    rem_rule) {
14009 		err = nl80211_parse_coalesce_rule(rdev, rule,
14010 						  &new_coalesce.rules[i]);
14011 		if (err)
14012 			goto error;
14013 
14014 		i++;
14015 	}
14016 
14017 	err = rdev_set_coalesce(rdev, &new_coalesce);
14018 	if (err)
14019 		goto error;
14020 
14021 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
14022 	if (!n_coalesce) {
14023 		err = -ENOMEM;
14024 		goto error;
14025 	}
14026 	cfg80211_rdev_free_coalesce(rdev);
14027 	rdev->coalesce = n_coalesce;
14028 
14029 	return 0;
14030 error:
14031 	for (i = 0; i < new_coalesce.n_rules; i++) {
14032 		tmp_rule = &new_coalesce.rules[i];
14033 		if (!tmp_rule)
14034 			continue;
14035 		for (j = 0; j < tmp_rule->n_patterns; j++)
14036 			kfree(tmp_rule->patterns[j].mask);
14037 		kfree(tmp_rule->patterns);
14038 	}
14039 	kfree(new_coalesce.rules);
14040 
14041 	return err;
14042 }
14043 
14044 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
14045 {
14046 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14047 	struct net_device *dev = info->user_ptr[1];
14048 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14049 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
14050 	struct cfg80211_gtk_rekey_data rekey_data = {};
14051 	int err;
14052 
14053 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
14054 		return -EINVAL;
14055 
14056 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
14057 					  info->attrs[NL80211_ATTR_REKEY_DATA],
14058 					  nl80211_rekey_policy, info->extack);
14059 	if (err)
14060 		return err;
14061 
14062 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
14063 	    !tb[NL80211_REKEY_DATA_KCK])
14064 		return -EINVAL;
14065 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN &&
14066 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
14067 	      nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KEK_EXT_LEN))
14068 		return -ERANGE;
14069 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN &&
14070 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
14071 	      nla_len(tb[NL80211_REKEY_DATA_KCK]) == NL80211_KCK_EXT_LEN) &&
14072 	     !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KCK_32 &&
14073 	       nla_len(tb[NL80211_REKEY_DATA_KCK]) == NL80211_KCK_EXT_LEN_32))
14074 		return -ERANGE;
14075 
14076 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
14077 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
14078 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
14079 	rekey_data.kek_len = nla_len(tb[NL80211_REKEY_DATA_KEK]);
14080 	rekey_data.kck_len = nla_len(tb[NL80211_REKEY_DATA_KCK]);
14081 	if (tb[NL80211_REKEY_DATA_AKM])
14082 		rekey_data.akm = nla_get_u32(tb[NL80211_REKEY_DATA_AKM]);
14083 
14084 	if (!wdev->connected)
14085 		return -ENOTCONN;
14086 
14087 	if (!rdev->ops->set_rekey_data)
14088 		return -EOPNOTSUPP;
14089 
14090 	return rdev_set_rekey_data(rdev, dev, &rekey_data);
14091 }
14092 
14093 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
14094 					     struct genl_info *info)
14095 {
14096 	struct net_device *dev = info->user_ptr[1];
14097 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14098 
14099 	if (wdev->iftype != NL80211_IFTYPE_AP &&
14100 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
14101 		return -EINVAL;
14102 
14103 	if (wdev->ap_unexpected_nlportid)
14104 		return -EBUSY;
14105 
14106 	wdev->ap_unexpected_nlportid = info->snd_portid;
14107 	return 0;
14108 }
14109 
14110 static int nl80211_probe_client(struct sk_buff *skb,
14111 				struct genl_info *info)
14112 {
14113 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14114 	struct net_device *dev = info->user_ptr[1];
14115 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14116 	struct sk_buff *msg;
14117 	void *hdr;
14118 	const u8 *addr;
14119 	u64 cookie;
14120 	int err;
14121 
14122 	if (wdev->iftype != NL80211_IFTYPE_AP &&
14123 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
14124 		return -EOPNOTSUPP;
14125 
14126 	if (!info->attrs[NL80211_ATTR_MAC])
14127 		return -EINVAL;
14128 
14129 	if (!rdev->ops->probe_client)
14130 		return -EOPNOTSUPP;
14131 
14132 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14133 	if (!msg)
14134 		return -ENOMEM;
14135 
14136 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14137 			     NL80211_CMD_PROBE_CLIENT);
14138 	if (!hdr) {
14139 		err = -ENOBUFS;
14140 		goto free_msg;
14141 	}
14142 
14143 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
14144 
14145 	err = rdev_probe_client(rdev, dev, addr, &cookie);
14146 	if (err)
14147 		goto free_msg;
14148 
14149 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14150 			      NL80211_ATTR_PAD))
14151 		goto nla_put_failure;
14152 
14153 	genlmsg_end(msg, hdr);
14154 
14155 	return genlmsg_reply(msg, info);
14156 
14157  nla_put_failure:
14158 	err = -ENOBUFS;
14159  free_msg:
14160 	nlmsg_free(msg);
14161 	return err;
14162 }
14163 
14164 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
14165 {
14166 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14167 	struct cfg80211_beacon_registration *reg, *nreg;
14168 	int rv;
14169 
14170 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
14171 		return -EOPNOTSUPP;
14172 
14173 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
14174 	if (!nreg)
14175 		return -ENOMEM;
14176 
14177 	/* First, check if already registered. */
14178 	spin_lock_bh(&rdev->beacon_registrations_lock);
14179 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
14180 		if (reg->nlportid == info->snd_portid) {
14181 			rv = -EALREADY;
14182 			goto out_err;
14183 		}
14184 	}
14185 	/* Add it to the list */
14186 	nreg->nlportid = info->snd_portid;
14187 	list_add(&nreg->list, &rdev->beacon_registrations);
14188 
14189 	spin_unlock_bh(&rdev->beacon_registrations_lock);
14190 
14191 	return 0;
14192 out_err:
14193 	spin_unlock_bh(&rdev->beacon_registrations_lock);
14194 	kfree(nreg);
14195 	return rv;
14196 }
14197 
14198 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
14199 {
14200 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14201 	struct wireless_dev *wdev = info->user_ptr[1];
14202 	int err;
14203 
14204 	if (!rdev->ops->start_p2p_device)
14205 		return -EOPNOTSUPP;
14206 
14207 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
14208 		return -EOPNOTSUPP;
14209 
14210 	if (wdev_running(wdev))
14211 		return 0;
14212 
14213 	if (rfkill_blocked(rdev->wiphy.rfkill))
14214 		return -ERFKILL;
14215 
14216 	err = rdev_start_p2p_device(rdev, wdev);
14217 	if (err)
14218 		return err;
14219 
14220 	wdev->is_running = true;
14221 	rdev->opencount++;
14222 
14223 	return 0;
14224 }
14225 
14226 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
14227 {
14228 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14229 	struct wireless_dev *wdev = info->user_ptr[1];
14230 
14231 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
14232 		return -EOPNOTSUPP;
14233 
14234 	if (!rdev->ops->stop_p2p_device)
14235 		return -EOPNOTSUPP;
14236 
14237 	cfg80211_stop_p2p_device(rdev, wdev);
14238 
14239 	return 0;
14240 }
14241 
14242 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
14243 {
14244 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14245 	struct wireless_dev *wdev = info->user_ptr[1];
14246 	struct cfg80211_nan_conf conf = {};
14247 	int err;
14248 
14249 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14250 		return -EOPNOTSUPP;
14251 
14252 	if (wdev_running(wdev))
14253 		return -EEXIST;
14254 
14255 	if (rfkill_blocked(rdev->wiphy.rfkill))
14256 		return -ERFKILL;
14257 
14258 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
14259 		return -EINVAL;
14260 
14261 	conf.master_pref =
14262 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14263 
14264 	if (info->attrs[NL80211_ATTR_BANDS]) {
14265 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14266 
14267 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14268 			return -EOPNOTSUPP;
14269 
14270 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14271 			return -EINVAL;
14272 
14273 		conf.bands = bands;
14274 	}
14275 
14276 	err = rdev_start_nan(rdev, wdev, &conf);
14277 	if (err)
14278 		return err;
14279 
14280 	wdev->is_running = true;
14281 	rdev->opencount++;
14282 
14283 	return 0;
14284 }
14285 
14286 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
14287 {
14288 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14289 	struct wireless_dev *wdev = info->user_ptr[1];
14290 
14291 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14292 		return -EOPNOTSUPP;
14293 
14294 	cfg80211_stop_nan(rdev, wdev);
14295 
14296 	return 0;
14297 }
14298 
14299 static int validate_nan_filter(struct nlattr *filter_attr)
14300 {
14301 	struct nlattr *attr;
14302 	int len = 0, n_entries = 0, rem;
14303 
14304 	nla_for_each_nested(attr, filter_attr, rem) {
14305 		len += nla_len(attr);
14306 		n_entries++;
14307 	}
14308 
14309 	if (len >= U8_MAX)
14310 		return -EINVAL;
14311 
14312 	return n_entries;
14313 }
14314 
14315 static int handle_nan_filter(struct nlattr *attr_filter,
14316 			     struct cfg80211_nan_func *func,
14317 			     bool tx)
14318 {
14319 	struct nlattr *attr;
14320 	int n_entries, rem, i;
14321 	struct cfg80211_nan_func_filter *filter;
14322 
14323 	n_entries = validate_nan_filter(attr_filter);
14324 	if (n_entries < 0)
14325 		return n_entries;
14326 
14327 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
14328 
14329 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
14330 	if (!filter)
14331 		return -ENOMEM;
14332 
14333 	i = 0;
14334 	nla_for_each_nested(attr, attr_filter, rem) {
14335 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
14336 		if (!filter[i].filter)
14337 			goto err;
14338 
14339 		filter[i].len = nla_len(attr);
14340 		i++;
14341 	}
14342 	if (tx) {
14343 		func->num_tx_filters = n_entries;
14344 		func->tx_filters = filter;
14345 	} else {
14346 		func->num_rx_filters = n_entries;
14347 		func->rx_filters = filter;
14348 	}
14349 
14350 	return 0;
14351 
14352 err:
14353 	i = 0;
14354 	nla_for_each_nested(attr, attr_filter, rem) {
14355 		kfree(filter[i].filter);
14356 		i++;
14357 	}
14358 	kfree(filter);
14359 	return -ENOMEM;
14360 }
14361 
14362 static int nl80211_nan_add_func(struct sk_buff *skb,
14363 				struct genl_info *info)
14364 {
14365 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14366 	struct wireless_dev *wdev = info->user_ptr[1];
14367 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
14368 	struct cfg80211_nan_func *func;
14369 	struct sk_buff *msg = NULL;
14370 	void *hdr = NULL;
14371 	int err = 0;
14372 
14373 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14374 		return -EOPNOTSUPP;
14375 
14376 	if (!wdev_running(wdev))
14377 		return -ENOTCONN;
14378 
14379 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
14380 		return -EINVAL;
14381 
14382 	err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
14383 					  info->attrs[NL80211_ATTR_NAN_FUNC],
14384 					  nl80211_nan_func_policy,
14385 					  info->extack);
14386 	if (err)
14387 		return err;
14388 
14389 	func = kzalloc(sizeof(*func), GFP_KERNEL);
14390 	if (!func)
14391 		return -ENOMEM;
14392 
14393 	func->cookie = cfg80211_assign_cookie(rdev);
14394 
14395 	if (!tb[NL80211_NAN_FUNC_TYPE]) {
14396 		err = -EINVAL;
14397 		goto out;
14398 	}
14399 
14400 
14401 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
14402 
14403 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
14404 		err = -EINVAL;
14405 		goto out;
14406 	}
14407 
14408 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
14409 	       sizeof(func->service_id));
14410 
14411 	func->close_range =
14412 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
14413 
14414 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
14415 		func->serv_spec_info_len =
14416 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
14417 		func->serv_spec_info =
14418 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
14419 				func->serv_spec_info_len,
14420 				GFP_KERNEL);
14421 		if (!func->serv_spec_info) {
14422 			err = -ENOMEM;
14423 			goto out;
14424 		}
14425 	}
14426 
14427 	if (tb[NL80211_NAN_FUNC_TTL])
14428 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
14429 
14430 	switch (func->type) {
14431 	case NL80211_NAN_FUNC_PUBLISH:
14432 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
14433 			err = -EINVAL;
14434 			goto out;
14435 		}
14436 
14437 		func->publish_type =
14438 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
14439 		func->publish_bcast =
14440 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
14441 
14442 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
14443 			func->publish_bcast) {
14444 			err = -EINVAL;
14445 			goto out;
14446 		}
14447 		break;
14448 	case NL80211_NAN_FUNC_SUBSCRIBE:
14449 		func->subscribe_active =
14450 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
14451 		break;
14452 	case NL80211_NAN_FUNC_FOLLOW_UP:
14453 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
14454 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
14455 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
14456 			err = -EINVAL;
14457 			goto out;
14458 		}
14459 
14460 		func->followup_id =
14461 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
14462 		func->followup_reqid =
14463 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
14464 		memcpy(func->followup_dest.addr,
14465 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
14466 		       sizeof(func->followup_dest.addr));
14467 		if (func->ttl) {
14468 			err = -EINVAL;
14469 			goto out;
14470 		}
14471 		break;
14472 	default:
14473 		err = -EINVAL;
14474 		goto out;
14475 	}
14476 
14477 	if (tb[NL80211_NAN_FUNC_SRF]) {
14478 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
14479 
14480 		err = nla_parse_nested_deprecated(srf_tb,
14481 						  NL80211_NAN_SRF_ATTR_MAX,
14482 						  tb[NL80211_NAN_FUNC_SRF],
14483 						  nl80211_nan_srf_policy,
14484 						  info->extack);
14485 		if (err)
14486 			goto out;
14487 
14488 		func->srf_include =
14489 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
14490 
14491 		if (srf_tb[NL80211_NAN_SRF_BF]) {
14492 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
14493 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
14494 				err = -EINVAL;
14495 				goto out;
14496 			}
14497 
14498 			func->srf_bf_len =
14499 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
14500 			func->srf_bf =
14501 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
14502 					func->srf_bf_len, GFP_KERNEL);
14503 			if (!func->srf_bf) {
14504 				err = -ENOMEM;
14505 				goto out;
14506 			}
14507 
14508 			func->srf_bf_idx =
14509 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
14510 		} else {
14511 			struct nlattr *attr, *mac_attr =
14512 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
14513 			int n_entries, rem, i = 0;
14514 
14515 			if (!mac_attr) {
14516 				err = -EINVAL;
14517 				goto out;
14518 			}
14519 
14520 			n_entries = validate_acl_mac_addrs(mac_attr);
14521 			if (n_entries <= 0) {
14522 				err = -EINVAL;
14523 				goto out;
14524 			}
14525 
14526 			func->srf_num_macs = n_entries;
14527 			func->srf_macs =
14528 				kcalloc(n_entries, sizeof(*func->srf_macs),
14529 					GFP_KERNEL);
14530 			if (!func->srf_macs) {
14531 				err = -ENOMEM;
14532 				goto out;
14533 			}
14534 
14535 			nla_for_each_nested(attr, mac_attr, rem)
14536 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
14537 				       sizeof(*func->srf_macs));
14538 		}
14539 	}
14540 
14541 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
14542 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
14543 					func, true);
14544 		if (err)
14545 			goto out;
14546 	}
14547 
14548 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
14549 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
14550 					func, false);
14551 		if (err)
14552 			goto out;
14553 	}
14554 
14555 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14556 	if (!msg) {
14557 		err = -ENOMEM;
14558 		goto out;
14559 	}
14560 
14561 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14562 			     NL80211_CMD_ADD_NAN_FUNCTION);
14563 	/* This can't really happen - we just allocated 4KB */
14564 	if (WARN_ON(!hdr)) {
14565 		err = -ENOMEM;
14566 		goto out;
14567 	}
14568 
14569 	err = rdev_add_nan_func(rdev, wdev, func);
14570 out:
14571 	if (err < 0) {
14572 		cfg80211_free_nan_func(func);
14573 		nlmsg_free(msg);
14574 		return err;
14575 	}
14576 
14577 	/* propagate the instance id and cookie to userspace  */
14578 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
14579 			      NL80211_ATTR_PAD))
14580 		goto nla_put_failure;
14581 
14582 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14583 	if (!func_attr)
14584 		goto nla_put_failure;
14585 
14586 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
14587 		       func->instance_id))
14588 		goto nla_put_failure;
14589 
14590 	nla_nest_end(msg, func_attr);
14591 
14592 	genlmsg_end(msg, hdr);
14593 	return genlmsg_reply(msg, info);
14594 
14595 nla_put_failure:
14596 	nlmsg_free(msg);
14597 	return -ENOBUFS;
14598 }
14599 
14600 static int nl80211_nan_del_func(struct sk_buff *skb,
14601 			       struct genl_info *info)
14602 {
14603 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14604 	struct wireless_dev *wdev = info->user_ptr[1];
14605 	u64 cookie;
14606 
14607 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14608 		return -EOPNOTSUPP;
14609 
14610 	if (!wdev_running(wdev))
14611 		return -ENOTCONN;
14612 
14613 	if (!info->attrs[NL80211_ATTR_COOKIE])
14614 		return -EINVAL;
14615 
14616 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
14617 
14618 	rdev_del_nan_func(rdev, wdev, cookie);
14619 
14620 	return 0;
14621 }
14622 
14623 static int nl80211_nan_change_config(struct sk_buff *skb,
14624 				     struct genl_info *info)
14625 {
14626 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14627 	struct wireless_dev *wdev = info->user_ptr[1];
14628 	struct cfg80211_nan_conf conf = {};
14629 	u32 changed = 0;
14630 
14631 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14632 		return -EOPNOTSUPP;
14633 
14634 	if (!wdev_running(wdev))
14635 		return -ENOTCONN;
14636 
14637 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
14638 		conf.master_pref =
14639 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14640 		if (conf.master_pref <= 1 || conf.master_pref == 255)
14641 			return -EINVAL;
14642 
14643 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
14644 	}
14645 
14646 	if (info->attrs[NL80211_ATTR_BANDS]) {
14647 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14648 
14649 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14650 			return -EOPNOTSUPP;
14651 
14652 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14653 			return -EINVAL;
14654 
14655 		conf.bands = bands;
14656 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
14657 	}
14658 
14659 	if (!changed)
14660 		return -EINVAL;
14661 
14662 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
14663 }
14664 
14665 void cfg80211_nan_match(struct wireless_dev *wdev,
14666 			struct cfg80211_nan_match_params *match, gfp_t gfp)
14667 {
14668 	struct wiphy *wiphy = wdev->wiphy;
14669 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14670 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
14671 	struct sk_buff *msg;
14672 	void *hdr;
14673 
14674 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
14675 		return;
14676 
14677 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14678 	if (!msg)
14679 		return;
14680 
14681 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
14682 	if (!hdr) {
14683 		nlmsg_free(msg);
14684 		return;
14685 	}
14686 
14687 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14688 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14689 					 wdev->netdev->ifindex)) ||
14690 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14691 			      NL80211_ATTR_PAD))
14692 		goto nla_put_failure;
14693 
14694 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
14695 			      NL80211_ATTR_PAD) ||
14696 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
14697 		goto nla_put_failure;
14698 
14699 	match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
14700 	if (!match_attr)
14701 		goto nla_put_failure;
14702 
14703 	local_func_attr = nla_nest_start_noflag(msg,
14704 						NL80211_NAN_MATCH_FUNC_LOCAL);
14705 	if (!local_func_attr)
14706 		goto nla_put_failure;
14707 
14708 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
14709 		goto nla_put_failure;
14710 
14711 	nla_nest_end(msg, local_func_attr);
14712 
14713 	peer_func_attr = nla_nest_start_noflag(msg,
14714 					       NL80211_NAN_MATCH_FUNC_PEER);
14715 	if (!peer_func_attr)
14716 		goto nla_put_failure;
14717 
14718 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
14719 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
14720 		goto nla_put_failure;
14721 
14722 	if (match->info && match->info_len &&
14723 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
14724 		    match->info))
14725 		goto nla_put_failure;
14726 
14727 	nla_nest_end(msg, peer_func_attr);
14728 	nla_nest_end(msg, match_attr);
14729 	genlmsg_end(msg, hdr);
14730 
14731 	if (!wdev->owner_nlportid)
14732 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14733 					msg, 0, NL80211_MCGRP_NAN, gfp);
14734 	else
14735 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14736 				wdev->owner_nlportid);
14737 
14738 	return;
14739 
14740 nla_put_failure:
14741 	nlmsg_free(msg);
14742 }
14743 EXPORT_SYMBOL(cfg80211_nan_match);
14744 
14745 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
14746 				  u8 inst_id,
14747 				  enum nl80211_nan_func_term_reason reason,
14748 				  u64 cookie, gfp_t gfp)
14749 {
14750 	struct wiphy *wiphy = wdev->wiphy;
14751 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14752 	struct sk_buff *msg;
14753 	struct nlattr *func_attr;
14754 	void *hdr;
14755 
14756 	if (WARN_ON(!inst_id))
14757 		return;
14758 
14759 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14760 	if (!msg)
14761 		return;
14762 
14763 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
14764 	if (!hdr) {
14765 		nlmsg_free(msg);
14766 		return;
14767 	}
14768 
14769 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14770 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14771 					 wdev->netdev->ifindex)) ||
14772 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14773 			      NL80211_ATTR_PAD))
14774 		goto nla_put_failure;
14775 
14776 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14777 			      NL80211_ATTR_PAD))
14778 		goto nla_put_failure;
14779 
14780 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14781 	if (!func_attr)
14782 		goto nla_put_failure;
14783 
14784 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
14785 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
14786 		goto nla_put_failure;
14787 
14788 	nla_nest_end(msg, func_attr);
14789 	genlmsg_end(msg, hdr);
14790 
14791 	if (!wdev->owner_nlportid)
14792 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14793 					msg, 0, NL80211_MCGRP_NAN, gfp);
14794 	else
14795 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14796 				wdev->owner_nlportid);
14797 
14798 	return;
14799 
14800 nla_put_failure:
14801 	nlmsg_free(msg);
14802 }
14803 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
14804 
14805 static int nl80211_get_protocol_features(struct sk_buff *skb,
14806 					 struct genl_info *info)
14807 {
14808 	void *hdr;
14809 	struct sk_buff *msg;
14810 
14811 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14812 	if (!msg)
14813 		return -ENOMEM;
14814 
14815 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14816 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
14817 	if (!hdr)
14818 		goto nla_put_failure;
14819 
14820 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
14821 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
14822 		goto nla_put_failure;
14823 
14824 	genlmsg_end(msg, hdr);
14825 	return genlmsg_reply(msg, info);
14826 
14827  nla_put_failure:
14828 	kfree_skb(msg);
14829 	return -ENOBUFS;
14830 }
14831 
14832 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
14833 {
14834 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14835 	struct cfg80211_update_ft_ies_params ft_params;
14836 	struct net_device *dev = info->user_ptr[1];
14837 
14838 	if (!rdev->ops->update_ft_ies)
14839 		return -EOPNOTSUPP;
14840 
14841 	if (!info->attrs[NL80211_ATTR_MDID] ||
14842 	    !info->attrs[NL80211_ATTR_IE])
14843 		return -EINVAL;
14844 
14845 	memset(&ft_params, 0, sizeof(ft_params));
14846 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
14847 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
14848 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
14849 
14850 	return rdev_update_ft_ies(rdev, dev, &ft_params);
14851 }
14852 
14853 static int nl80211_crit_protocol_start(struct sk_buff *skb,
14854 				       struct genl_info *info)
14855 {
14856 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14857 	struct wireless_dev *wdev = info->user_ptr[1];
14858 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
14859 	u16 duration;
14860 	int ret;
14861 
14862 	if (!rdev->ops->crit_proto_start)
14863 		return -EOPNOTSUPP;
14864 
14865 	if (WARN_ON(!rdev->ops->crit_proto_stop))
14866 		return -EINVAL;
14867 
14868 	if (rdev->crit_proto_nlportid)
14869 		return -EBUSY;
14870 
14871 	/* determine protocol if provided */
14872 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
14873 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
14874 
14875 	if (proto >= NUM_NL80211_CRIT_PROTO)
14876 		return -EINVAL;
14877 
14878 	/* timeout must be provided */
14879 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
14880 		return -EINVAL;
14881 
14882 	duration =
14883 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
14884 
14885 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
14886 	if (!ret)
14887 		rdev->crit_proto_nlportid = info->snd_portid;
14888 
14889 	return ret;
14890 }
14891 
14892 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
14893 				      struct genl_info *info)
14894 {
14895 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14896 	struct wireless_dev *wdev = info->user_ptr[1];
14897 
14898 	if (!rdev->ops->crit_proto_stop)
14899 		return -EOPNOTSUPP;
14900 
14901 	if (rdev->crit_proto_nlportid) {
14902 		rdev->crit_proto_nlportid = 0;
14903 		rdev_crit_proto_stop(rdev, wdev);
14904 	}
14905 	return 0;
14906 }
14907 
14908 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
14909 				       struct nlattr *attr,
14910 				       struct netlink_ext_ack *extack)
14911 {
14912 	if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
14913 		if (attr->nla_type & NLA_F_NESTED) {
14914 			NL_SET_ERR_MSG_ATTR(extack, attr,
14915 					    "unexpected nested data");
14916 			return -EINVAL;
14917 		}
14918 
14919 		return 0;
14920 	}
14921 
14922 	if (!(attr->nla_type & NLA_F_NESTED)) {
14923 		NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
14924 		return -EINVAL;
14925 	}
14926 
14927 	return nla_validate_nested(attr, vcmd->maxattr, vcmd->policy, extack);
14928 }
14929 
14930 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
14931 {
14932 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14933 	struct wireless_dev *wdev =
14934 		__cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
14935 					   info->attrs);
14936 	int i, err;
14937 	u32 vid, subcmd;
14938 
14939 	if (!rdev->wiphy.vendor_commands)
14940 		return -EOPNOTSUPP;
14941 
14942 	if (IS_ERR(wdev)) {
14943 		err = PTR_ERR(wdev);
14944 		if (err != -EINVAL)
14945 			return err;
14946 		wdev = NULL;
14947 	} else if (wdev->wiphy != &rdev->wiphy) {
14948 		return -EINVAL;
14949 	}
14950 
14951 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
14952 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
14953 		return -EINVAL;
14954 
14955 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
14956 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
14957 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
14958 		const struct wiphy_vendor_command *vcmd;
14959 		void *data = NULL;
14960 		int len = 0;
14961 
14962 		vcmd = &rdev->wiphy.vendor_commands[i];
14963 
14964 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
14965 			continue;
14966 
14967 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
14968 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
14969 			if (!wdev)
14970 				return -EINVAL;
14971 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
14972 			    !wdev->netdev)
14973 				return -EINVAL;
14974 
14975 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
14976 				if (!wdev_running(wdev))
14977 					return -ENETDOWN;
14978 			}
14979 		} else {
14980 			wdev = NULL;
14981 		}
14982 
14983 		if (!vcmd->doit)
14984 			return -EOPNOTSUPP;
14985 
14986 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
14987 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
14988 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
14989 
14990 			err = nl80211_vendor_check_policy(vcmd,
14991 					info->attrs[NL80211_ATTR_VENDOR_DATA],
14992 					info->extack);
14993 			if (err)
14994 				return err;
14995 		}
14996 
14997 		rdev->cur_cmd_info = info;
14998 		err = vcmd->doit(&rdev->wiphy, wdev, data, len);
14999 		rdev->cur_cmd_info = NULL;
15000 		return err;
15001 	}
15002 
15003 	return -EOPNOTSUPP;
15004 }
15005 
15006 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
15007 				       struct netlink_callback *cb,
15008 				       struct cfg80211_registered_device **rdev,
15009 				       struct wireless_dev **wdev)
15010 {
15011 	struct nlattr **attrbuf;
15012 	u32 vid, subcmd;
15013 	unsigned int i;
15014 	int vcmd_idx = -1;
15015 	int err;
15016 	void *data = NULL;
15017 	unsigned int data_len = 0;
15018 
15019 	if (cb->args[0]) {
15020 		/* subtract the 1 again here */
15021 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
15022 		struct wireless_dev *tmp;
15023 
15024 		if (!wiphy)
15025 			return -ENODEV;
15026 		*rdev = wiphy_to_rdev(wiphy);
15027 		*wdev = NULL;
15028 
15029 		if (cb->args[1]) {
15030 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
15031 				if (tmp->identifier == cb->args[1] - 1) {
15032 					*wdev = tmp;
15033 					break;
15034 				}
15035 			}
15036 		}
15037 
15038 		/* keep rtnl locked in successful case */
15039 		return 0;
15040 	}
15041 
15042 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
15043 	if (!attrbuf)
15044 		return -ENOMEM;
15045 
15046 	err = nlmsg_parse_deprecated(cb->nlh,
15047 				     GENL_HDRLEN + nl80211_fam.hdrsize,
15048 				     attrbuf, nl80211_fam.maxattr,
15049 				     nl80211_policy, NULL);
15050 	if (err)
15051 		goto out;
15052 
15053 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
15054 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
15055 		err = -EINVAL;
15056 		goto out;
15057 	}
15058 
15059 	*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(skb->sk), attrbuf);
15060 	if (IS_ERR(*wdev))
15061 		*wdev = NULL;
15062 
15063 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
15064 	if (IS_ERR(*rdev)) {
15065 		err = PTR_ERR(*rdev);
15066 		goto out;
15067 	}
15068 
15069 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
15070 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
15071 
15072 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
15073 		const struct wiphy_vendor_command *vcmd;
15074 
15075 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
15076 
15077 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
15078 			continue;
15079 
15080 		if (!vcmd->dumpit) {
15081 			err = -EOPNOTSUPP;
15082 			goto out;
15083 		}
15084 
15085 		vcmd_idx = i;
15086 		break;
15087 	}
15088 
15089 	if (vcmd_idx < 0) {
15090 		err = -EOPNOTSUPP;
15091 		goto out;
15092 	}
15093 
15094 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
15095 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
15096 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
15097 
15098 		err = nl80211_vendor_check_policy(
15099 				&(*rdev)->wiphy.vendor_commands[vcmd_idx],
15100 				attrbuf[NL80211_ATTR_VENDOR_DATA],
15101 				cb->extack);
15102 		if (err)
15103 			goto out;
15104 	}
15105 
15106 	/* 0 is the first index - add 1 to parse only once */
15107 	cb->args[0] = (*rdev)->wiphy_idx + 1;
15108 	/* add 1 to know if it was NULL */
15109 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
15110 	cb->args[2] = vcmd_idx;
15111 	cb->args[3] = (unsigned long)data;
15112 	cb->args[4] = data_len;
15113 
15114 	/* keep rtnl locked in successful case */
15115 	err = 0;
15116 out:
15117 	kfree(attrbuf);
15118 	return err;
15119 }
15120 
15121 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
15122 				   struct netlink_callback *cb)
15123 {
15124 	struct cfg80211_registered_device *rdev;
15125 	struct wireless_dev *wdev;
15126 	unsigned int vcmd_idx;
15127 	const struct wiphy_vendor_command *vcmd;
15128 	void *data;
15129 	int data_len;
15130 	int err;
15131 	struct nlattr *vendor_data;
15132 
15133 	rtnl_lock();
15134 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
15135 	if (err)
15136 		goto out;
15137 
15138 	vcmd_idx = cb->args[2];
15139 	data = (void *)cb->args[3];
15140 	data_len = cb->args[4];
15141 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
15142 
15143 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
15144 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
15145 		if (!wdev) {
15146 			err = -EINVAL;
15147 			goto out;
15148 		}
15149 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
15150 		    !wdev->netdev) {
15151 			err = -EINVAL;
15152 			goto out;
15153 		}
15154 
15155 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
15156 			if (!wdev_running(wdev)) {
15157 				err = -ENETDOWN;
15158 				goto out;
15159 			}
15160 		}
15161 	}
15162 
15163 	while (1) {
15164 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
15165 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
15166 					   NL80211_CMD_VENDOR);
15167 		if (!hdr)
15168 			break;
15169 
15170 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15171 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
15172 					       wdev_id(wdev),
15173 					       NL80211_ATTR_PAD))) {
15174 			genlmsg_cancel(skb, hdr);
15175 			break;
15176 		}
15177 
15178 		vendor_data = nla_nest_start_noflag(skb,
15179 						    NL80211_ATTR_VENDOR_DATA);
15180 		if (!vendor_data) {
15181 			genlmsg_cancel(skb, hdr);
15182 			break;
15183 		}
15184 
15185 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
15186 				   (unsigned long *)&cb->args[5]);
15187 		nla_nest_end(skb, vendor_data);
15188 
15189 		if (err == -ENOBUFS || err == -ENOENT) {
15190 			genlmsg_cancel(skb, hdr);
15191 			break;
15192 		} else if (err <= 0) {
15193 			genlmsg_cancel(skb, hdr);
15194 			goto out;
15195 		}
15196 
15197 		genlmsg_end(skb, hdr);
15198 	}
15199 
15200 	err = skb->len;
15201  out:
15202 	rtnl_unlock();
15203 	return err;
15204 }
15205 
15206 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
15207 					   enum nl80211_commands cmd,
15208 					   enum nl80211_attrs attr,
15209 					   int approxlen)
15210 {
15211 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15212 
15213 	if (WARN_ON(!rdev->cur_cmd_info))
15214 		return NULL;
15215 
15216 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
15217 					   rdev->cur_cmd_info->snd_portid,
15218 					   rdev->cur_cmd_info->snd_seq,
15219 					   cmd, attr, NULL, GFP_KERNEL);
15220 }
15221 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
15222 
15223 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
15224 {
15225 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
15226 	void *hdr = ((void **)skb->cb)[1];
15227 	struct nlattr *data = ((void **)skb->cb)[2];
15228 
15229 	/* clear CB data for netlink core to own from now on */
15230 	memset(skb->cb, 0, sizeof(skb->cb));
15231 
15232 	if (WARN_ON(!rdev->cur_cmd_info)) {
15233 		kfree_skb(skb);
15234 		return -EINVAL;
15235 	}
15236 
15237 	nla_nest_end(skb, data);
15238 	genlmsg_end(skb, hdr);
15239 	return genlmsg_reply(skb, rdev->cur_cmd_info);
15240 }
15241 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
15242 
15243 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
15244 {
15245 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15246 
15247 	if (WARN_ON(!rdev->cur_cmd_info))
15248 		return 0;
15249 
15250 	return rdev->cur_cmd_info->snd_portid;
15251 }
15252 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
15253 
15254 static int nl80211_set_qos_map(struct sk_buff *skb,
15255 			       struct genl_info *info)
15256 {
15257 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15258 	struct cfg80211_qos_map *qos_map = NULL;
15259 	struct net_device *dev = info->user_ptr[1];
15260 	u8 *pos, len, num_des, des_len, des;
15261 	int ret;
15262 
15263 	if (!rdev->ops->set_qos_map)
15264 		return -EOPNOTSUPP;
15265 
15266 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
15267 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
15268 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
15269 
15270 		if (len % 2)
15271 			return -EINVAL;
15272 
15273 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
15274 		if (!qos_map)
15275 			return -ENOMEM;
15276 
15277 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
15278 		if (num_des) {
15279 			des_len = num_des *
15280 				sizeof(struct cfg80211_dscp_exception);
15281 			memcpy(qos_map->dscp_exception, pos, des_len);
15282 			qos_map->num_des = num_des;
15283 			for (des = 0; des < num_des; des++) {
15284 				if (qos_map->dscp_exception[des].up > 7) {
15285 					kfree(qos_map);
15286 					return -EINVAL;
15287 				}
15288 			}
15289 			pos += des_len;
15290 		}
15291 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
15292 	}
15293 
15294 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
15295 	if (!ret)
15296 		ret = rdev_set_qos_map(rdev, dev, qos_map);
15297 
15298 	kfree(qos_map);
15299 	return ret;
15300 }
15301 
15302 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
15303 {
15304 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15305 	struct net_device *dev = info->user_ptr[1];
15306 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15307 	const u8 *peer;
15308 	u8 tsid, up;
15309 	u16 admitted_time = 0;
15310 
15311 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
15312 		return -EOPNOTSUPP;
15313 
15314 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
15315 	    !info->attrs[NL80211_ATTR_USER_PRIO])
15316 		return -EINVAL;
15317 
15318 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15319 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
15320 
15321 	/* WMM uses TIDs 0-7 even for TSPEC */
15322 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
15323 		/* TODO: handle 802.11 TSPEC/admission control
15324 		 * need more attributes for that (e.g. BA session requirement);
15325 		 * change the WMM adminssion test above to allow both then
15326 		 */
15327 		return -EINVAL;
15328 	}
15329 
15330 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15331 
15332 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
15333 		admitted_time =
15334 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
15335 		if (!admitted_time)
15336 			return -EINVAL;
15337 	}
15338 
15339 	switch (wdev->iftype) {
15340 	case NL80211_IFTYPE_STATION:
15341 	case NL80211_IFTYPE_P2P_CLIENT:
15342 		if (wdev->connected)
15343 			break;
15344 		return -ENOTCONN;
15345 	default:
15346 		return -EOPNOTSUPP;
15347 	}
15348 
15349 	return rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
15350 }
15351 
15352 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
15353 {
15354 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15355 	struct net_device *dev = info->user_ptr[1];
15356 	const u8 *peer;
15357 	u8 tsid;
15358 
15359 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
15360 		return -EINVAL;
15361 
15362 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15363 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15364 
15365 	return rdev_del_tx_ts(rdev, dev, tsid, peer);
15366 }
15367 
15368 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
15369 				       struct genl_info *info)
15370 {
15371 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15372 	struct net_device *dev = info->user_ptr[1];
15373 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15374 	struct cfg80211_chan_def chandef = {};
15375 	const u8 *addr;
15376 	u8 oper_class;
15377 	int err;
15378 
15379 	if (!rdev->ops->tdls_channel_switch ||
15380 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15381 		return -EOPNOTSUPP;
15382 
15383 	switch (dev->ieee80211_ptr->iftype) {
15384 	case NL80211_IFTYPE_STATION:
15385 	case NL80211_IFTYPE_P2P_CLIENT:
15386 		break;
15387 	default:
15388 		return -EOPNOTSUPP;
15389 	}
15390 
15391 	if (!info->attrs[NL80211_ATTR_MAC] ||
15392 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
15393 		return -EINVAL;
15394 
15395 	err = nl80211_parse_chandef(rdev, info, &chandef);
15396 	if (err)
15397 		return err;
15398 
15399 	/*
15400 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
15401 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
15402 	 * specification is not defined for them.
15403 	 */
15404 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
15405 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
15406 	    chandef.width != NL80211_CHAN_WIDTH_20)
15407 		return -EINVAL;
15408 
15409 	/* we will be active on the TDLS link */
15410 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
15411 					   wdev->iftype))
15412 		return -EINVAL;
15413 
15414 	/* don't allow switching to DFS channels */
15415 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
15416 		return -EINVAL;
15417 
15418 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15419 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
15420 
15421 	return rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
15422 }
15423 
15424 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
15425 					      struct genl_info *info)
15426 {
15427 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15428 	struct net_device *dev = info->user_ptr[1];
15429 	const u8 *addr;
15430 
15431 	if (!rdev->ops->tdls_channel_switch ||
15432 	    !rdev->ops->tdls_cancel_channel_switch ||
15433 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15434 		return -EOPNOTSUPP;
15435 
15436 	switch (dev->ieee80211_ptr->iftype) {
15437 	case NL80211_IFTYPE_STATION:
15438 	case NL80211_IFTYPE_P2P_CLIENT:
15439 		break;
15440 	default:
15441 		return -EOPNOTSUPP;
15442 	}
15443 
15444 	if (!info->attrs[NL80211_ATTR_MAC])
15445 		return -EINVAL;
15446 
15447 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15448 
15449 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
15450 
15451 	return 0;
15452 }
15453 
15454 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
15455 					    struct genl_info *info)
15456 {
15457 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15458 	struct net_device *dev = info->user_ptr[1];
15459 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15460 	const struct nlattr *nla;
15461 	bool enabled;
15462 
15463 	if (!rdev->ops->set_multicast_to_unicast)
15464 		return -EOPNOTSUPP;
15465 
15466 	if (wdev->iftype != NL80211_IFTYPE_AP &&
15467 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
15468 		return -EOPNOTSUPP;
15469 
15470 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
15471 	enabled = nla_get_flag(nla);
15472 
15473 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
15474 }
15475 
15476 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
15477 {
15478 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15479 	struct net_device *dev = info->user_ptr[1];
15480 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15481 	struct cfg80211_pmk_conf pmk_conf = {};
15482 
15483 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15484 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15485 		return -EOPNOTSUPP;
15486 
15487 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15488 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15489 		return -EOPNOTSUPP;
15490 
15491 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
15492 		return -EINVAL;
15493 
15494 	if (!wdev->connected)
15495 		return -ENOTCONN;
15496 
15497 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15498 	if (memcmp(pmk_conf.aa, wdev->u.client.connected_addr, ETH_ALEN))
15499 		return -EINVAL;
15500 
15501 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
15502 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
15503 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
15504 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192)
15505 		return -EINVAL;
15506 
15507 	if (info->attrs[NL80211_ATTR_PMKR0_NAME])
15508 		pmk_conf.pmk_r0_name =
15509 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
15510 
15511 	return rdev_set_pmk(rdev, dev, &pmk_conf);
15512 }
15513 
15514 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
15515 {
15516 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15517 	struct net_device *dev = info->user_ptr[1];
15518 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15519 	const u8 *aa;
15520 
15521 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15522 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15523 		return -EOPNOTSUPP;
15524 
15525 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15526 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15527 		return -EOPNOTSUPP;
15528 
15529 	if (!info->attrs[NL80211_ATTR_MAC])
15530 		return -EINVAL;
15531 
15532 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15533 	return rdev_del_pmk(rdev, dev, aa);
15534 }
15535 
15536 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
15537 {
15538 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15539 	struct net_device *dev = info->user_ptr[1];
15540 	struct cfg80211_external_auth_params params;
15541 
15542 	if (!rdev->ops->external_auth)
15543 		return -EOPNOTSUPP;
15544 
15545 	if (!info->attrs[NL80211_ATTR_SSID] &&
15546 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
15547 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
15548 		return -EINVAL;
15549 
15550 	if (!info->attrs[NL80211_ATTR_BSSID])
15551 		return -EINVAL;
15552 
15553 	if (!info->attrs[NL80211_ATTR_STATUS_CODE])
15554 		return -EINVAL;
15555 
15556 	memset(&params, 0, sizeof(params));
15557 
15558 	if (info->attrs[NL80211_ATTR_SSID]) {
15559 		params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
15560 		if (params.ssid.ssid_len == 0)
15561 			return -EINVAL;
15562 		memcpy(params.ssid.ssid,
15563 		       nla_data(info->attrs[NL80211_ATTR_SSID]),
15564 		       params.ssid.ssid_len);
15565 	}
15566 
15567 	memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
15568 	       ETH_ALEN);
15569 
15570 	params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15571 
15572 	if (info->attrs[NL80211_ATTR_PMKID])
15573 		params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
15574 
15575 	return rdev_external_auth(rdev, dev, &params);
15576 }
15577 
15578 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
15579 {
15580 	bool dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
15581 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15582 	struct net_device *dev = info->user_ptr[1];
15583 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15584 	const u8 *buf;
15585 	size_t len;
15586 	u8 *dest;
15587 	u16 proto;
15588 	bool noencrypt;
15589 	u64 cookie = 0;
15590 	int link_id;
15591 	int err;
15592 
15593 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15594 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
15595 		return -EOPNOTSUPP;
15596 
15597 	if (!rdev->ops->tx_control_port)
15598 		return -EOPNOTSUPP;
15599 
15600 	if (!info->attrs[NL80211_ATTR_FRAME] ||
15601 	    !info->attrs[NL80211_ATTR_MAC] ||
15602 	    !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
15603 		GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
15604 		return -EINVAL;
15605 	}
15606 
15607 	switch (wdev->iftype) {
15608 	case NL80211_IFTYPE_AP:
15609 	case NL80211_IFTYPE_P2P_GO:
15610 	case NL80211_IFTYPE_MESH_POINT:
15611 		break;
15612 	case NL80211_IFTYPE_ADHOC:
15613 		if (wdev->u.ibss.current_bss)
15614 			break;
15615 		return -ENOTCONN;
15616 	case NL80211_IFTYPE_STATION:
15617 	case NL80211_IFTYPE_P2P_CLIENT:
15618 		if (wdev->connected)
15619 			break;
15620 		return -ENOTCONN;
15621 	default:
15622 		return -EOPNOTSUPP;
15623 	}
15624 
15625 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15626 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15627 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15628 	proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
15629 	noencrypt =
15630 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
15631 
15632 	link_id = nl80211_link_id_or_invalid(info->attrs);
15633 
15634 	err = rdev_tx_control_port(rdev, dev, buf, len,
15635 				   dest, cpu_to_be16(proto), noencrypt, link_id,
15636 				   dont_wait_for_ack ? NULL : &cookie);
15637 	if (!err && !dont_wait_for_ack)
15638 		nl_set_extack_cookie_u64(info->extack, cookie);
15639 	return err;
15640 }
15641 
15642 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
15643 					   struct genl_info *info)
15644 {
15645 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15646 	struct net_device *dev = info->user_ptr[1];
15647 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15648 	struct cfg80211_ftm_responder_stats ftm_stats = {};
15649 	unsigned int link_id = nl80211_link_id(info->attrs);
15650 	struct sk_buff *msg;
15651 	void *hdr;
15652 	struct nlattr *ftm_stats_attr;
15653 	int err;
15654 
15655 	if (wdev->iftype != NL80211_IFTYPE_AP ||
15656 	    !wdev->links[link_id].ap.beacon_interval)
15657 		return -EOPNOTSUPP;
15658 
15659 	err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
15660 	if (err)
15661 		return err;
15662 
15663 	if (!ftm_stats.filled)
15664 		return -ENODATA;
15665 
15666 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15667 	if (!msg)
15668 		return -ENOMEM;
15669 
15670 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
15671 			     NL80211_CMD_GET_FTM_RESPONDER_STATS);
15672 	if (!hdr)
15673 		goto nla_put_failure;
15674 
15675 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15676 		goto nla_put_failure;
15677 
15678 	ftm_stats_attr = nla_nest_start_noflag(msg,
15679 					       NL80211_ATTR_FTM_RESPONDER_STATS);
15680 	if (!ftm_stats_attr)
15681 		goto nla_put_failure;
15682 
15683 #define SET_FTM(field, name, type)					 \
15684 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15685 	    nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,		 \
15686 			     ftm_stats.field))				 \
15687 		goto nla_put_failure; } while (0)
15688 #define SET_FTM_U64(field, name)					 \
15689 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15690 	    nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,		 \
15691 			      ftm_stats.field, NL80211_FTM_STATS_PAD))	 \
15692 		goto nla_put_failure; } while (0)
15693 
15694 	SET_FTM(success_num, SUCCESS_NUM, u32);
15695 	SET_FTM(partial_num, PARTIAL_NUM, u32);
15696 	SET_FTM(failed_num, FAILED_NUM, u32);
15697 	SET_FTM(asap_num, ASAP_NUM, u32);
15698 	SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
15699 	SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
15700 	SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
15701 	SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
15702 	SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
15703 #undef SET_FTM
15704 
15705 	nla_nest_end(msg, ftm_stats_attr);
15706 
15707 	genlmsg_end(msg, hdr);
15708 	return genlmsg_reply(msg, info);
15709 
15710 nla_put_failure:
15711 	nlmsg_free(msg);
15712 	return -ENOBUFS;
15713 }
15714 
15715 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
15716 {
15717 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15718 	struct cfg80211_update_owe_info owe_info;
15719 	struct net_device *dev = info->user_ptr[1];
15720 
15721 	if (!rdev->ops->update_owe_info)
15722 		return -EOPNOTSUPP;
15723 
15724 	if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
15725 	    !info->attrs[NL80211_ATTR_MAC])
15726 		return -EINVAL;
15727 
15728 	memset(&owe_info, 0, sizeof(owe_info));
15729 	owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15730 	nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
15731 
15732 	if (info->attrs[NL80211_ATTR_IE]) {
15733 		owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
15734 		owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
15735 	}
15736 
15737 	return rdev_update_owe_info(rdev, dev, &owe_info);
15738 }
15739 
15740 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
15741 {
15742 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15743 	struct net_device *dev = info->user_ptr[1];
15744 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15745 	struct station_info sinfo = {};
15746 	const u8 *buf;
15747 	size_t len;
15748 	u8 *dest;
15749 	int err;
15750 
15751 	if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
15752 		return -EOPNOTSUPP;
15753 
15754 	if (!info->attrs[NL80211_ATTR_MAC] ||
15755 	    !info->attrs[NL80211_ATTR_FRAME]) {
15756 		GENL_SET_ERR_MSG(info, "Frame or MAC missing");
15757 		return -EINVAL;
15758 	}
15759 
15760 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
15761 		return -EOPNOTSUPP;
15762 
15763 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15764 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15765 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15766 
15767 	if (len < sizeof(struct ethhdr))
15768 		return -EINVAL;
15769 
15770 	if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
15771 	    !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
15772 		return -EINVAL;
15773 
15774 	err = rdev_get_station(rdev, dev, dest, &sinfo);
15775 	if (err)
15776 		return err;
15777 
15778 	cfg80211_sinfo_release_content(&sinfo);
15779 
15780 	return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
15781 }
15782 
15783 static int parse_tid_conf(struct cfg80211_registered_device *rdev,
15784 			  struct nlattr *attrs[], struct net_device *dev,
15785 			  struct cfg80211_tid_cfg *tid_conf,
15786 			  struct genl_info *info, const u8 *peer,
15787 			  unsigned int link_id)
15788 {
15789 	struct netlink_ext_ack *extack = info->extack;
15790 	u64 mask;
15791 	int err;
15792 
15793 	if (!attrs[NL80211_TID_CONFIG_ATTR_TIDS])
15794 		return -EINVAL;
15795 
15796 	tid_conf->config_override =
15797 			nla_get_flag(attrs[NL80211_TID_CONFIG_ATTR_OVERRIDE]);
15798 	tid_conf->tids = nla_get_u16(attrs[NL80211_TID_CONFIG_ATTR_TIDS]);
15799 
15800 	if (tid_conf->config_override) {
15801 		if (rdev->ops->reset_tid_config) {
15802 			err = rdev_reset_tid_config(rdev, dev, peer,
15803 						    tid_conf->tids);
15804 			if (err)
15805 				return err;
15806 		} else {
15807 			return -EINVAL;
15808 		}
15809 	}
15810 
15811 	if (attrs[NL80211_TID_CONFIG_ATTR_NOACK]) {
15812 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_NOACK);
15813 		tid_conf->noack =
15814 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_NOACK]);
15815 	}
15816 
15817 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]) {
15818 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT);
15819 		tid_conf->retry_short =
15820 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]);
15821 
15822 		if (tid_conf->retry_short > rdev->wiphy.max_data_retry_count)
15823 			return -EINVAL;
15824 	}
15825 
15826 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]) {
15827 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
15828 		tid_conf->retry_long =
15829 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]);
15830 
15831 		if (tid_conf->retry_long > rdev->wiphy.max_data_retry_count)
15832 			return -EINVAL;
15833 	}
15834 
15835 	if (attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]) {
15836 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
15837 		tid_conf->ampdu =
15838 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]);
15839 	}
15840 
15841 	if (attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]) {
15842 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
15843 		tid_conf->rtscts =
15844 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]);
15845 	}
15846 
15847 	if (attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]) {
15848 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
15849 		tid_conf->amsdu =
15850 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]);
15851 	}
15852 
15853 	if (attrs[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE]) {
15854 		u32 idx = NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE, attr;
15855 
15856 		tid_conf->txrate_type = nla_get_u8(attrs[idx]);
15857 
15858 		if (tid_conf->txrate_type != NL80211_TX_RATE_AUTOMATIC) {
15859 			attr = NL80211_TID_CONFIG_ATTR_TX_RATE;
15860 			err = nl80211_parse_tx_bitrate_mask(info, attrs, attr,
15861 						    &tid_conf->txrate_mask, dev,
15862 						    true, link_id);
15863 			if (err)
15864 				return err;
15865 
15866 			tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE);
15867 		}
15868 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
15869 	}
15870 
15871 	if (peer)
15872 		mask = rdev->wiphy.tid_config_support.peer;
15873 	else
15874 		mask = rdev->wiphy.tid_config_support.vif;
15875 
15876 	if (tid_conf->mask & ~mask) {
15877 		NL_SET_ERR_MSG(extack, "unsupported TID configuration");
15878 		return -EOPNOTSUPP;
15879 	}
15880 
15881 	return 0;
15882 }
15883 
15884 static int nl80211_set_tid_config(struct sk_buff *skb,
15885 				  struct genl_info *info)
15886 {
15887 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15888 	struct nlattr *attrs[NL80211_TID_CONFIG_ATTR_MAX + 1];
15889 	unsigned int link_id = nl80211_link_id(info->attrs);
15890 	struct net_device *dev = info->user_ptr[1];
15891 	struct cfg80211_tid_config *tid_config;
15892 	struct nlattr *tid;
15893 	int conf_idx = 0, rem_conf;
15894 	int ret = -EINVAL;
15895 	u32 num_conf = 0;
15896 
15897 	if (!info->attrs[NL80211_ATTR_TID_CONFIG])
15898 		return -EINVAL;
15899 
15900 	if (!rdev->ops->set_tid_config)
15901 		return -EOPNOTSUPP;
15902 
15903 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
15904 			    rem_conf)
15905 		num_conf++;
15906 
15907 	tid_config = kzalloc(struct_size(tid_config, tid_conf, num_conf),
15908 			     GFP_KERNEL);
15909 	if (!tid_config)
15910 		return -ENOMEM;
15911 
15912 	tid_config->n_tid_conf = num_conf;
15913 
15914 	if (info->attrs[NL80211_ATTR_MAC])
15915 		tid_config->peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15916 
15917 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
15918 			    rem_conf) {
15919 		ret = nla_parse_nested(attrs, NL80211_TID_CONFIG_ATTR_MAX,
15920 				       tid, NULL, NULL);
15921 
15922 		if (ret)
15923 			goto bad_tid_conf;
15924 
15925 		ret = parse_tid_conf(rdev, attrs, dev,
15926 				     &tid_config->tid_conf[conf_idx],
15927 				     info, tid_config->peer, link_id);
15928 		if (ret)
15929 			goto bad_tid_conf;
15930 
15931 		conf_idx++;
15932 	}
15933 
15934 	ret = rdev_set_tid_config(rdev, dev, tid_config);
15935 
15936 bad_tid_conf:
15937 	kfree(tid_config);
15938 	return ret;
15939 }
15940 
15941 static int nl80211_color_change(struct sk_buff *skb, struct genl_info *info)
15942 {
15943 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15944 	struct cfg80211_color_change_settings params = {};
15945 	struct net_device *dev = info->user_ptr[1];
15946 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15947 	struct nlattr **tb;
15948 	u16 offset;
15949 	int err;
15950 
15951 	if (!rdev->ops->color_change)
15952 		return -EOPNOTSUPP;
15953 
15954 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15955 				     NL80211_EXT_FEATURE_BSS_COLOR))
15956 		return -EOPNOTSUPP;
15957 
15958 	if (wdev->iftype != NL80211_IFTYPE_AP)
15959 		return -EOPNOTSUPP;
15960 
15961 	if (!info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT] ||
15962 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR] ||
15963 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS])
15964 		return -EINVAL;
15965 
15966 	params.count = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT]);
15967 	params.color = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR]);
15968 
15969 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_next,
15970 				   info->extack);
15971 	if (err)
15972 		return err;
15973 
15974 	tb = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*tb), GFP_KERNEL);
15975 	if (!tb)
15976 		return -ENOMEM;
15977 
15978 	err = nla_parse_nested(tb, NL80211_ATTR_MAX,
15979 			       info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS],
15980 			       nl80211_policy, info->extack);
15981 	if (err)
15982 		goto out;
15983 
15984 	err = nl80211_parse_beacon(rdev, tb, &params.beacon_color_change,
15985 				   info->extack);
15986 	if (err)
15987 		goto out;
15988 
15989 	if (!tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
15990 		err = -EINVAL;
15991 		goto out;
15992 	}
15993 
15994 	if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) != sizeof(u16)) {
15995 		err = -EINVAL;
15996 		goto out;
15997 	}
15998 
15999 	offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
16000 	if (offset >= params.beacon_color_change.tail_len) {
16001 		err = -EINVAL;
16002 		goto out;
16003 	}
16004 
16005 	if (params.beacon_color_change.tail[offset] != params.count) {
16006 		err = -EINVAL;
16007 		goto out;
16008 	}
16009 
16010 	params.counter_offset_beacon = offset;
16011 
16012 	if (tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
16013 		if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) !=
16014 		    sizeof(u16)) {
16015 			err = -EINVAL;
16016 			goto out;
16017 		}
16018 
16019 		offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
16020 		if (offset >= params.beacon_color_change.probe_resp_len) {
16021 			err = -EINVAL;
16022 			goto out;
16023 		}
16024 
16025 		if (params.beacon_color_change.probe_resp[offset] !=
16026 		    params.count) {
16027 			err = -EINVAL;
16028 			goto out;
16029 		}
16030 
16031 		params.counter_offset_presp = offset;
16032 	}
16033 
16034 	err = rdev_color_change(rdev, dev, &params);
16035 
16036 out:
16037 	kfree(params.beacon_next.mbssid_ies);
16038 	kfree(params.beacon_color_change.mbssid_ies);
16039 	kfree(params.beacon_next.rnr_ies);
16040 	kfree(params.beacon_color_change.rnr_ies);
16041 	kfree(tb);
16042 	return err;
16043 }
16044 
16045 static int nl80211_set_fils_aad(struct sk_buff *skb,
16046 				struct genl_info *info)
16047 {
16048 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16049 	struct net_device *dev = info->user_ptr[1];
16050 	struct cfg80211_fils_aad fils_aad = {};
16051 	u8 *nonces;
16052 
16053 	if (!info->attrs[NL80211_ATTR_MAC] ||
16054 	    !info->attrs[NL80211_ATTR_FILS_KEK] ||
16055 	    !info->attrs[NL80211_ATTR_FILS_NONCES])
16056 		return -EINVAL;
16057 
16058 	fils_aad.macaddr = nla_data(info->attrs[NL80211_ATTR_MAC]);
16059 	fils_aad.kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
16060 	fils_aad.kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
16061 	nonces = nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
16062 	fils_aad.snonce = nonces;
16063 	fils_aad.anonce = nonces + FILS_NONCE_LEN;
16064 
16065 	return rdev_set_fils_aad(rdev, dev, &fils_aad);
16066 }
16067 
16068 static int nl80211_add_link(struct sk_buff *skb, struct genl_info *info)
16069 {
16070 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16071 	unsigned int link_id = nl80211_link_id(info->attrs);
16072 	struct net_device *dev = info->user_ptr[1];
16073 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16074 	int ret;
16075 
16076 	if (!(wdev->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
16077 		return -EINVAL;
16078 
16079 	switch (wdev->iftype) {
16080 	case NL80211_IFTYPE_AP:
16081 		break;
16082 	default:
16083 		return -EINVAL;
16084 	}
16085 
16086 	if (!info->attrs[NL80211_ATTR_MAC] ||
16087 	    !is_valid_ether_addr(nla_data(info->attrs[NL80211_ATTR_MAC])))
16088 		return -EINVAL;
16089 
16090 	wdev->valid_links |= BIT(link_id);
16091 	ether_addr_copy(wdev->links[link_id].addr,
16092 			nla_data(info->attrs[NL80211_ATTR_MAC]));
16093 
16094 	ret = rdev_add_intf_link(rdev, wdev, link_id);
16095 	if (ret) {
16096 		wdev->valid_links &= ~BIT(link_id);
16097 		eth_zero_addr(wdev->links[link_id].addr);
16098 	}
16099 
16100 	return ret;
16101 }
16102 
16103 static int nl80211_remove_link(struct sk_buff *skb, struct genl_info *info)
16104 {
16105 	unsigned int link_id = nl80211_link_id(info->attrs);
16106 	struct net_device *dev = info->user_ptr[1];
16107 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16108 
16109 	/* cannot remove if there's no link */
16110 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
16111 		return -EINVAL;
16112 
16113 	switch (wdev->iftype) {
16114 	case NL80211_IFTYPE_AP:
16115 		break;
16116 	default:
16117 		return -EINVAL;
16118 	}
16119 
16120 	cfg80211_remove_link(wdev, link_id);
16121 
16122 	return 0;
16123 }
16124 
16125 static int
16126 nl80211_add_mod_link_station(struct sk_buff *skb, struct genl_info *info,
16127 			     bool add)
16128 {
16129 	struct link_station_parameters params = {};
16130 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16131 	struct net_device *dev = info->user_ptr[1];
16132 	int err;
16133 
16134 	if ((add && !rdev->ops->add_link_station) ||
16135 	    (!add && !rdev->ops->mod_link_station))
16136 		return -EOPNOTSUPP;
16137 
16138 	if (add && !info->attrs[NL80211_ATTR_MAC])
16139 		return -EINVAL;
16140 
16141 	if (!info->attrs[NL80211_ATTR_MLD_ADDR])
16142 		return -EINVAL;
16143 
16144 	if (add && !info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
16145 		return -EINVAL;
16146 
16147 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
16148 
16149 	if (info->attrs[NL80211_ATTR_MAC]) {
16150 		params.link_mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
16151 		if (!is_valid_ether_addr(params.link_mac))
16152 			return -EINVAL;
16153 	}
16154 
16155 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
16156 		return -EINVAL;
16157 
16158 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
16159 
16160 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
16161 		params.supported_rates =
16162 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
16163 		params.supported_rates_len =
16164 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
16165 	}
16166 
16167 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
16168 		params.ht_capa =
16169 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
16170 
16171 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
16172 		params.vht_capa =
16173 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
16174 
16175 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
16176 		params.he_capa =
16177 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
16178 		params.he_capa_len =
16179 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
16180 
16181 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
16182 			params.eht_capa =
16183 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
16184 			params.eht_capa_len =
16185 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
16186 
16187 			if (!ieee80211_eht_capa_size_ok((const u8 *)params.he_capa,
16188 							(const u8 *)params.eht_capa,
16189 							params.eht_capa_len,
16190 							false))
16191 				return -EINVAL;
16192 		}
16193 	}
16194 
16195 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
16196 		params.he_6ghz_capa =
16197 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
16198 
16199 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
16200 		params.opmode_notif_used = true;
16201 		params.opmode_notif =
16202 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
16203 	}
16204 
16205 	err = nl80211_parse_sta_txpower_setting(info, &params.txpwr,
16206 						&params.txpwr_set);
16207 	if (err)
16208 		return err;
16209 
16210 	if (add)
16211 		return rdev_add_link_station(rdev, dev, &params);
16212 
16213 	return rdev_mod_link_station(rdev, dev, &params);
16214 }
16215 
16216 static int
16217 nl80211_add_link_station(struct sk_buff *skb, struct genl_info *info)
16218 {
16219 	return nl80211_add_mod_link_station(skb, info, true);
16220 }
16221 
16222 static int
16223 nl80211_modify_link_station(struct sk_buff *skb, struct genl_info *info)
16224 {
16225 	return nl80211_add_mod_link_station(skb, info, false);
16226 }
16227 
16228 static int
16229 nl80211_remove_link_station(struct sk_buff *skb, struct genl_info *info)
16230 {
16231 	struct link_station_del_parameters params = {};
16232 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16233 	struct net_device *dev = info->user_ptr[1];
16234 
16235 	if (!rdev->ops->del_link_station)
16236 		return -EOPNOTSUPP;
16237 
16238 	if (!info->attrs[NL80211_ATTR_MLD_ADDR] ||
16239 	    !info->attrs[NL80211_ATTR_MLO_LINK_ID])
16240 		return -EINVAL;
16241 
16242 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
16243 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
16244 
16245 	return rdev_del_link_station(rdev, dev, &params);
16246 }
16247 
16248 static int nl80211_set_hw_timestamp(struct sk_buff *skb,
16249 				    struct genl_info *info)
16250 {
16251 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16252 	struct net_device *dev = info->user_ptr[1];
16253 	struct cfg80211_set_hw_timestamp hwts = {};
16254 
16255 	if (!rdev->wiphy.hw_timestamp_max_peers)
16256 		return -EOPNOTSUPP;
16257 
16258 	if (!info->attrs[NL80211_ATTR_MAC] &&
16259 	    rdev->wiphy.hw_timestamp_max_peers != CFG80211_HW_TIMESTAMP_ALL_PEERS)
16260 		return -EOPNOTSUPP;
16261 
16262 	if (info->attrs[NL80211_ATTR_MAC])
16263 		hwts.macaddr = nla_data(info->attrs[NL80211_ATTR_MAC]);
16264 
16265 	hwts.enable =
16266 		nla_get_flag(info->attrs[NL80211_ATTR_HW_TIMESTAMP_ENABLED]);
16267 
16268 	return rdev_set_hw_timestamp(rdev, dev, &hwts);
16269 }
16270 
16271 static int
16272 nl80211_set_ttlm(struct sk_buff *skb, struct genl_info *info)
16273 {
16274 	struct cfg80211_ttlm_params params = {};
16275 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16276 	struct net_device *dev = info->user_ptr[1];
16277 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16278 
16279 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
16280 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
16281 		return -EOPNOTSUPP;
16282 
16283 	if (!wdev->connected)
16284 		return -ENOLINK;
16285 
16286 	if (!info->attrs[NL80211_ATTR_MLO_TTLM_DLINK] ||
16287 	    !info->attrs[NL80211_ATTR_MLO_TTLM_ULINK])
16288 		return -EINVAL;
16289 
16290 	nla_memcpy(params.dlink,
16291 		   info->attrs[NL80211_ATTR_MLO_TTLM_DLINK],
16292 		   sizeof(params.dlink));
16293 	nla_memcpy(params.ulink,
16294 		   info->attrs[NL80211_ATTR_MLO_TTLM_ULINK],
16295 		   sizeof(params.ulink));
16296 
16297 	return rdev_set_ttlm(rdev, dev, &params);
16298 }
16299 
16300 #define NL80211_FLAG_NEED_WIPHY		0x01
16301 #define NL80211_FLAG_NEED_NETDEV	0x02
16302 #define NL80211_FLAG_NEED_RTNL		0x04
16303 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
16304 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
16305 					 NL80211_FLAG_CHECK_NETDEV_UP)
16306 #define NL80211_FLAG_NEED_WDEV		0x10
16307 /* If a netdev is associated, it must be UP, P2P must be started */
16308 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
16309 					 NL80211_FLAG_CHECK_NETDEV_UP)
16310 #define NL80211_FLAG_CLEAR_SKB		0x20
16311 #define NL80211_FLAG_NO_WIPHY_MTX	0x40
16312 #define NL80211_FLAG_MLO_VALID_LINK_ID	0x80
16313 #define NL80211_FLAG_MLO_UNSUPPORTED	0x100
16314 
16315 #define INTERNAL_FLAG_SELECTORS(__sel)			\
16316 	SELECTOR(__sel, NONE, 0) /* must be first */	\
16317 	SELECTOR(__sel, WIPHY,				\
16318 		 NL80211_FLAG_NEED_WIPHY)		\
16319 	SELECTOR(__sel, WDEV,				\
16320 		 NL80211_FLAG_NEED_WDEV)		\
16321 	SELECTOR(__sel, NETDEV,				\
16322 		 NL80211_FLAG_NEED_NETDEV)		\
16323 	SELECTOR(__sel, NETDEV_LINK,			\
16324 		 NL80211_FLAG_NEED_NETDEV |		\
16325 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16326 	SELECTOR(__sel, NETDEV_NO_MLO,			\
16327 		 NL80211_FLAG_NEED_NETDEV |		\
16328 		 NL80211_FLAG_MLO_UNSUPPORTED)	\
16329 	SELECTOR(__sel, WIPHY_RTNL,			\
16330 		 NL80211_FLAG_NEED_WIPHY |		\
16331 		 NL80211_FLAG_NEED_RTNL)		\
16332 	SELECTOR(__sel, WIPHY_RTNL_NOMTX,		\
16333 		 NL80211_FLAG_NEED_WIPHY |		\
16334 		 NL80211_FLAG_NEED_RTNL |		\
16335 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16336 	SELECTOR(__sel, WDEV_RTNL,			\
16337 		 NL80211_FLAG_NEED_WDEV |		\
16338 		 NL80211_FLAG_NEED_RTNL)		\
16339 	SELECTOR(__sel, NETDEV_RTNL,			\
16340 		 NL80211_FLAG_NEED_NETDEV |		\
16341 		 NL80211_FLAG_NEED_RTNL)		\
16342 	SELECTOR(__sel, NETDEV_UP,			\
16343 		 NL80211_FLAG_NEED_NETDEV_UP)		\
16344 	SELECTOR(__sel, NETDEV_UP_LINK,			\
16345 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16346 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16347 	SELECTOR(__sel, NETDEV_UP_NO_MLO,		\
16348 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16349 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16350 	SELECTOR(__sel, NETDEV_UP_NO_MLO_CLEAR,		\
16351 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16352 		 NL80211_FLAG_CLEAR_SKB |		\
16353 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16354 	SELECTOR(__sel, NETDEV_UP_NOTMX,		\
16355 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16356 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16357 	SELECTOR(__sel, NETDEV_UP_NOTMX_NOMLO,		\
16358 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16359 		 NL80211_FLAG_NO_WIPHY_MTX |		\
16360 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16361 	SELECTOR(__sel, NETDEV_UP_CLEAR,		\
16362 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16363 		 NL80211_FLAG_CLEAR_SKB)		\
16364 	SELECTOR(__sel, WDEV_UP,			\
16365 		 NL80211_FLAG_NEED_WDEV_UP)		\
16366 	SELECTOR(__sel, WDEV_UP_LINK,			\
16367 		 NL80211_FLAG_NEED_WDEV_UP |		\
16368 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16369 	SELECTOR(__sel, WDEV_UP_RTNL,			\
16370 		 NL80211_FLAG_NEED_WDEV_UP |		\
16371 		 NL80211_FLAG_NEED_RTNL)		\
16372 	SELECTOR(__sel, WIPHY_CLEAR,			\
16373 		 NL80211_FLAG_NEED_WIPHY |		\
16374 		 NL80211_FLAG_CLEAR_SKB)
16375 
16376 enum nl80211_internal_flags_selector {
16377 #define SELECTOR(_, name, value)	NL80211_IFL_SEL_##name,
16378 	INTERNAL_FLAG_SELECTORS(_)
16379 #undef SELECTOR
16380 };
16381 
16382 static u32 nl80211_internal_flags[] = {
16383 #define SELECTOR(_, name, value)	[NL80211_IFL_SEL_##name] = value,
16384 	INTERNAL_FLAG_SELECTORS(_)
16385 #undef SELECTOR
16386 };
16387 
16388 static int nl80211_pre_doit(const struct genl_split_ops *ops,
16389 			    struct sk_buff *skb,
16390 			    struct genl_info *info)
16391 {
16392 	struct cfg80211_registered_device *rdev = NULL;
16393 	struct wireless_dev *wdev = NULL;
16394 	struct net_device *dev = NULL;
16395 	u32 internal_flags;
16396 	int err;
16397 
16398 	if (WARN_ON(ops->internal_flags >= ARRAY_SIZE(nl80211_internal_flags)))
16399 		return -EINVAL;
16400 
16401 	internal_flags = nl80211_internal_flags[ops->internal_flags];
16402 
16403 	rtnl_lock();
16404 	if (internal_flags & NL80211_FLAG_NEED_WIPHY) {
16405 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
16406 		if (IS_ERR(rdev)) {
16407 			err = PTR_ERR(rdev);
16408 			goto out_unlock;
16409 		}
16410 		info->user_ptr[0] = rdev;
16411 	} else if (internal_flags & NL80211_FLAG_NEED_NETDEV ||
16412 		   internal_flags & NL80211_FLAG_NEED_WDEV) {
16413 		wdev = __cfg80211_wdev_from_attrs(NULL, genl_info_net(info),
16414 						  info->attrs);
16415 		if (IS_ERR(wdev)) {
16416 			err = PTR_ERR(wdev);
16417 			goto out_unlock;
16418 		}
16419 
16420 		dev = wdev->netdev;
16421 		dev_hold(dev);
16422 		rdev = wiphy_to_rdev(wdev->wiphy);
16423 
16424 		if (internal_flags & NL80211_FLAG_NEED_NETDEV) {
16425 			if (!dev) {
16426 				err = -EINVAL;
16427 				goto out_unlock;
16428 			}
16429 
16430 			info->user_ptr[1] = dev;
16431 		} else {
16432 			info->user_ptr[1] = wdev;
16433 		}
16434 
16435 		if (internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
16436 		    !wdev_running(wdev)) {
16437 			err = -ENETDOWN;
16438 			goto out_unlock;
16439 		}
16440 
16441 		info->user_ptr[0] = rdev;
16442 	}
16443 
16444 	if (internal_flags & NL80211_FLAG_MLO_VALID_LINK_ID) {
16445 		struct nlattr *link_id = info->attrs[NL80211_ATTR_MLO_LINK_ID];
16446 
16447 		if (!wdev) {
16448 			err = -EINVAL;
16449 			goto out_unlock;
16450 		}
16451 
16452 		/* MLO -> require valid link ID */
16453 		if (wdev->valid_links &&
16454 		    (!link_id ||
16455 		     !(wdev->valid_links & BIT(nla_get_u8(link_id))))) {
16456 			err = -EINVAL;
16457 			goto out_unlock;
16458 		}
16459 
16460 		/* non-MLO -> no link ID attribute accepted */
16461 		if (!wdev->valid_links && link_id) {
16462 			err = -EINVAL;
16463 			goto out_unlock;
16464 		}
16465 	}
16466 
16467 	if (internal_flags & NL80211_FLAG_MLO_UNSUPPORTED) {
16468 		if (info->attrs[NL80211_ATTR_MLO_LINK_ID] ||
16469 		    (wdev && wdev->valid_links)) {
16470 			err = -EINVAL;
16471 			goto out_unlock;
16472 		}
16473 	}
16474 
16475 	if (rdev && !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16476 		wiphy_lock(&rdev->wiphy);
16477 		/* we keep the mutex locked until post_doit */
16478 		__release(&rdev->wiphy.mtx);
16479 	}
16480 	if (!(internal_flags & NL80211_FLAG_NEED_RTNL))
16481 		rtnl_unlock();
16482 
16483 	return 0;
16484 out_unlock:
16485 	rtnl_unlock();
16486 	dev_put(dev);
16487 	return err;
16488 }
16489 
16490 static void nl80211_post_doit(const struct genl_split_ops *ops,
16491 			      struct sk_buff *skb,
16492 			      struct genl_info *info)
16493 {
16494 	u32 internal_flags = nl80211_internal_flags[ops->internal_flags];
16495 
16496 	if (info->user_ptr[1]) {
16497 		if (internal_flags & NL80211_FLAG_NEED_WDEV) {
16498 			struct wireless_dev *wdev = info->user_ptr[1];
16499 
16500 			dev_put(wdev->netdev);
16501 		} else {
16502 			dev_put(info->user_ptr[1]);
16503 		}
16504 	}
16505 
16506 	if (info->user_ptr[0] &&
16507 	    !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16508 		struct cfg80211_registered_device *rdev = info->user_ptr[0];
16509 
16510 		/* we kept the mutex locked since pre_doit */
16511 		__acquire(&rdev->wiphy.mtx);
16512 		wiphy_unlock(&rdev->wiphy);
16513 	}
16514 
16515 	if (internal_flags & NL80211_FLAG_NEED_RTNL)
16516 		rtnl_unlock();
16517 
16518 	/* If needed, clear the netlink message payload from the SKB
16519 	 * as it might contain key data that shouldn't stick around on
16520 	 * the heap after the SKB is freed. The netlink message header
16521 	 * is still needed for further processing, so leave it intact.
16522 	 */
16523 	if (internal_flags & NL80211_FLAG_CLEAR_SKB) {
16524 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
16525 
16526 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
16527 	}
16528 }
16529 
16530 static int nl80211_set_sar_sub_specs(struct cfg80211_registered_device *rdev,
16531 				     struct cfg80211_sar_specs *sar_specs,
16532 				     struct nlattr *spec[], int index)
16533 {
16534 	u32 range_index, i;
16535 
16536 	if (!sar_specs || !spec)
16537 		return -EINVAL;
16538 
16539 	if (!spec[NL80211_SAR_ATTR_SPECS_POWER] ||
16540 	    !spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX])
16541 		return -EINVAL;
16542 
16543 	range_index = nla_get_u32(spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX]);
16544 
16545 	/* check if range_index exceeds num_freq_ranges */
16546 	if (range_index >= rdev->wiphy.sar_capa->num_freq_ranges)
16547 		return -EINVAL;
16548 
16549 	/* check if range_index duplicates */
16550 	for (i = 0; i < index; i++) {
16551 		if (sar_specs->sub_specs[i].freq_range_index == range_index)
16552 			return -EINVAL;
16553 	}
16554 
16555 	sar_specs->sub_specs[index].power =
16556 		nla_get_s32(spec[NL80211_SAR_ATTR_SPECS_POWER]);
16557 
16558 	sar_specs->sub_specs[index].freq_range_index = range_index;
16559 
16560 	return 0;
16561 }
16562 
16563 static int nl80211_set_sar_specs(struct sk_buff *skb, struct genl_info *info)
16564 {
16565 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16566 	struct nlattr *spec[NL80211_SAR_ATTR_SPECS_MAX + 1];
16567 	struct nlattr *tb[NL80211_SAR_ATTR_MAX + 1];
16568 	struct cfg80211_sar_specs *sar_spec;
16569 	enum nl80211_sar_type type;
16570 	struct nlattr *spec_list;
16571 	u32 specs;
16572 	int rem, err;
16573 
16574 	if (!rdev->wiphy.sar_capa || !rdev->ops->set_sar_specs)
16575 		return -EOPNOTSUPP;
16576 
16577 	if (!info->attrs[NL80211_ATTR_SAR_SPEC])
16578 		return -EINVAL;
16579 
16580 	nla_parse_nested(tb, NL80211_SAR_ATTR_MAX,
16581 			 info->attrs[NL80211_ATTR_SAR_SPEC],
16582 			 NULL, NULL);
16583 
16584 	if (!tb[NL80211_SAR_ATTR_TYPE] || !tb[NL80211_SAR_ATTR_SPECS])
16585 		return -EINVAL;
16586 
16587 	type = nla_get_u32(tb[NL80211_SAR_ATTR_TYPE]);
16588 	if (type != rdev->wiphy.sar_capa->type)
16589 		return -EINVAL;
16590 
16591 	specs = 0;
16592 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem)
16593 		specs++;
16594 
16595 	if (specs > rdev->wiphy.sar_capa->num_freq_ranges)
16596 		return -EINVAL;
16597 
16598 	sar_spec = kzalloc(struct_size(sar_spec, sub_specs, specs), GFP_KERNEL);
16599 	if (!sar_spec)
16600 		return -ENOMEM;
16601 
16602 	sar_spec->type = type;
16603 	specs = 0;
16604 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem) {
16605 		nla_parse_nested(spec, NL80211_SAR_ATTR_SPECS_MAX,
16606 				 spec_list, NULL, NULL);
16607 
16608 		switch (type) {
16609 		case NL80211_SAR_TYPE_POWER:
16610 			if (nl80211_set_sar_sub_specs(rdev, sar_spec,
16611 						      spec, specs)) {
16612 				err = -EINVAL;
16613 				goto error;
16614 			}
16615 			break;
16616 		default:
16617 			err = -EINVAL;
16618 			goto error;
16619 		}
16620 		specs++;
16621 	}
16622 
16623 	sar_spec->num_sub_specs = specs;
16624 
16625 	rdev->cur_cmd_info = info;
16626 	err = rdev_set_sar_specs(rdev, sar_spec);
16627 	rdev->cur_cmd_info = NULL;
16628 error:
16629 	kfree(sar_spec);
16630 	return err;
16631 }
16632 
16633 #define SELECTOR(__sel, name, value) \
16634 	((__sel) == (value)) ? NL80211_IFL_SEL_##name :
16635 int __missing_selector(void);
16636 #define IFLAGS(__val) INTERNAL_FLAG_SELECTORS(__val) __missing_selector()
16637 
16638 static const struct genl_ops nl80211_ops[] = {
16639 	{
16640 		.cmd = NL80211_CMD_GET_WIPHY,
16641 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16642 		.doit = nl80211_get_wiphy,
16643 		.dumpit = nl80211_dump_wiphy,
16644 		.done = nl80211_dump_wiphy_done,
16645 		/* can be retrieved by unprivileged users */
16646 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16647 	},
16648 };
16649 
16650 static const struct genl_small_ops nl80211_small_ops[] = {
16651 	{
16652 		.cmd = NL80211_CMD_SET_WIPHY,
16653 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16654 		.doit = nl80211_set_wiphy,
16655 		.flags = GENL_UNS_ADMIN_PERM,
16656 	},
16657 	{
16658 		.cmd = NL80211_CMD_GET_INTERFACE,
16659 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16660 		.doit = nl80211_get_interface,
16661 		.dumpit = nl80211_dump_interface,
16662 		/* can be retrieved by unprivileged users */
16663 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
16664 	},
16665 	{
16666 		.cmd = NL80211_CMD_SET_INTERFACE,
16667 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16668 		.doit = nl80211_set_interface,
16669 		.flags = GENL_UNS_ADMIN_PERM,
16670 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16671 					 NL80211_FLAG_NEED_RTNL),
16672 	},
16673 	{
16674 		.cmd = NL80211_CMD_NEW_INTERFACE,
16675 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16676 		.doit = nl80211_new_interface,
16677 		.flags = GENL_UNS_ADMIN_PERM,
16678 		.internal_flags =
16679 			IFLAGS(NL80211_FLAG_NEED_WIPHY |
16680 			       NL80211_FLAG_NEED_RTNL |
16681 			       /* we take the wiphy mutex later ourselves */
16682 			       NL80211_FLAG_NO_WIPHY_MTX),
16683 	},
16684 	{
16685 		.cmd = NL80211_CMD_DEL_INTERFACE,
16686 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16687 		.doit = nl80211_del_interface,
16688 		.flags = GENL_UNS_ADMIN_PERM,
16689 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
16690 					 NL80211_FLAG_NEED_RTNL),
16691 	},
16692 	{
16693 		.cmd = NL80211_CMD_GET_KEY,
16694 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16695 		.doit = nl80211_get_key,
16696 		.flags = GENL_UNS_ADMIN_PERM,
16697 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16698 	},
16699 	{
16700 		.cmd = NL80211_CMD_SET_KEY,
16701 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16702 		.doit = nl80211_set_key,
16703 		.flags = GENL_UNS_ADMIN_PERM,
16704 		/* cannot use NL80211_FLAG_MLO_VALID_LINK_ID, depends on key */
16705 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16706 					 NL80211_FLAG_CLEAR_SKB),
16707 	},
16708 	{
16709 		.cmd = NL80211_CMD_NEW_KEY,
16710 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16711 		.doit = nl80211_new_key,
16712 		.flags = GENL_UNS_ADMIN_PERM,
16713 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16714 					 NL80211_FLAG_CLEAR_SKB),
16715 	},
16716 	{
16717 		.cmd = NL80211_CMD_DEL_KEY,
16718 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16719 		.doit = nl80211_del_key,
16720 		.flags = GENL_UNS_ADMIN_PERM,
16721 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16722 	},
16723 	{
16724 		.cmd = NL80211_CMD_SET_BEACON,
16725 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16726 		.flags = GENL_UNS_ADMIN_PERM,
16727 		.doit = nl80211_set_beacon,
16728 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16729 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16730 	},
16731 	{
16732 		.cmd = NL80211_CMD_START_AP,
16733 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16734 		.flags = GENL_UNS_ADMIN_PERM,
16735 		.doit = nl80211_start_ap,
16736 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16737 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16738 	},
16739 	{
16740 		.cmd = NL80211_CMD_STOP_AP,
16741 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16742 		.flags = GENL_UNS_ADMIN_PERM,
16743 		.doit = nl80211_stop_ap,
16744 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16745 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16746 	},
16747 	{
16748 		.cmd = NL80211_CMD_GET_STATION,
16749 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16750 		.doit = nl80211_get_station,
16751 		.dumpit = nl80211_dump_station,
16752 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16753 	},
16754 	{
16755 		.cmd = NL80211_CMD_SET_STATION,
16756 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16757 		.doit = nl80211_set_station,
16758 		.flags = GENL_UNS_ADMIN_PERM,
16759 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16760 	},
16761 	{
16762 		.cmd = NL80211_CMD_NEW_STATION,
16763 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16764 		.doit = nl80211_new_station,
16765 		.flags = GENL_UNS_ADMIN_PERM,
16766 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16767 	},
16768 	{
16769 		.cmd = NL80211_CMD_DEL_STATION,
16770 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16771 		.doit = nl80211_del_station,
16772 		.flags = GENL_UNS_ADMIN_PERM,
16773 		/* cannot use NL80211_FLAG_MLO_VALID_LINK_ID, depends on
16774 		 * whether MAC address is passed or not. If MAC address is
16775 		 * passed, then even during MLO, link ID is not required.
16776 		 */
16777 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16778 	},
16779 	{
16780 		.cmd = NL80211_CMD_GET_MPATH,
16781 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16782 		.doit = nl80211_get_mpath,
16783 		.dumpit = nl80211_dump_mpath,
16784 		.flags = GENL_UNS_ADMIN_PERM,
16785 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16786 	},
16787 	{
16788 		.cmd = NL80211_CMD_GET_MPP,
16789 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16790 		.doit = nl80211_get_mpp,
16791 		.dumpit = nl80211_dump_mpp,
16792 		.flags = GENL_UNS_ADMIN_PERM,
16793 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16794 	},
16795 	{
16796 		.cmd = NL80211_CMD_SET_MPATH,
16797 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16798 		.doit = nl80211_set_mpath,
16799 		.flags = GENL_UNS_ADMIN_PERM,
16800 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16801 	},
16802 	{
16803 		.cmd = NL80211_CMD_NEW_MPATH,
16804 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16805 		.doit = nl80211_new_mpath,
16806 		.flags = GENL_UNS_ADMIN_PERM,
16807 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16808 	},
16809 	{
16810 		.cmd = NL80211_CMD_DEL_MPATH,
16811 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16812 		.doit = nl80211_del_mpath,
16813 		.flags = GENL_UNS_ADMIN_PERM,
16814 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16815 	},
16816 	{
16817 		.cmd = NL80211_CMD_SET_BSS,
16818 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16819 		.doit = nl80211_set_bss,
16820 		.flags = GENL_UNS_ADMIN_PERM,
16821 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16822 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16823 	},
16824 	{
16825 		.cmd = NL80211_CMD_GET_REG,
16826 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16827 		.doit = nl80211_get_reg_do,
16828 		.dumpit = nl80211_get_reg_dump,
16829 		/* can be retrieved by unprivileged users */
16830 	},
16831 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
16832 	{
16833 		.cmd = NL80211_CMD_SET_REG,
16834 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16835 		.doit = nl80211_set_reg,
16836 		.flags = GENL_ADMIN_PERM,
16837 	},
16838 #endif
16839 	{
16840 		.cmd = NL80211_CMD_REQ_SET_REG,
16841 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16842 		.doit = nl80211_req_set_reg,
16843 		.flags = GENL_ADMIN_PERM,
16844 	},
16845 	{
16846 		.cmd = NL80211_CMD_RELOAD_REGDB,
16847 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16848 		.doit = nl80211_reload_regdb,
16849 		.flags = GENL_ADMIN_PERM,
16850 	},
16851 	{
16852 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
16853 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16854 		.doit = nl80211_get_mesh_config,
16855 		/* can be retrieved by unprivileged users */
16856 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16857 	},
16858 	{
16859 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
16860 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16861 		.doit = nl80211_update_mesh_config,
16862 		.flags = GENL_UNS_ADMIN_PERM,
16863 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16864 	},
16865 	{
16866 		.cmd = NL80211_CMD_TRIGGER_SCAN,
16867 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16868 		.doit = nl80211_trigger_scan,
16869 		.flags = GENL_UNS_ADMIN_PERM,
16870 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16871 	},
16872 	{
16873 		.cmd = NL80211_CMD_ABORT_SCAN,
16874 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16875 		.doit = nl80211_abort_scan,
16876 		.flags = GENL_UNS_ADMIN_PERM,
16877 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16878 	},
16879 	{
16880 		.cmd = NL80211_CMD_GET_SCAN,
16881 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16882 		.dumpit = nl80211_dump_scan,
16883 	},
16884 	{
16885 		.cmd = NL80211_CMD_START_SCHED_SCAN,
16886 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16887 		.doit = nl80211_start_sched_scan,
16888 		.flags = GENL_UNS_ADMIN_PERM,
16889 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16890 	},
16891 	{
16892 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
16893 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16894 		.doit = nl80211_stop_sched_scan,
16895 		.flags = GENL_UNS_ADMIN_PERM,
16896 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16897 	},
16898 	{
16899 		.cmd = NL80211_CMD_AUTHENTICATE,
16900 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16901 		.doit = nl80211_authenticate,
16902 		.flags = GENL_UNS_ADMIN_PERM,
16903 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16904 					 NL80211_FLAG_CLEAR_SKB),
16905 	},
16906 	{
16907 		.cmd = NL80211_CMD_ASSOCIATE,
16908 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16909 		.doit = nl80211_associate,
16910 		.flags = GENL_UNS_ADMIN_PERM,
16911 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16912 					 NL80211_FLAG_CLEAR_SKB),
16913 	},
16914 	{
16915 		.cmd = NL80211_CMD_DEAUTHENTICATE,
16916 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16917 		.doit = nl80211_deauthenticate,
16918 		.flags = GENL_UNS_ADMIN_PERM,
16919 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16920 	},
16921 	{
16922 		.cmd = NL80211_CMD_DISASSOCIATE,
16923 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16924 		.doit = nl80211_disassociate,
16925 		.flags = GENL_UNS_ADMIN_PERM,
16926 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16927 	},
16928 	{
16929 		.cmd = NL80211_CMD_JOIN_IBSS,
16930 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16931 		.doit = nl80211_join_ibss,
16932 		.flags = GENL_UNS_ADMIN_PERM,
16933 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16934 	},
16935 	{
16936 		.cmd = NL80211_CMD_LEAVE_IBSS,
16937 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16938 		.doit = nl80211_leave_ibss,
16939 		.flags = GENL_UNS_ADMIN_PERM,
16940 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16941 	},
16942 #ifdef CONFIG_NL80211_TESTMODE
16943 	{
16944 		.cmd = NL80211_CMD_TESTMODE,
16945 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16946 		.doit = nl80211_testmode_do,
16947 		.dumpit = nl80211_testmode_dump,
16948 		.flags = GENL_UNS_ADMIN_PERM,
16949 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16950 	},
16951 #endif
16952 	{
16953 		.cmd = NL80211_CMD_CONNECT,
16954 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16955 		.doit = nl80211_connect,
16956 		.flags = GENL_UNS_ADMIN_PERM,
16957 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16958 					 NL80211_FLAG_CLEAR_SKB),
16959 	},
16960 	{
16961 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
16962 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16963 		.doit = nl80211_update_connect_params,
16964 		.flags = GENL_ADMIN_PERM,
16965 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16966 					 NL80211_FLAG_CLEAR_SKB),
16967 	},
16968 	{
16969 		.cmd = NL80211_CMD_DISCONNECT,
16970 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16971 		.doit = nl80211_disconnect,
16972 		.flags = GENL_UNS_ADMIN_PERM,
16973 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16974 	},
16975 	{
16976 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
16977 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16978 		.doit = nl80211_wiphy_netns,
16979 		.flags = GENL_UNS_ADMIN_PERM,
16980 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
16981 					 NL80211_FLAG_NEED_RTNL |
16982 					 NL80211_FLAG_NO_WIPHY_MTX),
16983 	},
16984 	{
16985 		.cmd = NL80211_CMD_GET_SURVEY,
16986 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16987 		.dumpit = nl80211_dump_survey,
16988 	},
16989 	{
16990 		.cmd = NL80211_CMD_SET_PMKSA,
16991 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16992 		.doit = nl80211_set_pmksa,
16993 		.flags = GENL_UNS_ADMIN_PERM,
16994 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16995 					 NL80211_FLAG_CLEAR_SKB),
16996 	},
16997 	{
16998 		.cmd = NL80211_CMD_DEL_PMKSA,
16999 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17000 		.doit = nl80211_del_pmksa,
17001 		.flags = GENL_UNS_ADMIN_PERM,
17002 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17003 	},
17004 	{
17005 		.cmd = NL80211_CMD_FLUSH_PMKSA,
17006 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17007 		.doit = nl80211_flush_pmksa,
17008 		.flags = GENL_UNS_ADMIN_PERM,
17009 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17010 	},
17011 	{
17012 		.cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
17013 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17014 		.doit = nl80211_remain_on_channel,
17015 		.flags = GENL_UNS_ADMIN_PERM,
17016 		/* FIXME: requiring a link ID here is probably not good */
17017 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17018 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17019 	},
17020 	{
17021 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
17022 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17023 		.doit = nl80211_cancel_remain_on_channel,
17024 		.flags = GENL_UNS_ADMIN_PERM,
17025 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17026 	},
17027 	{
17028 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
17029 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17030 		.doit = nl80211_set_tx_bitrate_mask,
17031 		.flags = GENL_UNS_ADMIN_PERM,
17032 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17033 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17034 	},
17035 	{
17036 		.cmd = NL80211_CMD_REGISTER_FRAME,
17037 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17038 		.doit = nl80211_register_mgmt,
17039 		.flags = GENL_UNS_ADMIN_PERM,
17040 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
17041 	},
17042 	{
17043 		.cmd = NL80211_CMD_FRAME,
17044 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17045 		.doit = nl80211_tx_mgmt,
17046 		.flags = GENL_UNS_ADMIN_PERM,
17047 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17048 	},
17049 	{
17050 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
17051 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17052 		.doit = nl80211_tx_mgmt_cancel_wait,
17053 		.flags = GENL_UNS_ADMIN_PERM,
17054 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17055 	},
17056 	{
17057 		.cmd = NL80211_CMD_SET_POWER_SAVE,
17058 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17059 		.doit = nl80211_set_power_save,
17060 		.flags = GENL_UNS_ADMIN_PERM,
17061 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17062 	},
17063 	{
17064 		.cmd = NL80211_CMD_GET_POWER_SAVE,
17065 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17066 		.doit = nl80211_get_power_save,
17067 		/* can be retrieved by unprivileged users */
17068 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17069 	},
17070 	{
17071 		.cmd = NL80211_CMD_SET_CQM,
17072 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17073 		.doit = nl80211_set_cqm,
17074 		.flags = GENL_UNS_ADMIN_PERM,
17075 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17076 	},
17077 	{
17078 		.cmd = NL80211_CMD_SET_CHANNEL,
17079 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17080 		.doit = nl80211_set_channel,
17081 		.flags = GENL_UNS_ADMIN_PERM,
17082 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17083 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17084 	},
17085 	{
17086 		.cmd = NL80211_CMD_JOIN_MESH,
17087 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17088 		.doit = nl80211_join_mesh,
17089 		.flags = GENL_UNS_ADMIN_PERM,
17090 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17091 	},
17092 	{
17093 		.cmd = NL80211_CMD_LEAVE_MESH,
17094 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17095 		.doit = nl80211_leave_mesh,
17096 		.flags = GENL_UNS_ADMIN_PERM,
17097 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17098 	},
17099 	{
17100 		.cmd = NL80211_CMD_JOIN_OCB,
17101 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17102 		.doit = nl80211_join_ocb,
17103 		.flags = GENL_UNS_ADMIN_PERM,
17104 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17105 	},
17106 	{
17107 		.cmd = NL80211_CMD_LEAVE_OCB,
17108 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17109 		.doit = nl80211_leave_ocb,
17110 		.flags = GENL_UNS_ADMIN_PERM,
17111 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17112 	},
17113 #ifdef CONFIG_PM
17114 	{
17115 		.cmd = NL80211_CMD_GET_WOWLAN,
17116 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17117 		.doit = nl80211_get_wowlan,
17118 		/* can be retrieved by unprivileged users */
17119 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17120 	},
17121 	{
17122 		.cmd = NL80211_CMD_SET_WOWLAN,
17123 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17124 		.doit = nl80211_set_wowlan,
17125 		.flags = GENL_UNS_ADMIN_PERM,
17126 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17127 	},
17128 #endif
17129 	{
17130 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
17131 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17132 		.doit = nl80211_set_rekey_data,
17133 		.flags = GENL_UNS_ADMIN_PERM,
17134 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17135 					 NL80211_FLAG_CLEAR_SKB),
17136 	},
17137 	{
17138 		.cmd = NL80211_CMD_TDLS_MGMT,
17139 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17140 		.doit = nl80211_tdls_mgmt,
17141 		.flags = GENL_UNS_ADMIN_PERM,
17142 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17143 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17144 	},
17145 	{
17146 		.cmd = NL80211_CMD_TDLS_OPER,
17147 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17148 		.doit = nl80211_tdls_oper,
17149 		.flags = GENL_UNS_ADMIN_PERM,
17150 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17151 	},
17152 	{
17153 		.cmd = NL80211_CMD_UNEXPECTED_FRAME,
17154 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17155 		.doit = nl80211_register_unexpected_frame,
17156 		.flags = GENL_UNS_ADMIN_PERM,
17157 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17158 	},
17159 	{
17160 		.cmd = NL80211_CMD_PROBE_CLIENT,
17161 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17162 		.doit = nl80211_probe_client,
17163 		.flags = GENL_UNS_ADMIN_PERM,
17164 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17165 	},
17166 	{
17167 		.cmd = NL80211_CMD_REGISTER_BEACONS,
17168 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17169 		.doit = nl80211_register_beacons,
17170 		.flags = GENL_UNS_ADMIN_PERM,
17171 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17172 	},
17173 	{
17174 		.cmd = NL80211_CMD_SET_NOACK_MAP,
17175 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17176 		.doit = nl80211_set_noack_map,
17177 		.flags = GENL_UNS_ADMIN_PERM,
17178 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17179 	},
17180 	{
17181 		.cmd = NL80211_CMD_START_P2P_DEVICE,
17182 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17183 		.doit = nl80211_start_p2p_device,
17184 		.flags = GENL_UNS_ADMIN_PERM,
17185 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
17186 					 NL80211_FLAG_NEED_RTNL),
17187 	},
17188 	{
17189 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
17190 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17191 		.doit = nl80211_stop_p2p_device,
17192 		.flags = GENL_UNS_ADMIN_PERM,
17193 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17194 					 NL80211_FLAG_NEED_RTNL),
17195 	},
17196 	{
17197 		.cmd = NL80211_CMD_START_NAN,
17198 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17199 		.doit = nl80211_start_nan,
17200 		.flags = GENL_ADMIN_PERM,
17201 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
17202 					 NL80211_FLAG_NEED_RTNL),
17203 	},
17204 	{
17205 		.cmd = NL80211_CMD_STOP_NAN,
17206 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17207 		.doit = nl80211_stop_nan,
17208 		.flags = GENL_ADMIN_PERM,
17209 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17210 					 NL80211_FLAG_NEED_RTNL),
17211 	},
17212 	{
17213 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
17214 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17215 		.doit = nl80211_nan_add_func,
17216 		.flags = GENL_ADMIN_PERM,
17217 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17218 	},
17219 	{
17220 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
17221 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17222 		.doit = nl80211_nan_del_func,
17223 		.flags = GENL_ADMIN_PERM,
17224 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17225 	},
17226 	{
17227 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
17228 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17229 		.doit = nl80211_nan_change_config,
17230 		.flags = GENL_ADMIN_PERM,
17231 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17232 	},
17233 	{
17234 		.cmd = NL80211_CMD_SET_MCAST_RATE,
17235 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17236 		.doit = nl80211_set_mcast_rate,
17237 		.flags = GENL_UNS_ADMIN_PERM,
17238 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17239 	},
17240 	{
17241 		.cmd = NL80211_CMD_SET_MAC_ACL,
17242 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17243 		.doit = nl80211_set_mac_acl,
17244 		.flags = GENL_UNS_ADMIN_PERM,
17245 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17246 					 NL80211_FLAG_MLO_UNSUPPORTED),
17247 	},
17248 	{
17249 		.cmd = NL80211_CMD_RADAR_DETECT,
17250 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17251 		.doit = nl80211_start_radar_detection,
17252 		.flags = GENL_UNS_ADMIN_PERM,
17253 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17254 					 NL80211_FLAG_NO_WIPHY_MTX |
17255 					 NL80211_FLAG_MLO_UNSUPPORTED),
17256 	},
17257 	{
17258 		.cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
17259 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17260 		.doit = nl80211_get_protocol_features,
17261 	},
17262 	{
17263 		.cmd = NL80211_CMD_UPDATE_FT_IES,
17264 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17265 		.doit = nl80211_update_ft_ies,
17266 		.flags = GENL_UNS_ADMIN_PERM,
17267 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17268 	},
17269 	{
17270 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
17271 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17272 		.doit = nl80211_crit_protocol_start,
17273 		.flags = GENL_UNS_ADMIN_PERM,
17274 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17275 	},
17276 	{
17277 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
17278 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17279 		.doit = nl80211_crit_protocol_stop,
17280 		.flags = GENL_UNS_ADMIN_PERM,
17281 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17282 	},
17283 	{
17284 		.cmd = NL80211_CMD_GET_COALESCE,
17285 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17286 		.doit = nl80211_get_coalesce,
17287 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17288 	},
17289 	{
17290 		.cmd = NL80211_CMD_SET_COALESCE,
17291 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17292 		.doit = nl80211_set_coalesce,
17293 		.flags = GENL_UNS_ADMIN_PERM,
17294 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17295 	},
17296 	{
17297 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
17298 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17299 		.doit = nl80211_channel_switch,
17300 		.flags = GENL_UNS_ADMIN_PERM,
17301 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17302 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17303 	},
17304 	{
17305 		.cmd = NL80211_CMD_VENDOR,
17306 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17307 		.doit = nl80211_vendor_cmd,
17308 		.dumpit = nl80211_vendor_cmd_dump,
17309 		.flags = GENL_UNS_ADMIN_PERM,
17310 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17311 					 NL80211_FLAG_CLEAR_SKB),
17312 	},
17313 	{
17314 		.cmd = NL80211_CMD_SET_QOS_MAP,
17315 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17316 		.doit = nl80211_set_qos_map,
17317 		.flags = GENL_UNS_ADMIN_PERM,
17318 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17319 	},
17320 	{
17321 		.cmd = NL80211_CMD_ADD_TX_TS,
17322 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17323 		.doit = nl80211_add_tx_ts,
17324 		.flags = GENL_UNS_ADMIN_PERM,
17325 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17326 					 NL80211_FLAG_MLO_UNSUPPORTED),
17327 	},
17328 	{
17329 		.cmd = NL80211_CMD_DEL_TX_TS,
17330 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17331 		.doit = nl80211_del_tx_ts,
17332 		.flags = GENL_UNS_ADMIN_PERM,
17333 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17334 	},
17335 	{
17336 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
17337 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17338 		.doit = nl80211_tdls_channel_switch,
17339 		.flags = GENL_UNS_ADMIN_PERM,
17340 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17341 	},
17342 	{
17343 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
17344 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17345 		.doit = nl80211_tdls_cancel_channel_switch,
17346 		.flags = GENL_UNS_ADMIN_PERM,
17347 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17348 	},
17349 	{
17350 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
17351 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17352 		.doit = nl80211_set_multicast_to_unicast,
17353 		.flags = GENL_UNS_ADMIN_PERM,
17354 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17355 	},
17356 	{
17357 		.cmd = NL80211_CMD_SET_PMK,
17358 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17359 		.doit = nl80211_set_pmk,
17360 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17361 					 NL80211_FLAG_CLEAR_SKB),
17362 	},
17363 	{
17364 		.cmd = NL80211_CMD_DEL_PMK,
17365 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17366 		.doit = nl80211_del_pmk,
17367 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17368 	},
17369 	{
17370 		.cmd = NL80211_CMD_EXTERNAL_AUTH,
17371 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17372 		.doit = nl80211_external_auth,
17373 		.flags = GENL_ADMIN_PERM,
17374 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17375 	},
17376 	{
17377 		.cmd = NL80211_CMD_CONTROL_PORT_FRAME,
17378 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17379 		.doit = nl80211_tx_control_port,
17380 		.flags = GENL_UNS_ADMIN_PERM,
17381 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17382 	},
17383 	{
17384 		.cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
17385 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17386 		.doit = nl80211_get_ftm_responder_stats,
17387 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17388 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17389 	},
17390 	{
17391 		.cmd = NL80211_CMD_PEER_MEASUREMENT_START,
17392 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17393 		.doit = nl80211_pmsr_start,
17394 		.flags = GENL_UNS_ADMIN_PERM,
17395 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17396 	},
17397 	{
17398 		.cmd = NL80211_CMD_NOTIFY_RADAR,
17399 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17400 		.doit = nl80211_notify_radar_detection,
17401 		.flags = GENL_UNS_ADMIN_PERM,
17402 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17403 	},
17404 	{
17405 		.cmd = NL80211_CMD_UPDATE_OWE_INFO,
17406 		.doit = nl80211_update_owe_info,
17407 		.flags = GENL_ADMIN_PERM,
17408 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17409 	},
17410 	{
17411 		.cmd = NL80211_CMD_PROBE_MESH_LINK,
17412 		.doit = nl80211_probe_mesh_link,
17413 		.flags = GENL_UNS_ADMIN_PERM,
17414 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17415 	},
17416 	{
17417 		.cmd = NL80211_CMD_SET_TID_CONFIG,
17418 		.doit = nl80211_set_tid_config,
17419 		.flags = GENL_UNS_ADMIN_PERM,
17420 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17421 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17422 	},
17423 	{
17424 		.cmd = NL80211_CMD_SET_SAR_SPECS,
17425 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17426 		.doit = nl80211_set_sar_specs,
17427 		.flags = GENL_UNS_ADMIN_PERM,
17428 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17429 					 NL80211_FLAG_NEED_RTNL),
17430 	},
17431 	{
17432 		.cmd = NL80211_CMD_COLOR_CHANGE_REQUEST,
17433 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17434 		.doit = nl80211_color_change,
17435 		.flags = GENL_UNS_ADMIN_PERM,
17436 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17437 	},
17438 	{
17439 		.cmd = NL80211_CMD_SET_FILS_AAD,
17440 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17441 		.doit = nl80211_set_fils_aad,
17442 		.flags = GENL_UNS_ADMIN_PERM,
17443 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17444 	},
17445 	{
17446 		.cmd = NL80211_CMD_ADD_LINK,
17447 		.doit = nl80211_add_link,
17448 		.flags = GENL_UNS_ADMIN_PERM,
17449 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17450 	},
17451 	{
17452 		.cmd = NL80211_CMD_REMOVE_LINK,
17453 		.doit = nl80211_remove_link,
17454 		.flags = GENL_UNS_ADMIN_PERM,
17455 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17456 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17457 	},
17458 	{
17459 		.cmd = NL80211_CMD_ADD_LINK_STA,
17460 		.doit = nl80211_add_link_station,
17461 		.flags = GENL_UNS_ADMIN_PERM,
17462 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17463 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17464 	},
17465 	{
17466 		.cmd = NL80211_CMD_MODIFY_LINK_STA,
17467 		.doit = nl80211_modify_link_station,
17468 		.flags = GENL_UNS_ADMIN_PERM,
17469 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17470 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17471 	},
17472 	{
17473 		.cmd = NL80211_CMD_REMOVE_LINK_STA,
17474 		.doit = nl80211_remove_link_station,
17475 		.flags = GENL_UNS_ADMIN_PERM,
17476 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17477 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17478 	},
17479 	{
17480 		.cmd = NL80211_CMD_SET_HW_TIMESTAMP,
17481 		.doit = nl80211_set_hw_timestamp,
17482 		.flags = GENL_UNS_ADMIN_PERM,
17483 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17484 	},
17485 	{
17486 		.cmd = NL80211_CMD_SET_TID_TO_LINK_MAPPING,
17487 		.doit = nl80211_set_ttlm,
17488 		.flags = GENL_UNS_ADMIN_PERM,
17489 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17490 	},
17491 };
17492 
17493 static struct genl_family nl80211_fam __ro_after_init = {
17494 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
17495 	.hdrsize = 0,			/* no private header */
17496 	.version = 1,			/* no particular meaning now */
17497 	.maxattr = NL80211_ATTR_MAX,
17498 	.policy = nl80211_policy,
17499 	.netnsok = true,
17500 	.pre_doit = nl80211_pre_doit,
17501 	.post_doit = nl80211_post_doit,
17502 	.module = THIS_MODULE,
17503 	.ops = nl80211_ops,
17504 	.n_ops = ARRAY_SIZE(nl80211_ops),
17505 	.small_ops = nl80211_small_ops,
17506 	.n_small_ops = ARRAY_SIZE(nl80211_small_ops),
17507 	.resv_start_op = NL80211_CMD_REMOVE_LINK_STA + 1,
17508 	.mcgrps = nl80211_mcgrps,
17509 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
17510 	.parallel_ops = true,
17511 };
17512 
17513 /* notification functions */
17514 
17515 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
17516 			  enum nl80211_commands cmd)
17517 {
17518 	struct sk_buff *msg;
17519 	struct nl80211_dump_wiphy_state state = {};
17520 
17521 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
17522 		cmd != NL80211_CMD_DEL_WIPHY);
17523 
17524 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17525 	if (!msg)
17526 		return;
17527 
17528 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
17529 		nlmsg_free(msg);
17530 		return;
17531 	}
17532 
17533 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17534 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17535 }
17536 
17537 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
17538 				struct wireless_dev *wdev,
17539 				enum nl80211_commands cmd)
17540 {
17541 	struct sk_buff *msg;
17542 
17543 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17544 	if (!msg)
17545 		return;
17546 
17547 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
17548 		nlmsg_free(msg);
17549 		return;
17550 	}
17551 
17552 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17553 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17554 }
17555 
17556 static int nl80211_add_scan_req(struct sk_buff *msg,
17557 				struct cfg80211_registered_device *rdev)
17558 {
17559 	struct cfg80211_scan_request *req = rdev->scan_req;
17560 	struct nlattr *nest;
17561 	int i;
17562 	struct cfg80211_scan_info *info;
17563 
17564 	if (WARN_ON(!req))
17565 		return 0;
17566 
17567 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
17568 	if (!nest)
17569 		goto nla_put_failure;
17570 	for (i = 0; i < req->n_ssids; i++) {
17571 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
17572 			goto nla_put_failure;
17573 	}
17574 	nla_nest_end(msg, nest);
17575 
17576 	if (req->flags & NL80211_SCAN_FLAG_FREQ_KHZ) {
17577 		nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQ_KHZ);
17578 		if (!nest)
17579 			goto nla_put_failure;
17580 		for (i = 0; i < req->n_channels; i++) {
17581 			if (nla_put_u32(msg, i,
17582 				   ieee80211_channel_to_khz(req->channels[i])))
17583 				goto nla_put_failure;
17584 		}
17585 		nla_nest_end(msg, nest);
17586 	} else {
17587 		nest = nla_nest_start_noflag(msg,
17588 					     NL80211_ATTR_SCAN_FREQUENCIES);
17589 		if (!nest)
17590 			goto nla_put_failure;
17591 		for (i = 0; i < req->n_channels; i++) {
17592 			if (nla_put_u32(msg, i, req->channels[i]->center_freq))
17593 				goto nla_put_failure;
17594 		}
17595 		nla_nest_end(msg, nest);
17596 	}
17597 
17598 	if (req->ie &&
17599 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
17600 		goto nla_put_failure;
17601 
17602 	if (req->flags &&
17603 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
17604 		goto nla_put_failure;
17605 
17606 	info = rdev->int_scan_req ? &rdev->int_scan_req->info :
17607 		&rdev->scan_req->info;
17608 	if (info->scan_start_tsf &&
17609 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
17610 			       info->scan_start_tsf, NL80211_BSS_PAD) ||
17611 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
17612 		     info->tsf_bssid)))
17613 		goto nla_put_failure;
17614 
17615 	return 0;
17616  nla_put_failure:
17617 	return -ENOBUFS;
17618 }
17619 
17620 static int nl80211_prep_scan_msg(struct sk_buff *msg,
17621 				 struct cfg80211_registered_device *rdev,
17622 				 struct wireless_dev *wdev,
17623 				 u32 portid, u32 seq, int flags,
17624 				 u32 cmd)
17625 {
17626 	void *hdr;
17627 
17628 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
17629 	if (!hdr)
17630 		return -1;
17631 
17632 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17633 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
17634 					 wdev->netdev->ifindex)) ||
17635 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17636 			      NL80211_ATTR_PAD))
17637 		goto nla_put_failure;
17638 
17639 	/* ignore errors and send incomplete event anyway */
17640 	nl80211_add_scan_req(msg, rdev);
17641 
17642 	genlmsg_end(msg, hdr);
17643 	return 0;
17644 
17645  nla_put_failure:
17646 	genlmsg_cancel(msg, hdr);
17647 	return -EMSGSIZE;
17648 }
17649 
17650 static int
17651 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
17652 			    struct cfg80211_sched_scan_request *req, u32 cmd)
17653 {
17654 	void *hdr;
17655 
17656 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17657 	if (!hdr)
17658 		return -1;
17659 
17660 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
17661 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
17662 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
17663 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
17664 			      NL80211_ATTR_PAD))
17665 		goto nla_put_failure;
17666 
17667 	genlmsg_end(msg, hdr);
17668 	return 0;
17669 
17670  nla_put_failure:
17671 	genlmsg_cancel(msg, hdr);
17672 	return -EMSGSIZE;
17673 }
17674 
17675 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
17676 			     struct wireless_dev *wdev)
17677 {
17678 	struct sk_buff *msg;
17679 
17680 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17681 	if (!msg)
17682 		return;
17683 
17684 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17685 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
17686 		nlmsg_free(msg);
17687 		return;
17688 	}
17689 
17690 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17691 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17692 }
17693 
17694 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
17695 				       struct wireless_dev *wdev, bool aborted)
17696 {
17697 	struct sk_buff *msg;
17698 
17699 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17700 	if (!msg)
17701 		return NULL;
17702 
17703 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17704 				  aborted ? NL80211_CMD_SCAN_ABORTED :
17705 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
17706 		nlmsg_free(msg);
17707 		return NULL;
17708 	}
17709 
17710 	return msg;
17711 }
17712 
17713 /* send message created by nl80211_build_scan_msg() */
17714 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
17715 			   struct sk_buff *msg)
17716 {
17717 	if (!msg)
17718 		return;
17719 
17720 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17721 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17722 }
17723 
17724 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
17725 {
17726 	struct sk_buff *msg;
17727 
17728 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17729 	if (!msg)
17730 		return;
17731 
17732 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
17733 		nlmsg_free(msg);
17734 		return;
17735 	}
17736 
17737 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
17738 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17739 }
17740 
17741 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
17742 					  struct regulatory_request *request)
17743 {
17744 	/* Userspace can always count this one always being set */
17745 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
17746 		goto nla_put_failure;
17747 
17748 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
17749 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17750 			       NL80211_REGDOM_TYPE_WORLD))
17751 			goto nla_put_failure;
17752 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
17753 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17754 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
17755 			goto nla_put_failure;
17756 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
17757 		   request->intersect) {
17758 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17759 			       NL80211_REGDOM_TYPE_INTERSECTION))
17760 			goto nla_put_failure;
17761 	} else {
17762 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17763 			       NL80211_REGDOM_TYPE_COUNTRY) ||
17764 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
17765 				   request->alpha2))
17766 			goto nla_put_failure;
17767 	}
17768 
17769 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
17770 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
17771 
17772 		if (wiphy &&
17773 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
17774 			goto nla_put_failure;
17775 
17776 		if (wiphy &&
17777 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
17778 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
17779 			goto nla_put_failure;
17780 	}
17781 
17782 	return true;
17783 
17784 nla_put_failure:
17785 	return false;
17786 }
17787 
17788 /*
17789  * This can happen on global regulatory changes or device specific settings
17790  * based on custom regulatory domains.
17791  */
17792 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
17793 				     struct regulatory_request *request)
17794 {
17795 	struct sk_buff *msg;
17796 	void *hdr;
17797 
17798 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17799 	if (!msg)
17800 		return;
17801 
17802 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
17803 	if (!hdr)
17804 		goto nla_put_failure;
17805 
17806 	if (!nl80211_reg_change_event_fill(msg, request))
17807 		goto nla_put_failure;
17808 
17809 	genlmsg_end(msg, hdr);
17810 
17811 	rcu_read_lock();
17812 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
17813 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
17814 	rcu_read_unlock();
17815 
17816 	return;
17817 
17818 nla_put_failure:
17819 	nlmsg_free(msg);
17820 }
17821 
17822 struct nl80211_mlme_event {
17823 	enum nl80211_commands cmd;
17824 	const u8 *buf;
17825 	size_t buf_len;
17826 	int uapsd_queues;
17827 	const u8 *req_ies;
17828 	size_t req_ies_len;
17829 	bool reconnect;
17830 };
17831 
17832 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
17833 				    struct net_device *netdev,
17834 				    const struct nl80211_mlme_event *event,
17835 				    gfp_t gfp)
17836 {
17837 	struct sk_buff *msg;
17838 	void *hdr;
17839 
17840 	msg = nlmsg_new(100 + event->buf_len + event->req_ies_len, gfp);
17841 	if (!msg)
17842 		return;
17843 
17844 	hdr = nl80211hdr_put(msg, 0, 0, 0, event->cmd);
17845 	if (!hdr) {
17846 		nlmsg_free(msg);
17847 		return;
17848 	}
17849 
17850 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17851 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17852 	    nla_put(msg, NL80211_ATTR_FRAME, event->buf_len, event->buf) ||
17853 	    (event->req_ies &&
17854 	     nla_put(msg, NL80211_ATTR_REQ_IE, event->req_ies_len,
17855 		     event->req_ies)))
17856 		goto nla_put_failure;
17857 
17858 	if (event->reconnect &&
17859 	    nla_put_flag(msg, NL80211_ATTR_RECONNECT_REQUESTED))
17860 		goto nla_put_failure;
17861 
17862 	if (event->uapsd_queues >= 0) {
17863 		struct nlattr *nla_wmm =
17864 			nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
17865 		if (!nla_wmm)
17866 			goto nla_put_failure;
17867 
17868 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
17869 			       event->uapsd_queues))
17870 			goto nla_put_failure;
17871 
17872 		nla_nest_end(msg, nla_wmm);
17873 	}
17874 
17875 	genlmsg_end(msg, hdr);
17876 
17877 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17878 				NL80211_MCGRP_MLME, gfp);
17879 	return;
17880 
17881  nla_put_failure:
17882 	nlmsg_free(msg);
17883 }
17884 
17885 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
17886 			  struct net_device *netdev, const u8 *buf,
17887 			  size_t len, gfp_t gfp)
17888 {
17889 	struct nl80211_mlme_event event = {
17890 		.cmd = NL80211_CMD_AUTHENTICATE,
17891 		.buf = buf,
17892 		.buf_len = len,
17893 		.uapsd_queues = -1,
17894 	};
17895 
17896 	nl80211_send_mlme_event(rdev, netdev, &event, gfp);
17897 }
17898 
17899 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
17900 			   struct net_device *netdev,
17901 			   const struct cfg80211_rx_assoc_resp_data *data)
17902 {
17903 	struct nl80211_mlme_event event = {
17904 		.cmd = NL80211_CMD_ASSOCIATE,
17905 		.buf = data->buf,
17906 		.buf_len = data->len,
17907 		.uapsd_queues = data->uapsd_queues,
17908 		.req_ies = data->req_ies,
17909 		.req_ies_len = data->req_ies_len,
17910 	};
17911 
17912 	nl80211_send_mlme_event(rdev, netdev, &event, GFP_KERNEL);
17913 }
17914 
17915 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
17916 			 struct net_device *netdev, const u8 *buf,
17917 			 size_t len, bool reconnect, gfp_t gfp)
17918 {
17919 	struct nl80211_mlme_event event = {
17920 		.cmd = NL80211_CMD_DEAUTHENTICATE,
17921 		.buf = buf,
17922 		.buf_len = len,
17923 		.reconnect = reconnect,
17924 		.uapsd_queues = -1,
17925 	};
17926 
17927 	nl80211_send_mlme_event(rdev, netdev, &event, gfp);
17928 }
17929 
17930 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
17931 			   struct net_device *netdev, const u8 *buf,
17932 			   size_t len, bool reconnect, gfp_t gfp)
17933 {
17934 	struct nl80211_mlme_event event = {
17935 		.cmd = NL80211_CMD_DISASSOCIATE,
17936 		.buf = buf,
17937 		.buf_len = len,
17938 		.reconnect = reconnect,
17939 		.uapsd_queues = -1,
17940 	};
17941 
17942 	nl80211_send_mlme_event(rdev, netdev, &event, gfp);
17943 }
17944 
17945 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
17946 				  size_t len)
17947 {
17948 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17949 	struct wiphy *wiphy = wdev->wiphy;
17950 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17951 	const struct ieee80211_mgmt *mgmt = (void *)buf;
17952 	struct nl80211_mlme_event event = {
17953 		.buf = buf,
17954 		.buf_len = len,
17955 		.uapsd_queues = -1,
17956 	};
17957 
17958 	if (WARN_ON(len < 2))
17959 		return;
17960 
17961 	if (ieee80211_is_deauth(mgmt->frame_control)) {
17962 		event.cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
17963 	} else if (ieee80211_is_disassoc(mgmt->frame_control)) {
17964 		event.cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
17965 	} else if (ieee80211_is_beacon(mgmt->frame_control)) {
17966 		if (wdev->unprot_beacon_reported &&
17967 		    elapsed_jiffies_msecs(wdev->unprot_beacon_reported) < 10000)
17968 			return;
17969 		event.cmd = NL80211_CMD_UNPROT_BEACON;
17970 		wdev->unprot_beacon_reported = jiffies;
17971 	} else {
17972 		return;
17973 	}
17974 
17975 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
17976 	nl80211_send_mlme_event(rdev, dev, &event, GFP_ATOMIC);
17977 }
17978 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
17979 
17980 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
17981 				      struct net_device *netdev, int cmd,
17982 				      const u8 *addr, gfp_t gfp)
17983 {
17984 	struct sk_buff *msg;
17985 	void *hdr;
17986 
17987 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17988 	if (!msg)
17989 		return;
17990 
17991 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17992 	if (!hdr) {
17993 		nlmsg_free(msg);
17994 		return;
17995 	}
17996 
17997 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17998 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17999 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
18000 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
18001 		goto nla_put_failure;
18002 
18003 	genlmsg_end(msg, hdr);
18004 
18005 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18006 				NL80211_MCGRP_MLME, gfp);
18007 	return;
18008 
18009  nla_put_failure:
18010 	nlmsg_free(msg);
18011 }
18012 
18013 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
18014 			       struct net_device *netdev, const u8 *addr,
18015 			       gfp_t gfp)
18016 {
18017 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
18018 				  addr, gfp);
18019 }
18020 
18021 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
18022 				struct net_device *netdev, const u8 *addr,
18023 				gfp_t gfp)
18024 {
18025 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
18026 				  addr, gfp);
18027 }
18028 
18029 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
18030 				 struct net_device *netdev,
18031 				 struct cfg80211_connect_resp_params *cr,
18032 				 gfp_t gfp)
18033 {
18034 	struct sk_buff *msg;
18035 	void *hdr;
18036 	unsigned int link;
18037 	size_t link_info_size = 0;
18038 	const u8 *connected_addr = cr->valid_links ?
18039 				   cr->ap_mld_addr : cr->links[0].bssid;
18040 
18041 	if (cr->valid_links) {
18042 		for_each_valid_link(cr, link) {
18043 			/* Nested attribute header */
18044 			link_info_size += NLA_HDRLEN;
18045 			/* Link ID */
18046 			link_info_size += nla_total_size(sizeof(u8));
18047 			link_info_size += cr->links[link].addr ?
18048 					  nla_total_size(ETH_ALEN) : 0;
18049 			link_info_size += (cr->links[link].bssid ||
18050 					   cr->links[link].bss) ?
18051 					  nla_total_size(ETH_ALEN) : 0;
18052 			link_info_size += nla_total_size(sizeof(u16));
18053 		}
18054 	}
18055 
18056 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
18057 			cr->fils.kek_len + cr->fils.pmk_len +
18058 			(cr->fils.pmkid ? WLAN_PMKID_LEN : 0) + link_info_size,
18059 			gfp);
18060 	if (!msg)
18061 		return;
18062 
18063 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
18064 	if (!hdr) {
18065 		nlmsg_free(msg);
18066 		return;
18067 	}
18068 
18069 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18070 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18071 	    (connected_addr &&
18072 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr)) ||
18073 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
18074 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
18075 			cr->status) ||
18076 	    (cr->status < 0 &&
18077 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
18078 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
18079 			  cr->timeout_reason))) ||
18080 	    (cr->req_ie &&
18081 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
18082 	    (cr->resp_ie &&
18083 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
18084 		     cr->resp_ie)) ||
18085 	    (cr->fils.update_erp_next_seq_num &&
18086 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
18087 			 cr->fils.erp_next_seq_num)) ||
18088 	    (cr->status == WLAN_STATUS_SUCCESS &&
18089 	     ((cr->fils.kek &&
18090 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
18091 		       cr->fils.kek)) ||
18092 	      (cr->fils.pmk &&
18093 	       nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
18094 	      (cr->fils.pmkid &&
18095 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
18096 		goto nla_put_failure;
18097 
18098 	if (cr->valid_links) {
18099 		int i = 1;
18100 		struct nlattr *nested;
18101 
18102 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18103 		if (!nested)
18104 			goto nla_put_failure;
18105 
18106 		for_each_valid_link(cr, link) {
18107 			struct nlattr *nested_mlo_links;
18108 			const u8 *bssid = cr->links[link].bss ?
18109 					  cr->links[link].bss->bssid :
18110 					  cr->links[link].bssid;
18111 
18112 			nested_mlo_links = nla_nest_start(msg, i);
18113 			if (!nested_mlo_links)
18114 				goto nla_put_failure;
18115 
18116 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
18117 			    (bssid &&
18118 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
18119 			    (cr->links[link].addr &&
18120 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
18121 				     cr->links[link].addr)) ||
18122 			    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
18123 					cr->links[link].status))
18124 				goto nla_put_failure;
18125 
18126 			nla_nest_end(msg, nested_mlo_links);
18127 			i++;
18128 		}
18129 		nla_nest_end(msg, nested);
18130 	}
18131 
18132 	genlmsg_end(msg, hdr);
18133 
18134 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18135 				NL80211_MCGRP_MLME, gfp);
18136 	return;
18137 
18138  nla_put_failure:
18139 	nlmsg_free(msg);
18140 }
18141 
18142 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
18143 			 struct net_device *netdev,
18144 			 struct cfg80211_roam_info *info, gfp_t gfp)
18145 {
18146 	struct sk_buff *msg;
18147 	void *hdr;
18148 	size_t link_info_size = 0;
18149 	unsigned int link;
18150 	const u8 *connected_addr = info->ap_mld_addr ?
18151 				   info->ap_mld_addr :
18152 				   (info->links[0].bss ?
18153 				    info->links[0].bss->bssid :
18154 				    info->links[0].bssid);
18155 
18156 	if (info->valid_links) {
18157 		for_each_valid_link(info, link) {
18158 			/* Nested attribute header */
18159 			link_info_size += NLA_HDRLEN;
18160 			/* Link ID */
18161 			link_info_size += nla_total_size(sizeof(u8));
18162 			link_info_size += info->links[link].addr ?
18163 					  nla_total_size(ETH_ALEN) : 0;
18164 			link_info_size += (info->links[link].bssid ||
18165 					   info->links[link].bss) ?
18166 					  nla_total_size(ETH_ALEN) : 0;
18167 		}
18168 	}
18169 
18170 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
18171 			info->fils.kek_len + info->fils.pmk_len +
18172 			(info->fils.pmkid ? WLAN_PMKID_LEN : 0) +
18173 			link_info_size, gfp);
18174 	if (!msg)
18175 		return;
18176 
18177 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
18178 	if (!hdr) {
18179 		nlmsg_free(msg);
18180 		return;
18181 	}
18182 
18183 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18184 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18185 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr) ||
18186 	    (info->req_ie &&
18187 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
18188 		     info->req_ie)) ||
18189 	    (info->resp_ie &&
18190 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
18191 		     info->resp_ie)) ||
18192 	    (info->fils.update_erp_next_seq_num &&
18193 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
18194 			 info->fils.erp_next_seq_num)) ||
18195 	    (info->fils.kek &&
18196 	     nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
18197 		     info->fils.kek)) ||
18198 	    (info->fils.pmk &&
18199 	     nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
18200 	    (info->fils.pmkid &&
18201 	     nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
18202 		goto nla_put_failure;
18203 
18204 	if (info->valid_links) {
18205 		int i = 1;
18206 		struct nlattr *nested;
18207 
18208 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18209 		if (!nested)
18210 			goto nla_put_failure;
18211 
18212 		for_each_valid_link(info, link) {
18213 			struct nlattr *nested_mlo_links;
18214 			const u8 *bssid = info->links[link].bss ?
18215 					  info->links[link].bss->bssid :
18216 					  info->links[link].bssid;
18217 
18218 			nested_mlo_links = nla_nest_start(msg, i);
18219 			if (!nested_mlo_links)
18220 				goto nla_put_failure;
18221 
18222 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
18223 			    (bssid &&
18224 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
18225 			    (info->links[link].addr &&
18226 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
18227 				     info->links[link].addr)))
18228 				goto nla_put_failure;
18229 
18230 			nla_nest_end(msg, nested_mlo_links);
18231 			i++;
18232 		}
18233 		nla_nest_end(msg, nested);
18234 	}
18235 
18236 	genlmsg_end(msg, hdr);
18237 
18238 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18239 				NL80211_MCGRP_MLME, gfp);
18240 	return;
18241 
18242  nla_put_failure:
18243 	nlmsg_free(msg);
18244 }
18245 
18246 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
18247 				  struct net_device *netdev, const u8 *peer_addr,
18248 				  const u8 *td_bitmap, u8 td_bitmap_len)
18249 {
18250 	struct sk_buff *msg;
18251 	void *hdr;
18252 
18253 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18254 	if (!msg)
18255 		return;
18256 
18257 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
18258 	if (!hdr) {
18259 		nlmsg_free(msg);
18260 		return;
18261 	}
18262 
18263 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18264 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18265 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer_addr))
18266 		goto nla_put_failure;
18267 
18268 	if ((td_bitmap_len > 0) && td_bitmap)
18269 		if (nla_put(msg, NL80211_ATTR_TD_BITMAP,
18270 			    td_bitmap_len, td_bitmap))
18271 			goto nla_put_failure;
18272 
18273 	genlmsg_end(msg, hdr);
18274 
18275 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18276 				NL80211_MCGRP_MLME, GFP_KERNEL);
18277 	return;
18278 
18279  nla_put_failure:
18280 	nlmsg_free(msg);
18281 }
18282 
18283 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
18284 			       struct net_device *netdev, u16 reason,
18285 			       const u8 *ie, size_t ie_len, bool from_ap)
18286 {
18287 	struct sk_buff *msg;
18288 	void *hdr;
18289 
18290 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
18291 	if (!msg)
18292 		return;
18293 
18294 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
18295 	if (!hdr) {
18296 		nlmsg_free(msg);
18297 		return;
18298 	}
18299 
18300 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18301 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18302 	    (reason &&
18303 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
18304 	    (from_ap &&
18305 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
18306 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
18307 		goto nla_put_failure;
18308 
18309 	genlmsg_end(msg, hdr);
18310 
18311 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18312 				NL80211_MCGRP_MLME, GFP_KERNEL);
18313 	return;
18314 
18315  nla_put_failure:
18316 	nlmsg_free(msg);
18317 }
18318 
18319 void cfg80211_links_removed(struct net_device *dev, u16 link_mask)
18320 {
18321 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18322 	struct wiphy *wiphy = wdev->wiphy;
18323 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18324 	struct sk_buff *msg;
18325 	struct nlattr *links;
18326 	void *hdr;
18327 
18328 	lockdep_assert_wiphy(wdev->wiphy);
18329 	trace_cfg80211_links_removed(dev, link_mask);
18330 
18331 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
18332 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
18333 		return;
18334 
18335 	if (WARN_ON(!wdev->valid_links || !link_mask ||
18336 		    (wdev->valid_links & link_mask) != link_mask ||
18337 		    wdev->valid_links == link_mask))
18338 		return;
18339 
18340 	cfg80211_wdev_release_link_bsses(wdev, link_mask);
18341 	wdev->valid_links &= ~link_mask;
18342 
18343 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18344 	if (!msg)
18345 		return;
18346 
18347 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_LINKS_REMOVED);
18348 	if (!hdr) {
18349 		nlmsg_free(msg);
18350 		return;
18351 	}
18352 
18353 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18354 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
18355 		goto nla_put_failure;
18356 
18357 	links = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18358 	if (!links)
18359 		goto nla_put_failure;
18360 
18361 	while (link_mask) {
18362 		struct nlattr *link;
18363 		int link_id = __ffs(link_mask);
18364 
18365 		link = nla_nest_start(msg, link_id + 1);
18366 		if (!link)
18367 			goto nla_put_failure;
18368 
18369 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
18370 			goto nla_put_failure;
18371 
18372 		nla_nest_end(msg, link);
18373 		link_mask &= ~(1 << link_id);
18374 	}
18375 
18376 	nla_nest_end(msg, links);
18377 
18378 	genlmsg_end(msg, hdr);
18379 
18380 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18381 				NL80211_MCGRP_MLME, GFP_KERNEL);
18382 	return;
18383 
18384  nla_put_failure:
18385 	nlmsg_free(msg);
18386 }
18387 EXPORT_SYMBOL(cfg80211_links_removed);
18388 
18389 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
18390 			     struct net_device *netdev, const u8 *bssid,
18391 			     gfp_t gfp)
18392 {
18393 	struct sk_buff *msg;
18394 	void *hdr;
18395 
18396 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18397 	if (!msg)
18398 		return;
18399 
18400 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
18401 	if (!hdr) {
18402 		nlmsg_free(msg);
18403 		return;
18404 	}
18405 
18406 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18407 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18408 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18409 		goto nla_put_failure;
18410 
18411 	genlmsg_end(msg, hdr);
18412 
18413 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18414 				NL80211_MCGRP_MLME, gfp);
18415 	return;
18416 
18417  nla_put_failure:
18418 	nlmsg_free(msg);
18419 }
18420 
18421 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
18422 					const u8 *ie, u8 ie_len,
18423 					int sig_dbm, gfp_t gfp)
18424 {
18425 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18426 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18427 	struct sk_buff *msg;
18428 	void *hdr;
18429 
18430 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
18431 		return;
18432 
18433 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
18434 
18435 	msg = nlmsg_new(100 + ie_len, gfp);
18436 	if (!msg)
18437 		return;
18438 
18439 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
18440 	if (!hdr) {
18441 		nlmsg_free(msg);
18442 		return;
18443 	}
18444 
18445 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18446 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18447 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
18448 	    (ie_len && ie &&
18449 	     nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
18450 	    (sig_dbm &&
18451 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
18452 		goto nla_put_failure;
18453 
18454 	genlmsg_end(msg, hdr);
18455 
18456 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18457 				NL80211_MCGRP_MLME, gfp);
18458 	return;
18459 
18460  nla_put_failure:
18461 	nlmsg_free(msg);
18462 }
18463 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
18464 
18465 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
18466 				 struct net_device *netdev, const u8 *addr,
18467 				 enum nl80211_key_type key_type, int key_id,
18468 				 const u8 *tsc, gfp_t gfp)
18469 {
18470 	struct sk_buff *msg;
18471 	void *hdr;
18472 
18473 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18474 	if (!msg)
18475 		return;
18476 
18477 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
18478 	if (!hdr) {
18479 		nlmsg_free(msg);
18480 		return;
18481 	}
18482 
18483 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18484 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18485 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
18486 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
18487 	    (key_id != -1 &&
18488 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
18489 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
18490 		goto nla_put_failure;
18491 
18492 	genlmsg_end(msg, hdr);
18493 
18494 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18495 				NL80211_MCGRP_MLME, gfp);
18496 	return;
18497 
18498  nla_put_failure:
18499 	nlmsg_free(msg);
18500 }
18501 
18502 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
18503 				    struct ieee80211_channel *channel_before,
18504 				    struct ieee80211_channel *channel_after)
18505 {
18506 	struct sk_buff *msg;
18507 	void *hdr;
18508 	struct nlattr *nl_freq;
18509 
18510 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
18511 	if (!msg)
18512 		return;
18513 
18514 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
18515 	if (!hdr) {
18516 		nlmsg_free(msg);
18517 		return;
18518 	}
18519 
18520 	/*
18521 	 * Since we are applying the beacon hint to a wiphy we know its
18522 	 * wiphy_idx is valid
18523 	 */
18524 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
18525 		goto nla_put_failure;
18526 
18527 	/* Before */
18528 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
18529 	if (!nl_freq)
18530 		goto nla_put_failure;
18531 
18532 	if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
18533 		goto nla_put_failure;
18534 	nla_nest_end(msg, nl_freq);
18535 
18536 	/* After */
18537 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
18538 	if (!nl_freq)
18539 		goto nla_put_failure;
18540 
18541 	if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
18542 		goto nla_put_failure;
18543 	nla_nest_end(msg, nl_freq);
18544 
18545 	genlmsg_end(msg, hdr);
18546 
18547 	rcu_read_lock();
18548 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
18549 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
18550 	rcu_read_unlock();
18551 
18552 	return;
18553 
18554 nla_put_failure:
18555 	nlmsg_free(msg);
18556 }
18557 
18558 static void nl80211_send_remain_on_chan_event(
18559 	int cmd, struct cfg80211_registered_device *rdev,
18560 	struct wireless_dev *wdev, u64 cookie,
18561 	struct ieee80211_channel *chan,
18562 	unsigned int duration, gfp_t gfp)
18563 {
18564 	struct sk_buff *msg;
18565 	void *hdr;
18566 
18567 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18568 	if (!msg)
18569 		return;
18570 
18571 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18572 	if (!hdr) {
18573 		nlmsg_free(msg);
18574 		return;
18575 	}
18576 
18577 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18578 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18579 					 wdev->netdev->ifindex)) ||
18580 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18581 			      NL80211_ATTR_PAD) ||
18582 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
18583 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
18584 			NL80211_CHAN_NO_HT) ||
18585 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
18586 			      NL80211_ATTR_PAD))
18587 		goto nla_put_failure;
18588 
18589 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
18590 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
18591 		goto nla_put_failure;
18592 
18593 	genlmsg_end(msg, hdr);
18594 
18595 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18596 				NL80211_MCGRP_MLME, gfp);
18597 	return;
18598 
18599  nla_put_failure:
18600 	nlmsg_free(msg);
18601 }
18602 
18603 void cfg80211_assoc_comeback(struct net_device *netdev,
18604 			     const u8 *ap_addr, u32 timeout)
18605 {
18606 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
18607 	struct wiphy *wiphy = wdev->wiphy;
18608 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18609 	struct sk_buff *msg;
18610 	void *hdr;
18611 
18612 	trace_cfg80211_assoc_comeback(wdev, ap_addr, timeout);
18613 
18614 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18615 	if (!msg)
18616 		return;
18617 
18618 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ASSOC_COMEBACK);
18619 	if (!hdr) {
18620 		nlmsg_free(msg);
18621 		return;
18622 	}
18623 
18624 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18625 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18626 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ap_addr) ||
18627 	    nla_put_u32(msg, NL80211_ATTR_TIMEOUT, timeout))
18628 		goto nla_put_failure;
18629 
18630 	genlmsg_end(msg, hdr);
18631 
18632 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18633 				NL80211_MCGRP_MLME, GFP_KERNEL);
18634 	return;
18635 
18636  nla_put_failure:
18637 	nlmsg_free(msg);
18638 }
18639 EXPORT_SYMBOL(cfg80211_assoc_comeback);
18640 
18641 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
18642 			       struct ieee80211_channel *chan,
18643 			       unsigned int duration, gfp_t gfp)
18644 {
18645 	struct wiphy *wiphy = wdev->wiphy;
18646 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18647 
18648 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
18649 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
18650 					  rdev, wdev, cookie, chan,
18651 					  duration, gfp);
18652 }
18653 EXPORT_SYMBOL(cfg80211_ready_on_channel);
18654 
18655 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
18656 					struct ieee80211_channel *chan,
18657 					gfp_t gfp)
18658 {
18659 	struct wiphy *wiphy = wdev->wiphy;
18660 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18661 
18662 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
18663 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
18664 					  rdev, wdev, cookie, chan, 0, gfp);
18665 }
18666 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
18667 
18668 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
18669 					struct ieee80211_channel *chan,
18670 					gfp_t gfp)
18671 {
18672 	struct wiphy *wiphy = wdev->wiphy;
18673 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18674 
18675 	trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
18676 	nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
18677 					  rdev, wdev, cookie, chan, 0, gfp);
18678 }
18679 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
18680 
18681 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
18682 		      struct station_info *sinfo, gfp_t gfp)
18683 {
18684 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18685 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18686 	struct sk_buff *msg;
18687 
18688 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
18689 
18690 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18691 	if (!msg)
18692 		return;
18693 
18694 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
18695 				 rdev, dev, mac_addr, sinfo) < 0) {
18696 		nlmsg_free(msg);
18697 		return;
18698 	}
18699 
18700 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18701 				NL80211_MCGRP_MLME, gfp);
18702 }
18703 EXPORT_SYMBOL(cfg80211_new_sta);
18704 
18705 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
18706 			    struct station_info *sinfo, gfp_t gfp)
18707 {
18708 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18709 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18710 	struct sk_buff *msg;
18711 	struct station_info empty_sinfo = {};
18712 
18713 	if (!sinfo)
18714 		sinfo = &empty_sinfo;
18715 
18716 	trace_cfg80211_del_sta(dev, mac_addr);
18717 
18718 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18719 	if (!msg) {
18720 		cfg80211_sinfo_release_content(sinfo);
18721 		return;
18722 	}
18723 
18724 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
18725 				 rdev, dev, mac_addr, sinfo) < 0) {
18726 		nlmsg_free(msg);
18727 		return;
18728 	}
18729 
18730 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18731 				NL80211_MCGRP_MLME, gfp);
18732 }
18733 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
18734 
18735 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
18736 			  enum nl80211_connect_failed_reason reason,
18737 			  gfp_t gfp)
18738 {
18739 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18740 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18741 	struct sk_buff *msg;
18742 	void *hdr;
18743 
18744 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
18745 	if (!msg)
18746 		return;
18747 
18748 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
18749 	if (!hdr) {
18750 		nlmsg_free(msg);
18751 		return;
18752 	}
18753 
18754 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18755 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
18756 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
18757 		goto nla_put_failure;
18758 
18759 	genlmsg_end(msg, hdr);
18760 
18761 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18762 				NL80211_MCGRP_MLME, gfp);
18763 	return;
18764 
18765  nla_put_failure:
18766 	nlmsg_free(msg);
18767 }
18768 EXPORT_SYMBOL(cfg80211_conn_failed);
18769 
18770 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
18771 				       const u8 *addr, gfp_t gfp)
18772 {
18773 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18774 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18775 	struct sk_buff *msg;
18776 	void *hdr;
18777 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
18778 
18779 	if (!nlportid)
18780 		return false;
18781 
18782 	msg = nlmsg_new(100, gfp);
18783 	if (!msg)
18784 		return true;
18785 
18786 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18787 	if (!hdr) {
18788 		nlmsg_free(msg);
18789 		return true;
18790 	}
18791 
18792 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18793 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18794 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
18795 		goto nla_put_failure;
18796 
18797 	genlmsg_end(msg, hdr);
18798 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18799 	return true;
18800 
18801  nla_put_failure:
18802 	nlmsg_free(msg);
18803 	return true;
18804 }
18805 
18806 bool cfg80211_rx_spurious_frame(struct net_device *dev,
18807 				const u8 *addr, gfp_t gfp)
18808 {
18809 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18810 	bool ret;
18811 
18812 	trace_cfg80211_rx_spurious_frame(dev, addr);
18813 
18814 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18815 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
18816 		trace_cfg80211_return_bool(false);
18817 		return false;
18818 	}
18819 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
18820 					 addr, gfp);
18821 	trace_cfg80211_return_bool(ret);
18822 	return ret;
18823 }
18824 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
18825 
18826 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
18827 					const u8 *addr, gfp_t gfp)
18828 {
18829 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18830 	bool ret;
18831 
18832 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
18833 
18834 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18835 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
18836 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
18837 		trace_cfg80211_return_bool(false);
18838 		return false;
18839 	}
18840 	ret = __nl80211_unexpected_frame(dev,
18841 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
18842 					 addr, gfp);
18843 	trace_cfg80211_return_bool(ret);
18844 	return ret;
18845 }
18846 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
18847 
18848 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
18849 		      struct wireless_dev *wdev, u32 nlportid,
18850 		      struct cfg80211_rx_info *info, gfp_t gfp)
18851 {
18852 	struct net_device *netdev = wdev->netdev;
18853 	struct sk_buff *msg;
18854 	void *hdr;
18855 
18856 	msg = nlmsg_new(100 + info->len, gfp);
18857 	if (!msg)
18858 		return -ENOMEM;
18859 
18860 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
18861 	if (!hdr) {
18862 		nlmsg_free(msg);
18863 		return -ENOMEM;
18864 	}
18865 
18866 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18867 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18868 					netdev->ifindex)) ||
18869 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18870 			      NL80211_ATTR_PAD) ||
18871 	    (info->have_link_id &&
18872 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, info->link_id)) ||
18873 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, KHZ_TO_MHZ(info->freq)) ||
18874 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET, info->freq % 1000) ||
18875 	    (info->sig_dbm &&
18876 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, info->sig_dbm)) ||
18877 	    nla_put(msg, NL80211_ATTR_FRAME, info->len, info->buf) ||
18878 	    (info->flags &&
18879 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, info->flags)) ||
18880 	    (info->rx_tstamp && nla_put_u64_64bit(msg,
18881 						  NL80211_ATTR_RX_HW_TIMESTAMP,
18882 						  info->rx_tstamp,
18883 						  NL80211_ATTR_PAD)) ||
18884 	    (info->ack_tstamp && nla_put_u64_64bit(msg,
18885 						   NL80211_ATTR_TX_HW_TIMESTAMP,
18886 						   info->ack_tstamp,
18887 						   NL80211_ATTR_PAD)))
18888 		goto nla_put_failure;
18889 
18890 	genlmsg_end(msg, hdr);
18891 
18892 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18893 
18894  nla_put_failure:
18895 	nlmsg_free(msg);
18896 	return -ENOBUFS;
18897 }
18898 
18899 static void nl80211_frame_tx_status(struct wireless_dev *wdev,
18900 				    struct cfg80211_tx_status *status,
18901 				    gfp_t gfp, enum nl80211_commands command)
18902 {
18903 	struct wiphy *wiphy = wdev->wiphy;
18904 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18905 	struct net_device *netdev = wdev->netdev;
18906 	struct sk_buff *msg;
18907 	void *hdr;
18908 
18909 	if (command == NL80211_CMD_FRAME_TX_STATUS)
18910 		trace_cfg80211_mgmt_tx_status(wdev, status->cookie,
18911 					      status->ack);
18912 	else
18913 		trace_cfg80211_control_port_tx_status(wdev, status->cookie,
18914 						      status->ack);
18915 
18916 	msg = nlmsg_new(100 + status->len, gfp);
18917 	if (!msg)
18918 		return;
18919 
18920 	hdr = nl80211hdr_put(msg, 0, 0, 0, command);
18921 	if (!hdr) {
18922 		nlmsg_free(msg);
18923 		return;
18924 	}
18925 
18926 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18927 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18928 				   netdev->ifindex)) ||
18929 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18930 			      NL80211_ATTR_PAD) ||
18931 	    nla_put(msg, NL80211_ATTR_FRAME, status->len, status->buf) ||
18932 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, status->cookie,
18933 			      NL80211_ATTR_PAD) ||
18934 	    (status->ack && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
18935 	    (status->tx_tstamp &&
18936 	     nla_put_u64_64bit(msg, NL80211_ATTR_TX_HW_TIMESTAMP,
18937 			       status->tx_tstamp, NL80211_ATTR_PAD)) ||
18938 	    (status->ack_tstamp &&
18939 	     nla_put_u64_64bit(msg, NL80211_ATTR_RX_HW_TIMESTAMP,
18940 			       status->ack_tstamp, NL80211_ATTR_PAD)))
18941 		goto nla_put_failure;
18942 
18943 	genlmsg_end(msg, hdr);
18944 
18945 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18946 				NL80211_MCGRP_MLME, gfp);
18947 	return;
18948 
18949 nla_put_failure:
18950 	nlmsg_free(msg);
18951 }
18952 
18953 void cfg80211_control_port_tx_status(struct wireless_dev *wdev, u64 cookie,
18954 				     const u8 *buf, size_t len, bool ack,
18955 				     gfp_t gfp)
18956 {
18957 	struct cfg80211_tx_status status = {
18958 		.cookie = cookie,
18959 		.buf = buf,
18960 		.len = len,
18961 		.ack = ack
18962 	};
18963 
18964 	nl80211_frame_tx_status(wdev, &status, gfp,
18965 				NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS);
18966 }
18967 EXPORT_SYMBOL(cfg80211_control_port_tx_status);
18968 
18969 void cfg80211_mgmt_tx_status_ext(struct wireless_dev *wdev,
18970 				 struct cfg80211_tx_status *status, gfp_t gfp)
18971 {
18972 	nl80211_frame_tx_status(wdev, status, gfp, NL80211_CMD_FRAME_TX_STATUS);
18973 }
18974 EXPORT_SYMBOL(cfg80211_mgmt_tx_status_ext);
18975 
18976 static int __nl80211_rx_control_port(struct net_device *dev,
18977 				     struct sk_buff *skb,
18978 				     bool unencrypted,
18979 				     int link_id,
18980 				     gfp_t gfp)
18981 {
18982 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18983 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18984 	struct ethhdr *ehdr = eth_hdr(skb);
18985 	const u8 *addr = ehdr->h_source;
18986 	u16 proto = be16_to_cpu(skb->protocol);
18987 	struct sk_buff *msg;
18988 	void *hdr;
18989 	struct nlattr *frame;
18990 
18991 	u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
18992 
18993 	if (!nlportid)
18994 		return -ENOENT;
18995 
18996 	msg = nlmsg_new(100 + skb->len, gfp);
18997 	if (!msg)
18998 		return -ENOMEM;
18999 
19000 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
19001 	if (!hdr) {
19002 		nlmsg_free(msg);
19003 		return -ENOBUFS;
19004 	}
19005 
19006 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19007 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19008 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19009 			      NL80211_ATTR_PAD) ||
19010 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
19011 	    nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
19012 	    (link_id >= 0 &&
19013 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
19014 	    (unencrypted && nla_put_flag(msg,
19015 					 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
19016 		goto nla_put_failure;
19017 
19018 	frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
19019 	if (!frame)
19020 		goto nla_put_failure;
19021 
19022 	skb_copy_bits(skb, 0, nla_data(frame), skb->len);
19023 	genlmsg_end(msg, hdr);
19024 
19025 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
19026 
19027  nla_put_failure:
19028 	nlmsg_free(msg);
19029 	return -ENOBUFS;
19030 }
19031 
19032 bool cfg80211_rx_control_port(struct net_device *dev, struct sk_buff *skb,
19033 			      bool unencrypted, int link_id)
19034 {
19035 	int ret;
19036 
19037 	trace_cfg80211_rx_control_port(dev, skb, unencrypted, link_id);
19038 	ret = __nl80211_rx_control_port(dev, skb, unencrypted, link_id,
19039 					GFP_ATOMIC);
19040 	trace_cfg80211_return_bool(ret == 0);
19041 	return ret == 0;
19042 }
19043 EXPORT_SYMBOL(cfg80211_rx_control_port);
19044 
19045 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
19046 					    const char *mac, gfp_t gfp)
19047 {
19048 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19049 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19050 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19051 	void **cb;
19052 
19053 	if (!msg)
19054 		return NULL;
19055 
19056 	cb = (void **)msg->cb;
19057 
19058 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
19059 	if (!cb[0]) {
19060 		nlmsg_free(msg);
19061 		return NULL;
19062 	}
19063 
19064 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19065 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19066 		goto nla_put_failure;
19067 
19068 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
19069 		goto nla_put_failure;
19070 
19071 	cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
19072 	if (!cb[1])
19073 		goto nla_put_failure;
19074 
19075 	cb[2] = rdev;
19076 
19077 	return msg;
19078  nla_put_failure:
19079 	nlmsg_free(msg);
19080 	return NULL;
19081 }
19082 
19083 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
19084 {
19085 	void **cb = (void **)msg->cb;
19086 	struct cfg80211_registered_device *rdev = cb[2];
19087 
19088 	nla_nest_end(msg, cb[1]);
19089 	genlmsg_end(msg, cb[0]);
19090 
19091 	memset(msg->cb, 0, sizeof(msg->cb));
19092 
19093 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19094 				NL80211_MCGRP_MLME, gfp);
19095 }
19096 
19097 void cfg80211_cqm_rssi_notify(struct net_device *dev,
19098 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
19099 			      s32 rssi_level, gfp_t gfp)
19100 {
19101 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19102 	struct cfg80211_cqm_config *cqm_config;
19103 
19104 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
19105 
19106 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
19107 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
19108 		return;
19109 
19110 	rcu_read_lock();
19111 	cqm_config = rcu_dereference(wdev->cqm_config);
19112 	if (cqm_config) {
19113 		cqm_config->last_rssi_event_value = rssi_level;
19114 		cqm_config->last_rssi_event_type = rssi_event;
19115 		wiphy_work_queue(wdev->wiphy, &wdev->cqm_rssi_work);
19116 	}
19117 	rcu_read_unlock();
19118 }
19119 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
19120 
19121 void cfg80211_cqm_rssi_notify_work(struct wiphy *wiphy, struct wiphy_work *work)
19122 {
19123 	struct wireless_dev *wdev = container_of(work, struct wireless_dev,
19124 						 cqm_rssi_work);
19125 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19126 	enum nl80211_cqm_rssi_threshold_event rssi_event;
19127 	struct cfg80211_cqm_config *cqm_config;
19128 	struct sk_buff *msg;
19129 	s32 rssi_level;
19130 
19131 	cqm_config = wiphy_dereference(wdev->wiphy, wdev->cqm_config);
19132 	if (!cqm_config)
19133 		return;
19134 
19135 	if (cqm_config->use_range_api)
19136 		cfg80211_cqm_rssi_update(rdev, wdev->netdev, cqm_config);
19137 
19138 	rssi_level = cqm_config->last_rssi_event_value;
19139 	rssi_event = cqm_config->last_rssi_event_type;
19140 
19141 	msg = cfg80211_prepare_cqm(wdev->netdev, NULL, GFP_KERNEL);
19142 	if (!msg)
19143 		return;
19144 
19145 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
19146 			rssi_event))
19147 		goto nla_put_failure;
19148 
19149 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
19150 				      rssi_level))
19151 		goto nla_put_failure;
19152 
19153 	cfg80211_send_cqm(msg, GFP_KERNEL);
19154 
19155 	return;
19156 
19157  nla_put_failure:
19158 	nlmsg_free(msg);
19159 }
19160 
19161 void cfg80211_cqm_txe_notify(struct net_device *dev,
19162 			     const u8 *peer, u32 num_packets,
19163 			     u32 rate, u32 intvl, gfp_t gfp)
19164 {
19165 	struct sk_buff *msg;
19166 
19167 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
19168 	if (!msg)
19169 		return;
19170 
19171 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
19172 		goto nla_put_failure;
19173 
19174 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
19175 		goto nla_put_failure;
19176 
19177 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
19178 		goto nla_put_failure;
19179 
19180 	cfg80211_send_cqm(msg, gfp);
19181 	return;
19182 
19183  nla_put_failure:
19184 	nlmsg_free(msg);
19185 }
19186 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
19187 
19188 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
19189 				 const u8 *peer, u32 num_packets, gfp_t gfp)
19190 {
19191 	struct sk_buff *msg;
19192 
19193 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
19194 
19195 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
19196 	if (!msg)
19197 		return;
19198 
19199 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
19200 		goto nla_put_failure;
19201 
19202 	cfg80211_send_cqm(msg, gfp);
19203 	return;
19204 
19205  nla_put_failure:
19206 	nlmsg_free(msg);
19207 }
19208 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
19209 
19210 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
19211 {
19212 	struct sk_buff *msg;
19213 
19214 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
19215 	if (!msg)
19216 		return;
19217 
19218 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
19219 		goto nla_put_failure;
19220 
19221 	cfg80211_send_cqm(msg, gfp);
19222 	return;
19223 
19224  nla_put_failure:
19225 	nlmsg_free(msg);
19226 }
19227 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
19228 
19229 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
19230 				     struct net_device *netdev, const u8 *bssid,
19231 				     const u8 *replay_ctr, gfp_t gfp)
19232 {
19233 	struct sk_buff *msg;
19234 	struct nlattr *rekey_attr;
19235 	void *hdr;
19236 
19237 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19238 	if (!msg)
19239 		return;
19240 
19241 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
19242 	if (!hdr) {
19243 		nlmsg_free(msg);
19244 		return;
19245 	}
19246 
19247 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19248 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19249 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
19250 		goto nla_put_failure;
19251 
19252 	rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
19253 	if (!rekey_attr)
19254 		goto nla_put_failure;
19255 
19256 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
19257 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
19258 		goto nla_put_failure;
19259 
19260 	nla_nest_end(msg, rekey_attr);
19261 
19262 	genlmsg_end(msg, hdr);
19263 
19264 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19265 				NL80211_MCGRP_MLME, gfp);
19266 	return;
19267 
19268  nla_put_failure:
19269 	nlmsg_free(msg);
19270 }
19271 
19272 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
19273 			       const u8 *replay_ctr, gfp_t gfp)
19274 {
19275 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19276 	struct wiphy *wiphy = wdev->wiphy;
19277 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19278 
19279 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
19280 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
19281 }
19282 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
19283 
19284 static void
19285 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
19286 			       struct net_device *netdev, int index,
19287 			       const u8 *bssid, bool preauth, gfp_t gfp)
19288 {
19289 	struct sk_buff *msg;
19290 	struct nlattr *attr;
19291 	void *hdr;
19292 
19293 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19294 	if (!msg)
19295 		return;
19296 
19297 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
19298 	if (!hdr) {
19299 		nlmsg_free(msg);
19300 		return;
19301 	}
19302 
19303 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19304 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
19305 		goto nla_put_failure;
19306 
19307 	attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
19308 	if (!attr)
19309 		goto nla_put_failure;
19310 
19311 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
19312 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
19313 	    (preauth &&
19314 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
19315 		goto nla_put_failure;
19316 
19317 	nla_nest_end(msg, attr);
19318 
19319 	genlmsg_end(msg, hdr);
19320 
19321 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19322 				NL80211_MCGRP_MLME, gfp);
19323 	return;
19324 
19325  nla_put_failure:
19326 	nlmsg_free(msg);
19327 }
19328 
19329 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
19330 				     const u8 *bssid, bool preauth, gfp_t gfp)
19331 {
19332 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19333 	struct wiphy *wiphy = wdev->wiphy;
19334 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19335 
19336 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
19337 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
19338 }
19339 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
19340 
19341 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
19342 				     struct net_device *netdev,
19343 				     unsigned int link_id,
19344 				     struct cfg80211_chan_def *chandef,
19345 				     gfp_t gfp,
19346 				     enum nl80211_commands notif,
19347 				     u8 count, bool quiet)
19348 {
19349 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
19350 	struct sk_buff *msg;
19351 	void *hdr;
19352 
19353 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19354 	if (!msg)
19355 		return;
19356 
19357 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
19358 	if (!hdr) {
19359 		nlmsg_free(msg);
19360 		return;
19361 	}
19362 
19363 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
19364 		goto nla_put_failure;
19365 
19366 	if (wdev->valid_links &&
19367 	    nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
19368 		goto nla_put_failure;
19369 
19370 	if (nl80211_send_chandef(msg, chandef))
19371 		goto nla_put_failure;
19372 
19373 	if (notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) {
19374 		if (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count))
19375 			goto nla_put_failure;
19376 		if (quiet &&
19377 		    nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX))
19378 			goto nla_put_failure;
19379 	}
19380 
19381 	genlmsg_end(msg, hdr);
19382 
19383 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19384 				NL80211_MCGRP_MLME, gfp);
19385 	return;
19386 
19387  nla_put_failure:
19388 	nlmsg_free(msg);
19389 }
19390 
19391 void cfg80211_ch_switch_notify(struct net_device *dev,
19392 			       struct cfg80211_chan_def *chandef,
19393 			       unsigned int link_id)
19394 {
19395 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19396 	struct wiphy *wiphy = wdev->wiphy;
19397 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19398 
19399 	lockdep_assert_wiphy(wdev->wiphy);
19400 	WARN_INVALID_LINK_ID(wdev, link_id);
19401 
19402 	trace_cfg80211_ch_switch_notify(dev, chandef, link_id);
19403 
19404 	switch (wdev->iftype) {
19405 	case NL80211_IFTYPE_STATION:
19406 	case NL80211_IFTYPE_P2P_CLIENT:
19407 		if (!WARN_ON(!wdev->links[link_id].client.current_bss))
19408 			cfg80211_update_assoc_bss_entry(wdev, link_id,
19409 							chandef->chan);
19410 		break;
19411 	case NL80211_IFTYPE_MESH_POINT:
19412 		wdev->u.mesh.chandef = *chandef;
19413 		wdev->u.mesh.preset_chandef = *chandef;
19414 		break;
19415 	case NL80211_IFTYPE_AP:
19416 	case NL80211_IFTYPE_P2P_GO:
19417 		wdev->links[link_id].ap.chandef = *chandef;
19418 		break;
19419 	case NL80211_IFTYPE_ADHOC:
19420 		wdev->u.ibss.chandef = *chandef;
19421 		break;
19422 	default:
19423 		WARN_ON(1);
19424 		break;
19425 	}
19426 
19427 	cfg80211_schedule_channels_check(wdev);
19428 	cfg80211_sched_dfs_chan_update(rdev);
19429 
19430 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19431 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0, false);
19432 }
19433 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
19434 
19435 void cfg80211_ch_switch_started_notify(struct net_device *dev,
19436 				       struct cfg80211_chan_def *chandef,
19437 				       unsigned int link_id, u8 count,
19438 				       bool quiet)
19439 {
19440 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19441 	struct wiphy *wiphy = wdev->wiphy;
19442 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19443 
19444 	lockdep_assert_wiphy(wdev->wiphy);
19445 	WARN_INVALID_LINK_ID(wdev, link_id);
19446 
19447 	trace_cfg80211_ch_switch_started_notify(dev, chandef, link_id);
19448 
19449 
19450 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19451 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY,
19452 				 count, quiet);
19453 }
19454 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
19455 
19456 int cfg80211_bss_color_notify(struct net_device *dev,
19457 			      enum nl80211_commands cmd, u8 count,
19458 			      u64 color_bitmap)
19459 {
19460 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19461 	struct wiphy *wiphy = wdev->wiphy;
19462 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19463 	struct sk_buff *msg;
19464 	void *hdr;
19465 
19466 	lockdep_assert_wiphy(wdev->wiphy);
19467 
19468 	trace_cfg80211_bss_color_notify(dev, cmd, count, color_bitmap);
19469 
19470 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
19471 	if (!msg)
19472 		return -ENOMEM;
19473 
19474 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
19475 	if (!hdr)
19476 		goto nla_put_failure;
19477 
19478 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19479 		goto nla_put_failure;
19480 
19481 	if (cmd == NL80211_CMD_COLOR_CHANGE_STARTED &&
19482 	    nla_put_u32(msg, NL80211_ATTR_COLOR_CHANGE_COUNT, count))
19483 		goto nla_put_failure;
19484 
19485 	if (cmd == NL80211_CMD_OBSS_COLOR_COLLISION &&
19486 	    nla_put_u64_64bit(msg, NL80211_ATTR_OBSS_COLOR_BITMAP,
19487 			      color_bitmap, NL80211_ATTR_PAD))
19488 		goto nla_put_failure;
19489 
19490 	genlmsg_end(msg, hdr);
19491 
19492 	return genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
19493 				       msg, 0, NL80211_MCGRP_MLME, GFP_KERNEL);
19494 
19495 nla_put_failure:
19496 	nlmsg_free(msg);
19497 	return -EINVAL;
19498 }
19499 EXPORT_SYMBOL(cfg80211_bss_color_notify);
19500 
19501 void
19502 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
19503 		     const struct cfg80211_chan_def *chandef,
19504 		     enum nl80211_radar_event event,
19505 		     struct net_device *netdev, gfp_t gfp)
19506 {
19507 	struct sk_buff *msg;
19508 	void *hdr;
19509 
19510 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19511 	if (!msg)
19512 		return;
19513 
19514 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
19515 	if (!hdr) {
19516 		nlmsg_free(msg);
19517 		return;
19518 	}
19519 
19520 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19521 		goto nla_put_failure;
19522 
19523 	/* NOP and radar events don't need a netdev parameter */
19524 	if (netdev) {
19525 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
19526 
19527 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19528 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19529 				      NL80211_ATTR_PAD))
19530 			goto nla_put_failure;
19531 	}
19532 
19533 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
19534 		goto nla_put_failure;
19535 
19536 	if (nl80211_send_chandef(msg, chandef))
19537 		goto nla_put_failure;
19538 
19539 	genlmsg_end(msg, hdr);
19540 
19541 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19542 				NL80211_MCGRP_MLME, gfp);
19543 	return;
19544 
19545  nla_put_failure:
19546 	nlmsg_free(msg);
19547 }
19548 
19549 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
19550 				       struct sta_opmode_info *sta_opmode,
19551 				       gfp_t gfp)
19552 {
19553 	struct sk_buff *msg;
19554 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19555 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19556 	void *hdr;
19557 
19558 	if (WARN_ON(!mac))
19559 		return;
19560 
19561 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19562 	if (!msg)
19563 		return;
19564 
19565 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
19566 	if (!hdr) {
19567 		nlmsg_free(msg);
19568 		return;
19569 	}
19570 
19571 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19572 		goto nla_put_failure;
19573 
19574 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19575 		goto nla_put_failure;
19576 
19577 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
19578 		goto nla_put_failure;
19579 
19580 	if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
19581 	    nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
19582 		goto nla_put_failure;
19583 
19584 	if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
19585 	    nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
19586 		goto nla_put_failure;
19587 
19588 	if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
19589 	    nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
19590 		goto nla_put_failure;
19591 
19592 	genlmsg_end(msg, hdr);
19593 
19594 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19595 				NL80211_MCGRP_MLME, gfp);
19596 
19597 	return;
19598 
19599 nla_put_failure:
19600 	nlmsg_free(msg);
19601 }
19602 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
19603 
19604 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
19605 			   u64 cookie, bool acked, s32 ack_signal,
19606 			   bool is_valid_ack_signal, gfp_t gfp)
19607 {
19608 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19609 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19610 	struct sk_buff *msg;
19611 	void *hdr;
19612 
19613 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
19614 
19615 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19616 
19617 	if (!msg)
19618 		return;
19619 
19620 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
19621 	if (!hdr) {
19622 		nlmsg_free(msg);
19623 		return;
19624 	}
19625 
19626 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19627 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19628 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
19629 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
19630 			      NL80211_ATTR_PAD) ||
19631 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
19632 	    (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
19633 						ack_signal)))
19634 		goto nla_put_failure;
19635 
19636 	genlmsg_end(msg, hdr);
19637 
19638 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19639 				NL80211_MCGRP_MLME, gfp);
19640 	return;
19641 
19642  nla_put_failure:
19643 	nlmsg_free(msg);
19644 }
19645 EXPORT_SYMBOL(cfg80211_probe_status);
19646 
19647 void cfg80211_report_obss_beacon_khz(struct wiphy *wiphy, const u8 *frame,
19648 				     size_t len, int freq, int sig_dbm)
19649 {
19650 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19651 	struct sk_buff *msg;
19652 	void *hdr;
19653 	struct cfg80211_beacon_registration *reg;
19654 
19655 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
19656 
19657 	spin_lock_bh(&rdev->beacon_registrations_lock);
19658 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
19659 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
19660 		if (!msg) {
19661 			spin_unlock_bh(&rdev->beacon_registrations_lock);
19662 			return;
19663 		}
19664 
19665 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
19666 		if (!hdr)
19667 			goto nla_put_failure;
19668 
19669 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19670 		    (freq &&
19671 		     (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
19672 				  KHZ_TO_MHZ(freq)) ||
19673 		      nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
19674 				  freq % 1000))) ||
19675 		    (sig_dbm &&
19676 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
19677 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
19678 			goto nla_put_failure;
19679 
19680 		genlmsg_end(msg, hdr);
19681 
19682 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
19683 	}
19684 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19685 	return;
19686 
19687  nla_put_failure:
19688 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19689 	nlmsg_free(msg);
19690 }
19691 EXPORT_SYMBOL(cfg80211_report_obss_beacon_khz);
19692 
19693 #ifdef CONFIG_PM
19694 static int cfg80211_net_detect_results(struct sk_buff *msg,
19695 				       struct cfg80211_wowlan_wakeup *wakeup)
19696 {
19697 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
19698 	struct nlattr *nl_results, *nl_match, *nl_freqs;
19699 	int i, j;
19700 
19701 	nl_results = nla_nest_start_noflag(msg,
19702 					   NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
19703 	if (!nl_results)
19704 		return -EMSGSIZE;
19705 
19706 	for (i = 0; i < nd->n_matches; i++) {
19707 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
19708 
19709 		nl_match = nla_nest_start_noflag(msg, i);
19710 		if (!nl_match)
19711 			break;
19712 
19713 		/* The SSID attribute is optional in nl80211, but for
19714 		 * simplicity reasons it's always present in the
19715 		 * cfg80211 structure.  If a driver can't pass the
19716 		 * SSID, that needs to be changed.  A zero length SSID
19717 		 * is still a valid SSID (wildcard), so it cannot be
19718 		 * used for this purpose.
19719 		 */
19720 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
19721 			    match->ssid.ssid)) {
19722 			nla_nest_cancel(msg, nl_match);
19723 			goto out;
19724 		}
19725 
19726 		if (match->n_channels) {
19727 			nl_freqs = nla_nest_start_noflag(msg,
19728 							 NL80211_ATTR_SCAN_FREQUENCIES);
19729 			if (!nl_freqs) {
19730 				nla_nest_cancel(msg, nl_match);
19731 				goto out;
19732 			}
19733 
19734 			for (j = 0; j < match->n_channels; j++) {
19735 				if (nla_put_u32(msg, j, match->channels[j])) {
19736 					nla_nest_cancel(msg, nl_freqs);
19737 					nla_nest_cancel(msg, nl_match);
19738 					goto out;
19739 				}
19740 			}
19741 
19742 			nla_nest_end(msg, nl_freqs);
19743 		}
19744 
19745 		nla_nest_end(msg, nl_match);
19746 	}
19747 
19748 out:
19749 	nla_nest_end(msg, nl_results);
19750 	return 0;
19751 }
19752 
19753 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
19754 				   struct cfg80211_wowlan_wakeup *wakeup,
19755 				   gfp_t gfp)
19756 {
19757 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19758 	struct sk_buff *msg;
19759 	void *hdr;
19760 	int size = 200;
19761 
19762 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
19763 
19764 	if (wakeup)
19765 		size += wakeup->packet_present_len;
19766 
19767 	msg = nlmsg_new(size, gfp);
19768 	if (!msg)
19769 		return;
19770 
19771 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
19772 	if (!hdr)
19773 		goto free_msg;
19774 
19775 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19776 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19777 			      NL80211_ATTR_PAD))
19778 		goto free_msg;
19779 
19780 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
19781 					wdev->netdev->ifindex))
19782 		goto free_msg;
19783 
19784 	if (wakeup) {
19785 		struct nlattr *reasons;
19786 
19787 		reasons = nla_nest_start_noflag(msg,
19788 						NL80211_ATTR_WOWLAN_TRIGGERS);
19789 		if (!reasons)
19790 			goto free_msg;
19791 
19792 		if (wakeup->disconnect &&
19793 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
19794 			goto free_msg;
19795 		if (wakeup->magic_pkt &&
19796 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
19797 			goto free_msg;
19798 		if (wakeup->gtk_rekey_failure &&
19799 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
19800 			goto free_msg;
19801 		if (wakeup->eap_identity_req &&
19802 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
19803 			goto free_msg;
19804 		if (wakeup->four_way_handshake &&
19805 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
19806 			goto free_msg;
19807 		if (wakeup->rfkill_release &&
19808 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
19809 			goto free_msg;
19810 
19811 		if (wakeup->pattern_idx >= 0 &&
19812 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
19813 				wakeup->pattern_idx))
19814 			goto free_msg;
19815 
19816 		if (wakeup->tcp_match &&
19817 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
19818 			goto free_msg;
19819 
19820 		if (wakeup->tcp_connlost &&
19821 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
19822 			goto free_msg;
19823 
19824 		if (wakeup->tcp_nomoretokens &&
19825 		    nla_put_flag(msg,
19826 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
19827 			goto free_msg;
19828 
19829 		if (wakeup->unprot_deauth_disassoc &&
19830 		    nla_put_flag(msg,
19831 				 NL80211_WOWLAN_TRIG_UNPROTECTED_DEAUTH_DISASSOC))
19832 			goto free_msg;
19833 
19834 		if (wakeup->packet) {
19835 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
19836 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
19837 
19838 			if (!wakeup->packet_80211) {
19839 				pkt_attr =
19840 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
19841 				len_attr =
19842 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
19843 			}
19844 
19845 			if (wakeup->packet_len &&
19846 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
19847 				goto free_msg;
19848 
19849 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
19850 				    wakeup->packet))
19851 				goto free_msg;
19852 		}
19853 
19854 		if (wakeup->net_detect &&
19855 		    cfg80211_net_detect_results(msg, wakeup))
19856 				goto free_msg;
19857 
19858 		nla_nest_end(msg, reasons);
19859 	}
19860 
19861 	genlmsg_end(msg, hdr);
19862 
19863 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19864 				NL80211_MCGRP_MLME, gfp);
19865 	return;
19866 
19867  free_msg:
19868 	nlmsg_free(msg);
19869 }
19870 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
19871 #endif
19872 
19873 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
19874 				enum nl80211_tdls_operation oper,
19875 				u16 reason_code, gfp_t gfp)
19876 {
19877 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19878 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19879 	struct sk_buff *msg;
19880 	void *hdr;
19881 
19882 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
19883 					 reason_code);
19884 
19885 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19886 	if (!msg)
19887 		return;
19888 
19889 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
19890 	if (!hdr) {
19891 		nlmsg_free(msg);
19892 		return;
19893 	}
19894 
19895 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19896 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19897 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
19898 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
19899 	    (reason_code > 0 &&
19900 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
19901 		goto nla_put_failure;
19902 
19903 	genlmsg_end(msg, hdr);
19904 
19905 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19906 				NL80211_MCGRP_MLME, gfp);
19907 	return;
19908 
19909  nla_put_failure:
19910 	nlmsg_free(msg);
19911 }
19912 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
19913 
19914 static int nl80211_netlink_notify(struct notifier_block * nb,
19915 				  unsigned long state,
19916 				  void *_notify)
19917 {
19918 	struct netlink_notify *notify = _notify;
19919 	struct cfg80211_registered_device *rdev;
19920 	struct wireless_dev *wdev;
19921 	struct cfg80211_beacon_registration *reg, *tmp;
19922 
19923 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
19924 		return NOTIFY_DONE;
19925 
19926 	rcu_read_lock();
19927 
19928 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
19929 		struct cfg80211_sched_scan_request *sched_scan_req;
19930 
19931 		list_for_each_entry_rcu(sched_scan_req,
19932 					&rdev->sched_scan_req_list,
19933 					list) {
19934 			if (sched_scan_req->owner_nlportid == notify->portid) {
19935 				sched_scan_req->nl_owner_dead = true;
19936 				wiphy_work_queue(&rdev->wiphy,
19937 						 &rdev->sched_scan_stop_wk);
19938 			}
19939 		}
19940 
19941 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
19942 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
19943 
19944 			if (wdev->owner_nlportid == notify->portid) {
19945 				wdev->nl_owner_dead = true;
19946 				schedule_work(&rdev->destroy_work);
19947 			} else if (wdev->conn_owner_nlportid == notify->portid) {
19948 				schedule_work(&wdev->disconnect_wk);
19949 			}
19950 
19951 			cfg80211_release_pmsr(wdev, notify->portid);
19952 		}
19953 
19954 		spin_lock_bh(&rdev->beacon_registrations_lock);
19955 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
19956 					 list) {
19957 			if (reg->nlportid == notify->portid) {
19958 				list_del(&reg->list);
19959 				kfree(reg);
19960 				break;
19961 			}
19962 		}
19963 		spin_unlock_bh(&rdev->beacon_registrations_lock);
19964 	}
19965 
19966 	rcu_read_unlock();
19967 
19968 	/*
19969 	 * It is possible that the user space process that is controlling the
19970 	 * indoor setting disappeared, so notify the regulatory core.
19971 	 */
19972 	regulatory_netlink_notify(notify->portid);
19973 	return NOTIFY_OK;
19974 }
19975 
19976 static struct notifier_block nl80211_netlink_notifier = {
19977 	.notifier_call = nl80211_netlink_notify,
19978 };
19979 
19980 void cfg80211_ft_event(struct net_device *netdev,
19981 		       struct cfg80211_ft_event_params *ft_event)
19982 {
19983 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
19984 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19985 	struct sk_buff *msg;
19986 	void *hdr;
19987 
19988 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
19989 
19990 	if (!ft_event->target_ap)
19991 		return;
19992 
19993 	msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
19994 			GFP_KERNEL);
19995 	if (!msg)
19996 		return;
19997 
19998 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
19999 	if (!hdr)
20000 		goto out;
20001 
20002 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20003 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
20004 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
20005 		goto out;
20006 
20007 	if (ft_event->ies &&
20008 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
20009 		goto out;
20010 	if (ft_event->ric_ies &&
20011 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
20012 		    ft_event->ric_ies))
20013 		goto out;
20014 
20015 	genlmsg_end(msg, hdr);
20016 
20017 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
20018 				NL80211_MCGRP_MLME, GFP_KERNEL);
20019 	return;
20020  out:
20021 	nlmsg_free(msg);
20022 }
20023 EXPORT_SYMBOL(cfg80211_ft_event);
20024 
20025 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
20026 {
20027 	struct cfg80211_registered_device *rdev;
20028 	struct sk_buff *msg;
20029 	void *hdr;
20030 	u32 nlportid;
20031 
20032 	rdev = wiphy_to_rdev(wdev->wiphy);
20033 	if (!rdev->crit_proto_nlportid)
20034 		return;
20035 
20036 	nlportid = rdev->crit_proto_nlportid;
20037 	rdev->crit_proto_nlportid = 0;
20038 
20039 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
20040 	if (!msg)
20041 		return;
20042 
20043 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
20044 	if (!hdr)
20045 		goto nla_put_failure;
20046 
20047 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20048 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
20049 			      NL80211_ATTR_PAD))
20050 		goto nla_put_failure;
20051 
20052 	genlmsg_end(msg, hdr);
20053 
20054 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
20055 	return;
20056 
20057  nla_put_failure:
20058 	nlmsg_free(msg);
20059 }
20060 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
20061 
20062 void nl80211_send_ap_stopped(struct wireless_dev *wdev, unsigned int link_id)
20063 {
20064 	struct wiphy *wiphy = wdev->wiphy;
20065 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
20066 	struct sk_buff *msg;
20067 	void *hdr;
20068 
20069 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
20070 	if (!msg)
20071 		return;
20072 
20073 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
20074 	if (!hdr)
20075 		goto out;
20076 
20077 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20078 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
20079 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
20080 			      NL80211_ATTR_PAD) ||
20081 	    (wdev->valid_links &&
20082 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)))
20083 		goto out;
20084 
20085 	genlmsg_end(msg, hdr);
20086 
20087 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
20088 				NL80211_MCGRP_MLME, GFP_KERNEL);
20089 	return;
20090  out:
20091 	nlmsg_free(msg);
20092 }
20093 
20094 int cfg80211_external_auth_request(struct net_device *dev,
20095 				   struct cfg80211_external_auth_params *params,
20096 				   gfp_t gfp)
20097 {
20098 	struct wireless_dev *wdev = dev->ieee80211_ptr;
20099 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
20100 	struct sk_buff *msg;
20101 	void *hdr;
20102 
20103 	if (!wdev->conn_owner_nlportid)
20104 		return -EINVAL;
20105 
20106 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
20107 	if (!msg)
20108 		return -ENOMEM;
20109 
20110 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
20111 	if (!hdr)
20112 		goto nla_put_failure;
20113 
20114 	/* Some historical mistakes in drivers <-> userspace interface (notably
20115 	 * between drivers and wpa_supplicant) led to a big-endian conversion
20116 	 * being needed on NL80211_ATTR_AKM_SUITES _only_ when its value is
20117 	 * WLAN_AKM_SUITE_SAE. This is now fixed on userspace side, but for the
20118 	 * benefit of older wpa_supplicant versions, send this particular value
20119 	 * in big-endian. Note that newer wpa_supplicant will also detect this
20120 	 * particular value in big endian still, so it all continues to work.
20121 	 */
20122 	if (params->key_mgmt_suite == WLAN_AKM_SUITE_SAE) {
20123 		if (nla_put_be32(msg, NL80211_ATTR_AKM_SUITES,
20124 				 cpu_to_be32(WLAN_AKM_SUITE_SAE)))
20125 			goto nla_put_failure;
20126 	} else {
20127 		if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES,
20128 				params->key_mgmt_suite))
20129 			goto nla_put_failure;
20130 	}
20131 
20132 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20133 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
20134 	    nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
20135 			params->action) ||
20136 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
20137 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
20138 		    params->ssid.ssid) ||
20139 	    (!is_zero_ether_addr(params->mld_addr) &&
20140 	     nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN, params->mld_addr)))
20141 		goto nla_put_failure;
20142 
20143 	genlmsg_end(msg, hdr);
20144 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
20145 			wdev->conn_owner_nlportid);
20146 	return 0;
20147 
20148  nla_put_failure:
20149 	nlmsg_free(msg);
20150 	return -ENOBUFS;
20151 }
20152 EXPORT_SYMBOL(cfg80211_external_auth_request);
20153 
20154 void cfg80211_update_owe_info_event(struct net_device *netdev,
20155 				    struct cfg80211_update_owe_info *owe_info,
20156 				    gfp_t gfp)
20157 {
20158 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
20159 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
20160 	struct sk_buff *msg;
20161 	void *hdr;
20162 
20163 	trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
20164 
20165 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
20166 	if (!msg)
20167 		return;
20168 
20169 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
20170 	if (!hdr)
20171 		goto nla_put_failure;
20172 
20173 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20174 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
20175 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
20176 		goto nla_put_failure;
20177 
20178 	if (!owe_info->ie_len ||
20179 	    nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
20180 		goto nla_put_failure;
20181 
20182 	if (owe_info->assoc_link_id != -1) {
20183 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
20184 			       owe_info->assoc_link_id))
20185 			goto nla_put_failure;
20186 
20187 		if (!is_zero_ether_addr(owe_info->peer_mld_addr) &&
20188 		    nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
20189 			    owe_info->peer_mld_addr))
20190 			goto nla_put_failure;
20191 	}
20192 
20193 	genlmsg_end(msg, hdr);
20194 
20195 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
20196 				NL80211_MCGRP_MLME, gfp);
20197 	return;
20198 
20199 nla_put_failure:
20200 	genlmsg_cancel(msg, hdr);
20201 	nlmsg_free(msg);
20202 }
20203 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
20204 
20205 void cfg80211_schedule_channels_check(struct wireless_dev *wdev)
20206 {
20207 	struct wiphy *wiphy = wdev->wiphy;
20208 
20209 	/* Schedule channels check if NO_IR or DFS relaxations are supported */
20210 	if (wdev->iftype == NL80211_IFTYPE_STATION &&
20211 	    (wiphy_ext_feature_isset(wiphy,
20212 				     NL80211_EXT_FEATURE_DFS_CONCURRENT) ||
20213 	    (IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) &&
20214 	     wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR)))
20215 		reg_check_channels();
20216 }
20217 EXPORT_SYMBOL(cfg80211_schedule_channels_check);
20218 
20219 /* initialisation/exit functions */
20220 
20221 int __init nl80211_init(void)
20222 {
20223 	int err;
20224 
20225 	err = genl_register_family(&nl80211_fam);
20226 	if (err)
20227 		return err;
20228 
20229 	err = netlink_register_notifier(&nl80211_netlink_notifier);
20230 	if (err)
20231 		goto err_out;
20232 
20233 	return 0;
20234  err_out:
20235 	genl_unregister_family(&nl80211_fam);
20236 	return err;
20237 }
20238 
20239 void nl80211_exit(void)
20240 {
20241 	netlink_unregister_notifier(&nl80211_netlink_notifier);
20242 	genl_unregister_family(&nl80211_fam);
20243 }
20244