xref: /openbmc/linux/net/wireless/nl80211.c (revision 3ddc8b84)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This is the new netlink-based wireless configuration interface.
4  *
5  * Copyright 2006-2010	Johannes Berg <johannes@sipsolutions.net>
6  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  * Copyright 2015-2017	Intel Deutschland GmbH
8  * Copyright (C) 2018-2023 Intel Corporation
9  */
10 
11 #include <linux/if.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/list.h>
16 #include <linux/if_ether.h>
17 #include <linux/ieee80211.h>
18 #include <linux/nl80211.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/netlink.h>
21 #include <linux/nospec.h>
22 #include <linux/etherdevice.h>
23 #include <linux/if_vlan.h>
24 #include <net/net_namespace.h>
25 #include <net/genetlink.h>
26 #include <net/cfg80211.h>
27 #include <net/sock.h>
28 #include <net/inet_connection_sock.h>
29 #include "core.h"
30 #include "nl80211.h"
31 #include "reg.h"
32 #include "rdev-ops.h"
33 
34 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
35 				   struct genl_info *info,
36 				   struct cfg80211_crypto_settings *settings,
37 				   int cipher_limit);
38 
39 /* the netlink family */
40 static struct genl_family nl80211_fam;
41 
42 /* multicast groups */
43 enum nl80211_multicast_groups {
44 	NL80211_MCGRP_CONFIG,
45 	NL80211_MCGRP_SCAN,
46 	NL80211_MCGRP_REGULATORY,
47 	NL80211_MCGRP_MLME,
48 	NL80211_MCGRP_VENDOR,
49 	NL80211_MCGRP_NAN,
50 	NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
51 };
52 
53 static const struct genl_multicast_group nl80211_mcgrps[] = {
54 	[NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
55 	[NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
56 	[NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
57 	[NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
58 	[NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
59 	[NL80211_MCGRP_NAN] = { .name = NL80211_MULTICAST_GROUP_NAN },
60 #ifdef CONFIG_NL80211_TESTMODE
61 	[NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
62 #endif
63 };
64 
65 /* returns ERR_PTR values */
66 static struct wireless_dev *
67 __cfg80211_wdev_from_attrs(struct cfg80211_registered_device *rdev,
68 			   struct net *netns, struct nlattr **attrs)
69 {
70 	struct wireless_dev *result = NULL;
71 	bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
72 	bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
73 	u64 wdev_id = 0;
74 	int wiphy_idx = -1;
75 	int ifidx = -1;
76 
77 	if (!have_ifidx && !have_wdev_id)
78 		return ERR_PTR(-EINVAL);
79 
80 	if (have_ifidx)
81 		ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
82 	if (have_wdev_id) {
83 		wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
84 		wiphy_idx = wdev_id >> 32;
85 	}
86 
87 	if (rdev) {
88 		struct wireless_dev *wdev;
89 
90 		lockdep_assert_held(&rdev->wiphy.mtx);
91 
92 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
93 			if (have_ifidx && wdev->netdev &&
94 			    wdev->netdev->ifindex == ifidx) {
95 				result = wdev;
96 				break;
97 			}
98 			if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
99 				result = wdev;
100 				break;
101 			}
102 		}
103 
104 		return result ?: ERR_PTR(-ENODEV);
105 	}
106 
107 	ASSERT_RTNL();
108 
109 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
110 		struct wireless_dev *wdev;
111 
112 		if (wiphy_net(&rdev->wiphy) != netns)
113 			continue;
114 
115 		if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
116 			continue;
117 
118 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
119 			if (have_ifidx && wdev->netdev &&
120 			    wdev->netdev->ifindex == ifidx) {
121 				result = wdev;
122 				break;
123 			}
124 			if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
125 				result = wdev;
126 				break;
127 			}
128 		}
129 
130 		if (result)
131 			break;
132 	}
133 
134 	if (result)
135 		return result;
136 	return ERR_PTR(-ENODEV);
137 }
138 
139 static struct cfg80211_registered_device *
140 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
141 {
142 	struct cfg80211_registered_device *rdev = NULL, *tmp;
143 	struct net_device *netdev;
144 
145 	ASSERT_RTNL();
146 
147 	if (!attrs[NL80211_ATTR_WIPHY] &&
148 	    !attrs[NL80211_ATTR_IFINDEX] &&
149 	    !attrs[NL80211_ATTR_WDEV])
150 		return ERR_PTR(-EINVAL);
151 
152 	if (attrs[NL80211_ATTR_WIPHY])
153 		rdev = cfg80211_rdev_by_wiphy_idx(
154 				nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
155 
156 	if (attrs[NL80211_ATTR_WDEV]) {
157 		u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
158 		struct wireless_dev *wdev;
159 		bool found = false;
160 
161 		tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
162 		if (tmp) {
163 			/* make sure wdev exists */
164 			list_for_each_entry(wdev, &tmp->wiphy.wdev_list, list) {
165 				if (wdev->identifier != (u32)wdev_id)
166 					continue;
167 				found = true;
168 				break;
169 			}
170 
171 			if (!found)
172 				tmp = NULL;
173 
174 			if (rdev && tmp != rdev)
175 				return ERR_PTR(-EINVAL);
176 			rdev = tmp;
177 		}
178 	}
179 
180 	if (attrs[NL80211_ATTR_IFINDEX]) {
181 		int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
182 
183 		netdev = __dev_get_by_index(netns, ifindex);
184 		if (netdev) {
185 			if (netdev->ieee80211_ptr)
186 				tmp = wiphy_to_rdev(
187 					netdev->ieee80211_ptr->wiphy);
188 			else
189 				tmp = NULL;
190 
191 			/* not wireless device -- return error */
192 			if (!tmp)
193 				return ERR_PTR(-EINVAL);
194 
195 			/* mismatch -- return error */
196 			if (rdev && tmp != rdev)
197 				return ERR_PTR(-EINVAL);
198 
199 			rdev = tmp;
200 		}
201 	}
202 
203 	if (!rdev)
204 		return ERR_PTR(-ENODEV);
205 
206 	if (netns != wiphy_net(&rdev->wiphy))
207 		return ERR_PTR(-ENODEV);
208 
209 	return rdev;
210 }
211 
212 /*
213  * This function returns a pointer to the driver
214  * that the genl_info item that is passed refers to.
215  *
216  * The result of this can be a PTR_ERR and hence must
217  * be checked with IS_ERR() for errors.
218  */
219 static struct cfg80211_registered_device *
220 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
221 {
222 	return __cfg80211_rdev_from_attrs(netns, info->attrs);
223 }
224 
225 static int validate_beacon_head(const struct nlattr *attr,
226 				struct netlink_ext_ack *extack)
227 {
228 	const u8 *data = nla_data(attr);
229 	unsigned int len = nla_len(attr);
230 	const struct element *elem;
231 	const struct ieee80211_mgmt *mgmt = (void *)data;
232 	unsigned int fixedlen, hdrlen;
233 	bool s1g_bcn;
234 
235 	if (len < offsetofend(typeof(*mgmt), frame_control))
236 		goto err;
237 
238 	s1g_bcn = ieee80211_is_s1g_beacon(mgmt->frame_control);
239 	if (s1g_bcn) {
240 		fixedlen = offsetof(struct ieee80211_ext,
241 				    u.s1g_beacon.variable);
242 		hdrlen = offsetof(struct ieee80211_ext, u.s1g_beacon);
243 	} else {
244 		fixedlen = offsetof(struct ieee80211_mgmt,
245 				    u.beacon.variable);
246 		hdrlen = offsetof(struct ieee80211_mgmt, u.beacon);
247 	}
248 
249 	if (len < fixedlen)
250 		goto err;
251 
252 	if (ieee80211_hdrlen(mgmt->frame_control) != hdrlen)
253 		goto err;
254 
255 	data += fixedlen;
256 	len -= fixedlen;
257 
258 	for_each_element(elem, data, len) {
259 		/* nothing */
260 	}
261 
262 	if (for_each_element_completed(elem, data, len))
263 		return 0;
264 
265 err:
266 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed beacon head");
267 	return -EINVAL;
268 }
269 
270 static int validate_ie_attr(const struct nlattr *attr,
271 			    struct netlink_ext_ack *extack)
272 {
273 	const u8 *data = nla_data(attr);
274 	unsigned int len = nla_len(attr);
275 	const struct element *elem;
276 
277 	for_each_element(elem, data, len) {
278 		/* nothing */
279 	}
280 
281 	if (for_each_element_completed(elem, data, len))
282 		return 0;
283 
284 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed information elements");
285 	return -EINVAL;
286 }
287 
288 static int validate_he_capa(const struct nlattr *attr,
289 			    struct netlink_ext_ack *extack)
290 {
291 	if (!ieee80211_he_capa_size_ok(nla_data(attr), nla_len(attr)))
292 		return -EINVAL;
293 
294 	return 0;
295 }
296 
297 /* policy for the attributes */
298 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR];
299 
300 static const struct nla_policy
301 nl80211_ftm_responder_policy[NL80211_FTM_RESP_ATTR_MAX + 1] = {
302 	[NL80211_FTM_RESP_ATTR_ENABLED] = { .type = NLA_FLAG, },
303 	[NL80211_FTM_RESP_ATTR_LCI] = { .type = NLA_BINARY,
304 					.len = U8_MAX },
305 	[NL80211_FTM_RESP_ATTR_CIVICLOC] = { .type = NLA_BINARY,
306 					     .len = U8_MAX },
307 };
308 
309 static const struct nla_policy
310 nl80211_pmsr_ftm_req_attr_policy[NL80211_PMSR_FTM_REQ_ATTR_MAX + 1] = {
311 	[NL80211_PMSR_FTM_REQ_ATTR_ASAP] = { .type = NLA_FLAG },
312 	[NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE] = { .type = NLA_U32 },
313 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP] =
314 		NLA_POLICY_MAX(NLA_U8, 15),
315 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD] = { .type = NLA_U16 },
316 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION] =
317 		NLA_POLICY_MAX(NLA_U8, 15),
318 	[NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST] =
319 		NLA_POLICY_MAX(NLA_U8, 31),
320 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES] = { .type = NLA_U8 },
321 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI] = { .type = NLA_FLAG },
322 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC] = { .type = NLA_FLAG },
323 	[NL80211_PMSR_FTM_REQ_ATTR_TRIGGER_BASED] = { .type = NLA_FLAG },
324 	[NL80211_PMSR_FTM_REQ_ATTR_NON_TRIGGER_BASED] = { .type = NLA_FLAG },
325 	[NL80211_PMSR_FTM_REQ_ATTR_LMR_FEEDBACK] = { .type = NLA_FLAG },
326 	[NL80211_PMSR_FTM_REQ_ATTR_BSS_COLOR] = { .type = NLA_U8 },
327 };
328 
329 static const struct nla_policy
330 nl80211_pmsr_req_data_policy[NL80211_PMSR_TYPE_MAX + 1] = {
331 	[NL80211_PMSR_TYPE_FTM] =
332 		NLA_POLICY_NESTED(nl80211_pmsr_ftm_req_attr_policy),
333 };
334 
335 static const struct nla_policy
336 nl80211_pmsr_req_attr_policy[NL80211_PMSR_REQ_ATTR_MAX + 1] = {
337 	[NL80211_PMSR_REQ_ATTR_DATA] =
338 		NLA_POLICY_NESTED(nl80211_pmsr_req_data_policy),
339 	[NL80211_PMSR_REQ_ATTR_GET_AP_TSF] = { .type = NLA_FLAG },
340 };
341 
342 static const struct nla_policy
343 nl80211_pmsr_peer_attr_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
344 	[NL80211_PMSR_PEER_ATTR_ADDR] = NLA_POLICY_ETH_ADDR,
345 	[NL80211_PMSR_PEER_ATTR_CHAN] = NLA_POLICY_NESTED(nl80211_policy),
346 	[NL80211_PMSR_PEER_ATTR_REQ] =
347 		NLA_POLICY_NESTED(nl80211_pmsr_req_attr_policy),
348 	[NL80211_PMSR_PEER_ATTR_RESP] = { .type = NLA_REJECT },
349 };
350 
351 static const struct nla_policy
352 nl80211_pmsr_attr_policy[NL80211_PMSR_ATTR_MAX + 1] = {
353 	[NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_REJECT },
354 	[NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_REJECT },
355 	[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
356 	[NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
357 	[NL80211_PMSR_ATTR_PEERS] =
358 		NLA_POLICY_NESTED_ARRAY(nl80211_pmsr_peer_attr_policy),
359 };
360 
361 static const struct nla_policy
362 he_obss_pd_policy[NL80211_HE_OBSS_PD_ATTR_MAX + 1] = {
363 	[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] =
364 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
365 	[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET] =
366 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
367 	[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET] =
368 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
369 	[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP] =
370 		NLA_POLICY_EXACT_LEN(8),
371 	[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP] =
372 		NLA_POLICY_EXACT_LEN(8),
373 	[NL80211_HE_OBSS_PD_ATTR_SR_CTRL] = { .type = NLA_U8 },
374 };
375 
376 static const struct nla_policy
377 he_bss_color_policy[NL80211_HE_BSS_COLOR_ATTR_MAX + 1] = {
378 	[NL80211_HE_BSS_COLOR_ATTR_COLOR] = NLA_POLICY_RANGE(NLA_U8, 1, 63),
379 	[NL80211_HE_BSS_COLOR_ATTR_DISABLED] = { .type = NLA_FLAG },
380 	[NL80211_HE_BSS_COLOR_ATTR_PARTIAL] = { .type = NLA_FLAG },
381 };
382 
383 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
384 	[NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
385 				    .len = NL80211_MAX_SUPP_RATES },
386 	[NL80211_TXRATE_HT] = { .type = NLA_BINARY,
387 				.len = NL80211_MAX_SUPP_HT_RATES },
388 	[NL80211_TXRATE_VHT] = NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_txrate_vht)),
389 	[NL80211_TXRATE_GI] = { .type = NLA_U8 },
390 	[NL80211_TXRATE_HE] = NLA_POLICY_EXACT_LEN(sizeof(struct nl80211_txrate_he)),
391 	[NL80211_TXRATE_HE_GI] =  NLA_POLICY_RANGE(NLA_U8,
392 						   NL80211_RATE_INFO_HE_GI_0_8,
393 						   NL80211_RATE_INFO_HE_GI_3_2),
394 	[NL80211_TXRATE_HE_LTF] = NLA_POLICY_RANGE(NLA_U8,
395 						   NL80211_RATE_INFO_HE_1XLTF,
396 						   NL80211_RATE_INFO_HE_4XLTF),
397 };
398 
399 static const struct nla_policy
400 nl80211_tid_config_attr_policy[NL80211_TID_CONFIG_ATTR_MAX + 1] = {
401 	[NL80211_TID_CONFIG_ATTR_VIF_SUPP] = { .type = NLA_U64 },
402 	[NL80211_TID_CONFIG_ATTR_PEER_SUPP] = { .type = NLA_U64 },
403 	[NL80211_TID_CONFIG_ATTR_OVERRIDE] = { .type = NLA_FLAG },
404 	[NL80211_TID_CONFIG_ATTR_TIDS] = NLA_POLICY_RANGE(NLA_U16, 1, 0xff),
405 	[NL80211_TID_CONFIG_ATTR_NOACK] =
406 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
407 	[NL80211_TID_CONFIG_ATTR_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
408 	[NL80211_TID_CONFIG_ATTR_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
409 	[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL] =
410 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
411 	[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL] =
412 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
413 	[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL] =
414 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
415 	[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE] =
416 			NLA_POLICY_MAX(NLA_U8, NL80211_TX_RATE_FIXED),
417 	[NL80211_TID_CONFIG_ATTR_TX_RATE] =
418 			NLA_POLICY_NESTED(nl80211_txattr_policy),
419 };
420 
421 static const struct nla_policy
422 nl80211_fils_discovery_policy[NL80211_FILS_DISCOVERY_ATTR_MAX + 1] = {
423 	[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] = NLA_POLICY_MAX(NLA_U32, 10000),
424 	[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] = NLA_POLICY_MAX(NLA_U32, 10000),
425 	[NL80211_FILS_DISCOVERY_ATTR_TMPL] =
426 			NLA_POLICY_RANGE(NLA_BINARY,
427 					 NL80211_FILS_DISCOVERY_TMPL_MIN_LEN,
428 					 IEEE80211_MAX_DATA_LEN),
429 };
430 
431 static const struct nla_policy
432 nl80211_unsol_bcast_probe_resp_policy[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1] = {
433 	[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] = NLA_POLICY_MAX(NLA_U32, 20),
434 	[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL] = { .type = NLA_BINARY,
435 						       .len = IEEE80211_MAX_DATA_LEN }
436 };
437 
438 static const struct nla_policy
439 sar_specs_policy[NL80211_SAR_ATTR_SPECS_MAX + 1] = {
440 	[NL80211_SAR_ATTR_SPECS_POWER] = { .type = NLA_S32 },
441 	[NL80211_SAR_ATTR_SPECS_RANGE_INDEX] = {.type = NLA_U32 },
442 };
443 
444 static const struct nla_policy
445 sar_policy[NL80211_SAR_ATTR_MAX + 1] = {
446 	[NL80211_SAR_ATTR_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_SAR_TYPE),
447 	[NL80211_SAR_ATTR_SPECS] = NLA_POLICY_NESTED_ARRAY(sar_specs_policy),
448 };
449 
450 static const struct nla_policy
451 nl80211_mbssid_config_policy[NL80211_MBSSID_CONFIG_ATTR_MAX + 1] = {
452 	[NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES] = NLA_POLICY_MIN(NLA_U8, 2),
453 	[NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY] =
454 						NLA_POLICY_MIN(NLA_U8, 1),
455 	[NL80211_MBSSID_CONFIG_ATTR_INDEX] = { .type = NLA_U8 },
456 	[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX] = { .type = NLA_U32 },
457 	[NL80211_MBSSID_CONFIG_ATTR_EMA] = { .type = NLA_FLAG },
458 };
459 
460 static const struct nla_policy
461 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
462 	[NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
463 	[NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
464 };
465 
466 static struct netlink_range_validation nl80211_punct_bitmap_range = {
467 	.min = 0,
468 	.max = 0xffff,
469 };
470 
471 static 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] = { .type = NLA_U32 },
585 	[NL80211_ATTR_PID] = { .type = NLA_U32 },
586 	[NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
587 	[NL80211_ATTR_PMKID] = NLA_POLICY_EXACT_LEN_WARN(WLAN_PMKID_LEN),
588 	[NL80211_ATTR_DURATION] = { .type = NLA_U32 },
589 	[NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
590 	[NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
591 	[NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
592 				 .len = IEEE80211_MAX_DATA_LEN },
593 	[NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
594 	[NL80211_ATTR_PS_STATE] = NLA_POLICY_RANGE(NLA_U32,
595 						   NL80211_PS_DISABLED,
596 						   NL80211_PS_ENABLED),
597 	[NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
598 	[NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
599 	[NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
600 	[NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
601 	[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
602 	[NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
603 	[NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
604 	[NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
605 	[NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
606 	[NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
607 	[NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
608 	[NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
609 	[NL80211_ATTR_STA_PLINK_STATE] =
610 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_STATES - 1),
611 	[NL80211_ATTR_MEASUREMENT_DURATION] = { .type = NLA_U16 },
612 	[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY] = { .type = NLA_FLAG },
613 	[NL80211_ATTR_MESH_PEER_AID] =
614 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
615 	[NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
616 	[NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
617 	[NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
618 	[NL80211_ATTR_HIDDEN_SSID] =
619 		NLA_POLICY_RANGE(NLA_U32,
620 				 NL80211_HIDDEN_SSID_NOT_IN_USE,
621 				 NL80211_HIDDEN_SSID_ZERO_CONTENTS),
622 	[NL80211_ATTR_IE_PROBE_RESP] =
623 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
624 				       IEEE80211_MAX_DATA_LEN),
625 	[NL80211_ATTR_IE_ASSOC_RESP] =
626 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
627 				       IEEE80211_MAX_DATA_LEN),
628 	[NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
629 	[NL80211_ATTR_STA_WME] = NLA_POLICY_NESTED(nl80211_sta_wme_policy),
630 	[NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
631 	[NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
632 	[NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
633 	[NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
634 	[NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
635 	[NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
636 	[NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
637 	[NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
638 	[NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
639 	[NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
640 				      .len = IEEE80211_MAX_DATA_LEN },
641 	[NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
642 	[NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
643 	[NL80211_ATTR_HT_CAPABILITY_MASK] = {
644 		.len = NL80211_HT_CAPABILITY_LEN
645 	},
646 	[NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
647 	[NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
648 	[NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
649 	[NL80211_ATTR_WDEV] = { .type = NLA_U64 },
650 	[NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
651 
652 	/* need to include at least Auth Transaction and Status Code */
653 	[NL80211_ATTR_AUTH_DATA] = NLA_POLICY_MIN_LEN(4),
654 
655 	[NL80211_ATTR_VHT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_VHT_CAPABILITY_LEN),
656 	[NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
657 	[NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
658 	[NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
659 	[NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
660 		NLA_POLICY_RANGE(NLA_U32,
661 				 NL80211_MESH_POWER_UNKNOWN + 1,
662 				 NL80211_MESH_POWER_MAX),
663 	[NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
664 	[NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
665 	[NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
666 	[NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
667 	[NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
668 	[NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
669 	[NL80211_ATTR_VHT_CAPABILITY_MASK] = {
670 		.len = NL80211_VHT_CAPABILITY_LEN,
671 	},
672 	[NL80211_ATTR_MDID] = { .type = NLA_U16 },
673 	[NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
674 				  .len = IEEE80211_MAX_DATA_LEN },
675 	[NL80211_ATTR_CRIT_PROT_ID] = { .type = NLA_U16 },
676 	[NL80211_ATTR_MAX_CRIT_PROT_DURATION] =
677 		NLA_POLICY_MAX(NLA_U16, NL80211_CRIT_PROTO_MAX_DURATION),
678 	[NL80211_ATTR_PEER_AID] =
679 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
680 	[NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
681 	[NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
682 	[NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
683 	[NL80211_ATTR_CNTDWN_OFFS_BEACON] = { .type = NLA_BINARY },
684 	[NL80211_ATTR_CNTDWN_OFFS_PRESP] = { .type = NLA_BINARY },
685 	[NL80211_ATTR_STA_SUPPORTED_CHANNELS] = NLA_POLICY_MIN_LEN(2),
686 	/*
687 	 * The value of the Length field of the Supported Operating
688 	 * Classes element is between 2 and 253.
689 	 */
690 	[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] =
691 		NLA_POLICY_RANGE(NLA_BINARY, 2, 253),
692 	[NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
693 	[NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
694 	[NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
695 	[NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
696 	[NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
697 	[NL80211_ATTR_QOS_MAP] = NLA_POLICY_RANGE(NLA_BINARY,
698 						  IEEE80211_QOS_MAP_LEN_MIN,
699 						  IEEE80211_QOS_MAP_LEN_MAX),
700 	[NL80211_ATTR_MAC_HINT] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
701 	[NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
702 	[NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
703 	[NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
704 	[NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
705 	[NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
706 	[NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
707 	[NL80211_ATTR_USER_PRIO] =
708 		NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
709 	[NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
710 	[NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
711 	[NL80211_ATTR_OPER_CLASS] = { .type = NLA_U8 },
712 	[NL80211_ATTR_MAC_MASK] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
713 	[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
714 	[NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
715 	[NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
716 	[NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
717 	[NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
718 	[NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
719 	[NL80211_ATTR_STA_SUPPORT_P2P_PS] =
720 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
721 	[NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
722 		.len = VHT_MUMIMO_GROUPS_DATA_LEN
723 	},
724 	[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
725 	[NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
726 	[NL80211_ATTR_BANDS] = { .type = NLA_U32 },
727 	[NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
728 	[NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
729 				    .len = FILS_MAX_KEK_LEN },
730 	[NL80211_ATTR_FILS_NONCES] = NLA_POLICY_EXACT_LEN_WARN(2 * FILS_NONCE_LEN),
731 	[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
732 	[NL80211_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
733 	[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
734 	[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
735 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
736 	},
737 	[NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
738 	[NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
739 					     .len = FILS_ERP_MAX_USERNAME_LEN },
740 	[NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
741 					  .len = FILS_ERP_MAX_REALM_LEN },
742 	[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
743 	[NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
744 					.len = FILS_ERP_MAX_RRK_LEN },
745 	[NL80211_ATTR_FILS_CACHE_ID] = NLA_POLICY_EXACT_LEN_WARN(2),
746 	[NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
747 	[NL80211_ATTR_PMKR0_NAME] = NLA_POLICY_EXACT_LEN(WLAN_PMK_NAME_LEN),
748 	[NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
749 	[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
750 
751 	[NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
752 	[NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
753 	[NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
754 	[NL80211_ATTR_HE_CAPABILITY] =
755 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_he_capa,
756 				       NL80211_HE_MAX_CAPABILITY_LEN),
757 	[NL80211_ATTR_FTM_RESPONDER] =
758 		NLA_POLICY_NESTED(nl80211_ftm_responder_policy),
759 	[NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
760 	[NL80211_ATTR_PEER_MEASUREMENTS] =
761 		NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
762 	[NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
763 	[NL80211_ATTR_SAE_PASSWORD] = { .type = NLA_BINARY,
764 					.len = SAE_PASSWORD_MAX_LEN },
765 	[NL80211_ATTR_TWT_RESPONDER] = { .type = NLA_FLAG },
766 	[NL80211_ATTR_HE_OBSS_PD] = NLA_POLICY_NESTED(he_obss_pd_policy),
767 	[NL80211_ATTR_VLAN_ID] = NLA_POLICY_RANGE(NLA_U16, 1, VLAN_N_VID - 2),
768 	[NL80211_ATTR_HE_BSS_COLOR] = NLA_POLICY_NESTED(he_bss_color_policy),
769 	[NL80211_ATTR_TID_CONFIG] =
770 		NLA_POLICY_NESTED_ARRAY(nl80211_tid_config_attr_policy),
771 	[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH] = { .type = NLA_FLAG },
772 	[NL80211_ATTR_PMK_LIFETIME] = NLA_POLICY_MIN(NLA_U32, 1),
773 	[NL80211_ATTR_PMK_REAUTH_THRESHOLD] = NLA_POLICY_RANGE(NLA_U8, 1, 100),
774 	[NL80211_ATTR_RECEIVE_MULTICAST] = { .type = NLA_FLAG },
775 	[NL80211_ATTR_WIPHY_FREQ_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
776 	[NL80211_ATTR_SCAN_FREQ_KHZ] = { .type = NLA_NESTED },
777 	[NL80211_ATTR_HE_6GHZ_CAPABILITY] =
778 		NLA_POLICY_EXACT_LEN(sizeof(struct ieee80211_he_6ghz_capa)),
779 	[NL80211_ATTR_FILS_DISCOVERY] =
780 		NLA_POLICY_NESTED(nl80211_fils_discovery_policy),
781 	[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP] =
782 		NLA_POLICY_NESTED(nl80211_unsol_bcast_probe_resp_policy),
783 	[NL80211_ATTR_S1G_CAPABILITY] =
784 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
785 	[NL80211_ATTR_S1G_CAPABILITY_MASK] =
786 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
787 	[NL80211_ATTR_SAE_PWE] =
788 		NLA_POLICY_RANGE(NLA_U8, NL80211_SAE_PWE_HUNT_AND_PECK,
789 				 NL80211_SAE_PWE_BOTH),
790 	[NL80211_ATTR_RECONNECT_REQUESTED] = { .type = NLA_REJECT },
791 	[NL80211_ATTR_SAR_SPEC] = NLA_POLICY_NESTED(sar_policy),
792 	[NL80211_ATTR_DISABLE_HE] = { .type = NLA_FLAG },
793 	[NL80211_ATTR_OBSS_COLOR_BITMAP] = { .type = NLA_U64 },
794 	[NL80211_ATTR_COLOR_CHANGE_COUNT] = { .type = NLA_U8 },
795 	[NL80211_ATTR_COLOR_CHANGE_COLOR] = { .type = NLA_U8 },
796 	[NL80211_ATTR_COLOR_CHANGE_ELEMS] = NLA_POLICY_NESTED(nl80211_policy),
797 	[NL80211_ATTR_MBSSID_CONFIG] =
798 			NLA_POLICY_NESTED(nl80211_mbssid_config_policy),
799 	[NL80211_ATTR_MBSSID_ELEMS] = { .type = NLA_NESTED },
800 	[NL80211_ATTR_RADAR_BACKGROUND] = { .type = NLA_FLAG },
801 	[NL80211_ATTR_AP_SETTINGS_FLAGS] = { .type = NLA_U32 },
802 	[NL80211_ATTR_EHT_CAPABILITY] =
803 		NLA_POLICY_RANGE(NLA_BINARY,
804 				 NL80211_EHT_MIN_CAPABILITY_LEN,
805 				 NL80211_EHT_MAX_CAPABILITY_LEN),
806 	[NL80211_ATTR_DISABLE_EHT] = { .type = NLA_FLAG },
807 	[NL80211_ATTR_MLO_LINKS] =
808 		NLA_POLICY_NESTED_ARRAY(nl80211_policy),
809 	[NL80211_ATTR_MLO_LINK_ID] =
810 		NLA_POLICY_RANGE(NLA_U8, 0, IEEE80211_MLD_MAX_NUM_LINKS),
811 	[NL80211_ATTR_MLD_ADDR] = NLA_POLICY_EXACT_LEN(ETH_ALEN),
812 	[NL80211_ATTR_MLO_SUPPORT] = { .type = NLA_FLAG },
813 	[NL80211_ATTR_MAX_NUM_AKM_SUITES] = { .type = NLA_REJECT },
814 	[NL80211_ATTR_PUNCT_BITMAP] =
815 		NLA_POLICY_FULL_RANGE(NLA_U32, &nl80211_punct_bitmap_range),
816 
817 	[NL80211_ATTR_MAX_HW_TIMESTAMP_PEERS] = { .type = NLA_U16 },
818 	[NL80211_ATTR_HW_TIMESTAMP_ENABLED] = { .type = NLA_FLAG },
819 	[NL80211_ATTR_EMA_RNR_ELEMS] = { .type = NLA_NESTED },
820 	[NL80211_ATTR_MLO_LINK_DISABLED] = { .type = NLA_FLAG },
821 };
822 
823 /* policy for the key attributes */
824 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
825 	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
826 	[NL80211_KEY_IDX] = { .type = NLA_U8 },
827 	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
828 	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
829 	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
830 	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
831 	[NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
832 	[NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
833 	[NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
834 };
835 
836 /* policy for the key default flags */
837 static const struct nla_policy
838 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
839 	[NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
840 	[NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
841 };
842 
843 #ifdef CONFIG_PM
844 /* policy for WoWLAN attributes */
845 static const struct nla_policy
846 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
847 	[NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
848 	[NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
849 	[NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
850 	[NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
851 	[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
852 	[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
853 	[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
854 	[NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
855 	[NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
856 	[NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
857 };
858 
859 static const struct nla_policy
860 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
861 	[NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
862 	[NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
863 	[NL80211_WOWLAN_TCP_DST_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
864 	[NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
865 	[NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
866 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
867 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
868 		.len = sizeof(struct nl80211_wowlan_tcp_data_seq)
869 	},
870 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
871 		.len = sizeof(struct nl80211_wowlan_tcp_data_token)
872 	},
873 	[NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
874 	[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
875 	[NL80211_WOWLAN_TCP_WAKE_MASK] = NLA_POLICY_MIN_LEN(1),
876 };
877 #endif /* CONFIG_PM */
878 
879 /* policy for coalesce rule attributes */
880 static const struct nla_policy
881 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
882 	[NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
883 	[NL80211_ATTR_COALESCE_RULE_CONDITION] =
884 		NLA_POLICY_RANGE(NLA_U32,
885 				 NL80211_COALESCE_CONDITION_MATCH,
886 				 NL80211_COALESCE_CONDITION_NO_MATCH),
887 	[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
888 };
889 
890 /* policy for GTK rekey offload attributes */
891 static const struct nla_policy
892 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
893 	[NL80211_REKEY_DATA_KEK] = {
894 		.type = NLA_BINARY,
895 		.len = NL80211_KEK_EXT_LEN
896 	},
897 	[NL80211_REKEY_DATA_KCK] = {
898 		.type = NLA_BINARY,
899 		.len = NL80211_KCK_EXT_LEN_32
900 	},
901 	[NL80211_REKEY_DATA_REPLAY_CTR] = NLA_POLICY_EXACT_LEN(NL80211_REPLAY_CTR_LEN),
902 	[NL80211_REKEY_DATA_AKM] = { .type = NLA_U32 },
903 };
904 
905 static const struct nla_policy
906 nl80211_match_band_rssi_policy[NUM_NL80211_BANDS] = {
907 	[NL80211_BAND_2GHZ] = { .type = NLA_S32 },
908 	[NL80211_BAND_5GHZ] = { .type = NLA_S32 },
909 	[NL80211_BAND_6GHZ] = { .type = NLA_S32 },
910 	[NL80211_BAND_60GHZ] = { .type = NLA_S32 },
911 	[NL80211_BAND_LC]    = { .type = NLA_S32 },
912 };
913 
914 static const struct nla_policy
915 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
916 	[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
917 						 .len = IEEE80211_MAX_SSID_LEN },
918 	[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
919 	[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
920 	[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI] =
921 		NLA_POLICY_NESTED(nl80211_match_band_rssi_policy),
922 };
923 
924 static const struct nla_policy
925 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
926 	[NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
927 	[NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
928 };
929 
930 static const struct nla_policy
931 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
932 	[NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
933 	[NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
934 	[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
935 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
936 	},
937 };
938 
939 /* policy for NAN function attributes */
940 static const struct nla_policy
941 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
942 	[NL80211_NAN_FUNC_TYPE] =
943 		NLA_POLICY_MAX(NLA_U8, NL80211_NAN_FUNC_MAX_TYPE),
944 	[NL80211_NAN_FUNC_SERVICE_ID] = {
945 				    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
946 	[NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
947 	[NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
948 	[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
949 	[NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
950 	[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
951 	[NL80211_NAN_FUNC_FOLLOW_UP_DEST] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
952 	[NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
953 	[NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
954 	[NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
955 			.len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
956 	[NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
957 	[NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
958 	[NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
959 	[NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
960 	[NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
961 };
962 
963 /* policy for Service Response Filter attributes */
964 static const struct nla_policy
965 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
966 	[NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
967 	[NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
968 				 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
969 	[NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
970 	[NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
971 };
972 
973 /* policy for packet pattern attributes */
974 static const struct nla_policy
975 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
976 	[NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
977 	[NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
978 	[NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
979 };
980 
981 static int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
982 				     struct cfg80211_registered_device **rdev,
983 				     struct wireless_dev **wdev,
984 				     struct nlattr **attrbuf)
985 {
986 	int err;
987 
988 	if (!cb->args[0]) {
989 		struct nlattr **attrbuf_free = NULL;
990 
991 		if (!attrbuf) {
992 			attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
993 					  GFP_KERNEL);
994 			if (!attrbuf)
995 				return -ENOMEM;
996 			attrbuf_free = attrbuf;
997 		}
998 
999 		err = nlmsg_parse_deprecated(cb->nlh,
1000 					     GENL_HDRLEN + nl80211_fam.hdrsize,
1001 					     attrbuf, nl80211_fam.maxattr,
1002 					     nl80211_policy, NULL);
1003 		if (err) {
1004 			kfree(attrbuf_free);
1005 			return err;
1006 		}
1007 
1008 		rtnl_lock();
1009 		*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(cb->skb->sk),
1010 						   attrbuf);
1011 		kfree(attrbuf_free);
1012 		if (IS_ERR(*wdev)) {
1013 			rtnl_unlock();
1014 			return PTR_ERR(*wdev);
1015 		}
1016 		*rdev = wiphy_to_rdev((*wdev)->wiphy);
1017 		mutex_lock(&(*rdev)->wiphy.mtx);
1018 		rtnl_unlock();
1019 		/* 0 is the first index - add 1 to parse only once */
1020 		cb->args[0] = (*rdev)->wiphy_idx + 1;
1021 		cb->args[1] = (*wdev)->identifier;
1022 	} else {
1023 		/* subtract the 1 again here */
1024 		struct wiphy *wiphy;
1025 		struct wireless_dev *tmp;
1026 
1027 		rtnl_lock();
1028 		wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
1029 		if (!wiphy) {
1030 			rtnl_unlock();
1031 			return -ENODEV;
1032 		}
1033 		*rdev = wiphy_to_rdev(wiphy);
1034 		*wdev = NULL;
1035 
1036 		list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
1037 			if (tmp->identifier == cb->args[1]) {
1038 				*wdev = tmp;
1039 				break;
1040 			}
1041 		}
1042 
1043 		if (!*wdev) {
1044 			rtnl_unlock();
1045 			return -ENODEV;
1046 		}
1047 		mutex_lock(&(*rdev)->wiphy.mtx);
1048 		rtnl_unlock();
1049 	}
1050 
1051 	return 0;
1052 }
1053 
1054 /* message building helper */
1055 void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1056 		     int flags, u8 cmd)
1057 {
1058 	/* since there is no private header just add the generic one */
1059 	return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
1060 }
1061 
1062 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
1063 				     const struct ieee80211_reg_rule *rule)
1064 {
1065 	int j;
1066 	struct nlattr *nl_wmm_rules =
1067 		nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
1068 
1069 	if (!nl_wmm_rules)
1070 		goto nla_put_failure;
1071 
1072 	for (j = 0; j < IEEE80211_NUM_ACS; j++) {
1073 		struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
1074 
1075 		if (!nl_wmm_rule)
1076 			goto nla_put_failure;
1077 
1078 		if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
1079 				rule->wmm_rule.client[j].cw_min) ||
1080 		    nla_put_u16(msg, NL80211_WMMR_CW_MAX,
1081 				rule->wmm_rule.client[j].cw_max) ||
1082 		    nla_put_u8(msg, NL80211_WMMR_AIFSN,
1083 			       rule->wmm_rule.client[j].aifsn) ||
1084 		    nla_put_u16(msg, NL80211_WMMR_TXOP,
1085 			        rule->wmm_rule.client[j].cot))
1086 			goto nla_put_failure;
1087 
1088 		nla_nest_end(msg, nl_wmm_rule);
1089 	}
1090 	nla_nest_end(msg, nl_wmm_rules);
1091 
1092 	return 0;
1093 
1094 nla_put_failure:
1095 	return -ENOBUFS;
1096 }
1097 
1098 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
1099 				   struct ieee80211_channel *chan,
1100 				   bool large)
1101 {
1102 	/* Some channels must be completely excluded from the
1103 	 * list to protect old user-space tools from breaking
1104 	 */
1105 	if (!large && chan->flags &
1106 	    (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
1107 		return 0;
1108 	if (!large && chan->freq_offset)
1109 		return 0;
1110 
1111 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
1112 			chan->center_freq))
1113 		goto nla_put_failure;
1114 
1115 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_OFFSET, chan->freq_offset))
1116 		goto nla_put_failure;
1117 
1118 	if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
1119 	    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
1120 		goto nla_put_failure;
1121 	if (chan->flags & IEEE80211_CHAN_NO_IR) {
1122 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
1123 			goto nla_put_failure;
1124 		if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
1125 			goto nla_put_failure;
1126 	}
1127 	if (chan->flags & IEEE80211_CHAN_RADAR) {
1128 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
1129 			goto nla_put_failure;
1130 		if (large) {
1131 			u32 time;
1132 
1133 			time = elapsed_jiffies_msecs(chan->dfs_state_entered);
1134 
1135 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
1136 					chan->dfs_state))
1137 				goto nla_put_failure;
1138 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
1139 					time))
1140 				goto nla_put_failure;
1141 			if (nla_put_u32(msg,
1142 					NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
1143 					chan->dfs_cac_ms))
1144 				goto nla_put_failure;
1145 		}
1146 	}
1147 
1148 	if (large) {
1149 		if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
1150 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
1151 			goto nla_put_failure;
1152 		if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
1153 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
1154 			goto nla_put_failure;
1155 		if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
1156 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
1157 			goto nla_put_failure;
1158 		if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
1159 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
1160 			goto nla_put_failure;
1161 		if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
1162 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
1163 			goto nla_put_failure;
1164 		if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
1165 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
1166 			goto nla_put_failure;
1167 		if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
1168 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
1169 			goto nla_put_failure;
1170 		if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
1171 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
1172 			goto nla_put_failure;
1173 		if ((chan->flags & IEEE80211_CHAN_NO_HE) &&
1174 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HE))
1175 			goto nla_put_failure;
1176 		if ((chan->flags & IEEE80211_CHAN_1MHZ) &&
1177 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_1MHZ))
1178 			goto nla_put_failure;
1179 		if ((chan->flags & IEEE80211_CHAN_2MHZ) &&
1180 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_2MHZ))
1181 			goto nla_put_failure;
1182 		if ((chan->flags & IEEE80211_CHAN_4MHZ) &&
1183 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_4MHZ))
1184 			goto nla_put_failure;
1185 		if ((chan->flags & IEEE80211_CHAN_8MHZ) &&
1186 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_8MHZ))
1187 			goto nla_put_failure;
1188 		if ((chan->flags & IEEE80211_CHAN_16MHZ) &&
1189 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_16MHZ))
1190 			goto nla_put_failure;
1191 		if ((chan->flags & IEEE80211_CHAN_NO_320MHZ) &&
1192 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_320MHZ))
1193 			goto nla_put_failure;
1194 		if ((chan->flags & IEEE80211_CHAN_NO_EHT) &&
1195 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_EHT))
1196 			goto nla_put_failure;
1197 	}
1198 
1199 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
1200 			DBM_TO_MBM(chan->max_power)))
1201 		goto nla_put_failure;
1202 
1203 	if (large) {
1204 		const struct ieee80211_reg_rule *rule =
1205 			freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1206 
1207 		if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
1208 			if (nl80211_msg_put_wmm_rules(msg, rule))
1209 				goto nla_put_failure;
1210 		}
1211 	}
1212 
1213 	return 0;
1214 
1215  nla_put_failure:
1216 	return -ENOBUFS;
1217 }
1218 
1219 static bool nl80211_put_txq_stats(struct sk_buff *msg,
1220 				  struct cfg80211_txq_stats *txqstats,
1221 				  int attrtype)
1222 {
1223 	struct nlattr *txqattr;
1224 
1225 #define PUT_TXQVAL_U32(attr, memb) do {					  \
1226 	if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&	  \
1227 	    nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
1228 		return false;						  \
1229 	} while (0)
1230 
1231 	txqattr = nla_nest_start_noflag(msg, attrtype);
1232 	if (!txqattr)
1233 		return false;
1234 
1235 	PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
1236 	PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
1237 	PUT_TXQVAL_U32(FLOWS, flows);
1238 	PUT_TXQVAL_U32(DROPS, drops);
1239 	PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
1240 	PUT_TXQVAL_U32(OVERLIMIT, overlimit);
1241 	PUT_TXQVAL_U32(OVERMEMORY, overmemory);
1242 	PUT_TXQVAL_U32(COLLISIONS, collisions);
1243 	PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
1244 	PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
1245 	PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
1246 	nla_nest_end(msg, txqattr);
1247 
1248 #undef PUT_TXQVAL_U32
1249 	return true;
1250 }
1251 
1252 /* netlink command implementations */
1253 
1254 /**
1255  * nl80211_link_id - return link ID
1256  * @attrs: attributes to look at
1257  *
1258  * Returns: the link ID or 0 if not given
1259  *
1260  * Note this function doesn't do any validation of the link
1261  * ID validity wrt. links that were actually added, so it must
1262  * be called only from ops with %NL80211_FLAG_MLO_VALID_LINK_ID
1263  * or if additional validation is done.
1264  */
1265 static unsigned int nl80211_link_id(struct nlattr **attrs)
1266 {
1267 	struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1268 
1269 	if (!linkid)
1270 		return 0;
1271 
1272 	return nla_get_u8(linkid);
1273 }
1274 
1275 static int nl80211_link_id_or_invalid(struct nlattr **attrs)
1276 {
1277 	struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1278 
1279 	if (!linkid)
1280 		return -1;
1281 
1282 	return nla_get_u8(linkid);
1283 }
1284 
1285 struct key_parse {
1286 	struct key_params p;
1287 	int idx;
1288 	int type;
1289 	bool def, defmgmt, defbeacon;
1290 	bool def_uni, def_multi;
1291 };
1292 
1293 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
1294 				 struct key_parse *k)
1295 {
1296 	struct nlattr *tb[NL80211_KEY_MAX + 1];
1297 	int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
1298 					      nl80211_key_policy,
1299 					      info->extack);
1300 	if (err)
1301 		return err;
1302 
1303 	k->def = !!tb[NL80211_KEY_DEFAULT];
1304 	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1305 	k->defbeacon = !!tb[NL80211_KEY_DEFAULT_BEACON];
1306 
1307 	if (k->def) {
1308 		k->def_uni = true;
1309 		k->def_multi = true;
1310 	}
1311 	if (k->defmgmt || k->defbeacon)
1312 		k->def_multi = true;
1313 
1314 	if (tb[NL80211_KEY_IDX])
1315 		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1316 
1317 	if (tb[NL80211_KEY_DATA]) {
1318 		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1319 		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1320 	}
1321 
1322 	if (tb[NL80211_KEY_SEQ]) {
1323 		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1324 		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1325 	}
1326 
1327 	if (tb[NL80211_KEY_CIPHER])
1328 		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1329 
1330 	if (tb[NL80211_KEY_TYPE])
1331 		k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1332 
1333 	if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1334 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1335 
1336 		err = nla_parse_nested_deprecated(kdt,
1337 						  NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1338 						  tb[NL80211_KEY_DEFAULT_TYPES],
1339 						  nl80211_key_default_policy,
1340 						  info->extack);
1341 		if (err)
1342 			return err;
1343 
1344 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1345 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1346 	}
1347 
1348 	if (tb[NL80211_KEY_MODE])
1349 		k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1350 
1351 	return 0;
1352 }
1353 
1354 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1355 {
1356 	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1357 		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1358 		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1359 	}
1360 
1361 	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1362 		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1363 		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1364 	}
1365 
1366 	if (info->attrs[NL80211_ATTR_KEY_IDX])
1367 		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1368 
1369 	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1370 		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1371 
1372 	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1373 	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1374 
1375 	if (k->def) {
1376 		k->def_uni = true;
1377 		k->def_multi = true;
1378 	}
1379 	if (k->defmgmt)
1380 		k->def_multi = true;
1381 
1382 	if (info->attrs[NL80211_ATTR_KEY_TYPE])
1383 		k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1384 
1385 	if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1386 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1387 		int err = nla_parse_nested_deprecated(kdt,
1388 						      NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1389 						      info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1390 						      nl80211_key_default_policy,
1391 						      info->extack);
1392 		if (err)
1393 			return err;
1394 
1395 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1396 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1397 	}
1398 
1399 	return 0;
1400 }
1401 
1402 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1403 {
1404 	int err;
1405 
1406 	memset(k, 0, sizeof(*k));
1407 	k->idx = -1;
1408 	k->type = -1;
1409 
1410 	if (info->attrs[NL80211_ATTR_KEY])
1411 		err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1412 	else
1413 		err = nl80211_parse_key_old(info, k);
1414 
1415 	if (err)
1416 		return err;
1417 
1418 	if ((k->def ? 1 : 0) + (k->defmgmt ? 1 : 0) +
1419 	    (k->defbeacon ? 1 : 0) > 1) {
1420 		GENL_SET_ERR_MSG(info,
1421 				 "key with multiple default flags is invalid");
1422 		return -EINVAL;
1423 	}
1424 
1425 	if (k->defmgmt || k->defbeacon) {
1426 		if (k->def_uni || !k->def_multi) {
1427 			GENL_SET_ERR_MSG(info,
1428 					 "defmgmt/defbeacon key must be mcast");
1429 			return -EINVAL;
1430 		}
1431 	}
1432 
1433 	if (k->idx != -1) {
1434 		if (k->defmgmt) {
1435 			if (k->idx < 4 || k->idx > 5) {
1436 				GENL_SET_ERR_MSG(info,
1437 						 "defmgmt key idx not 4 or 5");
1438 				return -EINVAL;
1439 			}
1440 		} else if (k->defbeacon) {
1441 			if (k->idx < 6 || k->idx > 7) {
1442 				GENL_SET_ERR_MSG(info,
1443 						 "defbeacon key idx not 6 or 7");
1444 				return -EINVAL;
1445 			}
1446 		} else if (k->def) {
1447 			if (k->idx < 0 || k->idx > 3) {
1448 				GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1449 				return -EINVAL;
1450 			}
1451 		} else {
1452 			if (k->idx < 0 || k->idx > 7) {
1453 				GENL_SET_ERR_MSG(info, "key idx not 0-7");
1454 				return -EINVAL;
1455 			}
1456 		}
1457 	}
1458 
1459 	return 0;
1460 }
1461 
1462 static struct cfg80211_cached_keys *
1463 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1464 		       struct genl_info *info, bool *no_ht)
1465 {
1466 	struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1467 	struct key_parse parse;
1468 	struct nlattr *key;
1469 	struct cfg80211_cached_keys *result;
1470 	int rem, err, def = 0;
1471 	bool have_key = false;
1472 
1473 	nla_for_each_nested(key, keys, rem) {
1474 		have_key = true;
1475 		break;
1476 	}
1477 
1478 	if (!have_key)
1479 		return NULL;
1480 
1481 	result = kzalloc(sizeof(*result), GFP_KERNEL);
1482 	if (!result)
1483 		return ERR_PTR(-ENOMEM);
1484 
1485 	result->def = -1;
1486 
1487 	nla_for_each_nested(key, keys, rem) {
1488 		memset(&parse, 0, sizeof(parse));
1489 		parse.idx = -1;
1490 
1491 		err = nl80211_parse_key_new(info, key, &parse);
1492 		if (err)
1493 			goto error;
1494 		err = -EINVAL;
1495 		if (!parse.p.key)
1496 			goto error;
1497 		if (parse.idx < 0 || parse.idx > 3) {
1498 			GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1499 			goto error;
1500 		}
1501 		if (parse.def) {
1502 			if (def) {
1503 				GENL_SET_ERR_MSG(info,
1504 						 "only one key can be default");
1505 				goto error;
1506 			}
1507 			def = 1;
1508 			result->def = parse.idx;
1509 			if (!parse.def_uni || !parse.def_multi)
1510 				goto error;
1511 		} else if (parse.defmgmt)
1512 			goto error;
1513 		err = cfg80211_validate_key_settings(rdev, &parse.p,
1514 						     parse.idx, false, NULL);
1515 		if (err)
1516 			goto error;
1517 		if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1518 		    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1519 			GENL_SET_ERR_MSG(info, "connect key must be WEP");
1520 			err = -EINVAL;
1521 			goto error;
1522 		}
1523 		result->params[parse.idx].cipher = parse.p.cipher;
1524 		result->params[parse.idx].key_len = parse.p.key_len;
1525 		result->params[parse.idx].key = result->data[parse.idx];
1526 		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1527 
1528 		/* must be WEP key if we got here */
1529 		if (no_ht)
1530 			*no_ht = true;
1531 	}
1532 
1533 	if (result->def < 0) {
1534 		err = -EINVAL;
1535 		GENL_SET_ERR_MSG(info, "need a default/TX key");
1536 		goto error;
1537 	}
1538 
1539 	return result;
1540  error:
1541 	kfree(result);
1542 	return ERR_PTR(err);
1543 }
1544 
1545 static int nl80211_key_allowed(struct wireless_dev *wdev)
1546 {
1547 	ASSERT_WDEV_LOCK(wdev);
1548 
1549 	switch (wdev->iftype) {
1550 	case NL80211_IFTYPE_AP:
1551 	case NL80211_IFTYPE_AP_VLAN:
1552 	case NL80211_IFTYPE_P2P_GO:
1553 	case NL80211_IFTYPE_MESH_POINT:
1554 		break;
1555 	case NL80211_IFTYPE_ADHOC:
1556 		if (wdev->u.ibss.current_bss)
1557 			return 0;
1558 		return -ENOLINK;
1559 	case NL80211_IFTYPE_STATION:
1560 	case NL80211_IFTYPE_P2P_CLIENT:
1561 		if (wdev->connected)
1562 			return 0;
1563 		return -ENOLINK;
1564 	case NL80211_IFTYPE_NAN:
1565 		if (wiphy_ext_feature_isset(wdev->wiphy,
1566 					    NL80211_EXT_FEATURE_SECURE_NAN))
1567 			return 0;
1568 		return -EINVAL;
1569 	case NL80211_IFTYPE_UNSPECIFIED:
1570 	case NL80211_IFTYPE_OCB:
1571 	case NL80211_IFTYPE_MONITOR:
1572 	case NL80211_IFTYPE_P2P_DEVICE:
1573 	case NL80211_IFTYPE_WDS:
1574 	case NUM_NL80211_IFTYPES:
1575 		return -EINVAL;
1576 	}
1577 
1578 	return 0;
1579 }
1580 
1581 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1582 							u32 freq)
1583 {
1584 	struct ieee80211_channel *chan;
1585 
1586 	chan = ieee80211_get_channel_khz(wiphy, freq);
1587 	if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1588 		return NULL;
1589 	return chan;
1590 }
1591 
1592 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1593 {
1594 	struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1595 	int i;
1596 
1597 	if (!nl_modes)
1598 		goto nla_put_failure;
1599 
1600 	i = 0;
1601 	while (ifmodes) {
1602 		if ((ifmodes & 1) && nla_put_flag(msg, i))
1603 			goto nla_put_failure;
1604 		ifmodes >>= 1;
1605 		i++;
1606 	}
1607 
1608 	nla_nest_end(msg, nl_modes);
1609 	return 0;
1610 
1611 nla_put_failure:
1612 	return -ENOBUFS;
1613 }
1614 
1615 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1616 					  struct sk_buff *msg,
1617 					  bool large)
1618 {
1619 	struct nlattr *nl_combis;
1620 	int i, j;
1621 
1622 	nl_combis = nla_nest_start_noflag(msg,
1623 					  NL80211_ATTR_INTERFACE_COMBINATIONS);
1624 	if (!nl_combis)
1625 		goto nla_put_failure;
1626 
1627 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1628 		const struct ieee80211_iface_combination *c;
1629 		struct nlattr *nl_combi, *nl_limits;
1630 
1631 		c = &wiphy->iface_combinations[i];
1632 
1633 		nl_combi = nla_nest_start_noflag(msg, i + 1);
1634 		if (!nl_combi)
1635 			goto nla_put_failure;
1636 
1637 		nl_limits = nla_nest_start_noflag(msg,
1638 						  NL80211_IFACE_COMB_LIMITS);
1639 		if (!nl_limits)
1640 			goto nla_put_failure;
1641 
1642 		for (j = 0; j < c->n_limits; j++) {
1643 			struct nlattr *nl_limit;
1644 
1645 			nl_limit = nla_nest_start_noflag(msg, j + 1);
1646 			if (!nl_limit)
1647 				goto nla_put_failure;
1648 			if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1649 					c->limits[j].max))
1650 				goto nla_put_failure;
1651 			if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1652 						c->limits[j].types))
1653 				goto nla_put_failure;
1654 			nla_nest_end(msg, nl_limit);
1655 		}
1656 
1657 		nla_nest_end(msg, nl_limits);
1658 
1659 		if (c->beacon_int_infra_match &&
1660 		    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1661 			goto nla_put_failure;
1662 		if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1663 				c->num_different_channels) ||
1664 		    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1665 				c->max_interfaces))
1666 			goto nla_put_failure;
1667 		if (large &&
1668 		    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1669 				c->radar_detect_widths) ||
1670 		     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1671 				c->radar_detect_regions)))
1672 			goto nla_put_failure;
1673 		if (c->beacon_int_min_gcd &&
1674 		    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1675 				c->beacon_int_min_gcd))
1676 			goto nla_put_failure;
1677 
1678 		nla_nest_end(msg, nl_combi);
1679 	}
1680 
1681 	nla_nest_end(msg, nl_combis);
1682 
1683 	return 0;
1684 nla_put_failure:
1685 	return -ENOBUFS;
1686 }
1687 
1688 #ifdef CONFIG_PM
1689 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1690 					struct sk_buff *msg)
1691 {
1692 	const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1693 	struct nlattr *nl_tcp;
1694 
1695 	if (!tcp)
1696 		return 0;
1697 
1698 	nl_tcp = nla_nest_start_noflag(msg,
1699 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1700 	if (!nl_tcp)
1701 		return -ENOBUFS;
1702 
1703 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1704 			tcp->data_payload_max))
1705 		return -ENOBUFS;
1706 
1707 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1708 			tcp->data_payload_max))
1709 		return -ENOBUFS;
1710 
1711 	if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1712 		return -ENOBUFS;
1713 
1714 	if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1715 				sizeof(*tcp->tok), tcp->tok))
1716 		return -ENOBUFS;
1717 
1718 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1719 			tcp->data_interval_max))
1720 		return -ENOBUFS;
1721 
1722 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1723 			tcp->wake_payload_max))
1724 		return -ENOBUFS;
1725 
1726 	nla_nest_end(msg, nl_tcp);
1727 	return 0;
1728 }
1729 
1730 static int nl80211_send_wowlan(struct sk_buff *msg,
1731 			       struct cfg80211_registered_device *rdev,
1732 			       bool large)
1733 {
1734 	struct nlattr *nl_wowlan;
1735 
1736 	if (!rdev->wiphy.wowlan)
1737 		return 0;
1738 
1739 	nl_wowlan = nla_nest_start_noflag(msg,
1740 					  NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1741 	if (!nl_wowlan)
1742 		return -ENOBUFS;
1743 
1744 	if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1745 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1746 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1747 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1748 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1749 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1750 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1751 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1752 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1753 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1754 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1755 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1756 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1757 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1758 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1759 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1760 		return -ENOBUFS;
1761 
1762 	if (rdev->wiphy.wowlan->n_patterns) {
1763 		struct nl80211_pattern_support pat = {
1764 			.max_patterns = rdev->wiphy.wowlan->n_patterns,
1765 			.min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1766 			.max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1767 			.max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1768 		};
1769 
1770 		if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1771 			    sizeof(pat), &pat))
1772 			return -ENOBUFS;
1773 	}
1774 
1775 	if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1776 	    nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1777 			rdev->wiphy.wowlan->max_nd_match_sets))
1778 		return -ENOBUFS;
1779 
1780 	if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1781 		return -ENOBUFS;
1782 
1783 	nla_nest_end(msg, nl_wowlan);
1784 
1785 	return 0;
1786 }
1787 #endif
1788 
1789 static int nl80211_send_coalesce(struct sk_buff *msg,
1790 				 struct cfg80211_registered_device *rdev)
1791 {
1792 	struct nl80211_coalesce_rule_support rule;
1793 
1794 	if (!rdev->wiphy.coalesce)
1795 		return 0;
1796 
1797 	rule.max_rules = rdev->wiphy.coalesce->n_rules;
1798 	rule.max_delay = rdev->wiphy.coalesce->max_delay;
1799 	rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1800 	rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1801 	rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1802 	rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1803 
1804 	if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1805 		return -ENOBUFS;
1806 
1807 	return 0;
1808 }
1809 
1810 static int
1811 nl80211_send_iftype_data(struct sk_buff *msg,
1812 			 const struct ieee80211_supported_band *sband,
1813 			 const struct ieee80211_sband_iftype_data *iftdata)
1814 {
1815 	const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1816 	const struct ieee80211_sta_eht_cap *eht_cap = &iftdata->eht_cap;
1817 
1818 	if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1819 				iftdata->types_mask))
1820 		return -ENOBUFS;
1821 
1822 	if (he_cap->has_he) {
1823 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1824 			    sizeof(he_cap->he_cap_elem.mac_cap_info),
1825 			    he_cap->he_cap_elem.mac_cap_info) ||
1826 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1827 			    sizeof(he_cap->he_cap_elem.phy_cap_info),
1828 			    he_cap->he_cap_elem.phy_cap_info) ||
1829 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1830 			    sizeof(he_cap->he_mcs_nss_supp),
1831 			    &he_cap->he_mcs_nss_supp) ||
1832 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1833 			    sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1834 			return -ENOBUFS;
1835 	}
1836 
1837 	if (eht_cap->has_eht && he_cap->has_he) {
1838 		u8 mcs_nss_size, ppe_thresh_size;
1839 		u16 ppe_thres_hdr;
1840 		bool is_ap;
1841 
1842 		is_ap = iftdata->types_mask & BIT(NL80211_IFTYPE_AP) ||
1843 			iftdata->types_mask & BIT(NL80211_IFTYPE_P2P_GO);
1844 
1845 		mcs_nss_size =
1846 			ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
1847 						   &eht_cap->eht_cap_elem,
1848 						   is_ap);
1849 
1850 		ppe_thres_hdr = get_unaligned_le16(&eht_cap->eht_ppe_thres[0]);
1851 		ppe_thresh_size =
1852 			ieee80211_eht_ppe_size(ppe_thres_hdr,
1853 					       eht_cap->eht_cap_elem.phy_cap_info);
1854 
1855 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MAC,
1856 			    sizeof(eht_cap->eht_cap_elem.mac_cap_info),
1857 			    eht_cap->eht_cap_elem.mac_cap_info) ||
1858 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY,
1859 			    sizeof(eht_cap->eht_cap_elem.phy_cap_info),
1860 			    eht_cap->eht_cap_elem.phy_cap_info) ||
1861 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MCS_SET,
1862 			    mcs_nss_size, &eht_cap->eht_mcs_nss_supp) ||
1863 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PPE,
1864 			    ppe_thresh_size, eht_cap->eht_ppe_thres))
1865 			return -ENOBUFS;
1866 	}
1867 
1868 	if (sband->band == NL80211_BAND_6GHZ &&
1869 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_6GHZ_CAPA,
1870 		    sizeof(iftdata->he_6ghz_capa),
1871 		    &iftdata->he_6ghz_capa))
1872 		return -ENOBUFS;
1873 
1874 	if (iftdata->vendor_elems.data && iftdata->vendor_elems.len &&
1875 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_VENDOR_ELEMS,
1876 		    iftdata->vendor_elems.len, iftdata->vendor_elems.data))
1877 		return -ENOBUFS;
1878 
1879 	return 0;
1880 }
1881 
1882 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1883 				      struct ieee80211_supported_band *sband,
1884 				      bool large)
1885 {
1886 	struct nlattr *nl_rates, *nl_rate;
1887 	struct ieee80211_rate *rate;
1888 	int i;
1889 
1890 	/* add HT info */
1891 	if (sband->ht_cap.ht_supported &&
1892 	    (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1893 		     sizeof(sband->ht_cap.mcs),
1894 		     &sband->ht_cap.mcs) ||
1895 	     nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1896 			 sband->ht_cap.cap) ||
1897 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1898 			sband->ht_cap.ampdu_factor) ||
1899 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1900 			sband->ht_cap.ampdu_density)))
1901 		return -ENOBUFS;
1902 
1903 	/* add VHT info */
1904 	if (sband->vht_cap.vht_supported &&
1905 	    (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1906 		     sizeof(sband->vht_cap.vht_mcs),
1907 		     &sband->vht_cap.vht_mcs) ||
1908 	     nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1909 			 sband->vht_cap.cap)))
1910 		return -ENOBUFS;
1911 
1912 	if (large && sband->n_iftype_data) {
1913 		struct nlattr *nl_iftype_data =
1914 			nla_nest_start_noflag(msg,
1915 					      NL80211_BAND_ATTR_IFTYPE_DATA);
1916 		int err;
1917 
1918 		if (!nl_iftype_data)
1919 			return -ENOBUFS;
1920 
1921 		for (i = 0; i < sband->n_iftype_data; i++) {
1922 			struct nlattr *iftdata;
1923 
1924 			iftdata = nla_nest_start_noflag(msg, i + 1);
1925 			if (!iftdata)
1926 				return -ENOBUFS;
1927 
1928 			err = nl80211_send_iftype_data(msg, sband,
1929 						       &sband->iftype_data[i]);
1930 			if (err)
1931 				return err;
1932 
1933 			nla_nest_end(msg, iftdata);
1934 		}
1935 
1936 		nla_nest_end(msg, nl_iftype_data);
1937 	}
1938 
1939 	/* add EDMG info */
1940 	if (large && sband->edmg_cap.channels &&
1941 	    (nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_CHANNELS,
1942 		       sband->edmg_cap.channels) ||
1943 	    nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_BW_CONFIG,
1944 		       sband->edmg_cap.bw_config)))
1945 
1946 		return -ENOBUFS;
1947 
1948 	/* add bitrates */
1949 	nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1950 	if (!nl_rates)
1951 		return -ENOBUFS;
1952 
1953 	for (i = 0; i < sband->n_bitrates; i++) {
1954 		nl_rate = nla_nest_start_noflag(msg, i);
1955 		if (!nl_rate)
1956 			return -ENOBUFS;
1957 
1958 		rate = &sband->bitrates[i];
1959 		if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1960 				rate->bitrate))
1961 			return -ENOBUFS;
1962 		if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1963 		    nla_put_flag(msg,
1964 				 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1965 			return -ENOBUFS;
1966 
1967 		nla_nest_end(msg, nl_rate);
1968 	}
1969 
1970 	nla_nest_end(msg, nl_rates);
1971 
1972 	/* S1G capabilities */
1973 	if (sband->band == NL80211_BAND_S1GHZ && sband->s1g_cap.s1g &&
1974 	    (nla_put(msg, NL80211_BAND_ATTR_S1G_CAPA,
1975 		     sizeof(sband->s1g_cap.cap),
1976 		     sband->s1g_cap.cap) ||
1977 	     nla_put(msg, NL80211_BAND_ATTR_S1G_MCS_NSS_SET,
1978 		     sizeof(sband->s1g_cap.nss_mcs),
1979 		     sband->s1g_cap.nss_mcs)))
1980 		return -ENOBUFS;
1981 
1982 	return 0;
1983 }
1984 
1985 static int
1986 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1987 			 const struct ieee80211_txrx_stypes *mgmt_stypes)
1988 {
1989 	u16 stypes;
1990 	struct nlattr *nl_ftypes, *nl_ifs;
1991 	enum nl80211_iftype ift;
1992 	int i;
1993 
1994 	if (!mgmt_stypes)
1995 		return 0;
1996 
1997 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
1998 	if (!nl_ifs)
1999 		return -ENOBUFS;
2000 
2001 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
2002 		nl_ftypes = nla_nest_start_noflag(msg, ift);
2003 		if (!nl_ftypes)
2004 			return -ENOBUFS;
2005 		i = 0;
2006 		stypes = mgmt_stypes[ift].tx;
2007 		while (stypes) {
2008 			if ((stypes & 1) &&
2009 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
2010 					(i << 4) | IEEE80211_FTYPE_MGMT))
2011 				return -ENOBUFS;
2012 			stypes >>= 1;
2013 			i++;
2014 		}
2015 		nla_nest_end(msg, nl_ftypes);
2016 	}
2017 
2018 	nla_nest_end(msg, nl_ifs);
2019 
2020 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
2021 	if (!nl_ifs)
2022 		return -ENOBUFS;
2023 
2024 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
2025 		nl_ftypes = nla_nest_start_noflag(msg, ift);
2026 		if (!nl_ftypes)
2027 			return -ENOBUFS;
2028 		i = 0;
2029 		stypes = mgmt_stypes[ift].rx;
2030 		while (stypes) {
2031 			if ((stypes & 1) &&
2032 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
2033 					(i << 4) | IEEE80211_FTYPE_MGMT))
2034 				return -ENOBUFS;
2035 			stypes >>= 1;
2036 			i++;
2037 		}
2038 		nla_nest_end(msg, nl_ftypes);
2039 	}
2040 	nla_nest_end(msg, nl_ifs);
2041 
2042 	return 0;
2043 }
2044 
2045 #define CMD(op, n)							\
2046 	 do {								\
2047 		if (rdev->ops->op) {					\
2048 			i++;						\
2049 			if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) 	\
2050 				goto nla_put_failure;			\
2051 		}							\
2052 	} while (0)
2053 
2054 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
2055 					struct sk_buff *msg)
2056 {
2057 	int i = 0;
2058 
2059 	/*
2060 	 * do *NOT* add anything into this function, new things need to be
2061 	 * advertised only to new versions of userspace that can deal with
2062 	 * the split (and they can't possibly care about new features...
2063 	 */
2064 	CMD(add_virtual_intf, NEW_INTERFACE);
2065 	CMD(change_virtual_intf, SET_INTERFACE);
2066 	CMD(add_key, NEW_KEY);
2067 	CMD(start_ap, START_AP);
2068 	CMD(add_station, NEW_STATION);
2069 	CMD(add_mpath, NEW_MPATH);
2070 	CMD(update_mesh_config, SET_MESH_CONFIG);
2071 	CMD(change_bss, SET_BSS);
2072 	CMD(auth, AUTHENTICATE);
2073 	CMD(assoc, ASSOCIATE);
2074 	CMD(deauth, DEAUTHENTICATE);
2075 	CMD(disassoc, DISASSOCIATE);
2076 	CMD(join_ibss, JOIN_IBSS);
2077 	CMD(join_mesh, JOIN_MESH);
2078 	CMD(set_pmksa, SET_PMKSA);
2079 	CMD(del_pmksa, DEL_PMKSA);
2080 	CMD(flush_pmksa, FLUSH_PMKSA);
2081 	if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
2082 		CMD(remain_on_channel, REMAIN_ON_CHANNEL);
2083 	CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
2084 	CMD(mgmt_tx, FRAME);
2085 	CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
2086 	if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
2087 		i++;
2088 		if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
2089 			goto nla_put_failure;
2090 	}
2091 	if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
2092 	    rdev->ops->join_mesh) {
2093 		i++;
2094 		if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
2095 			goto nla_put_failure;
2096 	}
2097 	if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2098 		CMD(tdls_mgmt, TDLS_MGMT);
2099 		CMD(tdls_oper, TDLS_OPER);
2100 	}
2101 	if (rdev->wiphy.max_sched_scan_reqs)
2102 		CMD(sched_scan_start, START_SCHED_SCAN);
2103 	CMD(probe_client, PROBE_CLIENT);
2104 	CMD(set_noack_map, SET_NOACK_MAP);
2105 	if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
2106 		i++;
2107 		if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
2108 			goto nla_put_failure;
2109 	}
2110 	CMD(start_p2p_device, START_P2P_DEVICE);
2111 	CMD(set_mcast_rate, SET_MCAST_RATE);
2112 #ifdef CONFIG_NL80211_TESTMODE
2113 	CMD(testmode_cmd, TESTMODE);
2114 #endif
2115 
2116 	if (rdev->ops->connect || rdev->ops->auth) {
2117 		i++;
2118 		if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
2119 			goto nla_put_failure;
2120 	}
2121 
2122 	if (rdev->ops->disconnect || rdev->ops->deauth) {
2123 		i++;
2124 		if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
2125 			goto nla_put_failure;
2126 	}
2127 
2128 	return i;
2129  nla_put_failure:
2130 	return -ENOBUFS;
2131 }
2132 
2133 static int
2134 nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
2135 			   struct sk_buff *msg)
2136 {
2137 	struct nlattr *ftm;
2138 
2139 	if (!cap->ftm.supported)
2140 		return 0;
2141 
2142 	ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
2143 	if (!ftm)
2144 		return -ENOBUFS;
2145 
2146 	if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
2147 		return -ENOBUFS;
2148 	if (cap->ftm.non_asap &&
2149 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
2150 		return -ENOBUFS;
2151 	if (cap->ftm.request_lci &&
2152 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
2153 		return -ENOBUFS;
2154 	if (cap->ftm.request_civicloc &&
2155 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
2156 		return -ENOBUFS;
2157 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
2158 			cap->ftm.preambles))
2159 		return -ENOBUFS;
2160 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
2161 			cap->ftm.bandwidths))
2162 		return -ENOBUFS;
2163 	if (cap->ftm.max_bursts_exponent >= 0 &&
2164 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
2165 			cap->ftm.max_bursts_exponent))
2166 		return -ENOBUFS;
2167 	if (cap->ftm.max_ftms_per_burst &&
2168 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
2169 			cap->ftm.max_ftms_per_burst))
2170 		return -ENOBUFS;
2171 	if (cap->ftm.trigger_based &&
2172 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_TRIGGER_BASED))
2173 		return -ENOBUFS;
2174 	if (cap->ftm.non_trigger_based &&
2175 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_TRIGGER_BASED))
2176 		return -ENOBUFS;
2177 
2178 	nla_nest_end(msg, ftm);
2179 	return 0;
2180 }
2181 
2182 static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
2183 				  struct sk_buff *msg)
2184 {
2185 	const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
2186 	struct nlattr *pmsr, *caps;
2187 
2188 	if (!cap)
2189 		return 0;
2190 
2191 	/*
2192 	 * we don't need to clean up anything here since the caller
2193 	 * will genlmsg_cancel() if we fail
2194 	 */
2195 
2196 	pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
2197 	if (!pmsr)
2198 		return -ENOBUFS;
2199 
2200 	if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
2201 		return -ENOBUFS;
2202 
2203 	if (cap->report_ap_tsf &&
2204 	    nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
2205 		return -ENOBUFS;
2206 
2207 	if (cap->randomize_mac_addr &&
2208 	    nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
2209 		return -ENOBUFS;
2210 
2211 	caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
2212 	if (!caps)
2213 		return -ENOBUFS;
2214 
2215 	if (nl80211_send_pmsr_ftm_capa(cap, msg))
2216 		return -ENOBUFS;
2217 
2218 	nla_nest_end(msg, caps);
2219 	nla_nest_end(msg, pmsr);
2220 
2221 	return 0;
2222 }
2223 
2224 static int
2225 nl80211_put_iftype_akm_suites(struct cfg80211_registered_device *rdev,
2226 			      struct sk_buff *msg)
2227 {
2228 	int i;
2229 	struct nlattr *nested, *nested_akms;
2230 	const struct wiphy_iftype_akm_suites *iftype_akms;
2231 
2232 	if (!rdev->wiphy.num_iftype_akm_suites ||
2233 	    !rdev->wiphy.iftype_akm_suites)
2234 		return 0;
2235 
2236 	nested = nla_nest_start(msg, NL80211_ATTR_IFTYPE_AKM_SUITES);
2237 	if (!nested)
2238 		return -ENOBUFS;
2239 
2240 	for (i = 0; i < rdev->wiphy.num_iftype_akm_suites; i++) {
2241 		nested_akms = nla_nest_start(msg, i + 1);
2242 		if (!nested_akms)
2243 			return -ENOBUFS;
2244 
2245 		iftype_akms = &rdev->wiphy.iftype_akm_suites[i];
2246 
2247 		if (nl80211_put_iftypes(msg, NL80211_IFTYPE_AKM_ATTR_IFTYPES,
2248 					iftype_akms->iftypes_mask))
2249 			return -ENOBUFS;
2250 
2251 		if (nla_put(msg, NL80211_IFTYPE_AKM_ATTR_SUITES,
2252 			    sizeof(u32) * iftype_akms->n_akm_suites,
2253 			    iftype_akms->akm_suites)) {
2254 			return -ENOBUFS;
2255 		}
2256 		nla_nest_end(msg, nested_akms);
2257 	}
2258 
2259 	nla_nest_end(msg, nested);
2260 
2261 	return 0;
2262 }
2263 
2264 static int
2265 nl80211_put_tid_config_support(struct cfg80211_registered_device *rdev,
2266 			       struct sk_buff *msg)
2267 {
2268 	struct nlattr *supp;
2269 
2270 	if (!rdev->wiphy.tid_config_support.vif &&
2271 	    !rdev->wiphy.tid_config_support.peer)
2272 		return 0;
2273 
2274 	supp = nla_nest_start(msg, NL80211_ATTR_TID_CONFIG);
2275 	if (!supp)
2276 		return -ENOSPC;
2277 
2278 	if (rdev->wiphy.tid_config_support.vif &&
2279 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_VIF_SUPP,
2280 			      rdev->wiphy.tid_config_support.vif,
2281 			      NL80211_TID_CONFIG_ATTR_PAD))
2282 		goto fail;
2283 
2284 	if (rdev->wiphy.tid_config_support.peer &&
2285 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_PEER_SUPP,
2286 			      rdev->wiphy.tid_config_support.peer,
2287 			      NL80211_TID_CONFIG_ATTR_PAD))
2288 		goto fail;
2289 
2290 	/* for now we just use the same value ... makes more sense */
2291 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_SHORT,
2292 		       rdev->wiphy.tid_config_support.max_retry))
2293 		goto fail;
2294 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_LONG,
2295 		       rdev->wiphy.tid_config_support.max_retry))
2296 		goto fail;
2297 
2298 	nla_nest_end(msg, supp);
2299 
2300 	return 0;
2301 fail:
2302 	nla_nest_cancel(msg, supp);
2303 	return -ENOBUFS;
2304 }
2305 
2306 static int
2307 nl80211_put_sar_specs(struct cfg80211_registered_device *rdev,
2308 		      struct sk_buff *msg)
2309 {
2310 	struct nlattr *sar_capa, *specs, *sub_freq_range;
2311 	u8 num_freq_ranges;
2312 	int i;
2313 
2314 	if (!rdev->wiphy.sar_capa)
2315 		return 0;
2316 
2317 	num_freq_ranges = rdev->wiphy.sar_capa->num_freq_ranges;
2318 
2319 	sar_capa = nla_nest_start(msg, NL80211_ATTR_SAR_SPEC);
2320 	if (!sar_capa)
2321 		return -ENOSPC;
2322 
2323 	if (nla_put_u32(msg, NL80211_SAR_ATTR_TYPE, rdev->wiphy.sar_capa->type))
2324 		goto fail;
2325 
2326 	specs = nla_nest_start(msg, NL80211_SAR_ATTR_SPECS);
2327 	if (!specs)
2328 		goto fail;
2329 
2330 	/* report supported freq_ranges */
2331 	for (i = 0; i < num_freq_ranges; i++) {
2332 		sub_freq_range = nla_nest_start(msg, i + 1);
2333 		if (!sub_freq_range)
2334 			goto fail;
2335 
2336 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_START_FREQ,
2337 				rdev->wiphy.sar_capa->freq_ranges[i].start_freq))
2338 			goto fail;
2339 
2340 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_END_FREQ,
2341 				rdev->wiphy.sar_capa->freq_ranges[i].end_freq))
2342 			goto fail;
2343 
2344 		nla_nest_end(msg, sub_freq_range);
2345 	}
2346 
2347 	nla_nest_end(msg, specs);
2348 	nla_nest_end(msg, sar_capa);
2349 
2350 	return 0;
2351 fail:
2352 	nla_nest_cancel(msg, sar_capa);
2353 	return -ENOBUFS;
2354 }
2355 
2356 static int nl80211_put_mbssid_support(struct wiphy *wiphy, struct sk_buff *msg)
2357 {
2358 	struct nlattr *config;
2359 
2360 	if (!wiphy->mbssid_max_interfaces)
2361 		return 0;
2362 
2363 	config = nla_nest_start(msg, NL80211_ATTR_MBSSID_CONFIG);
2364 	if (!config)
2365 		return -ENOBUFS;
2366 
2367 	if (nla_put_u8(msg, NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES,
2368 		       wiphy->mbssid_max_interfaces))
2369 		goto fail;
2370 
2371 	if (wiphy->ema_max_profile_periodicity &&
2372 	    nla_put_u8(msg,
2373 		       NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY,
2374 		       wiphy->ema_max_profile_periodicity))
2375 		goto fail;
2376 
2377 	nla_nest_end(msg, config);
2378 	return 0;
2379 
2380 fail:
2381 	nla_nest_cancel(msg, config);
2382 	return -ENOBUFS;
2383 }
2384 
2385 struct nl80211_dump_wiphy_state {
2386 	s64 filter_wiphy;
2387 	long start;
2388 	long split_start, band_start, chan_start, capa_start;
2389 	bool split;
2390 };
2391 
2392 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
2393 			      enum nl80211_commands cmd,
2394 			      struct sk_buff *msg, u32 portid, u32 seq,
2395 			      int flags, struct nl80211_dump_wiphy_state *state)
2396 {
2397 	void *hdr;
2398 	struct nlattr *nl_bands, *nl_band;
2399 	struct nlattr *nl_freqs, *nl_freq;
2400 	struct nlattr *nl_cmds;
2401 	enum nl80211_band band;
2402 	struct ieee80211_channel *chan;
2403 	int i;
2404 	const struct ieee80211_txrx_stypes *mgmt_stypes =
2405 				rdev->wiphy.mgmt_stypes;
2406 	u32 features;
2407 
2408 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2409 	if (!hdr)
2410 		return -ENOBUFS;
2411 
2412 	if (WARN_ON(!state))
2413 		return -EINVAL;
2414 
2415 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2416 	    nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
2417 			   wiphy_name(&rdev->wiphy)) ||
2418 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
2419 			cfg80211_rdev_list_generation))
2420 		goto nla_put_failure;
2421 
2422 	if (cmd != NL80211_CMD_NEW_WIPHY)
2423 		goto finish;
2424 
2425 	switch (state->split_start) {
2426 	case 0:
2427 		if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
2428 			       rdev->wiphy.retry_short) ||
2429 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
2430 			       rdev->wiphy.retry_long) ||
2431 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
2432 				rdev->wiphy.frag_threshold) ||
2433 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
2434 				rdev->wiphy.rts_threshold) ||
2435 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
2436 			       rdev->wiphy.coverage_class) ||
2437 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
2438 			       rdev->wiphy.max_scan_ssids) ||
2439 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
2440 			       rdev->wiphy.max_sched_scan_ssids) ||
2441 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
2442 				rdev->wiphy.max_scan_ie_len) ||
2443 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
2444 				rdev->wiphy.max_sched_scan_ie_len) ||
2445 		    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
2446 			       rdev->wiphy.max_match_sets))
2447 			goto nla_put_failure;
2448 
2449 		if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
2450 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
2451 			goto nla_put_failure;
2452 		if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
2453 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
2454 			goto nla_put_failure;
2455 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2456 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
2457 			goto nla_put_failure;
2458 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
2459 		    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
2460 			goto nla_put_failure;
2461 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2462 		    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
2463 			goto nla_put_failure;
2464 		if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
2465 		    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
2466 			goto nla_put_failure;
2467 		state->split_start++;
2468 		if (state->split)
2469 			break;
2470 		fallthrough;
2471 	case 1:
2472 		if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
2473 			    sizeof(u32) * rdev->wiphy.n_cipher_suites,
2474 			    rdev->wiphy.cipher_suites))
2475 			goto nla_put_failure;
2476 
2477 		if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
2478 			       rdev->wiphy.max_num_pmkids))
2479 			goto nla_put_failure;
2480 
2481 		if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
2482 		    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
2483 			goto nla_put_failure;
2484 
2485 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
2486 				rdev->wiphy.available_antennas_tx) ||
2487 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
2488 				rdev->wiphy.available_antennas_rx))
2489 			goto nla_put_failure;
2490 
2491 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
2492 		    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
2493 				rdev->wiphy.probe_resp_offload))
2494 			goto nla_put_failure;
2495 
2496 		if ((rdev->wiphy.available_antennas_tx ||
2497 		     rdev->wiphy.available_antennas_rx) &&
2498 		    rdev->ops->get_antenna) {
2499 			u32 tx_ant = 0, rx_ant = 0;
2500 			int res;
2501 
2502 			res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
2503 			if (!res) {
2504 				if (nla_put_u32(msg,
2505 						NL80211_ATTR_WIPHY_ANTENNA_TX,
2506 						tx_ant) ||
2507 				    nla_put_u32(msg,
2508 						NL80211_ATTR_WIPHY_ANTENNA_RX,
2509 						rx_ant))
2510 					goto nla_put_failure;
2511 			}
2512 		}
2513 
2514 		state->split_start++;
2515 		if (state->split)
2516 			break;
2517 		fallthrough;
2518 	case 2:
2519 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
2520 					rdev->wiphy.interface_modes))
2521 				goto nla_put_failure;
2522 		state->split_start++;
2523 		if (state->split)
2524 			break;
2525 		fallthrough;
2526 	case 3:
2527 		nl_bands = nla_nest_start_noflag(msg,
2528 						 NL80211_ATTR_WIPHY_BANDS);
2529 		if (!nl_bands)
2530 			goto nla_put_failure;
2531 
2532 		for (band = state->band_start;
2533 		     band < (state->split ?
2534 				NUM_NL80211_BANDS :
2535 				NL80211_BAND_60GHZ + 1);
2536 		     band++) {
2537 			struct ieee80211_supported_band *sband;
2538 
2539 			/* omit higher bands for ancient software */
2540 			if (band > NL80211_BAND_5GHZ && !state->split)
2541 				break;
2542 
2543 			sband = rdev->wiphy.bands[band];
2544 
2545 			if (!sband)
2546 				continue;
2547 
2548 			nl_band = nla_nest_start_noflag(msg, band);
2549 			if (!nl_band)
2550 				goto nla_put_failure;
2551 
2552 			switch (state->chan_start) {
2553 			case 0:
2554 				if (nl80211_send_band_rateinfo(msg, sband,
2555 							       state->split))
2556 					goto nla_put_failure;
2557 				state->chan_start++;
2558 				if (state->split)
2559 					break;
2560 				fallthrough;
2561 			default:
2562 				/* add frequencies */
2563 				nl_freqs = nla_nest_start_noflag(msg,
2564 								 NL80211_BAND_ATTR_FREQS);
2565 				if (!nl_freqs)
2566 					goto nla_put_failure;
2567 
2568 				for (i = state->chan_start - 1;
2569 				     i < sband->n_channels;
2570 				     i++) {
2571 					nl_freq = nla_nest_start_noflag(msg,
2572 									i);
2573 					if (!nl_freq)
2574 						goto nla_put_failure;
2575 
2576 					chan = &sband->channels[i];
2577 
2578 					if (nl80211_msg_put_channel(
2579 							msg, &rdev->wiphy, chan,
2580 							state->split))
2581 						goto nla_put_failure;
2582 
2583 					nla_nest_end(msg, nl_freq);
2584 					if (state->split)
2585 						break;
2586 				}
2587 				if (i < sband->n_channels)
2588 					state->chan_start = i + 2;
2589 				else
2590 					state->chan_start = 0;
2591 				nla_nest_end(msg, nl_freqs);
2592 			}
2593 
2594 			nla_nest_end(msg, nl_band);
2595 
2596 			if (state->split) {
2597 				/* start again here */
2598 				if (state->chan_start)
2599 					band--;
2600 				break;
2601 			}
2602 		}
2603 		nla_nest_end(msg, nl_bands);
2604 
2605 		if (band < NUM_NL80211_BANDS)
2606 			state->band_start = band + 1;
2607 		else
2608 			state->band_start = 0;
2609 
2610 		/* if bands & channels are done, continue outside */
2611 		if (state->band_start == 0 && state->chan_start == 0)
2612 			state->split_start++;
2613 		if (state->split)
2614 			break;
2615 		fallthrough;
2616 	case 4:
2617 		nl_cmds = nla_nest_start_noflag(msg,
2618 						NL80211_ATTR_SUPPORTED_COMMANDS);
2619 		if (!nl_cmds)
2620 			goto nla_put_failure;
2621 
2622 		i = nl80211_add_commands_unsplit(rdev, msg);
2623 		if (i < 0)
2624 			goto nla_put_failure;
2625 		if (state->split) {
2626 			CMD(crit_proto_start, CRIT_PROTOCOL_START);
2627 			CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2628 			if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2629 				CMD(channel_switch, CHANNEL_SWITCH);
2630 			CMD(set_qos_map, SET_QOS_MAP);
2631 			if (rdev->wiphy.features &
2632 					NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2633 				CMD(add_tx_ts, ADD_TX_TS);
2634 			CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2635 			CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2636 			CMD(update_ft_ies, UPDATE_FT_IES);
2637 			if (rdev->wiphy.sar_capa)
2638 				CMD(set_sar_specs, SET_SAR_SPECS);
2639 		}
2640 #undef CMD
2641 
2642 		nla_nest_end(msg, nl_cmds);
2643 		state->split_start++;
2644 		if (state->split)
2645 			break;
2646 		fallthrough;
2647 	case 5:
2648 		if (rdev->ops->remain_on_channel &&
2649 		    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2650 		    nla_put_u32(msg,
2651 				NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2652 				rdev->wiphy.max_remain_on_channel_duration))
2653 			goto nla_put_failure;
2654 
2655 		if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2656 		    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2657 			goto nla_put_failure;
2658 
2659 		state->split_start++;
2660 		if (state->split)
2661 			break;
2662 		fallthrough;
2663 	case 6:
2664 #ifdef CONFIG_PM
2665 		if (nl80211_send_wowlan(msg, rdev, state->split))
2666 			goto nla_put_failure;
2667 		state->split_start++;
2668 		if (state->split)
2669 			break;
2670 #else
2671 		state->split_start++;
2672 #endif
2673 		fallthrough;
2674 	case 7:
2675 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2676 					rdev->wiphy.software_iftypes))
2677 			goto nla_put_failure;
2678 
2679 		if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2680 						   state->split))
2681 			goto nla_put_failure;
2682 
2683 		state->split_start++;
2684 		if (state->split)
2685 			break;
2686 		fallthrough;
2687 	case 8:
2688 		if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2689 		    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2690 				rdev->wiphy.ap_sme_capa))
2691 			goto nla_put_failure;
2692 
2693 		features = rdev->wiphy.features;
2694 		/*
2695 		 * We can only add the per-channel limit information if the
2696 		 * dump is split, otherwise it makes it too big. Therefore
2697 		 * only advertise it in that case.
2698 		 */
2699 		if (state->split)
2700 			features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2701 		if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2702 			goto nla_put_failure;
2703 
2704 		if (rdev->wiphy.ht_capa_mod_mask &&
2705 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2706 			    sizeof(*rdev->wiphy.ht_capa_mod_mask),
2707 			    rdev->wiphy.ht_capa_mod_mask))
2708 			goto nla_put_failure;
2709 
2710 		if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2711 		    rdev->wiphy.max_acl_mac_addrs &&
2712 		    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2713 				rdev->wiphy.max_acl_mac_addrs))
2714 			goto nla_put_failure;
2715 
2716 		/*
2717 		 * Any information below this point is only available to
2718 		 * applications that can deal with it being split. This
2719 		 * helps ensure that newly added capabilities don't break
2720 		 * older tools by overrunning their buffers.
2721 		 *
2722 		 * We still increment split_start so that in the split
2723 		 * case we'll continue with more data in the next round,
2724 		 * but break unconditionally so unsplit data stops here.
2725 		 */
2726 		if (state->split)
2727 			state->split_start++;
2728 		else
2729 			state->split_start = 0;
2730 		break;
2731 	case 9:
2732 		if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2733 			goto nla_put_failure;
2734 
2735 		if (nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
2736 				rdev->wiphy.max_sched_scan_plans) ||
2737 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
2738 				rdev->wiphy.max_sched_scan_plan_interval) ||
2739 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
2740 				rdev->wiphy.max_sched_scan_plan_iterations))
2741 			goto nla_put_failure;
2742 
2743 		if (rdev->wiphy.extended_capabilities &&
2744 		    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2745 			     rdev->wiphy.extended_capabilities_len,
2746 			     rdev->wiphy.extended_capabilities) ||
2747 		     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2748 			     rdev->wiphy.extended_capabilities_len,
2749 			     rdev->wiphy.extended_capabilities_mask)))
2750 			goto nla_put_failure;
2751 
2752 		if (rdev->wiphy.vht_capa_mod_mask &&
2753 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2754 			    sizeof(*rdev->wiphy.vht_capa_mod_mask),
2755 			    rdev->wiphy.vht_capa_mod_mask))
2756 			goto nla_put_failure;
2757 
2758 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2759 			    rdev->wiphy.perm_addr))
2760 			goto nla_put_failure;
2761 
2762 		if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2763 		    nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2764 			    rdev->wiphy.addr_mask))
2765 			goto nla_put_failure;
2766 
2767 		if (rdev->wiphy.n_addresses > 1) {
2768 			void *attr;
2769 
2770 			attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2771 			if (!attr)
2772 				goto nla_put_failure;
2773 
2774 			for (i = 0; i < rdev->wiphy.n_addresses; i++)
2775 				if (nla_put(msg, i + 1, ETH_ALEN,
2776 					    rdev->wiphy.addresses[i].addr))
2777 					goto nla_put_failure;
2778 
2779 			nla_nest_end(msg, attr);
2780 		}
2781 
2782 		state->split_start++;
2783 		break;
2784 	case 10:
2785 		if (nl80211_send_coalesce(msg, rdev))
2786 			goto nla_put_failure;
2787 
2788 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2789 		    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2790 		     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2791 			goto nla_put_failure;
2792 
2793 		if (rdev->wiphy.max_ap_assoc_sta &&
2794 		    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2795 				rdev->wiphy.max_ap_assoc_sta))
2796 			goto nla_put_failure;
2797 
2798 		state->split_start++;
2799 		break;
2800 	case 11:
2801 		if (rdev->wiphy.n_vendor_commands) {
2802 			const struct nl80211_vendor_cmd_info *info;
2803 			struct nlattr *nested;
2804 
2805 			nested = nla_nest_start_noflag(msg,
2806 						       NL80211_ATTR_VENDOR_DATA);
2807 			if (!nested)
2808 				goto nla_put_failure;
2809 
2810 			for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2811 				info = &rdev->wiphy.vendor_commands[i].info;
2812 				if (nla_put(msg, i + 1, sizeof(*info), info))
2813 					goto nla_put_failure;
2814 			}
2815 			nla_nest_end(msg, nested);
2816 		}
2817 
2818 		if (rdev->wiphy.n_vendor_events) {
2819 			const struct nl80211_vendor_cmd_info *info;
2820 			struct nlattr *nested;
2821 
2822 			nested = nla_nest_start_noflag(msg,
2823 						       NL80211_ATTR_VENDOR_EVENTS);
2824 			if (!nested)
2825 				goto nla_put_failure;
2826 
2827 			for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2828 				info = &rdev->wiphy.vendor_events[i];
2829 				if (nla_put(msg, i + 1, sizeof(*info), info))
2830 					goto nla_put_failure;
2831 			}
2832 			nla_nest_end(msg, nested);
2833 		}
2834 		state->split_start++;
2835 		break;
2836 	case 12:
2837 		if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2838 		    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2839 			       rdev->wiphy.max_num_csa_counters))
2840 			goto nla_put_failure;
2841 
2842 		if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2843 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2844 			goto nla_put_failure;
2845 
2846 		if (rdev->wiphy.max_sched_scan_reqs &&
2847 		    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2848 				rdev->wiphy.max_sched_scan_reqs))
2849 			goto nla_put_failure;
2850 
2851 		if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2852 			    sizeof(rdev->wiphy.ext_features),
2853 			    rdev->wiphy.ext_features))
2854 			goto nla_put_failure;
2855 
2856 		if (rdev->wiphy.bss_select_support) {
2857 			struct nlattr *nested;
2858 			u32 bss_select_support = rdev->wiphy.bss_select_support;
2859 
2860 			nested = nla_nest_start_noflag(msg,
2861 						       NL80211_ATTR_BSS_SELECT);
2862 			if (!nested)
2863 				goto nla_put_failure;
2864 
2865 			i = 0;
2866 			while (bss_select_support) {
2867 				if ((bss_select_support & 1) &&
2868 				    nla_put_flag(msg, i))
2869 					goto nla_put_failure;
2870 				i++;
2871 				bss_select_support >>= 1;
2872 			}
2873 			nla_nest_end(msg, nested);
2874 		}
2875 
2876 		state->split_start++;
2877 		break;
2878 	case 13:
2879 		if (rdev->wiphy.num_iftype_ext_capab &&
2880 		    rdev->wiphy.iftype_ext_capab) {
2881 			struct nlattr *nested_ext_capab, *nested;
2882 
2883 			nested = nla_nest_start_noflag(msg,
2884 						       NL80211_ATTR_IFTYPE_EXT_CAPA);
2885 			if (!nested)
2886 				goto nla_put_failure;
2887 
2888 			for (i = state->capa_start;
2889 			     i < rdev->wiphy.num_iftype_ext_capab; i++) {
2890 				const struct wiphy_iftype_ext_capab *capab;
2891 
2892 				capab = &rdev->wiphy.iftype_ext_capab[i];
2893 
2894 				nested_ext_capab = nla_nest_start_noflag(msg,
2895 									 i);
2896 				if (!nested_ext_capab ||
2897 				    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2898 						capab->iftype) ||
2899 				    nla_put(msg, NL80211_ATTR_EXT_CAPA,
2900 					    capab->extended_capabilities_len,
2901 					    capab->extended_capabilities) ||
2902 				    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2903 					    capab->extended_capabilities_len,
2904 					    capab->extended_capabilities_mask))
2905 					goto nla_put_failure;
2906 
2907 				if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO &&
2908 				    (nla_put_u16(msg,
2909 						 NL80211_ATTR_EML_CAPABILITY,
2910 						 capab->eml_capabilities) ||
2911 				     nla_put_u16(msg,
2912 						 NL80211_ATTR_MLD_CAPA_AND_OPS,
2913 						 capab->mld_capa_and_ops)))
2914 					goto nla_put_failure;
2915 
2916 				nla_nest_end(msg, nested_ext_capab);
2917 				if (state->split)
2918 					break;
2919 			}
2920 			nla_nest_end(msg, nested);
2921 			if (i < rdev->wiphy.num_iftype_ext_capab) {
2922 				state->capa_start = i + 1;
2923 				break;
2924 			}
2925 		}
2926 
2927 		if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2928 				rdev->wiphy.nan_supported_bands))
2929 			goto nla_put_failure;
2930 
2931 		if (wiphy_ext_feature_isset(&rdev->wiphy,
2932 					    NL80211_EXT_FEATURE_TXQS)) {
2933 			struct cfg80211_txq_stats txqstats = {};
2934 			int res;
2935 
2936 			res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2937 			if (!res &&
2938 			    !nl80211_put_txq_stats(msg, &txqstats,
2939 						   NL80211_ATTR_TXQ_STATS))
2940 				goto nla_put_failure;
2941 
2942 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2943 					rdev->wiphy.txq_limit))
2944 				goto nla_put_failure;
2945 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2946 					rdev->wiphy.txq_memory_limit))
2947 				goto nla_put_failure;
2948 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2949 					rdev->wiphy.txq_quantum))
2950 				goto nla_put_failure;
2951 		}
2952 
2953 		state->split_start++;
2954 		break;
2955 	case 14:
2956 		if (nl80211_send_pmsr_capa(rdev, msg))
2957 			goto nla_put_failure;
2958 
2959 		state->split_start++;
2960 		break;
2961 	case 15:
2962 		if (rdev->wiphy.akm_suites &&
2963 		    nla_put(msg, NL80211_ATTR_AKM_SUITES,
2964 			    sizeof(u32) * rdev->wiphy.n_akm_suites,
2965 			    rdev->wiphy.akm_suites))
2966 			goto nla_put_failure;
2967 
2968 		if (nl80211_put_iftype_akm_suites(rdev, msg))
2969 			goto nla_put_failure;
2970 
2971 		if (nl80211_put_tid_config_support(rdev, msg))
2972 			goto nla_put_failure;
2973 		state->split_start++;
2974 		break;
2975 	case 16:
2976 		if (nl80211_put_sar_specs(rdev, msg))
2977 			goto nla_put_failure;
2978 
2979 		if (nl80211_put_mbssid_support(&rdev->wiphy, msg))
2980 			goto nla_put_failure;
2981 
2982 		if (nla_put_u16(msg, NL80211_ATTR_MAX_NUM_AKM_SUITES,
2983 				rdev->wiphy.max_num_akm_suites))
2984 			goto nla_put_failure;
2985 
2986 		if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO)
2987 			nla_put_flag(msg, NL80211_ATTR_MLO_SUPPORT);
2988 
2989 		if (rdev->wiphy.hw_timestamp_max_peers &&
2990 		    nla_put_u16(msg, NL80211_ATTR_MAX_HW_TIMESTAMP_PEERS,
2991 				rdev->wiphy.hw_timestamp_max_peers))
2992 			goto nla_put_failure;
2993 
2994 		/* done */
2995 		state->split_start = 0;
2996 		break;
2997 	}
2998  finish:
2999 	genlmsg_end(msg, hdr);
3000 	return 0;
3001 
3002  nla_put_failure:
3003 	genlmsg_cancel(msg, hdr);
3004 	return -EMSGSIZE;
3005 }
3006 
3007 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
3008 				    struct netlink_callback *cb,
3009 				    struct nl80211_dump_wiphy_state *state)
3010 {
3011 	struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
3012 	int ret;
3013 
3014 	if (!tb)
3015 		return -ENOMEM;
3016 
3017 	ret = nlmsg_parse_deprecated(cb->nlh,
3018 				     GENL_HDRLEN + nl80211_fam.hdrsize,
3019 				     tb, nl80211_fam.maxattr,
3020 				     nl80211_policy, NULL);
3021 	/* ignore parse errors for backward compatibility */
3022 	if (ret) {
3023 		ret = 0;
3024 		goto out;
3025 	}
3026 
3027 	state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
3028 	if (tb[NL80211_ATTR_WIPHY])
3029 		state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
3030 	if (tb[NL80211_ATTR_WDEV])
3031 		state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
3032 	if (tb[NL80211_ATTR_IFINDEX]) {
3033 		struct net_device *netdev;
3034 		struct cfg80211_registered_device *rdev;
3035 		int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3036 
3037 		netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
3038 		if (!netdev) {
3039 			ret = -ENODEV;
3040 			goto out;
3041 		}
3042 		if (netdev->ieee80211_ptr) {
3043 			rdev = wiphy_to_rdev(
3044 				netdev->ieee80211_ptr->wiphy);
3045 			state->filter_wiphy = rdev->wiphy_idx;
3046 		}
3047 	}
3048 
3049 	ret = 0;
3050 out:
3051 	kfree(tb);
3052 	return ret;
3053 }
3054 
3055 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
3056 {
3057 	int idx = 0, ret;
3058 	struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
3059 	struct cfg80211_registered_device *rdev;
3060 
3061 	rtnl_lock();
3062 	if (!state) {
3063 		state = kzalloc(sizeof(*state), GFP_KERNEL);
3064 		if (!state) {
3065 			rtnl_unlock();
3066 			return -ENOMEM;
3067 		}
3068 		state->filter_wiphy = -1;
3069 		ret = nl80211_dump_wiphy_parse(skb, cb, state);
3070 		if (ret) {
3071 			kfree(state);
3072 			rtnl_unlock();
3073 			return ret;
3074 		}
3075 		cb->args[0] = (long)state;
3076 	}
3077 
3078 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3079 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3080 			continue;
3081 		if (++idx <= state->start)
3082 			continue;
3083 		if (state->filter_wiphy != -1 &&
3084 		    state->filter_wiphy != rdev->wiphy_idx)
3085 			continue;
3086 		wiphy_lock(&rdev->wiphy);
3087 		/* attempt to fit multiple wiphy data chunks into the skb */
3088 		do {
3089 			ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
3090 						 skb,
3091 						 NETLINK_CB(cb->skb).portid,
3092 						 cb->nlh->nlmsg_seq,
3093 						 NLM_F_MULTI, state);
3094 			if (ret < 0) {
3095 				/*
3096 				 * If sending the wiphy data didn't fit (ENOBUFS
3097 				 * or EMSGSIZE returned), this SKB is still
3098 				 * empty (so it's not too big because another
3099 				 * wiphy dataset is already in the skb) and
3100 				 * we've not tried to adjust the dump allocation
3101 				 * yet ... then adjust the alloc size to be
3102 				 * bigger, and return 1 but with the empty skb.
3103 				 * This results in an empty message being RX'ed
3104 				 * in userspace, but that is ignored.
3105 				 *
3106 				 * We can then retry with the larger buffer.
3107 				 */
3108 				if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
3109 				    !skb->len && !state->split &&
3110 				    cb->min_dump_alloc < 4096) {
3111 					cb->min_dump_alloc = 4096;
3112 					state->split_start = 0;
3113 					wiphy_unlock(&rdev->wiphy);
3114 					rtnl_unlock();
3115 					return 1;
3116 				}
3117 				idx--;
3118 				break;
3119 			}
3120 		} while (state->split_start > 0);
3121 		wiphy_unlock(&rdev->wiphy);
3122 		break;
3123 	}
3124 	rtnl_unlock();
3125 
3126 	state->start = idx;
3127 
3128 	return skb->len;
3129 }
3130 
3131 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
3132 {
3133 	kfree((void *)cb->args[0]);
3134 	return 0;
3135 }
3136 
3137 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
3138 {
3139 	struct sk_buff *msg;
3140 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3141 	struct nl80211_dump_wiphy_state state = {};
3142 
3143 	msg = nlmsg_new(4096, GFP_KERNEL);
3144 	if (!msg)
3145 		return -ENOMEM;
3146 
3147 	if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
3148 			       info->snd_portid, info->snd_seq, 0,
3149 			       &state) < 0) {
3150 		nlmsg_free(msg);
3151 		return -ENOBUFS;
3152 	}
3153 
3154 	return genlmsg_reply(msg, info);
3155 }
3156 
3157 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
3158 	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
3159 	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
3160 	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
3161 	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
3162 	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
3163 };
3164 
3165 static int parse_txq_params(struct nlattr *tb[],
3166 			    struct ieee80211_txq_params *txq_params)
3167 {
3168 	u8 ac;
3169 
3170 	if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
3171 	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
3172 	    !tb[NL80211_TXQ_ATTR_AIFS])
3173 		return -EINVAL;
3174 
3175 	ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
3176 	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
3177 	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
3178 	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
3179 	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
3180 
3181 	if (ac >= NL80211_NUM_ACS)
3182 		return -EINVAL;
3183 	txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
3184 	return 0;
3185 }
3186 
3187 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
3188 {
3189 	/*
3190 	 * You can only set the channel explicitly for some interfaces,
3191 	 * most have their channel managed via their respective
3192 	 * "establish a connection" command (connect, join, ...)
3193 	 *
3194 	 * For AP/GO and mesh mode, the channel can be set with the
3195 	 * channel userspace API, but is only stored and passed to the
3196 	 * low-level driver when the AP starts or the mesh is joined.
3197 	 * This is for backward compatibility, userspace can also give
3198 	 * the channel in the start-ap or join-mesh commands instead.
3199 	 *
3200 	 * Monitors are special as they are normally slaved to
3201 	 * whatever else is going on, so they have their own special
3202 	 * operation to set the monitor channel if possible.
3203 	 */
3204 	return !wdev ||
3205 		wdev->iftype == NL80211_IFTYPE_AP ||
3206 		wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
3207 		wdev->iftype == NL80211_IFTYPE_MONITOR ||
3208 		wdev->iftype == NL80211_IFTYPE_P2P_GO;
3209 }
3210 
3211 static int nl80211_parse_punct_bitmap(struct cfg80211_registered_device *rdev,
3212 				      struct genl_info *info,
3213 				      const struct cfg80211_chan_def *chandef,
3214 				      u16 *punct_bitmap)
3215 {
3216 	if (!wiphy_ext_feature_isset(&rdev->wiphy, NL80211_EXT_FEATURE_PUNCT))
3217 		return -EINVAL;
3218 
3219 	*punct_bitmap = nla_get_u32(info->attrs[NL80211_ATTR_PUNCT_BITMAP]);
3220 	if (!cfg80211_valid_disable_subchannel_bitmap(punct_bitmap, chandef))
3221 		return -EINVAL;
3222 
3223 	return 0;
3224 }
3225 
3226 int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
3227 			  struct genl_info *info,
3228 			  struct cfg80211_chan_def *chandef)
3229 {
3230 	struct netlink_ext_ack *extack = info->extack;
3231 	struct nlattr **attrs = info->attrs;
3232 	u32 control_freq;
3233 
3234 	if (!attrs[NL80211_ATTR_WIPHY_FREQ]) {
3235 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
3236 				    "Frequency is missing");
3237 		return -EINVAL;
3238 	}
3239 
3240 	control_freq = MHZ_TO_KHZ(
3241 			nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3242 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
3243 		control_freq +=
3244 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
3245 
3246 	memset(chandef, 0, sizeof(*chandef));
3247 	chandef->chan = ieee80211_get_channel_khz(&rdev->wiphy, control_freq);
3248 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
3249 	chandef->center_freq1 = KHZ_TO_MHZ(control_freq);
3250 	chandef->freq1_offset = control_freq % 1000;
3251 	chandef->center_freq2 = 0;
3252 
3253 	/* Primary channel not allowed */
3254 	if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED) {
3255 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
3256 				    "Channel is disabled");
3257 		return -EINVAL;
3258 	}
3259 
3260 	if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
3261 		enum nl80211_channel_type chantype;
3262 
3263 		chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
3264 
3265 		switch (chantype) {
3266 		case NL80211_CHAN_NO_HT:
3267 		case NL80211_CHAN_HT20:
3268 		case NL80211_CHAN_HT40PLUS:
3269 		case NL80211_CHAN_HT40MINUS:
3270 			cfg80211_chandef_create(chandef, chandef->chan,
3271 						chantype);
3272 			/* user input for center_freq is incorrect */
3273 			if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
3274 			    chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
3275 				NL_SET_ERR_MSG_ATTR(extack,
3276 						    attrs[NL80211_ATTR_CENTER_FREQ1],
3277 						    "bad center frequency 1");
3278 				return -EINVAL;
3279 			}
3280 			/* center_freq2 must be zero */
3281 			if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
3282 			    nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
3283 				NL_SET_ERR_MSG_ATTR(extack,
3284 						    attrs[NL80211_ATTR_CENTER_FREQ2],
3285 						    "center frequency 2 can't be used");
3286 				return -EINVAL;
3287 			}
3288 			break;
3289 		default:
3290 			NL_SET_ERR_MSG_ATTR(extack,
3291 					    attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3292 					    "invalid channel type");
3293 			return -EINVAL;
3294 		}
3295 	} else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
3296 		chandef->width =
3297 			nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
3298 		if (chandef->chan->band == NL80211_BAND_S1GHZ) {
3299 			/* User input error for channel width doesn't match channel  */
3300 			if (chandef->width != ieee80211_s1g_channel_width(chandef->chan)) {
3301 				NL_SET_ERR_MSG_ATTR(extack,
3302 						    attrs[NL80211_ATTR_CHANNEL_WIDTH],
3303 						    "bad channel width");
3304 				return -EINVAL;
3305 			}
3306 		}
3307 		if (attrs[NL80211_ATTR_CENTER_FREQ1]) {
3308 			chandef->center_freq1 =
3309 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
3310 			if (attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET])
3311 				chandef->freq1_offset = nla_get_u32(
3312 				      attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET]);
3313 			else
3314 				chandef->freq1_offset = 0;
3315 		}
3316 		if (attrs[NL80211_ATTR_CENTER_FREQ2])
3317 			chandef->center_freq2 =
3318 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
3319 	}
3320 
3321 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
3322 		chandef->edmg.channels =
3323 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
3324 
3325 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
3326 			chandef->edmg.bw_config =
3327 		     nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
3328 	} else {
3329 		chandef->edmg.bw_config = 0;
3330 		chandef->edmg.channels = 0;
3331 	}
3332 
3333 	if (!cfg80211_chandef_valid(chandef)) {
3334 		NL_SET_ERR_MSG(extack, "invalid channel definition");
3335 		return -EINVAL;
3336 	}
3337 
3338 	if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
3339 				     IEEE80211_CHAN_DISABLED)) {
3340 		NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
3341 		return -EINVAL;
3342 	}
3343 
3344 	if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
3345 	     chandef->width == NL80211_CHAN_WIDTH_10) &&
3346 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
3347 		NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
3348 		return -EINVAL;
3349 	}
3350 
3351 	return 0;
3352 }
3353 
3354 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
3355 				 struct net_device *dev,
3356 				 struct genl_info *info,
3357 				 int _link_id)
3358 {
3359 	struct cfg80211_chan_def chandef;
3360 	int result;
3361 	enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
3362 	struct wireless_dev *wdev = NULL;
3363 	int link_id = _link_id;
3364 
3365 	if (dev)
3366 		wdev = dev->ieee80211_ptr;
3367 	if (!nl80211_can_set_dev_channel(wdev))
3368 		return -EOPNOTSUPP;
3369 	if (wdev)
3370 		iftype = wdev->iftype;
3371 
3372 	if (link_id < 0) {
3373 		if (wdev && wdev->valid_links)
3374 			return -EINVAL;
3375 		link_id = 0;
3376 	}
3377 
3378 	result = nl80211_parse_chandef(rdev, info, &chandef);
3379 	if (result)
3380 		return result;
3381 
3382 	switch (iftype) {
3383 	case NL80211_IFTYPE_AP:
3384 	case NL80211_IFTYPE_P2P_GO:
3385 		if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
3386 						   iftype))
3387 			return -EINVAL;
3388 		if (wdev->links[link_id].ap.beacon_interval) {
3389 			struct ieee80211_channel *cur_chan;
3390 
3391 			if (!dev || !rdev->ops->set_ap_chanwidth ||
3392 			    !(rdev->wiphy.features &
3393 			      NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE))
3394 				return -EBUSY;
3395 
3396 			/* Only allow dynamic channel width changes */
3397 			cur_chan = wdev->links[link_id].ap.chandef.chan;
3398 			if (chandef.chan != cur_chan)
3399 				return -EBUSY;
3400 
3401 			result = rdev_set_ap_chanwidth(rdev, dev, link_id,
3402 						       &chandef);
3403 			if (result)
3404 				return result;
3405 			wdev->links[link_id].ap.chandef = chandef;
3406 		} else {
3407 			wdev->u.ap.preset_chandef = chandef;
3408 		}
3409 		return 0;
3410 	case NL80211_IFTYPE_MESH_POINT:
3411 		return cfg80211_set_mesh_channel(rdev, wdev, &chandef);
3412 	case NL80211_IFTYPE_MONITOR:
3413 		return cfg80211_set_monitor_channel(rdev, &chandef);
3414 	default:
3415 		break;
3416 	}
3417 
3418 	return -EINVAL;
3419 }
3420 
3421 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
3422 {
3423 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3424 	int link_id = nl80211_link_id_or_invalid(info->attrs);
3425 	struct net_device *netdev = info->user_ptr[1];
3426 	int ret;
3427 
3428 	wdev_lock(netdev->ieee80211_ptr);
3429 	ret = __nl80211_set_channel(rdev, netdev, info, link_id);
3430 	wdev_unlock(netdev->ieee80211_ptr);
3431 
3432 	return ret;
3433 }
3434 
3435 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
3436 {
3437 	struct cfg80211_registered_device *rdev = NULL;
3438 	struct net_device *netdev = NULL;
3439 	struct wireless_dev *wdev;
3440 	int result = 0, rem_txq_params = 0;
3441 	struct nlattr *nl_txq_params;
3442 	u32 changed;
3443 	u8 retry_short = 0, retry_long = 0;
3444 	u32 frag_threshold = 0, rts_threshold = 0;
3445 	u8 coverage_class = 0;
3446 	u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
3447 
3448 	rtnl_lock();
3449 	/*
3450 	 * Try to find the wiphy and netdev. Normally this
3451 	 * function shouldn't need the netdev, but this is
3452 	 * done for backward compatibility -- previously
3453 	 * setting the channel was done per wiphy, but now
3454 	 * it is per netdev. Previous userland like hostapd
3455 	 * also passed a netdev to set_wiphy, so that it is
3456 	 * possible to let that go to the right netdev!
3457 	 */
3458 
3459 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
3460 		int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
3461 
3462 		netdev = __dev_get_by_index(genl_info_net(info), ifindex);
3463 		if (netdev && netdev->ieee80211_ptr)
3464 			rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
3465 		else
3466 			netdev = NULL;
3467 	}
3468 
3469 	if (!netdev) {
3470 		rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
3471 						  info->attrs);
3472 		if (IS_ERR(rdev)) {
3473 			rtnl_unlock();
3474 			return PTR_ERR(rdev);
3475 		}
3476 		wdev = NULL;
3477 		netdev = NULL;
3478 		result = 0;
3479 	} else
3480 		wdev = netdev->ieee80211_ptr;
3481 
3482 	wiphy_lock(&rdev->wiphy);
3483 
3484 	/*
3485 	 * end workaround code, by now the rdev is available
3486 	 * and locked, and wdev may or may not be NULL.
3487 	 */
3488 
3489 	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
3490 		result = cfg80211_dev_rename(
3491 			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
3492 	rtnl_unlock();
3493 
3494 	if (result)
3495 		goto out;
3496 
3497 	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
3498 		struct ieee80211_txq_params txq_params;
3499 		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
3500 
3501 		if (!rdev->ops->set_txq_params) {
3502 			result = -EOPNOTSUPP;
3503 			goto out;
3504 		}
3505 
3506 		if (!netdev) {
3507 			result = -EINVAL;
3508 			goto out;
3509 		}
3510 
3511 		if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3512 		    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3513 			result = -EINVAL;
3514 			goto out;
3515 		}
3516 
3517 		if (!netif_running(netdev)) {
3518 			result = -ENETDOWN;
3519 			goto out;
3520 		}
3521 
3522 		nla_for_each_nested(nl_txq_params,
3523 				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
3524 				    rem_txq_params) {
3525 			result = nla_parse_nested_deprecated(tb,
3526 							     NL80211_TXQ_ATTR_MAX,
3527 							     nl_txq_params,
3528 							     txq_params_policy,
3529 							     info->extack);
3530 			if (result)
3531 				goto out;
3532 			result = parse_txq_params(tb, &txq_params);
3533 			if (result)
3534 				goto out;
3535 
3536 			txq_params.link_id =
3537 				nl80211_link_id_or_invalid(info->attrs);
3538 
3539 			wdev_lock(netdev->ieee80211_ptr);
3540 			if (txq_params.link_id >= 0 &&
3541 			    !(netdev->ieee80211_ptr->valid_links &
3542 			      BIT(txq_params.link_id)))
3543 				result = -ENOLINK;
3544 			else if (txq_params.link_id >= 0 &&
3545 				 !netdev->ieee80211_ptr->valid_links)
3546 				result = -EINVAL;
3547 			else
3548 				result = rdev_set_txq_params(rdev, netdev,
3549 							     &txq_params);
3550 			wdev_unlock(netdev->ieee80211_ptr);
3551 			if (result)
3552 				goto out;
3553 		}
3554 	}
3555 
3556 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3557 		int link_id = nl80211_link_id_or_invalid(info->attrs);
3558 
3559 		if (wdev) {
3560 			wdev_lock(wdev);
3561 			result = __nl80211_set_channel(
3562 				rdev,
3563 				nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
3564 				info, link_id);
3565 			wdev_unlock(wdev);
3566 		} else {
3567 			result = __nl80211_set_channel(rdev, netdev, info, link_id);
3568 		}
3569 
3570 		if (result)
3571 			goto out;
3572 	}
3573 
3574 	if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
3575 		struct wireless_dev *txp_wdev = wdev;
3576 		enum nl80211_tx_power_setting type;
3577 		int idx, mbm = 0;
3578 
3579 		if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
3580 			txp_wdev = NULL;
3581 
3582 		if (!rdev->ops->set_tx_power) {
3583 			result = -EOPNOTSUPP;
3584 			goto out;
3585 		}
3586 
3587 		idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
3588 		type = nla_get_u32(info->attrs[idx]);
3589 
3590 		if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
3591 		    (type != NL80211_TX_POWER_AUTOMATIC)) {
3592 			result = -EINVAL;
3593 			goto out;
3594 		}
3595 
3596 		if (type != NL80211_TX_POWER_AUTOMATIC) {
3597 			idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
3598 			mbm = nla_get_u32(info->attrs[idx]);
3599 		}
3600 
3601 		result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
3602 		if (result)
3603 			goto out;
3604 	}
3605 
3606 	if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
3607 	    info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
3608 		u32 tx_ant, rx_ant;
3609 
3610 		if ((!rdev->wiphy.available_antennas_tx &&
3611 		     !rdev->wiphy.available_antennas_rx) ||
3612 		    !rdev->ops->set_antenna) {
3613 			result = -EOPNOTSUPP;
3614 			goto out;
3615 		}
3616 
3617 		tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
3618 		rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
3619 
3620 		/* reject antenna configurations which don't match the
3621 		 * available antenna masks, except for the "all" mask */
3622 		if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
3623 		    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
3624 			result = -EINVAL;
3625 			goto out;
3626 		}
3627 
3628 		tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
3629 		rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
3630 
3631 		result = rdev_set_antenna(rdev, tx_ant, rx_ant);
3632 		if (result)
3633 			goto out;
3634 	}
3635 
3636 	changed = 0;
3637 
3638 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
3639 		retry_short = nla_get_u8(
3640 			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
3641 
3642 		changed |= WIPHY_PARAM_RETRY_SHORT;
3643 	}
3644 
3645 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
3646 		retry_long = nla_get_u8(
3647 			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
3648 
3649 		changed |= WIPHY_PARAM_RETRY_LONG;
3650 	}
3651 
3652 	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
3653 		frag_threshold = nla_get_u32(
3654 			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
3655 		if (frag_threshold < 256) {
3656 			result = -EINVAL;
3657 			goto out;
3658 		}
3659 
3660 		if (frag_threshold != (u32) -1) {
3661 			/*
3662 			 * Fragments (apart from the last one) are required to
3663 			 * have even length. Make the fragmentation code
3664 			 * simpler by stripping LSB should someone try to use
3665 			 * odd threshold value.
3666 			 */
3667 			frag_threshold &= ~0x1;
3668 		}
3669 		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
3670 	}
3671 
3672 	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
3673 		rts_threshold = nla_get_u32(
3674 			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
3675 		changed |= WIPHY_PARAM_RTS_THRESHOLD;
3676 	}
3677 
3678 	if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
3679 		if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3680 			result = -EINVAL;
3681 			goto out;
3682 		}
3683 
3684 		coverage_class = nla_get_u8(
3685 			info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
3686 		changed |= WIPHY_PARAM_COVERAGE_CLASS;
3687 	}
3688 
3689 	if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3690 		if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION)) {
3691 			result = -EOPNOTSUPP;
3692 			goto out;
3693 		}
3694 
3695 		changed |= WIPHY_PARAM_DYN_ACK;
3696 	}
3697 
3698 	if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3699 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3700 					     NL80211_EXT_FEATURE_TXQS)) {
3701 			result = -EOPNOTSUPP;
3702 			goto out;
3703 		}
3704 		txq_limit = nla_get_u32(
3705 			info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3706 		changed |= WIPHY_PARAM_TXQ_LIMIT;
3707 	}
3708 
3709 	if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3710 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3711 					     NL80211_EXT_FEATURE_TXQS)) {
3712 			result = -EOPNOTSUPP;
3713 			goto out;
3714 		}
3715 		txq_memory_limit = nla_get_u32(
3716 			info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3717 		changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3718 	}
3719 
3720 	if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3721 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3722 					     NL80211_EXT_FEATURE_TXQS)) {
3723 			result = -EOPNOTSUPP;
3724 			goto out;
3725 		}
3726 		txq_quantum = nla_get_u32(
3727 			info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3728 		changed |= WIPHY_PARAM_TXQ_QUANTUM;
3729 	}
3730 
3731 	if (changed) {
3732 		u8 old_retry_short, old_retry_long;
3733 		u32 old_frag_threshold, old_rts_threshold;
3734 		u8 old_coverage_class;
3735 		u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3736 
3737 		if (!rdev->ops->set_wiphy_params) {
3738 			result = -EOPNOTSUPP;
3739 			goto out;
3740 		}
3741 
3742 		old_retry_short = rdev->wiphy.retry_short;
3743 		old_retry_long = rdev->wiphy.retry_long;
3744 		old_frag_threshold = rdev->wiphy.frag_threshold;
3745 		old_rts_threshold = rdev->wiphy.rts_threshold;
3746 		old_coverage_class = rdev->wiphy.coverage_class;
3747 		old_txq_limit = rdev->wiphy.txq_limit;
3748 		old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3749 		old_txq_quantum = rdev->wiphy.txq_quantum;
3750 
3751 		if (changed & WIPHY_PARAM_RETRY_SHORT)
3752 			rdev->wiphy.retry_short = retry_short;
3753 		if (changed & WIPHY_PARAM_RETRY_LONG)
3754 			rdev->wiphy.retry_long = retry_long;
3755 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3756 			rdev->wiphy.frag_threshold = frag_threshold;
3757 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3758 			rdev->wiphy.rts_threshold = rts_threshold;
3759 		if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3760 			rdev->wiphy.coverage_class = coverage_class;
3761 		if (changed & WIPHY_PARAM_TXQ_LIMIT)
3762 			rdev->wiphy.txq_limit = txq_limit;
3763 		if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3764 			rdev->wiphy.txq_memory_limit = txq_memory_limit;
3765 		if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3766 			rdev->wiphy.txq_quantum = txq_quantum;
3767 
3768 		result = rdev_set_wiphy_params(rdev, changed);
3769 		if (result) {
3770 			rdev->wiphy.retry_short = old_retry_short;
3771 			rdev->wiphy.retry_long = old_retry_long;
3772 			rdev->wiphy.frag_threshold = old_frag_threshold;
3773 			rdev->wiphy.rts_threshold = old_rts_threshold;
3774 			rdev->wiphy.coverage_class = old_coverage_class;
3775 			rdev->wiphy.txq_limit = old_txq_limit;
3776 			rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3777 			rdev->wiphy.txq_quantum = old_txq_quantum;
3778 			goto out;
3779 		}
3780 	}
3781 
3782 	result = 0;
3783 
3784 out:
3785 	wiphy_unlock(&rdev->wiphy);
3786 	return result;
3787 }
3788 
3789 int nl80211_send_chandef(struct sk_buff *msg, const struct cfg80211_chan_def *chandef)
3790 {
3791 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3792 		return -EINVAL;
3793 
3794 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3795 			chandef->chan->center_freq))
3796 		return -ENOBUFS;
3797 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
3798 			chandef->chan->freq_offset))
3799 		return -ENOBUFS;
3800 	switch (chandef->width) {
3801 	case NL80211_CHAN_WIDTH_20_NOHT:
3802 	case NL80211_CHAN_WIDTH_20:
3803 	case NL80211_CHAN_WIDTH_40:
3804 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3805 				cfg80211_get_chandef_type(chandef)))
3806 			return -ENOBUFS;
3807 		break;
3808 	default:
3809 		break;
3810 	}
3811 	if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3812 		return -ENOBUFS;
3813 	if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3814 		return -ENOBUFS;
3815 	if (chandef->center_freq2 &&
3816 	    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3817 		return -ENOBUFS;
3818 	return 0;
3819 }
3820 EXPORT_SYMBOL(nl80211_send_chandef);
3821 
3822 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3823 			      struct cfg80211_registered_device *rdev,
3824 			      struct wireless_dev *wdev,
3825 			      enum nl80211_commands cmd)
3826 {
3827 	struct net_device *dev = wdev->netdev;
3828 	void *hdr;
3829 
3830 	WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3831 		cmd != NL80211_CMD_DEL_INTERFACE &&
3832 		cmd != NL80211_CMD_SET_INTERFACE);
3833 
3834 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3835 	if (!hdr)
3836 		return -1;
3837 
3838 	if (dev &&
3839 	    (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3840 	     nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3841 		goto nla_put_failure;
3842 
3843 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3844 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3845 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3846 			      NL80211_ATTR_PAD) ||
3847 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3848 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
3849 			rdev->devlist_generation ^
3850 			(cfg80211_rdev_list_generation << 2)) ||
3851 	    nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3852 		goto nla_put_failure;
3853 
3854 	if (rdev->ops->get_channel && !wdev->valid_links) {
3855 		struct cfg80211_chan_def chandef = {};
3856 		int ret;
3857 
3858 		ret = rdev_get_channel(rdev, wdev, 0, &chandef);
3859 		if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3860 			goto nla_put_failure;
3861 	}
3862 
3863 	if (rdev->ops->get_tx_power) {
3864 		int dbm, ret;
3865 
3866 		ret = rdev_get_tx_power(rdev, wdev, &dbm);
3867 		if (ret == 0 &&
3868 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3869 				DBM_TO_MBM(dbm)))
3870 			goto nla_put_failure;
3871 	}
3872 
3873 	wdev_lock(wdev);
3874 	switch (wdev->iftype) {
3875 	case NL80211_IFTYPE_AP:
3876 	case NL80211_IFTYPE_P2P_GO:
3877 		if (wdev->u.ap.ssid_len &&
3878 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.ap.ssid_len,
3879 			    wdev->u.ap.ssid))
3880 			goto nla_put_failure_locked;
3881 		break;
3882 	case NL80211_IFTYPE_STATION:
3883 	case NL80211_IFTYPE_P2P_CLIENT:
3884 		if (wdev->u.client.ssid_len &&
3885 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.client.ssid_len,
3886 			    wdev->u.client.ssid))
3887 			goto nla_put_failure_locked;
3888 		break;
3889 	case NL80211_IFTYPE_ADHOC:
3890 		if (wdev->u.ibss.ssid_len &&
3891 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.ibss.ssid_len,
3892 			    wdev->u.ibss.ssid))
3893 			goto nla_put_failure_locked;
3894 		break;
3895 	default:
3896 		/* nothing */
3897 		break;
3898 	}
3899 	wdev_unlock(wdev);
3900 
3901 	if (rdev->ops->get_txq_stats) {
3902 		struct cfg80211_txq_stats txqstats = {};
3903 		int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3904 
3905 		if (ret == 0 &&
3906 		    !nl80211_put_txq_stats(msg, &txqstats,
3907 					   NL80211_ATTR_TXQ_STATS))
3908 			goto nla_put_failure;
3909 	}
3910 
3911 	if (wdev->valid_links) {
3912 		unsigned int link_id;
3913 		struct nlattr *links = nla_nest_start(msg,
3914 						      NL80211_ATTR_MLO_LINKS);
3915 
3916 		if (!links)
3917 			goto nla_put_failure;
3918 
3919 		for_each_valid_link(wdev, link_id) {
3920 			struct nlattr *link = nla_nest_start(msg, link_id + 1);
3921 			struct cfg80211_chan_def chandef = {};
3922 			int ret;
3923 
3924 			if (!link)
3925 				goto nla_put_failure;
3926 
3927 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
3928 				goto nla_put_failure;
3929 			if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
3930 				    wdev->links[link_id].addr))
3931 				goto nla_put_failure;
3932 
3933 			ret = rdev_get_channel(rdev, wdev, link_id, &chandef);
3934 			if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3935 				goto nla_put_failure;
3936 
3937 			nla_nest_end(msg, link);
3938 		}
3939 
3940 		nla_nest_end(msg, links);
3941 	}
3942 
3943 	genlmsg_end(msg, hdr);
3944 	return 0;
3945 
3946  nla_put_failure_locked:
3947 	wdev_unlock(wdev);
3948  nla_put_failure:
3949 	genlmsg_cancel(msg, hdr);
3950 	return -EMSGSIZE;
3951 }
3952 
3953 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3954 {
3955 	int wp_idx = 0;
3956 	int if_idx = 0;
3957 	int wp_start = cb->args[0];
3958 	int if_start = cb->args[1];
3959 	int filter_wiphy = -1;
3960 	struct cfg80211_registered_device *rdev;
3961 	struct wireless_dev *wdev;
3962 	int ret;
3963 
3964 	rtnl_lock();
3965 	if (!cb->args[2]) {
3966 		struct nl80211_dump_wiphy_state state = {
3967 			.filter_wiphy = -1,
3968 		};
3969 
3970 		ret = nl80211_dump_wiphy_parse(skb, cb, &state);
3971 		if (ret)
3972 			goto out_unlock;
3973 
3974 		filter_wiphy = state.filter_wiphy;
3975 
3976 		/*
3977 		 * if filtering, set cb->args[2] to +1 since 0 is the default
3978 		 * value needed to determine that parsing is necessary.
3979 		 */
3980 		if (filter_wiphy >= 0)
3981 			cb->args[2] = filter_wiphy + 1;
3982 		else
3983 			cb->args[2] = -1;
3984 	} else if (cb->args[2] > 0) {
3985 		filter_wiphy = cb->args[2] - 1;
3986 	}
3987 
3988 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3989 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3990 			continue;
3991 		if (wp_idx < wp_start) {
3992 			wp_idx++;
3993 			continue;
3994 		}
3995 
3996 		if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
3997 			continue;
3998 
3999 		if_idx = 0;
4000 
4001 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
4002 			if (if_idx < if_start) {
4003 				if_idx++;
4004 				continue;
4005 			}
4006 			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
4007 					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
4008 					       rdev, wdev,
4009 					       NL80211_CMD_NEW_INTERFACE) < 0) {
4010 				goto out;
4011 			}
4012 			if_idx++;
4013 		}
4014 
4015 		wp_idx++;
4016 	}
4017  out:
4018 	cb->args[0] = wp_idx;
4019 	cb->args[1] = if_idx;
4020 
4021 	ret = skb->len;
4022  out_unlock:
4023 	rtnl_unlock();
4024 
4025 	return ret;
4026 }
4027 
4028 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
4029 {
4030 	struct sk_buff *msg;
4031 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4032 	struct wireless_dev *wdev = info->user_ptr[1];
4033 
4034 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4035 	if (!msg)
4036 		return -ENOMEM;
4037 
4038 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
4039 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
4040 		nlmsg_free(msg);
4041 		return -ENOBUFS;
4042 	}
4043 
4044 	return genlmsg_reply(msg, info);
4045 }
4046 
4047 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
4048 	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
4049 	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
4050 	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
4051 	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
4052 	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
4053 	[NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
4054 };
4055 
4056 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
4057 {
4058 	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
4059 	int flag;
4060 
4061 	*mntrflags = 0;
4062 
4063 	if (!nla)
4064 		return -EINVAL;
4065 
4066 	if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
4067 		return -EINVAL;
4068 
4069 	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
4070 		if (flags[flag])
4071 			*mntrflags |= (1<<flag);
4072 
4073 	*mntrflags |= MONITOR_FLAG_CHANGED;
4074 
4075 	return 0;
4076 }
4077 
4078 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
4079 				     enum nl80211_iftype type,
4080 				     struct genl_info *info,
4081 				     struct vif_params *params)
4082 {
4083 	bool change = false;
4084 	int err;
4085 
4086 	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
4087 		if (type != NL80211_IFTYPE_MONITOR)
4088 			return -EINVAL;
4089 
4090 		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
4091 					  &params->flags);
4092 		if (err)
4093 			return err;
4094 
4095 		change = true;
4096 	}
4097 
4098 	if (params->flags & MONITOR_FLAG_ACTIVE &&
4099 	    !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
4100 		return -EOPNOTSUPP;
4101 
4102 	if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
4103 		const u8 *mumimo_groups;
4104 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4105 
4106 		if (type != NL80211_IFTYPE_MONITOR)
4107 			return -EINVAL;
4108 
4109 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4110 			return -EOPNOTSUPP;
4111 
4112 		mumimo_groups =
4113 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
4114 
4115 		/* bits 0 and 63 are reserved and must be zero */
4116 		if ((mumimo_groups[0] & BIT(0)) ||
4117 		    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
4118 			return -EINVAL;
4119 
4120 		params->vht_mumimo_groups = mumimo_groups;
4121 		change = true;
4122 	}
4123 
4124 	if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
4125 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4126 
4127 		if (type != NL80211_IFTYPE_MONITOR)
4128 			return -EINVAL;
4129 
4130 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4131 			return -EOPNOTSUPP;
4132 
4133 		params->vht_mumimo_follow_addr =
4134 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
4135 		change = true;
4136 	}
4137 
4138 	return change ? 1 : 0;
4139 }
4140 
4141 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
4142 			       struct net_device *netdev, u8 use_4addr,
4143 			       enum nl80211_iftype iftype)
4144 {
4145 	if (!use_4addr) {
4146 		if (netdev && netif_is_bridge_port(netdev))
4147 			return -EBUSY;
4148 		return 0;
4149 	}
4150 
4151 	switch (iftype) {
4152 	case NL80211_IFTYPE_AP_VLAN:
4153 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
4154 			return 0;
4155 		break;
4156 	case NL80211_IFTYPE_STATION:
4157 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
4158 			return 0;
4159 		break;
4160 	default:
4161 		break;
4162 	}
4163 
4164 	return -EOPNOTSUPP;
4165 }
4166 
4167 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
4168 {
4169 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4170 	struct vif_params params;
4171 	int err;
4172 	enum nl80211_iftype otype, ntype;
4173 	struct net_device *dev = info->user_ptr[1];
4174 	bool change = false;
4175 
4176 	memset(&params, 0, sizeof(params));
4177 
4178 	otype = ntype = dev->ieee80211_ptr->iftype;
4179 
4180 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
4181 		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4182 		if (otype != ntype)
4183 			change = true;
4184 	}
4185 
4186 	if (info->attrs[NL80211_ATTR_MESH_ID]) {
4187 		struct wireless_dev *wdev = dev->ieee80211_ptr;
4188 
4189 		if (ntype != NL80211_IFTYPE_MESH_POINT)
4190 			return -EINVAL;
4191 		if (netif_running(dev))
4192 			return -EBUSY;
4193 
4194 		wdev_lock(wdev);
4195 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
4196 			     IEEE80211_MAX_MESH_ID_LEN);
4197 		wdev->u.mesh.id_up_len =
4198 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4199 		memcpy(wdev->u.mesh.id,
4200 		       nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4201 		       wdev->u.mesh.id_up_len);
4202 		wdev_unlock(wdev);
4203 	}
4204 
4205 	if (info->attrs[NL80211_ATTR_4ADDR]) {
4206 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4207 		change = true;
4208 		err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
4209 		if (err)
4210 			return err;
4211 	} else {
4212 		params.use_4addr = -1;
4213 	}
4214 
4215 	err = nl80211_parse_mon_options(rdev, ntype, info, &params);
4216 	if (err < 0)
4217 		return err;
4218 	if (err > 0)
4219 		change = true;
4220 
4221 	if (change)
4222 		err = cfg80211_change_iface(rdev, dev, ntype, &params);
4223 	else
4224 		err = 0;
4225 
4226 	if (!err && params.use_4addr != -1)
4227 		dev->ieee80211_ptr->use_4addr = params.use_4addr;
4228 
4229 	if (change && !err) {
4230 		struct wireless_dev *wdev = dev->ieee80211_ptr;
4231 
4232 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
4233 	}
4234 
4235 	return err;
4236 }
4237 
4238 static int _nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4239 {
4240 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4241 	struct vif_params params;
4242 	struct wireless_dev *wdev;
4243 	struct sk_buff *msg;
4244 	int err;
4245 	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
4246 
4247 	memset(&params, 0, sizeof(params));
4248 
4249 	if (!info->attrs[NL80211_ATTR_IFNAME])
4250 		return -EINVAL;
4251 
4252 	if (info->attrs[NL80211_ATTR_IFTYPE])
4253 		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4254 
4255 	if (!rdev->ops->add_virtual_intf)
4256 		return -EOPNOTSUPP;
4257 
4258 	if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
4259 	     rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
4260 	    info->attrs[NL80211_ATTR_MAC]) {
4261 		nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
4262 			   ETH_ALEN);
4263 		if (!is_valid_ether_addr(params.macaddr))
4264 			return -EADDRNOTAVAIL;
4265 	}
4266 
4267 	if (info->attrs[NL80211_ATTR_4ADDR]) {
4268 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4269 		err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
4270 		if (err)
4271 			return err;
4272 	}
4273 
4274 	if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
4275 		return -EOPNOTSUPP;
4276 
4277 	err = nl80211_parse_mon_options(rdev, type, info, &params);
4278 	if (err < 0)
4279 		return err;
4280 
4281 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4282 	if (!msg)
4283 		return -ENOMEM;
4284 
4285 	wdev = rdev_add_virtual_intf(rdev,
4286 				nla_data(info->attrs[NL80211_ATTR_IFNAME]),
4287 				NET_NAME_USER, type, &params);
4288 	if (WARN_ON(!wdev)) {
4289 		nlmsg_free(msg);
4290 		return -EPROTO;
4291 	} else if (IS_ERR(wdev)) {
4292 		nlmsg_free(msg);
4293 		return PTR_ERR(wdev);
4294 	}
4295 
4296 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
4297 		wdev->owner_nlportid = info->snd_portid;
4298 
4299 	switch (type) {
4300 	case NL80211_IFTYPE_MESH_POINT:
4301 		if (!info->attrs[NL80211_ATTR_MESH_ID])
4302 			break;
4303 		wdev_lock(wdev);
4304 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
4305 			     IEEE80211_MAX_MESH_ID_LEN);
4306 		wdev->u.mesh.id_up_len =
4307 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4308 		memcpy(wdev->u.mesh.id,
4309 		       nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4310 		       wdev->u.mesh.id_up_len);
4311 		wdev_unlock(wdev);
4312 		break;
4313 	case NL80211_IFTYPE_NAN:
4314 	case NL80211_IFTYPE_P2P_DEVICE:
4315 		/*
4316 		 * P2P Device and NAN do not have a netdev, so don't go
4317 		 * through the netdev notifier and must be added here
4318 		 */
4319 		cfg80211_init_wdev(wdev);
4320 		cfg80211_register_wdev(rdev, wdev);
4321 		break;
4322 	default:
4323 		break;
4324 	}
4325 
4326 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
4327 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
4328 		nlmsg_free(msg);
4329 		return -ENOBUFS;
4330 	}
4331 
4332 	return genlmsg_reply(msg, info);
4333 }
4334 
4335 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4336 {
4337 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4338 	int ret;
4339 
4340 	/* to avoid failing a new interface creation due to pending removal */
4341 	cfg80211_destroy_ifaces(rdev);
4342 
4343 	wiphy_lock(&rdev->wiphy);
4344 	ret = _nl80211_new_interface(skb, info);
4345 	wiphy_unlock(&rdev->wiphy);
4346 
4347 	return ret;
4348 }
4349 
4350 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
4351 {
4352 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4353 	struct wireless_dev *wdev = info->user_ptr[1];
4354 
4355 	if (!rdev->ops->del_virtual_intf)
4356 		return -EOPNOTSUPP;
4357 
4358 	/*
4359 	 * We hold RTNL, so this is safe, without RTNL opencount cannot
4360 	 * reach 0, and thus the rdev cannot be deleted.
4361 	 *
4362 	 * We need to do it for the dev_close(), since that will call
4363 	 * the netdev notifiers, and we need to acquire the mutex there
4364 	 * but don't know if we get there from here or from some other
4365 	 * place (e.g. "ip link set ... down").
4366 	 */
4367 	mutex_unlock(&rdev->wiphy.mtx);
4368 
4369 	/*
4370 	 * If we remove a wireless device without a netdev then clear
4371 	 * user_ptr[1] so that nl80211_post_doit won't dereference it
4372 	 * to check if it needs to do dev_put(). Otherwise it crashes
4373 	 * since the wdev has been freed, unlike with a netdev where
4374 	 * we need the dev_put() for the netdev to really be freed.
4375 	 */
4376 	if (!wdev->netdev)
4377 		info->user_ptr[1] = NULL;
4378 	else
4379 		dev_close(wdev->netdev);
4380 
4381 	mutex_lock(&rdev->wiphy.mtx);
4382 
4383 	return cfg80211_remove_virtual_intf(rdev, wdev);
4384 }
4385 
4386 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
4387 {
4388 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4389 	struct net_device *dev = info->user_ptr[1];
4390 	u16 noack_map;
4391 
4392 	if (!info->attrs[NL80211_ATTR_NOACK_MAP])
4393 		return -EINVAL;
4394 
4395 	if (!rdev->ops->set_noack_map)
4396 		return -EOPNOTSUPP;
4397 
4398 	noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
4399 
4400 	return rdev_set_noack_map(rdev, dev, noack_map);
4401 }
4402 
4403 static int nl80211_validate_key_link_id(struct genl_info *info,
4404 					struct wireless_dev *wdev,
4405 					int link_id, bool pairwise)
4406 {
4407 	if (pairwise) {
4408 		if (link_id != -1) {
4409 			GENL_SET_ERR_MSG(info,
4410 					 "link ID not allowed for pairwise key");
4411 			return -EINVAL;
4412 		}
4413 
4414 		return 0;
4415 	}
4416 
4417 	if (wdev->valid_links) {
4418 		if (link_id == -1) {
4419 			GENL_SET_ERR_MSG(info,
4420 					 "link ID must for MLO group key");
4421 			return -EINVAL;
4422 		}
4423 		if (!(wdev->valid_links & BIT(link_id))) {
4424 			GENL_SET_ERR_MSG(info, "invalid link ID for MLO group key");
4425 			return -EINVAL;
4426 		}
4427 	} else if (link_id != -1) {
4428 		GENL_SET_ERR_MSG(info, "link ID not allowed for non-MLO group key");
4429 		return -EINVAL;
4430 	}
4431 
4432 	return 0;
4433 }
4434 
4435 struct get_key_cookie {
4436 	struct sk_buff *msg;
4437 	int error;
4438 	int idx;
4439 };
4440 
4441 static void get_key_callback(void *c, struct key_params *params)
4442 {
4443 	struct nlattr *key;
4444 	struct get_key_cookie *cookie = c;
4445 
4446 	if ((params->key &&
4447 	     nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
4448 		     params->key_len, params->key)) ||
4449 	    (params->seq &&
4450 	     nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
4451 		     params->seq_len, params->seq)) ||
4452 	    (params->cipher &&
4453 	     nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
4454 			 params->cipher)))
4455 		goto nla_put_failure;
4456 
4457 	key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
4458 	if (!key)
4459 		goto nla_put_failure;
4460 
4461 	if ((params->key &&
4462 	     nla_put(cookie->msg, NL80211_KEY_DATA,
4463 		     params->key_len, params->key)) ||
4464 	    (params->seq &&
4465 	     nla_put(cookie->msg, NL80211_KEY_SEQ,
4466 		     params->seq_len, params->seq)) ||
4467 	    (params->cipher &&
4468 	     nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
4469 			 params->cipher)))
4470 		goto nla_put_failure;
4471 
4472 	if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
4473 		goto nla_put_failure;
4474 
4475 	nla_nest_end(cookie->msg, key);
4476 
4477 	return;
4478  nla_put_failure:
4479 	cookie->error = 1;
4480 }
4481 
4482 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
4483 {
4484 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4485 	int err;
4486 	struct net_device *dev = info->user_ptr[1];
4487 	u8 key_idx = 0;
4488 	const u8 *mac_addr = NULL;
4489 	bool pairwise;
4490 	struct get_key_cookie cookie = {
4491 		.error = 0,
4492 	};
4493 	void *hdr;
4494 	struct sk_buff *msg;
4495 	bool bigtk_support = false;
4496 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4497 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4498 
4499 	if (wiphy_ext_feature_isset(&rdev->wiphy,
4500 				    NL80211_EXT_FEATURE_BEACON_PROTECTION))
4501 		bigtk_support = true;
4502 
4503 	if ((wdev->iftype == NL80211_IFTYPE_STATION ||
4504 	     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
4505 	    wiphy_ext_feature_isset(&rdev->wiphy,
4506 				    NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
4507 		bigtk_support = true;
4508 
4509 	if (info->attrs[NL80211_ATTR_KEY_IDX]) {
4510 		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
4511 
4512 		if (key_idx >= 6 && key_idx <= 7 && !bigtk_support) {
4513 			GENL_SET_ERR_MSG(info, "BIGTK not supported");
4514 			return -EINVAL;
4515 		}
4516 	}
4517 
4518 	if (info->attrs[NL80211_ATTR_MAC])
4519 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4520 
4521 	pairwise = !!mac_addr;
4522 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
4523 		u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
4524 
4525 		if (kt != NL80211_KEYTYPE_GROUP &&
4526 		    kt != NL80211_KEYTYPE_PAIRWISE)
4527 			return -EINVAL;
4528 		pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
4529 	}
4530 
4531 	if (!rdev->ops->get_key)
4532 		return -EOPNOTSUPP;
4533 
4534 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4535 		return -ENOENT;
4536 
4537 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4538 	if (!msg)
4539 		return -ENOMEM;
4540 
4541 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
4542 			     NL80211_CMD_NEW_KEY);
4543 	if (!hdr)
4544 		goto nla_put_failure;
4545 
4546 	cookie.msg = msg;
4547 	cookie.idx = key_idx;
4548 
4549 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4550 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
4551 		goto nla_put_failure;
4552 	if (mac_addr &&
4553 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
4554 		goto nla_put_failure;
4555 
4556 	err = nl80211_validate_key_link_id(info, wdev, link_id, pairwise);
4557 	if (err)
4558 		goto free_msg;
4559 
4560 	err = rdev_get_key(rdev, dev, link_id, key_idx, pairwise, mac_addr,
4561 			   &cookie, get_key_callback);
4562 
4563 	if (err)
4564 		goto free_msg;
4565 
4566 	if (cookie.error)
4567 		goto nla_put_failure;
4568 
4569 	genlmsg_end(msg, hdr);
4570 	return genlmsg_reply(msg, info);
4571 
4572  nla_put_failure:
4573 	err = -ENOBUFS;
4574  free_msg:
4575 	nlmsg_free(msg);
4576 	return err;
4577 }
4578 
4579 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
4580 {
4581 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4582 	struct key_parse key;
4583 	int err;
4584 	struct net_device *dev = info->user_ptr[1];
4585 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4586 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4587 
4588 	err = nl80211_parse_key(info, &key);
4589 	if (err)
4590 		return err;
4591 
4592 	if (key.idx < 0)
4593 		return -EINVAL;
4594 
4595 	/* Only support setting default key and
4596 	 * Extended Key ID action NL80211_KEY_SET_TX.
4597 	 */
4598 	if (!key.def && !key.defmgmt && !key.defbeacon &&
4599 	    !(key.p.mode == NL80211_KEY_SET_TX))
4600 		return -EINVAL;
4601 
4602 	wdev_lock(wdev);
4603 
4604 	if (key.def) {
4605 		if (!rdev->ops->set_default_key) {
4606 			err = -EOPNOTSUPP;
4607 			goto out;
4608 		}
4609 
4610 		err = nl80211_key_allowed(wdev);
4611 		if (err)
4612 			goto out;
4613 
4614 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4615 		if (err)
4616 			goto out;
4617 
4618 		err = rdev_set_default_key(rdev, dev, link_id, key.idx,
4619 					   key.def_uni, key.def_multi);
4620 
4621 		if (err)
4622 			goto out;
4623 
4624 #ifdef CONFIG_CFG80211_WEXT
4625 		wdev->wext.default_key = key.idx;
4626 #endif
4627 	} else if (key.defmgmt) {
4628 		if (key.def_uni || !key.def_multi) {
4629 			err = -EINVAL;
4630 			goto out;
4631 		}
4632 
4633 		if (!rdev->ops->set_default_mgmt_key) {
4634 			err = -EOPNOTSUPP;
4635 			goto out;
4636 		}
4637 
4638 		err = nl80211_key_allowed(wdev);
4639 		if (err)
4640 			goto out;
4641 
4642 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4643 		if (err)
4644 			goto out;
4645 
4646 		err = rdev_set_default_mgmt_key(rdev, dev, link_id, key.idx);
4647 		if (err)
4648 			goto out;
4649 
4650 #ifdef CONFIG_CFG80211_WEXT
4651 		wdev->wext.default_mgmt_key = key.idx;
4652 #endif
4653 	} else if (key.defbeacon) {
4654 		if (key.def_uni || !key.def_multi) {
4655 			err = -EINVAL;
4656 			goto out;
4657 		}
4658 
4659 		if (!rdev->ops->set_default_beacon_key) {
4660 			err = -EOPNOTSUPP;
4661 			goto out;
4662 		}
4663 
4664 		err = nl80211_key_allowed(wdev);
4665 		if (err)
4666 			goto out;
4667 
4668 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4669 		if (err)
4670 			goto out;
4671 
4672 		err = rdev_set_default_beacon_key(rdev, dev, link_id, key.idx);
4673 		if (err)
4674 			goto out;
4675 	} else if (key.p.mode == NL80211_KEY_SET_TX &&
4676 		   wiphy_ext_feature_isset(&rdev->wiphy,
4677 					   NL80211_EXT_FEATURE_EXT_KEY_ID)) {
4678 		u8 *mac_addr = NULL;
4679 
4680 		if (info->attrs[NL80211_ATTR_MAC])
4681 			mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4682 
4683 		if (!mac_addr || key.idx < 0 || key.idx > 1) {
4684 			err = -EINVAL;
4685 			goto out;
4686 		}
4687 
4688 		err = nl80211_validate_key_link_id(info, wdev, link_id, true);
4689 		if (err)
4690 			goto out;
4691 
4692 		err = rdev_add_key(rdev, dev, link_id, key.idx,
4693 				   NL80211_KEYTYPE_PAIRWISE,
4694 				   mac_addr, &key.p);
4695 	} else {
4696 		err = -EINVAL;
4697 	}
4698  out:
4699 	wdev_unlock(wdev);
4700 
4701 	return err;
4702 }
4703 
4704 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
4705 {
4706 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4707 	int err;
4708 	struct net_device *dev = info->user_ptr[1];
4709 	struct key_parse key;
4710 	const u8 *mac_addr = NULL;
4711 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4712 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4713 
4714 	err = nl80211_parse_key(info, &key);
4715 	if (err)
4716 		return err;
4717 
4718 	if (!key.p.key) {
4719 		GENL_SET_ERR_MSG(info, "no key");
4720 		return -EINVAL;
4721 	}
4722 
4723 	if (info->attrs[NL80211_ATTR_MAC])
4724 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4725 
4726 	if (key.type == -1) {
4727 		if (mac_addr)
4728 			key.type = NL80211_KEYTYPE_PAIRWISE;
4729 		else
4730 			key.type = NL80211_KEYTYPE_GROUP;
4731 	}
4732 
4733 	/* for now */
4734 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4735 	    key.type != NL80211_KEYTYPE_GROUP) {
4736 		GENL_SET_ERR_MSG(info, "key type not pairwise or group");
4737 		return -EINVAL;
4738 	}
4739 
4740 	if (key.type == NL80211_KEYTYPE_GROUP &&
4741 	    info->attrs[NL80211_ATTR_VLAN_ID])
4742 		key.p.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
4743 
4744 	if (!rdev->ops->add_key)
4745 		return -EOPNOTSUPP;
4746 
4747 	if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
4748 					   key.type == NL80211_KEYTYPE_PAIRWISE,
4749 					   mac_addr)) {
4750 		GENL_SET_ERR_MSG(info, "key setting validation failed");
4751 		return -EINVAL;
4752 	}
4753 
4754 	wdev_lock(wdev);
4755 	err = nl80211_key_allowed(wdev);
4756 	if (err)
4757 		GENL_SET_ERR_MSG(info, "key not allowed");
4758 
4759 	if (!err)
4760 		err = nl80211_validate_key_link_id(info, wdev, link_id,
4761 				key.type == NL80211_KEYTYPE_PAIRWISE);
4762 
4763 	if (!err) {
4764 		err = rdev_add_key(rdev, dev, link_id, key.idx,
4765 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4766 				    mac_addr, &key.p);
4767 		if (err)
4768 			GENL_SET_ERR_MSG(info, "key addition failed");
4769 	}
4770 	wdev_unlock(wdev);
4771 
4772 	return err;
4773 }
4774 
4775 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
4776 {
4777 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4778 	int err;
4779 	struct net_device *dev = info->user_ptr[1];
4780 	u8 *mac_addr = NULL;
4781 	struct key_parse key;
4782 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4783 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4784 
4785 	err = nl80211_parse_key(info, &key);
4786 	if (err)
4787 		return err;
4788 
4789 	if (info->attrs[NL80211_ATTR_MAC])
4790 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4791 
4792 	if (key.type == -1) {
4793 		if (mac_addr)
4794 			key.type = NL80211_KEYTYPE_PAIRWISE;
4795 		else
4796 			key.type = NL80211_KEYTYPE_GROUP;
4797 	}
4798 
4799 	/* for now */
4800 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4801 	    key.type != NL80211_KEYTYPE_GROUP)
4802 		return -EINVAL;
4803 
4804 	if (!cfg80211_valid_key_idx(rdev, key.idx,
4805 				    key.type == NL80211_KEYTYPE_PAIRWISE))
4806 		return -EINVAL;
4807 
4808 	if (!rdev->ops->del_key)
4809 		return -EOPNOTSUPP;
4810 
4811 	wdev_lock(wdev);
4812 	err = nl80211_key_allowed(wdev);
4813 
4814 	if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
4815 	    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4816 		err = -ENOENT;
4817 
4818 	if (!err)
4819 		err = nl80211_validate_key_link_id(info, wdev, link_id,
4820 				key.type == NL80211_KEYTYPE_PAIRWISE);
4821 
4822 	if (!err)
4823 		err = rdev_del_key(rdev, dev, link_id, key.idx,
4824 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4825 				   mac_addr);
4826 
4827 #ifdef CONFIG_CFG80211_WEXT
4828 	if (!err) {
4829 		if (key.idx == wdev->wext.default_key)
4830 			wdev->wext.default_key = -1;
4831 		else if (key.idx == wdev->wext.default_mgmt_key)
4832 			wdev->wext.default_mgmt_key = -1;
4833 	}
4834 #endif
4835 	wdev_unlock(wdev);
4836 
4837 	return err;
4838 }
4839 
4840 /* This function returns an error or the number of nested attributes */
4841 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
4842 {
4843 	struct nlattr *attr;
4844 	int n_entries = 0, tmp;
4845 
4846 	nla_for_each_nested(attr, nl_attr, tmp) {
4847 		if (nla_len(attr) != ETH_ALEN)
4848 			return -EINVAL;
4849 
4850 		n_entries++;
4851 	}
4852 
4853 	return n_entries;
4854 }
4855 
4856 /*
4857  * This function parses ACL information and allocates memory for ACL data.
4858  * On successful return, the calling function is responsible to free the
4859  * ACL buffer returned by this function.
4860  */
4861 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
4862 						struct genl_info *info)
4863 {
4864 	enum nl80211_acl_policy acl_policy;
4865 	struct nlattr *attr;
4866 	struct cfg80211_acl_data *acl;
4867 	int i = 0, n_entries, tmp;
4868 
4869 	if (!wiphy->max_acl_mac_addrs)
4870 		return ERR_PTR(-EOPNOTSUPP);
4871 
4872 	if (!info->attrs[NL80211_ATTR_ACL_POLICY])
4873 		return ERR_PTR(-EINVAL);
4874 
4875 	acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
4876 	if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
4877 	    acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
4878 		return ERR_PTR(-EINVAL);
4879 
4880 	if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
4881 		return ERR_PTR(-EINVAL);
4882 
4883 	n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
4884 	if (n_entries < 0)
4885 		return ERR_PTR(n_entries);
4886 
4887 	if (n_entries > wiphy->max_acl_mac_addrs)
4888 		return ERR_PTR(-ENOTSUPP);
4889 
4890 	acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4891 	if (!acl)
4892 		return ERR_PTR(-ENOMEM);
4893 	acl->n_acl_entries = n_entries;
4894 
4895 	nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4896 		memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4897 		i++;
4898 	}
4899 	acl->acl_policy = acl_policy;
4900 
4901 	return acl;
4902 }
4903 
4904 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4905 {
4906 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4907 	struct net_device *dev = info->user_ptr[1];
4908 	struct cfg80211_acl_data *acl;
4909 	int err;
4910 
4911 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4912 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4913 		return -EOPNOTSUPP;
4914 
4915 	if (!dev->ieee80211_ptr->links[0].ap.beacon_interval)
4916 		return -EINVAL;
4917 
4918 	acl = parse_acl_data(&rdev->wiphy, info);
4919 	if (IS_ERR(acl))
4920 		return PTR_ERR(acl);
4921 
4922 	err = rdev_set_mac_acl(rdev, dev, acl);
4923 
4924 	kfree(acl);
4925 
4926 	return err;
4927 }
4928 
4929 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4930 			   u8 *rates, u8 rates_len)
4931 {
4932 	u8 i;
4933 	u32 mask = 0;
4934 
4935 	for (i = 0; i < rates_len; i++) {
4936 		int rate = (rates[i] & 0x7f) * 5;
4937 		int ridx;
4938 
4939 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4940 			struct ieee80211_rate *srate =
4941 				&sband->bitrates[ridx];
4942 			if (rate == srate->bitrate) {
4943 				mask |= 1 << ridx;
4944 				break;
4945 			}
4946 		}
4947 		if (ridx == sband->n_bitrates)
4948 			return 0; /* rate not found */
4949 	}
4950 
4951 	return mask;
4952 }
4953 
4954 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4955 			       u8 *rates, u8 rates_len,
4956 			       u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4957 {
4958 	u8 i;
4959 
4960 	memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4961 
4962 	for (i = 0; i < rates_len; i++) {
4963 		int ridx, rbit;
4964 
4965 		ridx = rates[i] / 8;
4966 		rbit = BIT(rates[i] % 8);
4967 
4968 		/* check validity */
4969 		if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4970 			return false;
4971 
4972 		/* check availability */
4973 		ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
4974 		if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
4975 			mcs[ridx] |= rbit;
4976 		else
4977 			return false;
4978 	}
4979 
4980 	return true;
4981 }
4982 
4983 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
4984 {
4985 	u16 mcs_mask = 0;
4986 
4987 	switch (vht_mcs_map) {
4988 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
4989 		break;
4990 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
4991 		mcs_mask = 0x00FF;
4992 		break;
4993 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
4994 		mcs_mask = 0x01FF;
4995 		break;
4996 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
4997 		mcs_mask = 0x03FF;
4998 		break;
4999 	default:
5000 		break;
5001 	}
5002 
5003 	return mcs_mask;
5004 }
5005 
5006 static void vht_build_mcs_mask(u16 vht_mcs_map,
5007 			       u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
5008 {
5009 	u8 nss;
5010 
5011 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
5012 		vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
5013 		vht_mcs_map >>= 2;
5014 	}
5015 }
5016 
5017 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
5018 			     struct nl80211_txrate_vht *txrate,
5019 			     u16 mcs[NL80211_VHT_NSS_MAX])
5020 {
5021 	u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5022 	u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
5023 	u8 i;
5024 
5025 	if (!sband->vht_cap.vht_supported)
5026 		return false;
5027 
5028 	memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
5029 
5030 	/* Build vht_mcs_mask from VHT capabilities */
5031 	vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
5032 
5033 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5034 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
5035 			mcs[i] = txrate->mcs[i];
5036 		else
5037 			return false;
5038 	}
5039 
5040 	return true;
5041 }
5042 
5043 static u16 he_mcs_map_to_mcs_mask(u8 he_mcs_map)
5044 {
5045 	switch (he_mcs_map) {
5046 	case IEEE80211_HE_MCS_NOT_SUPPORTED:
5047 		return 0;
5048 	case IEEE80211_HE_MCS_SUPPORT_0_7:
5049 		return 0x00FF;
5050 	case IEEE80211_HE_MCS_SUPPORT_0_9:
5051 		return 0x03FF;
5052 	case IEEE80211_HE_MCS_SUPPORT_0_11:
5053 		return 0xFFF;
5054 	default:
5055 		break;
5056 	}
5057 	return 0;
5058 }
5059 
5060 static void he_build_mcs_mask(u16 he_mcs_map,
5061 			      u16 he_mcs_mask[NL80211_HE_NSS_MAX])
5062 {
5063 	u8 nss;
5064 
5065 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
5066 		he_mcs_mask[nss] = he_mcs_map_to_mcs_mask(he_mcs_map & 0x03);
5067 		he_mcs_map >>= 2;
5068 	}
5069 }
5070 
5071 static u16 he_get_txmcsmap(struct genl_info *info, unsigned int link_id,
5072 			   const struct ieee80211_sta_he_cap *he_cap)
5073 {
5074 	struct net_device *dev = info->user_ptr[1];
5075 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5076 	struct cfg80211_chan_def *chandef;
5077 	__le16 tx_mcs;
5078 
5079 	chandef = wdev_chandef(wdev, link_id);
5080 	if (!chandef) {
5081 		/*
5082 		 * This is probably broken, but we never maintained
5083 		 * a chandef in these cases, so it always was.
5084 		 */
5085 		return le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
5086 	}
5087 
5088 	switch (chandef->width) {
5089 	case NL80211_CHAN_WIDTH_80P80:
5090 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80p80;
5091 		break;
5092 	case NL80211_CHAN_WIDTH_160:
5093 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_160;
5094 		break;
5095 	default:
5096 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80;
5097 		break;
5098 	}
5099 
5100 	return le16_to_cpu(tx_mcs);
5101 }
5102 
5103 static bool he_set_mcs_mask(struct genl_info *info,
5104 			    struct wireless_dev *wdev,
5105 			    struct ieee80211_supported_band *sband,
5106 			    struct nl80211_txrate_he *txrate,
5107 			    u16 mcs[NL80211_HE_NSS_MAX],
5108 			    unsigned int link_id)
5109 {
5110 	const struct ieee80211_sta_he_cap *he_cap;
5111 	u16 tx_mcs_mask[NL80211_HE_NSS_MAX] = {};
5112 	u16 tx_mcs_map = 0;
5113 	u8 i;
5114 
5115 	he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5116 	if (!he_cap)
5117 		return false;
5118 
5119 	memset(mcs, 0, sizeof(u16) * NL80211_HE_NSS_MAX);
5120 
5121 	tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5122 
5123 	/* Build he_mcs_mask from HE capabilities */
5124 	he_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
5125 
5126 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5127 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
5128 			mcs[i] = txrate->mcs[i];
5129 		else
5130 			return false;
5131 	}
5132 
5133 	return true;
5134 }
5135 
5136 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
5137 					 struct nlattr *attrs[],
5138 					 enum nl80211_attrs attr,
5139 					 struct cfg80211_bitrate_mask *mask,
5140 					 struct net_device *dev,
5141 					 bool default_all_enabled,
5142 					 unsigned int link_id)
5143 {
5144 	struct nlattr *tb[NL80211_TXRATE_MAX + 1];
5145 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5146 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5147 	int rem, i;
5148 	struct nlattr *tx_rates;
5149 	struct ieee80211_supported_band *sband;
5150 	u16 vht_tx_mcs_map, he_tx_mcs_map;
5151 
5152 	memset(mask, 0, sizeof(*mask));
5153 	/* Default to all rates enabled */
5154 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
5155 		const struct ieee80211_sta_he_cap *he_cap;
5156 
5157 		if (!default_all_enabled)
5158 			break;
5159 
5160 		sband = rdev->wiphy.bands[i];
5161 
5162 		if (!sband)
5163 			continue;
5164 
5165 		mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
5166 		memcpy(mask->control[i].ht_mcs,
5167 		       sband->ht_cap.mcs.rx_mask,
5168 		       sizeof(mask->control[i].ht_mcs));
5169 
5170 		if (sband->vht_cap.vht_supported) {
5171 			vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5172 			vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
5173 		}
5174 
5175 		he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5176 		if (!he_cap)
5177 			continue;
5178 
5179 		he_tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5180 		he_build_mcs_mask(he_tx_mcs_map, mask->control[i].he_mcs);
5181 
5182 		mask->control[i].he_gi = 0xFF;
5183 		mask->control[i].he_ltf = 0xFF;
5184 	}
5185 
5186 	/* if no rates are given set it back to the defaults */
5187 	if (!attrs[attr])
5188 		goto out;
5189 
5190 	/* The nested attribute uses enum nl80211_band as the index. This maps
5191 	 * directly to the enum nl80211_band values used in cfg80211.
5192 	 */
5193 	BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
5194 	nla_for_each_nested(tx_rates, attrs[attr], rem) {
5195 		enum nl80211_band band = nla_type(tx_rates);
5196 		int err;
5197 
5198 		if (band < 0 || band >= NUM_NL80211_BANDS)
5199 			return -EINVAL;
5200 		sband = rdev->wiphy.bands[band];
5201 		if (sband == NULL)
5202 			return -EINVAL;
5203 		err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
5204 						  tx_rates,
5205 						  nl80211_txattr_policy,
5206 						  info->extack);
5207 		if (err)
5208 			return err;
5209 		if (tb[NL80211_TXRATE_LEGACY]) {
5210 			mask->control[band].legacy = rateset_to_mask(
5211 				sband,
5212 				nla_data(tb[NL80211_TXRATE_LEGACY]),
5213 				nla_len(tb[NL80211_TXRATE_LEGACY]));
5214 			if ((mask->control[band].legacy == 0) &&
5215 			    nla_len(tb[NL80211_TXRATE_LEGACY]))
5216 				return -EINVAL;
5217 		}
5218 		if (tb[NL80211_TXRATE_HT]) {
5219 			if (!ht_rateset_to_mask(
5220 					sband,
5221 					nla_data(tb[NL80211_TXRATE_HT]),
5222 					nla_len(tb[NL80211_TXRATE_HT]),
5223 					mask->control[band].ht_mcs))
5224 				return -EINVAL;
5225 		}
5226 
5227 		if (tb[NL80211_TXRATE_VHT]) {
5228 			if (!vht_set_mcs_mask(
5229 					sband,
5230 					nla_data(tb[NL80211_TXRATE_VHT]),
5231 					mask->control[band].vht_mcs))
5232 				return -EINVAL;
5233 		}
5234 
5235 		if (tb[NL80211_TXRATE_GI]) {
5236 			mask->control[band].gi =
5237 				nla_get_u8(tb[NL80211_TXRATE_GI]);
5238 			if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
5239 				return -EINVAL;
5240 		}
5241 		if (tb[NL80211_TXRATE_HE] &&
5242 		    !he_set_mcs_mask(info, wdev, sband,
5243 				     nla_data(tb[NL80211_TXRATE_HE]),
5244 				     mask->control[band].he_mcs,
5245 				     link_id))
5246 			return -EINVAL;
5247 
5248 		if (tb[NL80211_TXRATE_HE_GI])
5249 			mask->control[band].he_gi =
5250 				nla_get_u8(tb[NL80211_TXRATE_HE_GI]);
5251 		if (tb[NL80211_TXRATE_HE_LTF])
5252 			mask->control[band].he_ltf =
5253 				nla_get_u8(tb[NL80211_TXRATE_HE_LTF]);
5254 
5255 		if (mask->control[band].legacy == 0) {
5256 			/* don't allow empty legacy rates if HT, VHT or HE
5257 			 * are not even supported.
5258 			 */
5259 			if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
5260 			      rdev->wiphy.bands[band]->vht_cap.vht_supported ||
5261 			      ieee80211_get_he_iftype_cap(sband, wdev->iftype)))
5262 				return -EINVAL;
5263 
5264 			for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5265 				if (mask->control[band].ht_mcs[i])
5266 					goto out;
5267 
5268 			for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5269 				if (mask->control[band].vht_mcs[i])
5270 					goto out;
5271 
5272 			for (i = 0; i < NL80211_HE_NSS_MAX; i++)
5273 				if (mask->control[band].he_mcs[i])
5274 					goto out;
5275 
5276 			/* legacy and mcs rates may not be both empty */
5277 			return -EINVAL;
5278 		}
5279 	}
5280 
5281 out:
5282 	return 0;
5283 }
5284 
5285 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
5286 				   enum nl80211_band band,
5287 				   struct cfg80211_bitrate_mask *beacon_rate)
5288 {
5289 	u32 count_ht, count_vht, count_he, i;
5290 	u32 rate = beacon_rate->control[band].legacy;
5291 
5292 	/* Allow only one rate */
5293 	if (hweight32(rate) > 1)
5294 		return -EINVAL;
5295 
5296 	count_ht = 0;
5297 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
5298 		if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
5299 			return -EINVAL;
5300 		} else if (beacon_rate->control[band].ht_mcs[i]) {
5301 			count_ht++;
5302 			if (count_ht > 1)
5303 				return -EINVAL;
5304 		}
5305 		if (count_ht && rate)
5306 			return -EINVAL;
5307 	}
5308 
5309 	count_vht = 0;
5310 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5311 		if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
5312 			return -EINVAL;
5313 		} else if (beacon_rate->control[band].vht_mcs[i]) {
5314 			count_vht++;
5315 			if (count_vht > 1)
5316 				return -EINVAL;
5317 		}
5318 		if (count_vht && rate)
5319 			return -EINVAL;
5320 	}
5321 
5322 	count_he = 0;
5323 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5324 		if (hweight16(beacon_rate->control[band].he_mcs[i]) > 1) {
5325 			return -EINVAL;
5326 		} else if (beacon_rate->control[band].he_mcs[i]) {
5327 			count_he++;
5328 			if (count_he > 1)
5329 				return -EINVAL;
5330 		}
5331 		if (count_he && rate)
5332 			return -EINVAL;
5333 	}
5334 
5335 	if ((count_ht && count_vht && count_he) ||
5336 	    (!rate && !count_ht && !count_vht && !count_he))
5337 		return -EINVAL;
5338 
5339 	if (rate &&
5340 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5341 				     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
5342 		return -EINVAL;
5343 	if (count_ht &&
5344 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5345 				     NL80211_EXT_FEATURE_BEACON_RATE_HT))
5346 		return -EINVAL;
5347 	if (count_vht &&
5348 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5349 				     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
5350 		return -EINVAL;
5351 	if (count_he &&
5352 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5353 				     NL80211_EXT_FEATURE_BEACON_RATE_HE))
5354 		return -EINVAL;
5355 
5356 	return 0;
5357 }
5358 
5359 static int nl80211_parse_mbssid_config(struct wiphy *wiphy,
5360 				       struct net_device *dev,
5361 				       struct nlattr *attrs,
5362 				       struct cfg80211_mbssid_config *config,
5363 				       u8 num_elems)
5364 {
5365 	struct nlattr *tb[NL80211_MBSSID_CONFIG_ATTR_MAX + 1];
5366 
5367 	if (!wiphy->mbssid_max_interfaces)
5368 		return -EOPNOTSUPP;
5369 
5370 	if (nla_parse_nested(tb, NL80211_MBSSID_CONFIG_ATTR_MAX, attrs, NULL,
5371 			     NULL) ||
5372 	    !tb[NL80211_MBSSID_CONFIG_ATTR_INDEX])
5373 		return -EINVAL;
5374 
5375 	config->ema = nla_get_flag(tb[NL80211_MBSSID_CONFIG_ATTR_EMA]);
5376 	if (config->ema) {
5377 		if (!wiphy->ema_max_profile_periodicity)
5378 			return -EOPNOTSUPP;
5379 
5380 		if (num_elems > wiphy->ema_max_profile_periodicity)
5381 			return -EINVAL;
5382 	}
5383 
5384 	config->index = nla_get_u8(tb[NL80211_MBSSID_CONFIG_ATTR_INDEX]);
5385 	if (config->index >= wiphy->mbssid_max_interfaces ||
5386 	    (!config->index && !num_elems))
5387 		return -EINVAL;
5388 
5389 	if (tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]) {
5390 		u32 tx_ifindex =
5391 			nla_get_u32(tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]);
5392 
5393 		if ((!config->index && tx_ifindex != dev->ifindex) ||
5394 		    (config->index && tx_ifindex == dev->ifindex))
5395 			return -EINVAL;
5396 
5397 		if (tx_ifindex != dev->ifindex) {
5398 			struct net_device *tx_netdev =
5399 				dev_get_by_index(wiphy_net(wiphy), tx_ifindex);
5400 
5401 			if (!tx_netdev || !tx_netdev->ieee80211_ptr ||
5402 			    tx_netdev->ieee80211_ptr->wiphy != wiphy ||
5403 			    tx_netdev->ieee80211_ptr->iftype !=
5404 							NL80211_IFTYPE_AP) {
5405 				dev_put(tx_netdev);
5406 				return -EINVAL;
5407 			}
5408 
5409 			config->tx_wdev = tx_netdev->ieee80211_ptr;
5410 		} else {
5411 			config->tx_wdev = dev->ieee80211_ptr;
5412 		}
5413 	} else if (!config->index) {
5414 		config->tx_wdev = dev->ieee80211_ptr;
5415 	} else {
5416 		return -EINVAL;
5417 	}
5418 
5419 	return 0;
5420 }
5421 
5422 static struct cfg80211_mbssid_elems *
5423 nl80211_parse_mbssid_elems(struct wiphy *wiphy, struct nlattr *attrs)
5424 {
5425 	struct nlattr *nl_elems;
5426 	struct cfg80211_mbssid_elems *elems;
5427 	int rem_elems;
5428 	u8 i = 0, num_elems = 0;
5429 
5430 	if (!wiphy->mbssid_max_interfaces)
5431 		return ERR_PTR(-EINVAL);
5432 
5433 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5434 		if (num_elems >= 255)
5435 			return ERR_PTR(-EINVAL);
5436 		num_elems++;
5437 	}
5438 
5439 	elems = kzalloc(struct_size(elems, elem, num_elems), GFP_KERNEL);
5440 	if (!elems)
5441 		return ERR_PTR(-ENOMEM);
5442 	elems->cnt = num_elems;
5443 
5444 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5445 		elems->elem[i].data = nla_data(nl_elems);
5446 		elems->elem[i].len = nla_len(nl_elems);
5447 		i++;
5448 	}
5449 	return elems;
5450 }
5451 
5452 static struct cfg80211_rnr_elems *
5453 nl80211_parse_rnr_elems(struct wiphy *wiphy, struct nlattr *attrs,
5454 			struct netlink_ext_ack *extack)
5455 {
5456 	struct nlattr *nl_elems;
5457 	struct cfg80211_rnr_elems *elems;
5458 	int rem_elems;
5459 	u8 i = 0, num_elems = 0;
5460 
5461 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5462 		int ret;
5463 
5464 		ret = validate_ie_attr(nl_elems, extack);
5465 		if (ret)
5466 			return ERR_PTR(ret);
5467 
5468 		num_elems++;
5469 	}
5470 
5471 	elems = kzalloc(struct_size(elems, elem, num_elems), GFP_KERNEL);
5472 	if (!elems)
5473 		return ERR_PTR(-ENOMEM);
5474 	elems->cnt = num_elems;
5475 
5476 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5477 		elems->elem[i].data = nla_data(nl_elems);
5478 		elems->elem[i].len = nla_len(nl_elems);
5479 		i++;
5480 	}
5481 	return elems;
5482 }
5483 
5484 static int nl80211_parse_he_bss_color(struct nlattr *attrs,
5485 				      struct cfg80211_he_bss_color *he_bss_color)
5486 {
5487 	struct nlattr *tb[NL80211_HE_BSS_COLOR_ATTR_MAX + 1];
5488 	int err;
5489 
5490 	err = nla_parse_nested(tb, NL80211_HE_BSS_COLOR_ATTR_MAX, attrs,
5491 			       he_bss_color_policy, NULL);
5492 	if (err)
5493 		return err;
5494 
5495 	if (!tb[NL80211_HE_BSS_COLOR_ATTR_COLOR])
5496 		return -EINVAL;
5497 
5498 	he_bss_color->color =
5499 		nla_get_u8(tb[NL80211_HE_BSS_COLOR_ATTR_COLOR]);
5500 	he_bss_color->enabled =
5501 		!nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_DISABLED]);
5502 	he_bss_color->partial =
5503 		nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_PARTIAL]);
5504 
5505 	return 0;
5506 }
5507 
5508 static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
5509 				struct nlattr *attrs[],
5510 				struct cfg80211_beacon_data *bcn,
5511 				struct netlink_ext_ack *extack)
5512 {
5513 	bool haveinfo = false;
5514 	int err;
5515 
5516 	memset(bcn, 0, sizeof(*bcn));
5517 
5518 	bcn->link_id = nl80211_link_id(attrs);
5519 
5520 	if (attrs[NL80211_ATTR_BEACON_HEAD]) {
5521 		bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
5522 		bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
5523 		if (!bcn->head_len)
5524 			return -EINVAL;
5525 		haveinfo = true;
5526 	}
5527 
5528 	if (attrs[NL80211_ATTR_BEACON_TAIL]) {
5529 		bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
5530 		bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
5531 		haveinfo = true;
5532 	}
5533 
5534 	if (!haveinfo)
5535 		return -EINVAL;
5536 
5537 	if (attrs[NL80211_ATTR_IE]) {
5538 		bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
5539 		bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
5540 	}
5541 
5542 	if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
5543 		bcn->proberesp_ies =
5544 			nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5545 		bcn->proberesp_ies_len =
5546 			nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5547 	}
5548 
5549 	if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
5550 		bcn->assocresp_ies =
5551 			nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5552 		bcn->assocresp_ies_len =
5553 			nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5554 	}
5555 
5556 	if (attrs[NL80211_ATTR_PROBE_RESP]) {
5557 		bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
5558 		bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
5559 	}
5560 
5561 	if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
5562 		struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
5563 
5564 		err = nla_parse_nested_deprecated(tb,
5565 						  NL80211_FTM_RESP_ATTR_MAX,
5566 						  attrs[NL80211_ATTR_FTM_RESPONDER],
5567 						  NULL, NULL);
5568 		if (err)
5569 			return err;
5570 
5571 		if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
5572 		    wiphy_ext_feature_isset(&rdev->wiphy,
5573 					    NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
5574 			bcn->ftm_responder = 1;
5575 		else
5576 			return -EOPNOTSUPP;
5577 
5578 		if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
5579 			bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
5580 			bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
5581 		}
5582 
5583 		if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
5584 			bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5585 			bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5586 		}
5587 	} else {
5588 		bcn->ftm_responder = -1;
5589 	}
5590 
5591 	if (attrs[NL80211_ATTR_HE_BSS_COLOR]) {
5592 		err = nl80211_parse_he_bss_color(attrs[NL80211_ATTR_HE_BSS_COLOR],
5593 						 &bcn->he_bss_color);
5594 		if (err)
5595 			return err;
5596 		bcn->he_bss_color_valid = true;
5597 	}
5598 
5599 	if (attrs[NL80211_ATTR_MBSSID_ELEMS]) {
5600 		struct cfg80211_mbssid_elems *mbssid =
5601 			nl80211_parse_mbssid_elems(&rdev->wiphy,
5602 						   attrs[NL80211_ATTR_MBSSID_ELEMS]);
5603 
5604 		if (IS_ERR(mbssid))
5605 			return PTR_ERR(mbssid);
5606 
5607 		bcn->mbssid_ies = mbssid;
5608 
5609 		if (bcn->mbssid_ies && attrs[NL80211_ATTR_EMA_RNR_ELEMS]) {
5610 			struct cfg80211_rnr_elems *rnr =
5611 				nl80211_parse_rnr_elems(&rdev->wiphy,
5612 							attrs[NL80211_ATTR_EMA_RNR_ELEMS],
5613 							extack);
5614 
5615 			if (IS_ERR(rnr))
5616 				return PTR_ERR(rnr);
5617 
5618 			if (rnr && rnr->cnt < bcn->mbssid_ies->cnt)
5619 				return -EINVAL;
5620 
5621 			bcn->rnr_ies = rnr;
5622 		}
5623 	}
5624 
5625 	return 0;
5626 }
5627 
5628 static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
5629 				    struct ieee80211_he_obss_pd *he_obss_pd)
5630 {
5631 	struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
5632 	int err;
5633 
5634 	err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
5635 			       he_obss_pd_policy, NULL);
5636 	if (err)
5637 		return err;
5638 
5639 	if (!tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL])
5640 		return -EINVAL;
5641 
5642 	he_obss_pd->sr_ctrl = nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL]);
5643 
5644 	if (tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET])
5645 		he_obss_pd->min_offset =
5646 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
5647 	if (tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
5648 		he_obss_pd->max_offset =
5649 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
5650 	if (tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET])
5651 		he_obss_pd->non_srg_max_offset =
5652 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET]);
5653 
5654 	if (he_obss_pd->min_offset > he_obss_pd->max_offset)
5655 		return -EINVAL;
5656 
5657 	if (tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP])
5658 		memcpy(he_obss_pd->bss_color_bitmap,
5659 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP]),
5660 		       sizeof(he_obss_pd->bss_color_bitmap));
5661 
5662 	if (tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP])
5663 		memcpy(he_obss_pd->partial_bssid_bitmap,
5664 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP]),
5665 		       sizeof(he_obss_pd->partial_bssid_bitmap));
5666 
5667 	he_obss_pd->enable = true;
5668 
5669 	return 0;
5670 }
5671 
5672 static int nl80211_parse_fils_discovery(struct cfg80211_registered_device *rdev,
5673 					struct nlattr *attrs,
5674 					struct cfg80211_ap_settings *params)
5675 {
5676 	struct nlattr *tb[NL80211_FILS_DISCOVERY_ATTR_MAX + 1];
5677 	int ret;
5678 	struct cfg80211_fils_discovery *fd = &params->fils_discovery;
5679 
5680 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5681 				     NL80211_EXT_FEATURE_FILS_DISCOVERY))
5682 		return -EINVAL;
5683 
5684 	ret = nla_parse_nested(tb, NL80211_FILS_DISCOVERY_ATTR_MAX, attrs,
5685 			       NULL, NULL);
5686 	if (ret)
5687 		return ret;
5688 
5689 	if (!tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] ||
5690 	    !tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] ||
5691 	    !tb[NL80211_FILS_DISCOVERY_ATTR_TMPL])
5692 		return -EINVAL;
5693 
5694 	fd->tmpl_len = nla_len(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5695 	fd->tmpl = nla_data(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5696 	fd->min_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN]);
5697 	fd->max_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX]);
5698 
5699 	return 0;
5700 }
5701 
5702 static int
5703 nl80211_parse_unsol_bcast_probe_resp(struct cfg80211_registered_device *rdev,
5704 				     struct nlattr *attrs,
5705 				     struct cfg80211_ap_settings *params)
5706 {
5707 	struct nlattr *tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1];
5708 	int ret;
5709 	struct cfg80211_unsol_bcast_probe_resp *presp =
5710 					&params->unsol_bcast_probe_resp;
5711 
5712 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5713 				     NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP))
5714 		return -EINVAL;
5715 
5716 	ret = nla_parse_nested(tb, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX,
5717 			       attrs, NULL, NULL);
5718 	if (ret)
5719 		return ret;
5720 
5721 	if (!tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] ||
5722 	    !tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL])
5723 		return -EINVAL;
5724 
5725 	presp->tmpl = nla_data(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5726 	presp->tmpl_len = nla_len(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5727 	presp->interval = nla_get_u32(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT]);
5728 	return 0;
5729 }
5730 
5731 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
5732 					    const struct element *rates)
5733 {
5734 	int i;
5735 
5736 	if (!rates)
5737 		return;
5738 
5739 	for (i = 0; i < rates->datalen; i++) {
5740 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
5741 			params->ht_required = true;
5742 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
5743 			params->vht_required = true;
5744 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HE_PHY)
5745 			params->he_required = true;
5746 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_SAE_H2E)
5747 			params->sae_h2e_required = true;
5748 	}
5749 }
5750 
5751 /*
5752  * Since the nl80211 API didn't include, from the beginning, attributes about
5753  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
5754  * benefit of drivers that rebuild IEs in the firmware.
5755  */
5756 static int nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
5757 {
5758 	const struct cfg80211_beacon_data *bcn = &params->beacon;
5759 	size_t ies_len = bcn->tail_len;
5760 	const u8 *ies = bcn->tail;
5761 	const struct element *rates;
5762 	const struct element *cap;
5763 
5764 	rates = cfg80211_find_elem(WLAN_EID_SUPP_RATES, ies, ies_len);
5765 	nl80211_check_ap_rate_selectors(params, rates);
5766 
5767 	rates = cfg80211_find_elem(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
5768 	nl80211_check_ap_rate_selectors(params, rates);
5769 
5770 	cap = cfg80211_find_elem(WLAN_EID_HT_CAPABILITY, ies, ies_len);
5771 	if (cap && cap->datalen >= sizeof(*params->ht_cap))
5772 		params->ht_cap = (void *)cap->data;
5773 	cap = cfg80211_find_elem(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
5774 	if (cap && cap->datalen >= sizeof(*params->vht_cap))
5775 		params->vht_cap = (void *)cap->data;
5776 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
5777 	if (cap && cap->datalen >= sizeof(*params->he_cap) + 1)
5778 		params->he_cap = (void *)(cap->data + 1);
5779 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ies, ies_len);
5780 	if (cap && cap->datalen >= sizeof(*params->he_oper) + 1)
5781 		params->he_oper = (void *)(cap->data + 1);
5782 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_CAPABILITY, ies, ies_len);
5783 	if (cap) {
5784 		if (!cap->datalen)
5785 			return -EINVAL;
5786 		params->eht_cap = (void *)(cap->data + 1);
5787 		if (!ieee80211_eht_capa_size_ok((const u8 *)params->he_cap,
5788 						(const u8 *)params->eht_cap,
5789 						cap->datalen - 1, true))
5790 			return -EINVAL;
5791 	}
5792 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_OPERATION, ies, ies_len);
5793 	if (cap) {
5794 		if (!cap->datalen)
5795 			return -EINVAL;
5796 		params->eht_oper = (void *)(cap->data + 1);
5797 		if (!ieee80211_eht_oper_size_ok((const u8 *)params->eht_oper,
5798 						cap->datalen - 1))
5799 			return -EINVAL;
5800 	}
5801 	return 0;
5802 }
5803 
5804 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
5805 				   struct cfg80211_ap_settings *params)
5806 {
5807 	struct wireless_dev *wdev;
5808 
5809 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
5810 		if (wdev->iftype != NL80211_IFTYPE_AP &&
5811 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)
5812 			continue;
5813 
5814 		if (!wdev->u.ap.preset_chandef.chan)
5815 			continue;
5816 
5817 		params->chandef = wdev->u.ap.preset_chandef;
5818 		return true;
5819 	}
5820 
5821 	return false;
5822 }
5823 
5824 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
5825 				    enum nl80211_auth_type auth_type,
5826 				    enum nl80211_commands cmd)
5827 {
5828 	if (auth_type > NL80211_AUTHTYPE_MAX)
5829 		return false;
5830 
5831 	switch (cmd) {
5832 	case NL80211_CMD_AUTHENTICATE:
5833 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5834 		    auth_type == NL80211_AUTHTYPE_SAE)
5835 			return false;
5836 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5837 					     NL80211_EXT_FEATURE_FILS_STA) &&
5838 		    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5839 		     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5840 		     auth_type == NL80211_AUTHTYPE_FILS_PK))
5841 			return false;
5842 		return true;
5843 	case NL80211_CMD_CONNECT:
5844 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5845 		    !wiphy_ext_feature_isset(&rdev->wiphy,
5846 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
5847 		    auth_type == NL80211_AUTHTYPE_SAE)
5848 			return false;
5849 
5850 		/* FILS with SK PFS or PK not supported yet */
5851 		if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5852 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5853 			return false;
5854 		if (!wiphy_ext_feature_isset(
5855 			    &rdev->wiphy,
5856 			    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
5857 		    auth_type == NL80211_AUTHTYPE_FILS_SK)
5858 			return false;
5859 		return true;
5860 	case NL80211_CMD_START_AP:
5861 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5862 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP) &&
5863 		    auth_type == NL80211_AUTHTYPE_SAE)
5864 			return false;
5865 		/* FILS not supported yet */
5866 		if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5867 		    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5868 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5869 			return false;
5870 		return true;
5871 	default:
5872 		return false;
5873 	}
5874 }
5875 
5876 static void nl80211_send_ap_started(struct wireless_dev *wdev,
5877 				    unsigned int link_id)
5878 {
5879 	struct wiphy *wiphy = wdev->wiphy;
5880 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
5881 	struct sk_buff *msg;
5882 	void *hdr;
5883 
5884 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5885 	if (!msg)
5886 		return;
5887 
5888 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_START_AP);
5889 	if (!hdr)
5890 		goto out;
5891 
5892 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
5893 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
5894 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
5895 			      NL80211_ATTR_PAD) ||
5896 	    (wdev->u.ap.ssid_len &&
5897 	     nla_put(msg, NL80211_ATTR_SSID, wdev->u.ap.ssid_len,
5898 		     wdev->u.ap.ssid)) ||
5899 	    (wdev->valid_links &&
5900 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)))
5901 		goto out;
5902 
5903 	genlmsg_end(msg, hdr);
5904 
5905 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
5906 				NL80211_MCGRP_MLME, GFP_KERNEL);
5907 	return;
5908 out:
5909 	nlmsg_free(msg);
5910 }
5911 
5912 static int nl80211_validate_ap_phy_operation(struct cfg80211_ap_settings *params)
5913 {
5914 	struct ieee80211_channel *channel = params->chandef.chan;
5915 
5916 	if ((params->he_cap ||  params->he_oper) &&
5917 	    (channel->flags & IEEE80211_CHAN_NO_HE))
5918 		return -EOPNOTSUPP;
5919 
5920 	if ((params->eht_cap || params->eht_oper) &&
5921 	    (channel->flags & IEEE80211_CHAN_NO_EHT))
5922 		return -EOPNOTSUPP;
5923 
5924 	return 0;
5925 }
5926 
5927 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
5928 {
5929 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5930 	unsigned int link_id = nl80211_link_id(info->attrs);
5931 	struct net_device *dev = info->user_ptr[1];
5932 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5933 	struct cfg80211_ap_settings *params;
5934 	int err;
5935 
5936 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5937 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5938 		return -EOPNOTSUPP;
5939 
5940 	if (!rdev->ops->start_ap)
5941 		return -EOPNOTSUPP;
5942 
5943 	if (wdev->links[link_id].ap.beacon_interval)
5944 		return -EALREADY;
5945 
5946 	/* these are required for START_AP */
5947 	if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
5948 	    !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
5949 	    !info->attrs[NL80211_ATTR_BEACON_HEAD])
5950 		return -EINVAL;
5951 
5952 	params = kzalloc(sizeof(*params), GFP_KERNEL);
5953 	if (!params)
5954 		return -ENOMEM;
5955 
5956 	err = nl80211_parse_beacon(rdev, info->attrs, &params->beacon,
5957 				   info->extack);
5958 	if (err)
5959 		goto out;
5960 
5961 	params->beacon_interval =
5962 		nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5963 	params->dtim_period =
5964 		nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
5965 
5966 	err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
5967 					   params->beacon_interval);
5968 	if (err)
5969 		goto out;
5970 
5971 	/*
5972 	 * In theory, some of these attributes should be required here
5973 	 * but since they were not used when the command was originally
5974 	 * added, keep them optional for old user space programs to let
5975 	 * them continue to work with drivers that do not need the
5976 	 * additional information -- drivers must check!
5977 	 */
5978 	if (info->attrs[NL80211_ATTR_SSID]) {
5979 		params->ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5980 		params->ssid_len =
5981 			nla_len(info->attrs[NL80211_ATTR_SSID]);
5982 		if (params->ssid_len == 0) {
5983 			err = -EINVAL;
5984 			goto out;
5985 		}
5986 
5987 		if (wdev->u.ap.ssid_len &&
5988 		    (wdev->u.ap.ssid_len != params->ssid_len ||
5989 		     memcmp(wdev->u.ap.ssid, params->ssid, params->ssid_len))) {
5990 			/* require identical SSID for MLO */
5991 			err = -EINVAL;
5992 			goto out;
5993 		}
5994 	} else if (wdev->valid_links) {
5995 		/* require SSID for MLO */
5996 		err = -EINVAL;
5997 		goto out;
5998 	}
5999 
6000 	if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
6001 		params->hidden_ssid = nla_get_u32(
6002 			info->attrs[NL80211_ATTR_HIDDEN_SSID]);
6003 
6004 	params->privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
6005 
6006 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6007 		params->auth_type = nla_get_u32(
6008 			info->attrs[NL80211_ATTR_AUTH_TYPE]);
6009 		if (!nl80211_valid_auth_type(rdev, params->auth_type,
6010 					     NL80211_CMD_START_AP)) {
6011 			err = -EINVAL;
6012 			goto out;
6013 		}
6014 	} else
6015 		params->auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6016 
6017 	err = nl80211_crypto_settings(rdev, info, &params->crypto,
6018 				      NL80211_MAX_NR_CIPHER_SUITES);
6019 	if (err)
6020 		goto out;
6021 
6022 	if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
6023 		if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER)) {
6024 			err = -EOPNOTSUPP;
6025 			goto out;
6026 		}
6027 		params->inactivity_timeout = nla_get_u16(
6028 			info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
6029 	}
6030 
6031 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
6032 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
6033 			err = -EINVAL;
6034 			goto out;
6035 		}
6036 		params->p2p_ctwindow =
6037 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
6038 		if (params->p2p_ctwindow != 0 &&
6039 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN)) {
6040 			err = -EINVAL;
6041 			goto out;
6042 		}
6043 	}
6044 
6045 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
6046 		u8 tmp;
6047 
6048 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
6049 			err = -EINVAL;
6050 			goto out;
6051 		}
6052 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
6053 		params->p2p_opp_ps = tmp;
6054 		if (params->p2p_opp_ps != 0 &&
6055 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS)) {
6056 			err = -EINVAL;
6057 			goto out;
6058 		}
6059 	}
6060 
6061 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6062 		err = nl80211_parse_chandef(rdev, info, &params->chandef);
6063 		if (err)
6064 			goto out;
6065 	} else if (wdev->valid_links) {
6066 		/* with MLD need to specify the channel configuration */
6067 		err = -EINVAL;
6068 		goto out;
6069 	} else if (wdev->u.ap.preset_chandef.chan) {
6070 		params->chandef = wdev->u.ap.preset_chandef;
6071 	} else if (!nl80211_get_ap_channel(rdev, params)) {
6072 		err = -EINVAL;
6073 		goto out;
6074 	}
6075 
6076 	if (info->attrs[NL80211_ATTR_PUNCT_BITMAP]) {
6077 		err = nl80211_parse_punct_bitmap(rdev, info,
6078 						 &params->chandef,
6079 						 &params->punct_bitmap);
6080 		if (err)
6081 			goto out;
6082 	}
6083 
6084 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params->chandef,
6085 					   wdev->iftype)) {
6086 		err = -EINVAL;
6087 		goto out;
6088 	}
6089 
6090 	wdev_lock(wdev);
6091 
6092 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
6093 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
6094 						    NL80211_ATTR_TX_RATES,
6095 						    &params->beacon_rate,
6096 						    dev, false, link_id);
6097 		if (err)
6098 			goto out_unlock;
6099 
6100 		err = validate_beacon_tx_rate(rdev, params->chandef.chan->band,
6101 					      &params->beacon_rate);
6102 		if (err)
6103 			goto out_unlock;
6104 	}
6105 
6106 	if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
6107 		params->smps_mode =
6108 			nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
6109 		switch (params->smps_mode) {
6110 		case NL80211_SMPS_OFF:
6111 			break;
6112 		case NL80211_SMPS_STATIC:
6113 			if (!(rdev->wiphy.features &
6114 			      NL80211_FEATURE_STATIC_SMPS)) {
6115 				err = -EINVAL;
6116 				goto out_unlock;
6117 			}
6118 			break;
6119 		case NL80211_SMPS_DYNAMIC:
6120 			if (!(rdev->wiphy.features &
6121 			      NL80211_FEATURE_DYNAMIC_SMPS)) {
6122 				err = -EINVAL;
6123 				goto out_unlock;
6124 			}
6125 			break;
6126 		default:
6127 			err = -EINVAL;
6128 			goto out_unlock;
6129 		}
6130 	} else {
6131 		params->smps_mode = NL80211_SMPS_OFF;
6132 	}
6133 
6134 	params->pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
6135 	if (params->pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
6136 		err = -EOPNOTSUPP;
6137 		goto out_unlock;
6138 	}
6139 
6140 	if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
6141 		params->acl = parse_acl_data(&rdev->wiphy, info);
6142 		if (IS_ERR(params->acl)) {
6143 			err = PTR_ERR(params->acl);
6144 			params->acl = NULL;
6145 			goto out_unlock;
6146 		}
6147 	}
6148 
6149 	params->twt_responder =
6150 		    nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
6151 
6152 	if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
6153 		err = nl80211_parse_he_obss_pd(
6154 					info->attrs[NL80211_ATTR_HE_OBSS_PD],
6155 					&params->he_obss_pd);
6156 		if (err)
6157 			goto out_unlock;
6158 	}
6159 
6160 	if (info->attrs[NL80211_ATTR_FILS_DISCOVERY]) {
6161 		err = nl80211_parse_fils_discovery(rdev,
6162 						   info->attrs[NL80211_ATTR_FILS_DISCOVERY],
6163 						   params);
6164 		if (err)
6165 			goto out_unlock;
6166 	}
6167 
6168 	if (info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP]) {
6169 		err = nl80211_parse_unsol_bcast_probe_resp(
6170 			rdev, info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP],
6171 			params);
6172 		if (err)
6173 			goto out_unlock;
6174 	}
6175 
6176 	if (info->attrs[NL80211_ATTR_MBSSID_CONFIG]) {
6177 		err = nl80211_parse_mbssid_config(&rdev->wiphy, dev,
6178 						  info->attrs[NL80211_ATTR_MBSSID_CONFIG],
6179 						  &params->mbssid_config,
6180 						  params->beacon.mbssid_ies ?
6181 							params->beacon.mbssid_ies->cnt :
6182 							0);
6183 		if (err)
6184 			goto out_unlock;
6185 	}
6186 
6187 	if (!params->mbssid_config.ema && params->beacon.rnr_ies) {
6188 		err = -EINVAL;
6189 		goto out_unlock;
6190 	}
6191 
6192 	err = nl80211_calculate_ap_params(params);
6193 	if (err)
6194 		goto out_unlock;
6195 
6196 	err = nl80211_validate_ap_phy_operation(params);
6197 	if (err)
6198 		goto out_unlock;
6199 
6200 	if (info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS])
6201 		params->flags = nla_get_u32(
6202 			info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS]);
6203 	else if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
6204 		params->flags |= NL80211_AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
6205 
6206 	if (wdev->conn_owner_nlportid &&
6207 	    info->attrs[NL80211_ATTR_SOCKET_OWNER] &&
6208 	    wdev->conn_owner_nlportid != info->snd_portid) {
6209 		err = -EINVAL;
6210 		goto out_unlock;
6211 	}
6212 
6213 	/* FIXME: validate MLO/link-id against driver capabilities */
6214 
6215 	err = rdev_start_ap(rdev, dev, params);
6216 	if (!err) {
6217 		wdev->links[link_id].ap.beacon_interval = params->beacon_interval;
6218 		wdev->links[link_id].ap.chandef = params->chandef;
6219 		wdev->u.ap.ssid_len = params->ssid_len;
6220 		memcpy(wdev->u.ap.ssid, params->ssid,
6221 		       params->ssid_len);
6222 
6223 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
6224 			wdev->conn_owner_nlportid = info->snd_portid;
6225 
6226 		nl80211_send_ap_started(wdev, link_id);
6227 	}
6228 out_unlock:
6229 	wdev_unlock(wdev);
6230 out:
6231 	kfree(params->acl);
6232 	kfree(params->beacon.mbssid_ies);
6233 	if (params->mbssid_config.tx_wdev &&
6234 	    params->mbssid_config.tx_wdev->netdev &&
6235 	    params->mbssid_config.tx_wdev->netdev != dev)
6236 		dev_put(params->mbssid_config.tx_wdev->netdev);
6237 	kfree(params->beacon.rnr_ies);
6238 	kfree(params);
6239 
6240 	return err;
6241 }
6242 
6243 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
6244 {
6245 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6246 	unsigned int link_id = nl80211_link_id(info->attrs);
6247 	struct net_device *dev = info->user_ptr[1];
6248 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6249 	struct cfg80211_beacon_data params;
6250 	int err;
6251 
6252 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6253 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6254 		return -EOPNOTSUPP;
6255 
6256 	if (!rdev->ops->change_beacon)
6257 		return -EOPNOTSUPP;
6258 
6259 	if (!wdev->links[link_id].ap.beacon_interval)
6260 		return -EINVAL;
6261 
6262 	err = nl80211_parse_beacon(rdev, info->attrs, &params, info->extack);
6263 	if (err)
6264 		goto out;
6265 
6266 	wdev_lock(wdev);
6267 	err = rdev_change_beacon(rdev, dev, &params);
6268 	wdev_unlock(wdev);
6269 
6270 out:
6271 	kfree(params.mbssid_ies);
6272 	kfree(params.rnr_ies);
6273 	return err;
6274 }
6275 
6276 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
6277 {
6278 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6279 	unsigned int link_id = nl80211_link_id(info->attrs);
6280 	struct net_device *dev = info->user_ptr[1];
6281 
6282 	return cfg80211_stop_ap(rdev, dev, link_id, false);
6283 }
6284 
6285 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
6286 	[NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
6287 	[NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
6288 	[NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
6289 	[NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
6290 	[NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
6291 	[NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
6292 };
6293 
6294 static int parse_station_flags(struct genl_info *info,
6295 			       enum nl80211_iftype iftype,
6296 			       struct station_parameters *params)
6297 {
6298 	struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
6299 	struct nlattr *nla;
6300 	int flag;
6301 
6302 	/*
6303 	 * Try parsing the new attribute first so userspace
6304 	 * can specify both for older kernels.
6305 	 */
6306 	nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
6307 	if (nla) {
6308 		struct nl80211_sta_flag_update *sta_flags;
6309 
6310 		sta_flags = nla_data(nla);
6311 		params->sta_flags_mask = sta_flags->mask;
6312 		params->sta_flags_set = sta_flags->set;
6313 		params->sta_flags_set &= params->sta_flags_mask;
6314 		if ((params->sta_flags_mask |
6315 		     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
6316 			return -EINVAL;
6317 		return 0;
6318 	}
6319 
6320 	/* if present, parse the old attribute */
6321 
6322 	nla = info->attrs[NL80211_ATTR_STA_FLAGS];
6323 	if (!nla)
6324 		return 0;
6325 
6326 	if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
6327 		return -EINVAL;
6328 
6329 	/*
6330 	 * Only allow certain flags for interface types so that
6331 	 * other attributes are silently ignored. Remember that
6332 	 * this is backward compatibility code with old userspace
6333 	 * and shouldn't be hit in other cases anyway.
6334 	 */
6335 	switch (iftype) {
6336 	case NL80211_IFTYPE_AP:
6337 	case NL80211_IFTYPE_AP_VLAN:
6338 	case NL80211_IFTYPE_P2P_GO:
6339 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6340 					 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6341 					 BIT(NL80211_STA_FLAG_WME) |
6342 					 BIT(NL80211_STA_FLAG_MFP);
6343 		break;
6344 	case NL80211_IFTYPE_P2P_CLIENT:
6345 	case NL80211_IFTYPE_STATION:
6346 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6347 					 BIT(NL80211_STA_FLAG_TDLS_PEER);
6348 		break;
6349 	case NL80211_IFTYPE_MESH_POINT:
6350 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6351 					 BIT(NL80211_STA_FLAG_MFP) |
6352 					 BIT(NL80211_STA_FLAG_AUTHORIZED);
6353 		break;
6354 	default:
6355 		return -EINVAL;
6356 	}
6357 
6358 	for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
6359 		if (flags[flag]) {
6360 			params->sta_flags_set |= (1<<flag);
6361 
6362 			/* no longer support new API additions in old API */
6363 			if (flag > NL80211_STA_FLAG_MAX_OLD_API)
6364 				return -EINVAL;
6365 		}
6366 	}
6367 
6368 	return 0;
6369 }
6370 
6371 bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
6372 {
6373 	struct nlattr *rate;
6374 	u32 bitrate;
6375 	u16 bitrate_compat;
6376 	enum nl80211_rate_info rate_flg;
6377 
6378 	rate = nla_nest_start_noflag(msg, attr);
6379 	if (!rate)
6380 		return false;
6381 
6382 	/* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
6383 	bitrate = cfg80211_calculate_bitrate(info);
6384 	/* report 16-bit bitrate only if we can */
6385 	bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
6386 	if (bitrate > 0 &&
6387 	    nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
6388 		return false;
6389 	if (bitrate_compat > 0 &&
6390 	    nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
6391 		return false;
6392 
6393 	switch (info->bw) {
6394 	case RATE_INFO_BW_1:
6395 		rate_flg = NL80211_RATE_INFO_1_MHZ_WIDTH;
6396 		break;
6397 	case RATE_INFO_BW_2:
6398 		rate_flg = NL80211_RATE_INFO_2_MHZ_WIDTH;
6399 		break;
6400 	case RATE_INFO_BW_4:
6401 		rate_flg = NL80211_RATE_INFO_4_MHZ_WIDTH;
6402 		break;
6403 	case RATE_INFO_BW_5:
6404 		rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
6405 		break;
6406 	case RATE_INFO_BW_8:
6407 		rate_flg = NL80211_RATE_INFO_8_MHZ_WIDTH;
6408 		break;
6409 	case RATE_INFO_BW_10:
6410 		rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
6411 		break;
6412 	case RATE_INFO_BW_16:
6413 		rate_flg = NL80211_RATE_INFO_16_MHZ_WIDTH;
6414 		break;
6415 	default:
6416 		WARN_ON(1);
6417 		fallthrough;
6418 	case RATE_INFO_BW_20:
6419 		rate_flg = 0;
6420 		break;
6421 	case RATE_INFO_BW_40:
6422 		rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
6423 		break;
6424 	case RATE_INFO_BW_80:
6425 		rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
6426 		break;
6427 	case RATE_INFO_BW_160:
6428 		rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
6429 		break;
6430 	case RATE_INFO_BW_HE_RU:
6431 		rate_flg = 0;
6432 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
6433 		break;
6434 	case RATE_INFO_BW_320:
6435 		rate_flg = NL80211_RATE_INFO_320_MHZ_WIDTH;
6436 		break;
6437 	case RATE_INFO_BW_EHT_RU:
6438 		rate_flg = 0;
6439 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_EHT_MCS));
6440 		break;
6441 	}
6442 
6443 	if (rate_flg && nla_put_flag(msg, rate_flg))
6444 		return false;
6445 
6446 	if (info->flags & RATE_INFO_FLAGS_MCS) {
6447 		if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
6448 			return false;
6449 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6450 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6451 			return false;
6452 	} else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
6453 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
6454 			return false;
6455 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
6456 			return false;
6457 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6458 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6459 			return false;
6460 	} else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
6461 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
6462 			return false;
6463 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
6464 			return false;
6465 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
6466 			return false;
6467 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
6468 			return false;
6469 		if (info->bw == RATE_INFO_BW_HE_RU &&
6470 		    nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
6471 			       info->he_ru_alloc))
6472 			return false;
6473 	} else if (info->flags & RATE_INFO_FLAGS_S1G_MCS) {
6474 		if (nla_put_u8(msg, NL80211_RATE_INFO_S1G_MCS, info->mcs))
6475 			return false;
6476 		if (nla_put_u8(msg, NL80211_RATE_INFO_S1G_NSS, info->nss))
6477 			return false;
6478 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6479 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6480 			return false;
6481 	} else if (info->flags & RATE_INFO_FLAGS_EHT_MCS) {
6482 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_MCS, info->mcs))
6483 			return false;
6484 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_NSS, info->nss))
6485 			return false;
6486 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_GI, info->eht_gi))
6487 			return false;
6488 		if (info->bw == RATE_INFO_BW_EHT_RU &&
6489 		    nla_put_u8(msg, NL80211_RATE_INFO_EHT_RU_ALLOC,
6490 			       info->eht_ru_alloc))
6491 			return false;
6492 	}
6493 
6494 	nla_nest_end(msg, rate);
6495 	return true;
6496 }
6497 
6498 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
6499 			       int id)
6500 {
6501 	void *attr;
6502 	int i = 0;
6503 
6504 	if (!mask)
6505 		return true;
6506 
6507 	attr = nla_nest_start_noflag(msg, id);
6508 	if (!attr)
6509 		return false;
6510 
6511 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
6512 		if (!(mask & BIT(i)))
6513 			continue;
6514 
6515 		if (nla_put_u8(msg, i, signal[i]))
6516 			return false;
6517 	}
6518 
6519 	nla_nest_end(msg, attr);
6520 
6521 	return true;
6522 }
6523 
6524 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
6525 				u32 seq, int flags,
6526 				struct cfg80211_registered_device *rdev,
6527 				struct net_device *dev,
6528 				const u8 *mac_addr, struct station_info *sinfo)
6529 {
6530 	void *hdr;
6531 	struct nlattr *sinfoattr, *bss_param;
6532 
6533 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
6534 	if (!hdr) {
6535 		cfg80211_sinfo_release_content(sinfo);
6536 		return -1;
6537 	}
6538 
6539 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6540 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
6541 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
6542 		goto nla_put_failure;
6543 
6544 	sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
6545 	if (!sinfoattr)
6546 		goto nla_put_failure;
6547 
6548 #define PUT_SINFO(attr, memb, type) do {				\
6549 	BUILD_BUG_ON(sizeof(type) == sizeof(u64));			\
6550 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
6551 	    nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,		\
6552 			     sinfo->memb))				\
6553 		goto nla_put_failure;					\
6554 	} while (0)
6555 #define PUT_SINFO_U64(attr, memb) do {					\
6556 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
6557 	    nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,		\
6558 			      sinfo->memb, NL80211_STA_INFO_PAD))	\
6559 		goto nla_put_failure;					\
6560 	} while (0)
6561 
6562 	PUT_SINFO(CONNECTED_TIME, connected_time, u32);
6563 	PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
6564 	PUT_SINFO_U64(ASSOC_AT_BOOTTIME, assoc_at);
6565 
6566 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
6567 			     BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
6568 	    nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
6569 			(u32)sinfo->rx_bytes))
6570 		goto nla_put_failure;
6571 
6572 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
6573 			     BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
6574 	    nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
6575 			(u32)sinfo->tx_bytes))
6576 		goto nla_put_failure;
6577 
6578 	PUT_SINFO_U64(RX_BYTES64, rx_bytes);
6579 	PUT_SINFO_U64(TX_BYTES64, tx_bytes);
6580 	PUT_SINFO(LLID, llid, u16);
6581 	PUT_SINFO(PLID, plid, u16);
6582 	PUT_SINFO(PLINK_STATE, plink_state, u8);
6583 	PUT_SINFO_U64(RX_DURATION, rx_duration);
6584 	PUT_SINFO_U64(TX_DURATION, tx_duration);
6585 
6586 	if (wiphy_ext_feature_isset(&rdev->wiphy,
6587 				    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
6588 		PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
6589 
6590 	switch (rdev->wiphy.signal_type) {
6591 	case CFG80211_SIGNAL_TYPE_MBM:
6592 		PUT_SINFO(SIGNAL, signal, u8);
6593 		PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
6594 		break;
6595 	default:
6596 		break;
6597 	}
6598 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
6599 		if (!nl80211_put_signal(msg, sinfo->chains,
6600 					sinfo->chain_signal,
6601 					NL80211_STA_INFO_CHAIN_SIGNAL))
6602 			goto nla_put_failure;
6603 	}
6604 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
6605 		if (!nl80211_put_signal(msg, sinfo->chains,
6606 					sinfo->chain_signal_avg,
6607 					NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
6608 			goto nla_put_failure;
6609 	}
6610 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
6611 		if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
6612 					  NL80211_STA_INFO_TX_BITRATE))
6613 			goto nla_put_failure;
6614 	}
6615 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
6616 		if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
6617 					  NL80211_STA_INFO_RX_BITRATE))
6618 			goto nla_put_failure;
6619 	}
6620 
6621 	PUT_SINFO(RX_PACKETS, rx_packets, u32);
6622 	PUT_SINFO(TX_PACKETS, tx_packets, u32);
6623 	PUT_SINFO(TX_RETRIES, tx_retries, u32);
6624 	PUT_SINFO(TX_FAILED, tx_failed, u32);
6625 	PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
6626 	PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
6627 	PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
6628 	PUT_SINFO(LOCAL_PM, local_pm, u32);
6629 	PUT_SINFO(PEER_PM, peer_pm, u32);
6630 	PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
6631 	PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
6632 	PUT_SINFO(CONNECTED_TO_AS, connected_to_as, u8);
6633 
6634 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
6635 		bss_param = nla_nest_start_noflag(msg,
6636 						  NL80211_STA_INFO_BSS_PARAM);
6637 		if (!bss_param)
6638 			goto nla_put_failure;
6639 
6640 		if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
6641 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
6642 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
6643 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
6644 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
6645 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
6646 		    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
6647 			       sinfo->bss_param.dtim_period) ||
6648 		    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
6649 				sinfo->bss_param.beacon_interval))
6650 			goto nla_put_failure;
6651 
6652 		nla_nest_end(msg, bss_param);
6653 	}
6654 	if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
6655 	    nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
6656 		    sizeof(struct nl80211_sta_flag_update),
6657 		    &sinfo->sta_flags))
6658 		goto nla_put_failure;
6659 
6660 	PUT_SINFO_U64(T_OFFSET, t_offset);
6661 	PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
6662 	PUT_SINFO_U64(BEACON_RX, rx_beacon);
6663 	PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
6664 	PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
6665 	PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
6666 	if (wiphy_ext_feature_isset(&rdev->wiphy,
6667 				    NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
6668 		PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
6669 		PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
6670 	}
6671 
6672 #undef PUT_SINFO
6673 #undef PUT_SINFO_U64
6674 
6675 	if (sinfo->pertid) {
6676 		struct nlattr *tidsattr;
6677 		int tid;
6678 
6679 		tidsattr = nla_nest_start_noflag(msg,
6680 						 NL80211_STA_INFO_TID_STATS);
6681 		if (!tidsattr)
6682 			goto nla_put_failure;
6683 
6684 		for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
6685 			struct cfg80211_tid_stats *tidstats;
6686 			struct nlattr *tidattr;
6687 
6688 			tidstats = &sinfo->pertid[tid];
6689 
6690 			if (!tidstats->filled)
6691 				continue;
6692 
6693 			tidattr = nla_nest_start_noflag(msg, tid + 1);
6694 			if (!tidattr)
6695 				goto nla_put_failure;
6696 
6697 #define PUT_TIDVAL_U64(attr, memb) do {					\
6698 	if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&	\
6699 	    nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,		\
6700 			      tidstats->memb, NL80211_TID_STATS_PAD))	\
6701 		goto nla_put_failure;					\
6702 	} while (0)
6703 
6704 			PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
6705 			PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
6706 			PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
6707 			PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
6708 
6709 #undef PUT_TIDVAL_U64
6710 			if ((tidstats->filled &
6711 			     BIT(NL80211_TID_STATS_TXQ_STATS)) &&
6712 			    !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
6713 						   NL80211_TID_STATS_TXQ_STATS))
6714 				goto nla_put_failure;
6715 
6716 			nla_nest_end(msg, tidattr);
6717 		}
6718 
6719 		nla_nest_end(msg, tidsattr);
6720 	}
6721 
6722 	nla_nest_end(msg, sinfoattr);
6723 
6724 	if (sinfo->assoc_req_ies_len &&
6725 	    nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
6726 		    sinfo->assoc_req_ies))
6727 		goto nla_put_failure;
6728 
6729 	if (sinfo->assoc_resp_ies_len &&
6730 	    nla_put(msg, NL80211_ATTR_RESP_IE, sinfo->assoc_resp_ies_len,
6731 		    sinfo->assoc_resp_ies))
6732 		goto nla_put_failure;
6733 
6734 	if (sinfo->mlo_params_valid) {
6735 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
6736 			       sinfo->assoc_link_id))
6737 			goto nla_put_failure;
6738 
6739 		if (!is_zero_ether_addr(sinfo->mld_addr) &&
6740 		    nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
6741 			    sinfo->mld_addr))
6742 			goto nla_put_failure;
6743 	}
6744 
6745 	cfg80211_sinfo_release_content(sinfo);
6746 	genlmsg_end(msg, hdr);
6747 	return 0;
6748 
6749  nla_put_failure:
6750 	cfg80211_sinfo_release_content(sinfo);
6751 	genlmsg_cancel(msg, hdr);
6752 	return -EMSGSIZE;
6753 }
6754 
6755 static int nl80211_dump_station(struct sk_buff *skb,
6756 				struct netlink_callback *cb)
6757 {
6758 	struct station_info sinfo;
6759 	struct cfg80211_registered_device *rdev;
6760 	struct wireless_dev *wdev;
6761 	u8 mac_addr[ETH_ALEN];
6762 	int sta_idx = cb->args[2];
6763 	int err;
6764 
6765 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
6766 	if (err)
6767 		return err;
6768 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
6769 	__acquire(&rdev->wiphy.mtx);
6770 
6771 	if (!wdev->netdev) {
6772 		err = -EINVAL;
6773 		goto out_err;
6774 	}
6775 
6776 	if (!rdev->ops->dump_station) {
6777 		err = -EOPNOTSUPP;
6778 		goto out_err;
6779 	}
6780 
6781 	while (1) {
6782 		memset(&sinfo, 0, sizeof(sinfo));
6783 		err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
6784 					mac_addr, &sinfo);
6785 		if (err == -ENOENT)
6786 			break;
6787 		if (err)
6788 			goto out_err;
6789 
6790 		if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
6791 				NETLINK_CB(cb->skb).portid,
6792 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
6793 				rdev, wdev->netdev, mac_addr,
6794 				&sinfo) < 0)
6795 			goto out;
6796 
6797 		sta_idx++;
6798 	}
6799 
6800  out:
6801 	cb->args[2] = sta_idx;
6802 	err = skb->len;
6803  out_err:
6804 	wiphy_unlock(&rdev->wiphy);
6805 
6806 	return err;
6807 }
6808 
6809 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
6810 {
6811 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6812 	struct net_device *dev = info->user_ptr[1];
6813 	struct station_info sinfo;
6814 	struct sk_buff *msg;
6815 	u8 *mac_addr = NULL;
6816 	int err;
6817 
6818 	memset(&sinfo, 0, sizeof(sinfo));
6819 
6820 	if (!info->attrs[NL80211_ATTR_MAC])
6821 		return -EINVAL;
6822 
6823 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6824 
6825 	if (!rdev->ops->get_station)
6826 		return -EOPNOTSUPP;
6827 
6828 	err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
6829 	if (err)
6830 		return err;
6831 
6832 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6833 	if (!msg) {
6834 		cfg80211_sinfo_release_content(&sinfo);
6835 		return -ENOMEM;
6836 	}
6837 
6838 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
6839 				 info->snd_portid, info->snd_seq, 0,
6840 				 rdev, dev, mac_addr, &sinfo) < 0) {
6841 		nlmsg_free(msg);
6842 		return -ENOBUFS;
6843 	}
6844 
6845 	return genlmsg_reply(msg, info);
6846 }
6847 
6848 int cfg80211_check_station_change(struct wiphy *wiphy,
6849 				  struct station_parameters *params,
6850 				  enum cfg80211_station_type statype)
6851 {
6852 	if (params->listen_interval != -1 &&
6853 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6854 		return -EINVAL;
6855 
6856 	if (params->support_p2p_ps != -1 &&
6857 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6858 		return -EINVAL;
6859 
6860 	if (params->aid &&
6861 	    !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
6862 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6863 		return -EINVAL;
6864 
6865 	/* When you run into this, adjust the code below for the new flag */
6866 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
6867 
6868 	switch (statype) {
6869 	case CFG80211_STA_MESH_PEER_KERNEL:
6870 	case CFG80211_STA_MESH_PEER_USER:
6871 		/*
6872 		 * No ignoring the TDLS flag here -- the userspace mesh
6873 		 * code doesn't have the bug of including TDLS in the
6874 		 * mask everywhere.
6875 		 */
6876 		if (params->sta_flags_mask &
6877 				~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6878 				  BIT(NL80211_STA_FLAG_MFP) |
6879 				  BIT(NL80211_STA_FLAG_AUTHORIZED)))
6880 			return -EINVAL;
6881 		break;
6882 	case CFG80211_STA_TDLS_PEER_SETUP:
6883 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6884 		if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6885 			return -EINVAL;
6886 		/* ignore since it can't change */
6887 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6888 		break;
6889 	default:
6890 		/* disallow mesh-specific things */
6891 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
6892 			return -EINVAL;
6893 		if (params->local_pm)
6894 			return -EINVAL;
6895 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6896 			return -EINVAL;
6897 	}
6898 
6899 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6900 	    statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
6901 		/* TDLS can't be set, ... */
6902 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
6903 			return -EINVAL;
6904 		/*
6905 		 * ... but don't bother the driver with it. This works around
6906 		 * a hostapd/wpa_supplicant issue -- it always includes the
6907 		 * TLDS_PEER flag in the mask even for AP mode.
6908 		 */
6909 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6910 	}
6911 
6912 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6913 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6914 		/* reject other things that can't change */
6915 		if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
6916 			return -EINVAL;
6917 		if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
6918 			return -EINVAL;
6919 		if (params->link_sta_params.supported_rates)
6920 			return -EINVAL;
6921 		if (params->ext_capab || params->link_sta_params.ht_capa ||
6922 		    params->link_sta_params.vht_capa ||
6923 		    params->link_sta_params.he_capa ||
6924 		    params->link_sta_params.eht_capa)
6925 			return -EINVAL;
6926 	}
6927 
6928 	if (statype != CFG80211_STA_AP_CLIENT &&
6929 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6930 		if (params->vlan)
6931 			return -EINVAL;
6932 	}
6933 
6934 	switch (statype) {
6935 	case CFG80211_STA_AP_MLME_CLIENT:
6936 		/* Use this only for authorizing/unauthorizing a station */
6937 		if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
6938 			return -EOPNOTSUPP;
6939 		break;
6940 	case CFG80211_STA_AP_CLIENT:
6941 	case CFG80211_STA_AP_CLIENT_UNASSOC:
6942 		/* accept only the listed bits */
6943 		if (params->sta_flags_mask &
6944 				~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6945 				  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6946 				  BIT(NL80211_STA_FLAG_ASSOCIATED) |
6947 				  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6948 				  BIT(NL80211_STA_FLAG_WME) |
6949 				  BIT(NL80211_STA_FLAG_MFP)))
6950 			return -EINVAL;
6951 
6952 		/* but authenticated/associated only if driver handles it */
6953 		if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
6954 		    params->sta_flags_mask &
6955 				(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6956 				 BIT(NL80211_STA_FLAG_ASSOCIATED)))
6957 			return -EINVAL;
6958 		break;
6959 	case CFG80211_STA_IBSS:
6960 	case CFG80211_STA_AP_STA:
6961 		/* reject any changes other than AUTHORIZED */
6962 		if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
6963 			return -EINVAL;
6964 		break;
6965 	case CFG80211_STA_TDLS_PEER_SETUP:
6966 		/* reject any changes other than AUTHORIZED or WME */
6967 		if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6968 					       BIT(NL80211_STA_FLAG_WME)))
6969 			return -EINVAL;
6970 		/* force (at least) rates when authorizing */
6971 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
6972 		    !params->link_sta_params.supported_rates)
6973 			return -EINVAL;
6974 		break;
6975 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6976 		/* reject any changes */
6977 		return -EINVAL;
6978 	case CFG80211_STA_MESH_PEER_KERNEL:
6979 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6980 			return -EINVAL;
6981 		break;
6982 	case CFG80211_STA_MESH_PEER_USER:
6983 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
6984 		    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
6985 			return -EINVAL;
6986 		break;
6987 	}
6988 
6989 	/*
6990 	 * Older kernel versions ignored this attribute entirely, so don't
6991 	 * reject attempts to update it but mark it as unused instead so the
6992 	 * driver won't look at the data.
6993 	 */
6994 	if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
6995 	    statype != CFG80211_STA_TDLS_PEER_SETUP)
6996 		params->link_sta_params.opmode_notif_used = false;
6997 
6998 	return 0;
6999 }
7000 EXPORT_SYMBOL(cfg80211_check_station_change);
7001 
7002 /*
7003  * Get vlan interface making sure it is running and on the right wiphy.
7004  */
7005 static struct net_device *get_vlan(struct genl_info *info,
7006 				   struct cfg80211_registered_device *rdev)
7007 {
7008 	struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
7009 	struct net_device *v;
7010 	int ret;
7011 
7012 	if (!vlanattr)
7013 		return NULL;
7014 
7015 	v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
7016 	if (!v)
7017 		return ERR_PTR(-ENODEV);
7018 
7019 	if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
7020 		ret = -EINVAL;
7021 		goto error;
7022 	}
7023 
7024 	if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
7025 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
7026 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
7027 		ret = -EINVAL;
7028 		goto error;
7029 	}
7030 
7031 	if (!netif_running(v)) {
7032 		ret = -ENETDOWN;
7033 		goto error;
7034 	}
7035 
7036 	return v;
7037  error:
7038 	dev_put(v);
7039 	return ERR_PTR(ret);
7040 }
7041 
7042 static int nl80211_parse_sta_wme(struct genl_info *info,
7043 				 struct station_parameters *params)
7044 {
7045 	struct nlattr *tb[NL80211_STA_WME_MAX + 1];
7046 	struct nlattr *nla;
7047 	int err;
7048 
7049 	/* parse WME attributes if present */
7050 	if (!info->attrs[NL80211_ATTR_STA_WME])
7051 		return 0;
7052 
7053 	nla = info->attrs[NL80211_ATTR_STA_WME];
7054 	err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
7055 					  nl80211_sta_wme_policy,
7056 					  info->extack);
7057 	if (err)
7058 		return err;
7059 
7060 	if (tb[NL80211_STA_WME_UAPSD_QUEUES])
7061 		params->uapsd_queues = nla_get_u8(
7062 			tb[NL80211_STA_WME_UAPSD_QUEUES]);
7063 	if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
7064 		return -EINVAL;
7065 
7066 	if (tb[NL80211_STA_WME_MAX_SP])
7067 		params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
7068 
7069 	if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
7070 		return -EINVAL;
7071 
7072 	params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
7073 
7074 	return 0;
7075 }
7076 
7077 static int nl80211_parse_sta_channel_info(struct genl_info *info,
7078 				      struct station_parameters *params)
7079 {
7080 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
7081 		params->supported_channels =
7082 		     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
7083 		params->supported_channels_len =
7084 		     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
7085 		/*
7086 		 * Need to include at least one (first channel, number of
7087 		 * channels) tuple for each subband (checked in policy),
7088 		 * and must have proper tuples for the rest of the data as well.
7089 		 */
7090 		if (params->supported_channels_len % 2)
7091 			return -EINVAL;
7092 	}
7093 
7094 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
7095 		params->supported_oper_classes =
7096 		 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
7097 		params->supported_oper_classes_len =
7098 		  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
7099 	}
7100 	return 0;
7101 }
7102 
7103 static int nl80211_set_station_tdls(struct genl_info *info,
7104 				    struct station_parameters *params)
7105 {
7106 	int err;
7107 	/* Dummy STA entry gets updated once the peer capabilities are known */
7108 	if (info->attrs[NL80211_ATTR_PEER_AID])
7109 		params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
7110 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
7111 		params->link_sta_params.ht_capa =
7112 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
7113 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
7114 		params->link_sta_params.vht_capa =
7115 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
7116 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
7117 		params->link_sta_params.he_capa =
7118 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7119 		params->link_sta_params.he_capa_len =
7120 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7121 
7122 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
7123 			params->link_sta_params.eht_capa =
7124 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7125 			params->link_sta_params.eht_capa_len =
7126 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7127 
7128 			if (!ieee80211_eht_capa_size_ok((const u8 *)params->link_sta_params.he_capa,
7129 							(const u8 *)params->link_sta_params.eht_capa,
7130 							params->link_sta_params.eht_capa_len,
7131 							false))
7132 				return -EINVAL;
7133 		}
7134 	}
7135 
7136 	err = nl80211_parse_sta_channel_info(info, params);
7137 	if (err)
7138 		return err;
7139 
7140 	return nl80211_parse_sta_wme(info, params);
7141 }
7142 
7143 static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
7144 					     struct sta_txpwr *txpwr,
7145 					     bool *txpwr_set)
7146 {
7147 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7148 	int idx;
7149 
7150 	if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
7151 		if (!rdev->ops->set_tx_power ||
7152 		    !wiphy_ext_feature_isset(&rdev->wiphy,
7153 					 NL80211_EXT_FEATURE_STA_TX_PWR))
7154 			return -EOPNOTSUPP;
7155 
7156 		idx = NL80211_ATTR_STA_TX_POWER_SETTING;
7157 		txpwr->type = nla_get_u8(info->attrs[idx]);
7158 
7159 		if (txpwr->type == NL80211_TX_POWER_LIMITED) {
7160 			idx = NL80211_ATTR_STA_TX_POWER;
7161 
7162 			if (info->attrs[idx])
7163 				txpwr->power = nla_get_s16(info->attrs[idx]);
7164 			else
7165 				return -EINVAL;
7166 		}
7167 
7168 		*txpwr_set = true;
7169 	} else {
7170 		*txpwr_set = false;
7171 	}
7172 
7173 	return 0;
7174 }
7175 
7176 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
7177 {
7178 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7179 	struct net_device *dev = info->user_ptr[1];
7180 	struct station_parameters params;
7181 	u8 *mac_addr;
7182 	int err;
7183 
7184 	memset(&params, 0, sizeof(params));
7185 
7186 	if (!rdev->ops->change_station)
7187 		return -EOPNOTSUPP;
7188 
7189 	/*
7190 	 * AID and listen_interval properties can be set only for unassociated
7191 	 * station. Include these parameters here and will check them in
7192 	 * cfg80211_check_station_change().
7193 	 */
7194 	if (info->attrs[NL80211_ATTR_STA_AID])
7195 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
7196 
7197 	if (info->attrs[NL80211_ATTR_VLAN_ID])
7198 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
7199 
7200 	if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
7201 		params.listen_interval =
7202 		     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
7203 	else
7204 		params.listen_interval = -1;
7205 
7206 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
7207 		params.support_p2p_ps =
7208 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
7209 	else
7210 		params.support_p2p_ps = -1;
7211 
7212 	if (!info->attrs[NL80211_ATTR_MAC])
7213 		return -EINVAL;
7214 
7215 	params.link_sta_params.link_id =
7216 		nl80211_link_id_or_invalid(info->attrs);
7217 
7218 	if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7219 		/* If MLD_ADDR attribute is set then this is an MLD station
7220 		 * and the MLD_ADDR attribute holds the MLD address and the
7221 		 * MAC attribute holds for the LINK address.
7222 		 * In that case, the link_id is also expected to be valid.
7223 		 */
7224 		if (params.link_sta_params.link_id < 0)
7225 			return -EINVAL;
7226 
7227 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7228 		params.link_sta_params.mld_mac = mac_addr;
7229 		params.link_sta_params.link_mac =
7230 			nla_data(info->attrs[NL80211_ATTR_MAC]);
7231 		if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7232 			return -EINVAL;
7233 	} else {
7234 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7235 	}
7236 
7237 
7238 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
7239 		params.link_sta_params.supported_rates =
7240 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7241 		params.link_sta_params.supported_rates_len =
7242 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7243 	}
7244 
7245 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7246 		params.capability =
7247 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7248 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7249 	}
7250 
7251 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7252 		params.ext_capab =
7253 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7254 		params.ext_capab_len =
7255 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7256 	}
7257 
7258 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7259 		return -EINVAL;
7260 
7261 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7262 		params.plink_action =
7263 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7264 
7265 	if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
7266 		params.plink_state =
7267 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
7268 		if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
7269 			params.peer_aid = nla_get_u16(
7270 				info->attrs[NL80211_ATTR_MESH_PEER_AID]);
7271 		params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
7272 	}
7273 
7274 	if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
7275 		params.local_pm = nla_get_u32(
7276 			info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
7277 
7278 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7279 		params.link_sta_params.opmode_notif_used = true;
7280 		params.link_sta_params.opmode_notif =
7281 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7282 	}
7283 
7284 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7285 		params.link_sta_params.he_6ghz_capa =
7286 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7287 
7288 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7289 		params.airtime_weight =
7290 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7291 
7292 	if (params.airtime_weight &&
7293 	    !wiphy_ext_feature_isset(&rdev->wiphy,
7294 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7295 		return -EOPNOTSUPP;
7296 
7297 	err = nl80211_parse_sta_txpower_setting(info,
7298 						&params.link_sta_params.txpwr,
7299 						&params.link_sta_params.txpwr_set);
7300 	if (err)
7301 		return err;
7302 
7303 	/* Include parameters for TDLS peer (will check later) */
7304 	err = nl80211_set_station_tdls(info, &params);
7305 	if (err)
7306 		return err;
7307 
7308 	params.vlan = get_vlan(info, rdev);
7309 	if (IS_ERR(params.vlan))
7310 		return PTR_ERR(params.vlan);
7311 
7312 	switch (dev->ieee80211_ptr->iftype) {
7313 	case NL80211_IFTYPE_AP:
7314 	case NL80211_IFTYPE_AP_VLAN:
7315 	case NL80211_IFTYPE_P2P_GO:
7316 	case NL80211_IFTYPE_P2P_CLIENT:
7317 	case NL80211_IFTYPE_STATION:
7318 	case NL80211_IFTYPE_ADHOC:
7319 	case NL80211_IFTYPE_MESH_POINT:
7320 		break;
7321 	default:
7322 		err = -EOPNOTSUPP;
7323 		goto out_put_vlan;
7324 	}
7325 
7326 	/* driver will call cfg80211_check_station_change() */
7327 	wdev_lock(dev->ieee80211_ptr);
7328 	err = rdev_change_station(rdev, dev, mac_addr, &params);
7329 	wdev_unlock(dev->ieee80211_ptr);
7330 
7331  out_put_vlan:
7332 	dev_put(params.vlan);
7333 
7334 	return err;
7335 }
7336 
7337 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
7338 {
7339 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7340 	int err;
7341 	struct net_device *dev = info->user_ptr[1];
7342 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7343 	struct station_parameters params;
7344 	u8 *mac_addr = NULL;
7345 	u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
7346 			 BIT(NL80211_STA_FLAG_ASSOCIATED);
7347 
7348 	memset(&params, 0, sizeof(params));
7349 
7350 	if (!rdev->ops->add_station)
7351 		return -EOPNOTSUPP;
7352 
7353 	if (!info->attrs[NL80211_ATTR_MAC])
7354 		return -EINVAL;
7355 
7356 	if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
7357 		return -EINVAL;
7358 
7359 	if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
7360 		return -EINVAL;
7361 
7362 	if (!info->attrs[NL80211_ATTR_STA_AID] &&
7363 	    !info->attrs[NL80211_ATTR_PEER_AID])
7364 		return -EINVAL;
7365 
7366 	params.link_sta_params.link_id =
7367 		nl80211_link_id_or_invalid(info->attrs);
7368 
7369 	if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7370 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7371 		params.link_sta_params.mld_mac = mac_addr;
7372 		params.link_sta_params.link_mac =
7373 			nla_data(info->attrs[NL80211_ATTR_MAC]);
7374 		if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7375 			return -EINVAL;
7376 	} else {
7377 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7378 	}
7379 
7380 	params.link_sta_params.supported_rates =
7381 		nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7382 	params.link_sta_params.supported_rates_len =
7383 		nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7384 	params.listen_interval =
7385 		nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
7386 
7387 	if (info->attrs[NL80211_ATTR_VLAN_ID])
7388 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
7389 
7390 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
7391 		params.support_p2p_ps =
7392 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
7393 	} else {
7394 		/*
7395 		 * if not specified, assume it's supported for P2P GO interface,
7396 		 * and is NOT supported for AP interface
7397 		 */
7398 		params.support_p2p_ps =
7399 			dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
7400 	}
7401 
7402 	if (info->attrs[NL80211_ATTR_PEER_AID])
7403 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
7404 	else
7405 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
7406 
7407 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7408 		params.capability =
7409 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7410 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7411 	}
7412 
7413 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7414 		params.ext_capab =
7415 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7416 		params.ext_capab_len =
7417 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7418 	}
7419 
7420 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
7421 		params.link_sta_params.ht_capa =
7422 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
7423 
7424 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
7425 		params.link_sta_params.vht_capa =
7426 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
7427 
7428 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
7429 		params.link_sta_params.he_capa =
7430 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7431 		params.link_sta_params.he_capa_len =
7432 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7433 
7434 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
7435 			params.link_sta_params.eht_capa =
7436 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7437 			params.link_sta_params.eht_capa_len =
7438 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7439 
7440 			if (!ieee80211_eht_capa_size_ok((const u8 *)params.link_sta_params.he_capa,
7441 							(const u8 *)params.link_sta_params.eht_capa,
7442 							params.link_sta_params.eht_capa_len,
7443 							false))
7444 				return -EINVAL;
7445 		}
7446 	}
7447 
7448 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7449 		params.link_sta_params.he_6ghz_capa =
7450 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7451 
7452 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7453 		params.link_sta_params.opmode_notif_used = true;
7454 		params.link_sta_params.opmode_notif =
7455 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7456 	}
7457 
7458 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7459 		params.plink_action =
7460 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7461 
7462 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7463 		params.airtime_weight =
7464 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7465 
7466 	if (params.airtime_weight &&
7467 	    !wiphy_ext_feature_isset(&rdev->wiphy,
7468 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7469 		return -EOPNOTSUPP;
7470 
7471 	err = nl80211_parse_sta_txpower_setting(info,
7472 						&params.link_sta_params.txpwr,
7473 						&params.link_sta_params.txpwr_set);
7474 	if (err)
7475 		return err;
7476 
7477 	err = nl80211_parse_sta_channel_info(info, &params);
7478 	if (err)
7479 		return err;
7480 
7481 	err = nl80211_parse_sta_wme(info, &params);
7482 	if (err)
7483 		return err;
7484 
7485 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7486 		return -EINVAL;
7487 
7488 	/* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
7489 	 * as userspace might just pass through the capabilities from the IEs
7490 	 * directly, rather than enforcing this restriction and returning an
7491 	 * error in this case.
7492 	 */
7493 	if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
7494 		params.link_sta_params.ht_capa = NULL;
7495 		params.link_sta_params.vht_capa = NULL;
7496 
7497 		/* HE and EHT require WME */
7498 		if (params.link_sta_params.he_capa_len ||
7499 		    params.link_sta_params.he_6ghz_capa ||
7500 		    params.link_sta_params.eht_capa_len)
7501 			return -EINVAL;
7502 	}
7503 
7504 	/* Ensure that HT/VHT capabilities are not set for 6 GHz HE STA */
7505 	if (params.link_sta_params.he_6ghz_capa &&
7506 	    (params.link_sta_params.ht_capa || params.link_sta_params.vht_capa))
7507 		return -EINVAL;
7508 
7509 	/* When you run into this, adjust the code below for the new flag */
7510 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
7511 
7512 	switch (dev->ieee80211_ptr->iftype) {
7513 	case NL80211_IFTYPE_AP:
7514 	case NL80211_IFTYPE_AP_VLAN:
7515 	case NL80211_IFTYPE_P2P_GO:
7516 		/* ignore WME attributes if iface/sta is not capable */
7517 		if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
7518 		    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
7519 			params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7520 
7521 		/* TDLS peers cannot be added */
7522 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7523 		    info->attrs[NL80211_ATTR_PEER_AID])
7524 			return -EINVAL;
7525 		/* but don't bother the driver with it */
7526 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
7527 
7528 		/* allow authenticated/associated only if driver handles it */
7529 		if (!(rdev->wiphy.features &
7530 				NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
7531 		    params.sta_flags_mask & auth_assoc)
7532 			return -EINVAL;
7533 
7534 		/* Older userspace, or userspace wanting to be compatible with
7535 		 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
7536 		 * and assoc flags in the mask, but assumes the station will be
7537 		 * added as associated anyway since this was the required driver
7538 		 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
7539 		 * introduced.
7540 		 * In order to not bother drivers with this quirk in the API
7541 		 * set the flags in both the mask and set for new stations in
7542 		 * this case.
7543 		 */
7544 		if (!(params.sta_flags_mask & auth_assoc)) {
7545 			params.sta_flags_mask |= auth_assoc;
7546 			params.sta_flags_set |= auth_assoc;
7547 		}
7548 
7549 		/* must be last in here for error handling */
7550 		params.vlan = get_vlan(info, rdev);
7551 		if (IS_ERR(params.vlan))
7552 			return PTR_ERR(params.vlan);
7553 		break;
7554 	case NL80211_IFTYPE_MESH_POINT:
7555 		/* ignore uAPSD data */
7556 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7557 
7558 		/* associated is disallowed */
7559 		if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
7560 			return -EINVAL;
7561 		/* TDLS peers cannot be added */
7562 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7563 		    info->attrs[NL80211_ATTR_PEER_AID])
7564 			return -EINVAL;
7565 		break;
7566 	case NL80211_IFTYPE_STATION:
7567 	case NL80211_IFTYPE_P2P_CLIENT:
7568 		/* ignore uAPSD data */
7569 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7570 
7571 		/* these are disallowed */
7572 		if (params.sta_flags_mask &
7573 				(BIT(NL80211_STA_FLAG_ASSOCIATED) |
7574 				 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
7575 			return -EINVAL;
7576 		/* Only TDLS peers can be added */
7577 		if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
7578 			return -EINVAL;
7579 		/* Can only add if TDLS ... */
7580 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
7581 			return -EOPNOTSUPP;
7582 		/* ... with external setup is supported */
7583 		if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
7584 			return -EOPNOTSUPP;
7585 		/*
7586 		 * Older wpa_supplicant versions always mark the TDLS peer
7587 		 * as authorized, but it shouldn't yet be.
7588 		 */
7589 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
7590 		break;
7591 	default:
7592 		return -EOPNOTSUPP;
7593 	}
7594 
7595 	/* be aware of params.vlan when changing code here */
7596 
7597 	wdev_lock(dev->ieee80211_ptr);
7598 	if (wdev->valid_links) {
7599 		if (params.link_sta_params.link_id < 0) {
7600 			err = -EINVAL;
7601 			goto out;
7602 		}
7603 		if (!(wdev->valid_links & BIT(params.link_sta_params.link_id))) {
7604 			err = -ENOLINK;
7605 			goto out;
7606 		}
7607 	} else {
7608 		if (params.link_sta_params.link_id >= 0) {
7609 			err = -EINVAL;
7610 			goto out;
7611 		}
7612 	}
7613 	err = rdev_add_station(rdev, dev, mac_addr, &params);
7614 out:
7615 	wdev_unlock(dev->ieee80211_ptr);
7616 	dev_put(params.vlan);
7617 	return err;
7618 }
7619 
7620 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
7621 {
7622 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7623 	struct net_device *dev = info->user_ptr[1];
7624 	struct station_del_parameters params;
7625 	int ret;
7626 
7627 	memset(&params, 0, sizeof(params));
7628 
7629 	if (info->attrs[NL80211_ATTR_MAC])
7630 		params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
7631 
7632 	switch (dev->ieee80211_ptr->iftype) {
7633 	case NL80211_IFTYPE_AP:
7634 	case NL80211_IFTYPE_AP_VLAN:
7635 	case NL80211_IFTYPE_MESH_POINT:
7636 	case NL80211_IFTYPE_P2P_GO:
7637 		/* always accept these */
7638 		break;
7639 	case NL80211_IFTYPE_ADHOC:
7640 		/* conditionally accept */
7641 		if (wiphy_ext_feature_isset(&rdev->wiphy,
7642 					    NL80211_EXT_FEATURE_DEL_IBSS_STA))
7643 			break;
7644 		return -EINVAL;
7645 	default:
7646 		return -EINVAL;
7647 	}
7648 
7649 	if (!rdev->ops->del_station)
7650 		return -EOPNOTSUPP;
7651 
7652 	if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
7653 		params.subtype =
7654 			nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
7655 		if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
7656 		    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
7657 			return -EINVAL;
7658 	} else {
7659 		/* Default to Deauthentication frame */
7660 		params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
7661 	}
7662 
7663 	if (info->attrs[NL80211_ATTR_REASON_CODE]) {
7664 		params.reason_code =
7665 			nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7666 		if (params.reason_code == 0)
7667 			return -EINVAL; /* 0 is reserved */
7668 	} else {
7669 		/* Default to reason code 2 */
7670 		params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
7671 	}
7672 
7673 	wdev_lock(dev->ieee80211_ptr);
7674 	ret = rdev_del_station(rdev, dev, &params);
7675 	wdev_unlock(dev->ieee80211_ptr);
7676 
7677 	return ret;
7678 }
7679 
7680 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
7681 				int flags, struct net_device *dev,
7682 				u8 *dst, u8 *next_hop,
7683 				struct mpath_info *pinfo)
7684 {
7685 	void *hdr;
7686 	struct nlattr *pinfoattr;
7687 
7688 	hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
7689 	if (!hdr)
7690 		return -1;
7691 
7692 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7693 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7694 	    nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
7695 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
7696 		goto nla_put_failure;
7697 
7698 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
7699 	if (!pinfoattr)
7700 		goto nla_put_failure;
7701 	if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
7702 	    nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
7703 			pinfo->frame_qlen))
7704 		goto nla_put_failure;
7705 	if (((pinfo->filled & MPATH_INFO_SN) &&
7706 	     nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
7707 	    ((pinfo->filled & MPATH_INFO_METRIC) &&
7708 	     nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
7709 			 pinfo->metric)) ||
7710 	    ((pinfo->filled & MPATH_INFO_EXPTIME) &&
7711 	     nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
7712 			 pinfo->exptime)) ||
7713 	    ((pinfo->filled & MPATH_INFO_FLAGS) &&
7714 	     nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
7715 			pinfo->flags)) ||
7716 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
7717 	     nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
7718 			 pinfo->discovery_timeout)) ||
7719 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
7720 	     nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
7721 			pinfo->discovery_retries)) ||
7722 	    ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
7723 	     nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
7724 			pinfo->hop_count)) ||
7725 	    ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
7726 	     nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
7727 			 pinfo->path_change_count)))
7728 		goto nla_put_failure;
7729 
7730 	nla_nest_end(msg, pinfoattr);
7731 
7732 	genlmsg_end(msg, hdr);
7733 	return 0;
7734 
7735  nla_put_failure:
7736 	genlmsg_cancel(msg, hdr);
7737 	return -EMSGSIZE;
7738 }
7739 
7740 static int nl80211_dump_mpath(struct sk_buff *skb,
7741 			      struct netlink_callback *cb)
7742 {
7743 	struct mpath_info pinfo;
7744 	struct cfg80211_registered_device *rdev;
7745 	struct wireless_dev *wdev;
7746 	u8 dst[ETH_ALEN];
7747 	u8 next_hop[ETH_ALEN];
7748 	int path_idx = cb->args[2];
7749 	int err;
7750 
7751 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7752 	if (err)
7753 		return err;
7754 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
7755 	__acquire(&rdev->wiphy.mtx);
7756 
7757 	if (!rdev->ops->dump_mpath) {
7758 		err = -EOPNOTSUPP;
7759 		goto out_err;
7760 	}
7761 
7762 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7763 		err = -EOPNOTSUPP;
7764 		goto out_err;
7765 	}
7766 
7767 	while (1) {
7768 		err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
7769 				      next_hop, &pinfo);
7770 		if (err == -ENOENT)
7771 			break;
7772 		if (err)
7773 			goto out_err;
7774 
7775 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7776 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
7777 				       wdev->netdev, dst, next_hop,
7778 				       &pinfo) < 0)
7779 			goto out;
7780 
7781 		path_idx++;
7782 	}
7783 
7784  out:
7785 	cb->args[2] = path_idx;
7786 	err = skb->len;
7787  out_err:
7788 	wiphy_unlock(&rdev->wiphy);
7789 	return err;
7790 }
7791 
7792 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
7793 {
7794 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7795 	int err;
7796 	struct net_device *dev = info->user_ptr[1];
7797 	struct mpath_info pinfo;
7798 	struct sk_buff *msg;
7799 	u8 *dst = NULL;
7800 	u8 next_hop[ETH_ALEN];
7801 
7802 	memset(&pinfo, 0, sizeof(pinfo));
7803 
7804 	if (!info->attrs[NL80211_ATTR_MAC])
7805 		return -EINVAL;
7806 
7807 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7808 
7809 	if (!rdev->ops->get_mpath)
7810 		return -EOPNOTSUPP;
7811 
7812 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7813 		return -EOPNOTSUPP;
7814 
7815 	err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
7816 	if (err)
7817 		return err;
7818 
7819 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7820 	if (!msg)
7821 		return -ENOMEM;
7822 
7823 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7824 				 dev, dst, next_hop, &pinfo) < 0) {
7825 		nlmsg_free(msg);
7826 		return -ENOBUFS;
7827 	}
7828 
7829 	return genlmsg_reply(msg, info);
7830 }
7831 
7832 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
7833 {
7834 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7835 	struct net_device *dev = info->user_ptr[1];
7836 	u8 *dst = NULL;
7837 	u8 *next_hop = NULL;
7838 
7839 	if (!info->attrs[NL80211_ATTR_MAC])
7840 		return -EINVAL;
7841 
7842 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7843 		return -EINVAL;
7844 
7845 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7846 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7847 
7848 	if (!rdev->ops->change_mpath)
7849 		return -EOPNOTSUPP;
7850 
7851 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7852 		return -EOPNOTSUPP;
7853 
7854 	return rdev_change_mpath(rdev, dev, dst, next_hop);
7855 }
7856 
7857 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
7858 {
7859 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7860 	struct net_device *dev = info->user_ptr[1];
7861 	u8 *dst = NULL;
7862 	u8 *next_hop = NULL;
7863 
7864 	if (!info->attrs[NL80211_ATTR_MAC])
7865 		return -EINVAL;
7866 
7867 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7868 		return -EINVAL;
7869 
7870 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7871 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7872 
7873 	if (!rdev->ops->add_mpath)
7874 		return -EOPNOTSUPP;
7875 
7876 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7877 		return -EOPNOTSUPP;
7878 
7879 	return rdev_add_mpath(rdev, dev, dst, next_hop);
7880 }
7881 
7882 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
7883 {
7884 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7885 	struct net_device *dev = info->user_ptr[1];
7886 	u8 *dst = NULL;
7887 
7888 	if (info->attrs[NL80211_ATTR_MAC])
7889 		dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7890 
7891 	if (!rdev->ops->del_mpath)
7892 		return -EOPNOTSUPP;
7893 
7894 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7895 		return -EOPNOTSUPP;
7896 
7897 	return rdev_del_mpath(rdev, dev, dst);
7898 }
7899 
7900 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
7901 {
7902 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7903 	int err;
7904 	struct net_device *dev = info->user_ptr[1];
7905 	struct mpath_info pinfo;
7906 	struct sk_buff *msg;
7907 	u8 *dst = NULL;
7908 	u8 mpp[ETH_ALEN];
7909 
7910 	memset(&pinfo, 0, sizeof(pinfo));
7911 
7912 	if (!info->attrs[NL80211_ATTR_MAC])
7913 		return -EINVAL;
7914 
7915 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7916 
7917 	if (!rdev->ops->get_mpp)
7918 		return -EOPNOTSUPP;
7919 
7920 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7921 		return -EOPNOTSUPP;
7922 
7923 	err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
7924 	if (err)
7925 		return err;
7926 
7927 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7928 	if (!msg)
7929 		return -ENOMEM;
7930 
7931 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7932 			       dev, dst, mpp, &pinfo) < 0) {
7933 		nlmsg_free(msg);
7934 		return -ENOBUFS;
7935 	}
7936 
7937 	return genlmsg_reply(msg, info);
7938 }
7939 
7940 static int nl80211_dump_mpp(struct sk_buff *skb,
7941 			    struct netlink_callback *cb)
7942 {
7943 	struct mpath_info pinfo;
7944 	struct cfg80211_registered_device *rdev;
7945 	struct wireless_dev *wdev;
7946 	u8 dst[ETH_ALEN];
7947 	u8 mpp[ETH_ALEN];
7948 	int path_idx = cb->args[2];
7949 	int err;
7950 
7951 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7952 	if (err)
7953 		return err;
7954 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
7955 	__acquire(&rdev->wiphy.mtx);
7956 
7957 	if (!rdev->ops->dump_mpp) {
7958 		err = -EOPNOTSUPP;
7959 		goto out_err;
7960 	}
7961 
7962 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7963 		err = -EOPNOTSUPP;
7964 		goto out_err;
7965 	}
7966 
7967 	while (1) {
7968 		err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
7969 				    mpp, &pinfo);
7970 		if (err == -ENOENT)
7971 			break;
7972 		if (err)
7973 			goto out_err;
7974 
7975 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7976 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
7977 				       wdev->netdev, dst, mpp,
7978 				       &pinfo) < 0)
7979 			goto out;
7980 
7981 		path_idx++;
7982 	}
7983 
7984  out:
7985 	cb->args[2] = path_idx;
7986 	err = skb->len;
7987  out_err:
7988 	wiphy_unlock(&rdev->wiphy);
7989 	return err;
7990 }
7991 
7992 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
7993 {
7994 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7995 	struct net_device *dev = info->user_ptr[1];
7996 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7997 	struct bss_parameters params;
7998 	int err;
7999 
8000 	memset(&params, 0, sizeof(params));
8001 	params.link_id = nl80211_link_id_or_invalid(info->attrs);
8002 	/* default to not changing parameters */
8003 	params.use_cts_prot = -1;
8004 	params.use_short_preamble = -1;
8005 	params.use_short_slot_time = -1;
8006 	params.ap_isolate = -1;
8007 	params.ht_opmode = -1;
8008 	params.p2p_ctwindow = -1;
8009 	params.p2p_opp_ps = -1;
8010 
8011 	if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
8012 		params.use_cts_prot =
8013 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
8014 	if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
8015 		params.use_short_preamble =
8016 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
8017 	if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
8018 		params.use_short_slot_time =
8019 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
8020 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8021 		params.basic_rates =
8022 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8023 		params.basic_rates_len =
8024 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8025 	}
8026 	if (info->attrs[NL80211_ATTR_AP_ISOLATE])
8027 		params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
8028 	if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
8029 		params.ht_opmode =
8030 			nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
8031 
8032 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
8033 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
8034 			return -EINVAL;
8035 		params.p2p_ctwindow =
8036 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
8037 		if (params.p2p_ctwindow != 0 &&
8038 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
8039 			return -EINVAL;
8040 	}
8041 
8042 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
8043 		u8 tmp;
8044 
8045 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
8046 			return -EINVAL;
8047 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
8048 		params.p2p_opp_ps = tmp;
8049 		if (params.p2p_opp_ps &&
8050 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
8051 			return -EINVAL;
8052 	}
8053 
8054 	if (!rdev->ops->change_bss)
8055 		return -EOPNOTSUPP;
8056 
8057 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
8058 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
8059 		return -EOPNOTSUPP;
8060 
8061 	wdev_lock(wdev);
8062 	err = rdev_change_bss(rdev, dev, &params);
8063 	wdev_unlock(wdev);
8064 
8065 	return err;
8066 }
8067 
8068 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
8069 {
8070 	char *data = NULL;
8071 	bool is_indoor;
8072 	enum nl80211_user_reg_hint_type user_reg_hint_type;
8073 	u32 owner_nlportid;
8074 
8075 	/*
8076 	 * You should only get this when cfg80211 hasn't yet initialized
8077 	 * completely when built-in to the kernel right between the time
8078 	 * window between nl80211_init() and regulatory_init(), if that is
8079 	 * even possible.
8080 	 */
8081 	if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
8082 		return -EINPROGRESS;
8083 
8084 	if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
8085 		user_reg_hint_type =
8086 		  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
8087 	else
8088 		user_reg_hint_type = NL80211_USER_REG_HINT_USER;
8089 
8090 	switch (user_reg_hint_type) {
8091 	case NL80211_USER_REG_HINT_USER:
8092 	case NL80211_USER_REG_HINT_CELL_BASE:
8093 		if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
8094 			return -EINVAL;
8095 
8096 		data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
8097 		return regulatory_hint_user(data, user_reg_hint_type);
8098 	case NL80211_USER_REG_HINT_INDOOR:
8099 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
8100 			owner_nlportid = info->snd_portid;
8101 			is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
8102 		} else {
8103 			owner_nlportid = 0;
8104 			is_indoor = true;
8105 		}
8106 
8107 		return regulatory_hint_indoor(is_indoor, owner_nlportid);
8108 	default:
8109 		return -EINVAL;
8110 	}
8111 }
8112 
8113 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
8114 {
8115 	return reg_reload_regdb();
8116 }
8117 
8118 static int nl80211_get_mesh_config(struct sk_buff *skb,
8119 				   struct genl_info *info)
8120 {
8121 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8122 	struct net_device *dev = info->user_ptr[1];
8123 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8124 	struct mesh_config cur_params;
8125 	int err = 0;
8126 	void *hdr;
8127 	struct nlattr *pinfoattr;
8128 	struct sk_buff *msg;
8129 
8130 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
8131 		return -EOPNOTSUPP;
8132 
8133 	if (!rdev->ops->get_mesh_config)
8134 		return -EOPNOTSUPP;
8135 
8136 	wdev_lock(wdev);
8137 	/* If not connected, get default parameters */
8138 	if (!wdev->u.mesh.id_len)
8139 		memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
8140 	else
8141 		err = rdev_get_mesh_config(rdev, dev, &cur_params);
8142 	wdev_unlock(wdev);
8143 
8144 	if (err)
8145 		return err;
8146 
8147 	/* Draw up a netlink message to send back */
8148 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8149 	if (!msg)
8150 		return -ENOMEM;
8151 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8152 			     NL80211_CMD_GET_MESH_CONFIG);
8153 	if (!hdr)
8154 		goto out;
8155 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
8156 	if (!pinfoattr)
8157 		goto nla_put_failure;
8158 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8159 	    nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
8160 			cur_params.dot11MeshRetryTimeout) ||
8161 	    nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
8162 			cur_params.dot11MeshConfirmTimeout) ||
8163 	    nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
8164 			cur_params.dot11MeshHoldingTimeout) ||
8165 	    nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
8166 			cur_params.dot11MeshMaxPeerLinks) ||
8167 	    nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
8168 		       cur_params.dot11MeshMaxRetries) ||
8169 	    nla_put_u8(msg, NL80211_MESHCONF_TTL,
8170 		       cur_params.dot11MeshTTL) ||
8171 	    nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
8172 		       cur_params.element_ttl) ||
8173 	    nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8174 		       cur_params.auto_open_plinks) ||
8175 	    nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
8176 			cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
8177 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
8178 		       cur_params.dot11MeshHWMPmaxPREQretries) ||
8179 	    nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
8180 			cur_params.path_refresh_time) ||
8181 	    nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
8182 			cur_params.min_discovery_timeout) ||
8183 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
8184 			cur_params.dot11MeshHWMPactivePathTimeout) ||
8185 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
8186 			cur_params.dot11MeshHWMPpreqMinInterval) ||
8187 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
8188 			cur_params.dot11MeshHWMPperrMinInterval) ||
8189 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
8190 			cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
8191 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
8192 		       cur_params.dot11MeshHWMPRootMode) ||
8193 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
8194 			cur_params.dot11MeshHWMPRannInterval) ||
8195 	    nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
8196 		       cur_params.dot11MeshGateAnnouncementProtocol) ||
8197 	    nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
8198 		       cur_params.dot11MeshForwarding) ||
8199 	    nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
8200 			cur_params.rssi_threshold) ||
8201 	    nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
8202 			cur_params.ht_opmode) ||
8203 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
8204 			cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
8205 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
8206 			cur_params.dot11MeshHWMProotInterval) ||
8207 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
8208 			cur_params.dot11MeshHWMPconfirmationInterval) ||
8209 	    nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
8210 			cur_params.power_mode) ||
8211 	    nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
8212 			cur_params.dot11MeshAwakeWindowDuration) ||
8213 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
8214 			cur_params.plink_timeout) ||
8215 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
8216 		       cur_params.dot11MeshConnectedToMeshGate) ||
8217 	    nla_put_u8(msg, NL80211_MESHCONF_NOLEARN,
8218 		       cur_params.dot11MeshNolearn) ||
8219 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_AS,
8220 		       cur_params.dot11MeshConnectedToAuthServer))
8221 		goto nla_put_failure;
8222 	nla_nest_end(msg, pinfoattr);
8223 	genlmsg_end(msg, hdr);
8224 	return genlmsg_reply(msg, info);
8225 
8226  nla_put_failure:
8227  out:
8228 	nlmsg_free(msg);
8229 	return -ENOBUFS;
8230 }
8231 
8232 static const struct nla_policy
8233 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
8234 	[NL80211_MESHCONF_RETRY_TIMEOUT] =
8235 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8236 	[NL80211_MESHCONF_CONFIRM_TIMEOUT] =
8237 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8238 	[NL80211_MESHCONF_HOLDING_TIMEOUT] =
8239 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8240 	[NL80211_MESHCONF_MAX_PEER_LINKS] =
8241 		NLA_POLICY_RANGE(NLA_U16, 0, 255),
8242 	[NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
8243 	[NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8244 	[NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8245 	[NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
8246 	[NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
8247 		NLA_POLICY_RANGE(NLA_U32, 1, 255),
8248 	[NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
8249 	[NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
8250 	[NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
8251 	[NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
8252 	[NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
8253 		NLA_POLICY_MIN(NLA_U16, 1),
8254 	[NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
8255 		NLA_POLICY_MIN(NLA_U16, 1),
8256 	[NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
8257 		NLA_POLICY_MIN(NLA_U16, 1),
8258 	[NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
8259 	[NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
8260 		NLA_POLICY_MIN(NLA_U16, 1),
8261 	[NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
8262 	[NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
8263 	[NL80211_MESHCONF_RSSI_THRESHOLD] =
8264 		NLA_POLICY_RANGE(NLA_S32, -255, 0),
8265 	[NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
8266 	[NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
8267 	[NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
8268 		NLA_POLICY_MIN(NLA_U16, 1),
8269 	[NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
8270 		NLA_POLICY_MIN(NLA_U16, 1),
8271 	[NL80211_MESHCONF_POWER_MODE] =
8272 		NLA_POLICY_RANGE(NLA_U32,
8273 				 NL80211_MESH_POWER_ACTIVE,
8274 				 NL80211_MESH_POWER_MAX),
8275 	[NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
8276 	[NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
8277 	[NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8278 	[NL80211_MESHCONF_NOLEARN] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8279 	[NL80211_MESHCONF_CONNECTED_TO_AS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8280 };
8281 
8282 static const struct nla_policy
8283 	nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
8284 	[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
8285 	[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
8286 	[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
8287 	[NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
8288 	[NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
8289 	[NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
8290 	[NL80211_MESH_SETUP_IE] =
8291 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
8292 				       IEEE80211_MAX_DATA_LEN),
8293 	[NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
8294 };
8295 
8296 static int nl80211_parse_mesh_config(struct genl_info *info,
8297 				     struct mesh_config *cfg,
8298 				     u32 *mask_out)
8299 {
8300 	struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
8301 	u32 mask = 0;
8302 	u16 ht_opmode;
8303 
8304 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)	\
8305 do {									\
8306 	if (tb[attr]) {							\
8307 		cfg->param = fn(tb[attr]);				\
8308 		mask |= BIT((attr) - 1);				\
8309 	}								\
8310 } while (0)
8311 
8312 	if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
8313 		return -EINVAL;
8314 	if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
8315 		return -EINVAL;
8316 
8317 	/* This makes sure that there aren't more than 32 mesh config
8318 	 * parameters (otherwise our bitfield scheme would not work.) */
8319 	BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
8320 
8321 	/* Fill in the params struct */
8322 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
8323 				  NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
8324 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
8325 				  NL80211_MESHCONF_CONFIRM_TIMEOUT,
8326 				  nla_get_u16);
8327 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
8328 				  NL80211_MESHCONF_HOLDING_TIMEOUT,
8329 				  nla_get_u16);
8330 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
8331 				  NL80211_MESHCONF_MAX_PEER_LINKS,
8332 				  nla_get_u16);
8333 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
8334 				  NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
8335 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
8336 				  NL80211_MESHCONF_TTL, nla_get_u8);
8337 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
8338 				  NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
8339 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
8340 				  NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8341 				  nla_get_u8);
8342 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
8343 				  mask,
8344 				  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
8345 				  nla_get_u32);
8346 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
8347 				  NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
8348 				  nla_get_u8);
8349 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
8350 				  NL80211_MESHCONF_PATH_REFRESH_TIME,
8351 				  nla_get_u32);
8352 	if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
8353 	    (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
8354 		return -EINVAL;
8355 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
8356 				  NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
8357 				  nla_get_u16);
8358 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
8359 				  mask,
8360 				  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
8361 				  nla_get_u32);
8362 	if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
8363 	    (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
8364 	     cfg->dot11MeshHWMPactivePathTimeout > 65535))
8365 		return -EINVAL;
8366 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
8367 				  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
8368 				  nla_get_u16);
8369 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
8370 				  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
8371 				  nla_get_u16);
8372 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8373 				  dot11MeshHWMPnetDiameterTraversalTime, mask,
8374 				  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
8375 				  nla_get_u16);
8376 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
8377 				  NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
8378 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
8379 				  NL80211_MESHCONF_HWMP_RANN_INTERVAL,
8380 				  nla_get_u16);
8381 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
8382 				  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
8383 				  nla_get_u8);
8384 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
8385 				  NL80211_MESHCONF_FORWARDING, nla_get_u8);
8386 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
8387 				  NL80211_MESHCONF_RSSI_THRESHOLD,
8388 				  nla_get_s32);
8389 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
8390 				  NL80211_MESHCONF_CONNECTED_TO_GATE,
8391 				  nla_get_u8);
8392 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToAuthServer, mask,
8393 				  NL80211_MESHCONF_CONNECTED_TO_AS,
8394 				  nla_get_u8);
8395 	/*
8396 	 * Check HT operation mode based on
8397 	 * IEEE 802.11-2016 9.4.2.57 HT Operation element.
8398 	 */
8399 	if (tb[NL80211_MESHCONF_HT_OPMODE]) {
8400 		ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
8401 
8402 		if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
8403 				  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
8404 				  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
8405 			return -EINVAL;
8406 
8407 		/* NON_HT_STA bit is reserved, but some programs set it */
8408 		ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
8409 
8410 		cfg->ht_opmode = ht_opmode;
8411 		mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
8412 	}
8413 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8414 				  dot11MeshHWMPactivePathToRootTimeout, mask,
8415 				  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
8416 				  nla_get_u32);
8417 	if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
8418 	    (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
8419 	     cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
8420 		return -EINVAL;
8421 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
8422 				  NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
8423 				  nla_get_u16);
8424 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
8425 				  mask,
8426 				  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
8427 				  nla_get_u16);
8428 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
8429 				  NL80211_MESHCONF_POWER_MODE, nla_get_u32);
8430 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
8431 				  NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
8432 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
8433 				  NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
8434 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNolearn, mask,
8435 				  NL80211_MESHCONF_NOLEARN, nla_get_u8);
8436 	if (mask_out)
8437 		*mask_out = mask;
8438 
8439 	return 0;
8440 
8441 #undef FILL_IN_MESH_PARAM_IF_SET
8442 }
8443 
8444 static int nl80211_parse_mesh_setup(struct genl_info *info,
8445 				     struct mesh_setup *setup)
8446 {
8447 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8448 	struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
8449 
8450 	if (!info->attrs[NL80211_ATTR_MESH_SETUP])
8451 		return -EINVAL;
8452 	if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
8453 		return -EINVAL;
8454 
8455 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
8456 		setup->sync_method =
8457 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
8458 		 IEEE80211_SYNC_METHOD_VENDOR :
8459 		 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
8460 
8461 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
8462 		setup->path_sel_proto =
8463 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
8464 		 IEEE80211_PATH_PROTOCOL_VENDOR :
8465 		 IEEE80211_PATH_PROTOCOL_HWMP;
8466 
8467 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
8468 		setup->path_metric =
8469 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
8470 		 IEEE80211_PATH_METRIC_VENDOR :
8471 		 IEEE80211_PATH_METRIC_AIRTIME;
8472 
8473 	if (tb[NL80211_MESH_SETUP_IE]) {
8474 		struct nlattr *ieattr =
8475 			tb[NL80211_MESH_SETUP_IE];
8476 		setup->ie = nla_data(ieattr);
8477 		setup->ie_len = nla_len(ieattr);
8478 	}
8479 	if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
8480 	    !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
8481 		return -EINVAL;
8482 	setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
8483 	setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
8484 	setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
8485 	if (setup->is_secure)
8486 		setup->user_mpm = true;
8487 
8488 	if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
8489 		if (!setup->user_mpm)
8490 			return -EINVAL;
8491 		setup->auth_id =
8492 			nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
8493 	}
8494 
8495 	return 0;
8496 }
8497 
8498 static int nl80211_update_mesh_config(struct sk_buff *skb,
8499 				      struct genl_info *info)
8500 {
8501 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8502 	struct net_device *dev = info->user_ptr[1];
8503 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8504 	struct mesh_config cfg = {};
8505 	u32 mask;
8506 	int err;
8507 
8508 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
8509 		return -EOPNOTSUPP;
8510 
8511 	if (!rdev->ops->update_mesh_config)
8512 		return -EOPNOTSUPP;
8513 
8514 	err = nl80211_parse_mesh_config(info, &cfg, &mask);
8515 	if (err)
8516 		return err;
8517 
8518 	wdev_lock(wdev);
8519 	if (!wdev->u.mesh.id_len)
8520 		err = -ENOLINK;
8521 
8522 	if (!err)
8523 		err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
8524 
8525 	wdev_unlock(wdev);
8526 
8527 	return err;
8528 }
8529 
8530 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
8531 			      struct sk_buff *msg)
8532 {
8533 	struct nlattr *nl_reg_rules;
8534 	unsigned int i;
8535 
8536 	if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
8537 	    (regdom->dfs_region &&
8538 	     nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
8539 		goto nla_put_failure;
8540 
8541 	nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
8542 	if (!nl_reg_rules)
8543 		goto nla_put_failure;
8544 
8545 	for (i = 0; i < regdom->n_reg_rules; i++) {
8546 		struct nlattr *nl_reg_rule;
8547 		const struct ieee80211_reg_rule *reg_rule;
8548 		const struct ieee80211_freq_range *freq_range;
8549 		const struct ieee80211_power_rule *power_rule;
8550 		unsigned int max_bandwidth_khz;
8551 
8552 		reg_rule = &regdom->reg_rules[i];
8553 		freq_range = &reg_rule->freq_range;
8554 		power_rule = &reg_rule->power_rule;
8555 
8556 		nl_reg_rule = nla_nest_start_noflag(msg, i);
8557 		if (!nl_reg_rule)
8558 			goto nla_put_failure;
8559 
8560 		max_bandwidth_khz = freq_range->max_bandwidth_khz;
8561 		if (!max_bandwidth_khz)
8562 			max_bandwidth_khz = reg_get_max_bandwidth(regdom,
8563 								  reg_rule);
8564 
8565 		if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
8566 				reg_rule->flags) ||
8567 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
8568 				freq_range->start_freq_khz) ||
8569 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
8570 				freq_range->end_freq_khz) ||
8571 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
8572 				max_bandwidth_khz) ||
8573 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
8574 				power_rule->max_antenna_gain) ||
8575 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
8576 				power_rule->max_eirp) ||
8577 		    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
8578 				reg_rule->dfs_cac_ms))
8579 			goto nla_put_failure;
8580 
8581 		nla_nest_end(msg, nl_reg_rule);
8582 	}
8583 
8584 	nla_nest_end(msg, nl_reg_rules);
8585 	return 0;
8586 
8587 nla_put_failure:
8588 	return -EMSGSIZE;
8589 }
8590 
8591 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
8592 {
8593 	const struct ieee80211_regdomain *regdom = NULL;
8594 	struct cfg80211_registered_device *rdev;
8595 	struct wiphy *wiphy = NULL;
8596 	struct sk_buff *msg;
8597 	int err = -EMSGSIZE;
8598 	void *hdr;
8599 
8600 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8601 	if (!msg)
8602 		return -ENOBUFS;
8603 
8604 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8605 			     NL80211_CMD_GET_REG);
8606 	if (!hdr)
8607 		goto put_failure;
8608 
8609 	rtnl_lock();
8610 
8611 	if (info->attrs[NL80211_ATTR_WIPHY]) {
8612 		bool self_managed;
8613 
8614 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
8615 		if (IS_ERR(rdev)) {
8616 			err = PTR_ERR(rdev);
8617 			goto nla_put_failure;
8618 		}
8619 
8620 		wiphy = &rdev->wiphy;
8621 		self_managed = wiphy->regulatory_flags &
8622 			       REGULATORY_WIPHY_SELF_MANAGED;
8623 
8624 		rcu_read_lock();
8625 
8626 		regdom = get_wiphy_regdom(wiphy);
8627 
8628 		/* a self-managed-reg device must have a private regdom */
8629 		if (WARN_ON(!regdom && self_managed)) {
8630 			err = -EINVAL;
8631 			goto nla_put_failure_rcu;
8632 		}
8633 
8634 		if (regdom &&
8635 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8636 			goto nla_put_failure_rcu;
8637 	} else {
8638 		rcu_read_lock();
8639 	}
8640 
8641 	if (!wiphy && reg_last_request_cell_base() &&
8642 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8643 			NL80211_USER_REG_HINT_CELL_BASE))
8644 		goto nla_put_failure_rcu;
8645 
8646 	if (!regdom)
8647 		regdom = rcu_dereference(cfg80211_regdomain);
8648 
8649 	if (nl80211_put_regdom(regdom, msg))
8650 		goto nla_put_failure_rcu;
8651 
8652 	rcu_read_unlock();
8653 
8654 	genlmsg_end(msg, hdr);
8655 	rtnl_unlock();
8656 	return genlmsg_reply(msg, info);
8657 
8658 nla_put_failure_rcu:
8659 	rcu_read_unlock();
8660 nla_put_failure:
8661 	rtnl_unlock();
8662 put_failure:
8663 	nlmsg_free(msg);
8664 	return err;
8665 }
8666 
8667 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
8668 			       u32 seq, int flags, struct wiphy *wiphy,
8669 			       const struct ieee80211_regdomain *regdom)
8670 {
8671 	void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
8672 				   NL80211_CMD_GET_REG);
8673 
8674 	if (!hdr)
8675 		return -1;
8676 
8677 	genl_dump_check_consistent(cb, hdr);
8678 
8679 	if (nl80211_put_regdom(regdom, msg))
8680 		goto nla_put_failure;
8681 
8682 	if (!wiphy && reg_last_request_cell_base() &&
8683 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8684 			NL80211_USER_REG_HINT_CELL_BASE))
8685 		goto nla_put_failure;
8686 
8687 	if (wiphy &&
8688 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8689 		goto nla_put_failure;
8690 
8691 	if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
8692 	    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
8693 		goto nla_put_failure;
8694 
8695 	genlmsg_end(msg, hdr);
8696 	return 0;
8697 
8698 nla_put_failure:
8699 	genlmsg_cancel(msg, hdr);
8700 	return -EMSGSIZE;
8701 }
8702 
8703 static int nl80211_get_reg_dump(struct sk_buff *skb,
8704 				struct netlink_callback *cb)
8705 {
8706 	const struct ieee80211_regdomain *regdom = NULL;
8707 	struct cfg80211_registered_device *rdev;
8708 	int err, reg_idx, start = cb->args[2];
8709 
8710 	rcu_read_lock();
8711 
8712 	if (cfg80211_regdomain && start == 0) {
8713 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8714 					  NLM_F_MULTI, NULL,
8715 					  rcu_dereference(cfg80211_regdomain));
8716 		if (err < 0)
8717 			goto out_err;
8718 	}
8719 
8720 	/* the global regdom is idx 0 */
8721 	reg_idx = 1;
8722 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
8723 		regdom = get_wiphy_regdom(&rdev->wiphy);
8724 		if (!regdom)
8725 			continue;
8726 
8727 		if (++reg_idx <= start)
8728 			continue;
8729 
8730 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8731 					  NLM_F_MULTI, &rdev->wiphy, regdom);
8732 		if (err < 0) {
8733 			reg_idx--;
8734 			break;
8735 		}
8736 	}
8737 
8738 	cb->args[2] = reg_idx;
8739 	err = skb->len;
8740 out_err:
8741 	rcu_read_unlock();
8742 	return err;
8743 }
8744 
8745 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
8746 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
8747 	[NL80211_ATTR_REG_RULE_FLAGS]		= { .type = NLA_U32 },
8748 	[NL80211_ATTR_FREQ_RANGE_START]		= { .type = NLA_U32 },
8749 	[NL80211_ATTR_FREQ_RANGE_END]		= { .type = NLA_U32 },
8750 	[NL80211_ATTR_FREQ_RANGE_MAX_BW]	= { .type = NLA_U32 },
8751 	[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]	= { .type = NLA_U32 },
8752 	[NL80211_ATTR_POWER_RULE_MAX_EIRP]	= { .type = NLA_U32 },
8753 	[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
8754 };
8755 
8756 static int parse_reg_rule(struct nlattr *tb[],
8757 	struct ieee80211_reg_rule *reg_rule)
8758 {
8759 	struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
8760 	struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
8761 
8762 	if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
8763 		return -EINVAL;
8764 	if (!tb[NL80211_ATTR_FREQ_RANGE_START])
8765 		return -EINVAL;
8766 	if (!tb[NL80211_ATTR_FREQ_RANGE_END])
8767 		return -EINVAL;
8768 	if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
8769 		return -EINVAL;
8770 	if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
8771 		return -EINVAL;
8772 
8773 	reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
8774 
8775 	freq_range->start_freq_khz =
8776 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
8777 	freq_range->end_freq_khz =
8778 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
8779 	freq_range->max_bandwidth_khz =
8780 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
8781 
8782 	power_rule->max_eirp =
8783 		nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
8784 
8785 	if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
8786 		power_rule->max_antenna_gain =
8787 			nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
8788 
8789 	if (tb[NL80211_ATTR_DFS_CAC_TIME])
8790 		reg_rule->dfs_cac_ms =
8791 			nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
8792 
8793 	return 0;
8794 }
8795 
8796 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
8797 {
8798 	struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
8799 	struct nlattr *nl_reg_rule;
8800 	char *alpha2;
8801 	int rem_reg_rules, r;
8802 	u32 num_rules = 0, rule_idx = 0;
8803 	enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
8804 	struct ieee80211_regdomain *rd;
8805 
8806 	if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
8807 		return -EINVAL;
8808 
8809 	if (!info->attrs[NL80211_ATTR_REG_RULES])
8810 		return -EINVAL;
8811 
8812 	alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
8813 
8814 	if (info->attrs[NL80211_ATTR_DFS_REGION])
8815 		dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
8816 
8817 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8818 			    rem_reg_rules) {
8819 		num_rules++;
8820 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
8821 			return -EINVAL;
8822 	}
8823 
8824 	rtnl_lock();
8825 	if (!reg_is_valid_request(alpha2)) {
8826 		r = -EINVAL;
8827 		goto out;
8828 	}
8829 
8830 	rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
8831 	if (!rd) {
8832 		r = -ENOMEM;
8833 		goto out;
8834 	}
8835 
8836 	rd->n_reg_rules = num_rules;
8837 	rd->alpha2[0] = alpha2[0];
8838 	rd->alpha2[1] = alpha2[1];
8839 
8840 	/*
8841 	 * Disable DFS master mode if the DFS region was
8842 	 * not supported or known on this kernel.
8843 	 */
8844 	if (reg_supported_dfs_region(dfs_region))
8845 		rd->dfs_region = dfs_region;
8846 
8847 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8848 			    rem_reg_rules) {
8849 		r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
8850 						nl_reg_rule, reg_rule_policy,
8851 						info->extack);
8852 		if (r)
8853 			goto bad_reg;
8854 		r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
8855 		if (r)
8856 			goto bad_reg;
8857 
8858 		rule_idx++;
8859 
8860 		if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
8861 			r = -EINVAL;
8862 			goto bad_reg;
8863 		}
8864 	}
8865 
8866 	r = set_regdom(rd, REGD_SOURCE_CRDA);
8867 	/* set_regdom takes ownership of rd */
8868 	rd = NULL;
8869  bad_reg:
8870 	kfree(rd);
8871  out:
8872 	rtnl_unlock();
8873 	return r;
8874 }
8875 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
8876 
8877 static int validate_scan_freqs(struct nlattr *freqs)
8878 {
8879 	struct nlattr *attr1, *attr2;
8880 	int n_channels = 0, tmp1, tmp2;
8881 
8882 	nla_for_each_nested(attr1, freqs, tmp1)
8883 		if (nla_len(attr1) != sizeof(u32))
8884 			return 0;
8885 
8886 	nla_for_each_nested(attr1, freqs, tmp1) {
8887 		n_channels++;
8888 		/*
8889 		 * Some hardware has a limited channel list for
8890 		 * scanning, and it is pretty much nonsensical
8891 		 * to scan for a channel twice, so disallow that
8892 		 * and don't require drivers to check that the
8893 		 * channel list they get isn't longer than what
8894 		 * they can scan, as long as they can scan all
8895 		 * the channels they registered at once.
8896 		 */
8897 		nla_for_each_nested(attr2, freqs, tmp2)
8898 			if (attr1 != attr2 &&
8899 			    nla_get_u32(attr1) == nla_get_u32(attr2))
8900 				return 0;
8901 	}
8902 
8903 	return n_channels;
8904 }
8905 
8906 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
8907 {
8908 	return b < NUM_NL80211_BANDS && wiphy->bands[b];
8909 }
8910 
8911 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
8912 			    struct cfg80211_bss_selection *bss_select)
8913 {
8914 	struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
8915 	struct nlattr *nest;
8916 	int err;
8917 	bool found = false;
8918 	int i;
8919 
8920 	/* only process one nested attribute */
8921 	nest = nla_data(nla);
8922 	if (!nla_ok(nest, nla_len(nest)))
8923 		return -EINVAL;
8924 
8925 	err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
8926 					  nest, nl80211_bss_select_policy,
8927 					  NULL);
8928 	if (err)
8929 		return err;
8930 
8931 	/* only one attribute may be given */
8932 	for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
8933 		if (attr[i]) {
8934 			if (found)
8935 				return -EINVAL;
8936 			found = true;
8937 		}
8938 	}
8939 
8940 	bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
8941 
8942 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
8943 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
8944 
8945 	if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
8946 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
8947 		bss_select->param.band_pref =
8948 			nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
8949 		if (!is_band_valid(wiphy, bss_select->param.band_pref))
8950 			return -EINVAL;
8951 	}
8952 
8953 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
8954 		struct nl80211_bss_select_rssi_adjust *adj_param;
8955 
8956 		adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
8957 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
8958 		bss_select->param.adjust.band = adj_param->band;
8959 		bss_select->param.adjust.delta = adj_param->delta;
8960 		if (!is_band_valid(wiphy, bss_select->param.adjust.band))
8961 			return -EINVAL;
8962 	}
8963 
8964 	/* user-space did not provide behaviour attribute */
8965 	if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
8966 		return -EINVAL;
8967 
8968 	if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
8969 		return -EINVAL;
8970 
8971 	return 0;
8972 }
8973 
8974 int nl80211_parse_random_mac(struct nlattr **attrs,
8975 			     u8 *mac_addr, u8 *mac_addr_mask)
8976 {
8977 	int i;
8978 
8979 	if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
8980 		eth_zero_addr(mac_addr);
8981 		eth_zero_addr(mac_addr_mask);
8982 		mac_addr[0] = 0x2;
8983 		mac_addr_mask[0] = 0x3;
8984 
8985 		return 0;
8986 	}
8987 
8988 	/* need both or none */
8989 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
8990 		return -EINVAL;
8991 
8992 	memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
8993 	memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
8994 
8995 	/* don't allow or configure an mcast address */
8996 	if (!is_multicast_ether_addr(mac_addr_mask) ||
8997 	    is_multicast_ether_addr(mac_addr))
8998 		return -EINVAL;
8999 
9000 	/*
9001 	 * allow users to pass a MAC address that has bits set outside
9002 	 * of the mask, but don't bother drivers with having to deal
9003 	 * with such bits
9004 	 */
9005 	for (i = 0; i < ETH_ALEN; i++)
9006 		mac_addr[i] &= mac_addr_mask[i];
9007 
9008 	return 0;
9009 }
9010 
9011 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev,
9012 					      struct ieee80211_channel *chan)
9013 {
9014 	unsigned int link_id;
9015 	bool all_ok = true;
9016 
9017 	ASSERT_WDEV_LOCK(wdev);
9018 
9019 	if (!cfg80211_beaconing_iface_active(wdev))
9020 		return true;
9021 
9022 	/*
9023 	 * FIXME: check if we have a free HW resource/link for chan
9024 	 *
9025 	 * This, as well as the FIXME below, requires knowing the link
9026 	 * capabilities of the hardware.
9027 	 */
9028 
9029 	/* we cannot leave radar channels */
9030 	for_each_valid_link(wdev, link_id) {
9031 		struct cfg80211_chan_def *chandef;
9032 
9033 		chandef = wdev_chandef(wdev, link_id);
9034 		if (!chandef || !chandef->chan)
9035 			continue;
9036 
9037 		/*
9038 		 * FIXME: don't require all_ok, but rather check only the
9039 		 *	  correct HW resource/link onto which 'chan' falls,
9040 		 *	  as only that link leaves the channel for doing
9041 		 *	  the off-channel operation.
9042 		 */
9043 
9044 		if (chandef->chan->flags & IEEE80211_CHAN_RADAR)
9045 			all_ok = false;
9046 	}
9047 
9048 	if (all_ok)
9049 		return true;
9050 
9051 	return regulatory_pre_cac_allowed(wdev->wiphy);
9052 }
9053 
9054 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
9055 				    enum nl80211_ext_feature_index feat)
9056 {
9057 	if (!(flags & flag))
9058 		return true;
9059 	if (wiphy_ext_feature_isset(wiphy, feat))
9060 		return true;
9061 	return false;
9062 }
9063 
9064 static int
9065 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
9066 			 void *request, struct nlattr **attrs,
9067 			 bool is_sched_scan)
9068 {
9069 	u8 *mac_addr, *mac_addr_mask;
9070 	u32 *flags;
9071 	enum nl80211_feature_flags randomness_flag;
9072 
9073 	if (!attrs[NL80211_ATTR_SCAN_FLAGS])
9074 		return 0;
9075 
9076 	if (is_sched_scan) {
9077 		struct cfg80211_sched_scan_request *req = request;
9078 
9079 		randomness_flag = wdev ?
9080 				  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
9081 				  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
9082 		flags = &req->flags;
9083 		mac_addr = req->mac_addr;
9084 		mac_addr_mask = req->mac_addr_mask;
9085 	} else {
9086 		struct cfg80211_scan_request *req = request;
9087 
9088 		randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
9089 		flags = &req->flags;
9090 		mac_addr = req->mac_addr;
9091 		mac_addr_mask = req->mac_addr_mask;
9092 	}
9093 
9094 	*flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
9095 
9096 	if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
9097 	     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
9098 	    !nl80211_check_scan_feat(wiphy, *flags,
9099 				     NL80211_SCAN_FLAG_LOW_SPAN,
9100 				     NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
9101 	    !nl80211_check_scan_feat(wiphy, *flags,
9102 				     NL80211_SCAN_FLAG_LOW_POWER,
9103 				     NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
9104 	    !nl80211_check_scan_feat(wiphy, *flags,
9105 				     NL80211_SCAN_FLAG_HIGH_ACCURACY,
9106 				     NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
9107 	    !nl80211_check_scan_feat(wiphy, *flags,
9108 				     NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
9109 				     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
9110 	    !nl80211_check_scan_feat(wiphy, *flags,
9111 				     NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
9112 				     NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
9113 	    !nl80211_check_scan_feat(wiphy, *flags,
9114 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
9115 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
9116 	    !nl80211_check_scan_feat(wiphy, *flags,
9117 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
9118 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
9119 	    !nl80211_check_scan_feat(wiphy, *flags,
9120 				     NL80211_SCAN_FLAG_RANDOM_SN,
9121 				     NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
9122 	    !nl80211_check_scan_feat(wiphy, *flags,
9123 				     NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
9124 				     NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
9125 		return -EOPNOTSUPP;
9126 
9127 	if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
9128 		int err;
9129 
9130 		if (!(wiphy->features & randomness_flag) ||
9131 		    (wdev && wdev->connected))
9132 			return -EOPNOTSUPP;
9133 
9134 		err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
9135 		if (err)
9136 			return err;
9137 	}
9138 
9139 	return 0;
9140 }
9141 
9142 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
9143 {
9144 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9145 	struct wireless_dev *wdev = info->user_ptr[1];
9146 	struct cfg80211_scan_request *request;
9147 	struct nlattr *scan_freqs = NULL;
9148 	bool scan_freqs_khz = false;
9149 	struct nlattr *attr;
9150 	struct wiphy *wiphy;
9151 	int err, tmp, n_ssids = 0, n_channels, i;
9152 	size_t ie_len, size;
9153 
9154 	wiphy = &rdev->wiphy;
9155 
9156 	if (wdev->iftype == NL80211_IFTYPE_NAN)
9157 		return -EOPNOTSUPP;
9158 
9159 	if (!rdev->ops->scan)
9160 		return -EOPNOTSUPP;
9161 
9162 	if (rdev->scan_req || rdev->scan_msg)
9163 		return -EBUSY;
9164 
9165 	if (info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ]) {
9166 		if (!wiphy_ext_feature_isset(wiphy,
9167 					     NL80211_EXT_FEATURE_SCAN_FREQ_KHZ))
9168 			return -EOPNOTSUPP;
9169 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ];
9170 		scan_freqs_khz = true;
9171 	} else if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES])
9172 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQUENCIES];
9173 
9174 	if (scan_freqs) {
9175 		n_channels = validate_scan_freqs(scan_freqs);
9176 		if (!n_channels)
9177 			return -EINVAL;
9178 	} else {
9179 		n_channels = ieee80211_get_num_supported_channels(wiphy);
9180 	}
9181 
9182 	if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
9183 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
9184 			n_ssids++;
9185 
9186 	if (n_ssids > wiphy->max_scan_ssids)
9187 		return -EINVAL;
9188 
9189 	if (info->attrs[NL80211_ATTR_IE])
9190 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9191 	else
9192 		ie_len = 0;
9193 
9194 	if (ie_len > wiphy->max_scan_ie_len)
9195 		return -EINVAL;
9196 
9197 	size = struct_size(request, channels, n_channels);
9198 	size = size_add(size, array_size(sizeof(*request->ssids), n_ssids));
9199 	size = size_add(size, ie_len);
9200 	request = kzalloc(size, GFP_KERNEL);
9201 	if (!request)
9202 		return -ENOMEM;
9203 
9204 	if (n_ssids)
9205 		request->ssids = (void *)&request->channels[n_channels];
9206 	request->n_ssids = n_ssids;
9207 	if (ie_len) {
9208 		if (n_ssids)
9209 			request->ie = (void *)(request->ssids + n_ssids);
9210 		else
9211 			request->ie = (void *)(request->channels + n_channels);
9212 	}
9213 
9214 	i = 0;
9215 	if (scan_freqs) {
9216 		/* user specified, bail out if channel not found */
9217 		nla_for_each_nested(attr, scan_freqs, tmp) {
9218 			struct ieee80211_channel *chan;
9219 			int freq = nla_get_u32(attr);
9220 
9221 			if (!scan_freqs_khz)
9222 				freq = MHZ_TO_KHZ(freq);
9223 
9224 			chan = ieee80211_get_channel_khz(wiphy, freq);
9225 			if (!chan) {
9226 				err = -EINVAL;
9227 				goto out_free;
9228 			}
9229 
9230 			/* ignore disabled channels */
9231 			if (chan->flags & IEEE80211_CHAN_DISABLED)
9232 				continue;
9233 
9234 			request->channels[i] = chan;
9235 			i++;
9236 		}
9237 	} else {
9238 		enum nl80211_band band;
9239 
9240 		/* all channels */
9241 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
9242 			int j;
9243 
9244 			if (!wiphy->bands[band])
9245 				continue;
9246 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9247 				struct ieee80211_channel *chan;
9248 
9249 				chan = &wiphy->bands[band]->channels[j];
9250 
9251 				if (chan->flags & IEEE80211_CHAN_DISABLED)
9252 					continue;
9253 
9254 				request->channels[i] = chan;
9255 				i++;
9256 			}
9257 		}
9258 	}
9259 
9260 	if (!i) {
9261 		err = -EINVAL;
9262 		goto out_free;
9263 	}
9264 
9265 	request->n_channels = i;
9266 
9267 	wdev_lock(wdev);
9268 	for (i = 0; i < request->n_channels; i++) {
9269 		struct ieee80211_channel *chan = request->channels[i];
9270 
9271 		/* if we can go off-channel to the target channel we're good */
9272 		if (cfg80211_off_channel_oper_allowed(wdev, chan))
9273 			continue;
9274 
9275 		if (!cfg80211_wdev_on_sub_chan(wdev, chan, true)) {
9276 			wdev_unlock(wdev);
9277 			err = -EBUSY;
9278 			goto out_free;
9279 		}
9280 	}
9281 	wdev_unlock(wdev);
9282 
9283 	i = 0;
9284 	if (n_ssids) {
9285 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
9286 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9287 				err = -EINVAL;
9288 				goto out_free;
9289 			}
9290 			request->ssids[i].ssid_len = nla_len(attr);
9291 			memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
9292 			i++;
9293 		}
9294 	}
9295 
9296 	if (info->attrs[NL80211_ATTR_IE]) {
9297 		request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9298 		memcpy((void *)request->ie,
9299 		       nla_data(info->attrs[NL80211_ATTR_IE]),
9300 		       request->ie_len);
9301 	}
9302 
9303 	for (i = 0; i < NUM_NL80211_BANDS; i++)
9304 		if (wiphy->bands[i])
9305 			request->rates[i] =
9306 				(1 << wiphy->bands[i]->n_bitrates) - 1;
9307 
9308 	if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
9309 		nla_for_each_nested(attr,
9310 				    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
9311 				    tmp) {
9312 			enum nl80211_band band = nla_type(attr);
9313 
9314 			if (band < 0 || band >= NUM_NL80211_BANDS) {
9315 				err = -EINVAL;
9316 				goto out_free;
9317 			}
9318 
9319 			if (!wiphy->bands[band])
9320 				continue;
9321 
9322 			err = ieee80211_get_ratemask(wiphy->bands[band],
9323 						     nla_data(attr),
9324 						     nla_len(attr),
9325 						     &request->rates[band]);
9326 			if (err)
9327 				goto out_free;
9328 		}
9329 	}
9330 
9331 	if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
9332 		request->duration =
9333 			nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
9334 		request->duration_mandatory =
9335 			nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
9336 	}
9337 
9338 	err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
9339 				       false);
9340 	if (err)
9341 		goto out_free;
9342 
9343 	request->no_cck =
9344 		nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
9345 
9346 	/* Initial implementation used NL80211_ATTR_MAC to set the specific
9347 	 * BSSID to scan for. This was problematic because that same attribute
9348 	 * was already used for another purpose (local random MAC address). The
9349 	 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
9350 	 * compatibility with older userspace components, also use the
9351 	 * NL80211_ATTR_MAC value here if it can be determined to be used for
9352 	 * the specific BSSID use case instead of the random MAC address
9353 	 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
9354 	 */
9355 	if (info->attrs[NL80211_ATTR_BSSID])
9356 		memcpy(request->bssid,
9357 		       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
9358 	else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
9359 		 info->attrs[NL80211_ATTR_MAC])
9360 		memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
9361 		       ETH_ALEN);
9362 	else
9363 		eth_broadcast_addr(request->bssid);
9364 
9365 	request->wdev = wdev;
9366 	request->wiphy = &rdev->wiphy;
9367 	request->scan_start = jiffies;
9368 
9369 	rdev->scan_req = request;
9370 	err = cfg80211_scan(rdev);
9371 
9372 	if (err)
9373 		goto out_free;
9374 
9375 	nl80211_send_scan_start(rdev, wdev);
9376 	dev_hold(wdev->netdev);
9377 
9378 	return 0;
9379 
9380  out_free:
9381 	rdev->scan_req = NULL;
9382 	kfree(request);
9383 
9384 	return err;
9385 }
9386 
9387 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
9388 {
9389 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9390 	struct wireless_dev *wdev = info->user_ptr[1];
9391 
9392 	if (!rdev->ops->abort_scan)
9393 		return -EOPNOTSUPP;
9394 
9395 	if (rdev->scan_msg)
9396 		return 0;
9397 
9398 	if (!rdev->scan_req)
9399 		return -ENOENT;
9400 
9401 	rdev_abort_scan(rdev, wdev);
9402 	return 0;
9403 }
9404 
9405 static int
9406 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
9407 			       struct cfg80211_sched_scan_request *request,
9408 			       struct nlattr **attrs)
9409 {
9410 	int tmp, err, i = 0;
9411 	struct nlattr *attr;
9412 
9413 	if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9414 		u32 interval;
9415 
9416 		/*
9417 		 * If scan plans are not specified,
9418 		 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
9419 		 * case one scan plan will be set with the specified scan
9420 		 * interval and infinite number of iterations.
9421 		 */
9422 		interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
9423 		if (!interval)
9424 			return -EINVAL;
9425 
9426 		request->scan_plans[0].interval =
9427 			DIV_ROUND_UP(interval, MSEC_PER_SEC);
9428 		if (!request->scan_plans[0].interval)
9429 			return -EINVAL;
9430 
9431 		if (request->scan_plans[0].interval >
9432 		    wiphy->max_sched_scan_plan_interval)
9433 			request->scan_plans[0].interval =
9434 				wiphy->max_sched_scan_plan_interval;
9435 
9436 		return 0;
9437 	}
9438 
9439 	nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
9440 		struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
9441 
9442 		if (WARN_ON(i >= n_plans))
9443 			return -EINVAL;
9444 
9445 		err = nla_parse_nested_deprecated(plan,
9446 						  NL80211_SCHED_SCAN_PLAN_MAX,
9447 						  attr, nl80211_plan_policy,
9448 						  NULL);
9449 		if (err)
9450 			return err;
9451 
9452 		if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
9453 			return -EINVAL;
9454 
9455 		request->scan_plans[i].interval =
9456 			nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
9457 		if (!request->scan_plans[i].interval ||
9458 		    request->scan_plans[i].interval >
9459 		    wiphy->max_sched_scan_plan_interval)
9460 			return -EINVAL;
9461 
9462 		if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
9463 			request->scan_plans[i].iterations =
9464 				nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
9465 			if (!request->scan_plans[i].iterations ||
9466 			    (request->scan_plans[i].iterations >
9467 			     wiphy->max_sched_scan_plan_iterations))
9468 				return -EINVAL;
9469 		} else if (i < n_plans - 1) {
9470 			/*
9471 			 * All scan plans but the last one must specify
9472 			 * a finite number of iterations
9473 			 */
9474 			return -EINVAL;
9475 		}
9476 
9477 		i++;
9478 	}
9479 
9480 	/*
9481 	 * The last scan plan must not specify the number of
9482 	 * iterations, it is supposed to run infinitely
9483 	 */
9484 	if (request->scan_plans[n_plans - 1].iterations)
9485 		return  -EINVAL;
9486 
9487 	return 0;
9488 }
9489 
9490 static int
9491 nl80211_parse_sched_scan_per_band_rssi(struct wiphy *wiphy,
9492 				       struct cfg80211_match_set *match_sets,
9493 				       struct nlattr *tb_band_rssi,
9494 				       s32 rssi_thold)
9495 {
9496 	struct nlattr *attr;
9497 	int i, tmp, ret = 0;
9498 
9499 	if (!wiphy_ext_feature_isset(wiphy,
9500 		    NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD)) {
9501 		if (tb_band_rssi)
9502 			ret = -EOPNOTSUPP;
9503 		else
9504 			for (i = 0; i < NUM_NL80211_BANDS; i++)
9505 				match_sets->per_band_rssi_thold[i] =
9506 					NL80211_SCAN_RSSI_THOLD_OFF;
9507 		return ret;
9508 	}
9509 
9510 	for (i = 0; i < NUM_NL80211_BANDS; i++)
9511 		match_sets->per_band_rssi_thold[i] = rssi_thold;
9512 
9513 	nla_for_each_nested(attr, tb_band_rssi, tmp) {
9514 		enum nl80211_band band = nla_type(attr);
9515 
9516 		if (band < 0 || band >= NUM_NL80211_BANDS)
9517 			return -EINVAL;
9518 
9519 		match_sets->per_band_rssi_thold[band] =	nla_get_s32(attr);
9520 	}
9521 
9522 	return 0;
9523 }
9524 
9525 static struct cfg80211_sched_scan_request *
9526 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
9527 			 struct nlattr **attrs, int max_match_sets)
9528 {
9529 	struct cfg80211_sched_scan_request *request;
9530 	struct nlattr *attr;
9531 	int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
9532 	enum nl80211_band band;
9533 	size_t ie_len, size;
9534 	struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
9535 	s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
9536 
9537 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9538 		n_channels = validate_scan_freqs(
9539 				attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
9540 		if (!n_channels)
9541 			return ERR_PTR(-EINVAL);
9542 	} else {
9543 		n_channels = ieee80211_get_num_supported_channels(wiphy);
9544 	}
9545 
9546 	if (attrs[NL80211_ATTR_SCAN_SSIDS])
9547 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9548 				    tmp)
9549 			n_ssids++;
9550 
9551 	if (n_ssids > wiphy->max_sched_scan_ssids)
9552 		return ERR_PTR(-EINVAL);
9553 
9554 	/*
9555 	 * First, count the number of 'real' matchsets. Due to an issue with
9556 	 * the old implementation, matchsets containing only the RSSI attribute
9557 	 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
9558 	 * RSSI for all matchsets, rather than their own matchset for reporting
9559 	 * all APs with a strong RSSI. This is needed to be compatible with
9560 	 * older userspace that treated a matchset with only the RSSI as the
9561 	 * global RSSI for all other matchsets - if there are other matchsets.
9562 	 */
9563 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9564 		nla_for_each_nested(attr,
9565 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9566 				    tmp) {
9567 			struct nlattr *rssi;
9568 
9569 			err = nla_parse_nested_deprecated(tb,
9570 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9571 							  attr,
9572 							  nl80211_match_policy,
9573 							  NULL);
9574 			if (err)
9575 				return ERR_PTR(err);
9576 
9577 			/* SSID and BSSID are mutually exclusive */
9578 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
9579 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
9580 				return ERR_PTR(-EINVAL);
9581 
9582 			/* add other standalone attributes here */
9583 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
9584 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
9585 				n_match_sets++;
9586 				continue;
9587 			}
9588 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9589 			if (rssi)
9590 				default_match_rssi = nla_get_s32(rssi);
9591 		}
9592 	}
9593 
9594 	/* However, if there's no other matchset, add the RSSI one */
9595 	if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
9596 		n_match_sets = 1;
9597 
9598 	if (n_match_sets > max_match_sets)
9599 		return ERR_PTR(-EINVAL);
9600 
9601 	if (attrs[NL80211_ATTR_IE])
9602 		ie_len = nla_len(attrs[NL80211_ATTR_IE]);
9603 	else
9604 		ie_len = 0;
9605 
9606 	if (ie_len > wiphy->max_sched_scan_ie_len)
9607 		return ERR_PTR(-EINVAL);
9608 
9609 	if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9610 		/*
9611 		 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
9612 		 * each scan plan already specifies its own interval
9613 		 */
9614 		if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9615 			return ERR_PTR(-EINVAL);
9616 
9617 		nla_for_each_nested(attr,
9618 				    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
9619 			n_plans++;
9620 	} else {
9621 		/*
9622 		 * The scan interval attribute is kept for backward
9623 		 * compatibility. If no scan plans are specified and sched scan
9624 		 * interval is specified, one scan plan will be set with this
9625 		 * scan interval and infinite number of iterations.
9626 		 */
9627 		if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9628 			return ERR_PTR(-EINVAL);
9629 
9630 		n_plans = 1;
9631 	}
9632 
9633 	if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
9634 		return ERR_PTR(-EINVAL);
9635 
9636 	if (!wiphy_ext_feature_isset(
9637 		    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
9638 	    (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
9639 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
9640 		return ERR_PTR(-EINVAL);
9641 
9642 	size = struct_size(request, channels, n_channels);
9643 	size = size_add(size, array_size(sizeof(*request->ssids), n_ssids));
9644 	size = size_add(size, array_size(sizeof(*request->match_sets),
9645 					 n_match_sets));
9646 	size = size_add(size, array_size(sizeof(*request->scan_plans),
9647 					 n_plans));
9648 	size = size_add(size, ie_len);
9649 	request = kzalloc(size, GFP_KERNEL);
9650 	if (!request)
9651 		return ERR_PTR(-ENOMEM);
9652 
9653 	if (n_ssids)
9654 		request->ssids = (void *)&request->channels[n_channels];
9655 	request->n_ssids = n_ssids;
9656 	if (ie_len) {
9657 		if (n_ssids)
9658 			request->ie = (void *)(request->ssids + n_ssids);
9659 		else
9660 			request->ie = (void *)(request->channels + n_channels);
9661 	}
9662 
9663 	if (n_match_sets) {
9664 		if (request->ie)
9665 			request->match_sets = (void *)(request->ie + ie_len);
9666 		else if (n_ssids)
9667 			request->match_sets =
9668 				(void *)(request->ssids + n_ssids);
9669 		else
9670 			request->match_sets =
9671 				(void *)(request->channels + n_channels);
9672 	}
9673 	request->n_match_sets = n_match_sets;
9674 
9675 	if (n_match_sets)
9676 		request->scan_plans = (void *)(request->match_sets +
9677 					       n_match_sets);
9678 	else if (request->ie)
9679 		request->scan_plans = (void *)(request->ie + ie_len);
9680 	else if (n_ssids)
9681 		request->scan_plans = (void *)(request->ssids + n_ssids);
9682 	else
9683 		request->scan_plans = (void *)(request->channels + n_channels);
9684 
9685 	request->n_scan_plans = n_plans;
9686 
9687 	i = 0;
9688 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9689 		/* user specified, bail out if channel not found */
9690 		nla_for_each_nested(attr,
9691 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
9692 				    tmp) {
9693 			struct ieee80211_channel *chan;
9694 
9695 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
9696 
9697 			if (!chan) {
9698 				err = -EINVAL;
9699 				goto out_free;
9700 			}
9701 
9702 			/* ignore disabled channels */
9703 			if (chan->flags & IEEE80211_CHAN_DISABLED)
9704 				continue;
9705 
9706 			request->channels[i] = chan;
9707 			i++;
9708 		}
9709 	} else {
9710 		/* all channels */
9711 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
9712 			int j;
9713 
9714 			if (!wiphy->bands[band])
9715 				continue;
9716 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9717 				struct ieee80211_channel *chan;
9718 
9719 				chan = &wiphy->bands[band]->channels[j];
9720 
9721 				if (chan->flags & IEEE80211_CHAN_DISABLED)
9722 					continue;
9723 
9724 				request->channels[i] = chan;
9725 				i++;
9726 			}
9727 		}
9728 	}
9729 
9730 	if (!i) {
9731 		err = -EINVAL;
9732 		goto out_free;
9733 	}
9734 
9735 	request->n_channels = i;
9736 
9737 	i = 0;
9738 	if (n_ssids) {
9739 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9740 				    tmp) {
9741 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9742 				err = -EINVAL;
9743 				goto out_free;
9744 			}
9745 			request->ssids[i].ssid_len = nla_len(attr);
9746 			memcpy(request->ssids[i].ssid, nla_data(attr),
9747 			       nla_len(attr));
9748 			i++;
9749 		}
9750 	}
9751 
9752 	i = 0;
9753 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9754 		nla_for_each_nested(attr,
9755 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9756 				    tmp) {
9757 			struct nlattr *ssid, *bssid, *rssi;
9758 
9759 			err = nla_parse_nested_deprecated(tb,
9760 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9761 							  attr,
9762 							  nl80211_match_policy,
9763 							  NULL);
9764 			if (err)
9765 				goto out_free;
9766 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
9767 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
9768 
9769 			if (!ssid && !bssid) {
9770 				i++;
9771 				continue;
9772 			}
9773 
9774 			if (WARN_ON(i >= n_match_sets)) {
9775 				/* this indicates a programming error,
9776 				 * the loop above should have verified
9777 				 * things properly
9778 				 */
9779 				err = -EINVAL;
9780 				goto out_free;
9781 			}
9782 
9783 			if (ssid) {
9784 				memcpy(request->match_sets[i].ssid.ssid,
9785 				       nla_data(ssid), nla_len(ssid));
9786 				request->match_sets[i].ssid.ssid_len =
9787 					nla_len(ssid);
9788 			}
9789 			if (bssid)
9790 				memcpy(request->match_sets[i].bssid,
9791 				       nla_data(bssid), ETH_ALEN);
9792 
9793 			/* special attribute - old implementation w/a */
9794 			request->match_sets[i].rssi_thold = default_match_rssi;
9795 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9796 			if (rssi)
9797 				request->match_sets[i].rssi_thold =
9798 					nla_get_s32(rssi);
9799 
9800 			/* Parse per band RSSI attribute */
9801 			err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
9802 				&request->match_sets[i],
9803 				tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
9804 				request->match_sets[i].rssi_thold);
9805 			if (err)
9806 				goto out_free;
9807 
9808 			i++;
9809 		}
9810 
9811 		/* there was no other matchset, so the RSSI one is alone */
9812 		if (i == 0 && n_match_sets)
9813 			request->match_sets[0].rssi_thold = default_match_rssi;
9814 
9815 		request->min_rssi_thold = INT_MAX;
9816 		for (i = 0; i < n_match_sets; i++)
9817 			request->min_rssi_thold =
9818 				min(request->match_sets[i].rssi_thold,
9819 				    request->min_rssi_thold);
9820 	} else {
9821 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
9822 	}
9823 
9824 	if (ie_len) {
9825 		request->ie_len = ie_len;
9826 		memcpy((void *)request->ie,
9827 		       nla_data(attrs[NL80211_ATTR_IE]),
9828 		       request->ie_len);
9829 	}
9830 
9831 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
9832 	if (err)
9833 		goto out_free;
9834 
9835 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
9836 		request->delay =
9837 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
9838 
9839 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
9840 		request->relative_rssi = nla_get_s8(
9841 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
9842 		request->relative_rssi_set = true;
9843 	}
9844 
9845 	if (request->relative_rssi_set &&
9846 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
9847 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
9848 
9849 		rssi_adjust = nla_data(
9850 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
9851 		request->rssi_adjust.band = rssi_adjust->band;
9852 		request->rssi_adjust.delta = rssi_adjust->delta;
9853 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
9854 			err = -EINVAL;
9855 			goto out_free;
9856 		}
9857 	}
9858 
9859 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
9860 	if (err)
9861 		goto out_free;
9862 
9863 	request->scan_start = jiffies;
9864 
9865 	return request;
9866 
9867 out_free:
9868 	kfree(request);
9869 	return ERR_PTR(err);
9870 }
9871 
9872 static int nl80211_start_sched_scan(struct sk_buff *skb,
9873 				    struct genl_info *info)
9874 {
9875 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9876 	struct net_device *dev = info->user_ptr[1];
9877 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9878 	struct cfg80211_sched_scan_request *sched_scan_req;
9879 	bool want_multi;
9880 	int err;
9881 
9882 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
9883 		return -EOPNOTSUPP;
9884 
9885 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
9886 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
9887 	if (err)
9888 		return err;
9889 
9890 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
9891 						  info->attrs,
9892 						  rdev->wiphy.max_match_sets);
9893 
9894 	err = PTR_ERR_OR_ZERO(sched_scan_req);
9895 	if (err)
9896 		goto out_err;
9897 
9898 	/* leave request id zero for legacy request
9899 	 * or if driver does not support multi-scheduled scan
9900 	 */
9901 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1)
9902 		sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
9903 
9904 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
9905 	if (err)
9906 		goto out_free;
9907 
9908 	sched_scan_req->dev = dev;
9909 	sched_scan_req->wiphy = &rdev->wiphy;
9910 
9911 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9912 		sched_scan_req->owner_nlportid = info->snd_portid;
9913 
9914 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
9915 
9916 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
9917 	return 0;
9918 
9919 out_free:
9920 	kfree(sched_scan_req);
9921 out_err:
9922 	return err;
9923 }
9924 
9925 static int nl80211_stop_sched_scan(struct sk_buff *skb,
9926 				   struct genl_info *info)
9927 {
9928 	struct cfg80211_sched_scan_request *req;
9929 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9930 	u64 cookie;
9931 
9932 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
9933 		return -EOPNOTSUPP;
9934 
9935 	if (info->attrs[NL80211_ATTR_COOKIE]) {
9936 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9937 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
9938 	}
9939 
9940 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
9941 				     struct cfg80211_sched_scan_request,
9942 				     list);
9943 	if (!req || req->reqid ||
9944 	    (req->owner_nlportid &&
9945 	     req->owner_nlportid != info->snd_portid))
9946 		return -ENOENT;
9947 
9948 	return cfg80211_stop_sched_scan_req(rdev, req, false);
9949 }
9950 
9951 static int nl80211_start_radar_detection(struct sk_buff *skb,
9952 					 struct genl_info *info)
9953 {
9954 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9955 	struct net_device *dev = info->user_ptr[1];
9956 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9957 	struct wiphy *wiphy = wdev->wiphy;
9958 	struct cfg80211_chan_def chandef;
9959 	enum nl80211_dfs_regions dfs_region;
9960 	unsigned int cac_time_ms;
9961 	int err = -EINVAL;
9962 
9963 	flush_delayed_work(&rdev->dfs_update_channels_wk);
9964 
9965 	wiphy_lock(wiphy);
9966 
9967 	dfs_region = reg_get_dfs_region(wiphy);
9968 	if (dfs_region == NL80211_DFS_UNSET)
9969 		goto unlock;
9970 
9971 	err = nl80211_parse_chandef(rdev, info, &chandef);
9972 	if (err)
9973 		goto unlock;
9974 
9975 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
9976 	if (err < 0)
9977 		goto unlock;
9978 
9979 	if (err == 0) {
9980 		err = -EINVAL;
9981 		goto unlock;
9982 	}
9983 
9984 	if (!cfg80211_chandef_dfs_usable(wiphy, &chandef)) {
9985 		err = -EINVAL;
9986 		goto unlock;
9987 	}
9988 
9989 	if (nla_get_flag(info->attrs[NL80211_ATTR_RADAR_BACKGROUND])) {
9990 		err = cfg80211_start_background_radar_detection(rdev, wdev,
9991 								&chandef);
9992 		goto unlock;
9993 	}
9994 
9995 	if (netif_carrier_ok(dev)) {
9996 		err = -EBUSY;
9997 		goto unlock;
9998 	}
9999 
10000 	if (wdev->cac_started) {
10001 		err = -EBUSY;
10002 		goto unlock;
10003 	}
10004 
10005 	/* CAC start is offloaded to HW and can't be started manually */
10006 	if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD)) {
10007 		err = -EOPNOTSUPP;
10008 		goto unlock;
10009 	}
10010 
10011 	if (!rdev->ops->start_radar_detection) {
10012 		err = -EOPNOTSUPP;
10013 		goto unlock;
10014 	}
10015 
10016 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
10017 	if (WARN_ON(!cac_time_ms))
10018 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
10019 
10020 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
10021 	if (!err) {
10022 		wdev->links[0].ap.chandef = chandef;
10023 		wdev->cac_started = true;
10024 		wdev->cac_start_time = jiffies;
10025 		wdev->cac_time_ms = cac_time_ms;
10026 	}
10027 unlock:
10028 	wiphy_unlock(wiphy);
10029 
10030 	return err;
10031 }
10032 
10033 static int nl80211_notify_radar_detection(struct sk_buff *skb,
10034 					  struct genl_info *info)
10035 {
10036 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10037 	struct net_device *dev = info->user_ptr[1];
10038 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10039 	struct wiphy *wiphy = wdev->wiphy;
10040 	struct cfg80211_chan_def chandef;
10041 	enum nl80211_dfs_regions dfs_region;
10042 	int err;
10043 
10044 	dfs_region = reg_get_dfs_region(wiphy);
10045 	if (dfs_region == NL80211_DFS_UNSET) {
10046 		GENL_SET_ERR_MSG(info,
10047 				 "DFS Region is not set. Unexpected Radar indication");
10048 		return -EINVAL;
10049 	}
10050 
10051 	err = nl80211_parse_chandef(rdev, info, &chandef);
10052 	if (err) {
10053 		GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
10054 		return err;
10055 	}
10056 
10057 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
10058 	if (err < 0) {
10059 		GENL_SET_ERR_MSG(info, "chandef is invalid");
10060 		return err;
10061 	}
10062 
10063 	if (err == 0) {
10064 		GENL_SET_ERR_MSG(info,
10065 				 "Unexpected Radar indication for chandef/iftype");
10066 		return -EINVAL;
10067 	}
10068 
10069 	/* Do not process this notification if radar is already detected
10070 	 * by kernel on this channel, and return success.
10071 	 */
10072 	if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
10073 		return 0;
10074 
10075 	cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
10076 
10077 	cfg80211_sched_dfs_chan_update(rdev);
10078 
10079 	rdev->radar_chandef = chandef;
10080 
10081 	/* Propagate this notification to other radios as well */
10082 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
10083 
10084 	return 0;
10085 }
10086 
10087 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
10088 {
10089 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10090 	unsigned int link_id = nl80211_link_id(info->attrs);
10091 	struct net_device *dev = info->user_ptr[1];
10092 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10093 	struct cfg80211_csa_settings params;
10094 	struct nlattr **csa_attrs = NULL;
10095 	int err;
10096 	bool need_new_beacon = false;
10097 	bool need_handle_dfs_flag = true;
10098 	int len, i;
10099 	u32 cs_count;
10100 
10101 	if (!rdev->ops->channel_switch ||
10102 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
10103 		return -EOPNOTSUPP;
10104 
10105 	switch (dev->ieee80211_ptr->iftype) {
10106 	case NL80211_IFTYPE_AP:
10107 	case NL80211_IFTYPE_P2P_GO:
10108 		need_new_beacon = true;
10109 		/* For all modes except AP the handle_dfs flag needs to be
10110 		 * supplied to tell the kernel that userspace will handle radar
10111 		 * events when they happen. Otherwise a switch to a channel
10112 		 * requiring DFS will be rejected.
10113 		 */
10114 		need_handle_dfs_flag = false;
10115 
10116 		/* useless if AP is not running */
10117 		if (!wdev->links[link_id].ap.beacon_interval)
10118 			return -ENOTCONN;
10119 		break;
10120 	case NL80211_IFTYPE_ADHOC:
10121 		if (!wdev->u.ibss.ssid_len)
10122 			return -ENOTCONN;
10123 		break;
10124 	case NL80211_IFTYPE_MESH_POINT:
10125 		if (!wdev->u.mesh.id_len)
10126 			return -ENOTCONN;
10127 		break;
10128 	default:
10129 		return -EOPNOTSUPP;
10130 	}
10131 
10132 	memset(&params, 0, sizeof(params));
10133 	params.beacon_csa.ftm_responder = -1;
10134 
10135 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
10136 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
10137 		return -EINVAL;
10138 
10139 	/* only important for AP, IBSS and mesh create IEs internally */
10140 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
10141 		return -EINVAL;
10142 
10143 	/* Even though the attribute is u32, the specification says
10144 	 * u8, so let's make sure we don't overflow.
10145 	 */
10146 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
10147 	if (cs_count > 255)
10148 		return -EINVAL;
10149 
10150 	params.count = cs_count;
10151 
10152 	if (!need_new_beacon)
10153 		goto skip_beacons;
10154 
10155 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after,
10156 				   info->extack);
10157 	if (err)
10158 		goto free;
10159 
10160 	csa_attrs = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*csa_attrs),
10161 			    GFP_KERNEL);
10162 	if (!csa_attrs) {
10163 		err = -ENOMEM;
10164 		goto free;
10165 	}
10166 
10167 	err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
10168 					  info->attrs[NL80211_ATTR_CSA_IES],
10169 					  nl80211_policy, info->extack);
10170 	if (err)
10171 		goto free;
10172 
10173 	err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa,
10174 				   info->extack);
10175 	if (err)
10176 		goto free;
10177 
10178 	if (!csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
10179 		err = -EINVAL;
10180 		goto free;
10181 	}
10182 
10183 	len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
10184 	if (!len || (len % sizeof(u16))) {
10185 		err = -EINVAL;
10186 		goto free;
10187 	}
10188 
10189 	params.n_counter_offsets_beacon = len / sizeof(u16);
10190 	if (rdev->wiphy.max_num_csa_counters &&
10191 	    (params.n_counter_offsets_beacon >
10192 	     rdev->wiphy.max_num_csa_counters)) {
10193 		err = -EINVAL;
10194 		goto free;
10195 	}
10196 
10197 	params.counter_offsets_beacon =
10198 		nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
10199 
10200 	/* sanity checks - counters should fit and be the same */
10201 	for (i = 0; i < params.n_counter_offsets_beacon; i++) {
10202 		u16 offset = params.counter_offsets_beacon[i];
10203 
10204 		if (offset >= params.beacon_csa.tail_len) {
10205 			err = -EINVAL;
10206 			goto free;
10207 		}
10208 
10209 		if (params.beacon_csa.tail[offset] != params.count) {
10210 			err = -EINVAL;
10211 			goto free;
10212 		}
10213 	}
10214 
10215 	if (csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
10216 		len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
10217 		if (!len || (len % sizeof(u16))) {
10218 			err = -EINVAL;
10219 			goto free;
10220 		}
10221 
10222 		params.n_counter_offsets_presp = len / sizeof(u16);
10223 		if (rdev->wiphy.max_num_csa_counters &&
10224 		    (params.n_counter_offsets_presp >
10225 		     rdev->wiphy.max_num_csa_counters)) {
10226 			err = -EINVAL;
10227 			goto free;
10228 		}
10229 
10230 		params.counter_offsets_presp =
10231 			nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
10232 
10233 		/* sanity checks - counters should fit and be the same */
10234 		for (i = 0; i < params.n_counter_offsets_presp; i++) {
10235 			u16 offset = params.counter_offsets_presp[i];
10236 
10237 			if (offset >= params.beacon_csa.probe_resp_len) {
10238 				err = -EINVAL;
10239 				goto free;
10240 			}
10241 
10242 			if (params.beacon_csa.probe_resp[offset] !=
10243 			    params.count) {
10244 				err = -EINVAL;
10245 				goto free;
10246 			}
10247 		}
10248 	}
10249 
10250 skip_beacons:
10251 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
10252 	if (err)
10253 		goto free;
10254 
10255 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
10256 					   wdev->iftype)) {
10257 		err = -EINVAL;
10258 		goto free;
10259 	}
10260 
10261 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
10262 					    &params.chandef,
10263 					    wdev->iftype);
10264 	if (err < 0)
10265 		goto free;
10266 
10267 	if (err > 0) {
10268 		params.radar_required = true;
10269 		if (need_handle_dfs_flag &&
10270 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
10271 			err = -EINVAL;
10272 			goto free;
10273 		}
10274 	}
10275 
10276 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
10277 		params.block_tx = true;
10278 
10279 	if (info->attrs[NL80211_ATTR_PUNCT_BITMAP]) {
10280 		err = nl80211_parse_punct_bitmap(rdev, info,
10281 						 &params.chandef,
10282 						 &params.punct_bitmap);
10283 		if (err)
10284 			goto free;
10285 	}
10286 
10287 	wdev_lock(wdev);
10288 	err = rdev_channel_switch(rdev, dev, &params);
10289 	wdev_unlock(wdev);
10290 
10291 free:
10292 	kfree(params.beacon_after.mbssid_ies);
10293 	kfree(params.beacon_csa.mbssid_ies);
10294 	kfree(params.beacon_after.rnr_ies);
10295 	kfree(params.beacon_csa.rnr_ies);
10296 	kfree(csa_attrs);
10297 	return err;
10298 }
10299 
10300 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
10301 			    u32 seq, int flags,
10302 			    struct cfg80211_registered_device *rdev,
10303 			    struct wireless_dev *wdev,
10304 			    struct cfg80211_internal_bss *intbss)
10305 {
10306 	struct cfg80211_bss *res = &intbss->pub;
10307 	const struct cfg80211_bss_ies *ies;
10308 	unsigned int link_id;
10309 	void *hdr;
10310 	struct nlattr *bss;
10311 
10312 	ASSERT_WDEV_LOCK(wdev);
10313 
10314 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
10315 			     NL80211_CMD_NEW_SCAN_RESULTS);
10316 	if (!hdr)
10317 		return -1;
10318 
10319 	genl_dump_check_consistent(cb, hdr);
10320 
10321 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
10322 		goto nla_put_failure;
10323 	if (wdev->netdev &&
10324 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
10325 		goto nla_put_failure;
10326 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
10327 			      NL80211_ATTR_PAD))
10328 		goto nla_put_failure;
10329 
10330 	bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
10331 	if (!bss)
10332 		goto nla_put_failure;
10333 	if ((!is_zero_ether_addr(res->bssid) &&
10334 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
10335 		goto nla_put_failure;
10336 
10337 	rcu_read_lock();
10338 	/* indicate whether we have probe response data or not */
10339 	if (rcu_access_pointer(res->proberesp_ies) &&
10340 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
10341 		goto fail_unlock_rcu;
10342 
10343 	/* this pointer prefers to be pointed to probe response data
10344 	 * but is always valid
10345 	 */
10346 	ies = rcu_dereference(res->ies);
10347 	if (ies) {
10348 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
10349 				      NL80211_BSS_PAD))
10350 			goto fail_unlock_rcu;
10351 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
10352 					ies->len, ies->data))
10353 			goto fail_unlock_rcu;
10354 	}
10355 
10356 	/* and this pointer is always (unless driver didn't know) beacon data */
10357 	ies = rcu_dereference(res->beacon_ies);
10358 	if (ies && ies->from_beacon) {
10359 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
10360 				      NL80211_BSS_PAD))
10361 			goto fail_unlock_rcu;
10362 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
10363 					ies->len, ies->data))
10364 			goto fail_unlock_rcu;
10365 	}
10366 	rcu_read_unlock();
10367 
10368 	if (res->beacon_interval &&
10369 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
10370 		goto nla_put_failure;
10371 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
10372 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
10373 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY_OFFSET,
10374 			res->channel->freq_offset) ||
10375 	    nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
10376 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
10377 			jiffies_to_msecs(jiffies - intbss->ts)))
10378 		goto nla_put_failure;
10379 
10380 	if (intbss->parent_tsf &&
10381 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
10382 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
10383 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
10384 		     intbss->parent_bssid)))
10385 		goto nla_put_failure;
10386 
10387 	if (intbss->ts_boottime &&
10388 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
10389 			      intbss->ts_boottime, NL80211_BSS_PAD))
10390 		goto nla_put_failure;
10391 
10392 	if (!nl80211_put_signal(msg, intbss->pub.chains,
10393 				intbss->pub.chain_signal,
10394 				NL80211_BSS_CHAIN_SIGNAL))
10395 		goto nla_put_failure;
10396 
10397 	switch (rdev->wiphy.signal_type) {
10398 	case CFG80211_SIGNAL_TYPE_MBM:
10399 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
10400 			goto nla_put_failure;
10401 		break;
10402 	case CFG80211_SIGNAL_TYPE_UNSPEC:
10403 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
10404 			goto nla_put_failure;
10405 		break;
10406 	default:
10407 		break;
10408 	}
10409 
10410 	switch (wdev->iftype) {
10411 	case NL80211_IFTYPE_P2P_CLIENT:
10412 	case NL80211_IFTYPE_STATION:
10413 		for_each_valid_link(wdev, link_id) {
10414 			if (intbss == wdev->links[link_id].client.current_bss &&
10415 			    (nla_put_u32(msg, NL80211_BSS_STATUS,
10416 					 NL80211_BSS_STATUS_ASSOCIATED) ||
10417 			     (wdev->valid_links &&
10418 			      (nla_put_u8(msg, NL80211_BSS_MLO_LINK_ID,
10419 					  link_id) ||
10420 			       nla_put(msg, NL80211_BSS_MLD_ADDR, ETH_ALEN,
10421 				       wdev->u.client.connected_addr)))))
10422 				goto nla_put_failure;
10423 		}
10424 		break;
10425 	case NL80211_IFTYPE_ADHOC:
10426 		if (intbss == wdev->u.ibss.current_bss &&
10427 		    nla_put_u32(msg, NL80211_BSS_STATUS,
10428 				NL80211_BSS_STATUS_IBSS_JOINED))
10429 			goto nla_put_failure;
10430 		break;
10431 	default:
10432 		break;
10433 	}
10434 
10435 	nla_nest_end(msg, bss);
10436 
10437 	genlmsg_end(msg, hdr);
10438 	return 0;
10439 
10440  fail_unlock_rcu:
10441 	rcu_read_unlock();
10442  nla_put_failure:
10443 	genlmsg_cancel(msg, hdr);
10444 	return -EMSGSIZE;
10445 }
10446 
10447 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
10448 {
10449 	struct cfg80211_registered_device *rdev;
10450 	struct cfg80211_internal_bss *scan;
10451 	struct wireless_dev *wdev;
10452 	int start = cb->args[2], idx = 0;
10453 	int err;
10454 
10455 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
10456 	if (err)
10457 		return err;
10458 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10459 	__acquire(&rdev->wiphy.mtx);
10460 
10461 	wdev_lock(wdev);
10462 	spin_lock_bh(&rdev->bss_lock);
10463 
10464 	/*
10465 	 * dump_scan will be called multiple times to break up the scan results
10466 	 * into multiple messages.  It is unlikely that any more bss-es will be
10467 	 * expired after the first call, so only call only call this on the
10468 	 * first dump_scan invocation.
10469 	 */
10470 	if (start == 0)
10471 		cfg80211_bss_expire(rdev);
10472 
10473 	cb->seq = rdev->bss_generation;
10474 
10475 	list_for_each_entry(scan, &rdev->bss_list, list) {
10476 		if (++idx <= start)
10477 			continue;
10478 		if (nl80211_send_bss(skb, cb,
10479 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10480 				rdev, wdev, scan) < 0) {
10481 			idx--;
10482 			break;
10483 		}
10484 	}
10485 
10486 	spin_unlock_bh(&rdev->bss_lock);
10487 	wdev_unlock(wdev);
10488 
10489 	cb->args[2] = idx;
10490 	wiphy_unlock(&rdev->wiphy);
10491 
10492 	return skb->len;
10493 }
10494 
10495 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
10496 			       int flags, struct net_device *dev,
10497 			       bool allow_radio_stats,
10498 			       struct survey_info *survey)
10499 {
10500 	void *hdr;
10501 	struct nlattr *infoattr;
10502 
10503 	/* skip radio stats if userspace didn't request them */
10504 	if (!survey->channel && !allow_radio_stats)
10505 		return 0;
10506 
10507 	hdr = nl80211hdr_put(msg, portid, seq, flags,
10508 			     NL80211_CMD_NEW_SURVEY_RESULTS);
10509 	if (!hdr)
10510 		return -ENOMEM;
10511 
10512 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
10513 		goto nla_put_failure;
10514 
10515 	infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
10516 	if (!infoattr)
10517 		goto nla_put_failure;
10518 
10519 	if (survey->channel &&
10520 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
10521 			survey->channel->center_freq))
10522 		goto nla_put_failure;
10523 
10524 	if (survey->channel && survey->channel->freq_offset &&
10525 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY_OFFSET,
10526 			survey->channel->freq_offset))
10527 		goto nla_put_failure;
10528 
10529 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
10530 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
10531 		goto nla_put_failure;
10532 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
10533 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
10534 		goto nla_put_failure;
10535 	if ((survey->filled & SURVEY_INFO_TIME) &&
10536 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
10537 			survey->time, NL80211_SURVEY_INFO_PAD))
10538 		goto nla_put_failure;
10539 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
10540 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
10541 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
10542 		goto nla_put_failure;
10543 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
10544 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
10545 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
10546 		goto nla_put_failure;
10547 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
10548 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
10549 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
10550 		goto nla_put_failure;
10551 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
10552 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
10553 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
10554 		goto nla_put_failure;
10555 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
10556 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
10557 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
10558 		goto nla_put_failure;
10559 	if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
10560 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
10561 			      survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
10562 		goto nla_put_failure;
10563 
10564 	nla_nest_end(msg, infoattr);
10565 
10566 	genlmsg_end(msg, hdr);
10567 	return 0;
10568 
10569  nla_put_failure:
10570 	genlmsg_cancel(msg, hdr);
10571 	return -EMSGSIZE;
10572 }
10573 
10574 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
10575 {
10576 	struct nlattr **attrbuf;
10577 	struct survey_info survey;
10578 	struct cfg80211_registered_device *rdev;
10579 	struct wireless_dev *wdev;
10580 	int survey_idx = cb->args[2];
10581 	int res;
10582 	bool radio_stats;
10583 
10584 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
10585 	if (!attrbuf)
10586 		return -ENOMEM;
10587 
10588 	res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, attrbuf);
10589 	if (res) {
10590 		kfree(attrbuf);
10591 		return res;
10592 	}
10593 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10594 	__acquire(&rdev->wiphy.mtx);
10595 
10596 	/* prepare_wdev_dump parsed the attributes */
10597 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
10598 
10599 	if (!wdev->netdev) {
10600 		res = -EINVAL;
10601 		goto out_err;
10602 	}
10603 
10604 	if (!rdev->ops->dump_survey) {
10605 		res = -EOPNOTSUPP;
10606 		goto out_err;
10607 	}
10608 
10609 	while (1) {
10610 		wdev_lock(wdev);
10611 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
10612 		wdev_unlock(wdev);
10613 		if (res == -ENOENT)
10614 			break;
10615 		if (res)
10616 			goto out_err;
10617 
10618 		/* don't send disabled channels, but do send non-channel data */
10619 		if (survey.channel &&
10620 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
10621 			survey_idx++;
10622 			continue;
10623 		}
10624 
10625 		if (nl80211_send_survey(skb,
10626 				NETLINK_CB(cb->skb).portid,
10627 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10628 				wdev->netdev, radio_stats, &survey) < 0)
10629 			goto out;
10630 		survey_idx++;
10631 	}
10632 
10633  out:
10634 	cb->args[2] = survey_idx;
10635 	res = skb->len;
10636  out_err:
10637 	kfree(attrbuf);
10638 	wiphy_unlock(&rdev->wiphy);
10639 	return res;
10640 }
10641 
10642 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
10643 {
10644 	return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
10645 				  NL80211_WPA_VERSION_2 |
10646 				  NL80211_WPA_VERSION_3));
10647 }
10648 
10649 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
10650 {
10651 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10652 	struct net_device *dev = info->user_ptr[1];
10653 	struct ieee80211_channel *chan;
10654 	const u8 *bssid, *ssid;
10655 	int err, ssid_len;
10656 	enum nl80211_auth_type auth_type;
10657 	struct key_parse key;
10658 	bool local_state_change;
10659 	struct cfg80211_auth_request req = {};
10660 	u32 freq;
10661 
10662 	if (!info->attrs[NL80211_ATTR_MAC])
10663 		return -EINVAL;
10664 
10665 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
10666 		return -EINVAL;
10667 
10668 	if (!info->attrs[NL80211_ATTR_SSID])
10669 		return -EINVAL;
10670 
10671 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10672 		return -EINVAL;
10673 
10674 	err = nl80211_parse_key(info, &key);
10675 	if (err)
10676 		return err;
10677 
10678 	if (key.idx >= 0) {
10679 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
10680 			return -EINVAL;
10681 		if (!key.p.key || !key.p.key_len)
10682 			return -EINVAL;
10683 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
10684 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
10685 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
10686 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
10687 			return -EINVAL;
10688 		if (key.idx > 3)
10689 			return -EINVAL;
10690 	} else {
10691 		key.p.key_len = 0;
10692 		key.p.key = NULL;
10693 	}
10694 
10695 	if (key.idx >= 0) {
10696 		int i;
10697 		bool ok = false;
10698 
10699 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
10700 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
10701 				ok = true;
10702 				break;
10703 			}
10704 		}
10705 		if (!ok)
10706 			return -EINVAL;
10707 	}
10708 
10709 	if (!rdev->ops->auth)
10710 		return -EOPNOTSUPP;
10711 
10712 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10713 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10714 		return -EOPNOTSUPP;
10715 
10716 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10717 	freq = MHZ_TO_KHZ(nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
10718 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10719 		freq +=
10720 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10721 
10722 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10723 	if (!chan)
10724 		return -EINVAL;
10725 
10726 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10727 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10728 
10729 	if (info->attrs[NL80211_ATTR_IE]) {
10730 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10731 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10732 	}
10733 
10734 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10735 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
10736 		return -EINVAL;
10737 
10738 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
10739 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
10740 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
10741 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
10742 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
10743 		return -EINVAL;
10744 
10745 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
10746 		if (auth_type != NL80211_AUTHTYPE_SAE &&
10747 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
10748 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
10749 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
10750 			return -EINVAL;
10751 		req.auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
10752 		req.auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
10753 	}
10754 
10755 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
10756 
10757 	/*
10758 	 * Since we no longer track auth state, ignore
10759 	 * requests to only change local state.
10760 	 */
10761 	if (local_state_change)
10762 		return 0;
10763 
10764 	req.auth_type = auth_type;
10765 	req.key = key.p.key;
10766 	req.key_len = key.p.key_len;
10767 	req.key_idx = key.idx;
10768 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
10769 	if (req.link_id >= 0) {
10770 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
10771 			return -EINVAL;
10772 		if (!info->attrs[NL80211_ATTR_MLD_ADDR])
10773 			return -EINVAL;
10774 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
10775 		if (!is_valid_ether_addr(req.ap_mld_addr))
10776 			return -EINVAL;
10777 	}
10778 
10779 	req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
10780 				   IEEE80211_BSS_TYPE_ESS,
10781 				   IEEE80211_PRIVACY_ANY);
10782 	if (!req.bss)
10783 		return -ENOENT;
10784 
10785 	wdev_lock(dev->ieee80211_ptr);
10786 	err = cfg80211_mlme_auth(rdev, dev, &req);
10787 	wdev_unlock(dev->ieee80211_ptr);
10788 
10789 	cfg80211_put_bss(&rdev->wiphy, req.bss);
10790 
10791 	return err;
10792 }
10793 
10794 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
10795 				     struct genl_info *info)
10796 {
10797 	if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10798 		GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
10799 		return -EINVAL;
10800 	}
10801 
10802 	if (!rdev->ops->tx_control_port ||
10803 	    !wiphy_ext_feature_isset(&rdev->wiphy,
10804 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
10805 		return -EOPNOTSUPP;
10806 
10807 	return 0;
10808 }
10809 
10810 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
10811 				   struct genl_info *info,
10812 				   struct cfg80211_crypto_settings *settings,
10813 				   int cipher_limit)
10814 {
10815 	memset(settings, 0, sizeof(*settings));
10816 
10817 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
10818 
10819 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
10820 		u16 proto;
10821 
10822 		proto = nla_get_u16(
10823 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
10824 		settings->control_port_ethertype = cpu_to_be16(proto);
10825 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
10826 		    proto != ETH_P_PAE)
10827 			return -EINVAL;
10828 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
10829 			settings->control_port_no_encrypt = true;
10830 	} else
10831 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
10832 
10833 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
10834 		int r = validate_pae_over_nl80211(rdev, info);
10835 
10836 		if (r < 0)
10837 			return r;
10838 
10839 		settings->control_port_over_nl80211 = true;
10840 
10841 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH])
10842 			settings->control_port_no_preauth = true;
10843 	}
10844 
10845 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
10846 		void *data;
10847 		int len, i;
10848 
10849 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10850 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10851 		settings->n_ciphers_pairwise = len / sizeof(u32);
10852 
10853 		if (len % sizeof(u32))
10854 			return -EINVAL;
10855 
10856 		if (settings->n_ciphers_pairwise > cipher_limit)
10857 			return -EINVAL;
10858 
10859 		memcpy(settings->ciphers_pairwise, data, len);
10860 
10861 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
10862 			if (!cfg80211_supported_cipher_suite(
10863 					&rdev->wiphy,
10864 					settings->ciphers_pairwise[i]))
10865 				return -EINVAL;
10866 	}
10867 
10868 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
10869 		settings->cipher_group =
10870 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
10871 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
10872 						     settings->cipher_group))
10873 			return -EINVAL;
10874 	}
10875 
10876 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
10877 		settings->wpa_versions =
10878 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
10879 		if (!nl80211_valid_wpa_versions(settings->wpa_versions))
10880 			return -EINVAL;
10881 	}
10882 
10883 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
10884 		void *data;
10885 		int len;
10886 
10887 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
10888 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
10889 		settings->n_akm_suites = len / sizeof(u32);
10890 
10891 		if (len % sizeof(u32))
10892 			return -EINVAL;
10893 
10894 		if (settings->n_akm_suites > rdev->wiphy.max_num_akm_suites)
10895 			return -EINVAL;
10896 
10897 		memcpy(settings->akm_suites, data, len);
10898 	}
10899 
10900 	if (info->attrs[NL80211_ATTR_PMK]) {
10901 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
10902 			return -EINVAL;
10903 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10904 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK) &&
10905 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10906 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_AP_PSK))
10907 			return -EINVAL;
10908 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10909 	}
10910 
10911 	if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
10912 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10913 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
10914 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10915 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP))
10916 			return -EINVAL;
10917 		settings->sae_pwd =
10918 			nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10919 		settings->sae_pwd_len =
10920 			nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10921 	}
10922 
10923 	if (info->attrs[NL80211_ATTR_SAE_PWE])
10924 		settings->sae_pwe =
10925 			nla_get_u8(info->attrs[NL80211_ATTR_SAE_PWE]);
10926 	else
10927 		settings->sae_pwe = NL80211_SAE_PWE_UNSPECIFIED;
10928 
10929 	return 0;
10930 }
10931 
10932 static struct cfg80211_bss *nl80211_assoc_bss(struct cfg80211_registered_device *rdev,
10933 					      const u8 *ssid, int ssid_len,
10934 					      struct nlattr **attrs)
10935 {
10936 	struct ieee80211_channel *chan;
10937 	struct cfg80211_bss *bss;
10938 	const u8 *bssid;
10939 	u32 freq;
10940 
10941 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_WIPHY_FREQ])
10942 		return ERR_PTR(-EINVAL);
10943 
10944 	bssid = nla_data(attrs[NL80211_ATTR_MAC]);
10945 
10946 	freq = MHZ_TO_KHZ(nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ]));
10947 	if (attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10948 		freq += nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10949 
10950 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10951 	if (!chan)
10952 		return ERR_PTR(-EINVAL);
10953 
10954 	bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid,
10955 			       ssid, ssid_len,
10956 			       IEEE80211_BSS_TYPE_ESS,
10957 			       IEEE80211_PRIVACY_ANY);
10958 	if (!bss)
10959 		return ERR_PTR(-ENOENT);
10960 
10961 	return bss;
10962 }
10963 
10964 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
10965 {
10966 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10967 	struct net_device *dev = info->user_ptr[1];
10968 	struct cfg80211_assoc_request req = {};
10969 	struct nlattr **attrs = NULL;
10970 	const u8 *ap_addr, *ssid;
10971 	unsigned int link_id;
10972 	int err, ssid_len;
10973 
10974 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
10975 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10976 		return -EPERM;
10977 
10978 	if (!info->attrs[NL80211_ATTR_SSID])
10979 		return -EINVAL;
10980 
10981 	if (!rdev->ops->assoc)
10982 		return -EOPNOTSUPP;
10983 
10984 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10985 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10986 		return -EOPNOTSUPP;
10987 
10988 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10989 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10990 
10991 	if (info->attrs[NL80211_ATTR_IE]) {
10992 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10993 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10994 
10995 		if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
10996 					   req.ie, req.ie_len)) {
10997 			GENL_SET_ERR_MSG(info,
10998 					 "non-inheritance makes no sense");
10999 			return -EINVAL;
11000 		}
11001 	}
11002 
11003 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
11004 		enum nl80211_mfp mfp =
11005 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
11006 		if (mfp == NL80211_MFP_REQUIRED)
11007 			req.use_mfp = true;
11008 		else if (mfp != NL80211_MFP_NO)
11009 			return -EINVAL;
11010 	}
11011 
11012 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
11013 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
11014 
11015 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
11016 		req.flags |= ASSOC_REQ_DISABLE_HT;
11017 
11018 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11019 		memcpy(&req.ht_capa_mask,
11020 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11021 		       sizeof(req.ht_capa_mask));
11022 
11023 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11024 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11025 			return -EINVAL;
11026 		memcpy(&req.ht_capa,
11027 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11028 		       sizeof(req.ht_capa));
11029 	}
11030 
11031 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
11032 		req.flags |= ASSOC_REQ_DISABLE_VHT;
11033 
11034 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
11035 		req.flags |= ASSOC_REQ_DISABLE_HE;
11036 
11037 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
11038 		req.flags |= ASSOC_REQ_DISABLE_EHT;
11039 
11040 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11041 		memcpy(&req.vht_capa_mask,
11042 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
11043 		       sizeof(req.vht_capa_mask));
11044 
11045 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
11046 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11047 			return -EINVAL;
11048 		memcpy(&req.vht_capa,
11049 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
11050 		       sizeof(req.vht_capa));
11051 	}
11052 
11053 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
11054 		if (!((rdev->wiphy.features &
11055 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
11056 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
11057 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11058 					     NL80211_EXT_FEATURE_RRM))
11059 			return -EINVAL;
11060 		req.flags |= ASSOC_REQ_USE_RRM;
11061 	}
11062 
11063 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
11064 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
11065 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
11066 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
11067 			return -EINVAL;
11068 		req.fils_nonces =
11069 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
11070 	}
11071 
11072 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]) {
11073 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY])
11074 			return -EINVAL;
11075 		memcpy(&req.s1g_capa_mask,
11076 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]),
11077 		       sizeof(req.s1g_capa_mask));
11078 	}
11079 
11080 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY]) {
11081 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK])
11082 			return -EINVAL;
11083 		memcpy(&req.s1g_capa,
11084 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY]),
11085 		       sizeof(req.s1g_capa));
11086 	}
11087 
11088 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
11089 
11090 	if (info->attrs[NL80211_ATTR_MLO_LINKS]) {
11091 		unsigned int attrsize = NUM_NL80211_ATTR * sizeof(*attrs);
11092 		struct nlattr *link;
11093 		int rem = 0;
11094 
11095 		if (req.link_id < 0)
11096 			return -EINVAL;
11097 
11098 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
11099 			return -EINVAL;
11100 
11101 		if (info->attrs[NL80211_ATTR_MAC] ||
11102 		    info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
11103 		    !info->attrs[NL80211_ATTR_MLD_ADDR])
11104 			return -EINVAL;
11105 
11106 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
11107 		ap_addr = req.ap_mld_addr;
11108 
11109 		attrs = kzalloc(attrsize, GFP_KERNEL);
11110 		if (!attrs)
11111 			return -ENOMEM;
11112 
11113 		nla_for_each_nested(link,
11114 				    info->attrs[NL80211_ATTR_MLO_LINKS],
11115 				    rem) {
11116 			memset(attrs, 0, attrsize);
11117 
11118 			nla_parse_nested(attrs, NL80211_ATTR_MAX,
11119 					 link, NULL, NULL);
11120 
11121 			if (!attrs[NL80211_ATTR_MLO_LINK_ID]) {
11122 				err = -EINVAL;
11123 				goto free;
11124 			}
11125 
11126 			link_id = nla_get_u8(attrs[NL80211_ATTR_MLO_LINK_ID]);
11127 			/* cannot use the same link ID again */
11128 			if (req.links[link_id].bss) {
11129 				err = -EINVAL;
11130 				goto free;
11131 			}
11132 			req.links[link_id].bss =
11133 				nl80211_assoc_bss(rdev, ssid, ssid_len, attrs);
11134 			if (IS_ERR(req.links[link_id].bss)) {
11135 				err = PTR_ERR(req.links[link_id].bss);
11136 				req.links[link_id].bss = NULL;
11137 				goto free;
11138 			}
11139 
11140 			if (attrs[NL80211_ATTR_IE]) {
11141 				req.links[link_id].elems =
11142 					nla_data(attrs[NL80211_ATTR_IE]);
11143 				req.links[link_id].elems_len =
11144 					nla_len(attrs[NL80211_ATTR_IE]);
11145 
11146 				if (cfg80211_find_elem(WLAN_EID_FRAGMENT,
11147 						       req.links[link_id].elems,
11148 						       req.links[link_id].elems_len)) {
11149 					GENL_SET_ERR_MSG(info,
11150 							 "cannot deal with fragmentation");
11151 					err = -EINVAL;
11152 					goto free;
11153 				}
11154 
11155 				if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
11156 							   req.links[link_id].elems,
11157 							   req.links[link_id].elems_len)) {
11158 					GENL_SET_ERR_MSG(info,
11159 							 "cannot deal with non-inheritance");
11160 					err = -EINVAL;
11161 					goto free;
11162 				}
11163 			}
11164 
11165 			req.links[link_id].disabled =
11166 				nla_get_flag(attrs[NL80211_ATTR_MLO_LINK_DISABLED]);
11167 		}
11168 
11169 		if (!req.links[req.link_id].bss) {
11170 			err = -EINVAL;
11171 			goto free;
11172 		}
11173 
11174 		if (req.links[req.link_id].elems_len) {
11175 			GENL_SET_ERR_MSG(info,
11176 					 "cannot have per-link elems on assoc link");
11177 			err = -EINVAL;
11178 			goto free;
11179 		}
11180 
11181 		if (req.links[req.link_id].disabled) {
11182 			GENL_SET_ERR_MSG(info,
11183 					 "cannot have assoc link disabled");
11184 			err = -EINVAL;
11185 			goto free;
11186 		}
11187 
11188 		kfree(attrs);
11189 		attrs = NULL;
11190 	} else {
11191 		if (req.link_id >= 0)
11192 			return -EINVAL;
11193 
11194 		req.bss = nl80211_assoc_bss(rdev, ssid, ssid_len, info->attrs);
11195 		if (IS_ERR(req.bss))
11196 			return PTR_ERR(req.bss);
11197 		ap_addr = req.bss->bssid;
11198 	}
11199 
11200 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
11201 	if (!err) {
11202 		wdev_lock(dev->ieee80211_ptr);
11203 
11204 		err = cfg80211_mlme_assoc(rdev, dev, &req);
11205 
11206 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
11207 			dev->ieee80211_ptr->conn_owner_nlportid =
11208 				info->snd_portid;
11209 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
11210 			       ap_addr, ETH_ALEN);
11211 		}
11212 
11213 		wdev_unlock(dev->ieee80211_ptr);
11214 	}
11215 
11216 free:
11217 	for (link_id = 0; link_id < ARRAY_SIZE(req.links); link_id++)
11218 		cfg80211_put_bss(&rdev->wiphy, req.links[link_id].bss);
11219 	cfg80211_put_bss(&rdev->wiphy, req.bss);
11220 	kfree(attrs);
11221 
11222 	return err;
11223 }
11224 
11225 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
11226 {
11227 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11228 	struct net_device *dev = info->user_ptr[1];
11229 	const u8 *ie = NULL, *bssid;
11230 	int ie_len = 0, err;
11231 	u16 reason_code;
11232 	bool local_state_change;
11233 
11234 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11235 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11236 		return -EPERM;
11237 
11238 	if (!info->attrs[NL80211_ATTR_MAC])
11239 		return -EINVAL;
11240 
11241 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11242 		return -EINVAL;
11243 
11244 	if (!rdev->ops->deauth)
11245 		return -EOPNOTSUPP;
11246 
11247 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11248 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11249 		return -EOPNOTSUPP;
11250 
11251 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11252 
11253 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11254 	if (reason_code == 0) {
11255 		/* Reason Code 0 is reserved */
11256 		return -EINVAL;
11257 	}
11258 
11259 	if (info->attrs[NL80211_ATTR_IE]) {
11260 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11261 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11262 	}
11263 
11264 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11265 
11266 	wdev_lock(dev->ieee80211_ptr);
11267 	err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
11268 				   local_state_change);
11269 	wdev_unlock(dev->ieee80211_ptr);
11270 	return err;
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, err;
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 	wdev_lock(dev->ieee80211_ptr);
11315 	err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
11316 				     local_state_change);
11317 	wdev_unlock(dev->ieee80211_ptr);
11318 	return err;
11319 }
11320 
11321 static bool
11322 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
11323 			 int mcast_rate[NUM_NL80211_BANDS],
11324 			 int rateval)
11325 {
11326 	struct wiphy *wiphy = &rdev->wiphy;
11327 	bool found = false;
11328 	int band, i;
11329 
11330 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
11331 		struct ieee80211_supported_band *sband;
11332 
11333 		sband = wiphy->bands[band];
11334 		if (!sband)
11335 			continue;
11336 
11337 		for (i = 0; i < sband->n_bitrates; i++) {
11338 			if (sband->bitrates[i].bitrate == rateval) {
11339 				mcast_rate[band] = i + 1;
11340 				found = true;
11341 				break;
11342 			}
11343 		}
11344 	}
11345 
11346 	return found;
11347 }
11348 
11349 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
11350 {
11351 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11352 	struct net_device *dev = info->user_ptr[1];
11353 	struct cfg80211_ibss_params ibss;
11354 	struct wiphy *wiphy;
11355 	struct cfg80211_cached_keys *connkeys = NULL;
11356 	int err;
11357 
11358 	memset(&ibss, 0, sizeof(ibss));
11359 
11360 	if (!info->attrs[NL80211_ATTR_SSID] ||
11361 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11362 		return -EINVAL;
11363 
11364 	ibss.beacon_interval = 100;
11365 
11366 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
11367 		ibss.beacon_interval =
11368 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11369 
11370 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
11371 					   ibss.beacon_interval);
11372 	if (err)
11373 		return err;
11374 
11375 	if (!rdev->ops->join_ibss)
11376 		return -EOPNOTSUPP;
11377 
11378 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11379 		return -EOPNOTSUPP;
11380 
11381 	wiphy = &rdev->wiphy;
11382 
11383 	if (info->attrs[NL80211_ATTR_MAC]) {
11384 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11385 
11386 		if (!is_valid_ether_addr(ibss.bssid))
11387 			return -EINVAL;
11388 	}
11389 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11390 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11391 
11392 	if (info->attrs[NL80211_ATTR_IE]) {
11393 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11394 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11395 	}
11396 
11397 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
11398 	if (err)
11399 		return err;
11400 
11401 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
11402 				     NL80211_IFTYPE_ADHOC))
11403 		return -EINVAL;
11404 
11405 	switch (ibss.chandef.width) {
11406 	case NL80211_CHAN_WIDTH_5:
11407 	case NL80211_CHAN_WIDTH_10:
11408 	case NL80211_CHAN_WIDTH_20_NOHT:
11409 		break;
11410 	case NL80211_CHAN_WIDTH_20:
11411 	case NL80211_CHAN_WIDTH_40:
11412 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11413 			return -EINVAL;
11414 		break;
11415 	case NL80211_CHAN_WIDTH_80:
11416 	case NL80211_CHAN_WIDTH_80P80:
11417 	case NL80211_CHAN_WIDTH_160:
11418 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11419 			return -EINVAL;
11420 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
11421 					     NL80211_EXT_FEATURE_VHT_IBSS))
11422 			return -EINVAL;
11423 		break;
11424 	case NL80211_CHAN_WIDTH_320:
11425 		return -EINVAL;
11426 	default:
11427 		return -EINVAL;
11428 	}
11429 
11430 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
11431 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
11432 
11433 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11434 		u8 *rates =
11435 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11436 		int n_rates =
11437 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11438 		struct ieee80211_supported_band *sband =
11439 			wiphy->bands[ibss.chandef.chan->band];
11440 
11441 		err = ieee80211_get_ratemask(sband, rates, n_rates,
11442 					     &ibss.basic_rates);
11443 		if (err)
11444 			return err;
11445 	}
11446 
11447 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11448 		memcpy(&ibss.ht_capa_mask,
11449 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11450 		       sizeof(ibss.ht_capa_mask));
11451 
11452 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11453 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11454 			return -EINVAL;
11455 		memcpy(&ibss.ht_capa,
11456 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11457 		       sizeof(ibss.ht_capa));
11458 	}
11459 
11460 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11461 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
11462 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11463 		return -EINVAL;
11464 
11465 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11466 		bool no_ht = false;
11467 
11468 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
11469 		if (IS_ERR(connkeys))
11470 			return PTR_ERR(connkeys);
11471 
11472 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
11473 		    no_ht) {
11474 			kfree_sensitive(connkeys);
11475 			return -EINVAL;
11476 		}
11477 	}
11478 
11479 	ibss.control_port =
11480 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
11481 
11482 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11483 		int r = validate_pae_over_nl80211(rdev, info);
11484 
11485 		if (r < 0) {
11486 			kfree_sensitive(connkeys);
11487 			return r;
11488 		}
11489 
11490 		ibss.control_port_over_nl80211 = true;
11491 	}
11492 
11493 	ibss.userspace_handles_dfs =
11494 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11495 
11496 	wdev_lock(dev->ieee80211_ptr);
11497 	err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
11498 	if (err)
11499 		kfree_sensitive(connkeys);
11500 	else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
11501 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11502 	wdev_unlock(dev->ieee80211_ptr);
11503 
11504 	return err;
11505 }
11506 
11507 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
11508 {
11509 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11510 	struct net_device *dev = info->user_ptr[1];
11511 
11512 	if (!rdev->ops->leave_ibss)
11513 		return -EOPNOTSUPP;
11514 
11515 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11516 		return -EOPNOTSUPP;
11517 
11518 	return cfg80211_leave_ibss(rdev, dev, false);
11519 }
11520 
11521 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
11522 {
11523 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11524 	struct net_device *dev = info->user_ptr[1];
11525 	int mcast_rate[NUM_NL80211_BANDS];
11526 	u32 nla_rate;
11527 
11528 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
11529 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
11530 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
11531 		return -EOPNOTSUPP;
11532 
11533 	if (!rdev->ops->set_mcast_rate)
11534 		return -EOPNOTSUPP;
11535 
11536 	memset(mcast_rate, 0, sizeof(mcast_rate));
11537 
11538 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
11539 		return -EINVAL;
11540 
11541 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
11542 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
11543 		return -EINVAL;
11544 
11545 	return rdev_set_mcast_rate(rdev, dev, mcast_rate);
11546 }
11547 
11548 static struct sk_buff *
11549 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
11550 			    struct wireless_dev *wdev, int approxlen,
11551 			    u32 portid, u32 seq, enum nl80211_commands cmd,
11552 			    enum nl80211_attrs attr,
11553 			    const struct nl80211_vendor_cmd_info *info,
11554 			    gfp_t gfp)
11555 {
11556 	struct sk_buff *skb;
11557 	void *hdr;
11558 	struct nlattr *data;
11559 
11560 	skb = nlmsg_new(approxlen + 100, gfp);
11561 	if (!skb)
11562 		return NULL;
11563 
11564 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
11565 	if (!hdr) {
11566 		kfree_skb(skb);
11567 		return NULL;
11568 	}
11569 
11570 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11571 		goto nla_put_failure;
11572 
11573 	if (info) {
11574 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
11575 				info->vendor_id))
11576 			goto nla_put_failure;
11577 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
11578 				info->subcmd))
11579 			goto nla_put_failure;
11580 	}
11581 
11582 	if (wdev) {
11583 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
11584 				      wdev_id(wdev), NL80211_ATTR_PAD))
11585 			goto nla_put_failure;
11586 		if (wdev->netdev &&
11587 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
11588 				wdev->netdev->ifindex))
11589 			goto nla_put_failure;
11590 	}
11591 
11592 	data = nla_nest_start_noflag(skb, attr);
11593 	if (!data)
11594 		goto nla_put_failure;
11595 
11596 	((void **)skb->cb)[0] = rdev;
11597 	((void **)skb->cb)[1] = hdr;
11598 	((void **)skb->cb)[2] = data;
11599 
11600 	return skb;
11601 
11602  nla_put_failure:
11603 	kfree_skb(skb);
11604 	return NULL;
11605 }
11606 
11607 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
11608 					   struct wireless_dev *wdev,
11609 					   enum nl80211_commands cmd,
11610 					   enum nl80211_attrs attr,
11611 					   unsigned int portid,
11612 					   int vendor_event_idx,
11613 					   int approxlen, gfp_t gfp)
11614 {
11615 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11616 	const struct nl80211_vendor_cmd_info *info;
11617 
11618 	switch (cmd) {
11619 	case NL80211_CMD_TESTMODE:
11620 		if (WARN_ON(vendor_event_idx != -1))
11621 			return NULL;
11622 		info = NULL;
11623 		break;
11624 	case NL80211_CMD_VENDOR:
11625 		if (WARN_ON(vendor_event_idx < 0 ||
11626 			    vendor_event_idx >= wiphy->n_vendor_events))
11627 			return NULL;
11628 		info = &wiphy->vendor_events[vendor_event_idx];
11629 		break;
11630 	default:
11631 		WARN_ON(1);
11632 		return NULL;
11633 	}
11634 
11635 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
11636 					   cmd, attr, info, gfp);
11637 }
11638 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
11639 
11640 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
11641 {
11642 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
11643 	void *hdr = ((void **)skb->cb)[1];
11644 	struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
11645 	struct nlattr *data = ((void **)skb->cb)[2];
11646 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
11647 
11648 	/* clear CB data for netlink core to own from now on */
11649 	memset(skb->cb, 0, sizeof(skb->cb));
11650 
11651 	nla_nest_end(skb, data);
11652 	genlmsg_end(skb, hdr);
11653 
11654 	if (nlhdr->nlmsg_pid) {
11655 		genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
11656 				nlhdr->nlmsg_pid);
11657 	} else {
11658 		if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
11659 			mcgrp = NL80211_MCGRP_VENDOR;
11660 
11661 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11662 					skb, 0, mcgrp, gfp);
11663 	}
11664 }
11665 EXPORT_SYMBOL(__cfg80211_send_event_skb);
11666 
11667 #ifdef CONFIG_NL80211_TESTMODE
11668 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
11669 {
11670 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11671 	struct wireless_dev *wdev;
11672 	int err;
11673 
11674 	lockdep_assert_held(&rdev->wiphy.mtx);
11675 
11676 	wdev = __cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
11677 					  info->attrs);
11678 
11679 	if (!rdev->ops->testmode_cmd)
11680 		return -EOPNOTSUPP;
11681 
11682 	if (IS_ERR(wdev)) {
11683 		err = PTR_ERR(wdev);
11684 		if (err != -EINVAL)
11685 			return err;
11686 		wdev = NULL;
11687 	} else if (wdev->wiphy != &rdev->wiphy) {
11688 		return -EINVAL;
11689 	}
11690 
11691 	if (!info->attrs[NL80211_ATTR_TESTDATA])
11692 		return -EINVAL;
11693 
11694 	rdev->cur_cmd_info = info;
11695 	err = rdev_testmode_cmd(rdev, wdev,
11696 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
11697 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
11698 	rdev->cur_cmd_info = NULL;
11699 
11700 	return err;
11701 }
11702 
11703 static int nl80211_testmode_dump(struct sk_buff *skb,
11704 				 struct netlink_callback *cb)
11705 {
11706 	struct cfg80211_registered_device *rdev;
11707 	struct nlattr **attrbuf = NULL;
11708 	int err;
11709 	long phy_idx;
11710 	void *data = NULL;
11711 	int data_len = 0;
11712 
11713 	rtnl_lock();
11714 
11715 	if (cb->args[0]) {
11716 		/*
11717 		 * 0 is a valid index, but not valid for args[0],
11718 		 * so we need to offset by 1.
11719 		 */
11720 		phy_idx = cb->args[0] - 1;
11721 
11722 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
11723 		if (!rdev) {
11724 			err = -ENOENT;
11725 			goto out_err;
11726 		}
11727 	} else {
11728 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
11729 				  GFP_KERNEL);
11730 		if (!attrbuf) {
11731 			err = -ENOMEM;
11732 			goto out_err;
11733 		}
11734 
11735 		err = nlmsg_parse_deprecated(cb->nlh,
11736 					     GENL_HDRLEN + nl80211_fam.hdrsize,
11737 					     attrbuf, nl80211_fam.maxattr,
11738 					     nl80211_policy, NULL);
11739 		if (err)
11740 			goto out_err;
11741 
11742 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
11743 		if (IS_ERR(rdev)) {
11744 			err = PTR_ERR(rdev);
11745 			goto out_err;
11746 		}
11747 		phy_idx = rdev->wiphy_idx;
11748 
11749 		if (attrbuf[NL80211_ATTR_TESTDATA])
11750 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
11751 	}
11752 
11753 	if (cb->args[1]) {
11754 		data = nla_data((void *)cb->args[1]);
11755 		data_len = nla_len((void *)cb->args[1]);
11756 	}
11757 
11758 	if (!rdev->ops->testmode_dump) {
11759 		err = -EOPNOTSUPP;
11760 		goto out_err;
11761 	}
11762 
11763 	while (1) {
11764 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
11765 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
11766 					   NL80211_CMD_TESTMODE);
11767 		struct nlattr *tmdata;
11768 
11769 		if (!hdr)
11770 			break;
11771 
11772 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
11773 			genlmsg_cancel(skb, hdr);
11774 			break;
11775 		}
11776 
11777 		tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
11778 		if (!tmdata) {
11779 			genlmsg_cancel(skb, hdr);
11780 			break;
11781 		}
11782 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
11783 		nla_nest_end(skb, tmdata);
11784 
11785 		if (err == -ENOBUFS || err == -ENOENT) {
11786 			genlmsg_cancel(skb, hdr);
11787 			break;
11788 		} else if (err) {
11789 			genlmsg_cancel(skb, hdr);
11790 			goto out_err;
11791 		}
11792 
11793 		genlmsg_end(skb, hdr);
11794 	}
11795 
11796 	err = skb->len;
11797 	/* see above */
11798 	cb->args[0] = phy_idx + 1;
11799  out_err:
11800 	kfree(attrbuf);
11801 	rtnl_unlock();
11802 	return err;
11803 }
11804 #endif
11805 
11806 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
11807 {
11808 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11809 	struct net_device *dev = info->user_ptr[1];
11810 	struct cfg80211_connect_params connect;
11811 	struct wiphy *wiphy;
11812 	struct cfg80211_cached_keys *connkeys = NULL;
11813 	u32 freq = 0;
11814 	int err;
11815 
11816 	memset(&connect, 0, sizeof(connect));
11817 
11818 	if (!info->attrs[NL80211_ATTR_SSID] ||
11819 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11820 		return -EINVAL;
11821 
11822 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
11823 		connect.auth_type =
11824 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
11825 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
11826 					     NL80211_CMD_CONNECT))
11827 			return -EINVAL;
11828 	} else
11829 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
11830 
11831 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
11832 
11833 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
11834 	    !wiphy_ext_feature_isset(&rdev->wiphy,
11835 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
11836 		return -EINVAL;
11837 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
11838 
11839 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
11840 				      NL80211_MAX_NR_CIPHER_SUITES);
11841 	if (err)
11842 		return err;
11843 
11844 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11845 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11846 		return -EOPNOTSUPP;
11847 
11848 	wiphy = &rdev->wiphy;
11849 
11850 	connect.bg_scan_period = -1;
11851 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
11852 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
11853 		connect.bg_scan_period =
11854 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
11855 	}
11856 
11857 	if (info->attrs[NL80211_ATTR_MAC])
11858 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11859 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
11860 		connect.bssid_hint =
11861 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
11862 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11863 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11864 
11865 	if (info->attrs[NL80211_ATTR_IE]) {
11866 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11867 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11868 	}
11869 
11870 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
11871 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
11872 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
11873 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11874 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
11875 			return -EOPNOTSUPP;
11876 	} else {
11877 		connect.mfp = NL80211_MFP_NO;
11878 	}
11879 
11880 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
11881 		connect.prev_bssid =
11882 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
11883 
11884 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ])
11885 		freq = MHZ_TO_KHZ(nla_get_u32(
11886 					info->attrs[NL80211_ATTR_WIPHY_FREQ]));
11887 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
11888 		freq +=
11889 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
11890 
11891 	if (freq) {
11892 		connect.channel = nl80211_get_valid_chan(wiphy, freq);
11893 		if (!connect.channel)
11894 			return -EINVAL;
11895 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
11896 		freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
11897 		freq = MHZ_TO_KHZ(freq);
11898 		connect.channel_hint = nl80211_get_valid_chan(wiphy, freq);
11899 		if (!connect.channel_hint)
11900 			return -EINVAL;
11901 	}
11902 
11903 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
11904 		connect.edmg.channels =
11905 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
11906 
11907 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
11908 			connect.edmg.bw_config =
11909 				nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
11910 	}
11911 
11912 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11913 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
11914 		if (IS_ERR(connkeys))
11915 			return PTR_ERR(connkeys);
11916 	}
11917 
11918 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
11919 		connect.flags |= ASSOC_REQ_DISABLE_HT;
11920 
11921 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11922 		memcpy(&connect.ht_capa_mask,
11923 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11924 		       sizeof(connect.ht_capa_mask));
11925 
11926 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11927 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
11928 			kfree_sensitive(connkeys);
11929 			return -EINVAL;
11930 		}
11931 		memcpy(&connect.ht_capa,
11932 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11933 		       sizeof(connect.ht_capa));
11934 	}
11935 
11936 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
11937 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
11938 
11939 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
11940 		connect.flags |= ASSOC_REQ_DISABLE_HE;
11941 
11942 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
11943 		connect.flags |= ASSOC_REQ_DISABLE_EHT;
11944 
11945 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11946 		memcpy(&connect.vht_capa_mask,
11947 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
11948 		       sizeof(connect.vht_capa_mask));
11949 
11950 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
11951 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
11952 			kfree_sensitive(connkeys);
11953 			return -EINVAL;
11954 		}
11955 		memcpy(&connect.vht_capa,
11956 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
11957 		       sizeof(connect.vht_capa));
11958 	}
11959 
11960 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
11961 		if (!((rdev->wiphy.features &
11962 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
11963 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
11964 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11965 					     NL80211_EXT_FEATURE_RRM)) {
11966 			kfree_sensitive(connkeys);
11967 			return -EINVAL;
11968 		}
11969 		connect.flags |= ASSOC_REQ_USE_RRM;
11970 	}
11971 
11972 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
11973 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
11974 		kfree_sensitive(connkeys);
11975 		return -EOPNOTSUPP;
11976 	}
11977 
11978 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
11979 		/* bss selection makes no sense if bssid is set */
11980 		if (connect.bssid) {
11981 			kfree_sensitive(connkeys);
11982 			return -EINVAL;
11983 		}
11984 
11985 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
11986 				       wiphy, &connect.bss_select);
11987 		if (err) {
11988 			kfree_sensitive(connkeys);
11989 			return err;
11990 		}
11991 	}
11992 
11993 	if (wiphy_ext_feature_isset(&rdev->wiphy,
11994 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
11995 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
11996 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
11997 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
11998 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11999 		connect.fils_erp_username =
12000 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12001 		connect.fils_erp_username_len =
12002 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12003 		connect.fils_erp_realm =
12004 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12005 		connect.fils_erp_realm_len =
12006 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12007 		connect.fils_erp_next_seq_num =
12008 			nla_get_u16(
12009 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
12010 		connect.fils_erp_rrk =
12011 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12012 		connect.fils_erp_rrk_len =
12013 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12014 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
12015 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
12016 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
12017 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12018 		kfree_sensitive(connkeys);
12019 		return -EINVAL;
12020 	}
12021 
12022 	if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
12023 		if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
12024 			kfree_sensitive(connkeys);
12025 			GENL_SET_ERR_MSG(info,
12026 					 "external auth requires connection ownership");
12027 			return -EINVAL;
12028 		}
12029 		connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
12030 	}
12031 
12032 	if (nla_get_flag(info->attrs[NL80211_ATTR_MLO_SUPPORT]))
12033 		connect.flags |= CONNECT_REQ_MLO_SUPPORT;
12034 
12035 	wdev_lock(dev->ieee80211_ptr);
12036 
12037 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
12038 			       connect.prev_bssid);
12039 	if (err)
12040 		kfree_sensitive(connkeys);
12041 
12042 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
12043 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
12044 		if (connect.bssid)
12045 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
12046 			       connect.bssid, ETH_ALEN);
12047 		else
12048 			eth_zero_addr(dev->ieee80211_ptr->disconnect_bssid);
12049 	}
12050 
12051 	wdev_unlock(dev->ieee80211_ptr);
12052 
12053 	return err;
12054 }
12055 
12056 static int nl80211_update_connect_params(struct sk_buff *skb,
12057 					 struct genl_info *info)
12058 {
12059 	struct cfg80211_connect_params connect = {};
12060 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12061 	struct net_device *dev = info->user_ptr[1];
12062 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12063 	bool fils_sk_offload;
12064 	u32 auth_type;
12065 	u32 changed = 0;
12066 	int ret;
12067 
12068 	if (!rdev->ops->update_connect_params)
12069 		return -EOPNOTSUPP;
12070 
12071 	if (info->attrs[NL80211_ATTR_IE]) {
12072 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
12073 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
12074 		changed |= UPDATE_ASSOC_IES;
12075 	}
12076 
12077 	fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
12078 						  NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
12079 
12080 	/*
12081 	 * when driver supports fils-sk offload all attributes must be
12082 	 * provided. So the else covers "fils-sk-not-all" and
12083 	 * "no-fils-sk-any".
12084 	 */
12085 	if (fils_sk_offload &&
12086 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
12087 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
12088 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
12089 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12090 		connect.fils_erp_username =
12091 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12092 		connect.fils_erp_username_len =
12093 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12094 		connect.fils_erp_realm =
12095 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12096 		connect.fils_erp_realm_len =
12097 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12098 		connect.fils_erp_next_seq_num =
12099 			nla_get_u16(
12100 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
12101 		connect.fils_erp_rrk =
12102 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12103 		connect.fils_erp_rrk_len =
12104 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12105 		changed |= UPDATE_FILS_ERP_INFO;
12106 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
12107 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
12108 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
12109 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12110 		return -EINVAL;
12111 	}
12112 
12113 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
12114 		auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
12115 		if (!nl80211_valid_auth_type(rdev, auth_type,
12116 					     NL80211_CMD_CONNECT))
12117 			return -EINVAL;
12118 
12119 		if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
12120 		    fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
12121 			return -EINVAL;
12122 
12123 		connect.auth_type = auth_type;
12124 		changed |= UPDATE_AUTH_TYPE;
12125 	}
12126 
12127 	wdev_lock(dev->ieee80211_ptr);
12128 	if (!wdev->connected)
12129 		ret = -ENOLINK;
12130 	else
12131 		ret = rdev_update_connect_params(rdev, dev, &connect, changed);
12132 	wdev_unlock(dev->ieee80211_ptr);
12133 
12134 	return ret;
12135 }
12136 
12137 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
12138 {
12139 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12140 	struct net_device *dev = info->user_ptr[1];
12141 	u16 reason;
12142 	int ret;
12143 
12144 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
12145 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
12146 		return -EPERM;
12147 
12148 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
12149 		reason = WLAN_REASON_DEAUTH_LEAVING;
12150 	else
12151 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
12152 
12153 	if (reason == 0)
12154 		return -EINVAL;
12155 
12156 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12157 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
12158 		return -EOPNOTSUPP;
12159 
12160 	wdev_lock(dev->ieee80211_ptr);
12161 	ret = cfg80211_disconnect(rdev, dev, reason, true);
12162 	wdev_unlock(dev->ieee80211_ptr);
12163 	return ret;
12164 }
12165 
12166 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
12167 {
12168 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12169 	struct net *net;
12170 	int err;
12171 
12172 	if (info->attrs[NL80211_ATTR_PID]) {
12173 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
12174 
12175 		net = get_net_ns_by_pid(pid);
12176 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
12177 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
12178 
12179 		net = get_net_ns_by_fd(fd);
12180 	} else {
12181 		return -EINVAL;
12182 	}
12183 
12184 	if (IS_ERR(net))
12185 		return PTR_ERR(net);
12186 
12187 	err = 0;
12188 
12189 	/* check if anything to do */
12190 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
12191 		err = cfg80211_switch_netns(rdev, net);
12192 
12193 	put_net(net);
12194 	return err;
12195 }
12196 
12197 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
12198 {
12199 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12200 	int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
12201 			struct cfg80211_pmksa *pmksa) = NULL;
12202 	struct net_device *dev = info->user_ptr[1];
12203 	struct cfg80211_pmksa pmksa;
12204 
12205 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
12206 
12207 	if (!info->attrs[NL80211_ATTR_PMKID])
12208 		return -EINVAL;
12209 
12210 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
12211 
12212 	if (info->attrs[NL80211_ATTR_MAC]) {
12213 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
12214 	} else if (info->attrs[NL80211_ATTR_SSID] &&
12215 		   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
12216 		   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
12217 		    info->attrs[NL80211_ATTR_PMK])) {
12218 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
12219 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
12220 		pmksa.cache_id =
12221 			nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
12222 	} else {
12223 		return -EINVAL;
12224 	}
12225 	if (info->attrs[NL80211_ATTR_PMK]) {
12226 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
12227 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
12228 	}
12229 
12230 	if (info->attrs[NL80211_ATTR_PMK_LIFETIME])
12231 		pmksa.pmk_lifetime =
12232 			nla_get_u32(info->attrs[NL80211_ATTR_PMK_LIFETIME]);
12233 
12234 	if (info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD])
12235 		pmksa.pmk_reauth_threshold =
12236 			nla_get_u8(
12237 				info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD]);
12238 
12239 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12240 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
12241 	    !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
12242 	      wiphy_ext_feature_isset(&rdev->wiphy,
12243 				      NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
12244 		return -EOPNOTSUPP;
12245 
12246 	switch (info->genlhdr->cmd) {
12247 	case NL80211_CMD_SET_PMKSA:
12248 		rdev_ops = rdev->ops->set_pmksa;
12249 		break;
12250 	case NL80211_CMD_DEL_PMKSA:
12251 		rdev_ops = rdev->ops->del_pmksa;
12252 		break;
12253 	default:
12254 		WARN_ON(1);
12255 		break;
12256 	}
12257 
12258 	if (!rdev_ops)
12259 		return -EOPNOTSUPP;
12260 
12261 	return rdev_ops(&rdev->wiphy, dev, &pmksa);
12262 }
12263 
12264 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
12265 {
12266 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12267 	struct net_device *dev = info->user_ptr[1];
12268 
12269 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12270 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
12271 		return -EOPNOTSUPP;
12272 
12273 	if (!rdev->ops->flush_pmksa)
12274 		return -EOPNOTSUPP;
12275 
12276 	return rdev_flush_pmksa(rdev, dev);
12277 }
12278 
12279 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
12280 {
12281 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12282 	struct net_device *dev = info->user_ptr[1];
12283 	u8 action_code, dialog_token;
12284 	u32 peer_capability = 0;
12285 	u16 status_code;
12286 	u8 *peer;
12287 	int link_id;
12288 	bool initiator;
12289 
12290 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12291 	    !rdev->ops->tdls_mgmt)
12292 		return -EOPNOTSUPP;
12293 
12294 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
12295 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
12296 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
12297 	    !info->attrs[NL80211_ATTR_IE] ||
12298 	    !info->attrs[NL80211_ATTR_MAC])
12299 		return -EINVAL;
12300 
12301 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12302 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
12303 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
12304 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
12305 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
12306 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
12307 		peer_capability =
12308 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
12309 	link_id = nl80211_link_id_or_invalid(info->attrs);
12310 
12311 	return rdev_tdls_mgmt(rdev, dev, peer, link_id, action_code,
12312 			      dialog_token, status_code, peer_capability,
12313 			      initiator,
12314 			      nla_data(info->attrs[NL80211_ATTR_IE]),
12315 			      nla_len(info->attrs[NL80211_ATTR_IE]));
12316 }
12317 
12318 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
12319 {
12320 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12321 	struct net_device *dev = info->user_ptr[1];
12322 	enum nl80211_tdls_operation operation;
12323 	u8 *peer;
12324 
12325 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12326 	    !rdev->ops->tdls_oper)
12327 		return -EOPNOTSUPP;
12328 
12329 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
12330 	    !info->attrs[NL80211_ATTR_MAC])
12331 		return -EINVAL;
12332 
12333 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
12334 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12335 
12336 	return rdev_tdls_oper(rdev, dev, peer, operation);
12337 }
12338 
12339 static int nl80211_remain_on_channel(struct sk_buff *skb,
12340 				     struct genl_info *info)
12341 {
12342 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12343 	unsigned int link_id = nl80211_link_id(info->attrs);
12344 	struct wireless_dev *wdev = info->user_ptr[1];
12345 	struct cfg80211_chan_def chandef;
12346 	struct sk_buff *msg;
12347 	void *hdr;
12348 	u64 cookie;
12349 	u32 duration;
12350 	int err;
12351 
12352 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
12353 	    !info->attrs[NL80211_ATTR_DURATION])
12354 		return -EINVAL;
12355 
12356 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12357 
12358 	if (!rdev->ops->remain_on_channel ||
12359 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
12360 		return -EOPNOTSUPP;
12361 
12362 	/*
12363 	 * We should be on that channel for at least a minimum amount of
12364 	 * time (10ms) but no longer than the driver supports.
12365 	 */
12366 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12367 	    duration > rdev->wiphy.max_remain_on_channel_duration)
12368 		return -EINVAL;
12369 
12370 	err = nl80211_parse_chandef(rdev, info, &chandef);
12371 	if (err)
12372 		return err;
12373 
12374 	wdev_lock(wdev);
12375 	if (!cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12376 		const struct cfg80211_chan_def *oper_chandef, *compat_chandef;
12377 
12378 		oper_chandef = wdev_chandef(wdev, link_id);
12379 
12380 		if (WARN_ON(!oper_chandef)) {
12381 			/* cannot happen since we must beacon to get here */
12382 			WARN_ON(1);
12383 			wdev_unlock(wdev);
12384 			return -EBUSY;
12385 		}
12386 
12387 		/* note: returns first one if identical chandefs */
12388 		compat_chandef = cfg80211_chandef_compatible(&chandef,
12389 							     oper_chandef);
12390 
12391 		if (compat_chandef != &chandef) {
12392 			wdev_unlock(wdev);
12393 			return -EBUSY;
12394 		}
12395 	}
12396 	wdev_unlock(wdev);
12397 
12398 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12399 	if (!msg)
12400 		return -ENOMEM;
12401 
12402 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12403 			     NL80211_CMD_REMAIN_ON_CHANNEL);
12404 	if (!hdr) {
12405 		err = -ENOBUFS;
12406 		goto free_msg;
12407 	}
12408 
12409 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
12410 				     duration, &cookie);
12411 
12412 	if (err)
12413 		goto free_msg;
12414 
12415 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12416 			      NL80211_ATTR_PAD))
12417 		goto nla_put_failure;
12418 
12419 	genlmsg_end(msg, hdr);
12420 
12421 	return genlmsg_reply(msg, info);
12422 
12423  nla_put_failure:
12424 	err = -ENOBUFS;
12425  free_msg:
12426 	nlmsg_free(msg);
12427 	return err;
12428 }
12429 
12430 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
12431 					    struct genl_info *info)
12432 {
12433 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12434 	struct wireless_dev *wdev = info->user_ptr[1];
12435 	u64 cookie;
12436 
12437 	if (!info->attrs[NL80211_ATTR_COOKIE])
12438 		return -EINVAL;
12439 
12440 	if (!rdev->ops->cancel_remain_on_channel)
12441 		return -EOPNOTSUPP;
12442 
12443 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12444 
12445 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
12446 }
12447 
12448 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
12449 				       struct genl_info *info)
12450 {
12451 	struct cfg80211_bitrate_mask mask;
12452 	unsigned int link_id = nl80211_link_id(info->attrs);
12453 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12454 	struct net_device *dev = info->user_ptr[1];
12455 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12456 	int err;
12457 
12458 	if (!rdev->ops->set_bitrate_mask)
12459 		return -EOPNOTSUPP;
12460 
12461 	wdev_lock(wdev);
12462 	err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
12463 					    NL80211_ATTR_TX_RATES, &mask,
12464 					    dev, true, link_id);
12465 	if (err)
12466 		goto out;
12467 
12468 	err = rdev_set_bitrate_mask(rdev, dev, link_id, NULL, &mask);
12469 out:
12470 	wdev_unlock(wdev);
12471 	return err;
12472 }
12473 
12474 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
12475 {
12476 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12477 	struct wireless_dev *wdev = info->user_ptr[1];
12478 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
12479 
12480 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
12481 		return -EINVAL;
12482 
12483 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
12484 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
12485 
12486 	switch (wdev->iftype) {
12487 	case NL80211_IFTYPE_STATION:
12488 	case NL80211_IFTYPE_ADHOC:
12489 	case NL80211_IFTYPE_P2P_CLIENT:
12490 	case NL80211_IFTYPE_AP:
12491 	case NL80211_IFTYPE_AP_VLAN:
12492 	case NL80211_IFTYPE_MESH_POINT:
12493 	case NL80211_IFTYPE_P2P_GO:
12494 	case NL80211_IFTYPE_P2P_DEVICE:
12495 		break;
12496 	case NL80211_IFTYPE_NAN:
12497 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12498 					     NL80211_EXT_FEATURE_SECURE_NAN))
12499 			return -EOPNOTSUPP;
12500 		break;
12501 	default:
12502 		return -EOPNOTSUPP;
12503 	}
12504 
12505 	/* not much point in registering if we can't reply */
12506 	if (!rdev->ops->mgmt_tx)
12507 		return -EOPNOTSUPP;
12508 
12509 	if (info->attrs[NL80211_ATTR_RECEIVE_MULTICAST] &&
12510 	    !wiphy_ext_feature_isset(&rdev->wiphy,
12511 				     NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS)) {
12512 		GENL_SET_ERR_MSG(info,
12513 				 "multicast RX registrations are not supported");
12514 		return -EOPNOTSUPP;
12515 	}
12516 
12517 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
12518 					   nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12519 					   nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12520 					   info->attrs[NL80211_ATTR_RECEIVE_MULTICAST],
12521 					   info->extack);
12522 }
12523 
12524 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
12525 {
12526 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12527 	struct wireless_dev *wdev = info->user_ptr[1];
12528 	struct cfg80211_chan_def chandef;
12529 	int err;
12530 	void *hdr = NULL;
12531 	u64 cookie;
12532 	struct sk_buff *msg = NULL;
12533 	struct cfg80211_mgmt_tx_params params = {
12534 		.dont_wait_for_ack =
12535 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
12536 	};
12537 
12538 	if (!info->attrs[NL80211_ATTR_FRAME])
12539 		return -EINVAL;
12540 
12541 	if (!rdev->ops->mgmt_tx)
12542 		return -EOPNOTSUPP;
12543 
12544 	switch (wdev->iftype) {
12545 	case NL80211_IFTYPE_P2P_DEVICE:
12546 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
12547 			return -EINVAL;
12548 		break;
12549 	case NL80211_IFTYPE_STATION:
12550 	case NL80211_IFTYPE_ADHOC:
12551 	case NL80211_IFTYPE_P2P_CLIENT:
12552 	case NL80211_IFTYPE_AP:
12553 	case NL80211_IFTYPE_AP_VLAN:
12554 	case NL80211_IFTYPE_MESH_POINT:
12555 	case NL80211_IFTYPE_P2P_GO:
12556 		break;
12557 	case NL80211_IFTYPE_NAN:
12558 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12559 					     NL80211_EXT_FEATURE_SECURE_NAN))
12560 			return -EOPNOTSUPP;
12561 		break;
12562 	default:
12563 		return -EOPNOTSUPP;
12564 	}
12565 
12566 	if (info->attrs[NL80211_ATTR_DURATION]) {
12567 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12568 			return -EINVAL;
12569 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12570 
12571 		/*
12572 		 * We should wait on the channel for at least a minimum amount
12573 		 * of time (10ms) but no longer than the driver supports.
12574 		 */
12575 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12576 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
12577 			return -EINVAL;
12578 	}
12579 
12580 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
12581 
12582 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12583 		return -EINVAL;
12584 
12585 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
12586 
12587 	/* get the channel if any has been specified, otherwise pass NULL to
12588 	 * the driver. The latter will use the current one
12589 	 */
12590 	chandef.chan = NULL;
12591 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
12592 		err = nl80211_parse_chandef(rdev, info, &chandef);
12593 		if (err)
12594 			return err;
12595 	}
12596 
12597 	if (!chandef.chan && params.offchan)
12598 		return -EINVAL;
12599 
12600 	wdev_lock(wdev);
12601 	if (params.offchan &&
12602 	    !cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12603 		wdev_unlock(wdev);
12604 		return -EBUSY;
12605 	}
12606 
12607 	params.link_id = nl80211_link_id_or_invalid(info->attrs);
12608 	/*
12609 	 * This now races due to the unlock, but we cannot check
12610 	 * the valid links for the _station_ anyway, so that's up
12611 	 * to the driver.
12612 	 */
12613 	if (params.link_id >= 0 &&
12614 	    !(wdev->valid_links & BIT(params.link_id))) {
12615 		wdev_unlock(wdev);
12616 		return -EINVAL;
12617 	}
12618 	wdev_unlock(wdev);
12619 
12620 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
12621 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
12622 
12623 	if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
12624 		int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12625 		int i;
12626 
12627 		if (len % sizeof(u16))
12628 			return -EINVAL;
12629 
12630 		params.n_csa_offsets = len / sizeof(u16);
12631 		params.csa_offsets =
12632 			nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12633 
12634 		/* check that all the offsets fit the frame */
12635 		for (i = 0; i < params.n_csa_offsets; i++) {
12636 			if (params.csa_offsets[i] >= params.len)
12637 				return -EINVAL;
12638 		}
12639 	}
12640 
12641 	if (!params.dont_wait_for_ack) {
12642 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12643 		if (!msg)
12644 			return -ENOMEM;
12645 
12646 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12647 				     NL80211_CMD_FRAME);
12648 		if (!hdr) {
12649 			err = -ENOBUFS;
12650 			goto free_msg;
12651 		}
12652 	}
12653 
12654 	params.chan = chandef.chan;
12655 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
12656 	if (err)
12657 		goto free_msg;
12658 
12659 	if (msg) {
12660 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12661 				      NL80211_ATTR_PAD))
12662 			goto nla_put_failure;
12663 
12664 		genlmsg_end(msg, hdr);
12665 		return genlmsg_reply(msg, info);
12666 	}
12667 
12668 	return 0;
12669 
12670  nla_put_failure:
12671 	err = -ENOBUFS;
12672  free_msg:
12673 	nlmsg_free(msg);
12674 	return err;
12675 }
12676 
12677 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
12678 {
12679 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12680 	struct wireless_dev *wdev = info->user_ptr[1];
12681 	u64 cookie;
12682 
12683 	if (!info->attrs[NL80211_ATTR_COOKIE])
12684 		return -EINVAL;
12685 
12686 	if (!rdev->ops->mgmt_tx_cancel_wait)
12687 		return -EOPNOTSUPP;
12688 
12689 	switch (wdev->iftype) {
12690 	case NL80211_IFTYPE_STATION:
12691 	case NL80211_IFTYPE_ADHOC:
12692 	case NL80211_IFTYPE_P2P_CLIENT:
12693 	case NL80211_IFTYPE_AP:
12694 	case NL80211_IFTYPE_AP_VLAN:
12695 	case NL80211_IFTYPE_P2P_GO:
12696 	case NL80211_IFTYPE_P2P_DEVICE:
12697 		break;
12698 	case NL80211_IFTYPE_NAN:
12699 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12700 					     NL80211_EXT_FEATURE_SECURE_NAN))
12701 			return -EOPNOTSUPP;
12702 		break;
12703 	default:
12704 		return -EOPNOTSUPP;
12705 	}
12706 
12707 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12708 
12709 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
12710 }
12711 
12712 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
12713 {
12714 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12715 	struct wireless_dev *wdev;
12716 	struct net_device *dev = info->user_ptr[1];
12717 	u8 ps_state;
12718 	bool state;
12719 	int err;
12720 
12721 	if (!info->attrs[NL80211_ATTR_PS_STATE])
12722 		return -EINVAL;
12723 
12724 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
12725 
12726 	wdev = dev->ieee80211_ptr;
12727 
12728 	if (!rdev->ops->set_power_mgmt)
12729 		return -EOPNOTSUPP;
12730 
12731 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
12732 
12733 	if (state == wdev->ps)
12734 		return 0;
12735 
12736 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
12737 	if (!err)
12738 		wdev->ps = state;
12739 	return err;
12740 }
12741 
12742 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
12743 {
12744 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12745 	enum nl80211_ps_state ps_state;
12746 	struct wireless_dev *wdev;
12747 	struct net_device *dev = info->user_ptr[1];
12748 	struct sk_buff *msg;
12749 	void *hdr;
12750 	int err;
12751 
12752 	wdev = dev->ieee80211_ptr;
12753 
12754 	if (!rdev->ops->set_power_mgmt)
12755 		return -EOPNOTSUPP;
12756 
12757 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12758 	if (!msg)
12759 		return -ENOMEM;
12760 
12761 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12762 			     NL80211_CMD_GET_POWER_SAVE);
12763 	if (!hdr) {
12764 		err = -ENOBUFS;
12765 		goto free_msg;
12766 	}
12767 
12768 	if (wdev->ps)
12769 		ps_state = NL80211_PS_ENABLED;
12770 	else
12771 		ps_state = NL80211_PS_DISABLED;
12772 
12773 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
12774 		goto nla_put_failure;
12775 
12776 	genlmsg_end(msg, hdr);
12777 	return genlmsg_reply(msg, info);
12778 
12779  nla_put_failure:
12780 	err = -ENOBUFS;
12781  free_msg:
12782 	nlmsg_free(msg);
12783 	return err;
12784 }
12785 
12786 static const struct nla_policy
12787 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
12788 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
12789 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
12790 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
12791 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
12792 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
12793 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
12794 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
12795 };
12796 
12797 static int nl80211_set_cqm_txe(struct genl_info *info,
12798 			       u32 rate, u32 pkts, u32 intvl)
12799 {
12800 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12801 	struct net_device *dev = info->user_ptr[1];
12802 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12803 
12804 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
12805 		return -EINVAL;
12806 
12807 	if (!rdev->ops->set_cqm_txe_config)
12808 		return -EOPNOTSUPP;
12809 
12810 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12811 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12812 		return -EOPNOTSUPP;
12813 
12814 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
12815 }
12816 
12817 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
12818 				    struct net_device *dev,
12819 				    struct cfg80211_cqm_config *cqm_config)
12820 {
12821 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12822 	s32 last, low, high;
12823 	u32 hyst;
12824 	int i, n, low_index;
12825 	int err;
12826 
12827 	/*
12828 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
12829 	 * event has been received yet, we should receive an event after a
12830 	 * connection is established and enough beacons received to calculate
12831 	 * the average.
12832 	 */
12833 	if (!cqm_config->last_rssi_event_value &&
12834 	    wdev->links[0].client.current_bss &&
12835 	    rdev->ops->get_station) {
12836 		struct station_info sinfo = {};
12837 		u8 *mac_addr;
12838 
12839 		mac_addr = wdev->links[0].client.current_bss->pub.bssid;
12840 
12841 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
12842 		if (err)
12843 			return err;
12844 
12845 		cfg80211_sinfo_release_content(&sinfo);
12846 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
12847 			cqm_config->last_rssi_event_value =
12848 				(s8) sinfo.rx_beacon_signal_avg;
12849 	}
12850 
12851 	last = cqm_config->last_rssi_event_value;
12852 	hyst = cqm_config->rssi_hyst;
12853 	n = cqm_config->n_rssi_thresholds;
12854 
12855 	for (i = 0; i < n; i++) {
12856 		i = array_index_nospec(i, n);
12857 		if (last < cqm_config->rssi_thresholds[i])
12858 			break;
12859 	}
12860 
12861 	low_index = i - 1;
12862 	if (low_index >= 0) {
12863 		low_index = array_index_nospec(low_index, n);
12864 		low = cqm_config->rssi_thresholds[low_index] - hyst;
12865 	} else {
12866 		low = S32_MIN;
12867 	}
12868 	if (i < n) {
12869 		i = array_index_nospec(i, n);
12870 		high = cqm_config->rssi_thresholds[i] + hyst - 1;
12871 	} else {
12872 		high = S32_MAX;
12873 	}
12874 
12875 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
12876 }
12877 
12878 static int nl80211_set_cqm_rssi(struct genl_info *info,
12879 				const s32 *thresholds, int n_thresholds,
12880 				u32 hysteresis)
12881 {
12882 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12883 	struct cfg80211_cqm_config *cqm_config = NULL, *old;
12884 	struct net_device *dev = info->user_ptr[1];
12885 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12886 	int i, err;
12887 	s32 prev = S32_MIN;
12888 
12889 	/* Check all values negative and sorted */
12890 	for (i = 0; i < n_thresholds; i++) {
12891 		if (thresholds[i] > 0 || thresholds[i] <= prev)
12892 			return -EINVAL;
12893 
12894 		prev = thresholds[i];
12895 	}
12896 
12897 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12898 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12899 		return -EOPNOTSUPP;
12900 
12901 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
12902 		n_thresholds = 0;
12903 
12904 	wdev_lock(wdev);
12905 	old = rcu_dereference_protected(wdev->cqm_config,
12906 					lockdep_is_held(&wdev->mtx));
12907 
12908 	/* if already disabled just succeed */
12909 	if (!n_thresholds && !old) {
12910 		err = 0;
12911 		goto unlock;
12912 	}
12913 
12914 	if (n_thresholds > 1) {
12915 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
12916 					     NL80211_EXT_FEATURE_CQM_RSSI_LIST) ||
12917 		    !rdev->ops->set_cqm_rssi_range_config) {
12918 			err = -EOPNOTSUPP;
12919 			goto unlock;
12920 		}
12921 	} else {
12922 		if (!rdev->ops->set_cqm_rssi_config) {
12923 			err = -EOPNOTSUPP;
12924 			goto unlock;
12925 		}
12926 	}
12927 
12928 	if (n_thresholds) {
12929 		cqm_config = kzalloc(struct_size(cqm_config, rssi_thresholds,
12930 						 n_thresholds),
12931 				     GFP_KERNEL);
12932 		if (!cqm_config) {
12933 			err = -ENOMEM;
12934 			goto unlock;
12935 		}
12936 
12937 		cqm_config->rssi_hyst = hysteresis;
12938 		cqm_config->n_rssi_thresholds = n_thresholds;
12939 		memcpy(cqm_config->rssi_thresholds, thresholds,
12940 		       flex_array_size(cqm_config, rssi_thresholds,
12941 				       n_thresholds));
12942 		cqm_config->use_range_api = n_thresholds > 1 ||
12943 					    !rdev->ops->set_cqm_rssi_config;
12944 
12945 		rcu_assign_pointer(wdev->cqm_config, cqm_config);
12946 
12947 		if (cqm_config->use_range_api)
12948 			err = cfg80211_cqm_rssi_update(rdev, dev, cqm_config);
12949 		else
12950 			err = rdev_set_cqm_rssi_config(rdev, dev,
12951 						       thresholds[0],
12952 						       hysteresis);
12953 	} else {
12954 		RCU_INIT_POINTER(wdev->cqm_config, NULL);
12955 		/* if enabled as range also disable via range */
12956 		if (old->use_range_api)
12957 			err = rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
12958 		else
12959 			err = rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
12960 	}
12961 
12962 	if (err) {
12963 		rcu_assign_pointer(wdev->cqm_config, old);
12964 		kfree_rcu(cqm_config, rcu_head);
12965 	} else {
12966 		kfree_rcu(old, rcu_head);
12967 	}
12968 unlock:
12969 	wdev_unlock(wdev);
12970 
12971 	return err;
12972 }
12973 
12974 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
12975 {
12976 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
12977 	struct nlattr *cqm;
12978 	int err;
12979 
12980 	cqm = info->attrs[NL80211_ATTR_CQM];
12981 	if (!cqm)
12982 		return -EINVAL;
12983 
12984 	err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
12985 					  nl80211_attr_cqm_policy,
12986 					  info->extack);
12987 	if (err)
12988 		return err;
12989 
12990 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
12991 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
12992 		const s32 *thresholds =
12993 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
12994 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
12995 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
12996 
12997 		if (len % 4)
12998 			return -EINVAL;
12999 
13000 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
13001 					    hysteresis);
13002 	}
13003 
13004 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
13005 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
13006 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
13007 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
13008 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
13009 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
13010 
13011 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
13012 	}
13013 
13014 	return -EINVAL;
13015 }
13016 
13017 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
13018 {
13019 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13020 	struct net_device *dev = info->user_ptr[1];
13021 	struct ocb_setup setup = {};
13022 	int err;
13023 
13024 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
13025 	if (err)
13026 		return err;
13027 
13028 	return cfg80211_join_ocb(rdev, dev, &setup);
13029 }
13030 
13031 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
13032 {
13033 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13034 	struct net_device *dev = info->user_ptr[1];
13035 
13036 	return cfg80211_leave_ocb(rdev, dev);
13037 }
13038 
13039 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
13040 {
13041 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13042 	struct net_device *dev = info->user_ptr[1];
13043 	struct mesh_config cfg;
13044 	struct mesh_setup setup;
13045 	int err;
13046 
13047 	/* start with default */
13048 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
13049 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
13050 
13051 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
13052 		/* and parse parameters if given */
13053 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
13054 		if (err)
13055 			return err;
13056 	}
13057 
13058 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
13059 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
13060 		return -EINVAL;
13061 
13062 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
13063 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
13064 
13065 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
13066 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
13067 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
13068 			return -EINVAL;
13069 
13070 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
13071 		setup.beacon_interval =
13072 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
13073 
13074 		err = cfg80211_validate_beacon_int(rdev,
13075 						   NL80211_IFTYPE_MESH_POINT,
13076 						   setup.beacon_interval);
13077 		if (err)
13078 			return err;
13079 	}
13080 
13081 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
13082 		setup.dtim_period =
13083 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
13084 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
13085 			return -EINVAL;
13086 	}
13087 
13088 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
13089 		/* parse additional setup parameters if given */
13090 		err = nl80211_parse_mesh_setup(info, &setup);
13091 		if (err)
13092 			return err;
13093 	}
13094 
13095 	if (setup.user_mpm)
13096 		cfg.auto_open_plinks = false;
13097 
13098 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
13099 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
13100 		if (err)
13101 			return err;
13102 	} else {
13103 		/* __cfg80211_join_mesh() will sort it out */
13104 		setup.chandef.chan = NULL;
13105 	}
13106 
13107 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
13108 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
13109 		int n_rates =
13110 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
13111 		struct ieee80211_supported_band *sband;
13112 
13113 		if (!setup.chandef.chan)
13114 			return -EINVAL;
13115 
13116 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
13117 
13118 		err = ieee80211_get_ratemask(sband, rates, n_rates,
13119 					     &setup.basic_rates);
13120 		if (err)
13121 			return err;
13122 	}
13123 
13124 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
13125 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
13126 						    NL80211_ATTR_TX_RATES,
13127 						    &setup.beacon_rate,
13128 						    dev, false, 0);
13129 		if (err)
13130 			return err;
13131 
13132 		if (!setup.chandef.chan)
13133 			return -EINVAL;
13134 
13135 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
13136 					      &setup.beacon_rate);
13137 		if (err)
13138 			return err;
13139 	}
13140 
13141 	setup.userspace_handles_dfs =
13142 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
13143 
13144 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
13145 		int r = validate_pae_over_nl80211(rdev, info);
13146 
13147 		if (r < 0)
13148 			return r;
13149 
13150 		setup.control_port_over_nl80211 = true;
13151 	}
13152 
13153 	wdev_lock(dev->ieee80211_ptr);
13154 	err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
13155 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
13156 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
13157 	wdev_unlock(dev->ieee80211_ptr);
13158 
13159 	return err;
13160 }
13161 
13162 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
13163 {
13164 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13165 	struct net_device *dev = info->user_ptr[1];
13166 
13167 	return cfg80211_leave_mesh(rdev, dev);
13168 }
13169 
13170 #ifdef CONFIG_PM
13171 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
13172 					struct cfg80211_registered_device *rdev)
13173 {
13174 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
13175 	struct nlattr *nl_pats, *nl_pat;
13176 	int i, pat_len;
13177 
13178 	if (!wowlan->n_patterns)
13179 		return 0;
13180 
13181 	nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
13182 	if (!nl_pats)
13183 		return -ENOBUFS;
13184 
13185 	for (i = 0; i < wowlan->n_patterns; i++) {
13186 		nl_pat = nla_nest_start_noflag(msg, i + 1);
13187 		if (!nl_pat)
13188 			return -ENOBUFS;
13189 		pat_len = wowlan->patterns[i].pattern_len;
13190 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
13191 			    wowlan->patterns[i].mask) ||
13192 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
13193 			    wowlan->patterns[i].pattern) ||
13194 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
13195 				wowlan->patterns[i].pkt_offset))
13196 			return -ENOBUFS;
13197 		nla_nest_end(msg, nl_pat);
13198 	}
13199 	nla_nest_end(msg, nl_pats);
13200 
13201 	return 0;
13202 }
13203 
13204 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
13205 				   struct cfg80211_wowlan_tcp *tcp)
13206 {
13207 	struct nlattr *nl_tcp;
13208 
13209 	if (!tcp)
13210 		return 0;
13211 
13212 	nl_tcp = nla_nest_start_noflag(msg,
13213 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
13214 	if (!nl_tcp)
13215 		return -ENOBUFS;
13216 
13217 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
13218 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
13219 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
13220 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
13221 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
13222 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
13223 		    tcp->payload_len, tcp->payload) ||
13224 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
13225 			tcp->data_interval) ||
13226 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
13227 		    tcp->wake_len, tcp->wake_data) ||
13228 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
13229 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
13230 		return -ENOBUFS;
13231 
13232 	if (tcp->payload_seq.len &&
13233 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
13234 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
13235 		return -ENOBUFS;
13236 
13237 	if (tcp->payload_tok.len &&
13238 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
13239 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
13240 		    &tcp->payload_tok))
13241 		return -ENOBUFS;
13242 
13243 	nla_nest_end(msg, nl_tcp);
13244 
13245 	return 0;
13246 }
13247 
13248 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
13249 				  struct cfg80211_sched_scan_request *req)
13250 {
13251 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
13252 	int i;
13253 
13254 	if (!req)
13255 		return 0;
13256 
13257 	nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
13258 	if (!nd)
13259 		return -ENOBUFS;
13260 
13261 	if (req->n_scan_plans == 1 &&
13262 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
13263 			req->scan_plans[0].interval * 1000))
13264 		return -ENOBUFS;
13265 
13266 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
13267 		return -ENOBUFS;
13268 
13269 	if (req->relative_rssi_set) {
13270 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
13271 
13272 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
13273 			       req->relative_rssi))
13274 			return -ENOBUFS;
13275 
13276 		rssi_adjust.band = req->rssi_adjust.band;
13277 		rssi_adjust.delta = req->rssi_adjust.delta;
13278 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
13279 			    sizeof(rssi_adjust), &rssi_adjust))
13280 			return -ENOBUFS;
13281 	}
13282 
13283 	freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
13284 	if (!freqs)
13285 		return -ENOBUFS;
13286 
13287 	for (i = 0; i < req->n_channels; i++) {
13288 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
13289 			return -ENOBUFS;
13290 	}
13291 
13292 	nla_nest_end(msg, freqs);
13293 
13294 	if (req->n_match_sets) {
13295 		matches = nla_nest_start_noflag(msg,
13296 						NL80211_ATTR_SCHED_SCAN_MATCH);
13297 		if (!matches)
13298 			return -ENOBUFS;
13299 
13300 		for (i = 0; i < req->n_match_sets; i++) {
13301 			match = nla_nest_start_noflag(msg, i);
13302 			if (!match)
13303 				return -ENOBUFS;
13304 
13305 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
13306 				    req->match_sets[i].ssid.ssid_len,
13307 				    req->match_sets[i].ssid.ssid))
13308 				return -ENOBUFS;
13309 			nla_nest_end(msg, match);
13310 		}
13311 		nla_nest_end(msg, matches);
13312 	}
13313 
13314 	scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
13315 	if (!scan_plans)
13316 		return -ENOBUFS;
13317 
13318 	for (i = 0; i < req->n_scan_plans; i++) {
13319 		scan_plan = nla_nest_start_noflag(msg, i + 1);
13320 		if (!scan_plan)
13321 			return -ENOBUFS;
13322 
13323 		if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
13324 				req->scan_plans[i].interval) ||
13325 		    (req->scan_plans[i].iterations &&
13326 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
13327 				 req->scan_plans[i].iterations)))
13328 			return -ENOBUFS;
13329 		nla_nest_end(msg, scan_plan);
13330 	}
13331 	nla_nest_end(msg, scan_plans);
13332 
13333 	nla_nest_end(msg, nd);
13334 
13335 	return 0;
13336 }
13337 
13338 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
13339 {
13340 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13341 	struct sk_buff *msg;
13342 	void *hdr;
13343 	u32 size = NLMSG_DEFAULT_SIZE;
13344 
13345 	if (!rdev->wiphy.wowlan)
13346 		return -EOPNOTSUPP;
13347 
13348 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
13349 		/* adjust size to have room for all the data */
13350 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
13351 			rdev->wiphy.wowlan_config->tcp->payload_len +
13352 			rdev->wiphy.wowlan_config->tcp->wake_len +
13353 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
13354 	}
13355 
13356 	msg = nlmsg_new(size, GFP_KERNEL);
13357 	if (!msg)
13358 		return -ENOMEM;
13359 
13360 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13361 			     NL80211_CMD_GET_WOWLAN);
13362 	if (!hdr)
13363 		goto nla_put_failure;
13364 
13365 	if (rdev->wiphy.wowlan_config) {
13366 		struct nlattr *nl_wowlan;
13367 
13368 		nl_wowlan = nla_nest_start_noflag(msg,
13369 						  NL80211_ATTR_WOWLAN_TRIGGERS);
13370 		if (!nl_wowlan)
13371 			goto nla_put_failure;
13372 
13373 		if ((rdev->wiphy.wowlan_config->any &&
13374 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
13375 		    (rdev->wiphy.wowlan_config->disconnect &&
13376 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
13377 		    (rdev->wiphy.wowlan_config->magic_pkt &&
13378 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
13379 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
13380 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
13381 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
13382 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
13383 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
13384 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
13385 		    (rdev->wiphy.wowlan_config->rfkill_release &&
13386 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
13387 			goto nla_put_failure;
13388 
13389 		if (nl80211_send_wowlan_patterns(msg, rdev))
13390 			goto nla_put_failure;
13391 
13392 		if (nl80211_send_wowlan_tcp(msg,
13393 					    rdev->wiphy.wowlan_config->tcp))
13394 			goto nla_put_failure;
13395 
13396 		if (nl80211_send_wowlan_nd(
13397 			    msg,
13398 			    rdev->wiphy.wowlan_config->nd_config))
13399 			goto nla_put_failure;
13400 
13401 		nla_nest_end(msg, nl_wowlan);
13402 	}
13403 
13404 	genlmsg_end(msg, hdr);
13405 	return genlmsg_reply(msg, info);
13406 
13407 nla_put_failure:
13408 	nlmsg_free(msg);
13409 	return -ENOBUFS;
13410 }
13411 
13412 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
13413 				    struct nlattr *attr,
13414 				    struct cfg80211_wowlan *trig)
13415 {
13416 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
13417 	struct cfg80211_wowlan_tcp *cfg;
13418 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
13419 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
13420 	u32 size;
13421 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
13422 	int err, port;
13423 
13424 	if (!rdev->wiphy.wowlan->tcp)
13425 		return -EINVAL;
13426 
13427 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
13428 					  nl80211_wowlan_tcp_policy, NULL);
13429 	if (err)
13430 		return err;
13431 
13432 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
13433 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
13434 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
13435 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
13436 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
13437 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
13438 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
13439 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
13440 		return -EINVAL;
13441 
13442 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
13443 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
13444 		return -EINVAL;
13445 
13446 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
13447 			rdev->wiphy.wowlan->tcp->data_interval_max ||
13448 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
13449 		return -EINVAL;
13450 
13451 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
13452 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
13453 		return -EINVAL;
13454 
13455 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
13456 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
13457 		return -EINVAL;
13458 
13459 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
13460 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13461 
13462 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13463 		tokens_size = tokln - sizeof(*tok);
13464 
13465 		if (!tok->len || tokens_size % tok->len)
13466 			return -EINVAL;
13467 		if (!rdev->wiphy.wowlan->tcp->tok)
13468 			return -EINVAL;
13469 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
13470 			return -EINVAL;
13471 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
13472 			return -EINVAL;
13473 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
13474 			return -EINVAL;
13475 		if (tok->offset + tok->len > data_size)
13476 			return -EINVAL;
13477 	}
13478 
13479 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
13480 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
13481 		if (!rdev->wiphy.wowlan->tcp->seq)
13482 			return -EINVAL;
13483 		if (seq->len == 0 || seq->len > 4)
13484 			return -EINVAL;
13485 		if (seq->len + seq->offset > data_size)
13486 			return -EINVAL;
13487 	}
13488 
13489 	size = sizeof(*cfg);
13490 	size += data_size;
13491 	size += wake_size + wake_mask_size;
13492 	size += tokens_size;
13493 
13494 	cfg = kzalloc(size, GFP_KERNEL);
13495 	if (!cfg)
13496 		return -ENOMEM;
13497 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
13498 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
13499 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
13500 	       ETH_ALEN);
13501 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
13502 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
13503 	else
13504 		port = 0;
13505 #ifdef CONFIG_INET
13506 	/* allocate a socket and port for it and use it */
13507 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
13508 			    IPPROTO_TCP, &cfg->sock, 1);
13509 	if (err) {
13510 		kfree(cfg);
13511 		return err;
13512 	}
13513 	if (inet_csk_get_port(cfg->sock->sk, port)) {
13514 		sock_release(cfg->sock);
13515 		kfree(cfg);
13516 		return -EADDRINUSE;
13517 	}
13518 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
13519 #else
13520 	if (!port) {
13521 		kfree(cfg);
13522 		return -EINVAL;
13523 	}
13524 	cfg->src_port = port;
13525 #endif
13526 
13527 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
13528 	cfg->payload_len = data_size;
13529 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
13530 	memcpy((void *)cfg->payload,
13531 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
13532 	       data_size);
13533 	if (seq)
13534 		cfg->payload_seq = *seq;
13535 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
13536 	cfg->wake_len = wake_size;
13537 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
13538 	memcpy((void *)cfg->wake_data,
13539 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
13540 	       wake_size);
13541 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
13542 			 data_size + wake_size;
13543 	memcpy((void *)cfg->wake_mask,
13544 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
13545 	       wake_mask_size);
13546 	if (tok) {
13547 		cfg->tokens_size = tokens_size;
13548 		cfg->payload_tok = *tok;
13549 		memcpy(cfg->payload_tok.token_stream, tok->token_stream,
13550 		       tokens_size);
13551 	}
13552 
13553 	trig->tcp = cfg;
13554 
13555 	return 0;
13556 }
13557 
13558 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
13559 				   const struct wiphy_wowlan_support *wowlan,
13560 				   struct nlattr *attr,
13561 				   struct cfg80211_wowlan *trig)
13562 {
13563 	struct nlattr **tb;
13564 	int err;
13565 
13566 	tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
13567 	if (!tb)
13568 		return -ENOMEM;
13569 
13570 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
13571 		err = -EOPNOTSUPP;
13572 		goto out;
13573 	}
13574 
13575 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
13576 					  nl80211_policy, NULL);
13577 	if (err)
13578 		goto out;
13579 
13580 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
13581 						   wowlan->max_nd_match_sets);
13582 	err = PTR_ERR_OR_ZERO(trig->nd_config);
13583 	if (err)
13584 		trig->nd_config = NULL;
13585 
13586 out:
13587 	kfree(tb);
13588 	return err;
13589 }
13590 
13591 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
13592 {
13593 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13594 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
13595 	struct cfg80211_wowlan new_triggers = {};
13596 	struct cfg80211_wowlan *ntrig;
13597 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
13598 	int err, i;
13599 	bool prev_enabled = rdev->wiphy.wowlan_config;
13600 	bool regular = false;
13601 
13602 	if (!wowlan)
13603 		return -EOPNOTSUPP;
13604 
13605 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
13606 		cfg80211_rdev_free_wowlan(rdev);
13607 		rdev->wiphy.wowlan_config = NULL;
13608 		goto set_wakeup;
13609 	}
13610 
13611 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
13612 					  info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
13613 					  nl80211_wowlan_policy, info->extack);
13614 	if (err)
13615 		return err;
13616 
13617 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
13618 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
13619 			return -EINVAL;
13620 		new_triggers.any = true;
13621 	}
13622 
13623 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
13624 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
13625 			return -EINVAL;
13626 		new_triggers.disconnect = true;
13627 		regular = true;
13628 	}
13629 
13630 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
13631 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
13632 			return -EINVAL;
13633 		new_triggers.magic_pkt = true;
13634 		regular = true;
13635 	}
13636 
13637 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
13638 		return -EINVAL;
13639 
13640 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
13641 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
13642 			return -EINVAL;
13643 		new_triggers.gtk_rekey_failure = true;
13644 		regular = true;
13645 	}
13646 
13647 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
13648 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
13649 			return -EINVAL;
13650 		new_triggers.eap_identity_req = true;
13651 		regular = true;
13652 	}
13653 
13654 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
13655 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
13656 			return -EINVAL;
13657 		new_triggers.four_way_handshake = true;
13658 		regular = true;
13659 	}
13660 
13661 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
13662 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
13663 			return -EINVAL;
13664 		new_triggers.rfkill_release = true;
13665 		regular = true;
13666 	}
13667 
13668 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
13669 		struct nlattr *pat;
13670 		int n_patterns = 0;
13671 		int rem, pat_len, mask_len, pkt_offset;
13672 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13673 
13674 		regular = true;
13675 
13676 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13677 				    rem)
13678 			n_patterns++;
13679 		if (n_patterns > wowlan->n_patterns)
13680 			return -EINVAL;
13681 
13682 		new_triggers.patterns = kcalloc(n_patterns,
13683 						sizeof(new_triggers.patterns[0]),
13684 						GFP_KERNEL);
13685 		if (!new_triggers.patterns)
13686 			return -ENOMEM;
13687 
13688 		new_triggers.n_patterns = n_patterns;
13689 		i = 0;
13690 
13691 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13692 				    rem) {
13693 			u8 *mask_pat;
13694 
13695 			err = nla_parse_nested_deprecated(pat_tb,
13696 							  MAX_NL80211_PKTPAT,
13697 							  pat,
13698 							  nl80211_packet_pattern_policy,
13699 							  info->extack);
13700 			if (err)
13701 				goto error;
13702 
13703 			err = -EINVAL;
13704 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
13705 			    !pat_tb[NL80211_PKTPAT_PATTERN])
13706 				goto error;
13707 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13708 			mask_len = DIV_ROUND_UP(pat_len, 8);
13709 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13710 				goto error;
13711 			if (pat_len > wowlan->pattern_max_len ||
13712 			    pat_len < wowlan->pattern_min_len)
13713 				goto error;
13714 
13715 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
13716 				pkt_offset = 0;
13717 			else
13718 				pkt_offset = nla_get_u32(
13719 					pat_tb[NL80211_PKTPAT_OFFSET]);
13720 			if (pkt_offset > wowlan->max_pkt_offset)
13721 				goto error;
13722 			new_triggers.patterns[i].pkt_offset = pkt_offset;
13723 
13724 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13725 			if (!mask_pat) {
13726 				err = -ENOMEM;
13727 				goto error;
13728 			}
13729 			new_triggers.patterns[i].mask = mask_pat;
13730 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13731 			       mask_len);
13732 			mask_pat += mask_len;
13733 			new_triggers.patterns[i].pattern = mask_pat;
13734 			new_triggers.patterns[i].pattern_len = pat_len;
13735 			memcpy(mask_pat,
13736 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13737 			       pat_len);
13738 			i++;
13739 		}
13740 	}
13741 
13742 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
13743 		regular = true;
13744 		err = nl80211_parse_wowlan_tcp(
13745 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
13746 			&new_triggers);
13747 		if (err)
13748 			goto error;
13749 	}
13750 
13751 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
13752 		regular = true;
13753 		err = nl80211_parse_wowlan_nd(
13754 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
13755 			&new_triggers);
13756 		if (err)
13757 			goto error;
13758 	}
13759 
13760 	/* The 'any' trigger means the device continues operating more or less
13761 	 * as in its normal operation mode and wakes up the host on most of the
13762 	 * normal interrupts (like packet RX, ...)
13763 	 * It therefore makes little sense to combine with the more constrained
13764 	 * wakeup trigger modes.
13765 	 */
13766 	if (new_triggers.any && regular) {
13767 		err = -EINVAL;
13768 		goto error;
13769 	}
13770 
13771 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
13772 	if (!ntrig) {
13773 		err = -ENOMEM;
13774 		goto error;
13775 	}
13776 	cfg80211_rdev_free_wowlan(rdev);
13777 	rdev->wiphy.wowlan_config = ntrig;
13778 
13779  set_wakeup:
13780 	if (rdev->ops->set_wakeup &&
13781 	    prev_enabled != !!rdev->wiphy.wowlan_config)
13782 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
13783 
13784 	return 0;
13785  error:
13786 	for (i = 0; i < new_triggers.n_patterns; i++)
13787 		kfree(new_triggers.patterns[i].mask);
13788 	kfree(new_triggers.patterns);
13789 	if (new_triggers.tcp && new_triggers.tcp->sock)
13790 		sock_release(new_triggers.tcp->sock);
13791 	kfree(new_triggers.tcp);
13792 	kfree(new_triggers.nd_config);
13793 	return err;
13794 }
13795 #endif
13796 
13797 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
13798 				       struct cfg80211_registered_device *rdev)
13799 {
13800 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
13801 	int i, j, pat_len;
13802 	struct cfg80211_coalesce_rules *rule;
13803 
13804 	if (!rdev->coalesce->n_rules)
13805 		return 0;
13806 
13807 	nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
13808 	if (!nl_rules)
13809 		return -ENOBUFS;
13810 
13811 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
13812 		nl_rule = nla_nest_start_noflag(msg, i + 1);
13813 		if (!nl_rule)
13814 			return -ENOBUFS;
13815 
13816 		rule = &rdev->coalesce->rules[i];
13817 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
13818 				rule->delay))
13819 			return -ENOBUFS;
13820 
13821 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
13822 				rule->condition))
13823 			return -ENOBUFS;
13824 
13825 		nl_pats = nla_nest_start_noflag(msg,
13826 						NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
13827 		if (!nl_pats)
13828 			return -ENOBUFS;
13829 
13830 		for (j = 0; j < rule->n_patterns; j++) {
13831 			nl_pat = nla_nest_start_noflag(msg, j + 1);
13832 			if (!nl_pat)
13833 				return -ENOBUFS;
13834 			pat_len = rule->patterns[j].pattern_len;
13835 			if (nla_put(msg, NL80211_PKTPAT_MASK,
13836 				    DIV_ROUND_UP(pat_len, 8),
13837 				    rule->patterns[j].mask) ||
13838 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
13839 				    rule->patterns[j].pattern) ||
13840 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
13841 					rule->patterns[j].pkt_offset))
13842 				return -ENOBUFS;
13843 			nla_nest_end(msg, nl_pat);
13844 		}
13845 		nla_nest_end(msg, nl_pats);
13846 		nla_nest_end(msg, nl_rule);
13847 	}
13848 	nla_nest_end(msg, nl_rules);
13849 
13850 	return 0;
13851 }
13852 
13853 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
13854 {
13855 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13856 	struct sk_buff *msg;
13857 	void *hdr;
13858 
13859 	if (!rdev->wiphy.coalesce)
13860 		return -EOPNOTSUPP;
13861 
13862 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13863 	if (!msg)
13864 		return -ENOMEM;
13865 
13866 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13867 			     NL80211_CMD_GET_COALESCE);
13868 	if (!hdr)
13869 		goto nla_put_failure;
13870 
13871 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
13872 		goto nla_put_failure;
13873 
13874 	genlmsg_end(msg, hdr);
13875 	return genlmsg_reply(msg, info);
13876 
13877 nla_put_failure:
13878 	nlmsg_free(msg);
13879 	return -ENOBUFS;
13880 }
13881 
13882 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
13883 {
13884 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
13885 	int i, j;
13886 	struct cfg80211_coalesce_rules *rule;
13887 
13888 	if (!coalesce)
13889 		return;
13890 
13891 	for (i = 0; i < coalesce->n_rules; i++) {
13892 		rule = &coalesce->rules[i];
13893 		for (j = 0; j < rule->n_patterns; j++)
13894 			kfree(rule->patterns[j].mask);
13895 		kfree(rule->patterns);
13896 	}
13897 	kfree(coalesce->rules);
13898 	kfree(coalesce);
13899 	rdev->coalesce = NULL;
13900 }
13901 
13902 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
13903 				       struct nlattr *rule,
13904 				       struct cfg80211_coalesce_rules *new_rule)
13905 {
13906 	int err, i;
13907 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13908 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
13909 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
13910 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13911 
13912 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
13913 					  rule, nl80211_coalesce_policy, NULL);
13914 	if (err)
13915 		return err;
13916 
13917 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
13918 		new_rule->delay =
13919 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
13920 	if (new_rule->delay > coalesce->max_delay)
13921 		return -EINVAL;
13922 
13923 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
13924 		new_rule->condition =
13925 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
13926 
13927 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
13928 		return -EINVAL;
13929 
13930 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13931 			    rem)
13932 		n_patterns++;
13933 	if (n_patterns > coalesce->n_patterns)
13934 		return -EINVAL;
13935 
13936 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
13937 				     GFP_KERNEL);
13938 	if (!new_rule->patterns)
13939 		return -ENOMEM;
13940 
13941 	new_rule->n_patterns = n_patterns;
13942 	i = 0;
13943 
13944 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13945 			    rem) {
13946 		u8 *mask_pat;
13947 
13948 		err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
13949 						  pat,
13950 						  nl80211_packet_pattern_policy,
13951 						  NULL);
13952 		if (err)
13953 			return err;
13954 
13955 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
13956 		    !pat_tb[NL80211_PKTPAT_PATTERN])
13957 			return -EINVAL;
13958 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13959 		mask_len = DIV_ROUND_UP(pat_len, 8);
13960 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13961 			return -EINVAL;
13962 		if (pat_len > coalesce->pattern_max_len ||
13963 		    pat_len < coalesce->pattern_min_len)
13964 			return -EINVAL;
13965 
13966 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
13967 			pkt_offset = 0;
13968 		else
13969 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
13970 		if (pkt_offset > coalesce->max_pkt_offset)
13971 			return -EINVAL;
13972 		new_rule->patterns[i].pkt_offset = pkt_offset;
13973 
13974 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13975 		if (!mask_pat)
13976 			return -ENOMEM;
13977 
13978 		new_rule->patterns[i].mask = mask_pat;
13979 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13980 		       mask_len);
13981 
13982 		mask_pat += mask_len;
13983 		new_rule->patterns[i].pattern = mask_pat;
13984 		new_rule->patterns[i].pattern_len = pat_len;
13985 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13986 		       pat_len);
13987 		i++;
13988 	}
13989 
13990 	return 0;
13991 }
13992 
13993 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
13994 {
13995 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13996 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13997 	struct cfg80211_coalesce new_coalesce = {};
13998 	struct cfg80211_coalesce *n_coalesce;
13999 	int err, rem_rule, n_rules = 0, i, j;
14000 	struct nlattr *rule;
14001 	struct cfg80211_coalesce_rules *tmp_rule;
14002 
14003 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
14004 		return -EOPNOTSUPP;
14005 
14006 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
14007 		cfg80211_rdev_free_coalesce(rdev);
14008 		rdev_set_coalesce(rdev, NULL);
14009 		return 0;
14010 	}
14011 
14012 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
14013 			    rem_rule)
14014 		n_rules++;
14015 	if (n_rules > coalesce->n_rules)
14016 		return -EINVAL;
14017 
14018 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
14019 				     GFP_KERNEL);
14020 	if (!new_coalesce.rules)
14021 		return -ENOMEM;
14022 
14023 	new_coalesce.n_rules = n_rules;
14024 	i = 0;
14025 
14026 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
14027 			    rem_rule) {
14028 		err = nl80211_parse_coalesce_rule(rdev, rule,
14029 						  &new_coalesce.rules[i]);
14030 		if (err)
14031 			goto error;
14032 
14033 		i++;
14034 	}
14035 
14036 	err = rdev_set_coalesce(rdev, &new_coalesce);
14037 	if (err)
14038 		goto error;
14039 
14040 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
14041 	if (!n_coalesce) {
14042 		err = -ENOMEM;
14043 		goto error;
14044 	}
14045 	cfg80211_rdev_free_coalesce(rdev);
14046 	rdev->coalesce = n_coalesce;
14047 
14048 	return 0;
14049 error:
14050 	for (i = 0; i < new_coalesce.n_rules; i++) {
14051 		tmp_rule = &new_coalesce.rules[i];
14052 		for (j = 0; j < tmp_rule->n_patterns; j++)
14053 			kfree(tmp_rule->patterns[j].mask);
14054 		kfree(tmp_rule->patterns);
14055 	}
14056 	kfree(new_coalesce.rules);
14057 
14058 	return err;
14059 }
14060 
14061 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
14062 {
14063 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14064 	struct net_device *dev = info->user_ptr[1];
14065 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14066 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
14067 	struct cfg80211_gtk_rekey_data rekey_data = {};
14068 	int err;
14069 
14070 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
14071 		return -EINVAL;
14072 
14073 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
14074 					  info->attrs[NL80211_ATTR_REKEY_DATA],
14075 					  nl80211_rekey_policy, info->extack);
14076 	if (err)
14077 		return err;
14078 
14079 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
14080 	    !tb[NL80211_REKEY_DATA_KCK])
14081 		return -EINVAL;
14082 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN &&
14083 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
14084 	      nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KEK_EXT_LEN))
14085 		return -ERANGE;
14086 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN &&
14087 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
14088 	      nla_len(tb[NL80211_REKEY_DATA_KCK]) == NL80211_KCK_EXT_LEN) &&
14089 	     !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KCK_32 &&
14090 	       nla_len(tb[NL80211_REKEY_DATA_KCK]) == NL80211_KCK_EXT_LEN_32))
14091 		return -ERANGE;
14092 
14093 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
14094 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
14095 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
14096 	rekey_data.kek_len = nla_len(tb[NL80211_REKEY_DATA_KEK]);
14097 	rekey_data.kck_len = nla_len(tb[NL80211_REKEY_DATA_KCK]);
14098 	if (tb[NL80211_REKEY_DATA_AKM])
14099 		rekey_data.akm = nla_get_u32(tb[NL80211_REKEY_DATA_AKM]);
14100 
14101 	wdev_lock(wdev);
14102 	if (!wdev->connected) {
14103 		err = -ENOTCONN;
14104 		goto out;
14105 	}
14106 
14107 	if (!rdev->ops->set_rekey_data) {
14108 		err = -EOPNOTSUPP;
14109 		goto out;
14110 	}
14111 
14112 	err = rdev_set_rekey_data(rdev, dev, &rekey_data);
14113  out:
14114 	wdev_unlock(wdev);
14115 	return err;
14116 }
14117 
14118 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
14119 					     struct genl_info *info)
14120 {
14121 	struct net_device *dev = info->user_ptr[1];
14122 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14123 
14124 	if (wdev->iftype != NL80211_IFTYPE_AP &&
14125 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
14126 		return -EINVAL;
14127 
14128 	if (wdev->ap_unexpected_nlportid)
14129 		return -EBUSY;
14130 
14131 	wdev->ap_unexpected_nlportid = info->snd_portid;
14132 	return 0;
14133 }
14134 
14135 static int nl80211_probe_client(struct sk_buff *skb,
14136 				struct genl_info *info)
14137 {
14138 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14139 	struct net_device *dev = info->user_ptr[1];
14140 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14141 	struct sk_buff *msg;
14142 	void *hdr;
14143 	const u8 *addr;
14144 	u64 cookie;
14145 	int err;
14146 
14147 	if (wdev->iftype != NL80211_IFTYPE_AP &&
14148 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
14149 		return -EOPNOTSUPP;
14150 
14151 	if (!info->attrs[NL80211_ATTR_MAC])
14152 		return -EINVAL;
14153 
14154 	if (!rdev->ops->probe_client)
14155 		return -EOPNOTSUPP;
14156 
14157 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14158 	if (!msg)
14159 		return -ENOMEM;
14160 
14161 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14162 			     NL80211_CMD_PROBE_CLIENT);
14163 	if (!hdr) {
14164 		err = -ENOBUFS;
14165 		goto free_msg;
14166 	}
14167 
14168 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
14169 
14170 	err = rdev_probe_client(rdev, dev, addr, &cookie);
14171 	if (err)
14172 		goto free_msg;
14173 
14174 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14175 			      NL80211_ATTR_PAD))
14176 		goto nla_put_failure;
14177 
14178 	genlmsg_end(msg, hdr);
14179 
14180 	return genlmsg_reply(msg, info);
14181 
14182  nla_put_failure:
14183 	err = -ENOBUFS;
14184  free_msg:
14185 	nlmsg_free(msg);
14186 	return err;
14187 }
14188 
14189 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
14190 {
14191 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14192 	struct cfg80211_beacon_registration *reg, *nreg;
14193 	int rv;
14194 
14195 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
14196 		return -EOPNOTSUPP;
14197 
14198 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
14199 	if (!nreg)
14200 		return -ENOMEM;
14201 
14202 	/* First, check if already registered. */
14203 	spin_lock_bh(&rdev->beacon_registrations_lock);
14204 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
14205 		if (reg->nlportid == info->snd_portid) {
14206 			rv = -EALREADY;
14207 			goto out_err;
14208 		}
14209 	}
14210 	/* Add it to the list */
14211 	nreg->nlportid = info->snd_portid;
14212 	list_add(&nreg->list, &rdev->beacon_registrations);
14213 
14214 	spin_unlock_bh(&rdev->beacon_registrations_lock);
14215 
14216 	return 0;
14217 out_err:
14218 	spin_unlock_bh(&rdev->beacon_registrations_lock);
14219 	kfree(nreg);
14220 	return rv;
14221 }
14222 
14223 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
14224 {
14225 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14226 	struct wireless_dev *wdev = info->user_ptr[1];
14227 	int err;
14228 
14229 	if (!rdev->ops->start_p2p_device)
14230 		return -EOPNOTSUPP;
14231 
14232 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
14233 		return -EOPNOTSUPP;
14234 
14235 	if (wdev_running(wdev))
14236 		return 0;
14237 
14238 	if (rfkill_blocked(rdev->wiphy.rfkill))
14239 		return -ERFKILL;
14240 
14241 	err = rdev_start_p2p_device(rdev, wdev);
14242 	if (err)
14243 		return err;
14244 
14245 	wdev->is_running = true;
14246 	rdev->opencount++;
14247 
14248 	return 0;
14249 }
14250 
14251 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
14252 {
14253 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14254 	struct wireless_dev *wdev = info->user_ptr[1];
14255 
14256 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
14257 		return -EOPNOTSUPP;
14258 
14259 	if (!rdev->ops->stop_p2p_device)
14260 		return -EOPNOTSUPP;
14261 
14262 	cfg80211_stop_p2p_device(rdev, wdev);
14263 
14264 	return 0;
14265 }
14266 
14267 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
14268 {
14269 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14270 	struct wireless_dev *wdev = info->user_ptr[1];
14271 	struct cfg80211_nan_conf conf = {};
14272 	int err;
14273 
14274 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14275 		return -EOPNOTSUPP;
14276 
14277 	if (wdev_running(wdev))
14278 		return -EEXIST;
14279 
14280 	if (rfkill_blocked(rdev->wiphy.rfkill))
14281 		return -ERFKILL;
14282 
14283 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
14284 		return -EINVAL;
14285 
14286 	conf.master_pref =
14287 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14288 
14289 	if (info->attrs[NL80211_ATTR_BANDS]) {
14290 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14291 
14292 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14293 			return -EOPNOTSUPP;
14294 
14295 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14296 			return -EINVAL;
14297 
14298 		conf.bands = bands;
14299 	}
14300 
14301 	err = rdev_start_nan(rdev, wdev, &conf);
14302 	if (err)
14303 		return err;
14304 
14305 	wdev->is_running = true;
14306 	rdev->opencount++;
14307 
14308 	return 0;
14309 }
14310 
14311 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
14312 {
14313 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14314 	struct wireless_dev *wdev = info->user_ptr[1];
14315 
14316 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14317 		return -EOPNOTSUPP;
14318 
14319 	cfg80211_stop_nan(rdev, wdev);
14320 
14321 	return 0;
14322 }
14323 
14324 static int validate_nan_filter(struct nlattr *filter_attr)
14325 {
14326 	struct nlattr *attr;
14327 	int len = 0, n_entries = 0, rem;
14328 
14329 	nla_for_each_nested(attr, filter_attr, rem) {
14330 		len += nla_len(attr);
14331 		n_entries++;
14332 	}
14333 
14334 	if (len >= U8_MAX)
14335 		return -EINVAL;
14336 
14337 	return n_entries;
14338 }
14339 
14340 static int handle_nan_filter(struct nlattr *attr_filter,
14341 			     struct cfg80211_nan_func *func,
14342 			     bool tx)
14343 {
14344 	struct nlattr *attr;
14345 	int n_entries, rem, i;
14346 	struct cfg80211_nan_func_filter *filter;
14347 
14348 	n_entries = validate_nan_filter(attr_filter);
14349 	if (n_entries < 0)
14350 		return n_entries;
14351 
14352 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
14353 
14354 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
14355 	if (!filter)
14356 		return -ENOMEM;
14357 
14358 	i = 0;
14359 	nla_for_each_nested(attr, attr_filter, rem) {
14360 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
14361 		if (!filter[i].filter)
14362 			goto err;
14363 
14364 		filter[i].len = nla_len(attr);
14365 		i++;
14366 	}
14367 	if (tx) {
14368 		func->num_tx_filters = n_entries;
14369 		func->tx_filters = filter;
14370 	} else {
14371 		func->num_rx_filters = n_entries;
14372 		func->rx_filters = filter;
14373 	}
14374 
14375 	return 0;
14376 
14377 err:
14378 	i = 0;
14379 	nla_for_each_nested(attr, attr_filter, rem) {
14380 		kfree(filter[i].filter);
14381 		i++;
14382 	}
14383 	kfree(filter);
14384 	return -ENOMEM;
14385 }
14386 
14387 static int nl80211_nan_add_func(struct sk_buff *skb,
14388 				struct genl_info *info)
14389 {
14390 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14391 	struct wireless_dev *wdev = info->user_ptr[1];
14392 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
14393 	struct cfg80211_nan_func *func;
14394 	struct sk_buff *msg = NULL;
14395 	void *hdr = NULL;
14396 	int err = 0;
14397 
14398 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14399 		return -EOPNOTSUPP;
14400 
14401 	if (!wdev_running(wdev))
14402 		return -ENOTCONN;
14403 
14404 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
14405 		return -EINVAL;
14406 
14407 	err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
14408 					  info->attrs[NL80211_ATTR_NAN_FUNC],
14409 					  nl80211_nan_func_policy,
14410 					  info->extack);
14411 	if (err)
14412 		return err;
14413 
14414 	func = kzalloc(sizeof(*func), GFP_KERNEL);
14415 	if (!func)
14416 		return -ENOMEM;
14417 
14418 	func->cookie = cfg80211_assign_cookie(rdev);
14419 
14420 	if (!tb[NL80211_NAN_FUNC_TYPE]) {
14421 		err = -EINVAL;
14422 		goto out;
14423 	}
14424 
14425 
14426 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
14427 
14428 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
14429 		err = -EINVAL;
14430 		goto out;
14431 	}
14432 
14433 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
14434 	       sizeof(func->service_id));
14435 
14436 	func->close_range =
14437 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
14438 
14439 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
14440 		func->serv_spec_info_len =
14441 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
14442 		func->serv_spec_info =
14443 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
14444 				func->serv_spec_info_len,
14445 				GFP_KERNEL);
14446 		if (!func->serv_spec_info) {
14447 			err = -ENOMEM;
14448 			goto out;
14449 		}
14450 	}
14451 
14452 	if (tb[NL80211_NAN_FUNC_TTL])
14453 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
14454 
14455 	switch (func->type) {
14456 	case NL80211_NAN_FUNC_PUBLISH:
14457 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
14458 			err = -EINVAL;
14459 			goto out;
14460 		}
14461 
14462 		func->publish_type =
14463 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
14464 		func->publish_bcast =
14465 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
14466 
14467 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
14468 			func->publish_bcast) {
14469 			err = -EINVAL;
14470 			goto out;
14471 		}
14472 		break;
14473 	case NL80211_NAN_FUNC_SUBSCRIBE:
14474 		func->subscribe_active =
14475 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
14476 		break;
14477 	case NL80211_NAN_FUNC_FOLLOW_UP:
14478 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
14479 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
14480 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
14481 			err = -EINVAL;
14482 			goto out;
14483 		}
14484 
14485 		func->followup_id =
14486 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
14487 		func->followup_reqid =
14488 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
14489 		memcpy(func->followup_dest.addr,
14490 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
14491 		       sizeof(func->followup_dest.addr));
14492 		if (func->ttl) {
14493 			err = -EINVAL;
14494 			goto out;
14495 		}
14496 		break;
14497 	default:
14498 		err = -EINVAL;
14499 		goto out;
14500 	}
14501 
14502 	if (tb[NL80211_NAN_FUNC_SRF]) {
14503 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
14504 
14505 		err = nla_parse_nested_deprecated(srf_tb,
14506 						  NL80211_NAN_SRF_ATTR_MAX,
14507 						  tb[NL80211_NAN_FUNC_SRF],
14508 						  nl80211_nan_srf_policy,
14509 						  info->extack);
14510 		if (err)
14511 			goto out;
14512 
14513 		func->srf_include =
14514 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
14515 
14516 		if (srf_tb[NL80211_NAN_SRF_BF]) {
14517 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
14518 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
14519 				err = -EINVAL;
14520 				goto out;
14521 			}
14522 
14523 			func->srf_bf_len =
14524 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
14525 			func->srf_bf =
14526 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
14527 					func->srf_bf_len, GFP_KERNEL);
14528 			if (!func->srf_bf) {
14529 				err = -ENOMEM;
14530 				goto out;
14531 			}
14532 
14533 			func->srf_bf_idx =
14534 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
14535 		} else {
14536 			struct nlattr *attr, *mac_attr =
14537 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
14538 			int n_entries, rem, i = 0;
14539 
14540 			if (!mac_attr) {
14541 				err = -EINVAL;
14542 				goto out;
14543 			}
14544 
14545 			n_entries = validate_acl_mac_addrs(mac_attr);
14546 			if (n_entries <= 0) {
14547 				err = -EINVAL;
14548 				goto out;
14549 			}
14550 
14551 			func->srf_num_macs = n_entries;
14552 			func->srf_macs =
14553 				kcalloc(n_entries, sizeof(*func->srf_macs),
14554 					GFP_KERNEL);
14555 			if (!func->srf_macs) {
14556 				err = -ENOMEM;
14557 				goto out;
14558 			}
14559 
14560 			nla_for_each_nested(attr, mac_attr, rem)
14561 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
14562 				       sizeof(*func->srf_macs));
14563 		}
14564 	}
14565 
14566 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
14567 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
14568 					func, true);
14569 		if (err)
14570 			goto out;
14571 	}
14572 
14573 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
14574 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
14575 					func, false);
14576 		if (err)
14577 			goto out;
14578 	}
14579 
14580 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14581 	if (!msg) {
14582 		err = -ENOMEM;
14583 		goto out;
14584 	}
14585 
14586 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14587 			     NL80211_CMD_ADD_NAN_FUNCTION);
14588 	/* This can't really happen - we just allocated 4KB */
14589 	if (WARN_ON(!hdr)) {
14590 		err = -ENOMEM;
14591 		goto out;
14592 	}
14593 
14594 	err = rdev_add_nan_func(rdev, wdev, func);
14595 out:
14596 	if (err < 0) {
14597 		cfg80211_free_nan_func(func);
14598 		nlmsg_free(msg);
14599 		return err;
14600 	}
14601 
14602 	/* propagate the instance id and cookie to userspace  */
14603 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
14604 			      NL80211_ATTR_PAD))
14605 		goto nla_put_failure;
14606 
14607 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14608 	if (!func_attr)
14609 		goto nla_put_failure;
14610 
14611 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
14612 		       func->instance_id))
14613 		goto nla_put_failure;
14614 
14615 	nla_nest_end(msg, func_attr);
14616 
14617 	genlmsg_end(msg, hdr);
14618 	return genlmsg_reply(msg, info);
14619 
14620 nla_put_failure:
14621 	nlmsg_free(msg);
14622 	return -ENOBUFS;
14623 }
14624 
14625 static int nl80211_nan_del_func(struct sk_buff *skb,
14626 			       struct genl_info *info)
14627 {
14628 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14629 	struct wireless_dev *wdev = info->user_ptr[1];
14630 	u64 cookie;
14631 
14632 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14633 		return -EOPNOTSUPP;
14634 
14635 	if (!wdev_running(wdev))
14636 		return -ENOTCONN;
14637 
14638 	if (!info->attrs[NL80211_ATTR_COOKIE])
14639 		return -EINVAL;
14640 
14641 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
14642 
14643 	rdev_del_nan_func(rdev, wdev, cookie);
14644 
14645 	return 0;
14646 }
14647 
14648 static int nl80211_nan_change_config(struct sk_buff *skb,
14649 				     struct genl_info *info)
14650 {
14651 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14652 	struct wireless_dev *wdev = info->user_ptr[1];
14653 	struct cfg80211_nan_conf conf = {};
14654 	u32 changed = 0;
14655 
14656 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14657 		return -EOPNOTSUPP;
14658 
14659 	if (!wdev_running(wdev))
14660 		return -ENOTCONN;
14661 
14662 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
14663 		conf.master_pref =
14664 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14665 		if (conf.master_pref <= 1 || conf.master_pref == 255)
14666 			return -EINVAL;
14667 
14668 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
14669 	}
14670 
14671 	if (info->attrs[NL80211_ATTR_BANDS]) {
14672 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14673 
14674 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14675 			return -EOPNOTSUPP;
14676 
14677 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14678 			return -EINVAL;
14679 
14680 		conf.bands = bands;
14681 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
14682 	}
14683 
14684 	if (!changed)
14685 		return -EINVAL;
14686 
14687 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
14688 }
14689 
14690 void cfg80211_nan_match(struct wireless_dev *wdev,
14691 			struct cfg80211_nan_match_params *match, gfp_t gfp)
14692 {
14693 	struct wiphy *wiphy = wdev->wiphy;
14694 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14695 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
14696 	struct sk_buff *msg;
14697 	void *hdr;
14698 
14699 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
14700 		return;
14701 
14702 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14703 	if (!msg)
14704 		return;
14705 
14706 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
14707 	if (!hdr) {
14708 		nlmsg_free(msg);
14709 		return;
14710 	}
14711 
14712 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14713 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14714 					 wdev->netdev->ifindex)) ||
14715 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14716 			      NL80211_ATTR_PAD))
14717 		goto nla_put_failure;
14718 
14719 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
14720 			      NL80211_ATTR_PAD) ||
14721 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
14722 		goto nla_put_failure;
14723 
14724 	match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
14725 	if (!match_attr)
14726 		goto nla_put_failure;
14727 
14728 	local_func_attr = nla_nest_start_noflag(msg,
14729 						NL80211_NAN_MATCH_FUNC_LOCAL);
14730 	if (!local_func_attr)
14731 		goto nla_put_failure;
14732 
14733 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
14734 		goto nla_put_failure;
14735 
14736 	nla_nest_end(msg, local_func_attr);
14737 
14738 	peer_func_attr = nla_nest_start_noflag(msg,
14739 					       NL80211_NAN_MATCH_FUNC_PEER);
14740 	if (!peer_func_attr)
14741 		goto nla_put_failure;
14742 
14743 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
14744 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
14745 		goto nla_put_failure;
14746 
14747 	if (match->info && match->info_len &&
14748 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
14749 		    match->info))
14750 		goto nla_put_failure;
14751 
14752 	nla_nest_end(msg, peer_func_attr);
14753 	nla_nest_end(msg, match_attr);
14754 	genlmsg_end(msg, hdr);
14755 
14756 	if (!wdev->owner_nlportid)
14757 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14758 					msg, 0, NL80211_MCGRP_NAN, gfp);
14759 	else
14760 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14761 				wdev->owner_nlportid);
14762 
14763 	return;
14764 
14765 nla_put_failure:
14766 	nlmsg_free(msg);
14767 }
14768 EXPORT_SYMBOL(cfg80211_nan_match);
14769 
14770 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
14771 				  u8 inst_id,
14772 				  enum nl80211_nan_func_term_reason reason,
14773 				  u64 cookie, gfp_t gfp)
14774 {
14775 	struct wiphy *wiphy = wdev->wiphy;
14776 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14777 	struct sk_buff *msg;
14778 	struct nlattr *func_attr;
14779 	void *hdr;
14780 
14781 	if (WARN_ON(!inst_id))
14782 		return;
14783 
14784 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14785 	if (!msg)
14786 		return;
14787 
14788 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
14789 	if (!hdr) {
14790 		nlmsg_free(msg);
14791 		return;
14792 	}
14793 
14794 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14795 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14796 					 wdev->netdev->ifindex)) ||
14797 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14798 			      NL80211_ATTR_PAD))
14799 		goto nla_put_failure;
14800 
14801 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14802 			      NL80211_ATTR_PAD))
14803 		goto nla_put_failure;
14804 
14805 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14806 	if (!func_attr)
14807 		goto nla_put_failure;
14808 
14809 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
14810 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
14811 		goto nla_put_failure;
14812 
14813 	nla_nest_end(msg, func_attr);
14814 	genlmsg_end(msg, hdr);
14815 
14816 	if (!wdev->owner_nlportid)
14817 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14818 					msg, 0, NL80211_MCGRP_NAN, gfp);
14819 	else
14820 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14821 				wdev->owner_nlportid);
14822 
14823 	return;
14824 
14825 nla_put_failure:
14826 	nlmsg_free(msg);
14827 }
14828 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
14829 
14830 static int nl80211_get_protocol_features(struct sk_buff *skb,
14831 					 struct genl_info *info)
14832 {
14833 	void *hdr;
14834 	struct sk_buff *msg;
14835 
14836 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14837 	if (!msg)
14838 		return -ENOMEM;
14839 
14840 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14841 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
14842 	if (!hdr)
14843 		goto nla_put_failure;
14844 
14845 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
14846 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
14847 		goto nla_put_failure;
14848 
14849 	genlmsg_end(msg, hdr);
14850 	return genlmsg_reply(msg, info);
14851 
14852  nla_put_failure:
14853 	kfree_skb(msg);
14854 	return -ENOBUFS;
14855 }
14856 
14857 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
14858 {
14859 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14860 	struct cfg80211_update_ft_ies_params ft_params;
14861 	struct net_device *dev = info->user_ptr[1];
14862 
14863 	if (!rdev->ops->update_ft_ies)
14864 		return -EOPNOTSUPP;
14865 
14866 	if (!info->attrs[NL80211_ATTR_MDID] ||
14867 	    !info->attrs[NL80211_ATTR_IE])
14868 		return -EINVAL;
14869 
14870 	memset(&ft_params, 0, sizeof(ft_params));
14871 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
14872 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
14873 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
14874 
14875 	return rdev_update_ft_ies(rdev, dev, &ft_params);
14876 }
14877 
14878 static int nl80211_crit_protocol_start(struct sk_buff *skb,
14879 				       struct genl_info *info)
14880 {
14881 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14882 	struct wireless_dev *wdev = info->user_ptr[1];
14883 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
14884 	u16 duration;
14885 	int ret;
14886 
14887 	if (!rdev->ops->crit_proto_start)
14888 		return -EOPNOTSUPP;
14889 
14890 	if (WARN_ON(!rdev->ops->crit_proto_stop))
14891 		return -EINVAL;
14892 
14893 	if (rdev->crit_proto_nlportid)
14894 		return -EBUSY;
14895 
14896 	/* determine protocol if provided */
14897 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
14898 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
14899 
14900 	if (proto >= NUM_NL80211_CRIT_PROTO)
14901 		return -EINVAL;
14902 
14903 	/* timeout must be provided */
14904 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
14905 		return -EINVAL;
14906 
14907 	duration =
14908 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
14909 
14910 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
14911 	if (!ret)
14912 		rdev->crit_proto_nlportid = info->snd_portid;
14913 
14914 	return ret;
14915 }
14916 
14917 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
14918 				      struct genl_info *info)
14919 {
14920 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14921 	struct wireless_dev *wdev = info->user_ptr[1];
14922 
14923 	if (!rdev->ops->crit_proto_stop)
14924 		return -EOPNOTSUPP;
14925 
14926 	if (rdev->crit_proto_nlportid) {
14927 		rdev->crit_proto_nlportid = 0;
14928 		rdev_crit_proto_stop(rdev, wdev);
14929 	}
14930 	return 0;
14931 }
14932 
14933 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
14934 				       struct nlattr *attr,
14935 				       struct netlink_ext_ack *extack)
14936 {
14937 	if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
14938 		if (attr->nla_type & NLA_F_NESTED) {
14939 			NL_SET_ERR_MSG_ATTR(extack, attr,
14940 					    "unexpected nested data");
14941 			return -EINVAL;
14942 		}
14943 
14944 		return 0;
14945 	}
14946 
14947 	if (!(attr->nla_type & NLA_F_NESTED)) {
14948 		NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
14949 		return -EINVAL;
14950 	}
14951 
14952 	return nla_validate_nested(attr, vcmd->maxattr, vcmd->policy, extack);
14953 }
14954 
14955 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
14956 {
14957 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14958 	struct wireless_dev *wdev =
14959 		__cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
14960 					   info->attrs);
14961 	int i, err;
14962 	u32 vid, subcmd;
14963 
14964 	if (!rdev->wiphy.vendor_commands)
14965 		return -EOPNOTSUPP;
14966 
14967 	if (IS_ERR(wdev)) {
14968 		err = PTR_ERR(wdev);
14969 		if (err != -EINVAL)
14970 			return err;
14971 		wdev = NULL;
14972 	} else if (wdev->wiphy != &rdev->wiphy) {
14973 		return -EINVAL;
14974 	}
14975 
14976 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
14977 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
14978 		return -EINVAL;
14979 
14980 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
14981 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
14982 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
14983 		const struct wiphy_vendor_command *vcmd;
14984 		void *data = NULL;
14985 		int len = 0;
14986 
14987 		vcmd = &rdev->wiphy.vendor_commands[i];
14988 
14989 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
14990 			continue;
14991 
14992 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
14993 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
14994 			if (!wdev)
14995 				return -EINVAL;
14996 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
14997 			    !wdev->netdev)
14998 				return -EINVAL;
14999 
15000 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
15001 				if (!wdev_running(wdev))
15002 					return -ENETDOWN;
15003 			}
15004 		} else {
15005 			wdev = NULL;
15006 		}
15007 
15008 		if (!vcmd->doit)
15009 			return -EOPNOTSUPP;
15010 
15011 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
15012 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
15013 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
15014 
15015 			err = nl80211_vendor_check_policy(vcmd,
15016 					info->attrs[NL80211_ATTR_VENDOR_DATA],
15017 					info->extack);
15018 			if (err)
15019 				return err;
15020 		}
15021 
15022 		rdev->cur_cmd_info = info;
15023 		err = vcmd->doit(&rdev->wiphy, wdev, data, len);
15024 		rdev->cur_cmd_info = NULL;
15025 		return err;
15026 	}
15027 
15028 	return -EOPNOTSUPP;
15029 }
15030 
15031 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
15032 				       struct netlink_callback *cb,
15033 				       struct cfg80211_registered_device **rdev,
15034 				       struct wireless_dev **wdev)
15035 {
15036 	struct nlattr **attrbuf;
15037 	u32 vid, subcmd;
15038 	unsigned int i;
15039 	int vcmd_idx = -1;
15040 	int err;
15041 	void *data = NULL;
15042 	unsigned int data_len = 0;
15043 
15044 	if (cb->args[0]) {
15045 		/* subtract the 1 again here */
15046 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
15047 		struct wireless_dev *tmp;
15048 
15049 		if (!wiphy)
15050 			return -ENODEV;
15051 		*rdev = wiphy_to_rdev(wiphy);
15052 		*wdev = NULL;
15053 
15054 		if (cb->args[1]) {
15055 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
15056 				if (tmp->identifier == cb->args[1] - 1) {
15057 					*wdev = tmp;
15058 					break;
15059 				}
15060 			}
15061 		}
15062 
15063 		/* keep rtnl locked in successful case */
15064 		return 0;
15065 	}
15066 
15067 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
15068 	if (!attrbuf)
15069 		return -ENOMEM;
15070 
15071 	err = nlmsg_parse_deprecated(cb->nlh,
15072 				     GENL_HDRLEN + nl80211_fam.hdrsize,
15073 				     attrbuf, nl80211_fam.maxattr,
15074 				     nl80211_policy, NULL);
15075 	if (err)
15076 		goto out;
15077 
15078 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
15079 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
15080 		err = -EINVAL;
15081 		goto out;
15082 	}
15083 
15084 	*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(skb->sk), attrbuf);
15085 	if (IS_ERR(*wdev))
15086 		*wdev = NULL;
15087 
15088 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
15089 	if (IS_ERR(*rdev)) {
15090 		err = PTR_ERR(*rdev);
15091 		goto out;
15092 	}
15093 
15094 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
15095 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
15096 
15097 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
15098 		const struct wiphy_vendor_command *vcmd;
15099 
15100 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
15101 
15102 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
15103 			continue;
15104 
15105 		if (!vcmd->dumpit) {
15106 			err = -EOPNOTSUPP;
15107 			goto out;
15108 		}
15109 
15110 		vcmd_idx = i;
15111 		break;
15112 	}
15113 
15114 	if (vcmd_idx < 0) {
15115 		err = -EOPNOTSUPP;
15116 		goto out;
15117 	}
15118 
15119 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
15120 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
15121 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
15122 
15123 		err = nl80211_vendor_check_policy(
15124 				&(*rdev)->wiphy.vendor_commands[vcmd_idx],
15125 				attrbuf[NL80211_ATTR_VENDOR_DATA],
15126 				cb->extack);
15127 		if (err)
15128 			goto out;
15129 	}
15130 
15131 	/* 0 is the first index - add 1 to parse only once */
15132 	cb->args[0] = (*rdev)->wiphy_idx + 1;
15133 	/* add 1 to know if it was NULL */
15134 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
15135 	cb->args[2] = vcmd_idx;
15136 	cb->args[3] = (unsigned long)data;
15137 	cb->args[4] = data_len;
15138 
15139 	/* keep rtnl locked in successful case */
15140 	err = 0;
15141 out:
15142 	kfree(attrbuf);
15143 	return err;
15144 }
15145 
15146 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
15147 				   struct netlink_callback *cb)
15148 {
15149 	struct cfg80211_registered_device *rdev;
15150 	struct wireless_dev *wdev;
15151 	unsigned int vcmd_idx;
15152 	const struct wiphy_vendor_command *vcmd;
15153 	void *data;
15154 	int data_len;
15155 	int err;
15156 	struct nlattr *vendor_data;
15157 
15158 	rtnl_lock();
15159 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
15160 	if (err)
15161 		goto out;
15162 
15163 	vcmd_idx = cb->args[2];
15164 	data = (void *)cb->args[3];
15165 	data_len = cb->args[4];
15166 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
15167 
15168 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
15169 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
15170 		if (!wdev) {
15171 			err = -EINVAL;
15172 			goto out;
15173 		}
15174 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
15175 		    !wdev->netdev) {
15176 			err = -EINVAL;
15177 			goto out;
15178 		}
15179 
15180 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
15181 			if (!wdev_running(wdev)) {
15182 				err = -ENETDOWN;
15183 				goto out;
15184 			}
15185 		}
15186 	}
15187 
15188 	while (1) {
15189 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
15190 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
15191 					   NL80211_CMD_VENDOR);
15192 		if (!hdr)
15193 			break;
15194 
15195 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15196 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
15197 					       wdev_id(wdev),
15198 					       NL80211_ATTR_PAD))) {
15199 			genlmsg_cancel(skb, hdr);
15200 			break;
15201 		}
15202 
15203 		vendor_data = nla_nest_start_noflag(skb,
15204 						    NL80211_ATTR_VENDOR_DATA);
15205 		if (!vendor_data) {
15206 			genlmsg_cancel(skb, hdr);
15207 			break;
15208 		}
15209 
15210 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
15211 				   (unsigned long *)&cb->args[5]);
15212 		nla_nest_end(skb, vendor_data);
15213 
15214 		if (err == -ENOBUFS || err == -ENOENT) {
15215 			genlmsg_cancel(skb, hdr);
15216 			break;
15217 		} else if (err <= 0) {
15218 			genlmsg_cancel(skb, hdr);
15219 			goto out;
15220 		}
15221 
15222 		genlmsg_end(skb, hdr);
15223 	}
15224 
15225 	err = skb->len;
15226  out:
15227 	rtnl_unlock();
15228 	return err;
15229 }
15230 
15231 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
15232 					   enum nl80211_commands cmd,
15233 					   enum nl80211_attrs attr,
15234 					   int approxlen)
15235 {
15236 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15237 
15238 	if (WARN_ON(!rdev->cur_cmd_info))
15239 		return NULL;
15240 
15241 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
15242 					   rdev->cur_cmd_info->snd_portid,
15243 					   rdev->cur_cmd_info->snd_seq,
15244 					   cmd, attr, NULL, GFP_KERNEL);
15245 }
15246 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
15247 
15248 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
15249 {
15250 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
15251 	void *hdr = ((void **)skb->cb)[1];
15252 	struct nlattr *data = ((void **)skb->cb)[2];
15253 
15254 	/* clear CB data for netlink core to own from now on */
15255 	memset(skb->cb, 0, sizeof(skb->cb));
15256 
15257 	if (WARN_ON(!rdev->cur_cmd_info)) {
15258 		kfree_skb(skb);
15259 		return -EINVAL;
15260 	}
15261 
15262 	nla_nest_end(skb, data);
15263 	genlmsg_end(skb, hdr);
15264 	return genlmsg_reply(skb, rdev->cur_cmd_info);
15265 }
15266 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
15267 
15268 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
15269 {
15270 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15271 
15272 	if (WARN_ON(!rdev->cur_cmd_info))
15273 		return 0;
15274 
15275 	return rdev->cur_cmd_info->snd_portid;
15276 }
15277 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
15278 
15279 static int nl80211_set_qos_map(struct sk_buff *skb,
15280 			       struct genl_info *info)
15281 {
15282 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15283 	struct cfg80211_qos_map *qos_map = NULL;
15284 	struct net_device *dev = info->user_ptr[1];
15285 	u8 *pos, len, num_des, des_len, des;
15286 	int ret;
15287 
15288 	if (!rdev->ops->set_qos_map)
15289 		return -EOPNOTSUPP;
15290 
15291 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
15292 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
15293 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
15294 
15295 		if (len % 2)
15296 			return -EINVAL;
15297 
15298 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
15299 		if (!qos_map)
15300 			return -ENOMEM;
15301 
15302 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
15303 		if (num_des) {
15304 			des_len = num_des *
15305 				sizeof(struct cfg80211_dscp_exception);
15306 			memcpy(qos_map->dscp_exception, pos, des_len);
15307 			qos_map->num_des = num_des;
15308 			for (des = 0; des < num_des; des++) {
15309 				if (qos_map->dscp_exception[des].up > 7) {
15310 					kfree(qos_map);
15311 					return -EINVAL;
15312 				}
15313 			}
15314 			pos += des_len;
15315 		}
15316 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
15317 	}
15318 
15319 	wdev_lock(dev->ieee80211_ptr);
15320 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
15321 	if (!ret)
15322 		ret = rdev_set_qos_map(rdev, dev, qos_map);
15323 	wdev_unlock(dev->ieee80211_ptr);
15324 
15325 	kfree(qos_map);
15326 	return ret;
15327 }
15328 
15329 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
15330 {
15331 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15332 	struct net_device *dev = info->user_ptr[1];
15333 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15334 	const u8 *peer;
15335 	u8 tsid, up;
15336 	u16 admitted_time = 0;
15337 	int err;
15338 
15339 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
15340 		return -EOPNOTSUPP;
15341 
15342 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
15343 	    !info->attrs[NL80211_ATTR_USER_PRIO])
15344 		return -EINVAL;
15345 
15346 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15347 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
15348 
15349 	/* WMM uses TIDs 0-7 even for TSPEC */
15350 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
15351 		/* TODO: handle 802.11 TSPEC/admission control
15352 		 * need more attributes for that (e.g. BA session requirement);
15353 		 * change the WMM adminssion test above to allow both then
15354 		 */
15355 		return -EINVAL;
15356 	}
15357 
15358 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15359 
15360 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
15361 		admitted_time =
15362 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
15363 		if (!admitted_time)
15364 			return -EINVAL;
15365 	}
15366 
15367 	wdev_lock(wdev);
15368 	switch (wdev->iftype) {
15369 	case NL80211_IFTYPE_STATION:
15370 	case NL80211_IFTYPE_P2P_CLIENT:
15371 		if (wdev->connected)
15372 			break;
15373 		err = -ENOTCONN;
15374 		goto out;
15375 	default:
15376 		err = -EOPNOTSUPP;
15377 		goto out;
15378 	}
15379 
15380 	err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
15381 
15382  out:
15383 	wdev_unlock(wdev);
15384 	return err;
15385 }
15386 
15387 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
15388 {
15389 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15390 	struct net_device *dev = info->user_ptr[1];
15391 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15392 	const u8 *peer;
15393 	u8 tsid;
15394 	int err;
15395 
15396 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
15397 		return -EINVAL;
15398 
15399 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15400 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15401 
15402 	wdev_lock(wdev);
15403 	err = rdev_del_tx_ts(rdev, dev, tsid, peer);
15404 	wdev_unlock(wdev);
15405 
15406 	return err;
15407 }
15408 
15409 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
15410 				       struct genl_info *info)
15411 {
15412 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15413 	struct net_device *dev = info->user_ptr[1];
15414 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15415 	struct cfg80211_chan_def chandef = {};
15416 	const u8 *addr;
15417 	u8 oper_class;
15418 	int err;
15419 
15420 	if (!rdev->ops->tdls_channel_switch ||
15421 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15422 		return -EOPNOTSUPP;
15423 
15424 	switch (dev->ieee80211_ptr->iftype) {
15425 	case NL80211_IFTYPE_STATION:
15426 	case NL80211_IFTYPE_P2P_CLIENT:
15427 		break;
15428 	default:
15429 		return -EOPNOTSUPP;
15430 	}
15431 
15432 	if (!info->attrs[NL80211_ATTR_MAC] ||
15433 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
15434 		return -EINVAL;
15435 
15436 	err = nl80211_parse_chandef(rdev, info, &chandef);
15437 	if (err)
15438 		return err;
15439 
15440 	/*
15441 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
15442 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
15443 	 * specification is not defined for them.
15444 	 */
15445 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
15446 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
15447 	    chandef.width != NL80211_CHAN_WIDTH_20)
15448 		return -EINVAL;
15449 
15450 	/* we will be active on the TDLS link */
15451 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
15452 					   wdev->iftype))
15453 		return -EINVAL;
15454 
15455 	/* don't allow switching to DFS channels */
15456 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
15457 		return -EINVAL;
15458 
15459 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15460 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
15461 
15462 	wdev_lock(wdev);
15463 	err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
15464 	wdev_unlock(wdev);
15465 
15466 	return err;
15467 }
15468 
15469 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
15470 					      struct genl_info *info)
15471 {
15472 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15473 	struct net_device *dev = info->user_ptr[1];
15474 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15475 	const u8 *addr;
15476 
15477 	if (!rdev->ops->tdls_channel_switch ||
15478 	    !rdev->ops->tdls_cancel_channel_switch ||
15479 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15480 		return -EOPNOTSUPP;
15481 
15482 	switch (dev->ieee80211_ptr->iftype) {
15483 	case NL80211_IFTYPE_STATION:
15484 	case NL80211_IFTYPE_P2P_CLIENT:
15485 		break;
15486 	default:
15487 		return -EOPNOTSUPP;
15488 	}
15489 
15490 	if (!info->attrs[NL80211_ATTR_MAC])
15491 		return -EINVAL;
15492 
15493 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15494 
15495 	wdev_lock(wdev);
15496 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
15497 	wdev_unlock(wdev);
15498 
15499 	return 0;
15500 }
15501 
15502 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
15503 					    struct genl_info *info)
15504 {
15505 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15506 	struct net_device *dev = info->user_ptr[1];
15507 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15508 	const struct nlattr *nla;
15509 	bool enabled;
15510 
15511 	if (!rdev->ops->set_multicast_to_unicast)
15512 		return -EOPNOTSUPP;
15513 
15514 	if (wdev->iftype != NL80211_IFTYPE_AP &&
15515 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
15516 		return -EOPNOTSUPP;
15517 
15518 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
15519 	enabled = nla_get_flag(nla);
15520 
15521 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
15522 }
15523 
15524 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
15525 {
15526 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15527 	struct net_device *dev = info->user_ptr[1];
15528 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15529 	struct cfg80211_pmk_conf pmk_conf = {};
15530 	int ret;
15531 
15532 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15533 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15534 		return -EOPNOTSUPP;
15535 
15536 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15537 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15538 		return -EOPNOTSUPP;
15539 
15540 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
15541 		return -EINVAL;
15542 
15543 	wdev_lock(wdev);
15544 	if (!wdev->connected) {
15545 		ret = -ENOTCONN;
15546 		goto out;
15547 	}
15548 
15549 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15550 	if (memcmp(pmk_conf.aa, wdev->u.client.connected_addr, ETH_ALEN)) {
15551 		ret = -EINVAL;
15552 		goto out;
15553 	}
15554 
15555 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
15556 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
15557 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
15558 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
15559 		ret = -EINVAL;
15560 		goto out;
15561 	}
15562 
15563 	if (info->attrs[NL80211_ATTR_PMKR0_NAME])
15564 		pmk_conf.pmk_r0_name =
15565 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
15566 
15567 	ret = rdev_set_pmk(rdev, dev, &pmk_conf);
15568 out:
15569 	wdev_unlock(wdev);
15570 	return ret;
15571 }
15572 
15573 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
15574 {
15575 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15576 	struct net_device *dev = info->user_ptr[1];
15577 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15578 	const u8 *aa;
15579 	int ret;
15580 
15581 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15582 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15583 		return -EOPNOTSUPP;
15584 
15585 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15586 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15587 		return -EOPNOTSUPP;
15588 
15589 	if (!info->attrs[NL80211_ATTR_MAC])
15590 		return -EINVAL;
15591 
15592 	wdev_lock(wdev);
15593 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15594 	ret = rdev_del_pmk(rdev, dev, aa);
15595 	wdev_unlock(wdev);
15596 
15597 	return ret;
15598 }
15599 
15600 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
15601 {
15602 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15603 	struct net_device *dev = info->user_ptr[1];
15604 	struct cfg80211_external_auth_params params;
15605 
15606 	if (!rdev->ops->external_auth)
15607 		return -EOPNOTSUPP;
15608 
15609 	if (!info->attrs[NL80211_ATTR_SSID] &&
15610 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
15611 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
15612 		return -EINVAL;
15613 
15614 	if (!info->attrs[NL80211_ATTR_BSSID])
15615 		return -EINVAL;
15616 
15617 	if (!info->attrs[NL80211_ATTR_STATUS_CODE])
15618 		return -EINVAL;
15619 
15620 	memset(&params, 0, sizeof(params));
15621 
15622 	if (info->attrs[NL80211_ATTR_SSID]) {
15623 		params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
15624 		if (params.ssid.ssid_len == 0)
15625 			return -EINVAL;
15626 		memcpy(params.ssid.ssid,
15627 		       nla_data(info->attrs[NL80211_ATTR_SSID]),
15628 		       params.ssid.ssid_len);
15629 	}
15630 
15631 	memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
15632 	       ETH_ALEN);
15633 
15634 	params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15635 
15636 	if (info->attrs[NL80211_ATTR_PMKID])
15637 		params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
15638 
15639 	return rdev_external_auth(rdev, dev, &params);
15640 }
15641 
15642 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
15643 {
15644 	bool dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
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 	const u8 *buf;
15649 	size_t len;
15650 	u8 *dest;
15651 	u16 proto;
15652 	bool noencrypt;
15653 	u64 cookie = 0;
15654 	int link_id;
15655 	int err;
15656 
15657 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15658 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
15659 		return -EOPNOTSUPP;
15660 
15661 	if (!rdev->ops->tx_control_port)
15662 		return -EOPNOTSUPP;
15663 
15664 	if (!info->attrs[NL80211_ATTR_FRAME] ||
15665 	    !info->attrs[NL80211_ATTR_MAC] ||
15666 	    !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
15667 		GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
15668 		return -EINVAL;
15669 	}
15670 
15671 	wdev_lock(wdev);
15672 
15673 	switch (wdev->iftype) {
15674 	case NL80211_IFTYPE_AP:
15675 	case NL80211_IFTYPE_P2P_GO:
15676 	case NL80211_IFTYPE_MESH_POINT:
15677 		break;
15678 	case NL80211_IFTYPE_ADHOC:
15679 		if (wdev->u.ibss.current_bss)
15680 			break;
15681 		err = -ENOTCONN;
15682 		goto out;
15683 	case NL80211_IFTYPE_STATION:
15684 	case NL80211_IFTYPE_P2P_CLIENT:
15685 		if (wdev->connected)
15686 			break;
15687 		err = -ENOTCONN;
15688 		goto out;
15689 	default:
15690 		err = -EOPNOTSUPP;
15691 		goto out;
15692 	}
15693 
15694 	wdev_unlock(wdev);
15695 
15696 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15697 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15698 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15699 	proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
15700 	noencrypt =
15701 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
15702 
15703 	link_id = nl80211_link_id_or_invalid(info->attrs);
15704 
15705 	err = rdev_tx_control_port(rdev, dev, buf, len,
15706 				   dest, cpu_to_be16(proto), noencrypt, link_id,
15707 				   dont_wait_for_ack ? NULL : &cookie);
15708 	if (!err && !dont_wait_for_ack)
15709 		nl_set_extack_cookie_u64(info->extack, cookie);
15710 	return err;
15711  out:
15712 	wdev_unlock(wdev);
15713 	return err;
15714 }
15715 
15716 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
15717 					   struct genl_info *info)
15718 {
15719 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15720 	struct net_device *dev = info->user_ptr[1];
15721 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15722 	struct cfg80211_ftm_responder_stats ftm_stats = {};
15723 	unsigned int link_id = nl80211_link_id(info->attrs);
15724 	struct sk_buff *msg;
15725 	void *hdr;
15726 	struct nlattr *ftm_stats_attr;
15727 	int err;
15728 
15729 	if (wdev->iftype != NL80211_IFTYPE_AP ||
15730 	    !wdev->links[link_id].ap.beacon_interval)
15731 		return -EOPNOTSUPP;
15732 
15733 	err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
15734 	if (err)
15735 		return err;
15736 
15737 	if (!ftm_stats.filled)
15738 		return -ENODATA;
15739 
15740 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15741 	if (!msg)
15742 		return -ENOMEM;
15743 
15744 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
15745 			     NL80211_CMD_GET_FTM_RESPONDER_STATS);
15746 	if (!hdr)
15747 		goto nla_put_failure;
15748 
15749 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15750 		goto nla_put_failure;
15751 
15752 	ftm_stats_attr = nla_nest_start_noflag(msg,
15753 					       NL80211_ATTR_FTM_RESPONDER_STATS);
15754 	if (!ftm_stats_attr)
15755 		goto nla_put_failure;
15756 
15757 #define SET_FTM(field, name, type)					 \
15758 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15759 	    nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,		 \
15760 			     ftm_stats.field))				 \
15761 		goto nla_put_failure; } while (0)
15762 #define SET_FTM_U64(field, name)					 \
15763 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15764 	    nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,		 \
15765 			      ftm_stats.field, NL80211_FTM_STATS_PAD))	 \
15766 		goto nla_put_failure; } while (0)
15767 
15768 	SET_FTM(success_num, SUCCESS_NUM, u32);
15769 	SET_FTM(partial_num, PARTIAL_NUM, u32);
15770 	SET_FTM(failed_num, FAILED_NUM, u32);
15771 	SET_FTM(asap_num, ASAP_NUM, u32);
15772 	SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
15773 	SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
15774 	SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
15775 	SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
15776 	SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
15777 #undef SET_FTM
15778 
15779 	nla_nest_end(msg, ftm_stats_attr);
15780 
15781 	genlmsg_end(msg, hdr);
15782 	return genlmsg_reply(msg, info);
15783 
15784 nla_put_failure:
15785 	nlmsg_free(msg);
15786 	return -ENOBUFS;
15787 }
15788 
15789 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
15790 {
15791 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15792 	struct cfg80211_update_owe_info owe_info;
15793 	struct net_device *dev = info->user_ptr[1];
15794 
15795 	if (!rdev->ops->update_owe_info)
15796 		return -EOPNOTSUPP;
15797 
15798 	if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
15799 	    !info->attrs[NL80211_ATTR_MAC])
15800 		return -EINVAL;
15801 
15802 	memset(&owe_info, 0, sizeof(owe_info));
15803 	owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15804 	nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
15805 
15806 	if (info->attrs[NL80211_ATTR_IE]) {
15807 		owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
15808 		owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
15809 	}
15810 
15811 	return rdev_update_owe_info(rdev, dev, &owe_info);
15812 }
15813 
15814 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
15815 {
15816 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15817 	struct net_device *dev = info->user_ptr[1];
15818 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15819 	struct station_info sinfo = {};
15820 	const u8 *buf;
15821 	size_t len;
15822 	u8 *dest;
15823 	int err;
15824 
15825 	if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
15826 		return -EOPNOTSUPP;
15827 
15828 	if (!info->attrs[NL80211_ATTR_MAC] ||
15829 	    !info->attrs[NL80211_ATTR_FRAME]) {
15830 		GENL_SET_ERR_MSG(info, "Frame or MAC missing");
15831 		return -EINVAL;
15832 	}
15833 
15834 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
15835 		return -EOPNOTSUPP;
15836 
15837 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15838 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15839 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15840 
15841 	if (len < sizeof(struct ethhdr))
15842 		return -EINVAL;
15843 
15844 	if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
15845 	    !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
15846 		return -EINVAL;
15847 
15848 	err = rdev_get_station(rdev, dev, dest, &sinfo);
15849 	if (err)
15850 		return err;
15851 
15852 	cfg80211_sinfo_release_content(&sinfo);
15853 
15854 	return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
15855 }
15856 
15857 static int parse_tid_conf(struct cfg80211_registered_device *rdev,
15858 			  struct nlattr *attrs[], struct net_device *dev,
15859 			  struct cfg80211_tid_cfg *tid_conf,
15860 			  struct genl_info *info, const u8 *peer,
15861 			  unsigned int link_id)
15862 {
15863 	struct netlink_ext_ack *extack = info->extack;
15864 	u64 mask;
15865 	int err;
15866 
15867 	if (!attrs[NL80211_TID_CONFIG_ATTR_TIDS])
15868 		return -EINVAL;
15869 
15870 	tid_conf->config_override =
15871 			nla_get_flag(attrs[NL80211_TID_CONFIG_ATTR_OVERRIDE]);
15872 	tid_conf->tids = nla_get_u16(attrs[NL80211_TID_CONFIG_ATTR_TIDS]);
15873 
15874 	if (tid_conf->config_override) {
15875 		if (rdev->ops->reset_tid_config) {
15876 			err = rdev_reset_tid_config(rdev, dev, peer,
15877 						    tid_conf->tids);
15878 			if (err)
15879 				return err;
15880 		} else {
15881 			return -EINVAL;
15882 		}
15883 	}
15884 
15885 	if (attrs[NL80211_TID_CONFIG_ATTR_NOACK]) {
15886 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_NOACK);
15887 		tid_conf->noack =
15888 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_NOACK]);
15889 	}
15890 
15891 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]) {
15892 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT);
15893 		tid_conf->retry_short =
15894 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]);
15895 
15896 		if (tid_conf->retry_short > rdev->wiphy.max_data_retry_count)
15897 			return -EINVAL;
15898 	}
15899 
15900 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]) {
15901 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
15902 		tid_conf->retry_long =
15903 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]);
15904 
15905 		if (tid_conf->retry_long > rdev->wiphy.max_data_retry_count)
15906 			return -EINVAL;
15907 	}
15908 
15909 	if (attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]) {
15910 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
15911 		tid_conf->ampdu =
15912 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]);
15913 	}
15914 
15915 	if (attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]) {
15916 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
15917 		tid_conf->rtscts =
15918 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]);
15919 	}
15920 
15921 	if (attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]) {
15922 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
15923 		tid_conf->amsdu =
15924 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]);
15925 	}
15926 
15927 	if (attrs[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE]) {
15928 		u32 idx = NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE, attr;
15929 
15930 		tid_conf->txrate_type = nla_get_u8(attrs[idx]);
15931 
15932 		if (tid_conf->txrate_type != NL80211_TX_RATE_AUTOMATIC) {
15933 			attr = NL80211_TID_CONFIG_ATTR_TX_RATE;
15934 			err = nl80211_parse_tx_bitrate_mask(info, attrs, attr,
15935 						    &tid_conf->txrate_mask, dev,
15936 						    true, link_id);
15937 			if (err)
15938 				return err;
15939 
15940 			tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE);
15941 		}
15942 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
15943 	}
15944 
15945 	if (peer)
15946 		mask = rdev->wiphy.tid_config_support.peer;
15947 	else
15948 		mask = rdev->wiphy.tid_config_support.vif;
15949 
15950 	if (tid_conf->mask & ~mask) {
15951 		NL_SET_ERR_MSG(extack, "unsupported TID configuration");
15952 		return -ENOTSUPP;
15953 	}
15954 
15955 	return 0;
15956 }
15957 
15958 static int nl80211_set_tid_config(struct sk_buff *skb,
15959 				  struct genl_info *info)
15960 {
15961 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15962 	struct nlattr *attrs[NL80211_TID_CONFIG_ATTR_MAX + 1];
15963 	unsigned int link_id = nl80211_link_id(info->attrs);
15964 	struct net_device *dev = info->user_ptr[1];
15965 	struct cfg80211_tid_config *tid_config;
15966 	struct nlattr *tid;
15967 	int conf_idx = 0, rem_conf;
15968 	int ret = -EINVAL;
15969 	u32 num_conf = 0;
15970 
15971 	if (!info->attrs[NL80211_ATTR_TID_CONFIG])
15972 		return -EINVAL;
15973 
15974 	if (!rdev->ops->set_tid_config)
15975 		return -EOPNOTSUPP;
15976 
15977 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
15978 			    rem_conf)
15979 		num_conf++;
15980 
15981 	tid_config = kzalloc(struct_size(tid_config, tid_conf, num_conf),
15982 			     GFP_KERNEL);
15983 	if (!tid_config)
15984 		return -ENOMEM;
15985 
15986 	tid_config->n_tid_conf = num_conf;
15987 
15988 	if (info->attrs[NL80211_ATTR_MAC])
15989 		tid_config->peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15990 
15991 	wdev_lock(dev->ieee80211_ptr);
15992 
15993 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
15994 			    rem_conf) {
15995 		ret = nla_parse_nested(attrs, NL80211_TID_CONFIG_ATTR_MAX,
15996 				       tid, NULL, NULL);
15997 
15998 		if (ret)
15999 			goto bad_tid_conf;
16000 
16001 		ret = parse_tid_conf(rdev, attrs, dev,
16002 				     &tid_config->tid_conf[conf_idx],
16003 				     info, tid_config->peer, link_id);
16004 		if (ret)
16005 			goto bad_tid_conf;
16006 
16007 		conf_idx++;
16008 	}
16009 
16010 	ret = rdev_set_tid_config(rdev, dev, tid_config);
16011 
16012 bad_tid_conf:
16013 	kfree(tid_config);
16014 	wdev_unlock(dev->ieee80211_ptr);
16015 	return ret;
16016 }
16017 
16018 static int nl80211_color_change(struct sk_buff *skb, struct genl_info *info)
16019 {
16020 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16021 	struct cfg80211_color_change_settings params = {};
16022 	struct net_device *dev = info->user_ptr[1];
16023 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16024 	struct nlattr **tb;
16025 	u16 offset;
16026 	int err;
16027 
16028 	if (!rdev->ops->color_change)
16029 		return -EOPNOTSUPP;
16030 
16031 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
16032 				     NL80211_EXT_FEATURE_BSS_COLOR))
16033 		return -EOPNOTSUPP;
16034 
16035 	if (wdev->iftype != NL80211_IFTYPE_AP)
16036 		return -EOPNOTSUPP;
16037 
16038 	if (!info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT] ||
16039 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR] ||
16040 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS])
16041 		return -EINVAL;
16042 
16043 	params.count = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT]);
16044 	params.color = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR]);
16045 
16046 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_next,
16047 				   info->extack);
16048 	if (err)
16049 		return err;
16050 
16051 	tb = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*tb), GFP_KERNEL);
16052 	if (!tb)
16053 		return -ENOMEM;
16054 
16055 	err = nla_parse_nested(tb, NL80211_ATTR_MAX,
16056 			       info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS],
16057 			       nl80211_policy, info->extack);
16058 	if (err)
16059 		goto out;
16060 
16061 	err = nl80211_parse_beacon(rdev, tb, &params.beacon_color_change,
16062 				   info->extack);
16063 	if (err)
16064 		goto out;
16065 
16066 	if (!tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
16067 		err = -EINVAL;
16068 		goto out;
16069 	}
16070 
16071 	if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) != sizeof(u16)) {
16072 		err = -EINVAL;
16073 		goto out;
16074 	}
16075 
16076 	offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
16077 	if (offset >= params.beacon_color_change.tail_len) {
16078 		err = -EINVAL;
16079 		goto out;
16080 	}
16081 
16082 	if (params.beacon_color_change.tail[offset] != params.count) {
16083 		err = -EINVAL;
16084 		goto out;
16085 	}
16086 
16087 	params.counter_offset_beacon = offset;
16088 
16089 	if (tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
16090 		if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) !=
16091 		    sizeof(u16)) {
16092 			err = -EINVAL;
16093 			goto out;
16094 		}
16095 
16096 		offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
16097 		if (offset >= params.beacon_color_change.probe_resp_len) {
16098 			err = -EINVAL;
16099 			goto out;
16100 		}
16101 
16102 		if (params.beacon_color_change.probe_resp[offset] !=
16103 		    params.count) {
16104 			err = -EINVAL;
16105 			goto out;
16106 		}
16107 
16108 		params.counter_offset_presp = offset;
16109 	}
16110 
16111 	wdev_lock(wdev);
16112 	err = rdev_color_change(rdev, dev, &params);
16113 	wdev_unlock(wdev);
16114 
16115 out:
16116 	kfree(params.beacon_next.mbssid_ies);
16117 	kfree(params.beacon_color_change.mbssid_ies);
16118 	kfree(params.beacon_next.rnr_ies);
16119 	kfree(params.beacon_color_change.rnr_ies);
16120 	kfree(tb);
16121 	return err;
16122 }
16123 
16124 static int nl80211_set_fils_aad(struct sk_buff *skb,
16125 				struct genl_info *info)
16126 {
16127 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16128 	struct net_device *dev = info->user_ptr[1];
16129 	struct cfg80211_fils_aad fils_aad = {};
16130 	u8 *nonces;
16131 
16132 	if (!info->attrs[NL80211_ATTR_MAC] ||
16133 	    !info->attrs[NL80211_ATTR_FILS_KEK] ||
16134 	    !info->attrs[NL80211_ATTR_FILS_NONCES])
16135 		return -EINVAL;
16136 
16137 	fils_aad.macaddr = nla_data(info->attrs[NL80211_ATTR_MAC]);
16138 	fils_aad.kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
16139 	fils_aad.kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
16140 	nonces = nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
16141 	fils_aad.snonce = nonces;
16142 	fils_aad.anonce = nonces + FILS_NONCE_LEN;
16143 
16144 	return rdev_set_fils_aad(rdev, dev, &fils_aad);
16145 }
16146 
16147 static int nl80211_add_link(struct sk_buff *skb, struct genl_info *info)
16148 {
16149 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16150 	unsigned int link_id = nl80211_link_id(info->attrs);
16151 	struct net_device *dev = info->user_ptr[1];
16152 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16153 	int ret;
16154 
16155 	if (!(wdev->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
16156 		return -EINVAL;
16157 
16158 	switch (wdev->iftype) {
16159 	case NL80211_IFTYPE_AP:
16160 		break;
16161 	default:
16162 		return -EINVAL;
16163 	}
16164 
16165 	if (!info->attrs[NL80211_ATTR_MAC] ||
16166 	    !is_valid_ether_addr(nla_data(info->attrs[NL80211_ATTR_MAC])))
16167 		return -EINVAL;
16168 
16169 	wdev_lock(wdev);
16170 	wdev->valid_links |= BIT(link_id);
16171 	ether_addr_copy(wdev->links[link_id].addr,
16172 			nla_data(info->attrs[NL80211_ATTR_MAC]));
16173 
16174 	ret = rdev_add_intf_link(rdev, wdev, link_id);
16175 	if (ret) {
16176 		wdev->valid_links &= ~BIT(link_id);
16177 		eth_zero_addr(wdev->links[link_id].addr);
16178 	}
16179 	wdev_unlock(wdev);
16180 
16181 	return ret;
16182 }
16183 
16184 static int nl80211_remove_link(struct sk_buff *skb, struct genl_info *info)
16185 {
16186 	unsigned int link_id = nl80211_link_id(info->attrs);
16187 	struct net_device *dev = info->user_ptr[1];
16188 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16189 
16190 	/* cannot remove if there's no link */
16191 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
16192 		return -EINVAL;
16193 
16194 	switch (wdev->iftype) {
16195 	case NL80211_IFTYPE_AP:
16196 		break;
16197 	default:
16198 		return -EINVAL;
16199 	}
16200 
16201 	wdev_lock(wdev);
16202 	cfg80211_remove_link(wdev, link_id);
16203 	wdev_unlock(wdev);
16204 
16205 	return 0;
16206 }
16207 
16208 static int
16209 nl80211_add_mod_link_station(struct sk_buff *skb, struct genl_info *info,
16210 			     bool add)
16211 {
16212 	struct link_station_parameters params = {};
16213 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16214 	struct net_device *dev = info->user_ptr[1];
16215 	int err;
16216 
16217 	if ((add && !rdev->ops->add_link_station) ||
16218 	    (!add && !rdev->ops->mod_link_station))
16219 		return -EOPNOTSUPP;
16220 
16221 	if (add && !info->attrs[NL80211_ATTR_MAC])
16222 		return -EINVAL;
16223 
16224 	if (!info->attrs[NL80211_ATTR_MLD_ADDR])
16225 		return -EINVAL;
16226 
16227 	if (add && !info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
16228 		return -EINVAL;
16229 
16230 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
16231 
16232 	if (info->attrs[NL80211_ATTR_MAC]) {
16233 		params.link_mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
16234 		if (!is_valid_ether_addr(params.link_mac))
16235 			return -EINVAL;
16236 	}
16237 
16238 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
16239 		return -EINVAL;
16240 
16241 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
16242 
16243 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
16244 		params.supported_rates =
16245 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
16246 		params.supported_rates_len =
16247 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
16248 	}
16249 
16250 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
16251 		params.ht_capa =
16252 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
16253 
16254 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
16255 		params.vht_capa =
16256 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
16257 
16258 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
16259 		params.he_capa =
16260 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
16261 		params.he_capa_len =
16262 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
16263 
16264 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
16265 			params.eht_capa =
16266 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
16267 			params.eht_capa_len =
16268 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
16269 
16270 			if (!ieee80211_eht_capa_size_ok((const u8 *)params.he_capa,
16271 							(const u8 *)params.eht_capa,
16272 							params.eht_capa_len,
16273 							false))
16274 				return -EINVAL;
16275 		}
16276 	}
16277 
16278 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
16279 		params.he_6ghz_capa =
16280 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
16281 
16282 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
16283 		params.opmode_notif_used = true;
16284 		params.opmode_notif =
16285 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
16286 	}
16287 
16288 	err = nl80211_parse_sta_txpower_setting(info, &params.txpwr,
16289 						&params.txpwr_set);
16290 	if (err)
16291 		return err;
16292 
16293 	wdev_lock(dev->ieee80211_ptr);
16294 	if (add)
16295 		err = rdev_add_link_station(rdev, dev, &params);
16296 	else
16297 		err = rdev_mod_link_station(rdev, dev, &params);
16298 	wdev_unlock(dev->ieee80211_ptr);
16299 
16300 	return err;
16301 }
16302 
16303 static int
16304 nl80211_add_link_station(struct sk_buff *skb, struct genl_info *info)
16305 {
16306 	return nl80211_add_mod_link_station(skb, info, true);
16307 }
16308 
16309 static int
16310 nl80211_modify_link_station(struct sk_buff *skb, struct genl_info *info)
16311 {
16312 	return nl80211_add_mod_link_station(skb, info, false);
16313 }
16314 
16315 static int
16316 nl80211_remove_link_station(struct sk_buff *skb, struct genl_info *info)
16317 {
16318 	struct link_station_del_parameters params = {};
16319 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16320 	struct net_device *dev = info->user_ptr[1];
16321 	int ret;
16322 
16323 	if (!rdev->ops->del_link_station)
16324 		return -EOPNOTSUPP;
16325 
16326 	if (!info->attrs[NL80211_ATTR_MLD_ADDR] ||
16327 	    !info->attrs[NL80211_ATTR_MLO_LINK_ID])
16328 		return -EINVAL;
16329 
16330 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
16331 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
16332 
16333 	wdev_lock(dev->ieee80211_ptr);
16334 	ret = rdev_del_link_station(rdev, dev, &params);
16335 	wdev_unlock(dev->ieee80211_ptr);
16336 
16337 	return ret;
16338 }
16339 
16340 static int nl80211_set_hw_timestamp(struct sk_buff *skb,
16341 				    struct genl_info *info)
16342 {
16343 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16344 	struct net_device *dev = info->user_ptr[1];
16345 	struct cfg80211_set_hw_timestamp hwts = {};
16346 
16347 	if (!rdev->wiphy.hw_timestamp_max_peers)
16348 		return -EOPNOTSUPP;
16349 
16350 	if (!info->attrs[NL80211_ATTR_MAC] &&
16351 	    rdev->wiphy.hw_timestamp_max_peers != CFG80211_HW_TIMESTAMP_ALL_PEERS)
16352 		return -EOPNOTSUPP;
16353 
16354 	if (info->attrs[NL80211_ATTR_MAC])
16355 		hwts.macaddr = nla_data(info->attrs[NL80211_ATTR_MAC]);
16356 
16357 	hwts.enable =
16358 		nla_get_flag(info->attrs[NL80211_ATTR_HW_TIMESTAMP_ENABLED]);
16359 
16360 	return rdev_set_hw_timestamp(rdev, dev, &hwts);
16361 }
16362 
16363 #define NL80211_FLAG_NEED_WIPHY		0x01
16364 #define NL80211_FLAG_NEED_NETDEV	0x02
16365 #define NL80211_FLAG_NEED_RTNL		0x04
16366 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
16367 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
16368 					 NL80211_FLAG_CHECK_NETDEV_UP)
16369 #define NL80211_FLAG_NEED_WDEV		0x10
16370 /* If a netdev is associated, it must be UP, P2P must be started */
16371 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
16372 					 NL80211_FLAG_CHECK_NETDEV_UP)
16373 #define NL80211_FLAG_CLEAR_SKB		0x20
16374 #define NL80211_FLAG_NO_WIPHY_MTX	0x40
16375 #define NL80211_FLAG_MLO_VALID_LINK_ID	0x80
16376 #define NL80211_FLAG_MLO_UNSUPPORTED	0x100
16377 
16378 #define INTERNAL_FLAG_SELECTORS(__sel)			\
16379 	SELECTOR(__sel, NONE, 0) /* must be first */	\
16380 	SELECTOR(__sel, WIPHY,				\
16381 		 NL80211_FLAG_NEED_WIPHY)		\
16382 	SELECTOR(__sel, WDEV,				\
16383 		 NL80211_FLAG_NEED_WDEV)		\
16384 	SELECTOR(__sel, NETDEV,				\
16385 		 NL80211_FLAG_NEED_NETDEV)		\
16386 	SELECTOR(__sel, NETDEV_LINK,			\
16387 		 NL80211_FLAG_NEED_NETDEV |		\
16388 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16389 	SELECTOR(__sel, NETDEV_NO_MLO,			\
16390 		 NL80211_FLAG_NEED_NETDEV |		\
16391 		 NL80211_FLAG_MLO_UNSUPPORTED)	\
16392 	SELECTOR(__sel, WIPHY_RTNL,			\
16393 		 NL80211_FLAG_NEED_WIPHY |		\
16394 		 NL80211_FLAG_NEED_RTNL)		\
16395 	SELECTOR(__sel, WIPHY_RTNL_NOMTX,		\
16396 		 NL80211_FLAG_NEED_WIPHY |		\
16397 		 NL80211_FLAG_NEED_RTNL |		\
16398 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16399 	SELECTOR(__sel, WDEV_RTNL,			\
16400 		 NL80211_FLAG_NEED_WDEV |		\
16401 		 NL80211_FLAG_NEED_RTNL)		\
16402 	SELECTOR(__sel, NETDEV_RTNL,			\
16403 		 NL80211_FLAG_NEED_NETDEV |		\
16404 		 NL80211_FLAG_NEED_RTNL)		\
16405 	SELECTOR(__sel, NETDEV_UP,			\
16406 		 NL80211_FLAG_NEED_NETDEV_UP)		\
16407 	SELECTOR(__sel, NETDEV_UP_LINK,			\
16408 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16409 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16410 	SELECTOR(__sel, NETDEV_UP_NO_MLO,		\
16411 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16412 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16413 	SELECTOR(__sel, NETDEV_UP_NO_MLO_CLEAR,		\
16414 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16415 		 NL80211_FLAG_CLEAR_SKB |		\
16416 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16417 	SELECTOR(__sel, NETDEV_UP_NOTMX,		\
16418 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16419 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16420 	SELECTOR(__sel, NETDEV_UP_NOTMX_NOMLO,		\
16421 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16422 		 NL80211_FLAG_NO_WIPHY_MTX |		\
16423 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16424 	SELECTOR(__sel, NETDEV_UP_CLEAR,		\
16425 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16426 		 NL80211_FLAG_CLEAR_SKB)		\
16427 	SELECTOR(__sel, WDEV_UP,			\
16428 		 NL80211_FLAG_NEED_WDEV_UP)		\
16429 	SELECTOR(__sel, WDEV_UP_LINK,			\
16430 		 NL80211_FLAG_NEED_WDEV_UP |		\
16431 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16432 	SELECTOR(__sel, WDEV_UP_RTNL,			\
16433 		 NL80211_FLAG_NEED_WDEV_UP |		\
16434 		 NL80211_FLAG_NEED_RTNL)		\
16435 	SELECTOR(__sel, WIPHY_CLEAR,			\
16436 		 NL80211_FLAG_NEED_WIPHY |		\
16437 		 NL80211_FLAG_CLEAR_SKB)
16438 
16439 enum nl80211_internal_flags_selector {
16440 #define SELECTOR(_, name, value)	NL80211_IFL_SEL_##name,
16441 	INTERNAL_FLAG_SELECTORS(_)
16442 #undef SELECTOR
16443 };
16444 
16445 static u32 nl80211_internal_flags[] = {
16446 #define SELECTOR(_, name, value)	[NL80211_IFL_SEL_##name] = value,
16447 	INTERNAL_FLAG_SELECTORS(_)
16448 #undef SELECTOR
16449 };
16450 
16451 static int nl80211_pre_doit(const struct genl_split_ops *ops,
16452 			    struct sk_buff *skb,
16453 			    struct genl_info *info)
16454 {
16455 	struct cfg80211_registered_device *rdev = NULL;
16456 	struct wireless_dev *wdev = NULL;
16457 	struct net_device *dev = NULL;
16458 	u32 internal_flags;
16459 	int err;
16460 
16461 	if (WARN_ON(ops->internal_flags >= ARRAY_SIZE(nl80211_internal_flags)))
16462 		return -EINVAL;
16463 
16464 	internal_flags = nl80211_internal_flags[ops->internal_flags];
16465 
16466 	rtnl_lock();
16467 	if (internal_flags & NL80211_FLAG_NEED_WIPHY) {
16468 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
16469 		if (IS_ERR(rdev)) {
16470 			err = PTR_ERR(rdev);
16471 			goto out_unlock;
16472 		}
16473 		info->user_ptr[0] = rdev;
16474 	} else if (internal_flags & NL80211_FLAG_NEED_NETDEV ||
16475 		   internal_flags & NL80211_FLAG_NEED_WDEV) {
16476 		wdev = __cfg80211_wdev_from_attrs(NULL, genl_info_net(info),
16477 						  info->attrs);
16478 		if (IS_ERR(wdev)) {
16479 			err = PTR_ERR(wdev);
16480 			goto out_unlock;
16481 		}
16482 
16483 		dev = wdev->netdev;
16484 		dev_hold(dev);
16485 		rdev = wiphy_to_rdev(wdev->wiphy);
16486 
16487 		if (internal_flags & NL80211_FLAG_NEED_NETDEV) {
16488 			if (!dev) {
16489 				err = -EINVAL;
16490 				goto out_unlock;
16491 			}
16492 
16493 			info->user_ptr[1] = dev;
16494 		} else {
16495 			info->user_ptr[1] = wdev;
16496 		}
16497 
16498 		if (internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
16499 		    !wdev_running(wdev)) {
16500 			err = -ENETDOWN;
16501 			goto out_unlock;
16502 		}
16503 
16504 		info->user_ptr[0] = rdev;
16505 	}
16506 
16507 	if (internal_flags & NL80211_FLAG_MLO_VALID_LINK_ID) {
16508 		struct nlattr *link_id = info->attrs[NL80211_ATTR_MLO_LINK_ID];
16509 
16510 		if (!wdev) {
16511 			err = -EINVAL;
16512 			goto out_unlock;
16513 		}
16514 
16515 		/* MLO -> require valid link ID */
16516 		if (wdev->valid_links &&
16517 		    (!link_id ||
16518 		     !(wdev->valid_links & BIT(nla_get_u8(link_id))))) {
16519 			err = -EINVAL;
16520 			goto out_unlock;
16521 		}
16522 
16523 		/* non-MLO -> no link ID attribute accepted */
16524 		if (!wdev->valid_links && link_id) {
16525 			err = -EINVAL;
16526 			goto out_unlock;
16527 		}
16528 	}
16529 
16530 	if (internal_flags & NL80211_FLAG_MLO_UNSUPPORTED) {
16531 		if (info->attrs[NL80211_ATTR_MLO_LINK_ID] ||
16532 		    (wdev && wdev->valid_links)) {
16533 			err = -EINVAL;
16534 			goto out_unlock;
16535 		}
16536 	}
16537 
16538 	if (rdev && !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16539 		wiphy_lock(&rdev->wiphy);
16540 		/* we keep the mutex locked until post_doit */
16541 		__release(&rdev->wiphy.mtx);
16542 	}
16543 	if (!(internal_flags & NL80211_FLAG_NEED_RTNL))
16544 		rtnl_unlock();
16545 
16546 	return 0;
16547 out_unlock:
16548 	rtnl_unlock();
16549 	dev_put(dev);
16550 	return err;
16551 }
16552 
16553 static void nl80211_post_doit(const struct genl_split_ops *ops,
16554 			      struct sk_buff *skb,
16555 			      struct genl_info *info)
16556 {
16557 	u32 internal_flags = nl80211_internal_flags[ops->internal_flags];
16558 
16559 	if (info->user_ptr[1]) {
16560 		if (internal_flags & NL80211_FLAG_NEED_WDEV) {
16561 			struct wireless_dev *wdev = info->user_ptr[1];
16562 
16563 			dev_put(wdev->netdev);
16564 		} else {
16565 			dev_put(info->user_ptr[1]);
16566 		}
16567 	}
16568 
16569 	if (info->user_ptr[0] &&
16570 	    !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16571 		struct cfg80211_registered_device *rdev = info->user_ptr[0];
16572 
16573 		/* we kept the mutex locked since pre_doit */
16574 		__acquire(&rdev->wiphy.mtx);
16575 		wiphy_unlock(&rdev->wiphy);
16576 	}
16577 
16578 	if (internal_flags & NL80211_FLAG_NEED_RTNL)
16579 		rtnl_unlock();
16580 
16581 	/* If needed, clear the netlink message payload from the SKB
16582 	 * as it might contain key data that shouldn't stick around on
16583 	 * the heap after the SKB is freed. The netlink message header
16584 	 * is still needed for further processing, so leave it intact.
16585 	 */
16586 	if (internal_flags & NL80211_FLAG_CLEAR_SKB) {
16587 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
16588 
16589 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
16590 	}
16591 }
16592 
16593 static int nl80211_set_sar_sub_specs(struct cfg80211_registered_device *rdev,
16594 				     struct cfg80211_sar_specs *sar_specs,
16595 				     struct nlattr *spec[], int index)
16596 {
16597 	u32 range_index, i;
16598 
16599 	if (!sar_specs || !spec)
16600 		return -EINVAL;
16601 
16602 	if (!spec[NL80211_SAR_ATTR_SPECS_POWER] ||
16603 	    !spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX])
16604 		return -EINVAL;
16605 
16606 	range_index = nla_get_u32(spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX]);
16607 
16608 	/* check if range_index exceeds num_freq_ranges */
16609 	if (range_index >= rdev->wiphy.sar_capa->num_freq_ranges)
16610 		return -EINVAL;
16611 
16612 	/* check if range_index duplicates */
16613 	for (i = 0; i < index; i++) {
16614 		if (sar_specs->sub_specs[i].freq_range_index == range_index)
16615 			return -EINVAL;
16616 	}
16617 
16618 	sar_specs->sub_specs[index].power =
16619 		nla_get_s32(spec[NL80211_SAR_ATTR_SPECS_POWER]);
16620 
16621 	sar_specs->sub_specs[index].freq_range_index = range_index;
16622 
16623 	return 0;
16624 }
16625 
16626 static int nl80211_set_sar_specs(struct sk_buff *skb, struct genl_info *info)
16627 {
16628 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16629 	struct nlattr *spec[NL80211_SAR_ATTR_SPECS_MAX + 1];
16630 	struct nlattr *tb[NL80211_SAR_ATTR_MAX + 1];
16631 	struct cfg80211_sar_specs *sar_spec;
16632 	enum nl80211_sar_type type;
16633 	struct nlattr *spec_list;
16634 	u32 specs;
16635 	int rem, err;
16636 
16637 	if (!rdev->wiphy.sar_capa || !rdev->ops->set_sar_specs)
16638 		return -EOPNOTSUPP;
16639 
16640 	if (!info->attrs[NL80211_ATTR_SAR_SPEC])
16641 		return -EINVAL;
16642 
16643 	nla_parse_nested(tb, NL80211_SAR_ATTR_MAX,
16644 			 info->attrs[NL80211_ATTR_SAR_SPEC],
16645 			 NULL, NULL);
16646 
16647 	if (!tb[NL80211_SAR_ATTR_TYPE] || !tb[NL80211_SAR_ATTR_SPECS])
16648 		return -EINVAL;
16649 
16650 	type = nla_get_u32(tb[NL80211_SAR_ATTR_TYPE]);
16651 	if (type != rdev->wiphy.sar_capa->type)
16652 		return -EINVAL;
16653 
16654 	specs = 0;
16655 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem)
16656 		specs++;
16657 
16658 	if (specs > rdev->wiphy.sar_capa->num_freq_ranges)
16659 		return -EINVAL;
16660 
16661 	sar_spec = kzalloc(struct_size(sar_spec, sub_specs, specs), GFP_KERNEL);
16662 	if (!sar_spec)
16663 		return -ENOMEM;
16664 
16665 	sar_spec->type = type;
16666 	specs = 0;
16667 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem) {
16668 		nla_parse_nested(spec, NL80211_SAR_ATTR_SPECS_MAX,
16669 				 spec_list, NULL, NULL);
16670 
16671 		switch (type) {
16672 		case NL80211_SAR_TYPE_POWER:
16673 			if (nl80211_set_sar_sub_specs(rdev, sar_spec,
16674 						      spec, specs)) {
16675 				err = -EINVAL;
16676 				goto error;
16677 			}
16678 			break;
16679 		default:
16680 			err = -EINVAL;
16681 			goto error;
16682 		}
16683 		specs++;
16684 	}
16685 
16686 	sar_spec->num_sub_specs = specs;
16687 
16688 	rdev->cur_cmd_info = info;
16689 	err = rdev_set_sar_specs(rdev, sar_spec);
16690 	rdev->cur_cmd_info = NULL;
16691 error:
16692 	kfree(sar_spec);
16693 	return err;
16694 }
16695 
16696 #define SELECTOR(__sel, name, value) \
16697 	((__sel) == (value)) ? NL80211_IFL_SEL_##name :
16698 int __missing_selector(void);
16699 #define IFLAGS(__val) INTERNAL_FLAG_SELECTORS(__val) __missing_selector()
16700 
16701 static const struct genl_ops nl80211_ops[] = {
16702 	{
16703 		.cmd = NL80211_CMD_GET_WIPHY,
16704 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16705 		.doit = nl80211_get_wiphy,
16706 		.dumpit = nl80211_dump_wiphy,
16707 		.done = nl80211_dump_wiphy_done,
16708 		/* can be retrieved by unprivileged users */
16709 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16710 	},
16711 };
16712 
16713 static const struct genl_small_ops nl80211_small_ops[] = {
16714 	{
16715 		.cmd = NL80211_CMD_SET_WIPHY,
16716 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16717 		.doit = nl80211_set_wiphy,
16718 		.flags = GENL_UNS_ADMIN_PERM,
16719 	},
16720 	{
16721 		.cmd = NL80211_CMD_GET_INTERFACE,
16722 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16723 		.doit = nl80211_get_interface,
16724 		.dumpit = nl80211_dump_interface,
16725 		/* can be retrieved by unprivileged users */
16726 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
16727 	},
16728 	{
16729 		.cmd = NL80211_CMD_SET_INTERFACE,
16730 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16731 		.doit = nl80211_set_interface,
16732 		.flags = GENL_UNS_ADMIN_PERM,
16733 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16734 					 NL80211_FLAG_NEED_RTNL),
16735 	},
16736 	{
16737 		.cmd = NL80211_CMD_NEW_INTERFACE,
16738 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16739 		.doit = nl80211_new_interface,
16740 		.flags = GENL_UNS_ADMIN_PERM,
16741 		.internal_flags =
16742 			IFLAGS(NL80211_FLAG_NEED_WIPHY |
16743 			       NL80211_FLAG_NEED_RTNL |
16744 			       /* we take the wiphy mutex later ourselves */
16745 			       NL80211_FLAG_NO_WIPHY_MTX),
16746 	},
16747 	{
16748 		.cmd = NL80211_CMD_DEL_INTERFACE,
16749 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16750 		.doit = nl80211_del_interface,
16751 		.flags = GENL_UNS_ADMIN_PERM,
16752 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
16753 					 NL80211_FLAG_NEED_RTNL),
16754 	},
16755 	{
16756 		.cmd = NL80211_CMD_GET_KEY,
16757 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16758 		.doit = nl80211_get_key,
16759 		.flags = GENL_UNS_ADMIN_PERM,
16760 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16761 	},
16762 	{
16763 		.cmd = NL80211_CMD_SET_KEY,
16764 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16765 		.doit = nl80211_set_key,
16766 		.flags = GENL_UNS_ADMIN_PERM,
16767 		/* cannot use NL80211_FLAG_MLO_VALID_LINK_ID, depends on key */
16768 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16769 					 NL80211_FLAG_CLEAR_SKB),
16770 	},
16771 	{
16772 		.cmd = NL80211_CMD_NEW_KEY,
16773 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16774 		.doit = nl80211_new_key,
16775 		.flags = GENL_UNS_ADMIN_PERM,
16776 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16777 					 NL80211_FLAG_CLEAR_SKB),
16778 	},
16779 	{
16780 		.cmd = NL80211_CMD_DEL_KEY,
16781 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16782 		.doit = nl80211_del_key,
16783 		.flags = GENL_UNS_ADMIN_PERM,
16784 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16785 	},
16786 	{
16787 		.cmd = NL80211_CMD_SET_BEACON,
16788 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16789 		.flags = GENL_UNS_ADMIN_PERM,
16790 		.doit = nl80211_set_beacon,
16791 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16792 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16793 	},
16794 	{
16795 		.cmd = NL80211_CMD_START_AP,
16796 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16797 		.flags = GENL_UNS_ADMIN_PERM,
16798 		.doit = nl80211_start_ap,
16799 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16800 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16801 	},
16802 	{
16803 		.cmd = NL80211_CMD_STOP_AP,
16804 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16805 		.flags = GENL_UNS_ADMIN_PERM,
16806 		.doit = nl80211_stop_ap,
16807 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16808 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16809 	},
16810 	{
16811 		.cmd = NL80211_CMD_GET_STATION,
16812 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16813 		.doit = nl80211_get_station,
16814 		.dumpit = nl80211_dump_station,
16815 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16816 	},
16817 	{
16818 		.cmd = NL80211_CMD_SET_STATION,
16819 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16820 		.doit = nl80211_set_station,
16821 		.flags = GENL_UNS_ADMIN_PERM,
16822 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16823 	},
16824 	{
16825 		.cmd = NL80211_CMD_NEW_STATION,
16826 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16827 		.doit = nl80211_new_station,
16828 		.flags = GENL_UNS_ADMIN_PERM,
16829 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16830 	},
16831 	{
16832 		.cmd = NL80211_CMD_DEL_STATION,
16833 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16834 		.doit = nl80211_del_station,
16835 		.flags = GENL_UNS_ADMIN_PERM,
16836 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16837 	},
16838 	{
16839 		.cmd = NL80211_CMD_GET_MPATH,
16840 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16841 		.doit = nl80211_get_mpath,
16842 		.dumpit = nl80211_dump_mpath,
16843 		.flags = GENL_UNS_ADMIN_PERM,
16844 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16845 	},
16846 	{
16847 		.cmd = NL80211_CMD_GET_MPP,
16848 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16849 		.doit = nl80211_get_mpp,
16850 		.dumpit = nl80211_dump_mpp,
16851 		.flags = GENL_UNS_ADMIN_PERM,
16852 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16853 	},
16854 	{
16855 		.cmd = NL80211_CMD_SET_MPATH,
16856 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16857 		.doit = nl80211_set_mpath,
16858 		.flags = GENL_UNS_ADMIN_PERM,
16859 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16860 	},
16861 	{
16862 		.cmd = NL80211_CMD_NEW_MPATH,
16863 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16864 		.doit = nl80211_new_mpath,
16865 		.flags = GENL_UNS_ADMIN_PERM,
16866 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16867 	},
16868 	{
16869 		.cmd = NL80211_CMD_DEL_MPATH,
16870 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16871 		.doit = nl80211_del_mpath,
16872 		.flags = GENL_UNS_ADMIN_PERM,
16873 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16874 	},
16875 	{
16876 		.cmd = NL80211_CMD_SET_BSS,
16877 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16878 		.doit = nl80211_set_bss,
16879 		.flags = GENL_UNS_ADMIN_PERM,
16880 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16881 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16882 	},
16883 	{
16884 		.cmd = NL80211_CMD_GET_REG,
16885 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16886 		.doit = nl80211_get_reg_do,
16887 		.dumpit = nl80211_get_reg_dump,
16888 		/* can be retrieved by unprivileged users */
16889 	},
16890 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
16891 	{
16892 		.cmd = NL80211_CMD_SET_REG,
16893 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16894 		.doit = nl80211_set_reg,
16895 		.flags = GENL_ADMIN_PERM,
16896 	},
16897 #endif
16898 	{
16899 		.cmd = NL80211_CMD_REQ_SET_REG,
16900 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16901 		.doit = nl80211_req_set_reg,
16902 		.flags = GENL_ADMIN_PERM,
16903 	},
16904 	{
16905 		.cmd = NL80211_CMD_RELOAD_REGDB,
16906 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16907 		.doit = nl80211_reload_regdb,
16908 		.flags = GENL_ADMIN_PERM,
16909 	},
16910 	{
16911 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
16912 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16913 		.doit = nl80211_get_mesh_config,
16914 		/* can be retrieved by unprivileged users */
16915 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16916 	},
16917 	{
16918 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
16919 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16920 		.doit = nl80211_update_mesh_config,
16921 		.flags = GENL_UNS_ADMIN_PERM,
16922 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16923 	},
16924 	{
16925 		.cmd = NL80211_CMD_TRIGGER_SCAN,
16926 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16927 		.doit = nl80211_trigger_scan,
16928 		.flags = GENL_UNS_ADMIN_PERM,
16929 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16930 	},
16931 	{
16932 		.cmd = NL80211_CMD_ABORT_SCAN,
16933 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16934 		.doit = nl80211_abort_scan,
16935 		.flags = GENL_UNS_ADMIN_PERM,
16936 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16937 	},
16938 	{
16939 		.cmd = NL80211_CMD_GET_SCAN,
16940 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16941 		.dumpit = nl80211_dump_scan,
16942 	},
16943 	{
16944 		.cmd = NL80211_CMD_START_SCHED_SCAN,
16945 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16946 		.doit = nl80211_start_sched_scan,
16947 		.flags = GENL_UNS_ADMIN_PERM,
16948 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16949 	},
16950 	{
16951 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
16952 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16953 		.doit = nl80211_stop_sched_scan,
16954 		.flags = GENL_UNS_ADMIN_PERM,
16955 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16956 	},
16957 	{
16958 		.cmd = NL80211_CMD_AUTHENTICATE,
16959 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16960 		.doit = nl80211_authenticate,
16961 		.flags = GENL_UNS_ADMIN_PERM,
16962 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16963 					 NL80211_FLAG_CLEAR_SKB),
16964 	},
16965 	{
16966 		.cmd = NL80211_CMD_ASSOCIATE,
16967 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16968 		.doit = nl80211_associate,
16969 		.flags = GENL_UNS_ADMIN_PERM,
16970 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16971 					 NL80211_FLAG_CLEAR_SKB),
16972 	},
16973 	{
16974 		.cmd = NL80211_CMD_DEAUTHENTICATE,
16975 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16976 		.doit = nl80211_deauthenticate,
16977 		.flags = GENL_UNS_ADMIN_PERM,
16978 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16979 	},
16980 	{
16981 		.cmd = NL80211_CMD_DISASSOCIATE,
16982 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16983 		.doit = nl80211_disassociate,
16984 		.flags = GENL_UNS_ADMIN_PERM,
16985 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16986 	},
16987 	{
16988 		.cmd = NL80211_CMD_JOIN_IBSS,
16989 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16990 		.doit = nl80211_join_ibss,
16991 		.flags = GENL_UNS_ADMIN_PERM,
16992 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16993 	},
16994 	{
16995 		.cmd = NL80211_CMD_LEAVE_IBSS,
16996 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16997 		.doit = nl80211_leave_ibss,
16998 		.flags = GENL_UNS_ADMIN_PERM,
16999 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17000 	},
17001 #ifdef CONFIG_NL80211_TESTMODE
17002 	{
17003 		.cmd = NL80211_CMD_TESTMODE,
17004 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17005 		.doit = nl80211_testmode_do,
17006 		.dumpit = nl80211_testmode_dump,
17007 		.flags = GENL_UNS_ADMIN_PERM,
17008 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17009 	},
17010 #endif
17011 	{
17012 		.cmd = NL80211_CMD_CONNECT,
17013 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17014 		.doit = nl80211_connect,
17015 		.flags = GENL_UNS_ADMIN_PERM,
17016 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17017 					 NL80211_FLAG_CLEAR_SKB),
17018 	},
17019 	{
17020 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
17021 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17022 		.doit = nl80211_update_connect_params,
17023 		.flags = GENL_ADMIN_PERM,
17024 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17025 					 NL80211_FLAG_CLEAR_SKB),
17026 	},
17027 	{
17028 		.cmd = NL80211_CMD_DISCONNECT,
17029 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17030 		.doit = nl80211_disconnect,
17031 		.flags = GENL_UNS_ADMIN_PERM,
17032 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17033 	},
17034 	{
17035 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
17036 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17037 		.doit = nl80211_wiphy_netns,
17038 		.flags = GENL_UNS_ADMIN_PERM,
17039 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17040 					 NL80211_FLAG_NEED_RTNL |
17041 					 NL80211_FLAG_NO_WIPHY_MTX),
17042 	},
17043 	{
17044 		.cmd = NL80211_CMD_GET_SURVEY,
17045 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17046 		.dumpit = nl80211_dump_survey,
17047 	},
17048 	{
17049 		.cmd = NL80211_CMD_SET_PMKSA,
17050 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17051 		.doit = nl80211_setdel_pmksa,
17052 		.flags = GENL_UNS_ADMIN_PERM,
17053 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17054 					 NL80211_FLAG_CLEAR_SKB),
17055 	},
17056 	{
17057 		.cmd = NL80211_CMD_DEL_PMKSA,
17058 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17059 		.doit = nl80211_setdel_pmksa,
17060 		.flags = GENL_UNS_ADMIN_PERM,
17061 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17062 	},
17063 	{
17064 		.cmd = NL80211_CMD_FLUSH_PMKSA,
17065 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17066 		.doit = nl80211_flush_pmksa,
17067 		.flags = GENL_UNS_ADMIN_PERM,
17068 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17069 	},
17070 	{
17071 		.cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
17072 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17073 		.doit = nl80211_remain_on_channel,
17074 		.flags = GENL_UNS_ADMIN_PERM,
17075 		/* FIXME: requiring a link ID here is probably not good */
17076 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17077 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17078 	},
17079 	{
17080 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
17081 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17082 		.doit = nl80211_cancel_remain_on_channel,
17083 		.flags = GENL_UNS_ADMIN_PERM,
17084 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17085 	},
17086 	{
17087 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
17088 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17089 		.doit = nl80211_set_tx_bitrate_mask,
17090 		.flags = GENL_UNS_ADMIN_PERM,
17091 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17092 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17093 	},
17094 	{
17095 		.cmd = NL80211_CMD_REGISTER_FRAME,
17096 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17097 		.doit = nl80211_register_mgmt,
17098 		.flags = GENL_UNS_ADMIN_PERM,
17099 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
17100 	},
17101 	{
17102 		.cmd = NL80211_CMD_FRAME,
17103 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17104 		.doit = nl80211_tx_mgmt,
17105 		.flags = GENL_UNS_ADMIN_PERM,
17106 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17107 	},
17108 	{
17109 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
17110 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17111 		.doit = nl80211_tx_mgmt_cancel_wait,
17112 		.flags = GENL_UNS_ADMIN_PERM,
17113 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17114 	},
17115 	{
17116 		.cmd = NL80211_CMD_SET_POWER_SAVE,
17117 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17118 		.doit = nl80211_set_power_save,
17119 		.flags = GENL_UNS_ADMIN_PERM,
17120 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17121 	},
17122 	{
17123 		.cmd = NL80211_CMD_GET_POWER_SAVE,
17124 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17125 		.doit = nl80211_get_power_save,
17126 		/* can be retrieved by unprivileged users */
17127 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17128 	},
17129 	{
17130 		.cmd = NL80211_CMD_SET_CQM,
17131 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17132 		.doit = nl80211_set_cqm,
17133 		.flags = GENL_UNS_ADMIN_PERM,
17134 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17135 	},
17136 	{
17137 		.cmd = NL80211_CMD_SET_CHANNEL,
17138 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17139 		.doit = nl80211_set_channel,
17140 		.flags = GENL_UNS_ADMIN_PERM,
17141 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17142 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17143 	},
17144 	{
17145 		.cmd = NL80211_CMD_JOIN_MESH,
17146 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17147 		.doit = nl80211_join_mesh,
17148 		.flags = GENL_UNS_ADMIN_PERM,
17149 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17150 	},
17151 	{
17152 		.cmd = NL80211_CMD_LEAVE_MESH,
17153 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17154 		.doit = nl80211_leave_mesh,
17155 		.flags = GENL_UNS_ADMIN_PERM,
17156 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17157 	},
17158 	{
17159 		.cmd = NL80211_CMD_JOIN_OCB,
17160 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17161 		.doit = nl80211_join_ocb,
17162 		.flags = GENL_UNS_ADMIN_PERM,
17163 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17164 	},
17165 	{
17166 		.cmd = NL80211_CMD_LEAVE_OCB,
17167 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17168 		.doit = nl80211_leave_ocb,
17169 		.flags = GENL_UNS_ADMIN_PERM,
17170 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17171 	},
17172 #ifdef CONFIG_PM
17173 	{
17174 		.cmd = NL80211_CMD_GET_WOWLAN,
17175 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17176 		.doit = nl80211_get_wowlan,
17177 		/* can be retrieved by unprivileged users */
17178 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17179 	},
17180 	{
17181 		.cmd = NL80211_CMD_SET_WOWLAN,
17182 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17183 		.doit = nl80211_set_wowlan,
17184 		.flags = GENL_UNS_ADMIN_PERM,
17185 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17186 	},
17187 #endif
17188 	{
17189 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
17190 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17191 		.doit = nl80211_set_rekey_data,
17192 		.flags = GENL_UNS_ADMIN_PERM,
17193 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17194 					 NL80211_FLAG_CLEAR_SKB),
17195 	},
17196 	{
17197 		.cmd = NL80211_CMD_TDLS_MGMT,
17198 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17199 		.doit = nl80211_tdls_mgmt,
17200 		.flags = GENL_UNS_ADMIN_PERM,
17201 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17202 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17203 	},
17204 	{
17205 		.cmd = NL80211_CMD_TDLS_OPER,
17206 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17207 		.doit = nl80211_tdls_oper,
17208 		.flags = GENL_UNS_ADMIN_PERM,
17209 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17210 	},
17211 	{
17212 		.cmd = NL80211_CMD_UNEXPECTED_FRAME,
17213 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17214 		.doit = nl80211_register_unexpected_frame,
17215 		.flags = GENL_UNS_ADMIN_PERM,
17216 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17217 	},
17218 	{
17219 		.cmd = NL80211_CMD_PROBE_CLIENT,
17220 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17221 		.doit = nl80211_probe_client,
17222 		.flags = GENL_UNS_ADMIN_PERM,
17223 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17224 	},
17225 	{
17226 		.cmd = NL80211_CMD_REGISTER_BEACONS,
17227 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17228 		.doit = nl80211_register_beacons,
17229 		.flags = GENL_UNS_ADMIN_PERM,
17230 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17231 	},
17232 	{
17233 		.cmd = NL80211_CMD_SET_NOACK_MAP,
17234 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17235 		.doit = nl80211_set_noack_map,
17236 		.flags = GENL_UNS_ADMIN_PERM,
17237 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17238 	},
17239 	{
17240 		.cmd = NL80211_CMD_START_P2P_DEVICE,
17241 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17242 		.doit = nl80211_start_p2p_device,
17243 		.flags = GENL_UNS_ADMIN_PERM,
17244 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
17245 					 NL80211_FLAG_NEED_RTNL),
17246 	},
17247 	{
17248 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
17249 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17250 		.doit = nl80211_stop_p2p_device,
17251 		.flags = GENL_UNS_ADMIN_PERM,
17252 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17253 					 NL80211_FLAG_NEED_RTNL),
17254 	},
17255 	{
17256 		.cmd = NL80211_CMD_START_NAN,
17257 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17258 		.doit = nl80211_start_nan,
17259 		.flags = GENL_ADMIN_PERM,
17260 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
17261 					 NL80211_FLAG_NEED_RTNL),
17262 	},
17263 	{
17264 		.cmd = NL80211_CMD_STOP_NAN,
17265 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17266 		.doit = nl80211_stop_nan,
17267 		.flags = GENL_ADMIN_PERM,
17268 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17269 					 NL80211_FLAG_NEED_RTNL),
17270 	},
17271 	{
17272 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
17273 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17274 		.doit = nl80211_nan_add_func,
17275 		.flags = GENL_ADMIN_PERM,
17276 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17277 	},
17278 	{
17279 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
17280 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17281 		.doit = nl80211_nan_del_func,
17282 		.flags = GENL_ADMIN_PERM,
17283 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17284 	},
17285 	{
17286 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
17287 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17288 		.doit = nl80211_nan_change_config,
17289 		.flags = GENL_ADMIN_PERM,
17290 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17291 	},
17292 	{
17293 		.cmd = NL80211_CMD_SET_MCAST_RATE,
17294 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17295 		.doit = nl80211_set_mcast_rate,
17296 		.flags = GENL_UNS_ADMIN_PERM,
17297 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17298 	},
17299 	{
17300 		.cmd = NL80211_CMD_SET_MAC_ACL,
17301 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17302 		.doit = nl80211_set_mac_acl,
17303 		.flags = GENL_UNS_ADMIN_PERM,
17304 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17305 					 NL80211_FLAG_MLO_UNSUPPORTED),
17306 	},
17307 	{
17308 		.cmd = NL80211_CMD_RADAR_DETECT,
17309 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17310 		.doit = nl80211_start_radar_detection,
17311 		.flags = GENL_UNS_ADMIN_PERM,
17312 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17313 					 NL80211_FLAG_NO_WIPHY_MTX |
17314 					 NL80211_FLAG_MLO_UNSUPPORTED),
17315 	},
17316 	{
17317 		.cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
17318 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17319 		.doit = nl80211_get_protocol_features,
17320 	},
17321 	{
17322 		.cmd = NL80211_CMD_UPDATE_FT_IES,
17323 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17324 		.doit = nl80211_update_ft_ies,
17325 		.flags = GENL_UNS_ADMIN_PERM,
17326 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17327 	},
17328 	{
17329 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
17330 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17331 		.doit = nl80211_crit_protocol_start,
17332 		.flags = GENL_UNS_ADMIN_PERM,
17333 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17334 	},
17335 	{
17336 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
17337 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17338 		.doit = nl80211_crit_protocol_stop,
17339 		.flags = GENL_UNS_ADMIN_PERM,
17340 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17341 	},
17342 	{
17343 		.cmd = NL80211_CMD_GET_COALESCE,
17344 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17345 		.doit = nl80211_get_coalesce,
17346 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17347 	},
17348 	{
17349 		.cmd = NL80211_CMD_SET_COALESCE,
17350 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17351 		.doit = nl80211_set_coalesce,
17352 		.flags = GENL_UNS_ADMIN_PERM,
17353 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17354 	},
17355 	{
17356 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
17357 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17358 		.doit = nl80211_channel_switch,
17359 		.flags = GENL_UNS_ADMIN_PERM,
17360 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17361 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17362 	},
17363 	{
17364 		.cmd = NL80211_CMD_VENDOR,
17365 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17366 		.doit = nl80211_vendor_cmd,
17367 		.dumpit = nl80211_vendor_cmd_dump,
17368 		.flags = GENL_UNS_ADMIN_PERM,
17369 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17370 					 NL80211_FLAG_CLEAR_SKB),
17371 	},
17372 	{
17373 		.cmd = NL80211_CMD_SET_QOS_MAP,
17374 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17375 		.doit = nl80211_set_qos_map,
17376 		.flags = GENL_UNS_ADMIN_PERM,
17377 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17378 	},
17379 	{
17380 		.cmd = NL80211_CMD_ADD_TX_TS,
17381 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17382 		.doit = nl80211_add_tx_ts,
17383 		.flags = GENL_UNS_ADMIN_PERM,
17384 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17385 					 NL80211_FLAG_MLO_UNSUPPORTED),
17386 	},
17387 	{
17388 		.cmd = NL80211_CMD_DEL_TX_TS,
17389 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17390 		.doit = nl80211_del_tx_ts,
17391 		.flags = GENL_UNS_ADMIN_PERM,
17392 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17393 	},
17394 	{
17395 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
17396 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17397 		.doit = nl80211_tdls_channel_switch,
17398 		.flags = GENL_UNS_ADMIN_PERM,
17399 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17400 	},
17401 	{
17402 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
17403 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17404 		.doit = nl80211_tdls_cancel_channel_switch,
17405 		.flags = GENL_UNS_ADMIN_PERM,
17406 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17407 	},
17408 	{
17409 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
17410 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17411 		.doit = nl80211_set_multicast_to_unicast,
17412 		.flags = GENL_UNS_ADMIN_PERM,
17413 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17414 	},
17415 	{
17416 		.cmd = NL80211_CMD_SET_PMK,
17417 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17418 		.doit = nl80211_set_pmk,
17419 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17420 					 NL80211_FLAG_CLEAR_SKB),
17421 	},
17422 	{
17423 		.cmd = NL80211_CMD_DEL_PMK,
17424 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17425 		.doit = nl80211_del_pmk,
17426 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17427 	},
17428 	{
17429 		.cmd = NL80211_CMD_EXTERNAL_AUTH,
17430 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17431 		.doit = nl80211_external_auth,
17432 		.flags = GENL_ADMIN_PERM,
17433 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17434 	},
17435 	{
17436 		.cmd = NL80211_CMD_CONTROL_PORT_FRAME,
17437 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17438 		.doit = nl80211_tx_control_port,
17439 		.flags = GENL_UNS_ADMIN_PERM,
17440 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17441 	},
17442 	{
17443 		.cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
17444 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17445 		.doit = nl80211_get_ftm_responder_stats,
17446 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17447 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17448 	},
17449 	{
17450 		.cmd = NL80211_CMD_PEER_MEASUREMENT_START,
17451 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17452 		.doit = nl80211_pmsr_start,
17453 		.flags = GENL_UNS_ADMIN_PERM,
17454 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17455 	},
17456 	{
17457 		.cmd = NL80211_CMD_NOTIFY_RADAR,
17458 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17459 		.doit = nl80211_notify_radar_detection,
17460 		.flags = GENL_UNS_ADMIN_PERM,
17461 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17462 	},
17463 	{
17464 		.cmd = NL80211_CMD_UPDATE_OWE_INFO,
17465 		.doit = nl80211_update_owe_info,
17466 		.flags = GENL_ADMIN_PERM,
17467 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17468 	},
17469 	{
17470 		.cmd = NL80211_CMD_PROBE_MESH_LINK,
17471 		.doit = nl80211_probe_mesh_link,
17472 		.flags = GENL_UNS_ADMIN_PERM,
17473 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17474 	},
17475 	{
17476 		.cmd = NL80211_CMD_SET_TID_CONFIG,
17477 		.doit = nl80211_set_tid_config,
17478 		.flags = GENL_UNS_ADMIN_PERM,
17479 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17480 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17481 	},
17482 	{
17483 		.cmd = NL80211_CMD_SET_SAR_SPECS,
17484 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17485 		.doit = nl80211_set_sar_specs,
17486 		.flags = GENL_UNS_ADMIN_PERM,
17487 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17488 					 NL80211_FLAG_NEED_RTNL),
17489 	},
17490 	{
17491 		.cmd = NL80211_CMD_COLOR_CHANGE_REQUEST,
17492 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17493 		.doit = nl80211_color_change,
17494 		.flags = GENL_UNS_ADMIN_PERM,
17495 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17496 	},
17497 	{
17498 		.cmd = NL80211_CMD_SET_FILS_AAD,
17499 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17500 		.doit = nl80211_set_fils_aad,
17501 		.flags = GENL_UNS_ADMIN_PERM,
17502 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17503 	},
17504 	{
17505 		.cmd = NL80211_CMD_ADD_LINK,
17506 		.doit = nl80211_add_link,
17507 		.flags = GENL_UNS_ADMIN_PERM,
17508 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17509 	},
17510 	{
17511 		.cmd = NL80211_CMD_REMOVE_LINK,
17512 		.doit = nl80211_remove_link,
17513 		.flags = GENL_UNS_ADMIN_PERM,
17514 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17515 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17516 	},
17517 	{
17518 		.cmd = NL80211_CMD_ADD_LINK_STA,
17519 		.doit = nl80211_add_link_station,
17520 		.flags = GENL_UNS_ADMIN_PERM,
17521 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17522 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17523 	},
17524 	{
17525 		.cmd = NL80211_CMD_MODIFY_LINK_STA,
17526 		.doit = nl80211_modify_link_station,
17527 		.flags = GENL_UNS_ADMIN_PERM,
17528 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17529 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17530 	},
17531 	{
17532 		.cmd = NL80211_CMD_REMOVE_LINK_STA,
17533 		.doit = nl80211_remove_link_station,
17534 		.flags = GENL_UNS_ADMIN_PERM,
17535 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17536 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17537 	},
17538 	{
17539 		.cmd = NL80211_CMD_SET_HW_TIMESTAMP,
17540 		.doit = nl80211_set_hw_timestamp,
17541 		.flags = GENL_UNS_ADMIN_PERM,
17542 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17543 	},
17544 };
17545 
17546 static struct genl_family nl80211_fam __ro_after_init = {
17547 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
17548 	.hdrsize = 0,			/* no private header */
17549 	.version = 1,			/* no particular meaning now */
17550 	.maxattr = NL80211_ATTR_MAX,
17551 	.policy = nl80211_policy,
17552 	.netnsok = true,
17553 	.pre_doit = nl80211_pre_doit,
17554 	.post_doit = nl80211_post_doit,
17555 	.module = THIS_MODULE,
17556 	.ops = nl80211_ops,
17557 	.n_ops = ARRAY_SIZE(nl80211_ops),
17558 	.small_ops = nl80211_small_ops,
17559 	.n_small_ops = ARRAY_SIZE(nl80211_small_ops),
17560 	.resv_start_op = NL80211_CMD_REMOVE_LINK_STA + 1,
17561 	.mcgrps = nl80211_mcgrps,
17562 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
17563 	.parallel_ops = true,
17564 };
17565 
17566 /* notification functions */
17567 
17568 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
17569 			  enum nl80211_commands cmd)
17570 {
17571 	struct sk_buff *msg;
17572 	struct nl80211_dump_wiphy_state state = {};
17573 
17574 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
17575 		cmd != NL80211_CMD_DEL_WIPHY);
17576 
17577 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17578 	if (!msg)
17579 		return;
17580 
17581 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
17582 		nlmsg_free(msg);
17583 		return;
17584 	}
17585 
17586 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17587 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17588 }
17589 
17590 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
17591 				struct wireless_dev *wdev,
17592 				enum nl80211_commands cmd)
17593 {
17594 	struct sk_buff *msg;
17595 
17596 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17597 	if (!msg)
17598 		return;
17599 
17600 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
17601 		nlmsg_free(msg);
17602 		return;
17603 	}
17604 
17605 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17606 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17607 }
17608 
17609 static int nl80211_add_scan_req(struct sk_buff *msg,
17610 				struct cfg80211_registered_device *rdev)
17611 {
17612 	struct cfg80211_scan_request *req = rdev->scan_req;
17613 	struct nlattr *nest;
17614 	int i;
17615 	struct cfg80211_scan_info *info;
17616 
17617 	if (WARN_ON(!req))
17618 		return 0;
17619 
17620 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
17621 	if (!nest)
17622 		goto nla_put_failure;
17623 	for (i = 0; i < req->n_ssids; i++) {
17624 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
17625 			goto nla_put_failure;
17626 	}
17627 	nla_nest_end(msg, nest);
17628 
17629 	if (req->flags & NL80211_SCAN_FLAG_FREQ_KHZ) {
17630 		nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQ_KHZ);
17631 		if (!nest)
17632 			goto nla_put_failure;
17633 		for (i = 0; i < req->n_channels; i++) {
17634 			if (nla_put_u32(msg, i,
17635 				   ieee80211_channel_to_khz(req->channels[i])))
17636 				goto nla_put_failure;
17637 		}
17638 		nla_nest_end(msg, nest);
17639 	} else {
17640 		nest = nla_nest_start_noflag(msg,
17641 					     NL80211_ATTR_SCAN_FREQUENCIES);
17642 		if (!nest)
17643 			goto nla_put_failure;
17644 		for (i = 0; i < req->n_channels; i++) {
17645 			if (nla_put_u32(msg, i, req->channels[i]->center_freq))
17646 				goto nla_put_failure;
17647 		}
17648 		nla_nest_end(msg, nest);
17649 	}
17650 
17651 	if (req->ie &&
17652 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
17653 		goto nla_put_failure;
17654 
17655 	if (req->flags &&
17656 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
17657 		goto nla_put_failure;
17658 
17659 	info = rdev->int_scan_req ? &rdev->int_scan_req->info :
17660 		&rdev->scan_req->info;
17661 	if (info->scan_start_tsf &&
17662 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
17663 			       info->scan_start_tsf, NL80211_BSS_PAD) ||
17664 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
17665 		     info->tsf_bssid)))
17666 		goto nla_put_failure;
17667 
17668 	return 0;
17669  nla_put_failure:
17670 	return -ENOBUFS;
17671 }
17672 
17673 static int nl80211_prep_scan_msg(struct sk_buff *msg,
17674 				 struct cfg80211_registered_device *rdev,
17675 				 struct wireless_dev *wdev,
17676 				 u32 portid, u32 seq, int flags,
17677 				 u32 cmd)
17678 {
17679 	void *hdr;
17680 
17681 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
17682 	if (!hdr)
17683 		return -1;
17684 
17685 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17686 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
17687 					 wdev->netdev->ifindex)) ||
17688 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17689 			      NL80211_ATTR_PAD))
17690 		goto nla_put_failure;
17691 
17692 	/* ignore errors and send incomplete event anyway */
17693 	nl80211_add_scan_req(msg, rdev);
17694 
17695 	genlmsg_end(msg, hdr);
17696 	return 0;
17697 
17698  nla_put_failure:
17699 	genlmsg_cancel(msg, hdr);
17700 	return -EMSGSIZE;
17701 }
17702 
17703 static int
17704 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
17705 			    struct cfg80211_sched_scan_request *req, u32 cmd)
17706 {
17707 	void *hdr;
17708 
17709 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17710 	if (!hdr)
17711 		return -1;
17712 
17713 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
17714 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
17715 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
17716 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
17717 			      NL80211_ATTR_PAD))
17718 		goto nla_put_failure;
17719 
17720 	genlmsg_end(msg, hdr);
17721 	return 0;
17722 
17723  nla_put_failure:
17724 	genlmsg_cancel(msg, hdr);
17725 	return -EMSGSIZE;
17726 }
17727 
17728 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
17729 			     struct wireless_dev *wdev)
17730 {
17731 	struct sk_buff *msg;
17732 
17733 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17734 	if (!msg)
17735 		return;
17736 
17737 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17738 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
17739 		nlmsg_free(msg);
17740 		return;
17741 	}
17742 
17743 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17744 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17745 }
17746 
17747 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
17748 				       struct wireless_dev *wdev, bool aborted)
17749 {
17750 	struct sk_buff *msg;
17751 
17752 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17753 	if (!msg)
17754 		return NULL;
17755 
17756 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17757 				  aborted ? NL80211_CMD_SCAN_ABORTED :
17758 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
17759 		nlmsg_free(msg);
17760 		return NULL;
17761 	}
17762 
17763 	return msg;
17764 }
17765 
17766 /* send message created by nl80211_build_scan_msg() */
17767 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
17768 			   struct sk_buff *msg)
17769 {
17770 	if (!msg)
17771 		return;
17772 
17773 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17774 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17775 }
17776 
17777 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
17778 {
17779 	struct sk_buff *msg;
17780 
17781 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17782 	if (!msg)
17783 		return;
17784 
17785 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
17786 		nlmsg_free(msg);
17787 		return;
17788 	}
17789 
17790 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
17791 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17792 }
17793 
17794 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
17795 					  struct regulatory_request *request)
17796 {
17797 	/* Userspace can always count this one always being set */
17798 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
17799 		goto nla_put_failure;
17800 
17801 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
17802 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17803 			       NL80211_REGDOM_TYPE_WORLD))
17804 			goto nla_put_failure;
17805 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
17806 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17807 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
17808 			goto nla_put_failure;
17809 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
17810 		   request->intersect) {
17811 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17812 			       NL80211_REGDOM_TYPE_INTERSECTION))
17813 			goto nla_put_failure;
17814 	} else {
17815 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17816 			       NL80211_REGDOM_TYPE_COUNTRY) ||
17817 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
17818 				   request->alpha2))
17819 			goto nla_put_failure;
17820 	}
17821 
17822 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
17823 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
17824 
17825 		if (wiphy &&
17826 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
17827 			goto nla_put_failure;
17828 
17829 		if (wiphy &&
17830 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
17831 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
17832 			goto nla_put_failure;
17833 	}
17834 
17835 	return true;
17836 
17837 nla_put_failure:
17838 	return false;
17839 }
17840 
17841 /*
17842  * This can happen on global regulatory changes or device specific settings
17843  * based on custom regulatory domains.
17844  */
17845 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
17846 				     struct regulatory_request *request)
17847 {
17848 	struct sk_buff *msg;
17849 	void *hdr;
17850 
17851 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17852 	if (!msg)
17853 		return;
17854 
17855 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
17856 	if (!hdr)
17857 		goto nla_put_failure;
17858 
17859 	if (!nl80211_reg_change_event_fill(msg, request))
17860 		goto nla_put_failure;
17861 
17862 	genlmsg_end(msg, hdr);
17863 
17864 	rcu_read_lock();
17865 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
17866 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
17867 	rcu_read_unlock();
17868 
17869 	return;
17870 
17871 nla_put_failure:
17872 	nlmsg_free(msg);
17873 }
17874 
17875 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
17876 				    struct net_device *netdev,
17877 				    const u8 *buf, size_t len,
17878 				    enum nl80211_commands cmd, gfp_t gfp,
17879 				    int uapsd_queues, const u8 *req_ies,
17880 				    size_t req_ies_len, bool reconnect)
17881 {
17882 	struct sk_buff *msg;
17883 	void *hdr;
17884 
17885 	msg = nlmsg_new(100 + len + req_ies_len, gfp);
17886 	if (!msg)
17887 		return;
17888 
17889 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17890 	if (!hdr) {
17891 		nlmsg_free(msg);
17892 		return;
17893 	}
17894 
17895 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17896 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17897 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
17898 	    (req_ies &&
17899 	     nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
17900 		goto nla_put_failure;
17901 
17902 	if (reconnect && nla_put_flag(msg, NL80211_ATTR_RECONNECT_REQUESTED))
17903 		goto nla_put_failure;
17904 
17905 	if (uapsd_queues >= 0) {
17906 		struct nlattr *nla_wmm =
17907 			nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
17908 		if (!nla_wmm)
17909 			goto nla_put_failure;
17910 
17911 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
17912 			       uapsd_queues))
17913 			goto nla_put_failure;
17914 
17915 		nla_nest_end(msg, nla_wmm);
17916 	}
17917 
17918 	genlmsg_end(msg, hdr);
17919 
17920 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17921 				NL80211_MCGRP_MLME, gfp);
17922 	return;
17923 
17924  nla_put_failure:
17925 	nlmsg_free(msg);
17926 }
17927 
17928 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
17929 			  struct net_device *netdev, const u8 *buf,
17930 			  size_t len, gfp_t gfp)
17931 {
17932 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17933 				NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0,
17934 				false);
17935 }
17936 
17937 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
17938 			   struct net_device *netdev,
17939 			   struct cfg80211_rx_assoc_resp *data)
17940 {
17941 	nl80211_send_mlme_event(rdev, netdev, data->buf, data->len,
17942 				NL80211_CMD_ASSOCIATE, GFP_KERNEL,
17943 				data->uapsd_queues,
17944 				data->req_ies, data->req_ies_len, false);
17945 }
17946 
17947 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
17948 			 struct net_device *netdev, const u8 *buf,
17949 			 size_t len, bool reconnect, gfp_t gfp)
17950 {
17951 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17952 				NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0,
17953 				reconnect);
17954 }
17955 
17956 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
17957 			   struct net_device *netdev, const u8 *buf,
17958 			   size_t len, bool reconnect, gfp_t gfp)
17959 {
17960 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17961 				NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0,
17962 				reconnect);
17963 }
17964 
17965 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
17966 				  size_t len)
17967 {
17968 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17969 	struct wiphy *wiphy = wdev->wiphy;
17970 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17971 	const struct ieee80211_mgmt *mgmt = (void *)buf;
17972 	u32 cmd;
17973 
17974 	if (WARN_ON(len < 2))
17975 		return;
17976 
17977 	if (ieee80211_is_deauth(mgmt->frame_control)) {
17978 		cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
17979 	} else if (ieee80211_is_disassoc(mgmt->frame_control)) {
17980 		cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
17981 	} else if (ieee80211_is_beacon(mgmt->frame_control)) {
17982 		if (wdev->unprot_beacon_reported &&
17983 		    elapsed_jiffies_msecs(wdev->unprot_beacon_reported) < 10000)
17984 			return;
17985 		cmd = NL80211_CMD_UNPROT_BEACON;
17986 		wdev->unprot_beacon_reported = jiffies;
17987 	} else {
17988 		return;
17989 	}
17990 
17991 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
17992 	nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
17993 				NULL, 0, false);
17994 }
17995 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
17996 
17997 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
17998 				      struct net_device *netdev, int cmd,
17999 				      const u8 *addr, gfp_t gfp)
18000 {
18001 	struct sk_buff *msg;
18002 	void *hdr;
18003 
18004 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18005 	if (!msg)
18006 		return;
18007 
18008 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18009 	if (!hdr) {
18010 		nlmsg_free(msg);
18011 		return;
18012 	}
18013 
18014 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18015 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18016 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
18017 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
18018 		goto nla_put_failure;
18019 
18020 	genlmsg_end(msg, hdr);
18021 
18022 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18023 				NL80211_MCGRP_MLME, gfp);
18024 	return;
18025 
18026  nla_put_failure:
18027 	nlmsg_free(msg);
18028 }
18029 
18030 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
18031 			       struct net_device *netdev, const u8 *addr,
18032 			       gfp_t gfp)
18033 {
18034 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
18035 				  addr, gfp);
18036 }
18037 
18038 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
18039 				struct net_device *netdev, const u8 *addr,
18040 				gfp_t gfp)
18041 {
18042 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
18043 				  addr, gfp);
18044 }
18045 
18046 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
18047 				 struct net_device *netdev,
18048 				 struct cfg80211_connect_resp_params *cr,
18049 				 gfp_t gfp)
18050 {
18051 	struct sk_buff *msg;
18052 	void *hdr;
18053 	unsigned int link;
18054 	size_t link_info_size = 0;
18055 	const u8 *connected_addr = cr->valid_links ?
18056 				   cr->ap_mld_addr : cr->links[0].bssid;
18057 
18058 	if (cr->valid_links) {
18059 		for_each_valid_link(cr, link) {
18060 			/* Nested attribute header */
18061 			link_info_size += NLA_HDRLEN;
18062 			/* Link ID */
18063 			link_info_size += nla_total_size(sizeof(u8));
18064 			link_info_size += cr->links[link].addr ?
18065 					  nla_total_size(ETH_ALEN) : 0;
18066 			link_info_size += (cr->links[link].bssid ||
18067 					   cr->links[link].bss) ?
18068 					  nla_total_size(ETH_ALEN) : 0;
18069 			link_info_size += nla_total_size(sizeof(u16));
18070 		}
18071 	}
18072 
18073 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
18074 			cr->fils.kek_len + cr->fils.pmk_len +
18075 			(cr->fils.pmkid ? WLAN_PMKID_LEN : 0) + link_info_size,
18076 			gfp);
18077 	if (!msg)
18078 		return;
18079 
18080 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
18081 	if (!hdr) {
18082 		nlmsg_free(msg);
18083 		return;
18084 	}
18085 
18086 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18087 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18088 	    (connected_addr &&
18089 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr)) ||
18090 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
18091 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
18092 			cr->status) ||
18093 	    (cr->status < 0 &&
18094 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
18095 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
18096 			  cr->timeout_reason))) ||
18097 	    (cr->req_ie &&
18098 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
18099 	    (cr->resp_ie &&
18100 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
18101 		     cr->resp_ie)) ||
18102 	    (cr->fils.update_erp_next_seq_num &&
18103 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
18104 			 cr->fils.erp_next_seq_num)) ||
18105 	    (cr->status == WLAN_STATUS_SUCCESS &&
18106 	     ((cr->fils.kek &&
18107 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
18108 		       cr->fils.kek)) ||
18109 	      (cr->fils.pmk &&
18110 	       nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
18111 	      (cr->fils.pmkid &&
18112 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
18113 		goto nla_put_failure;
18114 
18115 	if (cr->valid_links) {
18116 		int i = 1;
18117 		struct nlattr *nested;
18118 
18119 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18120 		if (!nested)
18121 			goto nla_put_failure;
18122 
18123 		for_each_valid_link(cr, link) {
18124 			struct nlattr *nested_mlo_links;
18125 			const u8 *bssid = cr->links[link].bss ?
18126 					  cr->links[link].bss->bssid :
18127 					  cr->links[link].bssid;
18128 
18129 			nested_mlo_links = nla_nest_start(msg, i);
18130 			if (!nested_mlo_links)
18131 				goto nla_put_failure;
18132 
18133 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
18134 			    (bssid &&
18135 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
18136 			    (cr->links[link].addr &&
18137 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
18138 				     cr->links[link].addr)) ||
18139 			    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
18140 					cr->links[link].status))
18141 				goto nla_put_failure;
18142 
18143 			nla_nest_end(msg, nested_mlo_links);
18144 			i++;
18145 		}
18146 		nla_nest_end(msg, nested);
18147 	}
18148 
18149 	genlmsg_end(msg, hdr);
18150 
18151 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18152 				NL80211_MCGRP_MLME, gfp);
18153 	return;
18154 
18155  nla_put_failure:
18156 	nlmsg_free(msg);
18157 }
18158 
18159 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
18160 			 struct net_device *netdev,
18161 			 struct cfg80211_roam_info *info, gfp_t gfp)
18162 {
18163 	struct sk_buff *msg;
18164 	void *hdr;
18165 	size_t link_info_size = 0;
18166 	unsigned int link;
18167 	const u8 *connected_addr = info->ap_mld_addr ?
18168 				   info->ap_mld_addr :
18169 				   (info->links[0].bss ?
18170 				    info->links[0].bss->bssid :
18171 				    info->links[0].bssid);
18172 
18173 	if (info->valid_links) {
18174 		for_each_valid_link(info, link) {
18175 			/* Nested attribute header */
18176 			link_info_size += NLA_HDRLEN;
18177 			/* Link ID */
18178 			link_info_size += nla_total_size(sizeof(u8));
18179 			link_info_size += info->links[link].addr ?
18180 					  nla_total_size(ETH_ALEN) : 0;
18181 			link_info_size += (info->links[link].bssid ||
18182 					   info->links[link].bss) ?
18183 					  nla_total_size(ETH_ALEN) : 0;
18184 		}
18185 	}
18186 
18187 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
18188 			info->fils.kek_len + info->fils.pmk_len +
18189 			(info->fils.pmkid ? WLAN_PMKID_LEN : 0) +
18190 			link_info_size, gfp);
18191 	if (!msg)
18192 		return;
18193 
18194 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
18195 	if (!hdr) {
18196 		nlmsg_free(msg);
18197 		return;
18198 	}
18199 
18200 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18201 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18202 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr) ||
18203 	    (info->req_ie &&
18204 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
18205 		     info->req_ie)) ||
18206 	    (info->resp_ie &&
18207 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
18208 		     info->resp_ie)) ||
18209 	    (info->fils.update_erp_next_seq_num &&
18210 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
18211 			 info->fils.erp_next_seq_num)) ||
18212 	    (info->fils.kek &&
18213 	     nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
18214 		     info->fils.kek)) ||
18215 	    (info->fils.pmk &&
18216 	     nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
18217 	    (info->fils.pmkid &&
18218 	     nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
18219 		goto nla_put_failure;
18220 
18221 	if (info->valid_links) {
18222 		int i = 1;
18223 		struct nlattr *nested;
18224 
18225 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18226 		if (!nested)
18227 			goto nla_put_failure;
18228 
18229 		for_each_valid_link(info, link) {
18230 			struct nlattr *nested_mlo_links;
18231 			const u8 *bssid = info->links[link].bss ?
18232 					  info->links[link].bss->bssid :
18233 					  info->links[link].bssid;
18234 
18235 			nested_mlo_links = nla_nest_start(msg, i);
18236 			if (!nested_mlo_links)
18237 				goto nla_put_failure;
18238 
18239 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
18240 			    (bssid &&
18241 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
18242 			    (info->links[link].addr &&
18243 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
18244 				     info->links[link].addr)))
18245 				goto nla_put_failure;
18246 
18247 			nla_nest_end(msg, nested_mlo_links);
18248 			i++;
18249 		}
18250 		nla_nest_end(msg, nested);
18251 	}
18252 
18253 	genlmsg_end(msg, hdr);
18254 
18255 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18256 				NL80211_MCGRP_MLME, gfp);
18257 	return;
18258 
18259  nla_put_failure:
18260 	nlmsg_free(msg);
18261 }
18262 
18263 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
18264 				  struct net_device *netdev, const u8 *bssid,
18265 				  const u8 *td_bitmap, u8 td_bitmap_len)
18266 {
18267 	struct sk_buff *msg;
18268 	void *hdr;
18269 
18270 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18271 	if (!msg)
18272 		return;
18273 
18274 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
18275 	if (!hdr) {
18276 		nlmsg_free(msg);
18277 		return;
18278 	}
18279 
18280 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18281 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18282 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18283 		goto nla_put_failure;
18284 
18285 	if ((td_bitmap_len > 0) && td_bitmap)
18286 		if (nla_put(msg, NL80211_ATTR_TD_BITMAP,
18287 			    td_bitmap_len, td_bitmap))
18288 			goto nla_put_failure;
18289 
18290 	genlmsg_end(msg, hdr);
18291 
18292 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18293 				NL80211_MCGRP_MLME, GFP_KERNEL);
18294 	return;
18295 
18296  nla_put_failure:
18297 	nlmsg_free(msg);
18298 }
18299 
18300 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
18301 			       struct net_device *netdev, u16 reason,
18302 			       const u8 *ie, size_t ie_len, bool from_ap)
18303 {
18304 	struct sk_buff *msg;
18305 	void *hdr;
18306 
18307 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
18308 	if (!msg)
18309 		return;
18310 
18311 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
18312 	if (!hdr) {
18313 		nlmsg_free(msg);
18314 		return;
18315 	}
18316 
18317 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18318 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18319 	    (reason &&
18320 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
18321 	    (from_ap &&
18322 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
18323 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
18324 		goto nla_put_failure;
18325 
18326 	genlmsg_end(msg, hdr);
18327 
18328 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18329 				NL80211_MCGRP_MLME, GFP_KERNEL);
18330 	return;
18331 
18332  nla_put_failure:
18333 	nlmsg_free(msg);
18334 }
18335 
18336 void cfg80211_links_removed(struct net_device *dev, u16 link_mask)
18337 {
18338 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18339 	struct wiphy *wiphy = wdev->wiphy;
18340 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18341 	struct sk_buff *msg;
18342 	struct nlattr *links;
18343 	void *hdr;
18344 
18345 	ASSERT_WDEV_LOCK(wdev);
18346 	trace_cfg80211_links_removed(dev, link_mask);
18347 
18348 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
18349 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
18350 		return;
18351 
18352 	if (WARN_ON(!wdev->valid_links || !link_mask ||
18353 		    (wdev->valid_links & link_mask) != link_mask ||
18354 		    wdev->valid_links == link_mask))
18355 		return;
18356 
18357 	cfg80211_wdev_release_link_bsses(wdev, link_mask);
18358 	wdev->valid_links &= ~link_mask;
18359 
18360 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18361 	if (!msg)
18362 		return;
18363 
18364 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_LINKS_REMOVED);
18365 	if (!hdr) {
18366 		nlmsg_free(msg);
18367 		return;
18368 	}
18369 
18370 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18371 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
18372 		goto nla_put_failure;
18373 
18374 	links = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18375 	if (!links)
18376 		goto nla_put_failure;
18377 
18378 	while (link_mask) {
18379 		struct nlattr *link;
18380 		int link_id = __ffs(link_mask);
18381 
18382 		link = nla_nest_start(msg, link_id + 1);
18383 		if (!link)
18384 			goto nla_put_failure;
18385 
18386 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
18387 			goto nla_put_failure;
18388 
18389 		nla_nest_end(msg, link);
18390 		link_mask &= ~(1 << link_id);
18391 	}
18392 
18393 	nla_nest_end(msg, links);
18394 
18395 	genlmsg_end(msg, hdr);
18396 
18397 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18398 				NL80211_MCGRP_MLME, GFP_KERNEL);
18399 	return;
18400 
18401  nla_put_failure:
18402 	nlmsg_free(msg);
18403 }
18404 EXPORT_SYMBOL(cfg80211_links_removed);
18405 
18406 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
18407 			     struct net_device *netdev, const u8 *bssid,
18408 			     gfp_t gfp)
18409 {
18410 	struct sk_buff *msg;
18411 	void *hdr;
18412 
18413 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18414 	if (!msg)
18415 		return;
18416 
18417 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
18418 	if (!hdr) {
18419 		nlmsg_free(msg);
18420 		return;
18421 	}
18422 
18423 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18424 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18425 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18426 		goto nla_put_failure;
18427 
18428 	genlmsg_end(msg, hdr);
18429 
18430 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18431 				NL80211_MCGRP_MLME, gfp);
18432 	return;
18433 
18434  nla_put_failure:
18435 	nlmsg_free(msg);
18436 }
18437 
18438 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
18439 					const u8 *ie, u8 ie_len,
18440 					int sig_dbm, gfp_t gfp)
18441 {
18442 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18443 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18444 	struct sk_buff *msg;
18445 	void *hdr;
18446 
18447 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
18448 		return;
18449 
18450 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
18451 
18452 	msg = nlmsg_new(100 + ie_len, gfp);
18453 	if (!msg)
18454 		return;
18455 
18456 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
18457 	if (!hdr) {
18458 		nlmsg_free(msg);
18459 		return;
18460 	}
18461 
18462 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18463 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18464 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
18465 	    (ie_len && ie &&
18466 	     nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
18467 	    (sig_dbm &&
18468 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
18469 		goto nla_put_failure;
18470 
18471 	genlmsg_end(msg, hdr);
18472 
18473 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18474 				NL80211_MCGRP_MLME, gfp);
18475 	return;
18476 
18477  nla_put_failure:
18478 	nlmsg_free(msg);
18479 }
18480 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
18481 
18482 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
18483 				 struct net_device *netdev, const u8 *addr,
18484 				 enum nl80211_key_type key_type, int key_id,
18485 				 const u8 *tsc, gfp_t gfp)
18486 {
18487 	struct sk_buff *msg;
18488 	void *hdr;
18489 
18490 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18491 	if (!msg)
18492 		return;
18493 
18494 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
18495 	if (!hdr) {
18496 		nlmsg_free(msg);
18497 		return;
18498 	}
18499 
18500 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18501 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18502 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
18503 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
18504 	    (key_id != -1 &&
18505 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
18506 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
18507 		goto nla_put_failure;
18508 
18509 	genlmsg_end(msg, hdr);
18510 
18511 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18512 				NL80211_MCGRP_MLME, gfp);
18513 	return;
18514 
18515  nla_put_failure:
18516 	nlmsg_free(msg);
18517 }
18518 
18519 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
18520 				    struct ieee80211_channel *channel_before,
18521 				    struct ieee80211_channel *channel_after)
18522 {
18523 	struct sk_buff *msg;
18524 	void *hdr;
18525 	struct nlattr *nl_freq;
18526 
18527 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
18528 	if (!msg)
18529 		return;
18530 
18531 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
18532 	if (!hdr) {
18533 		nlmsg_free(msg);
18534 		return;
18535 	}
18536 
18537 	/*
18538 	 * Since we are applying the beacon hint to a wiphy we know its
18539 	 * wiphy_idx is valid
18540 	 */
18541 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
18542 		goto nla_put_failure;
18543 
18544 	/* Before */
18545 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
18546 	if (!nl_freq)
18547 		goto nla_put_failure;
18548 
18549 	if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
18550 		goto nla_put_failure;
18551 	nla_nest_end(msg, nl_freq);
18552 
18553 	/* After */
18554 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
18555 	if (!nl_freq)
18556 		goto nla_put_failure;
18557 
18558 	if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
18559 		goto nla_put_failure;
18560 	nla_nest_end(msg, nl_freq);
18561 
18562 	genlmsg_end(msg, hdr);
18563 
18564 	rcu_read_lock();
18565 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
18566 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
18567 	rcu_read_unlock();
18568 
18569 	return;
18570 
18571 nla_put_failure:
18572 	nlmsg_free(msg);
18573 }
18574 
18575 static void nl80211_send_remain_on_chan_event(
18576 	int cmd, struct cfg80211_registered_device *rdev,
18577 	struct wireless_dev *wdev, u64 cookie,
18578 	struct ieee80211_channel *chan,
18579 	unsigned int duration, gfp_t gfp)
18580 {
18581 	struct sk_buff *msg;
18582 	void *hdr;
18583 
18584 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18585 	if (!msg)
18586 		return;
18587 
18588 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18589 	if (!hdr) {
18590 		nlmsg_free(msg);
18591 		return;
18592 	}
18593 
18594 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18595 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18596 					 wdev->netdev->ifindex)) ||
18597 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18598 			      NL80211_ATTR_PAD) ||
18599 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
18600 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
18601 			NL80211_CHAN_NO_HT) ||
18602 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
18603 			      NL80211_ATTR_PAD))
18604 		goto nla_put_failure;
18605 
18606 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
18607 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
18608 		goto nla_put_failure;
18609 
18610 	genlmsg_end(msg, hdr);
18611 
18612 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18613 				NL80211_MCGRP_MLME, gfp);
18614 	return;
18615 
18616  nla_put_failure:
18617 	nlmsg_free(msg);
18618 }
18619 
18620 void cfg80211_assoc_comeback(struct net_device *netdev,
18621 			     const u8 *ap_addr, u32 timeout)
18622 {
18623 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
18624 	struct wiphy *wiphy = wdev->wiphy;
18625 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18626 	struct sk_buff *msg;
18627 	void *hdr;
18628 
18629 	trace_cfg80211_assoc_comeback(wdev, ap_addr, timeout);
18630 
18631 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18632 	if (!msg)
18633 		return;
18634 
18635 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ASSOC_COMEBACK);
18636 	if (!hdr) {
18637 		nlmsg_free(msg);
18638 		return;
18639 	}
18640 
18641 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18642 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18643 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ap_addr) ||
18644 	    nla_put_u32(msg, NL80211_ATTR_TIMEOUT, timeout))
18645 		goto nla_put_failure;
18646 
18647 	genlmsg_end(msg, hdr);
18648 
18649 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18650 				NL80211_MCGRP_MLME, GFP_KERNEL);
18651 	return;
18652 
18653  nla_put_failure:
18654 	nlmsg_free(msg);
18655 }
18656 EXPORT_SYMBOL(cfg80211_assoc_comeback);
18657 
18658 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
18659 			       struct ieee80211_channel *chan,
18660 			       unsigned int duration, gfp_t gfp)
18661 {
18662 	struct wiphy *wiphy = wdev->wiphy;
18663 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18664 
18665 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
18666 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
18667 					  rdev, wdev, cookie, chan,
18668 					  duration, gfp);
18669 }
18670 EXPORT_SYMBOL(cfg80211_ready_on_channel);
18671 
18672 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
18673 					struct ieee80211_channel *chan,
18674 					gfp_t gfp)
18675 {
18676 	struct wiphy *wiphy = wdev->wiphy;
18677 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18678 
18679 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
18680 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
18681 					  rdev, wdev, cookie, chan, 0, gfp);
18682 }
18683 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
18684 
18685 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
18686 					struct ieee80211_channel *chan,
18687 					gfp_t gfp)
18688 {
18689 	struct wiphy *wiphy = wdev->wiphy;
18690 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18691 
18692 	trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
18693 	nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
18694 					  rdev, wdev, cookie, chan, 0, gfp);
18695 }
18696 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
18697 
18698 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
18699 		      struct station_info *sinfo, gfp_t gfp)
18700 {
18701 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18702 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18703 	struct sk_buff *msg;
18704 
18705 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
18706 
18707 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18708 	if (!msg)
18709 		return;
18710 
18711 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
18712 				 rdev, dev, mac_addr, sinfo) < 0) {
18713 		nlmsg_free(msg);
18714 		return;
18715 	}
18716 
18717 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18718 				NL80211_MCGRP_MLME, gfp);
18719 }
18720 EXPORT_SYMBOL(cfg80211_new_sta);
18721 
18722 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
18723 			    struct station_info *sinfo, gfp_t gfp)
18724 {
18725 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18726 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18727 	struct sk_buff *msg;
18728 	struct station_info empty_sinfo = {};
18729 
18730 	if (!sinfo)
18731 		sinfo = &empty_sinfo;
18732 
18733 	trace_cfg80211_del_sta(dev, mac_addr);
18734 
18735 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18736 	if (!msg) {
18737 		cfg80211_sinfo_release_content(sinfo);
18738 		return;
18739 	}
18740 
18741 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
18742 				 rdev, dev, mac_addr, sinfo) < 0) {
18743 		nlmsg_free(msg);
18744 		return;
18745 	}
18746 
18747 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18748 				NL80211_MCGRP_MLME, gfp);
18749 }
18750 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
18751 
18752 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
18753 			  enum nl80211_connect_failed_reason reason,
18754 			  gfp_t gfp)
18755 {
18756 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18757 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18758 	struct sk_buff *msg;
18759 	void *hdr;
18760 
18761 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
18762 	if (!msg)
18763 		return;
18764 
18765 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
18766 	if (!hdr) {
18767 		nlmsg_free(msg);
18768 		return;
18769 	}
18770 
18771 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18772 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
18773 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
18774 		goto nla_put_failure;
18775 
18776 	genlmsg_end(msg, hdr);
18777 
18778 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18779 				NL80211_MCGRP_MLME, gfp);
18780 	return;
18781 
18782  nla_put_failure:
18783 	nlmsg_free(msg);
18784 }
18785 EXPORT_SYMBOL(cfg80211_conn_failed);
18786 
18787 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
18788 				       const u8 *addr, gfp_t gfp)
18789 {
18790 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18791 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18792 	struct sk_buff *msg;
18793 	void *hdr;
18794 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
18795 
18796 	if (!nlportid)
18797 		return false;
18798 
18799 	msg = nlmsg_new(100, gfp);
18800 	if (!msg)
18801 		return true;
18802 
18803 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18804 	if (!hdr) {
18805 		nlmsg_free(msg);
18806 		return true;
18807 	}
18808 
18809 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18810 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18811 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
18812 		goto nla_put_failure;
18813 
18814 	genlmsg_end(msg, hdr);
18815 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18816 	return true;
18817 
18818  nla_put_failure:
18819 	nlmsg_free(msg);
18820 	return true;
18821 }
18822 
18823 bool cfg80211_rx_spurious_frame(struct net_device *dev,
18824 				const u8 *addr, gfp_t gfp)
18825 {
18826 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18827 	bool ret;
18828 
18829 	trace_cfg80211_rx_spurious_frame(dev, addr);
18830 
18831 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18832 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
18833 		trace_cfg80211_return_bool(false);
18834 		return false;
18835 	}
18836 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
18837 					 addr, gfp);
18838 	trace_cfg80211_return_bool(ret);
18839 	return ret;
18840 }
18841 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
18842 
18843 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
18844 					const u8 *addr, gfp_t gfp)
18845 {
18846 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18847 	bool ret;
18848 
18849 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
18850 
18851 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18852 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
18853 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
18854 		trace_cfg80211_return_bool(false);
18855 		return false;
18856 	}
18857 	ret = __nl80211_unexpected_frame(dev,
18858 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
18859 					 addr, gfp);
18860 	trace_cfg80211_return_bool(ret);
18861 	return ret;
18862 }
18863 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
18864 
18865 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
18866 		      struct wireless_dev *wdev, u32 nlportid,
18867 		      struct cfg80211_rx_info *info, gfp_t gfp)
18868 {
18869 	struct net_device *netdev = wdev->netdev;
18870 	struct sk_buff *msg;
18871 	void *hdr;
18872 
18873 	msg = nlmsg_new(100 + info->len, gfp);
18874 	if (!msg)
18875 		return -ENOMEM;
18876 
18877 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
18878 	if (!hdr) {
18879 		nlmsg_free(msg);
18880 		return -ENOMEM;
18881 	}
18882 
18883 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18884 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18885 					netdev->ifindex)) ||
18886 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18887 			      NL80211_ATTR_PAD) ||
18888 	    (info->have_link_id &&
18889 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, info->link_id)) ||
18890 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, KHZ_TO_MHZ(info->freq)) ||
18891 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET, info->freq % 1000) ||
18892 	    (info->sig_dbm &&
18893 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, info->sig_dbm)) ||
18894 	    nla_put(msg, NL80211_ATTR_FRAME, info->len, info->buf) ||
18895 	    (info->flags &&
18896 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, info->flags)) ||
18897 	    (info->rx_tstamp && nla_put_u64_64bit(msg,
18898 						  NL80211_ATTR_RX_HW_TIMESTAMP,
18899 						  info->rx_tstamp,
18900 						  NL80211_ATTR_PAD)) ||
18901 	    (info->ack_tstamp && nla_put_u64_64bit(msg,
18902 						   NL80211_ATTR_TX_HW_TIMESTAMP,
18903 						   info->ack_tstamp,
18904 						   NL80211_ATTR_PAD)))
18905 		goto nla_put_failure;
18906 
18907 	genlmsg_end(msg, hdr);
18908 
18909 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18910 
18911  nla_put_failure:
18912 	nlmsg_free(msg);
18913 	return -ENOBUFS;
18914 }
18915 
18916 static void nl80211_frame_tx_status(struct wireless_dev *wdev,
18917 				    struct cfg80211_tx_status *status,
18918 				    gfp_t gfp, enum nl80211_commands command)
18919 {
18920 	struct wiphy *wiphy = wdev->wiphy;
18921 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18922 	struct net_device *netdev = wdev->netdev;
18923 	struct sk_buff *msg;
18924 	void *hdr;
18925 
18926 	if (command == NL80211_CMD_FRAME_TX_STATUS)
18927 		trace_cfg80211_mgmt_tx_status(wdev, status->cookie,
18928 					      status->ack);
18929 	else
18930 		trace_cfg80211_control_port_tx_status(wdev, status->cookie,
18931 						      status->ack);
18932 
18933 	msg = nlmsg_new(100 + status->len, gfp);
18934 	if (!msg)
18935 		return;
18936 
18937 	hdr = nl80211hdr_put(msg, 0, 0, 0, command);
18938 	if (!hdr) {
18939 		nlmsg_free(msg);
18940 		return;
18941 	}
18942 
18943 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18944 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18945 				   netdev->ifindex)) ||
18946 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18947 			      NL80211_ATTR_PAD) ||
18948 	    nla_put(msg, NL80211_ATTR_FRAME, status->len, status->buf) ||
18949 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, status->cookie,
18950 			      NL80211_ATTR_PAD) ||
18951 	    (status->ack && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
18952 	    (status->tx_tstamp &&
18953 	     nla_put_u64_64bit(msg, NL80211_ATTR_TX_HW_TIMESTAMP,
18954 			       status->tx_tstamp, NL80211_ATTR_PAD)) ||
18955 	    (status->ack_tstamp &&
18956 	     nla_put_u64_64bit(msg, NL80211_ATTR_RX_HW_TIMESTAMP,
18957 			       status->ack_tstamp, NL80211_ATTR_PAD)))
18958 		goto nla_put_failure;
18959 
18960 	genlmsg_end(msg, hdr);
18961 
18962 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18963 				NL80211_MCGRP_MLME, gfp);
18964 	return;
18965 
18966 nla_put_failure:
18967 	nlmsg_free(msg);
18968 }
18969 
18970 void cfg80211_control_port_tx_status(struct wireless_dev *wdev, u64 cookie,
18971 				     const u8 *buf, size_t len, bool ack,
18972 				     gfp_t gfp)
18973 {
18974 	struct cfg80211_tx_status status = {
18975 		.cookie = cookie,
18976 		.buf = buf,
18977 		.len = len,
18978 		.ack = ack
18979 	};
18980 
18981 	nl80211_frame_tx_status(wdev, &status, gfp,
18982 				NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS);
18983 }
18984 EXPORT_SYMBOL(cfg80211_control_port_tx_status);
18985 
18986 void cfg80211_mgmt_tx_status_ext(struct wireless_dev *wdev,
18987 				 struct cfg80211_tx_status *status, gfp_t gfp)
18988 {
18989 	nl80211_frame_tx_status(wdev, status, gfp, NL80211_CMD_FRAME_TX_STATUS);
18990 }
18991 EXPORT_SYMBOL(cfg80211_mgmt_tx_status_ext);
18992 
18993 static int __nl80211_rx_control_port(struct net_device *dev,
18994 				     struct sk_buff *skb,
18995 				     bool unencrypted,
18996 				     int link_id,
18997 				     gfp_t gfp)
18998 {
18999 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19000 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19001 	struct ethhdr *ehdr = eth_hdr(skb);
19002 	const u8 *addr = ehdr->h_source;
19003 	u16 proto = be16_to_cpu(skb->protocol);
19004 	struct sk_buff *msg;
19005 	void *hdr;
19006 	struct nlattr *frame;
19007 
19008 	u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
19009 
19010 	if (!nlportid)
19011 		return -ENOENT;
19012 
19013 	msg = nlmsg_new(100 + skb->len, gfp);
19014 	if (!msg)
19015 		return -ENOMEM;
19016 
19017 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
19018 	if (!hdr) {
19019 		nlmsg_free(msg);
19020 		return -ENOBUFS;
19021 	}
19022 
19023 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19024 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19025 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19026 			      NL80211_ATTR_PAD) ||
19027 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
19028 	    nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
19029 	    (link_id >= 0 &&
19030 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
19031 	    (unencrypted && nla_put_flag(msg,
19032 					 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
19033 		goto nla_put_failure;
19034 
19035 	frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
19036 	if (!frame)
19037 		goto nla_put_failure;
19038 
19039 	skb_copy_bits(skb, 0, nla_data(frame), skb->len);
19040 	genlmsg_end(msg, hdr);
19041 
19042 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
19043 
19044  nla_put_failure:
19045 	nlmsg_free(msg);
19046 	return -ENOBUFS;
19047 }
19048 
19049 bool cfg80211_rx_control_port(struct net_device *dev, struct sk_buff *skb,
19050 			      bool unencrypted, int link_id)
19051 {
19052 	int ret;
19053 
19054 	trace_cfg80211_rx_control_port(dev, skb, unencrypted, link_id);
19055 	ret = __nl80211_rx_control_port(dev, skb, unencrypted, link_id,
19056 					GFP_ATOMIC);
19057 	trace_cfg80211_return_bool(ret == 0);
19058 	return ret == 0;
19059 }
19060 EXPORT_SYMBOL(cfg80211_rx_control_port);
19061 
19062 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
19063 					    const char *mac, gfp_t gfp)
19064 {
19065 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19066 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19067 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19068 	void **cb;
19069 
19070 	if (!msg)
19071 		return NULL;
19072 
19073 	cb = (void **)msg->cb;
19074 
19075 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
19076 	if (!cb[0]) {
19077 		nlmsg_free(msg);
19078 		return NULL;
19079 	}
19080 
19081 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19082 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19083 		goto nla_put_failure;
19084 
19085 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
19086 		goto nla_put_failure;
19087 
19088 	cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
19089 	if (!cb[1])
19090 		goto nla_put_failure;
19091 
19092 	cb[2] = rdev;
19093 
19094 	return msg;
19095  nla_put_failure:
19096 	nlmsg_free(msg);
19097 	return NULL;
19098 }
19099 
19100 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
19101 {
19102 	void **cb = (void **)msg->cb;
19103 	struct cfg80211_registered_device *rdev = cb[2];
19104 
19105 	nla_nest_end(msg, cb[1]);
19106 	genlmsg_end(msg, cb[0]);
19107 
19108 	memset(msg->cb, 0, sizeof(msg->cb));
19109 
19110 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19111 				NL80211_MCGRP_MLME, gfp);
19112 }
19113 
19114 void cfg80211_cqm_rssi_notify(struct net_device *dev,
19115 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
19116 			      s32 rssi_level, gfp_t gfp)
19117 {
19118 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19119 	struct cfg80211_cqm_config *cqm_config;
19120 
19121 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
19122 
19123 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
19124 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
19125 		return;
19126 
19127 	rcu_read_lock();
19128 	cqm_config = rcu_dereference(wdev->cqm_config);
19129 	if (cqm_config) {
19130 		cqm_config->last_rssi_event_value = rssi_level;
19131 		cqm_config->last_rssi_event_type = rssi_event;
19132 		wiphy_work_queue(wdev->wiphy, &wdev->cqm_rssi_work);
19133 	}
19134 	rcu_read_unlock();
19135 }
19136 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
19137 
19138 void cfg80211_cqm_rssi_notify_work(struct wiphy *wiphy, struct wiphy_work *work)
19139 {
19140 	struct wireless_dev *wdev = container_of(work, struct wireless_dev,
19141 						 cqm_rssi_work);
19142 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19143 	enum nl80211_cqm_rssi_threshold_event rssi_event;
19144 	struct cfg80211_cqm_config *cqm_config;
19145 	struct sk_buff *msg;
19146 	s32 rssi_level;
19147 
19148 	wdev_lock(wdev);
19149 	cqm_config = rcu_dereference_protected(wdev->cqm_config,
19150 					       lockdep_is_held(&wdev->mtx));
19151 	if (!cqm_config)
19152 		goto unlock;
19153 
19154 	if (cqm_config->use_range_api)
19155 		cfg80211_cqm_rssi_update(rdev, wdev->netdev, cqm_config);
19156 
19157 	rssi_level = cqm_config->last_rssi_event_value;
19158 	rssi_event = cqm_config->last_rssi_event_type;
19159 
19160 	msg = cfg80211_prepare_cqm(wdev->netdev, NULL, GFP_KERNEL);
19161 	if (!msg)
19162 		goto unlock;
19163 
19164 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
19165 			rssi_event))
19166 		goto nla_put_failure;
19167 
19168 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
19169 				      rssi_level))
19170 		goto nla_put_failure;
19171 
19172 	cfg80211_send_cqm(msg, GFP_KERNEL);
19173 
19174 	goto unlock;
19175 
19176  nla_put_failure:
19177 	nlmsg_free(msg);
19178  unlock:
19179 	wdev_unlock(wdev);
19180 }
19181 
19182 void cfg80211_cqm_txe_notify(struct net_device *dev,
19183 			     const u8 *peer, u32 num_packets,
19184 			     u32 rate, u32 intvl, gfp_t gfp)
19185 {
19186 	struct sk_buff *msg;
19187 
19188 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
19189 	if (!msg)
19190 		return;
19191 
19192 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
19193 		goto nla_put_failure;
19194 
19195 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
19196 		goto nla_put_failure;
19197 
19198 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
19199 		goto nla_put_failure;
19200 
19201 	cfg80211_send_cqm(msg, gfp);
19202 	return;
19203 
19204  nla_put_failure:
19205 	nlmsg_free(msg);
19206 }
19207 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
19208 
19209 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
19210 				 const u8 *peer, u32 num_packets, gfp_t gfp)
19211 {
19212 	struct sk_buff *msg;
19213 
19214 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
19215 
19216 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
19217 	if (!msg)
19218 		return;
19219 
19220 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
19221 		goto nla_put_failure;
19222 
19223 	cfg80211_send_cqm(msg, gfp);
19224 	return;
19225 
19226  nla_put_failure:
19227 	nlmsg_free(msg);
19228 }
19229 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
19230 
19231 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
19232 {
19233 	struct sk_buff *msg;
19234 
19235 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
19236 	if (!msg)
19237 		return;
19238 
19239 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
19240 		goto nla_put_failure;
19241 
19242 	cfg80211_send_cqm(msg, gfp);
19243 	return;
19244 
19245  nla_put_failure:
19246 	nlmsg_free(msg);
19247 }
19248 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
19249 
19250 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
19251 				     struct net_device *netdev, const u8 *bssid,
19252 				     const u8 *replay_ctr, gfp_t gfp)
19253 {
19254 	struct sk_buff *msg;
19255 	struct nlattr *rekey_attr;
19256 	void *hdr;
19257 
19258 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19259 	if (!msg)
19260 		return;
19261 
19262 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
19263 	if (!hdr) {
19264 		nlmsg_free(msg);
19265 		return;
19266 	}
19267 
19268 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19269 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19270 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
19271 		goto nla_put_failure;
19272 
19273 	rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
19274 	if (!rekey_attr)
19275 		goto nla_put_failure;
19276 
19277 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
19278 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
19279 		goto nla_put_failure;
19280 
19281 	nla_nest_end(msg, rekey_attr);
19282 
19283 	genlmsg_end(msg, hdr);
19284 
19285 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19286 				NL80211_MCGRP_MLME, gfp);
19287 	return;
19288 
19289  nla_put_failure:
19290 	nlmsg_free(msg);
19291 }
19292 
19293 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
19294 			       const u8 *replay_ctr, gfp_t gfp)
19295 {
19296 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19297 	struct wiphy *wiphy = wdev->wiphy;
19298 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19299 
19300 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
19301 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
19302 }
19303 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
19304 
19305 static void
19306 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
19307 			       struct net_device *netdev, int index,
19308 			       const u8 *bssid, bool preauth, gfp_t gfp)
19309 {
19310 	struct sk_buff *msg;
19311 	struct nlattr *attr;
19312 	void *hdr;
19313 
19314 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19315 	if (!msg)
19316 		return;
19317 
19318 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
19319 	if (!hdr) {
19320 		nlmsg_free(msg);
19321 		return;
19322 	}
19323 
19324 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19325 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
19326 		goto nla_put_failure;
19327 
19328 	attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
19329 	if (!attr)
19330 		goto nla_put_failure;
19331 
19332 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
19333 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
19334 	    (preauth &&
19335 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
19336 		goto nla_put_failure;
19337 
19338 	nla_nest_end(msg, attr);
19339 
19340 	genlmsg_end(msg, hdr);
19341 
19342 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19343 				NL80211_MCGRP_MLME, gfp);
19344 	return;
19345 
19346  nla_put_failure:
19347 	nlmsg_free(msg);
19348 }
19349 
19350 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
19351 				     const u8 *bssid, bool preauth, gfp_t gfp)
19352 {
19353 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19354 	struct wiphy *wiphy = wdev->wiphy;
19355 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19356 
19357 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
19358 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
19359 }
19360 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
19361 
19362 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
19363 				     struct net_device *netdev,
19364 				     unsigned int link_id,
19365 				     struct cfg80211_chan_def *chandef,
19366 				     gfp_t gfp,
19367 				     enum nl80211_commands notif,
19368 				     u8 count, bool quiet, u16 punct_bitmap)
19369 {
19370 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
19371 	struct sk_buff *msg;
19372 	void *hdr;
19373 
19374 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19375 	if (!msg)
19376 		return;
19377 
19378 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
19379 	if (!hdr) {
19380 		nlmsg_free(msg);
19381 		return;
19382 	}
19383 
19384 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
19385 		goto nla_put_failure;
19386 
19387 	if (wdev->valid_links &&
19388 	    nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
19389 		goto nla_put_failure;
19390 
19391 	if (nl80211_send_chandef(msg, chandef))
19392 		goto nla_put_failure;
19393 
19394 	if (notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) {
19395 		if (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count))
19396 			goto nla_put_failure;
19397 		if (quiet &&
19398 		    nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX))
19399 			goto nla_put_failure;
19400 	}
19401 
19402 	if (nla_put_u32(msg, NL80211_ATTR_PUNCT_BITMAP, punct_bitmap))
19403 		goto nla_put_failure;
19404 
19405 	genlmsg_end(msg, hdr);
19406 
19407 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19408 				NL80211_MCGRP_MLME, gfp);
19409 	return;
19410 
19411  nla_put_failure:
19412 	nlmsg_free(msg);
19413 }
19414 
19415 void cfg80211_ch_switch_notify(struct net_device *dev,
19416 			       struct cfg80211_chan_def *chandef,
19417 			       unsigned int link_id, u16 punct_bitmap)
19418 {
19419 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19420 	struct wiphy *wiphy = wdev->wiphy;
19421 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19422 
19423 	ASSERT_WDEV_LOCK(wdev);
19424 	WARN_INVALID_LINK_ID(wdev, link_id);
19425 
19426 	trace_cfg80211_ch_switch_notify(dev, chandef, link_id, punct_bitmap);
19427 
19428 	switch (wdev->iftype) {
19429 	case NL80211_IFTYPE_STATION:
19430 	case NL80211_IFTYPE_P2P_CLIENT:
19431 		if (!WARN_ON(!wdev->links[link_id].client.current_bss))
19432 			cfg80211_update_assoc_bss_entry(wdev, link_id,
19433 							chandef->chan);
19434 		break;
19435 	case NL80211_IFTYPE_MESH_POINT:
19436 		wdev->u.mesh.chandef = *chandef;
19437 		wdev->u.mesh.preset_chandef = *chandef;
19438 		break;
19439 	case NL80211_IFTYPE_AP:
19440 	case NL80211_IFTYPE_P2P_GO:
19441 		wdev->links[link_id].ap.chandef = *chandef;
19442 		break;
19443 	case NL80211_IFTYPE_ADHOC:
19444 		wdev->u.ibss.chandef = *chandef;
19445 		break;
19446 	default:
19447 		WARN_ON(1);
19448 		break;
19449 	}
19450 
19451 	cfg80211_sched_dfs_chan_update(rdev);
19452 
19453 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19454 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0, false,
19455 				 punct_bitmap);
19456 }
19457 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
19458 
19459 void cfg80211_ch_switch_started_notify(struct net_device *dev,
19460 				       struct cfg80211_chan_def *chandef,
19461 				       unsigned int link_id, u8 count,
19462 				       bool quiet, u16 punct_bitmap)
19463 {
19464 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19465 	struct wiphy *wiphy = wdev->wiphy;
19466 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19467 
19468 	ASSERT_WDEV_LOCK(wdev);
19469 	WARN_INVALID_LINK_ID(wdev, link_id);
19470 
19471 	trace_cfg80211_ch_switch_started_notify(dev, chandef, link_id,
19472 						punct_bitmap);
19473 
19474 
19475 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19476 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY,
19477 				 count, quiet, punct_bitmap);
19478 }
19479 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
19480 
19481 int cfg80211_bss_color_notify(struct net_device *dev,
19482 			      enum nl80211_commands cmd, u8 count,
19483 			      u64 color_bitmap)
19484 {
19485 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19486 	struct wiphy *wiphy = wdev->wiphy;
19487 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19488 	struct sk_buff *msg;
19489 	void *hdr;
19490 
19491 	ASSERT_WDEV_LOCK(wdev);
19492 
19493 	trace_cfg80211_bss_color_notify(dev, cmd, count, color_bitmap);
19494 
19495 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
19496 	if (!msg)
19497 		return -ENOMEM;
19498 
19499 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
19500 	if (!hdr)
19501 		goto nla_put_failure;
19502 
19503 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19504 		goto nla_put_failure;
19505 
19506 	if (cmd == NL80211_CMD_COLOR_CHANGE_STARTED &&
19507 	    nla_put_u32(msg, NL80211_ATTR_COLOR_CHANGE_COUNT, count))
19508 		goto nla_put_failure;
19509 
19510 	if (cmd == NL80211_CMD_OBSS_COLOR_COLLISION &&
19511 	    nla_put_u64_64bit(msg, NL80211_ATTR_OBSS_COLOR_BITMAP,
19512 			      color_bitmap, NL80211_ATTR_PAD))
19513 		goto nla_put_failure;
19514 
19515 	genlmsg_end(msg, hdr);
19516 
19517 	return genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
19518 				       msg, 0, NL80211_MCGRP_MLME, GFP_KERNEL);
19519 
19520 nla_put_failure:
19521 	nlmsg_free(msg);
19522 	return -EINVAL;
19523 }
19524 EXPORT_SYMBOL(cfg80211_bss_color_notify);
19525 
19526 void
19527 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
19528 		     const struct cfg80211_chan_def *chandef,
19529 		     enum nl80211_radar_event event,
19530 		     struct net_device *netdev, gfp_t gfp)
19531 {
19532 	struct sk_buff *msg;
19533 	void *hdr;
19534 
19535 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19536 	if (!msg)
19537 		return;
19538 
19539 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
19540 	if (!hdr) {
19541 		nlmsg_free(msg);
19542 		return;
19543 	}
19544 
19545 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19546 		goto nla_put_failure;
19547 
19548 	/* NOP and radar events don't need a netdev parameter */
19549 	if (netdev) {
19550 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
19551 
19552 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19553 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19554 				      NL80211_ATTR_PAD))
19555 			goto nla_put_failure;
19556 	}
19557 
19558 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
19559 		goto nla_put_failure;
19560 
19561 	if (nl80211_send_chandef(msg, chandef))
19562 		goto nla_put_failure;
19563 
19564 	genlmsg_end(msg, hdr);
19565 
19566 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19567 				NL80211_MCGRP_MLME, gfp);
19568 	return;
19569 
19570  nla_put_failure:
19571 	nlmsg_free(msg);
19572 }
19573 
19574 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
19575 				       struct sta_opmode_info *sta_opmode,
19576 				       gfp_t gfp)
19577 {
19578 	struct sk_buff *msg;
19579 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19580 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19581 	void *hdr;
19582 
19583 	if (WARN_ON(!mac))
19584 		return;
19585 
19586 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19587 	if (!msg)
19588 		return;
19589 
19590 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
19591 	if (!hdr) {
19592 		nlmsg_free(msg);
19593 		return;
19594 	}
19595 
19596 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19597 		goto nla_put_failure;
19598 
19599 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19600 		goto nla_put_failure;
19601 
19602 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
19603 		goto nla_put_failure;
19604 
19605 	if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
19606 	    nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
19607 		goto nla_put_failure;
19608 
19609 	if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
19610 	    nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
19611 		goto nla_put_failure;
19612 
19613 	if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
19614 	    nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
19615 		goto nla_put_failure;
19616 
19617 	genlmsg_end(msg, hdr);
19618 
19619 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19620 				NL80211_MCGRP_MLME, gfp);
19621 
19622 	return;
19623 
19624 nla_put_failure:
19625 	nlmsg_free(msg);
19626 }
19627 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
19628 
19629 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
19630 			   u64 cookie, bool acked, s32 ack_signal,
19631 			   bool is_valid_ack_signal, gfp_t gfp)
19632 {
19633 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19634 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19635 	struct sk_buff *msg;
19636 	void *hdr;
19637 
19638 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
19639 
19640 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19641 
19642 	if (!msg)
19643 		return;
19644 
19645 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
19646 	if (!hdr) {
19647 		nlmsg_free(msg);
19648 		return;
19649 	}
19650 
19651 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19652 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19653 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
19654 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
19655 			      NL80211_ATTR_PAD) ||
19656 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
19657 	    (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
19658 						ack_signal)))
19659 		goto nla_put_failure;
19660 
19661 	genlmsg_end(msg, hdr);
19662 
19663 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19664 				NL80211_MCGRP_MLME, gfp);
19665 	return;
19666 
19667  nla_put_failure:
19668 	nlmsg_free(msg);
19669 }
19670 EXPORT_SYMBOL(cfg80211_probe_status);
19671 
19672 void cfg80211_report_obss_beacon_khz(struct wiphy *wiphy, const u8 *frame,
19673 				     size_t len, int freq, int sig_dbm)
19674 {
19675 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19676 	struct sk_buff *msg;
19677 	void *hdr;
19678 	struct cfg80211_beacon_registration *reg;
19679 
19680 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
19681 
19682 	spin_lock_bh(&rdev->beacon_registrations_lock);
19683 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
19684 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
19685 		if (!msg) {
19686 			spin_unlock_bh(&rdev->beacon_registrations_lock);
19687 			return;
19688 		}
19689 
19690 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
19691 		if (!hdr)
19692 			goto nla_put_failure;
19693 
19694 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19695 		    (freq &&
19696 		     (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
19697 				  KHZ_TO_MHZ(freq)) ||
19698 		      nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
19699 				  freq % 1000))) ||
19700 		    (sig_dbm &&
19701 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
19702 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
19703 			goto nla_put_failure;
19704 
19705 		genlmsg_end(msg, hdr);
19706 
19707 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
19708 	}
19709 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19710 	return;
19711 
19712  nla_put_failure:
19713 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19714 	nlmsg_free(msg);
19715 }
19716 EXPORT_SYMBOL(cfg80211_report_obss_beacon_khz);
19717 
19718 #ifdef CONFIG_PM
19719 static int cfg80211_net_detect_results(struct sk_buff *msg,
19720 				       struct cfg80211_wowlan_wakeup *wakeup)
19721 {
19722 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
19723 	struct nlattr *nl_results, *nl_match, *nl_freqs;
19724 	int i, j;
19725 
19726 	nl_results = nla_nest_start_noflag(msg,
19727 					   NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
19728 	if (!nl_results)
19729 		return -EMSGSIZE;
19730 
19731 	for (i = 0; i < nd->n_matches; i++) {
19732 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
19733 
19734 		nl_match = nla_nest_start_noflag(msg, i);
19735 		if (!nl_match)
19736 			break;
19737 
19738 		/* The SSID attribute is optional in nl80211, but for
19739 		 * simplicity reasons it's always present in the
19740 		 * cfg80211 structure.  If a driver can't pass the
19741 		 * SSID, that needs to be changed.  A zero length SSID
19742 		 * is still a valid SSID (wildcard), so it cannot be
19743 		 * used for this purpose.
19744 		 */
19745 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
19746 			    match->ssid.ssid)) {
19747 			nla_nest_cancel(msg, nl_match);
19748 			goto out;
19749 		}
19750 
19751 		if (match->n_channels) {
19752 			nl_freqs = nla_nest_start_noflag(msg,
19753 							 NL80211_ATTR_SCAN_FREQUENCIES);
19754 			if (!nl_freqs) {
19755 				nla_nest_cancel(msg, nl_match);
19756 				goto out;
19757 			}
19758 
19759 			for (j = 0; j < match->n_channels; j++) {
19760 				if (nla_put_u32(msg, j, match->channels[j])) {
19761 					nla_nest_cancel(msg, nl_freqs);
19762 					nla_nest_cancel(msg, nl_match);
19763 					goto out;
19764 				}
19765 			}
19766 
19767 			nla_nest_end(msg, nl_freqs);
19768 		}
19769 
19770 		nla_nest_end(msg, nl_match);
19771 	}
19772 
19773 out:
19774 	nla_nest_end(msg, nl_results);
19775 	return 0;
19776 }
19777 
19778 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
19779 				   struct cfg80211_wowlan_wakeup *wakeup,
19780 				   gfp_t gfp)
19781 {
19782 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19783 	struct sk_buff *msg;
19784 	void *hdr;
19785 	int size = 200;
19786 
19787 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
19788 
19789 	if (wakeup)
19790 		size += wakeup->packet_present_len;
19791 
19792 	msg = nlmsg_new(size, gfp);
19793 	if (!msg)
19794 		return;
19795 
19796 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
19797 	if (!hdr)
19798 		goto free_msg;
19799 
19800 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19801 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19802 			      NL80211_ATTR_PAD))
19803 		goto free_msg;
19804 
19805 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
19806 					wdev->netdev->ifindex))
19807 		goto free_msg;
19808 
19809 	if (wakeup) {
19810 		struct nlattr *reasons;
19811 
19812 		reasons = nla_nest_start_noflag(msg,
19813 						NL80211_ATTR_WOWLAN_TRIGGERS);
19814 		if (!reasons)
19815 			goto free_msg;
19816 
19817 		if (wakeup->disconnect &&
19818 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
19819 			goto free_msg;
19820 		if (wakeup->magic_pkt &&
19821 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
19822 			goto free_msg;
19823 		if (wakeup->gtk_rekey_failure &&
19824 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
19825 			goto free_msg;
19826 		if (wakeup->eap_identity_req &&
19827 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
19828 			goto free_msg;
19829 		if (wakeup->four_way_handshake &&
19830 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
19831 			goto free_msg;
19832 		if (wakeup->rfkill_release &&
19833 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
19834 			goto free_msg;
19835 
19836 		if (wakeup->pattern_idx >= 0 &&
19837 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
19838 				wakeup->pattern_idx))
19839 			goto free_msg;
19840 
19841 		if (wakeup->tcp_match &&
19842 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
19843 			goto free_msg;
19844 
19845 		if (wakeup->tcp_connlost &&
19846 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
19847 			goto free_msg;
19848 
19849 		if (wakeup->tcp_nomoretokens &&
19850 		    nla_put_flag(msg,
19851 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
19852 			goto free_msg;
19853 
19854 		if (wakeup->packet) {
19855 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
19856 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
19857 
19858 			if (!wakeup->packet_80211) {
19859 				pkt_attr =
19860 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
19861 				len_attr =
19862 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
19863 			}
19864 
19865 			if (wakeup->packet_len &&
19866 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
19867 				goto free_msg;
19868 
19869 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
19870 				    wakeup->packet))
19871 				goto free_msg;
19872 		}
19873 
19874 		if (wakeup->net_detect &&
19875 		    cfg80211_net_detect_results(msg, wakeup))
19876 				goto free_msg;
19877 
19878 		nla_nest_end(msg, reasons);
19879 	}
19880 
19881 	genlmsg_end(msg, hdr);
19882 
19883 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19884 				NL80211_MCGRP_MLME, gfp);
19885 	return;
19886 
19887  free_msg:
19888 	nlmsg_free(msg);
19889 }
19890 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
19891 #endif
19892 
19893 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
19894 				enum nl80211_tdls_operation oper,
19895 				u16 reason_code, gfp_t gfp)
19896 {
19897 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19898 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19899 	struct sk_buff *msg;
19900 	void *hdr;
19901 
19902 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
19903 					 reason_code);
19904 
19905 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19906 	if (!msg)
19907 		return;
19908 
19909 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
19910 	if (!hdr) {
19911 		nlmsg_free(msg);
19912 		return;
19913 	}
19914 
19915 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19916 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19917 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
19918 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
19919 	    (reason_code > 0 &&
19920 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
19921 		goto nla_put_failure;
19922 
19923 	genlmsg_end(msg, hdr);
19924 
19925 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19926 				NL80211_MCGRP_MLME, gfp);
19927 	return;
19928 
19929  nla_put_failure:
19930 	nlmsg_free(msg);
19931 }
19932 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
19933 
19934 static int nl80211_netlink_notify(struct notifier_block * nb,
19935 				  unsigned long state,
19936 				  void *_notify)
19937 {
19938 	struct netlink_notify *notify = _notify;
19939 	struct cfg80211_registered_device *rdev;
19940 	struct wireless_dev *wdev;
19941 	struct cfg80211_beacon_registration *reg, *tmp;
19942 
19943 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
19944 		return NOTIFY_DONE;
19945 
19946 	rcu_read_lock();
19947 
19948 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
19949 		struct cfg80211_sched_scan_request *sched_scan_req;
19950 
19951 		list_for_each_entry_rcu(sched_scan_req,
19952 					&rdev->sched_scan_req_list,
19953 					list) {
19954 			if (sched_scan_req->owner_nlportid == notify->portid) {
19955 				sched_scan_req->nl_owner_dead = true;
19956 				wiphy_work_queue(&rdev->wiphy,
19957 						 &rdev->sched_scan_stop_wk);
19958 			}
19959 		}
19960 
19961 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
19962 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
19963 
19964 			if (wdev->owner_nlportid == notify->portid) {
19965 				wdev->nl_owner_dead = true;
19966 				schedule_work(&rdev->destroy_work);
19967 			} else if (wdev->conn_owner_nlportid == notify->portid) {
19968 				schedule_work(&wdev->disconnect_wk);
19969 			}
19970 
19971 			cfg80211_release_pmsr(wdev, notify->portid);
19972 		}
19973 
19974 		spin_lock_bh(&rdev->beacon_registrations_lock);
19975 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
19976 					 list) {
19977 			if (reg->nlportid == notify->portid) {
19978 				list_del(&reg->list);
19979 				kfree(reg);
19980 				break;
19981 			}
19982 		}
19983 		spin_unlock_bh(&rdev->beacon_registrations_lock);
19984 	}
19985 
19986 	rcu_read_unlock();
19987 
19988 	/*
19989 	 * It is possible that the user space process that is controlling the
19990 	 * indoor setting disappeared, so notify the regulatory core.
19991 	 */
19992 	regulatory_netlink_notify(notify->portid);
19993 	return NOTIFY_OK;
19994 }
19995 
19996 static struct notifier_block nl80211_netlink_notifier = {
19997 	.notifier_call = nl80211_netlink_notify,
19998 };
19999 
20000 void cfg80211_ft_event(struct net_device *netdev,
20001 		       struct cfg80211_ft_event_params *ft_event)
20002 {
20003 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
20004 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
20005 	struct sk_buff *msg;
20006 	void *hdr;
20007 
20008 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
20009 
20010 	if (!ft_event->target_ap)
20011 		return;
20012 
20013 	msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
20014 			GFP_KERNEL);
20015 	if (!msg)
20016 		return;
20017 
20018 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
20019 	if (!hdr)
20020 		goto out;
20021 
20022 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20023 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
20024 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
20025 		goto out;
20026 
20027 	if (ft_event->ies &&
20028 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
20029 		goto out;
20030 	if (ft_event->ric_ies &&
20031 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
20032 		    ft_event->ric_ies))
20033 		goto out;
20034 
20035 	genlmsg_end(msg, hdr);
20036 
20037 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
20038 				NL80211_MCGRP_MLME, GFP_KERNEL);
20039 	return;
20040  out:
20041 	nlmsg_free(msg);
20042 }
20043 EXPORT_SYMBOL(cfg80211_ft_event);
20044 
20045 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
20046 {
20047 	struct cfg80211_registered_device *rdev;
20048 	struct sk_buff *msg;
20049 	void *hdr;
20050 	u32 nlportid;
20051 
20052 	rdev = wiphy_to_rdev(wdev->wiphy);
20053 	if (!rdev->crit_proto_nlportid)
20054 		return;
20055 
20056 	nlportid = rdev->crit_proto_nlportid;
20057 	rdev->crit_proto_nlportid = 0;
20058 
20059 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
20060 	if (!msg)
20061 		return;
20062 
20063 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
20064 	if (!hdr)
20065 		goto nla_put_failure;
20066 
20067 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20068 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
20069 			      NL80211_ATTR_PAD))
20070 		goto nla_put_failure;
20071 
20072 	genlmsg_end(msg, hdr);
20073 
20074 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
20075 	return;
20076 
20077  nla_put_failure:
20078 	nlmsg_free(msg);
20079 }
20080 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
20081 
20082 void nl80211_send_ap_stopped(struct wireless_dev *wdev, unsigned int link_id)
20083 {
20084 	struct wiphy *wiphy = wdev->wiphy;
20085 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
20086 	struct sk_buff *msg;
20087 	void *hdr;
20088 
20089 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
20090 	if (!msg)
20091 		return;
20092 
20093 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
20094 	if (!hdr)
20095 		goto out;
20096 
20097 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20098 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
20099 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
20100 			      NL80211_ATTR_PAD) ||
20101 	    (wdev->valid_links &&
20102 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)))
20103 		goto out;
20104 
20105 	genlmsg_end(msg, hdr);
20106 
20107 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
20108 				NL80211_MCGRP_MLME, GFP_KERNEL);
20109 	return;
20110  out:
20111 	nlmsg_free(msg);
20112 }
20113 
20114 int cfg80211_external_auth_request(struct net_device *dev,
20115 				   struct cfg80211_external_auth_params *params,
20116 				   gfp_t gfp)
20117 {
20118 	struct wireless_dev *wdev = dev->ieee80211_ptr;
20119 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
20120 	struct sk_buff *msg;
20121 	void *hdr;
20122 
20123 	if (!wdev->conn_owner_nlportid)
20124 		return -EINVAL;
20125 
20126 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
20127 	if (!msg)
20128 		return -ENOMEM;
20129 
20130 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
20131 	if (!hdr)
20132 		goto nla_put_failure;
20133 
20134 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20135 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
20136 	    nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
20137 	    nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
20138 			params->action) ||
20139 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
20140 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
20141 		    params->ssid.ssid) ||
20142 	    (!is_zero_ether_addr(params->mld_addr) &&
20143 	     nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN, params->mld_addr)))
20144 		goto nla_put_failure;
20145 
20146 	genlmsg_end(msg, hdr);
20147 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
20148 			wdev->conn_owner_nlportid);
20149 	return 0;
20150 
20151  nla_put_failure:
20152 	nlmsg_free(msg);
20153 	return -ENOBUFS;
20154 }
20155 EXPORT_SYMBOL(cfg80211_external_auth_request);
20156 
20157 void cfg80211_update_owe_info_event(struct net_device *netdev,
20158 				    struct cfg80211_update_owe_info *owe_info,
20159 				    gfp_t gfp)
20160 {
20161 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
20162 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
20163 	struct sk_buff *msg;
20164 	void *hdr;
20165 
20166 	trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
20167 
20168 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
20169 	if (!msg)
20170 		return;
20171 
20172 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
20173 	if (!hdr)
20174 		goto nla_put_failure;
20175 
20176 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20177 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
20178 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
20179 		goto nla_put_failure;
20180 
20181 	if (!owe_info->ie_len ||
20182 	    nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
20183 		goto nla_put_failure;
20184 
20185 	if (owe_info->assoc_link_id != -1) {
20186 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
20187 			       owe_info->assoc_link_id))
20188 			goto nla_put_failure;
20189 
20190 		if (!is_zero_ether_addr(owe_info->peer_mld_addr) &&
20191 		    nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
20192 			    owe_info->peer_mld_addr))
20193 			goto nla_put_failure;
20194 	}
20195 
20196 	genlmsg_end(msg, hdr);
20197 
20198 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
20199 				NL80211_MCGRP_MLME, gfp);
20200 	return;
20201 
20202 nla_put_failure:
20203 	genlmsg_cancel(msg, hdr);
20204 	nlmsg_free(msg);
20205 }
20206 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
20207 
20208 /* initialisation/exit functions */
20209 
20210 int __init nl80211_init(void)
20211 {
20212 	int err;
20213 
20214 	err = genl_register_family(&nl80211_fam);
20215 	if (err)
20216 		return err;
20217 
20218 	err = netlink_register_notifier(&nl80211_netlink_notifier);
20219 	if (err)
20220 		goto err_out;
20221 
20222 	return 0;
20223  err_out:
20224 	genl_unregister_family(&nl80211_fam);
20225 	return err;
20226 }
20227 
20228 void nl80211_exit(void)
20229 {
20230 	netlink_unregister_notifier(&nl80211_netlink_notifier);
20231 	genl_unregister_family(&nl80211_fam);
20232 }
20233