xref: /openbmc/linux/net/wireless/nl80211.c (revision c14679d7)
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 };
327 
328 static const struct nla_policy
329 nl80211_pmsr_req_data_policy[NL80211_PMSR_TYPE_MAX + 1] = {
330 	[NL80211_PMSR_TYPE_FTM] =
331 		NLA_POLICY_NESTED(nl80211_pmsr_ftm_req_attr_policy),
332 };
333 
334 static const struct nla_policy
335 nl80211_pmsr_req_attr_policy[NL80211_PMSR_REQ_ATTR_MAX + 1] = {
336 	[NL80211_PMSR_REQ_ATTR_DATA] =
337 		NLA_POLICY_NESTED(nl80211_pmsr_req_data_policy),
338 	[NL80211_PMSR_REQ_ATTR_GET_AP_TSF] = { .type = NLA_FLAG },
339 };
340 
341 static const struct nla_policy
342 nl80211_pmsr_peer_attr_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
343 	[NL80211_PMSR_PEER_ATTR_ADDR] = NLA_POLICY_ETH_ADDR,
344 	[NL80211_PMSR_PEER_ATTR_CHAN] = NLA_POLICY_NESTED(nl80211_policy),
345 	[NL80211_PMSR_PEER_ATTR_REQ] =
346 		NLA_POLICY_NESTED(nl80211_pmsr_req_attr_policy),
347 	[NL80211_PMSR_PEER_ATTR_RESP] = { .type = NLA_REJECT },
348 };
349 
350 static const struct nla_policy
351 nl80211_pmsr_attr_policy[NL80211_PMSR_ATTR_MAX + 1] = {
352 	[NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_REJECT },
353 	[NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_REJECT },
354 	[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
355 	[NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
356 	[NL80211_PMSR_ATTR_PEERS] =
357 		NLA_POLICY_NESTED_ARRAY(nl80211_pmsr_peer_attr_policy),
358 };
359 
360 static const struct nla_policy
361 he_obss_pd_policy[NL80211_HE_OBSS_PD_ATTR_MAX + 1] = {
362 	[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] =
363 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
364 	[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET] =
365 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
366 	[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET] =
367 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
368 	[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP] =
369 		NLA_POLICY_EXACT_LEN(8),
370 	[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP] =
371 		NLA_POLICY_EXACT_LEN(8),
372 	[NL80211_HE_OBSS_PD_ATTR_SR_CTRL] = { .type = NLA_U8 },
373 };
374 
375 static const struct nla_policy
376 he_bss_color_policy[NL80211_HE_BSS_COLOR_ATTR_MAX + 1] = {
377 	[NL80211_HE_BSS_COLOR_ATTR_COLOR] = NLA_POLICY_RANGE(NLA_U8, 1, 63),
378 	[NL80211_HE_BSS_COLOR_ATTR_DISABLED] = { .type = NLA_FLAG },
379 	[NL80211_HE_BSS_COLOR_ATTR_PARTIAL] = { .type = NLA_FLAG },
380 };
381 
382 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
383 	[NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
384 				    .len = NL80211_MAX_SUPP_RATES },
385 	[NL80211_TXRATE_HT] = { .type = NLA_BINARY,
386 				.len = NL80211_MAX_SUPP_HT_RATES },
387 	[NL80211_TXRATE_VHT] = NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_txrate_vht)),
388 	[NL80211_TXRATE_GI] = { .type = NLA_U8 },
389 	[NL80211_TXRATE_HE] = NLA_POLICY_EXACT_LEN(sizeof(struct nl80211_txrate_he)),
390 	[NL80211_TXRATE_HE_GI] =  NLA_POLICY_RANGE(NLA_U8,
391 						   NL80211_RATE_INFO_HE_GI_0_8,
392 						   NL80211_RATE_INFO_HE_GI_3_2),
393 	[NL80211_TXRATE_HE_LTF] = NLA_POLICY_RANGE(NLA_U8,
394 						   NL80211_RATE_INFO_HE_1XLTF,
395 						   NL80211_RATE_INFO_HE_4XLTF),
396 };
397 
398 static const struct nla_policy
399 nl80211_tid_config_attr_policy[NL80211_TID_CONFIG_ATTR_MAX + 1] = {
400 	[NL80211_TID_CONFIG_ATTR_VIF_SUPP] = { .type = NLA_U64 },
401 	[NL80211_TID_CONFIG_ATTR_PEER_SUPP] = { .type = NLA_U64 },
402 	[NL80211_TID_CONFIG_ATTR_OVERRIDE] = { .type = NLA_FLAG },
403 	[NL80211_TID_CONFIG_ATTR_TIDS] = NLA_POLICY_RANGE(NLA_U16, 1, 0xff),
404 	[NL80211_TID_CONFIG_ATTR_NOACK] =
405 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
406 	[NL80211_TID_CONFIG_ATTR_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
407 	[NL80211_TID_CONFIG_ATTR_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
408 	[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL] =
409 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
410 	[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL] =
411 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
412 	[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL] =
413 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
414 	[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE] =
415 			NLA_POLICY_MAX(NLA_U8, NL80211_TX_RATE_FIXED),
416 	[NL80211_TID_CONFIG_ATTR_TX_RATE] =
417 			NLA_POLICY_NESTED(nl80211_txattr_policy),
418 };
419 
420 static const struct nla_policy
421 nl80211_fils_discovery_policy[NL80211_FILS_DISCOVERY_ATTR_MAX + 1] = {
422 	[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] = NLA_POLICY_MAX(NLA_U32, 10000),
423 	[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] = NLA_POLICY_MAX(NLA_U32, 10000),
424 	[NL80211_FILS_DISCOVERY_ATTR_TMPL] =
425 			NLA_POLICY_RANGE(NLA_BINARY,
426 					 NL80211_FILS_DISCOVERY_TMPL_MIN_LEN,
427 					 IEEE80211_MAX_DATA_LEN),
428 };
429 
430 static const struct nla_policy
431 nl80211_unsol_bcast_probe_resp_policy[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1] = {
432 	[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] = NLA_POLICY_MAX(NLA_U32, 20),
433 	[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL] = { .type = NLA_BINARY,
434 						       .len = IEEE80211_MAX_DATA_LEN }
435 };
436 
437 static const struct nla_policy
438 sar_specs_policy[NL80211_SAR_ATTR_SPECS_MAX + 1] = {
439 	[NL80211_SAR_ATTR_SPECS_POWER] = { .type = NLA_S32 },
440 	[NL80211_SAR_ATTR_SPECS_RANGE_INDEX] = {.type = NLA_U32 },
441 };
442 
443 static const struct nla_policy
444 sar_policy[NL80211_SAR_ATTR_MAX + 1] = {
445 	[NL80211_SAR_ATTR_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_SAR_TYPE),
446 	[NL80211_SAR_ATTR_SPECS] = NLA_POLICY_NESTED_ARRAY(sar_specs_policy),
447 };
448 
449 static const struct nla_policy
450 nl80211_mbssid_config_policy[NL80211_MBSSID_CONFIG_ATTR_MAX + 1] = {
451 	[NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES] = NLA_POLICY_MIN(NLA_U8, 2),
452 	[NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY] =
453 						NLA_POLICY_MIN(NLA_U8, 1),
454 	[NL80211_MBSSID_CONFIG_ATTR_INDEX] = { .type = NLA_U8 },
455 	[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX] = { .type = NLA_U32 },
456 	[NL80211_MBSSID_CONFIG_ATTR_EMA] = { .type = NLA_FLAG },
457 };
458 
459 static const struct nla_policy
460 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
461 	[NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
462 	[NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
463 };
464 
465 static struct netlink_range_validation nl80211_punct_bitmap_range = {
466 	.min = 0,
467 	.max = 0xffff,
468 };
469 
470 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
471 	[0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
472 	[NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
473 	[NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
474 				      .len = 20-1 },
475 	[NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
476 
477 	[NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
478 	[NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
479 	[NL80211_ATTR_WIPHY_EDMG_CHANNELS] = NLA_POLICY_RANGE(NLA_U8,
480 						NL80211_EDMG_CHANNELS_MIN,
481 						NL80211_EDMG_CHANNELS_MAX),
482 	[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG] = NLA_POLICY_RANGE(NLA_U8,
483 						NL80211_EDMG_BW_CONFIG_MIN,
484 						NL80211_EDMG_BW_CONFIG_MAX),
485 
486 	[NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
487 	[NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
488 	[NL80211_ATTR_CENTER_FREQ1_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
489 	[NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
490 
491 	[NL80211_ATTR_WIPHY_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
492 	[NL80211_ATTR_WIPHY_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
493 	[NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
494 	[NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
495 	[NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
496 	[NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
497 
498 	[NL80211_ATTR_IFTYPE] = NLA_POLICY_MAX(NLA_U32, NL80211_IFTYPE_MAX),
499 	[NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
500 	[NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
501 
502 	[NL80211_ATTR_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
503 	[NL80211_ATTR_PREV_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
504 
505 	[NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
506 	[NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
507 				    .len = WLAN_MAX_KEY_LEN },
508 	[NL80211_ATTR_KEY_IDX] = NLA_POLICY_MAX(NLA_U8, 7),
509 	[NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
510 	[NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
511 	[NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
512 	[NL80211_ATTR_KEY_TYPE] =
513 		NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES),
514 
515 	[NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
516 	[NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
517 	[NL80211_ATTR_BEACON_HEAD] =
518 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_beacon_head,
519 				       IEEE80211_MAX_DATA_LEN),
520 	[NL80211_ATTR_BEACON_TAIL] =
521 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
522 				       IEEE80211_MAX_DATA_LEN),
523 	[NL80211_ATTR_STA_AID] =
524 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
525 	[NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
526 	[NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
527 	[NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
528 					       .len = NL80211_MAX_SUPP_RATES },
529 	[NL80211_ATTR_STA_PLINK_ACTION] =
530 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_ACTIONS - 1),
531 	[NL80211_ATTR_STA_TX_POWER_SETTING] =
532 		NLA_POLICY_RANGE(NLA_U8,
533 				 NL80211_TX_POWER_AUTOMATIC,
534 				 NL80211_TX_POWER_FIXED),
535 	[NL80211_ATTR_STA_TX_POWER] = { .type = NLA_S16 },
536 	[NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
537 	[NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
538 	[NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
539 				   .len = IEEE80211_MAX_MESH_ID_LEN },
540 	[NL80211_ATTR_MPATH_NEXT_HOP] = NLA_POLICY_ETH_ADDR_COMPAT,
541 
542 	/* allow 3 for NUL-termination, we used to declare this NLA_STRING */
543 	[NL80211_ATTR_REG_ALPHA2] = NLA_POLICY_RANGE(NLA_BINARY, 2, 3),
544 	[NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
545 
546 	[NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
547 	[NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
548 	[NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
549 	[NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
550 					   .len = NL80211_MAX_SUPP_RATES },
551 	[NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
552 
553 	[NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
554 	[NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
555 
556 	[NL80211_ATTR_HT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_HT_CAPABILITY_LEN),
557 
558 	[NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
559 	[NL80211_ATTR_IE] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
560 						   validate_ie_attr,
561 						   IEEE80211_MAX_DATA_LEN),
562 	[NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
563 	[NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
564 
565 	[NL80211_ATTR_SSID] = { .type = NLA_BINARY,
566 				.len = IEEE80211_MAX_SSID_LEN },
567 	[NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
568 	[NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
569 	[NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
570 	[NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
571 	[NL80211_ATTR_USE_MFP] = NLA_POLICY_RANGE(NLA_U32,
572 						  NL80211_MFP_NO,
573 						  NL80211_MFP_OPTIONAL),
574 	[NL80211_ATTR_STA_FLAGS2] =
575 		NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_sta_flag_update)),
576 	[NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
577 	[NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
578 	[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
579 	[NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
580 	[NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
581 	[NL80211_ATTR_STATUS_CODE] = { .type = NLA_U16 },
582 	[NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
583 	[NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
584 	[NL80211_ATTR_PID] = { .type = NLA_U32 },
585 	[NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
586 	[NL80211_ATTR_PMKID] = NLA_POLICY_EXACT_LEN_WARN(WLAN_PMKID_LEN),
587 	[NL80211_ATTR_DURATION] = { .type = NLA_U32 },
588 	[NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
589 	[NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
590 	[NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
591 				 .len = IEEE80211_MAX_DATA_LEN },
592 	[NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
593 	[NL80211_ATTR_PS_STATE] = NLA_POLICY_RANGE(NLA_U32,
594 						   NL80211_PS_DISABLED,
595 						   NL80211_PS_ENABLED),
596 	[NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
597 	[NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
598 	[NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
599 	[NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
600 	[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
601 	[NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
602 	[NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
603 	[NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
604 	[NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
605 	[NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
606 	[NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
607 	[NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
608 	[NL80211_ATTR_STA_PLINK_STATE] =
609 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_STATES - 1),
610 	[NL80211_ATTR_MEASUREMENT_DURATION] = { .type = NLA_U16 },
611 	[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY] = { .type = NLA_FLAG },
612 	[NL80211_ATTR_MESH_PEER_AID] =
613 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
614 	[NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
615 	[NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
616 	[NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
617 	[NL80211_ATTR_HIDDEN_SSID] =
618 		NLA_POLICY_RANGE(NLA_U32,
619 				 NL80211_HIDDEN_SSID_NOT_IN_USE,
620 				 NL80211_HIDDEN_SSID_ZERO_CONTENTS),
621 	[NL80211_ATTR_IE_PROBE_RESP] =
622 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
623 				       IEEE80211_MAX_DATA_LEN),
624 	[NL80211_ATTR_IE_ASSOC_RESP] =
625 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
626 				       IEEE80211_MAX_DATA_LEN),
627 	[NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
628 	[NL80211_ATTR_STA_WME] = NLA_POLICY_NESTED(nl80211_sta_wme_policy),
629 	[NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
630 	[NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
631 	[NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
632 	[NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
633 	[NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
634 	[NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
635 	[NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
636 	[NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
637 	[NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
638 	[NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
639 				      .len = IEEE80211_MAX_DATA_LEN },
640 	[NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
641 	[NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
642 	[NL80211_ATTR_HT_CAPABILITY_MASK] = {
643 		.len = NL80211_HT_CAPABILITY_LEN
644 	},
645 	[NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
646 	[NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
647 	[NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
648 	[NL80211_ATTR_WDEV] = { .type = NLA_U64 },
649 	[NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
650 
651 	/* need to include at least Auth Transaction and Status Code */
652 	[NL80211_ATTR_AUTH_DATA] = NLA_POLICY_MIN_LEN(4),
653 
654 	[NL80211_ATTR_VHT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_VHT_CAPABILITY_LEN),
655 	[NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
656 	[NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
657 	[NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
658 	[NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
659 		NLA_POLICY_RANGE(NLA_U32,
660 				 NL80211_MESH_POWER_UNKNOWN + 1,
661 				 NL80211_MESH_POWER_MAX),
662 	[NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
663 	[NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
664 	[NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
665 	[NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
666 	[NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
667 	[NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
668 	[NL80211_ATTR_VHT_CAPABILITY_MASK] = {
669 		.len = NL80211_VHT_CAPABILITY_LEN,
670 	},
671 	[NL80211_ATTR_MDID] = { .type = NLA_U16 },
672 	[NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
673 				  .len = IEEE80211_MAX_DATA_LEN },
674 	[NL80211_ATTR_CRIT_PROT_ID] = { .type = NLA_U16 },
675 	[NL80211_ATTR_MAX_CRIT_PROT_DURATION] =
676 		NLA_POLICY_MAX(NLA_U16, NL80211_CRIT_PROTO_MAX_DURATION),
677 	[NL80211_ATTR_PEER_AID] =
678 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
679 	[NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
680 	[NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
681 	[NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
682 	[NL80211_ATTR_CNTDWN_OFFS_BEACON] = { .type = NLA_BINARY },
683 	[NL80211_ATTR_CNTDWN_OFFS_PRESP] = { .type = NLA_BINARY },
684 	[NL80211_ATTR_STA_SUPPORTED_CHANNELS] = NLA_POLICY_MIN_LEN(2),
685 	/*
686 	 * The value of the Length field of the Supported Operating
687 	 * Classes element is between 2 and 253.
688 	 */
689 	[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] =
690 		NLA_POLICY_RANGE(NLA_BINARY, 2, 253),
691 	[NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
692 	[NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
693 	[NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
694 	[NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
695 	[NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
696 	[NL80211_ATTR_QOS_MAP] = NLA_POLICY_RANGE(NLA_BINARY,
697 						  IEEE80211_QOS_MAP_LEN_MIN,
698 						  IEEE80211_QOS_MAP_LEN_MAX),
699 	[NL80211_ATTR_MAC_HINT] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
700 	[NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
701 	[NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
702 	[NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
703 	[NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
704 	[NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
705 	[NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
706 	[NL80211_ATTR_USER_PRIO] =
707 		NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
708 	[NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
709 	[NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
710 	[NL80211_ATTR_OPER_CLASS] = { .type = NLA_U8 },
711 	[NL80211_ATTR_MAC_MASK] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
712 	[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
713 	[NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
714 	[NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
715 	[NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
716 	[NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
717 	[NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
718 	[NL80211_ATTR_STA_SUPPORT_P2P_PS] =
719 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
720 	[NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
721 		.len = VHT_MUMIMO_GROUPS_DATA_LEN
722 	},
723 	[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
724 	[NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
725 	[NL80211_ATTR_BANDS] = { .type = NLA_U32 },
726 	[NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
727 	[NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
728 				    .len = FILS_MAX_KEK_LEN },
729 	[NL80211_ATTR_FILS_NONCES] = NLA_POLICY_EXACT_LEN_WARN(2 * FILS_NONCE_LEN),
730 	[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
731 	[NL80211_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
732 	[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
733 	[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
734 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
735 	},
736 	[NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
737 	[NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
738 					     .len = FILS_ERP_MAX_USERNAME_LEN },
739 	[NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
740 					  .len = FILS_ERP_MAX_REALM_LEN },
741 	[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
742 	[NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
743 					.len = FILS_ERP_MAX_RRK_LEN },
744 	[NL80211_ATTR_FILS_CACHE_ID] = NLA_POLICY_EXACT_LEN_WARN(2),
745 	[NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
746 	[NL80211_ATTR_PMKR0_NAME] = NLA_POLICY_EXACT_LEN(WLAN_PMK_NAME_LEN),
747 	[NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
748 	[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
749 
750 	[NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
751 	[NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
752 	[NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
753 	[NL80211_ATTR_HE_CAPABILITY] =
754 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_he_capa,
755 				       NL80211_HE_MAX_CAPABILITY_LEN),
756 	[NL80211_ATTR_FTM_RESPONDER] =
757 		NLA_POLICY_NESTED(nl80211_ftm_responder_policy),
758 	[NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
759 	[NL80211_ATTR_PEER_MEASUREMENTS] =
760 		NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
761 	[NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
762 	[NL80211_ATTR_SAE_PASSWORD] = { .type = NLA_BINARY,
763 					.len = SAE_PASSWORD_MAX_LEN },
764 	[NL80211_ATTR_TWT_RESPONDER] = { .type = NLA_FLAG },
765 	[NL80211_ATTR_HE_OBSS_PD] = NLA_POLICY_NESTED(he_obss_pd_policy),
766 	[NL80211_ATTR_VLAN_ID] = NLA_POLICY_RANGE(NLA_U16, 1, VLAN_N_VID - 2),
767 	[NL80211_ATTR_HE_BSS_COLOR] = NLA_POLICY_NESTED(he_bss_color_policy),
768 	[NL80211_ATTR_TID_CONFIG] =
769 		NLA_POLICY_NESTED_ARRAY(nl80211_tid_config_attr_policy),
770 	[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH] = { .type = NLA_FLAG },
771 	[NL80211_ATTR_PMK_LIFETIME] = NLA_POLICY_MIN(NLA_U32, 1),
772 	[NL80211_ATTR_PMK_REAUTH_THRESHOLD] = NLA_POLICY_RANGE(NLA_U8, 1, 100),
773 	[NL80211_ATTR_RECEIVE_MULTICAST] = { .type = NLA_FLAG },
774 	[NL80211_ATTR_WIPHY_FREQ_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
775 	[NL80211_ATTR_SCAN_FREQ_KHZ] = { .type = NLA_NESTED },
776 	[NL80211_ATTR_HE_6GHZ_CAPABILITY] =
777 		NLA_POLICY_EXACT_LEN(sizeof(struct ieee80211_he_6ghz_capa)),
778 	[NL80211_ATTR_FILS_DISCOVERY] =
779 		NLA_POLICY_NESTED(nl80211_fils_discovery_policy),
780 	[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP] =
781 		NLA_POLICY_NESTED(nl80211_unsol_bcast_probe_resp_policy),
782 	[NL80211_ATTR_S1G_CAPABILITY] =
783 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
784 	[NL80211_ATTR_S1G_CAPABILITY_MASK] =
785 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
786 	[NL80211_ATTR_SAE_PWE] =
787 		NLA_POLICY_RANGE(NLA_U8, NL80211_SAE_PWE_HUNT_AND_PECK,
788 				 NL80211_SAE_PWE_BOTH),
789 	[NL80211_ATTR_RECONNECT_REQUESTED] = { .type = NLA_REJECT },
790 	[NL80211_ATTR_SAR_SPEC] = NLA_POLICY_NESTED(sar_policy),
791 	[NL80211_ATTR_DISABLE_HE] = { .type = NLA_FLAG },
792 	[NL80211_ATTR_OBSS_COLOR_BITMAP] = { .type = NLA_U64 },
793 	[NL80211_ATTR_COLOR_CHANGE_COUNT] = { .type = NLA_U8 },
794 	[NL80211_ATTR_COLOR_CHANGE_COLOR] = { .type = NLA_U8 },
795 	[NL80211_ATTR_COLOR_CHANGE_ELEMS] = NLA_POLICY_NESTED(nl80211_policy),
796 	[NL80211_ATTR_MBSSID_CONFIG] =
797 			NLA_POLICY_NESTED(nl80211_mbssid_config_policy),
798 	[NL80211_ATTR_MBSSID_ELEMS] = { .type = NLA_NESTED },
799 	[NL80211_ATTR_RADAR_BACKGROUND] = { .type = NLA_FLAG },
800 	[NL80211_ATTR_AP_SETTINGS_FLAGS] = { .type = NLA_U32 },
801 	[NL80211_ATTR_EHT_CAPABILITY] =
802 		NLA_POLICY_RANGE(NLA_BINARY,
803 				 NL80211_EHT_MIN_CAPABILITY_LEN,
804 				 NL80211_EHT_MAX_CAPABILITY_LEN),
805 	[NL80211_ATTR_DISABLE_EHT] = { .type = NLA_FLAG },
806 	[NL80211_ATTR_MLO_LINKS] =
807 		NLA_POLICY_NESTED_ARRAY(nl80211_policy),
808 	[NL80211_ATTR_MLO_LINK_ID] =
809 		NLA_POLICY_RANGE(NLA_U8, 0, IEEE80211_MLD_MAX_NUM_LINKS),
810 	[NL80211_ATTR_MLD_ADDR] = NLA_POLICY_EXACT_LEN(ETH_ALEN),
811 	[NL80211_ATTR_MLO_SUPPORT] = { .type = NLA_FLAG },
812 	[NL80211_ATTR_MAX_NUM_AKM_SUITES] = { .type = NLA_REJECT },
813 	[NL80211_ATTR_PUNCT_BITMAP] =
814 		NLA_POLICY_FULL_RANGE(NLA_U32, &nl80211_punct_bitmap_range),
815 
816 	[NL80211_ATTR_MAX_HW_TIMESTAMP_PEERS] = { .type = NLA_U16 },
817 	[NL80211_ATTR_HW_TIMESTAMP_ENABLED] = { .type = NLA_FLAG },
818 	[NL80211_ATTR_EMA_RNR_ELEMS] = { .type = NLA_NESTED },
819 	[NL80211_ATTR_MLO_LINK_DISABLED] = { .type = NLA_FLAG },
820 };
821 
822 /* policy for the key attributes */
823 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
824 	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
825 	[NL80211_KEY_IDX] = { .type = NLA_U8 },
826 	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
827 	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
828 	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
829 	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
830 	[NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
831 	[NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
832 	[NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
833 };
834 
835 /* policy for the key default flags */
836 static const struct nla_policy
837 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
838 	[NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
839 	[NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
840 };
841 
842 #ifdef CONFIG_PM
843 /* policy for WoWLAN attributes */
844 static const struct nla_policy
845 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
846 	[NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
847 	[NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
848 	[NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
849 	[NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
850 	[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
851 	[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
852 	[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
853 	[NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
854 	[NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
855 	[NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
856 };
857 
858 static const struct nla_policy
859 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
860 	[NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
861 	[NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
862 	[NL80211_WOWLAN_TCP_DST_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
863 	[NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
864 	[NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
865 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
866 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
867 		.len = sizeof(struct nl80211_wowlan_tcp_data_seq)
868 	},
869 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
870 		.len = sizeof(struct nl80211_wowlan_tcp_data_token)
871 	},
872 	[NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
873 	[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
874 	[NL80211_WOWLAN_TCP_WAKE_MASK] = NLA_POLICY_MIN_LEN(1),
875 };
876 #endif /* CONFIG_PM */
877 
878 /* policy for coalesce rule attributes */
879 static const struct nla_policy
880 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
881 	[NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
882 	[NL80211_ATTR_COALESCE_RULE_CONDITION] =
883 		NLA_POLICY_RANGE(NLA_U32,
884 				 NL80211_COALESCE_CONDITION_MATCH,
885 				 NL80211_COALESCE_CONDITION_NO_MATCH),
886 	[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
887 };
888 
889 /* policy for GTK rekey offload attributes */
890 static const struct nla_policy
891 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
892 	[NL80211_REKEY_DATA_KEK] = {
893 		.type = NLA_BINARY,
894 		.len = NL80211_KEK_EXT_LEN
895 	},
896 	[NL80211_REKEY_DATA_KCK] = {
897 		.type = NLA_BINARY,
898 		.len = NL80211_KCK_EXT_LEN_32
899 	},
900 	[NL80211_REKEY_DATA_REPLAY_CTR] = NLA_POLICY_EXACT_LEN(NL80211_REPLAY_CTR_LEN),
901 	[NL80211_REKEY_DATA_AKM] = { .type = NLA_U32 },
902 };
903 
904 static const struct nla_policy
905 nl80211_match_band_rssi_policy[NUM_NL80211_BANDS] = {
906 	[NL80211_BAND_2GHZ] = { .type = NLA_S32 },
907 	[NL80211_BAND_5GHZ] = { .type = NLA_S32 },
908 	[NL80211_BAND_6GHZ] = { .type = NLA_S32 },
909 	[NL80211_BAND_60GHZ] = { .type = NLA_S32 },
910 	[NL80211_BAND_LC]    = { .type = NLA_S32 },
911 };
912 
913 static const struct nla_policy
914 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
915 	[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
916 						 .len = IEEE80211_MAX_SSID_LEN },
917 	[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
918 	[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
919 	[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI] =
920 		NLA_POLICY_NESTED(nl80211_match_band_rssi_policy),
921 };
922 
923 static const struct nla_policy
924 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
925 	[NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
926 	[NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
927 };
928 
929 static const struct nla_policy
930 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
931 	[NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
932 	[NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
933 	[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
934 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
935 	},
936 };
937 
938 /* policy for NAN function attributes */
939 static const struct nla_policy
940 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
941 	[NL80211_NAN_FUNC_TYPE] =
942 		NLA_POLICY_MAX(NLA_U8, NL80211_NAN_FUNC_MAX_TYPE),
943 	[NL80211_NAN_FUNC_SERVICE_ID] = {
944 				    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
945 	[NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
946 	[NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
947 	[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
948 	[NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
949 	[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
950 	[NL80211_NAN_FUNC_FOLLOW_UP_DEST] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
951 	[NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
952 	[NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
953 	[NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
954 			.len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
955 	[NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
956 	[NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
957 	[NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
958 	[NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
959 	[NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
960 };
961 
962 /* policy for Service Response Filter attributes */
963 static const struct nla_policy
964 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
965 	[NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
966 	[NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
967 				 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
968 	[NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
969 	[NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
970 };
971 
972 /* policy for packet pattern attributes */
973 static const struct nla_policy
974 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
975 	[NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
976 	[NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
977 	[NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
978 };
979 
980 static int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
981 				     struct cfg80211_registered_device **rdev,
982 				     struct wireless_dev **wdev,
983 				     struct nlattr **attrbuf)
984 {
985 	int err;
986 
987 	if (!cb->args[0]) {
988 		struct nlattr **attrbuf_free = NULL;
989 
990 		if (!attrbuf) {
991 			attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
992 					  GFP_KERNEL);
993 			if (!attrbuf)
994 				return -ENOMEM;
995 			attrbuf_free = attrbuf;
996 		}
997 
998 		err = nlmsg_parse_deprecated(cb->nlh,
999 					     GENL_HDRLEN + nl80211_fam.hdrsize,
1000 					     attrbuf, nl80211_fam.maxattr,
1001 					     nl80211_policy, NULL);
1002 		if (err) {
1003 			kfree(attrbuf_free);
1004 			return err;
1005 		}
1006 
1007 		rtnl_lock();
1008 		*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(cb->skb->sk),
1009 						   attrbuf);
1010 		kfree(attrbuf_free);
1011 		if (IS_ERR(*wdev)) {
1012 			rtnl_unlock();
1013 			return PTR_ERR(*wdev);
1014 		}
1015 		*rdev = wiphy_to_rdev((*wdev)->wiphy);
1016 		mutex_lock(&(*rdev)->wiphy.mtx);
1017 		rtnl_unlock();
1018 		/* 0 is the first index - add 1 to parse only once */
1019 		cb->args[0] = (*rdev)->wiphy_idx + 1;
1020 		cb->args[1] = (*wdev)->identifier;
1021 	} else {
1022 		/* subtract the 1 again here */
1023 		struct wiphy *wiphy;
1024 		struct wireless_dev *tmp;
1025 
1026 		rtnl_lock();
1027 		wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
1028 		if (!wiphy) {
1029 			rtnl_unlock();
1030 			return -ENODEV;
1031 		}
1032 		*rdev = wiphy_to_rdev(wiphy);
1033 		*wdev = NULL;
1034 
1035 		list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
1036 			if (tmp->identifier == cb->args[1]) {
1037 				*wdev = tmp;
1038 				break;
1039 			}
1040 		}
1041 
1042 		if (!*wdev) {
1043 			rtnl_unlock();
1044 			return -ENODEV;
1045 		}
1046 		mutex_lock(&(*rdev)->wiphy.mtx);
1047 		rtnl_unlock();
1048 	}
1049 
1050 	return 0;
1051 }
1052 
1053 /* message building helper */
1054 void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1055 		     int flags, u8 cmd)
1056 {
1057 	/* since there is no private header just add the generic one */
1058 	return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
1059 }
1060 
1061 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
1062 				     const struct ieee80211_reg_rule *rule)
1063 {
1064 	int j;
1065 	struct nlattr *nl_wmm_rules =
1066 		nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
1067 
1068 	if (!nl_wmm_rules)
1069 		goto nla_put_failure;
1070 
1071 	for (j = 0; j < IEEE80211_NUM_ACS; j++) {
1072 		struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
1073 
1074 		if (!nl_wmm_rule)
1075 			goto nla_put_failure;
1076 
1077 		if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
1078 				rule->wmm_rule.client[j].cw_min) ||
1079 		    nla_put_u16(msg, NL80211_WMMR_CW_MAX,
1080 				rule->wmm_rule.client[j].cw_max) ||
1081 		    nla_put_u8(msg, NL80211_WMMR_AIFSN,
1082 			       rule->wmm_rule.client[j].aifsn) ||
1083 		    nla_put_u16(msg, NL80211_WMMR_TXOP,
1084 			        rule->wmm_rule.client[j].cot))
1085 			goto nla_put_failure;
1086 
1087 		nla_nest_end(msg, nl_wmm_rule);
1088 	}
1089 	nla_nest_end(msg, nl_wmm_rules);
1090 
1091 	return 0;
1092 
1093 nla_put_failure:
1094 	return -ENOBUFS;
1095 }
1096 
1097 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
1098 				   struct ieee80211_channel *chan,
1099 				   bool large)
1100 {
1101 	/* Some channels must be completely excluded from the
1102 	 * list to protect old user-space tools from breaking
1103 	 */
1104 	if (!large && chan->flags &
1105 	    (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
1106 		return 0;
1107 	if (!large && chan->freq_offset)
1108 		return 0;
1109 
1110 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
1111 			chan->center_freq))
1112 		goto nla_put_failure;
1113 
1114 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_OFFSET, chan->freq_offset))
1115 		goto nla_put_failure;
1116 
1117 	if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
1118 	    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
1119 		goto nla_put_failure;
1120 	if (chan->flags & IEEE80211_CHAN_NO_IR) {
1121 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
1122 			goto nla_put_failure;
1123 		if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
1124 			goto nla_put_failure;
1125 	}
1126 	if (chan->flags & IEEE80211_CHAN_RADAR) {
1127 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
1128 			goto nla_put_failure;
1129 		if (large) {
1130 			u32 time;
1131 
1132 			time = elapsed_jiffies_msecs(chan->dfs_state_entered);
1133 
1134 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
1135 					chan->dfs_state))
1136 				goto nla_put_failure;
1137 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
1138 					time))
1139 				goto nla_put_failure;
1140 			if (nla_put_u32(msg,
1141 					NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
1142 					chan->dfs_cac_ms))
1143 				goto nla_put_failure;
1144 		}
1145 	}
1146 
1147 	if (large) {
1148 		if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
1149 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
1150 			goto nla_put_failure;
1151 		if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
1152 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
1153 			goto nla_put_failure;
1154 		if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
1155 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
1156 			goto nla_put_failure;
1157 		if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
1158 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
1159 			goto nla_put_failure;
1160 		if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
1161 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
1162 			goto nla_put_failure;
1163 		if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
1164 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
1165 			goto nla_put_failure;
1166 		if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
1167 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
1168 			goto nla_put_failure;
1169 		if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
1170 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
1171 			goto nla_put_failure;
1172 		if ((chan->flags & IEEE80211_CHAN_NO_HE) &&
1173 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HE))
1174 			goto nla_put_failure;
1175 		if ((chan->flags & IEEE80211_CHAN_1MHZ) &&
1176 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_1MHZ))
1177 			goto nla_put_failure;
1178 		if ((chan->flags & IEEE80211_CHAN_2MHZ) &&
1179 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_2MHZ))
1180 			goto nla_put_failure;
1181 		if ((chan->flags & IEEE80211_CHAN_4MHZ) &&
1182 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_4MHZ))
1183 			goto nla_put_failure;
1184 		if ((chan->flags & IEEE80211_CHAN_8MHZ) &&
1185 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_8MHZ))
1186 			goto nla_put_failure;
1187 		if ((chan->flags & IEEE80211_CHAN_16MHZ) &&
1188 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_16MHZ))
1189 			goto nla_put_failure;
1190 		if ((chan->flags & IEEE80211_CHAN_NO_320MHZ) &&
1191 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_320MHZ))
1192 			goto nla_put_failure;
1193 		if ((chan->flags & IEEE80211_CHAN_NO_EHT) &&
1194 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_EHT))
1195 			goto nla_put_failure;
1196 	}
1197 
1198 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
1199 			DBM_TO_MBM(chan->max_power)))
1200 		goto nla_put_failure;
1201 
1202 	if (large) {
1203 		const struct ieee80211_reg_rule *rule =
1204 			freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1205 
1206 		if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
1207 			if (nl80211_msg_put_wmm_rules(msg, rule))
1208 				goto nla_put_failure;
1209 		}
1210 	}
1211 
1212 	return 0;
1213 
1214  nla_put_failure:
1215 	return -ENOBUFS;
1216 }
1217 
1218 static bool nl80211_put_txq_stats(struct sk_buff *msg,
1219 				  struct cfg80211_txq_stats *txqstats,
1220 				  int attrtype)
1221 {
1222 	struct nlattr *txqattr;
1223 
1224 #define PUT_TXQVAL_U32(attr, memb) do {					  \
1225 	if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&	  \
1226 	    nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
1227 		return false;						  \
1228 	} while (0)
1229 
1230 	txqattr = nla_nest_start_noflag(msg, attrtype);
1231 	if (!txqattr)
1232 		return false;
1233 
1234 	PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
1235 	PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
1236 	PUT_TXQVAL_U32(FLOWS, flows);
1237 	PUT_TXQVAL_U32(DROPS, drops);
1238 	PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
1239 	PUT_TXQVAL_U32(OVERLIMIT, overlimit);
1240 	PUT_TXQVAL_U32(OVERMEMORY, overmemory);
1241 	PUT_TXQVAL_U32(COLLISIONS, collisions);
1242 	PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
1243 	PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
1244 	PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
1245 	nla_nest_end(msg, txqattr);
1246 
1247 #undef PUT_TXQVAL_U32
1248 	return true;
1249 }
1250 
1251 /* netlink command implementations */
1252 
1253 /**
1254  * nl80211_link_id - return link ID
1255  * @attrs: attributes to look at
1256  *
1257  * Returns: the link ID or 0 if not given
1258  *
1259  * Note this function doesn't do any validation of the link
1260  * ID validity wrt. links that were actually added, so it must
1261  * be called only from ops with %NL80211_FLAG_MLO_VALID_LINK_ID
1262  * or if additional validation is done.
1263  */
1264 static unsigned int nl80211_link_id(struct nlattr **attrs)
1265 {
1266 	struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1267 
1268 	if (!linkid)
1269 		return 0;
1270 
1271 	return nla_get_u8(linkid);
1272 }
1273 
1274 static int nl80211_link_id_or_invalid(struct nlattr **attrs)
1275 {
1276 	struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1277 
1278 	if (!linkid)
1279 		return -1;
1280 
1281 	return nla_get_u8(linkid);
1282 }
1283 
1284 struct key_parse {
1285 	struct key_params p;
1286 	int idx;
1287 	int type;
1288 	bool def, defmgmt, defbeacon;
1289 	bool def_uni, def_multi;
1290 };
1291 
1292 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
1293 				 struct key_parse *k)
1294 {
1295 	struct nlattr *tb[NL80211_KEY_MAX + 1];
1296 	int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
1297 					      nl80211_key_policy,
1298 					      info->extack);
1299 	if (err)
1300 		return err;
1301 
1302 	k->def = !!tb[NL80211_KEY_DEFAULT];
1303 	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1304 	k->defbeacon = !!tb[NL80211_KEY_DEFAULT_BEACON];
1305 
1306 	if (k->def) {
1307 		k->def_uni = true;
1308 		k->def_multi = true;
1309 	}
1310 	if (k->defmgmt || k->defbeacon)
1311 		k->def_multi = true;
1312 
1313 	if (tb[NL80211_KEY_IDX])
1314 		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1315 
1316 	if (tb[NL80211_KEY_DATA]) {
1317 		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1318 		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1319 	}
1320 
1321 	if (tb[NL80211_KEY_SEQ]) {
1322 		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1323 		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1324 	}
1325 
1326 	if (tb[NL80211_KEY_CIPHER])
1327 		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1328 
1329 	if (tb[NL80211_KEY_TYPE])
1330 		k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1331 
1332 	if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1333 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1334 
1335 		err = nla_parse_nested_deprecated(kdt,
1336 						  NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1337 						  tb[NL80211_KEY_DEFAULT_TYPES],
1338 						  nl80211_key_default_policy,
1339 						  info->extack);
1340 		if (err)
1341 			return err;
1342 
1343 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1344 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1345 	}
1346 
1347 	if (tb[NL80211_KEY_MODE])
1348 		k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1349 
1350 	return 0;
1351 }
1352 
1353 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1354 {
1355 	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1356 		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1357 		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1358 	}
1359 
1360 	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1361 		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1362 		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1363 	}
1364 
1365 	if (info->attrs[NL80211_ATTR_KEY_IDX])
1366 		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1367 
1368 	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1369 		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1370 
1371 	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1372 	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1373 
1374 	if (k->def) {
1375 		k->def_uni = true;
1376 		k->def_multi = true;
1377 	}
1378 	if (k->defmgmt)
1379 		k->def_multi = true;
1380 
1381 	if (info->attrs[NL80211_ATTR_KEY_TYPE])
1382 		k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1383 
1384 	if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1385 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1386 		int err = nla_parse_nested_deprecated(kdt,
1387 						      NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1388 						      info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1389 						      nl80211_key_default_policy,
1390 						      info->extack);
1391 		if (err)
1392 			return err;
1393 
1394 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1395 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1396 	}
1397 
1398 	return 0;
1399 }
1400 
1401 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1402 {
1403 	int err;
1404 
1405 	memset(k, 0, sizeof(*k));
1406 	k->idx = -1;
1407 	k->type = -1;
1408 
1409 	if (info->attrs[NL80211_ATTR_KEY])
1410 		err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1411 	else
1412 		err = nl80211_parse_key_old(info, k);
1413 
1414 	if (err)
1415 		return err;
1416 
1417 	if ((k->def ? 1 : 0) + (k->defmgmt ? 1 : 0) +
1418 	    (k->defbeacon ? 1 : 0) > 1) {
1419 		GENL_SET_ERR_MSG(info,
1420 				 "key with multiple default flags is invalid");
1421 		return -EINVAL;
1422 	}
1423 
1424 	if (k->defmgmt || k->defbeacon) {
1425 		if (k->def_uni || !k->def_multi) {
1426 			GENL_SET_ERR_MSG(info,
1427 					 "defmgmt/defbeacon key must be mcast");
1428 			return -EINVAL;
1429 		}
1430 	}
1431 
1432 	if (k->idx != -1) {
1433 		if (k->defmgmt) {
1434 			if (k->idx < 4 || k->idx > 5) {
1435 				GENL_SET_ERR_MSG(info,
1436 						 "defmgmt key idx not 4 or 5");
1437 				return -EINVAL;
1438 			}
1439 		} else if (k->defbeacon) {
1440 			if (k->idx < 6 || k->idx > 7) {
1441 				GENL_SET_ERR_MSG(info,
1442 						 "defbeacon key idx not 6 or 7");
1443 				return -EINVAL;
1444 			}
1445 		} else if (k->def) {
1446 			if (k->idx < 0 || k->idx > 3) {
1447 				GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1448 				return -EINVAL;
1449 			}
1450 		} else {
1451 			if (k->idx < 0 || k->idx > 7) {
1452 				GENL_SET_ERR_MSG(info, "key idx not 0-7");
1453 				return -EINVAL;
1454 			}
1455 		}
1456 	}
1457 
1458 	return 0;
1459 }
1460 
1461 static struct cfg80211_cached_keys *
1462 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1463 		       struct genl_info *info, bool *no_ht)
1464 {
1465 	struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1466 	struct key_parse parse;
1467 	struct nlattr *key;
1468 	struct cfg80211_cached_keys *result;
1469 	int rem, err, def = 0;
1470 	bool have_key = false;
1471 
1472 	nla_for_each_nested(key, keys, rem) {
1473 		have_key = true;
1474 		break;
1475 	}
1476 
1477 	if (!have_key)
1478 		return NULL;
1479 
1480 	result = kzalloc(sizeof(*result), GFP_KERNEL);
1481 	if (!result)
1482 		return ERR_PTR(-ENOMEM);
1483 
1484 	result->def = -1;
1485 
1486 	nla_for_each_nested(key, keys, rem) {
1487 		memset(&parse, 0, sizeof(parse));
1488 		parse.idx = -1;
1489 
1490 		err = nl80211_parse_key_new(info, key, &parse);
1491 		if (err)
1492 			goto error;
1493 		err = -EINVAL;
1494 		if (!parse.p.key)
1495 			goto error;
1496 		if (parse.idx < 0 || parse.idx > 3) {
1497 			GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1498 			goto error;
1499 		}
1500 		if (parse.def) {
1501 			if (def) {
1502 				GENL_SET_ERR_MSG(info,
1503 						 "only one key can be default");
1504 				goto error;
1505 			}
1506 			def = 1;
1507 			result->def = parse.idx;
1508 			if (!parse.def_uni || !parse.def_multi)
1509 				goto error;
1510 		} else if (parse.defmgmt)
1511 			goto error;
1512 		err = cfg80211_validate_key_settings(rdev, &parse.p,
1513 						     parse.idx, false, NULL);
1514 		if (err)
1515 			goto error;
1516 		if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1517 		    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1518 			GENL_SET_ERR_MSG(info, "connect key must be WEP");
1519 			err = -EINVAL;
1520 			goto error;
1521 		}
1522 		result->params[parse.idx].cipher = parse.p.cipher;
1523 		result->params[parse.idx].key_len = parse.p.key_len;
1524 		result->params[parse.idx].key = result->data[parse.idx];
1525 		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1526 
1527 		/* must be WEP key if we got here */
1528 		if (no_ht)
1529 			*no_ht = true;
1530 	}
1531 
1532 	if (result->def < 0) {
1533 		err = -EINVAL;
1534 		GENL_SET_ERR_MSG(info, "need a default/TX key");
1535 		goto error;
1536 	}
1537 
1538 	return result;
1539  error:
1540 	kfree(result);
1541 	return ERR_PTR(err);
1542 }
1543 
1544 static int nl80211_key_allowed(struct wireless_dev *wdev)
1545 {
1546 	ASSERT_WDEV_LOCK(wdev);
1547 
1548 	switch (wdev->iftype) {
1549 	case NL80211_IFTYPE_AP:
1550 	case NL80211_IFTYPE_AP_VLAN:
1551 	case NL80211_IFTYPE_P2P_GO:
1552 	case NL80211_IFTYPE_MESH_POINT:
1553 		break;
1554 	case NL80211_IFTYPE_ADHOC:
1555 		if (wdev->u.ibss.current_bss)
1556 			return 0;
1557 		return -ENOLINK;
1558 	case NL80211_IFTYPE_STATION:
1559 	case NL80211_IFTYPE_P2P_CLIENT:
1560 		if (wdev->connected)
1561 			return 0;
1562 		return -ENOLINK;
1563 	case NL80211_IFTYPE_NAN:
1564 		if (wiphy_ext_feature_isset(wdev->wiphy,
1565 					    NL80211_EXT_FEATURE_SECURE_NAN))
1566 			return 0;
1567 		return -EINVAL;
1568 	case NL80211_IFTYPE_UNSPECIFIED:
1569 	case NL80211_IFTYPE_OCB:
1570 	case NL80211_IFTYPE_MONITOR:
1571 	case NL80211_IFTYPE_P2P_DEVICE:
1572 	case NL80211_IFTYPE_WDS:
1573 	case NUM_NL80211_IFTYPES:
1574 		return -EINVAL;
1575 	}
1576 
1577 	return 0;
1578 }
1579 
1580 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1581 							u32 freq)
1582 {
1583 	struct ieee80211_channel *chan;
1584 
1585 	chan = ieee80211_get_channel_khz(wiphy, freq);
1586 	if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1587 		return NULL;
1588 	return chan;
1589 }
1590 
1591 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1592 {
1593 	struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1594 	int i;
1595 
1596 	if (!nl_modes)
1597 		goto nla_put_failure;
1598 
1599 	i = 0;
1600 	while (ifmodes) {
1601 		if ((ifmodes & 1) && nla_put_flag(msg, i))
1602 			goto nla_put_failure;
1603 		ifmodes >>= 1;
1604 		i++;
1605 	}
1606 
1607 	nla_nest_end(msg, nl_modes);
1608 	return 0;
1609 
1610 nla_put_failure:
1611 	return -ENOBUFS;
1612 }
1613 
1614 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1615 					  struct sk_buff *msg,
1616 					  bool large)
1617 {
1618 	struct nlattr *nl_combis;
1619 	int i, j;
1620 
1621 	nl_combis = nla_nest_start_noflag(msg,
1622 					  NL80211_ATTR_INTERFACE_COMBINATIONS);
1623 	if (!nl_combis)
1624 		goto nla_put_failure;
1625 
1626 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1627 		const struct ieee80211_iface_combination *c;
1628 		struct nlattr *nl_combi, *nl_limits;
1629 
1630 		c = &wiphy->iface_combinations[i];
1631 
1632 		nl_combi = nla_nest_start_noflag(msg, i + 1);
1633 		if (!nl_combi)
1634 			goto nla_put_failure;
1635 
1636 		nl_limits = nla_nest_start_noflag(msg,
1637 						  NL80211_IFACE_COMB_LIMITS);
1638 		if (!nl_limits)
1639 			goto nla_put_failure;
1640 
1641 		for (j = 0; j < c->n_limits; j++) {
1642 			struct nlattr *nl_limit;
1643 
1644 			nl_limit = nla_nest_start_noflag(msg, j + 1);
1645 			if (!nl_limit)
1646 				goto nla_put_failure;
1647 			if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1648 					c->limits[j].max))
1649 				goto nla_put_failure;
1650 			if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1651 						c->limits[j].types))
1652 				goto nla_put_failure;
1653 			nla_nest_end(msg, nl_limit);
1654 		}
1655 
1656 		nla_nest_end(msg, nl_limits);
1657 
1658 		if (c->beacon_int_infra_match &&
1659 		    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1660 			goto nla_put_failure;
1661 		if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1662 				c->num_different_channels) ||
1663 		    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1664 				c->max_interfaces))
1665 			goto nla_put_failure;
1666 		if (large &&
1667 		    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1668 				c->radar_detect_widths) ||
1669 		     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1670 				c->radar_detect_regions)))
1671 			goto nla_put_failure;
1672 		if (c->beacon_int_min_gcd &&
1673 		    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1674 				c->beacon_int_min_gcd))
1675 			goto nla_put_failure;
1676 
1677 		nla_nest_end(msg, nl_combi);
1678 	}
1679 
1680 	nla_nest_end(msg, nl_combis);
1681 
1682 	return 0;
1683 nla_put_failure:
1684 	return -ENOBUFS;
1685 }
1686 
1687 #ifdef CONFIG_PM
1688 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1689 					struct sk_buff *msg)
1690 {
1691 	const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1692 	struct nlattr *nl_tcp;
1693 
1694 	if (!tcp)
1695 		return 0;
1696 
1697 	nl_tcp = nla_nest_start_noflag(msg,
1698 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1699 	if (!nl_tcp)
1700 		return -ENOBUFS;
1701 
1702 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1703 			tcp->data_payload_max))
1704 		return -ENOBUFS;
1705 
1706 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1707 			tcp->data_payload_max))
1708 		return -ENOBUFS;
1709 
1710 	if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1711 		return -ENOBUFS;
1712 
1713 	if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1714 				sizeof(*tcp->tok), tcp->tok))
1715 		return -ENOBUFS;
1716 
1717 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1718 			tcp->data_interval_max))
1719 		return -ENOBUFS;
1720 
1721 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1722 			tcp->wake_payload_max))
1723 		return -ENOBUFS;
1724 
1725 	nla_nest_end(msg, nl_tcp);
1726 	return 0;
1727 }
1728 
1729 static int nl80211_send_wowlan(struct sk_buff *msg,
1730 			       struct cfg80211_registered_device *rdev,
1731 			       bool large)
1732 {
1733 	struct nlattr *nl_wowlan;
1734 
1735 	if (!rdev->wiphy.wowlan)
1736 		return 0;
1737 
1738 	nl_wowlan = nla_nest_start_noflag(msg,
1739 					  NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1740 	if (!nl_wowlan)
1741 		return -ENOBUFS;
1742 
1743 	if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1744 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1745 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1746 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1747 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1748 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1749 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1750 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1751 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1752 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1753 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1754 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1755 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1756 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1757 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1758 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1759 		return -ENOBUFS;
1760 
1761 	if (rdev->wiphy.wowlan->n_patterns) {
1762 		struct nl80211_pattern_support pat = {
1763 			.max_patterns = rdev->wiphy.wowlan->n_patterns,
1764 			.min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1765 			.max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1766 			.max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1767 		};
1768 
1769 		if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1770 			    sizeof(pat), &pat))
1771 			return -ENOBUFS;
1772 	}
1773 
1774 	if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1775 	    nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1776 			rdev->wiphy.wowlan->max_nd_match_sets))
1777 		return -ENOBUFS;
1778 
1779 	if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1780 		return -ENOBUFS;
1781 
1782 	nla_nest_end(msg, nl_wowlan);
1783 
1784 	return 0;
1785 }
1786 #endif
1787 
1788 static int nl80211_send_coalesce(struct sk_buff *msg,
1789 				 struct cfg80211_registered_device *rdev)
1790 {
1791 	struct nl80211_coalesce_rule_support rule;
1792 
1793 	if (!rdev->wiphy.coalesce)
1794 		return 0;
1795 
1796 	rule.max_rules = rdev->wiphy.coalesce->n_rules;
1797 	rule.max_delay = rdev->wiphy.coalesce->max_delay;
1798 	rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1799 	rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1800 	rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1801 	rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1802 
1803 	if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1804 		return -ENOBUFS;
1805 
1806 	return 0;
1807 }
1808 
1809 static int
1810 nl80211_send_iftype_data(struct sk_buff *msg,
1811 			 const struct ieee80211_supported_band *sband,
1812 			 const struct ieee80211_sband_iftype_data *iftdata)
1813 {
1814 	const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1815 	const struct ieee80211_sta_eht_cap *eht_cap = &iftdata->eht_cap;
1816 
1817 	if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1818 				iftdata->types_mask))
1819 		return -ENOBUFS;
1820 
1821 	if (he_cap->has_he) {
1822 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1823 			    sizeof(he_cap->he_cap_elem.mac_cap_info),
1824 			    he_cap->he_cap_elem.mac_cap_info) ||
1825 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1826 			    sizeof(he_cap->he_cap_elem.phy_cap_info),
1827 			    he_cap->he_cap_elem.phy_cap_info) ||
1828 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1829 			    sizeof(he_cap->he_mcs_nss_supp),
1830 			    &he_cap->he_mcs_nss_supp) ||
1831 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1832 			    sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1833 			return -ENOBUFS;
1834 	}
1835 
1836 	if (eht_cap->has_eht && he_cap->has_he) {
1837 		u8 mcs_nss_size, ppe_thresh_size;
1838 		u16 ppe_thres_hdr;
1839 		bool is_ap;
1840 
1841 		is_ap = iftdata->types_mask & BIT(NL80211_IFTYPE_AP) ||
1842 			iftdata->types_mask & BIT(NL80211_IFTYPE_P2P_GO);
1843 
1844 		mcs_nss_size =
1845 			ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
1846 						   &eht_cap->eht_cap_elem,
1847 						   is_ap);
1848 
1849 		ppe_thres_hdr = get_unaligned_le16(&eht_cap->eht_ppe_thres[0]);
1850 		ppe_thresh_size =
1851 			ieee80211_eht_ppe_size(ppe_thres_hdr,
1852 					       eht_cap->eht_cap_elem.phy_cap_info);
1853 
1854 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MAC,
1855 			    sizeof(eht_cap->eht_cap_elem.mac_cap_info),
1856 			    eht_cap->eht_cap_elem.mac_cap_info) ||
1857 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY,
1858 			    sizeof(eht_cap->eht_cap_elem.phy_cap_info),
1859 			    eht_cap->eht_cap_elem.phy_cap_info) ||
1860 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MCS_SET,
1861 			    mcs_nss_size, &eht_cap->eht_mcs_nss_supp) ||
1862 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PPE,
1863 			    ppe_thresh_size, eht_cap->eht_ppe_thres))
1864 			return -ENOBUFS;
1865 	}
1866 
1867 	if (sband->band == NL80211_BAND_6GHZ &&
1868 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_6GHZ_CAPA,
1869 		    sizeof(iftdata->he_6ghz_capa),
1870 		    &iftdata->he_6ghz_capa))
1871 		return -ENOBUFS;
1872 
1873 	if (iftdata->vendor_elems.data && iftdata->vendor_elems.len &&
1874 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_VENDOR_ELEMS,
1875 		    iftdata->vendor_elems.len, iftdata->vendor_elems.data))
1876 		return -ENOBUFS;
1877 
1878 	return 0;
1879 }
1880 
1881 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1882 				      struct ieee80211_supported_band *sband,
1883 				      bool large)
1884 {
1885 	struct nlattr *nl_rates, *nl_rate;
1886 	struct ieee80211_rate *rate;
1887 	int i;
1888 
1889 	/* add HT info */
1890 	if (sband->ht_cap.ht_supported &&
1891 	    (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1892 		     sizeof(sband->ht_cap.mcs),
1893 		     &sband->ht_cap.mcs) ||
1894 	     nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1895 			 sband->ht_cap.cap) ||
1896 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1897 			sband->ht_cap.ampdu_factor) ||
1898 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1899 			sband->ht_cap.ampdu_density)))
1900 		return -ENOBUFS;
1901 
1902 	/* add VHT info */
1903 	if (sband->vht_cap.vht_supported &&
1904 	    (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1905 		     sizeof(sband->vht_cap.vht_mcs),
1906 		     &sband->vht_cap.vht_mcs) ||
1907 	     nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1908 			 sband->vht_cap.cap)))
1909 		return -ENOBUFS;
1910 
1911 	if (large && sband->n_iftype_data) {
1912 		struct nlattr *nl_iftype_data =
1913 			nla_nest_start_noflag(msg,
1914 					      NL80211_BAND_ATTR_IFTYPE_DATA);
1915 		int err;
1916 
1917 		if (!nl_iftype_data)
1918 			return -ENOBUFS;
1919 
1920 		for (i = 0; i < sband->n_iftype_data; i++) {
1921 			struct nlattr *iftdata;
1922 
1923 			iftdata = nla_nest_start_noflag(msg, i + 1);
1924 			if (!iftdata)
1925 				return -ENOBUFS;
1926 
1927 			err = nl80211_send_iftype_data(msg, sband,
1928 						       &sband->iftype_data[i]);
1929 			if (err)
1930 				return err;
1931 
1932 			nla_nest_end(msg, iftdata);
1933 		}
1934 
1935 		nla_nest_end(msg, nl_iftype_data);
1936 	}
1937 
1938 	/* add EDMG info */
1939 	if (large && sband->edmg_cap.channels &&
1940 	    (nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_CHANNELS,
1941 		       sband->edmg_cap.channels) ||
1942 	    nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_BW_CONFIG,
1943 		       sband->edmg_cap.bw_config)))
1944 
1945 		return -ENOBUFS;
1946 
1947 	/* add bitrates */
1948 	nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1949 	if (!nl_rates)
1950 		return -ENOBUFS;
1951 
1952 	for (i = 0; i < sband->n_bitrates; i++) {
1953 		nl_rate = nla_nest_start_noflag(msg, i);
1954 		if (!nl_rate)
1955 			return -ENOBUFS;
1956 
1957 		rate = &sband->bitrates[i];
1958 		if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1959 				rate->bitrate))
1960 			return -ENOBUFS;
1961 		if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1962 		    nla_put_flag(msg,
1963 				 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1964 			return -ENOBUFS;
1965 
1966 		nla_nest_end(msg, nl_rate);
1967 	}
1968 
1969 	nla_nest_end(msg, nl_rates);
1970 
1971 	/* S1G capabilities */
1972 	if (sband->band == NL80211_BAND_S1GHZ && sband->s1g_cap.s1g &&
1973 	    (nla_put(msg, NL80211_BAND_ATTR_S1G_CAPA,
1974 		     sizeof(sband->s1g_cap.cap),
1975 		     sband->s1g_cap.cap) ||
1976 	     nla_put(msg, NL80211_BAND_ATTR_S1G_MCS_NSS_SET,
1977 		     sizeof(sband->s1g_cap.nss_mcs),
1978 		     sband->s1g_cap.nss_mcs)))
1979 		return -ENOBUFS;
1980 
1981 	return 0;
1982 }
1983 
1984 static int
1985 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1986 			 const struct ieee80211_txrx_stypes *mgmt_stypes)
1987 {
1988 	u16 stypes;
1989 	struct nlattr *nl_ftypes, *nl_ifs;
1990 	enum nl80211_iftype ift;
1991 	int i;
1992 
1993 	if (!mgmt_stypes)
1994 		return 0;
1995 
1996 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
1997 	if (!nl_ifs)
1998 		return -ENOBUFS;
1999 
2000 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
2001 		nl_ftypes = nla_nest_start_noflag(msg, ift);
2002 		if (!nl_ftypes)
2003 			return -ENOBUFS;
2004 		i = 0;
2005 		stypes = mgmt_stypes[ift].tx;
2006 		while (stypes) {
2007 			if ((stypes & 1) &&
2008 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
2009 					(i << 4) | IEEE80211_FTYPE_MGMT))
2010 				return -ENOBUFS;
2011 			stypes >>= 1;
2012 			i++;
2013 		}
2014 		nla_nest_end(msg, nl_ftypes);
2015 	}
2016 
2017 	nla_nest_end(msg, nl_ifs);
2018 
2019 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
2020 	if (!nl_ifs)
2021 		return -ENOBUFS;
2022 
2023 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
2024 		nl_ftypes = nla_nest_start_noflag(msg, ift);
2025 		if (!nl_ftypes)
2026 			return -ENOBUFS;
2027 		i = 0;
2028 		stypes = mgmt_stypes[ift].rx;
2029 		while (stypes) {
2030 			if ((stypes & 1) &&
2031 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
2032 					(i << 4) | IEEE80211_FTYPE_MGMT))
2033 				return -ENOBUFS;
2034 			stypes >>= 1;
2035 			i++;
2036 		}
2037 		nla_nest_end(msg, nl_ftypes);
2038 	}
2039 	nla_nest_end(msg, nl_ifs);
2040 
2041 	return 0;
2042 }
2043 
2044 #define CMD(op, n)							\
2045 	 do {								\
2046 		if (rdev->ops->op) {					\
2047 			i++;						\
2048 			if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) 	\
2049 				goto nla_put_failure;			\
2050 		}							\
2051 	} while (0)
2052 
2053 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
2054 					struct sk_buff *msg)
2055 {
2056 	int i = 0;
2057 
2058 	/*
2059 	 * do *NOT* add anything into this function, new things need to be
2060 	 * advertised only to new versions of userspace that can deal with
2061 	 * the split (and they can't possibly care about new features...
2062 	 */
2063 	CMD(add_virtual_intf, NEW_INTERFACE);
2064 	CMD(change_virtual_intf, SET_INTERFACE);
2065 	CMD(add_key, NEW_KEY);
2066 	CMD(start_ap, START_AP);
2067 	CMD(add_station, NEW_STATION);
2068 	CMD(add_mpath, NEW_MPATH);
2069 	CMD(update_mesh_config, SET_MESH_CONFIG);
2070 	CMD(change_bss, SET_BSS);
2071 	CMD(auth, AUTHENTICATE);
2072 	CMD(assoc, ASSOCIATE);
2073 	CMD(deauth, DEAUTHENTICATE);
2074 	CMD(disassoc, DISASSOCIATE);
2075 	CMD(join_ibss, JOIN_IBSS);
2076 	CMD(join_mesh, JOIN_MESH);
2077 	CMD(set_pmksa, SET_PMKSA);
2078 	CMD(del_pmksa, DEL_PMKSA);
2079 	CMD(flush_pmksa, FLUSH_PMKSA);
2080 	if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
2081 		CMD(remain_on_channel, REMAIN_ON_CHANNEL);
2082 	CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
2083 	CMD(mgmt_tx, FRAME);
2084 	CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
2085 	if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
2086 		i++;
2087 		if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
2088 			goto nla_put_failure;
2089 	}
2090 	if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
2091 	    rdev->ops->join_mesh) {
2092 		i++;
2093 		if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
2094 			goto nla_put_failure;
2095 	}
2096 	if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2097 		CMD(tdls_mgmt, TDLS_MGMT);
2098 		CMD(tdls_oper, TDLS_OPER);
2099 	}
2100 	if (rdev->wiphy.max_sched_scan_reqs)
2101 		CMD(sched_scan_start, START_SCHED_SCAN);
2102 	CMD(probe_client, PROBE_CLIENT);
2103 	CMD(set_noack_map, SET_NOACK_MAP);
2104 	if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
2105 		i++;
2106 		if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
2107 			goto nla_put_failure;
2108 	}
2109 	CMD(start_p2p_device, START_P2P_DEVICE);
2110 	CMD(set_mcast_rate, SET_MCAST_RATE);
2111 #ifdef CONFIG_NL80211_TESTMODE
2112 	CMD(testmode_cmd, TESTMODE);
2113 #endif
2114 
2115 	if (rdev->ops->connect || rdev->ops->auth) {
2116 		i++;
2117 		if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
2118 			goto nla_put_failure;
2119 	}
2120 
2121 	if (rdev->ops->disconnect || rdev->ops->deauth) {
2122 		i++;
2123 		if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
2124 			goto nla_put_failure;
2125 	}
2126 
2127 	return i;
2128  nla_put_failure:
2129 	return -ENOBUFS;
2130 }
2131 
2132 static int
2133 nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
2134 			   struct sk_buff *msg)
2135 {
2136 	struct nlattr *ftm;
2137 
2138 	if (!cap->ftm.supported)
2139 		return 0;
2140 
2141 	ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
2142 	if (!ftm)
2143 		return -ENOBUFS;
2144 
2145 	if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
2146 		return -ENOBUFS;
2147 	if (cap->ftm.non_asap &&
2148 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
2149 		return -ENOBUFS;
2150 	if (cap->ftm.request_lci &&
2151 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
2152 		return -ENOBUFS;
2153 	if (cap->ftm.request_civicloc &&
2154 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
2155 		return -ENOBUFS;
2156 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
2157 			cap->ftm.preambles))
2158 		return -ENOBUFS;
2159 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
2160 			cap->ftm.bandwidths))
2161 		return -ENOBUFS;
2162 	if (cap->ftm.max_bursts_exponent >= 0 &&
2163 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
2164 			cap->ftm.max_bursts_exponent))
2165 		return -ENOBUFS;
2166 	if (cap->ftm.max_ftms_per_burst &&
2167 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
2168 			cap->ftm.max_ftms_per_burst))
2169 		return -ENOBUFS;
2170 	if (cap->ftm.trigger_based &&
2171 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_TRIGGER_BASED))
2172 		return -ENOBUFS;
2173 	if (cap->ftm.non_trigger_based &&
2174 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_TRIGGER_BASED))
2175 		return -ENOBUFS;
2176 
2177 	nla_nest_end(msg, ftm);
2178 	return 0;
2179 }
2180 
2181 static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
2182 				  struct sk_buff *msg)
2183 {
2184 	const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
2185 	struct nlattr *pmsr, *caps;
2186 
2187 	if (!cap)
2188 		return 0;
2189 
2190 	/*
2191 	 * we don't need to clean up anything here since the caller
2192 	 * will genlmsg_cancel() if we fail
2193 	 */
2194 
2195 	pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
2196 	if (!pmsr)
2197 		return -ENOBUFS;
2198 
2199 	if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
2200 		return -ENOBUFS;
2201 
2202 	if (cap->report_ap_tsf &&
2203 	    nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
2204 		return -ENOBUFS;
2205 
2206 	if (cap->randomize_mac_addr &&
2207 	    nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
2208 		return -ENOBUFS;
2209 
2210 	caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
2211 	if (!caps)
2212 		return -ENOBUFS;
2213 
2214 	if (nl80211_send_pmsr_ftm_capa(cap, msg))
2215 		return -ENOBUFS;
2216 
2217 	nla_nest_end(msg, caps);
2218 	nla_nest_end(msg, pmsr);
2219 
2220 	return 0;
2221 }
2222 
2223 static int
2224 nl80211_put_iftype_akm_suites(struct cfg80211_registered_device *rdev,
2225 			      struct sk_buff *msg)
2226 {
2227 	int i;
2228 	struct nlattr *nested, *nested_akms;
2229 	const struct wiphy_iftype_akm_suites *iftype_akms;
2230 
2231 	if (!rdev->wiphy.num_iftype_akm_suites ||
2232 	    !rdev->wiphy.iftype_akm_suites)
2233 		return 0;
2234 
2235 	nested = nla_nest_start(msg, NL80211_ATTR_IFTYPE_AKM_SUITES);
2236 	if (!nested)
2237 		return -ENOBUFS;
2238 
2239 	for (i = 0; i < rdev->wiphy.num_iftype_akm_suites; i++) {
2240 		nested_akms = nla_nest_start(msg, i + 1);
2241 		if (!nested_akms)
2242 			return -ENOBUFS;
2243 
2244 		iftype_akms = &rdev->wiphy.iftype_akm_suites[i];
2245 
2246 		if (nl80211_put_iftypes(msg, NL80211_IFTYPE_AKM_ATTR_IFTYPES,
2247 					iftype_akms->iftypes_mask))
2248 			return -ENOBUFS;
2249 
2250 		if (nla_put(msg, NL80211_IFTYPE_AKM_ATTR_SUITES,
2251 			    sizeof(u32) * iftype_akms->n_akm_suites,
2252 			    iftype_akms->akm_suites)) {
2253 			return -ENOBUFS;
2254 		}
2255 		nla_nest_end(msg, nested_akms);
2256 	}
2257 
2258 	nla_nest_end(msg, nested);
2259 
2260 	return 0;
2261 }
2262 
2263 static int
2264 nl80211_put_tid_config_support(struct cfg80211_registered_device *rdev,
2265 			       struct sk_buff *msg)
2266 {
2267 	struct nlattr *supp;
2268 
2269 	if (!rdev->wiphy.tid_config_support.vif &&
2270 	    !rdev->wiphy.tid_config_support.peer)
2271 		return 0;
2272 
2273 	supp = nla_nest_start(msg, NL80211_ATTR_TID_CONFIG);
2274 	if (!supp)
2275 		return -ENOSPC;
2276 
2277 	if (rdev->wiphy.tid_config_support.vif &&
2278 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_VIF_SUPP,
2279 			      rdev->wiphy.tid_config_support.vif,
2280 			      NL80211_TID_CONFIG_ATTR_PAD))
2281 		goto fail;
2282 
2283 	if (rdev->wiphy.tid_config_support.peer &&
2284 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_PEER_SUPP,
2285 			      rdev->wiphy.tid_config_support.peer,
2286 			      NL80211_TID_CONFIG_ATTR_PAD))
2287 		goto fail;
2288 
2289 	/* for now we just use the same value ... makes more sense */
2290 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_SHORT,
2291 		       rdev->wiphy.tid_config_support.max_retry))
2292 		goto fail;
2293 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_LONG,
2294 		       rdev->wiphy.tid_config_support.max_retry))
2295 		goto fail;
2296 
2297 	nla_nest_end(msg, supp);
2298 
2299 	return 0;
2300 fail:
2301 	nla_nest_cancel(msg, supp);
2302 	return -ENOBUFS;
2303 }
2304 
2305 static int
2306 nl80211_put_sar_specs(struct cfg80211_registered_device *rdev,
2307 		      struct sk_buff *msg)
2308 {
2309 	struct nlattr *sar_capa, *specs, *sub_freq_range;
2310 	u8 num_freq_ranges;
2311 	int i;
2312 
2313 	if (!rdev->wiphy.sar_capa)
2314 		return 0;
2315 
2316 	num_freq_ranges = rdev->wiphy.sar_capa->num_freq_ranges;
2317 
2318 	sar_capa = nla_nest_start(msg, NL80211_ATTR_SAR_SPEC);
2319 	if (!sar_capa)
2320 		return -ENOSPC;
2321 
2322 	if (nla_put_u32(msg, NL80211_SAR_ATTR_TYPE, rdev->wiphy.sar_capa->type))
2323 		goto fail;
2324 
2325 	specs = nla_nest_start(msg, NL80211_SAR_ATTR_SPECS);
2326 	if (!specs)
2327 		goto fail;
2328 
2329 	/* report supported freq_ranges */
2330 	for (i = 0; i < num_freq_ranges; i++) {
2331 		sub_freq_range = nla_nest_start(msg, i + 1);
2332 		if (!sub_freq_range)
2333 			goto fail;
2334 
2335 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_START_FREQ,
2336 				rdev->wiphy.sar_capa->freq_ranges[i].start_freq))
2337 			goto fail;
2338 
2339 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_END_FREQ,
2340 				rdev->wiphy.sar_capa->freq_ranges[i].end_freq))
2341 			goto fail;
2342 
2343 		nla_nest_end(msg, sub_freq_range);
2344 	}
2345 
2346 	nla_nest_end(msg, specs);
2347 	nla_nest_end(msg, sar_capa);
2348 
2349 	return 0;
2350 fail:
2351 	nla_nest_cancel(msg, sar_capa);
2352 	return -ENOBUFS;
2353 }
2354 
2355 static int nl80211_put_mbssid_support(struct wiphy *wiphy, struct sk_buff *msg)
2356 {
2357 	struct nlattr *config;
2358 
2359 	if (!wiphy->mbssid_max_interfaces)
2360 		return 0;
2361 
2362 	config = nla_nest_start(msg, NL80211_ATTR_MBSSID_CONFIG);
2363 	if (!config)
2364 		return -ENOBUFS;
2365 
2366 	if (nla_put_u8(msg, NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES,
2367 		       wiphy->mbssid_max_interfaces))
2368 		goto fail;
2369 
2370 	if (wiphy->ema_max_profile_periodicity &&
2371 	    nla_put_u8(msg,
2372 		       NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY,
2373 		       wiphy->ema_max_profile_periodicity))
2374 		goto fail;
2375 
2376 	nla_nest_end(msg, config);
2377 	return 0;
2378 
2379 fail:
2380 	nla_nest_cancel(msg, config);
2381 	return -ENOBUFS;
2382 }
2383 
2384 struct nl80211_dump_wiphy_state {
2385 	s64 filter_wiphy;
2386 	long start;
2387 	long split_start, band_start, chan_start, capa_start;
2388 	bool split;
2389 };
2390 
2391 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
2392 			      enum nl80211_commands cmd,
2393 			      struct sk_buff *msg, u32 portid, u32 seq,
2394 			      int flags, struct nl80211_dump_wiphy_state *state)
2395 {
2396 	void *hdr;
2397 	struct nlattr *nl_bands, *nl_band;
2398 	struct nlattr *nl_freqs, *nl_freq;
2399 	struct nlattr *nl_cmds;
2400 	enum nl80211_band band;
2401 	struct ieee80211_channel *chan;
2402 	int i;
2403 	const struct ieee80211_txrx_stypes *mgmt_stypes =
2404 				rdev->wiphy.mgmt_stypes;
2405 	u32 features;
2406 
2407 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2408 	if (!hdr)
2409 		return -ENOBUFS;
2410 
2411 	if (WARN_ON(!state))
2412 		return -EINVAL;
2413 
2414 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2415 	    nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
2416 			   wiphy_name(&rdev->wiphy)) ||
2417 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
2418 			cfg80211_rdev_list_generation))
2419 		goto nla_put_failure;
2420 
2421 	if (cmd != NL80211_CMD_NEW_WIPHY)
2422 		goto finish;
2423 
2424 	switch (state->split_start) {
2425 	case 0:
2426 		if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
2427 			       rdev->wiphy.retry_short) ||
2428 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
2429 			       rdev->wiphy.retry_long) ||
2430 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
2431 				rdev->wiphy.frag_threshold) ||
2432 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
2433 				rdev->wiphy.rts_threshold) ||
2434 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
2435 			       rdev->wiphy.coverage_class) ||
2436 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
2437 			       rdev->wiphy.max_scan_ssids) ||
2438 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
2439 			       rdev->wiphy.max_sched_scan_ssids) ||
2440 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
2441 				rdev->wiphy.max_scan_ie_len) ||
2442 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
2443 				rdev->wiphy.max_sched_scan_ie_len) ||
2444 		    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
2445 			       rdev->wiphy.max_match_sets))
2446 			goto nla_put_failure;
2447 
2448 		if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
2449 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
2450 			goto nla_put_failure;
2451 		if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
2452 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
2453 			goto nla_put_failure;
2454 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2455 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
2456 			goto nla_put_failure;
2457 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
2458 		    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
2459 			goto nla_put_failure;
2460 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2461 		    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
2462 			goto nla_put_failure;
2463 		if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
2464 		    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
2465 			goto nla_put_failure;
2466 		state->split_start++;
2467 		if (state->split)
2468 			break;
2469 		fallthrough;
2470 	case 1:
2471 		if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
2472 			    sizeof(u32) * rdev->wiphy.n_cipher_suites,
2473 			    rdev->wiphy.cipher_suites))
2474 			goto nla_put_failure;
2475 
2476 		if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
2477 			       rdev->wiphy.max_num_pmkids))
2478 			goto nla_put_failure;
2479 
2480 		if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
2481 		    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
2482 			goto nla_put_failure;
2483 
2484 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
2485 				rdev->wiphy.available_antennas_tx) ||
2486 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
2487 				rdev->wiphy.available_antennas_rx))
2488 			goto nla_put_failure;
2489 
2490 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
2491 		    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
2492 				rdev->wiphy.probe_resp_offload))
2493 			goto nla_put_failure;
2494 
2495 		if ((rdev->wiphy.available_antennas_tx ||
2496 		     rdev->wiphy.available_antennas_rx) &&
2497 		    rdev->ops->get_antenna) {
2498 			u32 tx_ant = 0, rx_ant = 0;
2499 			int res;
2500 
2501 			res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
2502 			if (!res) {
2503 				if (nla_put_u32(msg,
2504 						NL80211_ATTR_WIPHY_ANTENNA_TX,
2505 						tx_ant) ||
2506 				    nla_put_u32(msg,
2507 						NL80211_ATTR_WIPHY_ANTENNA_RX,
2508 						rx_ant))
2509 					goto nla_put_failure;
2510 			}
2511 		}
2512 
2513 		state->split_start++;
2514 		if (state->split)
2515 			break;
2516 		fallthrough;
2517 	case 2:
2518 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
2519 					rdev->wiphy.interface_modes))
2520 				goto nla_put_failure;
2521 		state->split_start++;
2522 		if (state->split)
2523 			break;
2524 		fallthrough;
2525 	case 3:
2526 		nl_bands = nla_nest_start_noflag(msg,
2527 						 NL80211_ATTR_WIPHY_BANDS);
2528 		if (!nl_bands)
2529 			goto nla_put_failure;
2530 
2531 		for (band = state->band_start;
2532 		     band < (state->split ?
2533 				NUM_NL80211_BANDS :
2534 				NL80211_BAND_60GHZ + 1);
2535 		     band++) {
2536 			struct ieee80211_supported_band *sband;
2537 
2538 			/* omit higher bands for ancient software */
2539 			if (band > NL80211_BAND_5GHZ && !state->split)
2540 				break;
2541 
2542 			sband = rdev->wiphy.bands[band];
2543 
2544 			if (!sband)
2545 				continue;
2546 
2547 			nl_band = nla_nest_start_noflag(msg, band);
2548 			if (!nl_band)
2549 				goto nla_put_failure;
2550 
2551 			switch (state->chan_start) {
2552 			case 0:
2553 				if (nl80211_send_band_rateinfo(msg, sband,
2554 							       state->split))
2555 					goto nla_put_failure;
2556 				state->chan_start++;
2557 				if (state->split)
2558 					break;
2559 				fallthrough;
2560 			default:
2561 				/* add frequencies */
2562 				nl_freqs = nla_nest_start_noflag(msg,
2563 								 NL80211_BAND_ATTR_FREQS);
2564 				if (!nl_freqs)
2565 					goto nla_put_failure;
2566 
2567 				for (i = state->chan_start - 1;
2568 				     i < sband->n_channels;
2569 				     i++) {
2570 					nl_freq = nla_nest_start_noflag(msg,
2571 									i);
2572 					if (!nl_freq)
2573 						goto nla_put_failure;
2574 
2575 					chan = &sband->channels[i];
2576 
2577 					if (nl80211_msg_put_channel(
2578 							msg, &rdev->wiphy, chan,
2579 							state->split))
2580 						goto nla_put_failure;
2581 
2582 					nla_nest_end(msg, nl_freq);
2583 					if (state->split)
2584 						break;
2585 				}
2586 				if (i < sband->n_channels)
2587 					state->chan_start = i + 2;
2588 				else
2589 					state->chan_start = 0;
2590 				nla_nest_end(msg, nl_freqs);
2591 			}
2592 
2593 			nla_nest_end(msg, nl_band);
2594 
2595 			if (state->split) {
2596 				/* start again here */
2597 				if (state->chan_start)
2598 					band--;
2599 				break;
2600 			}
2601 		}
2602 		nla_nest_end(msg, nl_bands);
2603 
2604 		if (band < NUM_NL80211_BANDS)
2605 			state->band_start = band + 1;
2606 		else
2607 			state->band_start = 0;
2608 
2609 		/* if bands & channels are done, continue outside */
2610 		if (state->band_start == 0 && state->chan_start == 0)
2611 			state->split_start++;
2612 		if (state->split)
2613 			break;
2614 		fallthrough;
2615 	case 4:
2616 		nl_cmds = nla_nest_start_noflag(msg,
2617 						NL80211_ATTR_SUPPORTED_COMMANDS);
2618 		if (!nl_cmds)
2619 			goto nla_put_failure;
2620 
2621 		i = nl80211_add_commands_unsplit(rdev, msg);
2622 		if (i < 0)
2623 			goto nla_put_failure;
2624 		if (state->split) {
2625 			CMD(crit_proto_start, CRIT_PROTOCOL_START);
2626 			CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2627 			if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2628 				CMD(channel_switch, CHANNEL_SWITCH);
2629 			CMD(set_qos_map, SET_QOS_MAP);
2630 			if (rdev->wiphy.features &
2631 					NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2632 				CMD(add_tx_ts, ADD_TX_TS);
2633 			CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2634 			CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2635 			CMD(update_ft_ies, UPDATE_FT_IES);
2636 			if (rdev->wiphy.sar_capa)
2637 				CMD(set_sar_specs, SET_SAR_SPECS);
2638 		}
2639 #undef CMD
2640 
2641 		nla_nest_end(msg, nl_cmds);
2642 		state->split_start++;
2643 		if (state->split)
2644 			break;
2645 		fallthrough;
2646 	case 5:
2647 		if (rdev->ops->remain_on_channel &&
2648 		    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2649 		    nla_put_u32(msg,
2650 				NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2651 				rdev->wiphy.max_remain_on_channel_duration))
2652 			goto nla_put_failure;
2653 
2654 		if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2655 		    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2656 			goto nla_put_failure;
2657 
2658 		state->split_start++;
2659 		if (state->split)
2660 			break;
2661 		fallthrough;
2662 	case 6:
2663 #ifdef CONFIG_PM
2664 		if (nl80211_send_wowlan(msg, rdev, state->split))
2665 			goto nla_put_failure;
2666 		state->split_start++;
2667 		if (state->split)
2668 			break;
2669 #else
2670 		state->split_start++;
2671 #endif
2672 		fallthrough;
2673 	case 7:
2674 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2675 					rdev->wiphy.software_iftypes))
2676 			goto nla_put_failure;
2677 
2678 		if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2679 						   state->split))
2680 			goto nla_put_failure;
2681 
2682 		state->split_start++;
2683 		if (state->split)
2684 			break;
2685 		fallthrough;
2686 	case 8:
2687 		if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2688 		    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2689 				rdev->wiphy.ap_sme_capa))
2690 			goto nla_put_failure;
2691 
2692 		features = rdev->wiphy.features;
2693 		/*
2694 		 * We can only add the per-channel limit information if the
2695 		 * dump is split, otherwise it makes it too big. Therefore
2696 		 * only advertise it in that case.
2697 		 */
2698 		if (state->split)
2699 			features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2700 		if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2701 			goto nla_put_failure;
2702 
2703 		if (rdev->wiphy.ht_capa_mod_mask &&
2704 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2705 			    sizeof(*rdev->wiphy.ht_capa_mod_mask),
2706 			    rdev->wiphy.ht_capa_mod_mask))
2707 			goto nla_put_failure;
2708 
2709 		if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2710 		    rdev->wiphy.max_acl_mac_addrs &&
2711 		    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2712 				rdev->wiphy.max_acl_mac_addrs))
2713 			goto nla_put_failure;
2714 
2715 		/*
2716 		 * Any information below this point is only available to
2717 		 * applications that can deal with it being split. This
2718 		 * helps ensure that newly added capabilities don't break
2719 		 * older tools by overrunning their buffers.
2720 		 *
2721 		 * We still increment split_start so that in the split
2722 		 * case we'll continue with more data in the next round,
2723 		 * but break unconditionally so unsplit data stops here.
2724 		 */
2725 		if (state->split)
2726 			state->split_start++;
2727 		else
2728 			state->split_start = 0;
2729 		break;
2730 	case 9:
2731 		if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2732 			goto nla_put_failure;
2733 
2734 		if (nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
2735 				rdev->wiphy.max_sched_scan_plans) ||
2736 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
2737 				rdev->wiphy.max_sched_scan_plan_interval) ||
2738 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
2739 				rdev->wiphy.max_sched_scan_plan_iterations))
2740 			goto nla_put_failure;
2741 
2742 		if (rdev->wiphy.extended_capabilities &&
2743 		    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2744 			     rdev->wiphy.extended_capabilities_len,
2745 			     rdev->wiphy.extended_capabilities) ||
2746 		     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2747 			     rdev->wiphy.extended_capabilities_len,
2748 			     rdev->wiphy.extended_capabilities_mask)))
2749 			goto nla_put_failure;
2750 
2751 		if (rdev->wiphy.vht_capa_mod_mask &&
2752 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2753 			    sizeof(*rdev->wiphy.vht_capa_mod_mask),
2754 			    rdev->wiphy.vht_capa_mod_mask))
2755 			goto nla_put_failure;
2756 
2757 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2758 			    rdev->wiphy.perm_addr))
2759 			goto nla_put_failure;
2760 
2761 		if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2762 		    nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2763 			    rdev->wiphy.addr_mask))
2764 			goto nla_put_failure;
2765 
2766 		if (rdev->wiphy.n_addresses > 1) {
2767 			void *attr;
2768 
2769 			attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2770 			if (!attr)
2771 				goto nla_put_failure;
2772 
2773 			for (i = 0; i < rdev->wiphy.n_addresses; i++)
2774 				if (nla_put(msg, i + 1, ETH_ALEN,
2775 					    rdev->wiphy.addresses[i].addr))
2776 					goto nla_put_failure;
2777 
2778 			nla_nest_end(msg, attr);
2779 		}
2780 
2781 		state->split_start++;
2782 		break;
2783 	case 10:
2784 		if (nl80211_send_coalesce(msg, rdev))
2785 			goto nla_put_failure;
2786 
2787 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2788 		    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2789 		     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2790 			goto nla_put_failure;
2791 
2792 		if (rdev->wiphy.max_ap_assoc_sta &&
2793 		    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2794 				rdev->wiphy.max_ap_assoc_sta))
2795 			goto nla_put_failure;
2796 
2797 		state->split_start++;
2798 		break;
2799 	case 11:
2800 		if (rdev->wiphy.n_vendor_commands) {
2801 			const struct nl80211_vendor_cmd_info *info;
2802 			struct nlattr *nested;
2803 
2804 			nested = nla_nest_start_noflag(msg,
2805 						       NL80211_ATTR_VENDOR_DATA);
2806 			if (!nested)
2807 				goto nla_put_failure;
2808 
2809 			for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2810 				info = &rdev->wiphy.vendor_commands[i].info;
2811 				if (nla_put(msg, i + 1, sizeof(*info), info))
2812 					goto nla_put_failure;
2813 			}
2814 			nla_nest_end(msg, nested);
2815 		}
2816 
2817 		if (rdev->wiphy.n_vendor_events) {
2818 			const struct nl80211_vendor_cmd_info *info;
2819 			struct nlattr *nested;
2820 
2821 			nested = nla_nest_start_noflag(msg,
2822 						       NL80211_ATTR_VENDOR_EVENTS);
2823 			if (!nested)
2824 				goto nla_put_failure;
2825 
2826 			for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2827 				info = &rdev->wiphy.vendor_events[i];
2828 				if (nla_put(msg, i + 1, sizeof(*info), info))
2829 					goto nla_put_failure;
2830 			}
2831 			nla_nest_end(msg, nested);
2832 		}
2833 		state->split_start++;
2834 		break;
2835 	case 12:
2836 		if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2837 		    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2838 			       rdev->wiphy.max_num_csa_counters))
2839 			goto nla_put_failure;
2840 
2841 		if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2842 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2843 			goto nla_put_failure;
2844 
2845 		if (rdev->wiphy.max_sched_scan_reqs &&
2846 		    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2847 				rdev->wiphy.max_sched_scan_reqs))
2848 			goto nla_put_failure;
2849 
2850 		if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2851 			    sizeof(rdev->wiphy.ext_features),
2852 			    rdev->wiphy.ext_features))
2853 			goto nla_put_failure;
2854 
2855 		if (rdev->wiphy.bss_select_support) {
2856 			struct nlattr *nested;
2857 			u32 bss_select_support = rdev->wiphy.bss_select_support;
2858 
2859 			nested = nla_nest_start_noflag(msg,
2860 						       NL80211_ATTR_BSS_SELECT);
2861 			if (!nested)
2862 				goto nla_put_failure;
2863 
2864 			i = 0;
2865 			while (bss_select_support) {
2866 				if ((bss_select_support & 1) &&
2867 				    nla_put_flag(msg, i))
2868 					goto nla_put_failure;
2869 				i++;
2870 				bss_select_support >>= 1;
2871 			}
2872 			nla_nest_end(msg, nested);
2873 		}
2874 
2875 		state->split_start++;
2876 		break;
2877 	case 13:
2878 		if (rdev->wiphy.num_iftype_ext_capab &&
2879 		    rdev->wiphy.iftype_ext_capab) {
2880 			struct nlattr *nested_ext_capab, *nested;
2881 
2882 			nested = nla_nest_start_noflag(msg,
2883 						       NL80211_ATTR_IFTYPE_EXT_CAPA);
2884 			if (!nested)
2885 				goto nla_put_failure;
2886 
2887 			for (i = state->capa_start;
2888 			     i < rdev->wiphy.num_iftype_ext_capab; i++) {
2889 				const struct wiphy_iftype_ext_capab *capab;
2890 
2891 				capab = &rdev->wiphy.iftype_ext_capab[i];
2892 
2893 				nested_ext_capab = nla_nest_start_noflag(msg,
2894 									 i);
2895 				if (!nested_ext_capab ||
2896 				    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2897 						capab->iftype) ||
2898 				    nla_put(msg, NL80211_ATTR_EXT_CAPA,
2899 					    capab->extended_capabilities_len,
2900 					    capab->extended_capabilities) ||
2901 				    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2902 					    capab->extended_capabilities_len,
2903 					    capab->extended_capabilities_mask))
2904 					goto nla_put_failure;
2905 
2906 				if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO &&
2907 				    (nla_put_u16(msg,
2908 						 NL80211_ATTR_EML_CAPABILITY,
2909 						 capab->eml_capabilities) ||
2910 				     nla_put_u16(msg,
2911 						 NL80211_ATTR_MLD_CAPA_AND_OPS,
2912 						 capab->mld_capa_and_ops)))
2913 					goto nla_put_failure;
2914 
2915 				nla_nest_end(msg, nested_ext_capab);
2916 				if (state->split)
2917 					break;
2918 			}
2919 			nla_nest_end(msg, nested);
2920 			if (i < rdev->wiphy.num_iftype_ext_capab) {
2921 				state->capa_start = i + 1;
2922 				break;
2923 			}
2924 		}
2925 
2926 		if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2927 				rdev->wiphy.nan_supported_bands))
2928 			goto nla_put_failure;
2929 
2930 		if (wiphy_ext_feature_isset(&rdev->wiphy,
2931 					    NL80211_EXT_FEATURE_TXQS)) {
2932 			struct cfg80211_txq_stats txqstats = {};
2933 			int res;
2934 
2935 			res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2936 			if (!res &&
2937 			    !nl80211_put_txq_stats(msg, &txqstats,
2938 						   NL80211_ATTR_TXQ_STATS))
2939 				goto nla_put_failure;
2940 
2941 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2942 					rdev->wiphy.txq_limit))
2943 				goto nla_put_failure;
2944 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2945 					rdev->wiphy.txq_memory_limit))
2946 				goto nla_put_failure;
2947 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2948 					rdev->wiphy.txq_quantum))
2949 				goto nla_put_failure;
2950 		}
2951 
2952 		state->split_start++;
2953 		break;
2954 	case 14:
2955 		if (nl80211_send_pmsr_capa(rdev, msg))
2956 			goto nla_put_failure;
2957 
2958 		state->split_start++;
2959 		break;
2960 	case 15:
2961 		if (rdev->wiphy.akm_suites &&
2962 		    nla_put(msg, NL80211_ATTR_AKM_SUITES,
2963 			    sizeof(u32) * rdev->wiphy.n_akm_suites,
2964 			    rdev->wiphy.akm_suites))
2965 			goto nla_put_failure;
2966 
2967 		if (nl80211_put_iftype_akm_suites(rdev, msg))
2968 			goto nla_put_failure;
2969 
2970 		if (nl80211_put_tid_config_support(rdev, msg))
2971 			goto nla_put_failure;
2972 		state->split_start++;
2973 		break;
2974 	case 16:
2975 		if (nl80211_put_sar_specs(rdev, msg))
2976 			goto nla_put_failure;
2977 
2978 		if (nl80211_put_mbssid_support(&rdev->wiphy, msg))
2979 			goto nla_put_failure;
2980 
2981 		if (nla_put_u16(msg, NL80211_ATTR_MAX_NUM_AKM_SUITES,
2982 				rdev->wiphy.max_num_akm_suites))
2983 			goto nla_put_failure;
2984 
2985 		if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO)
2986 			nla_put_flag(msg, NL80211_ATTR_MLO_SUPPORT);
2987 
2988 		if (rdev->wiphy.hw_timestamp_max_peers &&
2989 		    nla_put_u16(msg, NL80211_ATTR_MAX_HW_TIMESTAMP_PEERS,
2990 				rdev->wiphy.hw_timestamp_max_peers))
2991 			goto nla_put_failure;
2992 
2993 		/* done */
2994 		state->split_start = 0;
2995 		break;
2996 	}
2997  finish:
2998 	genlmsg_end(msg, hdr);
2999 	return 0;
3000 
3001  nla_put_failure:
3002 	genlmsg_cancel(msg, hdr);
3003 	return -EMSGSIZE;
3004 }
3005 
3006 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
3007 				    struct netlink_callback *cb,
3008 				    struct nl80211_dump_wiphy_state *state)
3009 {
3010 	struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
3011 	int ret;
3012 
3013 	if (!tb)
3014 		return -ENOMEM;
3015 
3016 	ret = nlmsg_parse_deprecated(cb->nlh,
3017 				     GENL_HDRLEN + nl80211_fam.hdrsize,
3018 				     tb, nl80211_fam.maxattr,
3019 				     nl80211_policy, NULL);
3020 	/* ignore parse errors for backward compatibility */
3021 	if (ret) {
3022 		ret = 0;
3023 		goto out;
3024 	}
3025 
3026 	state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
3027 	if (tb[NL80211_ATTR_WIPHY])
3028 		state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
3029 	if (tb[NL80211_ATTR_WDEV])
3030 		state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
3031 	if (tb[NL80211_ATTR_IFINDEX]) {
3032 		struct net_device *netdev;
3033 		struct cfg80211_registered_device *rdev;
3034 		int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3035 
3036 		netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
3037 		if (!netdev) {
3038 			ret = -ENODEV;
3039 			goto out;
3040 		}
3041 		if (netdev->ieee80211_ptr) {
3042 			rdev = wiphy_to_rdev(
3043 				netdev->ieee80211_ptr->wiphy);
3044 			state->filter_wiphy = rdev->wiphy_idx;
3045 		}
3046 	}
3047 
3048 	ret = 0;
3049 out:
3050 	kfree(tb);
3051 	return ret;
3052 }
3053 
3054 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
3055 {
3056 	int idx = 0, ret;
3057 	struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
3058 	struct cfg80211_registered_device *rdev;
3059 
3060 	rtnl_lock();
3061 	if (!state) {
3062 		state = kzalloc(sizeof(*state), GFP_KERNEL);
3063 		if (!state) {
3064 			rtnl_unlock();
3065 			return -ENOMEM;
3066 		}
3067 		state->filter_wiphy = -1;
3068 		ret = nl80211_dump_wiphy_parse(skb, cb, state);
3069 		if (ret) {
3070 			kfree(state);
3071 			rtnl_unlock();
3072 			return ret;
3073 		}
3074 		cb->args[0] = (long)state;
3075 	}
3076 
3077 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3078 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3079 			continue;
3080 		if (++idx <= state->start)
3081 			continue;
3082 		if (state->filter_wiphy != -1 &&
3083 		    state->filter_wiphy != rdev->wiphy_idx)
3084 			continue;
3085 		wiphy_lock(&rdev->wiphy);
3086 		/* attempt to fit multiple wiphy data chunks into the skb */
3087 		do {
3088 			ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
3089 						 skb,
3090 						 NETLINK_CB(cb->skb).portid,
3091 						 cb->nlh->nlmsg_seq,
3092 						 NLM_F_MULTI, state);
3093 			if (ret < 0) {
3094 				/*
3095 				 * If sending the wiphy data didn't fit (ENOBUFS
3096 				 * or EMSGSIZE returned), this SKB is still
3097 				 * empty (so it's not too big because another
3098 				 * wiphy dataset is already in the skb) and
3099 				 * we've not tried to adjust the dump allocation
3100 				 * yet ... then adjust the alloc size to be
3101 				 * bigger, and return 1 but with the empty skb.
3102 				 * This results in an empty message being RX'ed
3103 				 * in userspace, but that is ignored.
3104 				 *
3105 				 * We can then retry with the larger buffer.
3106 				 */
3107 				if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
3108 				    !skb->len && !state->split &&
3109 				    cb->min_dump_alloc < 4096) {
3110 					cb->min_dump_alloc = 4096;
3111 					state->split_start = 0;
3112 					wiphy_unlock(&rdev->wiphy);
3113 					rtnl_unlock();
3114 					return 1;
3115 				}
3116 				idx--;
3117 				break;
3118 			}
3119 		} while (state->split_start > 0);
3120 		wiphy_unlock(&rdev->wiphy);
3121 		break;
3122 	}
3123 	rtnl_unlock();
3124 
3125 	state->start = idx;
3126 
3127 	return skb->len;
3128 }
3129 
3130 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
3131 {
3132 	kfree((void *)cb->args[0]);
3133 	return 0;
3134 }
3135 
3136 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
3137 {
3138 	struct sk_buff *msg;
3139 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3140 	struct nl80211_dump_wiphy_state state = {};
3141 
3142 	msg = nlmsg_new(4096, GFP_KERNEL);
3143 	if (!msg)
3144 		return -ENOMEM;
3145 
3146 	if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
3147 			       info->snd_portid, info->snd_seq, 0,
3148 			       &state) < 0) {
3149 		nlmsg_free(msg);
3150 		return -ENOBUFS;
3151 	}
3152 
3153 	return genlmsg_reply(msg, info);
3154 }
3155 
3156 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
3157 	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
3158 	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
3159 	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
3160 	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
3161 	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
3162 };
3163 
3164 static int parse_txq_params(struct nlattr *tb[],
3165 			    struct ieee80211_txq_params *txq_params)
3166 {
3167 	u8 ac;
3168 
3169 	if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
3170 	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
3171 	    !tb[NL80211_TXQ_ATTR_AIFS])
3172 		return -EINVAL;
3173 
3174 	ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
3175 	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
3176 	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
3177 	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
3178 	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
3179 
3180 	if (ac >= NL80211_NUM_ACS)
3181 		return -EINVAL;
3182 	txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
3183 	return 0;
3184 }
3185 
3186 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
3187 {
3188 	/*
3189 	 * You can only set the channel explicitly for some interfaces,
3190 	 * most have their channel managed via their respective
3191 	 * "establish a connection" command (connect, join, ...)
3192 	 *
3193 	 * For AP/GO and mesh mode, the channel can be set with the
3194 	 * channel userspace API, but is only stored and passed to the
3195 	 * low-level driver when the AP starts or the mesh is joined.
3196 	 * This is for backward compatibility, userspace can also give
3197 	 * the channel in the start-ap or join-mesh commands instead.
3198 	 *
3199 	 * Monitors are special as they are normally slaved to
3200 	 * whatever else is going on, so they have their own special
3201 	 * operation to set the monitor channel if possible.
3202 	 */
3203 	return !wdev ||
3204 		wdev->iftype == NL80211_IFTYPE_AP ||
3205 		wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
3206 		wdev->iftype == NL80211_IFTYPE_MONITOR ||
3207 		wdev->iftype == NL80211_IFTYPE_P2P_GO;
3208 }
3209 
3210 static int nl80211_parse_punct_bitmap(struct cfg80211_registered_device *rdev,
3211 				      struct genl_info *info,
3212 				      const struct cfg80211_chan_def *chandef,
3213 				      u16 *punct_bitmap)
3214 {
3215 	if (!wiphy_ext_feature_isset(&rdev->wiphy, NL80211_EXT_FEATURE_PUNCT))
3216 		return -EINVAL;
3217 
3218 	*punct_bitmap = nla_get_u32(info->attrs[NL80211_ATTR_PUNCT_BITMAP]);
3219 	if (!cfg80211_valid_disable_subchannel_bitmap(punct_bitmap, chandef))
3220 		return -EINVAL;
3221 
3222 	return 0;
3223 }
3224 
3225 int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
3226 			  struct genl_info *info,
3227 			  struct cfg80211_chan_def *chandef)
3228 {
3229 	struct netlink_ext_ack *extack = info->extack;
3230 	struct nlattr **attrs = info->attrs;
3231 	u32 control_freq;
3232 
3233 	if (!attrs[NL80211_ATTR_WIPHY_FREQ]) {
3234 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
3235 				    "Frequency is missing");
3236 		return -EINVAL;
3237 	}
3238 
3239 	control_freq = MHZ_TO_KHZ(
3240 			nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3241 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
3242 		control_freq +=
3243 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
3244 
3245 	memset(chandef, 0, sizeof(*chandef));
3246 	chandef->chan = ieee80211_get_channel_khz(&rdev->wiphy, control_freq);
3247 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
3248 	chandef->center_freq1 = KHZ_TO_MHZ(control_freq);
3249 	chandef->freq1_offset = control_freq % 1000;
3250 	chandef->center_freq2 = 0;
3251 
3252 	/* Primary channel not allowed */
3253 	if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED) {
3254 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
3255 				    "Channel is disabled");
3256 		return -EINVAL;
3257 	}
3258 
3259 	if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
3260 		enum nl80211_channel_type chantype;
3261 
3262 		chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
3263 
3264 		switch (chantype) {
3265 		case NL80211_CHAN_NO_HT:
3266 		case NL80211_CHAN_HT20:
3267 		case NL80211_CHAN_HT40PLUS:
3268 		case NL80211_CHAN_HT40MINUS:
3269 			cfg80211_chandef_create(chandef, chandef->chan,
3270 						chantype);
3271 			/* user input for center_freq is incorrect */
3272 			if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
3273 			    chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
3274 				NL_SET_ERR_MSG_ATTR(extack,
3275 						    attrs[NL80211_ATTR_CENTER_FREQ1],
3276 						    "bad center frequency 1");
3277 				return -EINVAL;
3278 			}
3279 			/* center_freq2 must be zero */
3280 			if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
3281 			    nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
3282 				NL_SET_ERR_MSG_ATTR(extack,
3283 						    attrs[NL80211_ATTR_CENTER_FREQ2],
3284 						    "center frequency 2 can't be used");
3285 				return -EINVAL;
3286 			}
3287 			break;
3288 		default:
3289 			NL_SET_ERR_MSG_ATTR(extack,
3290 					    attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3291 					    "invalid channel type");
3292 			return -EINVAL;
3293 		}
3294 	} else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
3295 		chandef->width =
3296 			nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
3297 		if (chandef->chan->band == NL80211_BAND_S1GHZ) {
3298 			/* User input error for channel width doesn't match channel  */
3299 			if (chandef->width != ieee80211_s1g_channel_width(chandef->chan)) {
3300 				NL_SET_ERR_MSG_ATTR(extack,
3301 						    attrs[NL80211_ATTR_CHANNEL_WIDTH],
3302 						    "bad channel width");
3303 				return -EINVAL;
3304 			}
3305 		}
3306 		if (attrs[NL80211_ATTR_CENTER_FREQ1]) {
3307 			chandef->center_freq1 =
3308 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
3309 			if (attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET])
3310 				chandef->freq1_offset = nla_get_u32(
3311 				      attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET]);
3312 			else
3313 				chandef->freq1_offset = 0;
3314 		}
3315 		if (attrs[NL80211_ATTR_CENTER_FREQ2])
3316 			chandef->center_freq2 =
3317 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
3318 	}
3319 
3320 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
3321 		chandef->edmg.channels =
3322 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
3323 
3324 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
3325 			chandef->edmg.bw_config =
3326 		     nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
3327 	} else {
3328 		chandef->edmg.bw_config = 0;
3329 		chandef->edmg.channels = 0;
3330 	}
3331 
3332 	if (!cfg80211_chandef_valid(chandef)) {
3333 		NL_SET_ERR_MSG(extack, "invalid channel definition");
3334 		return -EINVAL;
3335 	}
3336 
3337 	if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
3338 				     IEEE80211_CHAN_DISABLED)) {
3339 		NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
3340 		return -EINVAL;
3341 	}
3342 
3343 	if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
3344 	     chandef->width == NL80211_CHAN_WIDTH_10) &&
3345 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
3346 		NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
3347 		return -EINVAL;
3348 	}
3349 
3350 	return 0;
3351 }
3352 
3353 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
3354 				 struct net_device *dev,
3355 				 struct genl_info *info,
3356 				 int _link_id)
3357 {
3358 	struct cfg80211_chan_def chandef;
3359 	int result;
3360 	enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
3361 	struct wireless_dev *wdev = NULL;
3362 	int link_id = _link_id;
3363 
3364 	if (dev)
3365 		wdev = dev->ieee80211_ptr;
3366 	if (!nl80211_can_set_dev_channel(wdev))
3367 		return -EOPNOTSUPP;
3368 	if (wdev)
3369 		iftype = wdev->iftype;
3370 
3371 	if (link_id < 0) {
3372 		if (wdev && wdev->valid_links)
3373 			return -EINVAL;
3374 		link_id = 0;
3375 	}
3376 
3377 	result = nl80211_parse_chandef(rdev, info, &chandef);
3378 	if (result)
3379 		return result;
3380 
3381 	switch (iftype) {
3382 	case NL80211_IFTYPE_AP:
3383 	case NL80211_IFTYPE_P2P_GO:
3384 		if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
3385 						   iftype))
3386 			return -EINVAL;
3387 		if (wdev->links[link_id].ap.beacon_interval) {
3388 			struct ieee80211_channel *cur_chan;
3389 
3390 			if (!dev || !rdev->ops->set_ap_chanwidth ||
3391 			    !(rdev->wiphy.features &
3392 			      NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE))
3393 				return -EBUSY;
3394 
3395 			/* Only allow dynamic channel width changes */
3396 			cur_chan = wdev->links[link_id].ap.chandef.chan;
3397 			if (chandef.chan != cur_chan)
3398 				return -EBUSY;
3399 
3400 			result = rdev_set_ap_chanwidth(rdev, dev, link_id,
3401 						       &chandef);
3402 			if (result)
3403 				return result;
3404 			wdev->links[link_id].ap.chandef = chandef;
3405 		} else {
3406 			wdev->u.ap.preset_chandef = chandef;
3407 		}
3408 		return 0;
3409 	case NL80211_IFTYPE_MESH_POINT:
3410 		return cfg80211_set_mesh_channel(rdev, wdev, &chandef);
3411 	case NL80211_IFTYPE_MONITOR:
3412 		return cfg80211_set_monitor_channel(rdev, &chandef);
3413 	default:
3414 		break;
3415 	}
3416 
3417 	return -EINVAL;
3418 }
3419 
3420 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
3421 {
3422 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3423 	int link_id = nl80211_link_id_or_invalid(info->attrs);
3424 	struct net_device *netdev = info->user_ptr[1];
3425 	int ret;
3426 
3427 	wdev_lock(netdev->ieee80211_ptr);
3428 	ret = __nl80211_set_channel(rdev, netdev, info, link_id);
3429 	wdev_unlock(netdev->ieee80211_ptr);
3430 
3431 	return ret;
3432 }
3433 
3434 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
3435 {
3436 	struct cfg80211_registered_device *rdev = NULL;
3437 	struct net_device *netdev = NULL;
3438 	struct wireless_dev *wdev;
3439 	int result = 0, rem_txq_params = 0;
3440 	struct nlattr *nl_txq_params;
3441 	u32 changed;
3442 	u8 retry_short = 0, retry_long = 0;
3443 	u32 frag_threshold = 0, rts_threshold = 0;
3444 	u8 coverage_class = 0;
3445 	u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
3446 
3447 	rtnl_lock();
3448 	/*
3449 	 * Try to find the wiphy and netdev. Normally this
3450 	 * function shouldn't need the netdev, but this is
3451 	 * done for backward compatibility -- previously
3452 	 * setting the channel was done per wiphy, but now
3453 	 * it is per netdev. Previous userland like hostapd
3454 	 * also passed a netdev to set_wiphy, so that it is
3455 	 * possible to let that go to the right netdev!
3456 	 */
3457 
3458 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
3459 		int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
3460 
3461 		netdev = __dev_get_by_index(genl_info_net(info), ifindex);
3462 		if (netdev && netdev->ieee80211_ptr)
3463 			rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
3464 		else
3465 			netdev = NULL;
3466 	}
3467 
3468 	if (!netdev) {
3469 		rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
3470 						  info->attrs);
3471 		if (IS_ERR(rdev)) {
3472 			rtnl_unlock();
3473 			return PTR_ERR(rdev);
3474 		}
3475 		wdev = NULL;
3476 		netdev = NULL;
3477 		result = 0;
3478 	} else
3479 		wdev = netdev->ieee80211_ptr;
3480 
3481 	wiphy_lock(&rdev->wiphy);
3482 
3483 	/*
3484 	 * end workaround code, by now the rdev is available
3485 	 * and locked, and wdev may or may not be NULL.
3486 	 */
3487 
3488 	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
3489 		result = cfg80211_dev_rename(
3490 			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
3491 	rtnl_unlock();
3492 
3493 	if (result)
3494 		goto out;
3495 
3496 	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
3497 		struct ieee80211_txq_params txq_params;
3498 		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
3499 
3500 		if (!rdev->ops->set_txq_params) {
3501 			result = -EOPNOTSUPP;
3502 			goto out;
3503 		}
3504 
3505 		if (!netdev) {
3506 			result = -EINVAL;
3507 			goto out;
3508 		}
3509 
3510 		if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3511 		    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3512 			result = -EINVAL;
3513 			goto out;
3514 		}
3515 
3516 		if (!netif_running(netdev)) {
3517 			result = -ENETDOWN;
3518 			goto out;
3519 		}
3520 
3521 		nla_for_each_nested(nl_txq_params,
3522 				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
3523 				    rem_txq_params) {
3524 			result = nla_parse_nested_deprecated(tb,
3525 							     NL80211_TXQ_ATTR_MAX,
3526 							     nl_txq_params,
3527 							     txq_params_policy,
3528 							     info->extack);
3529 			if (result)
3530 				goto out;
3531 			result = parse_txq_params(tb, &txq_params);
3532 			if (result)
3533 				goto out;
3534 
3535 			txq_params.link_id =
3536 				nl80211_link_id_or_invalid(info->attrs);
3537 
3538 			wdev_lock(netdev->ieee80211_ptr);
3539 			if (txq_params.link_id >= 0 &&
3540 			    !(netdev->ieee80211_ptr->valid_links &
3541 			      BIT(txq_params.link_id)))
3542 				result = -ENOLINK;
3543 			else if (txq_params.link_id >= 0 &&
3544 				 !netdev->ieee80211_ptr->valid_links)
3545 				result = -EINVAL;
3546 			else
3547 				result = rdev_set_txq_params(rdev, netdev,
3548 							     &txq_params);
3549 			wdev_unlock(netdev->ieee80211_ptr);
3550 			if (result)
3551 				goto out;
3552 		}
3553 	}
3554 
3555 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3556 		int link_id = nl80211_link_id_or_invalid(info->attrs);
3557 
3558 		if (wdev) {
3559 			wdev_lock(wdev);
3560 			result = __nl80211_set_channel(
3561 				rdev,
3562 				nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
3563 				info, link_id);
3564 			wdev_unlock(wdev);
3565 		} else {
3566 			result = __nl80211_set_channel(rdev, netdev, info, link_id);
3567 		}
3568 
3569 		if (result)
3570 			goto out;
3571 	}
3572 
3573 	if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
3574 		struct wireless_dev *txp_wdev = wdev;
3575 		enum nl80211_tx_power_setting type;
3576 		int idx, mbm = 0;
3577 
3578 		if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
3579 			txp_wdev = NULL;
3580 
3581 		if (!rdev->ops->set_tx_power) {
3582 			result = -EOPNOTSUPP;
3583 			goto out;
3584 		}
3585 
3586 		idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
3587 		type = nla_get_u32(info->attrs[idx]);
3588 
3589 		if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
3590 		    (type != NL80211_TX_POWER_AUTOMATIC)) {
3591 			result = -EINVAL;
3592 			goto out;
3593 		}
3594 
3595 		if (type != NL80211_TX_POWER_AUTOMATIC) {
3596 			idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
3597 			mbm = nla_get_u32(info->attrs[idx]);
3598 		}
3599 
3600 		result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
3601 		if (result)
3602 			goto out;
3603 	}
3604 
3605 	if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
3606 	    info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
3607 		u32 tx_ant, rx_ant;
3608 
3609 		if ((!rdev->wiphy.available_antennas_tx &&
3610 		     !rdev->wiphy.available_antennas_rx) ||
3611 		    !rdev->ops->set_antenna) {
3612 			result = -EOPNOTSUPP;
3613 			goto out;
3614 		}
3615 
3616 		tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
3617 		rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
3618 
3619 		/* reject antenna configurations which don't match the
3620 		 * available antenna masks, except for the "all" mask */
3621 		if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
3622 		    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
3623 			result = -EINVAL;
3624 			goto out;
3625 		}
3626 
3627 		tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
3628 		rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
3629 
3630 		result = rdev_set_antenna(rdev, tx_ant, rx_ant);
3631 		if (result)
3632 			goto out;
3633 	}
3634 
3635 	changed = 0;
3636 
3637 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
3638 		retry_short = nla_get_u8(
3639 			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
3640 
3641 		changed |= WIPHY_PARAM_RETRY_SHORT;
3642 	}
3643 
3644 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
3645 		retry_long = nla_get_u8(
3646 			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
3647 
3648 		changed |= WIPHY_PARAM_RETRY_LONG;
3649 	}
3650 
3651 	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
3652 		frag_threshold = nla_get_u32(
3653 			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
3654 		if (frag_threshold < 256) {
3655 			result = -EINVAL;
3656 			goto out;
3657 		}
3658 
3659 		if (frag_threshold != (u32) -1) {
3660 			/*
3661 			 * Fragments (apart from the last one) are required to
3662 			 * have even length. Make the fragmentation code
3663 			 * simpler by stripping LSB should someone try to use
3664 			 * odd threshold value.
3665 			 */
3666 			frag_threshold &= ~0x1;
3667 		}
3668 		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
3669 	}
3670 
3671 	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
3672 		rts_threshold = nla_get_u32(
3673 			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
3674 		changed |= WIPHY_PARAM_RTS_THRESHOLD;
3675 	}
3676 
3677 	if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
3678 		if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3679 			result = -EINVAL;
3680 			goto out;
3681 		}
3682 
3683 		coverage_class = nla_get_u8(
3684 			info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
3685 		changed |= WIPHY_PARAM_COVERAGE_CLASS;
3686 	}
3687 
3688 	if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3689 		if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION)) {
3690 			result = -EOPNOTSUPP;
3691 			goto out;
3692 		}
3693 
3694 		changed |= WIPHY_PARAM_DYN_ACK;
3695 	}
3696 
3697 	if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3698 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3699 					     NL80211_EXT_FEATURE_TXQS)) {
3700 			result = -EOPNOTSUPP;
3701 			goto out;
3702 		}
3703 		txq_limit = nla_get_u32(
3704 			info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3705 		changed |= WIPHY_PARAM_TXQ_LIMIT;
3706 	}
3707 
3708 	if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3709 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3710 					     NL80211_EXT_FEATURE_TXQS)) {
3711 			result = -EOPNOTSUPP;
3712 			goto out;
3713 		}
3714 		txq_memory_limit = nla_get_u32(
3715 			info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3716 		changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3717 	}
3718 
3719 	if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3720 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3721 					     NL80211_EXT_FEATURE_TXQS)) {
3722 			result = -EOPNOTSUPP;
3723 			goto out;
3724 		}
3725 		txq_quantum = nla_get_u32(
3726 			info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3727 		changed |= WIPHY_PARAM_TXQ_QUANTUM;
3728 	}
3729 
3730 	if (changed) {
3731 		u8 old_retry_short, old_retry_long;
3732 		u32 old_frag_threshold, old_rts_threshold;
3733 		u8 old_coverage_class;
3734 		u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3735 
3736 		if (!rdev->ops->set_wiphy_params) {
3737 			result = -EOPNOTSUPP;
3738 			goto out;
3739 		}
3740 
3741 		old_retry_short = rdev->wiphy.retry_short;
3742 		old_retry_long = rdev->wiphy.retry_long;
3743 		old_frag_threshold = rdev->wiphy.frag_threshold;
3744 		old_rts_threshold = rdev->wiphy.rts_threshold;
3745 		old_coverage_class = rdev->wiphy.coverage_class;
3746 		old_txq_limit = rdev->wiphy.txq_limit;
3747 		old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3748 		old_txq_quantum = rdev->wiphy.txq_quantum;
3749 
3750 		if (changed & WIPHY_PARAM_RETRY_SHORT)
3751 			rdev->wiphy.retry_short = retry_short;
3752 		if (changed & WIPHY_PARAM_RETRY_LONG)
3753 			rdev->wiphy.retry_long = retry_long;
3754 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3755 			rdev->wiphy.frag_threshold = frag_threshold;
3756 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3757 			rdev->wiphy.rts_threshold = rts_threshold;
3758 		if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3759 			rdev->wiphy.coverage_class = coverage_class;
3760 		if (changed & WIPHY_PARAM_TXQ_LIMIT)
3761 			rdev->wiphy.txq_limit = txq_limit;
3762 		if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3763 			rdev->wiphy.txq_memory_limit = txq_memory_limit;
3764 		if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3765 			rdev->wiphy.txq_quantum = txq_quantum;
3766 
3767 		result = rdev_set_wiphy_params(rdev, changed);
3768 		if (result) {
3769 			rdev->wiphy.retry_short = old_retry_short;
3770 			rdev->wiphy.retry_long = old_retry_long;
3771 			rdev->wiphy.frag_threshold = old_frag_threshold;
3772 			rdev->wiphy.rts_threshold = old_rts_threshold;
3773 			rdev->wiphy.coverage_class = old_coverage_class;
3774 			rdev->wiphy.txq_limit = old_txq_limit;
3775 			rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3776 			rdev->wiphy.txq_quantum = old_txq_quantum;
3777 			goto out;
3778 		}
3779 	}
3780 
3781 	result = 0;
3782 
3783 out:
3784 	wiphy_unlock(&rdev->wiphy);
3785 	return result;
3786 }
3787 
3788 int nl80211_send_chandef(struct sk_buff *msg, const struct cfg80211_chan_def *chandef)
3789 {
3790 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3791 		return -EINVAL;
3792 
3793 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3794 			chandef->chan->center_freq))
3795 		return -ENOBUFS;
3796 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
3797 			chandef->chan->freq_offset))
3798 		return -ENOBUFS;
3799 	switch (chandef->width) {
3800 	case NL80211_CHAN_WIDTH_20_NOHT:
3801 	case NL80211_CHAN_WIDTH_20:
3802 	case NL80211_CHAN_WIDTH_40:
3803 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3804 				cfg80211_get_chandef_type(chandef)))
3805 			return -ENOBUFS;
3806 		break;
3807 	default:
3808 		break;
3809 	}
3810 	if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3811 		return -ENOBUFS;
3812 	if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3813 		return -ENOBUFS;
3814 	if (chandef->center_freq2 &&
3815 	    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3816 		return -ENOBUFS;
3817 	return 0;
3818 }
3819 EXPORT_SYMBOL(nl80211_send_chandef);
3820 
3821 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3822 			      struct cfg80211_registered_device *rdev,
3823 			      struct wireless_dev *wdev,
3824 			      enum nl80211_commands cmd)
3825 {
3826 	struct net_device *dev = wdev->netdev;
3827 	void *hdr;
3828 
3829 	WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3830 		cmd != NL80211_CMD_DEL_INTERFACE &&
3831 		cmd != NL80211_CMD_SET_INTERFACE);
3832 
3833 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3834 	if (!hdr)
3835 		return -1;
3836 
3837 	if (dev &&
3838 	    (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3839 	     nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3840 		goto nla_put_failure;
3841 
3842 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3843 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3844 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3845 			      NL80211_ATTR_PAD) ||
3846 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3847 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
3848 			rdev->devlist_generation ^
3849 			(cfg80211_rdev_list_generation << 2)) ||
3850 	    nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3851 		goto nla_put_failure;
3852 
3853 	if (rdev->ops->get_channel && !wdev->valid_links) {
3854 		struct cfg80211_chan_def chandef = {};
3855 		int ret;
3856 
3857 		ret = rdev_get_channel(rdev, wdev, 0, &chandef);
3858 		if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3859 			goto nla_put_failure;
3860 	}
3861 
3862 	if (rdev->ops->get_tx_power) {
3863 		int dbm, ret;
3864 
3865 		ret = rdev_get_tx_power(rdev, wdev, &dbm);
3866 		if (ret == 0 &&
3867 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3868 				DBM_TO_MBM(dbm)))
3869 			goto nla_put_failure;
3870 	}
3871 
3872 	wdev_lock(wdev);
3873 	switch (wdev->iftype) {
3874 	case NL80211_IFTYPE_AP:
3875 	case NL80211_IFTYPE_P2P_GO:
3876 		if (wdev->u.ap.ssid_len &&
3877 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.ap.ssid_len,
3878 			    wdev->u.ap.ssid))
3879 			goto nla_put_failure_locked;
3880 		break;
3881 	case NL80211_IFTYPE_STATION:
3882 	case NL80211_IFTYPE_P2P_CLIENT:
3883 		if (wdev->u.client.ssid_len &&
3884 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.client.ssid_len,
3885 			    wdev->u.client.ssid))
3886 			goto nla_put_failure_locked;
3887 		break;
3888 	case NL80211_IFTYPE_ADHOC:
3889 		if (wdev->u.ibss.ssid_len &&
3890 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.ibss.ssid_len,
3891 			    wdev->u.ibss.ssid))
3892 			goto nla_put_failure_locked;
3893 		break;
3894 	default:
3895 		/* nothing */
3896 		break;
3897 	}
3898 	wdev_unlock(wdev);
3899 
3900 	if (rdev->ops->get_txq_stats) {
3901 		struct cfg80211_txq_stats txqstats = {};
3902 		int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3903 
3904 		if (ret == 0 &&
3905 		    !nl80211_put_txq_stats(msg, &txqstats,
3906 					   NL80211_ATTR_TXQ_STATS))
3907 			goto nla_put_failure;
3908 	}
3909 
3910 	if (wdev->valid_links) {
3911 		unsigned int link_id;
3912 		struct nlattr *links = nla_nest_start(msg,
3913 						      NL80211_ATTR_MLO_LINKS);
3914 
3915 		if (!links)
3916 			goto nla_put_failure;
3917 
3918 		for_each_valid_link(wdev, link_id) {
3919 			struct nlattr *link = nla_nest_start(msg, link_id + 1);
3920 			struct cfg80211_chan_def chandef = {};
3921 			int ret;
3922 
3923 			if (!link)
3924 				goto nla_put_failure;
3925 
3926 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
3927 				goto nla_put_failure;
3928 			if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
3929 				    wdev->links[link_id].addr))
3930 				goto nla_put_failure;
3931 
3932 			ret = rdev_get_channel(rdev, wdev, link_id, &chandef);
3933 			if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3934 				goto nla_put_failure;
3935 
3936 			nla_nest_end(msg, link);
3937 		}
3938 
3939 		nla_nest_end(msg, links);
3940 	}
3941 
3942 	genlmsg_end(msg, hdr);
3943 	return 0;
3944 
3945  nla_put_failure_locked:
3946 	wdev_unlock(wdev);
3947  nla_put_failure:
3948 	genlmsg_cancel(msg, hdr);
3949 	return -EMSGSIZE;
3950 }
3951 
3952 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3953 {
3954 	int wp_idx = 0;
3955 	int if_idx = 0;
3956 	int wp_start = cb->args[0];
3957 	int if_start = cb->args[1];
3958 	int filter_wiphy = -1;
3959 	struct cfg80211_registered_device *rdev;
3960 	struct wireless_dev *wdev;
3961 	int ret;
3962 
3963 	rtnl_lock();
3964 	if (!cb->args[2]) {
3965 		struct nl80211_dump_wiphy_state state = {
3966 			.filter_wiphy = -1,
3967 		};
3968 
3969 		ret = nl80211_dump_wiphy_parse(skb, cb, &state);
3970 		if (ret)
3971 			goto out_unlock;
3972 
3973 		filter_wiphy = state.filter_wiphy;
3974 
3975 		/*
3976 		 * if filtering, set cb->args[2] to +1 since 0 is the default
3977 		 * value needed to determine that parsing is necessary.
3978 		 */
3979 		if (filter_wiphy >= 0)
3980 			cb->args[2] = filter_wiphy + 1;
3981 		else
3982 			cb->args[2] = -1;
3983 	} else if (cb->args[2] > 0) {
3984 		filter_wiphy = cb->args[2] - 1;
3985 	}
3986 
3987 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3988 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3989 			continue;
3990 		if (wp_idx < wp_start) {
3991 			wp_idx++;
3992 			continue;
3993 		}
3994 
3995 		if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
3996 			continue;
3997 
3998 		if_idx = 0;
3999 
4000 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
4001 			if (if_idx < if_start) {
4002 				if_idx++;
4003 				continue;
4004 			}
4005 			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
4006 					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
4007 					       rdev, wdev,
4008 					       NL80211_CMD_NEW_INTERFACE) < 0) {
4009 				goto out;
4010 			}
4011 			if_idx++;
4012 		}
4013 
4014 		wp_idx++;
4015 	}
4016  out:
4017 	cb->args[0] = wp_idx;
4018 	cb->args[1] = if_idx;
4019 
4020 	ret = skb->len;
4021  out_unlock:
4022 	rtnl_unlock();
4023 
4024 	return ret;
4025 }
4026 
4027 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
4028 {
4029 	struct sk_buff *msg;
4030 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4031 	struct wireless_dev *wdev = info->user_ptr[1];
4032 
4033 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4034 	if (!msg)
4035 		return -ENOMEM;
4036 
4037 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
4038 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
4039 		nlmsg_free(msg);
4040 		return -ENOBUFS;
4041 	}
4042 
4043 	return genlmsg_reply(msg, info);
4044 }
4045 
4046 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
4047 	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
4048 	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
4049 	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
4050 	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
4051 	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
4052 	[NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
4053 };
4054 
4055 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
4056 {
4057 	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
4058 	int flag;
4059 
4060 	*mntrflags = 0;
4061 
4062 	if (!nla)
4063 		return -EINVAL;
4064 
4065 	if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
4066 		return -EINVAL;
4067 
4068 	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
4069 		if (flags[flag])
4070 			*mntrflags |= (1<<flag);
4071 
4072 	*mntrflags |= MONITOR_FLAG_CHANGED;
4073 
4074 	return 0;
4075 }
4076 
4077 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
4078 				     enum nl80211_iftype type,
4079 				     struct genl_info *info,
4080 				     struct vif_params *params)
4081 {
4082 	bool change = false;
4083 	int err;
4084 
4085 	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
4086 		if (type != NL80211_IFTYPE_MONITOR)
4087 			return -EINVAL;
4088 
4089 		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
4090 					  &params->flags);
4091 		if (err)
4092 			return err;
4093 
4094 		change = true;
4095 	}
4096 
4097 	if (params->flags & MONITOR_FLAG_ACTIVE &&
4098 	    !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
4099 		return -EOPNOTSUPP;
4100 
4101 	if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
4102 		const u8 *mumimo_groups;
4103 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4104 
4105 		if (type != NL80211_IFTYPE_MONITOR)
4106 			return -EINVAL;
4107 
4108 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4109 			return -EOPNOTSUPP;
4110 
4111 		mumimo_groups =
4112 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
4113 
4114 		/* bits 0 and 63 are reserved and must be zero */
4115 		if ((mumimo_groups[0] & BIT(0)) ||
4116 		    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
4117 			return -EINVAL;
4118 
4119 		params->vht_mumimo_groups = mumimo_groups;
4120 		change = true;
4121 	}
4122 
4123 	if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
4124 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4125 
4126 		if (type != NL80211_IFTYPE_MONITOR)
4127 			return -EINVAL;
4128 
4129 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4130 			return -EOPNOTSUPP;
4131 
4132 		params->vht_mumimo_follow_addr =
4133 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
4134 		change = true;
4135 	}
4136 
4137 	return change ? 1 : 0;
4138 }
4139 
4140 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
4141 			       struct net_device *netdev, u8 use_4addr,
4142 			       enum nl80211_iftype iftype)
4143 {
4144 	if (!use_4addr) {
4145 		if (netdev && netif_is_bridge_port(netdev))
4146 			return -EBUSY;
4147 		return 0;
4148 	}
4149 
4150 	switch (iftype) {
4151 	case NL80211_IFTYPE_AP_VLAN:
4152 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
4153 			return 0;
4154 		break;
4155 	case NL80211_IFTYPE_STATION:
4156 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
4157 			return 0;
4158 		break;
4159 	default:
4160 		break;
4161 	}
4162 
4163 	return -EOPNOTSUPP;
4164 }
4165 
4166 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
4167 {
4168 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4169 	struct vif_params params;
4170 	int err;
4171 	enum nl80211_iftype otype, ntype;
4172 	struct net_device *dev = info->user_ptr[1];
4173 	bool change = false;
4174 
4175 	memset(&params, 0, sizeof(params));
4176 
4177 	otype = ntype = dev->ieee80211_ptr->iftype;
4178 
4179 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
4180 		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4181 		if (otype != ntype)
4182 			change = true;
4183 	}
4184 
4185 	if (info->attrs[NL80211_ATTR_MESH_ID]) {
4186 		struct wireless_dev *wdev = dev->ieee80211_ptr;
4187 
4188 		if (ntype != NL80211_IFTYPE_MESH_POINT)
4189 			return -EINVAL;
4190 		if (netif_running(dev))
4191 			return -EBUSY;
4192 
4193 		wdev_lock(wdev);
4194 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
4195 			     IEEE80211_MAX_MESH_ID_LEN);
4196 		wdev->u.mesh.id_up_len =
4197 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4198 		memcpy(wdev->u.mesh.id,
4199 		       nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4200 		       wdev->u.mesh.id_up_len);
4201 		wdev_unlock(wdev);
4202 	}
4203 
4204 	if (info->attrs[NL80211_ATTR_4ADDR]) {
4205 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4206 		change = true;
4207 		err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
4208 		if (err)
4209 			return err;
4210 	} else {
4211 		params.use_4addr = -1;
4212 	}
4213 
4214 	err = nl80211_parse_mon_options(rdev, ntype, info, &params);
4215 	if (err < 0)
4216 		return err;
4217 	if (err > 0)
4218 		change = true;
4219 
4220 	if (change)
4221 		err = cfg80211_change_iface(rdev, dev, ntype, &params);
4222 	else
4223 		err = 0;
4224 
4225 	if (!err && params.use_4addr != -1)
4226 		dev->ieee80211_ptr->use_4addr = params.use_4addr;
4227 
4228 	if (change && !err) {
4229 		struct wireless_dev *wdev = dev->ieee80211_ptr;
4230 
4231 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
4232 	}
4233 
4234 	return err;
4235 }
4236 
4237 static int _nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4238 {
4239 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4240 	struct vif_params params;
4241 	struct wireless_dev *wdev;
4242 	struct sk_buff *msg;
4243 	int err;
4244 	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
4245 
4246 	memset(&params, 0, sizeof(params));
4247 
4248 	if (!info->attrs[NL80211_ATTR_IFNAME])
4249 		return -EINVAL;
4250 
4251 	if (info->attrs[NL80211_ATTR_IFTYPE])
4252 		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4253 
4254 	if (!rdev->ops->add_virtual_intf)
4255 		return -EOPNOTSUPP;
4256 
4257 	if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
4258 	     rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
4259 	    info->attrs[NL80211_ATTR_MAC]) {
4260 		nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
4261 			   ETH_ALEN);
4262 		if (!is_valid_ether_addr(params.macaddr))
4263 			return -EADDRNOTAVAIL;
4264 	}
4265 
4266 	if (info->attrs[NL80211_ATTR_4ADDR]) {
4267 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4268 		err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
4269 		if (err)
4270 			return err;
4271 	}
4272 
4273 	if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
4274 		return -EOPNOTSUPP;
4275 
4276 	err = nl80211_parse_mon_options(rdev, type, info, &params);
4277 	if (err < 0)
4278 		return err;
4279 
4280 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4281 	if (!msg)
4282 		return -ENOMEM;
4283 
4284 	wdev = rdev_add_virtual_intf(rdev,
4285 				nla_data(info->attrs[NL80211_ATTR_IFNAME]),
4286 				NET_NAME_USER, type, &params);
4287 	if (WARN_ON(!wdev)) {
4288 		nlmsg_free(msg);
4289 		return -EPROTO;
4290 	} else if (IS_ERR(wdev)) {
4291 		nlmsg_free(msg);
4292 		return PTR_ERR(wdev);
4293 	}
4294 
4295 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
4296 		wdev->owner_nlportid = info->snd_portid;
4297 
4298 	switch (type) {
4299 	case NL80211_IFTYPE_MESH_POINT:
4300 		if (!info->attrs[NL80211_ATTR_MESH_ID])
4301 			break;
4302 		wdev_lock(wdev);
4303 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
4304 			     IEEE80211_MAX_MESH_ID_LEN);
4305 		wdev->u.mesh.id_up_len =
4306 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4307 		memcpy(wdev->u.mesh.id,
4308 		       nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4309 		       wdev->u.mesh.id_up_len);
4310 		wdev_unlock(wdev);
4311 		break;
4312 	case NL80211_IFTYPE_NAN:
4313 	case NL80211_IFTYPE_P2P_DEVICE:
4314 		/*
4315 		 * P2P Device and NAN do not have a netdev, so don't go
4316 		 * through the netdev notifier and must be added here
4317 		 */
4318 		cfg80211_init_wdev(wdev);
4319 		cfg80211_register_wdev(rdev, wdev);
4320 		break;
4321 	default:
4322 		break;
4323 	}
4324 
4325 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
4326 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
4327 		nlmsg_free(msg);
4328 		return -ENOBUFS;
4329 	}
4330 
4331 	return genlmsg_reply(msg, info);
4332 }
4333 
4334 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4335 {
4336 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4337 	int ret;
4338 
4339 	/* to avoid failing a new interface creation due to pending removal */
4340 	cfg80211_destroy_ifaces(rdev);
4341 
4342 	wiphy_lock(&rdev->wiphy);
4343 	ret = _nl80211_new_interface(skb, info);
4344 	wiphy_unlock(&rdev->wiphy);
4345 
4346 	return ret;
4347 }
4348 
4349 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
4350 {
4351 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4352 	struct wireless_dev *wdev = info->user_ptr[1];
4353 
4354 	if (!rdev->ops->del_virtual_intf)
4355 		return -EOPNOTSUPP;
4356 
4357 	/*
4358 	 * We hold RTNL, so this is safe, without RTNL opencount cannot
4359 	 * reach 0, and thus the rdev cannot be deleted.
4360 	 *
4361 	 * We need to do it for the dev_close(), since that will call
4362 	 * the netdev notifiers, and we need to acquire the mutex there
4363 	 * but don't know if we get there from here or from some other
4364 	 * place (e.g. "ip link set ... down").
4365 	 */
4366 	mutex_unlock(&rdev->wiphy.mtx);
4367 
4368 	/*
4369 	 * If we remove a wireless device without a netdev then clear
4370 	 * user_ptr[1] so that nl80211_post_doit won't dereference it
4371 	 * to check if it needs to do dev_put(). Otherwise it crashes
4372 	 * since the wdev has been freed, unlike with a netdev where
4373 	 * we need the dev_put() for the netdev to really be freed.
4374 	 */
4375 	if (!wdev->netdev)
4376 		info->user_ptr[1] = NULL;
4377 	else
4378 		dev_close(wdev->netdev);
4379 
4380 	mutex_lock(&rdev->wiphy.mtx);
4381 
4382 	return cfg80211_remove_virtual_intf(rdev, wdev);
4383 }
4384 
4385 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
4386 {
4387 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4388 	struct net_device *dev = info->user_ptr[1];
4389 	u16 noack_map;
4390 
4391 	if (!info->attrs[NL80211_ATTR_NOACK_MAP])
4392 		return -EINVAL;
4393 
4394 	if (!rdev->ops->set_noack_map)
4395 		return -EOPNOTSUPP;
4396 
4397 	noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
4398 
4399 	return rdev_set_noack_map(rdev, dev, noack_map);
4400 }
4401 
4402 static int nl80211_validate_key_link_id(struct genl_info *info,
4403 					struct wireless_dev *wdev,
4404 					int link_id, bool pairwise)
4405 {
4406 	if (pairwise) {
4407 		if (link_id != -1) {
4408 			GENL_SET_ERR_MSG(info,
4409 					 "link ID not allowed for pairwise key");
4410 			return -EINVAL;
4411 		}
4412 
4413 		return 0;
4414 	}
4415 
4416 	if (wdev->valid_links) {
4417 		if (link_id == -1) {
4418 			GENL_SET_ERR_MSG(info,
4419 					 "link ID must for MLO group key");
4420 			return -EINVAL;
4421 		}
4422 		if (!(wdev->valid_links & BIT(link_id))) {
4423 			GENL_SET_ERR_MSG(info, "invalid link ID for MLO group key");
4424 			return -EINVAL;
4425 		}
4426 	} else if (link_id != -1) {
4427 		GENL_SET_ERR_MSG(info, "link ID not allowed for non-MLO group key");
4428 		return -EINVAL;
4429 	}
4430 
4431 	return 0;
4432 }
4433 
4434 struct get_key_cookie {
4435 	struct sk_buff *msg;
4436 	int error;
4437 	int idx;
4438 };
4439 
4440 static void get_key_callback(void *c, struct key_params *params)
4441 {
4442 	struct nlattr *key;
4443 	struct get_key_cookie *cookie = c;
4444 
4445 	if ((params->key &&
4446 	     nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
4447 		     params->key_len, params->key)) ||
4448 	    (params->seq &&
4449 	     nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
4450 		     params->seq_len, params->seq)) ||
4451 	    (params->cipher &&
4452 	     nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
4453 			 params->cipher)))
4454 		goto nla_put_failure;
4455 
4456 	key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
4457 	if (!key)
4458 		goto nla_put_failure;
4459 
4460 	if ((params->key &&
4461 	     nla_put(cookie->msg, NL80211_KEY_DATA,
4462 		     params->key_len, params->key)) ||
4463 	    (params->seq &&
4464 	     nla_put(cookie->msg, NL80211_KEY_SEQ,
4465 		     params->seq_len, params->seq)) ||
4466 	    (params->cipher &&
4467 	     nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
4468 			 params->cipher)))
4469 		goto nla_put_failure;
4470 
4471 	if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
4472 		goto nla_put_failure;
4473 
4474 	nla_nest_end(cookie->msg, key);
4475 
4476 	return;
4477  nla_put_failure:
4478 	cookie->error = 1;
4479 }
4480 
4481 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
4482 {
4483 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4484 	int err;
4485 	struct net_device *dev = info->user_ptr[1];
4486 	u8 key_idx = 0;
4487 	const u8 *mac_addr = NULL;
4488 	bool pairwise;
4489 	struct get_key_cookie cookie = {
4490 		.error = 0,
4491 	};
4492 	void *hdr;
4493 	struct sk_buff *msg;
4494 	bool bigtk_support = false;
4495 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4496 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4497 
4498 	if (wiphy_ext_feature_isset(&rdev->wiphy,
4499 				    NL80211_EXT_FEATURE_BEACON_PROTECTION))
4500 		bigtk_support = true;
4501 
4502 	if ((wdev->iftype == NL80211_IFTYPE_STATION ||
4503 	     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
4504 	    wiphy_ext_feature_isset(&rdev->wiphy,
4505 				    NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
4506 		bigtk_support = true;
4507 
4508 	if (info->attrs[NL80211_ATTR_KEY_IDX]) {
4509 		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
4510 
4511 		if (key_idx >= 6 && key_idx <= 7 && !bigtk_support) {
4512 			GENL_SET_ERR_MSG(info, "BIGTK not supported");
4513 			return -EINVAL;
4514 		}
4515 	}
4516 
4517 	if (info->attrs[NL80211_ATTR_MAC])
4518 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4519 
4520 	pairwise = !!mac_addr;
4521 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
4522 		u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
4523 
4524 		if (kt != NL80211_KEYTYPE_GROUP &&
4525 		    kt != NL80211_KEYTYPE_PAIRWISE)
4526 			return -EINVAL;
4527 		pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
4528 	}
4529 
4530 	if (!rdev->ops->get_key)
4531 		return -EOPNOTSUPP;
4532 
4533 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4534 		return -ENOENT;
4535 
4536 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4537 	if (!msg)
4538 		return -ENOMEM;
4539 
4540 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
4541 			     NL80211_CMD_NEW_KEY);
4542 	if (!hdr)
4543 		goto nla_put_failure;
4544 
4545 	cookie.msg = msg;
4546 	cookie.idx = key_idx;
4547 
4548 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4549 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
4550 		goto nla_put_failure;
4551 	if (mac_addr &&
4552 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
4553 		goto nla_put_failure;
4554 
4555 	err = nl80211_validate_key_link_id(info, wdev, link_id, pairwise);
4556 	if (err)
4557 		goto free_msg;
4558 
4559 	err = rdev_get_key(rdev, dev, link_id, key_idx, pairwise, mac_addr,
4560 			   &cookie, get_key_callback);
4561 
4562 	if (err)
4563 		goto free_msg;
4564 
4565 	if (cookie.error)
4566 		goto nla_put_failure;
4567 
4568 	genlmsg_end(msg, hdr);
4569 	return genlmsg_reply(msg, info);
4570 
4571  nla_put_failure:
4572 	err = -ENOBUFS;
4573  free_msg:
4574 	nlmsg_free(msg);
4575 	return err;
4576 }
4577 
4578 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
4579 {
4580 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4581 	struct key_parse key;
4582 	int err;
4583 	struct net_device *dev = info->user_ptr[1];
4584 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4585 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4586 
4587 	err = nl80211_parse_key(info, &key);
4588 	if (err)
4589 		return err;
4590 
4591 	if (key.idx < 0)
4592 		return -EINVAL;
4593 
4594 	/* Only support setting default key and
4595 	 * Extended Key ID action NL80211_KEY_SET_TX.
4596 	 */
4597 	if (!key.def && !key.defmgmt && !key.defbeacon &&
4598 	    !(key.p.mode == NL80211_KEY_SET_TX))
4599 		return -EINVAL;
4600 
4601 	wdev_lock(wdev);
4602 
4603 	if (key.def) {
4604 		if (!rdev->ops->set_default_key) {
4605 			err = -EOPNOTSUPP;
4606 			goto out;
4607 		}
4608 
4609 		err = nl80211_key_allowed(wdev);
4610 		if (err)
4611 			goto out;
4612 
4613 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4614 		if (err)
4615 			goto out;
4616 
4617 		err = rdev_set_default_key(rdev, dev, link_id, key.idx,
4618 					   key.def_uni, key.def_multi);
4619 
4620 		if (err)
4621 			goto out;
4622 
4623 #ifdef CONFIG_CFG80211_WEXT
4624 		wdev->wext.default_key = key.idx;
4625 #endif
4626 	} else if (key.defmgmt) {
4627 		if (key.def_uni || !key.def_multi) {
4628 			err = -EINVAL;
4629 			goto out;
4630 		}
4631 
4632 		if (!rdev->ops->set_default_mgmt_key) {
4633 			err = -EOPNOTSUPP;
4634 			goto out;
4635 		}
4636 
4637 		err = nl80211_key_allowed(wdev);
4638 		if (err)
4639 			goto out;
4640 
4641 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4642 		if (err)
4643 			goto out;
4644 
4645 		err = rdev_set_default_mgmt_key(rdev, dev, link_id, key.idx);
4646 		if (err)
4647 			goto out;
4648 
4649 #ifdef CONFIG_CFG80211_WEXT
4650 		wdev->wext.default_mgmt_key = key.idx;
4651 #endif
4652 	} else if (key.defbeacon) {
4653 		if (key.def_uni || !key.def_multi) {
4654 			err = -EINVAL;
4655 			goto out;
4656 		}
4657 
4658 		if (!rdev->ops->set_default_beacon_key) {
4659 			err = -EOPNOTSUPP;
4660 			goto out;
4661 		}
4662 
4663 		err = nl80211_key_allowed(wdev);
4664 		if (err)
4665 			goto out;
4666 
4667 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4668 		if (err)
4669 			goto out;
4670 
4671 		err = rdev_set_default_beacon_key(rdev, dev, link_id, key.idx);
4672 		if (err)
4673 			goto out;
4674 	} else if (key.p.mode == NL80211_KEY_SET_TX &&
4675 		   wiphy_ext_feature_isset(&rdev->wiphy,
4676 					   NL80211_EXT_FEATURE_EXT_KEY_ID)) {
4677 		u8 *mac_addr = NULL;
4678 
4679 		if (info->attrs[NL80211_ATTR_MAC])
4680 			mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4681 
4682 		if (!mac_addr || key.idx < 0 || key.idx > 1) {
4683 			err = -EINVAL;
4684 			goto out;
4685 		}
4686 
4687 		err = nl80211_validate_key_link_id(info, wdev, link_id, true);
4688 		if (err)
4689 			goto out;
4690 
4691 		err = rdev_add_key(rdev, dev, link_id, key.idx,
4692 				   NL80211_KEYTYPE_PAIRWISE,
4693 				   mac_addr, &key.p);
4694 	} else {
4695 		err = -EINVAL;
4696 	}
4697  out:
4698 	wdev_unlock(wdev);
4699 
4700 	return err;
4701 }
4702 
4703 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
4704 {
4705 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4706 	int err;
4707 	struct net_device *dev = info->user_ptr[1];
4708 	struct key_parse key;
4709 	const u8 *mac_addr = NULL;
4710 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4711 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4712 
4713 	err = nl80211_parse_key(info, &key);
4714 	if (err)
4715 		return err;
4716 
4717 	if (!key.p.key) {
4718 		GENL_SET_ERR_MSG(info, "no key");
4719 		return -EINVAL;
4720 	}
4721 
4722 	if (info->attrs[NL80211_ATTR_MAC])
4723 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4724 
4725 	if (key.type == -1) {
4726 		if (mac_addr)
4727 			key.type = NL80211_KEYTYPE_PAIRWISE;
4728 		else
4729 			key.type = NL80211_KEYTYPE_GROUP;
4730 	}
4731 
4732 	/* for now */
4733 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4734 	    key.type != NL80211_KEYTYPE_GROUP) {
4735 		GENL_SET_ERR_MSG(info, "key type not pairwise or group");
4736 		return -EINVAL;
4737 	}
4738 
4739 	if (key.type == NL80211_KEYTYPE_GROUP &&
4740 	    info->attrs[NL80211_ATTR_VLAN_ID])
4741 		key.p.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
4742 
4743 	if (!rdev->ops->add_key)
4744 		return -EOPNOTSUPP;
4745 
4746 	if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
4747 					   key.type == NL80211_KEYTYPE_PAIRWISE,
4748 					   mac_addr)) {
4749 		GENL_SET_ERR_MSG(info, "key setting validation failed");
4750 		return -EINVAL;
4751 	}
4752 
4753 	wdev_lock(wdev);
4754 	err = nl80211_key_allowed(wdev);
4755 	if (err)
4756 		GENL_SET_ERR_MSG(info, "key not allowed");
4757 
4758 	if (!err)
4759 		err = nl80211_validate_key_link_id(info, wdev, link_id,
4760 				key.type == NL80211_KEYTYPE_PAIRWISE);
4761 
4762 	if (!err) {
4763 		err = rdev_add_key(rdev, dev, link_id, key.idx,
4764 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4765 				    mac_addr, &key.p);
4766 		if (err)
4767 			GENL_SET_ERR_MSG(info, "key addition failed");
4768 	}
4769 	wdev_unlock(wdev);
4770 
4771 	return err;
4772 }
4773 
4774 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
4775 {
4776 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4777 	int err;
4778 	struct net_device *dev = info->user_ptr[1];
4779 	u8 *mac_addr = NULL;
4780 	struct key_parse key;
4781 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4782 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4783 
4784 	err = nl80211_parse_key(info, &key);
4785 	if (err)
4786 		return err;
4787 
4788 	if (info->attrs[NL80211_ATTR_MAC])
4789 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4790 
4791 	if (key.type == -1) {
4792 		if (mac_addr)
4793 			key.type = NL80211_KEYTYPE_PAIRWISE;
4794 		else
4795 			key.type = NL80211_KEYTYPE_GROUP;
4796 	}
4797 
4798 	/* for now */
4799 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4800 	    key.type != NL80211_KEYTYPE_GROUP)
4801 		return -EINVAL;
4802 
4803 	if (!cfg80211_valid_key_idx(rdev, key.idx,
4804 				    key.type == NL80211_KEYTYPE_PAIRWISE))
4805 		return -EINVAL;
4806 
4807 	if (!rdev->ops->del_key)
4808 		return -EOPNOTSUPP;
4809 
4810 	wdev_lock(wdev);
4811 	err = nl80211_key_allowed(wdev);
4812 
4813 	if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
4814 	    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4815 		err = -ENOENT;
4816 
4817 	if (!err)
4818 		err = nl80211_validate_key_link_id(info, wdev, link_id,
4819 				key.type == NL80211_KEYTYPE_PAIRWISE);
4820 
4821 	if (!err)
4822 		err = rdev_del_key(rdev, dev, link_id, key.idx,
4823 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4824 				   mac_addr);
4825 
4826 #ifdef CONFIG_CFG80211_WEXT
4827 	if (!err) {
4828 		if (key.idx == wdev->wext.default_key)
4829 			wdev->wext.default_key = -1;
4830 		else if (key.idx == wdev->wext.default_mgmt_key)
4831 			wdev->wext.default_mgmt_key = -1;
4832 	}
4833 #endif
4834 	wdev_unlock(wdev);
4835 
4836 	return err;
4837 }
4838 
4839 /* This function returns an error or the number of nested attributes */
4840 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
4841 {
4842 	struct nlattr *attr;
4843 	int n_entries = 0, tmp;
4844 
4845 	nla_for_each_nested(attr, nl_attr, tmp) {
4846 		if (nla_len(attr) != ETH_ALEN)
4847 			return -EINVAL;
4848 
4849 		n_entries++;
4850 	}
4851 
4852 	return n_entries;
4853 }
4854 
4855 /*
4856  * This function parses ACL information and allocates memory for ACL data.
4857  * On successful return, the calling function is responsible to free the
4858  * ACL buffer returned by this function.
4859  */
4860 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
4861 						struct genl_info *info)
4862 {
4863 	enum nl80211_acl_policy acl_policy;
4864 	struct nlattr *attr;
4865 	struct cfg80211_acl_data *acl;
4866 	int i = 0, n_entries, tmp;
4867 
4868 	if (!wiphy->max_acl_mac_addrs)
4869 		return ERR_PTR(-EOPNOTSUPP);
4870 
4871 	if (!info->attrs[NL80211_ATTR_ACL_POLICY])
4872 		return ERR_PTR(-EINVAL);
4873 
4874 	acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
4875 	if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
4876 	    acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
4877 		return ERR_PTR(-EINVAL);
4878 
4879 	if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
4880 		return ERR_PTR(-EINVAL);
4881 
4882 	n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
4883 	if (n_entries < 0)
4884 		return ERR_PTR(n_entries);
4885 
4886 	if (n_entries > wiphy->max_acl_mac_addrs)
4887 		return ERR_PTR(-ENOTSUPP);
4888 
4889 	acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4890 	if (!acl)
4891 		return ERR_PTR(-ENOMEM);
4892 	acl->n_acl_entries = n_entries;
4893 
4894 	nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4895 		memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4896 		i++;
4897 	}
4898 	acl->acl_policy = acl_policy;
4899 
4900 	return acl;
4901 }
4902 
4903 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4904 {
4905 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4906 	struct net_device *dev = info->user_ptr[1];
4907 	struct cfg80211_acl_data *acl;
4908 	int err;
4909 
4910 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4911 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4912 		return -EOPNOTSUPP;
4913 
4914 	if (!dev->ieee80211_ptr->links[0].ap.beacon_interval)
4915 		return -EINVAL;
4916 
4917 	acl = parse_acl_data(&rdev->wiphy, info);
4918 	if (IS_ERR(acl))
4919 		return PTR_ERR(acl);
4920 
4921 	err = rdev_set_mac_acl(rdev, dev, acl);
4922 
4923 	kfree(acl);
4924 
4925 	return err;
4926 }
4927 
4928 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4929 			   u8 *rates, u8 rates_len)
4930 {
4931 	u8 i;
4932 	u32 mask = 0;
4933 
4934 	for (i = 0; i < rates_len; i++) {
4935 		int rate = (rates[i] & 0x7f) * 5;
4936 		int ridx;
4937 
4938 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4939 			struct ieee80211_rate *srate =
4940 				&sband->bitrates[ridx];
4941 			if (rate == srate->bitrate) {
4942 				mask |= 1 << ridx;
4943 				break;
4944 			}
4945 		}
4946 		if (ridx == sband->n_bitrates)
4947 			return 0; /* rate not found */
4948 	}
4949 
4950 	return mask;
4951 }
4952 
4953 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4954 			       u8 *rates, u8 rates_len,
4955 			       u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4956 {
4957 	u8 i;
4958 
4959 	memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4960 
4961 	for (i = 0; i < rates_len; i++) {
4962 		int ridx, rbit;
4963 
4964 		ridx = rates[i] / 8;
4965 		rbit = BIT(rates[i] % 8);
4966 
4967 		/* check validity */
4968 		if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4969 			return false;
4970 
4971 		/* check availability */
4972 		ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
4973 		if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
4974 			mcs[ridx] |= rbit;
4975 		else
4976 			return false;
4977 	}
4978 
4979 	return true;
4980 }
4981 
4982 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
4983 {
4984 	u16 mcs_mask = 0;
4985 
4986 	switch (vht_mcs_map) {
4987 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
4988 		break;
4989 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
4990 		mcs_mask = 0x00FF;
4991 		break;
4992 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
4993 		mcs_mask = 0x01FF;
4994 		break;
4995 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
4996 		mcs_mask = 0x03FF;
4997 		break;
4998 	default:
4999 		break;
5000 	}
5001 
5002 	return mcs_mask;
5003 }
5004 
5005 static void vht_build_mcs_mask(u16 vht_mcs_map,
5006 			       u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
5007 {
5008 	u8 nss;
5009 
5010 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
5011 		vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
5012 		vht_mcs_map >>= 2;
5013 	}
5014 }
5015 
5016 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
5017 			     struct nl80211_txrate_vht *txrate,
5018 			     u16 mcs[NL80211_VHT_NSS_MAX])
5019 {
5020 	u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5021 	u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
5022 	u8 i;
5023 
5024 	if (!sband->vht_cap.vht_supported)
5025 		return false;
5026 
5027 	memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
5028 
5029 	/* Build vht_mcs_mask from VHT capabilities */
5030 	vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
5031 
5032 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5033 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
5034 			mcs[i] = txrate->mcs[i];
5035 		else
5036 			return false;
5037 	}
5038 
5039 	return true;
5040 }
5041 
5042 static u16 he_mcs_map_to_mcs_mask(u8 he_mcs_map)
5043 {
5044 	switch (he_mcs_map) {
5045 	case IEEE80211_HE_MCS_NOT_SUPPORTED:
5046 		return 0;
5047 	case IEEE80211_HE_MCS_SUPPORT_0_7:
5048 		return 0x00FF;
5049 	case IEEE80211_HE_MCS_SUPPORT_0_9:
5050 		return 0x03FF;
5051 	case IEEE80211_HE_MCS_SUPPORT_0_11:
5052 		return 0xFFF;
5053 	default:
5054 		break;
5055 	}
5056 	return 0;
5057 }
5058 
5059 static void he_build_mcs_mask(u16 he_mcs_map,
5060 			      u16 he_mcs_mask[NL80211_HE_NSS_MAX])
5061 {
5062 	u8 nss;
5063 
5064 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
5065 		he_mcs_mask[nss] = he_mcs_map_to_mcs_mask(he_mcs_map & 0x03);
5066 		he_mcs_map >>= 2;
5067 	}
5068 }
5069 
5070 static u16 he_get_txmcsmap(struct genl_info *info, unsigned int link_id,
5071 			   const struct ieee80211_sta_he_cap *he_cap)
5072 {
5073 	struct net_device *dev = info->user_ptr[1];
5074 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5075 	struct cfg80211_chan_def *chandef;
5076 	__le16 tx_mcs;
5077 
5078 	chandef = wdev_chandef(wdev, link_id);
5079 	if (!chandef) {
5080 		/*
5081 		 * This is probably broken, but we never maintained
5082 		 * a chandef in these cases, so it always was.
5083 		 */
5084 		return le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
5085 	}
5086 
5087 	switch (chandef->width) {
5088 	case NL80211_CHAN_WIDTH_80P80:
5089 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80p80;
5090 		break;
5091 	case NL80211_CHAN_WIDTH_160:
5092 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_160;
5093 		break;
5094 	default:
5095 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80;
5096 		break;
5097 	}
5098 
5099 	return le16_to_cpu(tx_mcs);
5100 }
5101 
5102 static bool he_set_mcs_mask(struct genl_info *info,
5103 			    struct wireless_dev *wdev,
5104 			    struct ieee80211_supported_band *sband,
5105 			    struct nl80211_txrate_he *txrate,
5106 			    u16 mcs[NL80211_HE_NSS_MAX],
5107 			    unsigned int link_id)
5108 {
5109 	const struct ieee80211_sta_he_cap *he_cap;
5110 	u16 tx_mcs_mask[NL80211_HE_NSS_MAX] = {};
5111 	u16 tx_mcs_map = 0;
5112 	u8 i;
5113 
5114 	he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5115 	if (!he_cap)
5116 		return false;
5117 
5118 	memset(mcs, 0, sizeof(u16) * NL80211_HE_NSS_MAX);
5119 
5120 	tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5121 
5122 	/* Build he_mcs_mask from HE capabilities */
5123 	he_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
5124 
5125 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5126 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
5127 			mcs[i] = txrate->mcs[i];
5128 		else
5129 			return false;
5130 	}
5131 
5132 	return true;
5133 }
5134 
5135 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
5136 					 struct nlattr *attrs[],
5137 					 enum nl80211_attrs attr,
5138 					 struct cfg80211_bitrate_mask *mask,
5139 					 struct net_device *dev,
5140 					 bool default_all_enabled,
5141 					 unsigned int link_id)
5142 {
5143 	struct nlattr *tb[NL80211_TXRATE_MAX + 1];
5144 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5145 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5146 	int rem, i;
5147 	struct nlattr *tx_rates;
5148 	struct ieee80211_supported_band *sband;
5149 	u16 vht_tx_mcs_map, he_tx_mcs_map;
5150 
5151 	memset(mask, 0, sizeof(*mask));
5152 	/* Default to all rates enabled */
5153 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
5154 		const struct ieee80211_sta_he_cap *he_cap;
5155 
5156 		if (!default_all_enabled)
5157 			break;
5158 
5159 		sband = rdev->wiphy.bands[i];
5160 
5161 		if (!sband)
5162 			continue;
5163 
5164 		mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
5165 		memcpy(mask->control[i].ht_mcs,
5166 		       sband->ht_cap.mcs.rx_mask,
5167 		       sizeof(mask->control[i].ht_mcs));
5168 
5169 		if (sband->vht_cap.vht_supported) {
5170 			vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5171 			vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
5172 		}
5173 
5174 		he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5175 		if (!he_cap)
5176 			continue;
5177 
5178 		he_tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5179 		he_build_mcs_mask(he_tx_mcs_map, mask->control[i].he_mcs);
5180 
5181 		mask->control[i].he_gi = 0xFF;
5182 		mask->control[i].he_ltf = 0xFF;
5183 	}
5184 
5185 	/* if no rates are given set it back to the defaults */
5186 	if (!attrs[attr])
5187 		goto out;
5188 
5189 	/* The nested attribute uses enum nl80211_band as the index. This maps
5190 	 * directly to the enum nl80211_band values used in cfg80211.
5191 	 */
5192 	BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
5193 	nla_for_each_nested(tx_rates, attrs[attr], rem) {
5194 		enum nl80211_band band = nla_type(tx_rates);
5195 		int err;
5196 
5197 		if (band < 0 || band >= NUM_NL80211_BANDS)
5198 			return -EINVAL;
5199 		sband = rdev->wiphy.bands[band];
5200 		if (sband == NULL)
5201 			return -EINVAL;
5202 		err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
5203 						  tx_rates,
5204 						  nl80211_txattr_policy,
5205 						  info->extack);
5206 		if (err)
5207 			return err;
5208 		if (tb[NL80211_TXRATE_LEGACY]) {
5209 			mask->control[band].legacy = rateset_to_mask(
5210 				sband,
5211 				nla_data(tb[NL80211_TXRATE_LEGACY]),
5212 				nla_len(tb[NL80211_TXRATE_LEGACY]));
5213 			if ((mask->control[band].legacy == 0) &&
5214 			    nla_len(tb[NL80211_TXRATE_LEGACY]))
5215 				return -EINVAL;
5216 		}
5217 		if (tb[NL80211_TXRATE_HT]) {
5218 			if (!ht_rateset_to_mask(
5219 					sband,
5220 					nla_data(tb[NL80211_TXRATE_HT]),
5221 					nla_len(tb[NL80211_TXRATE_HT]),
5222 					mask->control[band].ht_mcs))
5223 				return -EINVAL;
5224 		}
5225 
5226 		if (tb[NL80211_TXRATE_VHT]) {
5227 			if (!vht_set_mcs_mask(
5228 					sband,
5229 					nla_data(tb[NL80211_TXRATE_VHT]),
5230 					mask->control[band].vht_mcs))
5231 				return -EINVAL;
5232 		}
5233 
5234 		if (tb[NL80211_TXRATE_GI]) {
5235 			mask->control[band].gi =
5236 				nla_get_u8(tb[NL80211_TXRATE_GI]);
5237 			if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
5238 				return -EINVAL;
5239 		}
5240 		if (tb[NL80211_TXRATE_HE] &&
5241 		    !he_set_mcs_mask(info, wdev, sband,
5242 				     nla_data(tb[NL80211_TXRATE_HE]),
5243 				     mask->control[band].he_mcs,
5244 				     link_id))
5245 			return -EINVAL;
5246 
5247 		if (tb[NL80211_TXRATE_HE_GI])
5248 			mask->control[band].he_gi =
5249 				nla_get_u8(tb[NL80211_TXRATE_HE_GI]);
5250 		if (tb[NL80211_TXRATE_HE_LTF])
5251 			mask->control[band].he_ltf =
5252 				nla_get_u8(tb[NL80211_TXRATE_HE_LTF]);
5253 
5254 		if (mask->control[band].legacy == 0) {
5255 			/* don't allow empty legacy rates if HT, VHT or HE
5256 			 * are not even supported.
5257 			 */
5258 			if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
5259 			      rdev->wiphy.bands[band]->vht_cap.vht_supported ||
5260 			      ieee80211_get_he_iftype_cap(sband, wdev->iftype)))
5261 				return -EINVAL;
5262 
5263 			for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5264 				if (mask->control[band].ht_mcs[i])
5265 					goto out;
5266 
5267 			for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5268 				if (mask->control[band].vht_mcs[i])
5269 					goto out;
5270 
5271 			for (i = 0; i < NL80211_HE_NSS_MAX; i++)
5272 				if (mask->control[band].he_mcs[i])
5273 					goto out;
5274 
5275 			/* legacy and mcs rates may not be both empty */
5276 			return -EINVAL;
5277 		}
5278 	}
5279 
5280 out:
5281 	return 0;
5282 }
5283 
5284 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
5285 				   enum nl80211_band band,
5286 				   struct cfg80211_bitrate_mask *beacon_rate)
5287 {
5288 	u32 count_ht, count_vht, count_he, i;
5289 	u32 rate = beacon_rate->control[band].legacy;
5290 
5291 	/* Allow only one rate */
5292 	if (hweight32(rate) > 1)
5293 		return -EINVAL;
5294 
5295 	count_ht = 0;
5296 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
5297 		if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
5298 			return -EINVAL;
5299 		} else if (beacon_rate->control[band].ht_mcs[i]) {
5300 			count_ht++;
5301 			if (count_ht > 1)
5302 				return -EINVAL;
5303 		}
5304 		if (count_ht && rate)
5305 			return -EINVAL;
5306 	}
5307 
5308 	count_vht = 0;
5309 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5310 		if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
5311 			return -EINVAL;
5312 		} else if (beacon_rate->control[band].vht_mcs[i]) {
5313 			count_vht++;
5314 			if (count_vht > 1)
5315 				return -EINVAL;
5316 		}
5317 		if (count_vht && rate)
5318 			return -EINVAL;
5319 	}
5320 
5321 	count_he = 0;
5322 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5323 		if (hweight16(beacon_rate->control[band].he_mcs[i]) > 1) {
5324 			return -EINVAL;
5325 		} else if (beacon_rate->control[band].he_mcs[i]) {
5326 			count_he++;
5327 			if (count_he > 1)
5328 				return -EINVAL;
5329 		}
5330 		if (count_he && rate)
5331 			return -EINVAL;
5332 	}
5333 
5334 	if ((count_ht && count_vht && count_he) ||
5335 	    (!rate && !count_ht && !count_vht && !count_he))
5336 		return -EINVAL;
5337 
5338 	if (rate &&
5339 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5340 				     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
5341 		return -EINVAL;
5342 	if (count_ht &&
5343 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5344 				     NL80211_EXT_FEATURE_BEACON_RATE_HT))
5345 		return -EINVAL;
5346 	if (count_vht &&
5347 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5348 				     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
5349 		return -EINVAL;
5350 	if (count_he &&
5351 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5352 				     NL80211_EXT_FEATURE_BEACON_RATE_HE))
5353 		return -EINVAL;
5354 
5355 	return 0;
5356 }
5357 
5358 static int nl80211_parse_mbssid_config(struct wiphy *wiphy,
5359 				       struct net_device *dev,
5360 				       struct nlattr *attrs,
5361 				       struct cfg80211_mbssid_config *config,
5362 				       u8 num_elems)
5363 {
5364 	struct nlattr *tb[NL80211_MBSSID_CONFIG_ATTR_MAX + 1];
5365 
5366 	if (!wiphy->mbssid_max_interfaces)
5367 		return -EOPNOTSUPP;
5368 
5369 	if (nla_parse_nested(tb, NL80211_MBSSID_CONFIG_ATTR_MAX, attrs, NULL,
5370 			     NULL) ||
5371 	    !tb[NL80211_MBSSID_CONFIG_ATTR_INDEX])
5372 		return -EINVAL;
5373 
5374 	config->ema = nla_get_flag(tb[NL80211_MBSSID_CONFIG_ATTR_EMA]);
5375 	if (config->ema) {
5376 		if (!wiphy->ema_max_profile_periodicity)
5377 			return -EOPNOTSUPP;
5378 
5379 		if (num_elems > wiphy->ema_max_profile_periodicity)
5380 			return -EINVAL;
5381 	}
5382 
5383 	config->index = nla_get_u8(tb[NL80211_MBSSID_CONFIG_ATTR_INDEX]);
5384 	if (config->index >= wiphy->mbssid_max_interfaces ||
5385 	    (!config->index && !num_elems))
5386 		return -EINVAL;
5387 
5388 	if (tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]) {
5389 		u32 tx_ifindex =
5390 			nla_get_u32(tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]);
5391 
5392 		if ((!config->index && tx_ifindex != dev->ifindex) ||
5393 		    (config->index && tx_ifindex == dev->ifindex))
5394 			return -EINVAL;
5395 
5396 		if (tx_ifindex != dev->ifindex) {
5397 			struct net_device *tx_netdev =
5398 				dev_get_by_index(wiphy_net(wiphy), tx_ifindex);
5399 
5400 			if (!tx_netdev || !tx_netdev->ieee80211_ptr ||
5401 			    tx_netdev->ieee80211_ptr->wiphy != wiphy ||
5402 			    tx_netdev->ieee80211_ptr->iftype !=
5403 							NL80211_IFTYPE_AP) {
5404 				dev_put(tx_netdev);
5405 				return -EINVAL;
5406 			}
5407 
5408 			config->tx_wdev = tx_netdev->ieee80211_ptr;
5409 		} else {
5410 			config->tx_wdev = dev->ieee80211_ptr;
5411 		}
5412 	} else if (!config->index) {
5413 		config->tx_wdev = dev->ieee80211_ptr;
5414 	} else {
5415 		return -EINVAL;
5416 	}
5417 
5418 	return 0;
5419 }
5420 
5421 static struct cfg80211_mbssid_elems *
5422 nl80211_parse_mbssid_elems(struct wiphy *wiphy, struct nlattr *attrs)
5423 {
5424 	struct nlattr *nl_elems;
5425 	struct cfg80211_mbssid_elems *elems;
5426 	int rem_elems;
5427 	u8 i = 0, num_elems = 0;
5428 
5429 	if (!wiphy->mbssid_max_interfaces)
5430 		return ERR_PTR(-EINVAL);
5431 
5432 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5433 		if (num_elems >= 255)
5434 			return ERR_PTR(-EINVAL);
5435 		num_elems++;
5436 	}
5437 
5438 	elems = kzalloc(struct_size(elems, elem, num_elems), GFP_KERNEL);
5439 	if (!elems)
5440 		return ERR_PTR(-ENOMEM);
5441 	elems->cnt = num_elems;
5442 
5443 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5444 		elems->elem[i].data = nla_data(nl_elems);
5445 		elems->elem[i].len = nla_len(nl_elems);
5446 		i++;
5447 	}
5448 	return elems;
5449 }
5450 
5451 static struct cfg80211_rnr_elems *
5452 nl80211_parse_rnr_elems(struct wiphy *wiphy, struct nlattr *attrs,
5453 			struct netlink_ext_ack *extack)
5454 {
5455 	struct nlattr *nl_elems;
5456 	struct cfg80211_rnr_elems *elems;
5457 	int rem_elems;
5458 	u8 i = 0, num_elems = 0;
5459 
5460 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5461 		int ret;
5462 
5463 		ret = validate_ie_attr(nl_elems, extack);
5464 		if (ret)
5465 			return ERR_PTR(ret);
5466 
5467 		num_elems++;
5468 	}
5469 
5470 	elems = kzalloc(struct_size(elems, elem, num_elems), GFP_KERNEL);
5471 	if (!elems)
5472 		return ERR_PTR(-ENOMEM);
5473 
5474 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5475 		elems->elem[i].data = nla_data(nl_elems);
5476 		elems->elem[i].len = nla_len(nl_elems);
5477 		i++;
5478 	}
5479 	elems->cnt = num_elems;
5480 	return elems;
5481 }
5482 
5483 static int nl80211_parse_he_bss_color(struct nlattr *attrs,
5484 				      struct cfg80211_he_bss_color *he_bss_color)
5485 {
5486 	struct nlattr *tb[NL80211_HE_BSS_COLOR_ATTR_MAX + 1];
5487 	int err;
5488 
5489 	err = nla_parse_nested(tb, NL80211_HE_BSS_COLOR_ATTR_MAX, attrs,
5490 			       he_bss_color_policy, NULL);
5491 	if (err)
5492 		return err;
5493 
5494 	if (!tb[NL80211_HE_BSS_COLOR_ATTR_COLOR])
5495 		return -EINVAL;
5496 
5497 	he_bss_color->color =
5498 		nla_get_u8(tb[NL80211_HE_BSS_COLOR_ATTR_COLOR]);
5499 	he_bss_color->enabled =
5500 		!nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_DISABLED]);
5501 	he_bss_color->partial =
5502 		nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_PARTIAL]);
5503 
5504 	return 0;
5505 }
5506 
5507 static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
5508 				struct nlattr *attrs[],
5509 				struct cfg80211_beacon_data *bcn,
5510 				struct netlink_ext_ack *extack)
5511 {
5512 	bool haveinfo = false;
5513 	int err;
5514 
5515 	memset(bcn, 0, sizeof(*bcn));
5516 
5517 	bcn->link_id = nl80211_link_id(attrs);
5518 
5519 	if (attrs[NL80211_ATTR_BEACON_HEAD]) {
5520 		bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
5521 		bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
5522 		if (!bcn->head_len)
5523 			return -EINVAL;
5524 		haveinfo = true;
5525 	}
5526 
5527 	if (attrs[NL80211_ATTR_BEACON_TAIL]) {
5528 		bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
5529 		bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
5530 		haveinfo = true;
5531 	}
5532 
5533 	if (!haveinfo)
5534 		return -EINVAL;
5535 
5536 	if (attrs[NL80211_ATTR_IE]) {
5537 		bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
5538 		bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
5539 	}
5540 
5541 	if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
5542 		bcn->proberesp_ies =
5543 			nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5544 		bcn->proberesp_ies_len =
5545 			nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5546 	}
5547 
5548 	if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
5549 		bcn->assocresp_ies =
5550 			nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5551 		bcn->assocresp_ies_len =
5552 			nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5553 	}
5554 
5555 	if (attrs[NL80211_ATTR_PROBE_RESP]) {
5556 		bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
5557 		bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
5558 	}
5559 
5560 	if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
5561 		struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
5562 
5563 		err = nla_parse_nested_deprecated(tb,
5564 						  NL80211_FTM_RESP_ATTR_MAX,
5565 						  attrs[NL80211_ATTR_FTM_RESPONDER],
5566 						  NULL, NULL);
5567 		if (err)
5568 			return err;
5569 
5570 		if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
5571 		    wiphy_ext_feature_isset(&rdev->wiphy,
5572 					    NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
5573 			bcn->ftm_responder = 1;
5574 		else
5575 			return -EOPNOTSUPP;
5576 
5577 		if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
5578 			bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
5579 			bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
5580 		}
5581 
5582 		if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
5583 			bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5584 			bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5585 		}
5586 	} else {
5587 		bcn->ftm_responder = -1;
5588 	}
5589 
5590 	if (attrs[NL80211_ATTR_HE_BSS_COLOR]) {
5591 		err = nl80211_parse_he_bss_color(attrs[NL80211_ATTR_HE_BSS_COLOR],
5592 						 &bcn->he_bss_color);
5593 		if (err)
5594 			return err;
5595 		bcn->he_bss_color_valid = true;
5596 	}
5597 
5598 	if (attrs[NL80211_ATTR_MBSSID_ELEMS]) {
5599 		struct cfg80211_mbssid_elems *mbssid =
5600 			nl80211_parse_mbssid_elems(&rdev->wiphy,
5601 						   attrs[NL80211_ATTR_MBSSID_ELEMS]);
5602 
5603 		if (IS_ERR(mbssid))
5604 			return PTR_ERR(mbssid);
5605 
5606 		bcn->mbssid_ies = mbssid;
5607 
5608 		if (bcn->mbssid_ies && attrs[NL80211_ATTR_EMA_RNR_ELEMS]) {
5609 			struct cfg80211_rnr_elems *rnr =
5610 				nl80211_parse_rnr_elems(&rdev->wiphy,
5611 							attrs[NL80211_ATTR_EMA_RNR_ELEMS],
5612 							extack);
5613 
5614 			if (IS_ERR(rnr))
5615 				return PTR_ERR(rnr);
5616 
5617 			if (rnr && rnr->cnt < bcn->mbssid_ies->cnt)
5618 				return -EINVAL;
5619 
5620 			bcn->rnr_ies = rnr;
5621 		}
5622 	}
5623 
5624 	return 0;
5625 }
5626 
5627 static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
5628 				    struct ieee80211_he_obss_pd *he_obss_pd)
5629 {
5630 	struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
5631 	int err;
5632 
5633 	err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
5634 			       he_obss_pd_policy, NULL);
5635 	if (err)
5636 		return err;
5637 
5638 	if (!tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL])
5639 		return -EINVAL;
5640 
5641 	he_obss_pd->sr_ctrl = nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL]);
5642 
5643 	if (tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET])
5644 		he_obss_pd->min_offset =
5645 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
5646 	if (tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
5647 		he_obss_pd->max_offset =
5648 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
5649 	if (tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET])
5650 		he_obss_pd->non_srg_max_offset =
5651 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET]);
5652 
5653 	if (he_obss_pd->min_offset > he_obss_pd->max_offset)
5654 		return -EINVAL;
5655 
5656 	if (tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP])
5657 		memcpy(he_obss_pd->bss_color_bitmap,
5658 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP]),
5659 		       sizeof(he_obss_pd->bss_color_bitmap));
5660 
5661 	if (tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP])
5662 		memcpy(he_obss_pd->partial_bssid_bitmap,
5663 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP]),
5664 		       sizeof(he_obss_pd->partial_bssid_bitmap));
5665 
5666 	he_obss_pd->enable = true;
5667 
5668 	return 0;
5669 }
5670 
5671 static int nl80211_parse_fils_discovery(struct cfg80211_registered_device *rdev,
5672 					struct nlattr *attrs,
5673 					struct cfg80211_ap_settings *params)
5674 {
5675 	struct nlattr *tb[NL80211_FILS_DISCOVERY_ATTR_MAX + 1];
5676 	int ret;
5677 	struct cfg80211_fils_discovery *fd = &params->fils_discovery;
5678 
5679 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5680 				     NL80211_EXT_FEATURE_FILS_DISCOVERY))
5681 		return -EINVAL;
5682 
5683 	ret = nla_parse_nested(tb, NL80211_FILS_DISCOVERY_ATTR_MAX, attrs,
5684 			       NULL, NULL);
5685 	if (ret)
5686 		return ret;
5687 
5688 	if (!tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] ||
5689 	    !tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] ||
5690 	    !tb[NL80211_FILS_DISCOVERY_ATTR_TMPL])
5691 		return -EINVAL;
5692 
5693 	fd->tmpl_len = nla_len(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5694 	fd->tmpl = nla_data(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5695 	fd->min_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN]);
5696 	fd->max_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX]);
5697 
5698 	return 0;
5699 }
5700 
5701 static int
5702 nl80211_parse_unsol_bcast_probe_resp(struct cfg80211_registered_device *rdev,
5703 				     struct nlattr *attrs,
5704 				     struct cfg80211_ap_settings *params)
5705 {
5706 	struct nlattr *tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1];
5707 	int ret;
5708 	struct cfg80211_unsol_bcast_probe_resp *presp =
5709 					&params->unsol_bcast_probe_resp;
5710 
5711 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5712 				     NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP))
5713 		return -EINVAL;
5714 
5715 	ret = nla_parse_nested(tb, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX,
5716 			       attrs, NULL, NULL);
5717 	if (ret)
5718 		return ret;
5719 
5720 	if (!tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] ||
5721 	    !tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL])
5722 		return -EINVAL;
5723 
5724 	presp->tmpl = nla_data(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5725 	presp->tmpl_len = nla_len(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5726 	presp->interval = nla_get_u32(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT]);
5727 	return 0;
5728 }
5729 
5730 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
5731 					    const struct element *rates)
5732 {
5733 	int i;
5734 
5735 	if (!rates)
5736 		return;
5737 
5738 	for (i = 0; i < rates->datalen; i++) {
5739 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
5740 			params->ht_required = true;
5741 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
5742 			params->vht_required = true;
5743 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HE_PHY)
5744 			params->he_required = true;
5745 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_SAE_H2E)
5746 			params->sae_h2e_required = true;
5747 	}
5748 }
5749 
5750 /*
5751  * Since the nl80211 API didn't include, from the beginning, attributes about
5752  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
5753  * benefit of drivers that rebuild IEs in the firmware.
5754  */
5755 static int nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
5756 {
5757 	const struct cfg80211_beacon_data *bcn = &params->beacon;
5758 	size_t ies_len = bcn->tail_len;
5759 	const u8 *ies = bcn->tail;
5760 	const struct element *rates;
5761 	const struct element *cap;
5762 
5763 	rates = cfg80211_find_elem(WLAN_EID_SUPP_RATES, ies, ies_len);
5764 	nl80211_check_ap_rate_selectors(params, rates);
5765 
5766 	rates = cfg80211_find_elem(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
5767 	nl80211_check_ap_rate_selectors(params, rates);
5768 
5769 	cap = cfg80211_find_elem(WLAN_EID_HT_CAPABILITY, ies, ies_len);
5770 	if (cap && cap->datalen >= sizeof(*params->ht_cap))
5771 		params->ht_cap = (void *)cap->data;
5772 	cap = cfg80211_find_elem(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
5773 	if (cap && cap->datalen >= sizeof(*params->vht_cap))
5774 		params->vht_cap = (void *)cap->data;
5775 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
5776 	if (cap && cap->datalen >= sizeof(*params->he_cap) + 1)
5777 		params->he_cap = (void *)(cap->data + 1);
5778 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ies, ies_len);
5779 	if (cap && cap->datalen >= sizeof(*params->he_oper) + 1)
5780 		params->he_oper = (void *)(cap->data + 1);
5781 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_CAPABILITY, ies, ies_len);
5782 	if (cap) {
5783 		if (!cap->datalen)
5784 			return -EINVAL;
5785 		params->eht_cap = (void *)(cap->data + 1);
5786 		if (!ieee80211_eht_capa_size_ok((const u8 *)params->he_cap,
5787 						(const u8 *)params->eht_cap,
5788 						cap->datalen - 1, true))
5789 			return -EINVAL;
5790 	}
5791 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_OPERATION, ies, ies_len);
5792 	if (cap) {
5793 		if (!cap->datalen)
5794 			return -EINVAL;
5795 		params->eht_oper = (void *)(cap->data + 1);
5796 		if (!ieee80211_eht_oper_size_ok((const u8 *)params->eht_oper,
5797 						cap->datalen - 1))
5798 			return -EINVAL;
5799 	}
5800 	return 0;
5801 }
5802 
5803 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
5804 				   struct cfg80211_ap_settings *params)
5805 {
5806 	struct wireless_dev *wdev;
5807 
5808 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
5809 		if (wdev->iftype != NL80211_IFTYPE_AP &&
5810 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)
5811 			continue;
5812 
5813 		if (!wdev->u.ap.preset_chandef.chan)
5814 			continue;
5815 
5816 		params->chandef = wdev->u.ap.preset_chandef;
5817 		return true;
5818 	}
5819 
5820 	return false;
5821 }
5822 
5823 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
5824 				    enum nl80211_auth_type auth_type,
5825 				    enum nl80211_commands cmd)
5826 {
5827 	if (auth_type > NL80211_AUTHTYPE_MAX)
5828 		return false;
5829 
5830 	switch (cmd) {
5831 	case NL80211_CMD_AUTHENTICATE:
5832 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5833 		    auth_type == NL80211_AUTHTYPE_SAE)
5834 			return false;
5835 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5836 					     NL80211_EXT_FEATURE_FILS_STA) &&
5837 		    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5838 		     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5839 		     auth_type == NL80211_AUTHTYPE_FILS_PK))
5840 			return false;
5841 		return true;
5842 	case NL80211_CMD_CONNECT:
5843 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5844 		    !wiphy_ext_feature_isset(&rdev->wiphy,
5845 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
5846 		    auth_type == NL80211_AUTHTYPE_SAE)
5847 			return false;
5848 
5849 		/* FILS with SK PFS or PK not supported yet */
5850 		if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5851 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5852 			return false;
5853 		if (!wiphy_ext_feature_isset(
5854 			    &rdev->wiphy,
5855 			    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
5856 		    auth_type == NL80211_AUTHTYPE_FILS_SK)
5857 			return false;
5858 		return true;
5859 	case NL80211_CMD_START_AP:
5860 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5861 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP) &&
5862 		    auth_type == NL80211_AUTHTYPE_SAE)
5863 			return false;
5864 		/* FILS not supported yet */
5865 		if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5866 		    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5867 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5868 			return false;
5869 		return true;
5870 	default:
5871 		return false;
5872 	}
5873 }
5874 
5875 static void nl80211_send_ap_started(struct wireless_dev *wdev,
5876 				    unsigned int link_id)
5877 {
5878 	struct wiphy *wiphy = wdev->wiphy;
5879 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
5880 	struct sk_buff *msg;
5881 	void *hdr;
5882 
5883 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5884 	if (!msg)
5885 		return;
5886 
5887 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_START_AP);
5888 	if (!hdr)
5889 		goto out;
5890 
5891 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
5892 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
5893 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
5894 			      NL80211_ATTR_PAD) ||
5895 	    (wdev->u.ap.ssid_len &&
5896 	     nla_put(msg, NL80211_ATTR_SSID, wdev->u.ap.ssid_len,
5897 		     wdev->u.ap.ssid)) ||
5898 	    (wdev->valid_links &&
5899 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)))
5900 		goto out;
5901 
5902 	genlmsg_end(msg, hdr);
5903 
5904 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
5905 				NL80211_MCGRP_MLME, GFP_KERNEL);
5906 	return;
5907 out:
5908 	nlmsg_free(msg);
5909 }
5910 
5911 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
5912 {
5913 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5914 	unsigned int link_id = nl80211_link_id(info->attrs);
5915 	struct net_device *dev = info->user_ptr[1];
5916 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5917 	struct cfg80211_ap_settings *params;
5918 	int err;
5919 
5920 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5921 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5922 		return -EOPNOTSUPP;
5923 
5924 	if (!rdev->ops->start_ap)
5925 		return -EOPNOTSUPP;
5926 
5927 	if (wdev->links[link_id].ap.beacon_interval)
5928 		return -EALREADY;
5929 
5930 	/* these are required for START_AP */
5931 	if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
5932 	    !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
5933 	    !info->attrs[NL80211_ATTR_BEACON_HEAD])
5934 		return -EINVAL;
5935 
5936 	params = kzalloc(sizeof(*params), GFP_KERNEL);
5937 	if (!params)
5938 		return -ENOMEM;
5939 
5940 	err = nl80211_parse_beacon(rdev, info->attrs, &params->beacon,
5941 				   info->extack);
5942 	if (err)
5943 		goto out;
5944 
5945 	params->beacon_interval =
5946 		nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5947 	params->dtim_period =
5948 		nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
5949 
5950 	err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
5951 					   params->beacon_interval);
5952 	if (err)
5953 		goto out;
5954 
5955 	/*
5956 	 * In theory, some of these attributes should be required here
5957 	 * but since they were not used when the command was originally
5958 	 * added, keep them optional for old user space programs to let
5959 	 * them continue to work with drivers that do not need the
5960 	 * additional information -- drivers must check!
5961 	 */
5962 	if (info->attrs[NL80211_ATTR_SSID]) {
5963 		params->ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5964 		params->ssid_len =
5965 			nla_len(info->attrs[NL80211_ATTR_SSID]);
5966 		if (params->ssid_len == 0) {
5967 			err = -EINVAL;
5968 			goto out;
5969 		}
5970 
5971 		if (wdev->u.ap.ssid_len &&
5972 		    (wdev->u.ap.ssid_len != params->ssid_len ||
5973 		     memcmp(wdev->u.ap.ssid, params->ssid, params->ssid_len))) {
5974 			/* require identical SSID for MLO */
5975 			err = -EINVAL;
5976 			goto out;
5977 		}
5978 	} else if (wdev->valid_links) {
5979 		/* require SSID for MLO */
5980 		err = -EINVAL;
5981 		goto out;
5982 	}
5983 
5984 	if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
5985 		params->hidden_ssid = nla_get_u32(
5986 			info->attrs[NL80211_ATTR_HIDDEN_SSID]);
5987 
5988 	params->privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
5989 
5990 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5991 		params->auth_type = nla_get_u32(
5992 			info->attrs[NL80211_ATTR_AUTH_TYPE]);
5993 		if (!nl80211_valid_auth_type(rdev, params->auth_type,
5994 					     NL80211_CMD_START_AP)) {
5995 			err = -EINVAL;
5996 			goto out;
5997 		}
5998 	} else
5999 		params->auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6000 
6001 	err = nl80211_crypto_settings(rdev, info, &params->crypto,
6002 				      NL80211_MAX_NR_CIPHER_SUITES);
6003 	if (err)
6004 		goto out;
6005 
6006 	if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
6007 		if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER)) {
6008 			err = -EOPNOTSUPP;
6009 			goto out;
6010 		}
6011 		params->inactivity_timeout = nla_get_u16(
6012 			info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
6013 	}
6014 
6015 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
6016 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
6017 			err = -EINVAL;
6018 			goto out;
6019 		}
6020 		params->p2p_ctwindow =
6021 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
6022 		if (params->p2p_ctwindow != 0 &&
6023 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN)) {
6024 			err = -EINVAL;
6025 			goto out;
6026 		}
6027 	}
6028 
6029 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
6030 		u8 tmp;
6031 
6032 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
6033 			err = -EINVAL;
6034 			goto out;
6035 		}
6036 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
6037 		params->p2p_opp_ps = tmp;
6038 		if (params->p2p_opp_ps != 0 &&
6039 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS)) {
6040 			err = -EINVAL;
6041 			goto out;
6042 		}
6043 	}
6044 
6045 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6046 		err = nl80211_parse_chandef(rdev, info, &params->chandef);
6047 		if (err)
6048 			goto out;
6049 	} else if (wdev->valid_links) {
6050 		/* with MLD need to specify the channel configuration */
6051 		err = -EINVAL;
6052 		goto out;
6053 	} else if (wdev->u.ap.preset_chandef.chan) {
6054 		params->chandef = wdev->u.ap.preset_chandef;
6055 	} else if (!nl80211_get_ap_channel(rdev, params)) {
6056 		err = -EINVAL;
6057 		goto out;
6058 	}
6059 
6060 	if (info->attrs[NL80211_ATTR_PUNCT_BITMAP]) {
6061 		err = nl80211_parse_punct_bitmap(rdev, info,
6062 						 &params->chandef,
6063 						 &params->punct_bitmap);
6064 		if (err)
6065 			goto out;
6066 	}
6067 
6068 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params->chandef,
6069 					   wdev->iftype)) {
6070 		err = -EINVAL;
6071 		goto out;
6072 	}
6073 
6074 	wdev_lock(wdev);
6075 
6076 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
6077 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
6078 						    NL80211_ATTR_TX_RATES,
6079 						    &params->beacon_rate,
6080 						    dev, false, link_id);
6081 		if (err)
6082 			goto out_unlock;
6083 
6084 		err = validate_beacon_tx_rate(rdev, params->chandef.chan->band,
6085 					      &params->beacon_rate);
6086 		if (err)
6087 			goto out_unlock;
6088 	}
6089 
6090 	if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
6091 		params->smps_mode =
6092 			nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
6093 		switch (params->smps_mode) {
6094 		case NL80211_SMPS_OFF:
6095 			break;
6096 		case NL80211_SMPS_STATIC:
6097 			if (!(rdev->wiphy.features &
6098 			      NL80211_FEATURE_STATIC_SMPS)) {
6099 				err = -EINVAL;
6100 				goto out_unlock;
6101 			}
6102 			break;
6103 		case NL80211_SMPS_DYNAMIC:
6104 			if (!(rdev->wiphy.features &
6105 			      NL80211_FEATURE_DYNAMIC_SMPS)) {
6106 				err = -EINVAL;
6107 				goto out_unlock;
6108 			}
6109 			break;
6110 		default:
6111 			err = -EINVAL;
6112 			goto out_unlock;
6113 		}
6114 	} else {
6115 		params->smps_mode = NL80211_SMPS_OFF;
6116 	}
6117 
6118 	params->pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
6119 	if (params->pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
6120 		err = -EOPNOTSUPP;
6121 		goto out_unlock;
6122 	}
6123 
6124 	if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
6125 		params->acl = parse_acl_data(&rdev->wiphy, info);
6126 		if (IS_ERR(params->acl)) {
6127 			err = PTR_ERR(params->acl);
6128 			params->acl = NULL;
6129 			goto out_unlock;
6130 		}
6131 	}
6132 
6133 	params->twt_responder =
6134 		    nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
6135 
6136 	if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
6137 		err = nl80211_parse_he_obss_pd(
6138 					info->attrs[NL80211_ATTR_HE_OBSS_PD],
6139 					&params->he_obss_pd);
6140 		if (err)
6141 			goto out_unlock;
6142 	}
6143 
6144 	if (info->attrs[NL80211_ATTR_FILS_DISCOVERY]) {
6145 		err = nl80211_parse_fils_discovery(rdev,
6146 						   info->attrs[NL80211_ATTR_FILS_DISCOVERY],
6147 						   params);
6148 		if (err)
6149 			goto out_unlock;
6150 	}
6151 
6152 	if (info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP]) {
6153 		err = nl80211_parse_unsol_bcast_probe_resp(
6154 			rdev, info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP],
6155 			params);
6156 		if (err)
6157 			goto out_unlock;
6158 	}
6159 
6160 	if (info->attrs[NL80211_ATTR_MBSSID_CONFIG]) {
6161 		err = nl80211_parse_mbssid_config(&rdev->wiphy, dev,
6162 						  info->attrs[NL80211_ATTR_MBSSID_CONFIG],
6163 						  &params->mbssid_config,
6164 						  params->beacon.mbssid_ies ?
6165 							params->beacon.mbssid_ies->cnt :
6166 							0);
6167 		if (err)
6168 			goto out_unlock;
6169 	}
6170 
6171 	if (!params->mbssid_config.ema && params->beacon.rnr_ies) {
6172 		err = -EINVAL;
6173 		goto out_unlock;
6174 	}
6175 
6176 	err = nl80211_calculate_ap_params(params);
6177 	if (err)
6178 		goto out_unlock;
6179 
6180 	if (info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS])
6181 		params->flags = nla_get_u32(
6182 			info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS]);
6183 	else if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
6184 		params->flags |= NL80211_AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
6185 
6186 	if (wdev->conn_owner_nlportid &&
6187 	    info->attrs[NL80211_ATTR_SOCKET_OWNER] &&
6188 	    wdev->conn_owner_nlportid != info->snd_portid) {
6189 		err = -EINVAL;
6190 		goto out_unlock;
6191 	}
6192 
6193 	/* FIXME: validate MLO/link-id against driver capabilities */
6194 
6195 	err = rdev_start_ap(rdev, dev, params);
6196 	if (!err) {
6197 		wdev->links[link_id].ap.beacon_interval = params->beacon_interval;
6198 		wdev->links[link_id].ap.chandef = params->chandef;
6199 		wdev->u.ap.ssid_len = params->ssid_len;
6200 		memcpy(wdev->u.ap.ssid, params->ssid,
6201 		       params->ssid_len);
6202 
6203 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
6204 			wdev->conn_owner_nlportid = info->snd_portid;
6205 
6206 		nl80211_send_ap_started(wdev, link_id);
6207 	}
6208 out_unlock:
6209 	wdev_unlock(wdev);
6210 out:
6211 	kfree(params->acl);
6212 	kfree(params->beacon.mbssid_ies);
6213 	if (params->mbssid_config.tx_wdev &&
6214 	    params->mbssid_config.tx_wdev->netdev &&
6215 	    params->mbssid_config.tx_wdev->netdev != dev)
6216 		dev_put(params->mbssid_config.tx_wdev->netdev);
6217 	kfree(params->beacon.rnr_ies);
6218 	kfree(params);
6219 
6220 	return err;
6221 }
6222 
6223 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
6224 {
6225 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6226 	unsigned int link_id = nl80211_link_id(info->attrs);
6227 	struct net_device *dev = info->user_ptr[1];
6228 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6229 	struct cfg80211_beacon_data params;
6230 	int err;
6231 
6232 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6233 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6234 		return -EOPNOTSUPP;
6235 
6236 	if (!rdev->ops->change_beacon)
6237 		return -EOPNOTSUPP;
6238 
6239 	if (!wdev->links[link_id].ap.beacon_interval)
6240 		return -EINVAL;
6241 
6242 	err = nl80211_parse_beacon(rdev, info->attrs, &params, info->extack);
6243 	if (err)
6244 		goto out;
6245 
6246 	wdev_lock(wdev);
6247 	err = rdev_change_beacon(rdev, dev, &params);
6248 	wdev_unlock(wdev);
6249 
6250 out:
6251 	kfree(params.mbssid_ies);
6252 	kfree(params.rnr_ies);
6253 	return err;
6254 }
6255 
6256 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
6257 {
6258 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6259 	unsigned int link_id = nl80211_link_id(info->attrs);
6260 	struct net_device *dev = info->user_ptr[1];
6261 
6262 	return cfg80211_stop_ap(rdev, dev, link_id, false);
6263 }
6264 
6265 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
6266 	[NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
6267 	[NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
6268 	[NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
6269 	[NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
6270 	[NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
6271 	[NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
6272 };
6273 
6274 static int parse_station_flags(struct genl_info *info,
6275 			       enum nl80211_iftype iftype,
6276 			       struct station_parameters *params)
6277 {
6278 	struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
6279 	struct nlattr *nla;
6280 	int flag;
6281 
6282 	/*
6283 	 * Try parsing the new attribute first so userspace
6284 	 * can specify both for older kernels.
6285 	 */
6286 	nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
6287 	if (nla) {
6288 		struct nl80211_sta_flag_update *sta_flags;
6289 
6290 		sta_flags = nla_data(nla);
6291 		params->sta_flags_mask = sta_flags->mask;
6292 		params->sta_flags_set = sta_flags->set;
6293 		params->sta_flags_set &= params->sta_flags_mask;
6294 		if ((params->sta_flags_mask |
6295 		     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
6296 			return -EINVAL;
6297 		return 0;
6298 	}
6299 
6300 	/* if present, parse the old attribute */
6301 
6302 	nla = info->attrs[NL80211_ATTR_STA_FLAGS];
6303 	if (!nla)
6304 		return 0;
6305 
6306 	if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
6307 		return -EINVAL;
6308 
6309 	/*
6310 	 * Only allow certain flags for interface types so that
6311 	 * other attributes are silently ignored. Remember that
6312 	 * this is backward compatibility code with old userspace
6313 	 * and shouldn't be hit in other cases anyway.
6314 	 */
6315 	switch (iftype) {
6316 	case NL80211_IFTYPE_AP:
6317 	case NL80211_IFTYPE_AP_VLAN:
6318 	case NL80211_IFTYPE_P2P_GO:
6319 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6320 					 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6321 					 BIT(NL80211_STA_FLAG_WME) |
6322 					 BIT(NL80211_STA_FLAG_MFP);
6323 		break;
6324 	case NL80211_IFTYPE_P2P_CLIENT:
6325 	case NL80211_IFTYPE_STATION:
6326 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6327 					 BIT(NL80211_STA_FLAG_TDLS_PEER);
6328 		break;
6329 	case NL80211_IFTYPE_MESH_POINT:
6330 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6331 					 BIT(NL80211_STA_FLAG_MFP) |
6332 					 BIT(NL80211_STA_FLAG_AUTHORIZED);
6333 		break;
6334 	default:
6335 		return -EINVAL;
6336 	}
6337 
6338 	for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
6339 		if (flags[flag]) {
6340 			params->sta_flags_set |= (1<<flag);
6341 
6342 			/* no longer support new API additions in old API */
6343 			if (flag > NL80211_STA_FLAG_MAX_OLD_API)
6344 				return -EINVAL;
6345 		}
6346 	}
6347 
6348 	return 0;
6349 }
6350 
6351 bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
6352 {
6353 	struct nlattr *rate;
6354 	u32 bitrate;
6355 	u16 bitrate_compat;
6356 	enum nl80211_rate_info rate_flg;
6357 
6358 	rate = nla_nest_start_noflag(msg, attr);
6359 	if (!rate)
6360 		return false;
6361 
6362 	/* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
6363 	bitrate = cfg80211_calculate_bitrate(info);
6364 	/* report 16-bit bitrate only if we can */
6365 	bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
6366 	if (bitrate > 0 &&
6367 	    nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
6368 		return false;
6369 	if (bitrate_compat > 0 &&
6370 	    nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
6371 		return false;
6372 
6373 	switch (info->bw) {
6374 	case RATE_INFO_BW_1:
6375 		rate_flg = NL80211_RATE_INFO_1_MHZ_WIDTH;
6376 		break;
6377 	case RATE_INFO_BW_2:
6378 		rate_flg = NL80211_RATE_INFO_2_MHZ_WIDTH;
6379 		break;
6380 	case RATE_INFO_BW_4:
6381 		rate_flg = NL80211_RATE_INFO_4_MHZ_WIDTH;
6382 		break;
6383 	case RATE_INFO_BW_5:
6384 		rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
6385 		break;
6386 	case RATE_INFO_BW_8:
6387 		rate_flg = NL80211_RATE_INFO_8_MHZ_WIDTH;
6388 		break;
6389 	case RATE_INFO_BW_10:
6390 		rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
6391 		break;
6392 	case RATE_INFO_BW_16:
6393 		rate_flg = NL80211_RATE_INFO_16_MHZ_WIDTH;
6394 		break;
6395 	default:
6396 		WARN_ON(1);
6397 		fallthrough;
6398 	case RATE_INFO_BW_20:
6399 		rate_flg = 0;
6400 		break;
6401 	case RATE_INFO_BW_40:
6402 		rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
6403 		break;
6404 	case RATE_INFO_BW_80:
6405 		rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
6406 		break;
6407 	case RATE_INFO_BW_160:
6408 		rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
6409 		break;
6410 	case RATE_INFO_BW_HE_RU:
6411 		rate_flg = 0;
6412 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
6413 		break;
6414 	case RATE_INFO_BW_320:
6415 		rate_flg = NL80211_RATE_INFO_320_MHZ_WIDTH;
6416 		break;
6417 	case RATE_INFO_BW_EHT_RU:
6418 		rate_flg = 0;
6419 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_EHT_MCS));
6420 		break;
6421 	}
6422 
6423 	if (rate_flg && nla_put_flag(msg, rate_flg))
6424 		return false;
6425 
6426 	if (info->flags & RATE_INFO_FLAGS_MCS) {
6427 		if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
6428 			return false;
6429 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6430 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6431 			return false;
6432 	} else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
6433 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
6434 			return false;
6435 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
6436 			return false;
6437 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6438 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6439 			return false;
6440 	} else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
6441 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
6442 			return false;
6443 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
6444 			return false;
6445 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
6446 			return false;
6447 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
6448 			return false;
6449 		if (info->bw == RATE_INFO_BW_HE_RU &&
6450 		    nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
6451 			       info->he_ru_alloc))
6452 			return false;
6453 	} else if (info->flags & RATE_INFO_FLAGS_S1G_MCS) {
6454 		if (nla_put_u8(msg, NL80211_RATE_INFO_S1G_MCS, info->mcs))
6455 			return false;
6456 		if (nla_put_u8(msg, NL80211_RATE_INFO_S1G_NSS, info->nss))
6457 			return false;
6458 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6459 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6460 			return false;
6461 	} else if (info->flags & RATE_INFO_FLAGS_EHT_MCS) {
6462 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_MCS, info->mcs))
6463 			return false;
6464 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_NSS, info->nss))
6465 			return false;
6466 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_GI, info->eht_gi))
6467 			return false;
6468 		if (info->bw == RATE_INFO_BW_EHT_RU &&
6469 		    nla_put_u8(msg, NL80211_RATE_INFO_EHT_RU_ALLOC,
6470 			       info->eht_ru_alloc))
6471 			return false;
6472 	}
6473 
6474 	nla_nest_end(msg, rate);
6475 	return true;
6476 }
6477 
6478 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
6479 			       int id)
6480 {
6481 	void *attr;
6482 	int i = 0;
6483 
6484 	if (!mask)
6485 		return true;
6486 
6487 	attr = nla_nest_start_noflag(msg, id);
6488 	if (!attr)
6489 		return false;
6490 
6491 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
6492 		if (!(mask & BIT(i)))
6493 			continue;
6494 
6495 		if (nla_put_u8(msg, i, signal[i]))
6496 			return false;
6497 	}
6498 
6499 	nla_nest_end(msg, attr);
6500 
6501 	return true;
6502 }
6503 
6504 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
6505 				u32 seq, int flags,
6506 				struct cfg80211_registered_device *rdev,
6507 				struct net_device *dev,
6508 				const u8 *mac_addr, struct station_info *sinfo)
6509 {
6510 	void *hdr;
6511 	struct nlattr *sinfoattr, *bss_param;
6512 
6513 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
6514 	if (!hdr) {
6515 		cfg80211_sinfo_release_content(sinfo);
6516 		return -1;
6517 	}
6518 
6519 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6520 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
6521 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
6522 		goto nla_put_failure;
6523 
6524 	sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
6525 	if (!sinfoattr)
6526 		goto nla_put_failure;
6527 
6528 #define PUT_SINFO(attr, memb, type) do {				\
6529 	BUILD_BUG_ON(sizeof(type) == sizeof(u64));			\
6530 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
6531 	    nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,		\
6532 			     sinfo->memb))				\
6533 		goto nla_put_failure;					\
6534 	} while (0)
6535 #define PUT_SINFO_U64(attr, memb) do {					\
6536 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
6537 	    nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,		\
6538 			      sinfo->memb, NL80211_STA_INFO_PAD))	\
6539 		goto nla_put_failure;					\
6540 	} while (0)
6541 
6542 	PUT_SINFO(CONNECTED_TIME, connected_time, u32);
6543 	PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
6544 	PUT_SINFO_U64(ASSOC_AT_BOOTTIME, assoc_at);
6545 
6546 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
6547 			     BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
6548 	    nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
6549 			(u32)sinfo->rx_bytes))
6550 		goto nla_put_failure;
6551 
6552 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
6553 			     BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
6554 	    nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
6555 			(u32)sinfo->tx_bytes))
6556 		goto nla_put_failure;
6557 
6558 	PUT_SINFO_U64(RX_BYTES64, rx_bytes);
6559 	PUT_SINFO_U64(TX_BYTES64, tx_bytes);
6560 	PUT_SINFO(LLID, llid, u16);
6561 	PUT_SINFO(PLID, plid, u16);
6562 	PUT_SINFO(PLINK_STATE, plink_state, u8);
6563 	PUT_SINFO_U64(RX_DURATION, rx_duration);
6564 	PUT_SINFO_U64(TX_DURATION, tx_duration);
6565 
6566 	if (wiphy_ext_feature_isset(&rdev->wiphy,
6567 				    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
6568 		PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
6569 
6570 	switch (rdev->wiphy.signal_type) {
6571 	case CFG80211_SIGNAL_TYPE_MBM:
6572 		PUT_SINFO(SIGNAL, signal, u8);
6573 		PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
6574 		break;
6575 	default:
6576 		break;
6577 	}
6578 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
6579 		if (!nl80211_put_signal(msg, sinfo->chains,
6580 					sinfo->chain_signal,
6581 					NL80211_STA_INFO_CHAIN_SIGNAL))
6582 			goto nla_put_failure;
6583 	}
6584 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
6585 		if (!nl80211_put_signal(msg, sinfo->chains,
6586 					sinfo->chain_signal_avg,
6587 					NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
6588 			goto nla_put_failure;
6589 	}
6590 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
6591 		if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
6592 					  NL80211_STA_INFO_TX_BITRATE))
6593 			goto nla_put_failure;
6594 	}
6595 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
6596 		if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
6597 					  NL80211_STA_INFO_RX_BITRATE))
6598 			goto nla_put_failure;
6599 	}
6600 
6601 	PUT_SINFO(RX_PACKETS, rx_packets, u32);
6602 	PUT_SINFO(TX_PACKETS, tx_packets, u32);
6603 	PUT_SINFO(TX_RETRIES, tx_retries, u32);
6604 	PUT_SINFO(TX_FAILED, tx_failed, u32);
6605 	PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
6606 	PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
6607 	PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
6608 	PUT_SINFO(LOCAL_PM, local_pm, u32);
6609 	PUT_SINFO(PEER_PM, peer_pm, u32);
6610 	PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
6611 	PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
6612 	PUT_SINFO(CONNECTED_TO_AS, connected_to_as, u8);
6613 
6614 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
6615 		bss_param = nla_nest_start_noflag(msg,
6616 						  NL80211_STA_INFO_BSS_PARAM);
6617 		if (!bss_param)
6618 			goto nla_put_failure;
6619 
6620 		if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
6621 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
6622 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
6623 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
6624 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
6625 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
6626 		    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
6627 			       sinfo->bss_param.dtim_period) ||
6628 		    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
6629 				sinfo->bss_param.beacon_interval))
6630 			goto nla_put_failure;
6631 
6632 		nla_nest_end(msg, bss_param);
6633 	}
6634 	if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
6635 	    nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
6636 		    sizeof(struct nl80211_sta_flag_update),
6637 		    &sinfo->sta_flags))
6638 		goto nla_put_failure;
6639 
6640 	PUT_SINFO_U64(T_OFFSET, t_offset);
6641 	PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
6642 	PUT_SINFO_U64(BEACON_RX, rx_beacon);
6643 	PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
6644 	PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
6645 	PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
6646 	if (wiphy_ext_feature_isset(&rdev->wiphy,
6647 				    NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
6648 		PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
6649 		PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
6650 	}
6651 
6652 #undef PUT_SINFO
6653 #undef PUT_SINFO_U64
6654 
6655 	if (sinfo->pertid) {
6656 		struct nlattr *tidsattr;
6657 		int tid;
6658 
6659 		tidsattr = nla_nest_start_noflag(msg,
6660 						 NL80211_STA_INFO_TID_STATS);
6661 		if (!tidsattr)
6662 			goto nla_put_failure;
6663 
6664 		for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
6665 			struct cfg80211_tid_stats *tidstats;
6666 			struct nlattr *tidattr;
6667 
6668 			tidstats = &sinfo->pertid[tid];
6669 
6670 			if (!tidstats->filled)
6671 				continue;
6672 
6673 			tidattr = nla_nest_start_noflag(msg, tid + 1);
6674 			if (!tidattr)
6675 				goto nla_put_failure;
6676 
6677 #define PUT_TIDVAL_U64(attr, memb) do {					\
6678 	if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&	\
6679 	    nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,		\
6680 			      tidstats->memb, NL80211_TID_STATS_PAD))	\
6681 		goto nla_put_failure;					\
6682 	} while (0)
6683 
6684 			PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
6685 			PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
6686 			PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
6687 			PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
6688 
6689 #undef PUT_TIDVAL_U64
6690 			if ((tidstats->filled &
6691 			     BIT(NL80211_TID_STATS_TXQ_STATS)) &&
6692 			    !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
6693 						   NL80211_TID_STATS_TXQ_STATS))
6694 				goto nla_put_failure;
6695 
6696 			nla_nest_end(msg, tidattr);
6697 		}
6698 
6699 		nla_nest_end(msg, tidsattr);
6700 	}
6701 
6702 	nla_nest_end(msg, sinfoattr);
6703 
6704 	if (sinfo->assoc_req_ies_len &&
6705 	    nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
6706 		    sinfo->assoc_req_ies))
6707 		goto nla_put_failure;
6708 
6709 	if (sinfo->assoc_resp_ies_len &&
6710 	    nla_put(msg, NL80211_ATTR_RESP_IE, sinfo->assoc_resp_ies_len,
6711 		    sinfo->assoc_resp_ies))
6712 		goto nla_put_failure;
6713 
6714 	if (sinfo->mlo_params_valid) {
6715 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
6716 			       sinfo->assoc_link_id))
6717 			goto nla_put_failure;
6718 
6719 		if (!is_zero_ether_addr(sinfo->mld_addr) &&
6720 		    nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
6721 			    sinfo->mld_addr))
6722 			goto nla_put_failure;
6723 	}
6724 
6725 	cfg80211_sinfo_release_content(sinfo);
6726 	genlmsg_end(msg, hdr);
6727 	return 0;
6728 
6729  nla_put_failure:
6730 	cfg80211_sinfo_release_content(sinfo);
6731 	genlmsg_cancel(msg, hdr);
6732 	return -EMSGSIZE;
6733 }
6734 
6735 static int nl80211_dump_station(struct sk_buff *skb,
6736 				struct netlink_callback *cb)
6737 {
6738 	struct station_info sinfo;
6739 	struct cfg80211_registered_device *rdev;
6740 	struct wireless_dev *wdev;
6741 	u8 mac_addr[ETH_ALEN];
6742 	int sta_idx = cb->args[2];
6743 	int err;
6744 
6745 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
6746 	if (err)
6747 		return err;
6748 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
6749 	__acquire(&rdev->wiphy.mtx);
6750 
6751 	if (!wdev->netdev) {
6752 		err = -EINVAL;
6753 		goto out_err;
6754 	}
6755 
6756 	if (!rdev->ops->dump_station) {
6757 		err = -EOPNOTSUPP;
6758 		goto out_err;
6759 	}
6760 
6761 	while (1) {
6762 		memset(&sinfo, 0, sizeof(sinfo));
6763 		err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
6764 					mac_addr, &sinfo);
6765 		if (err == -ENOENT)
6766 			break;
6767 		if (err)
6768 			goto out_err;
6769 
6770 		if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
6771 				NETLINK_CB(cb->skb).portid,
6772 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
6773 				rdev, wdev->netdev, mac_addr,
6774 				&sinfo) < 0)
6775 			goto out;
6776 
6777 		sta_idx++;
6778 	}
6779 
6780  out:
6781 	cb->args[2] = sta_idx;
6782 	err = skb->len;
6783  out_err:
6784 	wiphy_unlock(&rdev->wiphy);
6785 
6786 	return err;
6787 }
6788 
6789 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
6790 {
6791 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6792 	struct net_device *dev = info->user_ptr[1];
6793 	struct station_info sinfo;
6794 	struct sk_buff *msg;
6795 	u8 *mac_addr = NULL;
6796 	int err;
6797 
6798 	memset(&sinfo, 0, sizeof(sinfo));
6799 
6800 	if (!info->attrs[NL80211_ATTR_MAC])
6801 		return -EINVAL;
6802 
6803 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6804 
6805 	if (!rdev->ops->get_station)
6806 		return -EOPNOTSUPP;
6807 
6808 	err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
6809 	if (err)
6810 		return err;
6811 
6812 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6813 	if (!msg) {
6814 		cfg80211_sinfo_release_content(&sinfo);
6815 		return -ENOMEM;
6816 	}
6817 
6818 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
6819 				 info->snd_portid, info->snd_seq, 0,
6820 				 rdev, dev, mac_addr, &sinfo) < 0) {
6821 		nlmsg_free(msg);
6822 		return -ENOBUFS;
6823 	}
6824 
6825 	return genlmsg_reply(msg, info);
6826 }
6827 
6828 int cfg80211_check_station_change(struct wiphy *wiphy,
6829 				  struct station_parameters *params,
6830 				  enum cfg80211_station_type statype)
6831 {
6832 	if (params->listen_interval != -1 &&
6833 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6834 		return -EINVAL;
6835 
6836 	if (params->support_p2p_ps != -1 &&
6837 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6838 		return -EINVAL;
6839 
6840 	if (params->aid &&
6841 	    !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
6842 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6843 		return -EINVAL;
6844 
6845 	/* When you run into this, adjust the code below for the new flag */
6846 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
6847 
6848 	switch (statype) {
6849 	case CFG80211_STA_MESH_PEER_KERNEL:
6850 	case CFG80211_STA_MESH_PEER_USER:
6851 		/*
6852 		 * No ignoring the TDLS flag here -- the userspace mesh
6853 		 * code doesn't have the bug of including TDLS in the
6854 		 * mask everywhere.
6855 		 */
6856 		if (params->sta_flags_mask &
6857 				~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6858 				  BIT(NL80211_STA_FLAG_MFP) |
6859 				  BIT(NL80211_STA_FLAG_AUTHORIZED)))
6860 			return -EINVAL;
6861 		break;
6862 	case CFG80211_STA_TDLS_PEER_SETUP:
6863 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6864 		if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6865 			return -EINVAL;
6866 		/* ignore since it can't change */
6867 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6868 		break;
6869 	default:
6870 		/* disallow mesh-specific things */
6871 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
6872 			return -EINVAL;
6873 		if (params->local_pm)
6874 			return -EINVAL;
6875 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6876 			return -EINVAL;
6877 	}
6878 
6879 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6880 	    statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
6881 		/* TDLS can't be set, ... */
6882 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
6883 			return -EINVAL;
6884 		/*
6885 		 * ... but don't bother the driver with it. This works around
6886 		 * a hostapd/wpa_supplicant issue -- it always includes the
6887 		 * TLDS_PEER flag in the mask even for AP mode.
6888 		 */
6889 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6890 	}
6891 
6892 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6893 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6894 		/* reject other things that can't change */
6895 		if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
6896 			return -EINVAL;
6897 		if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
6898 			return -EINVAL;
6899 		if (params->link_sta_params.supported_rates)
6900 			return -EINVAL;
6901 		if (params->ext_capab || params->link_sta_params.ht_capa ||
6902 		    params->link_sta_params.vht_capa ||
6903 		    params->link_sta_params.he_capa ||
6904 		    params->link_sta_params.eht_capa)
6905 			return -EINVAL;
6906 	}
6907 
6908 	if (statype != CFG80211_STA_AP_CLIENT &&
6909 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6910 		if (params->vlan)
6911 			return -EINVAL;
6912 	}
6913 
6914 	switch (statype) {
6915 	case CFG80211_STA_AP_MLME_CLIENT:
6916 		/* Use this only for authorizing/unauthorizing a station */
6917 		if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
6918 			return -EOPNOTSUPP;
6919 		break;
6920 	case CFG80211_STA_AP_CLIENT:
6921 	case CFG80211_STA_AP_CLIENT_UNASSOC:
6922 		/* accept only the listed bits */
6923 		if (params->sta_flags_mask &
6924 				~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6925 				  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6926 				  BIT(NL80211_STA_FLAG_ASSOCIATED) |
6927 				  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6928 				  BIT(NL80211_STA_FLAG_WME) |
6929 				  BIT(NL80211_STA_FLAG_MFP)))
6930 			return -EINVAL;
6931 
6932 		/* but authenticated/associated only if driver handles it */
6933 		if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
6934 		    params->sta_flags_mask &
6935 				(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6936 				 BIT(NL80211_STA_FLAG_ASSOCIATED)))
6937 			return -EINVAL;
6938 		break;
6939 	case CFG80211_STA_IBSS:
6940 	case CFG80211_STA_AP_STA:
6941 		/* reject any changes other than AUTHORIZED */
6942 		if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
6943 			return -EINVAL;
6944 		break;
6945 	case CFG80211_STA_TDLS_PEER_SETUP:
6946 		/* reject any changes other than AUTHORIZED or WME */
6947 		if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6948 					       BIT(NL80211_STA_FLAG_WME)))
6949 			return -EINVAL;
6950 		/* force (at least) rates when authorizing */
6951 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
6952 		    !params->link_sta_params.supported_rates)
6953 			return -EINVAL;
6954 		break;
6955 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6956 		/* reject any changes */
6957 		return -EINVAL;
6958 	case CFG80211_STA_MESH_PEER_KERNEL:
6959 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6960 			return -EINVAL;
6961 		break;
6962 	case CFG80211_STA_MESH_PEER_USER:
6963 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
6964 		    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
6965 			return -EINVAL;
6966 		break;
6967 	}
6968 
6969 	/*
6970 	 * Older kernel versions ignored this attribute entirely, so don't
6971 	 * reject attempts to update it but mark it as unused instead so the
6972 	 * driver won't look at the data.
6973 	 */
6974 	if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
6975 	    statype != CFG80211_STA_TDLS_PEER_SETUP)
6976 		params->link_sta_params.opmode_notif_used = false;
6977 
6978 	return 0;
6979 }
6980 EXPORT_SYMBOL(cfg80211_check_station_change);
6981 
6982 /*
6983  * Get vlan interface making sure it is running and on the right wiphy.
6984  */
6985 static struct net_device *get_vlan(struct genl_info *info,
6986 				   struct cfg80211_registered_device *rdev)
6987 {
6988 	struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
6989 	struct net_device *v;
6990 	int ret;
6991 
6992 	if (!vlanattr)
6993 		return NULL;
6994 
6995 	v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
6996 	if (!v)
6997 		return ERR_PTR(-ENODEV);
6998 
6999 	if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
7000 		ret = -EINVAL;
7001 		goto error;
7002 	}
7003 
7004 	if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
7005 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
7006 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
7007 		ret = -EINVAL;
7008 		goto error;
7009 	}
7010 
7011 	if (!netif_running(v)) {
7012 		ret = -ENETDOWN;
7013 		goto error;
7014 	}
7015 
7016 	return v;
7017  error:
7018 	dev_put(v);
7019 	return ERR_PTR(ret);
7020 }
7021 
7022 static int nl80211_parse_sta_wme(struct genl_info *info,
7023 				 struct station_parameters *params)
7024 {
7025 	struct nlattr *tb[NL80211_STA_WME_MAX + 1];
7026 	struct nlattr *nla;
7027 	int err;
7028 
7029 	/* parse WME attributes if present */
7030 	if (!info->attrs[NL80211_ATTR_STA_WME])
7031 		return 0;
7032 
7033 	nla = info->attrs[NL80211_ATTR_STA_WME];
7034 	err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
7035 					  nl80211_sta_wme_policy,
7036 					  info->extack);
7037 	if (err)
7038 		return err;
7039 
7040 	if (tb[NL80211_STA_WME_UAPSD_QUEUES])
7041 		params->uapsd_queues = nla_get_u8(
7042 			tb[NL80211_STA_WME_UAPSD_QUEUES]);
7043 	if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
7044 		return -EINVAL;
7045 
7046 	if (tb[NL80211_STA_WME_MAX_SP])
7047 		params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
7048 
7049 	if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
7050 		return -EINVAL;
7051 
7052 	params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
7053 
7054 	return 0;
7055 }
7056 
7057 static int nl80211_parse_sta_channel_info(struct genl_info *info,
7058 				      struct station_parameters *params)
7059 {
7060 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
7061 		params->supported_channels =
7062 		     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
7063 		params->supported_channels_len =
7064 		     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
7065 		/*
7066 		 * Need to include at least one (first channel, number of
7067 		 * channels) tuple for each subband (checked in policy),
7068 		 * and must have proper tuples for the rest of the data as well.
7069 		 */
7070 		if (params->supported_channels_len % 2)
7071 			return -EINVAL;
7072 	}
7073 
7074 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
7075 		params->supported_oper_classes =
7076 		 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
7077 		params->supported_oper_classes_len =
7078 		  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
7079 	}
7080 	return 0;
7081 }
7082 
7083 static int nl80211_set_station_tdls(struct genl_info *info,
7084 				    struct station_parameters *params)
7085 {
7086 	int err;
7087 	/* Dummy STA entry gets updated once the peer capabilities are known */
7088 	if (info->attrs[NL80211_ATTR_PEER_AID])
7089 		params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
7090 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
7091 		params->link_sta_params.ht_capa =
7092 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
7093 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
7094 		params->link_sta_params.vht_capa =
7095 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
7096 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
7097 		params->link_sta_params.he_capa =
7098 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7099 		params->link_sta_params.he_capa_len =
7100 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7101 
7102 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
7103 			params->link_sta_params.eht_capa =
7104 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7105 			params->link_sta_params.eht_capa_len =
7106 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7107 
7108 			if (!ieee80211_eht_capa_size_ok((const u8 *)params->link_sta_params.he_capa,
7109 							(const u8 *)params->link_sta_params.eht_capa,
7110 							params->link_sta_params.eht_capa_len,
7111 							false))
7112 				return -EINVAL;
7113 		}
7114 	}
7115 
7116 	err = nl80211_parse_sta_channel_info(info, params);
7117 	if (err)
7118 		return err;
7119 
7120 	return nl80211_parse_sta_wme(info, params);
7121 }
7122 
7123 static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
7124 					     struct sta_txpwr *txpwr,
7125 					     bool *txpwr_set)
7126 {
7127 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7128 	int idx;
7129 
7130 	if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
7131 		if (!rdev->ops->set_tx_power ||
7132 		    !wiphy_ext_feature_isset(&rdev->wiphy,
7133 					 NL80211_EXT_FEATURE_STA_TX_PWR))
7134 			return -EOPNOTSUPP;
7135 
7136 		idx = NL80211_ATTR_STA_TX_POWER_SETTING;
7137 		txpwr->type = nla_get_u8(info->attrs[idx]);
7138 
7139 		if (txpwr->type == NL80211_TX_POWER_LIMITED) {
7140 			idx = NL80211_ATTR_STA_TX_POWER;
7141 
7142 			if (info->attrs[idx])
7143 				txpwr->power = nla_get_s16(info->attrs[idx]);
7144 			else
7145 				return -EINVAL;
7146 		}
7147 
7148 		*txpwr_set = true;
7149 	} else {
7150 		*txpwr_set = false;
7151 	}
7152 
7153 	return 0;
7154 }
7155 
7156 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
7157 {
7158 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7159 	struct net_device *dev = info->user_ptr[1];
7160 	struct station_parameters params;
7161 	u8 *mac_addr;
7162 	int err;
7163 
7164 	memset(&params, 0, sizeof(params));
7165 
7166 	if (!rdev->ops->change_station)
7167 		return -EOPNOTSUPP;
7168 
7169 	/*
7170 	 * AID and listen_interval properties can be set only for unassociated
7171 	 * station. Include these parameters here and will check them in
7172 	 * cfg80211_check_station_change().
7173 	 */
7174 	if (info->attrs[NL80211_ATTR_STA_AID])
7175 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
7176 
7177 	if (info->attrs[NL80211_ATTR_VLAN_ID])
7178 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
7179 
7180 	if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
7181 		params.listen_interval =
7182 		     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
7183 	else
7184 		params.listen_interval = -1;
7185 
7186 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
7187 		params.support_p2p_ps =
7188 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
7189 	else
7190 		params.support_p2p_ps = -1;
7191 
7192 	if (!info->attrs[NL80211_ATTR_MAC])
7193 		return -EINVAL;
7194 
7195 	params.link_sta_params.link_id =
7196 		nl80211_link_id_or_invalid(info->attrs);
7197 
7198 	if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7199 		/* If MLD_ADDR attribute is set then this is an MLD station
7200 		 * and the MLD_ADDR attribute holds the MLD address and the
7201 		 * MAC attribute holds for the LINK address.
7202 		 * In that case, the link_id is also expected to be valid.
7203 		 */
7204 		if (params.link_sta_params.link_id < 0)
7205 			return -EINVAL;
7206 
7207 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7208 		params.link_sta_params.mld_mac = mac_addr;
7209 		params.link_sta_params.link_mac =
7210 			nla_data(info->attrs[NL80211_ATTR_MAC]);
7211 		if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7212 			return -EINVAL;
7213 	} else {
7214 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7215 	}
7216 
7217 
7218 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
7219 		params.link_sta_params.supported_rates =
7220 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7221 		params.link_sta_params.supported_rates_len =
7222 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7223 	}
7224 
7225 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7226 		params.capability =
7227 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7228 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7229 	}
7230 
7231 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7232 		params.ext_capab =
7233 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7234 		params.ext_capab_len =
7235 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7236 	}
7237 
7238 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7239 		return -EINVAL;
7240 
7241 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7242 		params.plink_action =
7243 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7244 
7245 	if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
7246 		params.plink_state =
7247 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
7248 		if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
7249 			params.peer_aid = nla_get_u16(
7250 				info->attrs[NL80211_ATTR_MESH_PEER_AID]);
7251 		params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
7252 	}
7253 
7254 	if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
7255 		params.local_pm = nla_get_u32(
7256 			info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
7257 
7258 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7259 		params.link_sta_params.opmode_notif_used = true;
7260 		params.link_sta_params.opmode_notif =
7261 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7262 	}
7263 
7264 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7265 		params.link_sta_params.he_6ghz_capa =
7266 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7267 
7268 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7269 		params.airtime_weight =
7270 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7271 
7272 	if (params.airtime_weight &&
7273 	    !wiphy_ext_feature_isset(&rdev->wiphy,
7274 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7275 		return -EOPNOTSUPP;
7276 
7277 	err = nl80211_parse_sta_txpower_setting(info,
7278 						&params.link_sta_params.txpwr,
7279 						&params.link_sta_params.txpwr_set);
7280 	if (err)
7281 		return err;
7282 
7283 	/* Include parameters for TDLS peer (will check later) */
7284 	err = nl80211_set_station_tdls(info, &params);
7285 	if (err)
7286 		return err;
7287 
7288 	params.vlan = get_vlan(info, rdev);
7289 	if (IS_ERR(params.vlan))
7290 		return PTR_ERR(params.vlan);
7291 
7292 	switch (dev->ieee80211_ptr->iftype) {
7293 	case NL80211_IFTYPE_AP:
7294 	case NL80211_IFTYPE_AP_VLAN:
7295 	case NL80211_IFTYPE_P2P_GO:
7296 	case NL80211_IFTYPE_P2P_CLIENT:
7297 	case NL80211_IFTYPE_STATION:
7298 	case NL80211_IFTYPE_ADHOC:
7299 	case NL80211_IFTYPE_MESH_POINT:
7300 		break;
7301 	default:
7302 		err = -EOPNOTSUPP;
7303 		goto out_put_vlan;
7304 	}
7305 
7306 	/* driver will call cfg80211_check_station_change() */
7307 	wdev_lock(dev->ieee80211_ptr);
7308 	err = rdev_change_station(rdev, dev, mac_addr, &params);
7309 	wdev_unlock(dev->ieee80211_ptr);
7310 
7311  out_put_vlan:
7312 	dev_put(params.vlan);
7313 
7314 	return err;
7315 }
7316 
7317 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
7318 {
7319 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7320 	int err;
7321 	struct net_device *dev = info->user_ptr[1];
7322 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7323 	struct station_parameters params;
7324 	u8 *mac_addr = NULL;
7325 	u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
7326 			 BIT(NL80211_STA_FLAG_ASSOCIATED);
7327 
7328 	memset(&params, 0, sizeof(params));
7329 
7330 	if (!rdev->ops->add_station)
7331 		return -EOPNOTSUPP;
7332 
7333 	if (!info->attrs[NL80211_ATTR_MAC])
7334 		return -EINVAL;
7335 
7336 	if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
7337 		return -EINVAL;
7338 
7339 	if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
7340 		return -EINVAL;
7341 
7342 	if (!info->attrs[NL80211_ATTR_STA_AID] &&
7343 	    !info->attrs[NL80211_ATTR_PEER_AID])
7344 		return -EINVAL;
7345 
7346 	params.link_sta_params.link_id =
7347 		nl80211_link_id_or_invalid(info->attrs);
7348 
7349 	if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7350 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7351 		params.link_sta_params.mld_mac = mac_addr;
7352 		params.link_sta_params.link_mac =
7353 			nla_data(info->attrs[NL80211_ATTR_MAC]);
7354 		if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7355 			return -EINVAL;
7356 	} else {
7357 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7358 	}
7359 
7360 	params.link_sta_params.supported_rates =
7361 		nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7362 	params.link_sta_params.supported_rates_len =
7363 		nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7364 	params.listen_interval =
7365 		nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
7366 
7367 	if (info->attrs[NL80211_ATTR_VLAN_ID])
7368 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
7369 
7370 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
7371 		params.support_p2p_ps =
7372 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
7373 	} else {
7374 		/*
7375 		 * if not specified, assume it's supported for P2P GO interface,
7376 		 * and is NOT supported for AP interface
7377 		 */
7378 		params.support_p2p_ps =
7379 			dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
7380 	}
7381 
7382 	if (info->attrs[NL80211_ATTR_PEER_AID])
7383 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
7384 	else
7385 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
7386 
7387 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7388 		params.capability =
7389 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7390 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7391 	}
7392 
7393 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7394 		params.ext_capab =
7395 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7396 		params.ext_capab_len =
7397 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7398 	}
7399 
7400 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
7401 		params.link_sta_params.ht_capa =
7402 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
7403 
7404 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
7405 		params.link_sta_params.vht_capa =
7406 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
7407 
7408 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
7409 		params.link_sta_params.he_capa =
7410 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7411 		params.link_sta_params.he_capa_len =
7412 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7413 
7414 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
7415 			params.link_sta_params.eht_capa =
7416 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7417 			params.link_sta_params.eht_capa_len =
7418 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7419 
7420 			if (!ieee80211_eht_capa_size_ok((const u8 *)params.link_sta_params.he_capa,
7421 							(const u8 *)params.link_sta_params.eht_capa,
7422 							params.link_sta_params.eht_capa_len,
7423 							false))
7424 				return -EINVAL;
7425 		}
7426 	}
7427 
7428 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7429 		params.link_sta_params.he_6ghz_capa =
7430 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7431 
7432 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7433 		params.link_sta_params.opmode_notif_used = true;
7434 		params.link_sta_params.opmode_notif =
7435 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7436 	}
7437 
7438 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7439 		params.plink_action =
7440 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7441 
7442 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7443 		params.airtime_weight =
7444 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7445 
7446 	if (params.airtime_weight &&
7447 	    !wiphy_ext_feature_isset(&rdev->wiphy,
7448 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7449 		return -EOPNOTSUPP;
7450 
7451 	err = nl80211_parse_sta_txpower_setting(info,
7452 						&params.link_sta_params.txpwr,
7453 						&params.link_sta_params.txpwr_set);
7454 	if (err)
7455 		return err;
7456 
7457 	err = nl80211_parse_sta_channel_info(info, &params);
7458 	if (err)
7459 		return err;
7460 
7461 	err = nl80211_parse_sta_wme(info, &params);
7462 	if (err)
7463 		return err;
7464 
7465 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7466 		return -EINVAL;
7467 
7468 	/* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
7469 	 * as userspace might just pass through the capabilities from the IEs
7470 	 * directly, rather than enforcing this restriction and returning an
7471 	 * error in this case.
7472 	 */
7473 	if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
7474 		params.link_sta_params.ht_capa = NULL;
7475 		params.link_sta_params.vht_capa = NULL;
7476 
7477 		/* HE and EHT require WME */
7478 		if (params.link_sta_params.he_capa_len ||
7479 		    params.link_sta_params.he_6ghz_capa ||
7480 		    params.link_sta_params.eht_capa_len)
7481 			return -EINVAL;
7482 	}
7483 
7484 	/* Ensure that HT/VHT capabilities are not set for 6 GHz HE STA */
7485 	if (params.link_sta_params.he_6ghz_capa &&
7486 	    (params.link_sta_params.ht_capa || params.link_sta_params.vht_capa))
7487 		return -EINVAL;
7488 
7489 	/* When you run into this, adjust the code below for the new flag */
7490 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
7491 
7492 	switch (dev->ieee80211_ptr->iftype) {
7493 	case NL80211_IFTYPE_AP:
7494 	case NL80211_IFTYPE_AP_VLAN:
7495 	case NL80211_IFTYPE_P2P_GO:
7496 		/* ignore WME attributes if iface/sta is not capable */
7497 		if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
7498 		    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
7499 			params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7500 
7501 		/* TDLS peers cannot be added */
7502 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7503 		    info->attrs[NL80211_ATTR_PEER_AID])
7504 			return -EINVAL;
7505 		/* but don't bother the driver with it */
7506 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
7507 
7508 		/* allow authenticated/associated only if driver handles it */
7509 		if (!(rdev->wiphy.features &
7510 				NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
7511 		    params.sta_flags_mask & auth_assoc)
7512 			return -EINVAL;
7513 
7514 		/* Older userspace, or userspace wanting to be compatible with
7515 		 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
7516 		 * and assoc flags in the mask, but assumes the station will be
7517 		 * added as associated anyway since this was the required driver
7518 		 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
7519 		 * introduced.
7520 		 * In order to not bother drivers with this quirk in the API
7521 		 * set the flags in both the mask and set for new stations in
7522 		 * this case.
7523 		 */
7524 		if (!(params.sta_flags_mask & auth_assoc)) {
7525 			params.sta_flags_mask |= auth_assoc;
7526 			params.sta_flags_set |= auth_assoc;
7527 		}
7528 
7529 		/* must be last in here for error handling */
7530 		params.vlan = get_vlan(info, rdev);
7531 		if (IS_ERR(params.vlan))
7532 			return PTR_ERR(params.vlan);
7533 		break;
7534 	case NL80211_IFTYPE_MESH_POINT:
7535 		/* ignore uAPSD data */
7536 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7537 
7538 		/* associated is disallowed */
7539 		if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
7540 			return -EINVAL;
7541 		/* TDLS peers cannot be added */
7542 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7543 		    info->attrs[NL80211_ATTR_PEER_AID])
7544 			return -EINVAL;
7545 		break;
7546 	case NL80211_IFTYPE_STATION:
7547 	case NL80211_IFTYPE_P2P_CLIENT:
7548 		/* ignore uAPSD data */
7549 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7550 
7551 		/* these are disallowed */
7552 		if (params.sta_flags_mask &
7553 				(BIT(NL80211_STA_FLAG_ASSOCIATED) |
7554 				 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
7555 			return -EINVAL;
7556 		/* Only TDLS peers can be added */
7557 		if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
7558 			return -EINVAL;
7559 		/* Can only add if TDLS ... */
7560 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
7561 			return -EOPNOTSUPP;
7562 		/* ... with external setup is supported */
7563 		if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
7564 			return -EOPNOTSUPP;
7565 		/*
7566 		 * Older wpa_supplicant versions always mark the TDLS peer
7567 		 * as authorized, but it shouldn't yet be.
7568 		 */
7569 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
7570 		break;
7571 	default:
7572 		return -EOPNOTSUPP;
7573 	}
7574 
7575 	/* be aware of params.vlan when changing code here */
7576 
7577 	wdev_lock(dev->ieee80211_ptr);
7578 	if (wdev->valid_links) {
7579 		if (params.link_sta_params.link_id < 0) {
7580 			err = -EINVAL;
7581 			goto out;
7582 		}
7583 		if (!(wdev->valid_links & BIT(params.link_sta_params.link_id))) {
7584 			err = -ENOLINK;
7585 			goto out;
7586 		}
7587 	} else {
7588 		if (params.link_sta_params.link_id >= 0) {
7589 			err = -EINVAL;
7590 			goto out;
7591 		}
7592 	}
7593 	err = rdev_add_station(rdev, dev, mac_addr, &params);
7594 out:
7595 	wdev_unlock(dev->ieee80211_ptr);
7596 	dev_put(params.vlan);
7597 	return err;
7598 }
7599 
7600 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
7601 {
7602 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7603 	struct net_device *dev = info->user_ptr[1];
7604 	struct station_del_parameters params;
7605 	int ret;
7606 
7607 	memset(&params, 0, sizeof(params));
7608 
7609 	if (info->attrs[NL80211_ATTR_MAC])
7610 		params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
7611 
7612 	switch (dev->ieee80211_ptr->iftype) {
7613 	case NL80211_IFTYPE_AP:
7614 	case NL80211_IFTYPE_AP_VLAN:
7615 	case NL80211_IFTYPE_MESH_POINT:
7616 	case NL80211_IFTYPE_P2P_GO:
7617 		/* always accept these */
7618 		break;
7619 	case NL80211_IFTYPE_ADHOC:
7620 		/* conditionally accept */
7621 		if (wiphy_ext_feature_isset(&rdev->wiphy,
7622 					    NL80211_EXT_FEATURE_DEL_IBSS_STA))
7623 			break;
7624 		return -EINVAL;
7625 	default:
7626 		return -EINVAL;
7627 	}
7628 
7629 	if (!rdev->ops->del_station)
7630 		return -EOPNOTSUPP;
7631 
7632 	if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
7633 		params.subtype =
7634 			nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
7635 		if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
7636 		    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
7637 			return -EINVAL;
7638 	} else {
7639 		/* Default to Deauthentication frame */
7640 		params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
7641 	}
7642 
7643 	if (info->attrs[NL80211_ATTR_REASON_CODE]) {
7644 		params.reason_code =
7645 			nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7646 		if (params.reason_code == 0)
7647 			return -EINVAL; /* 0 is reserved */
7648 	} else {
7649 		/* Default to reason code 2 */
7650 		params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
7651 	}
7652 
7653 	wdev_lock(dev->ieee80211_ptr);
7654 	ret = rdev_del_station(rdev, dev, &params);
7655 	wdev_unlock(dev->ieee80211_ptr);
7656 
7657 	return ret;
7658 }
7659 
7660 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
7661 				int flags, struct net_device *dev,
7662 				u8 *dst, u8 *next_hop,
7663 				struct mpath_info *pinfo)
7664 {
7665 	void *hdr;
7666 	struct nlattr *pinfoattr;
7667 
7668 	hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
7669 	if (!hdr)
7670 		return -1;
7671 
7672 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7673 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7674 	    nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
7675 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
7676 		goto nla_put_failure;
7677 
7678 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
7679 	if (!pinfoattr)
7680 		goto nla_put_failure;
7681 	if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
7682 	    nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
7683 			pinfo->frame_qlen))
7684 		goto nla_put_failure;
7685 	if (((pinfo->filled & MPATH_INFO_SN) &&
7686 	     nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
7687 	    ((pinfo->filled & MPATH_INFO_METRIC) &&
7688 	     nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
7689 			 pinfo->metric)) ||
7690 	    ((pinfo->filled & MPATH_INFO_EXPTIME) &&
7691 	     nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
7692 			 pinfo->exptime)) ||
7693 	    ((pinfo->filled & MPATH_INFO_FLAGS) &&
7694 	     nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
7695 			pinfo->flags)) ||
7696 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
7697 	     nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
7698 			 pinfo->discovery_timeout)) ||
7699 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
7700 	     nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
7701 			pinfo->discovery_retries)) ||
7702 	    ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
7703 	     nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
7704 			pinfo->hop_count)) ||
7705 	    ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
7706 	     nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
7707 			 pinfo->path_change_count)))
7708 		goto nla_put_failure;
7709 
7710 	nla_nest_end(msg, pinfoattr);
7711 
7712 	genlmsg_end(msg, hdr);
7713 	return 0;
7714 
7715  nla_put_failure:
7716 	genlmsg_cancel(msg, hdr);
7717 	return -EMSGSIZE;
7718 }
7719 
7720 static int nl80211_dump_mpath(struct sk_buff *skb,
7721 			      struct netlink_callback *cb)
7722 {
7723 	struct mpath_info pinfo;
7724 	struct cfg80211_registered_device *rdev;
7725 	struct wireless_dev *wdev;
7726 	u8 dst[ETH_ALEN];
7727 	u8 next_hop[ETH_ALEN];
7728 	int path_idx = cb->args[2];
7729 	int err;
7730 
7731 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7732 	if (err)
7733 		return err;
7734 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
7735 	__acquire(&rdev->wiphy.mtx);
7736 
7737 	if (!rdev->ops->dump_mpath) {
7738 		err = -EOPNOTSUPP;
7739 		goto out_err;
7740 	}
7741 
7742 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7743 		err = -EOPNOTSUPP;
7744 		goto out_err;
7745 	}
7746 
7747 	while (1) {
7748 		err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
7749 				      next_hop, &pinfo);
7750 		if (err == -ENOENT)
7751 			break;
7752 		if (err)
7753 			goto out_err;
7754 
7755 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7756 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
7757 				       wdev->netdev, dst, next_hop,
7758 				       &pinfo) < 0)
7759 			goto out;
7760 
7761 		path_idx++;
7762 	}
7763 
7764  out:
7765 	cb->args[2] = path_idx;
7766 	err = skb->len;
7767  out_err:
7768 	wiphy_unlock(&rdev->wiphy);
7769 	return err;
7770 }
7771 
7772 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
7773 {
7774 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7775 	int err;
7776 	struct net_device *dev = info->user_ptr[1];
7777 	struct mpath_info pinfo;
7778 	struct sk_buff *msg;
7779 	u8 *dst = NULL;
7780 	u8 next_hop[ETH_ALEN];
7781 
7782 	memset(&pinfo, 0, sizeof(pinfo));
7783 
7784 	if (!info->attrs[NL80211_ATTR_MAC])
7785 		return -EINVAL;
7786 
7787 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7788 
7789 	if (!rdev->ops->get_mpath)
7790 		return -EOPNOTSUPP;
7791 
7792 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7793 		return -EOPNOTSUPP;
7794 
7795 	err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
7796 	if (err)
7797 		return err;
7798 
7799 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7800 	if (!msg)
7801 		return -ENOMEM;
7802 
7803 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7804 				 dev, dst, next_hop, &pinfo) < 0) {
7805 		nlmsg_free(msg);
7806 		return -ENOBUFS;
7807 	}
7808 
7809 	return genlmsg_reply(msg, info);
7810 }
7811 
7812 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
7813 {
7814 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7815 	struct net_device *dev = info->user_ptr[1];
7816 	u8 *dst = NULL;
7817 	u8 *next_hop = NULL;
7818 
7819 	if (!info->attrs[NL80211_ATTR_MAC])
7820 		return -EINVAL;
7821 
7822 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7823 		return -EINVAL;
7824 
7825 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7826 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7827 
7828 	if (!rdev->ops->change_mpath)
7829 		return -EOPNOTSUPP;
7830 
7831 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7832 		return -EOPNOTSUPP;
7833 
7834 	return rdev_change_mpath(rdev, dev, dst, next_hop);
7835 }
7836 
7837 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
7838 {
7839 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7840 	struct net_device *dev = info->user_ptr[1];
7841 	u8 *dst = NULL;
7842 	u8 *next_hop = NULL;
7843 
7844 	if (!info->attrs[NL80211_ATTR_MAC])
7845 		return -EINVAL;
7846 
7847 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7848 		return -EINVAL;
7849 
7850 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7851 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7852 
7853 	if (!rdev->ops->add_mpath)
7854 		return -EOPNOTSUPP;
7855 
7856 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7857 		return -EOPNOTSUPP;
7858 
7859 	return rdev_add_mpath(rdev, dev, dst, next_hop);
7860 }
7861 
7862 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
7863 {
7864 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7865 	struct net_device *dev = info->user_ptr[1];
7866 	u8 *dst = NULL;
7867 
7868 	if (info->attrs[NL80211_ATTR_MAC])
7869 		dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7870 
7871 	if (!rdev->ops->del_mpath)
7872 		return -EOPNOTSUPP;
7873 
7874 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7875 		return -EOPNOTSUPP;
7876 
7877 	return rdev_del_mpath(rdev, dev, dst);
7878 }
7879 
7880 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
7881 {
7882 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7883 	int err;
7884 	struct net_device *dev = info->user_ptr[1];
7885 	struct mpath_info pinfo;
7886 	struct sk_buff *msg;
7887 	u8 *dst = NULL;
7888 	u8 mpp[ETH_ALEN];
7889 
7890 	memset(&pinfo, 0, sizeof(pinfo));
7891 
7892 	if (!info->attrs[NL80211_ATTR_MAC])
7893 		return -EINVAL;
7894 
7895 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7896 
7897 	if (!rdev->ops->get_mpp)
7898 		return -EOPNOTSUPP;
7899 
7900 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7901 		return -EOPNOTSUPP;
7902 
7903 	err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
7904 	if (err)
7905 		return err;
7906 
7907 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7908 	if (!msg)
7909 		return -ENOMEM;
7910 
7911 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7912 			       dev, dst, mpp, &pinfo) < 0) {
7913 		nlmsg_free(msg);
7914 		return -ENOBUFS;
7915 	}
7916 
7917 	return genlmsg_reply(msg, info);
7918 }
7919 
7920 static int nl80211_dump_mpp(struct sk_buff *skb,
7921 			    struct netlink_callback *cb)
7922 {
7923 	struct mpath_info pinfo;
7924 	struct cfg80211_registered_device *rdev;
7925 	struct wireless_dev *wdev;
7926 	u8 dst[ETH_ALEN];
7927 	u8 mpp[ETH_ALEN];
7928 	int path_idx = cb->args[2];
7929 	int err;
7930 
7931 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7932 	if (err)
7933 		return err;
7934 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
7935 	__acquire(&rdev->wiphy.mtx);
7936 
7937 	if (!rdev->ops->dump_mpp) {
7938 		err = -EOPNOTSUPP;
7939 		goto out_err;
7940 	}
7941 
7942 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7943 		err = -EOPNOTSUPP;
7944 		goto out_err;
7945 	}
7946 
7947 	while (1) {
7948 		err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
7949 				    mpp, &pinfo);
7950 		if (err == -ENOENT)
7951 			break;
7952 		if (err)
7953 			goto out_err;
7954 
7955 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7956 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
7957 				       wdev->netdev, dst, mpp,
7958 				       &pinfo) < 0)
7959 			goto out;
7960 
7961 		path_idx++;
7962 	}
7963 
7964  out:
7965 	cb->args[2] = path_idx;
7966 	err = skb->len;
7967  out_err:
7968 	wiphy_unlock(&rdev->wiphy);
7969 	return err;
7970 }
7971 
7972 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
7973 {
7974 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7975 	struct net_device *dev = info->user_ptr[1];
7976 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7977 	struct bss_parameters params;
7978 	int err;
7979 
7980 	memset(&params, 0, sizeof(params));
7981 	params.link_id = nl80211_link_id_or_invalid(info->attrs);
7982 	/* default to not changing parameters */
7983 	params.use_cts_prot = -1;
7984 	params.use_short_preamble = -1;
7985 	params.use_short_slot_time = -1;
7986 	params.ap_isolate = -1;
7987 	params.ht_opmode = -1;
7988 	params.p2p_ctwindow = -1;
7989 	params.p2p_opp_ps = -1;
7990 
7991 	if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
7992 		params.use_cts_prot =
7993 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
7994 	if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
7995 		params.use_short_preamble =
7996 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
7997 	if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
7998 		params.use_short_slot_time =
7999 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
8000 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8001 		params.basic_rates =
8002 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8003 		params.basic_rates_len =
8004 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8005 	}
8006 	if (info->attrs[NL80211_ATTR_AP_ISOLATE])
8007 		params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
8008 	if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
8009 		params.ht_opmode =
8010 			nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
8011 
8012 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
8013 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
8014 			return -EINVAL;
8015 		params.p2p_ctwindow =
8016 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
8017 		if (params.p2p_ctwindow != 0 &&
8018 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
8019 			return -EINVAL;
8020 	}
8021 
8022 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
8023 		u8 tmp;
8024 
8025 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
8026 			return -EINVAL;
8027 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
8028 		params.p2p_opp_ps = tmp;
8029 		if (params.p2p_opp_ps &&
8030 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
8031 			return -EINVAL;
8032 	}
8033 
8034 	if (!rdev->ops->change_bss)
8035 		return -EOPNOTSUPP;
8036 
8037 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
8038 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
8039 		return -EOPNOTSUPP;
8040 
8041 	wdev_lock(wdev);
8042 	err = rdev_change_bss(rdev, dev, &params);
8043 	wdev_unlock(wdev);
8044 
8045 	return err;
8046 }
8047 
8048 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
8049 {
8050 	char *data = NULL;
8051 	bool is_indoor;
8052 	enum nl80211_user_reg_hint_type user_reg_hint_type;
8053 	u32 owner_nlportid;
8054 
8055 	/*
8056 	 * You should only get this when cfg80211 hasn't yet initialized
8057 	 * completely when built-in to the kernel right between the time
8058 	 * window between nl80211_init() and regulatory_init(), if that is
8059 	 * even possible.
8060 	 */
8061 	if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
8062 		return -EINPROGRESS;
8063 
8064 	if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
8065 		user_reg_hint_type =
8066 		  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
8067 	else
8068 		user_reg_hint_type = NL80211_USER_REG_HINT_USER;
8069 
8070 	switch (user_reg_hint_type) {
8071 	case NL80211_USER_REG_HINT_USER:
8072 	case NL80211_USER_REG_HINT_CELL_BASE:
8073 		if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
8074 			return -EINVAL;
8075 
8076 		data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
8077 		return regulatory_hint_user(data, user_reg_hint_type);
8078 	case NL80211_USER_REG_HINT_INDOOR:
8079 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
8080 			owner_nlportid = info->snd_portid;
8081 			is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
8082 		} else {
8083 			owner_nlportid = 0;
8084 			is_indoor = true;
8085 		}
8086 
8087 		return regulatory_hint_indoor(is_indoor, owner_nlportid);
8088 	default:
8089 		return -EINVAL;
8090 	}
8091 }
8092 
8093 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
8094 {
8095 	return reg_reload_regdb();
8096 }
8097 
8098 static int nl80211_get_mesh_config(struct sk_buff *skb,
8099 				   struct genl_info *info)
8100 {
8101 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8102 	struct net_device *dev = info->user_ptr[1];
8103 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8104 	struct mesh_config cur_params;
8105 	int err = 0;
8106 	void *hdr;
8107 	struct nlattr *pinfoattr;
8108 	struct sk_buff *msg;
8109 
8110 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
8111 		return -EOPNOTSUPP;
8112 
8113 	if (!rdev->ops->get_mesh_config)
8114 		return -EOPNOTSUPP;
8115 
8116 	wdev_lock(wdev);
8117 	/* If not connected, get default parameters */
8118 	if (!wdev->u.mesh.id_len)
8119 		memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
8120 	else
8121 		err = rdev_get_mesh_config(rdev, dev, &cur_params);
8122 	wdev_unlock(wdev);
8123 
8124 	if (err)
8125 		return err;
8126 
8127 	/* Draw up a netlink message to send back */
8128 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8129 	if (!msg)
8130 		return -ENOMEM;
8131 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8132 			     NL80211_CMD_GET_MESH_CONFIG);
8133 	if (!hdr)
8134 		goto out;
8135 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
8136 	if (!pinfoattr)
8137 		goto nla_put_failure;
8138 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8139 	    nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
8140 			cur_params.dot11MeshRetryTimeout) ||
8141 	    nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
8142 			cur_params.dot11MeshConfirmTimeout) ||
8143 	    nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
8144 			cur_params.dot11MeshHoldingTimeout) ||
8145 	    nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
8146 			cur_params.dot11MeshMaxPeerLinks) ||
8147 	    nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
8148 		       cur_params.dot11MeshMaxRetries) ||
8149 	    nla_put_u8(msg, NL80211_MESHCONF_TTL,
8150 		       cur_params.dot11MeshTTL) ||
8151 	    nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
8152 		       cur_params.element_ttl) ||
8153 	    nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8154 		       cur_params.auto_open_plinks) ||
8155 	    nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
8156 			cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
8157 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
8158 		       cur_params.dot11MeshHWMPmaxPREQretries) ||
8159 	    nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
8160 			cur_params.path_refresh_time) ||
8161 	    nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
8162 			cur_params.min_discovery_timeout) ||
8163 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
8164 			cur_params.dot11MeshHWMPactivePathTimeout) ||
8165 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
8166 			cur_params.dot11MeshHWMPpreqMinInterval) ||
8167 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
8168 			cur_params.dot11MeshHWMPperrMinInterval) ||
8169 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
8170 			cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
8171 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
8172 		       cur_params.dot11MeshHWMPRootMode) ||
8173 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
8174 			cur_params.dot11MeshHWMPRannInterval) ||
8175 	    nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
8176 		       cur_params.dot11MeshGateAnnouncementProtocol) ||
8177 	    nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
8178 		       cur_params.dot11MeshForwarding) ||
8179 	    nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
8180 			cur_params.rssi_threshold) ||
8181 	    nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
8182 			cur_params.ht_opmode) ||
8183 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
8184 			cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
8185 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
8186 			cur_params.dot11MeshHWMProotInterval) ||
8187 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
8188 			cur_params.dot11MeshHWMPconfirmationInterval) ||
8189 	    nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
8190 			cur_params.power_mode) ||
8191 	    nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
8192 			cur_params.dot11MeshAwakeWindowDuration) ||
8193 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
8194 			cur_params.plink_timeout) ||
8195 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
8196 		       cur_params.dot11MeshConnectedToMeshGate) ||
8197 	    nla_put_u8(msg, NL80211_MESHCONF_NOLEARN,
8198 		       cur_params.dot11MeshNolearn) ||
8199 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_AS,
8200 		       cur_params.dot11MeshConnectedToAuthServer))
8201 		goto nla_put_failure;
8202 	nla_nest_end(msg, pinfoattr);
8203 	genlmsg_end(msg, hdr);
8204 	return genlmsg_reply(msg, info);
8205 
8206  nla_put_failure:
8207  out:
8208 	nlmsg_free(msg);
8209 	return -ENOBUFS;
8210 }
8211 
8212 static const struct nla_policy
8213 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
8214 	[NL80211_MESHCONF_RETRY_TIMEOUT] =
8215 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8216 	[NL80211_MESHCONF_CONFIRM_TIMEOUT] =
8217 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8218 	[NL80211_MESHCONF_HOLDING_TIMEOUT] =
8219 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8220 	[NL80211_MESHCONF_MAX_PEER_LINKS] =
8221 		NLA_POLICY_RANGE(NLA_U16, 0, 255),
8222 	[NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
8223 	[NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8224 	[NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8225 	[NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
8226 	[NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
8227 		NLA_POLICY_RANGE(NLA_U32, 1, 255),
8228 	[NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
8229 	[NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
8230 	[NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
8231 	[NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
8232 	[NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
8233 		NLA_POLICY_MIN(NLA_U16, 1),
8234 	[NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
8235 		NLA_POLICY_MIN(NLA_U16, 1),
8236 	[NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
8237 		NLA_POLICY_MIN(NLA_U16, 1),
8238 	[NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
8239 	[NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
8240 		NLA_POLICY_MIN(NLA_U16, 1),
8241 	[NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
8242 	[NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
8243 	[NL80211_MESHCONF_RSSI_THRESHOLD] =
8244 		NLA_POLICY_RANGE(NLA_S32, -255, 0),
8245 	[NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
8246 	[NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
8247 	[NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
8248 		NLA_POLICY_MIN(NLA_U16, 1),
8249 	[NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
8250 		NLA_POLICY_MIN(NLA_U16, 1),
8251 	[NL80211_MESHCONF_POWER_MODE] =
8252 		NLA_POLICY_RANGE(NLA_U32,
8253 				 NL80211_MESH_POWER_ACTIVE,
8254 				 NL80211_MESH_POWER_MAX),
8255 	[NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
8256 	[NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
8257 	[NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8258 	[NL80211_MESHCONF_NOLEARN] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8259 	[NL80211_MESHCONF_CONNECTED_TO_AS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8260 };
8261 
8262 static const struct nla_policy
8263 	nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
8264 	[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
8265 	[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
8266 	[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
8267 	[NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
8268 	[NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
8269 	[NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
8270 	[NL80211_MESH_SETUP_IE] =
8271 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
8272 				       IEEE80211_MAX_DATA_LEN),
8273 	[NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
8274 };
8275 
8276 static int nl80211_parse_mesh_config(struct genl_info *info,
8277 				     struct mesh_config *cfg,
8278 				     u32 *mask_out)
8279 {
8280 	struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
8281 	u32 mask = 0;
8282 	u16 ht_opmode;
8283 
8284 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)	\
8285 do {									\
8286 	if (tb[attr]) {							\
8287 		cfg->param = fn(tb[attr]);				\
8288 		mask |= BIT((attr) - 1);				\
8289 	}								\
8290 } while (0)
8291 
8292 	if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
8293 		return -EINVAL;
8294 	if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
8295 		return -EINVAL;
8296 
8297 	/* This makes sure that there aren't more than 32 mesh config
8298 	 * parameters (otherwise our bitfield scheme would not work.) */
8299 	BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
8300 
8301 	/* Fill in the params struct */
8302 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
8303 				  NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
8304 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
8305 				  NL80211_MESHCONF_CONFIRM_TIMEOUT,
8306 				  nla_get_u16);
8307 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
8308 				  NL80211_MESHCONF_HOLDING_TIMEOUT,
8309 				  nla_get_u16);
8310 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
8311 				  NL80211_MESHCONF_MAX_PEER_LINKS,
8312 				  nla_get_u16);
8313 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
8314 				  NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
8315 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
8316 				  NL80211_MESHCONF_TTL, nla_get_u8);
8317 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
8318 				  NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
8319 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
8320 				  NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8321 				  nla_get_u8);
8322 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
8323 				  mask,
8324 				  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
8325 				  nla_get_u32);
8326 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
8327 				  NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
8328 				  nla_get_u8);
8329 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
8330 				  NL80211_MESHCONF_PATH_REFRESH_TIME,
8331 				  nla_get_u32);
8332 	if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
8333 	    (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
8334 		return -EINVAL;
8335 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
8336 				  NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
8337 				  nla_get_u16);
8338 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
8339 				  mask,
8340 				  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
8341 				  nla_get_u32);
8342 	if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
8343 	    (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
8344 	     cfg->dot11MeshHWMPactivePathTimeout > 65535))
8345 		return -EINVAL;
8346 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
8347 				  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
8348 				  nla_get_u16);
8349 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
8350 				  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
8351 				  nla_get_u16);
8352 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8353 				  dot11MeshHWMPnetDiameterTraversalTime, mask,
8354 				  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
8355 				  nla_get_u16);
8356 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
8357 				  NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
8358 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
8359 				  NL80211_MESHCONF_HWMP_RANN_INTERVAL,
8360 				  nla_get_u16);
8361 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
8362 				  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
8363 				  nla_get_u8);
8364 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
8365 				  NL80211_MESHCONF_FORWARDING, nla_get_u8);
8366 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
8367 				  NL80211_MESHCONF_RSSI_THRESHOLD,
8368 				  nla_get_s32);
8369 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
8370 				  NL80211_MESHCONF_CONNECTED_TO_GATE,
8371 				  nla_get_u8);
8372 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToAuthServer, mask,
8373 				  NL80211_MESHCONF_CONNECTED_TO_AS,
8374 				  nla_get_u8);
8375 	/*
8376 	 * Check HT operation mode based on
8377 	 * IEEE 802.11-2016 9.4.2.57 HT Operation element.
8378 	 */
8379 	if (tb[NL80211_MESHCONF_HT_OPMODE]) {
8380 		ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
8381 
8382 		if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
8383 				  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
8384 				  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
8385 			return -EINVAL;
8386 
8387 		/* NON_HT_STA bit is reserved, but some programs set it */
8388 		ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
8389 
8390 		cfg->ht_opmode = ht_opmode;
8391 		mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
8392 	}
8393 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8394 				  dot11MeshHWMPactivePathToRootTimeout, mask,
8395 				  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
8396 				  nla_get_u32);
8397 	if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
8398 	    (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
8399 	     cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
8400 		return -EINVAL;
8401 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
8402 				  NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
8403 				  nla_get_u16);
8404 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
8405 				  mask,
8406 				  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
8407 				  nla_get_u16);
8408 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
8409 				  NL80211_MESHCONF_POWER_MODE, nla_get_u32);
8410 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
8411 				  NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
8412 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
8413 				  NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
8414 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNolearn, mask,
8415 				  NL80211_MESHCONF_NOLEARN, nla_get_u8);
8416 	if (mask_out)
8417 		*mask_out = mask;
8418 
8419 	return 0;
8420 
8421 #undef FILL_IN_MESH_PARAM_IF_SET
8422 }
8423 
8424 static int nl80211_parse_mesh_setup(struct genl_info *info,
8425 				     struct mesh_setup *setup)
8426 {
8427 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8428 	struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
8429 
8430 	if (!info->attrs[NL80211_ATTR_MESH_SETUP])
8431 		return -EINVAL;
8432 	if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
8433 		return -EINVAL;
8434 
8435 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
8436 		setup->sync_method =
8437 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
8438 		 IEEE80211_SYNC_METHOD_VENDOR :
8439 		 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
8440 
8441 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
8442 		setup->path_sel_proto =
8443 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
8444 		 IEEE80211_PATH_PROTOCOL_VENDOR :
8445 		 IEEE80211_PATH_PROTOCOL_HWMP;
8446 
8447 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
8448 		setup->path_metric =
8449 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
8450 		 IEEE80211_PATH_METRIC_VENDOR :
8451 		 IEEE80211_PATH_METRIC_AIRTIME;
8452 
8453 	if (tb[NL80211_MESH_SETUP_IE]) {
8454 		struct nlattr *ieattr =
8455 			tb[NL80211_MESH_SETUP_IE];
8456 		setup->ie = nla_data(ieattr);
8457 		setup->ie_len = nla_len(ieattr);
8458 	}
8459 	if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
8460 	    !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
8461 		return -EINVAL;
8462 	setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
8463 	setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
8464 	setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
8465 	if (setup->is_secure)
8466 		setup->user_mpm = true;
8467 
8468 	if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
8469 		if (!setup->user_mpm)
8470 			return -EINVAL;
8471 		setup->auth_id =
8472 			nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
8473 	}
8474 
8475 	return 0;
8476 }
8477 
8478 static int nl80211_update_mesh_config(struct sk_buff *skb,
8479 				      struct genl_info *info)
8480 {
8481 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8482 	struct net_device *dev = info->user_ptr[1];
8483 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8484 	struct mesh_config cfg;
8485 	u32 mask;
8486 	int err;
8487 
8488 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
8489 		return -EOPNOTSUPP;
8490 
8491 	if (!rdev->ops->update_mesh_config)
8492 		return -EOPNOTSUPP;
8493 
8494 	err = nl80211_parse_mesh_config(info, &cfg, &mask);
8495 	if (err)
8496 		return err;
8497 
8498 	wdev_lock(wdev);
8499 	if (!wdev->u.mesh.id_len)
8500 		err = -ENOLINK;
8501 
8502 	if (!err)
8503 		err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
8504 
8505 	wdev_unlock(wdev);
8506 
8507 	return err;
8508 }
8509 
8510 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
8511 			      struct sk_buff *msg)
8512 {
8513 	struct nlattr *nl_reg_rules;
8514 	unsigned int i;
8515 
8516 	if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
8517 	    (regdom->dfs_region &&
8518 	     nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
8519 		goto nla_put_failure;
8520 
8521 	nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
8522 	if (!nl_reg_rules)
8523 		goto nla_put_failure;
8524 
8525 	for (i = 0; i < regdom->n_reg_rules; i++) {
8526 		struct nlattr *nl_reg_rule;
8527 		const struct ieee80211_reg_rule *reg_rule;
8528 		const struct ieee80211_freq_range *freq_range;
8529 		const struct ieee80211_power_rule *power_rule;
8530 		unsigned int max_bandwidth_khz;
8531 
8532 		reg_rule = &regdom->reg_rules[i];
8533 		freq_range = &reg_rule->freq_range;
8534 		power_rule = &reg_rule->power_rule;
8535 
8536 		nl_reg_rule = nla_nest_start_noflag(msg, i);
8537 		if (!nl_reg_rule)
8538 			goto nla_put_failure;
8539 
8540 		max_bandwidth_khz = freq_range->max_bandwidth_khz;
8541 		if (!max_bandwidth_khz)
8542 			max_bandwidth_khz = reg_get_max_bandwidth(regdom,
8543 								  reg_rule);
8544 
8545 		if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
8546 				reg_rule->flags) ||
8547 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
8548 				freq_range->start_freq_khz) ||
8549 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
8550 				freq_range->end_freq_khz) ||
8551 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
8552 				max_bandwidth_khz) ||
8553 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
8554 				power_rule->max_antenna_gain) ||
8555 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
8556 				power_rule->max_eirp) ||
8557 		    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
8558 				reg_rule->dfs_cac_ms))
8559 			goto nla_put_failure;
8560 
8561 		nla_nest_end(msg, nl_reg_rule);
8562 	}
8563 
8564 	nla_nest_end(msg, nl_reg_rules);
8565 	return 0;
8566 
8567 nla_put_failure:
8568 	return -EMSGSIZE;
8569 }
8570 
8571 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
8572 {
8573 	const struct ieee80211_regdomain *regdom = NULL;
8574 	struct cfg80211_registered_device *rdev;
8575 	struct wiphy *wiphy = NULL;
8576 	struct sk_buff *msg;
8577 	int err = -EMSGSIZE;
8578 	void *hdr;
8579 
8580 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8581 	if (!msg)
8582 		return -ENOBUFS;
8583 
8584 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8585 			     NL80211_CMD_GET_REG);
8586 	if (!hdr)
8587 		goto put_failure;
8588 
8589 	rtnl_lock();
8590 
8591 	if (info->attrs[NL80211_ATTR_WIPHY]) {
8592 		bool self_managed;
8593 
8594 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
8595 		if (IS_ERR(rdev)) {
8596 			err = PTR_ERR(rdev);
8597 			goto nla_put_failure;
8598 		}
8599 
8600 		wiphy = &rdev->wiphy;
8601 		self_managed = wiphy->regulatory_flags &
8602 			       REGULATORY_WIPHY_SELF_MANAGED;
8603 
8604 		rcu_read_lock();
8605 
8606 		regdom = get_wiphy_regdom(wiphy);
8607 
8608 		/* a self-managed-reg device must have a private regdom */
8609 		if (WARN_ON(!regdom && self_managed)) {
8610 			err = -EINVAL;
8611 			goto nla_put_failure_rcu;
8612 		}
8613 
8614 		if (regdom &&
8615 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8616 			goto nla_put_failure_rcu;
8617 	} else {
8618 		rcu_read_lock();
8619 	}
8620 
8621 	if (!wiphy && reg_last_request_cell_base() &&
8622 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8623 			NL80211_USER_REG_HINT_CELL_BASE))
8624 		goto nla_put_failure_rcu;
8625 
8626 	if (!regdom)
8627 		regdom = rcu_dereference(cfg80211_regdomain);
8628 
8629 	if (nl80211_put_regdom(regdom, msg))
8630 		goto nla_put_failure_rcu;
8631 
8632 	rcu_read_unlock();
8633 
8634 	genlmsg_end(msg, hdr);
8635 	rtnl_unlock();
8636 	return genlmsg_reply(msg, info);
8637 
8638 nla_put_failure_rcu:
8639 	rcu_read_unlock();
8640 nla_put_failure:
8641 	rtnl_unlock();
8642 put_failure:
8643 	nlmsg_free(msg);
8644 	return err;
8645 }
8646 
8647 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
8648 			       u32 seq, int flags, struct wiphy *wiphy,
8649 			       const struct ieee80211_regdomain *regdom)
8650 {
8651 	void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
8652 				   NL80211_CMD_GET_REG);
8653 
8654 	if (!hdr)
8655 		return -1;
8656 
8657 	genl_dump_check_consistent(cb, hdr);
8658 
8659 	if (nl80211_put_regdom(regdom, msg))
8660 		goto nla_put_failure;
8661 
8662 	if (!wiphy && reg_last_request_cell_base() &&
8663 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8664 			NL80211_USER_REG_HINT_CELL_BASE))
8665 		goto nla_put_failure;
8666 
8667 	if (wiphy &&
8668 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8669 		goto nla_put_failure;
8670 
8671 	if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
8672 	    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
8673 		goto nla_put_failure;
8674 
8675 	genlmsg_end(msg, hdr);
8676 	return 0;
8677 
8678 nla_put_failure:
8679 	genlmsg_cancel(msg, hdr);
8680 	return -EMSGSIZE;
8681 }
8682 
8683 static int nl80211_get_reg_dump(struct sk_buff *skb,
8684 				struct netlink_callback *cb)
8685 {
8686 	const struct ieee80211_regdomain *regdom = NULL;
8687 	struct cfg80211_registered_device *rdev;
8688 	int err, reg_idx, start = cb->args[2];
8689 
8690 	rcu_read_lock();
8691 
8692 	if (cfg80211_regdomain && start == 0) {
8693 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8694 					  NLM_F_MULTI, NULL,
8695 					  rcu_dereference(cfg80211_regdomain));
8696 		if (err < 0)
8697 			goto out_err;
8698 	}
8699 
8700 	/* the global regdom is idx 0 */
8701 	reg_idx = 1;
8702 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
8703 		regdom = get_wiphy_regdom(&rdev->wiphy);
8704 		if (!regdom)
8705 			continue;
8706 
8707 		if (++reg_idx <= start)
8708 			continue;
8709 
8710 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8711 					  NLM_F_MULTI, &rdev->wiphy, regdom);
8712 		if (err < 0) {
8713 			reg_idx--;
8714 			break;
8715 		}
8716 	}
8717 
8718 	cb->args[2] = reg_idx;
8719 	err = skb->len;
8720 out_err:
8721 	rcu_read_unlock();
8722 	return err;
8723 }
8724 
8725 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
8726 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
8727 	[NL80211_ATTR_REG_RULE_FLAGS]		= { .type = NLA_U32 },
8728 	[NL80211_ATTR_FREQ_RANGE_START]		= { .type = NLA_U32 },
8729 	[NL80211_ATTR_FREQ_RANGE_END]		= { .type = NLA_U32 },
8730 	[NL80211_ATTR_FREQ_RANGE_MAX_BW]	= { .type = NLA_U32 },
8731 	[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]	= { .type = NLA_U32 },
8732 	[NL80211_ATTR_POWER_RULE_MAX_EIRP]	= { .type = NLA_U32 },
8733 	[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
8734 };
8735 
8736 static int parse_reg_rule(struct nlattr *tb[],
8737 	struct ieee80211_reg_rule *reg_rule)
8738 {
8739 	struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
8740 	struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
8741 
8742 	if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
8743 		return -EINVAL;
8744 	if (!tb[NL80211_ATTR_FREQ_RANGE_START])
8745 		return -EINVAL;
8746 	if (!tb[NL80211_ATTR_FREQ_RANGE_END])
8747 		return -EINVAL;
8748 	if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
8749 		return -EINVAL;
8750 	if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
8751 		return -EINVAL;
8752 
8753 	reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
8754 
8755 	freq_range->start_freq_khz =
8756 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
8757 	freq_range->end_freq_khz =
8758 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
8759 	freq_range->max_bandwidth_khz =
8760 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
8761 
8762 	power_rule->max_eirp =
8763 		nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
8764 
8765 	if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
8766 		power_rule->max_antenna_gain =
8767 			nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
8768 
8769 	if (tb[NL80211_ATTR_DFS_CAC_TIME])
8770 		reg_rule->dfs_cac_ms =
8771 			nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
8772 
8773 	return 0;
8774 }
8775 
8776 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
8777 {
8778 	struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
8779 	struct nlattr *nl_reg_rule;
8780 	char *alpha2;
8781 	int rem_reg_rules, r;
8782 	u32 num_rules = 0, rule_idx = 0;
8783 	enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
8784 	struct ieee80211_regdomain *rd;
8785 
8786 	if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
8787 		return -EINVAL;
8788 
8789 	if (!info->attrs[NL80211_ATTR_REG_RULES])
8790 		return -EINVAL;
8791 
8792 	alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
8793 
8794 	if (info->attrs[NL80211_ATTR_DFS_REGION])
8795 		dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
8796 
8797 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8798 			    rem_reg_rules) {
8799 		num_rules++;
8800 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
8801 			return -EINVAL;
8802 	}
8803 
8804 	rtnl_lock();
8805 	if (!reg_is_valid_request(alpha2)) {
8806 		r = -EINVAL;
8807 		goto out;
8808 	}
8809 
8810 	rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
8811 	if (!rd) {
8812 		r = -ENOMEM;
8813 		goto out;
8814 	}
8815 
8816 	rd->n_reg_rules = num_rules;
8817 	rd->alpha2[0] = alpha2[0];
8818 	rd->alpha2[1] = alpha2[1];
8819 
8820 	/*
8821 	 * Disable DFS master mode if the DFS region was
8822 	 * not supported or known on this kernel.
8823 	 */
8824 	if (reg_supported_dfs_region(dfs_region))
8825 		rd->dfs_region = dfs_region;
8826 
8827 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8828 			    rem_reg_rules) {
8829 		r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
8830 						nl_reg_rule, reg_rule_policy,
8831 						info->extack);
8832 		if (r)
8833 			goto bad_reg;
8834 		r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
8835 		if (r)
8836 			goto bad_reg;
8837 
8838 		rule_idx++;
8839 
8840 		if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
8841 			r = -EINVAL;
8842 			goto bad_reg;
8843 		}
8844 	}
8845 
8846 	r = set_regdom(rd, REGD_SOURCE_CRDA);
8847 	/* set_regdom takes ownership of rd */
8848 	rd = NULL;
8849  bad_reg:
8850 	kfree(rd);
8851  out:
8852 	rtnl_unlock();
8853 	return r;
8854 }
8855 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
8856 
8857 static int validate_scan_freqs(struct nlattr *freqs)
8858 {
8859 	struct nlattr *attr1, *attr2;
8860 	int n_channels = 0, tmp1, tmp2;
8861 
8862 	nla_for_each_nested(attr1, freqs, tmp1)
8863 		if (nla_len(attr1) != sizeof(u32))
8864 			return 0;
8865 
8866 	nla_for_each_nested(attr1, freqs, tmp1) {
8867 		n_channels++;
8868 		/*
8869 		 * Some hardware has a limited channel list for
8870 		 * scanning, and it is pretty much nonsensical
8871 		 * to scan for a channel twice, so disallow that
8872 		 * and don't require drivers to check that the
8873 		 * channel list they get isn't longer than what
8874 		 * they can scan, as long as they can scan all
8875 		 * the channels they registered at once.
8876 		 */
8877 		nla_for_each_nested(attr2, freqs, tmp2)
8878 			if (attr1 != attr2 &&
8879 			    nla_get_u32(attr1) == nla_get_u32(attr2))
8880 				return 0;
8881 	}
8882 
8883 	return n_channels;
8884 }
8885 
8886 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
8887 {
8888 	return b < NUM_NL80211_BANDS && wiphy->bands[b];
8889 }
8890 
8891 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
8892 			    struct cfg80211_bss_selection *bss_select)
8893 {
8894 	struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
8895 	struct nlattr *nest;
8896 	int err;
8897 	bool found = false;
8898 	int i;
8899 
8900 	/* only process one nested attribute */
8901 	nest = nla_data(nla);
8902 	if (!nla_ok(nest, nla_len(nest)))
8903 		return -EINVAL;
8904 
8905 	err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
8906 					  nest, nl80211_bss_select_policy,
8907 					  NULL);
8908 	if (err)
8909 		return err;
8910 
8911 	/* only one attribute may be given */
8912 	for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
8913 		if (attr[i]) {
8914 			if (found)
8915 				return -EINVAL;
8916 			found = true;
8917 		}
8918 	}
8919 
8920 	bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
8921 
8922 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
8923 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
8924 
8925 	if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
8926 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
8927 		bss_select->param.band_pref =
8928 			nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
8929 		if (!is_band_valid(wiphy, bss_select->param.band_pref))
8930 			return -EINVAL;
8931 	}
8932 
8933 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
8934 		struct nl80211_bss_select_rssi_adjust *adj_param;
8935 
8936 		adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
8937 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
8938 		bss_select->param.adjust.band = adj_param->band;
8939 		bss_select->param.adjust.delta = adj_param->delta;
8940 		if (!is_band_valid(wiphy, bss_select->param.adjust.band))
8941 			return -EINVAL;
8942 	}
8943 
8944 	/* user-space did not provide behaviour attribute */
8945 	if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
8946 		return -EINVAL;
8947 
8948 	if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
8949 		return -EINVAL;
8950 
8951 	return 0;
8952 }
8953 
8954 int nl80211_parse_random_mac(struct nlattr **attrs,
8955 			     u8 *mac_addr, u8 *mac_addr_mask)
8956 {
8957 	int i;
8958 
8959 	if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
8960 		eth_zero_addr(mac_addr);
8961 		eth_zero_addr(mac_addr_mask);
8962 		mac_addr[0] = 0x2;
8963 		mac_addr_mask[0] = 0x3;
8964 
8965 		return 0;
8966 	}
8967 
8968 	/* need both or none */
8969 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
8970 		return -EINVAL;
8971 
8972 	memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
8973 	memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
8974 
8975 	/* don't allow or configure an mcast address */
8976 	if (!is_multicast_ether_addr(mac_addr_mask) ||
8977 	    is_multicast_ether_addr(mac_addr))
8978 		return -EINVAL;
8979 
8980 	/*
8981 	 * allow users to pass a MAC address that has bits set outside
8982 	 * of the mask, but don't bother drivers with having to deal
8983 	 * with such bits
8984 	 */
8985 	for (i = 0; i < ETH_ALEN; i++)
8986 		mac_addr[i] &= mac_addr_mask[i];
8987 
8988 	return 0;
8989 }
8990 
8991 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev,
8992 					      struct ieee80211_channel *chan)
8993 {
8994 	unsigned int link_id;
8995 	bool all_ok = true;
8996 
8997 	ASSERT_WDEV_LOCK(wdev);
8998 
8999 	if (!cfg80211_beaconing_iface_active(wdev))
9000 		return true;
9001 
9002 	/*
9003 	 * FIXME: check if we have a free HW resource/link for chan
9004 	 *
9005 	 * This, as well as the FIXME below, requires knowing the link
9006 	 * capabilities of the hardware.
9007 	 */
9008 
9009 	/* we cannot leave radar channels */
9010 	for_each_valid_link(wdev, link_id) {
9011 		struct cfg80211_chan_def *chandef;
9012 
9013 		chandef = wdev_chandef(wdev, link_id);
9014 		if (!chandef || !chandef->chan)
9015 			continue;
9016 
9017 		/*
9018 		 * FIXME: don't require all_ok, but rather check only the
9019 		 *	  correct HW resource/link onto which 'chan' falls,
9020 		 *	  as only that link leaves the channel for doing
9021 		 *	  the off-channel operation.
9022 		 */
9023 
9024 		if (chandef->chan->flags & IEEE80211_CHAN_RADAR)
9025 			all_ok = false;
9026 	}
9027 
9028 	if (all_ok)
9029 		return true;
9030 
9031 	return regulatory_pre_cac_allowed(wdev->wiphy);
9032 }
9033 
9034 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
9035 				    enum nl80211_ext_feature_index feat)
9036 {
9037 	if (!(flags & flag))
9038 		return true;
9039 	if (wiphy_ext_feature_isset(wiphy, feat))
9040 		return true;
9041 	return false;
9042 }
9043 
9044 static int
9045 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
9046 			 void *request, struct nlattr **attrs,
9047 			 bool is_sched_scan)
9048 {
9049 	u8 *mac_addr, *mac_addr_mask;
9050 	u32 *flags;
9051 	enum nl80211_feature_flags randomness_flag;
9052 
9053 	if (!attrs[NL80211_ATTR_SCAN_FLAGS])
9054 		return 0;
9055 
9056 	if (is_sched_scan) {
9057 		struct cfg80211_sched_scan_request *req = request;
9058 
9059 		randomness_flag = wdev ?
9060 				  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
9061 				  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
9062 		flags = &req->flags;
9063 		mac_addr = req->mac_addr;
9064 		mac_addr_mask = req->mac_addr_mask;
9065 	} else {
9066 		struct cfg80211_scan_request *req = request;
9067 
9068 		randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
9069 		flags = &req->flags;
9070 		mac_addr = req->mac_addr;
9071 		mac_addr_mask = req->mac_addr_mask;
9072 	}
9073 
9074 	*flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
9075 
9076 	if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
9077 	     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
9078 	    !nl80211_check_scan_feat(wiphy, *flags,
9079 				     NL80211_SCAN_FLAG_LOW_SPAN,
9080 				     NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
9081 	    !nl80211_check_scan_feat(wiphy, *flags,
9082 				     NL80211_SCAN_FLAG_LOW_POWER,
9083 				     NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
9084 	    !nl80211_check_scan_feat(wiphy, *flags,
9085 				     NL80211_SCAN_FLAG_HIGH_ACCURACY,
9086 				     NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
9087 	    !nl80211_check_scan_feat(wiphy, *flags,
9088 				     NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
9089 				     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
9090 	    !nl80211_check_scan_feat(wiphy, *flags,
9091 				     NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
9092 				     NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
9093 	    !nl80211_check_scan_feat(wiphy, *flags,
9094 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
9095 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
9096 	    !nl80211_check_scan_feat(wiphy, *flags,
9097 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
9098 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
9099 	    !nl80211_check_scan_feat(wiphy, *flags,
9100 				     NL80211_SCAN_FLAG_RANDOM_SN,
9101 				     NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
9102 	    !nl80211_check_scan_feat(wiphy, *flags,
9103 				     NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
9104 				     NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
9105 		return -EOPNOTSUPP;
9106 
9107 	if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
9108 		int err;
9109 
9110 		if (!(wiphy->features & randomness_flag) ||
9111 		    (wdev && wdev->connected))
9112 			return -EOPNOTSUPP;
9113 
9114 		err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
9115 		if (err)
9116 			return err;
9117 	}
9118 
9119 	return 0;
9120 }
9121 
9122 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
9123 {
9124 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9125 	struct wireless_dev *wdev = info->user_ptr[1];
9126 	struct cfg80211_scan_request *request;
9127 	struct nlattr *scan_freqs = NULL;
9128 	bool scan_freqs_khz = false;
9129 	struct nlattr *attr;
9130 	struct wiphy *wiphy;
9131 	int err, tmp, n_ssids = 0, n_channels, i;
9132 	size_t ie_len, size;
9133 
9134 	wiphy = &rdev->wiphy;
9135 
9136 	if (wdev->iftype == NL80211_IFTYPE_NAN)
9137 		return -EOPNOTSUPP;
9138 
9139 	if (!rdev->ops->scan)
9140 		return -EOPNOTSUPP;
9141 
9142 	if (rdev->scan_req || rdev->scan_msg)
9143 		return -EBUSY;
9144 
9145 	if (info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ]) {
9146 		if (!wiphy_ext_feature_isset(wiphy,
9147 					     NL80211_EXT_FEATURE_SCAN_FREQ_KHZ))
9148 			return -EOPNOTSUPP;
9149 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ];
9150 		scan_freqs_khz = true;
9151 	} else if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES])
9152 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQUENCIES];
9153 
9154 	if (scan_freqs) {
9155 		n_channels = validate_scan_freqs(scan_freqs);
9156 		if (!n_channels)
9157 			return -EINVAL;
9158 	} else {
9159 		n_channels = ieee80211_get_num_supported_channels(wiphy);
9160 	}
9161 
9162 	if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
9163 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
9164 			n_ssids++;
9165 
9166 	if (n_ssids > wiphy->max_scan_ssids)
9167 		return -EINVAL;
9168 
9169 	if (info->attrs[NL80211_ATTR_IE])
9170 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9171 	else
9172 		ie_len = 0;
9173 
9174 	if (ie_len > wiphy->max_scan_ie_len)
9175 		return -EINVAL;
9176 
9177 	size = struct_size(request, channels, n_channels);
9178 	size = size_add(size, array_size(sizeof(*request->ssids), n_ssids));
9179 	size = size_add(size, ie_len);
9180 	request = kzalloc(size, GFP_KERNEL);
9181 	if (!request)
9182 		return -ENOMEM;
9183 
9184 	if (n_ssids)
9185 		request->ssids = (void *)&request->channels[n_channels];
9186 	request->n_ssids = n_ssids;
9187 	if (ie_len) {
9188 		if (n_ssids)
9189 			request->ie = (void *)(request->ssids + n_ssids);
9190 		else
9191 			request->ie = (void *)(request->channels + n_channels);
9192 	}
9193 
9194 	i = 0;
9195 	if (scan_freqs) {
9196 		/* user specified, bail out if channel not found */
9197 		nla_for_each_nested(attr, scan_freqs, tmp) {
9198 			struct ieee80211_channel *chan;
9199 			int freq = nla_get_u32(attr);
9200 
9201 			if (!scan_freqs_khz)
9202 				freq = MHZ_TO_KHZ(freq);
9203 
9204 			chan = ieee80211_get_channel_khz(wiphy, freq);
9205 			if (!chan) {
9206 				err = -EINVAL;
9207 				goto out_free;
9208 			}
9209 
9210 			/* ignore disabled channels */
9211 			if (chan->flags & IEEE80211_CHAN_DISABLED)
9212 				continue;
9213 
9214 			request->channels[i] = chan;
9215 			i++;
9216 		}
9217 	} else {
9218 		enum nl80211_band band;
9219 
9220 		/* all channels */
9221 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
9222 			int j;
9223 
9224 			if (!wiphy->bands[band])
9225 				continue;
9226 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9227 				struct ieee80211_channel *chan;
9228 
9229 				chan = &wiphy->bands[band]->channels[j];
9230 
9231 				if (chan->flags & IEEE80211_CHAN_DISABLED)
9232 					continue;
9233 
9234 				request->channels[i] = chan;
9235 				i++;
9236 			}
9237 		}
9238 	}
9239 
9240 	if (!i) {
9241 		err = -EINVAL;
9242 		goto out_free;
9243 	}
9244 
9245 	request->n_channels = i;
9246 
9247 	wdev_lock(wdev);
9248 	for (i = 0; i < request->n_channels; i++) {
9249 		struct ieee80211_channel *chan = request->channels[i];
9250 
9251 		/* if we can go off-channel to the target channel we're good */
9252 		if (cfg80211_off_channel_oper_allowed(wdev, chan))
9253 			continue;
9254 
9255 		if (!cfg80211_wdev_on_sub_chan(wdev, chan, true)) {
9256 			wdev_unlock(wdev);
9257 			err = -EBUSY;
9258 			goto out_free;
9259 		}
9260 	}
9261 	wdev_unlock(wdev);
9262 
9263 	i = 0;
9264 	if (n_ssids) {
9265 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
9266 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9267 				err = -EINVAL;
9268 				goto out_free;
9269 			}
9270 			request->ssids[i].ssid_len = nla_len(attr);
9271 			memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
9272 			i++;
9273 		}
9274 	}
9275 
9276 	if (info->attrs[NL80211_ATTR_IE]) {
9277 		request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9278 		memcpy((void *)request->ie,
9279 		       nla_data(info->attrs[NL80211_ATTR_IE]),
9280 		       request->ie_len);
9281 	}
9282 
9283 	for (i = 0; i < NUM_NL80211_BANDS; i++)
9284 		if (wiphy->bands[i])
9285 			request->rates[i] =
9286 				(1 << wiphy->bands[i]->n_bitrates) - 1;
9287 
9288 	if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
9289 		nla_for_each_nested(attr,
9290 				    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
9291 				    tmp) {
9292 			enum nl80211_band band = nla_type(attr);
9293 
9294 			if (band < 0 || band >= NUM_NL80211_BANDS) {
9295 				err = -EINVAL;
9296 				goto out_free;
9297 			}
9298 
9299 			if (!wiphy->bands[band])
9300 				continue;
9301 
9302 			err = ieee80211_get_ratemask(wiphy->bands[band],
9303 						     nla_data(attr),
9304 						     nla_len(attr),
9305 						     &request->rates[band]);
9306 			if (err)
9307 				goto out_free;
9308 		}
9309 	}
9310 
9311 	if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
9312 		request->duration =
9313 			nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
9314 		request->duration_mandatory =
9315 			nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
9316 	}
9317 
9318 	err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
9319 				       false);
9320 	if (err)
9321 		goto out_free;
9322 
9323 	request->no_cck =
9324 		nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
9325 
9326 	/* Initial implementation used NL80211_ATTR_MAC to set the specific
9327 	 * BSSID to scan for. This was problematic because that same attribute
9328 	 * was already used for another purpose (local random MAC address). The
9329 	 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
9330 	 * compatibility with older userspace components, also use the
9331 	 * NL80211_ATTR_MAC value here if it can be determined to be used for
9332 	 * the specific BSSID use case instead of the random MAC address
9333 	 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
9334 	 */
9335 	if (info->attrs[NL80211_ATTR_BSSID])
9336 		memcpy(request->bssid,
9337 		       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
9338 	else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
9339 		 info->attrs[NL80211_ATTR_MAC])
9340 		memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
9341 		       ETH_ALEN);
9342 	else
9343 		eth_broadcast_addr(request->bssid);
9344 
9345 	request->wdev = wdev;
9346 	request->wiphy = &rdev->wiphy;
9347 	request->scan_start = jiffies;
9348 
9349 	rdev->scan_req = request;
9350 	err = cfg80211_scan(rdev);
9351 
9352 	if (err)
9353 		goto out_free;
9354 
9355 	nl80211_send_scan_start(rdev, wdev);
9356 	dev_hold(wdev->netdev);
9357 
9358 	return 0;
9359 
9360  out_free:
9361 	rdev->scan_req = NULL;
9362 	kfree(request);
9363 
9364 	return err;
9365 }
9366 
9367 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
9368 {
9369 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9370 	struct wireless_dev *wdev = info->user_ptr[1];
9371 
9372 	if (!rdev->ops->abort_scan)
9373 		return -EOPNOTSUPP;
9374 
9375 	if (rdev->scan_msg)
9376 		return 0;
9377 
9378 	if (!rdev->scan_req)
9379 		return -ENOENT;
9380 
9381 	rdev_abort_scan(rdev, wdev);
9382 	return 0;
9383 }
9384 
9385 static int
9386 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
9387 			       struct cfg80211_sched_scan_request *request,
9388 			       struct nlattr **attrs)
9389 {
9390 	int tmp, err, i = 0;
9391 	struct nlattr *attr;
9392 
9393 	if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9394 		u32 interval;
9395 
9396 		/*
9397 		 * If scan plans are not specified,
9398 		 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
9399 		 * case one scan plan will be set with the specified scan
9400 		 * interval and infinite number of iterations.
9401 		 */
9402 		interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
9403 		if (!interval)
9404 			return -EINVAL;
9405 
9406 		request->scan_plans[0].interval =
9407 			DIV_ROUND_UP(interval, MSEC_PER_SEC);
9408 		if (!request->scan_plans[0].interval)
9409 			return -EINVAL;
9410 
9411 		if (request->scan_plans[0].interval >
9412 		    wiphy->max_sched_scan_plan_interval)
9413 			request->scan_plans[0].interval =
9414 				wiphy->max_sched_scan_plan_interval;
9415 
9416 		return 0;
9417 	}
9418 
9419 	nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
9420 		struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
9421 
9422 		if (WARN_ON(i >= n_plans))
9423 			return -EINVAL;
9424 
9425 		err = nla_parse_nested_deprecated(plan,
9426 						  NL80211_SCHED_SCAN_PLAN_MAX,
9427 						  attr, nl80211_plan_policy,
9428 						  NULL);
9429 		if (err)
9430 			return err;
9431 
9432 		if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
9433 			return -EINVAL;
9434 
9435 		request->scan_plans[i].interval =
9436 			nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
9437 		if (!request->scan_plans[i].interval ||
9438 		    request->scan_plans[i].interval >
9439 		    wiphy->max_sched_scan_plan_interval)
9440 			return -EINVAL;
9441 
9442 		if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
9443 			request->scan_plans[i].iterations =
9444 				nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
9445 			if (!request->scan_plans[i].iterations ||
9446 			    (request->scan_plans[i].iterations >
9447 			     wiphy->max_sched_scan_plan_iterations))
9448 				return -EINVAL;
9449 		} else if (i < n_plans - 1) {
9450 			/*
9451 			 * All scan plans but the last one must specify
9452 			 * a finite number of iterations
9453 			 */
9454 			return -EINVAL;
9455 		}
9456 
9457 		i++;
9458 	}
9459 
9460 	/*
9461 	 * The last scan plan must not specify the number of
9462 	 * iterations, it is supposed to run infinitely
9463 	 */
9464 	if (request->scan_plans[n_plans - 1].iterations)
9465 		return  -EINVAL;
9466 
9467 	return 0;
9468 }
9469 
9470 static int
9471 nl80211_parse_sched_scan_per_band_rssi(struct wiphy *wiphy,
9472 				       struct cfg80211_match_set *match_sets,
9473 				       struct nlattr *tb_band_rssi,
9474 				       s32 rssi_thold)
9475 {
9476 	struct nlattr *attr;
9477 	int i, tmp, ret = 0;
9478 
9479 	if (!wiphy_ext_feature_isset(wiphy,
9480 		    NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD)) {
9481 		if (tb_band_rssi)
9482 			ret = -EOPNOTSUPP;
9483 		else
9484 			for (i = 0; i < NUM_NL80211_BANDS; i++)
9485 				match_sets->per_band_rssi_thold[i] =
9486 					NL80211_SCAN_RSSI_THOLD_OFF;
9487 		return ret;
9488 	}
9489 
9490 	for (i = 0; i < NUM_NL80211_BANDS; i++)
9491 		match_sets->per_band_rssi_thold[i] = rssi_thold;
9492 
9493 	nla_for_each_nested(attr, tb_band_rssi, tmp) {
9494 		enum nl80211_band band = nla_type(attr);
9495 
9496 		if (band < 0 || band >= NUM_NL80211_BANDS)
9497 			return -EINVAL;
9498 
9499 		match_sets->per_band_rssi_thold[band] =	nla_get_s32(attr);
9500 	}
9501 
9502 	return 0;
9503 }
9504 
9505 static struct cfg80211_sched_scan_request *
9506 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
9507 			 struct nlattr **attrs, int max_match_sets)
9508 {
9509 	struct cfg80211_sched_scan_request *request;
9510 	struct nlattr *attr;
9511 	int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
9512 	enum nl80211_band band;
9513 	size_t ie_len, size;
9514 	struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
9515 	s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
9516 
9517 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9518 		n_channels = validate_scan_freqs(
9519 				attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
9520 		if (!n_channels)
9521 			return ERR_PTR(-EINVAL);
9522 	} else {
9523 		n_channels = ieee80211_get_num_supported_channels(wiphy);
9524 	}
9525 
9526 	if (attrs[NL80211_ATTR_SCAN_SSIDS])
9527 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9528 				    tmp)
9529 			n_ssids++;
9530 
9531 	if (n_ssids > wiphy->max_sched_scan_ssids)
9532 		return ERR_PTR(-EINVAL);
9533 
9534 	/*
9535 	 * First, count the number of 'real' matchsets. Due to an issue with
9536 	 * the old implementation, matchsets containing only the RSSI attribute
9537 	 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
9538 	 * RSSI for all matchsets, rather than their own matchset for reporting
9539 	 * all APs with a strong RSSI. This is needed to be compatible with
9540 	 * older userspace that treated a matchset with only the RSSI as the
9541 	 * global RSSI for all other matchsets - if there are other matchsets.
9542 	 */
9543 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9544 		nla_for_each_nested(attr,
9545 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9546 				    tmp) {
9547 			struct nlattr *rssi;
9548 
9549 			err = nla_parse_nested_deprecated(tb,
9550 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9551 							  attr,
9552 							  nl80211_match_policy,
9553 							  NULL);
9554 			if (err)
9555 				return ERR_PTR(err);
9556 
9557 			/* SSID and BSSID are mutually exclusive */
9558 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
9559 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
9560 				return ERR_PTR(-EINVAL);
9561 
9562 			/* add other standalone attributes here */
9563 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
9564 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
9565 				n_match_sets++;
9566 				continue;
9567 			}
9568 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9569 			if (rssi)
9570 				default_match_rssi = nla_get_s32(rssi);
9571 		}
9572 	}
9573 
9574 	/* However, if there's no other matchset, add the RSSI one */
9575 	if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
9576 		n_match_sets = 1;
9577 
9578 	if (n_match_sets > max_match_sets)
9579 		return ERR_PTR(-EINVAL);
9580 
9581 	if (attrs[NL80211_ATTR_IE])
9582 		ie_len = nla_len(attrs[NL80211_ATTR_IE]);
9583 	else
9584 		ie_len = 0;
9585 
9586 	if (ie_len > wiphy->max_sched_scan_ie_len)
9587 		return ERR_PTR(-EINVAL);
9588 
9589 	if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9590 		/*
9591 		 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
9592 		 * each scan plan already specifies its own interval
9593 		 */
9594 		if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9595 			return ERR_PTR(-EINVAL);
9596 
9597 		nla_for_each_nested(attr,
9598 				    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
9599 			n_plans++;
9600 	} else {
9601 		/*
9602 		 * The scan interval attribute is kept for backward
9603 		 * compatibility. If no scan plans are specified and sched scan
9604 		 * interval is specified, one scan plan will be set with this
9605 		 * scan interval and infinite number of iterations.
9606 		 */
9607 		if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9608 			return ERR_PTR(-EINVAL);
9609 
9610 		n_plans = 1;
9611 	}
9612 
9613 	if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
9614 		return ERR_PTR(-EINVAL);
9615 
9616 	if (!wiphy_ext_feature_isset(
9617 		    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
9618 	    (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
9619 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
9620 		return ERR_PTR(-EINVAL);
9621 
9622 	size = struct_size(request, channels, n_channels);
9623 	size = size_add(size, array_size(sizeof(*request->ssids), n_ssids));
9624 	size = size_add(size, array_size(sizeof(*request->match_sets),
9625 					 n_match_sets));
9626 	size = size_add(size, array_size(sizeof(*request->scan_plans),
9627 					 n_plans));
9628 	size = size_add(size, ie_len);
9629 	request = kzalloc(size, GFP_KERNEL);
9630 	if (!request)
9631 		return ERR_PTR(-ENOMEM);
9632 
9633 	if (n_ssids)
9634 		request->ssids = (void *)&request->channels[n_channels];
9635 	request->n_ssids = n_ssids;
9636 	if (ie_len) {
9637 		if (n_ssids)
9638 			request->ie = (void *)(request->ssids + n_ssids);
9639 		else
9640 			request->ie = (void *)(request->channels + n_channels);
9641 	}
9642 
9643 	if (n_match_sets) {
9644 		if (request->ie)
9645 			request->match_sets = (void *)(request->ie + ie_len);
9646 		else if (n_ssids)
9647 			request->match_sets =
9648 				(void *)(request->ssids + n_ssids);
9649 		else
9650 			request->match_sets =
9651 				(void *)(request->channels + n_channels);
9652 	}
9653 	request->n_match_sets = n_match_sets;
9654 
9655 	if (n_match_sets)
9656 		request->scan_plans = (void *)(request->match_sets +
9657 					       n_match_sets);
9658 	else if (request->ie)
9659 		request->scan_plans = (void *)(request->ie + ie_len);
9660 	else if (n_ssids)
9661 		request->scan_plans = (void *)(request->ssids + n_ssids);
9662 	else
9663 		request->scan_plans = (void *)(request->channels + n_channels);
9664 
9665 	request->n_scan_plans = n_plans;
9666 
9667 	i = 0;
9668 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9669 		/* user specified, bail out if channel not found */
9670 		nla_for_each_nested(attr,
9671 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
9672 				    tmp) {
9673 			struct ieee80211_channel *chan;
9674 
9675 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
9676 
9677 			if (!chan) {
9678 				err = -EINVAL;
9679 				goto out_free;
9680 			}
9681 
9682 			/* ignore disabled channels */
9683 			if (chan->flags & IEEE80211_CHAN_DISABLED)
9684 				continue;
9685 
9686 			request->channels[i] = chan;
9687 			i++;
9688 		}
9689 	} else {
9690 		/* all channels */
9691 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
9692 			int j;
9693 
9694 			if (!wiphy->bands[band])
9695 				continue;
9696 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9697 				struct ieee80211_channel *chan;
9698 
9699 				chan = &wiphy->bands[band]->channels[j];
9700 
9701 				if (chan->flags & IEEE80211_CHAN_DISABLED)
9702 					continue;
9703 
9704 				request->channels[i] = chan;
9705 				i++;
9706 			}
9707 		}
9708 	}
9709 
9710 	if (!i) {
9711 		err = -EINVAL;
9712 		goto out_free;
9713 	}
9714 
9715 	request->n_channels = i;
9716 
9717 	i = 0;
9718 	if (n_ssids) {
9719 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9720 				    tmp) {
9721 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9722 				err = -EINVAL;
9723 				goto out_free;
9724 			}
9725 			request->ssids[i].ssid_len = nla_len(attr);
9726 			memcpy(request->ssids[i].ssid, nla_data(attr),
9727 			       nla_len(attr));
9728 			i++;
9729 		}
9730 	}
9731 
9732 	i = 0;
9733 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9734 		nla_for_each_nested(attr,
9735 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9736 				    tmp) {
9737 			struct nlattr *ssid, *bssid, *rssi;
9738 
9739 			err = nla_parse_nested_deprecated(tb,
9740 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9741 							  attr,
9742 							  nl80211_match_policy,
9743 							  NULL);
9744 			if (err)
9745 				goto out_free;
9746 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
9747 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
9748 
9749 			if (!ssid && !bssid) {
9750 				i++;
9751 				continue;
9752 			}
9753 
9754 			if (WARN_ON(i >= n_match_sets)) {
9755 				/* this indicates a programming error,
9756 				 * the loop above should have verified
9757 				 * things properly
9758 				 */
9759 				err = -EINVAL;
9760 				goto out_free;
9761 			}
9762 
9763 			if (ssid) {
9764 				memcpy(request->match_sets[i].ssid.ssid,
9765 				       nla_data(ssid), nla_len(ssid));
9766 				request->match_sets[i].ssid.ssid_len =
9767 					nla_len(ssid);
9768 			}
9769 			if (bssid)
9770 				memcpy(request->match_sets[i].bssid,
9771 				       nla_data(bssid), ETH_ALEN);
9772 
9773 			/* special attribute - old implementation w/a */
9774 			request->match_sets[i].rssi_thold = default_match_rssi;
9775 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9776 			if (rssi)
9777 				request->match_sets[i].rssi_thold =
9778 					nla_get_s32(rssi);
9779 
9780 			/* Parse per band RSSI attribute */
9781 			err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
9782 				&request->match_sets[i],
9783 				tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
9784 				request->match_sets[i].rssi_thold);
9785 			if (err)
9786 				goto out_free;
9787 
9788 			i++;
9789 		}
9790 
9791 		/* there was no other matchset, so the RSSI one is alone */
9792 		if (i == 0 && n_match_sets)
9793 			request->match_sets[0].rssi_thold = default_match_rssi;
9794 
9795 		request->min_rssi_thold = INT_MAX;
9796 		for (i = 0; i < n_match_sets; i++)
9797 			request->min_rssi_thold =
9798 				min(request->match_sets[i].rssi_thold,
9799 				    request->min_rssi_thold);
9800 	} else {
9801 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
9802 	}
9803 
9804 	if (ie_len) {
9805 		request->ie_len = ie_len;
9806 		memcpy((void *)request->ie,
9807 		       nla_data(attrs[NL80211_ATTR_IE]),
9808 		       request->ie_len);
9809 	}
9810 
9811 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
9812 	if (err)
9813 		goto out_free;
9814 
9815 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
9816 		request->delay =
9817 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
9818 
9819 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
9820 		request->relative_rssi = nla_get_s8(
9821 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
9822 		request->relative_rssi_set = true;
9823 	}
9824 
9825 	if (request->relative_rssi_set &&
9826 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
9827 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
9828 
9829 		rssi_adjust = nla_data(
9830 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
9831 		request->rssi_adjust.band = rssi_adjust->band;
9832 		request->rssi_adjust.delta = rssi_adjust->delta;
9833 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
9834 			err = -EINVAL;
9835 			goto out_free;
9836 		}
9837 	}
9838 
9839 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
9840 	if (err)
9841 		goto out_free;
9842 
9843 	request->scan_start = jiffies;
9844 
9845 	return request;
9846 
9847 out_free:
9848 	kfree(request);
9849 	return ERR_PTR(err);
9850 }
9851 
9852 static int nl80211_start_sched_scan(struct sk_buff *skb,
9853 				    struct genl_info *info)
9854 {
9855 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9856 	struct net_device *dev = info->user_ptr[1];
9857 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9858 	struct cfg80211_sched_scan_request *sched_scan_req;
9859 	bool want_multi;
9860 	int err;
9861 
9862 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
9863 		return -EOPNOTSUPP;
9864 
9865 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
9866 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
9867 	if (err)
9868 		return err;
9869 
9870 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
9871 						  info->attrs,
9872 						  rdev->wiphy.max_match_sets);
9873 
9874 	err = PTR_ERR_OR_ZERO(sched_scan_req);
9875 	if (err)
9876 		goto out_err;
9877 
9878 	/* leave request id zero for legacy request
9879 	 * or if driver does not support multi-scheduled scan
9880 	 */
9881 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1)
9882 		sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
9883 
9884 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
9885 	if (err)
9886 		goto out_free;
9887 
9888 	sched_scan_req->dev = dev;
9889 	sched_scan_req->wiphy = &rdev->wiphy;
9890 
9891 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9892 		sched_scan_req->owner_nlportid = info->snd_portid;
9893 
9894 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
9895 
9896 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
9897 	return 0;
9898 
9899 out_free:
9900 	kfree(sched_scan_req);
9901 out_err:
9902 	return err;
9903 }
9904 
9905 static int nl80211_stop_sched_scan(struct sk_buff *skb,
9906 				   struct genl_info *info)
9907 {
9908 	struct cfg80211_sched_scan_request *req;
9909 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9910 	u64 cookie;
9911 
9912 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
9913 		return -EOPNOTSUPP;
9914 
9915 	if (info->attrs[NL80211_ATTR_COOKIE]) {
9916 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9917 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
9918 	}
9919 
9920 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
9921 				     struct cfg80211_sched_scan_request,
9922 				     list);
9923 	if (!req || req->reqid ||
9924 	    (req->owner_nlportid &&
9925 	     req->owner_nlportid != info->snd_portid))
9926 		return -ENOENT;
9927 
9928 	return cfg80211_stop_sched_scan_req(rdev, req, false);
9929 }
9930 
9931 static int nl80211_start_radar_detection(struct sk_buff *skb,
9932 					 struct genl_info *info)
9933 {
9934 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9935 	struct net_device *dev = info->user_ptr[1];
9936 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9937 	struct wiphy *wiphy = wdev->wiphy;
9938 	struct cfg80211_chan_def chandef;
9939 	enum nl80211_dfs_regions dfs_region;
9940 	unsigned int cac_time_ms;
9941 	int err = -EINVAL;
9942 
9943 	flush_delayed_work(&rdev->dfs_update_channels_wk);
9944 
9945 	wiphy_lock(wiphy);
9946 
9947 	dfs_region = reg_get_dfs_region(wiphy);
9948 	if (dfs_region == NL80211_DFS_UNSET)
9949 		goto unlock;
9950 
9951 	err = nl80211_parse_chandef(rdev, info, &chandef);
9952 	if (err)
9953 		goto unlock;
9954 
9955 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
9956 	if (err < 0)
9957 		goto unlock;
9958 
9959 	if (err == 0) {
9960 		err = -EINVAL;
9961 		goto unlock;
9962 	}
9963 
9964 	if (!cfg80211_chandef_dfs_usable(wiphy, &chandef)) {
9965 		err = -EINVAL;
9966 		goto unlock;
9967 	}
9968 
9969 	if (nla_get_flag(info->attrs[NL80211_ATTR_RADAR_BACKGROUND])) {
9970 		err = cfg80211_start_background_radar_detection(rdev, wdev,
9971 								&chandef);
9972 		goto unlock;
9973 	}
9974 
9975 	if (netif_carrier_ok(dev)) {
9976 		err = -EBUSY;
9977 		goto unlock;
9978 	}
9979 
9980 	if (wdev->cac_started) {
9981 		err = -EBUSY;
9982 		goto unlock;
9983 	}
9984 
9985 	/* CAC start is offloaded to HW and can't be started manually */
9986 	if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD)) {
9987 		err = -EOPNOTSUPP;
9988 		goto unlock;
9989 	}
9990 
9991 	if (!rdev->ops->start_radar_detection) {
9992 		err = -EOPNOTSUPP;
9993 		goto unlock;
9994 	}
9995 
9996 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
9997 	if (WARN_ON(!cac_time_ms))
9998 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
9999 
10000 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
10001 	if (!err) {
10002 		wdev->links[0].ap.chandef = chandef;
10003 		wdev->cac_started = true;
10004 		wdev->cac_start_time = jiffies;
10005 		wdev->cac_time_ms = cac_time_ms;
10006 	}
10007 unlock:
10008 	wiphy_unlock(wiphy);
10009 
10010 	return err;
10011 }
10012 
10013 static int nl80211_notify_radar_detection(struct sk_buff *skb,
10014 					  struct genl_info *info)
10015 {
10016 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10017 	struct net_device *dev = info->user_ptr[1];
10018 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10019 	struct wiphy *wiphy = wdev->wiphy;
10020 	struct cfg80211_chan_def chandef;
10021 	enum nl80211_dfs_regions dfs_region;
10022 	int err;
10023 
10024 	dfs_region = reg_get_dfs_region(wiphy);
10025 	if (dfs_region == NL80211_DFS_UNSET) {
10026 		GENL_SET_ERR_MSG(info,
10027 				 "DFS Region is not set. Unexpected Radar indication");
10028 		return -EINVAL;
10029 	}
10030 
10031 	err = nl80211_parse_chandef(rdev, info, &chandef);
10032 	if (err) {
10033 		GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
10034 		return err;
10035 	}
10036 
10037 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
10038 	if (err < 0) {
10039 		GENL_SET_ERR_MSG(info, "chandef is invalid");
10040 		return err;
10041 	}
10042 
10043 	if (err == 0) {
10044 		GENL_SET_ERR_MSG(info,
10045 				 "Unexpected Radar indication for chandef/iftype");
10046 		return -EINVAL;
10047 	}
10048 
10049 	/* Do not process this notification if radar is already detected
10050 	 * by kernel on this channel, and return success.
10051 	 */
10052 	if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
10053 		return 0;
10054 
10055 	cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
10056 
10057 	cfg80211_sched_dfs_chan_update(rdev);
10058 
10059 	rdev->radar_chandef = chandef;
10060 
10061 	/* Propagate this notification to other radios as well */
10062 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
10063 
10064 	return 0;
10065 }
10066 
10067 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
10068 {
10069 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10070 	unsigned int link_id = nl80211_link_id(info->attrs);
10071 	struct net_device *dev = info->user_ptr[1];
10072 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10073 	struct cfg80211_csa_settings params;
10074 	struct nlattr **csa_attrs = NULL;
10075 	int err;
10076 	bool need_new_beacon = false;
10077 	bool need_handle_dfs_flag = true;
10078 	int len, i;
10079 	u32 cs_count;
10080 
10081 	if (!rdev->ops->channel_switch ||
10082 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
10083 		return -EOPNOTSUPP;
10084 
10085 	switch (dev->ieee80211_ptr->iftype) {
10086 	case NL80211_IFTYPE_AP:
10087 	case NL80211_IFTYPE_P2P_GO:
10088 		need_new_beacon = true;
10089 		/* For all modes except AP the handle_dfs flag needs to be
10090 		 * supplied to tell the kernel that userspace will handle radar
10091 		 * events when they happen. Otherwise a switch to a channel
10092 		 * requiring DFS will be rejected.
10093 		 */
10094 		need_handle_dfs_flag = false;
10095 
10096 		/* useless if AP is not running */
10097 		if (!wdev->links[link_id].ap.beacon_interval)
10098 			return -ENOTCONN;
10099 		break;
10100 	case NL80211_IFTYPE_ADHOC:
10101 		if (!wdev->u.ibss.ssid_len)
10102 			return -ENOTCONN;
10103 		break;
10104 	case NL80211_IFTYPE_MESH_POINT:
10105 		if (!wdev->u.mesh.id_len)
10106 			return -ENOTCONN;
10107 		break;
10108 	default:
10109 		return -EOPNOTSUPP;
10110 	}
10111 
10112 	memset(&params, 0, sizeof(params));
10113 	params.beacon_csa.ftm_responder = -1;
10114 
10115 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
10116 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
10117 		return -EINVAL;
10118 
10119 	/* only important for AP, IBSS and mesh create IEs internally */
10120 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
10121 		return -EINVAL;
10122 
10123 	/* Even though the attribute is u32, the specification says
10124 	 * u8, so let's make sure we don't overflow.
10125 	 */
10126 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
10127 	if (cs_count > 255)
10128 		return -EINVAL;
10129 
10130 	params.count = cs_count;
10131 
10132 	if (!need_new_beacon)
10133 		goto skip_beacons;
10134 
10135 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after,
10136 				   info->extack);
10137 	if (err)
10138 		goto free;
10139 
10140 	csa_attrs = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*csa_attrs),
10141 			    GFP_KERNEL);
10142 	if (!csa_attrs) {
10143 		err = -ENOMEM;
10144 		goto free;
10145 	}
10146 
10147 	err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
10148 					  info->attrs[NL80211_ATTR_CSA_IES],
10149 					  nl80211_policy, info->extack);
10150 	if (err)
10151 		goto free;
10152 
10153 	err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa,
10154 				   info->extack);
10155 	if (err)
10156 		goto free;
10157 
10158 	if (!csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
10159 		err = -EINVAL;
10160 		goto free;
10161 	}
10162 
10163 	len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
10164 	if (!len || (len % sizeof(u16))) {
10165 		err = -EINVAL;
10166 		goto free;
10167 	}
10168 
10169 	params.n_counter_offsets_beacon = len / sizeof(u16);
10170 	if (rdev->wiphy.max_num_csa_counters &&
10171 	    (params.n_counter_offsets_beacon >
10172 	     rdev->wiphy.max_num_csa_counters)) {
10173 		err = -EINVAL;
10174 		goto free;
10175 	}
10176 
10177 	params.counter_offsets_beacon =
10178 		nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
10179 
10180 	/* sanity checks - counters should fit and be the same */
10181 	for (i = 0; i < params.n_counter_offsets_beacon; i++) {
10182 		u16 offset = params.counter_offsets_beacon[i];
10183 
10184 		if (offset >= params.beacon_csa.tail_len) {
10185 			err = -EINVAL;
10186 			goto free;
10187 		}
10188 
10189 		if (params.beacon_csa.tail[offset] != params.count) {
10190 			err = -EINVAL;
10191 			goto free;
10192 		}
10193 	}
10194 
10195 	if (csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
10196 		len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
10197 		if (!len || (len % sizeof(u16))) {
10198 			err = -EINVAL;
10199 			goto free;
10200 		}
10201 
10202 		params.n_counter_offsets_presp = len / sizeof(u16);
10203 		if (rdev->wiphy.max_num_csa_counters &&
10204 		    (params.n_counter_offsets_presp >
10205 		     rdev->wiphy.max_num_csa_counters)) {
10206 			err = -EINVAL;
10207 			goto free;
10208 		}
10209 
10210 		params.counter_offsets_presp =
10211 			nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
10212 
10213 		/* sanity checks - counters should fit and be the same */
10214 		for (i = 0; i < params.n_counter_offsets_presp; i++) {
10215 			u16 offset = params.counter_offsets_presp[i];
10216 
10217 			if (offset >= params.beacon_csa.probe_resp_len) {
10218 				err = -EINVAL;
10219 				goto free;
10220 			}
10221 
10222 			if (params.beacon_csa.probe_resp[offset] !=
10223 			    params.count) {
10224 				err = -EINVAL;
10225 				goto free;
10226 			}
10227 		}
10228 	}
10229 
10230 skip_beacons:
10231 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
10232 	if (err)
10233 		goto free;
10234 
10235 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
10236 					   wdev->iftype)) {
10237 		err = -EINVAL;
10238 		goto free;
10239 	}
10240 
10241 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
10242 					    &params.chandef,
10243 					    wdev->iftype);
10244 	if (err < 0)
10245 		goto free;
10246 
10247 	if (err > 0) {
10248 		params.radar_required = true;
10249 		if (need_handle_dfs_flag &&
10250 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
10251 			err = -EINVAL;
10252 			goto free;
10253 		}
10254 	}
10255 
10256 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
10257 		params.block_tx = true;
10258 
10259 	if (info->attrs[NL80211_ATTR_PUNCT_BITMAP]) {
10260 		err = nl80211_parse_punct_bitmap(rdev, info,
10261 						 &params.chandef,
10262 						 &params.punct_bitmap);
10263 		if (err)
10264 			goto free;
10265 	}
10266 
10267 	wdev_lock(wdev);
10268 	err = rdev_channel_switch(rdev, dev, &params);
10269 	wdev_unlock(wdev);
10270 
10271 free:
10272 	kfree(params.beacon_after.mbssid_ies);
10273 	kfree(params.beacon_csa.mbssid_ies);
10274 	kfree(params.beacon_after.rnr_ies);
10275 	kfree(params.beacon_csa.rnr_ies);
10276 	kfree(csa_attrs);
10277 	return err;
10278 }
10279 
10280 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
10281 			    u32 seq, int flags,
10282 			    struct cfg80211_registered_device *rdev,
10283 			    struct wireless_dev *wdev,
10284 			    struct cfg80211_internal_bss *intbss)
10285 {
10286 	struct cfg80211_bss *res = &intbss->pub;
10287 	const struct cfg80211_bss_ies *ies;
10288 	unsigned int link_id;
10289 	void *hdr;
10290 	struct nlattr *bss;
10291 
10292 	ASSERT_WDEV_LOCK(wdev);
10293 
10294 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
10295 			     NL80211_CMD_NEW_SCAN_RESULTS);
10296 	if (!hdr)
10297 		return -1;
10298 
10299 	genl_dump_check_consistent(cb, hdr);
10300 
10301 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
10302 		goto nla_put_failure;
10303 	if (wdev->netdev &&
10304 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
10305 		goto nla_put_failure;
10306 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
10307 			      NL80211_ATTR_PAD))
10308 		goto nla_put_failure;
10309 
10310 	bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
10311 	if (!bss)
10312 		goto nla_put_failure;
10313 	if ((!is_zero_ether_addr(res->bssid) &&
10314 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
10315 		goto nla_put_failure;
10316 
10317 	rcu_read_lock();
10318 	/* indicate whether we have probe response data or not */
10319 	if (rcu_access_pointer(res->proberesp_ies) &&
10320 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
10321 		goto fail_unlock_rcu;
10322 
10323 	/* this pointer prefers to be pointed to probe response data
10324 	 * but is always valid
10325 	 */
10326 	ies = rcu_dereference(res->ies);
10327 	if (ies) {
10328 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
10329 				      NL80211_BSS_PAD))
10330 			goto fail_unlock_rcu;
10331 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
10332 					ies->len, ies->data))
10333 			goto fail_unlock_rcu;
10334 	}
10335 
10336 	/* and this pointer is always (unless driver didn't know) beacon data */
10337 	ies = rcu_dereference(res->beacon_ies);
10338 	if (ies && ies->from_beacon) {
10339 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
10340 				      NL80211_BSS_PAD))
10341 			goto fail_unlock_rcu;
10342 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
10343 					ies->len, ies->data))
10344 			goto fail_unlock_rcu;
10345 	}
10346 	rcu_read_unlock();
10347 
10348 	if (res->beacon_interval &&
10349 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
10350 		goto nla_put_failure;
10351 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
10352 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
10353 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY_OFFSET,
10354 			res->channel->freq_offset) ||
10355 	    nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
10356 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
10357 			jiffies_to_msecs(jiffies - intbss->ts)))
10358 		goto nla_put_failure;
10359 
10360 	if (intbss->parent_tsf &&
10361 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
10362 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
10363 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
10364 		     intbss->parent_bssid)))
10365 		goto nla_put_failure;
10366 
10367 	if (intbss->ts_boottime &&
10368 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
10369 			      intbss->ts_boottime, NL80211_BSS_PAD))
10370 		goto nla_put_failure;
10371 
10372 	if (!nl80211_put_signal(msg, intbss->pub.chains,
10373 				intbss->pub.chain_signal,
10374 				NL80211_BSS_CHAIN_SIGNAL))
10375 		goto nla_put_failure;
10376 
10377 	switch (rdev->wiphy.signal_type) {
10378 	case CFG80211_SIGNAL_TYPE_MBM:
10379 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
10380 			goto nla_put_failure;
10381 		break;
10382 	case CFG80211_SIGNAL_TYPE_UNSPEC:
10383 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
10384 			goto nla_put_failure;
10385 		break;
10386 	default:
10387 		break;
10388 	}
10389 
10390 	switch (wdev->iftype) {
10391 	case NL80211_IFTYPE_P2P_CLIENT:
10392 	case NL80211_IFTYPE_STATION:
10393 		for_each_valid_link(wdev, link_id) {
10394 			if (intbss == wdev->links[link_id].client.current_bss &&
10395 			    (nla_put_u32(msg, NL80211_BSS_STATUS,
10396 					 NL80211_BSS_STATUS_ASSOCIATED) ||
10397 			     (wdev->valid_links &&
10398 			      (nla_put_u8(msg, NL80211_BSS_MLO_LINK_ID,
10399 					  link_id) ||
10400 			       nla_put(msg, NL80211_BSS_MLD_ADDR, ETH_ALEN,
10401 				       wdev->u.client.connected_addr)))))
10402 				goto nla_put_failure;
10403 		}
10404 		break;
10405 	case NL80211_IFTYPE_ADHOC:
10406 		if (intbss == wdev->u.ibss.current_bss &&
10407 		    nla_put_u32(msg, NL80211_BSS_STATUS,
10408 				NL80211_BSS_STATUS_IBSS_JOINED))
10409 			goto nla_put_failure;
10410 		break;
10411 	default:
10412 		break;
10413 	}
10414 
10415 	nla_nest_end(msg, bss);
10416 
10417 	genlmsg_end(msg, hdr);
10418 	return 0;
10419 
10420  fail_unlock_rcu:
10421 	rcu_read_unlock();
10422  nla_put_failure:
10423 	genlmsg_cancel(msg, hdr);
10424 	return -EMSGSIZE;
10425 }
10426 
10427 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
10428 {
10429 	struct cfg80211_registered_device *rdev;
10430 	struct cfg80211_internal_bss *scan;
10431 	struct wireless_dev *wdev;
10432 	int start = cb->args[2], idx = 0;
10433 	int err;
10434 
10435 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
10436 	if (err)
10437 		return err;
10438 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10439 	__acquire(&rdev->wiphy.mtx);
10440 
10441 	wdev_lock(wdev);
10442 	spin_lock_bh(&rdev->bss_lock);
10443 
10444 	/*
10445 	 * dump_scan will be called multiple times to break up the scan results
10446 	 * into multiple messages.  It is unlikely that any more bss-es will be
10447 	 * expired after the first call, so only call only call this on the
10448 	 * first dump_scan invocation.
10449 	 */
10450 	if (start == 0)
10451 		cfg80211_bss_expire(rdev);
10452 
10453 	cb->seq = rdev->bss_generation;
10454 
10455 	list_for_each_entry(scan, &rdev->bss_list, list) {
10456 		if (++idx <= start)
10457 			continue;
10458 		if (nl80211_send_bss(skb, cb,
10459 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10460 				rdev, wdev, scan) < 0) {
10461 			idx--;
10462 			break;
10463 		}
10464 	}
10465 
10466 	spin_unlock_bh(&rdev->bss_lock);
10467 	wdev_unlock(wdev);
10468 
10469 	cb->args[2] = idx;
10470 	wiphy_unlock(&rdev->wiphy);
10471 
10472 	return skb->len;
10473 }
10474 
10475 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
10476 			       int flags, struct net_device *dev,
10477 			       bool allow_radio_stats,
10478 			       struct survey_info *survey)
10479 {
10480 	void *hdr;
10481 	struct nlattr *infoattr;
10482 
10483 	/* skip radio stats if userspace didn't request them */
10484 	if (!survey->channel && !allow_radio_stats)
10485 		return 0;
10486 
10487 	hdr = nl80211hdr_put(msg, portid, seq, flags,
10488 			     NL80211_CMD_NEW_SURVEY_RESULTS);
10489 	if (!hdr)
10490 		return -ENOMEM;
10491 
10492 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
10493 		goto nla_put_failure;
10494 
10495 	infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
10496 	if (!infoattr)
10497 		goto nla_put_failure;
10498 
10499 	if (survey->channel &&
10500 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
10501 			survey->channel->center_freq))
10502 		goto nla_put_failure;
10503 
10504 	if (survey->channel && survey->channel->freq_offset &&
10505 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY_OFFSET,
10506 			survey->channel->freq_offset))
10507 		goto nla_put_failure;
10508 
10509 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
10510 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
10511 		goto nla_put_failure;
10512 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
10513 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
10514 		goto nla_put_failure;
10515 	if ((survey->filled & SURVEY_INFO_TIME) &&
10516 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
10517 			survey->time, NL80211_SURVEY_INFO_PAD))
10518 		goto nla_put_failure;
10519 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
10520 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
10521 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
10522 		goto nla_put_failure;
10523 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
10524 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
10525 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
10526 		goto nla_put_failure;
10527 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
10528 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
10529 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
10530 		goto nla_put_failure;
10531 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
10532 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
10533 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
10534 		goto nla_put_failure;
10535 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
10536 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
10537 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
10538 		goto nla_put_failure;
10539 	if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
10540 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
10541 			      survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
10542 		goto nla_put_failure;
10543 
10544 	nla_nest_end(msg, infoattr);
10545 
10546 	genlmsg_end(msg, hdr);
10547 	return 0;
10548 
10549  nla_put_failure:
10550 	genlmsg_cancel(msg, hdr);
10551 	return -EMSGSIZE;
10552 }
10553 
10554 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
10555 {
10556 	struct nlattr **attrbuf;
10557 	struct survey_info survey;
10558 	struct cfg80211_registered_device *rdev;
10559 	struct wireless_dev *wdev;
10560 	int survey_idx = cb->args[2];
10561 	int res;
10562 	bool radio_stats;
10563 
10564 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
10565 	if (!attrbuf)
10566 		return -ENOMEM;
10567 
10568 	res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, attrbuf);
10569 	if (res) {
10570 		kfree(attrbuf);
10571 		return res;
10572 	}
10573 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10574 	__acquire(&rdev->wiphy.mtx);
10575 
10576 	/* prepare_wdev_dump parsed the attributes */
10577 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
10578 
10579 	if (!wdev->netdev) {
10580 		res = -EINVAL;
10581 		goto out_err;
10582 	}
10583 
10584 	if (!rdev->ops->dump_survey) {
10585 		res = -EOPNOTSUPP;
10586 		goto out_err;
10587 	}
10588 
10589 	while (1) {
10590 		wdev_lock(wdev);
10591 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
10592 		wdev_unlock(wdev);
10593 		if (res == -ENOENT)
10594 			break;
10595 		if (res)
10596 			goto out_err;
10597 
10598 		/* don't send disabled channels, but do send non-channel data */
10599 		if (survey.channel &&
10600 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
10601 			survey_idx++;
10602 			continue;
10603 		}
10604 
10605 		if (nl80211_send_survey(skb,
10606 				NETLINK_CB(cb->skb).portid,
10607 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10608 				wdev->netdev, radio_stats, &survey) < 0)
10609 			goto out;
10610 		survey_idx++;
10611 	}
10612 
10613  out:
10614 	cb->args[2] = survey_idx;
10615 	res = skb->len;
10616  out_err:
10617 	kfree(attrbuf);
10618 	wiphy_unlock(&rdev->wiphy);
10619 	return res;
10620 }
10621 
10622 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
10623 {
10624 	return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
10625 				  NL80211_WPA_VERSION_2 |
10626 				  NL80211_WPA_VERSION_3));
10627 }
10628 
10629 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
10630 {
10631 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10632 	struct net_device *dev = info->user_ptr[1];
10633 	struct ieee80211_channel *chan;
10634 	const u8 *bssid, *ssid;
10635 	int err, ssid_len;
10636 	enum nl80211_auth_type auth_type;
10637 	struct key_parse key;
10638 	bool local_state_change;
10639 	struct cfg80211_auth_request req = {};
10640 	u32 freq;
10641 
10642 	if (!info->attrs[NL80211_ATTR_MAC])
10643 		return -EINVAL;
10644 
10645 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
10646 		return -EINVAL;
10647 
10648 	if (!info->attrs[NL80211_ATTR_SSID])
10649 		return -EINVAL;
10650 
10651 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10652 		return -EINVAL;
10653 
10654 	err = nl80211_parse_key(info, &key);
10655 	if (err)
10656 		return err;
10657 
10658 	if (key.idx >= 0) {
10659 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
10660 			return -EINVAL;
10661 		if (!key.p.key || !key.p.key_len)
10662 			return -EINVAL;
10663 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
10664 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
10665 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
10666 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
10667 			return -EINVAL;
10668 		if (key.idx > 3)
10669 			return -EINVAL;
10670 	} else {
10671 		key.p.key_len = 0;
10672 		key.p.key = NULL;
10673 	}
10674 
10675 	if (key.idx >= 0) {
10676 		int i;
10677 		bool ok = false;
10678 
10679 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
10680 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
10681 				ok = true;
10682 				break;
10683 			}
10684 		}
10685 		if (!ok)
10686 			return -EINVAL;
10687 	}
10688 
10689 	if (!rdev->ops->auth)
10690 		return -EOPNOTSUPP;
10691 
10692 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10693 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10694 		return -EOPNOTSUPP;
10695 
10696 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10697 	freq = MHZ_TO_KHZ(nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
10698 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10699 		freq +=
10700 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10701 
10702 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10703 	if (!chan)
10704 		return -EINVAL;
10705 
10706 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10707 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10708 
10709 	if (info->attrs[NL80211_ATTR_IE]) {
10710 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10711 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10712 	}
10713 
10714 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10715 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
10716 		return -EINVAL;
10717 
10718 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
10719 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
10720 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
10721 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
10722 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
10723 		return -EINVAL;
10724 
10725 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
10726 		if (auth_type != NL80211_AUTHTYPE_SAE &&
10727 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
10728 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
10729 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
10730 			return -EINVAL;
10731 		req.auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
10732 		req.auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
10733 	}
10734 
10735 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
10736 
10737 	/*
10738 	 * Since we no longer track auth state, ignore
10739 	 * requests to only change local state.
10740 	 */
10741 	if (local_state_change)
10742 		return 0;
10743 
10744 	req.auth_type = auth_type;
10745 	req.key = key.p.key;
10746 	req.key_len = key.p.key_len;
10747 	req.key_idx = key.idx;
10748 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
10749 	if (req.link_id >= 0) {
10750 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
10751 			return -EINVAL;
10752 		if (!info->attrs[NL80211_ATTR_MLD_ADDR])
10753 			return -EINVAL;
10754 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
10755 		if (!is_valid_ether_addr(req.ap_mld_addr))
10756 			return -EINVAL;
10757 	}
10758 
10759 	req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
10760 				   IEEE80211_BSS_TYPE_ESS,
10761 				   IEEE80211_PRIVACY_ANY);
10762 	if (!req.bss)
10763 		return -ENOENT;
10764 
10765 	wdev_lock(dev->ieee80211_ptr);
10766 	err = cfg80211_mlme_auth(rdev, dev, &req);
10767 	wdev_unlock(dev->ieee80211_ptr);
10768 
10769 	cfg80211_put_bss(&rdev->wiphy, req.bss);
10770 
10771 	return err;
10772 }
10773 
10774 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
10775 				     struct genl_info *info)
10776 {
10777 	if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10778 		GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
10779 		return -EINVAL;
10780 	}
10781 
10782 	if (!rdev->ops->tx_control_port ||
10783 	    !wiphy_ext_feature_isset(&rdev->wiphy,
10784 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
10785 		return -EOPNOTSUPP;
10786 
10787 	return 0;
10788 }
10789 
10790 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
10791 				   struct genl_info *info,
10792 				   struct cfg80211_crypto_settings *settings,
10793 				   int cipher_limit)
10794 {
10795 	memset(settings, 0, sizeof(*settings));
10796 
10797 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
10798 
10799 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
10800 		u16 proto;
10801 
10802 		proto = nla_get_u16(
10803 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
10804 		settings->control_port_ethertype = cpu_to_be16(proto);
10805 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
10806 		    proto != ETH_P_PAE)
10807 			return -EINVAL;
10808 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
10809 			settings->control_port_no_encrypt = true;
10810 	} else
10811 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
10812 
10813 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
10814 		int r = validate_pae_over_nl80211(rdev, info);
10815 
10816 		if (r < 0)
10817 			return r;
10818 
10819 		settings->control_port_over_nl80211 = true;
10820 
10821 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH])
10822 			settings->control_port_no_preauth = true;
10823 	}
10824 
10825 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
10826 		void *data;
10827 		int len, i;
10828 
10829 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10830 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10831 		settings->n_ciphers_pairwise = len / sizeof(u32);
10832 
10833 		if (len % sizeof(u32))
10834 			return -EINVAL;
10835 
10836 		if (settings->n_ciphers_pairwise > cipher_limit)
10837 			return -EINVAL;
10838 
10839 		memcpy(settings->ciphers_pairwise, data, len);
10840 
10841 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
10842 			if (!cfg80211_supported_cipher_suite(
10843 					&rdev->wiphy,
10844 					settings->ciphers_pairwise[i]))
10845 				return -EINVAL;
10846 	}
10847 
10848 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
10849 		settings->cipher_group =
10850 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
10851 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
10852 						     settings->cipher_group))
10853 			return -EINVAL;
10854 	}
10855 
10856 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
10857 		settings->wpa_versions =
10858 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
10859 		if (!nl80211_valid_wpa_versions(settings->wpa_versions))
10860 			return -EINVAL;
10861 	}
10862 
10863 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
10864 		void *data;
10865 		int len;
10866 
10867 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
10868 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
10869 		settings->n_akm_suites = len / sizeof(u32);
10870 
10871 		if (len % sizeof(u32))
10872 			return -EINVAL;
10873 
10874 		if (settings->n_akm_suites > rdev->wiphy.max_num_akm_suites)
10875 			return -EINVAL;
10876 
10877 		memcpy(settings->akm_suites, data, len);
10878 	}
10879 
10880 	if (info->attrs[NL80211_ATTR_PMK]) {
10881 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
10882 			return -EINVAL;
10883 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10884 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK) &&
10885 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10886 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_AP_PSK))
10887 			return -EINVAL;
10888 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10889 	}
10890 
10891 	if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
10892 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10893 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
10894 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10895 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP))
10896 			return -EINVAL;
10897 		settings->sae_pwd =
10898 			nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10899 		settings->sae_pwd_len =
10900 			nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10901 	}
10902 
10903 	if (info->attrs[NL80211_ATTR_SAE_PWE])
10904 		settings->sae_pwe =
10905 			nla_get_u8(info->attrs[NL80211_ATTR_SAE_PWE]);
10906 	else
10907 		settings->sae_pwe = NL80211_SAE_PWE_UNSPECIFIED;
10908 
10909 	return 0;
10910 }
10911 
10912 static struct cfg80211_bss *nl80211_assoc_bss(struct cfg80211_registered_device *rdev,
10913 					      const u8 *ssid, int ssid_len,
10914 					      struct nlattr **attrs)
10915 {
10916 	struct ieee80211_channel *chan;
10917 	struct cfg80211_bss *bss;
10918 	const u8 *bssid;
10919 	u32 freq;
10920 
10921 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_WIPHY_FREQ])
10922 		return ERR_PTR(-EINVAL);
10923 
10924 	bssid = nla_data(attrs[NL80211_ATTR_MAC]);
10925 
10926 	freq = MHZ_TO_KHZ(nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ]));
10927 	if (attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10928 		freq += nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10929 
10930 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10931 	if (!chan)
10932 		return ERR_PTR(-EINVAL);
10933 
10934 	bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid,
10935 			       ssid, ssid_len,
10936 			       IEEE80211_BSS_TYPE_ESS,
10937 			       IEEE80211_PRIVACY_ANY);
10938 	if (!bss)
10939 		return ERR_PTR(-ENOENT);
10940 
10941 	return bss;
10942 }
10943 
10944 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
10945 {
10946 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10947 	struct net_device *dev = info->user_ptr[1];
10948 	struct cfg80211_assoc_request req = {};
10949 	struct nlattr **attrs = NULL;
10950 	const u8 *ap_addr, *ssid;
10951 	unsigned int link_id;
10952 	int err, ssid_len;
10953 
10954 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
10955 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10956 		return -EPERM;
10957 
10958 	if (!info->attrs[NL80211_ATTR_SSID])
10959 		return -EINVAL;
10960 
10961 	if (!rdev->ops->assoc)
10962 		return -EOPNOTSUPP;
10963 
10964 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10965 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10966 		return -EOPNOTSUPP;
10967 
10968 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10969 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10970 
10971 	if (info->attrs[NL80211_ATTR_IE]) {
10972 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10973 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10974 
10975 		if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
10976 					   req.ie, req.ie_len)) {
10977 			GENL_SET_ERR_MSG(info,
10978 					 "non-inheritance makes no sense");
10979 			return -EINVAL;
10980 		}
10981 	}
10982 
10983 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
10984 		enum nl80211_mfp mfp =
10985 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
10986 		if (mfp == NL80211_MFP_REQUIRED)
10987 			req.use_mfp = true;
10988 		else if (mfp != NL80211_MFP_NO)
10989 			return -EINVAL;
10990 	}
10991 
10992 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
10993 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
10994 
10995 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
10996 		req.flags |= ASSOC_REQ_DISABLE_HT;
10997 
10998 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10999 		memcpy(&req.ht_capa_mask,
11000 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11001 		       sizeof(req.ht_capa_mask));
11002 
11003 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11004 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11005 			return -EINVAL;
11006 		memcpy(&req.ht_capa,
11007 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11008 		       sizeof(req.ht_capa));
11009 	}
11010 
11011 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
11012 		req.flags |= ASSOC_REQ_DISABLE_VHT;
11013 
11014 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
11015 		req.flags |= ASSOC_REQ_DISABLE_HE;
11016 
11017 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
11018 		req.flags |= ASSOC_REQ_DISABLE_EHT;
11019 
11020 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11021 		memcpy(&req.vht_capa_mask,
11022 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
11023 		       sizeof(req.vht_capa_mask));
11024 
11025 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
11026 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11027 			return -EINVAL;
11028 		memcpy(&req.vht_capa,
11029 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
11030 		       sizeof(req.vht_capa));
11031 	}
11032 
11033 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
11034 		if (!((rdev->wiphy.features &
11035 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
11036 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
11037 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11038 					     NL80211_EXT_FEATURE_RRM))
11039 			return -EINVAL;
11040 		req.flags |= ASSOC_REQ_USE_RRM;
11041 	}
11042 
11043 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
11044 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
11045 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
11046 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
11047 			return -EINVAL;
11048 		req.fils_nonces =
11049 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
11050 	}
11051 
11052 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]) {
11053 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY])
11054 			return -EINVAL;
11055 		memcpy(&req.s1g_capa_mask,
11056 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]),
11057 		       sizeof(req.s1g_capa_mask));
11058 	}
11059 
11060 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY]) {
11061 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK])
11062 			return -EINVAL;
11063 		memcpy(&req.s1g_capa,
11064 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY]),
11065 		       sizeof(req.s1g_capa));
11066 	}
11067 
11068 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
11069 
11070 	if (info->attrs[NL80211_ATTR_MLO_LINKS]) {
11071 		unsigned int attrsize = NUM_NL80211_ATTR * sizeof(*attrs);
11072 		struct nlattr *link;
11073 		int rem = 0;
11074 
11075 		if (req.link_id < 0)
11076 			return -EINVAL;
11077 
11078 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
11079 			return -EINVAL;
11080 
11081 		if (info->attrs[NL80211_ATTR_MAC] ||
11082 		    info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
11083 		    !info->attrs[NL80211_ATTR_MLD_ADDR])
11084 			return -EINVAL;
11085 
11086 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
11087 		ap_addr = req.ap_mld_addr;
11088 
11089 		attrs = kzalloc(attrsize, GFP_KERNEL);
11090 		if (!attrs)
11091 			return -ENOMEM;
11092 
11093 		nla_for_each_nested(link,
11094 				    info->attrs[NL80211_ATTR_MLO_LINKS],
11095 				    rem) {
11096 			memset(attrs, 0, attrsize);
11097 
11098 			nla_parse_nested(attrs, NL80211_ATTR_MAX,
11099 					 link, NULL, NULL);
11100 
11101 			if (!attrs[NL80211_ATTR_MLO_LINK_ID]) {
11102 				err = -EINVAL;
11103 				goto free;
11104 			}
11105 
11106 			link_id = nla_get_u8(attrs[NL80211_ATTR_MLO_LINK_ID]);
11107 			/* cannot use the same link ID again */
11108 			if (req.links[link_id].bss) {
11109 				err = -EINVAL;
11110 				goto free;
11111 			}
11112 			req.links[link_id].bss =
11113 				nl80211_assoc_bss(rdev, ssid, ssid_len, attrs);
11114 			if (IS_ERR(req.links[link_id].bss)) {
11115 				err = PTR_ERR(req.links[link_id].bss);
11116 				req.links[link_id].bss = NULL;
11117 				goto free;
11118 			}
11119 
11120 			if (attrs[NL80211_ATTR_IE]) {
11121 				req.links[link_id].elems =
11122 					nla_data(attrs[NL80211_ATTR_IE]);
11123 				req.links[link_id].elems_len =
11124 					nla_len(attrs[NL80211_ATTR_IE]);
11125 
11126 				if (cfg80211_find_elem(WLAN_EID_FRAGMENT,
11127 						       req.links[link_id].elems,
11128 						       req.links[link_id].elems_len)) {
11129 					GENL_SET_ERR_MSG(info,
11130 							 "cannot deal with fragmentation");
11131 					err = -EINVAL;
11132 					goto free;
11133 				}
11134 
11135 				if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
11136 							   req.links[link_id].elems,
11137 							   req.links[link_id].elems_len)) {
11138 					GENL_SET_ERR_MSG(info,
11139 							 "cannot deal with non-inheritance");
11140 					err = -EINVAL;
11141 					goto free;
11142 				}
11143 			}
11144 
11145 			req.links[link_id].disabled =
11146 				nla_get_flag(attrs[NL80211_ATTR_MLO_LINK_DISABLED]);
11147 		}
11148 
11149 		if (!req.links[req.link_id].bss) {
11150 			err = -EINVAL;
11151 			goto free;
11152 		}
11153 
11154 		if (req.links[req.link_id].elems_len) {
11155 			GENL_SET_ERR_MSG(info,
11156 					 "cannot have per-link elems on assoc link");
11157 			err = -EINVAL;
11158 			goto free;
11159 		}
11160 
11161 		if (req.links[req.link_id].disabled) {
11162 			GENL_SET_ERR_MSG(info,
11163 					 "cannot have assoc link disabled");
11164 			err = -EINVAL;
11165 			goto free;
11166 		}
11167 
11168 		kfree(attrs);
11169 		attrs = NULL;
11170 	} else {
11171 		if (req.link_id >= 0)
11172 			return -EINVAL;
11173 
11174 		req.bss = nl80211_assoc_bss(rdev, ssid, ssid_len, info->attrs);
11175 		if (IS_ERR(req.bss))
11176 			return PTR_ERR(req.bss);
11177 		ap_addr = req.bss->bssid;
11178 	}
11179 
11180 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
11181 	if (!err) {
11182 		wdev_lock(dev->ieee80211_ptr);
11183 
11184 		err = cfg80211_mlme_assoc(rdev, dev, &req);
11185 
11186 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
11187 			dev->ieee80211_ptr->conn_owner_nlportid =
11188 				info->snd_portid;
11189 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
11190 			       ap_addr, ETH_ALEN);
11191 		}
11192 
11193 		wdev_unlock(dev->ieee80211_ptr);
11194 	}
11195 
11196 free:
11197 	for (link_id = 0; link_id < ARRAY_SIZE(req.links); link_id++)
11198 		cfg80211_put_bss(&rdev->wiphy, req.links[link_id].bss);
11199 	cfg80211_put_bss(&rdev->wiphy, req.bss);
11200 	kfree(attrs);
11201 
11202 	return err;
11203 }
11204 
11205 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
11206 {
11207 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11208 	struct net_device *dev = info->user_ptr[1];
11209 	const u8 *ie = NULL, *bssid;
11210 	int ie_len = 0, err;
11211 	u16 reason_code;
11212 	bool local_state_change;
11213 
11214 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11215 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11216 		return -EPERM;
11217 
11218 	if (!info->attrs[NL80211_ATTR_MAC])
11219 		return -EINVAL;
11220 
11221 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11222 		return -EINVAL;
11223 
11224 	if (!rdev->ops->deauth)
11225 		return -EOPNOTSUPP;
11226 
11227 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11228 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11229 		return -EOPNOTSUPP;
11230 
11231 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11232 
11233 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11234 	if (reason_code == 0) {
11235 		/* Reason Code 0 is reserved */
11236 		return -EINVAL;
11237 	}
11238 
11239 	if (info->attrs[NL80211_ATTR_IE]) {
11240 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11241 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11242 	}
11243 
11244 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11245 
11246 	wdev_lock(dev->ieee80211_ptr);
11247 	err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
11248 				   local_state_change);
11249 	wdev_unlock(dev->ieee80211_ptr);
11250 	return err;
11251 }
11252 
11253 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
11254 {
11255 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11256 	struct net_device *dev = info->user_ptr[1];
11257 	const u8 *ie = NULL, *bssid;
11258 	int ie_len = 0, err;
11259 	u16 reason_code;
11260 	bool local_state_change;
11261 
11262 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11263 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11264 		return -EPERM;
11265 
11266 	if (!info->attrs[NL80211_ATTR_MAC])
11267 		return -EINVAL;
11268 
11269 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11270 		return -EINVAL;
11271 
11272 	if (!rdev->ops->disassoc)
11273 		return -EOPNOTSUPP;
11274 
11275 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11276 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11277 		return -EOPNOTSUPP;
11278 
11279 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11280 
11281 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11282 	if (reason_code == 0) {
11283 		/* Reason Code 0 is reserved */
11284 		return -EINVAL;
11285 	}
11286 
11287 	if (info->attrs[NL80211_ATTR_IE]) {
11288 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11289 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11290 	}
11291 
11292 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11293 
11294 	wdev_lock(dev->ieee80211_ptr);
11295 	err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
11296 				     local_state_change);
11297 	wdev_unlock(dev->ieee80211_ptr);
11298 	return err;
11299 }
11300 
11301 static bool
11302 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
11303 			 int mcast_rate[NUM_NL80211_BANDS],
11304 			 int rateval)
11305 {
11306 	struct wiphy *wiphy = &rdev->wiphy;
11307 	bool found = false;
11308 	int band, i;
11309 
11310 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
11311 		struct ieee80211_supported_band *sband;
11312 
11313 		sband = wiphy->bands[band];
11314 		if (!sband)
11315 			continue;
11316 
11317 		for (i = 0; i < sband->n_bitrates; i++) {
11318 			if (sband->bitrates[i].bitrate == rateval) {
11319 				mcast_rate[band] = i + 1;
11320 				found = true;
11321 				break;
11322 			}
11323 		}
11324 	}
11325 
11326 	return found;
11327 }
11328 
11329 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
11330 {
11331 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11332 	struct net_device *dev = info->user_ptr[1];
11333 	struct cfg80211_ibss_params ibss;
11334 	struct wiphy *wiphy;
11335 	struct cfg80211_cached_keys *connkeys = NULL;
11336 	int err;
11337 
11338 	memset(&ibss, 0, sizeof(ibss));
11339 
11340 	if (!info->attrs[NL80211_ATTR_SSID] ||
11341 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11342 		return -EINVAL;
11343 
11344 	ibss.beacon_interval = 100;
11345 
11346 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
11347 		ibss.beacon_interval =
11348 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11349 
11350 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
11351 					   ibss.beacon_interval);
11352 	if (err)
11353 		return err;
11354 
11355 	if (!rdev->ops->join_ibss)
11356 		return -EOPNOTSUPP;
11357 
11358 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11359 		return -EOPNOTSUPP;
11360 
11361 	wiphy = &rdev->wiphy;
11362 
11363 	if (info->attrs[NL80211_ATTR_MAC]) {
11364 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11365 
11366 		if (!is_valid_ether_addr(ibss.bssid))
11367 			return -EINVAL;
11368 	}
11369 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11370 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11371 
11372 	if (info->attrs[NL80211_ATTR_IE]) {
11373 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11374 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11375 	}
11376 
11377 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
11378 	if (err)
11379 		return err;
11380 
11381 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
11382 				     NL80211_IFTYPE_ADHOC))
11383 		return -EINVAL;
11384 
11385 	switch (ibss.chandef.width) {
11386 	case NL80211_CHAN_WIDTH_5:
11387 	case NL80211_CHAN_WIDTH_10:
11388 	case NL80211_CHAN_WIDTH_20_NOHT:
11389 		break;
11390 	case NL80211_CHAN_WIDTH_20:
11391 	case NL80211_CHAN_WIDTH_40:
11392 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11393 			return -EINVAL;
11394 		break;
11395 	case NL80211_CHAN_WIDTH_80:
11396 	case NL80211_CHAN_WIDTH_80P80:
11397 	case NL80211_CHAN_WIDTH_160:
11398 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11399 			return -EINVAL;
11400 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
11401 					     NL80211_EXT_FEATURE_VHT_IBSS))
11402 			return -EINVAL;
11403 		break;
11404 	case NL80211_CHAN_WIDTH_320:
11405 		return -EINVAL;
11406 	default:
11407 		return -EINVAL;
11408 	}
11409 
11410 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
11411 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
11412 
11413 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11414 		u8 *rates =
11415 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11416 		int n_rates =
11417 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11418 		struct ieee80211_supported_band *sband =
11419 			wiphy->bands[ibss.chandef.chan->band];
11420 
11421 		err = ieee80211_get_ratemask(sband, rates, n_rates,
11422 					     &ibss.basic_rates);
11423 		if (err)
11424 			return err;
11425 	}
11426 
11427 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11428 		memcpy(&ibss.ht_capa_mask,
11429 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11430 		       sizeof(ibss.ht_capa_mask));
11431 
11432 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11433 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11434 			return -EINVAL;
11435 		memcpy(&ibss.ht_capa,
11436 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11437 		       sizeof(ibss.ht_capa));
11438 	}
11439 
11440 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11441 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
11442 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11443 		return -EINVAL;
11444 
11445 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11446 		bool no_ht = false;
11447 
11448 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
11449 		if (IS_ERR(connkeys))
11450 			return PTR_ERR(connkeys);
11451 
11452 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
11453 		    no_ht) {
11454 			kfree_sensitive(connkeys);
11455 			return -EINVAL;
11456 		}
11457 	}
11458 
11459 	ibss.control_port =
11460 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
11461 
11462 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11463 		int r = validate_pae_over_nl80211(rdev, info);
11464 
11465 		if (r < 0) {
11466 			kfree_sensitive(connkeys);
11467 			return r;
11468 		}
11469 
11470 		ibss.control_port_over_nl80211 = true;
11471 	}
11472 
11473 	ibss.userspace_handles_dfs =
11474 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11475 
11476 	wdev_lock(dev->ieee80211_ptr);
11477 	err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
11478 	if (err)
11479 		kfree_sensitive(connkeys);
11480 	else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
11481 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11482 	wdev_unlock(dev->ieee80211_ptr);
11483 
11484 	return err;
11485 }
11486 
11487 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
11488 {
11489 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11490 	struct net_device *dev = info->user_ptr[1];
11491 
11492 	if (!rdev->ops->leave_ibss)
11493 		return -EOPNOTSUPP;
11494 
11495 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11496 		return -EOPNOTSUPP;
11497 
11498 	return cfg80211_leave_ibss(rdev, dev, false);
11499 }
11500 
11501 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
11502 {
11503 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11504 	struct net_device *dev = info->user_ptr[1];
11505 	int mcast_rate[NUM_NL80211_BANDS];
11506 	u32 nla_rate;
11507 
11508 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
11509 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
11510 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
11511 		return -EOPNOTSUPP;
11512 
11513 	if (!rdev->ops->set_mcast_rate)
11514 		return -EOPNOTSUPP;
11515 
11516 	memset(mcast_rate, 0, sizeof(mcast_rate));
11517 
11518 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
11519 		return -EINVAL;
11520 
11521 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
11522 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
11523 		return -EINVAL;
11524 
11525 	return rdev_set_mcast_rate(rdev, dev, mcast_rate);
11526 }
11527 
11528 static struct sk_buff *
11529 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
11530 			    struct wireless_dev *wdev, int approxlen,
11531 			    u32 portid, u32 seq, enum nl80211_commands cmd,
11532 			    enum nl80211_attrs attr,
11533 			    const struct nl80211_vendor_cmd_info *info,
11534 			    gfp_t gfp)
11535 {
11536 	struct sk_buff *skb;
11537 	void *hdr;
11538 	struct nlattr *data;
11539 
11540 	skb = nlmsg_new(approxlen + 100, gfp);
11541 	if (!skb)
11542 		return NULL;
11543 
11544 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
11545 	if (!hdr) {
11546 		kfree_skb(skb);
11547 		return NULL;
11548 	}
11549 
11550 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11551 		goto nla_put_failure;
11552 
11553 	if (info) {
11554 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
11555 				info->vendor_id))
11556 			goto nla_put_failure;
11557 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
11558 				info->subcmd))
11559 			goto nla_put_failure;
11560 	}
11561 
11562 	if (wdev) {
11563 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
11564 				      wdev_id(wdev), NL80211_ATTR_PAD))
11565 			goto nla_put_failure;
11566 		if (wdev->netdev &&
11567 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
11568 				wdev->netdev->ifindex))
11569 			goto nla_put_failure;
11570 	}
11571 
11572 	data = nla_nest_start_noflag(skb, attr);
11573 	if (!data)
11574 		goto nla_put_failure;
11575 
11576 	((void **)skb->cb)[0] = rdev;
11577 	((void **)skb->cb)[1] = hdr;
11578 	((void **)skb->cb)[2] = data;
11579 
11580 	return skb;
11581 
11582  nla_put_failure:
11583 	kfree_skb(skb);
11584 	return NULL;
11585 }
11586 
11587 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
11588 					   struct wireless_dev *wdev,
11589 					   enum nl80211_commands cmd,
11590 					   enum nl80211_attrs attr,
11591 					   unsigned int portid,
11592 					   int vendor_event_idx,
11593 					   int approxlen, gfp_t gfp)
11594 {
11595 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11596 	const struct nl80211_vendor_cmd_info *info;
11597 
11598 	switch (cmd) {
11599 	case NL80211_CMD_TESTMODE:
11600 		if (WARN_ON(vendor_event_idx != -1))
11601 			return NULL;
11602 		info = NULL;
11603 		break;
11604 	case NL80211_CMD_VENDOR:
11605 		if (WARN_ON(vendor_event_idx < 0 ||
11606 			    vendor_event_idx >= wiphy->n_vendor_events))
11607 			return NULL;
11608 		info = &wiphy->vendor_events[vendor_event_idx];
11609 		break;
11610 	default:
11611 		WARN_ON(1);
11612 		return NULL;
11613 	}
11614 
11615 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
11616 					   cmd, attr, info, gfp);
11617 }
11618 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
11619 
11620 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
11621 {
11622 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
11623 	void *hdr = ((void **)skb->cb)[1];
11624 	struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
11625 	struct nlattr *data = ((void **)skb->cb)[2];
11626 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
11627 
11628 	/* clear CB data for netlink core to own from now on */
11629 	memset(skb->cb, 0, sizeof(skb->cb));
11630 
11631 	nla_nest_end(skb, data);
11632 	genlmsg_end(skb, hdr);
11633 
11634 	if (nlhdr->nlmsg_pid) {
11635 		genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
11636 				nlhdr->nlmsg_pid);
11637 	} else {
11638 		if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
11639 			mcgrp = NL80211_MCGRP_VENDOR;
11640 
11641 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11642 					skb, 0, mcgrp, gfp);
11643 	}
11644 }
11645 EXPORT_SYMBOL(__cfg80211_send_event_skb);
11646 
11647 #ifdef CONFIG_NL80211_TESTMODE
11648 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
11649 {
11650 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11651 	struct wireless_dev *wdev;
11652 	int err;
11653 
11654 	lockdep_assert_held(&rdev->wiphy.mtx);
11655 
11656 	wdev = __cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
11657 					  info->attrs);
11658 
11659 	if (!rdev->ops->testmode_cmd)
11660 		return -EOPNOTSUPP;
11661 
11662 	if (IS_ERR(wdev)) {
11663 		err = PTR_ERR(wdev);
11664 		if (err != -EINVAL)
11665 			return err;
11666 		wdev = NULL;
11667 	} else if (wdev->wiphy != &rdev->wiphy) {
11668 		return -EINVAL;
11669 	}
11670 
11671 	if (!info->attrs[NL80211_ATTR_TESTDATA])
11672 		return -EINVAL;
11673 
11674 	rdev->cur_cmd_info = info;
11675 	err = rdev_testmode_cmd(rdev, wdev,
11676 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
11677 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
11678 	rdev->cur_cmd_info = NULL;
11679 
11680 	return err;
11681 }
11682 
11683 static int nl80211_testmode_dump(struct sk_buff *skb,
11684 				 struct netlink_callback *cb)
11685 {
11686 	struct cfg80211_registered_device *rdev;
11687 	struct nlattr **attrbuf = NULL;
11688 	int err;
11689 	long phy_idx;
11690 	void *data = NULL;
11691 	int data_len = 0;
11692 
11693 	rtnl_lock();
11694 
11695 	if (cb->args[0]) {
11696 		/*
11697 		 * 0 is a valid index, but not valid for args[0],
11698 		 * so we need to offset by 1.
11699 		 */
11700 		phy_idx = cb->args[0] - 1;
11701 
11702 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
11703 		if (!rdev) {
11704 			err = -ENOENT;
11705 			goto out_err;
11706 		}
11707 	} else {
11708 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
11709 				  GFP_KERNEL);
11710 		if (!attrbuf) {
11711 			err = -ENOMEM;
11712 			goto out_err;
11713 		}
11714 
11715 		err = nlmsg_parse_deprecated(cb->nlh,
11716 					     GENL_HDRLEN + nl80211_fam.hdrsize,
11717 					     attrbuf, nl80211_fam.maxattr,
11718 					     nl80211_policy, NULL);
11719 		if (err)
11720 			goto out_err;
11721 
11722 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
11723 		if (IS_ERR(rdev)) {
11724 			err = PTR_ERR(rdev);
11725 			goto out_err;
11726 		}
11727 		phy_idx = rdev->wiphy_idx;
11728 
11729 		if (attrbuf[NL80211_ATTR_TESTDATA])
11730 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
11731 	}
11732 
11733 	if (cb->args[1]) {
11734 		data = nla_data((void *)cb->args[1]);
11735 		data_len = nla_len((void *)cb->args[1]);
11736 	}
11737 
11738 	if (!rdev->ops->testmode_dump) {
11739 		err = -EOPNOTSUPP;
11740 		goto out_err;
11741 	}
11742 
11743 	while (1) {
11744 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
11745 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
11746 					   NL80211_CMD_TESTMODE);
11747 		struct nlattr *tmdata;
11748 
11749 		if (!hdr)
11750 			break;
11751 
11752 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
11753 			genlmsg_cancel(skb, hdr);
11754 			break;
11755 		}
11756 
11757 		tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
11758 		if (!tmdata) {
11759 			genlmsg_cancel(skb, hdr);
11760 			break;
11761 		}
11762 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
11763 		nla_nest_end(skb, tmdata);
11764 
11765 		if (err == -ENOBUFS || err == -ENOENT) {
11766 			genlmsg_cancel(skb, hdr);
11767 			break;
11768 		} else if (err) {
11769 			genlmsg_cancel(skb, hdr);
11770 			goto out_err;
11771 		}
11772 
11773 		genlmsg_end(skb, hdr);
11774 	}
11775 
11776 	err = skb->len;
11777 	/* see above */
11778 	cb->args[0] = phy_idx + 1;
11779  out_err:
11780 	kfree(attrbuf);
11781 	rtnl_unlock();
11782 	return err;
11783 }
11784 #endif
11785 
11786 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
11787 {
11788 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11789 	struct net_device *dev = info->user_ptr[1];
11790 	struct cfg80211_connect_params connect;
11791 	struct wiphy *wiphy;
11792 	struct cfg80211_cached_keys *connkeys = NULL;
11793 	u32 freq = 0;
11794 	int err;
11795 
11796 	memset(&connect, 0, sizeof(connect));
11797 
11798 	if (!info->attrs[NL80211_ATTR_SSID] ||
11799 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11800 		return -EINVAL;
11801 
11802 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
11803 		connect.auth_type =
11804 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
11805 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
11806 					     NL80211_CMD_CONNECT))
11807 			return -EINVAL;
11808 	} else
11809 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
11810 
11811 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
11812 
11813 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
11814 	    !wiphy_ext_feature_isset(&rdev->wiphy,
11815 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
11816 		return -EINVAL;
11817 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
11818 
11819 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
11820 				      NL80211_MAX_NR_CIPHER_SUITES);
11821 	if (err)
11822 		return err;
11823 
11824 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11825 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11826 		return -EOPNOTSUPP;
11827 
11828 	wiphy = &rdev->wiphy;
11829 
11830 	connect.bg_scan_period = -1;
11831 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
11832 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
11833 		connect.bg_scan_period =
11834 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
11835 	}
11836 
11837 	if (info->attrs[NL80211_ATTR_MAC])
11838 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11839 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
11840 		connect.bssid_hint =
11841 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
11842 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11843 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11844 
11845 	if (info->attrs[NL80211_ATTR_IE]) {
11846 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11847 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11848 	}
11849 
11850 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
11851 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
11852 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
11853 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11854 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
11855 			return -EOPNOTSUPP;
11856 	} else {
11857 		connect.mfp = NL80211_MFP_NO;
11858 	}
11859 
11860 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
11861 		connect.prev_bssid =
11862 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
11863 
11864 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ])
11865 		freq = MHZ_TO_KHZ(nla_get_u32(
11866 					info->attrs[NL80211_ATTR_WIPHY_FREQ]));
11867 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
11868 		freq +=
11869 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
11870 
11871 	if (freq) {
11872 		connect.channel = nl80211_get_valid_chan(wiphy, freq);
11873 		if (!connect.channel)
11874 			return -EINVAL;
11875 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
11876 		freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
11877 		freq = MHZ_TO_KHZ(freq);
11878 		connect.channel_hint = nl80211_get_valid_chan(wiphy, freq);
11879 		if (!connect.channel_hint)
11880 			return -EINVAL;
11881 	}
11882 
11883 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
11884 		connect.edmg.channels =
11885 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
11886 
11887 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
11888 			connect.edmg.bw_config =
11889 				nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
11890 	}
11891 
11892 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11893 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
11894 		if (IS_ERR(connkeys))
11895 			return PTR_ERR(connkeys);
11896 	}
11897 
11898 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
11899 		connect.flags |= ASSOC_REQ_DISABLE_HT;
11900 
11901 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11902 		memcpy(&connect.ht_capa_mask,
11903 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11904 		       sizeof(connect.ht_capa_mask));
11905 
11906 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11907 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
11908 			kfree_sensitive(connkeys);
11909 			return -EINVAL;
11910 		}
11911 		memcpy(&connect.ht_capa,
11912 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11913 		       sizeof(connect.ht_capa));
11914 	}
11915 
11916 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
11917 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
11918 
11919 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
11920 		connect.flags |= ASSOC_REQ_DISABLE_HE;
11921 
11922 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
11923 		connect.flags |= ASSOC_REQ_DISABLE_EHT;
11924 
11925 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11926 		memcpy(&connect.vht_capa_mask,
11927 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
11928 		       sizeof(connect.vht_capa_mask));
11929 
11930 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
11931 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
11932 			kfree_sensitive(connkeys);
11933 			return -EINVAL;
11934 		}
11935 		memcpy(&connect.vht_capa,
11936 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
11937 		       sizeof(connect.vht_capa));
11938 	}
11939 
11940 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
11941 		if (!((rdev->wiphy.features &
11942 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
11943 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
11944 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11945 					     NL80211_EXT_FEATURE_RRM)) {
11946 			kfree_sensitive(connkeys);
11947 			return -EINVAL;
11948 		}
11949 		connect.flags |= ASSOC_REQ_USE_RRM;
11950 	}
11951 
11952 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
11953 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
11954 		kfree_sensitive(connkeys);
11955 		return -EOPNOTSUPP;
11956 	}
11957 
11958 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
11959 		/* bss selection makes no sense if bssid is set */
11960 		if (connect.bssid) {
11961 			kfree_sensitive(connkeys);
11962 			return -EINVAL;
11963 		}
11964 
11965 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
11966 				       wiphy, &connect.bss_select);
11967 		if (err) {
11968 			kfree_sensitive(connkeys);
11969 			return err;
11970 		}
11971 	}
11972 
11973 	if (wiphy_ext_feature_isset(&rdev->wiphy,
11974 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
11975 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
11976 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
11977 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
11978 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11979 		connect.fils_erp_username =
11980 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11981 		connect.fils_erp_username_len =
11982 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11983 		connect.fils_erp_realm =
11984 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11985 		connect.fils_erp_realm_len =
11986 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11987 		connect.fils_erp_next_seq_num =
11988 			nla_get_u16(
11989 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
11990 		connect.fils_erp_rrk =
11991 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
11992 		connect.fils_erp_rrk_len =
11993 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
11994 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
11995 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
11996 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
11997 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11998 		kfree_sensitive(connkeys);
11999 		return -EINVAL;
12000 	}
12001 
12002 	if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
12003 		if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
12004 			kfree_sensitive(connkeys);
12005 			GENL_SET_ERR_MSG(info,
12006 					 "external auth requires connection ownership");
12007 			return -EINVAL;
12008 		}
12009 		connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
12010 	}
12011 
12012 	if (nla_get_flag(info->attrs[NL80211_ATTR_MLO_SUPPORT]))
12013 		connect.flags |= CONNECT_REQ_MLO_SUPPORT;
12014 
12015 	wdev_lock(dev->ieee80211_ptr);
12016 
12017 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
12018 			       connect.prev_bssid);
12019 	if (err)
12020 		kfree_sensitive(connkeys);
12021 
12022 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
12023 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
12024 		if (connect.bssid)
12025 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
12026 			       connect.bssid, ETH_ALEN);
12027 		else
12028 			eth_zero_addr(dev->ieee80211_ptr->disconnect_bssid);
12029 	}
12030 
12031 	wdev_unlock(dev->ieee80211_ptr);
12032 
12033 	return err;
12034 }
12035 
12036 static int nl80211_update_connect_params(struct sk_buff *skb,
12037 					 struct genl_info *info)
12038 {
12039 	struct cfg80211_connect_params connect = {};
12040 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12041 	struct net_device *dev = info->user_ptr[1];
12042 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12043 	bool fils_sk_offload;
12044 	u32 auth_type;
12045 	u32 changed = 0;
12046 	int ret;
12047 
12048 	if (!rdev->ops->update_connect_params)
12049 		return -EOPNOTSUPP;
12050 
12051 	if (info->attrs[NL80211_ATTR_IE]) {
12052 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
12053 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
12054 		changed |= UPDATE_ASSOC_IES;
12055 	}
12056 
12057 	fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
12058 						  NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
12059 
12060 	/*
12061 	 * when driver supports fils-sk offload all attributes must be
12062 	 * provided. So the else covers "fils-sk-not-all" and
12063 	 * "no-fils-sk-any".
12064 	 */
12065 	if (fils_sk_offload &&
12066 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
12067 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
12068 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
12069 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12070 		connect.fils_erp_username =
12071 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12072 		connect.fils_erp_username_len =
12073 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12074 		connect.fils_erp_realm =
12075 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12076 		connect.fils_erp_realm_len =
12077 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12078 		connect.fils_erp_next_seq_num =
12079 			nla_get_u16(
12080 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
12081 		connect.fils_erp_rrk =
12082 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12083 		connect.fils_erp_rrk_len =
12084 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12085 		changed |= UPDATE_FILS_ERP_INFO;
12086 	} else if (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 		return -EINVAL;
12091 	}
12092 
12093 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
12094 		auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
12095 		if (!nl80211_valid_auth_type(rdev, auth_type,
12096 					     NL80211_CMD_CONNECT))
12097 			return -EINVAL;
12098 
12099 		if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
12100 		    fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
12101 			return -EINVAL;
12102 
12103 		connect.auth_type = auth_type;
12104 		changed |= UPDATE_AUTH_TYPE;
12105 	}
12106 
12107 	wdev_lock(dev->ieee80211_ptr);
12108 	if (!wdev->connected)
12109 		ret = -ENOLINK;
12110 	else
12111 		ret = rdev_update_connect_params(rdev, dev, &connect, changed);
12112 	wdev_unlock(dev->ieee80211_ptr);
12113 
12114 	return ret;
12115 }
12116 
12117 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
12118 {
12119 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12120 	struct net_device *dev = info->user_ptr[1];
12121 	u16 reason;
12122 	int ret;
12123 
12124 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
12125 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
12126 		return -EPERM;
12127 
12128 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
12129 		reason = WLAN_REASON_DEAUTH_LEAVING;
12130 	else
12131 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
12132 
12133 	if (reason == 0)
12134 		return -EINVAL;
12135 
12136 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12137 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
12138 		return -EOPNOTSUPP;
12139 
12140 	wdev_lock(dev->ieee80211_ptr);
12141 	ret = cfg80211_disconnect(rdev, dev, reason, true);
12142 	wdev_unlock(dev->ieee80211_ptr);
12143 	return ret;
12144 }
12145 
12146 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
12147 {
12148 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12149 	struct net *net;
12150 	int err;
12151 
12152 	if (info->attrs[NL80211_ATTR_PID]) {
12153 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
12154 
12155 		net = get_net_ns_by_pid(pid);
12156 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
12157 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
12158 
12159 		net = get_net_ns_by_fd(fd);
12160 	} else {
12161 		return -EINVAL;
12162 	}
12163 
12164 	if (IS_ERR(net))
12165 		return PTR_ERR(net);
12166 
12167 	err = 0;
12168 
12169 	/* check if anything to do */
12170 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
12171 		err = cfg80211_switch_netns(rdev, net);
12172 
12173 	put_net(net);
12174 	return err;
12175 }
12176 
12177 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
12178 {
12179 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12180 	int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
12181 			struct cfg80211_pmksa *pmksa) = NULL;
12182 	struct net_device *dev = info->user_ptr[1];
12183 	struct cfg80211_pmksa pmksa;
12184 
12185 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
12186 
12187 	if (!info->attrs[NL80211_ATTR_PMKID])
12188 		return -EINVAL;
12189 
12190 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
12191 
12192 	if (info->attrs[NL80211_ATTR_MAC]) {
12193 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
12194 	} else if (info->attrs[NL80211_ATTR_SSID] &&
12195 		   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
12196 		   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
12197 		    info->attrs[NL80211_ATTR_PMK])) {
12198 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
12199 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
12200 		pmksa.cache_id =
12201 			nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
12202 	} else {
12203 		return -EINVAL;
12204 	}
12205 	if (info->attrs[NL80211_ATTR_PMK]) {
12206 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
12207 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
12208 	}
12209 
12210 	if (info->attrs[NL80211_ATTR_PMK_LIFETIME])
12211 		pmksa.pmk_lifetime =
12212 			nla_get_u32(info->attrs[NL80211_ATTR_PMK_LIFETIME]);
12213 
12214 	if (info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD])
12215 		pmksa.pmk_reauth_threshold =
12216 			nla_get_u8(
12217 				info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD]);
12218 
12219 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12220 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
12221 	    !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
12222 	      wiphy_ext_feature_isset(&rdev->wiphy,
12223 				      NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
12224 		return -EOPNOTSUPP;
12225 
12226 	switch (info->genlhdr->cmd) {
12227 	case NL80211_CMD_SET_PMKSA:
12228 		rdev_ops = rdev->ops->set_pmksa;
12229 		break;
12230 	case NL80211_CMD_DEL_PMKSA:
12231 		rdev_ops = rdev->ops->del_pmksa;
12232 		break;
12233 	default:
12234 		WARN_ON(1);
12235 		break;
12236 	}
12237 
12238 	if (!rdev_ops)
12239 		return -EOPNOTSUPP;
12240 
12241 	return rdev_ops(&rdev->wiphy, dev, &pmksa);
12242 }
12243 
12244 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
12245 {
12246 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12247 	struct net_device *dev = info->user_ptr[1];
12248 
12249 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12250 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
12251 		return -EOPNOTSUPP;
12252 
12253 	if (!rdev->ops->flush_pmksa)
12254 		return -EOPNOTSUPP;
12255 
12256 	return rdev_flush_pmksa(rdev, dev);
12257 }
12258 
12259 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
12260 {
12261 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12262 	struct net_device *dev = info->user_ptr[1];
12263 	u8 action_code, dialog_token;
12264 	u32 peer_capability = 0;
12265 	u16 status_code;
12266 	u8 *peer;
12267 	int link_id;
12268 	bool initiator;
12269 
12270 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12271 	    !rdev->ops->tdls_mgmt)
12272 		return -EOPNOTSUPP;
12273 
12274 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
12275 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
12276 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
12277 	    !info->attrs[NL80211_ATTR_IE] ||
12278 	    !info->attrs[NL80211_ATTR_MAC])
12279 		return -EINVAL;
12280 
12281 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12282 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
12283 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
12284 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
12285 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
12286 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
12287 		peer_capability =
12288 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
12289 	link_id = nl80211_link_id_or_invalid(info->attrs);
12290 
12291 	return rdev_tdls_mgmt(rdev, dev, peer, link_id, action_code,
12292 			      dialog_token, status_code, peer_capability,
12293 			      initiator,
12294 			      nla_data(info->attrs[NL80211_ATTR_IE]),
12295 			      nla_len(info->attrs[NL80211_ATTR_IE]));
12296 }
12297 
12298 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
12299 {
12300 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12301 	struct net_device *dev = info->user_ptr[1];
12302 	enum nl80211_tdls_operation operation;
12303 	u8 *peer;
12304 
12305 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12306 	    !rdev->ops->tdls_oper)
12307 		return -EOPNOTSUPP;
12308 
12309 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
12310 	    !info->attrs[NL80211_ATTR_MAC])
12311 		return -EINVAL;
12312 
12313 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
12314 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12315 
12316 	return rdev_tdls_oper(rdev, dev, peer, operation);
12317 }
12318 
12319 static int nl80211_remain_on_channel(struct sk_buff *skb,
12320 				     struct genl_info *info)
12321 {
12322 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12323 	unsigned int link_id = nl80211_link_id(info->attrs);
12324 	struct wireless_dev *wdev = info->user_ptr[1];
12325 	struct cfg80211_chan_def chandef;
12326 	struct sk_buff *msg;
12327 	void *hdr;
12328 	u64 cookie;
12329 	u32 duration;
12330 	int err;
12331 
12332 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
12333 	    !info->attrs[NL80211_ATTR_DURATION])
12334 		return -EINVAL;
12335 
12336 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12337 
12338 	if (!rdev->ops->remain_on_channel ||
12339 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
12340 		return -EOPNOTSUPP;
12341 
12342 	/*
12343 	 * We should be on that channel for at least a minimum amount of
12344 	 * time (10ms) but no longer than the driver supports.
12345 	 */
12346 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12347 	    duration > rdev->wiphy.max_remain_on_channel_duration)
12348 		return -EINVAL;
12349 
12350 	err = nl80211_parse_chandef(rdev, info, &chandef);
12351 	if (err)
12352 		return err;
12353 
12354 	wdev_lock(wdev);
12355 	if (!cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12356 		const struct cfg80211_chan_def *oper_chandef, *compat_chandef;
12357 
12358 		oper_chandef = wdev_chandef(wdev, link_id);
12359 
12360 		if (WARN_ON(!oper_chandef)) {
12361 			/* cannot happen since we must beacon to get here */
12362 			WARN_ON(1);
12363 			wdev_unlock(wdev);
12364 			return -EBUSY;
12365 		}
12366 
12367 		/* note: returns first one if identical chandefs */
12368 		compat_chandef = cfg80211_chandef_compatible(&chandef,
12369 							     oper_chandef);
12370 
12371 		if (compat_chandef != &chandef) {
12372 			wdev_unlock(wdev);
12373 			return -EBUSY;
12374 		}
12375 	}
12376 	wdev_unlock(wdev);
12377 
12378 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12379 	if (!msg)
12380 		return -ENOMEM;
12381 
12382 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12383 			     NL80211_CMD_REMAIN_ON_CHANNEL);
12384 	if (!hdr) {
12385 		err = -ENOBUFS;
12386 		goto free_msg;
12387 	}
12388 
12389 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
12390 				     duration, &cookie);
12391 
12392 	if (err)
12393 		goto free_msg;
12394 
12395 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12396 			      NL80211_ATTR_PAD))
12397 		goto nla_put_failure;
12398 
12399 	genlmsg_end(msg, hdr);
12400 
12401 	return genlmsg_reply(msg, info);
12402 
12403  nla_put_failure:
12404 	err = -ENOBUFS;
12405  free_msg:
12406 	nlmsg_free(msg);
12407 	return err;
12408 }
12409 
12410 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
12411 					    struct genl_info *info)
12412 {
12413 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12414 	struct wireless_dev *wdev = info->user_ptr[1];
12415 	u64 cookie;
12416 
12417 	if (!info->attrs[NL80211_ATTR_COOKIE])
12418 		return -EINVAL;
12419 
12420 	if (!rdev->ops->cancel_remain_on_channel)
12421 		return -EOPNOTSUPP;
12422 
12423 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12424 
12425 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
12426 }
12427 
12428 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
12429 				       struct genl_info *info)
12430 {
12431 	struct cfg80211_bitrate_mask mask;
12432 	unsigned int link_id = nl80211_link_id(info->attrs);
12433 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12434 	struct net_device *dev = info->user_ptr[1];
12435 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12436 	int err;
12437 
12438 	if (!rdev->ops->set_bitrate_mask)
12439 		return -EOPNOTSUPP;
12440 
12441 	wdev_lock(wdev);
12442 	err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
12443 					    NL80211_ATTR_TX_RATES, &mask,
12444 					    dev, true, link_id);
12445 	if (err)
12446 		goto out;
12447 
12448 	err = rdev_set_bitrate_mask(rdev, dev, link_id, NULL, &mask);
12449 out:
12450 	wdev_unlock(wdev);
12451 	return err;
12452 }
12453 
12454 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
12455 {
12456 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12457 	struct wireless_dev *wdev = info->user_ptr[1];
12458 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
12459 
12460 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
12461 		return -EINVAL;
12462 
12463 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
12464 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
12465 
12466 	switch (wdev->iftype) {
12467 	case NL80211_IFTYPE_STATION:
12468 	case NL80211_IFTYPE_ADHOC:
12469 	case NL80211_IFTYPE_P2P_CLIENT:
12470 	case NL80211_IFTYPE_AP:
12471 	case NL80211_IFTYPE_AP_VLAN:
12472 	case NL80211_IFTYPE_MESH_POINT:
12473 	case NL80211_IFTYPE_P2P_GO:
12474 	case NL80211_IFTYPE_P2P_DEVICE:
12475 		break;
12476 	case NL80211_IFTYPE_NAN:
12477 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12478 					     NL80211_EXT_FEATURE_SECURE_NAN))
12479 			return -EOPNOTSUPP;
12480 		break;
12481 	default:
12482 		return -EOPNOTSUPP;
12483 	}
12484 
12485 	/* not much point in registering if we can't reply */
12486 	if (!rdev->ops->mgmt_tx)
12487 		return -EOPNOTSUPP;
12488 
12489 	if (info->attrs[NL80211_ATTR_RECEIVE_MULTICAST] &&
12490 	    !wiphy_ext_feature_isset(&rdev->wiphy,
12491 				     NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS)) {
12492 		GENL_SET_ERR_MSG(info,
12493 				 "multicast RX registrations are not supported");
12494 		return -EOPNOTSUPP;
12495 	}
12496 
12497 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
12498 					   nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12499 					   nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12500 					   info->attrs[NL80211_ATTR_RECEIVE_MULTICAST],
12501 					   info->extack);
12502 }
12503 
12504 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
12505 {
12506 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12507 	struct wireless_dev *wdev = info->user_ptr[1];
12508 	struct cfg80211_chan_def chandef;
12509 	int err;
12510 	void *hdr = NULL;
12511 	u64 cookie;
12512 	struct sk_buff *msg = NULL;
12513 	struct cfg80211_mgmt_tx_params params = {
12514 		.dont_wait_for_ack =
12515 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
12516 	};
12517 
12518 	if (!info->attrs[NL80211_ATTR_FRAME])
12519 		return -EINVAL;
12520 
12521 	if (!rdev->ops->mgmt_tx)
12522 		return -EOPNOTSUPP;
12523 
12524 	switch (wdev->iftype) {
12525 	case NL80211_IFTYPE_P2P_DEVICE:
12526 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
12527 			return -EINVAL;
12528 		break;
12529 	case NL80211_IFTYPE_STATION:
12530 	case NL80211_IFTYPE_ADHOC:
12531 	case NL80211_IFTYPE_P2P_CLIENT:
12532 	case NL80211_IFTYPE_AP:
12533 	case NL80211_IFTYPE_AP_VLAN:
12534 	case NL80211_IFTYPE_MESH_POINT:
12535 	case NL80211_IFTYPE_P2P_GO:
12536 		break;
12537 	case NL80211_IFTYPE_NAN:
12538 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12539 					     NL80211_EXT_FEATURE_SECURE_NAN))
12540 			return -EOPNOTSUPP;
12541 		break;
12542 	default:
12543 		return -EOPNOTSUPP;
12544 	}
12545 
12546 	if (info->attrs[NL80211_ATTR_DURATION]) {
12547 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12548 			return -EINVAL;
12549 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12550 
12551 		/*
12552 		 * We should wait on the channel for at least a minimum amount
12553 		 * of time (10ms) but no longer than the driver supports.
12554 		 */
12555 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12556 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
12557 			return -EINVAL;
12558 	}
12559 
12560 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
12561 
12562 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12563 		return -EINVAL;
12564 
12565 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
12566 
12567 	/* get the channel if any has been specified, otherwise pass NULL to
12568 	 * the driver. The latter will use the current one
12569 	 */
12570 	chandef.chan = NULL;
12571 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
12572 		err = nl80211_parse_chandef(rdev, info, &chandef);
12573 		if (err)
12574 			return err;
12575 	}
12576 
12577 	if (!chandef.chan && params.offchan)
12578 		return -EINVAL;
12579 
12580 	wdev_lock(wdev);
12581 	if (params.offchan &&
12582 	    !cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12583 		wdev_unlock(wdev);
12584 		return -EBUSY;
12585 	}
12586 
12587 	params.link_id = nl80211_link_id_or_invalid(info->attrs);
12588 	/*
12589 	 * This now races due to the unlock, but we cannot check
12590 	 * the valid links for the _station_ anyway, so that's up
12591 	 * to the driver.
12592 	 */
12593 	if (params.link_id >= 0 &&
12594 	    !(wdev->valid_links & BIT(params.link_id))) {
12595 		wdev_unlock(wdev);
12596 		return -EINVAL;
12597 	}
12598 	wdev_unlock(wdev);
12599 
12600 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
12601 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
12602 
12603 	if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
12604 		int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12605 		int i;
12606 
12607 		if (len % sizeof(u16))
12608 			return -EINVAL;
12609 
12610 		params.n_csa_offsets = len / sizeof(u16);
12611 		params.csa_offsets =
12612 			nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12613 
12614 		/* check that all the offsets fit the frame */
12615 		for (i = 0; i < params.n_csa_offsets; i++) {
12616 			if (params.csa_offsets[i] >= params.len)
12617 				return -EINVAL;
12618 		}
12619 	}
12620 
12621 	if (!params.dont_wait_for_ack) {
12622 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12623 		if (!msg)
12624 			return -ENOMEM;
12625 
12626 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12627 				     NL80211_CMD_FRAME);
12628 		if (!hdr) {
12629 			err = -ENOBUFS;
12630 			goto free_msg;
12631 		}
12632 	}
12633 
12634 	params.chan = chandef.chan;
12635 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
12636 	if (err)
12637 		goto free_msg;
12638 
12639 	if (msg) {
12640 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12641 				      NL80211_ATTR_PAD))
12642 			goto nla_put_failure;
12643 
12644 		genlmsg_end(msg, hdr);
12645 		return genlmsg_reply(msg, info);
12646 	}
12647 
12648 	return 0;
12649 
12650  nla_put_failure:
12651 	err = -ENOBUFS;
12652  free_msg:
12653 	nlmsg_free(msg);
12654 	return err;
12655 }
12656 
12657 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
12658 {
12659 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12660 	struct wireless_dev *wdev = info->user_ptr[1];
12661 	u64 cookie;
12662 
12663 	if (!info->attrs[NL80211_ATTR_COOKIE])
12664 		return -EINVAL;
12665 
12666 	if (!rdev->ops->mgmt_tx_cancel_wait)
12667 		return -EOPNOTSUPP;
12668 
12669 	switch (wdev->iftype) {
12670 	case NL80211_IFTYPE_STATION:
12671 	case NL80211_IFTYPE_ADHOC:
12672 	case NL80211_IFTYPE_P2P_CLIENT:
12673 	case NL80211_IFTYPE_AP:
12674 	case NL80211_IFTYPE_AP_VLAN:
12675 	case NL80211_IFTYPE_P2P_GO:
12676 	case NL80211_IFTYPE_P2P_DEVICE:
12677 		break;
12678 	case NL80211_IFTYPE_NAN:
12679 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12680 					     NL80211_EXT_FEATURE_SECURE_NAN))
12681 			return -EOPNOTSUPP;
12682 		break;
12683 	default:
12684 		return -EOPNOTSUPP;
12685 	}
12686 
12687 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12688 
12689 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
12690 }
12691 
12692 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
12693 {
12694 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12695 	struct wireless_dev *wdev;
12696 	struct net_device *dev = info->user_ptr[1];
12697 	u8 ps_state;
12698 	bool state;
12699 	int err;
12700 
12701 	if (!info->attrs[NL80211_ATTR_PS_STATE])
12702 		return -EINVAL;
12703 
12704 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
12705 
12706 	wdev = dev->ieee80211_ptr;
12707 
12708 	if (!rdev->ops->set_power_mgmt)
12709 		return -EOPNOTSUPP;
12710 
12711 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
12712 
12713 	if (state == wdev->ps)
12714 		return 0;
12715 
12716 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
12717 	if (!err)
12718 		wdev->ps = state;
12719 	return err;
12720 }
12721 
12722 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
12723 {
12724 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12725 	enum nl80211_ps_state ps_state;
12726 	struct wireless_dev *wdev;
12727 	struct net_device *dev = info->user_ptr[1];
12728 	struct sk_buff *msg;
12729 	void *hdr;
12730 	int err;
12731 
12732 	wdev = dev->ieee80211_ptr;
12733 
12734 	if (!rdev->ops->set_power_mgmt)
12735 		return -EOPNOTSUPP;
12736 
12737 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12738 	if (!msg)
12739 		return -ENOMEM;
12740 
12741 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12742 			     NL80211_CMD_GET_POWER_SAVE);
12743 	if (!hdr) {
12744 		err = -ENOBUFS;
12745 		goto free_msg;
12746 	}
12747 
12748 	if (wdev->ps)
12749 		ps_state = NL80211_PS_ENABLED;
12750 	else
12751 		ps_state = NL80211_PS_DISABLED;
12752 
12753 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
12754 		goto nla_put_failure;
12755 
12756 	genlmsg_end(msg, hdr);
12757 	return genlmsg_reply(msg, info);
12758 
12759  nla_put_failure:
12760 	err = -ENOBUFS;
12761  free_msg:
12762 	nlmsg_free(msg);
12763 	return err;
12764 }
12765 
12766 static const struct nla_policy
12767 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
12768 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
12769 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
12770 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
12771 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
12772 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
12773 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
12774 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
12775 };
12776 
12777 static int nl80211_set_cqm_txe(struct genl_info *info,
12778 			       u32 rate, u32 pkts, u32 intvl)
12779 {
12780 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12781 	struct net_device *dev = info->user_ptr[1];
12782 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12783 
12784 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
12785 		return -EINVAL;
12786 
12787 	if (!rdev->ops->set_cqm_txe_config)
12788 		return -EOPNOTSUPP;
12789 
12790 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12791 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12792 		return -EOPNOTSUPP;
12793 
12794 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
12795 }
12796 
12797 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
12798 				    struct net_device *dev)
12799 {
12800 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12801 	s32 last, low, high;
12802 	u32 hyst;
12803 	int i, n, low_index;
12804 	int err;
12805 
12806 	/* RSSI reporting disabled? */
12807 	if (!wdev->cqm_config)
12808 		return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
12809 
12810 	/*
12811 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
12812 	 * event has been received yet, we should receive an event after a
12813 	 * connection is established and enough beacons received to calculate
12814 	 * the average.
12815 	 */
12816 	if (!wdev->cqm_config->last_rssi_event_value &&
12817 	    wdev->links[0].client.current_bss &&
12818 	    rdev->ops->get_station) {
12819 		struct station_info sinfo = {};
12820 		u8 *mac_addr;
12821 
12822 		mac_addr = wdev->links[0].client.current_bss->pub.bssid;
12823 
12824 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
12825 		if (err)
12826 			return err;
12827 
12828 		cfg80211_sinfo_release_content(&sinfo);
12829 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
12830 			wdev->cqm_config->last_rssi_event_value =
12831 				(s8) sinfo.rx_beacon_signal_avg;
12832 	}
12833 
12834 	last = wdev->cqm_config->last_rssi_event_value;
12835 	hyst = wdev->cqm_config->rssi_hyst;
12836 	n = wdev->cqm_config->n_rssi_thresholds;
12837 
12838 	for (i = 0; i < n; i++) {
12839 		i = array_index_nospec(i, n);
12840 		if (last < wdev->cqm_config->rssi_thresholds[i])
12841 			break;
12842 	}
12843 
12844 	low_index = i - 1;
12845 	if (low_index >= 0) {
12846 		low_index = array_index_nospec(low_index, n);
12847 		low = wdev->cqm_config->rssi_thresholds[low_index] - hyst;
12848 	} else {
12849 		low = S32_MIN;
12850 	}
12851 	if (i < n) {
12852 		i = array_index_nospec(i, n);
12853 		high = wdev->cqm_config->rssi_thresholds[i] + hyst - 1;
12854 	} else {
12855 		high = S32_MAX;
12856 	}
12857 
12858 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
12859 }
12860 
12861 static int nl80211_set_cqm_rssi(struct genl_info *info,
12862 				const s32 *thresholds, int n_thresholds,
12863 				u32 hysteresis)
12864 {
12865 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12866 	struct net_device *dev = info->user_ptr[1];
12867 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12868 	int i, err;
12869 	s32 prev = S32_MIN;
12870 
12871 	/* Check all values negative and sorted */
12872 	for (i = 0; i < n_thresholds; i++) {
12873 		if (thresholds[i] > 0 || thresholds[i] <= prev)
12874 			return -EINVAL;
12875 
12876 		prev = thresholds[i];
12877 	}
12878 
12879 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12880 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12881 		return -EOPNOTSUPP;
12882 
12883 	wdev_lock(wdev);
12884 	cfg80211_cqm_config_free(wdev);
12885 	wdev_unlock(wdev);
12886 
12887 	if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
12888 		if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
12889 			return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
12890 
12891 		return rdev_set_cqm_rssi_config(rdev, dev,
12892 						thresholds[0], hysteresis);
12893 	}
12894 
12895 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
12896 				     NL80211_EXT_FEATURE_CQM_RSSI_LIST))
12897 		return -EOPNOTSUPP;
12898 
12899 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
12900 		n_thresholds = 0;
12901 
12902 	wdev_lock(wdev);
12903 	if (n_thresholds) {
12904 		struct cfg80211_cqm_config *cqm_config;
12905 
12906 		cqm_config = kzalloc(struct_size(cqm_config, rssi_thresholds,
12907 						 n_thresholds),
12908 				     GFP_KERNEL);
12909 		if (!cqm_config) {
12910 			err = -ENOMEM;
12911 			goto unlock;
12912 		}
12913 
12914 		cqm_config->rssi_hyst = hysteresis;
12915 		cqm_config->n_rssi_thresholds = n_thresholds;
12916 		memcpy(cqm_config->rssi_thresholds, thresholds,
12917 		       flex_array_size(cqm_config, rssi_thresholds,
12918 				       n_thresholds));
12919 
12920 		wdev->cqm_config = cqm_config;
12921 	}
12922 
12923 	err = cfg80211_cqm_rssi_update(rdev, dev);
12924 
12925 unlock:
12926 	wdev_unlock(wdev);
12927 
12928 	return err;
12929 }
12930 
12931 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
12932 {
12933 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
12934 	struct nlattr *cqm;
12935 	int err;
12936 
12937 	cqm = info->attrs[NL80211_ATTR_CQM];
12938 	if (!cqm)
12939 		return -EINVAL;
12940 
12941 	err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
12942 					  nl80211_attr_cqm_policy,
12943 					  info->extack);
12944 	if (err)
12945 		return err;
12946 
12947 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
12948 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
12949 		const s32 *thresholds =
12950 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
12951 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
12952 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
12953 
12954 		if (len % 4)
12955 			return -EINVAL;
12956 
12957 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
12958 					    hysteresis);
12959 	}
12960 
12961 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
12962 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
12963 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
12964 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
12965 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
12966 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
12967 
12968 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
12969 	}
12970 
12971 	return -EINVAL;
12972 }
12973 
12974 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
12975 {
12976 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12977 	struct net_device *dev = info->user_ptr[1];
12978 	struct ocb_setup setup = {};
12979 	int err;
12980 
12981 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
12982 	if (err)
12983 		return err;
12984 
12985 	return cfg80211_join_ocb(rdev, dev, &setup);
12986 }
12987 
12988 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
12989 {
12990 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12991 	struct net_device *dev = info->user_ptr[1];
12992 
12993 	return cfg80211_leave_ocb(rdev, dev);
12994 }
12995 
12996 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
12997 {
12998 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12999 	struct net_device *dev = info->user_ptr[1];
13000 	struct mesh_config cfg;
13001 	struct mesh_setup setup;
13002 	int err;
13003 
13004 	/* start with default */
13005 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
13006 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
13007 
13008 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
13009 		/* and parse parameters if given */
13010 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
13011 		if (err)
13012 			return err;
13013 	}
13014 
13015 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
13016 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
13017 		return -EINVAL;
13018 
13019 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
13020 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
13021 
13022 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
13023 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
13024 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
13025 			return -EINVAL;
13026 
13027 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
13028 		setup.beacon_interval =
13029 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
13030 
13031 		err = cfg80211_validate_beacon_int(rdev,
13032 						   NL80211_IFTYPE_MESH_POINT,
13033 						   setup.beacon_interval);
13034 		if (err)
13035 			return err;
13036 	}
13037 
13038 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
13039 		setup.dtim_period =
13040 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
13041 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
13042 			return -EINVAL;
13043 	}
13044 
13045 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
13046 		/* parse additional setup parameters if given */
13047 		err = nl80211_parse_mesh_setup(info, &setup);
13048 		if (err)
13049 			return err;
13050 	}
13051 
13052 	if (setup.user_mpm)
13053 		cfg.auto_open_plinks = false;
13054 
13055 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
13056 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
13057 		if (err)
13058 			return err;
13059 	} else {
13060 		/* __cfg80211_join_mesh() will sort it out */
13061 		setup.chandef.chan = NULL;
13062 	}
13063 
13064 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
13065 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
13066 		int n_rates =
13067 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
13068 		struct ieee80211_supported_band *sband;
13069 
13070 		if (!setup.chandef.chan)
13071 			return -EINVAL;
13072 
13073 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
13074 
13075 		err = ieee80211_get_ratemask(sband, rates, n_rates,
13076 					     &setup.basic_rates);
13077 		if (err)
13078 			return err;
13079 	}
13080 
13081 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
13082 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
13083 						    NL80211_ATTR_TX_RATES,
13084 						    &setup.beacon_rate,
13085 						    dev, false, 0);
13086 		if (err)
13087 			return err;
13088 
13089 		if (!setup.chandef.chan)
13090 			return -EINVAL;
13091 
13092 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
13093 					      &setup.beacon_rate);
13094 		if (err)
13095 			return err;
13096 	}
13097 
13098 	setup.userspace_handles_dfs =
13099 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
13100 
13101 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
13102 		int r = validate_pae_over_nl80211(rdev, info);
13103 
13104 		if (r < 0)
13105 			return r;
13106 
13107 		setup.control_port_over_nl80211 = true;
13108 	}
13109 
13110 	wdev_lock(dev->ieee80211_ptr);
13111 	err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
13112 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
13113 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
13114 	wdev_unlock(dev->ieee80211_ptr);
13115 
13116 	return err;
13117 }
13118 
13119 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
13120 {
13121 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13122 	struct net_device *dev = info->user_ptr[1];
13123 
13124 	return cfg80211_leave_mesh(rdev, dev);
13125 }
13126 
13127 #ifdef CONFIG_PM
13128 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
13129 					struct cfg80211_registered_device *rdev)
13130 {
13131 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
13132 	struct nlattr *nl_pats, *nl_pat;
13133 	int i, pat_len;
13134 
13135 	if (!wowlan->n_patterns)
13136 		return 0;
13137 
13138 	nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
13139 	if (!nl_pats)
13140 		return -ENOBUFS;
13141 
13142 	for (i = 0; i < wowlan->n_patterns; i++) {
13143 		nl_pat = nla_nest_start_noflag(msg, i + 1);
13144 		if (!nl_pat)
13145 			return -ENOBUFS;
13146 		pat_len = wowlan->patterns[i].pattern_len;
13147 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
13148 			    wowlan->patterns[i].mask) ||
13149 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
13150 			    wowlan->patterns[i].pattern) ||
13151 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
13152 				wowlan->patterns[i].pkt_offset))
13153 			return -ENOBUFS;
13154 		nla_nest_end(msg, nl_pat);
13155 	}
13156 	nla_nest_end(msg, nl_pats);
13157 
13158 	return 0;
13159 }
13160 
13161 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
13162 				   struct cfg80211_wowlan_tcp *tcp)
13163 {
13164 	struct nlattr *nl_tcp;
13165 
13166 	if (!tcp)
13167 		return 0;
13168 
13169 	nl_tcp = nla_nest_start_noflag(msg,
13170 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
13171 	if (!nl_tcp)
13172 		return -ENOBUFS;
13173 
13174 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
13175 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
13176 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
13177 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
13178 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
13179 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
13180 		    tcp->payload_len, tcp->payload) ||
13181 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
13182 			tcp->data_interval) ||
13183 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
13184 		    tcp->wake_len, tcp->wake_data) ||
13185 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
13186 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
13187 		return -ENOBUFS;
13188 
13189 	if (tcp->payload_seq.len &&
13190 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
13191 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
13192 		return -ENOBUFS;
13193 
13194 	if (tcp->payload_tok.len &&
13195 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
13196 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
13197 		    &tcp->payload_tok))
13198 		return -ENOBUFS;
13199 
13200 	nla_nest_end(msg, nl_tcp);
13201 
13202 	return 0;
13203 }
13204 
13205 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
13206 				  struct cfg80211_sched_scan_request *req)
13207 {
13208 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
13209 	int i;
13210 
13211 	if (!req)
13212 		return 0;
13213 
13214 	nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
13215 	if (!nd)
13216 		return -ENOBUFS;
13217 
13218 	if (req->n_scan_plans == 1 &&
13219 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
13220 			req->scan_plans[0].interval * 1000))
13221 		return -ENOBUFS;
13222 
13223 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
13224 		return -ENOBUFS;
13225 
13226 	if (req->relative_rssi_set) {
13227 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
13228 
13229 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
13230 			       req->relative_rssi))
13231 			return -ENOBUFS;
13232 
13233 		rssi_adjust.band = req->rssi_adjust.band;
13234 		rssi_adjust.delta = req->rssi_adjust.delta;
13235 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
13236 			    sizeof(rssi_adjust), &rssi_adjust))
13237 			return -ENOBUFS;
13238 	}
13239 
13240 	freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
13241 	if (!freqs)
13242 		return -ENOBUFS;
13243 
13244 	for (i = 0; i < req->n_channels; i++) {
13245 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
13246 			return -ENOBUFS;
13247 	}
13248 
13249 	nla_nest_end(msg, freqs);
13250 
13251 	if (req->n_match_sets) {
13252 		matches = nla_nest_start_noflag(msg,
13253 						NL80211_ATTR_SCHED_SCAN_MATCH);
13254 		if (!matches)
13255 			return -ENOBUFS;
13256 
13257 		for (i = 0; i < req->n_match_sets; i++) {
13258 			match = nla_nest_start_noflag(msg, i);
13259 			if (!match)
13260 				return -ENOBUFS;
13261 
13262 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
13263 				    req->match_sets[i].ssid.ssid_len,
13264 				    req->match_sets[i].ssid.ssid))
13265 				return -ENOBUFS;
13266 			nla_nest_end(msg, match);
13267 		}
13268 		nla_nest_end(msg, matches);
13269 	}
13270 
13271 	scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
13272 	if (!scan_plans)
13273 		return -ENOBUFS;
13274 
13275 	for (i = 0; i < req->n_scan_plans; i++) {
13276 		scan_plan = nla_nest_start_noflag(msg, i + 1);
13277 		if (!scan_plan)
13278 			return -ENOBUFS;
13279 
13280 		if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
13281 				req->scan_plans[i].interval) ||
13282 		    (req->scan_plans[i].iterations &&
13283 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
13284 				 req->scan_plans[i].iterations)))
13285 			return -ENOBUFS;
13286 		nla_nest_end(msg, scan_plan);
13287 	}
13288 	nla_nest_end(msg, scan_plans);
13289 
13290 	nla_nest_end(msg, nd);
13291 
13292 	return 0;
13293 }
13294 
13295 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
13296 {
13297 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13298 	struct sk_buff *msg;
13299 	void *hdr;
13300 	u32 size = NLMSG_DEFAULT_SIZE;
13301 
13302 	if (!rdev->wiphy.wowlan)
13303 		return -EOPNOTSUPP;
13304 
13305 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
13306 		/* adjust size to have room for all the data */
13307 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
13308 			rdev->wiphy.wowlan_config->tcp->payload_len +
13309 			rdev->wiphy.wowlan_config->tcp->wake_len +
13310 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
13311 	}
13312 
13313 	msg = nlmsg_new(size, GFP_KERNEL);
13314 	if (!msg)
13315 		return -ENOMEM;
13316 
13317 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13318 			     NL80211_CMD_GET_WOWLAN);
13319 	if (!hdr)
13320 		goto nla_put_failure;
13321 
13322 	if (rdev->wiphy.wowlan_config) {
13323 		struct nlattr *nl_wowlan;
13324 
13325 		nl_wowlan = nla_nest_start_noflag(msg,
13326 						  NL80211_ATTR_WOWLAN_TRIGGERS);
13327 		if (!nl_wowlan)
13328 			goto nla_put_failure;
13329 
13330 		if ((rdev->wiphy.wowlan_config->any &&
13331 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
13332 		    (rdev->wiphy.wowlan_config->disconnect &&
13333 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
13334 		    (rdev->wiphy.wowlan_config->magic_pkt &&
13335 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
13336 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
13337 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
13338 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
13339 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
13340 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
13341 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
13342 		    (rdev->wiphy.wowlan_config->rfkill_release &&
13343 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
13344 			goto nla_put_failure;
13345 
13346 		if (nl80211_send_wowlan_patterns(msg, rdev))
13347 			goto nla_put_failure;
13348 
13349 		if (nl80211_send_wowlan_tcp(msg,
13350 					    rdev->wiphy.wowlan_config->tcp))
13351 			goto nla_put_failure;
13352 
13353 		if (nl80211_send_wowlan_nd(
13354 			    msg,
13355 			    rdev->wiphy.wowlan_config->nd_config))
13356 			goto nla_put_failure;
13357 
13358 		nla_nest_end(msg, nl_wowlan);
13359 	}
13360 
13361 	genlmsg_end(msg, hdr);
13362 	return genlmsg_reply(msg, info);
13363 
13364 nla_put_failure:
13365 	nlmsg_free(msg);
13366 	return -ENOBUFS;
13367 }
13368 
13369 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
13370 				    struct nlattr *attr,
13371 				    struct cfg80211_wowlan *trig)
13372 {
13373 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
13374 	struct cfg80211_wowlan_tcp *cfg;
13375 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
13376 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
13377 	u32 size;
13378 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
13379 	int err, port;
13380 
13381 	if (!rdev->wiphy.wowlan->tcp)
13382 		return -EINVAL;
13383 
13384 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
13385 					  nl80211_wowlan_tcp_policy, NULL);
13386 	if (err)
13387 		return err;
13388 
13389 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
13390 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
13391 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
13392 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
13393 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
13394 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
13395 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
13396 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
13397 		return -EINVAL;
13398 
13399 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
13400 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
13401 		return -EINVAL;
13402 
13403 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
13404 			rdev->wiphy.wowlan->tcp->data_interval_max ||
13405 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
13406 		return -EINVAL;
13407 
13408 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
13409 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
13410 		return -EINVAL;
13411 
13412 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
13413 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
13414 		return -EINVAL;
13415 
13416 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
13417 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13418 
13419 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13420 		tokens_size = tokln - sizeof(*tok);
13421 
13422 		if (!tok->len || tokens_size % tok->len)
13423 			return -EINVAL;
13424 		if (!rdev->wiphy.wowlan->tcp->tok)
13425 			return -EINVAL;
13426 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
13427 			return -EINVAL;
13428 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
13429 			return -EINVAL;
13430 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
13431 			return -EINVAL;
13432 		if (tok->offset + tok->len > data_size)
13433 			return -EINVAL;
13434 	}
13435 
13436 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
13437 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
13438 		if (!rdev->wiphy.wowlan->tcp->seq)
13439 			return -EINVAL;
13440 		if (seq->len == 0 || seq->len > 4)
13441 			return -EINVAL;
13442 		if (seq->len + seq->offset > data_size)
13443 			return -EINVAL;
13444 	}
13445 
13446 	size = sizeof(*cfg);
13447 	size += data_size;
13448 	size += wake_size + wake_mask_size;
13449 	size += tokens_size;
13450 
13451 	cfg = kzalloc(size, GFP_KERNEL);
13452 	if (!cfg)
13453 		return -ENOMEM;
13454 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
13455 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
13456 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
13457 	       ETH_ALEN);
13458 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
13459 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
13460 	else
13461 		port = 0;
13462 #ifdef CONFIG_INET
13463 	/* allocate a socket and port for it and use it */
13464 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
13465 			    IPPROTO_TCP, &cfg->sock, 1);
13466 	if (err) {
13467 		kfree(cfg);
13468 		return err;
13469 	}
13470 	if (inet_csk_get_port(cfg->sock->sk, port)) {
13471 		sock_release(cfg->sock);
13472 		kfree(cfg);
13473 		return -EADDRINUSE;
13474 	}
13475 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
13476 #else
13477 	if (!port) {
13478 		kfree(cfg);
13479 		return -EINVAL;
13480 	}
13481 	cfg->src_port = port;
13482 #endif
13483 
13484 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
13485 	cfg->payload_len = data_size;
13486 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
13487 	memcpy((void *)cfg->payload,
13488 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
13489 	       data_size);
13490 	if (seq)
13491 		cfg->payload_seq = *seq;
13492 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
13493 	cfg->wake_len = wake_size;
13494 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
13495 	memcpy((void *)cfg->wake_data,
13496 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
13497 	       wake_size);
13498 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
13499 			 data_size + wake_size;
13500 	memcpy((void *)cfg->wake_mask,
13501 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
13502 	       wake_mask_size);
13503 	if (tok) {
13504 		cfg->tokens_size = tokens_size;
13505 		cfg->payload_tok = *tok;
13506 		memcpy(cfg->payload_tok.token_stream, tok->token_stream,
13507 		       tokens_size);
13508 	}
13509 
13510 	trig->tcp = cfg;
13511 
13512 	return 0;
13513 }
13514 
13515 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
13516 				   const struct wiphy_wowlan_support *wowlan,
13517 				   struct nlattr *attr,
13518 				   struct cfg80211_wowlan *trig)
13519 {
13520 	struct nlattr **tb;
13521 	int err;
13522 
13523 	tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
13524 	if (!tb)
13525 		return -ENOMEM;
13526 
13527 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
13528 		err = -EOPNOTSUPP;
13529 		goto out;
13530 	}
13531 
13532 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
13533 					  nl80211_policy, NULL);
13534 	if (err)
13535 		goto out;
13536 
13537 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
13538 						   wowlan->max_nd_match_sets);
13539 	err = PTR_ERR_OR_ZERO(trig->nd_config);
13540 	if (err)
13541 		trig->nd_config = NULL;
13542 
13543 out:
13544 	kfree(tb);
13545 	return err;
13546 }
13547 
13548 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
13549 {
13550 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13551 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
13552 	struct cfg80211_wowlan new_triggers = {};
13553 	struct cfg80211_wowlan *ntrig;
13554 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
13555 	int err, i;
13556 	bool prev_enabled = rdev->wiphy.wowlan_config;
13557 	bool regular = false;
13558 
13559 	if (!wowlan)
13560 		return -EOPNOTSUPP;
13561 
13562 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
13563 		cfg80211_rdev_free_wowlan(rdev);
13564 		rdev->wiphy.wowlan_config = NULL;
13565 		goto set_wakeup;
13566 	}
13567 
13568 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
13569 					  info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
13570 					  nl80211_wowlan_policy, info->extack);
13571 	if (err)
13572 		return err;
13573 
13574 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
13575 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
13576 			return -EINVAL;
13577 		new_triggers.any = true;
13578 	}
13579 
13580 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
13581 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
13582 			return -EINVAL;
13583 		new_triggers.disconnect = true;
13584 		regular = true;
13585 	}
13586 
13587 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
13588 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
13589 			return -EINVAL;
13590 		new_triggers.magic_pkt = true;
13591 		regular = true;
13592 	}
13593 
13594 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
13595 		return -EINVAL;
13596 
13597 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
13598 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
13599 			return -EINVAL;
13600 		new_triggers.gtk_rekey_failure = true;
13601 		regular = true;
13602 	}
13603 
13604 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
13605 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
13606 			return -EINVAL;
13607 		new_triggers.eap_identity_req = true;
13608 		regular = true;
13609 	}
13610 
13611 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
13612 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
13613 			return -EINVAL;
13614 		new_triggers.four_way_handshake = true;
13615 		regular = true;
13616 	}
13617 
13618 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
13619 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
13620 			return -EINVAL;
13621 		new_triggers.rfkill_release = true;
13622 		regular = true;
13623 	}
13624 
13625 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
13626 		struct nlattr *pat;
13627 		int n_patterns = 0;
13628 		int rem, pat_len, mask_len, pkt_offset;
13629 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13630 
13631 		regular = true;
13632 
13633 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13634 				    rem)
13635 			n_patterns++;
13636 		if (n_patterns > wowlan->n_patterns)
13637 			return -EINVAL;
13638 
13639 		new_triggers.patterns = kcalloc(n_patterns,
13640 						sizeof(new_triggers.patterns[0]),
13641 						GFP_KERNEL);
13642 		if (!new_triggers.patterns)
13643 			return -ENOMEM;
13644 
13645 		new_triggers.n_patterns = n_patterns;
13646 		i = 0;
13647 
13648 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13649 				    rem) {
13650 			u8 *mask_pat;
13651 
13652 			err = nla_parse_nested_deprecated(pat_tb,
13653 							  MAX_NL80211_PKTPAT,
13654 							  pat,
13655 							  nl80211_packet_pattern_policy,
13656 							  info->extack);
13657 			if (err)
13658 				goto error;
13659 
13660 			err = -EINVAL;
13661 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
13662 			    !pat_tb[NL80211_PKTPAT_PATTERN])
13663 				goto error;
13664 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13665 			mask_len = DIV_ROUND_UP(pat_len, 8);
13666 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13667 				goto error;
13668 			if (pat_len > wowlan->pattern_max_len ||
13669 			    pat_len < wowlan->pattern_min_len)
13670 				goto error;
13671 
13672 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
13673 				pkt_offset = 0;
13674 			else
13675 				pkt_offset = nla_get_u32(
13676 					pat_tb[NL80211_PKTPAT_OFFSET]);
13677 			if (pkt_offset > wowlan->max_pkt_offset)
13678 				goto error;
13679 			new_triggers.patterns[i].pkt_offset = pkt_offset;
13680 
13681 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13682 			if (!mask_pat) {
13683 				err = -ENOMEM;
13684 				goto error;
13685 			}
13686 			new_triggers.patterns[i].mask = mask_pat;
13687 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13688 			       mask_len);
13689 			mask_pat += mask_len;
13690 			new_triggers.patterns[i].pattern = mask_pat;
13691 			new_triggers.patterns[i].pattern_len = pat_len;
13692 			memcpy(mask_pat,
13693 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13694 			       pat_len);
13695 			i++;
13696 		}
13697 	}
13698 
13699 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
13700 		regular = true;
13701 		err = nl80211_parse_wowlan_tcp(
13702 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
13703 			&new_triggers);
13704 		if (err)
13705 			goto error;
13706 	}
13707 
13708 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
13709 		regular = true;
13710 		err = nl80211_parse_wowlan_nd(
13711 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
13712 			&new_triggers);
13713 		if (err)
13714 			goto error;
13715 	}
13716 
13717 	/* The 'any' trigger means the device continues operating more or less
13718 	 * as in its normal operation mode and wakes up the host on most of the
13719 	 * normal interrupts (like packet RX, ...)
13720 	 * It therefore makes little sense to combine with the more constrained
13721 	 * wakeup trigger modes.
13722 	 */
13723 	if (new_triggers.any && regular) {
13724 		err = -EINVAL;
13725 		goto error;
13726 	}
13727 
13728 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
13729 	if (!ntrig) {
13730 		err = -ENOMEM;
13731 		goto error;
13732 	}
13733 	cfg80211_rdev_free_wowlan(rdev);
13734 	rdev->wiphy.wowlan_config = ntrig;
13735 
13736  set_wakeup:
13737 	if (rdev->ops->set_wakeup &&
13738 	    prev_enabled != !!rdev->wiphy.wowlan_config)
13739 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
13740 
13741 	return 0;
13742  error:
13743 	for (i = 0; i < new_triggers.n_patterns; i++)
13744 		kfree(new_triggers.patterns[i].mask);
13745 	kfree(new_triggers.patterns);
13746 	if (new_triggers.tcp && new_triggers.tcp->sock)
13747 		sock_release(new_triggers.tcp->sock);
13748 	kfree(new_triggers.tcp);
13749 	kfree(new_triggers.nd_config);
13750 	return err;
13751 }
13752 #endif
13753 
13754 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
13755 				       struct cfg80211_registered_device *rdev)
13756 {
13757 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
13758 	int i, j, pat_len;
13759 	struct cfg80211_coalesce_rules *rule;
13760 
13761 	if (!rdev->coalesce->n_rules)
13762 		return 0;
13763 
13764 	nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
13765 	if (!nl_rules)
13766 		return -ENOBUFS;
13767 
13768 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
13769 		nl_rule = nla_nest_start_noflag(msg, i + 1);
13770 		if (!nl_rule)
13771 			return -ENOBUFS;
13772 
13773 		rule = &rdev->coalesce->rules[i];
13774 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
13775 				rule->delay))
13776 			return -ENOBUFS;
13777 
13778 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
13779 				rule->condition))
13780 			return -ENOBUFS;
13781 
13782 		nl_pats = nla_nest_start_noflag(msg,
13783 						NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
13784 		if (!nl_pats)
13785 			return -ENOBUFS;
13786 
13787 		for (j = 0; j < rule->n_patterns; j++) {
13788 			nl_pat = nla_nest_start_noflag(msg, j + 1);
13789 			if (!nl_pat)
13790 				return -ENOBUFS;
13791 			pat_len = rule->patterns[j].pattern_len;
13792 			if (nla_put(msg, NL80211_PKTPAT_MASK,
13793 				    DIV_ROUND_UP(pat_len, 8),
13794 				    rule->patterns[j].mask) ||
13795 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
13796 				    rule->patterns[j].pattern) ||
13797 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
13798 					rule->patterns[j].pkt_offset))
13799 				return -ENOBUFS;
13800 			nla_nest_end(msg, nl_pat);
13801 		}
13802 		nla_nest_end(msg, nl_pats);
13803 		nla_nest_end(msg, nl_rule);
13804 	}
13805 	nla_nest_end(msg, nl_rules);
13806 
13807 	return 0;
13808 }
13809 
13810 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
13811 {
13812 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13813 	struct sk_buff *msg;
13814 	void *hdr;
13815 
13816 	if (!rdev->wiphy.coalesce)
13817 		return -EOPNOTSUPP;
13818 
13819 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13820 	if (!msg)
13821 		return -ENOMEM;
13822 
13823 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13824 			     NL80211_CMD_GET_COALESCE);
13825 	if (!hdr)
13826 		goto nla_put_failure;
13827 
13828 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
13829 		goto nla_put_failure;
13830 
13831 	genlmsg_end(msg, hdr);
13832 	return genlmsg_reply(msg, info);
13833 
13834 nla_put_failure:
13835 	nlmsg_free(msg);
13836 	return -ENOBUFS;
13837 }
13838 
13839 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
13840 {
13841 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
13842 	int i, j;
13843 	struct cfg80211_coalesce_rules *rule;
13844 
13845 	if (!coalesce)
13846 		return;
13847 
13848 	for (i = 0; i < coalesce->n_rules; i++) {
13849 		rule = &coalesce->rules[i];
13850 		for (j = 0; j < rule->n_patterns; j++)
13851 			kfree(rule->patterns[j].mask);
13852 		kfree(rule->patterns);
13853 	}
13854 	kfree(coalesce->rules);
13855 	kfree(coalesce);
13856 	rdev->coalesce = NULL;
13857 }
13858 
13859 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
13860 				       struct nlattr *rule,
13861 				       struct cfg80211_coalesce_rules *new_rule)
13862 {
13863 	int err, i;
13864 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13865 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
13866 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
13867 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13868 
13869 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
13870 					  rule, nl80211_coalesce_policy, NULL);
13871 	if (err)
13872 		return err;
13873 
13874 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
13875 		new_rule->delay =
13876 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
13877 	if (new_rule->delay > coalesce->max_delay)
13878 		return -EINVAL;
13879 
13880 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
13881 		new_rule->condition =
13882 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
13883 
13884 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
13885 		return -EINVAL;
13886 
13887 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13888 			    rem)
13889 		n_patterns++;
13890 	if (n_patterns > coalesce->n_patterns)
13891 		return -EINVAL;
13892 
13893 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
13894 				     GFP_KERNEL);
13895 	if (!new_rule->patterns)
13896 		return -ENOMEM;
13897 
13898 	new_rule->n_patterns = n_patterns;
13899 	i = 0;
13900 
13901 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13902 			    rem) {
13903 		u8 *mask_pat;
13904 
13905 		err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
13906 						  pat,
13907 						  nl80211_packet_pattern_policy,
13908 						  NULL);
13909 		if (err)
13910 			return err;
13911 
13912 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
13913 		    !pat_tb[NL80211_PKTPAT_PATTERN])
13914 			return -EINVAL;
13915 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13916 		mask_len = DIV_ROUND_UP(pat_len, 8);
13917 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13918 			return -EINVAL;
13919 		if (pat_len > coalesce->pattern_max_len ||
13920 		    pat_len < coalesce->pattern_min_len)
13921 			return -EINVAL;
13922 
13923 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
13924 			pkt_offset = 0;
13925 		else
13926 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
13927 		if (pkt_offset > coalesce->max_pkt_offset)
13928 			return -EINVAL;
13929 		new_rule->patterns[i].pkt_offset = pkt_offset;
13930 
13931 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13932 		if (!mask_pat)
13933 			return -ENOMEM;
13934 
13935 		new_rule->patterns[i].mask = mask_pat;
13936 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13937 		       mask_len);
13938 
13939 		mask_pat += mask_len;
13940 		new_rule->patterns[i].pattern = mask_pat;
13941 		new_rule->patterns[i].pattern_len = pat_len;
13942 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13943 		       pat_len);
13944 		i++;
13945 	}
13946 
13947 	return 0;
13948 }
13949 
13950 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
13951 {
13952 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13953 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13954 	struct cfg80211_coalesce new_coalesce = {};
13955 	struct cfg80211_coalesce *n_coalesce;
13956 	int err, rem_rule, n_rules = 0, i, j;
13957 	struct nlattr *rule;
13958 	struct cfg80211_coalesce_rules *tmp_rule;
13959 
13960 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
13961 		return -EOPNOTSUPP;
13962 
13963 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
13964 		cfg80211_rdev_free_coalesce(rdev);
13965 		rdev_set_coalesce(rdev, NULL);
13966 		return 0;
13967 	}
13968 
13969 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
13970 			    rem_rule)
13971 		n_rules++;
13972 	if (n_rules > coalesce->n_rules)
13973 		return -EINVAL;
13974 
13975 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
13976 				     GFP_KERNEL);
13977 	if (!new_coalesce.rules)
13978 		return -ENOMEM;
13979 
13980 	new_coalesce.n_rules = n_rules;
13981 	i = 0;
13982 
13983 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
13984 			    rem_rule) {
13985 		err = nl80211_parse_coalesce_rule(rdev, rule,
13986 						  &new_coalesce.rules[i]);
13987 		if (err)
13988 			goto error;
13989 
13990 		i++;
13991 	}
13992 
13993 	err = rdev_set_coalesce(rdev, &new_coalesce);
13994 	if (err)
13995 		goto error;
13996 
13997 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
13998 	if (!n_coalesce) {
13999 		err = -ENOMEM;
14000 		goto error;
14001 	}
14002 	cfg80211_rdev_free_coalesce(rdev);
14003 	rdev->coalesce = n_coalesce;
14004 
14005 	return 0;
14006 error:
14007 	for (i = 0; i < new_coalesce.n_rules; i++) {
14008 		tmp_rule = &new_coalesce.rules[i];
14009 		for (j = 0; j < tmp_rule->n_patterns; j++)
14010 			kfree(tmp_rule->patterns[j].mask);
14011 		kfree(tmp_rule->patterns);
14012 	}
14013 	kfree(new_coalesce.rules);
14014 
14015 	return err;
14016 }
14017 
14018 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
14019 {
14020 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14021 	struct net_device *dev = info->user_ptr[1];
14022 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14023 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
14024 	struct cfg80211_gtk_rekey_data rekey_data = {};
14025 	int err;
14026 
14027 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
14028 		return -EINVAL;
14029 
14030 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
14031 					  info->attrs[NL80211_ATTR_REKEY_DATA],
14032 					  nl80211_rekey_policy, info->extack);
14033 	if (err)
14034 		return err;
14035 
14036 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
14037 	    !tb[NL80211_REKEY_DATA_KCK])
14038 		return -EINVAL;
14039 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN &&
14040 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
14041 	      nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KEK_EXT_LEN))
14042 		return -ERANGE;
14043 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN &&
14044 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
14045 	      nla_len(tb[NL80211_REKEY_DATA_KCK]) == NL80211_KCK_EXT_LEN) &&
14046 	     !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KCK_32 &&
14047 	       nla_len(tb[NL80211_REKEY_DATA_KCK]) == NL80211_KCK_EXT_LEN_32))
14048 		return -ERANGE;
14049 
14050 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
14051 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
14052 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
14053 	rekey_data.kek_len = nla_len(tb[NL80211_REKEY_DATA_KEK]);
14054 	rekey_data.kck_len = nla_len(tb[NL80211_REKEY_DATA_KCK]);
14055 	if (tb[NL80211_REKEY_DATA_AKM])
14056 		rekey_data.akm = nla_get_u32(tb[NL80211_REKEY_DATA_AKM]);
14057 
14058 	wdev_lock(wdev);
14059 	if (!wdev->connected) {
14060 		err = -ENOTCONN;
14061 		goto out;
14062 	}
14063 
14064 	if (!rdev->ops->set_rekey_data) {
14065 		err = -EOPNOTSUPP;
14066 		goto out;
14067 	}
14068 
14069 	err = rdev_set_rekey_data(rdev, dev, &rekey_data);
14070  out:
14071 	wdev_unlock(wdev);
14072 	return err;
14073 }
14074 
14075 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
14076 					     struct genl_info *info)
14077 {
14078 	struct net_device *dev = info->user_ptr[1];
14079 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14080 
14081 	if (wdev->iftype != NL80211_IFTYPE_AP &&
14082 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
14083 		return -EINVAL;
14084 
14085 	if (wdev->ap_unexpected_nlportid)
14086 		return -EBUSY;
14087 
14088 	wdev->ap_unexpected_nlportid = info->snd_portid;
14089 	return 0;
14090 }
14091 
14092 static int nl80211_probe_client(struct sk_buff *skb,
14093 				struct genl_info *info)
14094 {
14095 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14096 	struct net_device *dev = info->user_ptr[1];
14097 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14098 	struct sk_buff *msg;
14099 	void *hdr;
14100 	const u8 *addr;
14101 	u64 cookie;
14102 	int err;
14103 
14104 	if (wdev->iftype != NL80211_IFTYPE_AP &&
14105 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
14106 		return -EOPNOTSUPP;
14107 
14108 	if (!info->attrs[NL80211_ATTR_MAC])
14109 		return -EINVAL;
14110 
14111 	if (!rdev->ops->probe_client)
14112 		return -EOPNOTSUPP;
14113 
14114 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14115 	if (!msg)
14116 		return -ENOMEM;
14117 
14118 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14119 			     NL80211_CMD_PROBE_CLIENT);
14120 	if (!hdr) {
14121 		err = -ENOBUFS;
14122 		goto free_msg;
14123 	}
14124 
14125 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
14126 
14127 	err = rdev_probe_client(rdev, dev, addr, &cookie);
14128 	if (err)
14129 		goto free_msg;
14130 
14131 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14132 			      NL80211_ATTR_PAD))
14133 		goto nla_put_failure;
14134 
14135 	genlmsg_end(msg, hdr);
14136 
14137 	return genlmsg_reply(msg, info);
14138 
14139  nla_put_failure:
14140 	err = -ENOBUFS;
14141  free_msg:
14142 	nlmsg_free(msg);
14143 	return err;
14144 }
14145 
14146 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
14147 {
14148 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14149 	struct cfg80211_beacon_registration *reg, *nreg;
14150 	int rv;
14151 
14152 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
14153 		return -EOPNOTSUPP;
14154 
14155 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
14156 	if (!nreg)
14157 		return -ENOMEM;
14158 
14159 	/* First, check if already registered. */
14160 	spin_lock_bh(&rdev->beacon_registrations_lock);
14161 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
14162 		if (reg->nlportid == info->snd_portid) {
14163 			rv = -EALREADY;
14164 			goto out_err;
14165 		}
14166 	}
14167 	/* Add it to the list */
14168 	nreg->nlportid = info->snd_portid;
14169 	list_add(&nreg->list, &rdev->beacon_registrations);
14170 
14171 	spin_unlock_bh(&rdev->beacon_registrations_lock);
14172 
14173 	return 0;
14174 out_err:
14175 	spin_unlock_bh(&rdev->beacon_registrations_lock);
14176 	kfree(nreg);
14177 	return rv;
14178 }
14179 
14180 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
14181 {
14182 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14183 	struct wireless_dev *wdev = info->user_ptr[1];
14184 	int err;
14185 
14186 	if (!rdev->ops->start_p2p_device)
14187 		return -EOPNOTSUPP;
14188 
14189 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
14190 		return -EOPNOTSUPP;
14191 
14192 	if (wdev_running(wdev))
14193 		return 0;
14194 
14195 	if (rfkill_blocked(rdev->wiphy.rfkill))
14196 		return -ERFKILL;
14197 
14198 	err = rdev_start_p2p_device(rdev, wdev);
14199 	if (err)
14200 		return err;
14201 
14202 	wdev->is_running = true;
14203 	rdev->opencount++;
14204 
14205 	return 0;
14206 }
14207 
14208 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
14209 {
14210 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14211 	struct wireless_dev *wdev = info->user_ptr[1];
14212 
14213 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
14214 		return -EOPNOTSUPP;
14215 
14216 	if (!rdev->ops->stop_p2p_device)
14217 		return -EOPNOTSUPP;
14218 
14219 	cfg80211_stop_p2p_device(rdev, wdev);
14220 
14221 	return 0;
14222 }
14223 
14224 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
14225 {
14226 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14227 	struct wireless_dev *wdev = info->user_ptr[1];
14228 	struct cfg80211_nan_conf conf = {};
14229 	int err;
14230 
14231 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14232 		return -EOPNOTSUPP;
14233 
14234 	if (wdev_running(wdev))
14235 		return -EEXIST;
14236 
14237 	if (rfkill_blocked(rdev->wiphy.rfkill))
14238 		return -ERFKILL;
14239 
14240 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
14241 		return -EINVAL;
14242 
14243 	conf.master_pref =
14244 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14245 
14246 	if (info->attrs[NL80211_ATTR_BANDS]) {
14247 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14248 
14249 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14250 			return -EOPNOTSUPP;
14251 
14252 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14253 			return -EINVAL;
14254 
14255 		conf.bands = bands;
14256 	}
14257 
14258 	err = rdev_start_nan(rdev, wdev, &conf);
14259 	if (err)
14260 		return err;
14261 
14262 	wdev->is_running = true;
14263 	rdev->opencount++;
14264 
14265 	return 0;
14266 }
14267 
14268 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
14269 {
14270 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14271 	struct wireless_dev *wdev = info->user_ptr[1];
14272 
14273 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14274 		return -EOPNOTSUPP;
14275 
14276 	cfg80211_stop_nan(rdev, wdev);
14277 
14278 	return 0;
14279 }
14280 
14281 static int validate_nan_filter(struct nlattr *filter_attr)
14282 {
14283 	struct nlattr *attr;
14284 	int len = 0, n_entries = 0, rem;
14285 
14286 	nla_for_each_nested(attr, filter_attr, rem) {
14287 		len += nla_len(attr);
14288 		n_entries++;
14289 	}
14290 
14291 	if (len >= U8_MAX)
14292 		return -EINVAL;
14293 
14294 	return n_entries;
14295 }
14296 
14297 static int handle_nan_filter(struct nlattr *attr_filter,
14298 			     struct cfg80211_nan_func *func,
14299 			     bool tx)
14300 {
14301 	struct nlattr *attr;
14302 	int n_entries, rem, i;
14303 	struct cfg80211_nan_func_filter *filter;
14304 
14305 	n_entries = validate_nan_filter(attr_filter);
14306 	if (n_entries < 0)
14307 		return n_entries;
14308 
14309 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
14310 
14311 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
14312 	if (!filter)
14313 		return -ENOMEM;
14314 
14315 	i = 0;
14316 	nla_for_each_nested(attr, attr_filter, rem) {
14317 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
14318 		if (!filter[i].filter)
14319 			goto err;
14320 
14321 		filter[i].len = nla_len(attr);
14322 		i++;
14323 	}
14324 	if (tx) {
14325 		func->num_tx_filters = n_entries;
14326 		func->tx_filters = filter;
14327 	} else {
14328 		func->num_rx_filters = n_entries;
14329 		func->rx_filters = filter;
14330 	}
14331 
14332 	return 0;
14333 
14334 err:
14335 	i = 0;
14336 	nla_for_each_nested(attr, attr_filter, rem) {
14337 		kfree(filter[i].filter);
14338 		i++;
14339 	}
14340 	kfree(filter);
14341 	return -ENOMEM;
14342 }
14343 
14344 static int nl80211_nan_add_func(struct sk_buff *skb,
14345 				struct genl_info *info)
14346 {
14347 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14348 	struct wireless_dev *wdev = info->user_ptr[1];
14349 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
14350 	struct cfg80211_nan_func *func;
14351 	struct sk_buff *msg = NULL;
14352 	void *hdr = NULL;
14353 	int err = 0;
14354 
14355 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14356 		return -EOPNOTSUPP;
14357 
14358 	if (!wdev_running(wdev))
14359 		return -ENOTCONN;
14360 
14361 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
14362 		return -EINVAL;
14363 
14364 	err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
14365 					  info->attrs[NL80211_ATTR_NAN_FUNC],
14366 					  nl80211_nan_func_policy,
14367 					  info->extack);
14368 	if (err)
14369 		return err;
14370 
14371 	func = kzalloc(sizeof(*func), GFP_KERNEL);
14372 	if (!func)
14373 		return -ENOMEM;
14374 
14375 	func->cookie = cfg80211_assign_cookie(rdev);
14376 
14377 	if (!tb[NL80211_NAN_FUNC_TYPE]) {
14378 		err = -EINVAL;
14379 		goto out;
14380 	}
14381 
14382 
14383 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
14384 
14385 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
14386 		err = -EINVAL;
14387 		goto out;
14388 	}
14389 
14390 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
14391 	       sizeof(func->service_id));
14392 
14393 	func->close_range =
14394 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
14395 
14396 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
14397 		func->serv_spec_info_len =
14398 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
14399 		func->serv_spec_info =
14400 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
14401 				func->serv_spec_info_len,
14402 				GFP_KERNEL);
14403 		if (!func->serv_spec_info) {
14404 			err = -ENOMEM;
14405 			goto out;
14406 		}
14407 	}
14408 
14409 	if (tb[NL80211_NAN_FUNC_TTL])
14410 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
14411 
14412 	switch (func->type) {
14413 	case NL80211_NAN_FUNC_PUBLISH:
14414 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
14415 			err = -EINVAL;
14416 			goto out;
14417 		}
14418 
14419 		func->publish_type =
14420 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
14421 		func->publish_bcast =
14422 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
14423 
14424 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
14425 			func->publish_bcast) {
14426 			err = -EINVAL;
14427 			goto out;
14428 		}
14429 		break;
14430 	case NL80211_NAN_FUNC_SUBSCRIBE:
14431 		func->subscribe_active =
14432 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
14433 		break;
14434 	case NL80211_NAN_FUNC_FOLLOW_UP:
14435 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
14436 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
14437 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
14438 			err = -EINVAL;
14439 			goto out;
14440 		}
14441 
14442 		func->followup_id =
14443 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
14444 		func->followup_reqid =
14445 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
14446 		memcpy(func->followup_dest.addr,
14447 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
14448 		       sizeof(func->followup_dest.addr));
14449 		if (func->ttl) {
14450 			err = -EINVAL;
14451 			goto out;
14452 		}
14453 		break;
14454 	default:
14455 		err = -EINVAL;
14456 		goto out;
14457 	}
14458 
14459 	if (tb[NL80211_NAN_FUNC_SRF]) {
14460 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
14461 
14462 		err = nla_parse_nested_deprecated(srf_tb,
14463 						  NL80211_NAN_SRF_ATTR_MAX,
14464 						  tb[NL80211_NAN_FUNC_SRF],
14465 						  nl80211_nan_srf_policy,
14466 						  info->extack);
14467 		if (err)
14468 			goto out;
14469 
14470 		func->srf_include =
14471 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
14472 
14473 		if (srf_tb[NL80211_NAN_SRF_BF]) {
14474 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
14475 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
14476 				err = -EINVAL;
14477 				goto out;
14478 			}
14479 
14480 			func->srf_bf_len =
14481 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
14482 			func->srf_bf =
14483 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
14484 					func->srf_bf_len, GFP_KERNEL);
14485 			if (!func->srf_bf) {
14486 				err = -ENOMEM;
14487 				goto out;
14488 			}
14489 
14490 			func->srf_bf_idx =
14491 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
14492 		} else {
14493 			struct nlattr *attr, *mac_attr =
14494 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
14495 			int n_entries, rem, i = 0;
14496 
14497 			if (!mac_attr) {
14498 				err = -EINVAL;
14499 				goto out;
14500 			}
14501 
14502 			n_entries = validate_acl_mac_addrs(mac_attr);
14503 			if (n_entries <= 0) {
14504 				err = -EINVAL;
14505 				goto out;
14506 			}
14507 
14508 			func->srf_num_macs = n_entries;
14509 			func->srf_macs =
14510 				kcalloc(n_entries, sizeof(*func->srf_macs),
14511 					GFP_KERNEL);
14512 			if (!func->srf_macs) {
14513 				err = -ENOMEM;
14514 				goto out;
14515 			}
14516 
14517 			nla_for_each_nested(attr, mac_attr, rem)
14518 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
14519 				       sizeof(*func->srf_macs));
14520 		}
14521 	}
14522 
14523 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
14524 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
14525 					func, true);
14526 		if (err)
14527 			goto out;
14528 	}
14529 
14530 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
14531 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
14532 					func, false);
14533 		if (err)
14534 			goto out;
14535 	}
14536 
14537 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14538 	if (!msg) {
14539 		err = -ENOMEM;
14540 		goto out;
14541 	}
14542 
14543 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14544 			     NL80211_CMD_ADD_NAN_FUNCTION);
14545 	/* This can't really happen - we just allocated 4KB */
14546 	if (WARN_ON(!hdr)) {
14547 		err = -ENOMEM;
14548 		goto out;
14549 	}
14550 
14551 	err = rdev_add_nan_func(rdev, wdev, func);
14552 out:
14553 	if (err < 0) {
14554 		cfg80211_free_nan_func(func);
14555 		nlmsg_free(msg);
14556 		return err;
14557 	}
14558 
14559 	/* propagate the instance id and cookie to userspace  */
14560 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
14561 			      NL80211_ATTR_PAD))
14562 		goto nla_put_failure;
14563 
14564 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14565 	if (!func_attr)
14566 		goto nla_put_failure;
14567 
14568 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
14569 		       func->instance_id))
14570 		goto nla_put_failure;
14571 
14572 	nla_nest_end(msg, func_attr);
14573 
14574 	genlmsg_end(msg, hdr);
14575 	return genlmsg_reply(msg, info);
14576 
14577 nla_put_failure:
14578 	nlmsg_free(msg);
14579 	return -ENOBUFS;
14580 }
14581 
14582 static int nl80211_nan_del_func(struct sk_buff *skb,
14583 			       struct genl_info *info)
14584 {
14585 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14586 	struct wireless_dev *wdev = info->user_ptr[1];
14587 	u64 cookie;
14588 
14589 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14590 		return -EOPNOTSUPP;
14591 
14592 	if (!wdev_running(wdev))
14593 		return -ENOTCONN;
14594 
14595 	if (!info->attrs[NL80211_ATTR_COOKIE])
14596 		return -EINVAL;
14597 
14598 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
14599 
14600 	rdev_del_nan_func(rdev, wdev, cookie);
14601 
14602 	return 0;
14603 }
14604 
14605 static int nl80211_nan_change_config(struct sk_buff *skb,
14606 				     struct genl_info *info)
14607 {
14608 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14609 	struct wireless_dev *wdev = info->user_ptr[1];
14610 	struct cfg80211_nan_conf conf = {};
14611 	u32 changed = 0;
14612 
14613 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14614 		return -EOPNOTSUPP;
14615 
14616 	if (!wdev_running(wdev))
14617 		return -ENOTCONN;
14618 
14619 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
14620 		conf.master_pref =
14621 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14622 		if (conf.master_pref <= 1 || conf.master_pref == 255)
14623 			return -EINVAL;
14624 
14625 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
14626 	}
14627 
14628 	if (info->attrs[NL80211_ATTR_BANDS]) {
14629 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14630 
14631 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14632 			return -EOPNOTSUPP;
14633 
14634 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14635 			return -EINVAL;
14636 
14637 		conf.bands = bands;
14638 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
14639 	}
14640 
14641 	if (!changed)
14642 		return -EINVAL;
14643 
14644 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
14645 }
14646 
14647 void cfg80211_nan_match(struct wireless_dev *wdev,
14648 			struct cfg80211_nan_match_params *match, gfp_t gfp)
14649 {
14650 	struct wiphy *wiphy = wdev->wiphy;
14651 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14652 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
14653 	struct sk_buff *msg;
14654 	void *hdr;
14655 
14656 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
14657 		return;
14658 
14659 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14660 	if (!msg)
14661 		return;
14662 
14663 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
14664 	if (!hdr) {
14665 		nlmsg_free(msg);
14666 		return;
14667 	}
14668 
14669 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14670 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14671 					 wdev->netdev->ifindex)) ||
14672 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14673 			      NL80211_ATTR_PAD))
14674 		goto nla_put_failure;
14675 
14676 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
14677 			      NL80211_ATTR_PAD) ||
14678 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
14679 		goto nla_put_failure;
14680 
14681 	match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
14682 	if (!match_attr)
14683 		goto nla_put_failure;
14684 
14685 	local_func_attr = nla_nest_start_noflag(msg,
14686 						NL80211_NAN_MATCH_FUNC_LOCAL);
14687 	if (!local_func_attr)
14688 		goto nla_put_failure;
14689 
14690 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
14691 		goto nla_put_failure;
14692 
14693 	nla_nest_end(msg, local_func_attr);
14694 
14695 	peer_func_attr = nla_nest_start_noflag(msg,
14696 					       NL80211_NAN_MATCH_FUNC_PEER);
14697 	if (!peer_func_attr)
14698 		goto nla_put_failure;
14699 
14700 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
14701 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
14702 		goto nla_put_failure;
14703 
14704 	if (match->info && match->info_len &&
14705 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
14706 		    match->info))
14707 		goto nla_put_failure;
14708 
14709 	nla_nest_end(msg, peer_func_attr);
14710 	nla_nest_end(msg, match_attr);
14711 	genlmsg_end(msg, hdr);
14712 
14713 	if (!wdev->owner_nlportid)
14714 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14715 					msg, 0, NL80211_MCGRP_NAN, gfp);
14716 	else
14717 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14718 				wdev->owner_nlportid);
14719 
14720 	return;
14721 
14722 nla_put_failure:
14723 	nlmsg_free(msg);
14724 }
14725 EXPORT_SYMBOL(cfg80211_nan_match);
14726 
14727 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
14728 				  u8 inst_id,
14729 				  enum nl80211_nan_func_term_reason reason,
14730 				  u64 cookie, gfp_t gfp)
14731 {
14732 	struct wiphy *wiphy = wdev->wiphy;
14733 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14734 	struct sk_buff *msg;
14735 	struct nlattr *func_attr;
14736 	void *hdr;
14737 
14738 	if (WARN_ON(!inst_id))
14739 		return;
14740 
14741 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14742 	if (!msg)
14743 		return;
14744 
14745 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
14746 	if (!hdr) {
14747 		nlmsg_free(msg);
14748 		return;
14749 	}
14750 
14751 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14752 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14753 					 wdev->netdev->ifindex)) ||
14754 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14755 			      NL80211_ATTR_PAD))
14756 		goto nla_put_failure;
14757 
14758 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14759 			      NL80211_ATTR_PAD))
14760 		goto nla_put_failure;
14761 
14762 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14763 	if (!func_attr)
14764 		goto nla_put_failure;
14765 
14766 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
14767 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
14768 		goto nla_put_failure;
14769 
14770 	nla_nest_end(msg, func_attr);
14771 	genlmsg_end(msg, hdr);
14772 
14773 	if (!wdev->owner_nlportid)
14774 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14775 					msg, 0, NL80211_MCGRP_NAN, gfp);
14776 	else
14777 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14778 				wdev->owner_nlportid);
14779 
14780 	return;
14781 
14782 nla_put_failure:
14783 	nlmsg_free(msg);
14784 }
14785 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
14786 
14787 static int nl80211_get_protocol_features(struct sk_buff *skb,
14788 					 struct genl_info *info)
14789 {
14790 	void *hdr;
14791 	struct sk_buff *msg;
14792 
14793 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14794 	if (!msg)
14795 		return -ENOMEM;
14796 
14797 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14798 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
14799 	if (!hdr)
14800 		goto nla_put_failure;
14801 
14802 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
14803 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
14804 		goto nla_put_failure;
14805 
14806 	genlmsg_end(msg, hdr);
14807 	return genlmsg_reply(msg, info);
14808 
14809  nla_put_failure:
14810 	kfree_skb(msg);
14811 	return -ENOBUFS;
14812 }
14813 
14814 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
14815 {
14816 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14817 	struct cfg80211_update_ft_ies_params ft_params;
14818 	struct net_device *dev = info->user_ptr[1];
14819 
14820 	if (!rdev->ops->update_ft_ies)
14821 		return -EOPNOTSUPP;
14822 
14823 	if (!info->attrs[NL80211_ATTR_MDID] ||
14824 	    !info->attrs[NL80211_ATTR_IE])
14825 		return -EINVAL;
14826 
14827 	memset(&ft_params, 0, sizeof(ft_params));
14828 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
14829 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
14830 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
14831 
14832 	return rdev_update_ft_ies(rdev, dev, &ft_params);
14833 }
14834 
14835 static int nl80211_crit_protocol_start(struct sk_buff *skb,
14836 				       struct genl_info *info)
14837 {
14838 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14839 	struct wireless_dev *wdev = info->user_ptr[1];
14840 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
14841 	u16 duration;
14842 	int ret;
14843 
14844 	if (!rdev->ops->crit_proto_start)
14845 		return -EOPNOTSUPP;
14846 
14847 	if (WARN_ON(!rdev->ops->crit_proto_stop))
14848 		return -EINVAL;
14849 
14850 	if (rdev->crit_proto_nlportid)
14851 		return -EBUSY;
14852 
14853 	/* determine protocol if provided */
14854 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
14855 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
14856 
14857 	if (proto >= NUM_NL80211_CRIT_PROTO)
14858 		return -EINVAL;
14859 
14860 	/* timeout must be provided */
14861 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
14862 		return -EINVAL;
14863 
14864 	duration =
14865 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
14866 
14867 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
14868 	if (!ret)
14869 		rdev->crit_proto_nlportid = info->snd_portid;
14870 
14871 	return ret;
14872 }
14873 
14874 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
14875 				      struct genl_info *info)
14876 {
14877 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14878 	struct wireless_dev *wdev = info->user_ptr[1];
14879 
14880 	if (!rdev->ops->crit_proto_stop)
14881 		return -EOPNOTSUPP;
14882 
14883 	if (rdev->crit_proto_nlportid) {
14884 		rdev->crit_proto_nlportid = 0;
14885 		rdev_crit_proto_stop(rdev, wdev);
14886 	}
14887 	return 0;
14888 }
14889 
14890 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
14891 				       struct nlattr *attr,
14892 				       struct netlink_ext_ack *extack)
14893 {
14894 	if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
14895 		if (attr->nla_type & NLA_F_NESTED) {
14896 			NL_SET_ERR_MSG_ATTR(extack, attr,
14897 					    "unexpected nested data");
14898 			return -EINVAL;
14899 		}
14900 
14901 		return 0;
14902 	}
14903 
14904 	if (!(attr->nla_type & NLA_F_NESTED)) {
14905 		NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
14906 		return -EINVAL;
14907 	}
14908 
14909 	return nla_validate_nested(attr, vcmd->maxattr, vcmd->policy, extack);
14910 }
14911 
14912 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
14913 {
14914 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14915 	struct wireless_dev *wdev =
14916 		__cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
14917 					   info->attrs);
14918 	int i, err;
14919 	u32 vid, subcmd;
14920 
14921 	if (!rdev->wiphy.vendor_commands)
14922 		return -EOPNOTSUPP;
14923 
14924 	if (IS_ERR(wdev)) {
14925 		err = PTR_ERR(wdev);
14926 		if (err != -EINVAL)
14927 			return err;
14928 		wdev = NULL;
14929 	} else if (wdev->wiphy != &rdev->wiphy) {
14930 		return -EINVAL;
14931 	}
14932 
14933 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
14934 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
14935 		return -EINVAL;
14936 
14937 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
14938 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
14939 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
14940 		const struct wiphy_vendor_command *vcmd;
14941 		void *data = NULL;
14942 		int len = 0;
14943 
14944 		vcmd = &rdev->wiphy.vendor_commands[i];
14945 
14946 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
14947 			continue;
14948 
14949 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
14950 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
14951 			if (!wdev)
14952 				return -EINVAL;
14953 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
14954 			    !wdev->netdev)
14955 				return -EINVAL;
14956 
14957 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
14958 				if (!wdev_running(wdev))
14959 					return -ENETDOWN;
14960 			}
14961 		} else {
14962 			wdev = NULL;
14963 		}
14964 
14965 		if (!vcmd->doit)
14966 			return -EOPNOTSUPP;
14967 
14968 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
14969 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
14970 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
14971 
14972 			err = nl80211_vendor_check_policy(vcmd,
14973 					info->attrs[NL80211_ATTR_VENDOR_DATA],
14974 					info->extack);
14975 			if (err)
14976 				return err;
14977 		}
14978 
14979 		rdev->cur_cmd_info = info;
14980 		err = vcmd->doit(&rdev->wiphy, wdev, data, len);
14981 		rdev->cur_cmd_info = NULL;
14982 		return err;
14983 	}
14984 
14985 	return -EOPNOTSUPP;
14986 }
14987 
14988 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
14989 				       struct netlink_callback *cb,
14990 				       struct cfg80211_registered_device **rdev,
14991 				       struct wireless_dev **wdev)
14992 {
14993 	struct nlattr **attrbuf;
14994 	u32 vid, subcmd;
14995 	unsigned int i;
14996 	int vcmd_idx = -1;
14997 	int err;
14998 	void *data = NULL;
14999 	unsigned int data_len = 0;
15000 
15001 	if (cb->args[0]) {
15002 		/* subtract the 1 again here */
15003 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
15004 		struct wireless_dev *tmp;
15005 
15006 		if (!wiphy)
15007 			return -ENODEV;
15008 		*rdev = wiphy_to_rdev(wiphy);
15009 		*wdev = NULL;
15010 
15011 		if (cb->args[1]) {
15012 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
15013 				if (tmp->identifier == cb->args[1] - 1) {
15014 					*wdev = tmp;
15015 					break;
15016 				}
15017 			}
15018 		}
15019 
15020 		/* keep rtnl locked in successful case */
15021 		return 0;
15022 	}
15023 
15024 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
15025 	if (!attrbuf)
15026 		return -ENOMEM;
15027 
15028 	err = nlmsg_parse_deprecated(cb->nlh,
15029 				     GENL_HDRLEN + nl80211_fam.hdrsize,
15030 				     attrbuf, nl80211_fam.maxattr,
15031 				     nl80211_policy, NULL);
15032 	if (err)
15033 		goto out;
15034 
15035 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
15036 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
15037 		err = -EINVAL;
15038 		goto out;
15039 	}
15040 
15041 	*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(skb->sk), attrbuf);
15042 	if (IS_ERR(*wdev))
15043 		*wdev = NULL;
15044 
15045 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
15046 	if (IS_ERR(*rdev)) {
15047 		err = PTR_ERR(*rdev);
15048 		goto out;
15049 	}
15050 
15051 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
15052 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
15053 
15054 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
15055 		const struct wiphy_vendor_command *vcmd;
15056 
15057 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
15058 
15059 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
15060 			continue;
15061 
15062 		if (!vcmd->dumpit) {
15063 			err = -EOPNOTSUPP;
15064 			goto out;
15065 		}
15066 
15067 		vcmd_idx = i;
15068 		break;
15069 	}
15070 
15071 	if (vcmd_idx < 0) {
15072 		err = -EOPNOTSUPP;
15073 		goto out;
15074 	}
15075 
15076 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
15077 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
15078 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
15079 
15080 		err = nl80211_vendor_check_policy(
15081 				&(*rdev)->wiphy.vendor_commands[vcmd_idx],
15082 				attrbuf[NL80211_ATTR_VENDOR_DATA],
15083 				cb->extack);
15084 		if (err)
15085 			goto out;
15086 	}
15087 
15088 	/* 0 is the first index - add 1 to parse only once */
15089 	cb->args[0] = (*rdev)->wiphy_idx + 1;
15090 	/* add 1 to know if it was NULL */
15091 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
15092 	cb->args[2] = vcmd_idx;
15093 	cb->args[3] = (unsigned long)data;
15094 	cb->args[4] = data_len;
15095 
15096 	/* keep rtnl locked in successful case */
15097 	err = 0;
15098 out:
15099 	kfree(attrbuf);
15100 	return err;
15101 }
15102 
15103 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
15104 				   struct netlink_callback *cb)
15105 {
15106 	struct cfg80211_registered_device *rdev;
15107 	struct wireless_dev *wdev;
15108 	unsigned int vcmd_idx;
15109 	const struct wiphy_vendor_command *vcmd;
15110 	void *data;
15111 	int data_len;
15112 	int err;
15113 	struct nlattr *vendor_data;
15114 
15115 	rtnl_lock();
15116 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
15117 	if (err)
15118 		goto out;
15119 
15120 	vcmd_idx = cb->args[2];
15121 	data = (void *)cb->args[3];
15122 	data_len = cb->args[4];
15123 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
15124 
15125 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
15126 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
15127 		if (!wdev) {
15128 			err = -EINVAL;
15129 			goto out;
15130 		}
15131 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
15132 		    !wdev->netdev) {
15133 			err = -EINVAL;
15134 			goto out;
15135 		}
15136 
15137 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
15138 			if (!wdev_running(wdev)) {
15139 				err = -ENETDOWN;
15140 				goto out;
15141 			}
15142 		}
15143 	}
15144 
15145 	while (1) {
15146 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
15147 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
15148 					   NL80211_CMD_VENDOR);
15149 		if (!hdr)
15150 			break;
15151 
15152 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15153 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
15154 					       wdev_id(wdev),
15155 					       NL80211_ATTR_PAD))) {
15156 			genlmsg_cancel(skb, hdr);
15157 			break;
15158 		}
15159 
15160 		vendor_data = nla_nest_start_noflag(skb,
15161 						    NL80211_ATTR_VENDOR_DATA);
15162 		if (!vendor_data) {
15163 			genlmsg_cancel(skb, hdr);
15164 			break;
15165 		}
15166 
15167 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
15168 				   (unsigned long *)&cb->args[5]);
15169 		nla_nest_end(skb, vendor_data);
15170 
15171 		if (err == -ENOBUFS || err == -ENOENT) {
15172 			genlmsg_cancel(skb, hdr);
15173 			break;
15174 		} else if (err <= 0) {
15175 			genlmsg_cancel(skb, hdr);
15176 			goto out;
15177 		}
15178 
15179 		genlmsg_end(skb, hdr);
15180 	}
15181 
15182 	err = skb->len;
15183  out:
15184 	rtnl_unlock();
15185 	return err;
15186 }
15187 
15188 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
15189 					   enum nl80211_commands cmd,
15190 					   enum nl80211_attrs attr,
15191 					   int approxlen)
15192 {
15193 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15194 
15195 	if (WARN_ON(!rdev->cur_cmd_info))
15196 		return NULL;
15197 
15198 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
15199 					   rdev->cur_cmd_info->snd_portid,
15200 					   rdev->cur_cmd_info->snd_seq,
15201 					   cmd, attr, NULL, GFP_KERNEL);
15202 }
15203 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
15204 
15205 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
15206 {
15207 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
15208 	void *hdr = ((void **)skb->cb)[1];
15209 	struct nlattr *data = ((void **)skb->cb)[2];
15210 
15211 	/* clear CB data for netlink core to own from now on */
15212 	memset(skb->cb, 0, sizeof(skb->cb));
15213 
15214 	if (WARN_ON(!rdev->cur_cmd_info)) {
15215 		kfree_skb(skb);
15216 		return -EINVAL;
15217 	}
15218 
15219 	nla_nest_end(skb, data);
15220 	genlmsg_end(skb, hdr);
15221 	return genlmsg_reply(skb, rdev->cur_cmd_info);
15222 }
15223 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
15224 
15225 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
15226 {
15227 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15228 
15229 	if (WARN_ON(!rdev->cur_cmd_info))
15230 		return 0;
15231 
15232 	return rdev->cur_cmd_info->snd_portid;
15233 }
15234 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
15235 
15236 static int nl80211_set_qos_map(struct sk_buff *skb,
15237 			       struct genl_info *info)
15238 {
15239 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15240 	struct cfg80211_qos_map *qos_map = NULL;
15241 	struct net_device *dev = info->user_ptr[1];
15242 	u8 *pos, len, num_des, des_len, des;
15243 	int ret;
15244 
15245 	if (!rdev->ops->set_qos_map)
15246 		return -EOPNOTSUPP;
15247 
15248 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
15249 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
15250 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
15251 
15252 		if (len % 2)
15253 			return -EINVAL;
15254 
15255 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
15256 		if (!qos_map)
15257 			return -ENOMEM;
15258 
15259 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
15260 		if (num_des) {
15261 			des_len = num_des *
15262 				sizeof(struct cfg80211_dscp_exception);
15263 			memcpy(qos_map->dscp_exception, pos, des_len);
15264 			qos_map->num_des = num_des;
15265 			for (des = 0; des < num_des; des++) {
15266 				if (qos_map->dscp_exception[des].up > 7) {
15267 					kfree(qos_map);
15268 					return -EINVAL;
15269 				}
15270 			}
15271 			pos += des_len;
15272 		}
15273 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
15274 	}
15275 
15276 	wdev_lock(dev->ieee80211_ptr);
15277 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
15278 	if (!ret)
15279 		ret = rdev_set_qos_map(rdev, dev, qos_map);
15280 	wdev_unlock(dev->ieee80211_ptr);
15281 
15282 	kfree(qos_map);
15283 	return ret;
15284 }
15285 
15286 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
15287 {
15288 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15289 	struct net_device *dev = info->user_ptr[1];
15290 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15291 	const u8 *peer;
15292 	u8 tsid, up;
15293 	u16 admitted_time = 0;
15294 	int err;
15295 
15296 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
15297 		return -EOPNOTSUPP;
15298 
15299 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
15300 	    !info->attrs[NL80211_ATTR_USER_PRIO])
15301 		return -EINVAL;
15302 
15303 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15304 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
15305 
15306 	/* WMM uses TIDs 0-7 even for TSPEC */
15307 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
15308 		/* TODO: handle 802.11 TSPEC/admission control
15309 		 * need more attributes for that (e.g. BA session requirement);
15310 		 * change the WMM adminssion test above to allow both then
15311 		 */
15312 		return -EINVAL;
15313 	}
15314 
15315 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15316 
15317 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
15318 		admitted_time =
15319 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
15320 		if (!admitted_time)
15321 			return -EINVAL;
15322 	}
15323 
15324 	wdev_lock(wdev);
15325 	switch (wdev->iftype) {
15326 	case NL80211_IFTYPE_STATION:
15327 	case NL80211_IFTYPE_P2P_CLIENT:
15328 		if (wdev->connected)
15329 			break;
15330 		err = -ENOTCONN;
15331 		goto out;
15332 	default:
15333 		err = -EOPNOTSUPP;
15334 		goto out;
15335 	}
15336 
15337 	err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
15338 
15339  out:
15340 	wdev_unlock(wdev);
15341 	return err;
15342 }
15343 
15344 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
15345 {
15346 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15347 	struct net_device *dev = info->user_ptr[1];
15348 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15349 	const u8 *peer;
15350 	u8 tsid;
15351 	int err;
15352 
15353 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
15354 		return -EINVAL;
15355 
15356 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15357 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15358 
15359 	wdev_lock(wdev);
15360 	err = rdev_del_tx_ts(rdev, dev, tsid, peer);
15361 	wdev_unlock(wdev);
15362 
15363 	return err;
15364 }
15365 
15366 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
15367 				       struct genl_info *info)
15368 {
15369 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15370 	struct net_device *dev = info->user_ptr[1];
15371 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15372 	struct cfg80211_chan_def chandef = {};
15373 	const u8 *addr;
15374 	u8 oper_class;
15375 	int err;
15376 
15377 	if (!rdev->ops->tdls_channel_switch ||
15378 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15379 		return -EOPNOTSUPP;
15380 
15381 	switch (dev->ieee80211_ptr->iftype) {
15382 	case NL80211_IFTYPE_STATION:
15383 	case NL80211_IFTYPE_P2P_CLIENT:
15384 		break;
15385 	default:
15386 		return -EOPNOTSUPP;
15387 	}
15388 
15389 	if (!info->attrs[NL80211_ATTR_MAC] ||
15390 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
15391 		return -EINVAL;
15392 
15393 	err = nl80211_parse_chandef(rdev, info, &chandef);
15394 	if (err)
15395 		return err;
15396 
15397 	/*
15398 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
15399 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
15400 	 * specification is not defined for them.
15401 	 */
15402 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
15403 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
15404 	    chandef.width != NL80211_CHAN_WIDTH_20)
15405 		return -EINVAL;
15406 
15407 	/* we will be active on the TDLS link */
15408 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
15409 					   wdev->iftype))
15410 		return -EINVAL;
15411 
15412 	/* don't allow switching to DFS channels */
15413 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
15414 		return -EINVAL;
15415 
15416 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15417 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
15418 
15419 	wdev_lock(wdev);
15420 	err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
15421 	wdev_unlock(wdev);
15422 
15423 	return err;
15424 }
15425 
15426 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
15427 					      struct genl_info *info)
15428 {
15429 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15430 	struct net_device *dev = info->user_ptr[1];
15431 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15432 	const u8 *addr;
15433 
15434 	if (!rdev->ops->tdls_channel_switch ||
15435 	    !rdev->ops->tdls_cancel_channel_switch ||
15436 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15437 		return -EOPNOTSUPP;
15438 
15439 	switch (dev->ieee80211_ptr->iftype) {
15440 	case NL80211_IFTYPE_STATION:
15441 	case NL80211_IFTYPE_P2P_CLIENT:
15442 		break;
15443 	default:
15444 		return -EOPNOTSUPP;
15445 	}
15446 
15447 	if (!info->attrs[NL80211_ATTR_MAC])
15448 		return -EINVAL;
15449 
15450 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15451 
15452 	wdev_lock(wdev);
15453 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
15454 	wdev_unlock(wdev);
15455 
15456 	return 0;
15457 }
15458 
15459 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
15460 					    struct genl_info *info)
15461 {
15462 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15463 	struct net_device *dev = info->user_ptr[1];
15464 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15465 	const struct nlattr *nla;
15466 	bool enabled;
15467 
15468 	if (!rdev->ops->set_multicast_to_unicast)
15469 		return -EOPNOTSUPP;
15470 
15471 	if (wdev->iftype != NL80211_IFTYPE_AP &&
15472 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
15473 		return -EOPNOTSUPP;
15474 
15475 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
15476 	enabled = nla_get_flag(nla);
15477 
15478 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
15479 }
15480 
15481 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
15482 {
15483 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15484 	struct net_device *dev = info->user_ptr[1];
15485 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15486 	struct cfg80211_pmk_conf pmk_conf = {};
15487 	int ret;
15488 
15489 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15490 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15491 		return -EOPNOTSUPP;
15492 
15493 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15494 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15495 		return -EOPNOTSUPP;
15496 
15497 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
15498 		return -EINVAL;
15499 
15500 	wdev_lock(wdev);
15501 	if (!wdev->connected) {
15502 		ret = -ENOTCONN;
15503 		goto out;
15504 	}
15505 
15506 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15507 	if (memcmp(pmk_conf.aa, wdev->u.client.connected_addr, ETH_ALEN)) {
15508 		ret = -EINVAL;
15509 		goto out;
15510 	}
15511 
15512 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
15513 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
15514 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
15515 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
15516 		ret = -EINVAL;
15517 		goto out;
15518 	}
15519 
15520 	if (info->attrs[NL80211_ATTR_PMKR0_NAME])
15521 		pmk_conf.pmk_r0_name =
15522 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
15523 
15524 	ret = rdev_set_pmk(rdev, dev, &pmk_conf);
15525 out:
15526 	wdev_unlock(wdev);
15527 	return ret;
15528 }
15529 
15530 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
15531 {
15532 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15533 	struct net_device *dev = info->user_ptr[1];
15534 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15535 	const u8 *aa;
15536 	int ret;
15537 
15538 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15539 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15540 		return -EOPNOTSUPP;
15541 
15542 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15543 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15544 		return -EOPNOTSUPP;
15545 
15546 	if (!info->attrs[NL80211_ATTR_MAC])
15547 		return -EINVAL;
15548 
15549 	wdev_lock(wdev);
15550 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15551 	ret = rdev_del_pmk(rdev, dev, aa);
15552 	wdev_unlock(wdev);
15553 
15554 	return ret;
15555 }
15556 
15557 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
15558 {
15559 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15560 	struct net_device *dev = info->user_ptr[1];
15561 	struct cfg80211_external_auth_params params;
15562 
15563 	if (!rdev->ops->external_auth)
15564 		return -EOPNOTSUPP;
15565 
15566 	if (!info->attrs[NL80211_ATTR_SSID] &&
15567 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
15568 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
15569 		return -EINVAL;
15570 
15571 	if (!info->attrs[NL80211_ATTR_BSSID])
15572 		return -EINVAL;
15573 
15574 	if (!info->attrs[NL80211_ATTR_STATUS_CODE])
15575 		return -EINVAL;
15576 
15577 	memset(&params, 0, sizeof(params));
15578 
15579 	if (info->attrs[NL80211_ATTR_SSID]) {
15580 		params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
15581 		if (params.ssid.ssid_len == 0)
15582 			return -EINVAL;
15583 		memcpy(params.ssid.ssid,
15584 		       nla_data(info->attrs[NL80211_ATTR_SSID]),
15585 		       params.ssid.ssid_len);
15586 	}
15587 
15588 	memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
15589 	       ETH_ALEN);
15590 
15591 	params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15592 
15593 	if (info->attrs[NL80211_ATTR_PMKID])
15594 		params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
15595 
15596 	return rdev_external_auth(rdev, dev, &params);
15597 }
15598 
15599 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
15600 {
15601 	bool dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
15602 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15603 	struct net_device *dev = info->user_ptr[1];
15604 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15605 	const u8 *buf;
15606 	size_t len;
15607 	u8 *dest;
15608 	u16 proto;
15609 	bool noencrypt;
15610 	u64 cookie = 0;
15611 	int link_id;
15612 	int err;
15613 
15614 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15615 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
15616 		return -EOPNOTSUPP;
15617 
15618 	if (!rdev->ops->tx_control_port)
15619 		return -EOPNOTSUPP;
15620 
15621 	if (!info->attrs[NL80211_ATTR_FRAME] ||
15622 	    !info->attrs[NL80211_ATTR_MAC] ||
15623 	    !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
15624 		GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
15625 		return -EINVAL;
15626 	}
15627 
15628 	wdev_lock(wdev);
15629 
15630 	switch (wdev->iftype) {
15631 	case NL80211_IFTYPE_AP:
15632 	case NL80211_IFTYPE_P2P_GO:
15633 	case NL80211_IFTYPE_MESH_POINT:
15634 		break;
15635 	case NL80211_IFTYPE_ADHOC:
15636 		if (wdev->u.ibss.current_bss)
15637 			break;
15638 		err = -ENOTCONN;
15639 		goto out;
15640 	case NL80211_IFTYPE_STATION:
15641 	case NL80211_IFTYPE_P2P_CLIENT:
15642 		if (wdev->connected)
15643 			break;
15644 		err = -ENOTCONN;
15645 		goto out;
15646 	default:
15647 		err = -EOPNOTSUPP;
15648 		goto out;
15649 	}
15650 
15651 	wdev_unlock(wdev);
15652 
15653 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15654 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15655 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15656 	proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
15657 	noencrypt =
15658 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
15659 
15660 	link_id = nl80211_link_id_or_invalid(info->attrs);
15661 
15662 	err = rdev_tx_control_port(rdev, dev, buf, len,
15663 				   dest, cpu_to_be16(proto), noencrypt, link_id,
15664 				   dont_wait_for_ack ? NULL : &cookie);
15665 	if (!err && !dont_wait_for_ack)
15666 		nl_set_extack_cookie_u64(info->extack, cookie);
15667 	return err;
15668  out:
15669 	wdev_unlock(wdev);
15670 	return err;
15671 }
15672 
15673 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
15674 					   struct genl_info *info)
15675 {
15676 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15677 	struct net_device *dev = info->user_ptr[1];
15678 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15679 	struct cfg80211_ftm_responder_stats ftm_stats = {};
15680 	unsigned int link_id = nl80211_link_id(info->attrs);
15681 	struct sk_buff *msg;
15682 	void *hdr;
15683 	struct nlattr *ftm_stats_attr;
15684 	int err;
15685 
15686 	if (wdev->iftype != NL80211_IFTYPE_AP ||
15687 	    !wdev->links[link_id].ap.beacon_interval)
15688 		return -EOPNOTSUPP;
15689 
15690 	err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
15691 	if (err)
15692 		return err;
15693 
15694 	if (!ftm_stats.filled)
15695 		return -ENODATA;
15696 
15697 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15698 	if (!msg)
15699 		return -ENOMEM;
15700 
15701 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
15702 			     NL80211_CMD_GET_FTM_RESPONDER_STATS);
15703 	if (!hdr)
15704 		goto nla_put_failure;
15705 
15706 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15707 		goto nla_put_failure;
15708 
15709 	ftm_stats_attr = nla_nest_start_noflag(msg,
15710 					       NL80211_ATTR_FTM_RESPONDER_STATS);
15711 	if (!ftm_stats_attr)
15712 		goto nla_put_failure;
15713 
15714 #define SET_FTM(field, name, type)					 \
15715 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15716 	    nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,		 \
15717 			     ftm_stats.field))				 \
15718 		goto nla_put_failure; } while (0)
15719 #define SET_FTM_U64(field, name)					 \
15720 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15721 	    nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,		 \
15722 			      ftm_stats.field, NL80211_FTM_STATS_PAD))	 \
15723 		goto nla_put_failure; } while (0)
15724 
15725 	SET_FTM(success_num, SUCCESS_NUM, u32);
15726 	SET_FTM(partial_num, PARTIAL_NUM, u32);
15727 	SET_FTM(failed_num, FAILED_NUM, u32);
15728 	SET_FTM(asap_num, ASAP_NUM, u32);
15729 	SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
15730 	SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
15731 	SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
15732 	SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
15733 	SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
15734 #undef SET_FTM
15735 
15736 	nla_nest_end(msg, ftm_stats_attr);
15737 
15738 	genlmsg_end(msg, hdr);
15739 	return genlmsg_reply(msg, info);
15740 
15741 nla_put_failure:
15742 	nlmsg_free(msg);
15743 	return -ENOBUFS;
15744 }
15745 
15746 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
15747 {
15748 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15749 	struct cfg80211_update_owe_info owe_info;
15750 	struct net_device *dev = info->user_ptr[1];
15751 
15752 	if (!rdev->ops->update_owe_info)
15753 		return -EOPNOTSUPP;
15754 
15755 	if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
15756 	    !info->attrs[NL80211_ATTR_MAC])
15757 		return -EINVAL;
15758 
15759 	memset(&owe_info, 0, sizeof(owe_info));
15760 	owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15761 	nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
15762 
15763 	if (info->attrs[NL80211_ATTR_IE]) {
15764 		owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
15765 		owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
15766 	}
15767 
15768 	return rdev_update_owe_info(rdev, dev, &owe_info);
15769 }
15770 
15771 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
15772 {
15773 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15774 	struct net_device *dev = info->user_ptr[1];
15775 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15776 	struct station_info sinfo = {};
15777 	const u8 *buf;
15778 	size_t len;
15779 	u8 *dest;
15780 	int err;
15781 
15782 	if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
15783 		return -EOPNOTSUPP;
15784 
15785 	if (!info->attrs[NL80211_ATTR_MAC] ||
15786 	    !info->attrs[NL80211_ATTR_FRAME]) {
15787 		GENL_SET_ERR_MSG(info, "Frame or MAC missing");
15788 		return -EINVAL;
15789 	}
15790 
15791 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
15792 		return -EOPNOTSUPP;
15793 
15794 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15795 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15796 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15797 
15798 	if (len < sizeof(struct ethhdr))
15799 		return -EINVAL;
15800 
15801 	if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
15802 	    !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
15803 		return -EINVAL;
15804 
15805 	err = rdev_get_station(rdev, dev, dest, &sinfo);
15806 	if (err)
15807 		return err;
15808 
15809 	cfg80211_sinfo_release_content(&sinfo);
15810 
15811 	return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
15812 }
15813 
15814 static int parse_tid_conf(struct cfg80211_registered_device *rdev,
15815 			  struct nlattr *attrs[], struct net_device *dev,
15816 			  struct cfg80211_tid_cfg *tid_conf,
15817 			  struct genl_info *info, const u8 *peer,
15818 			  unsigned int link_id)
15819 {
15820 	struct netlink_ext_ack *extack = info->extack;
15821 	u64 mask;
15822 	int err;
15823 
15824 	if (!attrs[NL80211_TID_CONFIG_ATTR_TIDS])
15825 		return -EINVAL;
15826 
15827 	tid_conf->config_override =
15828 			nla_get_flag(attrs[NL80211_TID_CONFIG_ATTR_OVERRIDE]);
15829 	tid_conf->tids = nla_get_u16(attrs[NL80211_TID_CONFIG_ATTR_TIDS]);
15830 
15831 	if (tid_conf->config_override) {
15832 		if (rdev->ops->reset_tid_config) {
15833 			err = rdev_reset_tid_config(rdev, dev, peer,
15834 						    tid_conf->tids);
15835 			if (err)
15836 				return err;
15837 		} else {
15838 			return -EINVAL;
15839 		}
15840 	}
15841 
15842 	if (attrs[NL80211_TID_CONFIG_ATTR_NOACK]) {
15843 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_NOACK);
15844 		tid_conf->noack =
15845 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_NOACK]);
15846 	}
15847 
15848 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]) {
15849 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT);
15850 		tid_conf->retry_short =
15851 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]);
15852 
15853 		if (tid_conf->retry_short > rdev->wiphy.max_data_retry_count)
15854 			return -EINVAL;
15855 	}
15856 
15857 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]) {
15858 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
15859 		tid_conf->retry_long =
15860 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]);
15861 
15862 		if (tid_conf->retry_long > rdev->wiphy.max_data_retry_count)
15863 			return -EINVAL;
15864 	}
15865 
15866 	if (attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]) {
15867 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
15868 		tid_conf->ampdu =
15869 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]);
15870 	}
15871 
15872 	if (attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]) {
15873 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
15874 		tid_conf->rtscts =
15875 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]);
15876 	}
15877 
15878 	if (attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]) {
15879 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
15880 		tid_conf->amsdu =
15881 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]);
15882 	}
15883 
15884 	if (attrs[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE]) {
15885 		u32 idx = NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE, attr;
15886 
15887 		tid_conf->txrate_type = nla_get_u8(attrs[idx]);
15888 
15889 		if (tid_conf->txrate_type != NL80211_TX_RATE_AUTOMATIC) {
15890 			attr = NL80211_TID_CONFIG_ATTR_TX_RATE;
15891 			err = nl80211_parse_tx_bitrate_mask(info, attrs, attr,
15892 						    &tid_conf->txrate_mask, dev,
15893 						    true, link_id);
15894 			if (err)
15895 				return err;
15896 
15897 			tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE);
15898 		}
15899 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
15900 	}
15901 
15902 	if (peer)
15903 		mask = rdev->wiphy.tid_config_support.peer;
15904 	else
15905 		mask = rdev->wiphy.tid_config_support.vif;
15906 
15907 	if (tid_conf->mask & ~mask) {
15908 		NL_SET_ERR_MSG(extack, "unsupported TID configuration");
15909 		return -ENOTSUPP;
15910 	}
15911 
15912 	return 0;
15913 }
15914 
15915 static int nl80211_set_tid_config(struct sk_buff *skb,
15916 				  struct genl_info *info)
15917 {
15918 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15919 	struct nlattr *attrs[NL80211_TID_CONFIG_ATTR_MAX + 1];
15920 	unsigned int link_id = nl80211_link_id(info->attrs);
15921 	struct net_device *dev = info->user_ptr[1];
15922 	struct cfg80211_tid_config *tid_config;
15923 	struct nlattr *tid;
15924 	int conf_idx = 0, rem_conf;
15925 	int ret = -EINVAL;
15926 	u32 num_conf = 0;
15927 
15928 	if (!info->attrs[NL80211_ATTR_TID_CONFIG])
15929 		return -EINVAL;
15930 
15931 	if (!rdev->ops->set_tid_config)
15932 		return -EOPNOTSUPP;
15933 
15934 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
15935 			    rem_conf)
15936 		num_conf++;
15937 
15938 	tid_config = kzalloc(struct_size(tid_config, tid_conf, num_conf),
15939 			     GFP_KERNEL);
15940 	if (!tid_config)
15941 		return -ENOMEM;
15942 
15943 	tid_config->n_tid_conf = num_conf;
15944 
15945 	if (info->attrs[NL80211_ATTR_MAC])
15946 		tid_config->peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15947 
15948 	wdev_lock(dev->ieee80211_ptr);
15949 
15950 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
15951 			    rem_conf) {
15952 		ret = nla_parse_nested(attrs, NL80211_TID_CONFIG_ATTR_MAX,
15953 				       tid, NULL, NULL);
15954 
15955 		if (ret)
15956 			goto bad_tid_conf;
15957 
15958 		ret = parse_tid_conf(rdev, attrs, dev,
15959 				     &tid_config->tid_conf[conf_idx],
15960 				     info, tid_config->peer, link_id);
15961 		if (ret)
15962 			goto bad_tid_conf;
15963 
15964 		conf_idx++;
15965 	}
15966 
15967 	ret = rdev_set_tid_config(rdev, dev, tid_config);
15968 
15969 bad_tid_conf:
15970 	kfree(tid_config);
15971 	wdev_unlock(dev->ieee80211_ptr);
15972 	return ret;
15973 }
15974 
15975 static int nl80211_color_change(struct sk_buff *skb, struct genl_info *info)
15976 {
15977 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15978 	struct cfg80211_color_change_settings params = {};
15979 	struct net_device *dev = info->user_ptr[1];
15980 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15981 	struct nlattr **tb;
15982 	u16 offset;
15983 	int err;
15984 
15985 	if (!rdev->ops->color_change)
15986 		return -EOPNOTSUPP;
15987 
15988 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15989 				     NL80211_EXT_FEATURE_BSS_COLOR))
15990 		return -EOPNOTSUPP;
15991 
15992 	if (wdev->iftype != NL80211_IFTYPE_AP)
15993 		return -EOPNOTSUPP;
15994 
15995 	if (!info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT] ||
15996 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR] ||
15997 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS])
15998 		return -EINVAL;
15999 
16000 	params.count = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT]);
16001 	params.color = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR]);
16002 
16003 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_next,
16004 				   info->extack);
16005 	if (err)
16006 		return err;
16007 
16008 	tb = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*tb), GFP_KERNEL);
16009 	if (!tb)
16010 		return -ENOMEM;
16011 
16012 	err = nla_parse_nested(tb, NL80211_ATTR_MAX,
16013 			       info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS],
16014 			       nl80211_policy, info->extack);
16015 	if (err)
16016 		goto out;
16017 
16018 	err = nl80211_parse_beacon(rdev, tb, &params.beacon_color_change,
16019 				   info->extack);
16020 	if (err)
16021 		goto out;
16022 
16023 	if (!tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
16024 		err = -EINVAL;
16025 		goto out;
16026 	}
16027 
16028 	if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) != sizeof(u16)) {
16029 		err = -EINVAL;
16030 		goto out;
16031 	}
16032 
16033 	offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
16034 	if (offset >= params.beacon_color_change.tail_len) {
16035 		err = -EINVAL;
16036 		goto out;
16037 	}
16038 
16039 	if (params.beacon_color_change.tail[offset] != params.count) {
16040 		err = -EINVAL;
16041 		goto out;
16042 	}
16043 
16044 	params.counter_offset_beacon = offset;
16045 
16046 	if (tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
16047 		if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) !=
16048 		    sizeof(u16)) {
16049 			err = -EINVAL;
16050 			goto out;
16051 		}
16052 
16053 		offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
16054 		if (offset >= params.beacon_color_change.probe_resp_len) {
16055 			err = -EINVAL;
16056 			goto out;
16057 		}
16058 
16059 		if (params.beacon_color_change.probe_resp[offset] !=
16060 		    params.count) {
16061 			err = -EINVAL;
16062 			goto out;
16063 		}
16064 
16065 		params.counter_offset_presp = offset;
16066 	}
16067 
16068 	wdev_lock(wdev);
16069 	err = rdev_color_change(rdev, dev, &params);
16070 	wdev_unlock(wdev);
16071 
16072 out:
16073 	kfree(params.beacon_next.mbssid_ies);
16074 	kfree(params.beacon_color_change.mbssid_ies);
16075 	kfree(params.beacon_next.rnr_ies);
16076 	kfree(params.beacon_color_change.rnr_ies);
16077 	kfree(tb);
16078 	return err;
16079 }
16080 
16081 static int nl80211_set_fils_aad(struct sk_buff *skb,
16082 				struct genl_info *info)
16083 {
16084 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16085 	struct net_device *dev = info->user_ptr[1];
16086 	struct cfg80211_fils_aad fils_aad = {};
16087 	u8 *nonces;
16088 
16089 	if (!info->attrs[NL80211_ATTR_MAC] ||
16090 	    !info->attrs[NL80211_ATTR_FILS_KEK] ||
16091 	    !info->attrs[NL80211_ATTR_FILS_NONCES])
16092 		return -EINVAL;
16093 
16094 	fils_aad.macaddr = nla_data(info->attrs[NL80211_ATTR_MAC]);
16095 	fils_aad.kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
16096 	fils_aad.kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
16097 	nonces = nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
16098 	fils_aad.snonce = nonces;
16099 	fils_aad.anonce = nonces + FILS_NONCE_LEN;
16100 
16101 	return rdev_set_fils_aad(rdev, dev, &fils_aad);
16102 }
16103 
16104 static int nl80211_add_link(struct sk_buff *skb, struct genl_info *info)
16105 {
16106 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16107 	unsigned int link_id = nl80211_link_id(info->attrs);
16108 	struct net_device *dev = info->user_ptr[1];
16109 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16110 	int ret;
16111 
16112 	if (!(wdev->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
16113 		return -EINVAL;
16114 
16115 	switch (wdev->iftype) {
16116 	case NL80211_IFTYPE_AP:
16117 		break;
16118 	default:
16119 		return -EINVAL;
16120 	}
16121 
16122 	if (!info->attrs[NL80211_ATTR_MAC] ||
16123 	    !is_valid_ether_addr(nla_data(info->attrs[NL80211_ATTR_MAC])))
16124 		return -EINVAL;
16125 
16126 	wdev_lock(wdev);
16127 	wdev->valid_links |= BIT(link_id);
16128 	ether_addr_copy(wdev->links[link_id].addr,
16129 			nla_data(info->attrs[NL80211_ATTR_MAC]));
16130 
16131 	ret = rdev_add_intf_link(rdev, wdev, link_id);
16132 	if (ret) {
16133 		wdev->valid_links &= ~BIT(link_id);
16134 		eth_zero_addr(wdev->links[link_id].addr);
16135 	}
16136 	wdev_unlock(wdev);
16137 
16138 	return ret;
16139 }
16140 
16141 static int nl80211_remove_link(struct sk_buff *skb, struct genl_info *info)
16142 {
16143 	unsigned int link_id = nl80211_link_id(info->attrs);
16144 	struct net_device *dev = info->user_ptr[1];
16145 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16146 
16147 	/* cannot remove if there's no link */
16148 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
16149 		return -EINVAL;
16150 
16151 	switch (wdev->iftype) {
16152 	case NL80211_IFTYPE_AP:
16153 		break;
16154 	default:
16155 		return -EINVAL;
16156 	}
16157 
16158 	wdev_lock(wdev);
16159 	cfg80211_remove_link(wdev, link_id);
16160 	wdev_unlock(wdev);
16161 
16162 	return 0;
16163 }
16164 
16165 static int
16166 nl80211_add_mod_link_station(struct sk_buff *skb, struct genl_info *info,
16167 			     bool add)
16168 {
16169 	struct link_station_parameters params = {};
16170 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16171 	struct net_device *dev = info->user_ptr[1];
16172 	int err;
16173 
16174 	if ((add && !rdev->ops->add_link_station) ||
16175 	    (!add && !rdev->ops->mod_link_station))
16176 		return -EOPNOTSUPP;
16177 
16178 	if (add && !info->attrs[NL80211_ATTR_MAC])
16179 		return -EINVAL;
16180 
16181 	if (!info->attrs[NL80211_ATTR_MLD_ADDR])
16182 		return -EINVAL;
16183 
16184 	if (add && !info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
16185 		return -EINVAL;
16186 
16187 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
16188 
16189 	if (info->attrs[NL80211_ATTR_MAC]) {
16190 		params.link_mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
16191 		if (!is_valid_ether_addr(params.link_mac))
16192 			return -EINVAL;
16193 	}
16194 
16195 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
16196 		return -EINVAL;
16197 
16198 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
16199 
16200 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
16201 		params.supported_rates =
16202 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
16203 		params.supported_rates_len =
16204 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
16205 	}
16206 
16207 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
16208 		params.ht_capa =
16209 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
16210 
16211 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
16212 		params.vht_capa =
16213 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
16214 
16215 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
16216 		params.he_capa =
16217 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
16218 		params.he_capa_len =
16219 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
16220 
16221 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
16222 			params.eht_capa =
16223 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
16224 			params.eht_capa_len =
16225 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
16226 
16227 			if (!ieee80211_eht_capa_size_ok((const u8 *)params.he_capa,
16228 							(const u8 *)params.eht_capa,
16229 							params.eht_capa_len,
16230 							false))
16231 				return -EINVAL;
16232 		}
16233 	}
16234 
16235 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
16236 		params.he_6ghz_capa =
16237 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
16238 
16239 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
16240 		params.opmode_notif_used = true;
16241 		params.opmode_notif =
16242 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
16243 	}
16244 
16245 	err = nl80211_parse_sta_txpower_setting(info, &params.txpwr,
16246 						&params.txpwr_set);
16247 	if (err)
16248 		return err;
16249 
16250 	wdev_lock(dev->ieee80211_ptr);
16251 	if (add)
16252 		err = rdev_add_link_station(rdev, dev, &params);
16253 	else
16254 		err = rdev_mod_link_station(rdev, dev, &params);
16255 	wdev_unlock(dev->ieee80211_ptr);
16256 
16257 	return err;
16258 }
16259 
16260 static int
16261 nl80211_add_link_station(struct sk_buff *skb, struct genl_info *info)
16262 {
16263 	return nl80211_add_mod_link_station(skb, info, true);
16264 }
16265 
16266 static int
16267 nl80211_modify_link_station(struct sk_buff *skb, struct genl_info *info)
16268 {
16269 	return nl80211_add_mod_link_station(skb, info, false);
16270 }
16271 
16272 static int
16273 nl80211_remove_link_station(struct sk_buff *skb, struct genl_info *info)
16274 {
16275 	struct link_station_del_parameters params = {};
16276 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16277 	struct net_device *dev = info->user_ptr[1];
16278 	int ret;
16279 
16280 	if (!rdev->ops->del_link_station)
16281 		return -EOPNOTSUPP;
16282 
16283 	if (!info->attrs[NL80211_ATTR_MLD_ADDR] ||
16284 	    !info->attrs[NL80211_ATTR_MLO_LINK_ID])
16285 		return -EINVAL;
16286 
16287 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
16288 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
16289 
16290 	wdev_lock(dev->ieee80211_ptr);
16291 	ret = rdev_del_link_station(rdev, dev, &params);
16292 	wdev_unlock(dev->ieee80211_ptr);
16293 
16294 	return ret;
16295 }
16296 
16297 static int nl80211_set_hw_timestamp(struct sk_buff *skb,
16298 				    struct genl_info *info)
16299 {
16300 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16301 	struct net_device *dev = info->user_ptr[1];
16302 	struct cfg80211_set_hw_timestamp hwts = {};
16303 
16304 	if (!rdev->wiphy.hw_timestamp_max_peers)
16305 		return -EOPNOTSUPP;
16306 
16307 	if (!info->attrs[NL80211_ATTR_MAC] &&
16308 	    rdev->wiphy.hw_timestamp_max_peers != CFG80211_HW_TIMESTAMP_ALL_PEERS)
16309 		return -EOPNOTSUPP;
16310 
16311 	if (info->attrs[NL80211_ATTR_MAC])
16312 		hwts.macaddr = nla_data(info->attrs[NL80211_ATTR_MAC]);
16313 
16314 	hwts.enable =
16315 		nla_get_flag(info->attrs[NL80211_ATTR_HW_TIMESTAMP_ENABLED]);
16316 
16317 	return rdev_set_hw_timestamp(rdev, dev, &hwts);
16318 }
16319 
16320 #define NL80211_FLAG_NEED_WIPHY		0x01
16321 #define NL80211_FLAG_NEED_NETDEV	0x02
16322 #define NL80211_FLAG_NEED_RTNL		0x04
16323 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
16324 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
16325 					 NL80211_FLAG_CHECK_NETDEV_UP)
16326 #define NL80211_FLAG_NEED_WDEV		0x10
16327 /* If a netdev is associated, it must be UP, P2P must be started */
16328 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
16329 					 NL80211_FLAG_CHECK_NETDEV_UP)
16330 #define NL80211_FLAG_CLEAR_SKB		0x20
16331 #define NL80211_FLAG_NO_WIPHY_MTX	0x40
16332 #define NL80211_FLAG_MLO_VALID_LINK_ID	0x80
16333 #define NL80211_FLAG_MLO_UNSUPPORTED	0x100
16334 
16335 #define INTERNAL_FLAG_SELECTORS(__sel)			\
16336 	SELECTOR(__sel, NONE, 0) /* must be first */	\
16337 	SELECTOR(__sel, WIPHY,				\
16338 		 NL80211_FLAG_NEED_WIPHY)		\
16339 	SELECTOR(__sel, WDEV,				\
16340 		 NL80211_FLAG_NEED_WDEV)		\
16341 	SELECTOR(__sel, NETDEV,				\
16342 		 NL80211_FLAG_NEED_NETDEV)		\
16343 	SELECTOR(__sel, NETDEV_LINK,			\
16344 		 NL80211_FLAG_NEED_NETDEV |		\
16345 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16346 	SELECTOR(__sel, NETDEV_NO_MLO,			\
16347 		 NL80211_FLAG_NEED_NETDEV |		\
16348 		 NL80211_FLAG_MLO_UNSUPPORTED)	\
16349 	SELECTOR(__sel, WIPHY_RTNL,			\
16350 		 NL80211_FLAG_NEED_WIPHY |		\
16351 		 NL80211_FLAG_NEED_RTNL)		\
16352 	SELECTOR(__sel, WIPHY_RTNL_NOMTX,		\
16353 		 NL80211_FLAG_NEED_WIPHY |		\
16354 		 NL80211_FLAG_NEED_RTNL |		\
16355 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16356 	SELECTOR(__sel, WDEV_RTNL,			\
16357 		 NL80211_FLAG_NEED_WDEV |		\
16358 		 NL80211_FLAG_NEED_RTNL)		\
16359 	SELECTOR(__sel, NETDEV_RTNL,			\
16360 		 NL80211_FLAG_NEED_NETDEV |		\
16361 		 NL80211_FLAG_NEED_RTNL)		\
16362 	SELECTOR(__sel, NETDEV_UP,			\
16363 		 NL80211_FLAG_NEED_NETDEV_UP)		\
16364 	SELECTOR(__sel, NETDEV_UP_LINK,			\
16365 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16366 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16367 	SELECTOR(__sel, NETDEV_UP_NO_MLO,		\
16368 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16369 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16370 	SELECTOR(__sel, NETDEV_UP_NO_MLO_CLEAR,		\
16371 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16372 		 NL80211_FLAG_CLEAR_SKB |		\
16373 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16374 	SELECTOR(__sel, NETDEV_UP_NOTMX,		\
16375 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16376 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16377 	SELECTOR(__sel, NETDEV_UP_NOTMX_NOMLO,		\
16378 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16379 		 NL80211_FLAG_NO_WIPHY_MTX |		\
16380 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16381 	SELECTOR(__sel, NETDEV_UP_CLEAR,		\
16382 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16383 		 NL80211_FLAG_CLEAR_SKB)		\
16384 	SELECTOR(__sel, WDEV_UP,			\
16385 		 NL80211_FLAG_NEED_WDEV_UP)		\
16386 	SELECTOR(__sel, WDEV_UP_LINK,			\
16387 		 NL80211_FLAG_NEED_WDEV_UP |		\
16388 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16389 	SELECTOR(__sel, WDEV_UP_RTNL,			\
16390 		 NL80211_FLAG_NEED_WDEV_UP |		\
16391 		 NL80211_FLAG_NEED_RTNL)		\
16392 	SELECTOR(__sel, WIPHY_CLEAR,			\
16393 		 NL80211_FLAG_NEED_WIPHY |		\
16394 		 NL80211_FLAG_CLEAR_SKB)
16395 
16396 enum nl80211_internal_flags_selector {
16397 #define SELECTOR(_, name, value)	NL80211_IFL_SEL_##name,
16398 	INTERNAL_FLAG_SELECTORS(_)
16399 #undef SELECTOR
16400 };
16401 
16402 static u32 nl80211_internal_flags[] = {
16403 #define SELECTOR(_, name, value)	[NL80211_IFL_SEL_##name] = value,
16404 	INTERNAL_FLAG_SELECTORS(_)
16405 #undef SELECTOR
16406 };
16407 
16408 static int nl80211_pre_doit(const struct genl_split_ops *ops,
16409 			    struct sk_buff *skb,
16410 			    struct genl_info *info)
16411 {
16412 	struct cfg80211_registered_device *rdev = NULL;
16413 	struct wireless_dev *wdev = NULL;
16414 	struct net_device *dev = NULL;
16415 	u32 internal_flags;
16416 	int err;
16417 
16418 	if (WARN_ON(ops->internal_flags >= ARRAY_SIZE(nl80211_internal_flags)))
16419 		return -EINVAL;
16420 
16421 	internal_flags = nl80211_internal_flags[ops->internal_flags];
16422 
16423 	rtnl_lock();
16424 	if (internal_flags & NL80211_FLAG_NEED_WIPHY) {
16425 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
16426 		if (IS_ERR(rdev)) {
16427 			err = PTR_ERR(rdev);
16428 			goto out_unlock;
16429 		}
16430 		info->user_ptr[0] = rdev;
16431 	} else if (internal_flags & NL80211_FLAG_NEED_NETDEV ||
16432 		   internal_flags & NL80211_FLAG_NEED_WDEV) {
16433 		wdev = __cfg80211_wdev_from_attrs(NULL, genl_info_net(info),
16434 						  info->attrs);
16435 		if (IS_ERR(wdev)) {
16436 			err = PTR_ERR(wdev);
16437 			goto out_unlock;
16438 		}
16439 
16440 		dev = wdev->netdev;
16441 		dev_hold(dev);
16442 		rdev = wiphy_to_rdev(wdev->wiphy);
16443 
16444 		if (internal_flags & NL80211_FLAG_NEED_NETDEV) {
16445 			if (!dev) {
16446 				err = -EINVAL;
16447 				goto out_unlock;
16448 			}
16449 
16450 			info->user_ptr[1] = dev;
16451 		} else {
16452 			info->user_ptr[1] = wdev;
16453 		}
16454 
16455 		if (internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
16456 		    !wdev_running(wdev)) {
16457 			err = -ENETDOWN;
16458 			goto out_unlock;
16459 		}
16460 
16461 		info->user_ptr[0] = rdev;
16462 	}
16463 
16464 	if (internal_flags & NL80211_FLAG_MLO_VALID_LINK_ID) {
16465 		struct nlattr *link_id = info->attrs[NL80211_ATTR_MLO_LINK_ID];
16466 
16467 		if (!wdev) {
16468 			err = -EINVAL;
16469 			goto out_unlock;
16470 		}
16471 
16472 		/* MLO -> require valid link ID */
16473 		if (wdev->valid_links &&
16474 		    (!link_id ||
16475 		     !(wdev->valid_links & BIT(nla_get_u8(link_id))))) {
16476 			err = -EINVAL;
16477 			goto out_unlock;
16478 		}
16479 
16480 		/* non-MLO -> no link ID attribute accepted */
16481 		if (!wdev->valid_links && link_id) {
16482 			err = -EINVAL;
16483 			goto out_unlock;
16484 		}
16485 	}
16486 
16487 	if (internal_flags & NL80211_FLAG_MLO_UNSUPPORTED) {
16488 		if (info->attrs[NL80211_ATTR_MLO_LINK_ID] ||
16489 		    (wdev && wdev->valid_links)) {
16490 			err = -EINVAL;
16491 			goto out_unlock;
16492 		}
16493 	}
16494 
16495 	if (rdev && !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16496 		wiphy_lock(&rdev->wiphy);
16497 		/* we keep the mutex locked until post_doit */
16498 		__release(&rdev->wiphy.mtx);
16499 	}
16500 	if (!(internal_flags & NL80211_FLAG_NEED_RTNL))
16501 		rtnl_unlock();
16502 
16503 	return 0;
16504 out_unlock:
16505 	rtnl_unlock();
16506 	dev_put(dev);
16507 	return err;
16508 }
16509 
16510 static void nl80211_post_doit(const struct genl_split_ops *ops,
16511 			      struct sk_buff *skb,
16512 			      struct genl_info *info)
16513 {
16514 	u32 internal_flags = nl80211_internal_flags[ops->internal_flags];
16515 
16516 	if (info->user_ptr[1]) {
16517 		if (internal_flags & NL80211_FLAG_NEED_WDEV) {
16518 			struct wireless_dev *wdev = info->user_ptr[1];
16519 
16520 			dev_put(wdev->netdev);
16521 		} else {
16522 			dev_put(info->user_ptr[1]);
16523 		}
16524 	}
16525 
16526 	if (info->user_ptr[0] &&
16527 	    !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16528 		struct cfg80211_registered_device *rdev = info->user_ptr[0];
16529 
16530 		/* we kept the mutex locked since pre_doit */
16531 		__acquire(&rdev->wiphy.mtx);
16532 		wiphy_unlock(&rdev->wiphy);
16533 	}
16534 
16535 	if (internal_flags & NL80211_FLAG_NEED_RTNL)
16536 		rtnl_unlock();
16537 
16538 	/* If needed, clear the netlink message payload from the SKB
16539 	 * as it might contain key data that shouldn't stick around on
16540 	 * the heap after the SKB is freed. The netlink message header
16541 	 * is still needed for further processing, so leave it intact.
16542 	 */
16543 	if (internal_flags & NL80211_FLAG_CLEAR_SKB) {
16544 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
16545 
16546 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
16547 	}
16548 }
16549 
16550 static int nl80211_set_sar_sub_specs(struct cfg80211_registered_device *rdev,
16551 				     struct cfg80211_sar_specs *sar_specs,
16552 				     struct nlattr *spec[], int index)
16553 {
16554 	u32 range_index, i;
16555 
16556 	if (!sar_specs || !spec)
16557 		return -EINVAL;
16558 
16559 	if (!spec[NL80211_SAR_ATTR_SPECS_POWER] ||
16560 	    !spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX])
16561 		return -EINVAL;
16562 
16563 	range_index = nla_get_u32(spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX]);
16564 
16565 	/* check if range_index exceeds num_freq_ranges */
16566 	if (range_index >= rdev->wiphy.sar_capa->num_freq_ranges)
16567 		return -EINVAL;
16568 
16569 	/* check if range_index duplicates */
16570 	for (i = 0; i < index; i++) {
16571 		if (sar_specs->sub_specs[i].freq_range_index == range_index)
16572 			return -EINVAL;
16573 	}
16574 
16575 	sar_specs->sub_specs[index].power =
16576 		nla_get_s32(spec[NL80211_SAR_ATTR_SPECS_POWER]);
16577 
16578 	sar_specs->sub_specs[index].freq_range_index = range_index;
16579 
16580 	return 0;
16581 }
16582 
16583 static int nl80211_set_sar_specs(struct sk_buff *skb, struct genl_info *info)
16584 {
16585 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16586 	struct nlattr *spec[NL80211_SAR_ATTR_SPECS_MAX + 1];
16587 	struct nlattr *tb[NL80211_SAR_ATTR_MAX + 1];
16588 	struct cfg80211_sar_specs *sar_spec;
16589 	enum nl80211_sar_type type;
16590 	struct nlattr *spec_list;
16591 	u32 specs;
16592 	int rem, err;
16593 
16594 	if (!rdev->wiphy.sar_capa || !rdev->ops->set_sar_specs)
16595 		return -EOPNOTSUPP;
16596 
16597 	if (!info->attrs[NL80211_ATTR_SAR_SPEC])
16598 		return -EINVAL;
16599 
16600 	nla_parse_nested(tb, NL80211_SAR_ATTR_MAX,
16601 			 info->attrs[NL80211_ATTR_SAR_SPEC],
16602 			 NULL, NULL);
16603 
16604 	if (!tb[NL80211_SAR_ATTR_TYPE] || !tb[NL80211_SAR_ATTR_SPECS])
16605 		return -EINVAL;
16606 
16607 	type = nla_get_u32(tb[NL80211_SAR_ATTR_TYPE]);
16608 	if (type != rdev->wiphy.sar_capa->type)
16609 		return -EINVAL;
16610 
16611 	specs = 0;
16612 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem)
16613 		specs++;
16614 
16615 	if (specs > rdev->wiphy.sar_capa->num_freq_ranges)
16616 		return -EINVAL;
16617 
16618 	sar_spec = kzalloc(struct_size(sar_spec, sub_specs, specs), GFP_KERNEL);
16619 	if (!sar_spec)
16620 		return -ENOMEM;
16621 
16622 	sar_spec->type = type;
16623 	specs = 0;
16624 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem) {
16625 		nla_parse_nested(spec, NL80211_SAR_ATTR_SPECS_MAX,
16626 				 spec_list, NULL, NULL);
16627 
16628 		switch (type) {
16629 		case NL80211_SAR_TYPE_POWER:
16630 			if (nl80211_set_sar_sub_specs(rdev, sar_spec,
16631 						      spec, specs)) {
16632 				err = -EINVAL;
16633 				goto error;
16634 			}
16635 			break;
16636 		default:
16637 			err = -EINVAL;
16638 			goto error;
16639 		}
16640 		specs++;
16641 	}
16642 
16643 	sar_spec->num_sub_specs = specs;
16644 
16645 	rdev->cur_cmd_info = info;
16646 	err = rdev_set_sar_specs(rdev, sar_spec);
16647 	rdev->cur_cmd_info = NULL;
16648 error:
16649 	kfree(sar_spec);
16650 	return err;
16651 }
16652 
16653 #define SELECTOR(__sel, name, value) \
16654 	((__sel) == (value)) ? NL80211_IFL_SEL_##name :
16655 int __missing_selector(void);
16656 #define IFLAGS(__val) INTERNAL_FLAG_SELECTORS(__val) __missing_selector()
16657 
16658 static const struct genl_ops nl80211_ops[] = {
16659 	{
16660 		.cmd = NL80211_CMD_GET_WIPHY,
16661 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16662 		.doit = nl80211_get_wiphy,
16663 		.dumpit = nl80211_dump_wiphy,
16664 		.done = nl80211_dump_wiphy_done,
16665 		/* can be retrieved by unprivileged users */
16666 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16667 	},
16668 };
16669 
16670 static const struct genl_small_ops nl80211_small_ops[] = {
16671 	{
16672 		.cmd = NL80211_CMD_SET_WIPHY,
16673 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16674 		.doit = nl80211_set_wiphy,
16675 		.flags = GENL_UNS_ADMIN_PERM,
16676 	},
16677 	{
16678 		.cmd = NL80211_CMD_GET_INTERFACE,
16679 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16680 		.doit = nl80211_get_interface,
16681 		.dumpit = nl80211_dump_interface,
16682 		/* can be retrieved by unprivileged users */
16683 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
16684 	},
16685 	{
16686 		.cmd = NL80211_CMD_SET_INTERFACE,
16687 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16688 		.doit = nl80211_set_interface,
16689 		.flags = GENL_UNS_ADMIN_PERM,
16690 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16691 					 NL80211_FLAG_NEED_RTNL),
16692 	},
16693 	{
16694 		.cmd = NL80211_CMD_NEW_INTERFACE,
16695 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16696 		.doit = nl80211_new_interface,
16697 		.flags = GENL_UNS_ADMIN_PERM,
16698 		.internal_flags =
16699 			IFLAGS(NL80211_FLAG_NEED_WIPHY |
16700 			       NL80211_FLAG_NEED_RTNL |
16701 			       /* we take the wiphy mutex later ourselves */
16702 			       NL80211_FLAG_NO_WIPHY_MTX),
16703 	},
16704 	{
16705 		.cmd = NL80211_CMD_DEL_INTERFACE,
16706 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16707 		.doit = nl80211_del_interface,
16708 		.flags = GENL_UNS_ADMIN_PERM,
16709 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
16710 					 NL80211_FLAG_NEED_RTNL),
16711 	},
16712 	{
16713 		.cmd = NL80211_CMD_GET_KEY,
16714 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16715 		.doit = nl80211_get_key,
16716 		.flags = GENL_UNS_ADMIN_PERM,
16717 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16718 	},
16719 	{
16720 		.cmd = NL80211_CMD_SET_KEY,
16721 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16722 		.doit = nl80211_set_key,
16723 		.flags = GENL_UNS_ADMIN_PERM,
16724 		/* cannot use NL80211_FLAG_MLO_VALID_LINK_ID, depends on key */
16725 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16726 					 NL80211_FLAG_CLEAR_SKB),
16727 	},
16728 	{
16729 		.cmd = NL80211_CMD_NEW_KEY,
16730 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16731 		.doit = nl80211_new_key,
16732 		.flags = GENL_UNS_ADMIN_PERM,
16733 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16734 					 NL80211_FLAG_CLEAR_SKB),
16735 	},
16736 	{
16737 		.cmd = NL80211_CMD_DEL_KEY,
16738 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16739 		.doit = nl80211_del_key,
16740 		.flags = GENL_UNS_ADMIN_PERM,
16741 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16742 	},
16743 	{
16744 		.cmd = NL80211_CMD_SET_BEACON,
16745 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16746 		.flags = GENL_UNS_ADMIN_PERM,
16747 		.doit = nl80211_set_beacon,
16748 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16749 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16750 	},
16751 	{
16752 		.cmd = NL80211_CMD_START_AP,
16753 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16754 		.flags = GENL_UNS_ADMIN_PERM,
16755 		.doit = nl80211_start_ap,
16756 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16757 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16758 	},
16759 	{
16760 		.cmd = NL80211_CMD_STOP_AP,
16761 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16762 		.flags = GENL_UNS_ADMIN_PERM,
16763 		.doit = nl80211_stop_ap,
16764 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16765 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16766 	},
16767 	{
16768 		.cmd = NL80211_CMD_GET_STATION,
16769 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16770 		.doit = nl80211_get_station,
16771 		.dumpit = nl80211_dump_station,
16772 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16773 	},
16774 	{
16775 		.cmd = NL80211_CMD_SET_STATION,
16776 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16777 		.doit = nl80211_set_station,
16778 		.flags = GENL_UNS_ADMIN_PERM,
16779 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16780 	},
16781 	{
16782 		.cmd = NL80211_CMD_NEW_STATION,
16783 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16784 		.doit = nl80211_new_station,
16785 		.flags = GENL_UNS_ADMIN_PERM,
16786 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16787 	},
16788 	{
16789 		.cmd = NL80211_CMD_DEL_STATION,
16790 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16791 		.doit = nl80211_del_station,
16792 		.flags = GENL_UNS_ADMIN_PERM,
16793 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16794 	},
16795 	{
16796 		.cmd = NL80211_CMD_GET_MPATH,
16797 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16798 		.doit = nl80211_get_mpath,
16799 		.dumpit = nl80211_dump_mpath,
16800 		.flags = GENL_UNS_ADMIN_PERM,
16801 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16802 	},
16803 	{
16804 		.cmd = NL80211_CMD_GET_MPP,
16805 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16806 		.doit = nl80211_get_mpp,
16807 		.dumpit = nl80211_dump_mpp,
16808 		.flags = GENL_UNS_ADMIN_PERM,
16809 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16810 	},
16811 	{
16812 		.cmd = NL80211_CMD_SET_MPATH,
16813 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16814 		.doit = nl80211_set_mpath,
16815 		.flags = GENL_UNS_ADMIN_PERM,
16816 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16817 	},
16818 	{
16819 		.cmd = NL80211_CMD_NEW_MPATH,
16820 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16821 		.doit = nl80211_new_mpath,
16822 		.flags = GENL_UNS_ADMIN_PERM,
16823 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16824 	},
16825 	{
16826 		.cmd = NL80211_CMD_DEL_MPATH,
16827 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16828 		.doit = nl80211_del_mpath,
16829 		.flags = GENL_UNS_ADMIN_PERM,
16830 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16831 	},
16832 	{
16833 		.cmd = NL80211_CMD_SET_BSS,
16834 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16835 		.doit = nl80211_set_bss,
16836 		.flags = GENL_UNS_ADMIN_PERM,
16837 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16838 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16839 	},
16840 	{
16841 		.cmd = NL80211_CMD_GET_REG,
16842 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16843 		.doit = nl80211_get_reg_do,
16844 		.dumpit = nl80211_get_reg_dump,
16845 		/* can be retrieved by unprivileged users */
16846 	},
16847 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
16848 	{
16849 		.cmd = NL80211_CMD_SET_REG,
16850 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16851 		.doit = nl80211_set_reg,
16852 		.flags = GENL_ADMIN_PERM,
16853 	},
16854 #endif
16855 	{
16856 		.cmd = NL80211_CMD_REQ_SET_REG,
16857 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16858 		.doit = nl80211_req_set_reg,
16859 		.flags = GENL_ADMIN_PERM,
16860 	},
16861 	{
16862 		.cmd = NL80211_CMD_RELOAD_REGDB,
16863 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16864 		.doit = nl80211_reload_regdb,
16865 		.flags = GENL_ADMIN_PERM,
16866 	},
16867 	{
16868 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
16869 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16870 		.doit = nl80211_get_mesh_config,
16871 		/* can be retrieved by unprivileged users */
16872 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16873 	},
16874 	{
16875 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
16876 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16877 		.doit = nl80211_update_mesh_config,
16878 		.flags = GENL_UNS_ADMIN_PERM,
16879 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16880 	},
16881 	{
16882 		.cmd = NL80211_CMD_TRIGGER_SCAN,
16883 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16884 		.doit = nl80211_trigger_scan,
16885 		.flags = GENL_UNS_ADMIN_PERM,
16886 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16887 	},
16888 	{
16889 		.cmd = NL80211_CMD_ABORT_SCAN,
16890 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16891 		.doit = nl80211_abort_scan,
16892 		.flags = GENL_UNS_ADMIN_PERM,
16893 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16894 	},
16895 	{
16896 		.cmd = NL80211_CMD_GET_SCAN,
16897 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16898 		.dumpit = nl80211_dump_scan,
16899 	},
16900 	{
16901 		.cmd = NL80211_CMD_START_SCHED_SCAN,
16902 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16903 		.doit = nl80211_start_sched_scan,
16904 		.flags = GENL_UNS_ADMIN_PERM,
16905 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16906 	},
16907 	{
16908 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
16909 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16910 		.doit = nl80211_stop_sched_scan,
16911 		.flags = GENL_UNS_ADMIN_PERM,
16912 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16913 	},
16914 	{
16915 		.cmd = NL80211_CMD_AUTHENTICATE,
16916 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16917 		.doit = nl80211_authenticate,
16918 		.flags = GENL_UNS_ADMIN_PERM,
16919 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16920 					 NL80211_FLAG_CLEAR_SKB),
16921 	},
16922 	{
16923 		.cmd = NL80211_CMD_ASSOCIATE,
16924 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16925 		.doit = nl80211_associate,
16926 		.flags = GENL_UNS_ADMIN_PERM,
16927 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16928 					 NL80211_FLAG_CLEAR_SKB),
16929 	},
16930 	{
16931 		.cmd = NL80211_CMD_DEAUTHENTICATE,
16932 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16933 		.doit = nl80211_deauthenticate,
16934 		.flags = GENL_UNS_ADMIN_PERM,
16935 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16936 	},
16937 	{
16938 		.cmd = NL80211_CMD_DISASSOCIATE,
16939 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16940 		.doit = nl80211_disassociate,
16941 		.flags = GENL_UNS_ADMIN_PERM,
16942 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16943 	},
16944 	{
16945 		.cmd = NL80211_CMD_JOIN_IBSS,
16946 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16947 		.doit = nl80211_join_ibss,
16948 		.flags = GENL_UNS_ADMIN_PERM,
16949 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16950 	},
16951 	{
16952 		.cmd = NL80211_CMD_LEAVE_IBSS,
16953 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16954 		.doit = nl80211_leave_ibss,
16955 		.flags = GENL_UNS_ADMIN_PERM,
16956 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16957 	},
16958 #ifdef CONFIG_NL80211_TESTMODE
16959 	{
16960 		.cmd = NL80211_CMD_TESTMODE,
16961 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16962 		.doit = nl80211_testmode_do,
16963 		.dumpit = nl80211_testmode_dump,
16964 		.flags = GENL_UNS_ADMIN_PERM,
16965 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16966 	},
16967 #endif
16968 	{
16969 		.cmd = NL80211_CMD_CONNECT,
16970 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16971 		.doit = nl80211_connect,
16972 		.flags = GENL_UNS_ADMIN_PERM,
16973 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16974 					 NL80211_FLAG_CLEAR_SKB),
16975 	},
16976 	{
16977 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
16978 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16979 		.doit = nl80211_update_connect_params,
16980 		.flags = GENL_ADMIN_PERM,
16981 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16982 					 NL80211_FLAG_CLEAR_SKB),
16983 	},
16984 	{
16985 		.cmd = NL80211_CMD_DISCONNECT,
16986 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16987 		.doit = nl80211_disconnect,
16988 		.flags = GENL_UNS_ADMIN_PERM,
16989 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16990 	},
16991 	{
16992 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
16993 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16994 		.doit = nl80211_wiphy_netns,
16995 		.flags = GENL_UNS_ADMIN_PERM,
16996 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
16997 					 NL80211_FLAG_NEED_RTNL |
16998 					 NL80211_FLAG_NO_WIPHY_MTX),
16999 	},
17000 	{
17001 		.cmd = NL80211_CMD_GET_SURVEY,
17002 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17003 		.dumpit = nl80211_dump_survey,
17004 	},
17005 	{
17006 		.cmd = NL80211_CMD_SET_PMKSA,
17007 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17008 		.doit = nl80211_setdel_pmksa,
17009 		.flags = GENL_UNS_ADMIN_PERM,
17010 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17011 					 NL80211_FLAG_CLEAR_SKB),
17012 	},
17013 	{
17014 		.cmd = NL80211_CMD_DEL_PMKSA,
17015 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17016 		.doit = nl80211_setdel_pmksa,
17017 		.flags = GENL_UNS_ADMIN_PERM,
17018 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17019 	},
17020 	{
17021 		.cmd = NL80211_CMD_FLUSH_PMKSA,
17022 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17023 		.doit = nl80211_flush_pmksa,
17024 		.flags = GENL_UNS_ADMIN_PERM,
17025 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17026 	},
17027 	{
17028 		.cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
17029 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17030 		.doit = nl80211_remain_on_channel,
17031 		.flags = GENL_UNS_ADMIN_PERM,
17032 		/* FIXME: requiring a link ID here is probably not good */
17033 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17034 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17035 	},
17036 	{
17037 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
17038 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17039 		.doit = nl80211_cancel_remain_on_channel,
17040 		.flags = GENL_UNS_ADMIN_PERM,
17041 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17042 	},
17043 	{
17044 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
17045 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17046 		.doit = nl80211_set_tx_bitrate_mask,
17047 		.flags = GENL_UNS_ADMIN_PERM,
17048 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17049 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17050 	},
17051 	{
17052 		.cmd = NL80211_CMD_REGISTER_FRAME,
17053 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17054 		.doit = nl80211_register_mgmt,
17055 		.flags = GENL_UNS_ADMIN_PERM,
17056 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
17057 	},
17058 	{
17059 		.cmd = NL80211_CMD_FRAME,
17060 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17061 		.doit = nl80211_tx_mgmt,
17062 		.flags = GENL_UNS_ADMIN_PERM,
17063 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17064 	},
17065 	{
17066 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
17067 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17068 		.doit = nl80211_tx_mgmt_cancel_wait,
17069 		.flags = GENL_UNS_ADMIN_PERM,
17070 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17071 	},
17072 	{
17073 		.cmd = NL80211_CMD_SET_POWER_SAVE,
17074 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17075 		.doit = nl80211_set_power_save,
17076 		.flags = GENL_UNS_ADMIN_PERM,
17077 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17078 	},
17079 	{
17080 		.cmd = NL80211_CMD_GET_POWER_SAVE,
17081 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17082 		.doit = nl80211_get_power_save,
17083 		/* can be retrieved by unprivileged users */
17084 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17085 	},
17086 	{
17087 		.cmd = NL80211_CMD_SET_CQM,
17088 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17089 		.doit = nl80211_set_cqm,
17090 		.flags = GENL_UNS_ADMIN_PERM,
17091 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17092 	},
17093 	{
17094 		.cmd = NL80211_CMD_SET_CHANNEL,
17095 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17096 		.doit = nl80211_set_channel,
17097 		.flags = GENL_UNS_ADMIN_PERM,
17098 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17099 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17100 	},
17101 	{
17102 		.cmd = NL80211_CMD_JOIN_MESH,
17103 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17104 		.doit = nl80211_join_mesh,
17105 		.flags = GENL_UNS_ADMIN_PERM,
17106 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17107 	},
17108 	{
17109 		.cmd = NL80211_CMD_LEAVE_MESH,
17110 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17111 		.doit = nl80211_leave_mesh,
17112 		.flags = GENL_UNS_ADMIN_PERM,
17113 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17114 	},
17115 	{
17116 		.cmd = NL80211_CMD_JOIN_OCB,
17117 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17118 		.doit = nl80211_join_ocb,
17119 		.flags = GENL_UNS_ADMIN_PERM,
17120 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17121 	},
17122 	{
17123 		.cmd = NL80211_CMD_LEAVE_OCB,
17124 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17125 		.doit = nl80211_leave_ocb,
17126 		.flags = GENL_UNS_ADMIN_PERM,
17127 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17128 	},
17129 #ifdef CONFIG_PM
17130 	{
17131 		.cmd = NL80211_CMD_GET_WOWLAN,
17132 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17133 		.doit = nl80211_get_wowlan,
17134 		/* can be retrieved by unprivileged users */
17135 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17136 	},
17137 	{
17138 		.cmd = NL80211_CMD_SET_WOWLAN,
17139 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17140 		.doit = nl80211_set_wowlan,
17141 		.flags = GENL_UNS_ADMIN_PERM,
17142 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17143 	},
17144 #endif
17145 	{
17146 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
17147 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17148 		.doit = nl80211_set_rekey_data,
17149 		.flags = GENL_UNS_ADMIN_PERM,
17150 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17151 					 NL80211_FLAG_CLEAR_SKB),
17152 	},
17153 	{
17154 		.cmd = NL80211_CMD_TDLS_MGMT,
17155 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17156 		.doit = nl80211_tdls_mgmt,
17157 		.flags = GENL_UNS_ADMIN_PERM,
17158 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17159 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17160 	},
17161 	{
17162 		.cmd = NL80211_CMD_TDLS_OPER,
17163 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17164 		.doit = nl80211_tdls_oper,
17165 		.flags = GENL_UNS_ADMIN_PERM,
17166 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17167 	},
17168 	{
17169 		.cmd = NL80211_CMD_UNEXPECTED_FRAME,
17170 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17171 		.doit = nl80211_register_unexpected_frame,
17172 		.flags = GENL_UNS_ADMIN_PERM,
17173 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17174 	},
17175 	{
17176 		.cmd = NL80211_CMD_PROBE_CLIENT,
17177 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17178 		.doit = nl80211_probe_client,
17179 		.flags = GENL_UNS_ADMIN_PERM,
17180 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17181 	},
17182 	{
17183 		.cmd = NL80211_CMD_REGISTER_BEACONS,
17184 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17185 		.doit = nl80211_register_beacons,
17186 		.flags = GENL_UNS_ADMIN_PERM,
17187 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17188 	},
17189 	{
17190 		.cmd = NL80211_CMD_SET_NOACK_MAP,
17191 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17192 		.doit = nl80211_set_noack_map,
17193 		.flags = GENL_UNS_ADMIN_PERM,
17194 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17195 	},
17196 	{
17197 		.cmd = NL80211_CMD_START_P2P_DEVICE,
17198 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17199 		.doit = nl80211_start_p2p_device,
17200 		.flags = GENL_UNS_ADMIN_PERM,
17201 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
17202 					 NL80211_FLAG_NEED_RTNL),
17203 	},
17204 	{
17205 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
17206 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17207 		.doit = nl80211_stop_p2p_device,
17208 		.flags = GENL_UNS_ADMIN_PERM,
17209 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17210 					 NL80211_FLAG_NEED_RTNL),
17211 	},
17212 	{
17213 		.cmd = NL80211_CMD_START_NAN,
17214 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17215 		.doit = nl80211_start_nan,
17216 		.flags = GENL_ADMIN_PERM,
17217 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
17218 					 NL80211_FLAG_NEED_RTNL),
17219 	},
17220 	{
17221 		.cmd = NL80211_CMD_STOP_NAN,
17222 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17223 		.doit = nl80211_stop_nan,
17224 		.flags = GENL_ADMIN_PERM,
17225 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17226 					 NL80211_FLAG_NEED_RTNL),
17227 	},
17228 	{
17229 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
17230 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17231 		.doit = nl80211_nan_add_func,
17232 		.flags = GENL_ADMIN_PERM,
17233 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17234 	},
17235 	{
17236 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
17237 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17238 		.doit = nl80211_nan_del_func,
17239 		.flags = GENL_ADMIN_PERM,
17240 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17241 	},
17242 	{
17243 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
17244 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17245 		.doit = nl80211_nan_change_config,
17246 		.flags = GENL_ADMIN_PERM,
17247 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17248 	},
17249 	{
17250 		.cmd = NL80211_CMD_SET_MCAST_RATE,
17251 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17252 		.doit = nl80211_set_mcast_rate,
17253 		.flags = GENL_UNS_ADMIN_PERM,
17254 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17255 	},
17256 	{
17257 		.cmd = NL80211_CMD_SET_MAC_ACL,
17258 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17259 		.doit = nl80211_set_mac_acl,
17260 		.flags = GENL_UNS_ADMIN_PERM,
17261 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17262 					 NL80211_FLAG_MLO_UNSUPPORTED),
17263 	},
17264 	{
17265 		.cmd = NL80211_CMD_RADAR_DETECT,
17266 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17267 		.doit = nl80211_start_radar_detection,
17268 		.flags = GENL_UNS_ADMIN_PERM,
17269 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17270 					 NL80211_FLAG_NO_WIPHY_MTX |
17271 					 NL80211_FLAG_MLO_UNSUPPORTED),
17272 	},
17273 	{
17274 		.cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
17275 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17276 		.doit = nl80211_get_protocol_features,
17277 	},
17278 	{
17279 		.cmd = NL80211_CMD_UPDATE_FT_IES,
17280 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17281 		.doit = nl80211_update_ft_ies,
17282 		.flags = GENL_UNS_ADMIN_PERM,
17283 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17284 	},
17285 	{
17286 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
17287 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17288 		.doit = nl80211_crit_protocol_start,
17289 		.flags = GENL_UNS_ADMIN_PERM,
17290 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17291 	},
17292 	{
17293 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
17294 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17295 		.doit = nl80211_crit_protocol_stop,
17296 		.flags = GENL_UNS_ADMIN_PERM,
17297 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17298 	},
17299 	{
17300 		.cmd = NL80211_CMD_GET_COALESCE,
17301 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17302 		.doit = nl80211_get_coalesce,
17303 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17304 	},
17305 	{
17306 		.cmd = NL80211_CMD_SET_COALESCE,
17307 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17308 		.doit = nl80211_set_coalesce,
17309 		.flags = GENL_UNS_ADMIN_PERM,
17310 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17311 	},
17312 	{
17313 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
17314 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17315 		.doit = nl80211_channel_switch,
17316 		.flags = GENL_UNS_ADMIN_PERM,
17317 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17318 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17319 	},
17320 	{
17321 		.cmd = NL80211_CMD_VENDOR,
17322 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17323 		.doit = nl80211_vendor_cmd,
17324 		.dumpit = nl80211_vendor_cmd_dump,
17325 		.flags = GENL_UNS_ADMIN_PERM,
17326 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17327 					 NL80211_FLAG_CLEAR_SKB),
17328 	},
17329 	{
17330 		.cmd = NL80211_CMD_SET_QOS_MAP,
17331 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17332 		.doit = nl80211_set_qos_map,
17333 		.flags = GENL_UNS_ADMIN_PERM,
17334 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17335 	},
17336 	{
17337 		.cmd = NL80211_CMD_ADD_TX_TS,
17338 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17339 		.doit = nl80211_add_tx_ts,
17340 		.flags = GENL_UNS_ADMIN_PERM,
17341 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17342 					 NL80211_FLAG_MLO_UNSUPPORTED),
17343 	},
17344 	{
17345 		.cmd = NL80211_CMD_DEL_TX_TS,
17346 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17347 		.doit = nl80211_del_tx_ts,
17348 		.flags = GENL_UNS_ADMIN_PERM,
17349 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17350 	},
17351 	{
17352 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
17353 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17354 		.doit = nl80211_tdls_channel_switch,
17355 		.flags = GENL_UNS_ADMIN_PERM,
17356 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17357 	},
17358 	{
17359 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
17360 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17361 		.doit = nl80211_tdls_cancel_channel_switch,
17362 		.flags = GENL_UNS_ADMIN_PERM,
17363 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17364 	},
17365 	{
17366 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
17367 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17368 		.doit = nl80211_set_multicast_to_unicast,
17369 		.flags = GENL_UNS_ADMIN_PERM,
17370 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17371 	},
17372 	{
17373 		.cmd = NL80211_CMD_SET_PMK,
17374 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17375 		.doit = nl80211_set_pmk,
17376 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17377 					 NL80211_FLAG_CLEAR_SKB),
17378 	},
17379 	{
17380 		.cmd = NL80211_CMD_DEL_PMK,
17381 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17382 		.doit = nl80211_del_pmk,
17383 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17384 	},
17385 	{
17386 		.cmd = NL80211_CMD_EXTERNAL_AUTH,
17387 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17388 		.doit = nl80211_external_auth,
17389 		.flags = GENL_ADMIN_PERM,
17390 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17391 	},
17392 	{
17393 		.cmd = NL80211_CMD_CONTROL_PORT_FRAME,
17394 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17395 		.doit = nl80211_tx_control_port,
17396 		.flags = GENL_UNS_ADMIN_PERM,
17397 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17398 	},
17399 	{
17400 		.cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
17401 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17402 		.doit = nl80211_get_ftm_responder_stats,
17403 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17404 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17405 	},
17406 	{
17407 		.cmd = NL80211_CMD_PEER_MEASUREMENT_START,
17408 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17409 		.doit = nl80211_pmsr_start,
17410 		.flags = GENL_UNS_ADMIN_PERM,
17411 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17412 	},
17413 	{
17414 		.cmd = NL80211_CMD_NOTIFY_RADAR,
17415 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17416 		.doit = nl80211_notify_radar_detection,
17417 		.flags = GENL_UNS_ADMIN_PERM,
17418 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17419 	},
17420 	{
17421 		.cmd = NL80211_CMD_UPDATE_OWE_INFO,
17422 		.doit = nl80211_update_owe_info,
17423 		.flags = GENL_ADMIN_PERM,
17424 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17425 	},
17426 	{
17427 		.cmd = NL80211_CMD_PROBE_MESH_LINK,
17428 		.doit = nl80211_probe_mesh_link,
17429 		.flags = GENL_UNS_ADMIN_PERM,
17430 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17431 	},
17432 	{
17433 		.cmd = NL80211_CMD_SET_TID_CONFIG,
17434 		.doit = nl80211_set_tid_config,
17435 		.flags = GENL_UNS_ADMIN_PERM,
17436 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17437 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17438 	},
17439 	{
17440 		.cmd = NL80211_CMD_SET_SAR_SPECS,
17441 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17442 		.doit = nl80211_set_sar_specs,
17443 		.flags = GENL_UNS_ADMIN_PERM,
17444 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17445 					 NL80211_FLAG_NEED_RTNL),
17446 	},
17447 	{
17448 		.cmd = NL80211_CMD_COLOR_CHANGE_REQUEST,
17449 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17450 		.doit = nl80211_color_change,
17451 		.flags = GENL_UNS_ADMIN_PERM,
17452 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17453 	},
17454 	{
17455 		.cmd = NL80211_CMD_SET_FILS_AAD,
17456 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17457 		.doit = nl80211_set_fils_aad,
17458 		.flags = GENL_UNS_ADMIN_PERM,
17459 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17460 	},
17461 	{
17462 		.cmd = NL80211_CMD_ADD_LINK,
17463 		.doit = nl80211_add_link,
17464 		.flags = GENL_UNS_ADMIN_PERM,
17465 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17466 	},
17467 	{
17468 		.cmd = NL80211_CMD_REMOVE_LINK,
17469 		.doit = nl80211_remove_link,
17470 		.flags = GENL_UNS_ADMIN_PERM,
17471 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17472 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17473 	},
17474 	{
17475 		.cmd = NL80211_CMD_ADD_LINK_STA,
17476 		.doit = nl80211_add_link_station,
17477 		.flags = GENL_UNS_ADMIN_PERM,
17478 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17479 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17480 	},
17481 	{
17482 		.cmd = NL80211_CMD_MODIFY_LINK_STA,
17483 		.doit = nl80211_modify_link_station,
17484 		.flags = GENL_UNS_ADMIN_PERM,
17485 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17486 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17487 	},
17488 	{
17489 		.cmd = NL80211_CMD_REMOVE_LINK_STA,
17490 		.doit = nl80211_remove_link_station,
17491 		.flags = GENL_UNS_ADMIN_PERM,
17492 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17493 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17494 	},
17495 	{
17496 		.cmd = NL80211_CMD_SET_HW_TIMESTAMP,
17497 		.doit = nl80211_set_hw_timestamp,
17498 		.flags = GENL_UNS_ADMIN_PERM,
17499 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17500 	},
17501 };
17502 
17503 static struct genl_family nl80211_fam __ro_after_init = {
17504 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
17505 	.hdrsize = 0,			/* no private header */
17506 	.version = 1,			/* no particular meaning now */
17507 	.maxattr = NL80211_ATTR_MAX,
17508 	.policy = nl80211_policy,
17509 	.netnsok = true,
17510 	.pre_doit = nl80211_pre_doit,
17511 	.post_doit = nl80211_post_doit,
17512 	.module = THIS_MODULE,
17513 	.ops = nl80211_ops,
17514 	.n_ops = ARRAY_SIZE(nl80211_ops),
17515 	.small_ops = nl80211_small_ops,
17516 	.n_small_ops = ARRAY_SIZE(nl80211_small_ops),
17517 	.resv_start_op = NL80211_CMD_REMOVE_LINK_STA + 1,
17518 	.mcgrps = nl80211_mcgrps,
17519 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
17520 	.parallel_ops = true,
17521 };
17522 
17523 /* notification functions */
17524 
17525 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
17526 			  enum nl80211_commands cmd)
17527 {
17528 	struct sk_buff *msg;
17529 	struct nl80211_dump_wiphy_state state = {};
17530 
17531 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
17532 		cmd != NL80211_CMD_DEL_WIPHY);
17533 
17534 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17535 	if (!msg)
17536 		return;
17537 
17538 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
17539 		nlmsg_free(msg);
17540 		return;
17541 	}
17542 
17543 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17544 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17545 }
17546 
17547 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
17548 				struct wireless_dev *wdev,
17549 				enum nl80211_commands cmd)
17550 {
17551 	struct sk_buff *msg;
17552 
17553 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17554 	if (!msg)
17555 		return;
17556 
17557 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
17558 		nlmsg_free(msg);
17559 		return;
17560 	}
17561 
17562 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17563 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17564 }
17565 
17566 static int nl80211_add_scan_req(struct sk_buff *msg,
17567 				struct cfg80211_registered_device *rdev)
17568 {
17569 	struct cfg80211_scan_request *req = rdev->scan_req;
17570 	struct nlattr *nest;
17571 	int i;
17572 	struct cfg80211_scan_info *info;
17573 
17574 	if (WARN_ON(!req))
17575 		return 0;
17576 
17577 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
17578 	if (!nest)
17579 		goto nla_put_failure;
17580 	for (i = 0; i < req->n_ssids; i++) {
17581 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
17582 			goto nla_put_failure;
17583 	}
17584 	nla_nest_end(msg, nest);
17585 
17586 	if (req->flags & NL80211_SCAN_FLAG_FREQ_KHZ) {
17587 		nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQ_KHZ);
17588 		if (!nest)
17589 			goto nla_put_failure;
17590 		for (i = 0; i < req->n_channels; i++) {
17591 			if (nla_put_u32(msg, i,
17592 				   ieee80211_channel_to_khz(req->channels[i])))
17593 				goto nla_put_failure;
17594 		}
17595 		nla_nest_end(msg, nest);
17596 	} else {
17597 		nest = nla_nest_start_noflag(msg,
17598 					     NL80211_ATTR_SCAN_FREQUENCIES);
17599 		if (!nest)
17600 			goto nla_put_failure;
17601 		for (i = 0; i < req->n_channels; i++) {
17602 			if (nla_put_u32(msg, i, req->channels[i]->center_freq))
17603 				goto nla_put_failure;
17604 		}
17605 		nla_nest_end(msg, nest);
17606 	}
17607 
17608 	if (req->ie &&
17609 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
17610 		goto nla_put_failure;
17611 
17612 	if (req->flags &&
17613 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
17614 		goto nla_put_failure;
17615 
17616 	info = rdev->int_scan_req ? &rdev->int_scan_req->info :
17617 		&rdev->scan_req->info;
17618 	if (info->scan_start_tsf &&
17619 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
17620 			       info->scan_start_tsf, NL80211_BSS_PAD) ||
17621 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
17622 		     info->tsf_bssid)))
17623 		goto nla_put_failure;
17624 
17625 	return 0;
17626  nla_put_failure:
17627 	return -ENOBUFS;
17628 }
17629 
17630 static int nl80211_prep_scan_msg(struct sk_buff *msg,
17631 				 struct cfg80211_registered_device *rdev,
17632 				 struct wireless_dev *wdev,
17633 				 u32 portid, u32 seq, int flags,
17634 				 u32 cmd)
17635 {
17636 	void *hdr;
17637 
17638 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
17639 	if (!hdr)
17640 		return -1;
17641 
17642 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17643 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
17644 					 wdev->netdev->ifindex)) ||
17645 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17646 			      NL80211_ATTR_PAD))
17647 		goto nla_put_failure;
17648 
17649 	/* ignore errors and send incomplete event anyway */
17650 	nl80211_add_scan_req(msg, rdev);
17651 
17652 	genlmsg_end(msg, hdr);
17653 	return 0;
17654 
17655  nla_put_failure:
17656 	genlmsg_cancel(msg, hdr);
17657 	return -EMSGSIZE;
17658 }
17659 
17660 static int
17661 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
17662 			    struct cfg80211_sched_scan_request *req, u32 cmd)
17663 {
17664 	void *hdr;
17665 
17666 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17667 	if (!hdr)
17668 		return -1;
17669 
17670 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
17671 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
17672 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
17673 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
17674 			      NL80211_ATTR_PAD))
17675 		goto nla_put_failure;
17676 
17677 	genlmsg_end(msg, hdr);
17678 	return 0;
17679 
17680  nla_put_failure:
17681 	genlmsg_cancel(msg, hdr);
17682 	return -EMSGSIZE;
17683 }
17684 
17685 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
17686 			     struct wireless_dev *wdev)
17687 {
17688 	struct sk_buff *msg;
17689 
17690 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17691 	if (!msg)
17692 		return;
17693 
17694 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17695 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
17696 		nlmsg_free(msg);
17697 		return;
17698 	}
17699 
17700 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17701 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17702 }
17703 
17704 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
17705 				       struct wireless_dev *wdev, bool aborted)
17706 {
17707 	struct sk_buff *msg;
17708 
17709 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17710 	if (!msg)
17711 		return NULL;
17712 
17713 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17714 				  aborted ? NL80211_CMD_SCAN_ABORTED :
17715 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
17716 		nlmsg_free(msg);
17717 		return NULL;
17718 	}
17719 
17720 	return msg;
17721 }
17722 
17723 /* send message created by nl80211_build_scan_msg() */
17724 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
17725 			   struct sk_buff *msg)
17726 {
17727 	if (!msg)
17728 		return;
17729 
17730 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17731 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17732 }
17733 
17734 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
17735 {
17736 	struct sk_buff *msg;
17737 
17738 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17739 	if (!msg)
17740 		return;
17741 
17742 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
17743 		nlmsg_free(msg);
17744 		return;
17745 	}
17746 
17747 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
17748 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17749 }
17750 
17751 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
17752 					  struct regulatory_request *request)
17753 {
17754 	/* Userspace can always count this one always being set */
17755 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
17756 		goto nla_put_failure;
17757 
17758 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
17759 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17760 			       NL80211_REGDOM_TYPE_WORLD))
17761 			goto nla_put_failure;
17762 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
17763 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17764 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
17765 			goto nla_put_failure;
17766 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
17767 		   request->intersect) {
17768 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17769 			       NL80211_REGDOM_TYPE_INTERSECTION))
17770 			goto nla_put_failure;
17771 	} else {
17772 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17773 			       NL80211_REGDOM_TYPE_COUNTRY) ||
17774 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
17775 				   request->alpha2))
17776 			goto nla_put_failure;
17777 	}
17778 
17779 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
17780 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
17781 
17782 		if (wiphy &&
17783 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
17784 			goto nla_put_failure;
17785 
17786 		if (wiphy &&
17787 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
17788 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
17789 			goto nla_put_failure;
17790 	}
17791 
17792 	return true;
17793 
17794 nla_put_failure:
17795 	return false;
17796 }
17797 
17798 /*
17799  * This can happen on global regulatory changes or device specific settings
17800  * based on custom regulatory domains.
17801  */
17802 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
17803 				     struct regulatory_request *request)
17804 {
17805 	struct sk_buff *msg;
17806 	void *hdr;
17807 
17808 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17809 	if (!msg)
17810 		return;
17811 
17812 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
17813 	if (!hdr)
17814 		goto nla_put_failure;
17815 
17816 	if (!nl80211_reg_change_event_fill(msg, request))
17817 		goto nla_put_failure;
17818 
17819 	genlmsg_end(msg, hdr);
17820 
17821 	rcu_read_lock();
17822 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
17823 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
17824 	rcu_read_unlock();
17825 
17826 	return;
17827 
17828 nla_put_failure:
17829 	nlmsg_free(msg);
17830 }
17831 
17832 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
17833 				    struct net_device *netdev,
17834 				    const u8 *buf, size_t len,
17835 				    enum nl80211_commands cmd, gfp_t gfp,
17836 				    int uapsd_queues, const u8 *req_ies,
17837 				    size_t req_ies_len, bool reconnect)
17838 {
17839 	struct sk_buff *msg;
17840 	void *hdr;
17841 
17842 	msg = nlmsg_new(100 + len + req_ies_len, gfp);
17843 	if (!msg)
17844 		return;
17845 
17846 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17847 	if (!hdr) {
17848 		nlmsg_free(msg);
17849 		return;
17850 	}
17851 
17852 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17853 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17854 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
17855 	    (req_ies &&
17856 	     nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
17857 		goto nla_put_failure;
17858 
17859 	if (reconnect && nla_put_flag(msg, NL80211_ATTR_RECONNECT_REQUESTED))
17860 		goto nla_put_failure;
17861 
17862 	if (uapsd_queues >= 0) {
17863 		struct nlattr *nla_wmm =
17864 			nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
17865 		if (!nla_wmm)
17866 			goto nla_put_failure;
17867 
17868 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
17869 			       uapsd_queues))
17870 			goto nla_put_failure;
17871 
17872 		nla_nest_end(msg, nla_wmm);
17873 	}
17874 
17875 	genlmsg_end(msg, hdr);
17876 
17877 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17878 				NL80211_MCGRP_MLME, gfp);
17879 	return;
17880 
17881  nla_put_failure:
17882 	nlmsg_free(msg);
17883 }
17884 
17885 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
17886 			  struct net_device *netdev, const u8 *buf,
17887 			  size_t len, gfp_t gfp)
17888 {
17889 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17890 				NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0,
17891 				false);
17892 }
17893 
17894 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
17895 			   struct net_device *netdev,
17896 			   struct cfg80211_rx_assoc_resp *data)
17897 {
17898 	nl80211_send_mlme_event(rdev, netdev, data->buf, data->len,
17899 				NL80211_CMD_ASSOCIATE, GFP_KERNEL,
17900 				data->uapsd_queues,
17901 				data->req_ies, data->req_ies_len, false);
17902 }
17903 
17904 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
17905 			 struct net_device *netdev, const u8 *buf,
17906 			 size_t len, bool reconnect, gfp_t gfp)
17907 {
17908 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17909 				NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0,
17910 				reconnect);
17911 }
17912 
17913 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
17914 			   struct net_device *netdev, const u8 *buf,
17915 			   size_t len, bool reconnect, gfp_t gfp)
17916 {
17917 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17918 				NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0,
17919 				reconnect);
17920 }
17921 
17922 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
17923 				  size_t len)
17924 {
17925 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17926 	struct wiphy *wiphy = wdev->wiphy;
17927 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17928 	const struct ieee80211_mgmt *mgmt = (void *)buf;
17929 	u32 cmd;
17930 
17931 	if (WARN_ON(len < 2))
17932 		return;
17933 
17934 	if (ieee80211_is_deauth(mgmt->frame_control)) {
17935 		cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
17936 	} else if (ieee80211_is_disassoc(mgmt->frame_control)) {
17937 		cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
17938 	} else if (ieee80211_is_beacon(mgmt->frame_control)) {
17939 		if (wdev->unprot_beacon_reported &&
17940 		    elapsed_jiffies_msecs(wdev->unprot_beacon_reported) < 10000)
17941 			return;
17942 		cmd = NL80211_CMD_UNPROT_BEACON;
17943 		wdev->unprot_beacon_reported = jiffies;
17944 	} else {
17945 		return;
17946 	}
17947 
17948 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
17949 	nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
17950 				NULL, 0, false);
17951 }
17952 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
17953 
17954 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
17955 				      struct net_device *netdev, int cmd,
17956 				      const u8 *addr, gfp_t gfp)
17957 {
17958 	struct sk_buff *msg;
17959 	void *hdr;
17960 
17961 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17962 	if (!msg)
17963 		return;
17964 
17965 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17966 	if (!hdr) {
17967 		nlmsg_free(msg);
17968 		return;
17969 	}
17970 
17971 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17972 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17973 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
17974 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
17975 		goto nla_put_failure;
17976 
17977 	genlmsg_end(msg, hdr);
17978 
17979 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17980 				NL80211_MCGRP_MLME, gfp);
17981 	return;
17982 
17983  nla_put_failure:
17984 	nlmsg_free(msg);
17985 }
17986 
17987 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
17988 			       struct net_device *netdev, const u8 *addr,
17989 			       gfp_t gfp)
17990 {
17991 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
17992 				  addr, gfp);
17993 }
17994 
17995 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
17996 				struct net_device *netdev, const u8 *addr,
17997 				gfp_t gfp)
17998 {
17999 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
18000 				  addr, gfp);
18001 }
18002 
18003 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
18004 				 struct net_device *netdev,
18005 				 struct cfg80211_connect_resp_params *cr,
18006 				 gfp_t gfp)
18007 {
18008 	struct sk_buff *msg;
18009 	void *hdr;
18010 	unsigned int link;
18011 	size_t link_info_size = 0;
18012 	const u8 *connected_addr = cr->valid_links ?
18013 				   cr->ap_mld_addr : cr->links[0].bssid;
18014 
18015 	if (cr->valid_links) {
18016 		for_each_valid_link(cr, link) {
18017 			/* Nested attribute header */
18018 			link_info_size += NLA_HDRLEN;
18019 			/* Link ID */
18020 			link_info_size += nla_total_size(sizeof(u8));
18021 			link_info_size += cr->links[link].addr ?
18022 					  nla_total_size(ETH_ALEN) : 0;
18023 			link_info_size += (cr->links[link].bssid ||
18024 					   cr->links[link].bss) ?
18025 					  nla_total_size(ETH_ALEN) : 0;
18026 			link_info_size += nla_total_size(sizeof(u16));
18027 		}
18028 	}
18029 
18030 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
18031 			cr->fils.kek_len + cr->fils.pmk_len +
18032 			(cr->fils.pmkid ? WLAN_PMKID_LEN : 0) + link_info_size,
18033 			gfp);
18034 	if (!msg)
18035 		return;
18036 
18037 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
18038 	if (!hdr) {
18039 		nlmsg_free(msg);
18040 		return;
18041 	}
18042 
18043 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18044 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18045 	    (connected_addr &&
18046 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr)) ||
18047 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
18048 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
18049 			cr->status) ||
18050 	    (cr->status < 0 &&
18051 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
18052 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
18053 			  cr->timeout_reason))) ||
18054 	    (cr->req_ie &&
18055 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
18056 	    (cr->resp_ie &&
18057 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
18058 		     cr->resp_ie)) ||
18059 	    (cr->fils.update_erp_next_seq_num &&
18060 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
18061 			 cr->fils.erp_next_seq_num)) ||
18062 	    (cr->status == WLAN_STATUS_SUCCESS &&
18063 	     ((cr->fils.kek &&
18064 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
18065 		       cr->fils.kek)) ||
18066 	      (cr->fils.pmk &&
18067 	       nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
18068 	      (cr->fils.pmkid &&
18069 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
18070 		goto nla_put_failure;
18071 
18072 	if (cr->valid_links) {
18073 		int i = 1;
18074 		struct nlattr *nested;
18075 
18076 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18077 		if (!nested)
18078 			goto nla_put_failure;
18079 
18080 		for_each_valid_link(cr, link) {
18081 			struct nlattr *nested_mlo_links;
18082 			const u8 *bssid = cr->links[link].bss ?
18083 					  cr->links[link].bss->bssid :
18084 					  cr->links[link].bssid;
18085 
18086 			nested_mlo_links = nla_nest_start(msg, i);
18087 			if (!nested_mlo_links)
18088 				goto nla_put_failure;
18089 
18090 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
18091 			    (bssid &&
18092 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
18093 			    (cr->links[link].addr &&
18094 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
18095 				     cr->links[link].addr)) ||
18096 			    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
18097 					cr->links[link].status))
18098 				goto nla_put_failure;
18099 
18100 			nla_nest_end(msg, nested_mlo_links);
18101 			i++;
18102 		}
18103 		nla_nest_end(msg, nested);
18104 	}
18105 
18106 	genlmsg_end(msg, hdr);
18107 
18108 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18109 				NL80211_MCGRP_MLME, gfp);
18110 	return;
18111 
18112  nla_put_failure:
18113 	nlmsg_free(msg);
18114 }
18115 
18116 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
18117 			 struct net_device *netdev,
18118 			 struct cfg80211_roam_info *info, gfp_t gfp)
18119 {
18120 	struct sk_buff *msg;
18121 	void *hdr;
18122 	size_t link_info_size = 0;
18123 	unsigned int link;
18124 	const u8 *connected_addr = info->ap_mld_addr ?
18125 				   info->ap_mld_addr :
18126 				   (info->links[0].bss ?
18127 				    info->links[0].bss->bssid :
18128 				    info->links[0].bssid);
18129 
18130 	if (info->valid_links) {
18131 		for_each_valid_link(info, link) {
18132 			/* Nested attribute header */
18133 			link_info_size += NLA_HDRLEN;
18134 			/* Link ID */
18135 			link_info_size += nla_total_size(sizeof(u8));
18136 			link_info_size += info->links[link].addr ?
18137 					  nla_total_size(ETH_ALEN) : 0;
18138 			link_info_size += (info->links[link].bssid ||
18139 					   info->links[link].bss) ?
18140 					  nla_total_size(ETH_ALEN) : 0;
18141 		}
18142 	}
18143 
18144 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
18145 			info->fils.kek_len + info->fils.pmk_len +
18146 			(info->fils.pmkid ? WLAN_PMKID_LEN : 0) +
18147 			link_info_size, gfp);
18148 	if (!msg)
18149 		return;
18150 
18151 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
18152 	if (!hdr) {
18153 		nlmsg_free(msg);
18154 		return;
18155 	}
18156 
18157 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18158 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18159 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr) ||
18160 	    (info->req_ie &&
18161 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
18162 		     info->req_ie)) ||
18163 	    (info->resp_ie &&
18164 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
18165 		     info->resp_ie)) ||
18166 	    (info->fils.update_erp_next_seq_num &&
18167 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
18168 			 info->fils.erp_next_seq_num)) ||
18169 	    (info->fils.kek &&
18170 	     nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
18171 		     info->fils.kek)) ||
18172 	    (info->fils.pmk &&
18173 	     nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
18174 	    (info->fils.pmkid &&
18175 	     nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
18176 		goto nla_put_failure;
18177 
18178 	if (info->valid_links) {
18179 		int i = 1;
18180 		struct nlattr *nested;
18181 
18182 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18183 		if (!nested)
18184 			goto nla_put_failure;
18185 
18186 		for_each_valid_link(info, link) {
18187 			struct nlattr *nested_mlo_links;
18188 			const u8 *bssid = info->links[link].bss ?
18189 					  info->links[link].bss->bssid :
18190 					  info->links[link].bssid;
18191 
18192 			nested_mlo_links = nla_nest_start(msg, i);
18193 			if (!nested_mlo_links)
18194 				goto nla_put_failure;
18195 
18196 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
18197 			    (bssid &&
18198 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
18199 			    (info->links[link].addr &&
18200 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
18201 				     info->links[link].addr)))
18202 				goto nla_put_failure;
18203 
18204 			nla_nest_end(msg, nested_mlo_links);
18205 			i++;
18206 		}
18207 		nla_nest_end(msg, nested);
18208 	}
18209 
18210 	genlmsg_end(msg, hdr);
18211 
18212 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18213 				NL80211_MCGRP_MLME, gfp);
18214 	return;
18215 
18216  nla_put_failure:
18217 	nlmsg_free(msg);
18218 }
18219 
18220 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
18221 				  struct net_device *netdev, const u8 *bssid,
18222 				  const u8 *td_bitmap, u8 td_bitmap_len)
18223 {
18224 	struct sk_buff *msg;
18225 	void *hdr;
18226 
18227 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18228 	if (!msg)
18229 		return;
18230 
18231 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
18232 	if (!hdr) {
18233 		nlmsg_free(msg);
18234 		return;
18235 	}
18236 
18237 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18238 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18239 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18240 		goto nla_put_failure;
18241 
18242 	if ((td_bitmap_len > 0) && td_bitmap)
18243 		if (nla_put(msg, NL80211_ATTR_TD_BITMAP,
18244 			    td_bitmap_len, td_bitmap))
18245 			goto nla_put_failure;
18246 
18247 	genlmsg_end(msg, hdr);
18248 
18249 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18250 				NL80211_MCGRP_MLME, GFP_KERNEL);
18251 	return;
18252 
18253  nla_put_failure:
18254 	nlmsg_free(msg);
18255 }
18256 
18257 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
18258 			       struct net_device *netdev, u16 reason,
18259 			       const u8 *ie, size_t ie_len, bool from_ap)
18260 {
18261 	struct sk_buff *msg;
18262 	void *hdr;
18263 
18264 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
18265 	if (!msg)
18266 		return;
18267 
18268 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
18269 	if (!hdr) {
18270 		nlmsg_free(msg);
18271 		return;
18272 	}
18273 
18274 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18275 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18276 	    (reason &&
18277 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
18278 	    (from_ap &&
18279 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
18280 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
18281 		goto nla_put_failure;
18282 
18283 	genlmsg_end(msg, hdr);
18284 
18285 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18286 				NL80211_MCGRP_MLME, GFP_KERNEL);
18287 	return;
18288 
18289  nla_put_failure:
18290 	nlmsg_free(msg);
18291 }
18292 
18293 void cfg80211_links_removed(struct net_device *dev, u16 link_mask)
18294 {
18295 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18296 	struct wiphy *wiphy = wdev->wiphy;
18297 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18298 	struct sk_buff *msg;
18299 	struct nlattr *links;
18300 	void *hdr;
18301 
18302 	ASSERT_WDEV_LOCK(wdev);
18303 	trace_cfg80211_links_removed(dev, link_mask);
18304 
18305 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
18306 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
18307 		return;
18308 
18309 	if (WARN_ON(!wdev->valid_links || !link_mask ||
18310 		    (wdev->valid_links & link_mask) != link_mask ||
18311 		    wdev->valid_links == link_mask))
18312 		return;
18313 
18314 	cfg80211_wdev_release_link_bsses(wdev, link_mask);
18315 	wdev->valid_links &= ~link_mask;
18316 
18317 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18318 	if (!msg)
18319 		return;
18320 
18321 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_LINKS_REMOVED);
18322 	if (!hdr) {
18323 		nlmsg_free(msg);
18324 		return;
18325 	}
18326 
18327 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18328 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
18329 		goto nla_put_failure;
18330 
18331 	links = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18332 	if (!links)
18333 		goto nla_put_failure;
18334 
18335 	while (link_mask) {
18336 		struct nlattr *link;
18337 		int link_id = __ffs(link_mask);
18338 
18339 		link = nla_nest_start(msg, link_id + 1);
18340 		if (!link)
18341 			goto nla_put_failure;
18342 
18343 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
18344 			goto nla_put_failure;
18345 
18346 		nla_nest_end(msg, link);
18347 		link_mask &= ~(1 << link_id);
18348 	}
18349 
18350 	nla_nest_end(msg, links);
18351 
18352 	genlmsg_end(msg, hdr);
18353 
18354 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18355 				NL80211_MCGRP_MLME, GFP_KERNEL);
18356 	return;
18357 
18358  nla_put_failure:
18359 	nlmsg_free(msg);
18360 }
18361 EXPORT_SYMBOL(cfg80211_links_removed);
18362 
18363 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
18364 			     struct net_device *netdev, const u8 *bssid,
18365 			     gfp_t gfp)
18366 {
18367 	struct sk_buff *msg;
18368 	void *hdr;
18369 
18370 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18371 	if (!msg)
18372 		return;
18373 
18374 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
18375 	if (!hdr) {
18376 		nlmsg_free(msg);
18377 		return;
18378 	}
18379 
18380 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18381 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18382 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18383 		goto nla_put_failure;
18384 
18385 	genlmsg_end(msg, hdr);
18386 
18387 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18388 				NL80211_MCGRP_MLME, gfp);
18389 	return;
18390 
18391  nla_put_failure:
18392 	nlmsg_free(msg);
18393 }
18394 
18395 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
18396 					const u8 *ie, u8 ie_len,
18397 					int sig_dbm, gfp_t gfp)
18398 {
18399 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18400 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18401 	struct sk_buff *msg;
18402 	void *hdr;
18403 
18404 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
18405 		return;
18406 
18407 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
18408 
18409 	msg = nlmsg_new(100 + ie_len, gfp);
18410 	if (!msg)
18411 		return;
18412 
18413 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
18414 	if (!hdr) {
18415 		nlmsg_free(msg);
18416 		return;
18417 	}
18418 
18419 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18420 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18421 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
18422 	    (ie_len && ie &&
18423 	     nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
18424 	    (sig_dbm &&
18425 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
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 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
18438 
18439 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
18440 				 struct net_device *netdev, const u8 *addr,
18441 				 enum nl80211_key_type key_type, int key_id,
18442 				 const u8 *tsc, gfp_t gfp)
18443 {
18444 	struct sk_buff *msg;
18445 	void *hdr;
18446 
18447 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18448 	if (!msg)
18449 		return;
18450 
18451 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
18452 	if (!hdr) {
18453 		nlmsg_free(msg);
18454 		return;
18455 	}
18456 
18457 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18458 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18459 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
18460 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
18461 	    (key_id != -1 &&
18462 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
18463 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
18464 		goto nla_put_failure;
18465 
18466 	genlmsg_end(msg, hdr);
18467 
18468 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18469 				NL80211_MCGRP_MLME, gfp);
18470 	return;
18471 
18472  nla_put_failure:
18473 	nlmsg_free(msg);
18474 }
18475 
18476 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
18477 				    struct ieee80211_channel *channel_before,
18478 				    struct ieee80211_channel *channel_after)
18479 {
18480 	struct sk_buff *msg;
18481 	void *hdr;
18482 	struct nlattr *nl_freq;
18483 
18484 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
18485 	if (!msg)
18486 		return;
18487 
18488 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
18489 	if (!hdr) {
18490 		nlmsg_free(msg);
18491 		return;
18492 	}
18493 
18494 	/*
18495 	 * Since we are applying the beacon hint to a wiphy we know its
18496 	 * wiphy_idx is valid
18497 	 */
18498 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
18499 		goto nla_put_failure;
18500 
18501 	/* Before */
18502 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
18503 	if (!nl_freq)
18504 		goto nla_put_failure;
18505 
18506 	if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
18507 		goto nla_put_failure;
18508 	nla_nest_end(msg, nl_freq);
18509 
18510 	/* After */
18511 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
18512 	if (!nl_freq)
18513 		goto nla_put_failure;
18514 
18515 	if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
18516 		goto nla_put_failure;
18517 	nla_nest_end(msg, nl_freq);
18518 
18519 	genlmsg_end(msg, hdr);
18520 
18521 	rcu_read_lock();
18522 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
18523 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
18524 	rcu_read_unlock();
18525 
18526 	return;
18527 
18528 nla_put_failure:
18529 	nlmsg_free(msg);
18530 }
18531 
18532 static void nl80211_send_remain_on_chan_event(
18533 	int cmd, struct cfg80211_registered_device *rdev,
18534 	struct wireless_dev *wdev, u64 cookie,
18535 	struct ieee80211_channel *chan,
18536 	unsigned int duration, gfp_t gfp)
18537 {
18538 	struct sk_buff *msg;
18539 	void *hdr;
18540 
18541 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18542 	if (!msg)
18543 		return;
18544 
18545 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18546 	if (!hdr) {
18547 		nlmsg_free(msg);
18548 		return;
18549 	}
18550 
18551 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18552 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18553 					 wdev->netdev->ifindex)) ||
18554 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18555 			      NL80211_ATTR_PAD) ||
18556 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
18557 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
18558 			NL80211_CHAN_NO_HT) ||
18559 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
18560 			      NL80211_ATTR_PAD))
18561 		goto nla_put_failure;
18562 
18563 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
18564 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
18565 		goto nla_put_failure;
18566 
18567 	genlmsg_end(msg, hdr);
18568 
18569 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18570 				NL80211_MCGRP_MLME, gfp);
18571 	return;
18572 
18573  nla_put_failure:
18574 	nlmsg_free(msg);
18575 }
18576 
18577 void cfg80211_assoc_comeback(struct net_device *netdev,
18578 			     const u8 *ap_addr, u32 timeout)
18579 {
18580 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
18581 	struct wiphy *wiphy = wdev->wiphy;
18582 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18583 	struct sk_buff *msg;
18584 	void *hdr;
18585 
18586 	trace_cfg80211_assoc_comeback(wdev, ap_addr, timeout);
18587 
18588 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18589 	if (!msg)
18590 		return;
18591 
18592 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ASSOC_COMEBACK);
18593 	if (!hdr) {
18594 		nlmsg_free(msg);
18595 		return;
18596 	}
18597 
18598 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18599 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18600 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ap_addr) ||
18601 	    nla_put_u32(msg, NL80211_ATTR_TIMEOUT, timeout))
18602 		goto nla_put_failure;
18603 
18604 	genlmsg_end(msg, hdr);
18605 
18606 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18607 				NL80211_MCGRP_MLME, GFP_KERNEL);
18608 	return;
18609 
18610  nla_put_failure:
18611 	nlmsg_free(msg);
18612 }
18613 EXPORT_SYMBOL(cfg80211_assoc_comeback);
18614 
18615 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
18616 			       struct ieee80211_channel *chan,
18617 			       unsigned int duration, gfp_t gfp)
18618 {
18619 	struct wiphy *wiphy = wdev->wiphy;
18620 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18621 
18622 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
18623 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
18624 					  rdev, wdev, cookie, chan,
18625 					  duration, gfp);
18626 }
18627 EXPORT_SYMBOL(cfg80211_ready_on_channel);
18628 
18629 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
18630 					struct ieee80211_channel *chan,
18631 					gfp_t gfp)
18632 {
18633 	struct wiphy *wiphy = wdev->wiphy;
18634 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18635 
18636 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
18637 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
18638 					  rdev, wdev, cookie, chan, 0, gfp);
18639 }
18640 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
18641 
18642 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
18643 					struct ieee80211_channel *chan,
18644 					gfp_t gfp)
18645 {
18646 	struct wiphy *wiphy = wdev->wiphy;
18647 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18648 
18649 	trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
18650 	nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
18651 					  rdev, wdev, cookie, chan, 0, gfp);
18652 }
18653 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
18654 
18655 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
18656 		      struct station_info *sinfo, gfp_t gfp)
18657 {
18658 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18659 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18660 	struct sk_buff *msg;
18661 
18662 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
18663 
18664 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18665 	if (!msg)
18666 		return;
18667 
18668 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
18669 				 rdev, dev, mac_addr, sinfo) < 0) {
18670 		nlmsg_free(msg);
18671 		return;
18672 	}
18673 
18674 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18675 				NL80211_MCGRP_MLME, gfp);
18676 }
18677 EXPORT_SYMBOL(cfg80211_new_sta);
18678 
18679 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
18680 			    struct station_info *sinfo, gfp_t gfp)
18681 {
18682 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18683 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18684 	struct sk_buff *msg;
18685 	struct station_info empty_sinfo = {};
18686 
18687 	if (!sinfo)
18688 		sinfo = &empty_sinfo;
18689 
18690 	trace_cfg80211_del_sta(dev, mac_addr);
18691 
18692 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18693 	if (!msg) {
18694 		cfg80211_sinfo_release_content(sinfo);
18695 		return;
18696 	}
18697 
18698 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
18699 				 rdev, dev, mac_addr, sinfo) < 0) {
18700 		nlmsg_free(msg);
18701 		return;
18702 	}
18703 
18704 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18705 				NL80211_MCGRP_MLME, gfp);
18706 }
18707 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
18708 
18709 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
18710 			  enum nl80211_connect_failed_reason reason,
18711 			  gfp_t gfp)
18712 {
18713 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18714 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18715 	struct sk_buff *msg;
18716 	void *hdr;
18717 
18718 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
18719 	if (!msg)
18720 		return;
18721 
18722 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
18723 	if (!hdr) {
18724 		nlmsg_free(msg);
18725 		return;
18726 	}
18727 
18728 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18729 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
18730 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
18731 		goto nla_put_failure;
18732 
18733 	genlmsg_end(msg, hdr);
18734 
18735 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18736 				NL80211_MCGRP_MLME, gfp);
18737 	return;
18738 
18739  nla_put_failure:
18740 	nlmsg_free(msg);
18741 }
18742 EXPORT_SYMBOL(cfg80211_conn_failed);
18743 
18744 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
18745 				       const u8 *addr, gfp_t gfp)
18746 {
18747 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18748 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18749 	struct sk_buff *msg;
18750 	void *hdr;
18751 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
18752 
18753 	if (!nlportid)
18754 		return false;
18755 
18756 	msg = nlmsg_new(100, gfp);
18757 	if (!msg)
18758 		return true;
18759 
18760 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18761 	if (!hdr) {
18762 		nlmsg_free(msg);
18763 		return true;
18764 	}
18765 
18766 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18767 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18768 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
18769 		goto nla_put_failure;
18770 
18771 	genlmsg_end(msg, hdr);
18772 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18773 	return true;
18774 
18775  nla_put_failure:
18776 	nlmsg_free(msg);
18777 	return true;
18778 }
18779 
18780 bool cfg80211_rx_spurious_frame(struct net_device *dev,
18781 				const u8 *addr, gfp_t gfp)
18782 {
18783 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18784 	bool ret;
18785 
18786 	trace_cfg80211_rx_spurious_frame(dev, addr);
18787 
18788 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18789 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
18790 		trace_cfg80211_return_bool(false);
18791 		return false;
18792 	}
18793 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
18794 					 addr, gfp);
18795 	trace_cfg80211_return_bool(ret);
18796 	return ret;
18797 }
18798 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
18799 
18800 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
18801 					const u8 *addr, gfp_t gfp)
18802 {
18803 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18804 	bool ret;
18805 
18806 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
18807 
18808 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18809 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
18810 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
18811 		trace_cfg80211_return_bool(false);
18812 		return false;
18813 	}
18814 	ret = __nl80211_unexpected_frame(dev,
18815 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
18816 					 addr, gfp);
18817 	trace_cfg80211_return_bool(ret);
18818 	return ret;
18819 }
18820 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
18821 
18822 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
18823 		      struct wireless_dev *wdev, u32 nlportid,
18824 		      struct cfg80211_rx_info *info, gfp_t gfp)
18825 {
18826 	struct net_device *netdev = wdev->netdev;
18827 	struct sk_buff *msg;
18828 	void *hdr;
18829 
18830 	msg = nlmsg_new(100 + info->len, gfp);
18831 	if (!msg)
18832 		return -ENOMEM;
18833 
18834 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
18835 	if (!hdr) {
18836 		nlmsg_free(msg);
18837 		return -ENOMEM;
18838 	}
18839 
18840 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18841 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18842 					netdev->ifindex)) ||
18843 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18844 			      NL80211_ATTR_PAD) ||
18845 	    (info->have_link_id &&
18846 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, info->link_id)) ||
18847 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, KHZ_TO_MHZ(info->freq)) ||
18848 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET, info->freq % 1000) ||
18849 	    (info->sig_dbm &&
18850 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, info->sig_dbm)) ||
18851 	    nla_put(msg, NL80211_ATTR_FRAME, info->len, info->buf) ||
18852 	    (info->flags &&
18853 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, info->flags)) ||
18854 	    (info->rx_tstamp && nla_put_u64_64bit(msg,
18855 						  NL80211_ATTR_RX_HW_TIMESTAMP,
18856 						  info->rx_tstamp,
18857 						  NL80211_ATTR_PAD)) ||
18858 	    (info->ack_tstamp && nla_put_u64_64bit(msg,
18859 						   NL80211_ATTR_TX_HW_TIMESTAMP,
18860 						   info->ack_tstamp,
18861 						   NL80211_ATTR_PAD)))
18862 		goto nla_put_failure;
18863 
18864 	genlmsg_end(msg, hdr);
18865 
18866 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18867 
18868  nla_put_failure:
18869 	nlmsg_free(msg);
18870 	return -ENOBUFS;
18871 }
18872 
18873 static void nl80211_frame_tx_status(struct wireless_dev *wdev,
18874 				    struct cfg80211_tx_status *status,
18875 				    gfp_t gfp, enum nl80211_commands command)
18876 {
18877 	struct wiphy *wiphy = wdev->wiphy;
18878 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18879 	struct net_device *netdev = wdev->netdev;
18880 	struct sk_buff *msg;
18881 	void *hdr;
18882 
18883 	if (command == NL80211_CMD_FRAME_TX_STATUS)
18884 		trace_cfg80211_mgmt_tx_status(wdev, status->cookie,
18885 					      status->ack);
18886 	else
18887 		trace_cfg80211_control_port_tx_status(wdev, status->cookie,
18888 						      status->ack);
18889 
18890 	msg = nlmsg_new(100 + status->len, gfp);
18891 	if (!msg)
18892 		return;
18893 
18894 	hdr = nl80211hdr_put(msg, 0, 0, 0, command);
18895 	if (!hdr) {
18896 		nlmsg_free(msg);
18897 		return;
18898 	}
18899 
18900 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18901 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18902 				   netdev->ifindex)) ||
18903 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18904 			      NL80211_ATTR_PAD) ||
18905 	    nla_put(msg, NL80211_ATTR_FRAME, status->len, status->buf) ||
18906 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, status->cookie,
18907 			      NL80211_ATTR_PAD) ||
18908 	    (status->ack && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
18909 	    (status->tx_tstamp &&
18910 	     nla_put_u64_64bit(msg, NL80211_ATTR_TX_HW_TIMESTAMP,
18911 			       status->tx_tstamp, NL80211_ATTR_PAD)) ||
18912 	    (status->ack_tstamp &&
18913 	     nla_put_u64_64bit(msg, NL80211_ATTR_RX_HW_TIMESTAMP,
18914 			       status->ack_tstamp, NL80211_ATTR_PAD)))
18915 		goto nla_put_failure;
18916 
18917 	genlmsg_end(msg, hdr);
18918 
18919 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18920 				NL80211_MCGRP_MLME, gfp);
18921 	return;
18922 
18923 nla_put_failure:
18924 	nlmsg_free(msg);
18925 }
18926 
18927 void cfg80211_control_port_tx_status(struct wireless_dev *wdev, u64 cookie,
18928 				     const u8 *buf, size_t len, bool ack,
18929 				     gfp_t gfp)
18930 {
18931 	struct cfg80211_tx_status status = {
18932 		.cookie = cookie,
18933 		.buf = buf,
18934 		.len = len,
18935 		.ack = ack
18936 	};
18937 
18938 	nl80211_frame_tx_status(wdev, &status, gfp,
18939 				NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS);
18940 }
18941 EXPORT_SYMBOL(cfg80211_control_port_tx_status);
18942 
18943 void cfg80211_mgmt_tx_status_ext(struct wireless_dev *wdev,
18944 				 struct cfg80211_tx_status *status, gfp_t gfp)
18945 {
18946 	nl80211_frame_tx_status(wdev, status, gfp, NL80211_CMD_FRAME_TX_STATUS);
18947 }
18948 EXPORT_SYMBOL(cfg80211_mgmt_tx_status_ext);
18949 
18950 static int __nl80211_rx_control_port(struct net_device *dev,
18951 				     struct sk_buff *skb,
18952 				     bool unencrypted,
18953 				     int link_id,
18954 				     gfp_t gfp)
18955 {
18956 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18957 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18958 	struct ethhdr *ehdr = eth_hdr(skb);
18959 	const u8 *addr = ehdr->h_source;
18960 	u16 proto = be16_to_cpu(skb->protocol);
18961 	struct sk_buff *msg;
18962 	void *hdr;
18963 	struct nlattr *frame;
18964 
18965 	u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
18966 
18967 	if (!nlportid)
18968 		return -ENOENT;
18969 
18970 	msg = nlmsg_new(100 + skb->len, gfp);
18971 	if (!msg)
18972 		return -ENOMEM;
18973 
18974 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
18975 	if (!hdr) {
18976 		nlmsg_free(msg);
18977 		return -ENOBUFS;
18978 	}
18979 
18980 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18981 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18982 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18983 			      NL80211_ATTR_PAD) ||
18984 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
18985 	    nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
18986 	    (link_id >= 0 &&
18987 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
18988 	    (unencrypted && nla_put_flag(msg,
18989 					 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
18990 		goto nla_put_failure;
18991 
18992 	frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
18993 	if (!frame)
18994 		goto nla_put_failure;
18995 
18996 	skb_copy_bits(skb, 0, nla_data(frame), skb->len);
18997 	genlmsg_end(msg, hdr);
18998 
18999 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
19000 
19001  nla_put_failure:
19002 	nlmsg_free(msg);
19003 	return -ENOBUFS;
19004 }
19005 
19006 bool cfg80211_rx_control_port(struct net_device *dev, struct sk_buff *skb,
19007 			      bool unencrypted, int link_id)
19008 {
19009 	int ret;
19010 
19011 	trace_cfg80211_rx_control_port(dev, skb, unencrypted, link_id);
19012 	ret = __nl80211_rx_control_port(dev, skb, unencrypted, link_id,
19013 					GFP_ATOMIC);
19014 	trace_cfg80211_return_bool(ret == 0);
19015 	return ret == 0;
19016 }
19017 EXPORT_SYMBOL(cfg80211_rx_control_port);
19018 
19019 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
19020 					    const char *mac, gfp_t gfp)
19021 {
19022 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19023 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19024 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19025 	void **cb;
19026 
19027 	if (!msg)
19028 		return NULL;
19029 
19030 	cb = (void **)msg->cb;
19031 
19032 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
19033 	if (!cb[0]) {
19034 		nlmsg_free(msg);
19035 		return NULL;
19036 	}
19037 
19038 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19039 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19040 		goto nla_put_failure;
19041 
19042 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
19043 		goto nla_put_failure;
19044 
19045 	cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
19046 	if (!cb[1])
19047 		goto nla_put_failure;
19048 
19049 	cb[2] = rdev;
19050 
19051 	return msg;
19052  nla_put_failure:
19053 	nlmsg_free(msg);
19054 	return NULL;
19055 }
19056 
19057 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
19058 {
19059 	void **cb = (void **)msg->cb;
19060 	struct cfg80211_registered_device *rdev = cb[2];
19061 
19062 	nla_nest_end(msg, cb[1]);
19063 	genlmsg_end(msg, cb[0]);
19064 
19065 	memset(msg->cb, 0, sizeof(msg->cb));
19066 
19067 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19068 				NL80211_MCGRP_MLME, gfp);
19069 }
19070 
19071 void cfg80211_cqm_rssi_notify(struct net_device *dev,
19072 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
19073 			      s32 rssi_level, gfp_t gfp)
19074 {
19075 	struct sk_buff *msg;
19076 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19077 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19078 
19079 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
19080 
19081 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
19082 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
19083 		return;
19084 
19085 	if (wdev->cqm_config) {
19086 		wdev->cqm_config->last_rssi_event_value = rssi_level;
19087 
19088 		cfg80211_cqm_rssi_update(rdev, dev);
19089 
19090 		if (rssi_level == 0)
19091 			rssi_level = wdev->cqm_config->last_rssi_event_value;
19092 	}
19093 
19094 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
19095 	if (!msg)
19096 		return;
19097 
19098 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
19099 			rssi_event))
19100 		goto nla_put_failure;
19101 
19102 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
19103 				      rssi_level))
19104 		goto nla_put_failure;
19105 
19106 	cfg80211_send_cqm(msg, gfp);
19107 
19108 	return;
19109 
19110  nla_put_failure:
19111 	nlmsg_free(msg);
19112 }
19113 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
19114 
19115 void cfg80211_cqm_txe_notify(struct net_device *dev,
19116 			     const u8 *peer, u32 num_packets,
19117 			     u32 rate, u32 intvl, gfp_t gfp)
19118 {
19119 	struct sk_buff *msg;
19120 
19121 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
19122 	if (!msg)
19123 		return;
19124 
19125 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
19126 		goto nla_put_failure;
19127 
19128 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
19129 		goto nla_put_failure;
19130 
19131 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
19132 		goto nla_put_failure;
19133 
19134 	cfg80211_send_cqm(msg, gfp);
19135 	return;
19136 
19137  nla_put_failure:
19138 	nlmsg_free(msg);
19139 }
19140 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
19141 
19142 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
19143 				 const u8 *peer, u32 num_packets, gfp_t gfp)
19144 {
19145 	struct sk_buff *msg;
19146 
19147 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
19148 
19149 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
19150 	if (!msg)
19151 		return;
19152 
19153 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
19154 		goto nla_put_failure;
19155 
19156 	cfg80211_send_cqm(msg, gfp);
19157 	return;
19158 
19159  nla_put_failure:
19160 	nlmsg_free(msg);
19161 }
19162 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
19163 
19164 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
19165 {
19166 	struct sk_buff *msg;
19167 
19168 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
19169 	if (!msg)
19170 		return;
19171 
19172 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
19173 		goto nla_put_failure;
19174 
19175 	cfg80211_send_cqm(msg, gfp);
19176 	return;
19177 
19178  nla_put_failure:
19179 	nlmsg_free(msg);
19180 }
19181 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
19182 
19183 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
19184 				     struct net_device *netdev, const u8 *bssid,
19185 				     const u8 *replay_ctr, gfp_t gfp)
19186 {
19187 	struct sk_buff *msg;
19188 	struct nlattr *rekey_attr;
19189 	void *hdr;
19190 
19191 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19192 	if (!msg)
19193 		return;
19194 
19195 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
19196 	if (!hdr) {
19197 		nlmsg_free(msg);
19198 		return;
19199 	}
19200 
19201 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19202 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19203 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
19204 		goto nla_put_failure;
19205 
19206 	rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
19207 	if (!rekey_attr)
19208 		goto nla_put_failure;
19209 
19210 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
19211 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
19212 		goto nla_put_failure;
19213 
19214 	nla_nest_end(msg, rekey_attr);
19215 
19216 	genlmsg_end(msg, hdr);
19217 
19218 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19219 				NL80211_MCGRP_MLME, gfp);
19220 	return;
19221 
19222  nla_put_failure:
19223 	nlmsg_free(msg);
19224 }
19225 
19226 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
19227 			       const u8 *replay_ctr, gfp_t gfp)
19228 {
19229 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19230 	struct wiphy *wiphy = wdev->wiphy;
19231 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19232 
19233 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
19234 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
19235 }
19236 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
19237 
19238 static void
19239 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
19240 			       struct net_device *netdev, int index,
19241 			       const u8 *bssid, bool preauth, gfp_t gfp)
19242 {
19243 	struct sk_buff *msg;
19244 	struct nlattr *attr;
19245 	void *hdr;
19246 
19247 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19248 	if (!msg)
19249 		return;
19250 
19251 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
19252 	if (!hdr) {
19253 		nlmsg_free(msg);
19254 		return;
19255 	}
19256 
19257 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19258 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
19259 		goto nla_put_failure;
19260 
19261 	attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
19262 	if (!attr)
19263 		goto nla_put_failure;
19264 
19265 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
19266 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
19267 	    (preauth &&
19268 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
19269 		goto nla_put_failure;
19270 
19271 	nla_nest_end(msg, attr);
19272 
19273 	genlmsg_end(msg, hdr);
19274 
19275 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19276 				NL80211_MCGRP_MLME, gfp);
19277 	return;
19278 
19279  nla_put_failure:
19280 	nlmsg_free(msg);
19281 }
19282 
19283 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
19284 				     const u8 *bssid, bool preauth, gfp_t gfp)
19285 {
19286 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19287 	struct wiphy *wiphy = wdev->wiphy;
19288 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19289 
19290 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
19291 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
19292 }
19293 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
19294 
19295 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
19296 				     struct net_device *netdev,
19297 				     unsigned int link_id,
19298 				     struct cfg80211_chan_def *chandef,
19299 				     gfp_t gfp,
19300 				     enum nl80211_commands notif,
19301 				     u8 count, bool quiet, u16 punct_bitmap)
19302 {
19303 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
19304 	struct sk_buff *msg;
19305 	void *hdr;
19306 
19307 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19308 	if (!msg)
19309 		return;
19310 
19311 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
19312 	if (!hdr) {
19313 		nlmsg_free(msg);
19314 		return;
19315 	}
19316 
19317 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
19318 		goto nla_put_failure;
19319 
19320 	if (wdev->valid_links &&
19321 	    nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
19322 		goto nla_put_failure;
19323 
19324 	if (nl80211_send_chandef(msg, chandef))
19325 		goto nla_put_failure;
19326 
19327 	if (notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) {
19328 		if (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count))
19329 			goto nla_put_failure;
19330 		if (quiet &&
19331 		    nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX))
19332 			goto nla_put_failure;
19333 	}
19334 
19335 	if (nla_put_u32(msg, NL80211_ATTR_PUNCT_BITMAP, punct_bitmap))
19336 		goto nla_put_failure;
19337 
19338 	genlmsg_end(msg, hdr);
19339 
19340 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19341 				NL80211_MCGRP_MLME, gfp);
19342 	return;
19343 
19344  nla_put_failure:
19345 	nlmsg_free(msg);
19346 }
19347 
19348 void cfg80211_ch_switch_notify(struct net_device *dev,
19349 			       struct cfg80211_chan_def *chandef,
19350 			       unsigned int link_id, u16 punct_bitmap)
19351 {
19352 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19353 	struct wiphy *wiphy = wdev->wiphy;
19354 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19355 
19356 	ASSERT_WDEV_LOCK(wdev);
19357 	WARN_INVALID_LINK_ID(wdev, link_id);
19358 
19359 	trace_cfg80211_ch_switch_notify(dev, chandef, link_id, punct_bitmap);
19360 
19361 	switch (wdev->iftype) {
19362 	case NL80211_IFTYPE_STATION:
19363 	case NL80211_IFTYPE_P2P_CLIENT:
19364 		if (!WARN_ON(!wdev->links[link_id].client.current_bss))
19365 			cfg80211_update_assoc_bss_entry(wdev, link_id,
19366 							chandef->chan);
19367 		break;
19368 	case NL80211_IFTYPE_MESH_POINT:
19369 		wdev->u.mesh.chandef = *chandef;
19370 		wdev->u.mesh.preset_chandef = *chandef;
19371 		break;
19372 	case NL80211_IFTYPE_AP:
19373 	case NL80211_IFTYPE_P2P_GO:
19374 		wdev->links[link_id].ap.chandef = *chandef;
19375 		break;
19376 	case NL80211_IFTYPE_ADHOC:
19377 		wdev->u.ibss.chandef = *chandef;
19378 		break;
19379 	default:
19380 		WARN_ON(1);
19381 		break;
19382 	}
19383 
19384 	cfg80211_sched_dfs_chan_update(rdev);
19385 
19386 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19387 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0, false,
19388 				 punct_bitmap);
19389 }
19390 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
19391 
19392 void cfg80211_ch_switch_started_notify(struct net_device *dev,
19393 				       struct cfg80211_chan_def *chandef,
19394 				       unsigned int link_id, u8 count,
19395 				       bool quiet, u16 punct_bitmap)
19396 {
19397 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19398 	struct wiphy *wiphy = wdev->wiphy;
19399 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19400 
19401 	ASSERT_WDEV_LOCK(wdev);
19402 	WARN_INVALID_LINK_ID(wdev, link_id);
19403 
19404 	trace_cfg80211_ch_switch_started_notify(dev, chandef, link_id,
19405 						punct_bitmap);
19406 
19407 
19408 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19409 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY,
19410 				 count, quiet, punct_bitmap);
19411 }
19412 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
19413 
19414 int cfg80211_bss_color_notify(struct net_device *dev,
19415 			      enum nl80211_commands cmd, u8 count,
19416 			      u64 color_bitmap)
19417 {
19418 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19419 	struct wiphy *wiphy = wdev->wiphy;
19420 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19421 	struct sk_buff *msg;
19422 	void *hdr;
19423 
19424 	ASSERT_WDEV_LOCK(wdev);
19425 
19426 	trace_cfg80211_bss_color_notify(dev, cmd, count, color_bitmap);
19427 
19428 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
19429 	if (!msg)
19430 		return -ENOMEM;
19431 
19432 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
19433 	if (!hdr)
19434 		goto nla_put_failure;
19435 
19436 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19437 		goto nla_put_failure;
19438 
19439 	if (cmd == NL80211_CMD_COLOR_CHANGE_STARTED &&
19440 	    nla_put_u32(msg, NL80211_ATTR_COLOR_CHANGE_COUNT, count))
19441 		goto nla_put_failure;
19442 
19443 	if (cmd == NL80211_CMD_OBSS_COLOR_COLLISION &&
19444 	    nla_put_u64_64bit(msg, NL80211_ATTR_OBSS_COLOR_BITMAP,
19445 			      color_bitmap, NL80211_ATTR_PAD))
19446 		goto nla_put_failure;
19447 
19448 	genlmsg_end(msg, hdr);
19449 
19450 	return genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
19451 				       msg, 0, NL80211_MCGRP_MLME, GFP_KERNEL);
19452 
19453 nla_put_failure:
19454 	nlmsg_free(msg);
19455 	return -EINVAL;
19456 }
19457 EXPORT_SYMBOL(cfg80211_bss_color_notify);
19458 
19459 void
19460 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
19461 		     const struct cfg80211_chan_def *chandef,
19462 		     enum nl80211_radar_event event,
19463 		     struct net_device *netdev, gfp_t gfp)
19464 {
19465 	struct sk_buff *msg;
19466 	void *hdr;
19467 
19468 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19469 	if (!msg)
19470 		return;
19471 
19472 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
19473 	if (!hdr) {
19474 		nlmsg_free(msg);
19475 		return;
19476 	}
19477 
19478 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19479 		goto nla_put_failure;
19480 
19481 	/* NOP and radar events don't need a netdev parameter */
19482 	if (netdev) {
19483 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
19484 
19485 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19486 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19487 				      NL80211_ATTR_PAD))
19488 			goto nla_put_failure;
19489 	}
19490 
19491 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
19492 		goto nla_put_failure;
19493 
19494 	if (nl80211_send_chandef(msg, chandef))
19495 		goto nla_put_failure;
19496 
19497 	genlmsg_end(msg, hdr);
19498 
19499 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19500 				NL80211_MCGRP_MLME, gfp);
19501 	return;
19502 
19503  nla_put_failure:
19504 	nlmsg_free(msg);
19505 }
19506 
19507 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
19508 				       struct sta_opmode_info *sta_opmode,
19509 				       gfp_t gfp)
19510 {
19511 	struct sk_buff *msg;
19512 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19513 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19514 	void *hdr;
19515 
19516 	if (WARN_ON(!mac))
19517 		return;
19518 
19519 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19520 	if (!msg)
19521 		return;
19522 
19523 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
19524 	if (!hdr) {
19525 		nlmsg_free(msg);
19526 		return;
19527 	}
19528 
19529 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19530 		goto nla_put_failure;
19531 
19532 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19533 		goto nla_put_failure;
19534 
19535 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
19536 		goto nla_put_failure;
19537 
19538 	if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
19539 	    nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
19540 		goto nla_put_failure;
19541 
19542 	if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
19543 	    nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
19544 		goto nla_put_failure;
19545 
19546 	if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
19547 	    nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
19548 		goto nla_put_failure;
19549 
19550 	genlmsg_end(msg, hdr);
19551 
19552 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19553 				NL80211_MCGRP_MLME, gfp);
19554 
19555 	return;
19556 
19557 nla_put_failure:
19558 	nlmsg_free(msg);
19559 }
19560 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
19561 
19562 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
19563 			   u64 cookie, bool acked, s32 ack_signal,
19564 			   bool is_valid_ack_signal, gfp_t gfp)
19565 {
19566 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19567 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19568 	struct sk_buff *msg;
19569 	void *hdr;
19570 
19571 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
19572 
19573 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19574 
19575 	if (!msg)
19576 		return;
19577 
19578 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
19579 	if (!hdr) {
19580 		nlmsg_free(msg);
19581 		return;
19582 	}
19583 
19584 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19585 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19586 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
19587 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
19588 			      NL80211_ATTR_PAD) ||
19589 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
19590 	    (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
19591 						ack_signal)))
19592 		goto nla_put_failure;
19593 
19594 	genlmsg_end(msg, hdr);
19595 
19596 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19597 				NL80211_MCGRP_MLME, gfp);
19598 	return;
19599 
19600  nla_put_failure:
19601 	nlmsg_free(msg);
19602 }
19603 EXPORT_SYMBOL(cfg80211_probe_status);
19604 
19605 void cfg80211_report_obss_beacon_khz(struct wiphy *wiphy, const u8 *frame,
19606 				     size_t len, int freq, int sig_dbm)
19607 {
19608 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19609 	struct sk_buff *msg;
19610 	void *hdr;
19611 	struct cfg80211_beacon_registration *reg;
19612 
19613 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
19614 
19615 	spin_lock_bh(&rdev->beacon_registrations_lock);
19616 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
19617 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
19618 		if (!msg) {
19619 			spin_unlock_bh(&rdev->beacon_registrations_lock);
19620 			return;
19621 		}
19622 
19623 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
19624 		if (!hdr)
19625 			goto nla_put_failure;
19626 
19627 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19628 		    (freq &&
19629 		     (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
19630 				  KHZ_TO_MHZ(freq)) ||
19631 		      nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
19632 				  freq % 1000))) ||
19633 		    (sig_dbm &&
19634 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
19635 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
19636 			goto nla_put_failure;
19637 
19638 		genlmsg_end(msg, hdr);
19639 
19640 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
19641 	}
19642 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19643 	return;
19644 
19645  nla_put_failure:
19646 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19647 	nlmsg_free(msg);
19648 }
19649 EXPORT_SYMBOL(cfg80211_report_obss_beacon_khz);
19650 
19651 #ifdef CONFIG_PM
19652 static int cfg80211_net_detect_results(struct sk_buff *msg,
19653 				       struct cfg80211_wowlan_wakeup *wakeup)
19654 {
19655 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
19656 	struct nlattr *nl_results, *nl_match, *nl_freqs;
19657 	int i, j;
19658 
19659 	nl_results = nla_nest_start_noflag(msg,
19660 					   NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
19661 	if (!nl_results)
19662 		return -EMSGSIZE;
19663 
19664 	for (i = 0; i < nd->n_matches; i++) {
19665 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
19666 
19667 		nl_match = nla_nest_start_noflag(msg, i);
19668 		if (!nl_match)
19669 			break;
19670 
19671 		/* The SSID attribute is optional in nl80211, but for
19672 		 * simplicity reasons it's always present in the
19673 		 * cfg80211 structure.  If a driver can't pass the
19674 		 * SSID, that needs to be changed.  A zero length SSID
19675 		 * is still a valid SSID (wildcard), so it cannot be
19676 		 * used for this purpose.
19677 		 */
19678 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
19679 			    match->ssid.ssid)) {
19680 			nla_nest_cancel(msg, nl_match);
19681 			goto out;
19682 		}
19683 
19684 		if (match->n_channels) {
19685 			nl_freqs = nla_nest_start_noflag(msg,
19686 							 NL80211_ATTR_SCAN_FREQUENCIES);
19687 			if (!nl_freqs) {
19688 				nla_nest_cancel(msg, nl_match);
19689 				goto out;
19690 			}
19691 
19692 			for (j = 0; j < match->n_channels; j++) {
19693 				if (nla_put_u32(msg, j, match->channels[j])) {
19694 					nla_nest_cancel(msg, nl_freqs);
19695 					nla_nest_cancel(msg, nl_match);
19696 					goto out;
19697 				}
19698 			}
19699 
19700 			nla_nest_end(msg, nl_freqs);
19701 		}
19702 
19703 		nla_nest_end(msg, nl_match);
19704 	}
19705 
19706 out:
19707 	nla_nest_end(msg, nl_results);
19708 	return 0;
19709 }
19710 
19711 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
19712 				   struct cfg80211_wowlan_wakeup *wakeup,
19713 				   gfp_t gfp)
19714 {
19715 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19716 	struct sk_buff *msg;
19717 	void *hdr;
19718 	int size = 200;
19719 
19720 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
19721 
19722 	if (wakeup)
19723 		size += wakeup->packet_present_len;
19724 
19725 	msg = nlmsg_new(size, gfp);
19726 	if (!msg)
19727 		return;
19728 
19729 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
19730 	if (!hdr)
19731 		goto free_msg;
19732 
19733 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19734 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19735 			      NL80211_ATTR_PAD))
19736 		goto free_msg;
19737 
19738 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
19739 					wdev->netdev->ifindex))
19740 		goto free_msg;
19741 
19742 	if (wakeup) {
19743 		struct nlattr *reasons;
19744 
19745 		reasons = nla_nest_start_noflag(msg,
19746 						NL80211_ATTR_WOWLAN_TRIGGERS);
19747 		if (!reasons)
19748 			goto free_msg;
19749 
19750 		if (wakeup->disconnect &&
19751 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
19752 			goto free_msg;
19753 		if (wakeup->magic_pkt &&
19754 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
19755 			goto free_msg;
19756 		if (wakeup->gtk_rekey_failure &&
19757 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
19758 			goto free_msg;
19759 		if (wakeup->eap_identity_req &&
19760 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
19761 			goto free_msg;
19762 		if (wakeup->four_way_handshake &&
19763 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
19764 			goto free_msg;
19765 		if (wakeup->rfkill_release &&
19766 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
19767 			goto free_msg;
19768 
19769 		if (wakeup->pattern_idx >= 0 &&
19770 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
19771 				wakeup->pattern_idx))
19772 			goto free_msg;
19773 
19774 		if (wakeup->tcp_match &&
19775 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
19776 			goto free_msg;
19777 
19778 		if (wakeup->tcp_connlost &&
19779 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
19780 			goto free_msg;
19781 
19782 		if (wakeup->tcp_nomoretokens &&
19783 		    nla_put_flag(msg,
19784 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
19785 			goto free_msg;
19786 
19787 		if (wakeup->packet) {
19788 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
19789 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
19790 
19791 			if (!wakeup->packet_80211) {
19792 				pkt_attr =
19793 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
19794 				len_attr =
19795 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
19796 			}
19797 
19798 			if (wakeup->packet_len &&
19799 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
19800 				goto free_msg;
19801 
19802 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
19803 				    wakeup->packet))
19804 				goto free_msg;
19805 		}
19806 
19807 		if (wakeup->net_detect &&
19808 		    cfg80211_net_detect_results(msg, wakeup))
19809 				goto free_msg;
19810 
19811 		nla_nest_end(msg, reasons);
19812 	}
19813 
19814 	genlmsg_end(msg, hdr);
19815 
19816 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19817 				NL80211_MCGRP_MLME, gfp);
19818 	return;
19819 
19820  free_msg:
19821 	nlmsg_free(msg);
19822 }
19823 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
19824 #endif
19825 
19826 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
19827 				enum nl80211_tdls_operation oper,
19828 				u16 reason_code, gfp_t gfp)
19829 {
19830 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19831 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19832 	struct sk_buff *msg;
19833 	void *hdr;
19834 
19835 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
19836 					 reason_code);
19837 
19838 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19839 	if (!msg)
19840 		return;
19841 
19842 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
19843 	if (!hdr) {
19844 		nlmsg_free(msg);
19845 		return;
19846 	}
19847 
19848 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19849 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19850 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
19851 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
19852 	    (reason_code > 0 &&
19853 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
19854 		goto nla_put_failure;
19855 
19856 	genlmsg_end(msg, hdr);
19857 
19858 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19859 				NL80211_MCGRP_MLME, gfp);
19860 	return;
19861 
19862  nla_put_failure:
19863 	nlmsg_free(msg);
19864 }
19865 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
19866 
19867 static int nl80211_netlink_notify(struct notifier_block * nb,
19868 				  unsigned long state,
19869 				  void *_notify)
19870 {
19871 	struct netlink_notify *notify = _notify;
19872 	struct cfg80211_registered_device *rdev;
19873 	struct wireless_dev *wdev;
19874 	struct cfg80211_beacon_registration *reg, *tmp;
19875 
19876 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
19877 		return NOTIFY_DONE;
19878 
19879 	rcu_read_lock();
19880 
19881 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
19882 		struct cfg80211_sched_scan_request *sched_scan_req;
19883 
19884 		list_for_each_entry_rcu(sched_scan_req,
19885 					&rdev->sched_scan_req_list,
19886 					list) {
19887 			if (sched_scan_req->owner_nlportid == notify->portid) {
19888 				sched_scan_req->nl_owner_dead = true;
19889 				wiphy_work_queue(&rdev->wiphy,
19890 						 &rdev->sched_scan_stop_wk);
19891 			}
19892 		}
19893 
19894 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
19895 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
19896 
19897 			if (wdev->owner_nlportid == notify->portid) {
19898 				wdev->nl_owner_dead = true;
19899 				schedule_work(&rdev->destroy_work);
19900 			} else if (wdev->conn_owner_nlportid == notify->portid) {
19901 				schedule_work(&wdev->disconnect_wk);
19902 			}
19903 
19904 			cfg80211_release_pmsr(wdev, notify->portid);
19905 		}
19906 
19907 		spin_lock_bh(&rdev->beacon_registrations_lock);
19908 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
19909 					 list) {
19910 			if (reg->nlportid == notify->portid) {
19911 				list_del(&reg->list);
19912 				kfree(reg);
19913 				break;
19914 			}
19915 		}
19916 		spin_unlock_bh(&rdev->beacon_registrations_lock);
19917 	}
19918 
19919 	rcu_read_unlock();
19920 
19921 	/*
19922 	 * It is possible that the user space process that is controlling the
19923 	 * indoor setting disappeared, so notify the regulatory core.
19924 	 */
19925 	regulatory_netlink_notify(notify->portid);
19926 	return NOTIFY_OK;
19927 }
19928 
19929 static struct notifier_block nl80211_netlink_notifier = {
19930 	.notifier_call = nl80211_netlink_notify,
19931 };
19932 
19933 void cfg80211_ft_event(struct net_device *netdev,
19934 		       struct cfg80211_ft_event_params *ft_event)
19935 {
19936 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
19937 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19938 	struct sk_buff *msg;
19939 	void *hdr;
19940 
19941 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
19942 
19943 	if (!ft_event->target_ap)
19944 		return;
19945 
19946 	msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
19947 			GFP_KERNEL);
19948 	if (!msg)
19949 		return;
19950 
19951 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
19952 	if (!hdr)
19953 		goto out;
19954 
19955 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19956 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19957 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
19958 		goto out;
19959 
19960 	if (ft_event->ies &&
19961 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
19962 		goto out;
19963 	if (ft_event->ric_ies &&
19964 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
19965 		    ft_event->ric_ies))
19966 		goto out;
19967 
19968 	genlmsg_end(msg, hdr);
19969 
19970 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19971 				NL80211_MCGRP_MLME, GFP_KERNEL);
19972 	return;
19973  out:
19974 	nlmsg_free(msg);
19975 }
19976 EXPORT_SYMBOL(cfg80211_ft_event);
19977 
19978 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
19979 {
19980 	struct cfg80211_registered_device *rdev;
19981 	struct sk_buff *msg;
19982 	void *hdr;
19983 	u32 nlportid;
19984 
19985 	rdev = wiphy_to_rdev(wdev->wiphy);
19986 	if (!rdev->crit_proto_nlportid)
19987 		return;
19988 
19989 	nlportid = rdev->crit_proto_nlportid;
19990 	rdev->crit_proto_nlportid = 0;
19991 
19992 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19993 	if (!msg)
19994 		return;
19995 
19996 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
19997 	if (!hdr)
19998 		goto nla_put_failure;
19999 
20000 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20001 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
20002 			      NL80211_ATTR_PAD))
20003 		goto nla_put_failure;
20004 
20005 	genlmsg_end(msg, hdr);
20006 
20007 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
20008 	return;
20009 
20010  nla_put_failure:
20011 	nlmsg_free(msg);
20012 }
20013 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
20014 
20015 void nl80211_send_ap_stopped(struct wireless_dev *wdev, unsigned int link_id)
20016 {
20017 	struct wiphy *wiphy = wdev->wiphy;
20018 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
20019 	struct sk_buff *msg;
20020 	void *hdr;
20021 
20022 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
20023 	if (!msg)
20024 		return;
20025 
20026 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
20027 	if (!hdr)
20028 		goto out;
20029 
20030 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20031 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
20032 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
20033 			      NL80211_ATTR_PAD) ||
20034 	    (wdev->valid_links &&
20035 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)))
20036 		goto out;
20037 
20038 	genlmsg_end(msg, hdr);
20039 
20040 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
20041 				NL80211_MCGRP_MLME, GFP_KERNEL);
20042 	return;
20043  out:
20044 	nlmsg_free(msg);
20045 }
20046 
20047 int cfg80211_external_auth_request(struct net_device *dev,
20048 				   struct cfg80211_external_auth_params *params,
20049 				   gfp_t gfp)
20050 {
20051 	struct wireless_dev *wdev = dev->ieee80211_ptr;
20052 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
20053 	struct sk_buff *msg;
20054 	void *hdr;
20055 
20056 	if (!wdev->conn_owner_nlportid)
20057 		return -EINVAL;
20058 
20059 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
20060 	if (!msg)
20061 		return -ENOMEM;
20062 
20063 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
20064 	if (!hdr)
20065 		goto nla_put_failure;
20066 
20067 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20068 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
20069 	    nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
20070 	    nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
20071 			params->action) ||
20072 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
20073 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
20074 		    params->ssid.ssid) ||
20075 	    (!is_zero_ether_addr(params->mld_addr) &&
20076 	     nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN, params->mld_addr)))
20077 		goto nla_put_failure;
20078 
20079 	genlmsg_end(msg, hdr);
20080 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
20081 			wdev->conn_owner_nlportid);
20082 	return 0;
20083 
20084  nla_put_failure:
20085 	nlmsg_free(msg);
20086 	return -ENOBUFS;
20087 }
20088 EXPORT_SYMBOL(cfg80211_external_auth_request);
20089 
20090 void cfg80211_update_owe_info_event(struct net_device *netdev,
20091 				    struct cfg80211_update_owe_info *owe_info,
20092 				    gfp_t gfp)
20093 {
20094 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
20095 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
20096 	struct sk_buff *msg;
20097 	void *hdr;
20098 
20099 	trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
20100 
20101 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
20102 	if (!msg)
20103 		return;
20104 
20105 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
20106 	if (!hdr)
20107 		goto nla_put_failure;
20108 
20109 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20110 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
20111 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
20112 		goto nla_put_failure;
20113 
20114 	if (!owe_info->ie_len ||
20115 	    nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
20116 		goto nla_put_failure;
20117 
20118 	if (owe_info->assoc_link_id != -1) {
20119 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
20120 			       owe_info->assoc_link_id))
20121 			goto nla_put_failure;
20122 
20123 		if (!is_zero_ether_addr(owe_info->peer_mld_addr) &&
20124 		    nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
20125 			    owe_info->peer_mld_addr))
20126 			goto nla_put_failure;
20127 	}
20128 
20129 	genlmsg_end(msg, hdr);
20130 
20131 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
20132 				NL80211_MCGRP_MLME, gfp);
20133 	return;
20134 
20135 nla_put_failure:
20136 	genlmsg_cancel(msg, hdr);
20137 	nlmsg_free(msg);
20138 }
20139 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
20140 
20141 /* initialisation/exit functions */
20142 
20143 int __init nl80211_init(void)
20144 {
20145 	int err;
20146 
20147 	err = genl_register_family(&nl80211_fam);
20148 	if (err)
20149 		return err;
20150 
20151 	err = netlink_register_notifier(&nl80211_netlink_notifier);
20152 	if (err)
20153 		goto err_out;
20154 
20155 	return 0;
20156  err_out:
20157 	genl_unregister_family(&nl80211_fam);
20158 	return err;
20159 }
20160 
20161 void nl80211_exit(void)
20162 {
20163 	netlink_unregister_notifier(&nl80211_netlink_notifier);
20164 	genl_unregister_family(&nl80211_fam);
20165 }
20166