xref: /linux/net/wireless/nl80211.c (revision 41e0d49104dbff888ef6446ea46842fde66c0a76)
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-2022 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 const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
466 	[0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
467 	[NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
468 	[NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
469 				      .len = 20-1 },
470 	[NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
471 
472 	[NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
473 	[NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
474 	[NL80211_ATTR_WIPHY_EDMG_CHANNELS] = NLA_POLICY_RANGE(NLA_U8,
475 						NL80211_EDMG_CHANNELS_MIN,
476 						NL80211_EDMG_CHANNELS_MAX),
477 	[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG] = NLA_POLICY_RANGE(NLA_U8,
478 						NL80211_EDMG_BW_CONFIG_MIN,
479 						NL80211_EDMG_BW_CONFIG_MAX),
480 
481 	[NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
482 	[NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
483 	[NL80211_ATTR_CENTER_FREQ1_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
484 	[NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
485 
486 	[NL80211_ATTR_WIPHY_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
487 	[NL80211_ATTR_WIPHY_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
488 	[NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
489 	[NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
490 	[NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
491 	[NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
492 
493 	[NL80211_ATTR_IFTYPE] = NLA_POLICY_MAX(NLA_U32, NL80211_IFTYPE_MAX),
494 	[NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
495 	[NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
496 
497 	[NL80211_ATTR_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
498 	[NL80211_ATTR_PREV_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
499 
500 	[NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
501 	[NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
502 				    .len = WLAN_MAX_KEY_LEN },
503 	[NL80211_ATTR_KEY_IDX] = NLA_POLICY_MAX(NLA_U8, 7),
504 	[NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
505 	[NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
506 	[NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
507 	[NL80211_ATTR_KEY_TYPE] =
508 		NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES),
509 
510 	[NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
511 	[NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
512 	[NL80211_ATTR_BEACON_HEAD] =
513 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_beacon_head,
514 				       IEEE80211_MAX_DATA_LEN),
515 	[NL80211_ATTR_BEACON_TAIL] =
516 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
517 				       IEEE80211_MAX_DATA_LEN),
518 	[NL80211_ATTR_STA_AID] =
519 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
520 	[NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
521 	[NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
522 	[NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
523 					       .len = NL80211_MAX_SUPP_RATES },
524 	[NL80211_ATTR_STA_PLINK_ACTION] =
525 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_ACTIONS - 1),
526 	[NL80211_ATTR_STA_TX_POWER_SETTING] =
527 		NLA_POLICY_RANGE(NLA_U8,
528 				 NL80211_TX_POWER_AUTOMATIC,
529 				 NL80211_TX_POWER_FIXED),
530 	[NL80211_ATTR_STA_TX_POWER] = { .type = NLA_S16 },
531 	[NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
532 	[NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
533 	[NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
534 				   .len = IEEE80211_MAX_MESH_ID_LEN },
535 	[NL80211_ATTR_MPATH_NEXT_HOP] = NLA_POLICY_ETH_ADDR_COMPAT,
536 
537 	/* allow 3 for NUL-termination, we used to declare this NLA_STRING */
538 	[NL80211_ATTR_REG_ALPHA2] = NLA_POLICY_RANGE(NLA_BINARY, 2, 3),
539 	[NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
540 
541 	[NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
542 	[NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
543 	[NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
544 	[NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
545 					   .len = NL80211_MAX_SUPP_RATES },
546 	[NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
547 
548 	[NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
549 	[NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
550 
551 	[NL80211_ATTR_HT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_HT_CAPABILITY_LEN),
552 
553 	[NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
554 	[NL80211_ATTR_IE] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
555 						   validate_ie_attr,
556 						   IEEE80211_MAX_DATA_LEN),
557 	[NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
558 	[NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
559 
560 	[NL80211_ATTR_SSID] = { .type = NLA_BINARY,
561 				.len = IEEE80211_MAX_SSID_LEN },
562 	[NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
563 	[NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
564 	[NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
565 	[NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
566 	[NL80211_ATTR_USE_MFP] = NLA_POLICY_RANGE(NLA_U32,
567 						  NL80211_MFP_NO,
568 						  NL80211_MFP_OPTIONAL),
569 	[NL80211_ATTR_STA_FLAGS2] =
570 		NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_sta_flag_update)),
571 	[NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
572 	[NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
573 	[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
574 	[NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
575 	[NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
576 	[NL80211_ATTR_STATUS_CODE] = { .type = NLA_U16 },
577 	[NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
578 	[NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
579 	[NL80211_ATTR_PID] = { .type = NLA_U32 },
580 	[NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
581 	[NL80211_ATTR_PMKID] = NLA_POLICY_EXACT_LEN_WARN(WLAN_PMKID_LEN),
582 	[NL80211_ATTR_DURATION] = { .type = NLA_U32 },
583 	[NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
584 	[NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
585 	[NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
586 				 .len = IEEE80211_MAX_DATA_LEN },
587 	[NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
588 	[NL80211_ATTR_PS_STATE] = NLA_POLICY_RANGE(NLA_U32,
589 						   NL80211_PS_DISABLED,
590 						   NL80211_PS_ENABLED),
591 	[NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
592 	[NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
593 	[NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
594 	[NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
595 	[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
596 	[NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
597 	[NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
598 	[NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
599 	[NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
600 	[NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
601 	[NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
602 	[NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
603 	[NL80211_ATTR_STA_PLINK_STATE] =
604 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_STATES - 1),
605 	[NL80211_ATTR_MEASUREMENT_DURATION] = { .type = NLA_U16 },
606 	[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY] = { .type = NLA_FLAG },
607 	[NL80211_ATTR_MESH_PEER_AID] =
608 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
609 	[NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
610 	[NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
611 	[NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
612 	[NL80211_ATTR_HIDDEN_SSID] =
613 		NLA_POLICY_RANGE(NLA_U32,
614 				 NL80211_HIDDEN_SSID_NOT_IN_USE,
615 				 NL80211_HIDDEN_SSID_ZERO_CONTENTS),
616 	[NL80211_ATTR_IE_PROBE_RESP] =
617 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
618 				       IEEE80211_MAX_DATA_LEN),
619 	[NL80211_ATTR_IE_ASSOC_RESP] =
620 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
621 				       IEEE80211_MAX_DATA_LEN),
622 	[NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
623 	[NL80211_ATTR_STA_WME] = NLA_POLICY_NESTED(nl80211_sta_wme_policy),
624 	[NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
625 	[NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
626 	[NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
627 	[NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
628 	[NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
629 	[NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
630 	[NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
631 	[NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
632 	[NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
633 	[NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
634 				      .len = IEEE80211_MAX_DATA_LEN },
635 	[NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
636 	[NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
637 	[NL80211_ATTR_HT_CAPABILITY_MASK] = {
638 		.len = NL80211_HT_CAPABILITY_LEN
639 	},
640 	[NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
641 	[NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
642 	[NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
643 	[NL80211_ATTR_WDEV] = { .type = NLA_U64 },
644 	[NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
645 
646 	/* need to include at least Auth Transaction and Status Code */
647 	[NL80211_ATTR_AUTH_DATA] = NLA_POLICY_MIN_LEN(4),
648 
649 	[NL80211_ATTR_VHT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_VHT_CAPABILITY_LEN),
650 	[NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
651 	[NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
652 	[NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
653 	[NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
654 		NLA_POLICY_RANGE(NLA_U32,
655 				 NL80211_MESH_POWER_UNKNOWN + 1,
656 				 NL80211_MESH_POWER_MAX),
657 	[NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
658 	[NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
659 	[NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
660 	[NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
661 	[NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
662 	[NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
663 	[NL80211_ATTR_VHT_CAPABILITY_MASK] = {
664 		.len = NL80211_VHT_CAPABILITY_LEN,
665 	},
666 	[NL80211_ATTR_MDID] = { .type = NLA_U16 },
667 	[NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
668 				  .len = IEEE80211_MAX_DATA_LEN },
669 	[NL80211_ATTR_CRIT_PROT_ID] = { .type = NLA_U16 },
670 	[NL80211_ATTR_MAX_CRIT_PROT_DURATION] =
671 		NLA_POLICY_MAX(NLA_U16, NL80211_CRIT_PROTO_MAX_DURATION),
672 	[NL80211_ATTR_PEER_AID] =
673 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
674 	[NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
675 	[NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
676 	[NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
677 	[NL80211_ATTR_CNTDWN_OFFS_BEACON] = { .type = NLA_BINARY },
678 	[NL80211_ATTR_CNTDWN_OFFS_PRESP] = { .type = NLA_BINARY },
679 	[NL80211_ATTR_STA_SUPPORTED_CHANNELS] = NLA_POLICY_MIN_LEN(2),
680 	/*
681 	 * The value of the Length field of the Supported Operating
682 	 * Classes element is between 2 and 253.
683 	 */
684 	[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] =
685 		NLA_POLICY_RANGE(NLA_BINARY, 2, 253),
686 	[NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
687 	[NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
688 	[NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
689 	[NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
690 	[NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
691 	[NL80211_ATTR_QOS_MAP] = NLA_POLICY_RANGE(NLA_BINARY,
692 						  IEEE80211_QOS_MAP_LEN_MIN,
693 						  IEEE80211_QOS_MAP_LEN_MAX),
694 	[NL80211_ATTR_MAC_HINT] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
695 	[NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
696 	[NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
697 	[NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
698 	[NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
699 	[NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
700 	[NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
701 	[NL80211_ATTR_USER_PRIO] =
702 		NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
703 	[NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
704 	[NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
705 	[NL80211_ATTR_OPER_CLASS] = { .type = NLA_U8 },
706 	[NL80211_ATTR_MAC_MASK] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
707 	[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
708 	[NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
709 	[NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
710 	[NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
711 	[NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
712 	[NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
713 	[NL80211_ATTR_STA_SUPPORT_P2P_PS] =
714 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
715 	[NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
716 		.len = VHT_MUMIMO_GROUPS_DATA_LEN
717 	},
718 	[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
719 	[NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
720 	[NL80211_ATTR_BANDS] = { .type = NLA_U32 },
721 	[NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
722 	[NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
723 				    .len = FILS_MAX_KEK_LEN },
724 	[NL80211_ATTR_FILS_NONCES] = NLA_POLICY_EXACT_LEN_WARN(2 * FILS_NONCE_LEN),
725 	[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
726 	[NL80211_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
727 	[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
728 	[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
729 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
730 	},
731 	[NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
732 	[NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
733 					     .len = FILS_ERP_MAX_USERNAME_LEN },
734 	[NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
735 					  .len = FILS_ERP_MAX_REALM_LEN },
736 	[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
737 	[NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
738 					.len = FILS_ERP_MAX_RRK_LEN },
739 	[NL80211_ATTR_FILS_CACHE_ID] = NLA_POLICY_EXACT_LEN_WARN(2),
740 	[NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
741 	[NL80211_ATTR_PMKR0_NAME] = NLA_POLICY_EXACT_LEN(WLAN_PMK_NAME_LEN),
742 	[NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
743 	[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
744 
745 	[NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
746 	[NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
747 	[NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
748 	[NL80211_ATTR_HE_CAPABILITY] =
749 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_he_capa,
750 				       NL80211_HE_MAX_CAPABILITY_LEN),
751 	[NL80211_ATTR_FTM_RESPONDER] =
752 		NLA_POLICY_NESTED(nl80211_ftm_responder_policy),
753 	[NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
754 	[NL80211_ATTR_PEER_MEASUREMENTS] =
755 		NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
756 	[NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
757 	[NL80211_ATTR_SAE_PASSWORD] = { .type = NLA_BINARY,
758 					.len = SAE_PASSWORD_MAX_LEN },
759 	[NL80211_ATTR_TWT_RESPONDER] = { .type = NLA_FLAG },
760 	[NL80211_ATTR_HE_OBSS_PD] = NLA_POLICY_NESTED(he_obss_pd_policy),
761 	[NL80211_ATTR_VLAN_ID] = NLA_POLICY_RANGE(NLA_U16, 1, VLAN_N_VID - 2),
762 	[NL80211_ATTR_HE_BSS_COLOR] = NLA_POLICY_NESTED(he_bss_color_policy),
763 	[NL80211_ATTR_TID_CONFIG] =
764 		NLA_POLICY_NESTED_ARRAY(nl80211_tid_config_attr_policy),
765 	[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH] = { .type = NLA_FLAG },
766 	[NL80211_ATTR_PMK_LIFETIME] = NLA_POLICY_MIN(NLA_U32, 1),
767 	[NL80211_ATTR_PMK_REAUTH_THRESHOLD] = NLA_POLICY_RANGE(NLA_U8, 1, 100),
768 	[NL80211_ATTR_RECEIVE_MULTICAST] = { .type = NLA_FLAG },
769 	[NL80211_ATTR_WIPHY_FREQ_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
770 	[NL80211_ATTR_SCAN_FREQ_KHZ] = { .type = NLA_NESTED },
771 	[NL80211_ATTR_HE_6GHZ_CAPABILITY] =
772 		NLA_POLICY_EXACT_LEN(sizeof(struct ieee80211_he_6ghz_capa)),
773 	[NL80211_ATTR_FILS_DISCOVERY] =
774 		NLA_POLICY_NESTED(nl80211_fils_discovery_policy),
775 	[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP] =
776 		NLA_POLICY_NESTED(nl80211_unsol_bcast_probe_resp_policy),
777 	[NL80211_ATTR_S1G_CAPABILITY] =
778 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
779 	[NL80211_ATTR_S1G_CAPABILITY_MASK] =
780 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
781 	[NL80211_ATTR_SAE_PWE] =
782 		NLA_POLICY_RANGE(NLA_U8, NL80211_SAE_PWE_HUNT_AND_PECK,
783 				 NL80211_SAE_PWE_BOTH),
784 	[NL80211_ATTR_RECONNECT_REQUESTED] = { .type = NLA_REJECT },
785 	[NL80211_ATTR_SAR_SPEC] = NLA_POLICY_NESTED(sar_policy),
786 	[NL80211_ATTR_DISABLE_HE] = { .type = NLA_FLAG },
787 	[NL80211_ATTR_OBSS_COLOR_BITMAP] = { .type = NLA_U64 },
788 	[NL80211_ATTR_COLOR_CHANGE_COUNT] = { .type = NLA_U8 },
789 	[NL80211_ATTR_COLOR_CHANGE_COLOR] = { .type = NLA_U8 },
790 	[NL80211_ATTR_COLOR_CHANGE_ELEMS] = NLA_POLICY_NESTED(nl80211_policy),
791 	[NL80211_ATTR_MBSSID_CONFIG] =
792 			NLA_POLICY_NESTED(nl80211_mbssid_config_policy),
793 	[NL80211_ATTR_MBSSID_ELEMS] = { .type = NLA_NESTED },
794 	[NL80211_ATTR_RADAR_BACKGROUND] = { .type = NLA_FLAG },
795 	[NL80211_ATTR_AP_SETTINGS_FLAGS] = { .type = NLA_U32 },
796 	[NL80211_ATTR_EHT_CAPABILITY] =
797 		NLA_POLICY_RANGE(NLA_BINARY,
798 				 NL80211_EHT_MIN_CAPABILITY_LEN,
799 				 NL80211_EHT_MAX_CAPABILITY_LEN),
800 	[NL80211_ATTR_DISABLE_EHT] = { .type = NLA_FLAG },
801 	[NL80211_ATTR_MLO_LINKS] =
802 		NLA_POLICY_NESTED_ARRAY(nl80211_policy),
803 	[NL80211_ATTR_MLO_LINK_ID] =
804 		NLA_POLICY_RANGE(NLA_U8, 0, IEEE80211_MLD_MAX_NUM_LINKS),
805 	[NL80211_ATTR_MLD_ADDR] = NLA_POLICY_EXACT_LEN(ETH_ALEN),
806 	[NL80211_ATTR_MLO_SUPPORT] = { .type = NLA_FLAG },
807 	[NL80211_ATTR_MAX_NUM_AKM_SUITES] = { .type = NLA_REJECT },
808 };
809 
810 /* policy for the key attributes */
811 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
812 	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
813 	[NL80211_KEY_IDX] = { .type = NLA_U8 },
814 	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
815 	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
816 	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
817 	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
818 	[NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
819 	[NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
820 	[NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
821 };
822 
823 /* policy for the key default flags */
824 static const struct nla_policy
825 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
826 	[NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
827 	[NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
828 };
829 
830 #ifdef CONFIG_PM
831 /* policy for WoWLAN attributes */
832 static const struct nla_policy
833 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
834 	[NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
835 	[NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
836 	[NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
837 	[NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
838 	[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
839 	[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
840 	[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
841 	[NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
842 	[NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
843 	[NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
844 };
845 
846 static const struct nla_policy
847 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
848 	[NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
849 	[NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
850 	[NL80211_WOWLAN_TCP_DST_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
851 	[NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
852 	[NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
853 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
854 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
855 		.len = sizeof(struct nl80211_wowlan_tcp_data_seq)
856 	},
857 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
858 		.len = sizeof(struct nl80211_wowlan_tcp_data_token)
859 	},
860 	[NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
861 	[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
862 	[NL80211_WOWLAN_TCP_WAKE_MASK] = NLA_POLICY_MIN_LEN(1),
863 };
864 #endif /* CONFIG_PM */
865 
866 /* policy for coalesce rule attributes */
867 static const struct nla_policy
868 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
869 	[NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
870 	[NL80211_ATTR_COALESCE_RULE_CONDITION] =
871 		NLA_POLICY_RANGE(NLA_U32,
872 				 NL80211_COALESCE_CONDITION_MATCH,
873 				 NL80211_COALESCE_CONDITION_NO_MATCH),
874 	[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
875 };
876 
877 /* policy for GTK rekey offload attributes */
878 static const struct nla_policy
879 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
880 	[NL80211_REKEY_DATA_KEK] = {
881 		.type = NLA_BINARY,
882 		.len = NL80211_KEK_EXT_LEN
883 	},
884 	[NL80211_REKEY_DATA_KCK] = {
885 		.type = NLA_BINARY,
886 		.len = NL80211_KCK_EXT_LEN
887 	},
888 	[NL80211_REKEY_DATA_REPLAY_CTR] = NLA_POLICY_EXACT_LEN(NL80211_REPLAY_CTR_LEN),
889 	[NL80211_REKEY_DATA_AKM] = { .type = NLA_U32 },
890 };
891 
892 static const struct nla_policy
893 nl80211_match_band_rssi_policy[NUM_NL80211_BANDS] = {
894 	[NL80211_BAND_2GHZ] = { .type = NLA_S32 },
895 	[NL80211_BAND_5GHZ] = { .type = NLA_S32 },
896 	[NL80211_BAND_6GHZ] = { .type = NLA_S32 },
897 	[NL80211_BAND_60GHZ] = { .type = NLA_S32 },
898 	[NL80211_BAND_LC]    = { .type = NLA_S32 },
899 };
900 
901 static const struct nla_policy
902 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
903 	[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
904 						 .len = IEEE80211_MAX_SSID_LEN },
905 	[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
906 	[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
907 	[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI] =
908 		NLA_POLICY_NESTED(nl80211_match_band_rssi_policy),
909 };
910 
911 static const struct nla_policy
912 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
913 	[NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
914 	[NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
915 };
916 
917 static const struct nla_policy
918 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
919 	[NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
920 	[NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
921 	[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
922 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
923 	},
924 };
925 
926 /* policy for NAN function attributes */
927 static const struct nla_policy
928 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
929 	[NL80211_NAN_FUNC_TYPE] =
930 		NLA_POLICY_MAX(NLA_U8, NL80211_NAN_FUNC_MAX_TYPE),
931 	[NL80211_NAN_FUNC_SERVICE_ID] = {
932 				    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
933 	[NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
934 	[NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
935 	[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
936 	[NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
937 	[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
938 	[NL80211_NAN_FUNC_FOLLOW_UP_DEST] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
939 	[NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
940 	[NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
941 	[NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
942 			.len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
943 	[NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
944 	[NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
945 	[NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
946 	[NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
947 	[NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
948 };
949 
950 /* policy for Service Response Filter attributes */
951 static const struct nla_policy
952 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
953 	[NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
954 	[NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
955 				 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
956 	[NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
957 	[NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
958 };
959 
960 /* policy for packet pattern attributes */
961 static const struct nla_policy
962 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
963 	[NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
964 	[NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
965 	[NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
966 };
967 
968 static int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
969 				     struct cfg80211_registered_device **rdev,
970 				     struct wireless_dev **wdev,
971 				     struct nlattr **attrbuf)
972 {
973 	int err;
974 
975 	if (!cb->args[0]) {
976 		struct nlattr **attrbuf_free = NULL;
977 
978 		if (!attrbuf) {
979 			attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
980 					  GFP_KERNEL);
981 			if (!attrbuf)
982 				return -ENOMEM;
983 			attrbuf_free = attrbuf;
984 		}
985 
986 		err = nlmsg_parse_deprecated(cb->nlh,
987 					     GENL_HDRLEN + nl80211_fam.hdrsize,
988 					     attrbuf, nl80211_fam.maxattr,
989 					     nl80211_policy, NULL);
990 		if (err) {
991 			kfree(attrbuf_free);
992 			return err;
993 		}
994 
995 		rtnl_lock();
996 		*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(cb->skb->sk),
997 						   attrbuf);
998 		kfree(attrbuf_free);
999 		if (IS_ERR(*wdev)) {
1000 			rtnl_unlock();
1001 			return PTR_ERR(*wdev);
1002 		}
1003 		*rdev = wiphy_to_rdev((*wdev)->wiphy);
1004 		mutex_lock(&(*rdev)->wiphy.mtx);
1005 		rtnl_unlock();
1006 		/* 0 is the first index - add 1 to parse only once */
1007 		cb->args[0] = (*rdev)->wiphy_idx + 1;
1008 		cb->args[1] = (*wdev)->identifier;
1009 	} else {
1010 		/* subtract the 1 again here */
1011 		struct wiphy *wiphy;
1012 		struct wireless_dev *tmp;
1013 
1014 		rtnl_lock();
1015 		wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
1016 		if (!wiphy) {
1017 			rtnl_unlock();
1018 			return -ENODEV;
1019 		}
1020 		*rdev = wiphy_to_rdev(wiphy);
1021 		*wdev = NULL;
1022 
1023 		list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
1024 			if (tmp->identifier == cb->args[1]) {
1025 				*wdev = tmp;
1026 				break;
1027 			}
1028 		}
1029 
1030 		if (!*wdev) {
1031 			rtnl_unlock();
1032 			return -ENODEV;
1033 		}
1034 		mutex_lock(&(*rdev)->wiphy.mtx);
1035 		rtnl_unlock();
1036 	}
1037 
1038 	return 0;
1039 }
1040 
1041 /* message building helper */
1042 void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1043 		     int flags, u8 cmd)
1044 {
1045 	/* since there is no private header just add the generic one */
1046 	return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
1047 }
1048 
1049 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
1050 				     const struct ieee80211_reg_rule *rule)
1051 {
1052 	int j;
1053 	struct nlattr *nl_wmm_rules =
1054 		nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
1055 
1056 	if (!nl_wmm_rules)
1057 		goto nla_put_failure;
1058 
1059 	for (j = 0; j < IEEE80211_NUM_ACS; j++) {
1060 		struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
1061 
1062 		if (!nl_wmm_rule)
1063 			goto nla_put_failure;
1064 
1065 		if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
1066 				rule->wmm_rule.client[j].cw_min) ||
1067 		    nla_put_u16(msg, NL80211_WMMR_CW_MAX,
1068 				rule->wmm_rule.client[j].cw_max) ||
1069 		    nla_put_u8(msg, NL80211_WMMR_AIFSN,
1070 			       rule->wmm_rule.client[j].aifsn) ||
1071 		    nla_put_u16(msg, NL80211_WMMR_TXOP,
1072 			        rule->wmm_rule.client[j].cot))
1073 			goto nla_put_failure;
1074 
1075 		nla_nest_end(msg, nl_wmm_rule);
1076 	}
1077 	nla_nest_end(msg, nl_wmm_rules);
1078 
1079 	return 0;
1080 
1081 nla_put_failure:
1082 	return -ENOBUFS;
1083 }
1084 
1085 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
1086 				   struct ieee80211_channel *chan,
1087 				   bool large)
1088 {
1089 	/* Some channels must be completely excluded from the
1090 	 * list to protect old user-space tools from breaking
1091 	 */
1092 	if (!large && chan->flags &
1093 	    (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
1094 		return 0;
1095 	if (!large && chan->freq_offset)
1096 		return 0;
1097 
1098 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
1099 			chan->center_freq))
1100 		goto nla_put_failure;
1101 
1102 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_OFFSET, chan->freq_offset))
1103 		goto nla_put_failure;
1104 
1105 	if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
1106 	    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
1107 		goto nla_put_failure;
1108 	if (chan->flags & IEEE80211_CHAN_NO_IR) {
1109 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
1110 			goto nla_put_failure;
1111 		if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
1112 			goto nla_put_failure;
1113 	}
1114 	if (chan->flags & IEEE80211_CHAN_RADAR) {
1115 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
1116 			goto nla_put_failure;
1117 		if (large) {
1118 			u32 time;
1119 
1120 			time = elapsed_jiffies_msecs(chan->dfs_state_entered);
1121 
1122 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
1123 					chan->dfs_state))
1124 				goto nla_put_failure;
1125 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
1126 					time))
1127 				goto nla_put_failure;
1128 			if (nla_put_u32(msg,
1129 					NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
1130 					chan->dfs_cac_ms))
1131 				goto nla_put_failure;
1132 		}
1133 	}
1134 
1135 	if (large) {
1136 		if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
1137 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
1138 			goto nla_put_failure;
1139 		if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
1140 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
1141 			goto nla_put_failure;
1142 		if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
1143 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
1144 			goto nla_put_failure;
1145 		if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
1146 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
1147 			goto nla_put_failure;
1148 		if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
1149 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
1150 			goto nla_put_failure;
1151 		if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
1152 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
1153 			goto nla_put_failure;
1154 		if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
1155 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
1156 			goto nla_put_failure;
1157 		if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
1158 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
1159 			goto nla_put_failure;
1160 		if ((chan->flags & IEEE80211_CHAN_NO_HE) &&
1161 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HE))
1162 			goto nla_put_failure;
1163 		if ((chan->flags & IEEE80211_CHAN_1MHZ) &&
1164 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_1MHZ))
1165 			goto nla_put_failure;
1166 		if ((chan->flags & IEEE80211_CHAN_2MHZ) &&
1167 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_2MHZ))
1168 			goto nla_put_failure;
1169 		if ((chan->flags & IEEE80211_CHAN_4MHZ) &&
1170 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_4MHZ))
1171 			goto nla_put_failure;
1172 		if ((chan->flags & IEEE80211_CHAN_8MHZ) &&
1173 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_8MHZ))
1174 			goto nla_put_failure;
1175 		if ((chan->flags & IEEE80211_CHAN_16MHZ) &&
1176 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_16MHZ))
1177 			goto nla_put_failure;
1178 		if ((chan->flags & IEEE80211_CHAN_NO_320MHZ) &&
1179 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_320MHZ))
1180 			goto nla_put_failure;
1181 		if ((chan->flags & IEEE80211_CHAN_NO_EHT) &&
1182 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_EHT))
1183 			goto nla_put_failure;
1184 	}
1185 
1186 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
1187 			DBM_TO_MBM(chan->max_power)))
1188 		goto nla_put_failure;
1189 
1190 	if (large) {
1191 		const struct ieee80211_reg_rule *rule =
1192 			freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1193 
1194 		if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
1195 			if (nl80211_msg_put_wmm_rules(msg, rule))
1196 				goto nla_put_failure;
1197 		}
1198 	}
1199 
1200 	return 0;
1201 
1202  nla_put_failure:
1203 	return -ENOBUFS;
1204 }
1205 
1206 static bool nl80211_put_txq_stats(struct sk_buff *msg,
1207 				  struct cfg80211_txq_stats *txqstats,
1208 				  int attrtype)
1209 {
1210 	struct nlattr *txqattr;
1211 
1212 #define PUT_TXQVAL_U32(attr, memb) do {					  \
1213 	if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&	  \
1214 	    nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
1215 		return false;						  \
1216 	} while (0)
1217 
1218 	txqattr = nla_nest_start_noflag(msg, attrtype);
1219 	if (!txqattr)
1220 		return false;
1221 
1222 	PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
1223 	PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
1224 	PUT_TXQVAL_U32(FLOWS, flows);
1225 	PUT_TXQVAL_U32(DROPS, drops);
1226 	PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
1227 	PUT_TXQVAL_U32(OVERLIMIT, overlimit);
1228 	PUT_TXQVAL_U32(OVERMEMORY, overmemory);
1229 	PUT_TXQVAL_U32(COLLISIONS, collisions);
1230 	PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
1231 	PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
1232 	PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
1233 	nla_nest_end(msg, txqattr);
1234 
1235 #undef PUT_TXQVAL_U32
1236 	return true;
1237 }
1238 
1239 /* netlink command implementations */
1240 
1241 /**
1242  * nl80211_link_id - return link ID
1243  * @attrs: attributes to look at
1244  *
1245  * Returns: the link ID or 0 if not given
1246  *
1247  * Note this function doesn't do any validation of the link
1248  * ID validity wrt. links that were actually added, so it must
1249  * be called only from ops with %NL80211_FLAG_MLO_VALID_LINK_ID
1250  * or if additional validation is done.
1251  */
1252 static unsigned int nl80211_link_id(struct nlattr **attrs)
1253 {
1254 	struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1255 
1256 	if (!linkid)
1257 		return 0;
1258 
1259 	return nla_get_u8(linkid);
1260 }
1261 
1262 static int nl80211_link_id_or_invalid(struct nlattr **attrs)
1263 {
1264 	struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1265 
1266 	if (!linkid)
1267 		return -1;
1268 
1269 	return nla_get_u8(linkid);
1270 }
1271 
1272 struct key_parse {
1273 	struct key_params p;
1274 	int idx;
1275 	int type;
1276 	bool def, defmgmt, defbeacon;
1277 	bool def_uni, def_multi;
1278 };
1279 
1280 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
1281 				 struct key_parse *k)
1282 {
1283 	struct nlattr *tb[NL80211_KEY_MAX + 1];
1284 	int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
1285 					      nl80211_key_policy,
1286 					      info->extack);
1287 	if (err)
1288 		return err;
1289 
1290 	k->def = !!tb[NL80211_KEY_DEFAULT];
1291 	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1292 	k->defbeacon = !!tb[NL80211_KEY_DEFAULT_BEACON];
1293 
1294 	if (k->def) {
1295 		k->def_uni = true;
1296 		k->def_multi = true;
1297 	}
1298 	if (k->defmgmt || k->defbeacon)
1299 		k->def_multi = true;
1300 
1301 	if (tb[NL80211_KEY_IDX])
1302 		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1303 
1304 	if (tb[NL80211_KEY_DATA]) {
1305 		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1306 		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1307 	}
1308 
1309 	if (tb[NL80211_KEY_SEQ]) {
1310 		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1311 		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1312 	}
1313 
1314 	if (tb[NL80211_KEY_CIPHER])
1315 		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1316 
1317 	if (tb[NL80211_KEY_TYPE])
1318 		k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1319 
1320 	if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1321 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1322 
1323 		err = nla_parse_nested_deprecated(kdt,
1324 						  NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1325 						  tb[NL80211_KEY_DEFAULT_TYPES],
1326 						  nl80211_key_default_policy,
1327 						  info->extack);
1328 		if (err)
1329 			return err;
1330 
1331 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1332 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1333 	}
1334 
1335 	if (tb[NL80211_KEY_MODE])
1336 		k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1337 
1338 	return 0;
1339 }
1340 
1341 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1342 {
1343 	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1344 		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1345 		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1346 	}
1347 
1348 	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1349 		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1350 		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1351 	}
1352 
1353 	if (info->attrs[NL80211_ATTR_KEY_IDX])
1354 		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1355 
1356 	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1357 		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1358 
1359 	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1360 	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1361 
1362 	if (k->def) {
1363 		k->def_uni = true;
1364 		k->def_multi = true;
1365 	}
1366 	if (k->defmgmt)
1367 		k->def_multi = true;
1368 
1369 	if (info->attrs[NL80211_ATTR_KEY_TYPE])
1370 		k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1371 
1372 	if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1373 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1374 		int err = nla_parse_nested_deprecated(kdt,
1375 						      NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1376 						      info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1377 						      nl80211_key_default_policy,
1378 						      info->extack);
1379 		if (err)
1380 			return err;
1381 
1382 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1383 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1384 	}
1385 
1386 	return 0;
1387 }
1388 
1389 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1390 {
1391 	int err;
1392 
1393 	memset(k, 0, sizeof(*k));
1394 	k->idx = -1;
1395 	k->type = -1;
1396 
1397 	if (info->attrs[NL80211_ATTR_KEY])
1398 		err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1399 	else
1400 		err = nl80211_parse_key_old(info, k);
1401 
1402 	if (err)
1403 		return err;
1404 
1405 	if ((k->def ? 1 : 0) + (k->defmgmt ? 1 : 0) +
1406 	    (k->defbeacon ? 1 : 0) > 1) {
1407 		GENL_SET_ERR_MSG(info,
1408 				 "key with multiple default flags is invalid");
1409 		return -EINVAL;
1410 	}
1411 
1412 	if (k->defmgmt || k->defbeacon) {
1413 		if (k->def_uni || !k->def_multi) {
1414 			GENL_SET_ERR_MSG(info,
1415 					 "defmgmt/defbeacon key must be mcast");
1416 			return -EINVAL;
1417 		}
1418 	}
1419 
1420 	if (k->idx != -1) {
1421 		if (k->defmgmt) {
1422 			if (k->idx < 4 || k->idx > 5) {
1423 				GENL_SET_ERR_MSG(info,
1424 						 "defmgmt key idx not 4 or 5");
1425 				return -EINVAL;
1426 			}
1427 		} else if (k->defbeacon) {
1428 			if (k->idx < 6 || k->idx > 7) {
1429 				GENL_SET_ERR_MSG(info,
1430 						 "defbeacon key idx not 6 or 7");
1431 				return -EINVAL;
1432 			}
1433 		} else if (k->def) {
1434 			if (k->idx < 0 || k->idx > 3) {
1435 				GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1436 				return -EINVAL;
1437 			}
1438 		} else {
1439 			if (k->idx < 0 || k->idx > 7) {
1440 				GENL_SET_ERR_MSG(info, "key idx not 0-7");
1441 				return -EINVAL;
1442 			}
1443 		}
1444 	}
1445 
1446 	return 0;
1447 }
1448 
1449 static struct cfg80211_cached_keys *
1450 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1451 		       struct genl_info *info, bool *no_ht)
1452 {
1453 	struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1454 	struct key_parse parse;
1455 	struct nlattr *key;
1456 	struct cfg80211_cached_keys *result;
1457 	int rem, err, def = 0;
1458 	bool have_key = false;
1459 
1460 	nla_for_each_nested(key, keys, rem) {
1461 		have_key = true;
1462 		break;
1463 	}
1464 
1465 	if (!have_key)
1466 		return NULL;
1467 
1468 	result = kzalloc(sizeof(*result), GFP_KERNEL);
1469 	if (!result)
1470 		return ERR_PTR(-ENOMEM);
1471 
1472 	result->def = -1;
1473 
1474 	nla_for_each_nested(key, keys, rem) {
1475 		memset(&parse, 0, sizeof(parse));
1476 		parse.idx = -1;
1477 
1478 		err = nl80211_parse_key_new(info, key, &parse);
1479 		if (err)
1480 			goto error;
1481 		err = -EINVAL;
1482 		if (!parse.p.key)
1483 			goto error;
1484 		if (parse.idx < 0 || parse.idx > 3) {
1485 			GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1486 			goto error;
1487 		}
1488 		if (parse.def) {
1489 			if (def) {
1490 				GENL_SET_ERR_MSG(info,
1491 						 "only one key can be default");
1492 				goto error;
1493 			}
1494 			def = 1;
1495 			result->def = parse.idx;
1496 			if (!parse.def_uni || !parse.def_multi)
1497 				goto error;
1498 		} else if (parse.defmgmt)
1499 			goto error;
1500 		err = cfg80211_validate_key_settings(rdev, &parse.p,
1501 						     parse.idx, false, NULL);
1502 		if (err)
1503 			goto error;
1504 		if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1505 		    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1506 			GENL_SET_ERR_MSG(info, "connect key must be WEP");
1507 			err = -EINVAL;
1508 			goto error;
1509 		}
1510 		result->params[parse.idx].cipher = parse.p.cipher;
1511 		result->params[parse.idx].key_len = parse.p.key_len;
1512 		result->params[parse.idx].key = result->data[parse.idx];
1513 		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1514 
1515 		/* must be WEP key if we got here */
1516 		if (no_ht)
1517 			*no_ht = true;
1518 	}
1519 
1520 	if (result->def < 0) {
1521 		err = -EINVAL;
1522 		GENL_SET_ERR_MSG(info, "need a default/TX key");
1523 		goto error;
1524 	}
1525 
1526 	return result;
1527  error:
1528 	kfree(result);
1529 	return ERR_PTR(err);
1530 }
1531 
1532 static int nl80211_key_allowed(struct wireless_dev *wdev)
1533 {
1534 	ASSERT_WDEV_LOCK(wdev);
1535 
1536 	switch (wdev->iftype) {
1537 	case NL80211_IFTYPE_AP:
1538 	case NL80211_IFTYPE_AP_VLAN:
1539 	case NL80211_IFTYPE_P2P_GO:
1540 	case NL80211_IFTYPE_MESH_POINT:
1541 		break;
1542 	case NL80211_IFTYPE_ADHOC:
1543 		if (wdev->u.ibss.current_bss)
1544 			return 0;
1545 		return -ENOLINK;
1546 	case NL80211_IFTYPE_STATION:
1547 	case NL80211_IFTYPE_P2P_CLIENT:
1548 		if (wdev->connected)
1549 			return 0;
1550 		return -ENOLINK;
1551 	case NL80211_IFTYPE_UNSPECIFIED:
1552 	case NL80211_IFTYPE_OCB:
1553 	case NL80211_IFTYPE_MONITOR:
1554 	case NL80211_IFTYPE_NAN:
1555 	case NL80211_IFTYPE_P2P_DEVICE:
1556 	case NL80211_IFTYPE_WDS:
1557 	case NUM_NL80211_IFTYPES:
1558 		return -EINVAL;
1559 	}
1560 
1561 	return 0;
1562 }
1563 
1564 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1565 							u32 freq)
1566 {
1567 	struct ieee80211_channel *chan;
1568 
1569 	chan = ieee80211_get_channel_khz(wiphy, freq);
1570 	if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1571 		return NULL;
1572 	return chan;
1573 }
1574 
1575 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1576 {
1577 	struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1578 	int i;
1579 
1580 	if (!nl_modes)
1581 		goto nla_put_failure;
1582 
1583 	i = 0;
1584 	while (ifmodes) {
1585 		if ((ifmodes & 1) && nla_put_flag(msg, i))
1586 			goto nla_put_failure;
1587 		ifmodes >>= 1;
1588 		i++;
1589 	}
1590 
1591 	nla_nest_end(msg, nl_modes);
1592 	return 0;
1593 
1594 nla_put_failure:
1595 	return -ENOBUFS;
1596 }
1597 
1598 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1599 					  struct sk_buff *msg,
1600 					  bool large)
1601 {
1602 	struct nlattr *nl_combis;
1603 	int i, j;
1604 
1605 	nl_combis = nla_nest_start_noflag(msg,
1606 					  NL80211_ATTR_INTERFACE_COMBINATIONS);
1607 	if (!nl_combis)
1608 		goto nla_put_failure;
1609 
1610 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1611 		const struct ieee80211_iface_combination *c;
1612 		struct nlattr *nl_combi, *nl_limits;
1613 
1614 		c = &wiphy->iface_combinations[i];
1615 
1616 		nl_combi = nla_nest_start_noflag(msg, i + 1);
1617 		if (!nl_combi)
1618 			goto nla_put_failure;
1619 
1620 		nl_limits = nla_nest_start_noflag(msg,
1621 						  NL80211_IFACE_COMB_LIMITS);
1622 		if (!nl_limits)
1623 			goto nla_put_failure;
1624 
1625 		for (j = 0; j < c->n_limits; j++) {
1626 			struct nlattr *nl_limit;
1627 
1628 			nl_limit = nla_nest_start_noflag(msg, j + 1);
1629 			if (!nl_limit)
1630 				goto nla_put_failure;
1631 			if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1632 					c->limits[j].max))
1633 				goto nla_put_failure;
1634 			if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1635 						c->limits[j].types))
1636 				goto nla_put_failure;
1637 			nla_nest_end(msg, nl_limit);
1638 		}
1639 
1640 		nla_nest_end(msg, nl_limits);
1641 
1642 		if (c->beacon_int_infra_match &&
1643 		    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1644 			goto nla_put_failure;
1645 		if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1646 				c->num_different_channels) ||
1647 		    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1648 				c->max_interfaces))
1649 			goto nla_put_failure;
1650 		if (large &&
1651 		    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1652 				c->radar_detect_widths) ||
1653 		     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1654 				c->radar_detect_regions)))
1655 			goto nla_put_failure;
1656 		if (c->beacon_int_min_gcd &&
1657 		    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1658 				c->beacon_int_min_gcd))
1659 			goto nla_put_failure;
1660 
1661 		nla_nest_end(msg, nl_combi);
1662 	}
1663 
1664 	nla_nest_end(msg, nl_combis);
1665 
1666 	return 0;
1667 nla_put_failure:
1668 	return -ENOBUFS;
1669 }
1670 
1671 #ifdef CONFIG_PM
1672 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1673 					struct sk_buff *msg)
1674 {
1675 	const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1676 	struct nlattr *nl_tcp;
1677 
1678 	if (!tcp)
1679 		return 0;
1680 
1681 	nl_tcp = nla_nest_start_noflag(msg,
1682 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1683 	if (!nl_tcp)
1684 		return -ENOBUFS;
1685 
1686 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1687 			tcp->data_payload_max))
1688 		return -ENOBUFS;
1689 
1690 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1691 			tcp->data_payload_max))
1692 		return -ENOBUFS;
1693 
1694 	if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1695 		return -ENOBUFS;
1696 
1697 	if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1698 				sizeof(*tcp->tok), tcp->tok))
1699 		return -ENOBUFS;
1700 
1701 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1702 			tcp->data_interval_max))
1703 		return -ENOBUFS;
1704 
1705 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1706 			tcp->wake_payload_max))
1707 		return -ENOBUFS;
1708 
1709 	nla_nest_end(msg, nl_tcp);
1710 	return 0;
1711 }
1712 
1713 static int nl80211_send_wowlan(struct sk_buff *msg,
1714 			       struct cfg80211_registered_device *rdev,
1715 			       bool large)
1716 {
1717 	struct nlattr *nl_wowlan;
1718 
1719 	if (!rdev->wiphy.wowlan)
1720 		return 0;
1721 
1722 	nl_wowlan = nla_nest_start_noflag(msg,
1723 					  NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1724 	if (!nl_wowlan)
1725 		return -ENOBUFS;
1726 
1727 	if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1728 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1729 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1730 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1731 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1732 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1733 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1734 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1735 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1736 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1737 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1738 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1739 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1740 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1741 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1742 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1743 		return -ENOBUFS;
1744 
1745 	if (rdev->wiphy.wowlan->n_patterns) {
1746 		struct nl80211_pattern_support pat = {
1747 			.max_patterns = rdev->wiphy.wowlan->n_patterns,
1748 			.min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1749 			.max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1750 			.max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1751 		};
1752 
1753 		if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1754 			    sizeof(pat), &pat))
1755 			return -ENOBUFS;
1756 	}
1757 
1758 	if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1759 	    nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1760 			rdev->wiphy.wowlan->max_nd_match_sets))
1761 		return -ENOBUFS;
1762 
1763 	if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1764 		return -ENOBUFS;
1765 
1766 	nla_nest_end(msg, nl_wowlan);
1767 
1768 	return 0;
1769 }
1770 #endif
1771 
1772 static int nl80211_send_coalesce(struct sk_buff *msg,
1773 				 struct cfg80211_registered_device *rdev)
1774 {
1775 	struct nl80211_coalesce_rule_support rule;
1776 
1777 	if (!rdev->wiphy.coalesce)
1778 		return 0;
1779 
1780 	rule.max_rules = rdev->wiphy.coalesce->n_rules;
1781 	rule.max_delay = rdev->wiphy.coalesce->max_delay;
1782 	rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1783 	rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1784 	rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1785 	rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1786 
1787 	if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1788 		return -ENOBUFS;
1789 
1790 	return 0;
1791 }
1792 
1793 static int
1794 nl80211_send_iftype_data(struct sk_buff *msg,
1795 			 const struct ieee80211_supported_band *sband,
1796 			 const struct ieee80211_sband_iftype_data *iftdata)
1797 {
1798 	const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1799 	const struct ieee80211_sta_eht_cap *eht_cap = &iftdata->eht_cap;
1800 
1801 	if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1802 				iftdata->types_mask))
1803 		return -ENOBUFS;
1804 
1805 	if (he_cap->has_he) {
1806 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1807 			    sizeof(he_cap->he_cap_elem.mac_cap_info),
1808 			    he_cap->he_cap_elem.mac_cap_info) ||
1809 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1810 			    sizeof(he_cap->he_cap_elem.phy_cap_info),
1811 			    he_cap->he_cap_elem.phy_cap_info) ||
1812 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1813 			    sizeof(he_cap->he_mcs_nss_supp),
1814 			    &he_cap->he_mcs_nss_supp) ||
1815 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1816 			    sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1817 			return -ENOBUFS;
1818 	}
1819 
1820 	if (eht_cap->has_eht && he_cap->has_he) {
1821 		u8 mcs_nss_size, ppe_thresh_size;
1822 		u16 ppe_thres_hdr;
1823 		bool is_ap;
1824 
1825 		is_ap = iftdata->types_mask & BIT(NL80211_IFTYPE_AP) ||
1826 			iftdata->types_mask & BIT(NL80211_IFTYPE_P2P_GO);
1827 
1828 		mcs_nss_size =
1829 			ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
1830 						   &eht_cap->eht_cap_elem,
1831 						   is_ap);
1832 
1833 		ppe_thres_hdr = get_unaligned_le16(&eht_cap->eht_ppe_thres[0]);
1834 		ppe_thresh_size =
1835 			ieee80211_eht_ppe_size(ppe_thres_hdr,
1836 					       eht_cap->eht_cap_elem.phy_cap_info);
1837 
1838 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MAC,
1839 			    sizeof(eht_cap->eht_cap_elem.mac_cap_info),
1840 			    eht_cap->eht_cap_elem.mac_cap_info) ||
1841 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY,
1842 			    sizeof(eht_cap->eht_cap_elem.phy_cap_info),
1843 			    eht_cap->eht_cap_elem.phy_cap_info) ||
1844 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MCS_SET,
1845 			    mcs_nss_size, &eht_cap->eht_mcs_nss_supp) ||
1846 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PPE,
1847 			    ppe_thresh_size, eht_cap->eht_ppe_thres))
1848 			return -ENOBUFS;
1849 	}
1850 
1851 	if (sband->band == NL80211_BAND_6GHZ &&
1852 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_6GHZ_CAPA,
1853 		    sizeof(iftdata->he_6ghz_capa),
1854 		    &iftdata->he_6ghz_capa))
1855 		return -ENOBUFS;
1856 
1857 	if (iftdata->vendor_elems.data && iftdata->vendor_elems.len &&
1858 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_VENDOR_ELEMS,
1859 		    iftdata->vendor_elems.len, iftdata->vendor_elems.data))
1860 		return -ENOBUFS;
1861 
1862 	return 0;
1863 }
1864 
1865 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1866 				      struct ieee80211_supported_band *sband,
1867 				      bool large)
1868 {
1869 	struct nlattr *nl_rates, *nl_rate;
1870 	struct ieee80211_rate *rate;
1871 	int i;
1872 
1873 	/* add HT info */
1874 	if (sband->ht_cap.ht_supported &&
1875 	    (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1876 		     sizeof(sband->ht_cap.mcs),
1877 		     &sband->ht_cap.mcs) ||
1878 	     nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1879 			 sband->ht_cap.cap) ||
1880 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1881 			sband->ht_cap.ampdu_factor) ||
1882 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1883 			sband->ht_cap.ampdu_density)))
1884 		return -ENOBUFS;
1885 
1886 	/* add VHT info */
1887 	if (sband->vht_cap.vht_supported &&
1888 	    (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1889 		     sizeof(sband->vht_cap.vht_mcs),
1890 		     &sband->vht_cap.vht_mcs) ||
1891 	     nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1892 			 sband->vht_cap.cap)))
1893 		return -ENOBUFS;
1894 
1895 	if (large && sband->n_iftype_data) {
1896 		struct nlattr *nl_iftype_data =
1897 			nla_nest_start_noflag(msg,
1898 					      NL80211_BAND_ATTR_IFTYPE_DATA);
1899 		int err;
1900 
1901 		if (!nl_iftype_data)
1902 			return -ENOBUFS;
1903 
1904 		for (i = 0; i < sband->n_iftype_data; i++) {
1905 			struct nlattr *iftdata;
1906 
1907 			iftdata = nla_nest_start_noflag(msg, i + 1);
1908 			if (!iftdata)
1909 				return -ENOBUFS;
1910 
1911 			err = nl80211_send_iftype_data(msg, sband,
1912 						       &sband->iftype_data[i]);
1913 			if (err)
1914 				return err;
1915 
1916 			nla_nest_end(msg, iftdata);
1917 		}
1918 
1919 		nla_nest_end(msg, nl_iftype_data);
1920 	}
1921 
1922 	/* add EDMG info */
1923 	if (large && sband->edmg_cap.channels &&
1924 	    (nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_CHANNELS,
1925 		       sband->edmg_cap.channels) ||
1926 	    nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_BW_CONFIG,
1927 		       sband->edmg_cap.bw_config)))
1928 
1929 		return -ENOBUFS;
1930 
1931 	/* add bitrates */
1932 	nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1933 	if (!nl_rates)
1934 		return -ENOBUFS;
1935 
1936 	for (i = 0; i < sband->n_bitrates; i++) {
1937 		nl_rate = nla_nest_start_noflag(msg, i);
1938 		if (!nl_rate)
1939 			return -ENOBUFS;
1940 
1941 		rate = &sband->bitrates[i];
1942 		if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1943 				rate->bitrate))
1944 			return -ENOBUFS;
1945 		if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1946 		    nla_put_flag(msg,
1947 				 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1948 			return -ENOBUFS;
1949 
1950 		nla_nest_end(msg, nl_rate);
1951 	}
1952 
1953 	nla_nest_end(msg, nl_rates);
1954 
1955 	return 0;
1956 }
1957 
1958 static int
1959 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1960 			 const struct ieee80211_txrx_stypes *mgmt_stypes)
1961 {
1962 	u16 stypes;
1963 	struct nlattr *nl_ftypes, *nl_ifs;
1964 	enum nl80211_iftype ift;
1965 	int i;
1966 
1967 	if (!mgmt_stypes)
1968 		return 0;
1969 
1970 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
1971 	if (!nl_ifs)
1972 		return -ENOBUFS;
1973 
1974 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1975 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1976 		if (!nl_ftypes)
1977 			return -ENOBUFS;
1978 		i = 0;
1979 		stypes = mgmt_stypes[ift].tx;
1980 		while (stypes) {
1981 			if ((stypes & 1) &&
1982 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1983 					(i << 4) | IEEE80211_FTYPE_MGMT))
1984 				return -ENOBUFS;
1985 			stypes >>= 1;
1986 			i++;
1987 		}
1988 		nla_nest_end(msg, nl_ftypes);
1989 	}
1990 
1991 	nla_nest_end(msg, nl_ifs);
1992 
1993 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
1994 	if (!nl_ifs)
1995 		return -ENOBUFS;
1996 
1997 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1998 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1999 		if (!nl_ftypes)
2000 			return -ENOBUFS;
2001 		i = 0;
2002 		stypes = mgmt_stypes[ift].rx;
2003 		while (stypes) {
2004 			if ((stypes & 1) &&
2005 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
2006 					(i << 4) | IEEE80211_FTYPE_MGMT))
2007 				return -ENOBUFS;
2008 			stypes >>= 1;
2009 			i++;
2010 		}
2011 		nla_nest_end(msg, nl_ftypes);
2012 	}
2013 	nla_nest_end(msg, nl_ifs);
2014 
2015 	return 0;
2016 }
2017 
2018 #define CMD(op, n)							\
2019 	 do {								\
2020 		if (rdev->ops->op) {					\
2021 			i++;						\
2022 			if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) 	\
2023 				goto nla_put_failure;			\
2024 		}							\
2025 	} while (0)
2026 
2027 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
2028 					struct sk_buff *msg)
2029 {
2030 	int i = 0;
2031 
2032 	/*
2033 	 * do *NOT* add anything into this function, new things need to be
2034 	 * advertised only to new versions of userspace that can deal with
2035 	 * the split (and they can't possibly care about new features...
2036 	 */
2037 	CMD(add_virtual_intf, NEW_INTERFACE);
2038 	CMD(change_virtual_intf, SET_INTERFACE);
2039 	CMD(add_key, NEW_KEY);
2040 	CMD(start_ap, START_AP);
2041 	CMD(add_station, NEW_STATION);
2042 	CMD(add_mpath, NEW_MPATH);
2043 	CMD(update_mesh_config, SET_MESH_CONFIG);
2044 	CMD(change_bss, SET_BSS);
2045 	CMD(auth, AUTHENTICATE);
2046 	CMD(assoc, ASSOCIATE);
2047 	CMD(deauth, DEAUTHENTICATE);
2048 	CMD(disassoc, DISASSOCIATE);
2049 	CMD(join_ibss, JOIN_IBSS);
2050 	CMD(join_mesh, JOIN_MESH);
2051 	CMD(set_pmksa, SET_PMKSA);
2052 	CMD(del_pmksa, DEL_PMKSA);
2053 	CMD(flush_pmksa, FLUSH_PMKSA);
2054 	if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
2055 		CMD(remain_on_channel, REMAIN_ON_CHANNEL);
2056 	CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
2057 	CMD(mgmt_tx, FRAME);
2058 	CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
2059 	if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
2060 		i++;
2061 		if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
2062 			goto nla_put_failure;
2063 	}
2064 	if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
2065 	    rdev->ops->join_mesh) {
2066 		i++;
2067 		if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
2068 			goto nla_put_failure;
2069 	}
2070 	if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2071 		CMD(tdls_mgmt, TDLS_MGMT);
2072 		CMD(tdls_oper, TDLS_OPER);
2073 	}
2074 	if (rdev->wiphy.max_sched_scan_reqs)
2075 		CMD(sched_scan_start, START_SCHED_SCAN);
2076 	CMD(probe_client, PROBE_CLIENT);
2077 	CMD(set_noack_map, SET_NOACK_MAP);
2078 	if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
2079 		i++;
2080 		if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
2081 			goto nla_put_failure;
2082 	}
2083 	CMD(start_p2p_device, START_P2P_DEVICE);
2084 	CMD(set_mcast_rate, SET_MCAST_RATE);
2085 #ifdef CONFIG_NL80211_TESTMODE
2086 	CMD(testmode_cmd, TESTMODE);
2087 #endif
2088 
2089 	if (rdev->ops->connect || rdev->ops->auth) {
2090 		i++;
2091 		if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
2092 			goto nla_put_failure;
2093 	}
2094 
2095 	if (rdev->ops->disconnect || rdev->ops->deauth) {
2096 		i++;
2097 		if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
2098 			goto nla_put_failure;
2099 	}
2100 
2101 	return i;
2102  nla_put_failure:
2103 	return -ENOBUFS;
2104 }
2105 
2106 static int
2107 nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
2108 			   struct sk_buff *msg)
2109 {
2110 	struct nlattr *ftm;
2111 
2112 	if (!cap->ftm.supported)
2113 		return 0;
2114 
2115 	ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
2116 	if (!ftm)
2117 		return -ENOBUFS;
2118 
2119 	if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
2120 		return -ENOBUFS;
2121 	if (cap->ftm.non_asap &&
2122 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
2123 		return -ENOBUFS;
2124 	if (cap->ftm.request_lci &&
2125 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
2126 		return -ENOBUFS;
2127 	if (cap->ftm.request_civicloc &&
2128 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
2129 		return -ENOBUFS;
2130 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
2131 			cap->ftm.preambles))
2132 		return -ENOBUFS;
2133 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
2134 			cap->ftm.bandwidths))
2135 		return -ENOBUFS;
2136 	if (cap->ftm.max_bursts_exponent >= 0 &&
2137 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
2138 			cap->ftm.max_bursts_exponent))
2139 		return -ENOBUFS;
2140 	if (cap->ftm.max_ftms_per_burst &&
2141 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
2142 			cap->ftm.max_ftms_per_burst))
2143 		return -ENOBUFS;
2144 	if (cap->ftm.trigger_based &&
2145 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_TRIGGER_BASED))
2146 		return -ENOBUFS;
2147 	if (cap->ftm.non_trigger_based &&
2148 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_TRIGGER_BASED))
2149 		return -ENOBUFS;
2150 
2151 	nla_nest_end(msg, ftm);
2152 	return 0;
2153 }
2154 
2155 static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
2156 				  struct sk_buff *msg)
2157 {
2158 	const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
2159 	struct nlattr *pmsr, *caps;
2160 
2161 	if (!cap)
2162 		return 0;
2163 
2164 	/*
2165 	 * we don't need to clean up anything here since the caller
2166 	 * will genlmsg_cancel() if we fail
2167 	 */
2168 
2169 	pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
2170 	if (!pmsr)
2171 		return -ENOBUFS;
2172 
2173 	if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
2174 		return -ENOBUFS;
2175 
2176 	if (cap->report_ap_tsf &&
2177 	    nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
2178 		return -ENOBUFS;
2179 
2180 	if (cap->randomize_mac_addr &&
2181 	    nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
2182 		return -ENOBUFS;
2183 
2184 	caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
2185 	if (!caps)
2186 		return -ENOBUFS;
2187 
2188 	if (nl80211_send_pmsr_ftm_capa(cap, msg))
2189 		return -ENOBUFS;
2190 
2191 	nla_nest_end(msg, caps);
2192 	nla_nest_end(msg, pmsr);
2193 
2194 	return 0;
2195 }
2196 
2197 static int
2198 nl80211_put_iftype_akm_suites(struct cfg80211_registered_device *rdev,
2199 			      struct sk_buff *msg)
2200 {
2201 	int i;
2202 	struct nlattr *nested, *nested_akms;
2203 	const struct wiphy_iftype_akm_suites *iftype_akms;
2204 
2205 	if (!rdev->wiphy.num_iftype_akm_suites ||
2206 	    !rdev->wiphy.iftype_akm_suites)
2207 		return 0;
2208 
2209 	nested = nla_nest_start(msg, NL80211_ATTR_IFTYPE_AKM_SUITES);
2210 	if (!nested)
2211 		return -ENOBUFS;
2212 
2213 	for (i = 0; i < rdev->wiphy.num_iftype_akm_suites; i++) {
2214 		nested_akms = nla_nest_start(msg, i + 1);
2215 		if (!nested_akms)
2216 			return -ENOBUFS;
2217 
2218 		iftype_akms = &rdev->wiphy.iftype_akm_suites[i];
2219 
2220 		if (nl80211_put_iftypes(msg, NL80211_IFTYPE_AKM_ATTR_IFTYPES,
2221 					iftype_akms->iftypes_mask))
2222 			return -ENOBUFS;
2223 
2224 		if (nla_put(msg, NL80211_IFTYPE_AKM_ATTR_SUITES,
2225 			    sizeof(u32) * iftype_akms->n_akm_suites,
2226 			    iftype_akms->akm_suites)) {
2227 			return -ENOBUFS;
2228 		}
2229 		nla_nest_end(msg, nested_akms);
2230 	}
2231 
2232 	nla_nest_end(msg, nested);
2233 
2234 	return 0;
2235 }
2236 
2237 static int
2238 nl80211_put_tid_config_support(struct cfg80211_registered_device *rdev,
2239 			       struct sk_buff *msg)
2240 {
2241 	struct nlattr *supp;
2242 
2243 	if (!rdev->wiphy.tid_config_support.vif &&
2244 	    !rdev->wiphy.tid_config_support.peer)
2245 		return 0;
2246 
2247 	supp = nla_nest_start(msg, NL80211_ATTR_TID_CONFIG);
2248 	if (!supp)
2249 		return -ENOSPC;
2250 
2251 	if (rdev->wiphy.tid_config_support.vif &&
2252 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_VIF_SUPP,
2253 			      rdev->wiphy.tid_config_support.vif,
2254 			      NL80211_TID_CONFIG_ATTR_PAD))
2255 		goto fail;
2256 
2257 	if (rdev->wiphy.tid_config_support.peer &&
2258 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_PEER_SUPP,
2259 			      rdev->wiphy.tid_config_support.peer,
2260 			      NL80211_TID_CONFIG_ATTR_PAD))
2261 		goto fail;
2262 
2263 	/* for now we just use the same value ... makes more sense */
2264 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_SHORT,
2265 		       rdev->wiphy.tid_config_support.max_retry))
2266 		goto fail;
2267 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_LONG,
2268 		       rdev->wiphy.tid_config_support.max_retry))
2269 		goto fail;
2270 
2271 	nla_nest_end(msg, supp);
2272 
2273 	return 0;
2274 fail:
2275 	nla_nest_cancel(msg, supp);
2276 	return -ENOBUFS;
2277 }
2278 
2279 static int
2280 nl80211_put_sar_specs(struct cfg80211_registered_device *rdev,
2281 		      struct sk_buff *msg)
2282 {
2283 	struct nlattr *sar_capa, *specs, *sub_freq_range;
2284 	u8 num_freq_ranges;
2285 	int i;
2286 
2287 	if (!rdev->wiphy.sar_capa)
2288 		return 0;
2289 
2290 	num_freq_ranges = rdev->wiphy.sar_capa->num_freq_ranges;
2291 
2292 	sar_capa = nla_nest_start(msg, NL80211_ATTR_SAR_SPEC);
2293 	if (!sar_capa)
2294 		return -ENOSPC;
2295 
2296 	if (nla_put_u32(msg, NL80211_SAR_ATTR_TYPE, rdev->wiphy.sar_capa->type))
2297 		goto fail;
2298 
2299 	specs = nla_nest_start(msg, NL80211_SAR_ATTR_SPECS);
2300 	if (!specs)
2301 		goto fail;
2302 
2303 	/* report supported freq_ranges */
2304 	for (i = 0; i < num_freq_ranges; i++) {
2305 		sub_freq_range = nla_nest_start(msg, i + 1);
2306 		if (!sub_freq_range)
2307 			goto fail;
2308 
2309 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_START_FREQ,
2310 				rdev->wiphy.sar_capa->freq_ranges[i].start_freq))
2311 			goto fail;
2312 
2313 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_END_FREQ,
2314 				rdev->wiphy.sar_capa->freq_ranges[i].end_freq))
2315 			goto fail;
2316 
2317 		nla_nest_end(msg, sub_freq_range);
2318 	}
2319 
2320 	nla_nest_end(msg, specs);
2321 	nla_nest_end(msg, sar_capa);
2322 
2323 	return 0;
2324 fail:
2325 	nla_nest_cancel(msg, sar_capa);
2326 	return -ENOBUFS;
2327 }
2328 
2329 static int nl80211_put_mbssid_support(struct wiphy *wiphy, struct sk_buff *msg)
2330 {
2331 	struct nlattr *config;
2332 
2333 	if (!wiphy->mbssid_max_interfaces)
2334 		return 0;
2335 
2336 	config = nla_nest_start(msg, NL80211_ATTR_MBSSID_CONFIG);
2337 	if (!config)
2338 		return -ENOBUFS;
2339 
2340 	if (nla_put_u8(msg, NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES,
2341 		       wiphy->mbssid_max_interfaces))
2342 		goto fail;
2343 
2344 	if (wiphy->ema_max_profile_periodicity &&
2345 	    nla_put_u8(msg,
2346 		       NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY,
2347 		       wiphy->ema_max_profile_periodicity))
2348 		goto fail;
2349 
2350 	nla_nest_end(msg, config);
2351 	return 0;
2352 
2353 fail:
2354 	nla_nest_cancel(msg, config);
2355 	return -ENOBUFS;
2356 }
2357 
2358 struct nl80211_dump_wiphy_state {
2359 	s64 filter_wiphy;
2360 	long start;
2361 	long split_start, band_start, chan_start, capa_start;
2362 	bool split;
2363 };
2364 
2365 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
2366 			      enum nl80211_commands cmd,
2367 			      struct sk_buff *msg, u32 portid, u32 seq,
2368 			      int flags, struct nl80211_dump_wiphy_state *state)
2369 {
2370 	void *hdr;
2371 	struct nlattr *nl_bands, *nl_band;
2372 	struct nlattr *nl_freqs, *nl_freq;
2373 	struct nlattr *nl_cmds;
2374 	enum nl80211_band band;
2375 	struct ieee80211_channel *chan;
2376 	int i;
2377 	const struct ieee80211_txrx_stypes *mgmt_stypes =
2378 				rdev->wiphy.mgmt_stypes;
2379 	u32 features;
2380 
2381 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2382 	if (!hdr)
2383 		return -ENOBUFS;
2384 
2385 	if (WARN_ON(!state))
2386 		return -EINVAL;
2387 
2388 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2389 	    nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
2390 			   wiphy_name(&rdev->wiphy)) ||
2391 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
2392 			cfg80211_rdev_list_generation))
2393 		goto nla_put_failure;
2394 
2395 	if (cmd != NL80211_CMD_NEW_WIPHY)
2396 		goto finish;
2397 
2398 	switch (state->split_start) {
2399 	case 0:
2400 		if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
2401 			       rdev->wiphy.retry_short) ||
2402 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
2403 			       rdev->wiphy.retry_long) ||
2404 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
2405 				rdev->wiphy.frag_threshold) ||
2406 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
2407 				rdev->wiphy.rts_threshold) ||
2408 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
2409 			       rdev->wiphy.coverage_class) ||
2410 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
2411 			       rdev->wiphy.max_scan_ssids) ||
2412 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
2413 			       rdev->wiphy.max_sched_scan_ssids) ||
2414 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
2415 				rdev->wiphy.max_scan_ie_len) ||
2416 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
2417 				rdev->wiphy.max_sched_scan_ie_len) ||
2418 		    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
2419 			       rdev->wiphy.max_match_sets))
2420 			goto nla_put_failure;
2421 
2422 		if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
2423 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
2424 			goto nla_put_failure;
2425 		if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
2426 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
2427 			goto nla_put_failure;
2428 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2429 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
2430 			goto nla_put_failure;
2431 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
2432 		    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
2433 			goto nla_put_failure;
2434 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2435 		    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
2436 			goto nla_put_failure;
2437 		if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
2438 		    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
2439 			goto nla_put_failure;
2440 		state->split_start++;
2441 		if (state->split)
2442 			break;
2443 		fallthrough;
2444 	case 1:
2445 		if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
2446 			    sizeof(u32) * rdev->wiphy.n_cipher_suites,
2447 			    rdev->wiphy.cipher_suites))
2448 			goto nla_put_failure;
2449 
2450 		if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
2451 			       rdev->wiphy.max_num_pmkids))
2452 			goto nla_put_failure;
2453 
2454 		if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
2455 		    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
2456 			goto nla_put_failure;
2457 
2458 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
2459 				rdev->wiphy.available_antennas_tx) ||
2460 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
2461 				rdev->wiphy.available_antennas_rx))
2462 			goto nla_put_failure;
2463 
2464 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
2465 		    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
2466 				rdev->wiphy.probe_resp_offload))
2467 			goto nla_put_failure;
2468 
2469 		if ((rdev->wiphy.available_antennas_tx ||
2470 		     rdev->wiphy.available_antennas_rx) &&
2471 		    rdev->ops->get_antenna) {
2472 			u32 tx_ant = 0, rx_ant = 0;
2473 			int res;
2474 
2475 			res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
2476 			if (!res) {
2477 				if (nla_put_u32(msg,
2478 						NL80211_ATTR_WIPHY_ANTENNA_TX,
2479 						tx_ant) ||
2480 				    nla_put_u32(msg,
2481 						NL80211_ATTR_WIPHY_ANTENNA_RX,
2482 						rx_ant))
2483 					goto nla_put_failure;
2484 			}
2485 		}
2486 
2487 		state->split_start++;
2488 		if (state->split)
2489 			break;
2490 		fallthrough;
2491 	case 2:
2492 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
2493 					rdev->wiphy.interface_modes))
2494 				goto nla_put_failure;
2495 		state->split_start++;
2496 		if (state->split)
2497 			break;
2498 		fallthrough;
2499 	case 3:
2500 		nl_bands = nla_nest_start_noflag(msg,
2501 						 NL80211_ATTR_WIPHY_BANDS);
2502 		if (!nl_bands)
2503 			goto nla_put_failure;
2504 
2505 		for (band = state->band_start;
2506 		     band < (state->split ?
2507 				NUM_NL80211_BANDS :
2508 				NL80211_BAND_60GHZ + 1);
2509 		     band++) {
2510 			struct ieee80211_supported_band *sband;
2511 
2512 			/* omit higher bands for ancient software */
2513 			if (band > NL80211_BAND_5GHZ && !state->split)
2514 				break;
2515 
2516 			sband = rdev->wiphy.bands[band];
2517 
2518 			if (!sband)
2519 				continue;
2520 
2521 			nl_band = nla_nest_start_noflag(msg, band);
2522 			if (!nl_band)
2523 				goto nla_put_failure;
2524 
2525 			switch (state->chan_start) {
2526 			case 0:
2527 				if (nl80211_send_band_rateinfo(msg, sband,
2528 							       state->split))
2529 					goto nla_put_failure;
2530 				state->chan_start++;
2531 				if (state->split)
2532 					break;
2533 				fallthrough;
2534 			default:
2535 				/* add frequencies */
2536 				nl_freqs = nla_nest_start_noflag(msg,
2537 								 NL80211_BAND_ATTR_FREQS);
2538 				if (!nl_freqs)
2539 					goto nla_put_failure;
2540 
2541 				for (i = state->chan_start - 1;
2542 				     i < sband->n_channels;
2543 				     i++) {
2544 					nl_freq = nla_nest_start_noflag(msg,
2545 									i);
2546 					if (!nl_freq)
2547 						goto nla_put_failure;
2548 
2549 					chan = &sband->channels[i];
2550 
2551 					if (nl80211_msg_put_channel(
2552 							msg, &rdev->wiphy, chan,
2553 							state->split))
2554 						goto nla_put_failure;
2555 
2556 					nla_nest_end(msg, nl_freq);
2557 					if (state->split)
2558 						break;
2559 				}
2560 				if (i < sband->n_channels)
2561 					state->chan_start = i + 2;
2562 				else
2563 					state->chan_start = 0;
2564 				nla_nest_end(msg, nl_freqs);
2565 			}
2566 
2567 			nla_nest_end(msg, nl_band);
2568 
2569 			if (state->split) {
2570 				/* start again here */
2571 				if (state->chan_start)
2572 					band--;
2573 				break;
2574 			}
2575 		}
2576 		nla_nest_end(msg, nl_bands);
2577 
2578 		if (band < NUM_NL80211_BANDS)
2579 			state->band_start = band + 1;
2580 		else
2581 			state->band_start = 0;
2582 
2583 		/* if bands & channels are done, continue outside */
2584 		if (state->band_start == 0 && state->chan_start == 0)
2585 			state->split_start++;
2586 		if (state->split)
2587 			break;
2588 		fallthrough;
2589 	case 4:
2590 		nl_cmds = nla_nest_start_noflag(msg,
2591 						NL80211_ATTR_SUPPORTED_COMMANDS);
2592 		if (!nl_cmds)
2593 			goto nla_put_failure;
2594 
2595 		i = nl80211_add_commands_unsplit(rdev, msg);
2596 		if (i < 0)
2597 			goto nla_put_failure;
2598 		if (state->split) {
2599 			CMD(crit_proto_start, CRIT_PROTOCOL_START);
2600 			CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2601 			if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2602 				CMD(channel_switch, CHANNEL_SWITCH);
2603 			CMD(set_qos_map, SET_QOS_MAP);
2604 			if (rdev->wiphy.features &
2605 					NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2606 				CMD(add_tx_ts, ADD_TX_TS);
2607 			CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2608 			CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2609 			CMD(update_ft_ies, UPDATE_FT_IES);
2610 			if (rdev->wiphy.sar_capa)
2611 				CMD(set_sar_specs, SET_SAR_SPECS);
2612 		}
2613 #undef CMD
2614 
2615 		nla_nest_end(msg, nl_cmds);
2616 		state->split_start++;
2617 		if (state->split)
2618 			break;
2619 		fallthrough;
2620 	case 5:
2621 		if (rdev->ops->remain_on_channel &&
2622 		    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2623 		    nla_put_u32(msg,
2624 				NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2625 				rdev->wiphy.max_remain_on_channel_duration))
2626 			goto nla_put_failure;
2627 
2628 		if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2629 		    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2630 			goto nla_put_failure;
2631 
2632 		state->split_start++;
2633 		if (state->split)
2634 			break;
2635 		fallthrough;
2636 	case 6:
2637 #ifdef CONFIG_PM
2638 		if (nl80211_send_wowlan(msg, rdev, state->split))
2639 			goto nla_put_failure;
2640 		state->split_start++;
2641 		if (state->split)
2642 			break;
2643 #else
2644 		state->split_start++;
2645 #endif
2646 		fallthrough;
2647 	case 7:
2648 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2649 					rdev->wiphy.software_iftypes))
2650 			goto nla_put_failure;
2651 
2652 		if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2653 						   state->split))
2654 			goto nla_put_failure;
2655 
2656 		state->split_start++;
2657 		if (state->split)
2658 			break;
2659 		fallthrough;
2660 	case 8:
2661 		if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2662 		    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2663 				rdev->wiphy.ap_sme_capa))
2664 			goto nla_put_failure;
2665 
2666 		features = rdev->wiphy.features;
2667 		/*
2668 		 * We can only add the per-channel limit information if the
2669 		 * dump is split, otherwise it makes it too big. Therefore
2670 		 * only advertise it in that case.
2671 		 */
2672 		if (state->split)
2673 			features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2674 		if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2675 			goto nla_put_failure;
2676 
2677 		if (rdev->wiphy.ht_capa_mod_mask &&
2678 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2679 			    sizeof(*rdev->wiphy.ht_capa_mod_mask),
2680 			    rdev->wiphy.ht_capa_mod_mask))
2681 			goto nla_put_failure;
2682 
2683 		if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2684 		    rdev->wiphy.max_acl_mac_addrs &&
2685 		    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2686 				rdev->wiphy.max_acl_mac_addrs))
2687 			goto nla_put_failure;
2688 
2689 		/*
2690 		 * Any information below this point is only available to
2691 		 * applications that can deal with it being split. This
2692 		 * helps ensure that newly added capabilities don't break
2693 		 * older tools by overrunning their buffers.
2694 		 *
2695 		 * We still increment split_start so that in the split
2696 		 * case we'll continue with more data in the next round,
2697 		 * but break unconditionally so unsplit data stops here.
2698 		 */
2699 		if (state->split)
2700 			state->split_start++;
2701 		else
2702 			state->split_start = 0;
2703 		break;
2704 	case 9:
2705 		if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2706 			goto nla_put_failure;
2707 
2708 		if (nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
2709 				rdev->wiphy.max_sched_scan_plans) ||
2710 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
2711 				rdev->wiphy.max_sched_scan_plan_interval) ||
2712 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
2713 				rdev->wiphy.max_sched_scan_plan_iterations))
2714 			goto nla_put_failure;
2715 
2716 		if (rdev->wiphy.extended_capabilities &&
2717 		    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2718 			     rdev->wiphy.extended_capabilities_len,
2719 			     rdev->wiphy.extended_capabilities) ||
2720 		     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2721 			     rdev->wiphy.extended_capabilities_len,
2722 			     rdev->wiphy.extended_capabilities_mask)))
2723 			goto nla_put_failure;
2724 
2725 		if (rdev->wiphy.vht_capa_mod_mask &&
2726 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2727 			    sizeof(*rdev->wiphy.vht_capa_mod_mask),
2728 			    rdev->wiphy.vht_capa_mod_mask))
2729 			goto nla_put_failure;
2730 
2731 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2732 			    rdev->wiphy.perm_addr))
2733 			goto nla_put_failure;
2734 
2735 		if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2736 		    nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2737 			    rdev->wiphy.addr_mask))
2738 			goto nla_put_failure;
2739 
2740 		if (rdev->wiphy.n_addresses > 1) {
2741 			void *attr;
2742 
2743 			attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2744 			if (!attr)
2745 				goto nla_put_failure;
2746 
2747 			for (i = 0; i < rdev->wiphy.n_addresses; i++)
2748 				if (nla_put(msg, i + 1, ETH_ALEN,
2749 					    rdev->wiphy.addresses[i].addr))
2750 					goto nla_put_failure;
2751 
2752 			nla_nest_end(msg, attr);
2753 		}
2754 
2755 		state->split_start++;
2756 		break;
2757 	case 10:
2758 		if (nl80211_send_coalesce(msg, rdev))
2759 			goto nla_put_failure;
2760 
2761 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2762 		    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2763 		     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2764 			goto nla_put_failure;
2765 
2766 		if (rdev->wiphy.max_ap_assoc_sta &&
2767 		    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2768 				rdev->wiphy.max_ap_assoc_sta))
2769 			goto nla_put_failure;
2770 
2771 		state->split_start++;
2772 		break;
2773 	case 11:
2774 		if (rdev->wiphy.n_vendor_commands) {
2775 			const struct nl80211_vendor_cmd_info *info;
2776 			struct nlattr *nested;
2777 
2778 			nested = nla_nest_start_noflag(msg,
2779 						       NL80211_ATTR_VENDOR_DATA);
2780 			if (!nested)
2781 				goto nla_put_failure;
2782 
2783 			for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2784 				info = &rdev->wiphy.vendor_commands[i].info;
2785 				if (nla_put(msg, i + 1, sizeof(*info), info))
2786 					goto nla_put_failure;
2787 			}
2788 			nla_nest_end(msg, nested);
2789 		}
2790 
2791 		if (rdev->wiphy.n_vendor_events) {
2792 			const struct nl80211_vendor_cmd_info *info;
2793 			struct nlattr *nested;
2794 
2795 			nested = nla_nest_start_noflag(msg,
2796 						       NL80211_ATTR_VENDOR_EVENTS);
2797 			if (!nested)
2798 				goto nla_put_failure;
2799 
2800 			for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2801 				info = &rdev->wiphy.vendor_events[i];
2802 				if (nla_put(msg, i + 1, sizeof(*info), info))
2803 					goto nla_put_failure;
2804 			}
2805 			nla_nest_end(msg, nested);
2806 		}
2807 		state->split_start++;
2808 		break;
2809 	case 12:
2810 		if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2811 		    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2812 			       rdev->wiphy.max_num_csa_counters))
2813 			goto nla_put_failure;
2814 
2815 		if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2816 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2817 			goto nla_put_failure;
2818 
2819 		if (rdev->wiphy.max_sched_scan_reqs &&
2820 		    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2821 				rdev->wiphy.max_sched_scan_reqs))
2822 			goto nla_put_failure;
2823 
2824 		if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2825 			    sizeof(rdev->wiphy.ext_features),
2826 			    rdev->wiphy.ext_features))
2827 			goto nla_put_failure;
2828 
2829 		if (rdev->wiphy.bss_select_support) {
2830 			struct nlattr *nested;
2831 			u32 bss_select_support = rdev->wiphy.bss_select_support;
2832 
2833 			nested = nla_nest_start_noflag(msg,
2834 						       NL80211_ATTR_BSS_SELECT);
2835 			if (!nested)
2836 				goto nla_put_failure;
2837 
2838 			i = 0;
2839 			while (bss_select_support) {
2840 				if ((bss_select_support & 1) &&
2841 				    nla_put_flag(msg, i))
2842 					goto nla_put_failure;
2843 				i++;
2844 				bss_select_support >>= 1;
2845 			}
2846 			nla_nest_end(msg, nested);
2847 		}
2848 
2849 		state->split_start++;
2850 		break;
2851 	case 13:
2852 		if (rdev->wiphy.num_iftype_ext_capab &&
2853 		    rdev->wiphy.iftype_ext_capab) {
2854 			struct nlattr *nested_ext_capab, *nested;
2855 
2856 			nested = nla_nest_start_noflag(msg,
2857 						       NL80211_ATTR_IFTYPE_EXT_CAPA);
2858 			if (!nested)
2859 				goto nla_put_failure;
2860 
2861 			for (i = state->capa_start;
2862 			     i < rdev->wiphy.num_iftype_ext_capab; i++) {
2863 				const struct wiphy_iftype_ext_capab *capab;
2864 
2865 				capab = &rdev->wiphy.iftype_ext_capab[i];
2866 
2867 				nested_ext_capab = nla_nest_start_noflag(msg,
2868 									 i);
2869 				if (!nested_ext_capab ||
2870 				    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2871 						capab->iftype) ||
2872 				    nla_put(msg, NL80211_ATTR_EXT_CAPA,
2873 					    capab->extended_capabilities_len,
2874 					    capab->extended_capabilities) ||
2875 				    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2876 					    capab->extended_capabilities_len,
2877 					    capab->extended_capabilities_mask))
2878 					goto nla_put_failure;
2879 
2880 				if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO &&
2881 				    (nla_put_u16(msg,
2882 						 NL80211_ATTR_EML_CAPABILITY,
2883 						 capab->eml_capabilities) ||
2884 				     nla_put_u16(msg,
2885 						 NL80211_ATTR_MLD_CAPA_AND_OPS,
2886 						 capab->mld_capa_and_ops)))
2887 					goto nla_put_failure;
2888 
2889 				nla_nest_end(msg, nested_ext_capab);
2890 				if (state->split)
2891 					break;
2892 			}
2893 			nla_nest_end(msg, nested);
2894 			if (i < rdev->wiphy.num_iftype_ext_capab) {
2895 				state->capa_start = i + 1;
2896 				break;
2897 			}
2898 		}
2899 
2900 		if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2901 				rdev->wiphy.nan_supported_bands))
2902 			goto nla_put_failure;
2903 
2904 		if (wiphy_ext_feature_isset(&rdev->wiphy,
2905 					    NL80211_EXT_FEATURE_TXQS)) {
2906 			struct cfg80211_txq_stats txqstats = {};
2907 			int res;
2908 
2909 			res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2910 			if (!res &&
2911 			    !nl80211_put_txq_stats(msg, &txqstats,
2912 						   NL80211_ATTR_TXQ_STATS))
2913 				goto nla_put_failure;
2914 
2915 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2916 					rdev->wiphy.txq_limit))
2917 				goto nla_put_failure;
2918 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2919 					rdev->wiphy.txq_memory_limit))
2920 				goto nla_put_failure;
2921 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2922 					rdev->wiphy.txq_quantum))
2923 				goto nla_put_failure;
2924 		}
2925 
2926 		state->split_start++;
2927 		break;
2928 	case 14:
2929 		if (nl80211_send_pmsr_capa(rdev, msg))
2930 			goto nla_put_failure;
2931 
2932 		state->split_start++;
2933 		break;
2934 	case 15:
2935 		if (rdev->wiphy.akm_suites &&
2936 		    nla_put(msg, NL80211_ATTR_AKM_SUITES,
2937 			    sizeof(u32) * rdev->wiphy.n_akm_suites,
2938 			    rdev->wiphy.akm_suites))
2939 			goto nla_put_failure;
2940 
2941 		if (nl80211_put_iftype_akm_suites(rdev, msg))
2942 			goto nla_put_failure;
2943 
2944 		if (nl80211_put_tid_config_support(rdev, msg))
2945 			goto nla_put_failure;
2946 		state->split_start++;
2947 		break;
2948 	case 16:
2949 		if (nl80211_put_sar_specs(rdev, msg))
2950 			goto nla_put_failure;
2951 
2952 		if (nl80211_put_mbssid_support(&rdev->wiphy, msg))
2953 			goto nla_put_failure;
2954 
2955 		if (nla_put_u16(msg, NL80211_ATTR_MAX_NUM_AKM_SUITES,
2956 				rdev->wiphy.max_num_akm_suites))
2957 			goto nla_put_failure;
2958 
2959 		if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO)
2960 			nla_put_flag(msg, NL80211_ATTR_MLO_SUPPORT);
2961 
2962 		/* done */
2963 		state->split_start = 0;
2964 		break;
2965 	}
2966  finish:
2967 	genlmsg_end(msg, hdr);
2968 	return 0;
2969 
2970  nla_put_failure:
2971 	genlmsg_cancel(msg, hdr);
2972 	return -EMSGSIZE;
2973 }
2974 
2975 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
2976 				    struct netlink_callback *cb,
2977 				    struct nl80211_dump_wiphy_state *state)
2978 {
2979 	struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
2980 	int ret;
2981 
2982 	if (!tb)
2983 		return -ENOMEM;
2984 
2985 	ret = nlmsg_parse_deprecated(cb->nlh,
2986 				     GENL_HDRLEN + nl80211_fam.hdrsize,
2987 				     tb, nl80211_fam.maxattr,
2988 				     nl80211_policy, NULL);
2989 	/* ignore parse errors for backward compatibility */
2990 	if (ret) {
2991 		ret = 0;
2992 		goto out;
2993 	}
2994 
2995 	state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
2996 	if (tb[NL80211_ATTR_WIPHY])
2997 		state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2998 	if (tb[NL80211_ATTR_WDEV])
2999 		state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
3000 	if (tb[NL80211_ATTR_IFINDEX]) {
3001 		struct net_device *netdev;
3002 		struct cfg80211_registered_device *rdev;
3003 		int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3004 
3005 		netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
3006 		if (!netdev) {
3007 			ret = -ENODEV;
3008 			goto out;
3009 		}
3010 		if (netdev->ieee80211_ptr) {
3011 			rdev = wiphy_to_rdev(
3012 				netdev->ieee80211_ptr->wiphy);
3013 			state->filter_wiphy = rdev->wiphy_idx;
3014 		}
3015 	}
3016 
3017 	ret = 0;
3018 out:
3019 	kfree(tb);
3020 	return ret;
3021 }
3022 
3023 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
3024 {
3025 	int idx = 0, ret;
3026 	struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
3027 	struct cfg80211_registered_device *rdev;
3028 
3029 	rtnl_lock();
3030 	if (!state) {
3031 		state = kzalloc(sizeof(*state), GFP_KERNEL);
3032 		if (!state) {
3033 			rtnl_unlock();
3034 			return -ENOMEM;
3035 		}
3036 		state->filter_wiphy = -1;
3037 		ret = nl80211_dump_wiphy_parse(skb, cb, state);
3038 		if (ret) {
3039 			kfree(state);
3040 			rtnl_unlock();
3041 			return ret;
3042 		}
3043 		cb->args[0] = (long)state;
3044 	}
3045 
3046 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3047 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3048 			continue;
3049 		if (++idx <= state->start)
3050 			continue;
3051 		if (state->filter_wiphy != -1 &&
3052 		    state->filter_wiphy != rdev->wiphy_idx)
3053 			continue;
3054 		/* attempt to fit multiple wiphy data chunks into the skb */
3055 		do {
3056 			ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
3057 						 skb,
3058 						 NETLINK_CB(cb->skb).portid,
3059 						 cb->nlh->nlmsg_seq,
3060 						 NLM_F_MULTI, state);
3061 			if (ret < 0) {
3062 				/*
3063 				 * If sending the wiphy data didn't fit (ENOBUFS
3064 				 * or EMSGSIZE returned), this SKB is still
3065 				 * empty (so it's not too big because another
3066 				 * wiphy dataset is already in the skb) and
3067 				 * we've not tried to adjust the dump allocation
3068 				 * yet ... then adjust the alloc size to be
3069 				 * bigger, and return 1 but with the empty skb.
3070 				 * This results in an empty message being RX'ed
3071 				 * in userspace, but that is ignored.
3072 				 *
3073 				 * We can then retry with the larger buffer.
3074 				 */
3075 				if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
3076 				    !skb->len && !state->split &&
3077 				    cb->min_dump_alloc < 4096) {
3078 					cb->min_dump_alloc = 4096;
3079 					state->split_start = 0;
3080 					rtnl_unlock();
3081 					return 1;
3082 				}
3083 				idx--;
3084 				break;
3085 			}
3086 		} while (state->split_start > 0);
3087 		break;
3088 	}
3089 	rtnl_unlock();
3090 
3091 	state->start = idx;
3092 
3093 	return skb->len;
3094 }
3095 
3096 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
3097 {
3098 	kfree((void *)cb->args[0]);
3099 	return 0;
3100 }
3101 
3102 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
3103 {
3104 	struct sk_buff *msg;
3105 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3106 	struct nl80211_dump_wiphy_state state = {};
3107 
3108 	msg = nlmsg_new(4096, GFP_KERNEL);
3109 	if (!msg)
3110 		return -ENOMEM;
3111 
3112 	if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
3113 			       info->snd_portid, info->snd_seq, 0,
3114 			       &state) < 0) {
3115 		nlmsg_free(msg);
3116 		return -ENOBUFS;
3117 	}
3118 
3119 	return genlmsg_reply(msg, info);
3120 }
3121 
3122 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
3123 	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
3124 	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
3125 	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
3126 	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
3127 	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
3128 };
3129 
3130 static int parse_txq_params(struct nlattr *tb[],
3131 			    struct ieee80211_txq_params *txq_params)
3132 {
3133 	u8 ac;
3134 
3135 	if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
3136 	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
3137 	    !tb[NL80211_TXQ_ATTR_AIFS])
3138 		return -EINVAL;
3139 
3140 	ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
3141 	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
3142 	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
3143 	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
3144 	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
3145 
3146 	if (ac >= NL80211_NUM_ACS)
3147 		return -EINVAL;
3148 	txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
3149 	return 0;
3150 }
3151 
3152 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
3153 {
3154 	/*
3155 	 * You can only set the channel explicitly for some interfaces,
3156 	 * most have their channel managed via their respective
3157 	 * "establish a connection" command (connect, join, ...)
3158 	 *
3159 	 * For AP/GO and mesh mode, the channel can be set with the
3160 	 * channel userspace API, but is only stored and passed to the
3161 	 * low-level driver when the AP starts or the mesh is joined.
3162 	 * This is for backward compatibility, userspace can also give
3163 	 * the channel in the start-ap or join-mesh commands instead.
3164 	 *
3165 	 * Monitors are special as they are normally slaved to
3166 	 * whatever else is going on, so they have their own special
3167 	 * operation to set the monitor channel if possible.
3168 	 */
3169 	return !wdev ||
3170 		wdev->iftype == NL80211_IFTYPE_AP ||
3171 		wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
3172 		wdev->iftype == NL80211_IFTYPE_MONITOR ||
3173 		wdev->iftype == NL80211_IFTYPE_P2P_GO;
3174 }
3175 
3176 int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
3177 			  struct genl_info *info,
3178 			  struct cfg80211_chan_def *chandef)
3179 {
3180 	struct netlink_ext_ack *extack = info->extack;
3181 	struct nlattr **attrs = info->attrs;
3182 	u32 control_freq;
3183 
3184 	if (!attrs[NL80211_ATTR_WIPHY_FREQ])
3185 		return -EINVAL;
3186 
3187 	control_freq = MHZ_TO_KHZ(
3188 			nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3189 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
3190 		control_freq +=
3191 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
3192 
3193 	memset(chandef, 0, sizeof(*chandef));
3194 	chandef->chan = ieee80211_get_channel_khz(&rdev->wiphy, control_freq);
3195 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
3196 	chandef->center_freq1 = KHZ_TO_MHZ(control_freq);
3197 	chandef->freq1_offset = control_freq % 1000;
3198 	chandef->center_freq2 = 0;
3199 
3200 	/* Primary channel not allowed */
3201 	if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED) {
3202 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
3203 				    "Channel is disabled");
3204 		return -EINVAL;
3205 	}
3206 
3207 	if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
3208 		enum nl80211_channel_type chantype;
3209 
3210 		chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
3211 
3212 		switch (chantype) {
3213 		case NL80211_CHAN_NO_HT:
3214 		case NL80211_CHAN_HT20:
3215 		case NL80211_CHAN_HT40PLUS:
3216 		case NL80211_CHAN_HT40MINUS:
3217 			cfg80211_chandef_create(chandef, chandef->chan,
3218 						chantype);
3219 			/* user input for center_freq is incorrect */
3220 			if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
3221 			    chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
3222 				NL_SET_ERR_MSG_ATTR(extack,
3223 						    attrs[NL80211_ATTR_CENTER_FREQ1],
3224 						    "bad center frequency 1");
3225 				return -EINVAL;
3226 			}
3227 			/* center_freq2 must be zero */
3228 			if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
3229 			    nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
3230 				NL_SET_ERR_MSG_ATTR(extack,
3231 						    attrs[NL80211_ATTR_CENTER_FREQ2],
3232 						    "center frequency 2 can't be used");
3233 				return -EINVAL;
3234 			}
3235 			break;
3236 		default:
3237 			NL_SET_ERR_MSG_ATTR(extack,
3238 					    attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3239 					    "invalid channel type");
3240 			return -EINVAL;
3241 		}
3242 	} else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
3243 		chandef->width =
3244 			nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
3245 		if (chandef->chan->band == NL80211_BAND_S1GHZ) {
3246 			/* User input error for channel width doesn't match channel  */
3247 			if (chandef->width != ieee80211_s1g_channel_width(chandef->chan)) {
3248 				NL_SET_ERR_MSG_ATTR(extack,
3249 						    attrs[NL80211_ATTR_CHANNEL_WIDTH],
3250 						    "bad channel width");
3251 				return -EINVAL;
3252 			}
3253 		}
3254 		if (attrs[NL80211_ATTR_CENTER_FREQ1]) {
3255 			chandef->center_freq1 =
3256 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
3257 			if (attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET])
3258 				chandef->freq1_offset = nla_get_u32(
3259 				      attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET]);
3260 			else
3261 				chandef->freq1_offset = 0;
3262 		}
3263 		if (attrs[NL80211_ATTR_CENTER_FREQ2])
3264 			chandef->center_freq2 =
3265 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
3266 	}
3267 
3268 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
3269 		chandef->edmg.channels =
3270 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
3271 
3272 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
3273 			chandef->edmg.bw_config =
3274 		     nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
3275 	} else {
3276 		chandef->edmg.bw_config = 0;
3277 		chandef->edmg.channels = 0;
3278 	}
3279 
3280 	if (!cfg80211_chandef_valid(chandef)) {
3281 		NL_SET_ERR_MSG(extack, "invalid channel definition");
3282 		return -EINVAL;
3283 	}
3284 
3285 	if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
3286 				     IEEE80211_CHAN_DISABLED)) {
3287 		NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
3288 		return -EINVAL;
3289 	}
3290 
3291 	if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
3292 	     chandef->width == NL80211_CHAN_WIDTH_10) &&
3293 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
3294 		NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
3295 		return -EINVAL;
3296 	}
3297 
3298 	return 0;
3299 }
3300 
3301 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
3302 				 struct net_device *dev,
3303 				 struct genl_info *info,
3304 				 int _link_id)
3305 {
3306 	struct cfg80211_chan_def chandef;
3307 	int result;
3308 	enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
3309 	struct wireless_dev *wdev = NULL;
3310 	int link_id = _link_id;
3311 
3312 	if (dev)
3313 		wdev = dev->ieee80211_ptr;
3314 	if (!nl80211_can_set_dev_channel(wdev))
3315 		return -EOPNOTSUPP;
3316 	if (wdev)
3317 		iftype = wdev->iftype;
3318 
3319 	if (link_id < 0) {
3320 		if (wdev && wdev->valid_links)
3321 			return -EINVAL;
3322 		link_id = 0;
3323 	}
3324 
3325 	result = nl80211_parse_chandef(rdev, info, &chandef);
3326 	if (result)
3327 		return result;
3328 
3329 	switch (iftype) {
3330 	case NL80211_IFTYPE_AP:
3331 	case NL80211_IFTYPE_P2P_GO:
3332 		if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
3333 						   iftype))
3334 			return -EINVAL;
3335 		if (wdev->links[link_id].ap.beacon_interval) {
3336 			struct ieee80211_channel *cur_chan;
3337 
3338 			if (!dev || !rdev->ops->set_ap_chanwidth ||
3339 			    !(rdev->wiphy.features &
3340 			      NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE))
3341 				return -EBUSY;
3342 
3343 			/* Only allow dynamic channel width changes */
3344 			cur_chan = wdev->links[link_id].ap.chandef.chan;
3345 			if (chandef.chan != cur_chan)
3346 				return -EBUSY;
3347 
3348 			result = rdev_set_ap_chanwidth(rdev, dev, link_id,
3349 						       &chandef);
3350 			if (result)
3351 				return result;
3352 			wdev->links[link_id].ap.chandef = chandef;
3353 		} else {
3354 			wdev->u.ap.preset_chandef = chandef;
3355 		}
3356 		return 0;
3357 	case NL80211_IFTYPE_MESH_POINT:
3358 		return cfg80211_set_mesh_channel(rdev, wdev, &chandef);
3359 	case NL80211_IFTYPE_MONITOR:
3360 		return cfg80211_set_monitor_channel(rdev, &chandef);
3361 	default:
3362 		break;
3363 	}
3364 
3365 	return -EINVAL;
3366 }
3367 
3368 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
3369 {
3370 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3371 	int link_id = nl80211_link_id_or_invalid(info->attrs);
3372 	struct net_device *netdev = info->user_ptr[1];
3373 	int ret;
3374 
3375 	wdev_lock(netdev->ieee80211_ptr);
3376 	ret = __nl80211_set_channel(rdev, netdev, info, link_id);
3377 	wdev_unlock(netdev->ieee80211_ptr);
3378 
3379 	return ret;
3380 }
3381 
3382 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
3383 {
3384 	struct cfg80211_registered_device *rdev = NULL;
3385 	struct net_device *netdev = NULL;
3386 	struct wireless_dev *wdev;
3387 	int result = 0, rem_txq_params = 0;
3388 	struct nlattr *nl_txq_params;
3389 	u32 changed;
3390 	u8 retry_short = 0, retry_long = 0;
3391 	u32 frag_threshold = 0, rts_threshold = 0;
3392 	u8 coverage_class = 0;
3393 	u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
3394 
3395 	rtnl_lock();
3396 	/*
3397 	 * Try to find the wiphy and netdev. Normally this
3398 	 * function shouldn't need the netdev, but this is
3399 	 * done for backward compatibility -- previously
3400 	 * setting the channel was done per wiphy, but now
3401 	 * it is per netdev. Previous userland like hostapd
3402 	 * also passed a netdev to set_wiphy, so that it is
3403 	 * possible to let that go to the right netdev!
3404 	 */
3405 
3406 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
3407 		int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
3408 
3409 		netdev = __dev_get_by_index(genl_info_net(info), ifindex);
3410 		if (netdev && netdev->ieee80211_ptr)
3411 			rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
3412 		else
3413 			netdev = NULL;
3414 	}
3415 
3416 	if (!netdev) {
3417 		rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
3418 						  info->attrs);
3419 		if (IS_ERR(rdev)) {
3420 			rtnl_unlock();
3421 			return PTR_ERR(rdev);
3422 		}
3423 		wdev = NULL;
3424 		netdev = NULL;
3425 		result = 0;
3426 	} else
3427 		wdev = netdev->ieee80211_ptr;
3428 
3429 	wiphy_lock(&rdev->wiphy);
3430 
3431 	/*
3432 	 * end workaround code, by now the rdev is available
3433 	 * and locked, and wdev may or may not be NULL.
3434 	 */
3435 
3436 	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
3437 		result = cfg80211_dev_rename(
3438 			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
3439 	rtnl_unlock();
3440 
3441 	if (result)
3442 		goto out;
3443 
3444 	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
3445 		struct ieee80211_txq_params txq_params;
3446 		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
3447 
3448 		if (!rdev->ops->set_txq_params) {
3449 			result = -EOPNOTSUPP;
3450 			goto out;
3451 		}
3452 
3453 		if (!netdev) {
3454 			result = -EINVAL;
3455 			goto out;
3456 		}
3457 
3458 		if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3459 		    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3460 			result = -EINVAL;
3461 			goto out;
3462 		}
3463 
3464 		if (!netif_running(netdev)) {
3465 			result = -ENETDOWN;
3466 			goto out;
3467 		}
3468 
3469 		nla_for_each_nested(nl_txq_params,
3470 				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
3471 				    rem_txq_params) {
3472 			result = nla_parse_nested_deprecated(tb,
3473 							     NL80211_TXQ_ATTR_MAX,
3474 							     nl_txq_params,
3475 							     txq_params_policy,
3476 							     info->extack);
3477 			if (result)
3478 				goto out;
3479 			result = parse_txq_params(tb, &txq_params);
3480 			if (result)
3481 				goto out;
3482 
3483 			txq_params.link_id =
3484 				nl80211_link_id_or_invalid(info->attrs);
3485 
3486 			wdev_lock(netdev->ieee80211_ptr);
3487 			if (txq_params.link_id >= 0 &&
3488 			    !(netdev->ieee80211_ptr->valid_links &
3489 			      BIT(txq_params.link_id)))
3490 				result = -ENOLINK;
3491 			else if (txq_params.link_id >= 0 &&
3492 				 !netdev->ieee80211_ptr->valid_links)
3493 				result = -EINVAL;
3494 			else
3495 				result = rdev_set_txq_params(rdev, netdev,
3496 							     &txq_params);
3497 			wdev_unlock(netdev->ieee80211_ptr);
3498 			if (result)
3499 				goto out;
3500 		}
3501 	}
3502 
3503 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3504 		int link_id = nl80211_link_id_or_invalid(info->attrs);
3505 
3506 		if (wdev) {
3507 			wdev_lock(wdev);
3508 			result = __nl80211_set_channel(
3509 				rdev,
3510 				nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
3511 				info, link_id);
3512 			wdev_unlock(wdev);
3513 		} else {
3514 			result = __nl80211_set_channel(rdev, netdev, info, link_id);
3515 		}
3516 
3517 		if (result)
3518 			goto out;
3519 	}
3520 
3521 	if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
3522 		struct wireless_dev *txp_wdev = wdev;
3523 		enum nl80211_tx_power_setting type;
3524 		int idx, mbm = 0;
3525 
3526 		if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
3527 			txp_wdev = NULL;
3528 
3529 		if (!rdev->ops->set_tx_power) {
3530 			result = -EOPNOTSUPP;
3531 			goto out;
3532 		}
3533 
3534 		idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
3535 		type = nla_get_u32(info->attrs[idx]);
3536 
3537 		if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
3538 		    (type != NL80211_TX_POWER_AUTOMATIC)) {
3539 			result = -EINVAL;
3540 			goto out;
3541 		}
3542 
3543 		if (type != NL80211_TX_POWER_AUTOMATIC) {
3544 			idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
3545 			mbm = nla_get_u32(info->attrs[idx]);
3546 		}
3547 
3548 		result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
3549 		if (result)
3550 			goto out;
3551 	}
3552 
3553 	if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
3554 	    info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
3555 		u32 tx_ant, rx_ant;
3556 
3557 		if ((!rdev->wiphy.available_antennas_tx &&
3558 		     !rdev->wiphy.available_antennas_rx) ||
3559 		    !rdev->ops->set_antenna) {
3560 			result = -EOPNOTSUPP;
3561 			goto out;
3562 		}
3563 
3564 		tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
3565 		rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
3566 
3567 		/* reject antenna configurations which don't match the
3568 		 * available antenna masks, except for the "all" mask */
3569 		if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
3570 		    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
3571 			result = -EINVAL;
3572 			goto out;
3573 		}
3574 
3575 		tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
3576 		rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
3577 
3578 		result = rdev_set_antenna(rdev, tx_ant, rx_ant);
3579 		if (result)
3580 			goto out;
3581 	}
3582 
3583 	changed = 0;
3584 
3585 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
3586 		retry_short = nla_get_u8(
3587 			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
3588 
3589 		changed |= WIPHY_PARAM_RETRY_SHORT;
3590 	}
3591 
3592 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
3593 		retry_long = nla_get_u8(
3594 			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
3595 
3596 		changed |= WIPHY_PARAM_RETRY_LONG;
3597 	}
3598 
3599 	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
3600 		frag_threshold = nla_get_u32(
3601 			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
3602 		if (frag_threshold < 256) {
3603 			result = -EINVAL;
3604 			goto out;
3605 		}
3606 
3607 		if (frag_threshold != (u32) -1) {
3608 			/*
3609 			 * Fragments (apart from the last one) are required to
3610 			 * have even length. Make the fragmentation code
3611 			 * simpler by stripping LSB should someone try to use
3612 			 * odd threshold value.
3613 			 */
3614 			frag_threshold &= ~0x1;
3615 		}
3616 		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
3617 	}
3618 
3619 	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
3620 		rts_threshold = nla_get_u32(
3621 			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
3622 		changed |= WIPHY_PARAM_RTS_THRESHOLD;
3623 	}
3624 
3625 	if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
3626 		if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3627 			result = -EINVAL;
3628 			goto out;
3629 		}
3630 
3631 		coverage_class = nla_get_u8(
3632 			info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
3633 		changed |= WIPHY_PARAM_COVERAGE_CLASS;
3634 	}
3635 
3636 	if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3637 		if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION)) {
3638 			result = -EOPNOTSUPP;
3639 			goto out;
3640 		}
3641 
3642 		changed |= WIPHY_PARAM_DYN_ACK;
3643 	}
3644 
3645 	if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3646 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3647 					     NL80211_EXT_FEATURE_TXQS)) {
3648 			result = -EOPNOTSUPP;
3649 			goto out;
3650 		}
3651 		txq_limit = nla_get_u32(
3652 			info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3653 		changed |= WIPHY_PARAM_TXQ_LIMIT;
3654 	}
3655 
3656 	if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3657 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3658 					     NL80211_EXT_FEATURE_TXQS)) {
3659 			result = -EOPNOTSUPP;
3660 			goto out;
3661 		}
3662 		txq_memory_limit = nla_get_u32(
3663 			info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3664 		changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3665 	}
3666 
3667 	if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3668 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3669 					     NL80211_EXT_FEATURE_TXQS)) {
3670 			result = -EOPNOTSUPP;
3671 			goto out;
3672 		}
3673 		txq_quantum = nla_get_u32(
3674 			info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3675 		changed |= WIPHY_PARAM_TXQ_QUANTUM;
3676 	}
3677 
3678 	if (changed) {
3679 		u8 old_retry_short, old_retry_long;
3680 		u32 old_frag_threshold, old_rts_threshold;
3681 		u8 old_coverage_class;
3682 		u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3683 
3684 		if (!rdev->ops->set_wiphy_params) {
3685 			result = -EOPNOTSUPP;
3686 			goto out;
3687 		}
3688 
3689 		old_retry_short = rdev->wiphy.retry_short;
3690 		old_retry_long = rdev->wiphy.retry_long;
3691 		old_frag_threshold = rdev->wiphy.frag_threshold;
3692 		old_rts_threshold = rdev->wiphy.rts_threshold;
3693 		old_coverage_class = rdev->wiphy.coverage_class;
3694 		old_txq_limit = rdev->wiphy.txq_limit;
3695 		old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3696 		old_txq_quantum = rdev->wiphy.txq_quantum;
3697 
3698 		if (changed & WIPHY_PARAM_RETRY_SHORT)
3699 			rdev->wiphy.retry_short = retry_short;
3700 		if (changed & WIPHY_PARAM_RETRY_LONG)
3701 			rdev->wiphy.retry_long = retry_long;
3702 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3703 			rdev->wiphy.frag_threshold = frag_threshold;
3704 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3705 			rdev->wiphy.rts_threshold = rts_threshold;
3706 		if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3707 			rdev->wiphy.coverage_class = coverage_class;
3708 		if (changed & WIPHY_PARAM_TXQ_LIMIT)
3709 			rdev->wiphy.txq_limit = txq_limit;
3710 		if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3711 			rdev->wiphy.txq_memory_limit = txq_memory_limit;
3712 		if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3713 			rdev->wiphy.txq_quantum = txq_quantum;
3714 
3715 		result = rdev_set_wiphy_params(rdev, changed);
3716 		if (result) {
3717 			rdev->wiphy.retry_short = old_retry_short;
3718 			rdev->wiphy.retry_long = old_retry_long;
3719 			rdev->wiphy.frag_threshold = old_frag_threshold;
3720 			rdev->wiphy.rts_threshold = old_rts_threshold;
3721 			rdev->wiphy.coverage_class = old_coverage_class;
3722 			rdev->wiphy.txq_limit = old_txq_limit;
3723 			rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3724 			rdev->wiphy.txq_quantum = old_txq_quantum;
3725 			goto out;
3726 		}
3727 	}
3728 
3729 	result = 0;
3730 
3731 out:
3732 	wiphy_unlock(&rdev->wiphy);
3733 	return result;
3734 }
3735 
3736 static int nl80211_send_chandef(struct sk_buff *msg,
3737 				const struct cfg80211_chan_def *chandef)
3738 {
3739 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3740 		return -EINVAL;
3741 
3742 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3743 			chandef->chan->center_freq))
3744 		return -ENOBUFS;
3745 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
3746 			chandef->chan->freq_offset))
3747 		return -ENOBUFS;
3748 	switch (chandef->width) {
3749 	case NL80211_CHAN_WIDTH_20_NOHT:
3750 	case NL80211_CHAN_WIDTH_20:
3751 	case NL80211_CHAN_WIDTH_40:
3752 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3753 				cfg80211_get_chandef_type(chandef)))
3754 			return -ENOBUFS;
3755 		break;
3756 	default:
3757 		break;
3758 	}
3759 	if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3760 		return -ENOBUFS;
3761 	if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3762 		return -ENOBUFS;
3763 	if (chandef->center_freq2 &&
3764 	    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3765 		return -ENOBUFS;
3766 	return 0;
3767 }
3768 
3769 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3770 			      struct cfg80211_registered_device *rdev,
3771 			      struct wireless_dev *wdev,
3772 			      enum nl80211_commands cmd)
3773 {
3774 	struct net_device *dev = wdev->netdev;
3775 	void *hdr;
3776 
3777 	WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3778 		cmd != NL80211_CMD_DEL_INTERFACE &&
3779 		cmd != NL80211_CMD_SET_INTERFACE);
3780 
3781 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3782 	if (!hdr)
3783 		return -1;
3784 
3785 	if (dev &&
3786 	    (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3787 	     nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3788 		goto nla_put_failure;
3789 
3790 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3791 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3792 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3793 			      NL80211_ATTR_PAD) ||
3794 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3795 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
3796 			rdev->devlist_generation ^
3797 			(cfg80211_rdev_list_generation << 2)) ||
3798 	    nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3799 		goto nla_put_failure;
3800 
3801 	if (rdev->ops->get_channel && !wdev->valid_links) {
3802 		struct cfg80211_chan_def chandef = {};
3803 		int ret;
3804 
3805 		ret = rdev_get_channel(rdev, wdev, 0, &chandef);
3806 		if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3807 			goto nla_put_failure;
3808 	}
3809 
3810 	if (rdev->ops->get_tx_power) {
3811 		int dbm, ret;
3812 
3813 		ret = rdev_get_tx_power(rdev, wdev, &dbm);
3814 		if (ret == 0 &&
3815 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3816 				DBM_TO_MBM(dbm)))
3817 			goto nla_put_failure;
3818 	}
3819 
3820 	wdev_lock(wdev);
3821 	switch (wdev->iftype) {
3822 	case NL80211_IFTYPE_AP:
3823 	case NL80211_IFTYPE_P2P_GO:
3824 		if (wdev->u.ap.ssid_len &&
3825 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.ap.ssid_len,
3826 			    wdev->u.ap.ssid))
3827 			goto nla_put_failure_locked;
3828 		break;
3829 	case NL80211_IFTYPE_STATION:
3830 	case NL80211_IFTYPE_P2P_CLIENT:
3831 		if (wdev->u.client.ssid_len &&
3832 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.client.ssid_len,
3833 			    wdev->u.client.ssid))
3834 			goto nla_put_failure_locked;
3835 		break;
3836 	case NL80211_IFTYPE_ADHOC:
3837 		if (wdev->u.ibss.ssid_len &&
3838 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.ibss.ssid_len,
3839 			    wdev->u.ibss.ssid))
3840 			goto nla_put_failure_locked;
3841 		break;
3842 	default:
3843 		/* nothing */
3844 		break;
3845 	}
3846 	wdev_unlock(wdev);
3847 
3848 	if (rdev->ops->get_txq_stats) {
3849 		struct cfg80211_txq_stats txqstats = {};
3850 		int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3851 
3852 		if (ret == 0 &&
3853 		    !nl80211_put_txq_stats(msg, &txqstats,
3854 					   NL80211_ATTR_TXQ_STATS))
3855 			goto nla_put_failure;
3856 	}
3857 
3858 	if (wdev->valid_links) {
3859 		unsigned int link_id;
3860 		struct nlattr *links = nla_nest_start(msg,
3861 						      NL80211_ATTR_MLO_LINKS);
3862 
3863 		if (!links)
3864 			goto nla_put_failure;
3865 
3866 		for_each_valid_link(wdev, link_id) {
3867 			struct nlattr *link = nla_nest_start(msg, link_id + 1);
3868 			struct cfg80211_chan_def chandef = {};
3869 			int ret;
3870 
3871 			if (!link)
3872 				goto nla_put_failure;
3873 
3874 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
3875 				goto nla_put_failure;
3876 			if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
3877 				    wdev->links[link_id].addr))
3878 				goto nla_put_failure;
3879 
3880 			ret = rdev_get_channel(rdev, wdev, link_id, &chandef);
3881 			if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3882 				goto nla_put_failure;
3883 
3884 			nla_nest_end(msg, link);
3885 		}
3886 
3887 		nla_nest_end(msg, links);
3888 	}
3889 
3890 	genlmsg_end(msg, hdr);
3891 	return 0;
3892 
3893  nla_put_failure_locked:
3894 	wdev_unlock(wdev);
3895  nla_put_failure:
3896 	genlmsg_cancel(msg, hdr);
3897 	return -EMSGSIZE;
3898 }
3899 
3900 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3901 {
3902 	int wp_idx = 0;
3903 	int if_idx = 0;
3904 	int wp_start = cb->args[0];
3905 	int if_start = cb->args[1];
3906 	int filter_wiphy = -1;
3907 	struct cfg80211_registered_device *rdev;
3908 	struct wireless_dev *wdev;
3909 	int ret;
3910 
3911 	rtnl_lock();
3912 	if (!cb->args[2]) {
3913 		struct nl80211_dump_wiphy_state state = {
3914 			.filter_wiphy = -1,
3915 		};
3916 
3917 		ret = nl80211_dump_wiphy_parse(skb, cb, &state);
3918 		if (ret)
3919 			goto out_unlock;
3920 
3921 		filter_wiphy = state.filter_wiphy;
3922 
3923 		/*
3924 		 * if filtering, set cb->args[2] to +1 since 0 is the default
3925 		 * value needed to determine that parsing is necessary.
3926 		 */
3927 		if (filter_wiphy >= 0)
3928 			cb->args[2] = filter_wiphy + 1;
3929 		else
3930 			cb->args[2] = -1;
3931 	} else if (cb->args[2] > 0) {
3932 		filter_wiphy = cb->args[2] - 1;
3933 	}
3934 
3935 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3936 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3937 			continue;
3938 		if (wp_idx < wp_start) {
3939 			wp_idx++;
3940 			continue;
3941 		}
3942 
3943 		if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
3944 			continue;
3945 
3946 		if_idx = 0;
3947 
3948 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3949 			if (if_idx < if_start) {
3950 				if_idx++;
3951 				continue;
3952 			}
3953 			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
3954 					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
3955 					       rdev, wdev,
3956 					       NL80211_CMD_NEW_INTERFACE) < 0) {
3957 				goto out;
3958 			}
3959 			if_idx++;
3960 		}
3961 
3962 		wp_idx++;
3963 	}
3964  out:
3965 	cb->args[0] = wp_idx;
3966 	cb->args[1] = if_idx;
3967 
3968 	ret = skb->len;
3969  out_unlock:
3970 	rtnl_unlock();
3971 
3972 	return ret;
3973 }
3974 
3975 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
3976 {
3977 	struct sk_buff *msg;
3978 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3979 	struct wireless_dev *wdev = info->user_ptr[1];
3980 
3981 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3982 	if (!msg)
3983 		return -ENOMEM;
3984 
3985 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3986 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3987 		nlmsg_free(msg);
3988 		return -ENOBUFS;
3989 	}
3990 
3991 	return genlmsg_reply(msg, info);
3992 }
3993 
3994 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
3995 	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
3996 	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
3997 	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
3998 	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
3999 	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
4000 	[NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
4001 };
4002 
4003 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
4004 {
4005 	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
4006 	int flag;
4007 
4008 	*mntrflags = 0;
4009 
4010 	if (!nla)
4011 		return -EINVAL;
4012 
4013 	if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
4014 		return -EINVAL;
4015 
4016 	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
4017 		if (flags[flag])
4018 			*mntrflags |= (1<<flag);
4019 
4020 	*mntrflags |= MONITOR_FLAG_CHANGED;
4021 
4022 	return 0;
4023 }
4024 
4025 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
4026 				     enum nl80211_iftype type,
4027 				     struct genl_info *info,
4028 				     struct vif_params *params)
4029 {
4030 	bool change = false;
4031 	int err;
4032 
4033 	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
4034 		if (type != NL80211_IFTYPE_MONITOR)
4035 			return -EINVAL;
4036 
4037 		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
4038 					  &params->flags);
4039 		if (err)
4040 			return err;
4041 
4042 		change = true;
4043 	}
4044 
4045 	if (params->flags & MONITOR_FLAG_ACTIVE &&
4046 	    !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
4047 		return -EOPNOTSUPP;
4048 
4049 	if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
4050 		const u8 *mumimo_groups;
4051 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4052 
4053 		if (type != NL80211_IFTYPE_MONITOR)
4054 			return -EINVAL;
4055 
4056 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4057 			return -EOPNOTSUPP;
4058 
4059 		mumimo_groups =
4060 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
4061 
4062 		/* bits 0 and 63 are reserved and must be zero */
4063 		if ((mumimo_groups[0] & BIT(0)) ||
4064 		    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
4065 			return -EINVAL;
4066 
4067 		params->vht_mumimo_groups = mumimo_groups;
4068 		change = true;
4069 	}
4070 
4071 	if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
4072 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4073 
4074 		if (type != NL80211_IFTYPE_MONITOR)
4075 			return -EINVAL;
4076 
4077 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4078 			return -EOPNOTSUPP;
4079 
4080 		params->vht_mumimo_follow_addr =
4081 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
4082 		change = true;
4083 	}
4084 
4085 	return change ? 1 : 0;
4086 }
4087 
4088 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
4089 			       struct net_device *netdev, u8 use_4addr,
4090 			       enum nl80211_iftype iftype)
4091 {
4092 	if (!use_4addr) {
4093 		if (netdev && netif_is_bridge_port(netdev))
4094 			return -EBUSY;
4095 		return 0;
4096 	}
4097 
4098 	switch (iftype) {
4099 	case NL80211_IFTYPE_AP_VLAN:
4100 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
4101 			return 0;
4102 		break;
4103 	case NL80211_IFTYPE_STATION:
4104 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
4105 			return 0;
4106 		break;
4107 	default:
4108 		break;
4109 	}
4110 
4111 	return -EOPNOTSUPP;
4112 }
4113 
4114 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
4115 {
4116 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4117 	struct vif_params params;
4118 	int err;
4119 	enum nl80211_iftype otype, ntype;
4120 	struct net_device *dev = info->user_ptr[1];
4121 	bool change = false;
4122 
4123 	memset(&params, 0, sizeof(params));
4124 
4125 	otype = ntype = dev->ieee80211_ptr->iftype;
4126 
4127 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
4128 		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4129 		if (otype != ntype)
4130 			change = true;
4131 	}
4132 
4133 	if (info->attrs[NL80211_ATTR_MESH_ID]) {
4134 		struct wireless_dev *wdev = dev->ieee80211_ptr;
4135 
4136 		if (ntype != NL80211_IFTYPE_MESH_POINT)
4137 			return -EINVAL;
4138 		if (netif_running(dev))
4139 			return -EBUSY;
4140 
4141 		wdev_lock(wdev);
4142 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
4143 			     IEEE80211_MAX_MESH_ID_LEN);
4144 		wdev->u.mesh.id_up_len =
4145 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4146 		memcpy(wdev->u.mesh.id,
4147 		       nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4148 		       wdev->u.mesh.id_up_len);
4149 		wdev_unlock(wdev);
4150 	}
4151 
4152 	if (info->attrs[NL80211_ATTR_4ADDR]) {
4153 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4154 		change = true;
4155 		err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
4156 		if (err)
4157 			return err;
4158 	} else {
4159 		params.use_4addr = -1;
4160 	}
4161 
4162 	err = nl80211_parse_mon_options(rdev, ntype, info, &params);
4163 	if (err < 0)
4164 		return err;
4165 	if (err > 0)
4166 		change = true;
4167 
4168 	if (change)
4169 		err = cfg80211_change_iface(rdev, dev, ntype, &params);
4170 	else
4171 		err = 0;
4172 
4173 	if (!err && params.use_4addr != -1)
4174 		dev->ieee80211_ptr->use_4addr = params.use_4addr;
4175 
4176 	if (change && !err) {
4177 		struct wireless_dev *wdev = dev->ieee80211_ptr;
4178 
4179 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
4180 	}
4181 
4182 	return err;
4183 }
4184 
4185 static int _nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4186 {
4187 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4188 	struct vif_params params;
4189 	struct wireless_dev *wdev;
4190 	struct sk_buff *msg;
4191 	int err;
4192 	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
4193 
4194 	memset(&params, 0, sizeof(params));
4195 
4196 	if (!info->attrs[NL80211_ATTR_IFNAME])
4197 		return -EINVAL;
4198 
4199 	if (info->attrs[NL80211_ATTR_IFTYPE])
4200 		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4201 
4202 	if (!rdev->ops->add_virtual_intf)
4203 		return -EOPNOTSUPP;
4204 
4205 	if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
4206 	     rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
4207 	    info->attrs[NL80211_ATTR_MAC]) {
4208 		nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
4209 			   ETH_ALEN);
4210 		if (!is_valid_ether_addr(params.macaddr))
4211 			return -EADDRNOTAVAIL;
4212 	}
4213 
4214 	if (info->attrs[NL80211_ATTR_4ADDR]) {
4215 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4216 		err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
4217 		if (err)
4218 			return err;
4219 	}
4220 
4221 	if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
4222 		return -EOPNOTSUPP;
4223 
4224 	err = nl80211_parse_mon_options(rdev, type, info, &params);
4225 	if (err < 0)
4226 		return err;
4227 
4228 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4229 	if (!msg)
4230 		return -ENOMEM;
4231 
4232 	wdev = rdev_add_virtual_intf(rdev,
4233 				nla_data(info->attrs[NL80211_ATTR_IFNAME]),
4234 				NET_NAME_USER, type, &params);
4235 	if (WARN_ON(!wdev)) {
4236 		nlmsg_free(msg);
4237 		return -EPROTO;
4238 	} else if (IS_ERR(wdev)) {
4239 		nlmsg_free(msg);
4240 		return PTR_ERR(wdev);
4241 	}
4242 
4243 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
4244 		wdev->owner_nlportid = info->snd_portid;
4245 
4246 	switch (type) {
4247 	case NL80211_IFTYPE_MESH_POINT:
4248 		if (!info->attrs[NL80211_ATTR_MESH_ID])
4249 			break;
4250 		wdev_lock(wdev);
4251 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
4252 			     IEEE80211_MAX_MESH_ID_LEN);
4253 		wdev->u.mesh.id_up_len =
4254 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4255 		memcpy(wdev->u.mesh.id,
4256 		       nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4257 		       wdev->u.mesh.id_up_len);
4258 		wdev_unlock(wdev);
4259 		break;
4260 	case NL80211_IFTYPE_NAN:
4261 	case NL80211_IFTYPE_P2P_DEVICE:
4262 		/*
4263 		 * P2P Device and NAN do not have a netdev, so don't go
4264 		 * through the netdev notifier and must be added here
4265 		 */
4266 		cfg80211_init_wdev(wdev);
4267 		cfg80211_register_wdev(rdev, wdev);
4268 		break;
4269 	default:
4270 		break;
4271 	}
4272 
4273 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
4274 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
4275 		nlmsg_free(msg);
4276 		return -ENOBUFS;
4277 	}
4278 
4279 	return genlmsg_reply(msg, info);
4280 }
4281 
4282 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4283 {
4284 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4285 	int ret;
4286 
4287 	/* to avoid failing a new interface creation due to pending removal */
4288 	cfg80211_destroy_ifaces(rdev);
4289 
4290 	wiphy_lock(&rdev->wiphy);
4291 	ret = _nl80211_new_interface(skb, info);
4292 	wiphy_unlock(&rdev->wiphy);
4293 
4294 	return ret;
4295 }
4296 
4297 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
4298 {
4299 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4300 	struct wireless_dev *wdev = info->user_ptr[1];
4301 
4302 	if (!rdev->ops->del_virtual_intf)
4303 		return -EOPNOTSUPP;
4304 
4305 	/*
4306 	 * We hold RTNL, so this is safe, without RTNL opencount cannot
4307 	 * reach 0, and thus the rdev cannot be deleted.
4308 	 *
4309 	 * We need to do it for the dev_close(), since that will call
4310 	 * the netdev notifiers, and we need to acquire the mutex there
4311 	 * but don't know if we get there from here or from some other
4312 	 * place (e.g. "ip link set ... down").
4313 	 */
4314 	mutex_unlock(&rdev->wiphy.mtx);
4315 
4316 	/*
4317 	 * If we remove a wireless device without a netdev then clear
4318 	 * user_ptr[1] so that nl80211_post_doit won't dereference it
4319 	 * to check if it needs to do dev_put(). Otherwise it crashes
4320 	 * since the wdev has been freed, unlike with a netdev where
4321 	 * we need the dev_put() for the netdev to really be freed.
4322 	 */
4323 	if (!wdev->netdev)
4324 		info->user_ptr[1] = NULL;
4325 	else
4326 		dev_close(wdev->netdev);
4327 
4328 	mutex_lock(&rdev->wiphy.mtx);
4329 
4330 	return cfg80211_remove_virtual_intf(rdev, wdev);
4331 }
4332 
4333 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
4334 {
4335 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4336 	struct net_device *dev = info->user_ptr[1];
4337 	u16 noack_map;
4338 
4339 	if (!info->attrs[NL80211_ATTR_NOACK_MAP])
4340 		return -EINVAL;
4341 
4342 	if (!rdev->ops->set_noack_map)
4343 		return -EOPNOTSUPP;
4344 
4345 	noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
4346 
4347 	return rdev_set_noack_map(rdev, dev, noack_map);
4348 }
4349 
4350 static int nl80211_validate_key_link_id(struct genl_info *info,
4351 					struct wireless_dev *wdev,
4352 					int link_id, bool pairwise)
4353 {
4354 	if (pairwise) {
4355 		if (link_id != -1) {
4356 			GENL_SET_ERR_MSG(info,
4357 					 "link ID not allowed for pairwise key");
4358 			return -EINVAL;
4359 		}
4360 
4361 		return 0;
4362 	}
4363 
4364 	if (wdev->valid_links) {
4365 		if (link_id == -1) {
4366 			GENL_SET_ERR_MSG(info,
4367 					 "link ID must for MLO group key");
4368 			return -EINVAL;
4369 		}
4370 		if (!(wdev->valid_links & BIT(link_id))) {
4371 			GENL_SET_ERR_MSG(info, "invalid link ID for MLO group key");
4372 			return -EINVAL;
4373 		}
4374 	} else if (link_id != -1) {
4375 		GENL_SET_ERR_MSG(info, "link ID not allowed for non-MLO group key");
4376 		return -EINVAL;
4377 	}
4378 
4379 	return 0;
4380 }
4381 
4382 struct get_key_cookie {
4383 	struct sk_buff *msg;
4384 	int error;
4385 	int idx;
4386 };
4387 
4388 static void get_key_callback(void *c, struct key_params *params)
4389 {
4390 	struct nlattr *key;
4391 	struct get_key_cookie *cookie = c;
4392 
4393 	if ((params->key &&
4394 	     nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
4395 		     params->key_len, params->key)) ||
4396 	    (params->seq &&
4397 	     nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
4398 		     params->seq_len, params->seq)) ||
4399 	    (params->cipher &&
4400 	     nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
4401 			 params->cipher)))
4402 		goto nla_put_failure;
4403 
4404 	key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
4405 	if (!key)
4406 		goto nla_put_failure;
4407 
4408 	if ((params->key &&
4409 	     nla_put(cookie->msg, NL80211_KEY_DATA,
4410 		     params->key_len, params->key)) ||
4411 	    (params->seq &&
4412 	     nla_put(cookie->msg, NL80211_KEY_SEQ,
4413 		     params->seq_len, params->seq)) ||
4414 	    (params->cipher &&
4415 	     nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
4416 			 params->cipher)))
4417 		goto nla_put_failure;
4418 
4419 	if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
4420 		goto nla_put_failure;
4421 
4422 	nla_nest_end(cookie->msg, key);
4423 
4424 	return;
4425  nla_put_failure:
4426 	cookie->error = 1;
4427 }
4428 
4429 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
4430 {
4431 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4432 	int err;
4433 	struct net_device *dev = info->user_ptr[1];
4434 	u8 key_idx = 0;
4435 	const u8 *mac_addr = NULL;
4436 	bool pairwise;
4437 	struct get_key_cookie cookie = {
4438 		.error = 0,
4439 	};
4440 	void *hdr;
4441 	struct sk_buff *msg;
4442 	bool bigtk_support = false;
4443 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4444 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4445 
4446 	if (wiphy_ext_feature_isset(&rdev->wiphy,
4447 				    NL80211_EXT_FEATURE_BEACON_PROTECTION))
4448 		bigtk_support = true;
4449 
4450 	if ((wdev->iftype == NL80211_IFTYPE_STATION ||
4451 	     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
4452 	    wiphy_ext_feature_isset(&rdev->wiphy,
4453 				    NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
4454 		bigtk_support = true;
4455 
4456 	if (info->attrs[NL80211_ATTR_KEY_IDX]) {
4457 		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
4458 
4459 		if (key_idx >= 6 && key_idx <= 7 && !bigtk_support) {
4460 			GENL_SET_ERR_MSG(info, "BIGTK not supported");
4461 			return -EINVAL;
4462 		}
4463 	}
4464 
4465 	if (info->attrs[NL80211_ATTR_MAC])
4466 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4467 
4468 	pairwise = !!mac_addr;
4469 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
4470 		u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
4471 
4472 		if (kt != NL80211_KEYTYPE_GROUP &&
4473 		    kt != NL80211_KEYTYPE_PAIRWISE)
4474 			return -EINVAL;
4475 		pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
4476 	}
4477 
4478 	if (!rdev->ops->get_key)
4479 		return -EOPNOTSUPP;
4480 
4481 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4482 		return -ENOENT;
4483 
4484 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4485 	if (!msg)
4486 		return -ENOMEM;
4487 
4488 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
4489 			     NL80211_CMD_NEW_KEY);
4490 	if (!hdr)
4491 		goto nla_put_failure;
4492 
4493 	cookie.msg = msg;
4494 	cookie.idx = key_idx;
4495 
4496 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4497 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
4498 		goto nla_put_failure;
4499 	if (mac_addr &&
4500 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
4501 		goto nla_put_failure;
4502 
4503 	err = nl80211_validate_key_link_id(info, wdev, link_id, pairwise);
4504 	if (err)
4505 		goto free_msg;
4506 
4507 	err = rdev_get_key(rdev, dev, link_id, key_idx, pairwise, mac_addr,
4508 			   &cookie, get_key_callback);
4509 
4510 	if (err)
4511 		goto free_msg;
4512 
4513 	if (cookie.error)
4514 		goto nla_put_failure;
4515 
4516 	genlmsg_end(msg, hdr);
4517 	return genlmsg_reply(msg, info);
4518 
4519  nla_put_failure:
4520 	err = -ENOBUFS;
4521  free_msg:
4522 	nlmsg_free(msg);
4523 	return err;
4524 }
4525 
4526 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
4527 {
4528 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4529 	struct key_parse key;
4530 	int err;
4531 	struct net_device *dev = info->user_ptr[1];
4532 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4533 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4534 
4535 	err = nl80211_parse_key(info, &key);
4536 	if (err)
4537 		return err;
4538 
4539 	if (key.idx < 0)
4540 		return -EINVAL;
4541 
4542 	/* Only support setting default key and
4543 	 * Extended Key ID action NL80211_KEY_SET_TX.
4544 	 */
4545 	if (!key.def && !key.defmgmt && !key.defbeacon &&
4546 	    !(key.p.mode == NL80211_KEY_SET_TX))
4547 		return -EINVAL;
4548 
4549 	wdev_lock(wdev);
4550 
4551 	if (key.def) {
4552 		if (!rdev->ops->set_default_key) {
4553 			err = -EOPNOTSUPP;
4554 			goto out;
4555 		}
4556 
4557 		err = nl80211_key_allowed(wdev);
4558 		if (err)
4559 			goto out;
4560 
4561 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4562 		if (err)
4563 			goto out;
4564 
4565 		err = rdev_set_default_key(rdev, dev, link_id, key.idx,
4566 					   key.def_uni, key.def_multi);
4567 
4568 		if (err)
4569 			goto out;
4570 
4571 #ifdef CONFIG_CFG80211_WEXT
4572 		wdev->wext.default_key = key.idx;
4573 #endif
4574 	} else if (key.defmgmt) {
4575 		if (key.def_uni || !key.def_multi) {
4576 			err = -EINVAL;
4577 			goto out;
4578 		}
4579 
4580 		if (!rdev->ops->set_default_mgmt_key) {
4581 			err = -EOPNOTSUPP;
4582 			goto out;
4583 		}
4584 
4585 		err = nl80211_key_allowed(wdev);
4586 		if (err)
4587 			goto out;
4588 
4589 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4590 		if (err)
4591 			goto out;
4592 
4593 		err = rdev_set_default_mgmt_key(rdev, dev, link_id, key.idx);
4594 		if (err)
4595 			goto out;
4596 
4597 #ifdef CONFIG_CFG80211_WEXT
4598 		wdev->wext.default_mgmt_key = key.idx;
4599 #endif
4600 	} else if (key.defbeacon) {
4601 		if (key.def_uni || !key.def_multi) {
4602 			err = -EINVAL;
4603 			goto out;
4604 		}
4605 
4606 		if (!rdev->ops->set_default_beacon_key) {
4607 			err = -EOPNOTSUPP;
4608 			goto out;
4609 		}
4610 
4611 		err = nl80211_key_allowed(wdev);
4612 		if (err)
4613 			goto out;
4614 
4615 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4616 		if (err)
4617 			goto out;
4618 
4619 		err = rdev_set_default_beacon_key(rdev, dev, link_id, key.idx);
4620 		if (err)
4621 			goto out;
4622 	} else if (key.p.mode == NL80211_KEY_SET_TX &&
4623 		   wiphy_ext_feature_isset(&rdev->wiphy,
4624 					   NL80211_EXT_FEATURE_EXT_KEY_ID)) {
4625 		u8 *mac_addr = NULL;
4626 
4627 		if (info->attrs[NL80211_ATTR_MAC])
4628 			mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4629 
4630 		if (!mac_addr || key.idx < 0 || key.idx > 1) {
4631 			err = -EINVAL;
4632 			goto out;
4633 		}
4634 
4635 		err = nl80211_validate_key_link_id(info, wdev, link_id, true);
4636 		if (err)
4637 			goto out;
4638 
4639 		err = rdev_add_key(rdev, dev, link_id, key.idx,
4640 				   NL80211_KEYTYPE_PAIRWISE,
4641 				   mac_addr, &key.p);
4642 	} else {
4643 		err = -EINVAL;
4644 	}
4645  out:
4646 	wdev_unlock(wdev);
4647 
4648 	return err;
4649 }
4650 
4651 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
4652 {
4653 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4654 	int err;
4655 	struct net_device *dev = info->user_ptr[1];
4656 	struct key_parse key;
4657 	const u8 *mac_addr = NULL;
4658 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4659 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4660 
4661 	err = nl80211_parse_key(info, &key);
4662 	if (err)
4663 		return err;
4664 
4665 	if (!key.p.key) {
4666 		GENL_SET_ERR_MSG(info, "no key");
4667 		return -EINVAL;
4668 	}
4669 
4670 	if (info->attrs[NL80211_ATTR_MAC])
4671 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4672 
4673 	if (key.type == -1) {
4674 		if (mac_addr)
4675 			key.type = NL80211_KEYTYPE_PAIRWISE;
4676 		else
4677 			key.type = NL80211_KEYTYPE_GROUP;
4678 	}
4679 
4680 	/* for now */
4681 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4682 	    key.type != NL80211_KEYTYPE_GROUP) {
4683 		GENL_SET_ERR_MSG(info, "key type not pairwise or group");
4684 		return -EINVAL;
4685 	}
4686 
4687 	if (key.type == NL80211_KEYTYPE_GROUP &&
4688 	    info->attrs[NL80211_ATTR_VLAN_ID])
4689 		key.p.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
4690 
4691 	if (!rdev->ops->add_key)
4692 		return -EOPNOTSUPP;
4693 
4694 	if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
4695 					   key.type == NL80211_KEYTYPE_PAIRWISE,
4696 					   mac_addr)) {
4697 		GENL_SET_ERR_MSG(info, "key setting validation failed");
4698 		return -EINVAL;
4699 	}
4700 
4701 	wdev_lock(wdev);
4702 	err = nl80211_key_allowed(wdev);
4703 	if (err)
4704 		GENL_SET_ERR_MSG(info, "key not allowed");
4705 
4706 	if (!err)
4707 		err = nl80211_validate_key_link_id(info, wdev, link_id,
4708 				key.type == NL80211_KEYTYPE_PAIRWISE);
4709 
4710 	if (!err) {
4711 		err = rdev_add_key(rdev, dev, link_id, key.idx,
4712 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4713 				    mac_addr, &key.p);
4714 		if (err)
4715 			GENL_SET_ERR_MSG(info, "key addition failed");
4716 	}
4717 	wdev_unlock(wdev);
4718 
4719 	return err;
4720 }
4721 
4722 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
4723 {
4724 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4725 	int err;
4726 	struct net_device *dev = info->user_ptr[1];
4727 	u8 *mac_addr = NULL;
4728 	struct key_parse key;
4729 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4730 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4731 
4732 	err = nl80211_parse_key(info, &key);
4733 	if (err)
4734 		return err;
4735 
4736 	if (info->attrs[NL80211_ATTR_MAC])
4737 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4738 
4739 	if (key.type == -1) {
4740 		if (mac_addr)
4741 			key.type = NL80211_KEYTYPE_PAIRWISE;
4742 		else
4743 			key.type = NL80211_KEYTYPE_GROUP;
4744 	}
4745 
4746 	/* for now */
4747 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4748 	    key.type != NL80211_KEYTYPE_GROUP)
4749 		return -EINVAL;
4750 
4751 	if (!cfg80211_valid_key_idx(rdev, key.idx,
4752 				    key.type == NL80211_KEYTYPE_PAIRWISE))
4753 		return -EINVAL;
4754 
4755 	if (!rdev->ops->del_key)
4756 		return -EOPNOTSUPP;
4757 
4758 	wdev_lock(wdev);
4759 	err = nl80211_key_allowed(wdev);
4760 
4761 	if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
4762 	    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4763 		err = -ENOENT;
4764 
4765 	if (!err)
4766 		err = nl80211_validate_key_link_id(info, wdev, link_id,
4767 				key.type == NL80211_KEYTYPE_PAIRWISE);
4768 
4769 	if (!err)
4770 		err = rdev_del_key(rdev, dev, link_id, key.idx,
4771 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4772 				   mac_addr);
4773 
4774 #ifdef CONFIG_CFG80211_WEXT
4775 	if (!err) {
4776 		if (key.idx == wdev->wext.default_key)
4777 			wdev->wext.default_key = -1;
4778 		else if (key.idx == wdev->wext.default_mgmt_key)
4779 			wdev->wext.default_mgmt_key = -1;
4780 	}
4781 #endif
4782 	wdev_unlock(wdev);
4783 
4784 	return err;
4785 }
4786 
4787 /* This function returns an error or the number of nested attributes */
4788 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
4789 {
4790 	struct nlattr *attr;
4791 	int n_entries = 0, tmp;
4792 
4793 	nla_for_each_nested(attr, nl_attr, tmp) {
4794 		if (nla_len(attr) != ETH_ALEN)
4795 			return -EINVAL;
4796 
4797 		n_entries++;
4798 	}
4799 
4800 	return n_entries;
4801 }
4802 
4803 /*
4804  * This function parses ACL information and allocates memory for ACL data.
4805  * On successful return, the calling function is responsible to free the
4806  * ACL buffer returned by this function.
4807  */
4808 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
4809 						struct genl_info *info)
4810 {
4811 	enum nl80211_acl_policy acl_policy;
4812 	struct nlattr *attr;
4813 	struct cfg80211_acl_data *acl;
4814 	int i = 0, n_entries, tmp;
4815 
4816 	if (!wiphy->max_acl_mac_addrs)
4817 		return ERR_PTR(-EOPNOTSUPP);
4818 
4819 	if (!info->attrs[NL80211_ATTR_ACL_POLICY])
4820 		return ERR_PTR(-EINVAL);
4821 
4822 	acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
4823 	if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
4824 	    acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
4825 		return ERR_PTR(-EINVAL);
4826 
4827 	if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
4828 		return ERR_PTR(-EINVAL);
4829 
4830 	n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
4831 	if (n_entries < 0)
4832 		return ERR_PTR(n_entries);
4833 
4834 	if (n_entries > wiphy->max_acl_mac_addrs)
4835 		return ERR_PTR(-ENOTSUPP);
4836 
4837 	acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4838 	if (!acl)
4839 		return ERR_PTR(-ENOMEM);
4840 
4841 	nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4842 		memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4843 		i++;
4844 	}
4845 
4846 	acl->n_acl_entries = n_entries;
4847 	acl->acl_policy = acl_policy;
4848 
4849 	return acl;
4850 }
4851 
4852 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4853 {
4854 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4855 	struct net_device *dev = info->user_ptr[1];
4856 	struct cfg80211_acl_data *acl;
4857 	int err;
4858 
4859 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4860 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4861 		return -EOPNOTSUPP;
4862 
4863 	if (!dev->ieee80211_ptr->links[0].ap.beacon_interval)
4864 		return -EINVAL;
4865 
4866 	acl = parse_acl_data(&rdev->wiphy, info);
4867 	if (IS_ERR(acl))
4868 		return PTR_ERR(acl);
4869 
4870 	err = rdev_set_mac_acl(rdev, dev, acl);
4871 
4872 	kfree(acl);
4873 
4874 	return err;
4875 }
4876 
4877 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4878 			   u8 *rates, u8 rates_len)
4879 {
4880 	u8 i;
4881 	u32 mask = 0;
4882 
4883 	for (i = 0; i < rates_len; i++) {
4884 		int rate = (rates[i] & 0x7f) * 5;
4885 		int ridx;
4886 
4887 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4888 			struct ieee80211_rate *srate =
4889 				&sband->bitrates[ridx];
4890 			if (rate == srate->bitrate) {
4891 				mask |= 1 << ridx;
4892 				break;
4893 			}
4894 		}
4895 		if (ridx == sband->n_bitrates)
4896 			return 0; /* rate not found */
4897 	}
4898 
4899 	return mask;
4900 }
4901 
4902 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4903 			       u8 *rates, u8 rates_len,
4904 			       u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4905 {
4906 	u8 i;
4907 
4908 	memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4909 
4910 	for (i = 0; i < rates_len; i++) {
4911 		int ridx, rbit;
4912 
4913 		ridx = rates[i] / 8;
4914 		rbit = BIT(rates[i] % 8);
4915 
4916 		/* check validity */
4917 		if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4918 			return false;
4919 
4920 		/* check availability */
4921 		ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
4922 		if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
4923 			mcs[ridx] |= rbit;
4924 		else
4925 			return false;
4926 	}
4927 
4928 	return true;
4929 }
4930 
4931 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
4932 {
4933 	u16 mcs_mask = 0;
4934 
4935 	switch (vht_mcs_map) {
4936 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
4937 		break;
4938 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
4939 		mcs_mask = 0x00FF;
4940 		break;
4941 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
4942 		mcs_mask = 0x01FF;
4943 		break;
4944 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
4945 		mcs_mask = 0x03FF;
4946 		break;
4947 	default:
4948 		break;
4949 	}
4950 
4951 	return mcs_mask;
4952 }
4953 
4954 static void vht_build_mcs_mask(u16 vht_mcs_map,
4955 			       u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
4956 {
4957 	u8 nss;
4958 
4959 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
4960 		vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
4961 		vht_mcs_map >>= 2;
4962 	}
4963 }
4964 
4965 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
4966 			     struct nl80211_txrate_vht *txrate,
4967 			     u16 mcs[NL80211_VHT_NSS_MAX])
4968 {
4969 	u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4970 	u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
4971 	u8 i;
4972 
4973 	if (!sband->vht_cap.vht_supported)
4974 		return false;
4975 
4976 	memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
4977 
4978 	/* Build vht_mcs_mask from VHT capabilities */
4979 	vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
4980 
4981 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4982 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
4983 			mcs[i] = txrate->mcs[i];
4984 		else
4985 			return false;
4986 	}
4987 
4988 	return true;
4989 }
4990 
4991 static u16 he_mcs_map_to_mcs_mask(u8 he_mcs_map)
4992 {
4993 	switch (he_mcs_map) {
4994 	case IEEE80211_HE_MCS_NOT_SUPPORTED:
4995 		return 0;
4996 	case IEEE80211_HE_MCS_SUPPORT_0_7:
4997 		return 0x00FF;
4998 	case IEEE80211_HE_MCS_SUPPORT_0_9:
4999 		return 0x03FF;
5000 	case IEEE80211_HE_MCS_SUPPORT_0_11:
5001 		return 0xFFF;
5002 	default:
5003 		break;
5004 	}
5005 	return 0;
5006 }
5007 
5008 static void he_build_mcs_mask(u16 he_mcs_map,
5009 			      u16 he_mcs_mask[NL80211_HE_NSS_MAX])
5010 {
5011 	u8 nss;
5012 
5013 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
5014 		he_mcs_mask[nss] = he_mcs_map_to_mcs_mask(he_mcs_map & 0x03);
5015 		he_mcs_map >>= 2;
5016 	}
5017 }
5018 
5019 static u16 he_get_txmcsmap(struct genl_info *info, unsigned int link_id,
5020 			   const struct ieee80211_sta_he_cap *he_cap)
5021 {
5022 	struct net_device *dev = info->user_ptr[1];
5023 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5024 	struct cfg80211_chan_def *chandef;
5025 	__le16 tx_mcs;
5026 
5027 	chandef = wdev_chandef(wdev, link_id);
5028 	if (!chandef) {
5029 		/*
5030 		 * This is probably broken, but we never maintained
5031 		 * a chandef in these cases, so it always was.
5032 		 */
5033 		return le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
5034 	}
5035 
5036 	switch (chandef->width) {
5037 	case NL80211_CHAN_WIDTH_80P80:
5038 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80p80;
5039 		break;
5040 	case NL80211_CHAN_WIDTH_160:
5041 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_160;
5042 		break;
5043 	default:
5044 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80;
5045 		break;
5046 	}
5047 
5048 	return le16_to_cpu(tx_mcs);
5049 }
5050 
5051 static bool he_set_mcs_mask(struct genl_info *info,
5052 			    struct wireless_dev *wdev,
5053 			    struct ieee80211_supported_band *sband,
5054 			    struct nl80211_txrate_he *txrate,
5055 			    u16 mcs[NL80211_HE_NSS_MAX],
5056 			    unsigned int link_id)
5057 {
5058 	const struct ieee80211_sta_he_cap *he_cap;
5059 	u16 tx_mcs_mask[NL80211_HE_NSS_MAX] = {};
5060 	u16 tx_mcs_map = 0;
5061 	u8 i;
5062 
5063 	he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5064 	if (!he_cap)
5065 		return false;
5066 
5067 	memset(mcs, 0, sizeof(u16) * NL80211_HE_NSS_MAX);
5068 
5069 	tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5070 
5071 	/* Build he_mcs_mask from HE capabilities */
5072 	he_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
5073 
5074 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5075 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
5076 			mcs[i] = txrate->mcs[i];
5077 		else
5078 			return false;
5079 	}
5080 
5081 	return true;
5082 }
5083 
5084 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
5085 					 struct nlattr *attrs[],
5086 					 enum nl80211_attrs attr,
5087 					 struct cfg80211_bitrate_mask *mask,
5088 					 struct net_device *dev,
5089 					 bool default_all_enabled,
5090 					 unsigned int link_id)
5091 {
5092 	struct nlattr *tb[NL80211_TXRATE_MAX + 1];
5093 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5094 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5095 	int rem, i;
5096 	struct nlattr *tx_rates;
5097 	struct ieee80211_supported_band *sband;
5098 	u16 vht_tx_mcs_map, he_tx_mcs_map;
5099 
5100 	memset(mask, 0, sizeof(*mask));
5101 	/* Default to all rates enabled */
5102 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
5103 		const struct ieee80211_sta_he_cap *he_cap;
5104 
5105 		if (!default_all_enabled)
5106 			break;
5107 
5108 		sband = rdev->wiphy.bands[i];
5109 
5110 		if (!sband)
5111 			continue;
5112 
5113 		mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
5114 		memcpy(mask->control[i].ht_mcs,
5115 		       sband->ht_cap.mcs.rx_mask,
5116 		       sizeof(mask->control[i].ht_mcs));
5117 
5118 		if (sband->vht_cap.vht_supported) {
5119 			vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5120 			vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
5121 		}
5122 
5123 		he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5124 		if (!he_cap)
5125 			continue;
5126 
5127 		he_tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5128 		he_build_mcs_mask(he_tx_mcs_map, mask->control[i].he_mcs);
5129 
5130 		mask->control[i].he_gi = 0xFF;
5131 		mask->control[i].he_ltf = 0xFF;
5132 	}
5133 
5134 	/* if no rates are given set it back to the defaults */
5135 	if (!attrs[attr])
5136 		goto out;
5137 
5138 	/* The nested attribute uses enum nl80211_band as the index. This maps
5139 	 * directly to the enum nl80211_band values used in cfg80211.
5140 	 */
5141 	BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
5142 	nla_for_each_nested(tx_rates, attrs[attr], rem) {
5143 		enum nl80211_band band = nla_type(tx_rates);
5144 		int err;
5145 
5146 		if (band < 0 || band >= NUM_NL80211_BANDS)
5147 			return -EINVAL;
5148 		sband = rdev->wiphy.bands[band];
5149 		if (sband == NULL)
5150 			return -EINVAL;
5151 		err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
5152 						  tx_rates,
5153 						  nl80211_txattr_policy,
5154 						  info->extack);
5155 		if (err)
5156 			return err;
5157 		if (tb[NL80211_TXRATE_LEGACY]) {
5158 			mask->control[band].legacy = rateset_to_mask(
5159 				sband,
5160 				nla_data(tb[NL80211_TXRATE_LEGACY]),
5161 				nla_len(tb[NL80211_TXRATE_LEGACY]));
5162 			if ((mask->control[band].legacy == 0) &&
5163 			    nla_len(tb[NL80211_TXRATE_LEGACY]))
5164 				return -EINVAL;
5165 		}
5166 		if (tb[NL80211_TXRATE_HT]) {
5167 			if (!ht_rateset_to_mask(
5168 					sband,
5169 					nla_data(tb[NL80211_TXRATE_HT]),
5170 					nla_len(tb[NL80211_TXRATE_HT]),
5171 					mask->control[band].ht_mcs))
5172 				return -EINVAL;
5173 		}
5174 
5175 		if (tb[NL80211_TXRATE_VHT]) {
5176 			if (!vht_set_mcs_mask(
5177 					sband,
5178 					nla_data(tb[NL80211_TXRATE_VHT]),
5179 					mask->control[band].vht_mcs))
5180 				return -EINVAL;
5181 		}
5182 
5183 		if (tb[NL80211_TXRATE_GI]) {
5184 			mask->control[band].gi =
5185 				nla_get_u8(tb[NL80211_TXRATE_GI]);
5186 			if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
5187 				return -EINVAL;
5188 		}
5189 		if (tb[NL80211_TXRATE_HE] &&
5190 		    !he_set_mcs_mask(info, wdev, sband,
5191 				     nla_data(tb[NL80211_TXRATE_HE]),
5192 				     mask->control[band].he_mcs,
5193 				     link_id))
5194 			return -EINVAL;
5195 
5196 		if (tb[NL80211_TXRATE_HE_GI])
5197 			mask->control[band].he_gi =
5198 				nla_get_u8(tb[NL80211_TXRATE_HE_GI]);
5199 		if (tb[NL80211_TXRATE_HE_LTF])
5200 			mask->control[band].he_ltf =
5201 				nla_get_u8(tb[NL80211_TXRATE_HE_LTF]);
5202 
5203 		if (mask->control[band].legacy == 0) {
5204 			/* don't allow empty legacy rates if HT, VHT or HE
5205 			 * are not even supported.
5206 			 */
5207 			if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
5208 			      rdev->wiphy.bands[band]->vht_cap.vht_supported ||
5209 			      ieee80211_get_he_iftype_cap(sband, wdev->iftype)))
5210 				return -EINVAL;
5211 
5212 			for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5213 				if (mask->control[band].ht_mcs[i])
5214 					goto out;
5215 
5216 			for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5217 				if (mask->control[band].vht_mcs[i])
5218 					goto out;
5219 
5220 			for (i = 0; i < NL80211_HE_NSS_MAX; i++)
5221 				if (mask->control[band].he_mcs[i])
5222 					goto out;
5223 
5224 			/* legacy and mcs rates may not be both empty */
5225 			return -EINVAL;
5226 		}
5227 	}
5228 
5229 out:
5230 	return 0;
5231 }
5232 
5233 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
5234 				   enum nl80211_band band,
5235 				   struct cfg80211_bitrate_mask *beacon_rate)
5236 {
5237 	u32 count_ht, count_vht, count_he, i;
5238 	u32 rate = beacon_rate->control[band].legacy;
5239 
5240 	/* Allow only one rate */
5241 	if (hweight32(rate) > 1)
5242 		return -EINVAL;
5243 
5244 	count_ht = 0;
5245 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
5246 		if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
5247 			return -EINVAL;
5248 		} else if (beacon_rate->control[band].ht_mcs[i]) {
5249 			count_ht++;
5250 			if (count_ht > 1)
5251 				return -EINVAL;
5252 		}
5253 		if (count_ht && rate)
5254 			return -EINVAL;
5255 	}
5256 
5257 	count_vht = 0;
5258 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5259 		if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
5260 			return -EINVAL;
5261 		} else if (beacon_rate->control[band].vht_mcs[i]) {
5262 			count_vht++;
5263 			if (count_vht > 1)
5264 				return -EINVAL;
5265 		}
5266 		if (count_vht && rate)
5267 			return -EINVAL;
5268 	}
5269 
5270 	count_he = 0;
5271 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5272 		if (hweight16(beacon_rate->control[band].he_mcs[i]) > 1) {
5273 			return -EINVAL;
5274 		} else if (beacon_rate->control[band].he_mcs[i]) {
5275 			count_he++;
5276 			if (count_he > 1)
5277 				return -EINVAL;
5278 		}
5279 		if (count_he && rate)
5280 			return -EINVAL;
5281 	}
5282 
5283 	if ((count_ht && count_vht && count_he) ||
5284 	    (!rate && !count_ht && !count_vht && !count_he))
5285 		return -EINVAL;
5286 
5287 	if (rate &&
5288 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5289 				     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
5290 		return -EINVAL;
5291 	if (count_ht &&
5292 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5293 				     NL80211_EXT_FEATURE_BEACON_RATE_HT))
5294 		return -EINVAL;
5295 	if (count_vht &&
5296 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5297 				     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
5298 		return -EINVAL;
5299 	if (count_he &&
5300 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5301 				     NL80211_EXT_FEATURE_BEACON_RATE_HE))
5302 		return -EINVAL;
5303 
5304 	return 0;
5305 }
5306 
5307 static int nl80211_parse_mbssid_config(struct wiphy *wiphy,
5308 				       struct net_device *dev,
5309 				       struct nlattr *attrs,
5310 				       struct cfg80211_mbssid_config *config,
5311 				       u8 num_elems)
5312 {
5313 	struct nlattr *tb[NL80211_MBSSID_CONFIG_ATTR_MAX + 1];
5314 
5315 	if (!wiphy->mbssid_max_interfaces)
5316 		return -EOPNOTSUPP;
5317 
5318 	if (nla_parse_nested(tb, NL80211_MBSSID_CONFIG_ATTR_MAX, attrs, NULL,
5319 			     NULL) ||
5320 	    !tb[NL80211_MBSSID_CONFIG_ATTR_INDEX])
5321 		return -EINVAL;
5322 
5323 	config->ema = nla_get_flag(tb[NL80211_MBSSID_CONFIG_ATTR_EMA]);
5324 	if (config->ema) {
5325 		if (!wiphy->ema_max_profile_periodicity)
5326 			return -EOPNOTSUPP;
5327 
5328 		if (num_elems > wiphy->ema_max_profile_periodicity)
5329 			return -EINVAL;
5330 	}
5331 
5332 	config->index = nla_get_u8(tb[NL80211_MBSSID_CONFIG_ATTR_INDEX]);
5333 	if (config->index >= wiphy->mbssid_max_interfaces ||
5334 	    (!config->index && !num_elems))
5335 		return -EINVAL;
5336 
5337 	if (tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]) {
5338 		u32 tx_ifindex =
5339 			nla_get_u32(tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]);
5340 
5341 		if ((!config->index && tx_ifindex != dev->ifindex) ||
5342 		    (config->index && tx_ifindex == dev->ifindex))
5343 			return -EINVAL;
5344 
5345 		if (tx_ifindex != dev->ifindex) {
5346 			struct net_device *tx_netdev =
5347 				dev_get_by_index(wiphy_net(wiphy), tx_ifindex);
5348 
5349 			if (!tx_netdev || !tx_netdev->ieee80211_ptr ||
5350 			    tx_netdev->ieee80211_ptr->wiphy != wiphy ||
5351 			    tx_netdev->ieee80211_ptr->iftype !=
5352 							NL80211_IFTYPE_AP) {
5353 				dev_put(tx_netdev);
5354 				return -EINVAL;
5355 			}
5356 
5357 			config->tx_wdev = tx_netdev->ieee80211_ptr;
5358 		} else {
5359 			config->tx_wdev = dev->ieee80211_ptr;
5360 		}
5361 	} else if (!config->index) {
5362 		config->tx_wdev = dev->ieee80211_ptr;
5363 	} else {
5364 		return -EINVAL;
5365 	}
5366 
5367 	return 0;
5368 }
5369 
5370 static struct cfg80211_mbssid_elems *
5371 nl80211_parse_mbssid_elems(struct wiphy *wiphy, struct nlattr *attrs)
5372 {
5373 	struct nlattr *nl_elems;
5374 	struct cfg80211_mbssid_elems *elems;
5375 	int rem_elems;
5376 	u8 i = 0, num_elems = 0;
5377 
5378 	if (!wiphy->mbssid_max_interfaces)
5379 		return ERR_PTR(-EINVAL);
5380 
5381 	nla_for_each_nested(nl_elems, attrs, rem_elems)
5382 		num_elems++;
5383 
5384 	elems = kzalloc(struct_size(elems, elem, num_elems), GFP_KERNEL);
5385 	if (!elems)
5386 		return ERR_PTR(-ENOMEM);
5387 
5388 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5389 		elems->elem[i].data = nla_data(nl_elems);
5390 		elems->elem[i].len = nla_len(nl_elems);
5391 		i++;
5392 	}
5393 	elems->cnt = num_elems;
5394 	return elems;
5395 }
5396 
5397 static int nl80211_parse_he_bss_color(struct nlattr *attrs,
5398 				      struct cfg80211_he_bss_color *he_bss_color)
5399 {
5400 	struct nlattr *tb[NL80211_HE_BSS_COLOR_ATTR_MAX + 1];
5401 	int err;
5402 
5403 	err = nla_parse_nested(tb, NL80211_HE_BSS_COLOR_ATTR_MAX, attrs,
5404 			       he_bss_color_policy, NULL);
5405 	if (err)
5406 		return err;
5407 
5408 	if (!tb[NL80211_HE_BSS_COLOR_ATTR_COLOR])
5409 		return -EINVAL;
5410 
5411 	he_bss_color->color =
5412 		nla_get_u8(tb[NL80211_HE_BSS_COLOR_ATTR_COLOR]);
5413 	he_bss_color->enabled =
5414 		!nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_DISABLED]);
5415 	he_bss_color->partial =
5416 		nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_PARTIAL]);
5417 
5418 	return 0;
5419 }
5420 
5421 static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
5422 				struct nlattr *attrs[],
5423 				struct cfg80211_beacon_data *bcn)
5424 {
5425 	bool haveinfo = false;
5426 	int err;
5427 
5428 	memset(bcn, 0, sizeof(*bcn));
5429 
5430 	bcn->link_id = nl80211_link_id(attrs);
5431 
5432 	if (attrs[NL80211_ATTR_BEACON_HEAD]) {
5433 		bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
5434 		bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
5435 		if (!bcn->head_len)
5436 			return -EINVAL;
5437 		haveinfo = true;
5438 	}
5439 
5440 	if (attrs[NL80211_ATTR_BEACON_TAIL]) {
5441 		bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
5442 		bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
5443 		haveinfo = true;
5444 	}
5445 
5446 	if (!haveinfo)
5447 		return -EINVAL;
5448 
5449 	if (attrs[NL80211_ATTR_IE]) {
5450 		bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
5451 		bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
5452 	}
5453 
5454 	if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
5455 		bcn->proberesp_ies =
5456 			nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5457 		bcn->proberesp_ies_len =
5458 			nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5459 	}
5460 
5461 	if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
5462 		bcn->assocresp_ies =
5463 			nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5464 		bcn->assocresp_ies_len =
5465 			nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5466 	}
5467 
5468 	if (attrs[NL80211_ATTR_PROBE_RESP]) {
5469 		bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
5470 		bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
5471 	}
5472 
5473 	if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
5474 		struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
5475 
5476 		err = nla_parse_nested_deprecated(tb,
5477 						  NL80211_FTM_RESP_ATTR_MAX,
5478 						  attrs[NL80211_ATTR_FTM_RESPONDER],
5479 						  NULL, NULL);
5480 		if (err)
5481 			return err;
5482 
5483 		if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
5484 		    wiphy_ext_feature_isset(&rdev->wiphy,
5485 					    NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
5486 			bcn->ftm_responder = 1;
5487 		else
5488 			return -EOPNOTSUPP;
5489 
5490 		if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
5491 			bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
5492 			bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
5493 		}
5494 
5495 		if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
5496 			bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5497 			bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5498 		}
5499 	} else {
5500 		bcn->ftm_responder = -1;
5501 	}
5502 
5503 	if (attrs[NL80211_ATTR_HE_BSS_COLOR]) {
5504 		err = nl80211_parse_he_bss_color(attrs[NL80211_ATTR_HE_BSS_COLOR],
5505 						 &bcn->he_bss_color);
5506 		if (err)
5507 			return err;
5508 		bcn->he_bss_color_valid = true;
5509 	}
5510 
5511 	if (attrs[NL80211_ATTR_MBSSID_ELEMS]) {
5512 		struct cfg80211_mbssid_elems *mbssid =
5513 			nl80211_parse_mbssid_elems(&rdev->wiphy,
5514 						   attrs[NL80211_ATTR_MBSSID_ELEMS]);
5515 
5516 		if (IS_ERR(mbssid))
5517 			return PTR_ERR(mbssid);
5518 
5519 		bcn->mbssid_ies = mbssid;
5520 	}
5521 
5522 	return 0;
5523 }
5524 
5525 static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
5526 				    struct ieee80211_he_obss_pd *he_obss_pd)
5527 {
5528 	struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
5529 	int err;
5530 
5531 	err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
5532 			       he_obss_pd_policy, NULL);
5533 	if (err)
5534 		return err;
5535 
5536 	if (!tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL])
5537 		return -EINVAL;
5538 
5539 	he_obss_pd->sr_ctrl = nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL]);
5540 
5541 	if (tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET])
5542 		he_obss_pd->min_offset =
5543 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
5544 	if (tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
5545 		he_obss_pd->max_offset =
5546 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
5547 	if (tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET])
5548 		he_obss_pd->non_srg_max_offset =
5549 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET]);
5550 
5551 	if (he_obss_pd->min_offset > he_obss_pd->max_offset)
5552 		return -EINVAL;
5553 
5554 	if (tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP])
5555 		memcpy(he_obss_pd->bss_color_bitmap,
5556 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP]),
5557 		       sizeof(he_obss_pd->bss_color_bitmap));
5558 
5559 	if (tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP])
5560 		memcpy(he_obss_pd->partial_bssid_bitmap,
5561 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP]),
5562 		       sizeof(he_obss_pd->partial_bssid_bitmap));
5563 
5564 	he_obss_pd->enable = true;
5565 
5566 	return 0;
5567 }
5568 
5569 static int nl80211_parse_fils_discovery(struct cfg80211_registered_device *rdev,
5570 					struct nlattr *attrs,
5571 					struct cfg80211_ap_settings *params)
5572 {
5573 	struct nlattr *tb[NL80211_FILS_DISCOVERY_ATTR_MAX + 1];
5574 	int ret;
5575 	struct cfg80211_fils_discovery *fd = &params->fils_discovery;
5576 
5577 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5578 				     NL80211_EXT_FEATURE_FILS_DISCOVERY))
5579 		return -EINVAL;
5580 
5581 	ret = nla_parse_nested(tb, NL80211_FILS_DISCOVERY_ATTR_MAX, attrs,
5582 			       NULL, NULL);
5583 	if (ret)
5584 		return ret;
5585 
5586 	if (!tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] ||
5587 	    !tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] ||
5588 	    !tb[NL80211_FILS_DISCOVERY_ATTR_TMPL])
5589 		return -EINVAL;
5590 
5591 	fd->tmpl_len = nla_len(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5592 	fd->tmpl = nla_data(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5593 	fd->min_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN]);
5594 	fd->max_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX]);
5595 
5596 	return 0;
5597 }
5598 
5599 static int
5600 nl80211_parse_unsol_bcast_probe_resp(struct cfg80211_registered_device *rdev,
5601 				     struct nlattr *attrs,
5602 				     struct cfg80211_ap_settings *params)
5603 {
5604 	struct nlattr *tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1];
5605 	int ret;
5606 	struct cfg80211_unsol_bcast_probe_resp *presp =
5607 					&params->unsol_bcast_probe_resp;
5608 
5609 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5610 				     NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP))
5611 		return -EINVAL;
5612 
5613 	ret = nla_parse_nested(tb, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX,
5614 			       attrs, NULL, NULL);
5615 	if (ret)
5616 		return ret;
5617 
5618 	if (!tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] ||
5619 	    !tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL])
5620 		return -EINVAL;
5621 
5622 	presp->tmpl = nla_data(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5623 	presp->tmpl_len = nla_len(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5624 	presp->interval = nla_get_u32(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT]);
5625 	return 0;
5626 }
5627 
5628 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
5629 					    const struct element *rates)
5630 {
5631 	int i;
5632 
5633 	if (!rates)
5634 		return;
5635 
5636 	for (i = 0; i < rates->datalen; i++) {
5637 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
5638 			params->ht_required = true;
5639 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
5640 			params->vht_required = true;
5641 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HE_PHY)
5642 			params->he_required = true;
5643 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_SAE_H2E)
5644 			params->sae_h2e_required = true;
5645 	}
5646 }
5647 
5648 /*
5649  * Since the nl80211 API didn't include, from the beginning, attributes about
5650  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
5651  * benefit of drivers that rebuild IEs in the firmware.
5652  */
5653 static int nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
5654 {
5655 	const struct cfg80211_beacon_data *bcn = &params->beacon;
5656 	size_t ies_len = bcn->tail_len;
5657 	const u8 *ies = bcn->tail;
5658 	const struct element *rates;
5659 	const struct element *cap;
5660 
5661 	rates = cfg80211_find_elem(WLAN_EID_SUPP_RATES, ies, ies_len);
5662 	nl80211_check_ap_rate_selectors(params, rates);
5663 
5664 	rates = cfg80211_find_elem(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
5665 	nl80211_check_ap_rate_selectors(params, rates);
5666 
5667 	cap = cfg80211_find_elem(WLAN_EID_HT_CAPABILITY, ies, ies_len);
5668 	if (cap && cap->datalen >= sizeof(*params->ht_cap))
5669 		params->ht_cap = (void *)cap->data;
5670 	cap = cfg80211_find_elem(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
5671 	if (cap && cap->datalen >= sizeof(*params->vht_cap))
5672 		params->vht_cap = (void *)cap->data;
5673 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
5674 	if (cap && cap->datalen >= sizeof(*params->he_cap) + 1)
5675 		params->he_cap = (void *)(cap->data + 1);
5676 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ies, ies_len);
5677 	if (cap && cap->datalen >= sizeof(*params->he_oper) + 1)
5678 		params->he_oper = (void *)(cap->data + 1);
5679 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_CAPABILITY, ies, ies_len);
5680 	if (cap) {
5681 		if (!cap->datalen)
5682 			return -EINVAL;
5683 		params->eht_cap = (void *)(cap->data + 1);
5684 		if (!ieee80211_eht_capa_size_ok((const u8 *)params->he_cap,
5685 						(const u8 *)params->eht_cap,
5686 						cap->datalen - 1, true))
5687 			return -EINVAL;
5688 	}
5689 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_OPERATION, ies, ies_len);
5690 	if (cap) {
5691 		if (!cap->datalen)
5692 			return -EINVAL;
5693 		params->eht_oper = (void *)(cap->data + 1);
5694 		if (!ieee80211_eht_oper_size_ok((const u8 *)params->eht_oper,
5695 						cap->datalen - 1))
5696 			return -EINVAL;
5697 	}
5698 	return 0;
5699 }
5700 
5701 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
5702 				   struct cfg80211_ap_settings *params)
5703 {
5704 	struct wireless_dev *wdev;
5705 
5706 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
5707 		if (wdev->iftype != NL80211_IFTYPE_AP &&
5708 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)
5709 			continue;
5710 
5711 		if (!wdev->u.ap.preset_chandef.chan)
5712 			continue;
5713 
5714 		params->chandef = wdev->u.ap.preset_chandef;
5715 		return true;
5716 	}
5717 
5718 	return false;
5719 }
5720 
5721 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
5722 				    enum nl80211_auth_type auth_type,
5723 				    enum nl80211_commands cmd)
5724 {
5725 	if (auth_type > NL80211_AUTHTYPE_MAX)
5726 		return false;
5727 
5728 	switch (cmd) {
5729 	case NL80211_CMD_AUTHENTICATE:
5730 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5731 		    auth_type == NL80211_AUTHTYPE_SAE)
5732 			return false;
5733 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5734 					     NL80211_EXT_FEATURE_FILS_STA) &&
5735 		    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5736 		     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5737 		     auth_type == NL80211_AUTHTYPE_FILS_PK))
5738 			return false;
5739 		return true;
5740 	case NL80211_CMD_CONNECT:
5741 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5742 		    !wiphy_ext_feature_isset(&rdev->wiphy,
5743 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
5744 		    auth_type == NL80211_AUTHTYPE_SAE)
5745 			return false;
5746 
5747 		/* FILS with SK PFS or PK not supported yet */
5748 		if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5749 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5750 			return false;
5751 		if (!wiphy_ext_feature_isset(
5752 			    &rdev->wiphy,
5753 			    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
5754 		    auth_type == NL80211_AUTHTYPE_FILS_SK)
5755 			return false;
5756 		return true;
5757 	case NL80211_CMD_START_AP:
5758 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5759 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP) &&
5760 		    auth_type == NL80211_AUTHTYPE_SAE)
5761 			return false;
5762 		/* FILS not supported yet */
5763 		if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5764 		    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5765 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5766 			return false;
5767 		return true;
5768 	default:
5769 		return false;
5770 	}
5771 }
5772 
5773 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
5774 {
5775 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5776 	unsigned int link_id = nl80211_link_id(info->attrs);
5777 	struct net_device *dev = info->user_ptr[1];
5778 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5779 	struct cfg80211_ap_settings *params;
5780 	int err;
5781 
5782 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5783 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5784 		return -EOPNOTSUPP;
5785 
5786 	if (!rdev->ops->start_ap)
5787 		return -EOPNOTSUPP;
5788 
5789 	if (wdev->links[link_id].ap.beacon_interval)
5790 		return -EALREADY;
5791 
5792 	/* these are required for START_AP */
5793 	if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
5794 	    !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
5795 	    !info->attrs[NL80211_ATTR_BEACON_HEAD])
5796 		return -EINVAL;
5797 
5798 	params = kzalloc(sizeof(*params), GFP_KERNEL);
5799 	if (!params)
5800 		return -ENOMEM;
5801 
5802 	err = nl80211_parse_beacon(rdev, info->attrs, &params->beacon);
5803 	if (err)
5804 		goto out;
5805 
5806 	params->beacon_interval =
5807 		nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5808 	params->dtim_period =
5809 		nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
5810 
5811 	err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
5812 					   params->beacon_interval);
5813 	if (err)
5814 		goto out;
5815 
5816 	/*
5817 	 * In theory, some of these attributes should be required here
5818 	 * but since they were not used when the command was originally
5819 	 * added, keep them optional for old user space programs to let
5820 	 * them continue to work with drivers that do not need the
5821 	 * additional information -- drivers must check!
5822 	 */
5823 	if (info->attrs[NL80211_ATTR_SSID]) {
5824 		params->ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5825 		params->ssid_len =
5826 			nla_len(info->attrs[NL80211_ATTR_SSID]);
5827 		if (params->ssid_len == 0) {
5828 			err = -EINVAL;
5829 			goto out;
5830 		}
5831 
5832 		if (wdev->u.ap.ssid_len &&
5833 		    (wdev->u.ap.ssid_len != params->ssid_len ||
5834 		     memcmp(wdev->u.ap.ssid, params->ssid, params->ssid_len))) {
5835 			/* require identical SSID for MLO */
5836 			err = -EINVAL;
5837 			goto out;
5838 		}
5839 	} else if (wdev->valid_links) {
5840 		/* require SSID for MLO */
5841 		err = -EINVAL;
5842 		goto out;
5843 	}
5844 
5845 	if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
5846 		params->hidden_ssid = nla_get_u32(
5847 			info->attrs[NL80211_ATTR_HIDDEN_SSID]);
5848 
5849 	params->privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
5850 
5851 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5852 		params->auth_type = nla_get_u32(
5853 			info->attrs[NL80211_ATTR_AUTH_TYPE]);
5854 		if (!nl80211_valid_auth_type(rdev, params->auth_type,
5855 					     NL80211_CMD_START_AP)) {
5856 			err = -EINVAL;
5857 			goto out;
5858 		}
5859 	} else
5860 		params->auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5861 
5862 	err = nl80211_crypto_settings(rdev, info, &params->crypto,
5863 				      NL80211_MAX_NR_CIPHER_SUITES);
5864 	if (err)
5865 		goto out;
5866 
5867 	if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
5868 		if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER)) {
5869 			err = -EOPNOTSUPP;
5870 			goto out;
5871 		}
5872 		params->inactivity_timeout = nla_get_u16(
5873 			info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
5874 	}
5875 
5876 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
5877 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
5878 			err = -EINVAL;
5879 			goto out;
5880 		}
5881 		params->p2p_ctwindow =
5882 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
5883 		if (params->p2p_ctwindow != 0 &&
5884 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN)) {
5885 			err = -EINVAL;
5886 			goto out;
5887 		}
5888 	}
5889 
5890 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
5891 		u8 tmp;
5892 
5893 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
5894 			err = -EINVAL;
5895 			goto out;
5896 		}
5897 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
5898 		params->p2p_opp_ps = tmp;
5899 		if (params->p2p_opp_ps != 0 &&
5900 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS)) {
5901 			err = -EINVAL;
5902 			goto out;
5903 		}
5904 	}
5905 
5906 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5907 		err = nl80211_parse_chandef(rdev, info, &params->chandef);
5908 		if (err)
5909 			goto out;
5910 	} else if (wdev->valid_links) {
5911 		/* with MLD need to specify the channel configuration */
5912 		err = -EINVAL;
5913 		goto out;
5914 	} else if (wdev->u.ap.preset_chandef.chan) {
5915 		params->chandef = wdev->u.ap.preset_chandef;
5916 	} else if (!nl80211_get_ap_channel(rdev, params)) {
5917 		err = -EINVAL;
5918 		goto out;
5919 	}
5920 
5921 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params->chandef,
5922 					   wdev->iftype)) {
5923 		err = -EINVAL;
5924 		goto out;
5925 	}
5926 
5927 	wdev_lock(wdev);
5928 
5929 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
5930 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
5931 						    NL80211_ATTR_TX_RATES,
5932 						    &params->beacon_rate,
5933 						    dev, false, link_id);
5934 		if (err)
5935 			goto out_unlock;
5936 
5937 		err = validate_beacon_tx_rate(rdev, params->chandef.chan->band,
5938 					      &params->beacon_rate);
5939 		if (err)
5940 			goto out_unlock;
5941 	}
5942 
5943 	if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
5944 		params->smps_mode =
5945 			nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
5946 		switch (params->smps_mode) {
5947 		case NL80211_SMPS_OFF:
5948 			break;
5949 		case NL80211_SMPS_STATIC:
5950 			if (!(rdev->wiphy.features &
5951 			      NL80211_FEATURE_STATIC_SMPS)) {
5952 				err = -EINVAL;
5953 				goto out_unlock;
5954 			}
5955 			break;
5956 		case NL80211_SMPS_DYNAMIC:
5957 			if (!(rdev->wiphy.features &
5958 			      NL80211_FEATURE_DYNAMIC_SMPS)) {
5959 				err = -EINVAL;
5960 				goto out_unlock;
5961 			}
5962 			break;
5963 		default:
5964 			err = -EINVAL;
5965 			goto out_unlock;
5966 		}
5967 	} else {
5968 		params->smps_mode = NL80211_SMPS_OFF;
5969 	}
5970 
5971 	params->pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
5972 	if (params->pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
5973 		err = -EOPNOTSUPP;
5974 		goto out_unlock;
5975 	}
5976 
5977 	if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
5978 		params->acl = parse_acl_data(&rdev->wiphy, info);
5979 		if (IS_ERR(params->acl)) {
5980 			err = PTR_ERR(params->acl);
5981 			params->acl = NULL;
5982 			goto out_unlock;
5983 		}
5984 	}
5985 
5986 	params->twt_responder =
5987 		    nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
5988 
5989 	if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
5990 		err = nl80211_parse_he_obss_pd(
5991 					info->attrs[NL80211_ATTR_HE_OBSS_PD],
5992 					&params->he_obss_pd);
5993 		if (err)
5994 			goto out_unlock;
5995 	}
5996 
5997 	if (info->attrs[NL80211_ATTR_FILS_DISCOVERY]) {
5998 		err = nl80211_parse_fils_discovery(rdev,
5999 						   info->attrs[NL80211_ATTR_FILS_DISCOVERY],
6000 						   params);
6001 		if (err)
6002 			goto out_unlock;
6003 	}
6004 
6005 	if (info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP]) {
6006 		err = nl80211_parse_unsol_bcast_probe_resp(
6007 			rdev, info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP],
6008 			params);
6009 		if (err)
6010 			goto out_unlock;
6011 	}
6012 
6013 	if (info->attrs[NL80211_ATTR_MBSSID_CONFIG]) {
6014 		err = nl80211_parse_mbssid_config(&rdev->wiphy, dev,
6015 						  info->attrs[NL80211_ATTR_MBSSID_CONFIG],
6016 						  &params->mbssid_config,
6017 						  params->beacon.mbssid_ies ?
6018 							params->beacon.mbssid_ies->cnt :
6019 							0);
6020 		if (err)
6021 			goto out_unlock;
6022 	}
6023 
6024 	err = nl80211_calculate_ap_params(params);
6025 	if (err)
6026 		goto out_unlock;
6027 
6028 	if (info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS])
6029 		params->flags = nla_get_u32(
6030 			info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS]);
6031 	else if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
6032 		params->flags |= NL80211_AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
6033 
6034 	if (wdev->conn_owner_nlportid &&
6035 	    info->attrs[NL80211_ATTR_SOCKET_OWNER] &&
6036 	    wdev->conn_owner_nlportid != info->snd_portid) {
6037 		err = -EINVAL;
6038 		goto out_unlock;
6039 	}
6040 
6041 	/* FIXME: validate MLO/link-id against driver capabilities */
6042 
6043 	err = rdev_start_ap(rdev, dev, params);
6044 	if (!err) {
6045 		wdev->links[link_id].ap.beacon_interval = params->beacon_interval;
6046 		wdev->links[link_id].ap.chandef = params->chandef;
6047 		wdev->u.ap.ssid_len = params->ssid_len;
6048 		memcpy(wdev->u.ap.ssid, params->ssid,
6049 		       params->ssid_len);
6050 
6051 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
6052 			wdev->conn_owner_nlportid = info->snd_portid;
6053 	}
6054 out_unlock:
6055 	wdev_unlock(wdev);
6056 out:
6057 	kfree(params->acl);
6058 	kfree(params->beacon.mbssid_ies);
6059 	if (params->mbssid_config.tx_wdev &&
6060 	    params->mbssid_config.tx_wdev->netdev &&
6061 	    params->mbssid_config.tx_wdev->netdev != dev)
6062 		dev_put(params->mbssid_config.tx_wdev->netdev);
6063 	kfree(params);
6064 
6065 	return err;
6066 }
6067 
6068 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
6069 {
6070 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6071 	unsigned int link_id = nl80211_link_id(info->attrs);
6072 	struct net_device *dev = info->user_ptr[1];
6073 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6074 	struct cfg80211_beacon_data params;
6075 	int err;
6076 
6077 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6078 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6079 		return -EOPNOTSUPP;
6080 
6081 	if (!rdev->ops->change_beacon)
6082 		return -EOPNOTSUPP;
6083 
6084 	if (!wdev->links[link_id].ap.beacon_interval)
6085 		return -EINVAL;
6086 
6087 	err = nl80211_parse_beacon(rdev, info->attrs, &params);
6088 	if (err)
6089 		goto out;
6090 
6091 	wdev_lock(wdev);
6092 	err = rdev_change_beacon(rdev, dev, &params);
6093 	wdev_unlock(wdev);
6094 
6095 out:
6096 	kfree(params.mbssid_ies);
6097 	return err;
6098 }
6099 
6100 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
6101 {
6102 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6103 	unsigned int link_id = nl80211_link_id(info->attrs);
6104 	struct net_device *dev = info->user_ptr[1];
6105 
6106 	return cfg80211_stop_ap(rdev, dev, link_id, false);
6107 }
6108 
6109 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
6110 	[NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
6111 	[NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
6112 	[NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
6113 	[NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
6114 	[NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
6115 	[NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
6116 };
6117 
6118 static int parse_station_flags(struct genl_info *info,
6119 			       enum nl80211_iftype iftype,
6120 			       struct station_parameters *params)
6121 {
6122 	struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
6123 	struct nlattr *nla;
6124 	int flag;
6125 
6126 	/*
6127 	 * Try parsing the new attribute first so userspace
6128 	 * can specify both for older kernels.
6129 	 */
6130 	nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
6131 	if (nla) {
6132 		struct nl80211_sta_flag_update *sta_flags;
6133 
6134 		sta_flags = nla_data(nla);
6135 		params->sta_flags_mask = sta_flags->mask;
6136 		params->sta_flags_set = sta_flags->set;
6137 		params->sta_flags_set &= params->sta_flags_mask;
6138 		if ((params->sta_flags_mask |
6139 		     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
6140 			return -EINVAL;
6141 		return 0;
6142 	}
6143 
6144 	/* if present, parse the old attribute */
6145 
6146 	nla = info->attrs[NL80211_ATTR_STA_FLAGS];
6147 	if (!nla)
6148 		return 0;
6149 
6150 	if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
6151 		return -EINVAL;
6152 
6153 	/*
6154 	 * Only allow certain flags for interface types so that
6155 	 * other attributes are silently ignored. Remember that
6156 	 * this is backward compatibility code with old userspace
6157 	 * and shouldn't be hit in other cases anyway.
6158 	 */
6159 	switch (iftype) {
6160 	case NL80211_IFTYPE_AP:
6161 	case NL80211_IFTYPE_AP_VLAN:
6162 	case NL80211_IFTYPE_P2P_GO:
6163 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6164 					 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6165 					 BIT(NL80211_STA_FLAG_WME) |
6166 					 BIT(NL80211_STA_FLAG_MFP);
6167 		break;
6168 	case NL80211_IFTYPE_P2P_CLIENT:
6169 	case NL80211_IFTYPE_STATION:
6170 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6171 					 BIT(NL80211_STA_FLAG_TDLS_PEER);
6172 		break;
6173 	case NL80211_IFTYPE_MESH_POINT:
6174 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6175 					 BIT(NL80211_STA_FLAG_MFP) |
6176 					 BIT(NL80211_STA_FLAG_AUTHORIZED);
6177 		break;
6178 	default:
6179 		return -EINVAL;
6180 	}
6181 
6182 	for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
6183 		if (flags[flag]) {
6184 			params->sta_flags_set |= (1<<flag);
6185 
6186 			/* no longer support new API additions in old API */
6187 			if (flag > NL80211_STA_FLAG_MAX_OLD_API)
6188 				return -EINVAL;
6189 		}
6190 	}
6191 
6192 	return 0;
6193 }
6194 
6195 bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
6196 {
6197 	struct nlattr *rate;
6198 	u32 bitrate;
6199 	u16 bitrate_compat;
6200 	enum nl80211_rate_info rate_flg;
6201 
6202 	rate = nla_nest_start_noflag(msg, attr);
6203 	if (!rate)
6204 		return false;
6205 
6206 	/* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
6207 	bitrate = cfg80211_calculate_bitrate(info);
6208 	/* report 16-bit bitrate only if we can */
6209 	bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
6210 	if (bitrate > 0 &&
6211 	    nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
6212 		return false;
6213 	if (bitrate_compat > 0 &&
6214 	    nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
6215 		return false;
6216 
6217 	switch (info->bw) {
6218 	case RATE_INFO_BW_5:
6219 		rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
6220 		break;
6221 	case RATE_INFO_BW_10:
6222 		rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
6223 		break;
6224 	default:
6225 		WARN_ON(1);
6226 		fallthrough;
6227 	case RATE_INFO_BW_20:
6228 		rate_flg = 0;
6229 		break;
6230 	case RATE_INFO_BW_40:
6231 		rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
6232 		break;
6233 	case RATE_INFO_BW_80:
6234 		rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
6235 		break;
6236 	case RATE_INFO_BW_160:
6237 		rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
6238 		break;
6239 	case RATE_INFO_BW_HE_RU:
6240 		rate_flg = 0;
6241 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
6242 		break;
6243 	case RATE_INFO_BW_320:
6244 		rate_flg = NL80211_RATE_INFO_320_MHZ_WIDTH;
6245 		break;
6246 	case RATE_INFO_BW_EHT_RU:
6247 		rate_flg = 0;
6248 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_EHT_MCS));
6249 		break;
6250 	}
6251 
6252 	if (rate_flg && nla_put_flag(msg, rate_flg))
6253 		return false;
6254 
6255 	if (info->flags & RATE_INFO_FLAGS_MCS) {
6256 		if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
6257 			return false;
6258 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6259 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6260 			return false;
6261 	} else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
6262 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
6263 			return false;
6264 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
6265 			return false;
6266 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6267 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6268 			return false;
6269 	} else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
6270 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
6271 			return false;
6272 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
6273 			return false;
6274 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
6275 			return false;
6276 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
6277 			return false;
6278 		if (info->bw == RATE_INFO_BW_HE_RU &&
6279 		    nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
6280 			       info->he_ru_alloc))
6281 			return false;
6282 	} else if (info->flags & RATE_INFO_FLAGS_EHT_MCS) {
6283 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_MCS, info->mcs))
6284 			return false;
6285 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_NSS, info->nss))
6286 			return false;
6287 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_GI, info->eht_gi))
6288 			return false;
6289 		if (info->bw == RATE_INFO_BW_EHT_RU &&
6290 		    nla_put_u8(msg, NL80211_RATE_INFO_EHT_RU_ALLOC,
6291 			       info->eht_ru_alloc))
6292 			return false;
6293 	}
6294 
6295 	nla_nest_end(msg, rate);
6296 	return true;
6297 }
6298 
6299 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
6300 			       int id)
6301 {
6302 	void *attr;
6303 	int i = 0;
6304 
6305 	if (!mask)
6306 		return true;
6307 
6308 	attr = nla_nest_start_noflag(msg, id);
6309 	if (!attr)
6310 		return false;
6311 
6312 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
6313 		if (!(mask & BIT(i)))
6314 			continue;
6315 
6316 		if (nla_put_u8(msg, i, signal[i]))
6317 			return false;
6318 	}
6319 
6320 	nla_nest_end(msg, attr);
6321 
6322 	return true;
6323 }
6324 
6325 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
6326 				u32 seq, int flags,
6327 				struct cfg80211_registered_device *rdev,
6328 				struct net_device *dev,
6329 				const u8 *mac_addr, struct station_info *sinfo)
6330 {
6331 	void *hdr;
6332 	struct nlattr *sinfoattr, *bss_param;
6333 
6334 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
6335 	if (!hdr) {
6336 		cfg80211_sinfo_release_content(sinfo);
6337 		return -1;
6338 	}
6339 
6340 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6341 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
6342 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
6343 		goto nla_put_failure;
6344 
6345 	sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
6346 	if (!sinfoattr)
6347 		goto nla_put_failure;
6348 
6349 #define PUT_SINFO(attr, memb, type) do {				\
6350 	BUILD_BUG_ON(sizeof(type) == sizeof(u64));			\
6351 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
6352 	    nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,		\
6353 			     sinfo->memb))				\
6354 		goto nla_put_failure;					\
6355 	} while (0)
6356 #define PUT_SINFO_U64(attr, memb) do {					\
6357 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
6358 	    nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,		\
6359 			      sinfo->memb, NL80211_STA_INFO_PAD))	\
6360 		goto nla_put_failure;					\
6361 	} while (0)
6362 
6363 	PUT_SINFO(CONNECTED_TIME, connected_time, u32);
6364 	PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
6365 	PUT_SINFO_U64(ASSOC_AT_BOOTTIME, assoc_at);
6366 
6367 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
6368 			     BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
6369 	    nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
6370 			(u32)sinfo->rx_bytes))
6371 		goto nla_put_failure;
6372 
6373 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
6374 			     BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
6375 	    nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
6376 			(u32)sinfo->tx_bytes))
6377 		goto nla_put_failure;
6378 
6379 	PUT_SINFO_U64(RX_BYTES64, rx_bytes);
6380 	PUT_SINFO_U64(TX_BYTES64, tx_bytes);
6381 	PUT_SINFO(LLID, llid, u16);
6382 	PUT_SINFO(PLID, plid, u16);
6383 	PUT_SINFO(PLINK_STATE, plink_state, u8);
6384 	PUT_SINFO_U64(RX_DURATION, rx_duration);
6385 	PUT_SINFO_U64(TX_DURATION, tx_duration);
6386 
6387 	if (wiphy_ext_feature_isset(&rdev->wiphy,
6388 				    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
6389 		PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
6390 
6391 	switch (rdev->wiphy.signal_type) {
6392 	case CFG80211_SIGNAL_TYPE_MBM:
6393 		PUT_SINFO(SIGNAL, signal, u8);
6394 		PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
6395 		break;
6396 	default:
6397 		break;
6398 	}
6399 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
6400 		if (!nl80211_put_signal(msg, sinfo->chains,
6401 					sinfo->chain_signal,
6402 					NL80211_STA_INFO_CHAIN_SIGNAL))
6403 			goto nla_put_failure;
6404 	}
6405 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
6406 		if (!nl80211_put_signal(msg, sinfo->chains,
6407 					sinfo->chain_signal_avg,
6408 					NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
6409 			goto nla_put_failure;
6410 	}
6411 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
6412 		if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
6413 					  NL80211_STA_INFO_TX_BITRATE))
6414 			goto nla_put_failure;
6415 	}
6416 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
6417 		if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
6418 					  NL80211_STA_INFO_RX_BITRATE))
6419 			goto nla_put_failure;
6420 	}
6421 
6422 	PUT_SINFO(RX_PACKETS, rx_packets, u32);
6423 	PUT_SINFO(TX_PACKETS, tx_packets, u32);
6424 	PUT_SINFO(TX_RETRIES, tx_retries, u32);
6425 	PUT_SINFO(TX_FAILED, tx_failed, u32);
6426 	PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
6427 	PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
6428 	PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
6429 	PUT_SINFO(LOCAL_PM, local_pm, u32);
6430 	PUT_SINFO(PEER_PM, peer_pm, u32);
6431 	PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
6432 	PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
6433 	PUT_SINFO(CONNECTED_TO_AS, connected_to_as, u8);
6434 
6435 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
6436 		bss_param = nla_nest_start_noflag(msg,
6437 						  NL80211_STA_INFO_BSS_PARAM);
6438 		if (!bss_param)
6439 			goto nla_put_failure;
6440 
6441 		if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
6442 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
6443 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
6444 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
6445 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
6446 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
6447 		    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
6448 			       sinfo->bss_param.dtim_period) ||
6449 		    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
6450 				sinfo->bss_param.beacon_interval))
6451 			goto nla_put_failure;
6452 
6453 		nla_nest_end(msg, bss_param);
6454 	}
6455 	if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
6456 	    nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
6457 		    sizeof(struct nl80211_sta_flag_update),
6458 		    &sinfo->sta_flags))
6459 		goto nla_put_failure;
6460 
6461 	PUT_SINFO_U64(T_OFFSET, t_offset);
6462 	PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
6463 	PUT_SINFO_U64(BEACON_RX, rx_beacon);
6464 	PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
6465 	PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
6466 	PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
6467 	if (wiphy_ext_feature_isset(&rdev->wiphy,
6468 				    NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
6469 		PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
6470 		PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
6471 	}
6472 
6473 #undef PUT_SINFO
6474 #undef PUT_SINFO_U64
6475 
6476 	if (sinfo->pertid) {
6477 		struct nlattr *tidsattr;
6478 		int tid;
6479 
6480 		tidsattr = nla_nest_start_noflag(msg,
6481 						 NL80211_STA_INFO_TID_STATS);
6482 		if (!tidsattr)
6483 			goto nla_put_failure;
6484 
6485 		for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
6486 			struct cfg80211_tid_stats *tidstats;
6487 			struct nlattr *tidattr;
6488 
6489 			tidstats = &sinfo->pertid[tid];
6490 
6491 			if (!tidstats->filled)
6492 				continue;
6493 
6494 			tidattr = nla_nest_start_noflag(msg, tid + 1);
6495 			if (!tidattr)
6496 				goto nla_put_failure;
6497 
6498 #define PUT_TIDVAL_U64(attr, memb) do {					\
6499 	if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&	\
6500 	    nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,		\
6501 			      tidstats->memb, NL80211_TID_STATS_PAD))	\
6502 		goto nla_put_failure;					\
6503 	} while (0)
6504 
6505 			PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
6506 			PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
6507 			PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
6508 			PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
6509 
6510 #undef PUT_TIDVAL_U64
6511 			if ((tidstats->filled &
6512 			     BIT(NL80211_TID_STATS_TXQ_STATS)) &&
6513 			    !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
6514 						   NL80211_TID_STATS_TXQ_STATS))
6515 				goto nla_put_failure;
6516 
6517 			nla_nest_end(msg, tidattr);
6518 		}
6519 
6520 		nla_nest_end(msg, tidsattr);
6521 	}
6522 
6523 	nla_nest_end(msg, sinfoattr);
6524 
6525 	if (sinfo->assoc_req_ies_len &&
6526 	    nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
6527 		    sinfo->assoc_req_ies))
6528 		goto nla_put_failure;
6529 
6530 	cfg80211_sinfo_release_content(sinfo);
6531 	genlmsg_end(msg, hdr);
6532 	return 0;
6533 
6534  nla_put_failure:
6535 	cfg80211_sinfo_release_content(sinfo);
6536 	genlmsg_cancel(msg, hdr);
6537 	return -EMSGSIZE;
6538 }
6539 
6540 static int nl80211_dump_station(struct sk_buff *skb,
6541 				struct netlink_callback *cb)
6542 {
6543 	struct station_info sinfo;
6544 	struct cfg80211_registered_device *rdev;
6545 	struct wireless_dev *wdev;
6546 	u8 mac_addr[ETH_ALEN];
6547 	int sta_idx = cb->args[2];
6548 	int err;
6549 
6550 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
6551 	if (err)
6552 		return err;
6553 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
6554 	__acquire(&rdev->wiphy.mtx);
6555 
6556 	if (!wdev->netdev) {
6557 		err = -EINVAL;
6558 		goto out_err;
6559 	}
6560 
6561 	if (!rdev->ops->dump_station) {
6562 		err = -EOPNOTSUPP;
6563 		goto out_err;
6564 	}
6565 
6566 	while (1) {
6567 		memset(&sinfo, 0, sizeof(sinfo));
6568 		err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
6569 					mac_addr, &sinfo);
6570 		if (err == -ENOENT)
6571 			break;
6572 		if (err)
6573 			goto out_err;
6574 
6575 		if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
6576 				NETLINK_CB(cb->skb).portid,
6577 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
6578 				rdev, wdev->netdev, mac_addr,
6579 				&sinfo) < 0)
6580 			goto out;
6581 
6582 		sta_idx++;
6583 	}
6584 
6585  out:
6586 	cb->args[2] = sta_idx;
6587 	err = skb->len;
6588  out_err:
6589 	wiphy_unlock(&rdev->wiphy);
6590 
6591 	return err;
6592 }
6593 
6594 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
6595 {
6596 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6597 	struct net_device *dev = info->user_ptr[1];
6598 	struct station_info sinfo;
6599 	struct sk_buff *msg;
6600 	u8 *mac_addr = NULL;
6601 	int err;
6602 
6603 	memset(&sinfo, 0, sizeof(sinfo));
6604 
6605 	if (!info->attrs[NL80211_ATTR_MAC])
6606 		return -EINVAL;
6607 
6608 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6609 
6610 	if (!rdev->ops->get_station)
6611 		return -EOPNOTSUPP;
6612 
6613 	err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
6614 	if (err)
6615 		return err;
6616 
6617 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6618 	if (!msg) {
6619 		cfg80211_sinfo_release_content(&sinfo);
6620 		return -ENOMEM;
6621 	}
6622 
6623 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
6624 				 info->snd_portid, info->snd_seq, 0,
6625 				 rdev, dev, mac_addr, &sinfo) < 0) {
6626 		nlmsg_free(msg);
6627 		return -ENOBUFS;
6628 	}
6629 
6630 	return genlmsg_reply(msg, info);
6631 }
6632 
6633 int cfg80211_check_station_change(struct wiphy *wiphy,
6634 				  struct station_parameters *params,
6635 				  enum cfg80211_station_type statype)
6636 {
6637 	if (params->listen_interval != -1 &&
6638 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6639 		return -EINVAL;
6640 
6641 	if (params->support_p2p_ps != -1 &&
6642 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6643 		return -EINVAL;
6644 
6645 	if (params->aid &&
6646 	    !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
6647 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6648 		return -EINVAL;
6649 
6650 	/* When you run into this, adjust the code below for the new flag */
6651 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
6652 
6653 	switch (statype) {
6654 	case CFG80211_STA_MESH_PEER_KERNEL:
6655 	case CFG80211_STA_MESH_PEER_USER:
6656 		/*
6657 		 * No ignoring the TDLS flag here -- the userspace mesh
6658 		 * code doesn't have the bug of including TDLS in the
6659 		 * mask everywhere.
6660 		 */
6661 		if (params->sta_flags_mask &
6662 				~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6663 				  BIT(NL80211_STA_FLAG_MFP) |
6664 				  BIT(NL80211_STA_FLAG_AUTHORIZED)))
6665 			return -EINVAL;
6666 		break;
6667 	case CFG80211_STA_TDLS_PEER_SETUP:
6668 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6669 		if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6670 			return -EINVAL;
6671 		/* ignore since it can't change */
6672 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6673 		break;
6674 	default:
6675 		/* disallow mesh-specific things */
6676 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
6677 			return -EINVAL;
6678 		if (params->local_pm)
6679 			return -EINVAL;
6680 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6681 			return -EINVAL;
6682 	}
6683 
6684 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6685 	    statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
6686 		/* TDLS can't be set, ... */
6687 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
6688 			return -EINVAL;
6689 		/*
6690 		 * ... but don't bother the driver with it. This works around
6691 		 * a hostapd/wpa_supplicant issue -- it always includes the
6692 		 * TLDS_PEER flag in the mask even for AP mode.
6693 		 */
6694 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6695 	}
6696 
6697 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6698 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6699 		/* reject other things that can't change */
6700 		if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
6701 			return -EINVAL;
6702 		if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
6703 			return -EINVAL;
6704 		if (params->link_sta_params.supported_rates)
6705 			return -EINVAL;
6706 		if (params->ext_capab || params->link_sta_params.ht_capa ||
6707 		    params->link_sta_params.vht_capa ||
6708 		    params->link_sta_params.he_capa ||
6709 		    params->link_sta_params.eht_capa)
6710 			return -EINVAL;
6711 	}
6712 
6713 	if (statype != CFG80211_STA_AP_CLIENT &&
6714 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6715 		if (params->vlan)
6716 			return -EINVAL;
6717 	}
6718 
6719 	switch (statype) {
6720 	case CFG80211_STA_AP_MLME_CLIENT:
6721 		/* Use this only for authorizing/unauthorizing a station */
6722 		if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
6723 			return -EOPNOTSUPP;
6724 		break;
6725 	case CFG80211_STA_AP_CLIENT:
6726 	case CFG80211_STA_AP_CLIENT_UNASSOC:
6727 		/* accept only the listed bits */
6728 		if (params->sta_flags_mask &
6729 				~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6730 				  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6731 				  BIT(NL80211_STA_FLAG_ASSOCIATED) |
6732 				  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6733 				  BIT(NL80211_STA_FLAG_WME) |
6734 				  BIT(NL80211_STA_FLAG_MFP)))
6735 			return -EINVAL;
6736 
6737 		/* but authenticated/associated only if driver handles it */
6738 		if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
6739 		    params->sta_flags_mask &
6740 				(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6741 				 BIT(NL80211_STA_FLAG_ASSOCIATED)))
6742 			return -EINVAL;
6743 		break;
6744 	case CFG80211_STA_IBSS:
6745 	case CFG80211_STA_AP_STA:
6746 		/* reject any changes other than AUTHORIZED */
6747 		if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
6748 			return -EINVAL;
6749 		break;
6750 	case CFG80211_STA_TDLS_PEER_SETUP:
6751 		/* reject any changes other than AUTHORIZED or WME */
6752 		if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6753 					       BIT(NL80211_STA_FLAG_WME)))
6754 			return -EINVAL;
6755 		/* force (at least) rates when authorizing */
6756 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
6757 		    !params->link_sta_params.supported_rates)
6758 			return -EINVAL;
6759 		break;
6760 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6761 		/* reject any changes */
6762 		return -EINVAL;
6763 	case CFG80211_STA_MESH_PEER_KERNEL:
6764 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6765 			return -EINVAL;
6766 		break;
6767 	case CFG80211_STA_MESH_PEER_USER:
6768 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
6769 		    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
6770 			return -EINVAL;
6771 		break;
6772 	}
6773 
6774 	/*
6775 	 * Older kernel versions ignored this attribute entirely, so don't
6776 	 * reject attempts to update it but mark it as unused instead so the
6777 	 * driver won't look at the data.
6778 	 */
6779 	if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
6780 	    statype != CFG80211_STA_TDLS_PEER_SETUP)
6781 		params->link_sta_params.opmode_notif_used = false;
6782 
6783 	return 0;
6784 }
6785 EXPORT_SYMBOL(cfg80211_check_station_change);
6786 
6787 /*
6788  * Get vlan interface making sure it is running and on the right wiphy.
6789  */
6790 static struct net_device *get_vlan(struct genl_info *info,
6791 				   struct cfg80211_registered_device *rdev)
6792 {
6793 	struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
6794 	struct net_device *v;
6795 	int ret;
6796 
6797 	if (!vlanattr)
6798 		return NULL;
6799 
6800 	v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
6801 	if (!v)
6802 		return ERR_PTR(-ENODEV);
6803 
6804 	if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
6805 		ret = -EINVAL;
6806 		goto error;
6807 	}
6808 
6809 	if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
6810 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6811 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
6812 		ret = -EINVAL;
6813 		goto error;
6814 	}
6815 
6816 	if (!netif_running(v)) {
6817 		ret = -ENETDOWN;
6818 		goto error;
6819 	}
6820 
6821 	return v;
6822  error:
6823 	dev_put(v);
6824 	return ERR_PTR(ret);
6825 }
6826 
6827 static int nl80211_parse_sta_wme(struct genl_info *info,
6828 				 struct station_parameters *params)
6829 {
6830 	struct nlattr *tb[NL80211_STA_WME_MAX + 1];
6831 	struct nlattr *nla;
6832 	int err;
6833 
6834 	/* parse WME attributes if present */
6835 	if (!info->attrs[NL80211_ATTR_STA_WME])
6836 		return 0;
6837 
6838 	nla = info->attrs[NL80211_ATTR_STA_WME];
6839 	err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
6840 					  nl80211_sta_wme_policy,
6841 					  info->extack);
6842 	if (err)
6843 		return err;
6844 
6845 	if (tb[NL80211_STA_WME_UAPSD_QUEUES])
6846 		params->uapsd_queues = nla_get_u8(
6847 			tb[NL80211_STA_WME_UAPSD_QUEUES]);
6848 	if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
6849 		return -EINVAL;
6850 
6851 	if (tb[NL80211_STA_WME_MAX_SP])
6852 		params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
6853 
6854 	if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
6855 		return -EINVAL;
6856 
6857 	params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
6858 
6859 	return 0;
6860 }
6861 
6862 static int nl80211_parse_sta_channel_info(struct genl_info *info,
6863 				      struct station_parameters *params)
6864 {
6865 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
6866 		params->supported_channels =
6867 		     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
6868 		params->supported_channels_len =
6869 		     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
6870 		/*
6871 		 * Need to include at least one (first channel, number of
6872 		 * channels) tuple for each subband (checked in policy),
6873 		 * and must have proper tuples for the rest of the data as well.
6874 		 */
6875 		if (params->supported_channels_len % 2)
6876 			return -EINVAL;
6877 	}
6878 
6879 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
6880 		params->supported_oper_classes =
6881 		 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
6882 		params->supported_oper_classes_len =
6883 		  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
6884 	}
6885 	return 0;
6886 }
6887 
6888 static int nl80211_set_station_tdls(struct genl_info *info,
6889 				    struct station_parameters *params)
6890 {
6891 	int err;
6892 	/* Dummy STA entry gets updated once the peer capabilities are known */
6893 	if (info->attrs[NL80211_ATTR_PEER_AID])
6894 		params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
6895 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
6896 		params->link_sta_params.ht_capa =
6897 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
6898 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
6899 		params->link_sta_params.vht_capa =
6900 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
6901 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
6902 		params->link_sta_params.he_capa =
6903 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6904 		params->link_sta_params.he_capa_len =
6905 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6906 
6907 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
6908 			params->link_sta_params.eht_capa =
6909 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
6910 			params->link_sta_params.eht_capa_len =
6911 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
6912 
6913 			if (!ieee80211_eht_capa_size_ok((const u8 *)params->link_sta_params.he_capa,
6914 							(const u8 *)params->link_sta_params.eht_capa,
6915 							params->link_sta_params.eht_capa_len,
6916 							false))
6917 				return -EINVAL;
6918 		}
6919 	}
6920 
6921 	err = nl80211_parse_sta_channel_info(info, params);
6922 	if (err)
6923 		return err;
6924 
6925 	return nl80211_parse_sta_wme(info, params);
6926 }
6927 
6928 static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
6929 					     struct sta_txpwr *txpwr,
6930 					     bool *txpwr_set)
6931 {
6932 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6933 	int idx;
6934 
6935 	if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
6936 		if (!rdev->ops->set_tx_power ||
6937 		    !wiphy_ext_feature_isset(&rdev->wiphy,
6938 					 NL80211_EXT_FEATURE_STA_TX_PWR))
6939 			return -EOPNOTSUPP;
6940 
6941 		idx = NL80211_ATTR_STA_TX_POWER_SETTING;
6942 		txpwr->type = nla_get_u8(info->attrs[idx]);
6943 
6944 		if (txpwr->type == NL80211_TX_POWER_LIMITED) {
6945 			idx = NL80211_ATTR_STA_TX_POWER;
6946 
6947 			if (info->attrs[idx])
6948 				txpwr->power = nla_get_s16(info->attrs[idx]);
6949 			else
6950 				return -EINVAL;
6951 		}
6952 
6953 		*txpwr_set = true;
6954 	} else {
6955 		*txpwr_set = false;
6956 	}
6957 
6958 	return 0;
6959 }
6960 
6961 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
6962 {
6963 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6964 	struct net_device *dev = info->user_ptr[1];
6965 	struct station_parameters params;
6966 	u8 *mac_addr;
6967 	int err;
6968 
6969 	memset(&params, 0, sizeof(params));
6970 
6971 	if (!rdev->ops->change_station)
6972 		return -EOPNOTSUPP;
6973 
6974 	/*
6975 	 * AID and listen_interval properties can be set only for unassociated
6976 	 * station. Include these parameters here and will check them in
6977 	 * cfg80211_check_station_change().
6978 	 */
6979 	if (info->attrs[NL80211_ATTR_STA_AID])
6980 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
6981 
6982 	if (info->attrs[NL80211_ATTR_VLAN_ID])
6983 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
6984 
6985 	if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
6986 		params.listen_interval =
6987 		     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
6988 	else
6989 		params.listen_interval = -1;
6990 
6991 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
6992 		params.support_p2p_ps =
6993 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
6994 	else
6995 		params.support_p2p_ps = -1;
6996 
6997 	if (!info->attrs[NL80211_ATTR_MAC])
6998 		return -EINVAL;
6999 
7000 	params.link_sta_params.link_id =
7001 		nl80211_link_id_or_invalid(info->attrs);
7002 
7003 	if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7004 		/* If MLD_ADDR attribute is set then this is an MLD station
7005 		 * and the MLD_ADDR attribute holds the MLD address and the
7006 		 * MAC attribute holds for the LINK address.
7007 		 * In that case, the link_id is also expected to be valid.
7008 		 */
7009 		if (params.link_sta_params.link_id < 0)
7010 			return -EINVAL;
7011 
7012 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7013 		params.link_sta_params.mld_mac = mac_addr;
7014 		params.link_sta_params.link_mac =
7015 			nla_data(info->attrs[NL80211_ATTR_MAC]);
7016 		if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7017 			return -EINVAL;
7018 	} else {
7019 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7020 	}
7021 
7022 
7023 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
7024 		params.link_sta_params.supported_rates =
7025 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7026 		params.link_sta_params.supported_rates_len =
7027 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7028 	}
7029 
7030 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7031 		params.capability =
7032 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7033 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7034 	}
7035 
7036 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7037 		params.ext_capab =
7038 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7039 		params.ext_capab_len =
7040 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7041 	}
7042 
7043 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7044 		return -EINVAL;
7045 
7046 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7047 		params.plink_action =
7048 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7049 
7050 	if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
7051 		params.plink_state =
7052 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
7053 		if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
7054 			params.peer_aid = nla_get_u16(
7055 				info->attrs[NL80211_ATTR_MESH_PEER_AID]);
7056 		params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
7057 	}
7058 
7059 	if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
7060 		params.local_pm = nla_get_u32(
7061 			info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
7062 
7063 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7064 		params.link_sta_params.opmode_notif_used = true;
7065 		params.link_sta_params.opmode_notif =
7066 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7067 	}
7068 
7069 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7070 		params.link_sta_params.he_6ghz_capa =
7071 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7072 
7073 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7074 		params.airtime_weight =
7075 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7076 
7077 	if (params.airtime_weight &&
7078 	    !wiphy_ext_feature_isset(&rdev->wiphy,
7079 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7080 		return -EOPNOTSUPP;
7081 
7082 	err = nl80211_parse_sta_txpower_setting(info,
7083 						&params.link_sta_params.txpwr,
7084 						&params.link_sta_params.txpwr_set);
7085 	if (err)
7086 		return err;
7087 
7088 	/* Include parameters for TDLS peer (will check later) */
7089 	err = nl80211_set_station_tdls(info, &params);
7090 	if (err)
7091 		return err;
7092 
7093 	params.vlan = get_vlan(info, rdev);
7094 	if (IS_ERR(params.vlan))
7095 		return PTR_ERR(params.vlan);
7096 
7097 	switch (dev->ieee80211_ptr->iftype) {
7098 	case NL80211_IFTYPE_AP:
7099 	case NL80211_IFTYPE_AP_VLAN:
7100 	case NL80211_IFTYPE_P2P_GO:
7101 	case NL80211_IFTYPE_P2P_CLIENT:
7102 	case NL80211_IFTYPE_STATION:
7103 	case NL80211_IFTYPE_ADHOC:
7104 	case NL80211_IFTYPE_MESH_POINT:
7105 		break;
7106 	default:
7107 		err = -EOPNOTSUPP;
7108 		goto out_put_vlan;
7109 	}
7110 
7111 	/* driver will call cfg80211_check_station_change() */
7112 	wdev_lock(dev->ieee80211_ptr);
7113 	err = rdev_change_station(rdev, dev, mac_addr, &params);
7114 	wdev_unlock(dev->ieee80211_ptr);
7115 
7116  out_put_vlan:
7117 	dev_put(params.vlan);
7118 
7119 	return err;
7120 }
7121 
7122 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
7123 {
7124 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7125 	int err;
7126 	struct net_device *dev = info->user_ptr[1];
7127 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7128 	struct station_parameters params;
7129 	u8 *mac_addr = NULL;
7130 	u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
7131 			 BIT(NL80211_STA_FLAG_ASSOCIATED);
7132 
7133 	memset(&params, 0, sizeof(params));
7134 
7135 	if (!rdev->ops->add_station)
7136 		return -EOPNOTSUPP;
7137 
7138 	if (!info->attrs[NL80211_ATTR_MAC])
7139 		return -EINVAL;
7140 
7141 	if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
7142 		return -EINVAL;
7143 
7144 	if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
7145 		return -EINVAL;
7146 
7147 	if (!info->attrs[NL80211_ATTR_STA_AID] &&
7148 	    !info->attrs[NL80211_ATTR_PEER_AID])
7149 		return -EINVAL;
7150 
7151 	params.link_sta_params.link_id =
7152 		nl80211_link_id_or_invalid(info->attrs);
7153 
7154 	if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7155 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7156 		params.link_sta_params.mld_mac = mac_addr;
7157 		params.link_sta_params.link_mac =
7158 			nla_data(info->attrs[NL80211_ATTR_MAC]);
7159 		if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7160 			return -EINVAL;
7161 	} else {
7162 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7163 	}
7164 
7165 	params.link_sta_params.supported_rates =
7166 		nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7167 	params.link_sta_params.supported_rates_len =
7168 		nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7169 	params.listen_interval =
7170 		nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
7171 
7172 	if (info->attrs[NL80211_ATTR_VLAN_ID])
7173 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
7174 
7175 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
7176 		params.support_p2p_ps =
7177 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
7178 	} else {
7179 		/*
7180 		 * if not specified, assume it's supported for P2P GO interface,
7181 		 * and is NOT supported for AP interface
7182 		 */
7183 		params.support_p2p_ps =
7184 			dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
7185 	}
7186 
7187 	if (info->attrs[NL80211_ATTR_PEER_AID])
7188 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
7189 	else
7190 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
7191 
7192 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7193 		params.capability =
7194 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7195 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7196 	}
7197 
7198 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7199 		params.ext_capab =
7200 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7201 		params.ext_capab_len =
7202 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7203 	}
7204 
7205 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
7206 		params.link_sta_params.ht_capa =
7207 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
7208 
7209 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
7210 		params.link_sta_params.vht_capa =
7211 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
7212 
7213 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
7214 		params.link_sta_params.he_capa =
7215 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7216 		params.link_sta_params.he_capa_len =
7217 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7218 
7219 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
7220 			params.link_sta_params.eht_capa =
7221 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7222 			params.link_sta_params.eht_capa_len =
7223 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7224 
7225 			if (!ieee80211_eht_capa_size_ok((const u8 *)params.link_sta_params.he_capa,
7226 							(const u8 *)params.link_sta_params.eht_capa,
7227 							params.link_sta_params.eht_capa_len,
7228 							false))
7229 				return -EINVAL;
7230 		}
7231 	}
7232 
7233 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7234 		params.link_sta_params.he_6ghz_capa =
7235 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7236 
7237 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7238 		params.link_sta_params.opmode_notif_used = true;
7239 		params.link_sta_params.opmode_notif =
7240 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7241 	}
7242 
7243 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7244 		params.plink_action =
7245 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7246 
7247 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7248 		params.airtime_weight =
7249 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7250 
7251 	if (params.airtime_weight &&
7252 	    !wiphy_ext_feature_isset(&rdev->wiphy,
7253 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7254 		return -EOPNOTSUPP;
7255 
7256 	err = nl80211_parse_sta_txpower_setting(info,
7257 						&params.link_sta_params.txpwr,
7258 						&params.link_sta_params.txpwr_set);
7259 	if (err)
7260 		return err;
7261 
7262 	err = nl80211_parse_sta_channel_info(info, &params);
7263 	if (err)
7264 		return err;
7265 
7266 	err = nl80211_parse_sta_wme(info, &params);
7267 	if (err)
7268 		return err;
7269 
7270 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7271 		return -EINVAL;
7272 
7273 	/* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
7274 	 * as userspace might just pass through the capabilities from the IEs
7275 	 * directly, rather than enforcing this restriction and returning an
7276 	 * error in this case.
7277 	 */
7278 	if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
7279 		params.link_sta_params.ht_capa = NULL;
7280 		params.link_sta_params.vht_capa = NULL;
7281 
7282 		/* HE and EHT require WME */
7283 		if (params.link_sta_params.he_capa_len ||
7284 		    params.link_sta_params.he_6ghz_capa ||
7285 		    params.link_sta_params.eht_capa_len)
7286 			return -EINVAL;
7287 	}
7288 
7289 	/* Ensure that HT/VHT capabilities are not set for 6 GHz HE STA */
7290 	if (params.link_sta_params.he_6ghz_capa &&
7291 	    (params.link_sta_params.ht_capa || params.link_sta_params.vht_capa))
7292 		return -EINVAL;
7293 
7294 	/* When you run into this, adjust the code below for the new flag */
7295 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
7296 
7297 	switch (dev->ieee80211_ptr->iftype) {
7298 	case NL80211_IFTYPE_AP:
7299 	case NL80211_IFTYPE_AP_VLAN:
7300 	case NL80211_IFTYPE_P2P_GO:
7301 		/* ignore WME attributes if iface/sta is not capable */
7302 		if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
7303 		    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
7304 			params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7305 
7306 		/* TDLS peers cannot be added */
7307 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7308 		    info->attrs[NL80211_ATTR_PEER_AID])
7309 			return -EINVAL;
7310 		/* but don't bother the driver with it */
7311 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
7312 
7313 		/* allow authenticated/associated only if driver handles it */
7314 		if (!(rdev->wiphy.features &
7315 				NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
7316 		    params.sta_flags_mask & auth_assoc)
7317 			return -EINVAL;
7318 
7319 		/* Older userspace, or userspace wanting to be compatible with
7320 		 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
7321 		 * and assoc flags in the mask, but assumes the station will be
7322 		 * added as associated anyway since this was the required driver
7323 		 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
7324 		 * introduced.
7325 		 * In order to not bother drivers with this quirk in the API
7326 		 * set the flags in both the mask and set for new stations in
7327 		 * this case.
7328 		 */
7329 		if (!(params.sta_flags_mask & auth_assoc)) {
7330 			params.sta_flags_mask |= auth_assoc;
7331 			params.sta_flags_set |= auth_assoc;
7332 		}
7333 
7334 		/* must be last in here for error handling */
7335 		params.vlan = get_vlan(info, rdev);
7336 		if (IS_ERR(params.vlan))
7337 			return PTR_ERR(params.vlan);
7338 		break;
7339 	case NL80211_IFTYPE_MESH_POINT:
7340 		/* ignore uAPSD data */
7341 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7342 
7343 		/* associated is disallowed */
7344 		if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
7345 			return -EINVAL;
7346 		/* TDLS peers cannot be added */
7347 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7348 		    info->attrs[NL80211_ATTR_PEER_AID])
7349 			return -EINVAL;
7350 		break;
7351 	case NL80211_IFTYPE_STATION:
7352 	case NL80211_IFTYPE_P2P_CLIENT:
7353 		/* ignore uAPSD data */
7354 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7355 
7356 		/* these are disallowed */
7357 		if (params.sta_flags_mask &
7358 				(BIT(NL80211_STA_FLAG_ASSOCIATED) |
7359 				 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
7360 			return -EINVAL;
7361 		/* Only TDLS peers can be added */
7362 		if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
7363 			return -EINVAL;
7364 		/* Can only add if TDLS ... */
7365 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
7366 			return -EOPNOTSUPP;
7367 		/* ... with external setup is supported */
7368 		if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
7369 			return -EOPNOTSUPP;
7370 		/*
7371 		 * Older wpa_supplicant versions always mark the TDLS peer
7372 		 * as authorized, but it shouldn't yet be.
7373 		 */
7374 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
7375 		break;
7376 	default:
7377 		return -EOPNOTSUPP;
7378 	}
7379 
7380 	/* be aware of params.vlan when changing code here */
7381 
7382 	wdev_lock(dev->ieee80211_ptr);
7383 	if (wdev->valid_links) {
7384 		if (params.link_sta_params.link_id < 0) {
7385 			err = -EINVAL;
7386 			goto out;
7387 		}
7388 		if (!(wdev->valid_links & BIT(params.link_sta_params.link_id))) {
7389 			err = -ENOLINK;
7390 			goto out;
7391 		}
7392 	} else {
7393 		if (params.link_sta_params.link_id >= 0) {
7394 			err = -EINVAL;
7395 			goto out;
7396 		}
7397 	}
7398 	err = rdev_add_station(rdev, dev, mac_addr, &params);
7399 out:
7400 	wdev_unlock(dev->ieee80211_ptr);
7401 	dev_put(params.vlan);
7402 	return err;
7403 }
7404 
7405 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
7406 {
7407 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7408 	struct net_device *dev = info->user_ptr[1];
7409 	struct station_del_parameters params;
7410 	int ret;
7411 
7412 	memset(&params, 0, sizeof(params));
7413 
7414 	if (info->attrs[NL80211_ATTR_MAC])
7415 		params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
7416 
7417 	switch (dev->ieee80211_ptr->iftype) {
7418 	case NL80211_IFTYPE_AP:
7419 	case NL80211_IFTYPE_AP_VLAN:
7420 	case NL80211_IFTYPE_MESH_POINT:
7421 	case NL80211_IFTYPE_P2P_GO:
7422 		/* always accept these */
7423 		break;
7424 	case NL80211_IFTYPE_ADHOC:
7425 		/* conditionally accept */
7426 		if (wiphy_ext_feature_isset(&rdev->wiphy,
7427 					    NL80211_EXT_FEATURE_DEL_IBSS_STA))
7428 			break;
7429 		return -EINVAL;
7430 	default:
7431 		return -EINVAL;
7432 	}
7433 
7434 	if (!rdev->ops->del_station)
7435 		return -EOPNOTSUPP;
7436 
7437 	if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
7438 		params.subtype =
7439 			nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
7440 		if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
7441 		    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
7442 			return -EINVAL;
7443 	} else {
7444 		/* Default to Deauthentication frame */
7445 		params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
7446 	}
7447 
7448 	if (info->attrs[NL80211_ATTR_REASON_CODE]) {
7449 		params.reason_code =
7450 			nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7451 		if (params.reason_code == 0)
7452 			return -EINVAL; /* 0 is reserved */
7453 	} else {
7454 		/* Default to reason code 2 */
7455 		params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
7456 	}
7457 
7458 	wdev_lock(dev->ieee80211_ptr);
7459 	ret = rdev_del_station(rdev, dev, &params);
7460 	wdev_unlock(dev->ieee80211_ptr);
7461 
7462 	return ret;
7463 }
7464 
7465 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
7466 				int flags, struct net_device *dev,
7467 				u8 *dst, u8 *next_hop,
7468 				struct mpath_info *pinfo)
7469 {
7470 	void *hdr;
7471 	struct nlattr *pinfoattr;
7472 
7473 	hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
7474 	if (!hdr)
7475 		return -1;
7476 
7477 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7478 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7479 	    nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
7480 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
7481 		goto nla_put_failure;
7482 
7483 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
7484 	if (!pinfoattr)
7485 		goto nla_put_failure;
7486 	if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
7487 	    nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
7488 			pinfo->frame_qlen))
7489 		goto nla_put_failure;
7490 	if (((pinfo->filled & MPATH_INFO_SN) &&
7491 	     nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
7492 	    ((pinfo->filled & MPATH_INFO_METRIC) &&
7493 	     nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
7494 			 pinfo->metric)) ||
7495 	    ((pinfo->filled & MPATH_INFO_EXPTIME) &&
7496 	     nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
7497 			 pinfo->exptime)) ||
7498 	    ((pinfo->filled & MPATH_INFO_FLAGS) &&
7499 	     nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
7500 			pinfo->flags)) ||
7501 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
7502 	     nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
7503 			 pinfo->discovery_timeout)) ||
7504 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
7505 	     nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
7506 			pinfo->discovery_retries)) ||
7507 	    ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
7508 	     nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
7509 			pinfo->hop_count)) ||
7510 	    ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
7511 	     nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
7512 			 pinfo->path_change_count)))
7513 		goto nla_put_failure;
7514 
7515 	nla_nest_end(msg, pinfoattr);
7516 
7517 	genlmsg_end(msg, hdr);
7518 	return 0;
7519 
7520  nla_put_failure:
7521 	genlmsg_cancel(msg, hdr);
7522 	return -EMSGSIZE;
7523 }
7524 
7525 static int nl80211_dump_mpath(struct sk_buff *skb,
7526 			      struct netlink_callback *cb)
7527 {
7528 	struct mpath_info pinfo;
7529 	struct cfg80211_registered_device *rdev;
7530 	struct wireless_dev *wdev;
7531 	u8 dst[ETH_ALEN];
7532 	u8 next_hop[ETH_ALEN];
7533 	int path_idx = cb->args[2];
7534 	int err;
7535 
7536 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7537 	if (err)
7538 		return err;
7539 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
7540 	__acquire(&rdev->wiphy.mtx);
7541 
7542 	if (!rdev->ops->dump_mpath) {
7543 		err = -EOPNOTSUPP;
7544 		goto out_err;
7545 	}
7546 
7547 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7548 		err = -EOPNOTSUPP;
7549 		goto out_err;
7550 	}
7551 
7552 	while (1) {
7553 		err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
7554 				      next_hop, &pinfo);
7555 		if (err == -ENOENT)
7556 			break;
7557 		if (err)
7558 			goto out_err;
7559 
7560 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7561 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
7562 				       wdev->netdev, dst, next_hop,
7563 				       &pinfo) < 0)
7564 			goto out;
7565 
7566 		path_idx++;
7567 	}
7568 
7569  out:
7570 	cb->args[2] = path_idx;
7571 	err = skb->len;
7572  out_err:
7573 	wiphy_unlock(&rdev->wiphy);
7574 	return err;
7575 }
7576 
7577 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
7578 {
7579 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7580 	int err;
7581 	struct net_device *dev = info->user_ptr[1];
7582 	struct mpath_info pinfo;
7583 	struct sk_buff *msg;
7584 	u8 *dst = NULL;
7585 	u8 next_hop[ETH_ALEN];
7586 
7587 	memset(&pinfo, 0, sizeof(pinfo));
7588 
7589 	if (!info->attrs[NL80211_ATTR_MAC])
7590 		return -EINVAL;
7591 
7592 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7593 
7594 	if (!rdev->ops->get_mpath)
7595 		return -EOPNOTSUPP;
7596 
7597 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7598 		return -EOPNOTSUPP;
7599 
7600 	err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
7601 	if (err)
7602 		return err;
7603 
7604 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7605 	if (!msg)
7606 		return -ENOMEM;
7607 
7608 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7609 				 dev, dst, next_hop, &pinfo) < 0) {
7610 		nlmsg_free(msg);
7611 		return -ENOBUFS;
7612 	}
7613 
7614 	return genlmsg_reply(msg, info);
7615 }
7616 
7617 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
7618 {
7619 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7620 	struct net_device *dev = info->user_ptr[1];
7621 	u8 *dst = NULL;
7622 	u8 *next_hop = NULL;
7623 
7624 	if (!info->attrs[NL80211_ATTR_MAC])
7625 		return -EINVAL;
7626 
7627 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7628 		return -EINVAL;
7629 
7630 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7631 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7632 
7633 	if (!rdev->ops->change_mpath)
7634 		return -EOPNOTSUPP;
7635 
7636 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7637 		return -EOPNOTSUPP;
7638 
7639 	return rdev_change_mpath(rdev, dev, dst, next_hop);
7640 }
7641 
7642 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
7643 {
7644 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7645 	struct net_device *dev = info->user_ptr[1];
7646 	u8 *dst = NULL;
7647 	u8 *next_hop = NULL;
7648 
7649 	if (!info->attrs[NL80211_ATTR_MAC])
7650 		return -EINVAL;
7651 
7652 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7653 		return -EINVAL;
7654 
7655 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7656 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7657 
7658 	if (!rdev->ops->add_mpath)
7659 		return -EOPNOTSUPP;
7660 
7661 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7662 		return -EOPNOTSUPP;
7663 
7664 	return rdev_add_mpath(rdev, dev, dst, next_hop);
7665 }
7666 
7667 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
7668 {
7669 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7670 	struct net_device *dev = info->user_ptr[1];
7671 	u8 *dst = NULL;
7672 
7673 	if (info->attrs[NL80211_ATTR_MAC])
7674 		dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7675 
7676 	if (!rdev->ops->del_mpath)
7677 		return -EOPNOTSUPP;
7678 
7679 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7680 		return -EOPNOTSUPP;
7681 
7682 	return rdev_del_mpath(rdev, dev, dst);
7683 }
7684 
7685 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
7686 {
7687 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7688 	int err;
7689 	struct net_device *dev = info->user_ptr[1];
7690 	struct mpath_info pinfo;
7691 	struct sk_buff *msg;
7692 	u8 *dst = NULL;
7693 	u8 mpp[ETH_ALEN];
7694 
7695 	memset(&pinfo, 0, sizeof(pinfo));
7696 
7697 	if (!info->attrs[NL80211_ATTR_MAC])
7698 		return -EINVAL;
7699 
7700 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7701 
7702 	if (!rdev->ops->get_mpp)
7703 		return -EOPNOTSUPP;
7704 
7705 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7706 		return -EOPNOTSUPP;
7707 
7708 	err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
7709 	if (err)
7710 		return err;
7711 
7712 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7713 	if (!msg)
7714 		return -ENOMEM;
7715 
7716 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7717 			       dev, dst, mpp, &pinfo) < 0) {
7718 		nlmsg_free(msg);
7719 		return -ENOBUFS;
7720 	}
7721 
7722 	return genlmsg_reply(msg, info);
7723 }
7724 
7725 static int nl80211_dump_mpp(struct sk_buff *skb,
7726 			    struct netlink_callback *cb)
7727 {
7728 	struct mpath_info pinfo;
7729 	struct cfg80211_registered_device *rdev;
7730 	struct wireless_dev *wdev;
7731 	u8 dst[ETH_ALEN];
7732 	u8 mpp[ETH_ALEN];
7733 	int path_idx = cb->args[2];
7734 	int err;
7735 
7736 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7737 	if (err)
7738 		return err;
7739 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
7740 	__acquire(&rdev->wiphy.mtx);
7741 
7742 	if (!rdev->ops->dump_mpp) {
7743 		err = -EOPNOTSUPP;
7744 		goto out_err;
7745 	}
7746 
7747 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7748 		err = -EOPNOTSUPP;
7749 		goto out_err;
7750 	}
7751 
7752 	while (1) {
7753 		err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
7754 				    mpp, &pinfo);
7755 		if (err == -ENOENT)
7756 			break;
7757 		if (err)
7758 			goto out_err;
7759 
7760 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7761 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
7762 				       wdev->netdev, dst, mpp,
7763 				       &pinfo) < 0)
7764 			goto out;
7765 
7766 		path_idx++;
7767 	}
7768 
7769  out:
7770 	cb->args[2] = path_idx;
7771 	err = skb->len;
7772  out_err:
7773 	wiphy_unlock(&rdev->wiphy);
7774 	return err;
7775 }
7776 
7777 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
7778 {
7779 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7780 	struct net_device *dev = info->user_ptr[1];
7781 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7782 	struct bss_parameters params;
7783 	int err;
7784 
7785 	memset(&params, 0, sizeof(params));
7786 	params.link_id = nl80211_link_id_or_invalid(info->attrs);
7787 	/* default to not changing parameters */
7788 	params.use_cts_prot = -1;
7789 	params.use_short_preamble = -1;
7790 	params.use_short_slot_time = -1;
7791 	params.ap_isolate = -1;
7792 	params.ht_opmode = -1;
7793 	params.p2p_ctwindow = -1;
7794 	params.p2p_opp_ps = -1;
7795 
7796 	if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
7797 		params.use_cts_prot =
7798 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
7799 	if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
7800 		params.use_short_preamble =
7801 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
7802 	if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
7803 		params.use_short_slot_time =
7804 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
7805 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
7806 		params.basic_rates =
7807 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7808 		params.basic_rates_len =
7809 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7810 	}
7811 	if (info->attrs[NL80211_ATTR_AP_ISOLATE])
7812 		params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
7813 	if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
7814 		params.ht_opmode =
7815 			nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
7816 
7817 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
7818 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
7819 			return -EINVAL;
7820 		params.p2p_ctwindow =
7821 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
7822 		if (params.p2p_ctwindow != 0 &&
7823 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
7824 			return -EINVAL;
7825 	}
7826 
7827 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
7828 		u8 tmp;
7829 
7830 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
7831 			return -EINVAL;
7832 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
7833 		params.p2p_opp_ps = tmp;
7834 		if (params.p2p_opp_ps &&
7835 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
7836 			return -EINVAL;
7837 	}
7838 
7839 	if (!rdev->ops->change_bss)
7840 		return -EOPNOTSUPP;
7841 
7842 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
7843 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
7844 		return -EOPNOTSUPP;
7845 
7846 	wdev_lock(wdev);
7847 	err = rdev_change_bss(rdev, dev, &params);
7848 	wdev_unlock(wdev);
7849 
7850 	return err;
7851 }
7852 
7853 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
7854 {
7855 	char *data = NULL;
7856 	bool is_indoor;
7857 	enum nl80211_user_reg_hint_type user_reg_hint_type;
7858 	u32 owner_nlportid;
7859 
7860 	/*
7861 	 * You should only get this when cfg80211 hasn't yet initialized
7862 	 * completely when built-in to the kernel right between the time
7863 	 * window between nl80211_init() and regulatory_init(), if that is
7864 	 * even possible.
7865 	 */
7866 	if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
7867 		return -EINPROGRESS;
7868 
7869 	if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
7870 		user_reg_hint_type =
7871 		  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
7872 	else
7873 		user_reg_hint_type = NL80211_USER_REG_HINT_USER;
7874 
7875 	switch (user_reg_hint_type) {
7876 	case NL80211_USER_REG_HINT_USER:
7877 	case NL80211_USER_REG_HINT_CELL_BASE:
7878 		if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
7879 			return -EINVAL;
7880 
7881 		data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
7882 		return regulatory_hint_user(data, user_reg_hint_type);
7883 	case NL80211_USER_REG_HINT_INDOOR:
7884 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
7885 			owner_nlportid = info->snd_portid;
7886 			is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
7887 		} else {
7888 			owner_nlportid = 0;
7889 			is_indoor = true;
7890 		}
7891 
7892 		return regulatory_hint_indoor(is_indoor, owner_nlportid);
7893 	default:
7894 		return -EINVAL;
7895 	}
7896 }
7897 
7898 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
7899 {
7900 	return reg_reload_regdb();
7901 }
7902 
7903 static int nl80211_get_mesh_config(struct sk_buff *skb,
7904 				   struct genl_info *info)
7905 {
7906 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7907 	struct net_device *dev = info->user_ptr[1];
7908 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7909 	struct mesh_config cur_params;
7910 	int err = 0;
7911 	void *hdr;
7912 	struct nlattr *pinfoattr;
7913 	struct sk_buff *msg;
7914 
7915 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
7916 		return -EOPNOTSUPP;
7917 
7918 	if (!rdev->ops->get_mesh_config)
7919 		return -EOPNOTSUPP;
7920 
7921 	wdev_lock(wdev);
7922 	/* If not connected, get default parameters */
7923 	if (!wdev->u.mesh.id_len)
7924 		memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
7925 	else
7926 		err = rdev_get_mesh_config(rdev, dev, &cur_params);
7927 	wdev_unlock(wdev);
7928 
7929 	if (err)
7930 		return err;
7931 
7932 	/* Draw up a netlink message to send back */
7933 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7934 	if (!msg)
7935 		return -ENOMEM;
7936 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7937 			     NL80211_CMD_GET_MESH_CONFIG);
7938 	if (!hdr)
7939 		goto out;
7940 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
7941 	if (!pinfoattr)
7942 		goto nla_put_failure;
7943 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7944 	    nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
7945 			cur_params.dot11MeshRetryTimeout) ||
7946 	    nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
7947 			cur_params.dot11MeshConfirmTimeout) ||
7948 	    nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
7949 			cur_params.dot11MeshHoldingTimeout) ||
7950 	    nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
7951 			cur_params.dot11MeshMaxPeerLinks) ||
7952 	    nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
7953 		       cur_params.dot11MeshMaxRetries) ||
7954 	    nla_put_u8(msg, NL80211_MESHCONF_TTL,
7955 		       cur_params.dot11MeshTTL) ||
7956 	    nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
7957 		       cur_params.element_ttl) ||
7958 	    nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
7959 		       cur_params.auto_open_plinks) ||
7960 	    nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
7961 			cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
7962 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
7963 		       cur_params.dot11MeshHWMPmaxPREQretries) ||
7964 	    nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
7965 			cur_params.path_refresh_time) ||
7966 	    nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
7967 			cur_params.min_discovery_timeout) ||
7968 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
7969 			cur_params.dot11MeshHWMPactivePathTimeout) ||
7970 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
7971 			cur_params.dot11MeshHWMPpreqMinInterval) ||
7972 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
7973 			cur_params.dot11MeshHWMPperrMinInterval) ||
7974 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
7975 			cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
7976 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
7977 		       cur_params.dot11MeshHWMPRootMode) ||
7978 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
7979 			cur_params.dot11MeshHWMPRannInterval) ||
7980 	    nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
7981 		       cur_params.dot11MeshGateAnnouncementProtocol) ||
7982 	    nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
7983 		       cur_params.dot11MeshForwarding) ||
7984 	    nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
7985 			cur_params.rssi_threshold) ||
7986 	    nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
7987 			cur_params.ht_opmode) ||
7988 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
7989 			cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
7990 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
7991 			cur_params.dot11MeshHWMProotInterval) ||
7992 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
7993 			cur_params.dot11MeshHWMPconfirmationInterval) ||
7994 	    nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
7995 			cur_params.power_mode) ||
7996 	    nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
7997 			cur_params.dot11MeshAwakeWindowDuration) ||
7998 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
7999 			cur_params.plink_timeout) ||
8000 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
8001 		       cur_params.dot11MeshConnectedToMeshGate) ||
8002 	    nla_put_u8(msg, NL80211_MESHCONF_NOLEARN,
8003 		       cur_params.dot11MeshNolearn) ||
8004 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_AS,
8005 		       cur_params.dot11MeshConnectedToAuthServer))
8006 		goto nla_put_failure;
8007 	nla_nest_end(msg, pinfoattr);
8008 	genlmsg_end(msg, hdr);
8009 	return genlmsg_reply(msg, info);
8010 
8011  nla_put_failure:
8012  out:
8013 	nlmsg_free(msg);
8014 	return -ENOBUFS;
8015 }
8016 
8017 static const struct nla_policy
8018 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
8019 	[NL80211_MESHCONF_RETRY_TIMEOUT] =
8020 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8021 	[NL80211_MESHCONF_CONFIRM_TIMEOUT] =
8022 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8023 	[NL80211_MESHCONF_HOLDING_TIMEOUT] =
8024 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8025 	[NL80211_MESHCONF_MAX_PEER_LINKS] =
8026 		NLA_POLICY_RANGE(NLA_U16, 0, 255),
8027 	[NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
8028 	[NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8029 	[NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8030 	[NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
8031 	[NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
8032 		NLA_POLICY_RANGE(NLA_U32, 1, 255),
8033 	[NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
8034 	[NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
8035 	[NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
8036 	[NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
8037 	[NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
8038 		NLA_POLICY_MIN(NLA_U16, 1),
8039 	[NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
8040 		NLA_POLICY_MIN(NLA_U16, 1),
8041 	[NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
8042 		NLA_POLICY_MIN(NLA_U16, 1),
8043 	[NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
8044 	[NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
8045 		NLA_POLICY_MIN(NLA_U16, 1),
8046 	[NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
8047 	[NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
8048 	[NL80211_MESHCONF_RSSI_THRESHOLD] =
8049 		NLA_POLICY_RANGE(NLA_S32, -255, 0),
8050 	[NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
8051 	[NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
8052 	[NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
8053 		NLA_POLICY_MIN(NLA_U16, 1),
8054 	[NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
8055 		NLA_POLICY_MIN(NLA_U16, 1),
8056 	[NL80211_MESHCONF_POWER_MODE] =
8057 		NLA_POLICY_RANGE(NLA_U32,
8058 				 NL80211_MESH_POWER_ACTIVE,
8059 				 NL80211_MESH_POWER_MAX),
8060 	[NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
8061 	[NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
8062 	[NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8063 	[NL80211_MESHCONF_NOLEARN] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8064 	[NL80211_MESHCONF_CONNECTED_TO_AS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8065 };
8066 
8067 static const struct nla_policy
8068 	nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
8069 	[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
8070 	[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
8071 	[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
8072 	[NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
8073 	[NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
8074 	[NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
8075 	[NL80211_MESH_SETUP_IE] =
8076 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
8077 				       IEEE80211_MAX_DATA_LEN),
8078 	[NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
8079 };
8080 
8081 static int nl80211_parse_mesh_config(struct genl_info *info,
8082 				     struct mesh_config *cfg,
8083 				     u32 *mask_out)
8084 {
8085 	struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
8086 	u32 mask = 0;
8087 	u16 ht_opmode;
8088 
8089 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)	\
8090 do {									\
8091 	if (tb[attr]) {							\
8092 		cfg->param = fn(tb[attr]);				\
8093 		mask |= BIT((attr) - 1);				\
8094 	}								\
8095 } while (0)
8096 
8097 	if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
8098 		return -EINVAL;
8099 	if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
8100 		return -EINVAL;
8101 
8102 	/* This makes sure that there aren't more than 32 mesh config
8103 	 * parameters (otherwise our bitfield scheme would not work.) */
8104 	BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
8105 
8106 	/* Fill in the params struct */
8107 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
8108 				  NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
8109 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
8110 				  NL80211_MESHCONF_CONFIRM_TIMEOUT,
8111 				  nla_get_u16);
8112 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
8113 				  NL80211_MESHCONF_HOLDING_TIMEOUT,
8114 				  nla_get_u16);
8115 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
8116 				  NL80211_MESHCONF_MAX_PEER_LINKS,
8117 				  nla_get_u16);
8118 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
8119 				  NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
8120 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
8121 				  NL80211_MESHCONF_TTL, nla_get_u8);
8122 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
8123 				  NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
8124 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
8125 				  NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8126 				  nla_get_u8);
8127 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
8128 				  mask,
8129 				  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
8130 				  nla_get_u32);
8131 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
8132 				  NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
8133 				  nla_get_u8);
8134 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
8135 				  NL80211_MESHCONF_PATH_REFRESH_TIME,
8136 				  nla_get_u32);
8137 	if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
8138 	    (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
8139 		return -EINVAL;
8140 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
8141 				  NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
8142 				  nla_get_u16);
8143 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
8144 				  mask,
8145 				  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
8146 				  nla_get_u32);
8147 	if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
8148 	    (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
8149 	     cfg->dot11MeshHWMPactivePathTimeout > 65535))
8150 		return -EINVAL;
8151 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
8152 				  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
8153 				  nla_get_u16);
8154 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
8155 				  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
8156 				  nla_get_u16);
8157 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8158 				  dot11MeshHWMPnetDiameterTraversalTime, mask,
8159 				  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
8160 				  nla_get_u16);
8161 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
8162 				  NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
8163 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
8164 				  NL80211_MESHCONF_HWMP_RANN_INTERVAL,
8165 				  nla_get_u16);
8166 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
8167 				  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
8168 				  nla_get_u8);
8169 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
8170 				  NL80211_MESHCONF_FORWARDING, nla_get_u8);
8171 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
8172 				  NL80211_MESHCONF_RSSI_THRESHOLD,
8173 				  nla_get_s32);
8174 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
8175 				  NL80211_MESHCONF_CONNECTED_TO_GATE,
8176 				  nla_get_u8);
8177 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToAuthServer, mask,
8178 				  NL80211_MESHCONF_CONNECTED_TO_AS,
8179 				  nla_get_u8);
8180 	/*
8181 	 * Check HT operation mode based on
8182 	 * IEEE 802.11-2016 9.4.2.57 HT Operation element.
8183 	 */
8184 	if (tb[NL80211_MESHCONF_HT_OPMODE]) {
8185 		ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
8186 
8187 		if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
8188 				  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
8189 				  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
8190 			return -EINVAL;
8191 
8192 		/* NON_HT_STA bit is reserved, but some programs set it */
8193 		ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
8194 
8195 		cfg->ht_opmode = ht_opmode;
8196 		mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
8197 	}
8198 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8199 				  dot11MeshHWMPactivePathToRootTimeout, mask,
8200 				  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
8201 				  nla_get_u32);
8202 	if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
8203 	    (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
8204 	     cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
8205 		return -EINVAL;
8206 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
8207 				  NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
8208 				  nla_get_u16);
8209 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
8210 				  mask,
8211 				  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
8212 				  nla_get_u16);
8213 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
8214 				  NL80211_MESHCONF_POWER_MODE, nla_get_u32);
8215 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
8216 				  NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
8217 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
8218 				  NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
8219 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNolearn, mask,
8220 				  NL80211_MESHCONF_NOLEARN, nla_get_u8);
8221 	if (mask_out)
8222 		*mask_out = mask;
8223 
8224 	return 0;
8225 
8226 #undef FILL_IN_MESH_PARAM_IF_SET
8227 }
8228 
8229 static int nl80211_parse_mesh_setup(struct genl_info *info,
8230 				     struct mesh_setup *setup)
8231 {
8232 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8233 	struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
8234 
8235 	if (!info->attrs[NL80211_ATTR_MESH_SETUP])
8236 		return -EINVAL;
8237 	if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
8238 		return -EINVAL;
8239 
8240 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
8241 		setup->sync_method =
8242 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
8243 		 IEEE80211_SYNC_METHOD_VENDOR :
8244 		 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
8245 
8246 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
8247 		setup->path_sel_proto =
8248 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
8249 		 IEEE80211_PATH_PROTOCOL_VENDOR :
8250 		 IEEE80211_PATH_PROTOCOL_HWMP;
8251 
8252 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
8253 		setup->path_metric =
8254 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
8255 		 IEEE80211_PATH_METRIC_VENDOR :
8256 		 IEEE80211_PATH_METRIC_AIRTIME;
8257 
8258 	if (tb[NL80211_MESH_SETUP_IE]) {
8259 		struct nlattr *ieattr =
8260 			tb[NL80211_MESH_SETUP_IE];
8261 		setup->ie = nla_data(ieattr);
8262 		setup->ie_len = nla_len(ieattr);
8263 	}
8264 	if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
8265 	    !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
8266 		return -EINVAL;
8267 	setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
8268 	setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
8269 	setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
8270 	if (setup->is_secure)
8271 		setup->user_mpm = true;
8272 
8273 	if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
8274 		if (!setup->user_mpm)
8275 			return -EINVAL;
8276 		setup->auth_id =
8277 			nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
8278 	}
8279 
8280 	return 0;
8281 }
8282 
8283 static int nl80211_update_mesh_config(struct sk_buff *skb,
8284 				      struct genl_info *info)
8285 {
8286 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8287 	struct net_device *dev = info->user_ptr[1];
8288 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8289 	struct mesh_config cfg;
8290 	u32 mask;
8291 	int err;
8292 
8293 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
8294 		return -EOPNOTSUPP;
8295 
8296 	if (!rdev->ops->update_mesh_config)
8297 		return -EOPNOTSUPP;
8298 
8299 	err = nl80211_parse_mesh_config(info, &cfg, &mask);
8300 	if (err)
8301 		return err;
8302 
8303 	wdev_lock(wdev);
8304 	if (!wdev->u.mesh.id_len)
8305 		err = -ENOLINK;
8306 
8307 	if (!err)
8308 		err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
8309 
8310 	wdev_unlock(wdev);
8311 
8312 	return err;
8313 }
8314 
8315 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
8316 			      struct sk_buff *msg)
8317 {
8318 	struct nlattr *nl_reg_rules;
8319 	unsigned int i;
8320 
8321 	if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
8322 	    (regdom->dfs_region &&
8323 	     nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
8324 		goto nla_put_failure;
8325 
8326 	nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
8327 	if (!nl_reg_rules)
8328 		goto nla_put_failure;
8329 
8330 	for (i = 0; i < regdom->n_reg_rules; i++) {
8331 		struct nlattr *nl_reg_rule;
8332 		const struct ieee80211_reg_rule *reg_rule;
8333 		const struct ieee80211_freq_range *freq_range;
8334 		const struct ieee80211_power_rule *power_rule;
8335 		unsigned int max_bandwidth_khz;
8336 
8337 		reg_rule = &regdom->reg_rules[i];
8338 		freq_range = &reg_rule->freq_range;
8339 		power_rule = &reg_rule->power_rule;
8340 
8341 		nl_reg_rule = nla_nest_start_noflag(msg, i);
8342 		if (!nl_reg_rule)
8343 			goto nla_put_failure;
8344 
8345 		max_bandwidth_khz = freq_range->max_bandwidth_khz;
8346 		if (!max_bandwidth_khz)
8347 			max_bandwidth_khz = reg_get_max_bandwidth(regdom,
8348 								  reg_rule);
8349 
8350 		if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
8351 				reg_rule->flags) ||
8352 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
8353 				freq_range->start_freq_khz) ||
8354 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
8355 				freq_range->end_freq_khz) ||
8356 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
8357 				max_bandwidth_khz) ||
8358 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
8359 				power_rule->max_antenna_gain) ||
8360 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
8361 				power_rule->max_eirp) ||
8362 		    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
8363 				reg_rule->dfs_cac_ms))
8364 			goto nla_put_failure;
8365 
8366 		nla_nest_end(msg, nl_reg_rule);
8367 	}
8368 
8369 	nla_nest_end(msg, nl_reg_rules);
8370 	return 0;
8371 
8372 nla_put_failure:
8373 	return -EMSGSIZE;
8374 }
8375 
8376 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
8377 {
8378 	const struct ieee80211_regdomain *regdom = NULL;
8379 	struct cfg80211_registered_device *rdev;
8380 	struct wiphy *wiphy = NULL;
8381 	struct sk_buff *msg;
8382 	int err = -EMSGSIZE;
8383 	void *hdr;
8384 
8385 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8386 	if (!msg)
8387 		return -ENOBUFS;
8388 
8389 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8390 			     NL80211_CMD_GET_REG);
8391 	if (!hdr)
8392 		goto put_failure;
8393 
8394 	rtnl_lock();
8395 
8396 	if (info->attrs[NL80211_ATTR_WIPHY]) {
8397 		bool self_managed;
8398 
8399 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
8400 		if (IS_ERR(rdev)) {
8401 			err = PTR_ERR(rdev);
8402 			goto nla_put_failure;
8403 		}
8404 
8405 		wiphy = &rdev->wiphy;
8406 		self_managed = wiphy->regulatory_flags &
8407 			       REGULATORY_WIPHY_SELF_MANAGED;
8408 
8409 		rcu_read_lock();
8410 
8411 		regdom = get_wiphy_regdom(wiphy);
8412 
8413 		/* a self-managed-reg device must have a private regdom */
8414 		if (WARN_ON(!regdom && self_managed)) {
8415 			err = -EINVAL;
8416 			goto nla_put_failure_rcu;
8417 		}
8418 
8419 		if (regdom &&
8420 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8421 			goto nla_put_failure_rcu;
8422 	} else {
8423 		rcu_read_lock();
8424 	}
8425 
8426 	if (!wiphy && reg_last_request_cell_base() &&
8427 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8428 			NL80211_USER_REG_HINT_CELL_BASE))
8429 		goto nla_put_failure_rcu;
8430 
8431 	if (!regdom)
8432 		regdom = rcu_dereference(cfg80211_regdomain);
8433 
8434 	if (nl80211_put_regdom(regdom, msg))
8435 		goto nla_put_failure_rcu;
8436 
8437 	rcu_read_unlock();
8438 
8439 	genlmsg_end(msg, hdr);
8440 	rtnl_unlock();
8441 	return genlmsg_reply(msg, info);
8442 
8443 nla_put_failure_rcu:
8444 	rcu_read_unlock();
8445 nla_put_failure:
8446 	rtnl_unlock();
8447 put_failure:
8448 	nlmsg_free(msg);
8449 	return err;
8450 }
8451 
8452 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
8453 			       u32 seq, int flags, struct wiphy *wiphy,
8454 			       const struct ieee80211_regdomain *regdom)
8455 {
8456 	void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
8457 				   NL80211_CMD_GET_REG);
8458 
8459 	if (!hdr)
8460 		return -1;
8461 
8462 	genl_dump_check_consistent(cb, hdr);
8463 
8464 	if (nl80211_put_regdom(regdom, msg))
8465 		goto nla_put_failure;
8466 
8467 	if (!wiphy && reg_last_request_cell_base() &&
8468 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8469 			NL80211_USER_REG_HINT_CELL_BASE))
8470 		goto nla_put_failure;
8471 
8472 	if (wiphy &&
8473 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8474 		goto nla_put_failure;
8475 
8476 	if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
8477 	    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
8478 		goto nla_put_failure;
8479 
8480 	genlmsg_end(msg, hdr);
8481 	return 0;
8482 
8483 nla_put_failure:
8484 	genlmsg_cancel(msg, hdr);
8485 	return -EMSGSIZE;
8486 }
8487 
8488 static int nl80211_get_reg_dump(struct sk_buff *skb,
8489 				struct netlink_callback *cb)
8490 {
8491 	const struct ieee80211_regdomain *regdom = NULL;
8492 	struct cfg80211_registered_device *rdev;
8493 	int err, reg_idx, start = cb->args[2];
8494 
8495 	rcu_read_lock();
8496 
8497 	if (cfg80211_regdomain && start == 0) {
8498 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8499 					  NLM_F_MULTI, NULL,
8500 					  rcu_dereference(cfg80211_regdomain));
8501 		if (err < 0)
8502 			goto out_err;
8503 	}
8504 
8505 	/* the global regdom is idx 0 */
8506 	reg_idx = 1;
8507 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
8508 		regdom = get_wiphy_regdom(&rdev->wiphy);
8509 		if (!regdom)
8510 			continue;
8511 
8512 		if (++reg_idx <= start)
8513 			continue;
8514 
8515 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8516 					  NLM_F_MULTI, &rdev->wiphy, regdom);
8517 		if (err < 0) {
8518 			reg_idx--;
8519 			break;
8520 		}
8521 	}
8522 
8523 	cb->args[2] = reg_idx;
8524 	err = skb->len;
8525 out_err:
8526 	rcu_read_unlock();
8527 	return err;
8528 }
8529 
8530 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
8531 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
8532 	[NL80211_ATTR_REG_RULE_FLAGS]		= { .type = NLA_U32 },
8533 	[NL80211_ATTR_FREQ_RANGE_START]		= { .type = NLA_U32 },
8534 	[NL80211_ATTR_FREQ_RANGE_END]		= { .type = NLA_U32 },
8535 	[NL80211_ATTR_FREQ_RANGE_MAX_BW]	= { .type = NLA_U32 },
8536 	[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]	= { .type = NLA_U32 },
8537 	[NL80211_ATTR_POWER_RULE_MAX_EIRP]	= { .type = NLA_U32 },
8538 	[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
8539 };
8540 
8541 static int parse_reg_rule(struct nlattr *tb[],
8542 	struct ieee80211_reg_rule *reg_rule)
8543 {
8544 	struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
8545 	struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
8546 
8547 	if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
8548 		return -EINVAL;
8549 	if (!tb[NL80211_ATTR_FREQ_RANGE_START])
8550 		return -EINVAL;
8551 	if (!tb[NL80211_ATTR_FREQ_RANGE_END])
8552 		return -EINVAL;
8553 	if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
8554 		return -EINVAL;
8555 	if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
8556 		return -EINVAL;
8557 
8558 	reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
8559 
8560 	freq_range->start_freq_khz =
8561 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
8562 	freq_range->end_freq_khz =
8563 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
8564 	freq_range->max_bandwidth_khz =
8565 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
8566 
8567 	power_rule->max_eirp =
8568 		nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
8569 
8570 	if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
8571 		power_rule->max_antenna_gain =
8572 			nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
8573 
8574 	if (tb[NL80211_ATTR_DFS_CAC_TIME])
8575 		reg_rule->dfs_cac_ms =
8576 			nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
8577 
8578 	return 0;
8579 }
8580 
8581 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
8582 {
8583 	struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
8584 	struct nlattr *nl_reg_rule;
8585 	char *alpha2;
8586 	int rem_reg_rules, r;
8587 	u32 num_rules = 0, rule_idx = 0;
8588 	enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
8589 	struct ieee80211_regdomain *rd;
8590 
8591 	if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
8592 		return -EINVAL;
8593 
8594 	if (!info->attrs[NL80211_ATTR_REG_RULES])
8595 		return -EINVAL;
8596 
8597 	alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
8598 
8599 	if (info->attrs[NL80211_ATTR_DFS_REGION])
8600 		dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
8601 
8602 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8603 			    rem_reg_rules) {
8604 		num_rules++;
8605 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
8606 			return -EINVAL;
8607 	}
8608 
8609 	rtnl_lock();
8610 	if (!reg_is_valid_request(alpha2)) {
8611 		r = -EINVAL;
8612 		goto out;
8613 	}
8614 
8615 	rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
8616 	if (!rd) {
8617 		r = -ENOMEM;
8618 		goto out;
8619 	}
8620 
8621 	rd->n_reg_rules = num_rules;
8622 	rd->alpha2[0] = alpha2[0];
8623 	rd->alpha2[1] = alpha2[1];
8624 
8625 	/*
8626 	 * Disable DFS master mode if the DFS region was
8627 	 * not supported or known on this kernel.
8628 	 */
8629 	if (reg_supported_dfs_region(dfs_region))
8630 		rd->dfs_region = dfs_region;
8631 
8632 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8633 			    rem_reg_rules) {
8634 		r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
8635 						nl_reg_rule, reg_rule_policy,
8636 						info->extack);
8637 		if (r)
8638 			goto bad_reg;
8639 		r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
8640 		if (r)
8641 			goto bad_reg;
8642 
8643 		rule_idx++;
8644 
8645 		if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
8646 			r = -EINVAL;
8647 			goto bad_reg;
8648 		}
8649 	}
8650 
8651 	r = set_regdom(rd, REGD_SOURCE_CRDA);
8652 	/* set_regdom takes ownership of rd */
8653 	rd = NULL;
8654  bad_reg:
8655 	kfree(rd);
8656  out:
8657 	rtnl_unlock();
8658 	return r;
8659 }
8660 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
8661 
8662 static int validate_scan_freqs(struct nlattr *freqs)
8663 {
8664 	struct nlattr *attr1, *attr2;
8665 	int n_channels = 0, tmp1, tmp2;
8666 
8667 	nla_for_each_nested(attr1, freqs, tmp1)
8668 		if (nla_len(attr1) != sizeof(u32))
8669 			return 0;
8670 
8671 	nla_for_each_nested(attr1, freqs, tmp1) {
8672 		n_channels++;
8673 		/*
8674 		 * Some hardware has a limited channel list for
8675 		 * scanning, and it is pretty much nonsensical
8676 		 * to scan for a channel twice, so disallow that
8677 		 * and don't require drivers to check that the
8678 		 * channel list they get isn't longer than what
8679 		 * they can scan, as long as they can scan all
8680 		 * the channels they registered at once.
8681 		 */
8682 		nla_for_each_nested(attr2, freqs, tmp2)
8683 			if (attr1 != attr2 &&
8684 			    nla_get_u32(attr1) == nla_get_u32(attr2))
8685 				return 0;
8686 	}
8687 
8688 	return n_channels;
8689 }
8690 
8691 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
8692 {
8693 	return b < NUM_NL80211_BANDS && wiphy->bands[b];
8694 }
8695 
8696 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
8697 			    struct cfg80211_bss_selection *bss_select)
8698 {
8699 	struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
8700 	struct nlattr *nest;
8701 	int err;
8702 	bool found = false;
8703 	int i;
8704 
8705 	/* only process one nested attribute */
8706 	nest = nla_data(nla);
8707 	if (!nla_ok(nest, nla_len(nest)))
8708 		return -EINVAL;
8709 
8710 	err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
8711 					  nest, nl80211_bss_select_policy,
8712 					  NULL);
8713 	if (err)
8714 		return err;
8715 
8716 	/* only one attribute may be given */
8717 	for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
8718 		if (attr[i]) {
8719 			if (found)
8720 				return -EINVAL;
8721 			found = true;
8722 		}
8723 	}
8724 
8725 	bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
8726 
8727 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
8728 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
8729 
8730 	if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
8731 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
8732 		bss_select->param.band_pref =
8733 			nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
8734 		if (!is_band_valid(wiphy, bss_select->param.band_pref))
8735 			return -EINVAL;
8736 	}
8737 
8738 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
8739 		struct nl80211_bss_select_rssi_adjust *adj_param;
8740 
8741 		adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
8742 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
8743 		bss_select->param.adjust.band = adj_param->band;
8744 		bss_select->param.adjust.delta = adj_param->delta;
8745 		if (!is_band_valid(wiphy, bss_select->param.adjust.band))
8746 			return -EINVAL;
8747 	}
8748 
8749 	/* user-space did not provide behaviour attribute */
8750 	if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
8751 		return -EINVAL;
8752 
8753 	if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
8754 		return -EINVAL;
8755 
8756 	return 0;
8757 }
8758 
8759 int nl80211_parse_random_mac(struct nlattr **attrs,
8760 			     u8 *mac_addr, u8 *mac_addr_mask)
8761 {
8762 	int i;
8763 
8764 	if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
8765 		eth_zero_addr(mac_addr);
8766 		eth_zero_addr(mac_addr_mask);
8767 		mac_addr[0] = 0x2;
8768 		mac_addr_mask[0] = 0x3;
8769 
8770 		return 0;
8771 	}
8772 
8773 	/* need both or none */
8774 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
8775 		return -EINVAL;
8776 
8777 	memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
8778 	memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
8779 
8780 	/* don't allow or configure an mcast address */
8781 	if (!is_multicast_ether_addr(mac_addr_mask) ||
8782 	    is_multicast_ether_addr(mac_addr))
8783 		return -EINVAL;
8784 
8785 	/*
8786 	 * allow users to pass a MAC address that has bits set outside
8787 	 * of the mask, but don't bother drivers with having to deal
8788 	 * with such bits
8789 	 */
8790 	for (i = 0; i < ETH_ALEN; i++)
8791 		mac_addr[i] &= mac_addr_mask[i];
8792 
8793 	return 0;
8794 }
8795 
8796 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev,
8797 					      struct ieee80211_channel *chan)
8798 {
8799 	unsigned int link_id;
8800 	bool all_ok = true;
8801 
8802 	ASSERT_WDEV_LOCK(wdev);
8803 
8804 	if (!cfg80211_beaconing_iface_active(wdev))
8805 		return true;
8806 
8807 	/*
8808 	 * FIXME: check if we have a free HW resource/link for chan
8809 	 *
8810 	 * This, as well as the FIXME below, requires knowing the link
8811 	 * capabilities of the hardware.
8812 	 */
8813 
8814 	/* we cannot leave radar channels */
8815 	for_each_valid_link(wdev, link_id) {
8816 		struct cfg80211_chan_def *chandef;
8817 
8818 		chandef = wdev_chandef(wdev, link_id);
8819 		if (!chandef)
8820 			continue;
8821 
8822 		/*
8823 		 * FIXME: don't require all_ok, but rather check only the
8824 		 *	  correct HW resource/link onto which 'chan' falls,
8825 		 *	  as only that link leaves the channel for doing
8826 		 *	  the off-channel operation.
8827 		 */
8828 
8829 		if (chandef->chan->flags & IEEE80211_CHAN_RADAR)
8830 			all_ok = false;
8831 	}
8832 
8833 	if (all_ok)
8834 		return true;
8835 
8836 	return regulatory_pre_cac_allowed(wdev->wiphy);
8837 }
8838 
8839 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
8840 				    enum nl80211_ext_feature_index feat)
8841 {
8842 	if (!(flags & flag))
8843 		return true;
8844 	if (wiphy_ext_feature_isset(wiphy, feat))
8845 		return true;
8846 	return false;
8847 }
8848 
8849 static int
8850 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
8851 			 void *request, struct nlattr **attrs,
8852 			 bool is_sched_scan)
8853 {
8854 	u8 *mac_addr, *mac_addr_mask;
8855 	u32 *flags;
8856 	enum nl80211_feature_flags randomness_flag;
8857 
8858 	if (!attrs[NL80211_ATTR_SCAN_FLAGS])
8859 		return 0;
8860 
8861 	if (is_sched_scan) {
8862 		struct cfg80211_sched_scan_request *req = request;
8863 
8864 		randomness_flag = wdev ?
8865 				  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
8866 				  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
8867 		flags = &req->flags;
8868 		mac_addr = req->mac_addr;
8869 		mac_addr_mask = req->mac_addr_mask;
8870 	} else {
8871 		struct cfg80211_scan_request *req = request;
8872 
8873 		randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
8874 		flags = &req->flags;
8875 		mac_addr = req->mac_addr;
8876 		mac_addr_mask = req->mac_addr_mask;
8877 	}
8878 
8879 	*flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
8880 
8881 	if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
8882 	     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
8883 	    !nl80211_check_scan_feat(wiphy, *flags,
8884 				     NL80211_SCAN_FLAG_LOW_SPAN,
8885 				     NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
8886 	    !nl80211_check_scan_feat(wiphy, *flags,
8887 				     NL80211_SCAN_FLAG_LOW_POWER,
8888 				     NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
8889 	    !nl80211_check_scan_feat(wiphy, *flags,
8890 				     NL80211_SCAN_FLAG_HIGH_ACCURACY,
8891 				     NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
8892 	    !nl80211_check_scan_feat(wiphy, *flags,
8893 				     NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
8894 				     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
8895 	    !nl80211_check_scan_feat(wiphy, *flags,
8896 				     NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
8897 				     NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
8898 	    !nl80211_check_scan_feat(wiphy, *flags,
8899 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
8900 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
8901 	    !nl80211_check_scan_feat(wiphy, *flags,
8902 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
8903 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
8904 	    !nl80211_check_scan_feat(wiphy, *flags,
8905 				     NL80211_SCAN_FLAG_RANDOM_SN,
8906 				     NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
8907 	    !nl80211_check_scan_feat(wiphy, *flags,
8908 				     NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
8909 				     NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
8910 		return -EOPNOTSUPP;
8911 
8912 	if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
8913 		int err;
8914 
8915 		if (!(wiphy->features & randomness_flag) ||
8916 		    (wdev && wdev->connected))
8917 			return -EOPNOTSUPP;
8918 
8919 		err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
8920 		if (err)
8921 			return err;
8922 	}
8923 
8924 	return 0;
8925 }
8926 
8927 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
8928 {
8929 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8930 	struct wireless_dev *wdev = info->user_ptr[1];
8931 	struct cfg80211_scan_request *request;
8932 	struct nlattr *scan_freqs = NULL;
8933 	bool scan_freqs_khz = false;
8934 	struct nlattr *attr;
8935 	struct wiphy *wiphy;
8936 	int err, tmp, n_ssids = 0, n_channels, i;
8937 	size_t ie_len;
8938 
8939 	wiphy = &rdev->wiphy;
8940 
8941 	if (wdev->iftype == NL80211_IFTYPE_NAN)
8942 		return -EOPNOTSUPP;
8943 
8944 	if (!rdev->ops->scan)
8945 		return -EOPNOTSUPP;
8946 
8947 	if (rdev->scan_req || rdev->scan_msg)
8948 		return -EBUSY;
8949 
8950 	if (info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ]) {
8951 		if (!wiphy_ext_feature_isset(wiphy,
8952 					     NL80211_EXT_FEATURE_SCAN_FREQ_KHZ))
8953 			return -EOPNOTSUPP;
8954 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ];
8955 		scan_freqs_khz = true;
8956 	} else if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES])
8957 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQUENCIES];
8958 
8959 	if (scan_freqs) {
8960 		n_channels = validate_scan_freqs(scan_freqs);
8961 		if (!n_channels)
8962 			return -EINVAL;
8963 	} else {
8964 		n_channels = ieee80211_get_num_supported_channels(wiphy);
8965 	}
8966 
8967 	if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
8968 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
8969 			n_ssids++;
8970 
8971 	if (n_ssids > wiphy->max_scan_ssids)
8972 		return -EINVAL;
8973 
8974 	if (info->attrs[NL80211_ATTR_IE])
8975 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8976 	else
8977 		ie_len = 0;
8978 
8979 	if (ie_len > wiphy->max_scan_ie_len)
8980 		return -EINVAL;
8981 
8982 	request = kzalloc(sizeof(*request)
8983 			+ sizeof(*request->ssids) * n_ssids
8984 			+ sizeof(*request->channels) * n_channels
8985 			+ ie_len, GFP_KERNEL);
8986 	if (!request)
8987 		return -ENOMEM;
8988 
8989 	if (n_ssids)
8990 		request->ssids = (void *)&request->channels[n_channels];
8991 	request->n_ssids = n_ssids;
8992 	if (ie_len) {
8993 		if (n_ssids)
8994 			request->ie = (void *)(request->ssids + n_ssids);
8995 		else
8996 			request->ie = (void *)(request->channels + n_channels);
8997 	}
8998 
8999 	i = 0;
9000 	if (scan_freqs) {
9001 		/* user specified, bail out if channel not found */
9002 		nla_for_each_nested(attr, scan_freqs, tmp) {
9003 			struct ieee80211_channel *chan;
9004 			int freq = nla_get_u32(attr);
9005 
9006 			if (!scan_freqs_khz)
9007 				freq = MHZ_TO_KHZ(freq);
9008 
9009 			chan = ieee80211_get_channel_khz(wiphy, freq);
9010 			if (!chan) {
9011 				err = -EINVAL;
9012 				goto out_free;
9013 			}
9014 
9015 			/* ignore disabled channels */
9016 			if (chan->flags & IEEE80211_CHAN_DISABLED)
9017 				continue;
9018 
9019 			request->channels[i] = chan;
9020 			i++;
9021 		}
9022 	} else {
9023 		enum nl80211_band band;
9024 
9025 		/* all channels */
9026 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
9027 			int j;
9028 
9029 			if (!wiphy->bands[band])
9030 				continue;
9031 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9032 				struct ieee80211_channel *chan;
9033 
9034 				chan = &wiphy->bands[band]->channels[j];
9035 
9036 				if (chan->flags & IEEE80211_CHAN_DISABLED)
9037 					continue;
9038 
9039 				request->channels[i] = chan;
9040 				i++;
9041 			}
9042 		}
9043 	}
9044 
9045 	if (!i) {
9046 		err = -EINVAL;
9047 		goto out_free;
9048 	}
9049 
9050 	request->n_channels = i;
9051 
9052 	wdev_lock(wdev);
9053 	for (i = 0; i < request->n_channels; i++) {
9054 		struct ieee80211_channel *chan = request->channels[i];
9055 
9056 		/* if we can go off-channel to the target channel we're good */
9057 		if (cfg80211_off_channel_oper_allowed(wdev, chan))
9058 			continue;
9059 
9060 		if (!cfg80211_wdev_on_sub_chan(wdev, chan, true)) {
9061 			wdev_unlock(wdev);
9062 			err = -EBUSY;
9063 			goto out_free;
9064 		}
9065 	}
9066 	wdev_unlock(wdev);
9067 
9068 	i = 0;
9069 	if (n_ssids) {
9070 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
9071 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9072 				err = -EINVAL;
9073 				goto out_free;
9074 			}
9075 			request->ssids[i].ssid_len = nla_len(attr);
9076 			memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
9077 			i++;
9078 		}
9079 	}
9080 
9081 	if (info->attrs[NL80211_ATTR_IE]) {
9082 		request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9083 		memcpy((void *)request->ie,
9084 		       nla_data(info->attrs[NL80211_ATTR_IE]),
9085 		       request->ie_len);
9086 	}
9087 
9088 	for (i = 0; i < NUM_NL80211_BANDS; i++)
9089 		if (wiphy->bands[i])
9090 			request->rates[i] =
9091 				(1 << wiphy->bands[i]->n_bitrates) - 1;
9092 
9093 	if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
9094 		nla_for_each_nested(attr,
9095 				    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
9096 				    tmp) {
9097 			enum nl80211_band band = nla_type(attr);
9098 
9099 			if (band < 0 || band >= NUM_NL80211_BANDS) {
9100 				err = -EINVAL;
9101 				goto out_free;
9102 			}
9103 
9104 			if (!wiphy->bands[band])
9105 				continue;
9106 
9107 			err = ieee80211_get_ratemask(wiphy->bands[band],
9108 						     nla_data(attr),
9109 						     nla_len(attr),
9110 						     &request->rates[band]);
9111 			if (err)
9112 				goto out_free;
9113 		}
9114 	}
9115 
9116 	if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
9117 		request->duration =
9118 			nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
9119 		request->duration_mandatory =
9120 			nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
9121 	}
9122 
9123 	err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
9124 				       false);
9125 	if (err)
9126 		goto out_free;
9127 
9128 	request->no_cck =
9129 		nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
9130 
9131 	/* Initial implementation used NL80211_ATTR_MAC to set the specific
9132 	 * BSSID to scan for. This was problematic because that same attribute
9133 	 * was already used for another purpose (local random MAC address). The
9134 	 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
9135 	 * compatibility with older userspace components, also use the
9136 	 * NL80211_ATTR_MAC value here if it can be determined to be used for
9137 	 * the specific BSSID use case instead of the random MAC address
9138 	 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
9139 	 */
9140 	if (info->attrs[NL80211_ATTR_BSSID])
9141 		memcpy(request->bssid,
9142 		       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
9143 	else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
9144 		 info->attrs[NL80211_ATTR_MAC])
9145 		memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
9146 		       ETH_ALEN);
9147 	else
9148 		eth_broadcast_addr(request->bssid);
9149 
9150 	request->wdev = wdev;
9151 	request->wiphy = &rdev->wiphy;
9152 	request->scan_start = jiffies;
9153 
9154 	rdev->scan_req = request;
9155 	err = cfg80211_scan(rdev);
9156 
9157 	if (err)
9158 		goto out_free;
9159 
9160 	nl80211_send_scan_start(rdev, wdev);
9161 	dev_hold(wdev->netdev);
9162 
9163 	return 0;
9164 
9165  out_free:
9166 	rdev->scan_req = NULL;
9167 	kfree(request);
9168 
9169 	return err;
9170 }
9171 
9172 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
9173 {
9174 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9175 	struct wireless_dev *wdev = info->user_ptr[1];
9176 
9177 	if (!rdev->ops->abort_scan)
9178 		return -EOPNOTSUPP;
9179 
9180 	if (rdev->scan_msg)
9181 		return 0;
9182 
9183 	if (!rdev->scan_req)
9184 		return -ENOENT;
9185 
9186 	rdev_abort_scan(rdev, wdev);
9187 	return 0;
9188 }
9189 
9190 static int
9191 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
9192 			       struct cfg80211_sched_scan_request *request,
9193 			       struct nlattr **attrs)
9194 {
9195 	int tmp, err, i = 0;
9196 	struct nlattr *attr;
9197 
9198 	if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9199 		u32 interval;
9200 
9201 		/*
9202 		 * If scan plans are not specified,
9203 		 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
9204 		 * case one scan plan will be set with the specified scan
9205 		 * interval and infinite number of iterations.
9206 		 */
9207 		interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
9208 		if (!interval)
9209 			return -EINVAL;
9210 
9211 		request->scan_plans[0].interval =
9212 			DIV_ROUND_UP(interval, MSEC_PER_SEC);
9213 		if (!request->scan_plans[0].interval)
9214 			return -EINVAL;
9215 
9216 		if (request->scan_plans[0].interval >
9217 		    wiphy->max_sched_scan_plan_interval)
9218 			request->scan_plans[0].interval =
9219 				wiphy->max_sched_scan_plan_interval;
9220 
9221 		return 0;
9222 	}
9223 
9224 	nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
9225 		struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
9226 
9227 		if (WARN_ON(i >= n_plans))
9228 			return -EINVAL;
9229 
9230 		err = nla_parse_nested_deprecated(plan,
9231 						  NL80211_SCHED_SCAN_PLAN_MAX,
9232 						  attr, nl80211_plan_policy,
9233 						  NULL);
9234 		if (err)
9235 			return err;
9236 
9237 		if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
9238 			return -EINVAL;
9239 
9240 		request->scan_plans[i].interval =
9241 			nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
9242 		if (!request->scan_plans[i].interval ||
9243 		    request->scan_plans[i].interval >
9244 		    wiphy->max_sched_scan_plan_interval)
9245 			return -EINVAL;
9246 
9247 		if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
9248 			request->scan_plans[i].iterations =
9249 				nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
9250 			if (!request->scan_plans[i].iterations ||
9251 			    (request->scan_plans[i].iterations >
9252 			     wiphy->max_sched_scan_plan_iterations))
9253 				return -EINVAL;
9254 		} else if (i < n_plans - 1) {
9255 			/*
9256 			 * All scan plans but the last one must specify
9257 			 * a finite number of iterations
9258 			 */
9259 			return -EINVAL;
9260 		}
9261 
9262 		i++;
9263 	}
9264 
9265 	/*
9266 	 * The last scan plan must not specify the number of
9267 	 * iterations, it is supposed to run infinitely
9268 	 */
9269 	if (request->scan_plans[n_plans - 1].iterations)
9270 		return  -EINVAL;
9271 
9272 	return 0;
9273 }
9274 
9275 static int
9276 nl80211_parse_sched_scan_per_band_rssi(struct wiphy *wiphy,
9277 				       struct cfg80211_match_set *match_sets,
9278 				       struct nlattr *tb_band_rssi,
9279 				       s32 rssi_thold)
9280 {
9281 	struct nlattr *attr;
9282 	int i, tmp, ret = 0;
9283 
9284 	if (!wiphy_ext_feature_isset(wiphy,
9285 		    NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD)) {
9286 		if (tb_band_rssi)
9287 			ret = -EOPNOTSUPP;
9288 		else
9289 			for (i = 0; i < NUM_NL80211_BANDS; i++)
9290 				match_sets->per_band_rssi_thold[i] =
9291 					NL80211_SCAN_RSSI_THOLD_OFF;
9292 		return ret;
9293 	}
9294 
9295 	for (i = 0; i < NUM_NL80211_BANDS; i++)
9296 		match_sets->per_band_rssi_thold[i] = rssi_thold;
9297 
9298 	nla_for_each_nested(attr, tb_band_rssi, tmp) {
9299 		enum nl80211_band band = nla_type(attr);
9300 
9301 		if (band < 0 || band >= NUM_NL80211_BANDS)
9302 			return -EINVAL;
9303 
9304 		match_sets->per_band_rssi_thold[band] =	nla_get_s32(attr);
9305 	}
9306 
9307 	return 0;
9308 }
9309 
9310 static struct cfg80211_sched_scan_request *
9311 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
9312 			 struct nlattr **attrs, int max_match_sets)
9313 {
9314 	struct cfg80211_sched_scan_request *request;
9315 	struct nlattr *attr;
9316 	int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
9317 	enum nl80211_band band;
9318 	size_t ie_len;
9319 	struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
9320 	s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
9321 
9322 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9323 		n_channels = validate_scan_freqs(
9324 				attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
9325 		if (!n_channels)
9326 			return ERR_PTR(-EINVAL);
9327 	} else {
9328 		n_channels = ieee80211_get_num_supported_channels(wiphy);
9329 	}
9330 
9331 	if (attrs[NL80211_ATTR_SCAN_SSIDS])
9332 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9333 				    tmp)
9334 			n_ssids++;
9335 
9336 	if (n_ssids > wiphy->max_sched_scan_ssids)
9337 		return ERR_PTR(-EINVAL);
9338 
9339 	/*
9340 	 * First, count the number of 'real' matchsets. Due to an issue with
9341 	 * the old implementation, matchsets containing only the RSSI attribute
9342 	 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
9343 	 * RSSI for all matchsets, rather than their own matchset for reporting
9344 	 * all APs with a strong RSSI. This is needed to be compatible with
9345 	 * older userspace that treated a matchset with only the RSSI as the
9346 	 * global RSSI for all other matchsets - if there are other matchsets.
9347 	 */
9348 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9349 		nla_for_each_nested(attr,
9350 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9351 				    tmp) {
9352 			struct nlattr *rssi;
9353 
9354 			err = nla_parse_nested_deprecated(tb,
9355 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9356 							  attr,
9357 							  nl80211_match_policy,
9358 							  NULL);
9359 			if (err)
9360 				return ERR_PTR(err);
9361 
9362 			/* SSID and BSSID are mutually exclusive */
9363 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
9364 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
9365 				return ERR_PTR(-EINVAL);
9366 
9367 			/* add other standalone attributes here */
9368 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
9369 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
9370 				n_match_sets++;
9371 				continue;
9372 			}
9373 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9374 			if (rssi)
9375 				default_match_rssi = nla_get_s32(rssi);
9376 		}
9377 	}
9378 
9379 	/* However, if there's no other matchset, add the RSSI one */
9380 	if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
9381 		n_match_sets = 1;
9382 
9383 	if (n_match_sets > max_match_sets)
9384 		return ERR_PTR(-EINVAL);
9385 
9386 	if (attrs[NL80211_ATTR_IE])
9387 		ie_len = nla_len(attrs[NL80211_ATTR_IE]);
9388 	else
9389 		ie_len = 0;
9390 
9391 	if (ie_len > wiphy->max_sched_scan_ie_len)
9392 		return ERR_PTR(-EINVAL);
9393 
9394 	if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9395 		/*
9396 		 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
9397 		 * each scan plan already specifies its own interval
9398 		 */
9399 		if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9400 			return ERR_PTR(-EINVAL);
9401 
9402 		nla_for_each_nested(attr,
9403 				    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
9404 			n_plans++;
9405 	} else {
9406 		/*
9407 		 * The scan interval attribute is kept for backward
9408 		 * compatibility. If no scan plans are specified and sched scan
9409 		 * interval is specified, one scan plan will be set with this
9410 		 * scan interval and infinite number of iterations.
9411 		 */
9412 		if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9413 			return ERR_PTR(-EINVAL);
9414 
9415 		n_plans = 1;
9416 	}
9417 
9418 	if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
9419 		return ERR_PTR(-EINVAL);
9420 
9421 	if (!wiphy_ext_feature_isset(
9422 		    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
9423 	    (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
9424 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
9425 		return ERR_PTR(-EINVAL);
9426 
9427 	request = kzalloc(sizeof(*request)
9428 			+ sizeof(*request->ssids) * n_ssids
9429 			+ sizeof(*request->match_sets) * n_match_sets
9430 			+ sizeof(*request->scan_plans) * n_plans
9431 			+ sizeof(*request->channels) * n_channels
9432 			+ ie_len, GFP_KERNEL);
9433 	if (!request)
9434 		return ERR_PTR(-ENOMEM);
9435 
9436 	if (n_ssids)
9437 		request->ssids = (void *)&request->channels[n_channels];
9438 	request->n_ssids = n_ssids;
9439 	if (ie_len) {
9440 		if (n_ssids)
9441 			request->ie = (void *)(request->ssids + n_ssids);
9442 		else
9443 			request->ie = (void *)(request->channels + n_channels);
9444 	}
9445 
9446 	if (n_match_sets) {
9447 		if (request->ie)
9448 			request->match_sets = (void *)(request->ie + ie_len);
9449 		else if (n_ssids)
9450 			request->match_sets =
9451 				(void *)(request->ssids + n_ssids);
9452 		else
9453 			request->match_sets =
9454 				(void *)(request->channels + n_channels);
9455 	}
9456 	request->n_match_sets = n_match_sets;
9457 
9458 	if (n_match_sets)
9459 		request->scan_plans = (void *)(request->match_sets +
9460 					       n_match_sets);
9461 	else if (request->ie)
9462 		request->scan_plans = (void *)(request->ie + ie_len);
9463 	else if (n_ssids)
9464 		request->scan_plans = (void *)(request->ssids + n_ssids);
9465 	else
9466 		request->scan_plans = (void *)(request->channels + n_channels);
9467 
9468 	request->n_scan_plans = n_plans;
9469 
9470 	i = 0;
9471 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9472 		/* user specified, bail out if channel not found */
9473 		nla_for_each_nested(attr,
9474 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
9475 				    tmp) {
9476 			struct ieee80211_channel *chan;
9477 
9478 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
9479 
9480 			if (!chan) {
9481 				err = -EINVAL;
9482 				goto out_free;
9483 			}
9484 
9485 			/* ignore disabled channels */
9486 			if (chan->flags & IEEE80211_CHAN_DISABLED)
9487 				continue;
9488 
9489 			request->channels[i] = chan;
9490 			i++;
9491 		}
9492 	} else {
9493 		/* all channels */
9494 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
9495 			int j;
9496 
9497 			if (!wiphy->bands[band])
9498 				continue;
9499 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9500 				struct ieee80211_channel *chan;
9501 
9502 				chan = &wiphy->bands[band]->channels[j];
9503 
9504 				if (chan->flags & IEEE80211_CHAN_DISABLED)
9505 					continue;
9506 
9507 				request->channels[i] = chan;
9508 				i++;
9509 			}
9510 		}
9511 	}
9512 
9513 	if (!i) {
9514 		err = -EINVAL;
9515 		goto out_free;
9516 	}
9517 
9518 	request->n_channels = i;
9519 
9520 	i = 0;
9521 	if (n_ssids) {
9522 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9523 				    tmp) {
9524 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9525 				err = -EINVAL;
9526 				goto out_free;
9527 			}
9528 			request->ssids[i].ssid_len = nla_len(attr);
9529 			memcpy(request->ssids[i].ssid, nla_data(attr),
9530 			       nla_len(attr));
9531 			i++;
9532 		}
9533 	}
9534 
9535 	i = 0;
9536 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9537 		nla_for_each_nested(attr,
9538 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9539 				    tmp) {
9540 			struct nlattr *ssid, *bssid, *rssi;
9541 
9542 			err = nla_parse_nested_deprecated(tb,
9543 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9544 							  attr,
9545 							  nl80211_match_policy,
9546 							  NULL);
9547 			if (err)
9548 				goto out_free;
9549 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
9550 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
9551 
9552 			if (!ssid && !bssid) {
9553 				i++;
9554 				continue;
9555 			}
9556 
9557 			if (WARN_ON(i >= n_match_sets)) {
9558 				/* this indicates a programming error,
9559 				 * the loop above should have verified
9560 				 * things properly
9561 				 */
9562 				err = -EINVAL;
9563 				goto out_free;
9564 			}
9565 
9566 			if (ssid) {
9567 				memcpy(request->match_sets[i].ssid.ssid,
9568 				       nla_data(ssid), nla_len(ssid));
9569 				request->match_sets[i].ssid.ssid_len =
9570 					nla_len(ssid);
9571 			}
9572 			if (bssid)
9573 				memcpy(request->match_sets[i].bssid,
9574 				       nla_data(bssid), ETH_ALEN);
9575 
9576 			/* special attribute - old implementation w/a */
9577 			request->match_sets[i].rssi_thold = default_match_rssi;
9578 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9579 			if (rssi)
9580 				request->match_sets[i].rssi_thold =
9581 					nla_get_s32(rssi);
9582 
9583 			/* Parse per band RSSI attribute */
9584 			err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
9585 				&request->match_sets[i],
9586 				tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
9587 				request->match_sets[i].rssi_thold);
9588 			if (err)
9589 				goto out_free;
9590 
9591 			i++;
9592 		}
9593 
9594 		/* there was no other matchset, so the RSSI one is alone */
9595 		if (i == 0 && n_match_sets)
9596 			request->match_sets[0].rssi_thold = default_match_rssi;
9597 
9598 		request->min_rssi_thold = INT_MAX;
9599 		for (i = 0; i < n_match_sets; i++)
9600 			request->min_rssi_thold =
9601 				min(request->match_sets[i].rssi_thold,
9602 				    request->min_rssi_thold);
9603 	} else {
9604 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
9605 	}
9606 
9607 	if (ie_len) {
9608 		request->ie_len = ie_len;
9609 		memcpy((void *)request->ie,
9610 		       nla_data(attrs[NL80211_ATTR_IE]),
9611 		       request->ie_len);
9612 	}
9613 
9614 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
9615 	if (err)
9616 		goto out_free;
9617 
9618 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
9619 		request->delay =
9620 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
9621 
9622 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
9623 		request->relative_rssi = nla_get_s8(
9624 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
9625 		request->relative_rssi_set = true;
9626 	}
9627 
9628 	if (request->relative_rssi_set &&
9629 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
9630 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
9631 
9632 		rssi_adjust = nla_data(
9633 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
9634 		request->rssi_adjust.band = rssi_adjust->band;
9635 		request->rssi_adjust.delta = rssi_adjust->delta;
9636 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
9637 			err = -EINVAL;
9638 			goto out_free;
9639 		}
9640 	}
9641 
9642 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
9643 	if (err)
9644 		goto out_free;
9645 
9646 	request->scan_start = jiffies;
9647 
9648 	return request;
9649 
9650 out_free:
9651 	kfree(request);
9652 	return ERR_PTR(err);
9653 }
9654 
9655 static int nl80211_start_sched_scan(struct sk_buff *skb,
9656 				    struct genl_info *info)
9657 {
9658 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9659 	struct net_device *dev = info->user_ptr[1];
9660 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9661 	struct cfg80211_sched_scan_request *sched_scan_req;
9662 	bool want_multi;
9663 	int err;
9664 
9665 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
9666 		return -EOPNOTSUPP;
9667 
9668 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
9669 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
9670 	if (err)
9671 		return err;
9672 
9673 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
9674 						  info->attrs,
9675 						  rdev->wiphy.max_match_sets);
9676 
9677 	err = PTR_ERR_OR_ZERO(sched_scan_req);
9678 	if (err)
9679 		goto out_err;
9680 
9681 	/* leave request id zero for legacy request
9682 	 * or if driver does not support multi-scheduled scan
9683 	 */
9684 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1)
9685 		sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
9686 
9687 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
9688 	if (err)
9689 		goto out_free;
9690 
9691 	sched_scan_req->dev = dev;
9692 	sched_scan_req->wiphy = &rdev->wiphy;
9693 
9694 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9695 		sched_scan_req->owner_nlportid = info->snd_portid;
9696 
9697 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
9698 
9699 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
9700 	return 0;
9701 
9702 out_free:
9703 	kfree(sched_scan_req);
9704 out_err:
9705 	return err;
9706 }
9707 
9708 static int nl80211_stop_sched_scan(struct sk_buff *skb,
9709 				   struct genl_info *info)
9710 {
9711 	struct cfg80211_sched_scan_request *req;
9712 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9713 	u64 cookie;
9714 
9715 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
9716 		return -EOPNOTSUPP;
9717 
9718 	if (info->attrs[NL80211_ATTR_COOKIE]) {
9719 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9720 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
9721 	}
9722 
9723 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
9724 				     struct cfg80211_sched_scan_request,
9725 				     list);
9726 	if (!req || req->reqid ||
9727 	    (req->owner_nlportid &&
9728 	     req->owner_nlportid != info->snd_portid))
9729 		return -ENOENT;
9730 
9731 	return cfg80211_stop_sched_scan_req(rdev, req, false);
9732 }
9733 
9734 static int nl80211_start_radar_detection(struct sk_buff *skb,
9735 					 struct genl_info *info)
9736 {
9737 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9738 	struct net_device *dev = info->user_ptr[1];
9739 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9740 	struct wiphy *wiphy = wdev->wiphy;
9741 	struct cfg80211_chan_def chandef;
9742 	enum nl80211_dfs_regions dfs_region;
9743 	unsigned int cac_time_ms;
9744 	int err = -EINVAL;
9745 
9746 	flush_delayed_work(&rdev->dfs_update_channels_wk);
9747 
9748 	wiphy_lock(wiphy);
9749 
9750 	dfs_region = reg_get_dfs_region(wiphy);
9751 	if (dfs_region == NL80211_DFS_UNSET)
9752 		goto unlock;
9753 
9754 	err = nl80211_parse_chandef(rdev, info, &chandef);
9755 	if (err)
9756 		goto unlock;
9757 
9758 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
9759 	if (err < 0)
9760 		goto unlock;
9761 
9762 	if (err == 0) {
9763 		err = -EINVAL;
9764 		goto unlock;
9765 	}
9766 
9767 	if (!cfg80211_chandef_dfs_usable(wiphy, &chandef)) {
9768 		err = -EINVAL;
9769 		goto unlock;
9770 	}
9771 
9772 	if (nla_get_flag(info->attrs[NL80211_ATTR_RADAR_BACKGROUND])) {
9773 		err = cfg80211_start_background_radar_detection(rdev, wdev,
9774 								&chandef);
9775 		goto unlock;
9776 	}
9777 
9778 	if (netif_carrier_ok(dev)) {
9779 		err = -EBUSY;
9780 		goto unlock;
9781 	}
9782 
9783 	if (wdev->cac_started) {
9784 		err = -EBUSY;
9785 		goto unlock;
9786 	}
9787 
9788 	/* CAC start is offloaded to HW and can't be started manually */
9789 	if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD)) {
9790 		err = -EOPNOTSUPP;
9791 		goto unlock;
9792 	}
9793 
9794 	if (!rdev->ops->start_radar_detection) {
9795 		err = -EOPNOTSUPP;
9796 		goto unlock;
9797 	}
9798 
9799 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
9800 	if (WARN_ON(!cac_time_ms))
9801 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
9802 
9803 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
9804 	if (!err) {
9805 		wdev->links[0].ap.chandef = chandef;
9806 		wdev->cac_started = true;
9807 		wdev->cac_start_time = jiffies;
9808 		wdev->cac_time_ms = cac_time_ms;
9809 	}
9810 unlock:
9811 	wiphy_unlock(wiphy);
9812 
9813 	return err;
9814 }
9815 
9816 static int nl80211_notify_radar_detection(struct sk_buff *skb,
9817 					  struct genl_info *info)
9818 {
9819 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9820 	struct net_device *dev = info->user_ptr[1];
9821 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9822 	struct wiphy *wiphy = wdev->wiphy;
9823 	struct cfg80211_chan_def chandef;
9824 	enum nl80211_dfs_regions dfs_region;
9825 	int err;
9826 
9827 	dfs_region = reg_get_dfs_region(wiphy);
9828 	if (dfs_region == NL80211_DFS_UNSET) {
9829 		GENL_SET_ERR_MSG(info,
9830 				 "DFS Region is not set. Unexpected Radar indication");
9831 		return -EINVAL;
9832 	}
9833 
9834 	err = nl80211_parse_chandef(rdev, info, &chandef);
9835 	if (err) {
9836 		GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
9837 		return err;
9838 	}
9839 
9840 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
9841 	if (err < 0) {
9842 		GENL_SET_ERR_MSG(info, "chandef is invalid");
9843 		return err;
9844 	}
9845 
9846 	if (err == 0) {
9847 		GENL_SET_ERR_MSG(info,
9848 				 "Unexpected Radar indication for chandef/iftype");
9849 		return -EINVAL;
9850 	}
9851 
9852 	/* Do not process this notification if radar is already detected
9853 	 * by kernel on this channel, and return success.
9854 	 */
9855 	if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
9856 		return 0;
9857 
9858 	cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
9859 
9860 	cfg80211_sched_dfs_chan_update(rdev);
9861 
9862 	rdev->radar_chandef = chandef;
9863 
9864 	/* Propagate this notification to other radios as well */
9865 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
9866 
9867 	return 0;
9868 }
9869 
9870 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
9871 {
9872 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9873 	unsigned int link_id = nl80211_link_id(info->attrs);
9874 	struct net_device *dev = info->user_ptr[1];
9875 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9876 	struct cfg80211_csa_settings params;
9877 	struct nlattr **csa_attrs = NULL;
9878 	int err;
9879 	bool need_new_beacon = false;
9880 	bool need_handle_dfs_flag = true;
9881 	int len, i;
9882 	u32 cs_count;
9883 
9884 	if (!rdev->ops->channel_switch ||
9885 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
9886 		return -EOPNOTSUPP;
9887 
9888 	switch (dev->ieee80211_ptr->iftype) {
9889 	case NL80211_IFTYPE_AP:
9890 	case NL80211_IFTYPE_P2P_GO:
9891 		need_new_beacon = true;
9892 		/* For all modes except AP the handle_dfs flag needs to be
9893 		 * supplied to tell the kernel that userspace will handle radar
9894 		 * events when they happen. Otherwise a switch to a channel
9895 		 * requiring DFS will be rejected.
9896 		 */
9897 		need_handle_dfs_flag = false;
9898 
9899 		/* useless if AP is not running */
9900 		if (!wdev->links[link_id].ap.beacon_interval)
9901 			return -ENOTCONN;
9902 		break;
9903 	case NL80211_IFTYPE_ADHOC:
9904 		if (!wdev->u.ibss.ssid_len)
9905 			return -ENOTCONN;
9906 		break;
9907 	case NL80211_IFTYPE_MESH_POINT:
9908 		if (!wdev->u.mesh.id_len)
9909 			return -ENOTCONN;
9910 		break;
9911 	default:
9912 		return -EOPNOTSUPP;
9913 	}
9914 
9915 	memset(&params, 0, sizeof(params));
9916 	params.beacon_csa.ftm_responder = -1;
9917 
9918 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
9919 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
9920 		return -EINVAL;
9921 
9922 	/* only important for AP, IBSS and mesh create IEs internally */
9923 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
9924 		return -EINVAL;
9925 
9926 	/* Even though the attribute is u32, the specification says
9927 	 * u8, so let's make sure we don't overflow.
9928 	 */
9929 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
9930 	if (cs_count > 255)
9931 		return -EINVAL;
9932 
9933 	params.count = cs_count;
9934 
9935 	if (!need_new_beacon)
9936 		goto skip_beacons;
9937 
9938 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after);
9939 	if (err)
9940 		goto free;
9941 
9942 	csa_attrs = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*csa_attrs),
9943 			    GFP_KERNEL);
9944 	if (!csa_attrs) {
9945 		err = -ENOMEM;
9946 		goto free;
9947 	}
9948 
9949 	err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
9950 					  info->attrs[NL80211_ATTR_CSA_IES],
9951 					  nl80211_policy, info->extack);
9952 	if (err)
9953 		goto free;
9954 
9955 	err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa);
9956 	if (err)
9957 		goto free;
9958 
9959 	if (!csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
9960 		err = -EINVAL;
9961 		goto free;
9962 	}
9963 
9964 	len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
9965 	if (!len || (len % sizeof(u16))) {
9966 		err = -EINVAL;
9967 		goto free;
9968 	}
9969 
9970 	params.n_counter_offsets_beacon = len / sizeof(u16);
9971 	if (rdev->wiphy.max_num_csa_counters &&
9972 	    (params.n_counter_offsets_beacon >
9973 	     rdev->wiphy.max_num_csa_counters)) {
9974 		err = -EINVAL;
9975 		goto free;
9976 	}
9977 
9978 	params.counter_offsets_beacon =
9979 		nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
9980 
9981 	/* sanity checks - counters should fit and be the same */
9982 	for (i = 0; i < params.n_counter_offsets_beacon; i++) {
9983 		u16 offset = params.counter_offsets_beacon[i];
9984 
9985 		if (offset >= params.beacon_csa.tail_len) {
9986 			err = -EINVAL;
9987 			goto free;
9988 		}
9989 
9990 		if (params.beacon_csa.tail[offset] != params.count) {
9991 			err = -EINVAL;
9992 			goto free;
9993 		}
9994 	}
9995 
9996 	if (csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
9997 		len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
9998 		if (!len || (len % sizeof(u16))) {
9999 			err = -EINVAL;
10000 			goto free;
10001 		}
10002 
10003 		params.n_counter_offsets_presp = len / sizeof(u16);
10004 		if (rdev->wiphy.max_num_csa_counters &&
10005 		    (params.n_counter_offsets_presp >
10006 		     rdev->wiphy.max_num_csa_counters)) {
10007 			err = -EINVAL;
10008 			goto free;
10009 		}
10010 
10011 		params.counter_offsets_presp =
10012 			nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
10013 
10014 		/* sanity checks - counters should fit and be the same */
10015 		for (i = 0; i < params.n_counter_offsets_presp; i++) {
10016 			u16 offset = params.counter_offsets_presp[i];
10017 
10018 			if (offset >= params.beacon_csa.probe_resp_len) {
10019 				err = -EINVAL;
10020 				goto free;
10021 			}
10022 
10023 			if (params.beacon_csa.probe_resp[offset] !=
10024 			    params.count) {
10025 				err = -EINVAL;
10026 				goto free;
10027 			}
10028 		}
10029 	}
10030 
10031 skip_beacons:
10032 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
10033 	if (err)
10034 		goto free;
10035 
10036 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
10037 					   wdev->iftype)) {
10038 		err = -EINVAL;
10039 		goto free;
10040 	}
10041 
10042 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
10043 					    &params.chandef,
10044 					    wdev->iftype);
10045 	if (err < 0)
10046 		goto free;
10047 
10048 	if (err > 0) {
10049 		params.radar_required = true;
10050 		if (need_handle_dfs_flag &&
10051 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
10052 			err = -EINVAL;
10053 			goto free;
10054 		}
10055 	}
10056 
10057 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
10058 		params.block_tx = true;
10059 
10060 	wdev_lock(wdev);
10061 	err = rdev_channel_switch(rdev, dev, &params);
10062 	wdev_unlock(wdev);
10063 
10064 free:
10065 	kfree(params.beacon_after.mbssid_ies);
10066 	kfree(params.beacon_csa.mbssid_ies);
10067 	kfree(csa_attrs);
10068 	return err;
10069 }
10070 
10071 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
10072 			    u32 seq, int flags,
10073 			    struct cfg80211_registered_device *rdev,
10074 			    struct wireless_dev *wdev,
10075 			    struct cfg80211_internal_bss *intbss)
10076 {
10077 	struct cfg80211_bss *res = &intbss->pub;
10078 	const struct cfg80211_bss_ies *ies;
10079 	unsigned int link_id;
10080 	void *hdr;
10081 	struct nlattr *bss;
10082 
10083 	ASSERT_WDEV_LOCK(wdev);
10084 
10085 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
10086 			     NL80211_CMD_NEW_SCAN_RESULTS);
10087 	if (!hdr)
10088 		return -1;
10089 
10090 	genl_dump_check_consistent(cb, hdr);
10091 
10092 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
10093 		goto nla_put_failure;
10094 	if (wdev->netdev &&
10095 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
10096 		goto nla_put_failure;
10097 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
10098 			      NL80211_ATTR_PAD))
10099 		goto nla_put_failure;
10100 
10101 	bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
10102 	if (!bss)
10103 		goto nla_put_failure;
10104 	if ((!is_zero_ether_addr(res->bssid) &&
10105 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
10106 		goto nla_put_failure;
10107 
10108 	rcu_read_lock();
10109 	/* indicate whether we have probe response data or not */
10110 	if (rcu_access_pointer(res->proberesp_ies) &&
10111 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
10112 		goto fail_unlock_rcu;
10113 
10114 	/* this pointer prefers to be pointed to probe response data
10115 	 * but is always valid
10116 	 */
10117 	ies = rcu_dereference(res->ies);
10118 	if (ies) {
10119 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
10120 				      NL80211_BSS_PAD))
10121 			goto fail_unlock_rcu;
10122 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
10123 					ies->len, ies->data))
10124 			goto fail_unlock_rcu;
10125 	}
10126 
10127 	/* and this pointer is always (unless driver didn't know) beacon data */
10128 	ies = rcu_dereference(res->beacon_ies);
10129 	if (ies && ies->from_beacon) {
10130 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
10131 				      NL80211_BSS_PAD))
10132 			goto fail_unlock_rcu;
10133 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
10134 					ies->len, ies->data))
10135 			goto fail_unlock_rcu;
10136 	}
10137 	rcu_read_unlock();
10138 
10139 	if (res->beacon_interval &&
10140 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
10141 		goto nla_put_failure;
10142 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
10143 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
10144 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY_OFFSET,
10145 			res->channel->freq_offset) ||
10146 	    nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
10147 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
10148 			jiffies_to_msecs(jiffies - intbss->ts)))
10149 		goto nla_put_failure;
10150 
10151 	if (intbss->parent_tsf &&
10152 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
10153 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
10154 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
10155 		     intbss->parent_bssid)))
10156 		goto nla_put_failure;
10157 
10158 	if (intbss->ts_boottime &&
10159 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
10160 			      intbss->ts_boottime, NL80211_BSS_PAD))
10161 		goto nla_put_failure;
10162 
10163 	if (!nl80211_put_signal(msg, intbss->pub.chains,
10164 				intbss->pub.chain_signal,
10165 				NL80211_BSS_CHAIN_SIGNAL))
10166 		goto nla_put_failure;
10167 
10168 	switch (rdev->wiphy.signal_type) {
10169 	case CFG80211_SIGNAL_TYPE_MBM:
10170 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
10171 			goto nla_put_failure;
10172 		break;
10173 	case CFG80211_SIGNAL_TYPE_UNSPEC:
10174 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
10175 			goto nla_put_failure;
10176 		break;
10177 	default:
10178 		break;
10179 	}
10180 
10181 	switch (wdev->iftype) {
10182 	case NL80211_IFTYPE_P2P_CLIENT:
10183 	case NL80211_IFTYPE_STATION:
10184 		for_each_valid_link(wdev, link_id) {
10185 			if (intbss == wdev->links[link_id].client.current_bss &&
10186 			    (nla_put_u32(msg, NL80211_BSS_STATUS,
10187 					 NL80211_BSS_STATUS_ASSOCIATED) ||
10188 			     (wdev->valid_links &&
10189 			      (nla_put_u8(msg, NL80211_BSS_MLO_LINK_ID,
10190 					  link_id) ||
10191 			       nla_put(msg, NL80211_BSS_MLD_ADDR, ETH_ALEN,
10192 				       wdev->u.client.connected_addr)))))
10193 				goto nla_put_failure;
10194 		}
10195 		break;
10196 	case NL80211_IFTYPE_ADHOC:
10197 		if (intbss == wdev->u.ibss.current_bss &&
10198 		    nla_put_u32(msg, NL80211_BSS_STATUS,
10199 				NL80211_BSS_STATUS_IBSS_JOINED))
10200 			goto nla_put_failure;
10201 		break;
10202 	default:
10203 		break;
10204 	}
10205 
10206 	nla_nest_end(msg, bss);
10207 
10208 	genlmsg_end(msg, hdr);
10209 	return 0;
10210 
10211  fail_unlock_rcu:
10212 	rcu_read_unlock();
10213  nla_put_failure:
10214 	genlmsg_cancel(msg, hdr);
10215 	return -EMSGSIZE;
10216 }
10217 
10218 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
10219 {
10220 	struct cfg80211_registered_device *rdev;
10221 	struct cfg80211_internal_bss *scan;
10222 	struct wireless_dev *wdev;
10223 	int start = cb->args[2], idx = 0;
10224 	int err;
10225 
10226 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
10227 	if (err)
10228 		return err;
10229 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10230 	__acquire(&rdev->wiphy.mtx);
10231 
10232 	wdev_lock(wdev);
10233 	spin_lock_bh(&rdev->bss_lock);
10234 
10235 	/*
10236 	 * dump_scan will be called multiple times to break up the scan results
10237 	 * into multiple messages.  It is unlikely that any more bss-es will be
10238 	 * expired after the first call, so only call only call this on the
10239 	 * first dump_scan invocation.
10240 	 */
10241 	if (start == 0)
10242 		cfg80211_bss_expire(rdev);
10243 
10244 	cb->seq = rdev->bss_generation;
10245 
10246 	list_for_each_entry(scan, &rdev->bss_list, list) {
10247 		if (++idx <= start)
10248 			continue;
10249 		if (nl80211_send_bss(skb, cb,
10250 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10251 				rdev, wdev, scan) < 0) {
10252 			idx--;
10253 			break;
10254 		}
10255 	}
10256 
10257 	spin_unlock_bh(&rdev->bss_lock);
10258 	wdev_unlock(wdev);
10259 
10260 	cb->args[2] = idx;
10261 	wiphy_unlock(&rdev->wiphy);
10262 
10263 	return skb->len;
10264 }
10265 
10266 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
10267 			       int flags, struct net_device *dev,
10268 			       bool allow_radio_stats,
10269 			       struct survey_info *survey)
10270 {
10271 	void *hdr;
10272 	struct nlattr *infoattr;
10273 
10274 	/* skip radio stats if userspace didn't request them */
10275 	if (!survey->channel && !allow_radio_stats)
10276 		return 0;
10277 
10278 	hdr = nl80211hdr_put(msg, portid, seq, flags,
10279 			     NL80211_CMD_NEW_SURVEY_RESULTS);
10280 	if (!hdr)
10281 		return -ENOMEM;
10282 
10283 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
10284 		goto nla_put_failure;
10285 
10286 	infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
10287 	if (!infoattr)
10288 		goto nla_put_failure;
10289 
10290 	if (survey->channel &&
10291 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
10292 			survey->channel->center_freq))
10293 		goto nla_put_failure;
10294 
10295 	if (survey->channel && survey->channel->freq_offset &&
10296 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY_OFFSET,
10297 			survey->channel->freq_offset))
10298 		goto nla_put_failure;
10299 
10300 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
10301 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
10302 		goto nla_put_failure;
10303 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
10304 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
10305 		goto nla_put_failure;
10306 	if ((survey->filled & SURVEY_INFO_TIME) &&
10307 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
10308 			survey->time, NL80211_SURVEY_INFO_PAD))
10309 		goto nla_put_failure;
10310 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
10311 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
10312 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
10313 		goto nla_put_failure;
10314 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
10315 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
10316 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
10317 		goto nla_put_failure;
10318 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
10319 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
10320 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
10321 		goto nla_put_failure;
10322 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
10323 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
10324 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
10325 		goto nla_put_failure;
10326 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
10327 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
10328 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
10329 		goto nla_put_failure;
10330 	if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
10331 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
10332 			      survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
10333 		goto nla_put_failure;
10334 
10335 	nla_nest_end(msg, infoattr);
10336 
10337 	genlmsg_end(msg, hdr);
10338 	return 0;
10339 
10340  nla_put_failure:
10341 	genlmsg_cancel(msg, hdr);
10342 	return -EMSGSIZE;
10343 }
10344 
10345 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
10346 {
10347 	struct nlattr **attrbuf;
10348 	struct survey_info survey;
10349 	struct cfg80211_registered_device *rdev;
10350 	struct wireless_dev *wdev;
10351 	int survey_idx = cb->args[2];
10352 	int res;
10353 	bool radio_stats;
10354 
10355 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
10356 	if (!attrbuf)
10357 		return -ENOMEM;
10358 
10359 	res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, attrbuf);
10360 	if (res) {
10361 		kfree(attrbuf);
10362 		return res;
10363 	}
10364 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10365 	__acquire(&rdev->wiphy.mtx);
10366 
10367 	/* prepare_wdev_dump parsed the attributes */
10368 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
10369 
10370 	if (!wdev->netdev) {
10371 		res = -EINVAL;
10372 		goto out_err;
10373 	}
10374 
10375 	if (!rdev->ops->dump_survey) {
10376 		res = -EOPNOTSUPP;
10377 		goto out_err;
10378 	}
10379 
10380 	while (1) {
10381 		wdev_lock(wdev);
10382 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
10383 		wdev_unlock(wdev);
10384 		if (res == -ENOENT)
10385 			break;
10386 		if (res)
10387 			goto out_err;
10388 
10389 		/* don't send disabled channels, but do send non-channel data */
10390 		if (survey.channel &&
10391 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
10392 			survey_idx++;
10393 			continue;
10394 		}
10395 
10396 		if (nl80211_send_survey(skb,
10397 				NETLINK_CB(cb->skb).portid,
10398 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10399 				wdev->netdev, radio_stats, &survey) < 0)
10400 			goto out;
10401 		survey_idx++;
10402 	}
10403 
10404  out:
10405 	cb->args[2] = survey_idx;
10406 	res = skb->len;
10407  out_err:
10408 	kfree(attrbuf);
10409 	wiphy_unlock(&rdev->wiphy);
10410 	return res;
10411 }
10412 
10413 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
10414 {
10415 	return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
10416 				  NL80211_WPA_VERSION_2 |
10417 				  NL80211_WPA_VERSION_3));
10418 }
10419 
10420 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
10421 {
10422 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10423 	struct net_device *dev = info->user_ptr[1];
10424 	struct ieee80211_channel *chan;
10425 	const u8 *bssid, *ssid;
10426 	int err, ssid_len;
10427 	enum nl80211_auth_type auth_type;
10428 	struct key_parse key;
10429 	bool local_state_change;
10430 	struct cfg80211_auth_request req = {};
10431 	u32 freq;
10432 
10433 	if (!info->attrs[NL80211_ATTR_MAC])
10434 		return -EINVAL;
10435 
10436 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
10437 		return -EINVAL;
10438 
10439 	if (!info->attrs[NL80211_ATTR_SSID])
10440 		return -EINVAL;
10441 
10442 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10443 		return -EINVAL;
10444 
10445 	err = nl80211_parse_key(info, &key);
10446 	if (err)
10447 		return err;
10448 
10449 	if (key.idx >= 0) {
10450 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
10451 			return -EINVAL;
10452 		if (!key.p.key || !key.p.key_len)
10453 			return -EINVAL;
10454 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
10455 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
10456 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
10457 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
10458 			return -EINVAL;
10459 		if (key.idx > 3)
10460 			return -EINVAL;
10461 	} else {
10462 		key.p.key_len = 0;
10463 		key.p.key = NULL;
10464 	}
10465 
10466 	if (key.idx >= 0) {
10467 		int i;
10468 		bool ok = false;
10469 
10470 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
10471 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
10472 				ok = true;
10473 				break;
10474 			}
10475 		}
10476 		if (!ok)
10477 			return -EINVAL;
10478 	}
10479 
10480 	if (!rdev->ops->auth)
10481 		return -EOPNOTSUPP;
10482 
10483 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10484 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10485 		return -EOPNOTSUPP;
10486 
10487 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10488 	freq = MHZ_TO_KHZ(nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
10489 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10490 		freq +=
10491 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10492 
10493 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10494 	if (!chan)
10495 		return -EINVAL;
10496 
10497 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10498 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10499 
10500 	if (info->attrs[NL80211_ATTR_IE]) {
10501 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10502 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10503 	}
10504 
10505 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10506 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
10507 		return -EINVAL;
10508 
10509 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
10510 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
10511 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
10512 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
10513 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
10514 		return -EINVAL;
10515 
10516 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
10517 		if (auth_type != NL80211_AUTHTYPE_SAE &&
10518 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
10519 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
10520 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
10521 			return -EINVAL;
10522 		req.auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
10523 		req.auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
10524 	}
10525 
10526 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
10527 
10528 	/*
10529 	 * Since we no longer track auth state, ignore
10530 	 * requests to only change local state.
10531 	 */
10532 	if (local_state_change)
10533 		return 0;
10534 
10535 	req.auth_type = auth_type;
10536 	req.key = key.p.key;
10537 	req.key_len = key.p.key_len;
10538 	req.key_idx = key.idx;
10539 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
10540 	if (req.link_id >= 0) {
10541 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
10542 			return -EINVAL;
10543 		if (!info->attrs[NL80211_ATTR_MLD_ADDR])
10544 			return -EINVAL;
10545 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
10546 	}
10547 
10548 	req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
10549 				   IEEE80211_BSS_TYPE_ESS,
10550 				   IEEE80211_PRIVACY_ANY);
10551 	if (!req.bss)
10552 		return -ENOENT;
10553 
10554 	wdev_lock(dev->ieee80211_ptr);
10555 	err = cfg80211_mlme_auth(rdev, dev, &req);
10556 	wdev_unlock(dev->ieee80211_ptr);
10557 
10558 	cfg80211_put_bss(&rdev->wiphy, req.bss);
10559 
10560 	return err;
10561 }
10562 
10563 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
10564 				     struct genl_info *info)
10565 {
10566 	if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10567 		GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
10568 		return -EINVAL;
10569 	}
10570 
10571 	if (!rdev->ops->tx_control_port ||
10572 	    !wiphy_ext_feature_isset(&rdev->wiphy,
10573 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
10574 		return -EOPNOTSUPP;
10575 
10576 	return 0;
10577 }
10578 
10579 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
10580 				   struct genl_info *info,
10581 				   struct cfg80211_crypto_settings *settings,
10582 				   int cipher_limit)
10583 {
10584 	memset(settings, 0, sizeof(*settings));
10585 
10586 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
10587 
10588 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
10589 		u16 proto;
10590 
10591 		proto = nla_get_u16(
10592 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
10593 		settings->control_port_ethertype = cpu_to_be16(proto);
10594 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
10595 		    proto != ETH_P_PAE)
10596 			return -EINVAL;
10597 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
10598 			settings->control_port_no_encrypt = true;
10599 	} else
10600 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
10601 
10602 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
10603 		int r = validate_pae_over_nl80211(rdev, info);
10604 
10605 		if (r < 0)
10606 			return r;
10607 
10608 		settings->control_port_over_nl80211 = true;
10609 
10610 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH])
10611 			settings->control_port_no_preauth = true;
10612 	}
10613 
10614 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
10615 		void *data;
10616 		int len, i;
10617 
10618 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10619 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10620 		settings->n_ciphers_pairwise = len / sizeof(u32);
10621 
10622 		if (len % sizeof(u32))
10623 			return -EINVAL;
10624 
10625 		if (settings->n_ciphers_pairwise > cipher_limit)
10626 			return -EINVAL;
10627 
10628 		memcpy(settings->ciphers_pairwise, data, len);
10629 
10630 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
10631 			if (!cfg80211_supported_cipher_suite(
10632 					&rdev->wiphy,
10633 					settings->ciphers_pairwise[i]))
10634 				return -EINVAL;
10635 	}
10636 
10637 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
10638 		settings->cipher_group =
10639 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
10640 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
10641 						     settings->cipher_group))
10642 			return -EINVAL;
10643 	}
10644 
10645 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
10646 		settings->wpa_versions =
10647 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
10648 		if (!nl80211_valid_wpa_versions(settings->wpa_versions))
10649 			return -EINVAL;
10650 	}
10651 
10652 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
10653 		void *data;
10654 		int len;
10655 
10656 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
10657 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
10658 		settings->n_akm_suites = len / sizeof(u32);
10659 
10660 		if (len % sizeof(u32))
10661 			return -EINVAL;
10662 
10663 		if (settings->n_akm_suites > rdev->wiphy.max_num_akm_suites)
10664 			return -EINVAL;
10665 
10666 		memcpy(settings->akm_suites, data, len);
10667 	}
10668 
10669 	if (info->attrs[NL80211_ATTR_PMK]) {
10670 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
10671 			return -EINVAL;
10672 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10673 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK) &&
10674 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10675 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_AP_PSK))
10676 			return -EINVAL;
10677 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10678 	}
10679 
10680 	if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
10681 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10682 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
10683 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10684 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP))
10685 			return -EINVAL;
10686 		settings->sae_pwd =
10687 			nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10688 		settings->sae_pwd_len =
10689 			nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10690 	}
10691 
10692 	if (info->attrs[NL80211_ATTR_SAE_PWE])
10693 		settings->sae_pwe =
10694 			nla_get_u8(info->attrs[NL80211_ATTR_SAE_PWE]);
10695 	else
10696 		settings->sae_pwe = NL80211_SAE_PWE_UNSPECIFIED;
10697 
10698 	return 0;
10699 }
10700 
10701 static struct cfg80211_bss *nl80211_assoc_bss(struct cfg80211_registered_device *rdev,
10702 					      const u8 *ssid, int ssid_len,
10703 					      struct nlattr **attrs,
10704 					      const u8 **bssid_out)
10705 {
10706 	struct ieee80211_channel *chan;
10707 	struct cfg80211_bss *bss;
10708 	const u8 *bssid;
10709 	u32 freq;
10710 
10711 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_WIPHY_FREQ])
10712 		return ERR_PTR(-EINVAL);
10713 
10714 	bssid = nla_data(attrs[NL80211_ATTR_MAC]);
10715 
10716 	freq = MHZ_TO_KHZ(nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ]));
10717 	if (attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10718 		freq += nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10719 
10720 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10721 	if (!chan)
10722 		return ERR_PTR(-EINVAL);
10723 
10724 	bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid,
10725 			       ssid, ssid_len,
10726 			       IEEE80211_BSS_TYPE_ESS,
10727 			       IEEE80211_PRIVACY_ANY);
10728 	if (!bss)
10729 		return ERR_PTR(-ENOENT);
10730 
10731 	*bssid_out = bssid;
10732 	return bss;
10733 }
10734 
10735 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
10736 {
10737 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10738 	struct net_device *dev = info->user_ptr[1];
10739 	struct cfg80211_assoc_request req = {};
10740 	struct nlattr **attrs = NULL;
10741 	const u8 *bssid, *ssid;
10742 	unsigned int link_id;
10743 	int err, ssid_len;
10744 
10745 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
10746 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10747 		return -EPERM;
10748 
10749 	if (!info->attrs[NL80211_ATTR_SSID])
10750 		return -EINVAL;
10751 
10752 	if (!rdev->ops->assoc)
10753 		return -EOPNOTSUPP;
10754 
10755 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10756 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10757 		return -EOPNOTSUPP;
10758 
10759 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10760 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10761 
10762 	if (info->attrs[NL80211_ATTR_IE]) {
10763 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10764 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10765 
10766 		if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
10767 					   req.ie, req.ie_len)) {
10768 			GENL_SET_ERR_MSG(info,
10769 					 "non-inheritance makes no sense");
10770 			return -EINVAL;
10771 		}
10772 	}
10773 
10774 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
10775 		enum nl80211_mfp mfp =
10776 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
10777 		if (mfp == NL80211_MFP_REQUIRED)
10778 			req.use_mfp = true;
10779 		else if (mfp != NL80211_MFP_NO)
10780 			return -EINVAL;
10781 	}
10782 
10783 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
10784 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
10785 
10786 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
10787 		req.flags |= ASSOC_REQ_DISABLE_HT;
10788 
10789 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10790 		memcpy(&req.ht_capa_mask,
10791 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
10792 		       sizeof(req.ht_capa_mask));
10793 
10794 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
10795 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10796 			return -EINVAL;
10797 		memcpy(&req.ht_capa,
10798 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
10799 		       sizeof(req.ht_capa));
10800 	}
10801 
10802 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
10803 		req.flags |= ASSOC_REQ_DISABLE_VHT;
10804 
10805 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
10806 		req.flags |= ASSOC_REQ_DISABLE_HE;
10807 
10808 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
10809 		req.flags |= ASSOC_REQ_DISABLE_EHT;
10810 
10811 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10812 		memcpy(&req.vht_capa_mask,
10813 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
10814 		       sizeof(req.vht_capa_mask));
10815 
10816 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
10817 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10818 			return -EINVAL;
10819 		memcpy(&req.vht_capa,
10820 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
10821 		       sizeof(req.vht_capa));
10822 	}
10823 
10824 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
10825 		if (!((rdev->wiphy.features &
10826 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
10827 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
10828 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10829 					     NL80211_EXT_FEATURE_RRM))
10830 			return -EINVAL;
10831 		req.flags |= ASSOC_REQ_USE_RRM;
10832 	}
10833 
10834 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
10835 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
10836 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
10837 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
10838 			return -EINVAL;
10839 		req.fils_nonces =
10840 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
10841 	}
10842 
10843 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]) {
10844 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY])
10845 			return -EINVAL;
10846 		memcpy(&req.s1g_capa_mask,
10847 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]),
10848 		       sizeof(req.s1g_capa_mask));
10849 	}
10850 
10851 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY]) {
10852 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK])
10853 			return -EINVAL;
10854 		memcpy(&req.s1g_capa,
10855 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY]),
10856 		       sizeof(req.s1g_capa));
10857 	}
10858 
10859 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
10860 
10861 	if (info->attrs[NL80211_ATTR_MLO_LINKS]) {
10862 		unsigned int attrsize = NUM_NL80211_ATTR * sizeof(*attrs);
10863 		struct nlattr *link;
10864 		int rem = 0;
10865 
10866 		if (req.link_id < 0)
10867 			return -EINVAL;
10868 
10869 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
10870 			return -EINVAL;
10871 
10872 		if (info->attrs[NL80211_ATTR_MAC] ||
10873 		    info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
10874 		    !info->attrs[NL80211_ATTR_MLD_ADDR])
10875 			return -EINVAL;
10876 
10877 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
10878 
10879 		attrs = kzalloc(attrsize, GFP_KERNEL);
10880 		if (!attrs)
10881 			return -ENOMEM;
10882 
10883 		nla_for_each_nested(link,
10884 				    info->attrs[NL80211_ATTR_MLO_LINKS],
10885 				    rem) {
10886 			memset(attrs, 0, attrsize);
10887 
10888 			nla_parse_nested(attrs, NL80211_ATTR_MAX,
10889 					 link, NULL, NULL);
10890 
10891 			if (!attrs[NL80211_ATTR_MLO_LINK_ID]) {
10892 				err = -EINVAL;
10893 				goto free;
10894 			}
10895 
10896 			link_id = nla_get_u8(attrs[NL80211_ATTR_MLO_LINK_ID]);
10897 			/* cannot use the same link ID again */
10898 			if (req.links[link_id].bss) {
10899 				err = -EINVAL;
10900 				goto free;
10901 			}
10902 			req.links[link_id].bss =
10903 				nl80211_assoc_bss(rdev, ssid, ssid_len, attrs,
10904 						  &bssid);
10905 			if (IS_ERR(req.links[link_id].bss)) {
10906 				err = PTR_ERR(req.links[link_id].bss);
10907 				req.links[link_id].bss = NULL;
10908 				goto free;
10909 			}
10910 
10911 			if (attrs[NL80211_ATTR_IE]) {
10912 				req.links[link_id].elems =
10913 					nla_data(attrs[NL80211_ATTR_IE]);
10914 				req.links[link_id].elems_len =
10915 					nla_len(attrs[NL80211_ATTR_IE]);
10916 
10917 				if (cfg80211_find_elem(WLAN_EID_FRAGMENT,
10918 						       req.links[link_id].elems,
10919 						       req.links[link_id].elems_len)) {
10920 					GENL_SET_ERR_MSG(info,
10921 							 "cannot deal with fragmentation");
10922 					err = -EINVAL;
10923 					goto free;
10924 				}
10925 
10926 				if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
10927 							   req.links[link_id].elems,
10928 							   req.links[link_id].elems_len)) {
10929 					GENL_SET_ERR_MSG(info,
10930 							 "cannot deal with non-inheritance");
10931 					err = -EINVAL;
10932 					goto free;
10933 				}
10934 			}
10935 		}
10936 
10937 		if (!req.links[req.link_id].bss) {
10938 			err = -EINVAL;
10939 			goto free;
10940 		}
10941 
10942 		if (req.links[req.link_id].elems_len) {
10943 			GENL_SET_ERR_MSG(info,
10944 					 "cannot have per-link elems on assoc link");
10945 			err = -EINVAL;
10946 			goto free;
10947 		}
10948 
10949 		kfree(attrs);
10950 		attrs = NULL;
10951 	} else {
10952 		if (req.link_id >= 0)
10953 			return -EINVAL;
10954 
10955 		req.bss = nl80211_assoc_bss(rdev, ssid, ssid_len, info->attrs,
10956 					    &bssid);
10957 		if (IS_ERR(req.bss))
10958 			return PTR_ERR(req.bss);
10959 	}
10960 
10961 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
10962 	if (!err) {
10963 		wdev_lock(dev->ieee80211_ptr);
10964 
10965 		err = cfg80211_mlme_assoc(rdev, dev, &req);
10966 
10967 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10968 			dev->ieee80211_ptr->conn_owner_nlportid =
10969 				info->snd_portid;
10970 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
10971 			       bssid, ETH_ALEN);
10972 		}
10973 
10974 		wdev_unlock(dev->ieee80211_ptr);
10975 	}
10976 
10977 free:
10978 	for (link_id = 0; link_id < ARRAY_SIZE(req.links); link_id++)
10979 		cfg80211_put_bss(&rdev->wiphy, req.links[link_id].bss);
10980 	cfg80211_put_bss(&rdev->wiphy, req.bss);
10981 	kfree(attrs);
10982 
10983 	return err;
10984 }
10985 
10986 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
10987 {
10988 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10989 	struct net_device *dev = info->user_ptr[1];
10990 	const u8 *ie = NULL, *bssid;
10991 	int ie_len = 0, err;
10992 	u16 reason_code;
10993 	bool local_state_change;
10994 
10995 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
10996 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10997 		return -EPERM;
10998 
10999 	if (!info->attrs[NL80211_ATTR_MAC])
11000 		return -EINVAL;
11001 
11002 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11003 		return -EINVAL;
11004 
11005 	if (!rdev->ops->deauth)
11006 		return -EOPNOTSUPP;
11007 
11008 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11009 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11010 		return -EOPNOTSUPP;
11011 
11012 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11013 
11014 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11015 	if (reason_code == 0) {
11016 		/* Reason Code 0 is reserved */
11017 		return -EINVAL;
11018 	}
11019 
11020 	if (info->attrs[NL80211_ATTR_IE]) {
11021 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11022 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11023 	}
11024 
11025 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11026 
11027 	wdev_lock(dev->ieee80211_ptr);
11028 	err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
11029 				   local_state_change);
11030 	wdev_unlock(dev->ieee80211_ptr);
11031 	return err;
11032 }
11033 
11034 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
11035 {
11036 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11037 	struct net_device *dev = info->user_ptr[1];
11038 	const u8 *ie = NULL, *bssid;
11039 	int ie_len = 0, err;
11040 	u16 reason_code;
11041 	bool local_state_change;
11042 
11043 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11044 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11045 		return -EPERM;
11046 
11047 	if (!info->attrs[NL80211_ATTR_MAC])
11048 		return -EINVAL;
11049 
11050 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11051 		return -EINVAL;
11052 
11053 	if (!rdev->ops->disassoc)
11054 		return -EOPNOTSUPP;
11055 
11056 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11057 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11058 		return -EOPNOTSUPP;
11059 
11060 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11061 
11062 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11063 	if (reason_code == 0) {
11064 		/* Reason Code 0 is reserved */
11065 		return -EINVAL;
11066 	}
11067 
11068 	if (info->attrs[NL80211_ATTR_IE]) {
11069 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11070 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11071 	}
11072 
11073 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11074 
11075 	wdev_lock(dev->ieee80211_ptr);
11076 	err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
11077 				     local_state_change);
11078 	wdev_unlock(dev->ieee80211_ptr);
11079 	return err;
11080 }
11081 
11082 static bool
11083 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
11084 			 int mcast_rate[NUM_NL80211_BANDS],
11085 			 int rateval)
11086 {
11087 	struct wiphy *wiphy = &rdev->wiphy;
11088 	bool found = false;
11089 	int band, i;
11090 
11091 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
11092 		struct ieee80211_supported_band *sband;
11093 
11094 		sband = wiphy->bands[band];
11095 		if (!sband)
11096 			continue;
11097 
11098 		for (i = 0; i < sband->n_bitrates; i++) {
11099 			if (sband->bitrates[i].bitrate == rateval) {
11100 				mcast_rate[band] = i + 1;
11101 				found = true;
11102 				break;
11103 			}
11104 		}
11105 	}
11106 
11107 	return found;
11108 }
11109 
11110 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
11111 {
11112 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11113 	struct net_device *dev = info->user_ptr[1];
11114 	struct cfg80211_ibss_params ibss;
11115 	struct wiphy *wiphy;
11116 	struct cfg80211_cached_keys *connkeys = NULL;
11117 	int err;
11118 
11119 	memset(&ibss, 0, sizeof(ibss));
11120 
11121 	if (!info->attrs[NL80211_ATTR_SSID] ||
11122 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11123 		return -EINVAL;
11124 
11125 	ibss.beacon_interval = 100;
11126 
11127 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
11128 		ibss.beacon_interval =
11129 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11130 
11131 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
11132 					   ibss.beacon_interval);
11133 	if (err)
11134 		return err;
11135 
11136 	if (!rdev->ops->join_ibss)
11137 		return -EOPNOTSUPP;
11138 
11139 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11140 		return -EOPNOTSUPP;
11141 
11142 	wiphy = &rdev->wiphy;
11143 
11144 	if (info->attrs[NL80211_ATTR_MAC]) {
11145 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11146 
11147 		if (!is_valid_ether_addr(ibss.bssid))
11148 			return -EINVAL;
11149 	}
11150 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11151 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11152 
11153 	if (info->attrs[NL80211_ATTR_IE]) {
11154 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11155 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11156 	}
11157 
11158 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
11159 	if (err)
11160 		return err;
11161 
11162 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
11163 				     NL80211_IFTYPE_ADHOC))
11164 		return -EINVAL;
11165 
11166 	switch (ibss.chandef.width) {
11167 	case NL80211_CHAN_WIDTH_5:
11168 	case NL80211_CHAN_WIDTH_10:
11169 	case NL80211_CHAN_WIDTH_20_NOHT:
11170 		break;
11171 	case NL80211_CHAN_WIDTH_20:
11172 	case NL80211_CHAN_WIDTH_40:
11173 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11174 			return -EINVAL;
11175 		break;
11176 	case NL80211_CHAN_WIDTH_80:
11177 	case NL80211_CHAN_WIDTH_80P80:
11178 	case NL80211_CHAN_WIDTH_160:
11179 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11180 			return -EINVAL;
11181 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
11182 					     NL80211_EXT_FEATURE_VHT_IBSS))
11183 			return -EINVAL;
11184 		break;
11185 	case NL80211_CHAN_WIDTH_320:
11186 		return -EINVAL;
11187 	default:
11188 		return -EINVAL;
11189 	}
11190 
11191 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
11192 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
11193 
11194 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11195 		u8 *rates =
11196 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11197 		int n_rates =
11198 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11199 		struct ieee80211_supported_band *sband =
11200 			wiphy->bands[ibss.chandef.chan->band];
11201 
11202 		err = ieee80211_get_ratemask(sband, rates, n_rates,
11203 					     &ibss.basic_rates);
11204 		if (err)
11205 			return err;
11206 	}
11207 
11208 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11209 		memcpy(&ibss.ht_capa_mask,
11210 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11211 		       sizeof(ibss.ht_capa_mask));
11212 
11213 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11214 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11215 			return -EINVAL;
11216 		memcpy(&ibss.ht_capa,
11217 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11218 		       sizeof(ibss.ht_capa));
11219 	}
11220 
11221 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11222 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
11223 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11224 		return -EINVAL;
11225 
11226 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11227 		bool no_ht = false;
11228 
11229 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
11230 		if (IS_ERR(connkeys))
11231 			return PTR_ERR(connkeys);
11232 
11233 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
11234 		    no_ht) {
11235 			kfree_sensitive(connkeys);
11236 			return -EINVAL;
11237 		}
11238 	}
11239 
11240 	ibss.control_port =
11241 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
11242 
11243 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11244 		int r = validate_pae_over_nl80211(rdev, info);
11245 
11246 		if (r < 0) {
11247 			kfree_sensitive(connkeys);
11248 			return r;
11249 		}
11250 
11251 		ibss.control_port_over_nl80211 = true;
11252 	}
11253 
11254 	ibss.userspace_handles_dfs =
11255 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11256 
11257 	wdev_lock(dev->ieee80211_ptr);
11258 	err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
11259 	if (err)
11260 		kfree_sensitive(connkeys);
11261 	else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
11262 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11263 	wdev_unlock(dev->ieee80211_ptr);
11264 
11265 	return err;
11266 }
11267 
11268 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
11269 {
11270 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11271 	struct net_device *dev = info->user_ptr[1];
11272 
11273 	if (!rdev->ops->leave_ibss)
11274 		return -EOPNOTSUPP;
11275 
11276 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11277 		return -EOPNOTSUPP;
11278 
11279 	return cfg80211_leave_ibss(rdev, dev, false);
11280 }
11281 
11282 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
11283 {
11284 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11285 	struct net_device *dev = info->user_ptr[1];
11286 	int mcast_rate[NUM_NL80211_BANDS];
11287 	u32 nla_rate;
11288 
11289 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
11290 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
11291 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
11292 		return -EOPNOTSUPP;
11293 
11294 	if (!rdev->ops->set_mcast_rate)
11295 		return -EOPNOTSUPP;
11296 
11297 	memset(mcast_rate, 0, sizeof(mcast_rate));
11298 
11299 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
11300 		return -EINVAL;
11301 
11302 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
11303 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
11304 		return -EINVAL;
11305 
11306 	return rdev_set_mcast_rate(rdev, dev, mcast_rate);
11307 }
11308 
11309 static struct sk_buff *
11310 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
11311 			    struct wireless_dev *wdev, int approxlen,
11312 			    u32 portid, u32 seq, enum nl80211_commands cmd,
11313 			    enum nl80211_attrs attr,
11314 			    const struct nl80211_vendor_cmd_info *info,
11315 			    gfp_t gfp)
11316 {
11317 	struct sk_buff *skb;
11318 	void *hdr;
11319 	struct nlattr *data;
11320 
11321 	skb = nlmsg_new(approxlen + 100, gfp);
11322 	if (!skb)
11323 		return NULL;
11324 
11325 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
11326 	if (!hdr) {
11327 		kfree_skb(skb);
11328 		return NULL;
11329 	}
11330 
11331 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11332 		goto nla_put_failure;
11333 
11334 	if (info) {
11335 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
11336 				info->vendor_id))
11337 			goto nla_put_failure;
11338 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
11339 				info->subcmd))
11340 			goto nla_put_failure;
11341 	}
11342 
11343 	if (wdev) {
11344 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
11345 				      wdev_id(wdev), NL80211_ATTR_PAD))
11346 			goto nla_put_failure;
11347 		if (wdev->netdev &&
11348 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
11349 				wdev->netdev->ifindex))
11350 			goto nla_put_failure;
11351 	}
11352 
11353 	data = nla_nest_start_noflag(skb, attr);
11354 	if (!data)
11355 		goto nla_put_failure;
11356 
11357 	((void **)skb->cb)[0] = rdev;
11358 	((void **)skb->cb)[1] = hdr;
11359 	((void **)skb->cb)[2] = data;
11360 
11361 	return skb;
11362 
11363  nla_put_failure:
11364 	kfree_skb(skb);
11365 	return NULL;
11366 }
11367 
11368 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
11369 					   struct wireless_dev *wdev,
11370 					   enum nl80211_commands cmd,
11371 					   enum nl80211_attrs attr,
11372 					   unsigned int portid,
11373 					   int vendor_event_idx,
11374 					   int approxlen, gfp_t gfp)
11375 {
11376 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11377 	const struct nl80211_vendor_cmd_info *info;
11378 
11379 	switch (cmd) {
11380 	case NL80211_CMD_TESTMODE:
11381 		if (WARN_ON(vendor_event_idx != -1))
11382 			return NULL;
11383 		info = NULL;
11384 		break;
11385 	case NL80211_CMD_VENDOR:
11386 		if (WARN_ON(vendor_event_idx < 0 ||
11387 			    vendor_event_idx >= wiphy->n_vendor_events))
11388 			return NULL;
11389 		info = &wiphy->vendor_events[vendor_event_idx];
11390 		break;
11391 	default:
11392 		WARN_ON(1);
11393 		return NULL;
11394 	}
11395 
11396 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
11397 					   cmd, attr, info, gfp);
11398 }
11399 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
11400 
11401 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
11402 {
11403 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
11404 	void *hdr = ((void **)skb->cb)[1];
11405 	struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
11406 	struct nlattr *data = ((void **)skb->cb)[2];
11407 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
11408 
11409 	/* clear CB data for netlink core to own from now on */
11410 	memset(skb->cb, 0, sizeof(skb->cb));
11411 
11412 	nla_nest_end(skb, data);
11413 	genlmsg_end(skb, hdr);
11414 
11415 	if (nlhdr->nlmsg_pid) {
11416 		genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
11417 				nlhdr->nlmsg_pid);
11418 	} else {
11419 		if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
11420 			mcgrp = NL80211_MCGRP_VENDOR;
11421 
11422 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11423 					skb, 0, mcgrp, gfp);
11424 	}
11425 }
11426 EXPORT_SYMBOL(__cfg80211_send_event_skb);
11427 
11428 #ifdef CONFIG_NL80211_TESTMODE
11429 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
11430 {
11431 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11432 	struct wireless_dev *wdev;
11433 	int err;
11434 
11435 	lockdep_assert_held(&rdev->wiphy.mtx);
11436 
11437 	wdev = __cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
11438 					  info->attrs);
11439 
11440 	if (!rdev->ops->testmode_cmd)
11441 		return -EOPNOTSUPP;
11442 
11443 	if (IS_ERR(wdev)) {
11444 		err = PTR_ERR(wdev);
11445 		if (err != -EINVAL)
11446 			return err;
11447 		wdev = NULL;
11448 	} else if (wdev->wiphy != &rdev->wiphy) {
11449 		return -EINVAL;
11450 	}
11451 
11452 	if (!info->attrs[NL80211_ATTR_TESTDATA])
11453 		return -EINVAL;
11454 
11455 	rdev->cur_cmd_info = info;
11456 	err = rdev_testmode_cmd(rdev, wdev,
11457 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
11458 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
11459 	rdev->cur_cmd_info = NULL;
11460 
11461 	return err;
11462 }
11463 
11464 static int nl80211_testmode_dump(struct sk_buff *skb,
11465 				 struct netlink_callback *cb)
11466 {
11467 	struct cfg80211_registered_device *rdev;
11468 	struct nlattr **attrbuf = NULL;
11469 	int err;
11470 	long phy_idx;
11471 	void *data = NULL;
11472 	int data_len = 0;
11473 
11474 	rtnl_lock();
11475 
11476 	if (cb->args[0]) {
11477 		/*
11478 		 * 0 is a valid index, but not valid for args[0],
11479 		 * so we need to offset by 1.
11480 		 */
11481 		phy_idx = cb->args[0] - 1;
11482 
11483 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
11484 		if (!rdev) {
11485 			err = -ENOENT;
11486 			goto out_err;
11487 		}
11488 	} else {
11489 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
11490 				  GFP_KERNEL);
11491 		if (!attrbuf) {
11492 			err = -ENOMEM;
11493 			goto out_err;
11494 		}
11495 
11496 		err = nlmsg_parse_deprecated(cb->nlh,
11497 					     GENL_HDRLEN + nl80211_fam.hdrsize,
11498 					     attrbuf, nl80211_fam.maxattr,
11499 					     nl80211_policy, NULL);
11500 		if (err)
11501 			goto out_err;
11502 
11503 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
11504 		if (IS_ERR(rdev)) {
11505 			err = PTR_ERR(rdev);
11506 			goto out_err;
11507 		}
11508 		phy_idx = rdev->wiphy_idx;
11509 
11510 		if (attrbuf[NL80211_ATTR_TESTDATA])
11511 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
11512 	}
11513 
11514 	if (cb->args[1]) {
11515 		data = nla_data((void *)cb->args[1]);
11516 		data_len = nla_len((void *)cb->args[1]);
11517 	}
11518 
11519 	if (!rdev->ops->testmode_dump) {
11520 		err = -EOPNOTSUPP;
11521 		goto out_err;
11522 	}
11523 
11524 	while (1) {
11525 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
11526 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
11527 					   NL80211_CMD_TESTMODE);
11528 		struct nlattr *tmdata;
11529 
11530 		if (!hdr)
11531 			break;
11532 
11533 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
11534 			genlmsg_cancel(skb, hdr);
11535 			break;
11536 		}
11537 
11538 		tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
11539 		if (!tmdata) {
11540 			genlmsg_cancel(skb, hdr);
11541 			break;
11542 		}
11543 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
11544 		nla_nest_end(skb, tmdata);
11545 
11546 		if (err == -ENOBUFS || err == -ENOENT) {
11547 			genlmsg_cancel(skb, hdr);
11548 			break;
11549 		} else if (err) {
11550 			genlmsg_cancel(skb, hdr);
11551 			goto out_err;
11552 		}
11553 
11554 		genlmsg_end(skb, hdr);
11555 	}
11556 
11557 	err = skb->len;
11558 	/* see above */
11559 	cb->args[0] = phy_idx + 1;
11560  out_err:
11561 	kfree(attrbuf);
11562 	rtnl_unlock();
11563 	return err;
11564 }
11565 #endif
11566 
11567 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
11568 {
11569 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11570 	struct net_device *dev = info->user_ptr[1];
11571 	struct cfg80211_connect_params connect;
11572 	struct wiphy *wiphy;
11573 	struct cfg80211_cached_keys *connkeys = NULL;
11574 	u32 freq = 0;
11575 	int err;
11576 
11577 	memset(&connect, 0, sizeof(connect));
11578 
11579 	if (!info->attrs[NL80211_ATTR_SSID] ||
11580 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11581 		return -EINVAL;
11582 
11583 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
11584 		connect.auth_type =
11585 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
11586 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
11587 					     NL80211_CMD_CONNECT))
11588 			return -EINVAL;
11589 	} else
11590 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
11591 
11592 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
11593 
11594 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
11595 	    !wiphy_ext_feature_isset(&rdev->wiphy,
11596 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
11597 		return -EINVAL;
11598 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
11599 
11600 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
11601 				      NL80211_MAX_NR_CIPHER_SUITES);
11602 	if (err)
11603 		return err;
11604 
11605 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11606 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11607 		return -EOPNOTSUPP;
11608 
11609 	wiphy = &rdev->wiphy;
11610 
11611 	connect.bg_scan_period = -1;
11612 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
11613 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
11614 		connect.bg_scan_period =
11615 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
11616 	}
11617 
11618 	if (info->attrs[NL80211_ATTR_MAC])
11619 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11620 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
11621 		connect.bssid_hint =
11622 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
11623 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11624 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11625 
11626 	if (info->attrs[NL80211_ATTR_IE]) {
11627 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11628 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11629 	}
11630 
11631 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
11632 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
11633 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
11634 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11635 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
11636 			return -EOPNOTSUPP;
11637 	} else {
11638 		connect.mfp = NL80211_MFP_NO;
11639 	}
11640 
11641 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
11642 		connect.prev_bssid =
11643 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
11644 
11645 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ])
11646 		freq = MHZ_TO_KHZ(nla_get_u32(
11647 					info->attrs[NL80211_ATTR_WIPHY_FREQ]));
11648 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
11649 		freq +=
11650 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
11651 
11652 	if (freq) {
11653 		connect.channel = nl80211_get_valid_chan(wiphy, freq);
11654 		if (!connect.channel)
11655 			return -EINVAL;
11656 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
11657 		freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
11658 		freq = MHZ_TO_KHZ(freq);
11659 		connect.channel_hint = nl80211_get_valid_chan(wiphy, freq);
11660 		if (!connect.channel_hint)
11661 			return -EINVAL;
11662 	}
11663 
11664 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
11665 		connect.edmg.channels =
11666 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
11667 
11668 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
11669 			connect.edmg.bw_config =
11670 				nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
11671 	}
11672 
11673 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11674 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
11675 		if (IS_ERR(connkeys))
11676 			return PTR_ERR(connkeys);
11677 	}
11678 
11679 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
11680 		connect.flags |= ASSOC_REQ_DISABLE_HT;
11681 
11682 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11683 		memcpy(&connect.ht_capa_mask,
11684 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11685 		       sizeof(connect.ht_capa_mask));
11686 
11687 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11688 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
11689 			kfree_sensitive(connkeys);
11690 			return -EINVAL;
11691 		}
11692 		memcpy(&connect.ht_capa,
11693 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11694 		       sizeof(connect.ht_capa));
11695 	}
11696 
11697 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
11698 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
11699 
11700 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
11701 		connect.flags |= ASSOC_REQ_DISABLE_HE;
11702 
11703 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
11704 		connect.flags |= ASSOC_REQ_DISABLE_EHT;
11705 
11706 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11707 		memcpy(&connect.vht_capa_mask,
11708 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
11709 		       sizeof(connect.vht_capa_mask));
11710 
11711 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
11712 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
11713 			kfree_sensitive(connkeys);
11714 			return -EINVAL;
11715 		}
11716 		memcpy(&connect.vht_capa,
11717 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
11718 		       sizeof(connect.vht_capa));
11719 	}
11720 
11721 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
11722 		if (!((rdev->wiphy.features &
11723 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
11724 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
11725 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11726 					     NL80211_EXT_FEATURE_RRM)) {
11727 			kfree_sensitive(connkeys);
11728 			return -EINVAL;
11729 		}
11730 		connect.flags |= ASSOC_REQ_USE_RRM;
11731 	}
11732 
11733 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
11734 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
11735 		kfree_sensitive(connkeys);
11736 		return -EOPNOTSUPP;
11737 	}
11738 
11739 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
11740 		/* bss selection makes no sense if bssid is set */
11741 		if (connect.bssid) {
11742 			kfree_sensitive(connkeys);
11743 			return -EINVAL;
11744 		}
11745 
11746 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
11747 				       wiphy, &connect.bss_select);
11748 		if (err) {
11749 			kfree_sensitive(connkeys);
11750 			return err;
11751 		}
11752 	}
11753 
11754 	if (wiphy_ext_feature_isset(&rdev->wiphy,
11755 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
11756 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
11757 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
11758 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
11759 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11760 		connect.fils_erp_username =
11761 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11762 		connect.fils_erp_username_len =
11763 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11764 		connect.fils_erp_realm =
11765 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11766 		connect.fils_erp_realm_len =
11767 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11768 		connect.fils_erp_next_seq_num =
11769 			nla_get_u16(
11770 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
11771 		connect.fils_erp_rrk =
11772 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
11773 		connect.fils_erp_rrk_len =
11774 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
11775 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
11776 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
11777 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
11778 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11779 		kfree_sensitive(connkeys);
11780 		return -EINVAL;
11781 	}
11782 
11783 	if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
11784 		if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
11785 			kfree_sensitive(connkeys);
11786 			GENL_SET_ERR_MSG(info,
11787 					 "external auth requires connection ownership");
11788 			return -EINVAL;
11789 		}
11790 		connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
11791 	}
11792 
11793 	if (nla_get_flag(info->attrs[NL80211_ATTR_MLO_SUPPORT]))
11794 		connect.flags |= CONNECT_REQ_MLO_SUPPORT;
11795 
11796 	wdev_lock(dev->ieee80211_ptr);
11797 
11798 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
11799 			       connect.prev_bssid);
11800 	if (err)
11801 		kfree_sensitive(connkeys);
11802 
11803 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
11804 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11805 		if (connect.bssid)
11806 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
11807 			       connect.bssid, ETH_ALEN);
11808 		else
11809 			eth_zero_addr(dev->ieee80211_ptr->disconnect_bssid);
11810 	}
11811 
11812 	wdev_unlock(dev->ieee80211_ptr);
11813 
11814 	return err;
11815 }
11816 
11817 static int nl80211_update_connect_params(struct sk_buff *skb,
11818 					 struct genl_info *info)
11819 {
11820 	struct cfg80211_connect_params connect = {};
11821 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11822 	struct net_device *dev = info->user_ptr[1];
11823 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11824 	bool fils_sk_offload;
11825 	u32 auth_type;
11826 	u32 changed = 0;
11827 	int ret;
11828 
11829 	if (!rdev->ops->update_connect_params)
11830 		return -EOPNOTSUPP;
11831 
11832 	if (info->attrs[NL80211_ATTR_IE]) {
11833 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11834 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11835 		changed |= UPDATE_ASSOC_IES;
11836 	}
11837 
11838 	fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
11839 						  NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
11840 
11841 	/*
11842 	 * when driver supports fils-sk offload all attributes must be
11843 	 * provided. So the else covers "fils-sk-not-all" and
11844 	 * "no-fils-sk-any".
11845 	 */
11846 	if (fils_sk_offload &&
11847 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
11848 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
11849 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
11850 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11851 		connect.fils_erp_username =
11852 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11853 		connect.fils_erp_username_len =
11854 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11855 		connect.fils_erp_realm =
11856 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11857 		connect.fils_erp_realm_len =
11858 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11859 		connect.fils_erp_next_seq_num =
11860 			nla_get_u16(
11861 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
11862 		connect.fils_erp_rrk =
11863 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
11864 		connect.fils_erp_rrk_len =
11865 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
11866 		changed |= UPDATE_FILS_ERP_INFO;
11867 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
11868 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
11869 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
11870 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11871 		return -EINVAL;
11872 	}
11873 
11874 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
11875 		auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
11876 		if (!nl80211_valid_auth_type(rdev, auth_type,
11877 					     NL80211_CMD_CONNECT))
11878 			return -EINVAL;
11879 
11880 		if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
11881 		    fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
11882 			return -EINVAL;
11883 
11884 		connect.auth_type = auth_type;
11885 		changed |= UPDATE_AUTH_TYPE;
11886 	}
11887 
11888 	wdev_lock(dev->ieee80211_ptr);
11889 	if (!wdev->connected)
11890 		ret = -ENOLINK;
11891 	else
11892 		ret = rdev_update_connect_params(rdev, dev, &connect, changed);
11893 	wdev_unlock(dev->ieee80211_ptr);
11894 
11895 	return ret;
11896 }
11897 
11898 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
11899 {
11900 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11901 	struct net_device *dev = info->user_ptr[1];
11902 	u16 reason;
11903 	int ret;
11904 
11905 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11906 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11907 		return -EPERM;
11908 
11909 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11910 		reason = WLAN_REASON_DEAUTH_LEAVING;
11911 	else
11912 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11913 
11914 	if (reason == 0)
11915 		return -EINVAL;
11916 
11917 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11918 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11919 		return -EOPNOTSUPP;
11920 
11921 	wdev_lock(dev->ieee80211_ptr);
11922 	ret = cfg80211_disconnect(rdev, dev, reason, true);
11923 	wdev_unlock(dev->ieee80211_ptr);
11924 	return ret;
11925 }
11926 
11927 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
11928 {
11929 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11930 	struct net *net;
11931 	int err;
11932 
11933 	if (info->attrs[NL80211_ATTR_PID]) {
11934 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
11935 
11936 		net = get_net_ns_by_pid(pid);
11937 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
11938 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
11939 
11940 		net = get_net_ns_by_fd(fd);
11941 	} else {
11942 		return -EINVAL;
11943 	}
11944 
11945 	if (IS_ERR(net))
11946 		return PTR_ERR(net);
11947 
11948 	err = 0;
11949 
11950 	/* check if anything to do */
11951 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
11952 		err = cfg80211_switch_netns(rdev, net);
11953 
11954 	put_net(net);
11955 	return err;
11956 }
11957 
11958 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
11959 {
11960 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11961 	int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
11962 			struct cfg80211_pmksa *pmksa) = NULL;
11963 	struct net_device *dev = info->user_ptr[1];
11964 	struct cfg80211_pmksa pmksa;
11965 
11966 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
11967 
11968 	if (!info->attrs[NL80211_ATTR_PMKID])
11969 		return -EINVAL;
11970 
11971 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
11972 
11973 	if (info->attrs[NL80211_ATTR_MAC]) {
11974 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11975 	} else if (info->attrs[NL80211_ATTR_SSID] &&
11976 		   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
11977 		   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
11978 		    info->attrs[NL80211_ATTR_PMK])) {
11979 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11980 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11981 		pmksa.cache_id =
11982 			nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
11983 	} else {
11984 		return -EINVAL;
11985 	}
11986 	if (info->attrs[NL80211_ATTR_PMK]) {
11987 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
11988 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
11989 	}
11990 
11991 	if (info->attrs[NL80211_ATTR_PMK_LIFETIME])
11992 		pmksa.pmk_lifetime =
11993 			nla_get_u32(info->attrs[NL80211_ATTR_PMK_LIFETIME]);
11994 
11995 	if (info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD])
11996 		pmksa.pmk_reauth_threshold =
11997 			nla_get_u8(
11998 				info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD]);
11999 
12000 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12001 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
12002 	    !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
12003 	      wiphy_ext_feature_isset(&rdev->wiphy,
12004 				      NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
12005 		return -EOPNOTSUPP;
12006 
12007 	switch (info->genlhdr->cmd) {
12008 	case NL80211_CMD_SET_PMKSA:
12009 		rdev_ops = rdev->ops->set_pmksa;
12010 		break;
12011 	case NL80211_CMD_DEL_PMKSA:
12012 		rdev_ops = rdev->ops->del_pmksa;
12013 		break;
12014 	default:
12015 		WARN_ON(1);
12016 		break;
12017 	}
12018 
12019 	if (!rdev_ops)
12020 		return -EOPNOTSUPP;
12021 
12022 	return rdev_ops(&rdev->wiphy, dev, &pmksa);
12023 }
12024 
12025 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
12026 {
12027 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12028 	struct net_device *dev = info->user_ptr[1];
12029 
12030 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12031 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
12032 		return -EOPNOTSUPP;
12033 
12034 	if (!rdev->ops->flush_pmksa)
12035 		return -EOPNOTSUPP;
12036 
12037 	return rdev_flush_pmksa(rdev, dev);
12038 }
12039 
12040 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
12041 {
12042 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12043 	struct net_device *dev = info->user_ptr[1];
12044 	u8 action_code, dialog_token;
12045 	u32 peer_capability = 0;
12046 	u16 status_code;
12047 	u8 *peer;
12048 	bool initiator;
12049 
12050 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12051 	    !rdev->ops->tdls_mgmt)
12052 		return -EOPNOTSUPP;
12053 
12054 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
12055 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
12056 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
12057 	    !info->attrs[NL80211_ATTR_IE] ||
12058 	    !info->attrs[NL80211_ATTR_MAC])
12059 		return -EINVAL;
12060 
12061 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12062 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
12063 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
12064 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
12065 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
12066 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
12067 		peer_capability =
12068 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
12069 
12070 	return rdev_tdls_mgmt(rdev, dev, peer, action_code,
12071 			      dialog_token, status_code, peer_capability,
12072 			      initiator,
12073 			      nla_data(info->attrs[NL80211_ATTR_IE]),
12074 			      nla_len(info->attrs[NL80211_ATTR_IE]));
12075 }
12076 
12077 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
12078 {
12079 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12080 	struct net_device *dev = info->user_ptr[1];
12081 	enum nl80211_tdls_operation operation;
12082 	u8 *peer;
12083 
12084 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12085 	    !rdev->ops->tdls_oper)
12086 		return -EOPNOTSUPP;
12087 
12088 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
12089 	    !info->attrs[NL80211_ATTR_MAC])
12090 		return -EINVAL;
12091 
12092 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
12093 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12094 
12095 	return rdev_tdls_oper(rdev, dev, peer, operation);
12096 }
12097 
12098 static int nl80211_remain_on_channel(struct sk_buff *skb,
12099 				     struct genl_info *info)
12100 {
12101 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12102 	unsigned int link_id = nl80211_link_id(info->attrs);
12103 	struct wireless_dev *wdev = info->user_ptr[1];
12104 	struct cfg80211_chan_def chandef;
12105 	struct sk_buff *msg;
12106 	void *hdr;
12107 	u64 cookie;
12108 	u32 duration;
12109 	int err;
12110 
12111 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
12112 	    !info->attrs[NL80211_ATTR_DURATION])
12113 		return -EINVAL;
12114 
12115 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12116 
12117 	if (!rdev->ops->remain_on_channel ||
12118 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
12119 		return -EOPNOTSUPP;
12120 
12121 	/*
12122 	 * We should be on that channel for at least a minimum amount of
12123 	 * time (10ms) but no longer than the driver supports.
12124 	 */
12125 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12126 	    duration > rdev->wiphy.max_remain_on_channel_duration)
12127 		return -EINVAL;
12128 
12129 	err = nl80211_parse_chandef(rdev, info, &chandef);
12130 	if (err)
12131 		return err;
12132 
12133 	wdev_lock(wdev);
12134 	if (!cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12135 		const struct cfg80211_chan_def *oper_chandef, *compat_chandef;
12136 
12137 		oper_chandef = wdev_chandef(wdev, link_id);
12138 
12139 		if (WARN_ON(!oper_chandef)) {
12140 			/* cannot happen since we must beacon to get here */
12141 			WARN_ON(1);
12142 			wdev_unlock(wdev);
12143 			return -EBUSY;
12144 		}
12145 
12146 		/* note: returns first one if identical chandefs */
12147 		compat_chandef = cfg80211_chandef_compatible(&chandef,
12148 							     oper_chandef);
12149 
12150 		if (compat_chandef != &chandef) {
12151 			wdev_unlock(wdev);
12152 			return -EBUSY;
12153 		}
12154 	}
12155 	wdev_unlock(wdev);
12156 
12157 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12158 	if (!msg)
12159 		return -ENOMEM;
12160 
12161 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12162 			     NL80211_CMD_REMAIN_ON_CHANNEL);
12163 	if (!hdr) {
12164 		err = -ENOBUFS;
12165 		goto free_msg;
12166 	}
12167 
12168 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
12169 				     duration, &cookie);
12170 
12171 	if (err)
12172 		goto free_msg;
12173 
12174 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12175 			      NL80211_ATTR_PAD))
12176 		goto nla_put_failure;
12177 
12178 	genlmsg_end(msg, hdr);
12179 
12180 	return genlmsg_reply(msg, info);
12181 
12182  nla_put_failure:
12183 	err = -ENOBUFS;
12184  free_msg:
12185 	nlmsg_free(msg);
12186 	return err;
12187 }
12188 
12189 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
12190 					    struct genl_info *info)
12191 {
12192 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12193 	struct wireless_dev *wdev = info->user_ptr[1];
12194 	u64 cookie;
12195 
12196 	if (!info->attrs[NL80211_ATTR_COOKIE])
12197 		return -EINVAL;
12198 
12199 	if (!rdev->ops->cancel_remain_on_channel)
12200 		return -EOPNOTSUPP;
12201 
12202 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12203 
12204 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
12205 }
12206 
12207 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
12208 				       struct genl_info *info)
12209 {
12210 	struct cfg80211_bitrate_mask mask;
12211 	unsigned int link_id = nl80211_link_id(info->attrs);
12212 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12213 	struct net_device *dev = info->user_ptr[1];
12214 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12215 	int err;
12216 
12217 	if (!rdev->ops->set_bitrate_mask)
12218 		return -EOPNOTSUPP;
12219 
12220 	wdev_lock(wdev);
12221 	err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
12222 					    NL80211_ATTR_TX_RATES, &mask,
12223 					    dev, true, link_id);
12224 	if (err)
12225 		goto out;
12226 
12227 	err = rdev_set_bitrate_mask(rdev, dev, link_id, NULL, &mask);
12228 out:
12229 	wdev_unlock(wdev);
12230 	return err;
12231 }
12232 
12233 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
12234 {
12235 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12236 	struct wireless_dev *wdev = info->user_ptr[1];
12237 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
12238 
12239 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
12240 		return -EINVAL;
12241 
12242 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
12243 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
12244 
12245 	switch (wdev->iftype) {
12246 	case NL80211_IFTYPE_STATION:
12247 	case NL80211_IFTYPE_ADHOC:
12248 	case NL80211_IFTYPE_P2P_CLIENT:
12249 	case NL80211_IFTYPE_AP:
12250 	case NL80211_IFTYPE_AP_VLAN:
12251 	case NL80211_IFTYPE_MESH_POINT:
12252 	case NL80211_IFTYPE_P2P_GO:
12253 	case NL80211_IFTYPE_P2P_DEVICE:
12254 		break;
12255 	case NL80211_IFTYPE_NAN:
12256 	default:
12257 		return -EOPNOTSUPP;
12258 	}
12259 
12260 	/* not much point in registering if we can't reply */
12261 	if (!rdev->ops->mgmt_tx)
12262 		return -EOPNOTSUPP;
12263 
12264 	if (info->attrs[NL80211_ATTR_RECEIVE_MULTICAST] &&
12265 	    !wiphy_ext_feature_isset(&rdev->wiphy,
12266 				     NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS)) {
12267 		GENL_SET_ERR_MSG(info,
12268 				 "multicast RX registrations are not supported");
12269 		return -EOPNOTSUPP;
12270 	}
12271 
12272 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
12273 					   nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12274 					   nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12275 					   info->attrs[NL80211_ATTR_RECEIVE_MULTICAST],
12276 					   info->extack);
12277 }
12278 
12279 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
12280 {
12281 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12282 	struct wireless_dev *wdev = info->user_ptr[1];
12283 	struct cfg80211_chan_def chandef;
12284 	int err;
12285 	void *hdr = NULL;
12286 	u64 cookie;
12287 	struct sk_buff *msg = NULL;
12288 	struct cfg80211_mgmt_tx_params params = {
12289 		.dont_wait_for_ack =
12290 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
12291 	};
12292 
12293 	if (!info->attrs[NL80211_ATTR_FRAME])
12294 		return -EINVAL;
12295 
12296 	if (!rdev->ops->mgmt_tx)
12297 		return -EOPNOTSUPP;
12298 
12299 	switch (wdev->iftype) {
12300 	case NL80211_IFTYPE_P2P_DEVICE:
12301 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
12302 			return -EINVAL;
12303 		break;
12304 	case NL80211_IFTYPE_STATION:
12305 	case NL80211_IFTYPE_ADHOC:
12306 	case NL80211_IFTYPE_P2P_CLIENT:
12307 	case NL80211_IFTYPE_AP:
12308 	case NL80211_IFTYPE_AP_VLAN:
12309 	case NL80211_IFTYPE_MESH_POINT:
12310 	case NL80211_IFTYPE_P2P_GO:
12311 		break;
12312 	case NL80211_IFTYPE_NAN:
12313 	default:
12314 		return -EOPNOTSUPP;
12315 	}
12316 
12317 	if (info->attrs[NL80211_ATTR_DURATION]) {
12318 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12319 			return -EINVAL;
12320 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12321 
12322 		/*
12323 		 * We should wait on the channel for at least a minimum amount
12324 		 * of time (10ms) but no longer than the driver supports.
12325 		 */
12326 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12327 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
12328 			return -EINVAL;
12329 	}
12330 
12331 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
12332 
12333 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12334 		return -EINVAL;
12335 
12336 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
12337 
12338 	/* get the channel if any has been specified, otherwise pass NULL to
12339 	 * the driver. The latter will use the current one
12340 	 */
12341 	chandef.chan = NULL;
12342 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
12343 		err = nl80211_parse_chandef(rdev, info, &chandef);
12344 		if (err)
12345 			return err;
12346 	}
12347 
12348 	if (!chandef.chan && params.offchan)
12349 		return -EINVAL;
12350 
12351 	wdev_lock(wdev);
12352 	if (params.offchan &&
12353 	    !cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12354 		wdev_unlock(wdev);
12355 		return -EBUSY;
12356 	}
12357 
12358 	params.link_id = nl80211_link_id_or_invalid(info->attrs);
12359 	/*
12360 	 * This now races due to the unlock, but we cannot check
12361 	 * the valid links for the _station_ anyway, so that's up
12362 	 * to the driver.
12363 	 */
12364 	if (params.link_id >= 0 &&
12365 	    !(wdev->valid_links & BIT(params.link_id))) {
12366 		wdev_unlock(wdev);
12367 		return -EINVAL;
12368 	}
12369 	wdev_unlock(wdev);
12370 
12371 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
12372 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
12373 
12374 	if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
12375 		int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12376 		int i;
12377 
12378 		if (len % sizeof(u16))
12379 			return -EINVAL;
12380 
12381 		params.n_csa_offsets = len / sizeof(u16);
12382 		params.csa_offsets =
12383 			nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12384 
12385 		/* check that all the offsets fit the frame */
12386 		for (i = 0; i < params.n_csa_offsets; i++) {
12387 			if (params.csa_offsets[i] >= params.len)
12388 				return -EINVAL;
12389 		}
12390 	}
12391 
12392 	if (!params.dont_wait_for_ack) {
12393 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12394 		if (!msg)
12395 			return -ENOMEM;
12396 
12397 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12398 				     NL80211_CMD_FRAME);
12399 		if (!hdr) {
12400 			err = -ENOBUFS;
12401 			goto free_msg;
12402 		}
12403 	}
12404 
12405 	params.chan = chandef.chan;
12406 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
12407 	if (err)
12408 		goto free_msg;
12409 
12410 	if (msg) {
12411 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12412 				      NL80211_ATTR_PAD))
12413 			goto nla_put_failure;
12414 
12415 		genlmsg_end(msg, hdr);
12416 		return genlmsg_reply(msg, info);
12417 	}
12418 
12419 	return 0;
12420 
12421  nla_put_failure:
12422 	err = -ENOBUFS;
12423  free_msg:
12424 	nlmsg_free(msg);
12425 	return err;
12426 }
12427 
12428 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
12429 {
12430 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12431 	struct wireless_dev *wdev = info->user_ptr[1];
12432 	u64 cookie;
12433 
12434 	if (!info->attrs[NL80211_ATTR_COOKIE])
12435 		return -EINVAL;
12436 
12437 	if (!rdev->ops->mgmt_tx_cancel_wait)
12438 		return -EOPNOTSUPP;
12439 
12440 	switch (wdev->iftype) {
12441 	case NL80211_IFTYPE_STATION:
12442 	case NL80211_IFTYPE_ADHOC:
12443 	case NL80211_IFTYPE_P2P_CLIENT:
12444 	case NL80211_IFTYPE_AP:
12445 	case NL80211_IFTYPE_AP_VLAN:
12446 	case NL80211_IFTYPE_P2P_GO:
12447 	case NL80211_IFTYPE_P2P_DEVICE:
12448 		break;
12449 	case NL80211_IFTYPE_NAN:
12450 	default:
12451 		return -EOPNOTSUPP;
12452 	}
12453 
12454 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12455 
12456 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
12457 }
12458 
12459 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
12460 {
12461 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12462 	struct wireless_dev *wdev;
12463 	struct net_device *dev = info->user_ptr[1];
12464 	u8 ps_state;
12465 	bool state;
12466 	int err;
12467 
12468 	if (!info->attrs[NL80211_ATTR_PS_STATE])
12469 		return -EINVAL;
12470 
12471 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
12472 
12473 	wdev = dev->ieee80211_ptr;
12474 
12475 	if (!rdev->ops->set_power_mgmt)
12476 		return -EOPNOTSUPP;
12477 
12478 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
12479 
12480 	if (state == wdev->ps)
12481 		return 0;
12482 
12483 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
12484 	if (!err)
12485 		wdev->ps = state;
12486 	return err;
12487 }
12488 
12489 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
12490 {
12491 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12492 	enum nl80211_ps_state ps_state;
12493 	struct wireless_dev *wdev;
12494 	struct net_device *dev = info->user_ptr[1];
12495 	struct sk_buff *msg;
12496 	void *hdr;
12497 	int err;
12498 
12499 	wdev = dev->ieee80211_ptr;
12500 
12501 	if (!rdev->ops->set_power_mgmt)
12502 		return -EOPNOTSUPP;
12503 
12504 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12505 	if (!msg)
12506 		return -ENOMEM;
12507 
12508 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12509 			     NL80211_CMD_GET_POWER_SAVE);
12510 	if (!hdr) {
12511 		err = -ENOBUFS;
12512 		goto free_msg;
12513 	}
12514 
12515 	if (wdev->ps)
12516 		ps_state = NL80211_PS_ENABLED;
12517 	else
12518 		ps_state = NL80211_PS_DISABLED;
12519 
12520 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
12521 		goto nla_put_failure;
12522 
12523 	genlmsg_end(msg, hdr);
12524 	return genlmsg_reply(msg, info);
12525 
12526  nla_put_failure:
12527 	err = -ENOBUFS;
12528  free_msg:
12529 	nlmsg_free(msg);
12530 	return err;
12531 }
12532 
12533 static const struct nla_policy
12534 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
12535 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
12536 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
12537 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
12538 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
12539 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
12540 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
12541 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
12542 };
12543 
12544 static int nl80211_set_cqm_txe(struct genl_info *info,
12545 			       u32 rate, u32 pkts, u32 intvl)
12546 {
12547 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12548 	struct net_device *dev = info->user_ptr[1];
12549 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12550 
12551 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
12552 		return -EINVAL;
12553 
12554 	if (!rdev->ops->set_cqm_txe_config)
12555 		return -EOPNOTSUPP;
12556 
12557 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12558 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12559 		return -EOPNOTSUPP;
12560 
12561 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
12562 }
12563 
12564 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
12565 				    struct net_device *dev)
12566 {
12567 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12568 	s32 last, low, high;
12569 	u32 hyst;
12570 	int i, n, low_index;
12571 	int err;
12572 
12573 	/* RSSI reporting disabled? */
12574 	if (!wdev->cqm_config)
12575 		return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
12576 
12577 	/*
12578 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
12579 	 * event has been received yet, we should receive an event after a
12580 	 * connection is established and enough beacons received to calculate
12581 	 * the average.
12582 	 */
12583 	if (!wdev->cqm_config->last_rssi_event_value &&
12584 	    wdev->links[0].client.current_bss &&
12585 	    rdev->ops->get_station) {
12586 		struct station_info sinfo = {};
12587 		u8 *mac_addr;
12588 
12589 		mac_addr = wdev->links[0].client.current_bss->pub.bssid;
12590 
12591 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
12592 		if (err)
12593 			return err;
12594 
12595 		cfg80211_sinfo_release_content(&sinfo);
12596 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
12597 			wdev->cqm_config->last_rssi_event_value =
12598 				(s8) sinfo.rx_beacon_signal_avg;
12599 	}
12600 
12601 	last = wdev->cqm_config->last_rssi_event_value;
12602 	hyst = wdev->cqm_config->rssi_hyst;
12603 	n = wdev->cqm_config->n_rssi_thresholds;
12604 
12605 	for (i = 0; i < n; i++) {
12606 		i = array_index_nospec(i, n);
12607 		if (last < wdev->cqm_config->rssi_thresholds[i])
12608 			break;
12609 	}
12610 
12611 	low_index = i - 1;
12612 	if (low_index >= 0) {
12613 		low_index = array_index_nospec(low_index, n);
12614 		low = wdev->cqm_config->rssi_thresholds[low_index] - hyst;
12615 	} else {
12616 		low = S32_MIN;
12617 	}
12618 	if (i < n) {
12619 		i = array_index_nospec(i, n);
12620 		high = wdev->cqm_config->rssi_thresholds[i] + hyst - 1;
12621 	} else {
12622 		high = S32_MAX;
12623 	}
12624 
12625 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
12626 }
12627 
12628 static int nl80211_set_cqm_rssi(struct genl_info *info,
12629 				const s32 *thresholds, int n_thresholds,
12630 				u32 hysteresis)
12631 {
12632 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12633 	struct net_device *dev = info->user_ptr[1];
12634 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12635 	int i, err;
12636 	s32 prev = S32_MIN;
12637 
12638 	/* Check all values negative and sorted */
12639 	for (i = 0; i < n_thresholds; i++) {
12640 		if (thresholds[i] > 0 || thresholds[i] <= prev)
12641 			return -EINVAL;
12642 
12643 		prev = thresholds[i];
12644 	}
12645 
12646 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12647 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12648 		return -EOPNOTSUPP;
12649 
12650 	wdev_lock(wdev);
12651 	cfg80211_cqm_config_free(wdev);
12652 	wdev_unlock(wdev);
12653 
12654 	if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
12655 		if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
12656 			return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
12657 
12658 		return rdev_set_cqm_rssi_config(rdev, dev,
12659 						thresholds[0], hysteresis);
12660 	}
12661 
12662 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
12663 				     NL80211_EXT_FEATURE_CQM_RSSI_LIST))
12664 		return -EOPNOTSUPP;
12665 
12666 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
12667 		n_thresholds = 0;
12668 
12669 	wdev_lock(wdev);
12670 	if (n_thresholds) {
12671 		struct cfg80211_cqm_config *cqm_config;
12672 
12673 		cqm_config = kzalloc(struct_size(cqm_config, rssi_thresholds,
12674 						 n_thresholds),
12675 				     GFP_KERNEL);
12676 		if (!cqm_config) {
12677 			err = -ENOMEM;
12678 			goto unlock;
12679 		}
12680 
12681 		cqm_config->rssi_hyst = hysteresis;
12682 		cqm_config->n_rssi_thresholds = n_thresholds;
12683 		memcpy(cqm_config->rssi_thresholds, thresholds,
12684 		       flex_array_size(cqm_config, rssi_thresholds,
12685 				       n_thresholds));
12686 
12687 		wdev->cqm_config = cqm_config;
12688 	}
12689 
12690 	err = cfg80211_cqm_rssi_update(rdev, dev);
12691 
12692 unlock:
12693 	wdev_unlock(wdev);
12694 
12695 	return err;
12696 }
12697 
12698 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
12699 {
12700 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
12701 	struct nlattr *cqm;
12702 	int err;
12703 
12704 	cqm = info->attrs[NL80211_ATTR_CQM];
12705 	if (!cqm)
12706 		return -EINVAL;
12707 
12708 	err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
12709 					  nl80211_attr_cqm_policy,
12710 					  info->extack);
12711 	if (err)
12712 		return err;
12713 
12714 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
12715 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
12716 		const s32 *thresholds =
12717 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
12718 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
12719 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
12720 
12721 		if (len % 4)
12722 			return -EINVAL;
12723 
12724 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
12725 					    hysteresis);
12726 	}
12727 
12728 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
12729 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
12730 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
12731 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
12732 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
12733 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
12734 
12735 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
12736 	}
12737 
12738 	return -EINVAL;
12739 }
12740 
12741 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
12742 {
12743 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12744 	struct net_device *dev = info->user_ptr[1];
12745 	struct ocb_setup setup = {};
12746 	int err;
12747 
12748 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
12749 	if (err)
12750 		return err;
12751 
12752 	return cfg80211_join_ocb(rdev, dev, &setup);
12753 }
12754 
12755 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
12756 {
12757 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12758 	struct net_device *dev = info->user_ptr[1];
12759 
12760 	return cfg80211_leave_ocb(rdev, dev);
12761 }
12762 
12763 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
12764 {
12765 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12766 	struct net_device *dev = info->user_ptr[1];
12767 	struct mesh_config cfg;
12768 	struct mesh_setup setup;
12769 	int err;
12770 
12771 	/* start with default */
12772 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
12773 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
12774 
12775 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
12776 		/* and parse parameters if given */
12777 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
12778 		if (err)
12779 			return err;
12780 	}
12781 
12782 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
12783 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
12784 		return -EINVAL;
12785 
12786 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
12787 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
12788 
12789 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
12790 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
12791 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
12792 			return -EINVAL;
12793 
12794 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
12795 		setup.beacon_interval =
12796 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
12797 
12798 		err = cfg80211_validate_beacon_int(rdev,
12799 						   NL80211_IFTYPE_MESH_POINT,
12800 						   setup.beacon_interval);
12801 		if (err)
12802 			return err;
12803 	}
12804 
12805 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
12806 		setup.dtim_period =
12807 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
12808 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
12809 			return -EINVAL;
12810 	}
12811 
12812 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
12813 		/* parse additional setup parameters if given */
12814 		err = nl80211_parse_mesh_setup(info, &setup);
12815 		if (err)
12816 			return err;
12817 	}
12818 
12819 	if (setup.user_mpm)
12820 		cfg.auto_open_plinks = false;
12821 
12822 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
12823 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
12824 		if (err)
12825 			return err;
12826 	} else {
12827 		/* __cfg80211_join_mesh() will sort it out */
12828 		setup.chandef.chan = NULL;
12829 	}
12830 
12831 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
12832 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
12833 		int n_rates =
12834 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
12835 		struct ieee80211_supported_band *sband;
12836 
12837 		if (!setup.chandef.chan)
12838 			return -EINVAL;
12839 
12840 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
12841 
12842 		err = ieee80211_get_ratemask(sband, rates, n_rates,
12843 					     &setup.basic_rates);
12844 		if (err)
12845 			return err;
12846 	}
12847 
12848 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
12849 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
12850 						    NL80211_ATTR_TX_RATES,
12851 						    &setup.beacon_rate,
12852 						    dev, false, 0);
12853 		if (err)
12854 			return err;
12855 
12856 		if (!setup.chandef.chan)
12857 			return -EINVAL;
12858 
12859 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
12860 					      &setup.beacon_rate);
12861 		if (err)
12862 			return err;
12863 	}
12864 
12865 	setup.userspace_handles_dfs =
12866 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
12867 
12868 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
12869 		int r = validate_pae_over_nl80211(rdev, info);
12870 
12871 		if (r < 0)
12872 			return r;
12873 
12874 		setup.control_port_over_nl80211 = true;
12875 	}
12876 
12877 	wdev_lock(dev->ieee80211_ptr);
12878 	err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
12879 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
12880 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
12881 	wdev_unlock(dev->ieee80211_ptr);
12882 
12883 	return err;
12884 }
12885 
12886 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
12887 {
12888 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12889 	struct net_device *dev = info->user_ptr[1];
12890 
12891 	return cfg80211_leave_mesh(rdev, dev);
12892 }
12893 
12894 #ifdef CONFIG_PM
12895 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
12896 					struct cfg80211_registered_device *rdev)
12897 {
12898 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
12899 	struct nlattr *nl_pats, *nl_pat;
12900 	int i, pat_len;
12901 
12902 	if (!wowlan->n_patterns)
12903 		return 0;
12904 
12905 	nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
12906 	if (!nl_pats)
12907 		return -ENOBUFS;
12908 
12909 	for (i = 0; i < wowlan->n_patterns; i++) {
12910 		nl_pat = nla_nest_start_noflag(msg, i + 1);
12911 		if (!nl_pat)
12912 			return -ENOBUFS;
12913 		pat_len = wowlan->patterns[i].pattern_len;
12914 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
12915 			    wowlan->patterns[i].mask) ||
12916 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
12917 			    wowlan->patterns[i].pattern) ||
12918 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
12919 				wowlan->patterns[i].pkt_offset))
12920 			return -ENOBUFS;
12921 		nla_nest_end(msg, nl_pat);
12922 	}
12923 	nla_nest_end(msg, nl_pats);
12924 
12925 	return 0;
12926 }
12927 
12928 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
12929 				   struct cfg80211_wowlan_tcp *tcp)
12930 {
12931 	struct nlattr *nl_tcp;
12932 
12933 	if (!tcp)
12934 		return 0;
12935 
12936 	nl_tcp = nla_nest_start_noflag(msg,
12937 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
12938 	if (!nl_tcp)
12939 		return -ENOBUFS;
12940 
12941 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
12942 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
12943 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
12944 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
12945 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
12946 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
12947 		    tcp->payload_len, tcp->payload) ||
12948 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
12949 			tcp->data_interval) ||
12950 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
12951 		    tcp->wake_len, tcp->wake_data) ||
12952 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
12953 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
12954 		return -ENOBUFS;
12955 
12956 	if (tcp->payload_seq.len &&
12957 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
12958 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
12959 		return -ENOBUFS;
12960 
12961 	if (tcp->payload_tok.len &&
12962 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
12963 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
12964 		    &tcp->payload_tok))
12965 		return -ENOBUFS;
12966 
12967 	nla_nest_end(msg, nl_tcp);
12968 
12969 	return 0;
12970 }
12971 
12972 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
12973 				  struct cfg80211_sched_scan_request *req)
12974 {
12975 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
12976 	int i;
12977 
12978 	if (!req)
12979 		return 0;
12980 
12981 	nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
12982 	if (!nd)
12983 		return -ENOBUFS;
12984 
12985 	if (req->n_scan_plans == 1 &&
12986 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
12987 			req->scan_plans[0].interval * 1000))
12988 		return -ENOBUFS;
12989 
12990 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
12991 		return -ENOBUFS;
12992 
12993 	if (req->relative_rssi_set) {
12994 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
12995 
12996 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
12997 			       req->relative_rssi))
12998 			return -ENOBUFS;
12999 
13000 		rssi_adjust.band = req->rssi_adjust.band;
13001 		rssi_adjust.delta = req->rssi_adjust.delta;
13002 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
13003 			    sizeof(rssi_adjust), &rssi_adjust))
13004 			return -ENOBUFS;
13005 	}
13006 
13007 	freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
13008 	if (!freqs)
13009 		return -ENOBUFS;
13010 
13011 	for (i = 0; i < req->n_channels; i++) {
13012 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
13013 			return -ENOBUFS;
13014 	}
13015 
13016 	nla_nest_end(msg, freqs);
13017 
13018 	if (req->n_match_sets) {
13019 		matches = nla_nest_start_noflag(msg,
13020 						NL80211_ATTR_SCHED_SCAN_MATCH);
13021 		if (!matches)
13022 			return -ENOBUFS;
13023 
13024 		for (i = 0; i < req->n_match_sets; i++) {
13025 			match = nla_nest_start_noflag(msg, i);
13026 			if (!match)
13027 				return -ENOBUFS;
13028 
13029 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
13030 				    req->match_sets[i].ssid.ssid_len,
13031 				    req->match_sets[i].ssid.ssid))
13032 				return -ENOBUFS;
13033 			nla_nest_end(msg, match);
13034 		}
13035 		nla_nest_end(msg, matches);
13036 	}
13037 
13038 	scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
13039 	if (!scan_plans)
13040 		return -ENOBUFS;
13041 
13042 	for (i = 0; i < req->n_scan_plans; i++) {
13043 		scan_plan = nla_nest_start_noflag(msg, i + 1);
13044 		if (!scan_plan)
13045 			return -ENOBUFS;
13046 
13047 		if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
13048 				req->scan_plans[i].interval) ||
13049 		    (req->scan_plans[i].iterations &&
13050 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
13051 				 req->scan_plans[i].iterations)))
13052 			return -ENOBUFS;
13053 		nla_nest_end(msg, scan_plan);
13054 	}
13055 	nla_nest_end(msg, scan_plans);
13056 
13057 	nla_nest_end(msg, nd);
13058 
13059 	return 0;
13060 }
13061 
13062 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
13063 {
13064 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13065 	struct sk_buff *msg;
13066 	void *hdr;
13067 	u32 size = NLMSG_DEFAULT_SIZE;
13068 
13069 	if (!rdev->wiphy.wowlan)
13070 		return -EOPNOTSUPP;
13071 
13072 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
13073 		/* adjust size to have room for all the data */
13074 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
13075 			rdev->wiphy.wowlan_config->tcp->payload_len +
13076 			rdev->wiphy.wowlan_config->tcp->wake_len +
13077 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
13078 	}
13079 
13080 	msg = nlmsg_new(size, GFP_KERNEL);
13081 	if (!msg)
13082 		return -ENOMEM;
13083 
13084 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13085 			     NL80211_CMD_GET_WOWLAN);
13086 	if (!hdr)
13087 		goto nla_put_failure;
13088 
13089 	if (rdev->wiphy.wowlan_config) {
13090 		struct nlattr *nl_wowlan;
13091 
13092 		nl_wowlan = nla_nest_start_noflag(msg,
13093 						  NL80211_ATTR_WOWLAN_TRIGGERS);
13094 		if (!nl_wowlan)
13095 			goto nla_put_failure;
13096 
13097 		if ((rdev->wiphy.wowlan_config->any &&
13098 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
13099 		    (rdev->wiphy.wowlan_config->disconnect &&
13100 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
13101 		    (rdev->wiphy.wowlan_config->magic_pkt &&
13102 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
13103 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
13104 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
13105 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
13106 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
13107 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
13108 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
13109 		    (rdev->wiphy.wowlan_config->rfkill_release &&
13110 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
13111 			goto nla_put_failure;
13112 
13113 		if (nl80211_send_wowlan_patterns(msg, rdev))
13114 			goto nla_put_failure;
13115 
13116 		if (nl80211_send_wowlan_tcp(msg,
13117 					    rdev->wiphy.wowlan_config->tcp))
13118 			goto nla_put_failure;
13119 
13120 		if (nl80211_send_wowlan_nd(
13121 			    msg,
13122 			    rdev->wiphy.wowlan_config->nd_config))
13123 			goto nla_put_failure;
13124 
13125 		nla_nest_end(msg, nl_wowlan);
13126 	}
13127 
13128 	genlmsg_end(msg, hdr);
13129 	return genlmsg_reply(msg, info);
13130 
13131 nla_put_failure:
13132 	nlmsg_free(msg);
13133 	return -ENOBUFS;
13134 }
13135 
13136 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
13137 				    struct nlattr *attr,
13138 				    struct cfg80211_wowlan *trig)
13139 {
13140 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
13141 	struct cfg80211_wowlan_tcp *cfg;
13142 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
13143 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
13144 	u32 size;
13145 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
13146 	int err, port;
13147 
13148 	if (!rdev->wiphy.wowlan->tcp)
13149 		return -EINVAL;
13150 
13151 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
13152 					  nl80211_wowlan_tcp_policy, NULL);
13153 	if (err)
13154 		return err;
13155 
13156 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
13157 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
13158 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
13159 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
13160 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
13161 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
13162 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
13163 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
13164 		return -EINVAL;
13165 
13166 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
13167 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
13168 		return -EINVAL;
13169 
13170 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
13171 			rdev->wiphy.wowlan->tcp->data_interval_max ||
13172 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
13173 		return -EINVAL;
13174 
13175 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
13176 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
13177 		return -EINVAL;
13178 
13179 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
13180 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
13181 		return -EINVAL;
13182 
13183 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
13184 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13185 
13186 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13187 		tokens_size = tokln - sizeof(*tok);
13188 
13189 		if (!tok->len || tokens_size % tok->len)
13190 			return -EINVAL;
13191 		if (!rdev->wiphy.wowlan->tcp->tok)
13192 			return -EINVAL;
13193 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
13194 			return -EINVAL;
13195 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
13196 			return -EINVAL;
13197 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
13198 			return -EINVAL;
13199 		if (tok->offset + tok->len > data_size)
13200 			return -EINVAL;
13201 	}
13202 
13203 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
13204 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
13205 		if (!rdev->wiphy.wowlan->tcp->seq)
13206 			return -EINVAL;
13207 		if (seq->len == 0 || seq->len > 4)
13208 			return -EINVAL;
13209 		if (seq->len + seq->offset > data_size)
13210 			return -EINVAL;
13211 	}
13212 
13213 	size = sizeof(*cfg);
13214 	size += data_size;
13215 	size += wake_size + wake_mask_size;
13216 	size += tokens_size;
13217 
13218 	cfg = kzalloc(size, GFP_KERNEL);
13219 	if (!cfg)
13220 		return -ENOMEM;
13221 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
13222 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
13223 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
13224 	       ETH_ALEN);
13225 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
13226 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
13227 	else
13228 		port = 0;
13229 #ifdef CONFIG_INET
13230 	/* allocate a socket and port for it and use it */
13231 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
13232 			    IPPROTO_TCP, &cfg->sock, 1);
13233 	if (err) {
13234 		kfree(cfg);
13235 		return err;
13236 	}
13237 	if (inet_csk_get_port(cfg->sock->sk, port)) {
13238 		sock_release(cfg->sock);
13239 		kfree(cfg);
13240 		return -EADDRINUSE;
13241 	}
13242 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
13243 #else
13244 	if (!port) {
13245 		kfree(cfg);
13246 		return -EINVAL;
13247 	}
13248 	cfg->src_port = port;
13249 #endif
13250 
13251 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
13252 	cfg->payload_len = data_size;
13253 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
13254 	memcpy((void *)cfg->payload,
13255 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
13256 	       data_size);
13257 	if (seq)
13258 		cfg->payload_seq = *seq;
13259 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
13260 	cfg->wake_len = wake_size;
13261 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
13262 	memcpy((void *)cfg->wake_data,
13263 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
13264 	       wake_size);
13265 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
13266 			 data_size + wake_size;
13267 	memcpy((void *)cfg->wake_mask,
13268 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
13269 	       wake_mask_size);
13270 	if (tok) {
13271 		cfg->tokens_size = tokens_size;
13272 		cfg->payload_tok = *tok;
13273 		memcpy(cfg->payload_tok.token_stream, tok->token_stream,
13274 		       tokens_size);
13275 	}
13276 
13277 	trig->tcp = cfg;
13278 
13279 	return 0;
13280 }
13281 
13282 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
13283 				   const struct wiphy_wowlan_support *wowlan,
13284 				   struct nlattr *attr,
13285 				   struct cfg80211_wowlan *trig)
13286 {
13287 	struct nlattr **tb;
13288 	int err;
13289 
13290 	tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
13291 	if (!tb)
13292 		return -ENOMEM;
13293 
13294 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
13295 		err = -EOPNOTSUPP;
13296 		goto out;
13297 	}
13298 
13299 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
13300 					  nl80211_policy, NULL);
13301 	if (err)
13302 		goto out;
13303 
13304 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
13305 						   wowlan->max_nd_match_sets);
13306 	err = PTR_ERR_OR_ZERO(trig->nd_config);
13307 	if (err)
13308 		trig->nd_config = NULL;
13309 
13310 out:
13311 	kfree(tb);
13312 	return err;
13313 }
13314 
13315 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
13316 {
13317 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13318 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
13319 	struct cfg80211_wowlan new_triggers = {};
13320 	struct cfg80211_wowlan *ntrig;
13321 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
13322 	int err, i;
13323 	bool prev_enabled = rdev->wiphy.wowlan_config;
13324 	bool regular = false;
13325 
13326 	if (!wowlan)
13327 		return -EOPNOTSUPP;
13328 
13329 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
13330 		cfg80211_rdev_free_wowlan(rdev);
13331 		rdev->wiphy.wowlan_config = NULL;
13332 		goto set_wakeup;
13333 	}
13334 
13335 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
13336 					  info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
13337 					  nl80211_wowlan_policy, info->extack);
13338 	if (err)
13339 		return err;
13340 
13341 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
13342 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
13343 			return -EINVAL;
13344 		new_triggers.any = true;
13345 	}
13346 
13347 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
13348 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
13349 			return -EINVAL;
13350 		new_triggers.disconnect = true;
13351 		regular = true;
13352 	}
13353 
13354 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
13355 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
13356 			return -EINVAL;
13357 		new_triggers.magic_pkt = true;
13358 		regular = true;
13359 	}
13360 
13361 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
13362 		return -EINVAL;
13363 
13364 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
13365 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
13366 			return -EINVAL;
13367 		new_triggers.gtk_rekey_failure = true;
13368 		regular = true;
13369 	}
13370 
13371 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
13372 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
13373 			return -EINVAL;
13374 		new_triggers.eap_identity_req = true;
13375 		regular = true;
13376 	}
13377 
13378 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
13379 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
13380 			return -EINVAL;
13381 		new_triggers.four_way_handshake = true;
13382 		regular = true;
13383 	}
13384 
13385 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
13386 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
13387 			return -EINVAL;
13388 		new_triggers.rfkill_release = true;
13389 		regular = true;
13390 	}
13391 
13392 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
13393 		struct nlattr *pat;
13394 		int n_patterns = 0;
13395 		int rem, pat_len, mask_len, pkt_offset;
13396 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13397 
13398 		regular = true;
13399 
13400 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13401 				    rem)
13402 			n_patterns++;
13403 		if (n_patterns > wowlan->n_patterns)
13404 			return -EINVAL;
13405 
13406 		new_triggers.patterns = kcalloc(n_patterns,
13407 						sizeof(new_triggers.patterns[0]),
13408 						GFP_KERNEL);
13409 		if (!new_triggers.patterns)
13410 			return -ENOMEM;
13411 
13412 		new_triggers.n_patterns = n_patterns;
13413 		i = 0;
13414 
13415 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13416 				    rem) {
13417 			u8 *mask_pat;
13418 
13419 			err = nla_parse_nested_deprecated(pat_tb,
13420 							  MAX_NL80211_PKTPAT,
13421 							  pat,
13422 							  nl80211_packet_pattern_policy,
13423 							  info->extack);
13424 			if (err)
13425 				goto error;
13426 
13427 			err = -EINVAL;
13428 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
13429 			    !pat_tb[NL80211_PKTPAT_PATTERN])
13430 				goto error;
13431 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13432 			mask_len = DIV_ROUND_UP(pat_len, 8);
13433 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13434 				goto error;
13435 			if (pat_len > wowlan->pattern_max_len ||
13436 			    pat_len < wowlan->pattern_min_len)
13437 				goto error;
13438 
13439 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
13440 				pkt_offset = 0;
13441 			else
13442 				pkt_offset = nla_get_u32(
13443 					pat_tb[NL80211_PKTPAT_OFFSET]);
13444 			if (pkt_offset > wowlan->max_pkt_offset)
13445 				goto error;
13446 			new_triggers.patterns[i].pkt_offset = pkt_offset;
13447 
13448 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13449 			if (!mask_pat) {
13450 				err = -ENOMEM;
13451 				goto error;
13452 			}
13453 			new_triggers.patterns[i].mask = mask_pat;
13454 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13455 			       mask_len);
13456 			mask_pat += mask_len;
13457 			new_triggers.patterns[i].pattern = mask_pat;
13458 			new_triggers.patterns[i].pattern_len = pat_len;
13459 			memcpy(mask_pat,
13460 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13461 			       pat_len);
13462 			i++;
13463 		}
13464 	}
13465 
13466 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
13467 		regular = true;
13468 		err = nl80211_parse_wowlan_tcp(
13469 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
13470 			&new_triggers);
13471 		if (err)
13472 			goto error;
13473 	}
13474 
13475 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
13476 		regular = true;
13477 		err = nl80211_parse_wowlan_nd(
13478 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
13479 			&new_triggers);
13480 		if (err)
13481 			goto error;
13482 	}
13483 
13484 	/* The 'any' trigger means the device continues operating more or less
13485 	 * as in its normal operation mode and wakes up the host on most of the
13486 	 * normal interrupts (like packet RX, ...)
13487 	 * It therefore makes little sense to combine with the more constrained
13488 	 * wakeup trigger modes.
13489 	 */
13490 	if (new_triggers.any && regular) {
13491 		err = -EINVAL;
13492 		goto error;
13493 	}
13494 
13495 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
13496 	if (!ntrig) {
13497 		err = -ENOMEM;
13498 		goto error;
13499 	}
13500 	cfg80211_rdev_free_wowlan(rdev);
13501 	rdev->wiphy.wowlan_config = ntrig;
13502 
13503  set_wakeup:
13504 	if (rdev->ops->set_wakeup &&
13505 	    prev_enabled != !!rdev->wiphy.wowlan_config)
13506 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
13507 
13508 	return 0;
13509  error:
13510 	for (i = 0; i < new_triggers.n_patterns; i++)
13511 		kfree(new_triggers.patterns[i].mask);
13512 	kfree(new_triggers.patterns);
13513 	if (new_triggers.tcp && new_triggers.tcp->sock)
13514 		sock_release(new_triggers.tcp->sock);
13515 	kfree(new_triggers.tcp);
13516 	kfree(new_triggers.nd_config);
13517 	return err;
13518 }
13519 #endif
13520 
13521 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
13522 				       struct cfg80211_registered_device *rdev)
13523 {
13524 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
13525 	int i, j, pat_len;
13526 	struct cfg80211_coalesce_rules *rule;
13527 
13528 	if (!rdev->coalesce->n_rules)
13529 		return 0;
13530 
13531 	nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
13532 	if (!nl_rules)
13533 		return -ENOBUFS;
13534 
13535 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
13536 		nl_rule = nla_nest_start_noflag(msg, i + 1);
13537 		if (!nl_rule)
13538 			return -ENOBUFS;
13539 
13540 		rule = &rdev->coalesce->rules[i];
13541 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
13542 				rule->delay))
13543 			return -ENOBUFS;
13544 
13545 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
13546 				rule->condition))
13547 			return -ENOBUFS;
13548 
13549 		nl_pats = nla_nest_start_noflag(msg,
13550 						NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
13551 		if (!nl_pats)
13552 			return -ENOBUFS;
13553 
13554 		for (j = 0; j < rule->n_patterns; j++) {
13555 			nl_pat = nla_nest_start_noflag(msg, j + 1);
13556 			if (!nl_pat)
13557 				return -ENOBUFS;
13558 			pat_len = rule->patterns[j].pattern_len;
13559 			if (nla_put(msg, NL80211_PKTPAT_MASK,
13560 				    DIV_ROUND_UP(pat_len, 8),
13561 				    rule->patterns[j].mask) ||
13562 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
13563 				    rule->patterns[j].pattern) ||
13564 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
13565 					rule->patterns[j].pkt_offset))
13566 				return -ENOBUFS;
13567 			nla_nest_end(msg, nl_pat);
13568 		}
13569 		nla_nest_end(msg, nl_pats);
13570 		nla_nest_end(msg, nl_rule);
13571 	}
13572 	nla_nest_end(msg, nl_rules);
13573 
13574 	return 0;
13575 }
13576 
13577 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
13578 {
13579 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13580 	struct sk_buff *msg;
13581 	void *hdr;
13582 
13583 	if (!rdev->wiphy.coalesce)
13584 		return -EOPNOTSUPP;
13585 
13586 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13587 	if (!msg)
13588 		return -ENOMEM;
13589 
13590 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13591 			     NL80211_CMD_GET_COALESCE);
13592 	if (!hdr)
13593 		goto nla_put_failure;
13594 
13595 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
13596 		goto nla_put_failure;
13597 
13598 	genlmsg_end(msg, hdr);
13599 	return genlmsg_reply(msg, info);
13600 
13601 nla_put_failure:
13602 	nlmsg_free(msg);
13603 	return -ENOBUFS;
13604 }
13605 
13606 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
13607 {
13608 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
13609 	int i, j;
13610 	struct cfg80211_coalesce_rules *rule;
13611 
13612 	if (!coalesce)
13613 		return;
13614 
13615 	for (i = 0; i < coalesce->n_rules; i++) {
13616 		rule = &coalesce->rules[i];
13617 		for (j = 0; j < rule->n_patterns; j++)
13618 			kfree(rule->patterns[j].mask);
13619 		kfree(rule->patterns);
13620 	}
13621 	kfree(coalesce->rules);
13622 	kfree(coalesce);
13623 	rdev->coalesce = NULL;
13624 }
13625 
13626 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
13627 				       struct nlattr *rule,
13628 				       struct cfg80211_coalesce_rules *new_rule)
13629 {
13630 	int err, i;
13631 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13632 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
13633 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
13634 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13635 
13636 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
13637 					  rule, nl80211_coalesce_policy, NULL);
13638 	if (err)
13639 		return err;
13640 
13641 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
13642 		new_rule->delay =
13643 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
13644 	if (new_rule->delay > coalesce->max_delay)
13645 		return -EINVAL;
13646 
13647 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
13648 		new_rule->condition =
13649 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
13650 
13651 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
13652 		return -EINVAL;
13653 
13654 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13655 			    rem)
13656 		n_patterns++;
13657 	if (n_patterns > coalesce->n_patterns)
13658 		return -EINVAL;
13659 
13660 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
13661 				     GFP_KERNEL);
13662 	if (!new_rule->patterns)
13663 		return -ENOMEM;
13664 
13665 	new_rule->n_patterns = n_patterns;
13666 	i = 0;
13667 
13668 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13669 			    rem) {
13670 		u8 *mask_pat;
13671 
13672 		err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
13673 						  pat,
13674 						  nl80211_packet_pattern_policy,
13675 						  NULL);
13676 		if (err)
13677 			return err;
13678 
13679 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
13680 		    !pat_tb[NL80211_PKTPAT_PATTERN])
13681 			return -EINVAL;
13682 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13683 		mask_len = DIV_ROUND_UP(pat_len, 8);
13684 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13685 			return -EINVAL;
13686 		if (pat_len > coalesce->pattern_max_len ||
13687 		    pat_len < coalesce->pattern_min_len)
13688 			return -EINVAL;
13689 
13690 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
13691 			pkt_offset = 0;
13692 		else
13693 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
13694 		if (pkt_offset > coalesce->max_pkt_offset)
13695 			return -EINVAL;
13696 		new_rule->patterns[i].pkt_offset = pkt_offset;
13697 
13698 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13699 		if (!mask_pat)
13700 			return -ENOMEM;
13701 
13702 		new_rule->patterns[i].mask = mask_pat;
13703 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13704 		       mask_len);
13705 
13706 		mask_pat += mask_len;
13707 		new_rule->patterns[i].pattern = mask_pat;
13708 		new_rule->patterns[i].pattern_len = pat_len;
13709 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13710 		       pat_len);
13711 		i++;
13712 	}
13713 
13714 	return 0;
13715 }
13716 
13717 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
13718 {
13719 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13720 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13721 	struct cfg80211_coalesce new_coalesce = {};
13722 	struct cfg80211_coalesce *n_coalesce;
13723 	int err, rem_rule, n_rules = 0, i, j;
13724 	struct nlattr *rule;
13725 	struct cfg80211_coalesce_rules *tmp_rule;
13726 
13727 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
13728 		return -EOPNOTSUPP;
13729 
13730 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
13731 		cfg80211_rdev_free_coalesce(rdev);
13732 		rdev_set_coalesce(rdev, NULL);
13733 		return 0;
13734 	}
13735 
13736 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
13737 			    rem_rule)
13738 		n_rules++;
13739 	if (n_rules > coalesce->n_rules)
13740 		return -EINVAL;
13741 
13742 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
13743 				     GFP_KERNEL);
13744 	if (!new_coalesce.rules)
13745 		return -ENOMEM;
13746 
13747 	new_coalesce.n_rules = n_rules;
13748 	i = 0;
13749 
13750 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
13751 			    rem_rule) {
13752 		err = nl80211_parse_coalesce_rule(rdev, rule,
13753 						  &new_coalesce.rules[i]);
13754 		if (err)
13755 			goto error;
13756 
13757 		i++;
13758 	}
13759 
13760 	err = rdev_set_coalesce(rdev, &new_coalesce);
13761 	if (err)
13762 		goto error;
13763 
13764 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
13765 	if (!n_coalesce) {
13766 		err = -ENOMEM;
13767 		goto error;
13768 	}
13769 	cfg80211_rdev_free_coalesce(rdev);
13770 	rdev->coalesce = n_coalesce;
13771 
13772 	return 0;
13773 error:
13774 	for (i = 0; i < new_coalesce.n_rules; i++) {
13775 		tmp_rule = &new_coalesce.rules[i];
13776 		for (j = 0; j < tmp_rule->n_patterns; j++)
13777 			kfree(tmp_rule->patterns[j].mask);
13778 		kfree(tmp_rule->patterns);
13779 	}
13780 	kfree(new_coalesce.rules);
13781 
13782 	return err;
13783 }
13784 
13785 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
13786 {
13787 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13788 	struct net_device *dev = info->user_ptr[1];
13789 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13790 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
13791 	struct cfg80211_gtk_rekey_data rekey_data = {};
13792 	int err;
13793 
13794 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
13795 		return -EINVAL;
13796 
13797 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
13798 					  info->attrs[NL80211_ATTR_REKEY_DATA],
13799 					  nl80211_rekey_policy, info->extack);
13800 	if (err)
13801 		return err;
13802 
13803 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
13804 	    !tb[NL80211_REKEY_DATA_KCK])
13805 		return -EINVAL;
13806 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN &&
13807 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
13808 	      nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KEK_EXT_LEN))
13809 		return -ERANGE;
13810 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN &&
13811 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
13812 	      nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KCK_EXT_LEN))
13813 		return -ERANGE;
13814 
13815 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
13816 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
13817 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
13818 	rekey_data.kek_len = nla_len(tb[NL80211_REKEY_DATA_KEK]);
13819 	rekey_data.kck_len = nla_len(tb[NL80211_REKEY_DATA_KCK]);
13820 	if (tb[NL80211_REKEY_DATA_AKM])
13821 		rekey_data.akm = nla_get_u32(tb[NL80211_REKEY_DATA_AKM]);
13822 
13823 	wdev_lock(wdev);
13824 	if (!wdev->connected) {
13825 		err = -ENOTCONN;
13826 		goto out;
13827 	}
13828 
13829 	if (!rdev->ops->set_rekey_data) {
13830 		err = -EOPNOTSUPP;
13831 		goto out;
13832 	}
13833 
13834 	err = rdev_set_rekey_data(rdev, dev, &rekey_data);
13835  out:
13836 	wdev_unlock(wdev);
13837 	return err;
13838 }
13839 
13840 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
13841 					     struct genl_info *info)
13842 {
13843 	struct net_device *dev = info->user_ptr[1];
13844 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13845 
13846 	if (wdev->iftype != NL80211_IFTYPE_AP &&
13847 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
13848 		return -EINVAL;
13849 
13850 	if (wdev->ap_unexpected_nlportid)
13851 		return -EBUSY;
13852 
13853 	wdev->ap_unexpected_nlportid = info->snd_portid;
13854 	return 0;
13855 }
13856 
13857 static int nl80211_probe_client(struct sk_buff *skb,
13858 				struct genl_info *info)
13859 {
13860 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13861 	struct net_device *dev = info->user_ptr[1];
13862 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13863 	struct sk_buff *msg;
13864 	void *hdr;
13865 	const u8 *addr;
13866 	u64 cookie;
13867 	int err;
13868 
13869 	if (wdev->iftype != NL80211_IFTYPE_AP &&
13870 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
13871 		return -EOPNOTSUPP;
13872 
13873 	if (!info->attrs[NL80211_ATTR_MAC])
13874 		return -EINVAL;
13875 
13876 	if (!rdev->ops->probe_client)
13877 		return -EOPNOTSUPP;
13878 
13879 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13880 	if (!msg)
13881 		return -ENOMEM;
13882 
13883 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13884 			     NL80211_CMD_PROBE_CLIENT);
13885 	if (!hdr) {
13886 		err = -ENOBUFS;
13887 		goto free_msg;
13888 	}
13889 
13890 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
13891 
13892 	err = rdev_probe_client(rdev, dev, addr, &cookie);
13893 	if (err)
13894 		goto free_msg;
13895 
13896 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
13897 			      NL80211_ATTR_PAD))
13898 		goto nla_put_failure;
13899 
13900 	genlmsg_end(msg, hdr);
13901 
13902 	return genlmsg_reply(msg, info);
13903 
13904  nla_put_failure:
13905 	err = -ENOBUFS;
13906  free_msg:
13907 	nlmsg_free(msg);
13908 	return err;
13909 }
13910 
13911 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
13912 {
13913 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13914 	struct cfg80211_beacon_registration *reg, *nreg;
13915 	int rv;
13916 
13917 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
13918 		return -EOPNOTSUPP;
13919 
13920 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
13921 	if (!nreg)
13922 		return -ENOMEM;
13923 
13924 	/* First, check if already registered. */
13925 	spin_lock_bh(&rdev->beacon_registrations_lock);
13926 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
13927 		if (reg->nlportid == info->snd_portid) {
13928 			rv = -EALREADY;
13929 			goto out_err;
13930 		}
13931 	}
13932 	/* Add it to the list */
13933 	nreg->nlportid = info->snd_portid;
13934 	list_add(&nreg->list, &rdev->beacon_registrations);
13935 
13936 	spin_unlock_bh(&rdev->beacon_registrations_lock);
13937 
13938 	return 0;
13939 out_err:
13940 	spin_unlock_bh(&rdev->beacon_registrations_lock);
13941 	kfree(nreg);
13942 	return rv;
13943 }
13944 
13945 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
13946 {
13947 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13948 	struct wireless_dev *wdev = info->user_ptr[1];
13949 	int err;
13950 
13951 	if (!rdev->ops->start_p2p_device)
13952 		return -EOPNOTSUPP;
13953 
13954 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
13955 		return -EOPNOTSUPP;
13956 
13957 	if (wdev_running(wdev))
13958 		return 0;
13959 
13960 	if (rfkill_blocked(rdev->wiphy.rfkill))
13961 		return -ERFKILL;
13962 
13963 	err = rdev_start_p2p_device(rdev, wdev);
13964 	if (err)
13965 		return err;
13966 
13967 	wdev->is_running = true;
13968 	rdev->opencount++;
13969 
13970 	return 0;
13971 }
13972 
13973 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
13974 {
13975 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13976 	struct wireless_dev *wdev = info->user_ptr[1];
13977 
13978 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
13979 		return -EOPNOTSUPP;
13980 
13981 	if (!rdev->ops->stop_p2p_device)
13982 		return -EOPNOTSUPP;
13983 
13984 	cfg80211_stop_p2p_device(rdev, wdev);
13985 
13986 	return 0;
13987 }
13988 
13989 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
13990 {
13991 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13992 	struct wireless_dev *wdev = info->user_ptr[1];
13993 	struct cfg80211_nan_conf conf = {};
13994 	int err;
13995 
13996 	if (wdev->iftype != NL80211_IFTYPE_NAN)
13997 		return -EOPNOTSUPP;
13998 
13999 	if (wdev_running(wdev))
14000 		return -EEXIST;
14001 
14002 	if (rfkill_blocked(rdev->wiphy.rfkill))
14003 		return -ERFKILL;
14004 
14005 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
14006 		return -EINVAL;
14007 
14008 	conf.master_pref =
14009 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14010 
14011 	if (info->attrs[NL80211_ATTR_BANDS]) {
14012 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14013 
14014 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14015 			return -EOPNOTSUPP;
14016 
14017 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14018 			return -EINVAL;
14019 
14020 		conf.bands = bands;
14021 	}
14022 
14023 	err = rdev_start_nan(rdev, wdev, &conf);
14024 	if (err)
14025 		return err;
14026 
14027 	wdev->is_running = true;
14028 	rdev->opencount++;
14029 
14030 	return 0;
14031 }
14032 
14033 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
14034 {
14035 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14036 	struct wireless_dev *wdev = info->user_ptr[1];
14037 
14038 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14039 		return -EOPNOTSUPP;
14040 
14041 	cfg80211_stop_nan(rdev, wdev);
14042 
14043 	return 0;
14044 }
14045 
14046 static int validate_nan_filter(struct nlattr *filter_attr)
14047 {
14048 	struct nlattr *attr;
14049 	int len = 0, n_entries = 0, rem;
14050 
14051 	nla_for_each_nested(attr, filter_attr, rem) {
14052 		len += nla_len(attr);
14053 		n_entries++;
14054 	}
14055 
14056 	if (len >= U8_MAX)
14057 		return -EINVAL;
14058 
14059 	return n_entries;
14060 }
14061 
14062 static int handle_nan_filter(struct nlattr *attr_filter,
14063 			     struct cfg80211_nan_func *func,
14064 			     bool tx)
14065 {
14066 	struct nlattr *attr;
14067 	int n_entries, rem, i;
14068 	struct cfg80211_nan_func_filter *filter;
14069 
14070 	n_entries = validate_nan_filter(attr_filter);
14071 	if (n_entries < 0)
14072 		return n_entries;
14073 
14074 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
14075 
14076 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
14077 	if (!filter)
14078 		return -ENOMEM;
14079 
14080 	i = 0;
14081 	nla_for_each_nested(attr, attr_filter, rem) {
14082 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
14083 		if (!filter[i].filter)
14084 			goto err;
14085 
14086 		filter[i].len = nla_len(attr);
14087 		i++;
14088 	}
14089 	if (tx) {
14090 		func->num_tx_filters = n_entries;
14091 		func->tx_filters = filter;
14092 	} else {
14093 		func->num_rx_filters = n_entries;
14094 		func->rx_filters = filter;
14095 	}
14096 
14097 	return 0;
14098 
14099 err:
14100 	i = 0;
14101 	nla_for_each_nested(attr, attr_filter, rem) {
14102 		kfree(filter[i].filter);
14103 		i++;
14104 	}
14105 	kfree(filter);
14106 	return -ENOMEM;
14107 }
14108 
14109 static int nl80211_nan_add_func(struct sk_buff *skb,
14110 				struct genl_info *info)
14111 {
14112 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14113 	struct wireless_dev *wdev = info->user_ptr[1];
14114 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
14115 	struct cfg80211_nan_func *func;
14116 	struct sk_buff *msg = NULL;
14117 	void *hdr = NULL;
14118 	int err = 0;
14119 
14120 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14121 		return -EOPNOTSUPP;
14122 
14123 	if (!wdev_running(wdev))
14124 		return -ENOTCONN;
14125 
14126 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
14127 		return -EINVAL;
14128 
14129 	err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
14130 					  info->attrs[NL80211_ATTR_NAN_FUNC],
14131 					  nl80211_nan_func_policy,
14132 					  info->extack);
14133 	if (err)
14134 		return err;
14135 
14136 	func = kzalloc(sizeof(*func), GFP_KERNEL);
14137 	if (!func)
14138 		return -ENOMEM;
14139 
14140 	func->cookie = cfg80211_assign_cookie(rdev);
14141 
14142 	if (!tb[NL80211_NAN_FUNC_TYPE]) {
14143 		err = -EINVAL;
14144 		goto out;
14145 	}
14146 
14147 
14148 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
14149 
14150 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
14151 		err = -EINVAL;
14152 		goto out;
14153 	}
14154 
14155 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
14156 	       sizeof(func->service_id));
14157 
14158 	func->close_range =
14159 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
14160 
14161 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
14162 		func->serv_spec_info_len =
14163 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
14164 		func->serv_spec_info =
14165 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
14166 				func->serv_spec_info_len,
14167 				GFP_KERNEL);
14168 		if (!func->serv_spec_info) {
14169 			err = -ENOMEM;
14170 			goto out;
14171 		}
14172 	}
14173 
14174 	if (tb[NL80211_NAN_FUNC_TTL])
14175 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
14176 
14177 	switch (func->type) {
14178 	case NL80211_NAN_FUNC_PUBLISH:
14179 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
14180 			err = -EINVAL;
14181 			goto out;
14182 		}
14183 
14184 		func->publish_type =
14185 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
14186 		func->publish_bcast =
14187 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
14188 
14189 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
14190 			func->publish_bcast) {
14191 			err = -EINVAL;
14192 			goto out;
14193 		}
14194 		break;
14195 	case NL80211_NAN_FUNC_SUBSCRIBE:
14196 		func->subscribe_active =
14197 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
14198 		break;
14199 	case NL80211_NAN_FUNC_FOLLOW_UP:
14200 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
14201 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
14202 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
14203 			err = -EINVAL;
14204 			goto out;
14205 		}
14206 
14207 		func->followup_id =
14208 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
14209 		func->followup_reqid =
14210 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
14211 		memcpy(func->followup_dest.addr,
14212 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
14213 		       sizeof(func->followup_dest.addr));
14214 		if (func->ttl) {
14215 			err = -EINVAL;
14216 			goto out;
14217 		}
14218 		break;
14219 	default:
14220 		err = -EINVAL;
14221 		goto out;
14222 	}
14223 
14224 	if (tb[NL80211_NAN_FUNC_SRF]) {
14225 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
14226 
14227 		err = nla_parse_nested_deprecated(srf_tb,
14228 						  NL80211_NAN_SRF_ATTR_MAX,
14229 						  tb[NL80211_NAN_FUNC_SRF],
14230 						  nl80211_nan_srf_policy,
14231 						  info->extack);
14232 		if (err)
14233 			goto out;
14234 
14235 		func->srf_include =
14236 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
14237 
14238 		if (srf_tb[NL80211_NAN_SRF_BF]) {
14239 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
14240 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
14241 				err = -EINVAL;
14242 				goto out;
14243 			}
14244 
14245 			func->srf_bf_len =
14246 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
14247 			func->srf_bf =
14248 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
14249 					func->srf_bf_len, GFP_KERNEL);
14250 			if (!func->srf_bf) {
14251 				err = -ENOMEM;
14252 				goto out;
14253 			}
14254 
14255 			func->srf_bf_idx =
14256 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
14257 		} else {
14258 			struct nlattr *attr, *mac_attr =
14259 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
14260 			int n_entries, rem, i = 0;
14261 
14262 			if (!mac_attr) {
14263 				err = -EINVAL;
14264 				goto out;
14265 			}
14266 
14267 			n_entries = validate_acl_mac_addrs(mac_attr);
14268 			if (n_entries <= 0) {
14269 				err = -EINVAL;
14270 				goto out;
14271 			}
14272 
14273 			func->srf_num_macs = n_entries;
14274 			func->srf_macs =
14275 				kcalloc(n_entries, sizeof(*func->srf_macs),
14276 					GFP_KERNEL);
14277 			if (!func->srf_macs) {
14278 				err = -ENOMEM;
14279 				goto out;
14280 			}
14281 
14282 			nla_for_each_nested(attr, mac_attr, rem)
14283 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
14284 				       sizeof(*func->srf_macs));
14285 		}
14286 	}
14287 
14288 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
14289 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
14290 					func, true);
14291 		if (err)
14292 			goto out;
14293 	}
14294 
14295 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
14296 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
14297 					func, false);
14298 		if (err)
14299 			goto out;
14300 	}
14301 
14302 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14303 	if (!msg) {
14304 		err = -ENOMEM;
14305 		goto out;
14306 	}
14307 
14308 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14309 			     NL80211_CMD_ADD_NAN_FUNCTION);
14310 	/* This can't really happen - we just allocated 4KB */
14311 	if (WARN_ON(!hdr)) {
14312 		err = -ENOMEM;
14313 		goto out;
14314 	}
14315 
14316 	err = rdev_add_nan_func(rdev, wdev, func);
14317 out:
14318 	if (err < 0) {
14319 		cfg80211_free_nan_func(func);
14320 		nlmsg_free(msg);
14321 		return err;
14322 	}
14323 
14324 	/* propagate the instance id and cookie to userspace  */
14325 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
14326 			      NL80211_ATTR_PAD))
14327 		goto nla_put_failure;
14328 
14329 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14330 	if (!func_attr)
14331 		goto nla_put_failure;
14332 
14333 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
14334 		       func->instance_id))
14335 		goto nla_put_failure;
14336 
14337 	nla_nest_end(msg, func_attr);
14338 
14339 	genlmsg_end(msg, hdr);
14340 	return genlmsg_reply(msg, info);
14341 
14342 nla_put_failure:
14343 	nlmsg_free(msg);
14344 	return -ENOBUFS;
14345 }
14346 
14347 static int nl80211_nan_del_func(struct sk_buff *skb,
14348 			       struct genl_info *info)
14349 {
14350 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14351 	struct wireless_dev *wdev = info->user_ptr[1];
14352 	u64 cookie;
14353 
14354 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14355 		return -EOPNOTSUPP;
14356 
14357 	if (!wdev_running(wdev))
14358 		return -ENOTCONN;
14359 
14360 	if (!info->attrs[NL80211_ATTR_COOKIE])
14361 		return -EINVAL;
14362 
14363 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
14364 
14365 	rdev_del_nan_func(rdev, wdev, cookie);
14366 
14367 	return 0;
14368 }
14369 
14370 static int nl80211_nan_change_config(struct sk_buff *skb,
14371 				     struct genl_info *info)
14372 {
14373 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14374 	struct wireless_dev *wdev = info->user_ptr[1];
14375 	struct cfg80211_nan_conf conf = {};
14376 	u32 changed = 0;
14377 
14378 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14379 		return -EOPNOTSUPP;
14380 
14381 	if (!wdev_running(wdev))
14382 		return -ENOTCONN;
14383 
14384 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
14385 		conf.master_pref =
14386 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14387 		if (conf.master_pref <= 1 || conf.master_pref == 255)
14388 			return -EINVAL;
14389 
14390 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
14391 	}
14392 
14393 	if (info->attrs[NL80211_ATTR_BANDS]) {
14394 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14395 
14396 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14397 			return -EOPNOTSUPP;
14398 
14399 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14400 			return -EINVAL;
14401 
14402 		conf.bands = bands;
14403 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
14404 	}
14405 
14406 	if (!changed)
14407 		return -EINVAL;
14408 
14409 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
14410 }
14411 
14412 void cfg80211_nan_match(struct wireless_dev *wdev,
14413 			struct cfg80211_nan_match_params *match, gfp_t gfp)
14414 {
14415 	struct wiphy *wiphy = wdev->wiphy;
14416 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14417 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
14418 	struct sk_buff *msg;
14419 	void *hdr;
14420 
14421 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
14422 		return;
14423 
14424 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14425 	if (!msg)
14426 		return;
14427 
14428 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
14429 	if (!hdr) {
14430 		nlmsg_free(msg);
14431 		return;
14432 	}
14433 
14434 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14435 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14436 					 wdev->netdev->ifindex)) ||
14437 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14438 			      NL80211_ATTR_PAD))
14439 		goto nla_put_failure;
14440 
14441 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
14442 			      NL80211_ATTR_PAD) ||
14443 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
14444 		goto nla_put_failure;
14445 
14446 	match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
14447 	if (!match_attr)
14448 		goto nla_put_failure;
14449 
14450 	local_func_attr = nla_nest_start_noflag(msg,
14451 						NL80211_NAN_MATCH_FUNC_LOCAL);
14452 	if (!local_func_attr)
14453 		goto nla_put_failure;
14454 
14455 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
14456 		goto nla_put_failure;
14457 
14458 	nla_nest_end(msg, local_func_attr);
14459 
14460 	peer_func_attr = nla_nest_start_noflag(msg,
14461 					       NL80211_NAN_MATCH_FUNC_PEER);
14462 	if (!peer_func_attr)
14463 		goto nla_put_failure;
14464 
14465 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
14466 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
14467 		goto nla_put_failure;
14468 
14469 	if (match->info && match->info_len &&
14470 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
14471 		    match->info))
14472 		goto nla_put_failure;
14473 
14474 	nla_nest_end(msg, peer_func_attr);
14475 	nla_nest_end(msg, match_attr);
14476 	genlmsg_end(msg, hdr);
14477 
14478 	if (!wdev->owner_nlportid)
14479 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14480 					msg, 0, NL80211_MCGRP_NAN, gfp);
14481 	else
14482 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14483 				wdev->owner_nlportid);
14484 
14485 	return;
14486 
14487 nla_put_failure:
14488 	nlmsg_free(msg);
14489 }
14490 EXPORT_SYMBOL(cfg80211_nan_match);
14491 
14492 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
14493 				  u8 inst_id,
14494 				  enum nl80211_nan_func_term_reason reason,
14495 				  u64 cookie, gfp_t gfp)
14496 {
14497 	struct wiphy *wiphy = wdev->wiphy;
14498 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14499 	struct sk_buff *msg;
14500 	struct nlattr *func_attr;
14501 	void *hdr;
14502 
14503 	if (WARN_ON(!inst_id))
14504 		return;
14505 
14506 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14507 	if (!msg)
14508 		return;
14509 
14510 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
14511 	if (!hdr) {
14512 		nlmsg_free(msg);
14513 		return;
14514 	}
14515 
14516 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14517 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14518 					 wdev->netdev->ifindex)) ||
14519 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14520 			      NL80211_ATTR_PAD))
14521 		goto nla_put_failure;
14522 
14523 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14524 			      NL80211_ATTR_PAD))
14525 		goto nla_put_failure;
14526 
14527 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14528 	if (!func_attr)
14529 		goto nla_put_failure;
14530 
14531 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
14532 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
14533 		goto nla_put_failure;
14534 
14535 	nla_nest_end(msg, func_attr);
14536 	genlmsg_end(msg, hdr);
14537 
14538 	if (!wdev->owner_nlportid)
14539 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14540 					msg, 0, NL80211_MCGRP_NAN, gfp);
14541 	else
14542 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14543 				wdev->owner_nlportid);
14544 
14545 	return;
14546 
14547 nla_put_failure:
14548 	nlmsg_free(msg);
14549 }
14550 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
14551 
14552 static int nl80211_get_protocol_features(struct sk_buff *skb,
14553 					 struct genl_info *info)
14554 {
14555 	void *hdr;
14556 	struct sk_buff *msg;
14557 
14558 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14559 	if (!msg)
14560 		return -ENOMEM;
14561 
14562 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14563 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
14564 	if (!hdr)
14565 		goto nla_put_failure;
14566 
14567 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
14568 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
14569 		goto nla_put_failure;
14570 
14571 	genlmsg_end(msg, hdr);
14572 	return genlmsg_reply(msg, info);
14573 
14574  nla_put_failure:
14575 	kfree_skb(msg);
14576 	return -ENOBUFS;
14577 }
14578 
14579 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
14580 {
14581 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14582 	struct cfg80211_update_ft_ies_params ft_params;
14583 	struct net_device *dev = info->user_ptr[1];
14584 
14585 	if (!rdev->ops->update_ft_ies)
14586 		return -EOPNOTSUPP;
14587 
14588 	if (!info->attrs[NL80211_ATTR_MDID] ||
14589 	    !info->attrs[NL80211_ATTR_IE])
14590 		return -EINVAL;
14591 
14592 	memset(&ft_params, 0, sizeof(ft_params));
14593 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
14594 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
14595 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
14596 
14597 	return rdev_update_ft_ies(rdev, dev, &ft_params);
14598 }
14599 
14600 static int nl80211_crit_protocol_start(struct sk_buff *skb,
14601 				       struct genl_info *info)
14602 {
14603 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14604 	struct wireless_dev *wdev = info->user_ptr[1];
14605 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
14606 	u16 duration;
14607 	int ret;
14608 
14609 	if (!rdev->ops->crit_proto_start)
14610 		return -EOPNOTSUPP;
14611 
14612 	if (WARN_ON(!rdev->ops->crit_proto_stop))
14613 		return -EINVAL;
14614 
14615 	if (rdev->crit_proto_nlportid)
14616 		return -EBUSY;
14617 
14618 	/* determine protocol if provided */
14619 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
14620 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
14621 
14622 	if (proto >= NUM_NL80211_CRIT_PROTO)
14623 		return -EINVAL;
14624 
14625 	/* timeout must be provided */
14626 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
14627 		return -EINVAL;
14628 
14629 	duration =
14630 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
14631 
14632 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
14633 	if (!ret)
14634 		rdev->crit_proto_nlportid = info->snd_portid;
14635 
14636 	return ret;
14637 }
14638 
14639 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
14640 				      struct genl_info *info)
14641 {
14642 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14643 	struct wireless_dev *wdev = info->user_ptr[1];
14644 
14645 	if (!rdev->ops->crit_proto_stop)
14646 		return -EOPNOTSUPP;
14647 
14648 	if (rdev->crit_proto_nlportid) {
14649 		rdev->crit_proto_nlportid = 0;
14650 		rdev_crit_proto_stop(rdev, wdev);
14651 	}
14652 	return 0;
14653 }
14654 
14655 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
14656 				       struct nlattr *attr,
14657 				       struct netlink_ext_ack *extack)
14658 {
14659 	if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
14660 		if (attr->nla_type & NLA_F_NESTED) {
14661 			NL_SET_ERR_MSG_ATTR(extack, attr,
14662 					    "unexpected nested data");
14663 			return -EINVAL;
14664 		}
14665 
14666 		return 0;
14667 	}
14668 
14669 	if (!(attr->nla_type & NLA_F_NESTED)) {
14670 		NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
14671 		return -EINVAL;
14672 	}
14673 
14674 	return nla_validate_nested(attr, vcmd->maxattr, vcmd->policy, extack);
14675 }
14676 
14677 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
14678 {
14679 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14680 	struct wireless_dev *wdev =
14681 		__cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
14682 					   info->attrs);
14683 	int i, err;
14684 	u32 vid, subcmd;
14685 
14686 	if (!rdev->wiphy.vendor_commands)
14687 		return -EOPNOTSUPP;
14688 
14689 	if (IS_ERR(wdev)) {
14690 		err = PTR_ERR(wdev);
14691 		if (err != -EINVAL)
14692 			return err;
14693 		wdev = NULL;
14694 	} else if (wdev->wiphy != &rdev->wiphy) {
14695 		return -EINVAL;
14696 	}
14697 
14698 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
14699 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
14700 		return -EINVAL;
14701 
14702 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
14703 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
14704 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
14705 		const struct wiphy_vendor_command *vcmd;
14706 		void *data = NULL;
14707 		int len = 0;
14708 
14709 		vcmd = &rdev->wiphy.vendor_commands[i];
14710 
14711 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
14712 			continue;
14713 
14714 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
14715 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
14716 			if (!wdev)
14717 				return -EINVAL;
14718 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
14719 			    !wdev->netdev)
14720 				return -EINVAL;
14721 
14722 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
14723 				if (!wdev_running(wdev))
14724 					return -ENETDOWN;
14725 			}
14726 		} else {
14727 			wdev = NULL;
14728 		}
14729 
14730 		if (!vcmd->doit)
14731 			return -EOPNOTSUPP;
14732 
14733 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
14734 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
14735 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
14736 
14737 			err = nl80211_vendor_check_policy(vcmd,
14738 					info->attrs[NL80211_ATTR_VENDOR_DATA],
14739 					info->extack);
14740 			if (err)
14741 				return err;
14742 		}
14743 
14744 		rdev->cur_cmd_info = info;
14745 		err = vcmd->doit(&rdev->wiphy, wdev, data, len);
14746 		rdev->cur_cmd_info = NULL;
14747 		return err;
14748 	}
14749 
14750 	return -EOPNOTSUPP;
14751 }
14752 
14753 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
14754 				       struct netlink_callback *cb,
14755 				       struct cfg80211_registered_device **rdev,
14756 				       struct wireless_dev **wdev)
14757 {
14758 	struct nlattr **attrbuf;
14759 	u32 vid, subcmd;
14760 	unsigned int i;
14761 	int vcmd_idx = -1;
14762 	int err;
14763 	void *data = NULL;
14764 	unsigned int data_len = 0;
14765 
14766 	if (cb->args[0]) {
14767 		/* subtract the 1 again here */
14768 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
14769 		struct wireless_dev *tmp;
14770 
14771 		if (!wiphy)
14772 			return -ENODEV;
14773 		*rdev = wiphy_to_rdev(wiphy);
14774 		*wdev = NULL;
14775 
14776 		if (cb->args[1]) {
14777 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
14778 				if (tmp->identifier == cb->args[1] - 1) {
14779 					*wdev = tmp;
14780 					break;
14781 				}
14782 			}
14783 		}
14784 
14785 		/* keep rtnl locked in successful case */
14786 		return 0;
14787 	}
14788 
14789 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
14790 	if (!attrbuf)
14791 		return -ENOMEM;
14792 
14793 	err = nlmsg_parse_deprecated(cb->nlh,
14794 				     GENL_HDRLEN + nl80211_fam.hdrsize,
14795 				     attrbuf, nl80211_fam.maxattr,
14796 				     nl80211_policy, NULL);
14797 	if (err)
14798 		goto out;
14799 
14800 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
14801 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
14802 		err = -EINVAL;
14803 		goto out;
14804 	}
14805 
14806 	*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(skb->sk), attrbuf);
14807 	if (IS_ERR(*wdev))
14808 		*wdev = NULL;
14809 
14810 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
14811 	if (IS_ERR(*rdev)) {
14812 		err = PTR_ERR(*rdev);
14813 		goto out;
14814 	}
14815 
14816 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
14817 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
14818 
14819 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
14820 		const struct wiphy_vendor_command *vcmd;
14821 
14822 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
14823 
14824 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
14825 			continue;
14826 
14827 		if (!vcmd->dumpit) {
14828 			err = -EOPNOTSUPP;
14829 			goto out;
14830 		}
14831 
14832 		vcmd_idx = i;
14833 		break;
14834 	}
14835 
14836 	if (vcmd_idx < 0) {
14837 		err = -EOPNOTSUPP;
14838 		goto out;
14839 	}
14840 
14841 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
14842 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
14843 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
14844 
14845 		err = nl80211_vendor_check_policy(
14846 				&(*rdev)->wiphy.vendor_commands[vcmd_idx],
14847 				attrbuf[NL80211_ATTR_VENDOR_DATA],
14848 				cb->extack);
14849 		if (err)
14850 			goto out;
14851 	}
14852 
14853 	/* 0 is the first index - add 1 to parse only once */
14854 	cb->args[0] = (*rdev)->wiphy_idx + 1;
14855 	/* add 1 to know if it was NULL */
14856 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
14857 	cb->args[2] = vcmd_idx;
14858 	cb->args[3] = (unsigned long)data;
14859 	cb->args[4] = data_len;
14860 
14861 	/* keep rtnl locked in successful case */
14862 	err = 0;
14863 out:
14864 	kfree(attrbuf);
14865 	return err;
14866 }
14867 
14868 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
14869 				   struct netlink_callback *cb)
14870 {
14871 	struct cfg80211_registered_device *rdev;
14872 	struct wireless_dev *wdev;
14873 	unsigned int vcmd_idx;
14874 	const struct wiphy_vendor_command *vcmd;
14875 	void *data;
14876 	int data_len;
14877 	int err;
14878 	struct nlattr *vendor_data;
14879 
14880 	rtnl_lock();
14881 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
14882 	if (err)
14883 		goto out;
14884 
14885 	vcmd_idx = cb->args[2];
14886 	data = (void *)cb->args[3];
14887 	data_len = cb->args[4];
14888 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
14889 
14890 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
14891 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
14892 		if (!wdev) {
14893 			err = -EINVAL;
14894 			goto out;
14895 		}
14896 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
14897 		    !wdev->netdev) {
14898 			err = -EINVAL;
14899 			goto out;
14900 		}
14901 
14902 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
14903 			if (!wdev_running(wdev)) {
14904 				err = -ENETDOWN;
14905 				goto out;
14906 			}
14907 		}
14908 	}
14909 
14910 	while (1) {
14911 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
14912 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
14913 					   NL80211_CMD_VENDOR);
14914 		if (!hdr)
14915 			break;
14916 
14917 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14918 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
14919 					       wdev_id(wdev),
14920 					       NL80211_ATTR_PAD))) {
14921 			genlmsg_cancel(skb, hdr);
14922 			break;
14923 		}
14924 
14925 		vendor_data = nla_nest_start_noflag(skb,
14926 						    NL80211_ATTR_VENDOR_DATA);
14927 		if (!vendor_data) {
14928 			genlmsg_cancel(skb, hdr);
14929 			break;
14930 		}
14931 
14932 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
14933 				   (unsigned long *)&cb->args[5]);
14934 		nla_nest_end(skb, vendor_data);
14935 
14936 		if (err == -ENOBUFS || err == -ENOENT) {
14937 			genlmsg_cancel(skb, hdr);
14938 			break;
14939 		} else if (err <= 0) {
14940 			genlmsg_cancel(skb, hdr);
14941 			goto out;
14942 		}
14943 
14944 		genlmsg_end(skb, hdr);
14945 	}
14946 
14947 	err = skb->len;
14948  out:
14949 	rtnl_unlock();
14950 	return err;
14951 }
14952 
14953 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
14954 					   enum nl80211_commands cmd,
14955 					   enum nl80211_attrs attr,
14956 					   int approxlen)
14957 {
14958 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14959 
14960 	if (WARN_ON(!rdev->cur_cmd_info))
14961 		return NULL;
14962 
14963 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
14964 					   rdev->cur_cmd_info->snd_portid,
14965 					   rdev->cur_cmd_info->snd_seq,
14966 					   cmd, attr, NULL, GFP_KERNEL);
14967 }
14968 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
14969 
14970 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
14971 {
14972 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
14973 	void *hdr = ((void **)skb->cb)[1];
14974 	struct nlattr *data = ((void **)skb->cb)[2];
14975 
14976 	/* clear CB data for netlink core to own from now on */
14977 	memset(skb->cb, 0, sizeof(skb->cb));
14978 
14979 	if (WARN_ON(!rdev->cur_cmd_info)) {
14980 		kfree_skb(skb);
14981 		return -EINVAL;
14982 	}
14983 
14984 	nla_nest_end(skb, data);
14985 	genlmsg_end(skb, hdr);
14986 	return genlmsg_reply(skb, rdev->cur_cmd_info);
14987 }
14988 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
14989 
14990 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
14991 {
14992 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14993 
14994 	if (WARN_ON(!rdev->cur_cmd_info))
14995 		return 0;
14996 
14997 	return rdev->cur_cmd_info->snd_portid;
14998 }
14999 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
15000 
15001 static int nl80211_set_qos_map(struct sk_buff *skb,
15002 			       struct genl_info *info)
15003 {
15004 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15005 	struct cfg80211_qos_map *qos_map = NULL;
15006 	struct net_device *dev = info->user_ptr[1];
15007 	u8 *pos, len, num_des, des_len, des;
15008 	int ret;
15009 
15010 	if (!rdev->ops->set_qos_map)
15011 		return -EOPNOTSUPP;
15012 
15013 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
15014 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
15015 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
15016 
15017 		if (len % 2)
15018 			return -EINVAL;
15019 
15020 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
15021 		if (!qos_map)
15022 			return -ENOMEM;
15023 
15024 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
15025 		if (num_des) {
15026 			des_len = num_des *
15027 				sizeof(struct cfg80211_dscp_exception);
15028 			memcpy(qos_map->dscp_exception, pos, des_len);
15029 			qos_map->num_des = num_des;
15030 			for (des = 0; des < num_des; des++) {
15031 				if (qos_map->dscp_exception[des].up > 7) {
15032 					kfree(qos_map);
15033 					return -EINVAL;
15034 				}
15035 			}
15036 			pos += des_len;
15037 		}
15038 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
15039 	}
15040 
15041 	wdev_lock(dev->ieee80211_ptr);
15042 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
15043 	if (!ret)
15044 		ret = rdev_set_qos_map(rdev, dev, qos_map);
15045 	wdev_unlock(dev->ieee80211_ptr);
15046 
15047 	kfree(qos_map);
15048 	return ret;
15049 }
15050 
15051 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
15052 {
15053 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15054 	struct net_device *dev = info->user_ptr[1];
15055 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15056 	const u8 *peer;
15057 	u8 tsid, up;
15058 	u16 admitted_time = 0;
15059 	int err;
15060 
15061 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
15062 		return -EOPNOTSUPP;
15063 
15064 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
15065 	    !info->attrs[NL80211_ATTR_USER_PRIO])
15066 		return -EINVAL;
15067 
15068 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15069 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
15070 
15071 	/* WMM uses TIDs 0-7 even for TSPEC */
15072 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
15073 		/* TODO: handle 802.11 TSPEC/admission control
15074 		 * need more attributes for that (e.g. BA session requirement);
15075 		 * change the WMM adminssion test above to allow both then
15076 		 */
15077 		return -EINVAL;
15078 	}
15079 
15080 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15081 
15082 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
15083 		admitted_time =
15084 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
15085 		if (!admitted_time)
15086 			return -EINVAL;
15087 	}
15088 
15089 	wdev_lock(wdev);
15090 	switch (wdev->iftype) {
15091 	case NL80211_IFTYPE_STATION:
15092 	case NL80211_IFTYPE_P2P_CLIENT:
15093 		if (wdev->connected)
15094 			break;
15095 		err = -ENOTCONN;
15096 		goto out;
15097 	default:
15098 		err = -EOPNOTSUPP;
15099 		goto out;
15100 	}
15101 
15102 	err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
15103 
15104  out:
15105 	wdev_unlock(wdev);
15106 	return err;
15107 }
15108 
15109 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
15110 {
15111 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15112 	struct net_device *dev = info->user_ptr[1];
15113 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15114 	const u8 *peer;
15115 	u8 tsid;
15116 	int err;
15117 
15118 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
15119 		return -EINVAL;
15120 
15121 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15122 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15123 
15124 	wdev_lock(wdev);
15125 	err = rdev_del_tx_ts(rdev, dev, tsid, peer);
15126 	wdev_unlock(wdev);
15127 
15128 	return err;
15129 }
15130 
15131 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
15132 				       struct genl_info *info)
15133 {
15134 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15135 	struct net_device *dev = info->user_ptr[1];
15136 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15137 	struct cfg80211_chan_def chandef = {};
15138 	const u8 *addr;
15139 	u8 oper_class;
15140 	int err;
15141 
15142 	if (!rdev->ops->tdls_channel_switch ||
15143 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15144 		return -EOPNOTSUPP;
15145 
15146 	switch (dev->ieee80211_ptr->iftype) {
15147 	case NL80211_IFTYPE_STATION:
15148 	case NL80211_IFTYPE_P2P_CLIENT:
15149 		break;
15150 	default:
15151 		return -EOPNOTSUPP;
15152 	}
15153 
15154 	if (!info->attrs[NL80211_ATTR_MAC] ||
15155 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
15156 		return -EINVAL;
15157 
15158 	err = nl80211_parse_chandef(rdev, info, &chandef);
15159 	if (err)
15160 		return err;
15161 
15162 	/*
15163 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
15164 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
15165 	 * specification is not defined for them.
15166 	 */
15167 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
15168 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
15169 	    chandef.width != NL80211_CHAN_WIDTH_20)
15170 		return -EINVAL;
15171 
15172 	/* we will be active on the TDLS link */
15173 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
15174 					   wdev->iftype))
15175 		return -EINVAL;
15176 
15177 	/* don't allow switching to DFS channels */
15178 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
15179 		return -EINVAL;
15180 
15181 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15182 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
15183 
15184 	wdev_lock(wdev);
15185 	err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
15186 	wdev_unlock(wdev);
15187 
15188 	return err;
15189 }
15190 
15191 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
15192 					      struct genl_info *info)
15193 {
15194 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15195 	struct net_device *dev = info->user_ptr[1];
15196 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15197 	const u8 *addr;
15198 
15199 	if (!rdev->ops->tdls_channel_switch ||
15200 	    !rdev->ops->tdls_cancel_channel_switch ||
15201 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15202 		return -EOPNOTSUPP;
15203 
15204 	switch (dev->ieee80211_ptr->iftype) {
15205 	case NL80211_IFTYPE_STATION:
15206 	case NL80211_IFTYPE_P2P_CLIENT:
15207 		break;
15208 	default:
15209 		return -EOPNOTSUPP;
15210 	}
15211 
15212 	if (!info->attrs[NL80211_ATTR_MAC])
15213 		return -EINVAL;
15214 
15215 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15216 
15217 	wdev_lock(wdev);
15218 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
15219 	wdev_unlock(wdev);
15220 
15221 	return 0;
15222 }
15223 
15224 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
15225 					    struct genl_info *info)
15226 {
15227 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15228 	struct net_device *dev = info->user_ptr[1];
15229 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15230 	const struct nlattr *nla;
15231 	bool enabled;
15232 
15233 	if (!rdev->ops->set_multicast_to_unicast)
15234 		return -EOPNOTSUPP;
15235 
15236 	if (wdev->iftype != NL80211_IFTYPE_AP &&
15237 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
15238 		return -EOPNOTSUPP;
15239 
15240 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
15241 	enabled = nla_get_flag(nla);
15242 
15243 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
15244 }
15245 
15246 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
15247 {
15248 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15249 	struct net_device *dev = info->user_ptr[1];
15250 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15251 	struct cfg80211_pmk_conf pmk_conf = {};
15252 	int ret;
15253 
15254 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15255 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15256 		return -EOPNOTSUPP;
15257 
15258 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15259 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15260 		return -EOPNOTSUPP;
15261 
15262 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
15263 		return -EINVAL;
15264 
15265 	wdev_lock(wdev);
15266 	if (!wdev->connected) {
15267 		ret = -ENOTCONN;
15268 		goto out;
15269 	}
15270 
15271 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15272 	if (memcmp(pmk_conf.aa, wdev->u.client.connected_addr, ETH_ALEN)) {
15273 		ret = -EINVAL;
15274 		goto out;
15275 	}
15276 
15277 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
15278 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
15279 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
15280 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
15281 		ret = -EINVAL;
15282 		goto out;
15283 	}
15284 
15285 	if (info->attrs[NL80211_ATTR_PMKR0_NAME])
15286 		pmk_conf.pmk_r0_name =
15287 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
15288 
15289 	ret = rdev_set_pmk(rdev, dev, &pmk_conf);
15290 out:
15291 	wdev_unlock(wdev);
15292 	return ret;
15293 }
15294 
15295 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
15296 {
15297 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15298 	struct net_device *dev = info->user_ptr[1];
15299 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15300 	const u8 *aa;
15301 	int ret;
15302 
15303 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15304 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15305 		return -EOPNOTSUPP;
15306 
15307 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15308 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15309 		return -EOPNOTSUPP;
15310 
15311 	if (!info->attrs[NL80211_ATTR_MAC])
15312 		return -EINVAL;
15313 
15314 	wdev_lock(wdev);
15315 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15316 	ret = rdev_del_pmk(rdev, dev, aa);
15317 	wdev_unlock(wdev);
15318 
15319 	return ret;
15320 }
15321 
15322 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
15323 {
15324 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15325 	struct net_device *dev = info->user_ptr[1];
15326 	struct cfg80211_external_auth_params params;
15327 
15328 	if (!rdev->ops->external_auth)
15329 		return -EOPNOTSUPP;
15330 
15331 	if (!info->attrs[NL80211_ATTR_SSID] &&
15332 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
15333 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
15334 		return -EINVAL;
15335 
15336 	if (!info->attrs[NL80211_ATTR_BSSID])
15337 		return -EINVAL;
15338 
15339 	if (!info->attrs[NL80211_ATTR_STATUS_CODE])
15340 		return -EINVAL;
15341 
15342 	memset(&params, 0, sizeof(params));
15343 
15344 	if (info->attrs[NL80211_ATTR_SSID]) {
15345 		params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
15346 		if (params.ssid.ssid_len == 0)
15347 			return -EINVAL;
15348 		memcpy(params.ssid.ssid,
15349 		       nla_data(info->attrs[NL80211_ATTR_SSID]),
15350 		       params.ssid.ssid_len);
15351 	}
15352 
15353 	memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
15354 	       ETH_ALEN);
15355 
15356 	params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15357 
15358 	if (info->attrs[NL80211_ATTR_PMKID])
15359 		params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
15360 
15361 	return rdev_external_auth(rdev, dev, &params);
15362 }
15363 
15364 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
15365 {
15366 	bool dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
15367 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15368 	struct net_device *dev = info->user_ptr[1];
15369 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15370 	const u8 *buf;
15371 	size_t len;
15372 	u8 *dest;
15373 	u16 proto;
15374 	bool noencrypt;
15375 	u64 cookie = 0;
15376 	int link_id;
15377 	int err;
15378 
15379 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15380 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
15381 		return -EOPNOTSUPP;
15382 
15383 	if (!rdev->ops->tx_control_port)
15384 		return -EOPNOTSUPP;
15385 
15386 	if (!info->attrs[NL80211_ATTR_FRAME] ||
15387 	    !info->attrs[NL80211_ATTR_MAC] ||
15388 	    !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
15389 		GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
15390 		return -EINVAL;
15391 	}
15392 
15393 	wdev_lock(wdev);
15394 
15395 	switch (wdev->iftype) {
15396 	case NL80211_IFTYPE_AP:
15397 	case NL80211_IFTYPE_P2P_GO:
15398 	case NL80211_IFTYPE_MESH_POINT:
15399 		break;
15400 	case NL80211_IFTYPE_ADHOC:
15401 		if (wdev->u.ibss.current_bss)
15402 			break;
15403 		err = -ENOTCONN;
15404 		goto out;
15405 	case NL80211_IFTYPE_STATION:
15406 	case NL80211_IFTYPE_P2P_CLIENT:
15407 		if (wdev->connected)
15408 			break;
15409 		err = -ENOTCONN;
15410 		goto out;
15411 	default:
15412 		err = -EOPNOTSUPP;
15413 		goto out;
15414 	}
15415 
15416 	wdev_unlock(wdev);
15417 
15418 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15419 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15420 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15421 	proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
15422 	noencrypt =
15423 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
15424 
15425 	link_id = nl80211_link_id_or_invalid(info->attrs);
15426 
15427 	err = rdev_tx_control_port(rdev, dev, buf, len,
15428 				   dest, cpu_to_be16(proto), noencrypt, link_id,
15429 				   dont_wait_for_ack ? NULL : &cookie);
15430 	if (!err && !dont_wait_for_ack)
15431 		nl_set_extack_cookie_u64(info->extack, cookie);
15432 	return err;
15433  out:
15434 	wdev_unlock(wdev);
15435 	return err;
15436 }
15437 
15438 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
15439 					   struct genl_info *info)
15440 {
15441 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15442 	struct net_device *dev = info->user_ptr[1];
15443 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15444 	struct cfg80211_ftm_responder_stats ftm_stats = {};
15445 	unsigned int link_id = nl80211_link_id(info->attrs);
15446 	struct sk_buff *msg;
15447 	void *hdr;
15448 	struct nlattr *ftm_stats_attr;
15449 	int err;
15450 
15451 	if (wdev->iftype != NL80211_IFTYPE_AP ||
15452 	    !wdev->links[link_id].ap.beacon_interval)
15453 		return -EOPNOTSUPP;
15454 
15455 	err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
15456 	if (err)
15457 		return err;
15458 
15459 	if (!ftm_stats.filled)
15460 		return -ENODATA;
15461 
15462 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15463 	if (!msg)
15464 		return -ENOMEM;
15465 
15466 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
15467 			     NL80211_CMD_GET_FTM_RESPONDER_STATS);
15468 	if (!hdr)
15469 		goto nla_put_failure;
15470 
15471 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15472 		goto nla_put_failure;
15473 
15474 	ftm_stats_attr = nla_nest_start_noflag(msg,
15475 					       NL80211_ATTR_FTM_RESPONDER_STATS);
15476 	if (!ftm_stats_attr)
15477 		goto nla_put_failure;
15478 
15479 #define SET_FTM(field, name, type)					 \
15480 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15481 	    nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,		 \
15482 			     ftm_stats.field))				 \
15483 		goto nla_put_failure; } while (0)
15484 #define SET_FTM_U64(field, name)					 \
15485 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15486 	    nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,		 \
15487 			      ftm_stats.field, NL80211_FTM_STATS_PAD))	 \
15488 		goto nla_put_failure; } while (0)
15489 
15490 	SET_FTM(success_num, SUCCESS_NUM, u32);
15491 	SET_FTM(partial_num, PARTIAL_NUM, u32);
15492 	SET_FTM(failed_num, FAILED_NUM, u32);
15493 	SET_FTM(asap_num, ASAP_NUM, u32);
15494 	SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
15495 	SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
15496 	SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
15497 	SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
15498 	SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
15499 #undef SET_FTM
15500 
15501 	nla_nest_end(msg, ftm_stats_attr);
15502 
15503 	genlmsg_end(msg, hdr);
15504 	return genlmsg_reply(msg, info);
15505 
15506 nla_put_failure:
15507 	nlmsg_free(msg);
15508 	return -ENOBUFS;
15509 }
15510 
15511 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
15512 {
15513 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15514 	struct cfg80211_update_owe_info owe_info;
15515 	struct net_device *dev = info->user_ptr[1];
15516 
15517 	if (!rdev->ops->update_owe_info)
15518 		return -EOPNOTSUPP;
15519 
15520 	if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
15521 	    !info->attrs[NL80211_ATTR_MAC])
15522 		return -EINVAL;
15523 
15524 	memset(&owe_info, 0, sizeof(owe_info));
15525 	owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15526 	nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
15527 
15528 	if (info->attrs[NL80211_ATTR_IE]) {
15529 		owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
15530 		owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
15531 	}
15532 
15533 	return rdev_update_owe_info(rdev, dev, &owe_info);
15534 }
15535 
15536 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
15537 {
15538 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15539 	struct net_device *dev = info->user_ptr[1];
15540 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15541 	struct station_info sinfo = {};
15542 	const u8 *buf;
15543 	size_t len;
15544 	u8 *dest;
15545 	int err;
15546 
15547 	if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
15548 		return -EOPNOTSUPP;
15549 
15550 	if (!info->attrs[NL80211_ATTR_MAC] ||
15551 	    !info->attrs[NL80211_ATTR_FRAME]) {
15552 		GENL_SET_ERR_MSG(info, "Frame or MAC missing");
15553 		return -EINVAL;
15554 	}
15555 
15556 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
15557 		return -EOPNOTSUPP;
15558 
15559 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15560 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15561 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15562 
15563 	if (len < sizeof(struct ethhdr))
15564 		return -EINVAL;
15565 
15566 	if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
15567 	    !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
15568 		return -EINVAL;
15569 
15570 	err = rdev_get_station(rdev, dev, dest, &sinfo);
15571 	if (err)
15572 		return err;
15573 
15574 	cfg80211_sinfo_release_content(&sinfo);
15575 
15576 	return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
15577 }
15578 
15579 static int parse_tid_conf(struct cfg80211_registered_device *rdev,
15580 			  struct nlattr *attrs[], struct net_device *dev,
15581 			  struct cfg80211_tid_cfg *tid_conf,
15582 			  struct genl_info *info, const u8 *peer,
15583 			  unsigned int link_id)
15584 {
15585 	struct netlink_ext_ack *extack = info->extack;
15586 	u64 mask;
15587 	int err;
15588 
15589 	if (!attrs[NL80211_TID_CONFIG_ATTR_TIDS])
15590 		return -EINVAL;
15591 
15592 	tid_conf->config_override =
15593 			nla_get_flag(attrs[NL80211_TID_CONFIG_ATTR_OVERRIDE]);
15594 	tid_conf->tids = nla_get_u16(attrs[NL80211_TID_CONFIG_ATTR_TIDS]);
15595 
15596 	if (tid_conf->config_override) {
15597 		if (rdev->ops->reset_tid_config) {
15598 			err = rdev_reset_tid_config(rdev, dev, peer,
15599 						    tid_conf->tids);
15600 			if (err)
15601 				return err;
15602 		} else {
15603 			return -EINVAL;
15604 		}
15605 	}
15606 
15607 	if (attrs[NL80211_TID_CONFIG_ATTR_NOACK]) {
15608 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_NOACK);
15609 		tid_conf->noack =
15610 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_NOACK]);
15611 	}
15612 
15613 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]) {
15614 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT);
15615 		tid_conf->retry_short =
15616 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]);
15617 
15618 		if (tid_conf->retry_short > rdev->wiphy.max_data_retry_count)
15619 			return -EINVAL;
15620 	}
15621 
15622 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]) {
15623 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
15624 		tid_conf->retry_long =
15625 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]);
15626 
15627 		if (tid_conf->retry_long > rdev->wiphy.max_data_retry_count)
15628 			return -EINVAL;
15629 	}
15630 
15631 	if (attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]) {
15632 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
15633 		tid_conf->ampdu =
15634 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]);
15635 	}
15636 
15637 	if (attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]) {
15638 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
15639 		tid_conf->rtscts =
15640 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]);
15641 	}
15642 
15643 	if (attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]) {
15644 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
15645 		tid_conf->amsdu =
15646 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]);
15647 	}
15648 
15649 	if (attrs[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE]) {
15650 		u32 idx = NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE, attr;
15651 
15652 		tid_conf->txrate_type = nla_get_u8(attrs[idx]);
15653 
15654 		if (tid_conf->txrate_type != NL80211_TX_RATE_AUTOMATIC) {
15655 			attr = NL80211_TID_CONFIG_ATTR_TX_RATE;
15656 			err = nl80211_parse_tx_bitrate_mask(info, attrs, attr,
15657 						    &tid_conf->txrate_mask, dev,
15658 						    true, link_id);
15659 			if (err)
15660 				return err;
15661 
15662 			tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE);
15663 		}
15664 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
15665 	}
15666 
15667 	if (peer)
15668 		mask = rdev->wiphy.tid_config_support.peer;
15669 	else
15670 		mask = rdev->wiphy.tid_config_support.vif;
15671 
15672 	if (tid_conf->mask & ~mask) {
15673 		NL_SET_ERR_MSG(extack, "unsupported TID configuration");
15674 		return -ENOTSUPP;
15675 	}
15676 
15677 	return 0;
15678 }
15679 
15680 static int nl80211_set_tid_config(struct sk_buff *skb,
15681 				  struct genl_info *info)
15682 {
15683 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15684 	struct nlattr *attrs[NL80211_TID_CONFIG_ATTR_MAX + 1];
15685 	unsigned int link_id = nl80211_link_id(info->attrs);
15686 	struct net_device *dev = info->user_ptr[1];
15687 	struct cfg80211_tid_config *tid_config;
15688 	struct nlattr *tid;
15689 	int conf_idx = 0, rem_conf;
15690 	int ret = -EINVAL;
15691 	u32 num_conf = 0;
15692 
15693 	if (!info->attrs[NL80211_ATTR_TID_CONFIG])
15694 		return -EINVAL;
15695 
15696 	if (!rdev->ops->set_tid_config)
15697 		return -EOPNOTSUPP;
15698 
15699 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
15700 			    rem_conf)
15701 		num_conf++;
15702 
15703 	tid_config = kzalloc(struct_size(tid_config, tid_conf, num_conf),
15704 			     GFP_KERNEL);
15705 	if (!tid_config)
15706 		return -ENOMEM;
15707 
15708 	tid_config->n_tid_conf = num_conf;
15709 
15710 	if (info->attrs[NL80211_ATTR_MAC])
15711 		tid_config->peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15712 
15713 	wdev_lock(dev->ieee80211_ptr);
15714 
15715 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
15716 			    rem_conf) {
15717 		ret = nla_parse_nested(attrs, NL80211_TID_CONFIG_ATTR_MAX,
15718 				       tid, NULL, NULL);
15719 
15720 		if (ret)
15721 			goto bad_tid_conf;
15722 
15723 		ret = parse_tid_conf(rdev, attrs, dev,
15724 				     &tid_config->tid_conf[conf_idx],
15725 				     info, tid_config->peer, link_id);
15726 		if (ret)
15727 			goto bad_tid_conf;
15728 
15729 		conf_idx++;
15730 	}
15731 
15732 	ret = rdev_set_tid_config(rdev, dev, tid_config);
15733 
15734 bad_tid_conf:
15735 	kfree(tid_config);
15736 	wdev_unlock(dev->ieee80211_ptr);
15737 	return ret;
15738 }
15739 
15740 static int nl80211_color_change(struct sk_buff *skb, struct genl_info *info)
15741 {
15742 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15743 	struct cfg80211_color_change_settings params = {};
15744 	struct net_device *dev = info->user_ptr[1];
15745 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15746 	struct nlattr **tb;
15747 	u16 offset;
15748 	int err;
15749 
15750 	if (!rdev->ops->color_change)
15751 		return -EOPNOTSUPP;
15752 
15753 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15754 				     NL80211_EXT_FEATURE_BSS_COLOR))
15755 		return -EOPNOTSUPP;
15756 
15757 	if (wdev->iftype != NL80211_IFTYPE_AP)
15758 		return -EOPNOTSUPP;
15759 
15760 	if (!info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT] ||
15761 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR] ||
15762 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS])
15763 		return -EINVAL;
15764 
15765 	params.count = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT]);
15766 	params.color = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR]);
15767 
15768 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_next);
15769 	if (err)
15770 		return err;
15771 
15772 	tb = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*tb), GFP_KERNEL);
15773 	if (!tb)
15774 		return -ENOMEM;
15775 
15776 	err = nla_parse_nested(tb, NL80211_ATTR_MAX,
15777 			       info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS],
15778 			       nl80211_policy, info->extack);
15779 	if (err)
15780 		goto out;
15781 
15782 	err = nl80211_parse_beacon(rdev, tb, &params.beacon_color_change);
15783 	if (err)
15784 		goto out;
15785 
15786 	if (!tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
15787 		err = -EINVAL;
15788 		goto out;
15789 	}
15790 
15791 	if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) != sizeof(u16)) {
15792 		err = -EINVAL;
15793 		goto out;
15794 	}
15795 
15796 	offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
15797 	if (offset >= params.beacon_color_change.tail_len) {
15798 		err = -EINVAL;
15799 		goto out;
15800 	}
15801 
15802 	if (params.beacon_color_change.tail[offset] != params.count) {
15803 		err = -EINVAL;
15804 		goto out;
15805 	}
15806 
15807 	params.counter_offset_beacon = offset;
15808 
15809 	if (tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
15810 		if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) !=
15811 		    sizeof(u16)) {
15812 			err = -EINVAL;
15813 			goto out;
15814 		}
15815 
15816 		offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
15817 		if (offset >= params.beacon_color_change.probe_resp_len) {
15818 			err = -EINVAL;
15819 			goto out;
15820 		}
15821 
15822 		if (params.beacon_color_change.probe_resp[offset] !=
15823 		    params.count) {
15824 			err = -EINVAL;
15825 			goto out;
15826 		}
15827 
15828 		params.counter_offset_presp = offset;
15829 	}
15830 
15831 	wdev_lock(wdev);
15832 	err = rdev_color_change(rdev, dev, &params);
15833 	wdev_unlock(wdev);
15834 
15835 out:
15836 	kfree(params.beacon_next.mbssid_ies);
15837 	kfree(params.beacon_color_change.mbssid_ies);
15838 	kfree(tb);
15839 	return err;
15840 }
15841 
15842 static int nl80211_set_fils_aad(struct sk_buff *skb,
15843 				struct genl_info *info)
15844 {
15845 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15846 	struct net_device *dev = info->user_ptr[1];
15847 	struct cfg80211_fils_aad fils_aad = {};
15848 	u8 *nonces;
15849 
15850 	if (!info->attrs[NL80211_ATTR_MAC] ||
15851 	    !info->attrs[NL80211_ATTR_FILS_KEK] ||
15852 	    !info->attrs[NL80211_ATTR_FILS_NONCES])
15853 		return -EINVAL;
15854 
15855 	fils_aad.macaddr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15856 	fils_aad.kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
15857 	fils_aad.kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
15858 	nonces = nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
15859 	fils_aad.snonce = nonces;
15860 	fils_aad.anonce = nonces + FILS_NONCE_LEN;
15861 
15862 	return rdev_set_fils_aad(rdev, dev, &fils_aad);
15863 }
15864 
15865 static int nl80211_add_link(struct sk_buff *skb, struct genl_info *info)
15866 {
15867 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15868 	unsigned int link_id = nl80211_link_id(info->attrs);
15869 	struct net_device *dev = info->user_ptr[1];
15870 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15871 	int ret;
15872 
15873 	if (!(wdev->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
15874 		return -EINVAL;
15875 
15876 	switch (wdev->iftype) {
15877 	case NL80211_IFTYPE_AP:
15878 		break;
15879 	default:
15880 		return -EINVAL;
15881 	}
15882 
15883 	if (!info->attrs[NL80211_ATTR_MAC] ||
15884 	    !is_valid_ether_addr(nla_data(info->attrs[NL80211_ATTR_MAC])))
15885 		return -EINVAL;
15886 
15887 	wdev_lock(wdev);
15888 	wdev->valid_links |= BIT(link_id);
15889 	ether_addr_copy(wdev->links[link_id].addr,
15890 			nla_data(info->attrs[NL80211_ATTR_MAC]));
15891 
15892 	ret = rdev_add_intf_link(rdev, wdev, link_id);
15893 	if (ret) {
15894 		wdev->valid_links &= ~BIT(link_id);
15895 		eth_zero_addr(wdev->links[link_id].addr);
15896 	}
15897 	wdev_unlock(wdev);
15898 
15899 	return ret;
15900 }
15901 
15902 static int nl80211_remove_link(struct sk_buff *skb, struct genl_info *info)
15903 {
15904 	unsigned int link_id = nl80211_link_id(info->attrs);
15905 	struct net_device *dev = info->user_ptr[1];
15906 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15907 
15908 	/* cannot remove if there's no link */
15909 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
15910 		return -EINVAL;
15911 
15912 	switch (wdev->iftype) {
15913 	case NL80211_IFTYPE_AP:
15914 		break;
15915 	default:
15916 		return -EINVAL;
15917 	}
15918 
15919 	wdev_lock(wdev);
15920 	cfg80211_remove_link(wdev, link_id);
15921 	wdev_unlock(wdev);
15922 
15923 	return 0;
15924 }
15925 
15926 static int
15927 nl80211_add_mod_link_station(struct sk_buff *skb, struct genl_info *info,
15928 			     bool add)
15929 {
15930 	struct link_station_parameters params = {};
15931 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15932 	struct net_device *dev = info->user_ptr[1];
15933 	int err;
15934 
15935 	if ((add && !rdev->ops->add_link_station) ||
15936 	    (!add && !rdev->ops->mod_link_station))
15937 		return -EOPNOTSUPP;
15938 
15939 	if (add && !info->attrs[NL80211_ATTR_MAC])
15940 		return -EINVAL;
15941 
15942 	if (!info->attrs[NL80211_ATTR_MLD_ADDR])
15943 		return -EINVAL;
15944 
15945 	if (add && !info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
15946 		return -EINVAL;
15947 
15948 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
15949 
15950 	if (info->attrs[NL80211_ATTR_MAC]) {
15951 		params.link_mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
15952 		if (!is_valid_ether_addr(params.link_mac))
15953 			return -EINVAL;
15954 	}
15955 
15956 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
15957 		return -EINVAL;
15958 
15959 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
15960 
15961 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
15962 		params.supported_rates =
15963 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
15964 		params.supported_rates_len =
15965 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
15966 	}
15967 
15968 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
15969 		params.ht_capa =
15970 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
15971 
15972 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
15973 		params.vht_capa =
15974 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
15975 
15976 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
15977 		params.he_capa =
15978 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
15979 		params.he_capa_len =
15980 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
15981 
15982 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
15983 			params.eht_capa =
15984 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
15985 			params.eht_capa_len =
15986 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
15987 
15988 			if (!ieee80211_eht_capa_size_ok((const u8 *)params.he_capa,
15989 							(const u8 *)params.eht_capa,
15990 							params.eht_capa_len,
15991 							false))
15992 				return -EINVAL;
15993 		}
15994 	}
15995 
15996 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
15997 		params.he_6ghz_capa =
15998 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
15999 
16000 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
16001 		params.opmode_notif_used = true;
16002 		params.opmode_notif =
16003 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
16004 	}
16005 
16006 	err = nl80211_parse_sta_txpower_setting(info, &params.txpwr,
16007 						&params.txpwr_set);
16008 	if (err)
16009 		return err;
16010 
16011 	wdev_lock(dev->ieee80211_ptr);
16012 	if (add)
16013 		err = rdev_add_link_station(rdev, dev, &params);
16014 	else
16015 		err = rdev_mod_link_station(rdev, dev, &params);
16016 	wdev_unlock(dev->ieee80211_ptr);
16017 
16018 	return err;
16019 }
16020 
16021 static int
16022 nl80211_add_link_station(struct sk_buff *skb, struct genl_info *info)
16023 {
16024 	return nl80211_add_mod_link_station(skb, info, true);
16025 }
16026 
16027 static int
16028 nl80211_modify_link_station(struct sk_buff *skb, struct genl_info *info)
16029 {
16030 	return nl80211_add_mod_link_station(skb, info, false);
16031 }
16032 
16033 static int
16034 nl80211_remove_link_station(struct sk_buff *skb, struct genl_info *info)
16035 {
16036 	struct link_station_del_parameters params = {};
16037 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16038 	struct net_device *dev = info->user_ptr[1];
16039 	int ret;
16040 
16041 	if (!rdev->ops->del_link_station)
16042 		return -EOPNOTSUPP;
16043 
16044 	if (!info->attrs[NL80211_ATTR_MLD_ADDR] ||
16045 	    !info->attrs[NL80211_ATTR_MLO_LINK_ID])
16046 		return -EINVAL;
16047 
16048 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
16049 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
16050 
16051 	wdev_lock(dev->ieee80211_ptr);
16052 	ret = rdev_del_link_station(rdev, dev, &params);
16053 	wdev_unlock(dev->ieee80211_ptr);
16054 
16055 	return ret;
16056 }
16057 
16058 #define NL80211_FLAG_NEED_WIPHY		0x01
16059 #define NL80211_FLAG_NEED_NETDEV	0x02
16060 #define NL80211_FLAG_NEED_RTNL		0x04
16061 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
16062 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
16063 					 NL80211_FLAG_CHECK_NETDEV_UP)
16064 #define NL80211_FLAG_NEED_WDEV		0x10
16065 /* If a netdev is associated, it must be UP, P2P must be started */
16066 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
16067 					 NL80211_FLAG_CHECK_NETDEV_UP)
16068 #define NL80211_FLAG_CLEAR_SKB		0x20
16069 #define NL80211_FLAG_NO_WIPHY_MTX	0x40
16070 #define NL80211_FLAG_MLO_VALID_LINK_ID	0x80
16071 #define NL80211_FLAG_MLO_UNSUPPORTED	0x100
16072 
16073 #define INTERNAL_FLAG_SELECTORS(__sel)			\
16074 	SELECTOR(__sel, NONE, 0) /* must be first */	\
16075 	SELECTOR(__sel, WIPHY,				\
16076 		 NL80211_FLAG_NEED_WIPHY)		\
16077 	SELECTOR(__sel, WDEV,				\
16078 		 NL80211_FLAG_NEED_WDEV)		\
16079 	SELECTOR(__sel, NETDEV,				\
16080 		 NL80211_FLAG_NEED_NETDEV)		\
16081 	SELECTOR(__sel, NETDEV_LINK,			\
16082 		 NL80211_FLAG_NEED_NETDEV |		\
16083 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16084 	SELECTOR(__sel, NETDEV_NO_MLO,			\
16085 		 NL80211_FLAG_NEED_NETDEV |		\
16086 		 NL80211_FLAG_MLO_UNSUPPORTED)	\
16087 	SELECTOR(__sel, WIPHY_RTNL,			\
16088 		 NL80211_FLAG_NEED_WIPHY |		\
16089 		 NL80211_FLAG_NEED_RTNL)		\
16090 	SELECTOR(__sel, WIPHY_RTNL_NOMTX,		\
16091 		 NL80211_FLAG_NEED_WIPHY |		\
16092 		 NL80211_FLAG_NEED_RTNL |		\
16093 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16094 	SELECTOR(__sel, WDEV_RTNL,			\
16095 		 NL80211_FLAG_NEED_WDEV |		\
16096 		 NL80211_FLAG_NEED_RTNL)		\
16097 	SELECTOR(__sel, NETDEV_RTNL,			\
16098 		 NL80211_FLAG_NEED_NETDEV |		\
16099 		 NL80211_FLAG_NEED_RTNL)		\
16100 	SELECTOR(__sel, NETDEV_UP,			\
16101 		 NL80211_FLAG_NEED_NETDEV_UP)		\
16102 	SELECTOR(__sel, NETDEV_UP_LINK,			\
16103 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16104 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16105 	SELECTOR(__sel, NETDEV_UP_NO_MLO,		\
16106 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16107 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16108 	SELECTOR(__sel, NETDEV_UP_NO_MLO_CLEAR,		\
16109 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16110 		 NL80211_FLAG_CLEAR_SKB |		\
16111 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16112 	SELECTOR(__sel, NETDEV_UP_NOTMX,		\
16113 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16114 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16115 	SELECTOR(__sel, NETDEV_UP_NOTMX_NOMLO,		\
16116 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16117 		 NL80211_FLAG_NO_WIPHY_MTX |		\
16118 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16119 	SELECTOR(__sel, NETDEV_UP_CLEAR,		\
16120 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16121 		 NL80211_FLAG_CLEAR_SKB)		\
16122 	SELECTOR(__sel, WDEV_UP,			\
16123 		 NL80211_FLAG_NEED_WDEV_UP)		\
16124 	SELECTOR(__sel, WDEV_UP_LINK,			\
16125 		 NL80211_FLAG_NEED_WDEV_UP |		\
16126 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16127 	SELECTOR(__sel, WDEV_UP_RTNL,			\
16128 		 NL80211_FLAG_NEED_WDEV_UP |		\
16129 		 NL80211_FLAG_NEED_RTNL)		\
16130 	SELECTOR(__sel, WIPHY_CLEAR,			\
16131 		 NL80211_FLAG_NEED_WIPHY |		\
16132 		 NL80211_FLAG_CLEAR_SKB)
16133 
16134 enum nl80211_internal_flags_selector {
16135 #define SELECTOR(_, name, value)	NL80211_IFL_SEL_##name,
16136 	INTERNAL_FLAG_SELECTORS(_)
16137 #undef SELECTOR
16138 };
16139 
16140 static u32 nl80211_internal_flags[] = {
16141 #define SELECTOR(_, name, value)	[NL80211_IFL_SEL_##name] = value,
16142 	INTERNAL_FLAG_SELECTORS(_)
16143 #undef SELECTOR
16144 };
16145 
16146 static int nl80211_pre_doit(const struct genl_split_ops *ops,
16147 			    struct sk_buff *skb,
16148 			    struct genl_info *info)
16149 {
16150 	struct cfg80211_registered_device *rdev = NULL;
16151 	struct wireless_dev *wdev = NULL;
16152 	struct net_device *dev = NULL;
16153 	u32 internal_flags;
16154 	int err;
16155 
16156 	if (WARN_ON(ops->internal_flags >= ARRAY_SIZE(nl80211_internal_flags)))
16157 		return -EINVAL;
16158 
16159 	internal_flags = nl80211_internal_flags[ops->internal_flags];
16160 
16161 	rtnl_lock();
16162 	if (internal_flags & NL80211_FLAG_NEED_WIPHY) {
16163 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
16164 		if (IS_ERR(rdev)) {
16165 			err = PTR_ERR(rdev);
16166 			goto out_unlock;
16167 		}
16168 		info->user_ptr[0] = rdev;
16169 	} else if (internal_flags & NL80211_FLAG_NEED_NETDEV ||
16170 		   internal_flags & NL80211_FLAG_NEED_WDEV) {
16171 		wdev = __cfg80211_wdev_from_attrs(NULL, genl_info_net(info),
16172 						  info->attrs);
16173 		if (IS_ERR(wdev)) {
16174 			err = PTR_ERR(wdev);
16175 			goto out_unlock;
16176 		}
16177 
16178 		dev = wdev->netdev;
16179 		dev_hold(dev);
16180 		rdev = wiphy_to_rdev(wdev->wiphy);
16181 
16182 		if (internal_flags & NL80211_FLAG_NEED_NETDEV) {
16183 			if (!dev) {
16184 				err = -EINVAL;
16185 				goto out_unlock;
16186 			}
16187 
16188 			info->user_ptr[1] = dev;
16189 		} else {
16190 			info->user_ptr[1] = wdev;
16191 		}
16192 
16193 		if (internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
16194 		    !wdev_running(wdev)) {
16195 			err = -ENETDOWN;
16196 			goto out_unlock;
16197 		}
16198 
16199 		info->user_ptr[0] = rdev;
16200 	}
16201 
16202 	if (internal_flags & NL80211_FLAG_MLO_VALID_LINK_ID) {
16203 		struct nlattr *link_id = info->attrs[NL80211_ATTR_MLO_LINK_ID];
16204 
16205 		if (!wdev) {
16206 			err = -EINVAL;
16207 			goto out_unlock;
16208 		}
16209 
16210 		/* MLO -> require valid link ID */
16211 		if (wdev->valid_links &&
16212 		    (!link_id ||
16213 		     !(wdev->valid_links & BIT(nla_get_u8(link_id))))) {
16214 			err = -EINVAL;
16215 			goto out_unlock;
16216 		}
16217 
16218 		/* non-MLO -> no link ID attribute accepted */
16219 		if (!wdev->valid_links && link_id) {
16220 			err = -EINVAL;
16221 			goto out_unlock;
16222 		}
16223 	}
16224 
16225 	if (internal_flags & NL80211_FLAG_MLO_UNSUPPORTED) {
16226 		if (info->attrs[NL80211_ATTR_MLO_LINK_ID] ||
16227 		    (wdev && wdev->valid_links)) {
16228 			err = -EINVAL;
16229 			goto out_unlock;
16230 		}
16231 	}
16232 
16233 	if (rdev && !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16234 		wiphy_lock(&rdev->wiphy);
16235 		/* we keep the mutex locked until post_doit */
16236 		__release(&rdev->wiphy.mtx);
16237 	}
16238 	if (!(internal_flags & NL80211_FLAG_NEED_RTNL))
16239 		rtnl_unlock();
16240 
16241 	return 0;
16242 out_unlock:
16243 	rtnl_unlock();
16244 	dev_put(dev);
16245 	return err;
16246 }
16247 
16248 static void nl80211_post_doit(const struct genl_split_ops *ops,
16249 			      struct sk_buff *skb,
16250 			      struct genl_info *info)
16251 {
16252 	u32 internal_flags = nl80211_internal_flags[ops->internal_flags];
16253 
16254 	if (info->user_ptr[1]) {
16255 		if (internal_flags & NL80211_FLAG_NEED_WDEV) {
16256 			struct wireless_dev *wdev = info->user_ptr[1];
16257 
16258 			dev_put(wdev->netdev);
16259 		} else {
16260 			dev_put(info->user_ptr[1]);
16261 		}
16262 	}
16263 
16264 	if (info->user_ptr[0] &&
16265 	    !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16266 		struct cfg80211_registered_device *rdev = info->user_ptr[0];
16267 
16268 		/* we kept the mutex locked since pre_doit */
16269 		__acquire(&rdev->wiphy.mtx);
16270 		wiphy_unlock(&rdev->wiphy);
16271 	}
16272 
16273 	if (internal_flags & NL80211_FLAG_NEED_RTNL)
16274 		rtnl_unlock();
16275 
16276 	/* If needed, clear the netlink message payload from the SKB
16277 	 * as it might contain key data that shouldn't stick around on
16278 	 * the heap after the SKB is freed. The netlink message header
16279 	 * is still needed for further processing, so leave it intact.
16280 	 */
16281 	if (internal_flags & NL80211_FLAG_CLEAR_SKB) {
16282 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
16283 
16284 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
16285 	}
16286 }
16287 
16288 static int nl80211_set_sar_sub_specs(struct cfg80211_registered_device *rdev,
16289 				     struct cfg80211_sar_specs *sar_specs,
16290 				     struct nlattr *spec[], int index)
16291 {
16292 	u32 range_index, i;
16293 
16294 	if (!sar_specs || !spec)
16295 		return -EINVAL;
16296 
16297 	if (!spec[NL80211_SAR_ATTR_SPECS_POWER] ||
16298 	    !spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX])
16299 		return -EINVAL;
16300 
16301 	range_index = nla_get_u32(spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX]);
16302 
16303 	/* check if range_index exceeds num_freq_ranges */
16304 	if (range_index >= rdev->wiphy.sar_capa->num_freq_ranges)
16305 		return -EINVAL;
16306 
16307 	/* check if range_index duplicates */
16308 	for (i = 0; i < index; i++) {
16309 		if (sar_specs->sub_specs[i].freq_range_index == range_index)
16310 			return -EINVAL;
16311 	}
16312 
16313 	sar_specs->sub_specs[index].power =
16314 		nla_get_s32(spec[NL80211_SAR_ATTR_SPECS_POWER]);
16315 
16316 	sar_specs->sub_specs[index].freq_range_index = range_index;
16317 
16318 	return 0;
16319 }
16320 
16321 static int nl80211_set_sar_specs(struct sk_buff *skb, struct genl_info *info)
16322 {
16323 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16324 	struct nlattr *spec[NL80211_SAR_ATTR_SPECS_MAX + 1];
16325 	struct nlattr *tb[NL80211_SAR_ATTR_MAX + 1];
16326 	struct cfg80211_sar_specs *sar_spec;
16327 	enum nl80211_sar_type type;
16328 	struct nlattr *spec_list;
16329 	u32 specs;
16330 	int rem, err;
16331 
16332 	if (!rdev->wiphy.sar_capa || !rdev->ops->set_sar_specs)
16333 		return -EOPNOTSUPP;
16334 
16335 	if (!info->attrs[NL80211_ATTR_SAR_SPEC])
16336 		return -EINVAL;
16337 
16338 	nla_parse_nested(tb, NL80211_SAR_ATTR_MAX,
16339 			 info->attrs[NL80211_ATTR_SAR_SPEC],
16340 			 NULL, NULL);
16341 
16342 	if (!tb[NL80211_SAR_ATTR_TYPE] || !tb[NL80211_SAR_ATTR_SPECS])
16343 		return -EINVAL;
16344 
16345 	type = nla_get_u32(tb[NL80211_SAR_ATTR_TYPE]);
16346 	if (type != rdev->wiphy.sar_capa->type)
16347 		return -EINVAL;
16348 
16349 	specs = 0;
16350 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem)
16351 		specs++;
16352 
16353 	if (specs > rdev->wiphy.sar_capa->num_freq_ranges)
16354 		return -EINVAL;
16355 
16356 	sar_spec = kzalloc(struct_size(sar_spec, sub_specs, specs), GFP_KERNEL);
16357 	if (!sar_spec)
16358 		return -ENOMEM;
16359 
16360 	sar_spec->type = type;
16361 	specs = 0;
16362 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem) {
16363 		nla_parse_nested(spec, NL80211_SAR_ATTR_SPECS_MAX,
16364 				 spec_list, NULL, NULL);
16365 
16366 		switch (type) {
16367 		case NL80211_SAR_TYPE_POWER:
16368 			if (nl80211_set_sar_sub_specs(rdev, sar_spec,
16369 						      spec, specs)) {
16370 				err = -EINVAL;
16371 				goto error;
16372 			}
16373 			break;
16374 		default:
16375 			err = -EINVAL;
16376 			goto error;
16377 		}
16378 		specs++;
16379 	}
16380 
16381 	sar_spec->num_sub_specs = specs;
16382 
16383 	rdev->cur_cmd_info = info;
16384 	err = rdev_set_sar_specs(rdev, sar_spec);
16385 	rdev->cur_cmd_info = NULL;
16386 error:
16387 	kfree(sar_spec);
16388 	return err;
16389 }
16390 
16391 #define SELECTOR(__sel, name, value) \
16392 	((__sel) == (value)) ? NL80211_IFL_SEL_##name :
16393 int __missing_selector(void);
16394 #define IFLAGS(__val) INTERNAL_FLAG_SELECTORS(__val) __missing_selector()
16395 
16396 static const struct genl_ops nl80211_ops[] = {
16397 	{
16398 		.cmd = NL80211_CMD_GET_WIPHY,
16399 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16400 		.doit = nl80211_get_wiphy,
16401 		.dumpit = nl80211_dump_wiphy,
16402 		.done = nl80211_dump_wiphy_done,
16403 		/* can be retrieved by unprivileged users */
16404 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16405 	},
16406 };
16407 
16408 static const struct genl_small_ops nl80211_small_ops[] = {
16409 	{
16410 		.cmd = NL80211_CMD_SET_WIPHY,
16411 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16412 		.doit = nl80211_set_wiphy,
16413 		.flags = GENL_UNS_ADMIN_PERM,
16414 	},
16415 	{
16416 		.cmd = NL80211_CMD_GET_INTERFACE,
16417 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16418 		.doit = nl80211_get_interface,
16419 		.dumpit = nl80211_dump_interface,
16420 		/* can be retrieved by unprivileged users */
16421 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
16422 	},
16423 	{
16424 		.cmd = NL80211_CMD_SET_INTERFACE,
16425 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16426 		.doit = nl80211_set_interface,
16427 		.flags = GENL_UNS_ADMIN_PERM,
16428 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16429 					 NL80211_FLAG_NEED_RTNL),
16430 	},
16431 	{
16432 		.cmd = NL80211_CMD_NEW_INTERFACE,
16433 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16434 		.doit = nl80211_new_interface,
16435 		.flags = GENL_UNS_ADMIN_PERM,
16436 		.internal_flags =
16437 			IFLAGS(NL80211_FLAG_NEED_WIPHY |
16438 			       NL80211_FLAG_NEED_RTNL |
16439 			       /* we take the wiphy mutex later ourselves */
16440 			       NL80211_FLAG_NO_WIPHY_MTX),
16441 	},
16442 	{
16443 		.cmd = NL80211_CMD_DEL_INTERFACE,
16444 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16445 		.doit = nl80211_del_interface,
16446 		.flags = GENL_UNS_ADMIN_PERM,
16447 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
16448 					 NL80211_FLAG_NEED_RTNL),
16449 	},
16450 	{
16451 		.cmd = NL80211_CMD_GET_KEY,
16452 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16453 		.doit = nl80211_get_key,
16454 		.flags = GENL_UNS_ADMIN_PERM,
16455 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16456 	},
16457 	{
16458 		.cmd = NL80211_CMD_SET_KEY,
16459 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16460 		.doit = nl80211_set_key,
16461 		.flags = GENL_UNS_ADMIN_PERM,
16462 		/* cannot use NL80211_FLAG_MLO_VALID_LINK_ID, depends on key */
16463 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16464 					 NL80211_FLAG_CLEAR_SKB),
16465 	},
16466 	{
16467 		.cmd = NL80211_CMD_NEW_KEY,
16468 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16469 		.doit = nl80211_new_key,
16470 		.flags = GENL_UNS_ADMIN_PERM,
16471 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16472 					 NL80211_FLAG_CLEAR_SKB),
16473 	},
16474 	{
16475 		.cmd = NL80211_CMD_DEL_KEY,
16476 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16477 		.doit = nl80211_del_key,
16478 		.flags = GENL_UNS_ADMIN_PERM,
16479 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16480 	},
16481 	{
16482 		.cmd = NL80211_CMD_SET_BEACON,
16483 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16484 		.flags = GENL_UNS_ADMIN_PERM,
16485 		.doit = nl80211_set_beacon,
16486 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16487 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16488 	},
16489 	{
16490 		.cmd = NL80211_CMD_START_AP,
16491 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16492 		.flags = GENL_UNS_ADMIN_PERM,
16493 		.doit = nl80211_start_ap,
16494 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16495 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16496 	},
16497 	{
16498 		.cmd = NL80211_CMD_STOP_AP,
16499 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16500 		.flags = GENL_UNS_ADMIN_PERM,
16501 		.doit = nl80211_stop_ap,
16502 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16503 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16504 	},
16505 	{
16506 		.cmd = NL80211_CMD_GET_STATION,
16507 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16508 		.doit = nl80211_get_station,
16509 		.dumpit = nl80211_dump_station,
16510 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16511 	},
16512 	{
16513 		.cmd = NL80211_CMD_SET_STATION,
16514 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16515 		.doit = nl80211_set_station,
16516 		.flags = GENL_UNS_ADMIN_PERM,
16517 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16518 	},
16519 	{
16520 		.cmd = NL80211_CMD_NEW_STATION,
16521 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16522 		.doit = nl80211_new_station,
16523 		.flags = GENL_UNS_ADMIN_PERM,
16524 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16525 	},
16526 	{
16527 		.cmd = NL80211_CMD_DEL_STATION,
16528 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16529 		.doit = nl80211_del_station,
16530 		.flags = GENL_UNS_ADMIN_PERM,
16531 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16532 	},
16533 	{
16534 		.cmd = NL80211_CMD_GET_MPATH,
16535 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16536 		.doit = nl80211_get_mpath,
16537 		.dumpit = nl80211_dump_mpath,
16538 		.flags = GENL_UNS_ADMIN_PERM,
16539 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16540 	},
16541 	{
16542 		.cmd = NL80211_CMD_GET_MPP,
16543 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16544 		.doit = nl80211_get_mpp,
16545 		.dumpit = nl80211_dump_mpp,
16546 		.flags = GENL_UNS_ADMIN_PERM,
16547 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16548 	},
16549 	{
16550 		.cmd = NL80211_CMD_SET_MPATH,
16551 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16552 		.doit = nl80211_set_mpath,
16553 		.flags = GENL_UNS_ADMIN_PERM,
16554 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16555 	},
16556 	{
16557 		.cmd = NL80211_CMD_NEW_MPATH,
16558 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16559 		.doit = nl80211_new_mpath,
16560 		.flags = GENL_UNS_ADMIN_PERM,
16561 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16562 	},
16563 	{
16564 		.cmd = NL80211_CMD_DEL_MPATH,
16565 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16566 		.doit = nl80211_del_mpath,
16567 		.flags = GENL_UNS_ADMIN_PERM,
16568 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16569 	},
16570 	{
16571 		.cmd = NL80211_CMD_SET_BSS,
16572 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16573 		.doit = nl80211_set_bss,
16574 		.flags = GENL_UNS_ADMIN_PERM,
16575 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16576 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16577 	},
16578 	{
16579 		.cmd = NL80211_CMD_GET_REG,
16580 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16581 		.doit = nl80211_get_reg_do,
16582 		.dumpit = nl80211_get_reg_dump,
16583 		/* can be retrieved by unprivileged users */
16584 	},
16585 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
16586 	{
16587 		.cmd = NL80211_CMD_SET_REG,
16588 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16589 		.doit = nl80211_set_reg,
16590 		.flags = GENL_ADMIN_PERM,
16591 	},
16592 #endif
16593 	{
16594 		.cmd = NL80211_CMD_REQ_SET_REG,
16595 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16596 		.doit = nl80211_req_set_reg,
16597 		.flags = GENL_ADMIN_PERM,
16598 	},
16599 	{
16600 		.cmd = NL80211_CMD_RELOAD_REGDB,
16601 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16602 		.doit = nl80211_reload_regdb,
16603 		.flags = GENL_ADMIN_PERM,
16604 	},
16605 	{
16606 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
16607 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16608 		.doit = nl80211_get_mesh_config,
16609 		/* can be retrieved by unprivileged users */
16610 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16611 	},
16612 	{
16613 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
16614 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16615 		.doit = nl80211_update_mesh_config,
16616 		.flags = GENL_UNS_ADMIN_PERM,
16617 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16618 	},
16619 	{
16620 		.cmd = NL80211_CMD_TRIGGER_SCAN,
16621 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16622 		.doit = nl80211_trigger_scan,
16623 		.flags = GENL_UNS_ADMIN_PERM,
16624 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16625 	},
16626 	{
16627 		.cmd = NL80211_CMD_ABORT_SCAN,
16628 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16629 		.doit = nl80211_abort_scan,
16630 		.flags = GENL_UNS_ADMIN_PERM,
16631 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16632 	},
16633 	{
16634 		.cmd = NL80211_CMD_GET_SCAN,
16635 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16636 		.dumpit = nl80211_dump_scan,
16637 	},
16638 	{
16639 		.cmd = NL80211_CMD_START_SCHED_SCAN,
16640 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16641 		.doit = nl80211_start_sched_scan,
16642 		.flags = GENL_UNS_ADMIN_PERM,
16643 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16644 	},
16645 	{
16646 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
16647 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16648 		.doit = nl80211_stop_sched_scan,
16649 		.flags = GENL_UNS_ADMIN_PERM,
16650 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16651 	},
16652 	{
16653 		.cmd = NL80211_CMD_AUTHENTICATE,
16654 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16655 		.doit = nl80211_authenticate,
16656 		.flags = GENL_UNS_ADMIN_PERM,
16657 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16658 					 NL80211_FLAG_CLEAR_SKB),
16659 	},
16660 	{
16661 		.cmd = NL80211_CMD_ASSOCIATE,
16662 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16663 		.doit = nl80211_associate,
16664 		.flags = GENL_UNS_ADMIN_PERM,
16665 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16666 					 NL80211_FLAG_CLEAR_SKB),
16667 	},
16668 	{
16669 		.cmd = NL80211_CMD_DEAUTHENTICATE,
16670 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16671 		.doit = nl80211_deauthenticate,
16672 		.flags = GENL_UNS_ADMIN_PERM,
16673 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16674 	},
16675 	{
16676 		.cmd = NL80211_CMD_DISASSOCIATE,
16677 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16678 		.doit = nl80211_disassociate,
16679 		.flags = GENL_UNS_ADMIN_PERM,
16680 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16681 	},
16682 	{
16683 		.cmd = NL80211_CMD_JOIN_IBSS,
16684 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16685 		.doit = nl80211_join_ibss,
16686 		.flags = GENL_UNS_ADMIN_PERM,
16687 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16688 	},
16689 	{
16690 		.cmd = NL80211_CMD_LEAVE_IBSS,
16691 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16692 		.doit = nl80211_leave_ibss,
16693 		.flags = GENL_UNS_ADMIN_PERM,
16694 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16695 	},
16696 #ifdef CONFIG_NL80211_TESTMODE
16697 	{
16698 		.cmd = NL80211_CMD_TESTMODE,
16699 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16700 		.doit = nl80211_testmode_do,
16701 		.dumpit = nl80211_testmode_dump,
16702 		.flags = GENL_UNS_ADMIN_PERM,
16703 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16704 	},
16705 #endif
16706 	{
16707 		.cmd = NL80211_CMD_CONNECT,
16708 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16709 		.doit = nl80211_connect,
16710 		.flags = GENL_UNS_ADMIN_PERM,
16711 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16712 					 NL80211_FLAG_CLEAR_SKB),
16713 	},
16714 	{
16715 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
16716 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16717 		.doit = nl80211_update_connect_params,
16718 		.flags = GENL_ADMIN_PERM,
16719 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16720 					 NL80211_FLAG_CLEAR_SKB),
16721 	},
16722 	{
16723 		.cmd = NL80211_CMD_DISCONNECT,
16724 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16725 		.doit = nl80211_disconnect,
16726 		.flags = GENL_UNS_ADMIN_PERM,
16727 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16728 	},
16729 	{
16730 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
16731 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16732 		.doit = nl80211_wiphy_netns,
16733 		.flags = GENL_UNS_ADMIN_PERM,
16734 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
16735 					 NL80211_FLAG_NEED_RTNL |
16736 					 NL80211_FLAG_NO_WIPHY_MTX),
16737 	},
16738 	{
16739 		.cmd = NL80211_CMD_GET_SURVEY,
16740 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16741 		.dumpit = nl80211_dump_survey,
16742 	},
16743 	{
16744 		.cmd = NL80211_CMD_SET_PMKSA,
16745 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16746 		.doit = nl80211_setdel_pmksa,
16747 		.flags = GENL_UNS_ADMIN_PERM,
16748 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16749 					 NL80211_FLAG_CLEAR_SKB),
16750 	},
16751 	{
16752 		.cmd = NL80211_CMD_DEL_PMKSA,
16753 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16754 		.doit = nl80211_setdel_pmksa,
16755 		.flags = GENL_UNS_ADMIN_PERM,
16756 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16757 	},
16758 	{
16759 		.cmd = NL80211_CMD_FLUSH_PMKSA,
16760 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16761 		.doit = nl80211_flush_pmksa,
16762 		.flags = GENL_UNS_ADMIN_PERM,
16763 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16764 	},
16765 	{
16766 		.cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
16767 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16768 		.doit = nl80211_remain_on_channel,
16769 		.flags = GENL_UNS_ADMIN_PERM,
16770 		/* FIXME: requiring a link ID here is probably not good */
16771 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
16772 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16773 	},
16774 	{
16775 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
16776 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16777 		.doit = nl80211_cancel_remain_on_channel,
16778 		.flags = GENL_UNS_ADMIN_PERM,
16779 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16780 	},
16781 	{
16782 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
16783 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16784 		.doit = nl80211_set_tx_bitrate_mask,
16785 		.flags = GENL_UNS_ADMIN_PERM,
16786 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16787 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16788 	},
16789 	{
16790 		.cmd = NL80211_CMD_REGISTER_FRAME,
16791 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16792 		.doit = nl80211_register_mgmt,
16793 		.flags = GENL_UNS_ADMIN_PERM,
16794 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
16795 	},
16796 	{
16797 		.cmd = NL80211_CMD_FRAME,
16798 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16799 		.doit = nl80211_tx_mgmt,
16800 		.flags = GENL_UNS_ADMIN_PERM,
16801 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16802 	},
16803 	{
16804 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
16805 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16806 		.doit = nl80211_tx_mgmt_cancel_wait,
16807 		.flags = GENL_UNS_ADMIN_PERM,
16808 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16809 	},
16810 	{
16811 		.cmd = NL80211_CMD_SET_POWER_SAVE,
16812 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16813 		.doit = nl80211_set_power_save,
16814 		.flags = GENL_UNS_ADMIN_PERM,
16815 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16816 	},
16817 	{
16818 		.cmd = NL80211_CMD_GET_POWER_SAVE,
16819 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16820 		.doit = nl80211_get_power_save,
16821 		/* can be retrieved by unprivileged users */
16822 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16823 	},
16824 	{
16825 		.cmd = NL80211_CMD_SET_CQM,
16826 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16827 		.doit = nl80211_set_cqm,
16828 		.flags = GENL_UNS_ADMIN_PERM,
16829 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16830 	},
16831 	{
16832 		.cmd = NL80211_CMD_SET_CHANNEL,
16833 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16834 		.doit = nl80211_set_channel,
16835 		.flags = GENL_UNS_ADMIN_PERM,
16836 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16837 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16838 	},
16839 	{
16840 		.cmd = NL80211_CMD_JOIN_MESH,
16841 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16842 		.doit = nl80211_join_mesh,
16843 		.flags = GENL_UNS_ADMIN_PERM,
16844 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16845 	},
16846 	{
16847 		.cmd = NL80211_CMD_LEAVE_MESH,
16848 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16849 		.doit = nl80211_leave_mesh,
16850 		.flags = GENL_UNS_ADMIN_PERM,
16851 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16852 	},
16853 	{
16854 		.cmd = NL80211_CMD_JOIN_OCB,
16855 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16856 		.doit = nl80211_join_ocb,
16857 		.flags = GENL_UNS_ADMIN_PERM,
16858 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16859 	},
16860 	{
16861 		.cmd = NL80211_CMD_LEAVE_OCB,
16862 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16863 		.doit = nl80211_leave_ocb,
16864 		.flags = GENL_UNS_ADMIN_PERM,
16865 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16866 	},
16867 #ifdef CONFIG_PM
16868 	{
16869 		.cmd = NL80211_CMD_GET_WOWLAN,
16870 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16871 		.doit = nl80211_get_wowlan,
16872 		/* can be retrieved by unprivileged users */
16873 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16874 	},
16875 	{
16876 		.cmd = NL80211_CMD_SET_WOWLAN,
16877 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16878 		.doit = nl80211_set_wowlan,
16879 		.flags = GENL_UNS_ADMIN_PERM,
16880 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16881 	},
16882 #endif
16883 	{
16884 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
16885 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16886 		.doit = nl80211_set_rekey_data,
16887 		.flags = GENL_UNS_ADMIN_PERM,
16888 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16889 					 NL80211_FLAG_CLEAR_SKB),
16890 	},
16891 	{
16892 		.cmd = NL80211_CMD_TDLS_MGMT,
16893 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16894 		.doit = nl80211_tdls_mgmt,
16895 		.flags = GENL_UNS_ADMIN_PERM,
16896 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16897 	},
16898 	{
16899 		.cmd = NL80211_CMD_TDLS_OPER,
16900 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16901 		.doit = nl80211_tdls_oper,
16902 		.flags = GENL_UNS_ADMIN_PERM,
16903 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16904 	},
16905 	{
16906 		.cmd = NL80211_CMD_UNEXPECTED_FRAME,
16907 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16908 		.doit = nl80211_register_unexpected_frame,
16909 		.flags = GENL_UNS_ADMIN_PERM,
16910 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16911 	},
16912 	{
16913 		.cmd = NL80211_CMD_PROBE_CLIENT,
16914 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16915 		.doit = nl80211_probe_client,
16916 		.flags = GENL_UNS_ADMIN_PERM,
16917 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16918 	},
16919 	{
16920 		.cmd = NL80211_CMD_REGISTER_BEACONS,
16921 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16922 		.doit = nl80211_register_beacons,
16923 		.flags = GENL_UNS_ADMIN_PERM,
16924 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16925 	},
16926 	{
16927 		.cmd = NL80211_CMD_SET_NOACK_MAP,
16928 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16929 		.doit = nl80211_set_noack_map,
16930 		.flags = GENL_UNS_ADMIN_PERM,
16931 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16932 	},
16933 	{
16934 		.cmd = NL80211_CMD_START_P2P_DEVICE,
16935 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16936 		.doit = nl80211_start_p2p_device,
16937 		.flags = GENL_UNS_ADMIN_PERM,
16938 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
16939 					 NL80211_FLAG_NEED_RTNL),
16940 	},
16941 	{
16942 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
16943 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16944 		.doit = nl80211_stop_p2p_device,
16945 		.flags = GENL_UNS_ADMIN_PERM,
16946 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
16947 					 NL80211_FLAG_NEED_RTNL),
16948 	},
16949 	{
16950 		.cmd = NL80211_CMD_START_NAN,
16951 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16952 		.doit = nl80211_start_nan,
16953 		.flags = GENL_ADMIN_PERM,
16954 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
16955 					 NL80211_FLAG_NEED_RTNL),
16956 	},
16957 	{
16958 		.cmd = NL80211_CMD_STOP_NAN,
16959 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16960 		.doit = nl80211_stop_nan,
16961 		.flags = GENL_ADMIN_PERM,
16962 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
16963 					 NL80211_FLAG_NEED_RTNL),
16964 	},
16965 	{
16966 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
16967 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16968 		.doit = nl80211_nan_add_func,
16969 		.flags = GENL_ADMIN_PERM,
16970 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16971 	},
16972 	{
16973 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
16974 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16975 		.doit = nl80211_nan_del_func,
16976 		.flags = GENL_ADMIN_PERM,
16977 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16978 	},
16979 	{
16980 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
16981 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16982 		.doit = nl80211_nan_change_config,
16983 		.flags = GENL_ADMIN_PERM,
16984 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16985 	},
16986 	{
16987 		.cmd = NL80211_CMD_SET_MCAST_RATE,
16988 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16989 		.doit = nl80211_set_mcast_rate,
16990 		.flags = GENL_UNS_ADMIN_PERM,
16991 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16992 	},
16993 	{
16994 		.cmd = NL80211_CMD_SET_MAC_ACL,
16995 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16996 		.doit = nl80211_set_mac_acl,
16997 		.flags = GENL_UNS_ADMIN_PERM,
16998 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16999 					 NL80211_FLAG_MLO_UNSUPPORTED),
17000 	},
17001 	{
17002 		.cmd = NL80211_CMD_RADAR_DETECT,
17003 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17004 		.doit = nl80211_start_radar_detection,
17005 		.flags = GENL_UNS_ADMIN_PERM,
17006 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17007 					 NL80211_FLAG_NO_WIPHY_MTX |
17008 					 NL80211_FLAG_MLO_UNSUPPORTED),
17009 	},
17010 	{
17011 		.cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
17012 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17013 		.doit = nl80211_get_protocol_features,
17014 	},
17015 	{
17016 		.cmd = NL80211_CMD_UPDATE_FT_IES,
17017 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17018 		.doit = nl80211_update_ft_ies,
17019 		.flags = GENL_UNS_ADMIN_PERM,
17020 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17021 	},
17022 	{
17023 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
17024 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17025 		.doit = nl80211_crit_protocol_start,
17026 		.flags = GENL_UNS_ADMIN_PERM,
17027 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17028 	},
17029 	{
17030 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
17031 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17032 		.doit = nl80211_crit_protocol_stop,
17033 		.flags = GENL_UNS_ADMIN_PERM,
17034 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17035 	},
17036 	{
17037 		.cmd = NL80211_CMD_GET_COALESCE,
17038 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17039 		.doit = nl80211_get_coalesce,
17040 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17041 	},
17042 	{
17043 		.cmd = NL80211_CMD_SET_COALESCE,
17044 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17045 		.doit = nl80211_set_coalesce,
17046 		.flags = GENL_UNS_ADMIN_PERM,
17047 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17048 	},
17049 	{
17050 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
17051 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17052 		.doit = nl80211_channel_switch,
17053 		.flags = GENL_UNS_ADMIN_PERM,
17054 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17055 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17056 	},
17057 	{
17058 		.cmd = NL80211_CMD_VENDOR,
17059 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17060 		.doit = nl80211_vendor_cmd,
17061 		.dumpit = nl80211_vendor_cmd_dump,
17062 		.flags = GENL_UNS_ADMIN_PERM,
17063 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17064 					 NL80211_FLAG_CLEAR_SKB),
17065 	},
17066 	{
17067 		.cmd = NL80211_CMD_SET_QOS_MAP,
17068 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17069 		.doit = nl80211_set_qos_map,
17070 		.flags = GENL_UNS_ADMIN_PERM,
17071 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17072 	},
17073 	{
17074 		.cmd = NL80211_CMD_ADD_TX_TS,
17075 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17076 		.doit = nl80211_add_tx_ts,
17077 		.flags = GENL_UNS_ADMIN_PERM,
17078 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17079 					 NL80211_FLAG_MLO_UNSUPPORTED),
17080 	},
17081 	{
17082 		.cmd = NL80211_CMD_DEL_TX_TS,
17083 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17084 		.doit = nl80211_del_tx_ts,
17085 		.flags = GENL_UNS_ADMIN_PERM,
17086 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17087 	},
17088 	{
17089 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
17090 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17091 		.doit = nl80211_tdls_channel_switch,
17092 		.flags = GENL_UNS_ADMIN_PERM,
17093 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17094 	},
17095 	{
17096 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
17097 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17098 		.doit = nl80211_tdls_cancel_channel_switch,
17099 		.flags = GENL_UNS_ADMIN_PERM,
17100 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17101 	},
17102 	{
17103 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
17104 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17105 		.doit = nl80211_set_multicast_to_unicast,
17106 		.flags = GENL_UNS_ADMIN_PERM,
17107 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17108 	},
17109 	{
17110 		.cmd = NL80211_CMD_SET_PMK,
17111 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17112 		.doit = nl80211_set_pmk,
17113 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17114 					 NL80211_FLAG_CLEAR_SKB),
17115 	},
17116 	{
17117 		.cmd = NL80211_CMD_DEL_PMK,
17118 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17119 		.doit = nl80211_del_pmk,
17120 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17121 	},
17122 	{
17123 		.cmd = NL80211_CMD_EXTERNAL_AUTH,
17124 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17125 		.doit = nl80211_external_auth,
17126 		.flags = GENL_ADMIN_PERM,
17127 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17128 	},
17129 	{
17130 		.cmd = NL80211_CMD_CONTROL_PORT_FRAME,
17131 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17132 		.doit = nl80211_tx_control_port,
17133 		.flags = GENL_UNS_ADMIN_PERM,
17134 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17135 	},
17136 	{
17137 		.cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
17138 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17139 		.doit = nl80211_get_ftm_responder_stats,
17140 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17141 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17142 	},
17143 	{
17144 		.cmd = NL80211_CMD_PEER_MEASUREMENT_START,
17145 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17146 		.doit = nl80211_pmsr_start,
17147 		.flags = GENL_UNS_ADMIN_PERM,
17148 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17149 	},
17150 	{
17151 		.cmd = NL80211_CMD_NOTIFY_RADAR,
17152 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17153 		.doit = nl80211_notify_radar_detection,
17154 		.flags = GENL_UNS_ADMIN_PERM,
17155 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17156 	},
17157 	{
17158 		.cmd = NL80211_CMD_UPDATE_OWE_INFO,
17159 		.doit = nl80211_update_owe_info,
17160 		.flags = GENL_ADMIN_PERM,
17161 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17162 	},
17163 	{
17164 		.cmd = NL80211_CMD_PROBE_MESH_LINK,
17165 		.doit = nl80211_probe_mesh_link,
17166 		.flags = GENL_UNS_ADMIN_PERM,
17167 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17168 	},
17169 	{
17170 		.cmd = NL80211_CMD_SET_TID_CONFIG,
17171 		.doit = nl80211_set_tid_config,
17172 		.flags = GENL_UNS_ADMIN_PERM,
17173 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17174 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17175 	},
17176 	{
17177 		.cmd = NL80211_CMD_SET_SAR_SPECS,
17178 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17179 		.doit = nl80211_set_sar_specs,
17180 		.flags = GENL_UNS_ADMIN_PERM,
17181 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17182 					 NL80211_FLAG_NEED_RTNL),
17183 	},
17184 	{
17185 		.cmd = NL80211_CMD_COLOR_CHANGE_REQUEST,
17186 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17187 		.doit = nl80211_color_change,
17188 		.flags = GENL_UNS_ADMIN_PERM,
17189 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17190 	},
17191 	{
17192 		.cmd = NL80211_CMD_SET_FILS_AAD,
17193 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17194 		.doit = nl80211_set_fils_aad,
17195 		.flags = GENL_UNS_ADMIN_PERM,
17196 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17197 	},
17198 	{
17199 		.cmd = NL80211_CMD_ADD_LINK,
17200 		.doit = nl80211_add_link,
17201 		.flags = GENL_UNS_ADMIN_PERM,
17202 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17203 	},
17204 	{
17205 		.cmd = NL80211_CMD_REMOVE_LINK,
17206 		.doit = nl80211_remove_link,
17207 		.flags = GENL_UNS_ADMIN_PERM,
17208 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17209 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17210 	},
17211 	{
17212 		.cmd = NL80211_CMD_ADD_LINK_STA,
17213 		.doit = nl80211_add_link_station,
17214 		.flags = GENL_UNS_ADMIN_PERM,
17215 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17216 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17217 	},
17218 	{
17219 		.cmd = NL80211_CMD_MODIFY_LINK_STA,
17220 		.doit = nl80211_modify_link_station,
17221 		.flags = GENL_UNS_ADMIN_PERM,
17222 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17223 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17224 	},
17225 	{
17226 		.cmd = NL80211_CMD_REMOVE_LINK_STA,
17227 		.doit = nl80211_remove_link_station,
17228 		.flags = GENL_UNS_ADMIN_PERM,
17229 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17230 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17231 	},
17232 };
17233 
17234 static struct genl_family nl80211_fam __ro_after_init = {
17235 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
17236 	.hdrsize = 0,			/* no private header */
17237 	.version = 1,			/* no particular meaning now */
17238 	.maxattr = NL80211_ATTR_MAX,
17239 	.policy = nl80211_policy,
17240 	.netnsok = true,
17241 	.pre_doit = nl80211_pre_doit,
17242 	.post_doit = nl80211_post_doit,
17243 	.module = THIS_MODULE,
17244 	.ops = nl80211_ops,
17245 	.n_ops = ARRAY_SIZE(nl80211_ops),
17246 	.small_ops = nl80211_small_ops,
17247 	.n_small_ops = ARRAY_SIZE(nl80211_small_ops),
17248 	.resv_start_op = NL80211_CMD_REMOVE_LINK_STA + 1,
17249 	.mcgrps = nl80211_mcgrps,
17250 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
17251 	.parallel_ops = true,
17252 };
17253 
17254 /* notification functions */
17255 
17256 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
17257 			  enum nl80211_commands cmd)
17258 {
17259 	struct sk_buff *msg;
17260 	struct nl80211_dump_wiphy_state state = {};
17261 
17262 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
17263 		cmd != NL80211_CMD_DEL_WIPHY);
17264 
17265 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17266 	if (!msg)
17267 		return;
17268 
17269 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
17270 		nlmsg_free(msg);
17271 		return;
17272 	}
17273 
17274 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17275 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17276 }
17277 
17278 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
17279 				struct wireless_dev *wdev,
17280 				enum nl80211_commands cmd)
17281 {
17282 	struct sk_buff *msg;
17283 
17284 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17285 	if (!msg)
17286 		return;
17287 
17288 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
17289 		nlmsg_free(msg);
17290 		return;
17291 	}
17292 
17293 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17294 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17295 }
17296 
17297 static int nl80211_add_scan_req(struct sk_buff *msg,
17298 				struct cfg80211_registered_device *rdev)
17299 {
17300 	struct cfg80211_scan_request *req = rdev->scan_req;
17301 	struct nlattr *nest;
17302 	int i;
17303 	struct cfg80211_scan_info *info;
17304 
17305 	if (WARN_ON(!req))
17306 		return 0;
17307 
17308 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
17309 	if (!nest)
17310 		goto nla_put_failure;
17311 	for (i = 0; i < req->n_ssids; i++) {
17312 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
17313 			goto nla_put_failure;
17314 	}
17315 	nla_nest_end(msg, nest);
17316 
17317 	if (req->flags & NL80211_SCAN_FLAG_FREQ_KHZ) {
17318 		nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQ_KHZ);
17319 		if (!nest)
17320 			goto nla_put_failure;
17321 		for (i = 0; i < req->n_channels; i++) {
17322 			if (nla_put_u32(msg, i,
17323 				   ieee80211_channel_to_khz(req->channels[i])))
17324 				goto nla_put_failure;
17325 		}
17326 		nla_nest_end(msg, nest);
17327 	} else {
17328 		nest = nla_nest_start_noflag(msg,
17329 					     NL80211_ATTR_SCAN_FREQUENCIES);
17330 		if (!nest)
17331 			goto nla_put_failure;
17332 		for (i = 0; i < req->n_channels; i++) {
17333 			if (nla_put_u32(msg, i, req->channels[i]->center_freq))
17334 				goto nla_put_failure;
17335 		}
17336 		nla_nest_end(msg, nest);
17337 	}
17338 
17339 	if (req->ie &&
17340 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
17341 		goto nla_put_failure;
17342 
17343 	if (req->flags &&
17344 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
17345 		goto nla_put_failure;
17346 
17347 	info = rdev->int_scan_req ? &rdev->int_scan_req->info :
17348 		&rdev->scan_req->info;
17349 	if (info->scan_start_tsf &&
17350 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
17351 			       info->scan_start_tsf, NL80211_BSS_PAD) ||
17352 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
17353 		     info->tsf_bssid)))
17354 		goto nla_put_failure;
17355 
17356 	return 0;
17357  nla_put_failure:
17358 	return -ENOBUFS;
17359 }
17360 
17361 static int nl80211_prep_scan_msg(struct sk_buff *msg,
17362 				 struct cfg80211_registered_device *rdev,
17363 				 struct wireless_dev *wdev,
17364 				 u32 portid, u32 seq, int flags,
17365 				 u32 cmd)
17366 {
17367 	void *hdr;
17368 
17369 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
17370 	if (!hdr)
17371 		return -1;
17372 
17373 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17374 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
17375 					 wdev->netdev->ifindex)) ||
17376 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17377 			      NL80211_ATTR_PAD))
17378 		goto nla_put_failure;
17379 
17380 	/* ignore errors and send incomplete event anyway */
17381 	nl80211_add_scan_req(msg, rdev);
17382 
17383 	genlmsg_end(msg, hdr);
17384 	return 0;
17385 
17386  nla_put_failure:
17387 	genlmsg_cancel(msg, hdr);
17388 	return -EMSGSIZE;
17389 }
17390 
17391 static int
17392 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
17393 			    struct cfg80211_sched_scan_request *req, u32 cmd)
17394 {
17395 	void *hdr;
17396 
17397 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17398 	if (!hdr)
17399 		return -1;
17400 
17401 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
17402 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
17403 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
17404 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
17405 			      NL80211_ATTR_PAD))
17406 		goto nla_put_failure;
17407 
17408 	genlmsg_end(msg, hdr);
17409 	return 0;
17410 
17411  nla_put_failure:
17412 	genlmsg_cancel(msg, hdr);
17413 	return -EMSGSIZE;
17414 }
17415 
17416 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
17417 			     struct wireless_dev *wdev)
17418 {
17419 	struct sk_buff *msg;
17420 
17421 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17422 	if (!msg)
17423 		return;
17424 
17425 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17426 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
17427 		nlmsg_free(msg);
17428 		return;
17429 	}
17430 
17431 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17432 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17433 }
17434 
17435 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
17436 				       struct wireless_dev *wdev, bool aborted)
17437 {
17438 	struct sk_buff *msg;
17439 
17440 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17441 	if (!msg)
17442 		return NULL;
17443 
17444 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17445 				  aborted ? NL80211_CMD_SCAN_ABORTED :
17446 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
17447 		nlmsg_free(msg);
17448 		return NULL;
17449 	}
17450 
17451 	return msg;
17452 }
17453 
17454 /* send message created by nl80211_build_scan_msg() */
17455 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
17456 			   struct sk_buff *msg)
17457 {
17458 	if (!msg)
17459 		return;
17460 
17461 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17462 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17463 }
17464 
17465 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
17466 {
17467 	struct sk_buff *msg;
17468 
17469 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17470 	if (!msg)
17471 		return;
17472 
17473 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
17474 		nlmsg_free(msg);
17475 		return;
17476 	}
17477 
17478 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
17479 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17480 }
17481 
17482 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
17483 					  struct regulatory_request *request)
17484 {
17485 	/* Userspace can always count this one always being set */
17486 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
17487 		goto nla_put_failure;
17488 
17489 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
17490 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17491 			       NL80211_REGDOM_TYPE_WORLD))
17492 			goto nla_put_failure;
17493 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
17494 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17495 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
17496 			goto nla_put_failure;
17497 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
17498 		   request->intersect) {
17499 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17500 			       NL80211_REGDOM_TYPE_INTERSECTION))
17501 			goto nla_put_failure;
17502 	} else {
17503 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17504 			       NL80211_REGDOM_TYPE_COUNTRY) ||
17505 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
17506 				   request->alpha2))
17507 			goto nla_put_failure;
17508 	}
17509 
17510 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
17511 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
17512 
17513 		if (wiphy &&
17514 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
17515 			goto nla_put_failure;
17516 
17517 		if (wiphy &&
17518 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
17519 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
17520 			goto nla_put_failure;
17521 	}
17522 
17523 	return true;
17524 
17525 nla_put_failure:
17526 	return false;
17527 }
17528 
17529 /*
17530  * This can happen on global regulatory changes or device specific settings
17531  * based on custom regulatory domains.
17532  */
17533 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
17534 				     struct regulatory_request *request)
17535 {
17536 	struct sk_buff *msg;
17537 	void *hdr;
17538 
17539 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17540 	if (!msg)
17541 		return;
17542 
17543 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
17544 	if (!hdr)
17545 		goto nla_put_failure;
17546 
17547 	if (!nl80211_reg_change_event_fill(msg, request))
17548 		goto nla_put_failure;
17549 
17550 	genlmsg_end(msg, hdr);
17551 
17552 	rcu_read_lock();
17553 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
17554 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
17555 	rcu_read_unlock();
17556 
17557 	return;
17558 
17559 nla_put_failure:
17560 	nlmsg_free(msg);
17561 }
17562 
17563 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
17564 				    struct net_device *netdev,
17565 				    const u8 *buf, size_t len,
17566 				    enum nl80211_commands cmd, gfp_t gfp,
17567 				    int uapsd_queues, const u8 *req_ies,
17568 				    size_t req_ies_len, bool reconnect)
17569 {
17570 	struct sk_buff *msg;
17571 	void *hdr;
17572 
17573 	msg = nlmsg_new(100 + len + req_ies_len, gfp);
17574 	if (!msg)
17575 		return;
17576 
17577 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17578 	if (!hdr) {
17579 		nlmsg_free(msg);
17580 		return;
17581 	}
17582 
17583 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17584 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17585 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
17586 	    (req_ies &&
17587 	     nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
17588 		goto nla_put_failure;
17589 
17590 	if (reconnect && nla_put_flag(msg, NL80211_ATTR_RECONNECT_REQUESTED))
17591 		goto nla_put_failure;
17592 
17593 	if (uapsd_queues >= 0) {
17594 		struct nlattr *nla_wmm =
17595 			nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
17596 		if (!nla_wmm)
17597 			goto nla_put_failure;
17598 
17599 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
17600 			       uapsd_queues))
17601 			goto nla_put_failure;
17602 
17603 		nla_nest_end(msg, nla_wmm);
17604 	}
17605 
17606 	genlmsg_end(msg, hdr);
17607 
17608 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17609 				NL80211_MCGRP_MLME, gfp);
17610 	return;
17611 
17612  nla_put_failure:
17613 	nlmsg_free(msg);
17614 }
17615 
17616 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
17617 			  struct net_device *netdev, const u8 *buf,
17618 			  size_t len, gfp_t gfp)
17619 {
17620 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17621 				NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0,
17622 				false);
17623 }
17624 
17625 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
17626 			   struct net_device *netdev,
17627 			   struct cfg80211_rx_assoc_resp *data)
17628 {
17629 	nl80211_send_mlme_event(rdev, netdev, data->buf, data->len,
17630 				NL80211_CMD_ASSOCIATE, GFP_KERNEL,
17631 				data->uapsd_queues,
17632 				data->req_ies, data->req_ies_len, false);
17633 }
17634 
17635 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
17636 			 struct net_device *netdev, const u8 *buf,
17637 			 size_t len, bool reconnect, gfp_t gfp)
17638 {
17639 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17640 				NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0,
17641 				reconnect);
17642 }
17643 
17644 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
17645 			   struct net_device *netdev, const u8 *buf,
17646 			   size_t len, bool reconnect, gfp_t gfp)
17647 {
17648 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17649 				NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0,
17650 				reconnect);
17651 }
17652 
17653 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
17654 				  size_t len)
17655 {
17656 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17657 	struct wiphy *wiphy = wdev->wiphy;
17658 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17659 	const struct ieee80211_mgmt *mgmt = (void *)buf;
17660 	u32 cmd;
17661 
17662 	if (WARN_ON(len < 2))
17663 		return;
17664 
17665 	if (ieee80211_is_deauth(mgmt->frame_control)) {
17666 		cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
17667 	} else if (ieee80211_is_disassoc(mgmt->frame_control)) {
17668 		cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
17669 	} else if (ieee80211_is_beacon(mgmt->frame_control)) {
17670 		if (wdev->unprot_beacon_reported &&
17671 		    elapsed_jiffies_msecs(wdev->unprot_beacon_reported) < 10000)
17672 			return;
17673 		cmd = NL80211_CMD_UNPROT_BEACON;
17674 		wdev->unprot_beacon_reported = jiffies;
17675 	} else {
17676 		return;
17677 	}
17678 
17679 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
17680 	nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
17681 				NULL, 0, false);
17682 }
17683 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
17684 
17685 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
17686 				      struct net_device *netdev, int cmd,
17687 				      const u8 *addr, gfp_t gfp)
17688 {
17689 	struct sk_buff *msg;
17690 	void *hdr;
17691 
17692 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17693 	if (!msg)
17694 		return;
17695 
17696 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17697 	if (!hdr) {
17698 		nlmsg_free(msg);
17699 		return;
17700 	}
17701 
17702 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17703 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17704 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
17705 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
17706 		goto nla_put_failure;
17707 
17708 	genlmsg_end(msg, hdr);
17709 
17710 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17711 				NL80211_MCGRP_MLME, gfp);
17712 	return;
17713 
17714  nla_put_failure:
17715 	nlmsg_free(msg);
17716 }
17717 
17718 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
17719 			       struct net_device *netdev, const u8 *addr,
17720 			       gfp_t gfp)
17721 {
17722 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
17723 				  addr, gfp);
17724 }
17725 
17726 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
17727 				struct net_device *netdev, const u8 *addr,
17728 				gfp_t gfp)
17729 {
17730 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
17731 				  addr, gfp);
17732 }
17733 
17734 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
17735 				 struct net_device *netdev,
17736 				 struct cfg80211_connect_resp_params *cr,
17737 				 gfp_t gfp)
17738 {
17739 	struct sk_buff *msg;
17740 	void *hdr;
17741 	unsigned int link;
17742 	size_t link_info_size = 0;
17743 	const u8 *connected_addr = cr->valid_links ?
17744 				   cr->ap_mld_addr : cr->links[0].bssid;
17745 
17746 	if (cr->valid_links) {
17747 		for_each_valid_link(cr, link) {
17748 			/* Nested attribute header */
17749 			link_info_size += NLA_HDRLEN;
17750 			/* Link ID */
17751 			link_info_size += nla_total_size(sizeof(u8));
17752 			link_info_size += cr->links[link].addr ?
17753 					  nla_total_size(ETH_ALEN) : 0;
17754 			link_info_size += (cr->links[link].bssid ||
17755 					   cr->links[link].bss) ?
17756 					  nla_total_size(ETH_ALEN) : 0;
17757 			link_info_size += nla_total_size(sizeof(u16));
17758 		}
17759 	}
17760 
17761 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
17762 			cr->fils.kek_len + cr->fils.pmk_len +
17763 			(cr->fils.pmkid ? WLAN_PMKID_LEN : 0) + link_info_size,
17764 			gfp);
17765 	if (!msg)
17766 		return;
17767 
17768 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
17769 	if (!hdr) {
17770 		nlmsg_free(msg);
17771 		return;
17772 	}
17773 
17774 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17775 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17776 	    (connected_addr &&
17777 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr)) ||
17778 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
17779 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
17780 			cr->status) ||
17781 	    (cr->status < 0 &&
17782 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
17783 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
17784 			  cr->timeout_reason))) ||
17785 	    (cr->req_ie &&
17786 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
17787 	    (cr->resp_ie &&
17788 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
17789 		     cr->resp_ie)) ||
17790 	    (cr->fils.update_erp_next_seq_num &&
17791 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
17792 			 cr->fils.erp_next_seq_num)) ||
17793 	    (cr->status == WLAN_STATUS_SUCCESS &&
17794 	     ((cr->fils.kek &&
17795 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
17796 		       cr->fils.kek)) ||
17797 	      (cr->fils.pmk &&
17798 	       nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
17799 	      (cr->fils.pmkid &&
17800 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
17801 		goto nla_put_failure;
17802 
17803 	if (cr->valid_links) {
17804 		int i = 1;
17805 		struct nlattr *nested;
17806 
17807 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
17808 		if (!nested)
17809 			goto nla_put_failure;
17810 
17811 		for_each_valid_link(cr, link) {
17812 			struct nlattr *nested_mlo_links;
17813 			const u8 *bssid = cr->links[link].bss ?
17814 					  cr->links[link].bss->bssid :
17815 					  cr->links[link].bssid;
17816 
17817 			nested_mlo_links = nla_nest_start(msg, i);
17818 			if (!nested_mlo_links)
17819 				goto nla_put_failure;
17820 
17821 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
17822 			    (bssid &&
17823 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
17824 			    (cr->links[link].addr &&
17825 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
17826 				     cr->links[link].addr)) ||
17827 			    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
17828 					cr->links[link].status))
17829 				goto nla_put_failure;
17830 
17831 			nla_nest_end(msg, nested_mlo_links);
17832 			i++;
17833 		}
17834 		nla_nest_end(msg, nested);
17835 	}
17836 
17837 	genlmsg_end(msg, hdr);
17838 
17839 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17840 				NL80211_MCGRP_MLME, gfp);
17841 	return;
17842 
17843  nla_put_failure:
17844 	nlmsg_free(msg);
17845 }
17846 
17847 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
17848 			 struct net_device *netdev,
17849 			 struct cfg80211_roam_info *info, gfp_t gfp)
17850 {
17851 	struct sk_buff *msg;
17852 	void *hdr;
17853 	size_t link_info_size = 0;
17854 	unsigned int link;
17855 	const u8 *connected_addr = info->ap_mld_addr ?
17856 				   info->ap_mld_addr :
17857 				   (info->links[0].bss ?
17858 				    info->links[0].bss->bssid :
17859 				    info->links[0].bssid);
17860 
17861 	if (info->valid_links) {
17862 		for_each_valid_link(info, link) {
17863 			/* Nested attribute header */
17864 			link_info_size += NLA_HDRLEN;
17865 			/* Link ID */
17866 			link_info_size += nla_total_size(sizeof(u8));
17867 			link_info_size += info->links[link].addr ?
17868 					  nla_total_size(ETH_ALEN) : 0;
17869 			link_info_size += (info->links[link].bssid ||
17870 					   info->links[link].bss) ?
17871 					  nla_total_size(ETH_ALEN) : 0;
17872 		}
17873 	}
17874 
17875 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
17876 			info->fils.kek_len + info->fils.pmk_len +
17877 			(info->fils.pmkid ? WLAN_PMKID_LEN : 0) +
17878 			link_info_size, gfp);
17879 	if (!msg)
17880 		return;
17881 
17882 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
17883 	if (!hdr) {
17884 		nlmsg_free(msg);
17885 		return;
17886 	}
17887 
17888 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17889 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17890 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr) ||
17891 	    (info->req_ie &&
17892 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
17893 		     info->req_ie)) ||
17894 	    (info->resp_ie &&
17895 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
17896 		     info->resp_ie)) ||
17897 	    (info->fils.update_erp_next_seq_num &&
17898 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
17899 			 info->fils.erp_next_seq_num)) ||
17900 	    (info->fils.kek &&
17901 	     nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
17902 		     info->fils.kek)) ||
17903 	    (info->fils.pmk &&
17904 	     nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
17905 	    (info->fils.pmkid &&
17906 	     nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
17907 		goto nla_put_failure;
17908 
17909 	if (info->valid_links) {
17910 		int i = 1;
17911 		struct nlattr *nested;
17912 
17913 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
17914 		if (!nested)
17915 			goto nla_put_failure;
17916 
17917 		for_each_valid_link(info, link) {
17918 			struct nlattr *nested_mlo_links;
17919 			const u8 *bssid = info->links[link].bss ?
17920 					  info->links[link].bss->bssid :
17921 					  info->links[link].bssid;
17922 
17923 			nested_mlo_links = nla_nest_start(msg, i);
17924 			if (!nested_mlo_links)
17925 				goto nla_put_failure;
17926 
17927 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
17928 			    (bssid &&
17929 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
17930 			    (info->links[link].addr &&
17931 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
17932 				     info->links[link].addr)))
17933 				goto nla_put_failure;
17934 
17935 			nla_nest_end(msg, nested_mlo_links);
17936 			i++;
17937 		}
17938 		nla_nest_end(msg, nested);
17939 	}
17940 
17941 	genlmsg_end(msg, hdr);
17942 
17943 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17944 				NL80211_MCGRP_MLME, gfp);
17945 	return;
17946 
17947  nla_put_failure:
17948 	nlmsg_free(msg);
17949 }
17950 
17951 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
17952 				  struct net_device *netdev, const u8 *bssid,
17953 				  const u8 *td_bitmap, u8 td_bitmap_len)
17954 {
17955 	struct sk_buff *msg;
17956 	void *hdr;
17957 
17958 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17959 	if (!msg)
17960 		return;
17961 
17962 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
17963 	if (!hdr) {
17964 		nlmsg_free(msg);
17965 		return;
17966 	}
17967 
17968 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17969 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17970 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
17971 		goto nla_put_failure;
17972 
17973 	if ((td_bitmap_len > 0) && td_bitmap)
17974 		if (nla_put(msg, NL80211_ATTR_TD_BITMAP,
17975 			    td_bitmap_len, td_bitmap))
17976 			goto nla_put_failure;
17977 
17978 	genlmsg_end(msg, hdr);
17979 
17980 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17981 				NL80211_MCGRP_MLME, GFP_KERNEL);
17982 	return;
17983 
17984  nla_put_failure:
17985 	nlmsg_free(msg);
17986 }
17987 
17988 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
17989 			       struct net_device *netdev, u16 reason,
17990 			       const u8 *ie, size_t ie_len, bool from_ap)
17991 {
17992 	struct sk_buff *msg;
17993 	void *hdr;
17994 
17995 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
17996 	if (!msg)
17997 		return;
17998 
17999 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
18000 	if (!hdr) {
18001 		nlmsg_free(msg);
18002 		return;
18003 	}
18004 
18005 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18006 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18007 	    (reason &&
18008 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
18009 	    (from_ap &&
18010 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
18011 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
18012 		goto nla_put_failure;
18013 
18014 	genlmsg_end(msg, hdr);
18015 
18016 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18017 				NL80211_MCGRP_MLME, GFP_KERNEL);
18018 	return;
18019 
18020  nla_put_failure:
18021 	nlmsg_free(msg);
18022 }
18023 
18024 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
18025 			     struct net_device *netdev, const u8 *bssid,
18026 			     gfp_t gfp)
18027 {
18028 	struct sk_buff *msg;
18029 	void *hdr;
18030 
18031 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18032 	if (!msg)
18033 		return;
18034 
18035 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
18036 	if (!hdr) {
18037 		nlmsg_free(msg);
18038 		return;
18039 	}
18040 
18041 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18042 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18043 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18044 		goto nla_put_failure;
18045 
18046 	genlmsg_end(msg, hdr);
18047 
18048 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18049 				NL80211_MCGRP_MLME, gfp);
18050 	return;
18051 
18052  nla_put_failure:
18053 	nlmsg_free(msg);
18054 }
18055 
18056 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
18057 					const u8 *ie, u8 ie_len,
18058 					int sig_dbm, gfp_t gfp)
18059 {
18060 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18061 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18062 	struct sk_buff *msg;
18063 	void *hdr;
18064 
18065 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
18066 		return;
18067 
18068 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
18069 
18070 	msg = nlmsg_new(100 + ie_len, gfp);
18071 	if (!msg)
18072 		return;
18073 
18074 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
18075 	if (!hdr) {
18076 		nlmsg_free(msg);
18077 		return;
18078 	}
18079 
18080 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18081 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18082 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
18083 	    (ie_len && ie &&
18084 	     nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
18085 	    (sig_dbm &&
18086 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
18087 		goto nla_put_failure;
18088 
18089 	genlmsg_end(msg, hdr);
18090 
18091 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18092 				NL80211_MCGRP_MLME, gfp);
18093 	return;
18094 
18095  nla_put_failure:
18096 	nlmsg_free(msg);
18097 }
18098 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
18099 
18100 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
18101 				 struct net_device *netdev, const u8 *addr,
18102 				 enum nl80211_key_type key_type, int key_id,
18103 				 const u8 *tsc, gfp_t gfp)
18104 {
18105 	struct sk_buff *msg;
18106 	void *hdr;
18107 
18108 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18109 	if (!msg)
18110 		return;
18111 
18112 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
18113 	if (!hdr) {
18114 		nlmsg_free(msg);
18115 		return;
18116 	}
18117 
18118 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18119 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18120 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
18121 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
18122 	    (key_id != -1 &&
18123 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
18124 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
18125 		goto nla_put_failure;
18126 
18127 	genlmsg_end(msg, hdr);
18128 
18129 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18130 				NL80211_MCGRP_MLME, gfp);
18131 	return;
18132 
18133  nla_put_failure:
18134 	nlmsg_free(msg);
18135 }
18136 
18137 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
18138 				    struct ieee80211_channel *channel_before,
18139 				    struct ieee80211_channel *channel_after)
18140 {
18141 	struct sk_buff *msg;
18142 	void *hdr;
18143 	struct nlattr *nl_freq;
18144 
18145 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
18146 	if (!msg)
18147 		return;
18148 
18149 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
18150 	if (!hdr) {
18151 		nlmsg_free(msg);
18152 		return;
18153 	}
18154 
18155 	/*
18156 	 * Since we are applying the beacon hint to a wiphy we know its
18157 	 * wiphy_idx is valid
18158 	 */
18159 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
18160 		goto nla_put_failure;
18161 
18162 	/* Before */
18163 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
18164 	if (!nl_freq)
18165 		goto nla_put_failure;
18166 
18167 	if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
18168 		goto nla_put_failure;
18169 	nla_nest_end(msg, nl_freq);
18170 
18171 	/* After */
18172 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
18173 	if (!nl_freq)
18174 		goto nla_put_failure;
18175 
18176 	if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
18177 		goto nla_put_failure;
18178 	nla_nest_end(msg, nl_freq);
18179 
18180 	genlmsg_end(msg, hdr);
18181 
18182 	rcu_read_lock();
18183 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
18184 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
18185 	rcu_read_unlock();
18186 
18187 	return;
18188 
18189 nla_put_failure:
18190 	nlmsg_free(msg);
18191 }
18192 
18193 static void nl80211_send_remain_on_chan_event(
18194 	int cmd, struct cfg80211_registered_device *rdev,
18195 	struct wireless_dev *wdev, u64 cookie,
18196 	struct ieee80211_channel *chan,
18197 	unsigned int duration, gfp_t gfp)
18198 {
18199 	struct sk_buff *msg;
18200 	void *hdr;
18201 
18202 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18203 	if (!msg)
18204 		return;
18205 
18206 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18207 	if (!hdr) {
18208 		nlmsg_free(msg);
18209 		return;
18210 	}
18211 
18212 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18213 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18214 					 wdev->netdev->ifindex)) ||
18215 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18216 			      NL80211_ATTR_PAD) ||
18217 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
18218 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
18219 			NL80211_CHAN_NO_HT) ||
18220 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
18221 			      NL80211_ATTR_PAD))
18222 		goto nla_put_failure;
18223 
18224 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
18225 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
18226 		goto nla_put_failure;
18227 
18228 	genlmsg_end(msg, hdr);
18229 
18230 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18231 				NL80211_MCGRP_MLME, gfp);
18232 	return;
18233 
18234  nla_put_failure:
18235 	nlmsg_free(msg);
18236 }
18237 
18238 void cfg80211_assoc_comeback(struct net_device *netdev,
18239 			     const u8 *ap_addr, u32 timeout)
18240 {
18241 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
18242 	struct wiphy *wiphy = wdev->wiphy;
18243 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18244 	struct sk_buff *msg;
18245 	void *hdr;
18246 
18247 	trace_cfg80211_assoc_comeback(wdev, ap_addr, timeout);
18248 
18249 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18250 	if (!msg)
18251 		return;
18252 
18253 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ASSOC_COMEBACK);
18254 	if (!hdr) {
18255 		nlmsg_free(msg);
18256 		return;
18257 	}
18258 
18259 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18260 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18261 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ap_addr) ||
18262 	    nla_put_u32(msg, NL80211_ATTR_TIMEOUT, timeout))
18263 		goto nla_put_failure;
18264 
18265 	genlmsg_end(msg, hdr);
18266 
18267 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18268 				NL80211_MCGRP_MLME, GFP_KERNEL);
18269 	return;
18270 
18271  nla_put_failure:
18272 	nlmsg_free(msg);
18273 }
18274 EXPORT_SYMBOL(cfg80211_assoc_comeback);
18275 
18276 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
18277 			       struct ieee80211_channel *chan,
18278 			       unsigned int duration, gfp_t gfp)
18279 {
18280 	struct wiphy *wiphy = wdev->wiphy;
18281 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18282 
18283 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
18284 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
18285 					  rdev, wdev, cookie, chan,
18286 					  duration, gfp);
18287 }
18288 EXPORT_SYMBOL(cfg80211_ready_on_channel);
18289 
18290 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
18291 					struct ieee80211_channel *chan,
18292 					gfp_t gfp)
18293 {
18294 	struct wiphy *wiphy = wdev->wiphy;
18295 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18296 
18297 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
18298 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
18299 					  rdev, wdev, cookie, chan, 0, gfp);
18300 }
18301 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
18302 
18303 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
18304 					struct ieee80211_channel *chan,
18305 					gfp_t gfp)
18306 {
18307 	struct wiphy *wiphy = wdev->wiphy;
18308 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18309 
18310 	trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
18311 	nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
18312 					  rdev, wdev, cookie, chan, 0, gfp);
18313 }
18314 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
18315 
18316 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
18317 		      struct station_info *sinfo, gfp_t gfp)
18318 {
18319 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18320 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18321 	struct sk_buff *msg;
18322 
18323 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
18324 
18325 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18326 	if (!msg)
18327 		return;
18328 
18329 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
18330 				 rdev, dev, mac_addr, sinfo) < 0) {
18331 		nlmsg_free(msg);
18332 		return;
18333 	}
18334 
18335 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18336 				NL80211_MCGRP_MLME, gfp);
18337 }
18338 EXPORT_SYMBOL(cfg80211_new_sta);
18339 
18340 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
18341 			    struct station_info *sinfo, gfp_t gfp)
18342 {
18343 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18344 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18345 	struct sk_buff *msg;
18346 	struct station_info empty_sinfo = {};
18347 
18348 	if (!sinfo)
18349 		sinfo = &empty_sinfo;
18350 
18351 	trace_cfg80211_del_sta(dev, mac_addr);
18352 
18353 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18354 	if (!msg) {
18355 		cfg80211_sinfo_release_content(sinfo);
18356 		return;
18357 	}
18358 
18359 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
18360 				 rdev, dev, mac_addr, sinfo) < 0) {
18361 		nlmsg_free(msg);
18362 		return;
18363 	}
18364 
18365 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18366 				NL80211_MCGRP_MLME, gfp);
18367 }
18368 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
18369 
18370 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
18371 			  enum nl80211_connect_failed_reason reason,
18372 			  gfp_t gfp)
18373 {
18374 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18375 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18376 	struct sk_buff *msg;
18377 	void *hdr;
18378 
18379 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
18380 	if (!msg)
18381 		return;
18382 
18383 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
18384 	if (!hdr) {
18385 		nlmsg_free(msg);
18386 		return;
18387 	}
18388 
18389 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18390 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
18391 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
18392 		goto nla_put_failure;
18393 
18394 	genlmsg_end(msg, hdr);
18395 
18396 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18397 				NL80211_MCGRP_MLME, gfp);
18398 	return;
18399 
18400  nla_put_failure:
18401 	nlmsg_free(msg);
18402 }
18403 EXPORT_SYMBOL(cfg80211_conn_failed);
18404 
18405 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
18406 				       const u8 *addr, gfp_t gfp)
18407 {
18408 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18409 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18410 	struct sk_buff *msg;
18411 	void *hdr;
18412 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
18413 
18414 	if (!nlportid)
18415 		return false;
18416 
18417 	msg = nlmsg_new(100, gfp);
18418 	if (!msg)
18419 		return true;
18420 
18421 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18422 	if (!hdr) {
18423 		nlmsg_free(msg);
18424 		return true;
18425 	}
18426 
18427 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18428 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18429 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
18430 		goto nla_put_failure;
18431 
18432 	genlmsg_end(msg, hdr);
18433 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18434 	return true;
18435 
18436  nla_put_failure:
18437 	nlmsg_free(msg);
18438 	return true;
18439 }
18440 
18441 bool cfg80211_rx_spurious_frame(struct net_device *dev,
18442 				const u8 *addr, gfp_t gfp)
18443 {
18444 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18445 	bool ret;
18446 
18447 	trace_cfg80211_rx_spurious_frame(dev, addr);
18448 
18449 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18450 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
18451 		trace_cfg80211_return_bool(false);
18452 		return false;
18453 	}
18454 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
18455 					 addr, gfp);
18456 	trace_cfg80211_return_bool(ret);
18457 	return ret;
18458 }
18459 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
18460 
18461 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
18462 					const u8 *addr, gfp_t gfp)
18463 {
18464 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18465 	bool ret;
18466 
18467 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
18468 
18469 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18470 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
18471 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
18472 		trace_cfg80211_return_bool(false);
18473 		return false;
18474 	}
18475 	ret = __nl80211_unexpected_frame(dev,
18476 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
18477 					 addr, gfp);
18478 	trace_cfg80211_return_bool(ret);
18479 	return ret;
18480 }
18481 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
18482 
18483 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
18484 		      struct wireless_dev *wdev, u32 nlportid,
18485 		      struct cfg80211_rx_info *info, gfp_t gfp)
18486 {
18487 	struct net_device *netdev = wdev->netdev;
18488 	struct sk_buff *msg;
18489 	void *hdr;
18490 
18491 	msg = nlmsg_new(100 + info->len, gfp);
18492 	if (!msg)
18493 		return -ENOMEM;
18494 
18495 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
18496 	if (!hdr) {
18497 		nlmsg_free(msg);
18498 		return -ENOMEM;
18499 	}
18500 
18501 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18502 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18503 					netdev->ifindex)) ||
18504 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18505 			      NL80211_ATTR_PAD) ||
18506 	    (info->have_link_id &&
18507 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, info->link_id)) ||
18508 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, KHZ_TO_MHZ(info->freq)) ||
18509 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET, info->freq % 1000) ||
18510 	    (info->sig_dbm &&
18511 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, info->sig_dbm)) ||
18512 	    nla_put(msg, NL80211_ATTR_FRAME, info->len, info->buf) ||
18513 	    (info->flags &&
18514 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, info->flags)) ||
18515 	    (info->rx_tstamp && nla_put_u64_64bit(msg,
18516 						  NL80211_ATTR_RX_HW_TIMESTAMP,
18517 						  info->rx_tstamp,
18518 						  NL80211_ATTR_PAD)) ||
18519 	    (info->ack_tstamp && nla_put_u64_64bit(msg,
18520 						   NL80211_ATTR_TX_HW_TIMESTAMP,
18521 						   info->ack_tstamp,
18522 						   NL80211_ATTR_PAD)))
18523 		goto nla_put_failure;
18524 
18525 	genlmsg_end(msg, hdr);
18526 
18527 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18528 
18529  nla_put_failure:
18530 	nlmsg_free(msg);
18531 	return -ENOBUFS;
18532 }
18533 
18534 static void nl80211_frame_tx_status(struct wireless_dev *wdev,
18535 				    struct cfg80211_tx_status *status,
18536 				    gfp_t gfp, enum nl80211_commands command)
18537 {
18538 	struct wiphy *wiphy = wdev->wiphy;
18539 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18540 	struct net_device *netdev = wdev->netdev;
18541 	struct sk_buff *msg;
18542 	void *hdr;
18543 
18544 	if (command == NL80211_CMD_FRAME_TX_STATUS)
18545 		trace_cfg80211_mgmt_tx_status(wdev, status->cookie,
18546 					      status->ack);
18547 	else
18548 		trace_cfg80211_control_port_tx_status(wdev, status->cookie,
18549 						      status->ack);
18550 
18551 	msg = nlmsg_new(100 + status->len, gfp);
18552 	if (!msg)
18553 		return;
18554 
18555 	hdr = nl80211hdr_put(msg, 0, 0, 0, command);
18556 	if (!hdr) {
18557 		nlmsg_free(msg);
18558 		return;
18559 	}
18560 
18561 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18562 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18563 				   netdev->ifindex)) ||
18564 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18565 			      NL80211_ATTR_PAD) ||
18566 	    nla_put(msg, NL80211_ATTR_FRAME, status->len, status->buf) ||
18567 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, status->cookie,
18568 			      NL80211_ATTR_PAD) ||
18569 	    (status->ack && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
18570 	    (status->tx_tstamp &&
18571 	     nla_put_u64_64bit(msg, NL80211_ATTR_TX_HW_TIMESTAMP,
18572 			       status->tx_tstamp, NL80211_ATTR_PAD)) ||
18573 	    (status->ack_tstamp &&
18574 	     nla_put_u64_64bit(msg, NL80211_ATTR_RX_HW_TIMESTAMP,
18575 			       status->ack_tstamp, NL80211_ATTR_PAD)))
18576 		goto nla_put_failure;
18577 
18578 	genlmsg_end(msg, hdr);
18579 
18580 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18581 				NL80211_MCGRP_MLME, gfp);
18582 	return;
18583 
18584 nla_put_failure:
18585 	nlmsg_free(msg);
18586 }
18587 
18588 void cfg80211_control_port_tx_status(struct wireless_dev *wdev, u64 cookie,
18589 				     const u8 *buf, size_t len, bool ack,
18590 				     gfp_t gfp)
18591 {
18592 	struct cfg80211_tx_status status = {
18593 		.cookie = cookie,
18594 		.buf = buf,
18595 		.len = len,
18596 		.ack = ack
18597 	};
18598 
18599 	nl80211_frame_tx_status(wdev, &status, gfp,
18600 				NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS);
18601 }
18602 EXPORT_SYMBOL(cfg80211_control_port_tx_status);
18603 
18604 void cfg80211_mgmt_tx_status_ext(struct wireless_dev *wdev,
18605 				 struct cfg80211_tx_status *status, gfp_t gfp)
18606 {
18607 	nl80211_frame_tx_status(wdev, status, gfp, NL80211_CMD_FRAME_TX_STATUS);
18608 }
18609 EXPORT_SYMBOL(cfg80211_mgmt_tx_status_ext);
18610 
18611 static int __nl80211_rx_control_port(struct net_device *dev,
18612 				     struct sk_buff *skb,
18613 				     bool unencrypted, gfp_t gfp)
18614 {
18615 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18616 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18617 	struct ethhdr *ehdr = eth_hdr(skb);
18618 	const u8 *addr = ehdr->h_source;
18619 	u16 proto = be16_to_cpu(skb->protocol);
18620 	struct sk_buff *msg;
18621 	void *hdr;
18622 	struct nlattr *frame;
18623 
18624 	u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
18625 
18626 	if (!nlportid)
18627 		return -ENOENT;
18628 
18629 	msg = nlmsg_new(100 + skb->len, gfp);
18630 	if (!msg)
18631 		return -ENOMEM;
18632 
18633 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
18634 	if (!hdr) {
18635 		nlmsg_free(msg);
18636 		return -ENOBUFS;
18637 	}
18638 
18639 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18640 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18641 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18642 			      NL80211_ATTR_PAD) ||
18643 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
18644 	    nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
18645 	    (unencrypted && nla_put_flag(msg,
18646 					 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
18647 		goto nla_put_failure;
18648 
18649 	frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
18650 	if (!frame)
18651 		goto nla_put_failure;
18652 
18653 	skb_copy_bits(skb, 0, nla_data(frame), skb->len);
18654 	genlmsg_end(msg, hdr);
18655 
18656 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18657 
18658  nla_put_failure:
18659 	nlmsg_free(msg);
18660 	return -ENOBUFS;
18661 }
18662 
18663 bool cfg80211_rx_control_port(struct net_device *dev,
18664 			      struct sk_buff *skb, bool unencrypted)
18665 {
18666 	int ret;
18667 
18668 	trace_cfg80211_rx_control_port(dev, skb, unencrypted);
18669 	ret = __nl80211_rx_control_port(dev, skb, unencrypted, GFP_ATOMIC);
18670 	trace_cfg80211_return_bool(ret == 0);
18671 	return ret == 0;
18672 }
18673 EXPORT_SYMBOL(cfg80211_rx_control_port);
18674 
18675 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
18676 					    const char *mac, gfp_t gfp)
18677 {
18678 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18679 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18680 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18681 	void **cb;
18682 
18683 	if (!msg)
18684 		return NULL;
18685 
18686 	cb = (void **)msg->cb;
18687 
18688 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
18689 	if (!cb[0]) {
18690 		nlmsg_free(msg);
18691 		return NULL;
18692 	}
18693 
18694 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18695 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
18696 		goto nla_put_failure;
18697 
18698 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
18699 		goto nla_put_failure;
18700 
18701 	cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
18702 	if (!cb[1])
18703 		goto nla_put_failure;
18704 
18705 	cb[2] = rdev;
18706 
18707 	return msg;
18708  nla_put_failure:
18709 	nlmsg_free(msg);
18710 	return NULL;
18711 }
18712 
18713 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
18714 {
18715 	void **cb = (void **)msg->cb;
18716 	struct cfg80211_registered_device *rdev = cb[2];
18717 
18718 	nla_nest_end(msg, cb[1]);
18719 	genlmsg_end(msg, cb[0]);
18720 
18721 	memset(msg->cb, 0, sizeof(msg->cb));
18722 
18723 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18724 				NL80211_MCGRP_MLME, gfp);
18725 }
18726 
18727 void cfg80211_cqm_rssi_notify(struct net_device *dev,
18728 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
18729 			      s32 rssi_level, gfp_t gfp)
18730 {
18731 	struct sk_buff *msg;
18732 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18733 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18734 
18735 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
18736 
18737 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
18738 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
18739 		return;
18740 
18741 	if (wdev->cqm_config) {
18742 		wdev->cqm_config->last_rssi_event_value = rssi_level;
18743 
18744 		cfg80211_cqm_rssi_update(rdev, dev);
18745 
18746 		if (rssi_level == 0)
18747 			rssi_level = wdev->cqm_config->last_rssi_event_value;
18748 	}
18749 
18750 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
18751 	if (!msg)
18752 		return;
18753 
18754 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
18755 			rssi_event))
18756 		goto nla_put_failure;
18757 
18758 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
18759 				      rssi_level))
18760 		goto nla_put_failure;
18761 
18762 	cfg80211_send_cqm(msg, gfp);
18763 
18764 	return;
18765 
18766  nla_put_failure:
18767 	nlmsg_free(msg);
18768 }
18769 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
18770 
18771 void cfg80211_cqm_txe_notify(struct net_device *dev,
18772 			     const u8 *peer, u32 num_packets,
18773 			     u32 rate, u32 intvl, gfp_t gfp)
18774 {
18775 	struct sk_buff *msg;
18776 
18777 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
18778 	if (!msg)
18779 		return;
18780 
18781 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
18782 		goto nla_put_failure;
18783 
18784 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
18785 		goto nla_put_failure;
18786 
18787 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
18788 		goto nla_put_failure;
18789 
18790 	cfg80211_send_cqm(msg, gfp);
18791 	return;
18792 
18793  nla_put_failure:
18794 	nlmsg_free(msg);
18795 }
18796 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
18797 
18798 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
18799 				 const u8 *peer, u32 num_packets, gfp_t gfp)
18800 {
18801 	struct sk_buff *msg;
18802 
18803 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
18804 
18805 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
18806 	if (!msg)
18807 		return;
18808 
18809 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
18810 		goto nla_put_failure;
18811 
18812 	cfg80211_send_cqm(msg, gfp);
18813 	return;
18814 
18815  nla_put_failure:
18816 	nlmsg_free(msg);
18817 }
18818 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
18819 
18820 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
18821 {
18822 	struct sk_buff *msg;
18823 
18824 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
18825 	if (!msg)
18826 		return;
18827 
18828 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
18829 		goto nla_put_failure;
18830 
18831 	cfg80211_send_cqm(msg, gfp);
18832 	return;
18833 
18834  nla_put_failure:
18835 	nlmsg_free(msg);
18836 }
18837 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
18838 
18839 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
18840 				     struct net_device *netdev, const u8 *bssid,
18841 				     const u8 *replay_ctr, gfp_t gfp)
18842 {
18843 	struct sk_buff *msg;
18844 	struct nlattr *rekey_attr;
18845 	void *hdr;
18846 
18847 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18848 	if (!msg)
18849 		return;
18850 
18851 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
18852 	if (!hdr) {
18853 		nlmsg_free(msg);
18854 		return;
18855 	}
18856 
18857 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18858 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18859 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18860 		goto nla_put_failure;
18861 
18862 	rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
18863 	if (!rekey_attr)
18864 		goto nla_put_failure;
18865 
18866 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
18867 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
18868 		goto nla_put_failure;
18869 
18870 	nla_nest_end(msg, rekey_attr);
18871 
18872 	genlmsg_end(msg, hdr);
18873 
18874 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18875 				NL80211_MCGRP_MLME, gfp);
18876 	return;
18877 
18878  nla_put_failure:
18879 	nlmsg_free(msg);
18880 }
18881 
18882 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
18883 			       const u8 *replay_ctr, gfp_t gfp)
18884 {
18885 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18886 	struct wiphy *wiphy = wdev->wiphy;
18887 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18888 
18889 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
18890 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
18891 }
18892 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
18893 
18894 static void
18895 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
18896 			       struct net_device *netdev, int index,
18897 			       const u8 *bssid, bool preauth, gfp_t gfp)
18898 {
18899 	struct sk_buff *msg;
18900 	struct nlattr *attr;
18901 	void *hdr;
18902 
18903 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18904 	if (!msg)
18905 		return;
18906 
18907 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
18908 	if (!hdr) {
18909 		nlmsg_free(msg);
18910 		return;
18911 	}
18912 
18913 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18914 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
18915 		goto nla_put_failure;
18916 
18917 	attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
18918 	if (!attr)
18919 		goto nla_put_failure;
18920 
18921 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
18922 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
18923 	    (preauth &&
18924 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
18925 		goto nla_put_failure;
18926 
18927 	nla_nest_end(msg, attr);
18928 
18929 	genlmsg_end(msg, hdr);
18930 
18931 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18932 				NL80211_MCGRP_MLME, gfp);
18933 	return;
18934 
18935  nla_put_failure:
18936 	nlmsg_free(msg);
18937 }
18938 
18939 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
18940 				     const u8 *bssid, bool preauth, gfp_t gfp)
18941 {
18942 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18943 	struct wiphy *wiphy = wdev->wiphy;
18944 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18945 
18946 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
18947 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
18948 }
18949 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
18950 
18951 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
18952 				     struct net_device *netdev,
18953 				     unsigned int link_id,
18954 				     struct cfg80211_chan_def *chandef,
18955 				     gfp_t gfp,
18956 				     enum nl80211_commands notif,
18957 				     u8 count, bool quiet)
18958 {
18959 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
18960 	struct sk_buff *msg;
18961 	void *hdr;
18962 
18963 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18964 	if (!msg)
18965 		return;
18966 
18967 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
18968 	if (!hdr) {
18969 		nlmsg_free(msg);
18970 		return;
18971 	}
18972 
18973 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
18974 		goto nla_put_failure;
18975 
18976 	if (wdev->valid_links &&
18977 	    nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
18978 		goto nla_put_failure;
18979 
18980 	if (nl80211_send_chandef(msg, chandef))
18981 		goto nla_put_failure;
18982 
18983 	if (notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) {
18984 		if (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count))
18985 			goto nla_put_failure;
18986 		if (quiet &&
18987 		    nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX))
18988 			goto nla_put_failure;
18989 	}
18990 
18991 	genlmsg_end(msg, hdr);
18992 
18993 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18994 				NL80211_MCGRP_MLME, gfp);
18995 	return;
18996 
18997  nla_put_failure:
18998 	nlmsg_free(msg);
18999 }
19000 
19001 void cfg80211_ch_switch_notify(struct net_device *dev,
19002 			       struct cfg80211_chan_def *chandef,
19003 			       unsigned int link_id)
19004 {
19005 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19006 	struct wiphy *wiphy = wdev->wiphy;
19007 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19008 
19009 	ASSERT_WDEV_LOCK(wdev);
19010 	WARN_INVALID_LINK_ID(wdev, link_id);
19011 
19012 	trace_cfg80211_ch_switch_notify(dev, chandef, link_id);
19013 
19014 	switch (wdev->iftype) {
19015 	case NL80211_IFTYPE_STATION:
19016 	case NL80211_IFTYPE_P2P_CLIENT:
19017 		if (!WARN_ON(!wdev->links[link_id].client.current_bss))
19018 			cfg80211_update_assoc_bss_entry(wdev, link_id,
19019 							chandef->chan);
19020 		break;
19021 	case NL80211_IFTYPE_MESH_POINT:
19022 		wdev->u.mesh.chandef = *chandef;
19023 		wdev->u.mesh.preset_chandef = *chandef;
19024 		break;
19025 	case NL80211_IFTYPE_AP:
19026 	case NL80211_IFTYPE_P2P_GO:
19027 		wdev->links[link_id].ap.chandef = *chandef;
19028 		break;
19029 	case NL80211_IFTYPE_ADHOC:
19030 		wdev->u.ibss.chandef = *chandef;
19031 		break;
19032 	default:
19033 		WARN_ON(1);
19034 		break;
19035 	}
19036 
19037 	cfg80211_sched_dfs_chan_update(rdev);
19038 
19039 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19040 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0, false);
19041 }
19042 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
19043 
19044 void cfg80211_ch_switch_started_notify(struct net_device *dev,
19045 				       struct cfg80211_chan_def *chandef,
19046 				       unsigned int link_id, u8 count,
19047 				       bool quiet)
19048 {
19049 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19050 	struct wiphy *wiphy = wdev->wiphy;
19051 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19052 
19053 	ASSERT_WDEV_LOCK(wdev);
19054 	WARN_INVALID_LINK_ID(wdev, link_id);
19055 
19056 	trace_cfg80211_ch_switch_started_notify(dev, chandef, link_id);
19057 
19058 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19059 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY,
19060 				 count, quiet);
19061 }
19062 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
19063 
19064 int cfg80211_bss_color_notify(struct net_device *dev, gfp_t gfp,
19065 			      enum nl80211_commands cmd, u8 count,
19066 			      u64 color_bitmap)
19067 {
19068 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19069 	struct wiphy *wiphy = wdev->wiphy;
19070 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19071 	struct sk_buff *msg;
19072 	void *hdr;
19073 
19074 	ASSERT_WDEV_LOCK(wdev);
19075 
19076 	trace_cfg80211_bss_color_notify(dev, cmd, count, color_bitmap);
19077 
19078 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19079 	if (!msg)
19080 		return -ENOMEM;
19081 
19082 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
19083 	if (!hdr)
19084 		goto nla_put_failure;
19085 
19086 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19087 		goto nla_put_failure;
19088 
19089 	if (cmd == NL80211_CMD_COLOR_CHANGE_STARTED &&
19090 	    nla_put_u32(msg, NL80211_ATTR_COLOR_CHANGE_COUNT, count))
19091 		goto nla_put_failure;
19092 
19093 	if (cmd == NL80211_CMD_OBSS_COLOR_COLLISION &&
19094 	    nla_put_u64_64bit(msg, NL80211_ATTR_OBSS_COLOR_BITMAP,
19095 			      color_bitmap, NL80211_ATTR_PAD))
19096 		goto nla_put_failure;
19097 
19098 	genlmsg_end(msg, hdr);
19099 
19100 	return genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
19101 				       msg, 0, NL80211_MCGRP_MLME, gfp);
19102 
19103 nla_put_failure:
19104 	nlmsg_free(msg);
19105 	return -EINVAL;
19106 }
19107 EXPORT_SYMBOL(cfg80211_bss_color_notify);
19108 
19109 void
19110 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
19111 		     const struct cfg80211_chan_def *chandef,
19112 		     enum nl80211_radar_event event,
19113 		     struct net_device *netdev, gfp_t gfp)
19114 {
19115 	struct sk_buff *msg;
19116 	void *hdr;
19117 
19118 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19119 	if (!msg)
19120 		return;
19121 
19122 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
19123 	if (!hdr) {
19124 		nlmsg_free(msg);
19125 		return;
19126 	}
19127 
19128 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19129 		goto nla_put_failure;
19130 
19131 	/* NOP and radar events don't need a netdev parameter */
19132 	if (netdev) {
19133 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
19134 
19135 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19136 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19137 				      NL80211_ATTR_PAD))
19138 			goto nla_put_failure;
19139 	}
19140 
19141 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
19142 		goto nla_put_failure;
19143 
19144 	if (nl80211_send_chandef(msg, chandef))
19145 		goto nla_put_failure;
19146 
19147 	genlmsg_end(msg, hdr);
19148 
19149 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19150 				NL80211_MCGRP_MLME, gfp);
19151 	return;
19152 
19153  nla_put_failure:
19154 	nlmsg_free(msg);
19155 }
19156 
19157 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
19158 				       struct sta_opmode_info *sta_opmode,
19159 				       gfp_t gfp)
19160 {
19161 	struct sk_buff *msg;
19162 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19163 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19164 	void *hdr;
19165 
19166 	if (WARN_ON(!mac))
19167 		return;
19168 
19169 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19170 	if (!msg)
19171 		return;
19172 
19173 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
19174 	if (!hdr) {
19175 		nlmsg_free(msg);
19176 		return;
19177 	}
19178 
19179 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19180 		goto nla_put_failure;
19181 
19182 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19183 		goto nla_put_failure;
19184 
19185 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
19186 		goto nla_put_failure;
19187 
19188 	if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
19189 	    nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
19190 		goto nla_put_failure;
19191 
19192 	if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
19193 	    nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
19194 		goto nla_put_failure;
19195 
19196 	if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
19197 	    nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
19198 		goto nla_put_failure;
19199 
19200 	genlmsg_end(msg, hdr);
19201 
19202 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19203 				NL80211_MCGRP_MLME, gfp);
19204 
19205 	return;
19206 
19207 nla_put_failure:
19208 	nlmsg_free(msg);
19209 }
19210 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
19211 
19212 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
19213 			   u64 cookie, bool acked, s32 ack_signal,
19214 			   bool is_valid_ack_signal, gfp_t gfp)
19215 {
19216 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19217 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19218 	struct sk_buff *msg;
19219 	void *hdr;
19220 
19221 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
19222 
19223 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19224 
19225 	if (!msg)
19226 		return;
19227 
19228 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
19229 	if (!hdr) {
19230 		nlmsg_free(msg);
19231 		return;
19232 	}
19233 
19234 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19235 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19236 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
19237 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
19238 			      NL80211_ATTR_PAD) ||
19239 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
19240 	    (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
19241 						ack_signal)))
19242 		goto nla_put_failure;
19243 
19244 	genlmsg_end(msg, hdr);
19245 
19246 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19247 				NL80211_MCGRP_MLME, gfp);
19248 	return;
19249 
19250  nla_put_failure:
19251 	nlmsg_free(msg);
19252 }
19253 EXPORT_SYMBOL(cfg80211_probe_status);
19254 
19255 void cfg80211_report_obss_beacon_khz(struct wiphy *wiphy, const u8 *frame,
19256 				     size_t len, int freq, int sig_dbm)
19257 {
19258 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19259 	struct sk_buff *msg;
19260 	void *hdr;
19261 	struct cfg80211_beacon_registration *reg;
19262 
19263 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
19264 
19265 	spin_lock_bh(&rdev->beacon_registrations_lock);
19266 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
19267 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
19268 		if (!msg) {
19269 			spin_unlock_bh(&rdev->beacon_registrations_lock);
19270 			return;
19271 		}
19272 
19273 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
19274 		if (!hdr)
19275 			goto nla_put_failure;
19276 
19277 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19278 		    (freq &&
19279 		     (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
19280 				  KHZ_TO_MHZ(freq)) ||
19281 		      nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
19282 				  freq % 1000))) ||
19283 		    (sig_dbm &&
19284 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
19285 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
19286 			goto nla_put_failure;
19287 
19288 		genlmsg_end(msg, hdr);
19289 
19290 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
19291 	}
19292 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19293 	return;
19294 
19295  nla_put_failure:
19296 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19297 	nlmsg_free(msg);
19298 }
19299 EXPORT_SYMBOL(cfg80211_report_obss_beacon_khz);
19300 
19301 #ifdef CONFIG_PM
19302 static int cfg80211_net_detect_results(struct sk_buff *msg,
19303 				       struct cfg80211_wowlan_wakeup *wakeup)
19304 {
19305 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
19306 	struct nlattr *nl_results, *nl_match, *nl_freqs;
19307 	int i, j;
19308 
19309 	nl_results = nla_nest_start_noflag(msg,
19310 					   NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
19311 	if (!nl_results)
19312 		return -EMSGSIZE;
19313 
19314 	for (i = 0; i < nd->n_matches; i++) {
19315 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
19316 
19317 		nl_match = nla_nest_start_noflag(msg, i);
19318 		if (!nl_match)
19319 			break;
19320 
19321 		/* The SSID attribute is optional in nl80211, but for
19322 		 * simplicity reasons it's always present in the
19323 		 * cfg80211 structure.  If a driver can't pass the
19324 		 * SSID, that needs to be changed.  A zero length SSID
19325 		 * is still a valid SSID (wildcard), so it cannot be
19326 		 * used for this purpose.
19327 		 */
19328 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
19329 			    match->ssid.ssid)) {
19330 			nla_nest_cancel(msg, nl_match);
19331 			goto out;
19332 		}
19333 
19334 		if (match->n_channels) {
19335 			nl_freqs = nla_nest_start_noflag(msg,
19336 							 NL80211_ATTR_SCAN_FREQUENCIES);
19337 			if (!nl_freqs) {
19338 				nla_nest_cancel(msg, nl_match);
19339 				goto out;
19340 			}
19341 
19342 			for (j = 0; j < match->n_channels; j++) {
19343 				if (nla_put_u32(msg, j, match->channels[j])) {
19344 					nla_nest_cancel(msg, nl_freqs);
19345 					nla_nest_cancel(msg, nl_match);
19346 					goto out;
19347 				}
19348 			}
19349 
19350 			nla_nest_end(msg, nl_freqs);
19351 		}
19352 
19353 		nla_nest_end(msg, nl_match);
19354 	}
19355 
19356 out:
19357 	nla_nest_end(msg, nl_results);
19358 	return 0;
19359 }
19360 
19361 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
19362 				   struct cfg80211_wowlan_wakeup *wakeup,
19363 				   gfp_t gfp)
19364 {
19365 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19366 	struct sk_buff *msg;
19367 	void *hdr;
19368 	int size = 200;
19369 
19370 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
19371 
19372 	if (wakeup)
19373 		size += wakeup->packet_present_len;
19374 
19375 	msg = nlmsg_new(size, gfp);
19376 	if (!msg)
19377 		return;
19378 
19379 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
19380 	if (!hdr)
19381 		goto free_msg;
19382 
19383 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19384 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19385 			      NL80211_ATTR_PAD))
19386 		goto free_msg;
19387 
19388 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
19389 					wdev->netdev->ifindex))
19390 		goto free_msg;
19391 
19392 	if (wakeup) {
19393 		struct nlattr *reasons;
19394 
19395 		reasons = nla_nest_start_noflag(msg,
19396 						NL80211_ATTR_WOWLAN_TRIGGERS);
19397 		if (!reasons)
19398 			goto free_msg;
19399 
19400 		if (wakeup->disconnect &&
19401 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
19402 			goto free_msg;
19403 		if (wakeup->magic_pkt &&
19404 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
19405 			goto free_msg;
19406 		if (wakeup->gtk_rekey_failure &&
19407 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
19408 			goto free_msg;
19409 		if (wakeup->eap_identity_req &&
19410 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
19411 			goto free_msg;
19412 		if (wakeup->four_way_handshake &&
19413 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
19414 			goto free_msg;
19415 		if (wakeup->rfkill_release &&
19416 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
19417 			goto free_msg;
19418 
19419 		if (wakeup->pattern_idx >= 0 &&
19420 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
19421 				wakeup->pattern_idx))
19422 			goto free_msg;
19423 
19424 		if (wakeup->tcp_match &&
19425 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
19426 			goto free_msg;
19427 
19428 		if (wakeup->tcp_connlost &&
19429 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
19430 			goto free_msg;
19431 
19432 		if (wakeup->tcp_nomoretokens &&
19433 		    nla_put_flag(msg,
19434 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
19435 			goto free_msg;
19436 
19437 		if (wakeup->packet) {
19438 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
19439 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
19440 
19441 			if (!wakeup->packet_80211) {
19442 				pkt_attr =
19443 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
19444 				len_attr =
19445 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
19446 			}
19447 
19448 			if (wakeup->packet_len &&
19449 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
19450 				goto free_msg;
19451 
19452 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
19453 				    wakeup->packet))
19454 				goto free_msg;
19455 		}
19456 
19457 		if (wakeup->net_detect &&
19458 		    cfg80211_net_detect_results(msg, wakeup))
19459 				goto free_msg;
19460 
19461 		nla_nest_end(msg, reasons);
19462 	}
19463 
19464 	genlmsg_end(msg, hdr);
19465 
19466 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19467 				NL80211_MCGRP_MLME, gfp);
19468 	return;
19469 
19470  free_msg:
19471 	nlmsg_free(msg);
19472 }
19473 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
19474 #endif
19475 
19476 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
19477 				enum nl80211_tdls_operation oper,
19478 				u16 reason_code, gfp_t gfp)
19479 {
19480 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19481 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19482 	struct sk_buff *msg;
19483 	void *hdr;
19484 
19485 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
19486 					 reason_code);
19487 
19488 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19489 	if (!msg)
19490 		return;
19491 
19492 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
19493 	if (!hdr) {
19494 		nlmsg_free(msg);
19495 		return;
19496 	}
19497 
19498 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19499 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19500 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
19501 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
19502 	    (reason_code > 0 &&
19503 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
19504 		goto nla_put_failure;
19505 
19506 	genlmsg_end(msg, hdr);
19507 
19508 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19509 				NL80211_MCGRP_MLME, gfp);
19510 	return;
19511 
19512  nla_put_failure:
19513 	nlmsg_free(msg);
19514 }
19515 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
19516 
19517 static int nl80211_netlink_notify(struct notifier_block * nb,
19518 				  unsigned long state,
19519 				  void *_notify)
19520 {
19521 	struct netlink_notify *notify = _notify;
19522 	struct cfg80211_registered_device *rdev;
19523 	struct wireless_dev *wdev;
19524 	struct cfg80211_beacon_registration *reg, *tmp;
19525 
19526 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
19527 		return NOTIFY_DONE;
19528 
19529 	rcu_read_lock();
19530 
19531 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
19532 		struct cfg80211_sched_scan_request *sched_scan_req;
19533 
19534 		list_for_each_entry_rcu(sched_scan_req,
19535 					&rdev->sched_scan_req_list,
19536 					list) {
19537 			if (sched_scan_req->owner_nlportid == notify->portid) {
19538 				sched_scan_req->nl_owner_dead = true;
19539 				schedule_work(&rdev->sched_scan_stop_wk);
19540 			}
19541 		}
19542 
19543 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
19544 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
19545 
19546 			if (wdev->owner_nlportid == notify->portid) {
19547 				wdev->nl_owner_dead = true;
19548 				schedule_work(&rdev->destroy_work);
19549 			} else if (wdev->conn_owner_nlportid == notify->portid) {
19550 				schedule_work(&wdev->disconnect_wk);
19551 			}
19552 
19553 			cfg80211_release_pmsr(wdev, notify->portid);
19554 		}
19555 
19556 		spin_lock_bh(&rdev->beacon_registrations_lock);
19557 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
19558 					 list) {
19559 			if (reg->nlportid == notify->portid) {
19560 				list_del(&reg->list);
19561 				kfree(reg);
19562 				break;
19563 			}
19564 		}
19565 		spin_unlock_bh(&rdev->beacon_registrations_lock);
19566 	}
19567 
19568 	rcu_read_unlock();
19569 
19570 	/*
19571 	 * It is possible that the user space process that is controlling the
19572 	 * indoor setting disappeared, so notify the regulatory core.
19573 	 */
19574 	regulatory_netlink_notify(notify->portid);
19575 	return NOTIFY_OK;
19576 }
19577 
19578 static struct notifier_block nl80211_netlink_notifier = {
19579 	.notifier_call = nl80211_netlink_notify,
19580 };
19581 
19582 void cfg80211_ft_event(struct net_device *netdev,
19583 		       struct cfg80211_ft_event_params *ft_event)
19584 {
19585 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
19586 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19587 	struct sk_buff *msg;
19588 	void *hdr;
19589 
19590 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
19591 
19592 	if (!ft_event->target_ap)
19593 		return;
19594 
19595 	msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
19596 			GFP_KERNEL);
19597 	if (!msg)
19598 		return;
19599 
19600 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
19601 	if (!hdr)
19602 		goto out;
19603 
19604 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19605 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19606 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
19607 		goto out;
19608 
19609 	if (ft_event->ies &&
19610 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
19611 		goto out;
19612 	if (ft_event->ric_ies &&
19613 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
19614 		    ft_event->ric_ies))
19615 		goto out;
19616 
19617 	genlmsg_end(msg, hdr);
19618 
19619 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19620 				NL80211_MCGRP_MLME, GFP_KERNEL);
19621 	return;
19622  out:
19623 	nlmsg_free(msg);
19624 }
19625 EXPORT_SYMBOL(cfg80211_ft_event);
19626 
19627 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
19628 {
19629 	struct cfg80211_registered_device *rdev;
19630 	struct sk_buff *msg;
19631 	void *hdr;
19632 	u32 nlportid;
19633 
19634 	rdev = wiphy_to_rdev(wdev->wiphy);
19635 	if (!rdev->crit_proto_nlportid)
19636 		return;
19637 
19638 	nlportid = rdev->crit_proto_nlportid;
19639 	rdev->crit_proto_nlportid = 0;
19640 
19641 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19642 	if (!msg)
19643 		return;
19644 
19645 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
19646 	if (!hdr)
19647 		goto nla_put_failure;
19648 
19649 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19650 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19651 			      NL80211_ATTR_PAD))
19652 		goto nla_put_failure;
19653 
19654 	genlmsg_end(msg, hdr);
19655 
19656 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
19657 	return;
19658 
19659  nla_put_failure:
19660 	nlmsg_free(msg);
19661 }
19662 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
19663 
19664 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
19665 {
19666 	struct wiphy *wiphy = wdev->wiphy;
19667 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19668 	struct sk_buff *msg;
19669 	void *hdr;
19670 
19671 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
19672 	if (!msg)
19673 		return;
19674 
19675 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
19676 	if (!hdr)
19677 		goto out;
19678 
19679 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19680 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
19681 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19682 			      NL80211_ATTR_PAD))
19683 		goto out;
19684 
19685 	genlmsg_end(msg, hdr);
19686 
19687 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
19688 				NL80211_MCGRP_MLME, GFP_KERNEL);
19689 	return;
19690  out:
19691 	nlmsg_free(msg);
19692 }
19693 
19694 int cfg80211_external_auth_request(struct net_device *dev,
19695 				   struct cfg80211_external_auth_params *params,
19696 				   gfp_t gfp)
19697 {
19698 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19699 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19700 	struct sk_buff *msg;
19701 	void *hdr;
19702 
19703 	if (!wdev->conn_owner_nlportid)
19704 		return -EINVAL;
19705 
19706 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19707 	if (!msg)
19708 		return -ENOMEM;
19709 
19710 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
19711 	if (!hdr)
19712 		goto nla_put_failure;
19713 
19714 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19715 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19716 	    nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
19717 	    nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
19718 			params->action) ||
19719 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
19720 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
19721 		    params->ssid.ssid))
19722 		goto nla_put_failure;
19723 
19724 	genlmsg_end(msg, hdr);
19725 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
19726 			wdev->conn_owner_nlportid);
19727 	return 0;
19728 
19729  nla_put_failure:
19730 	nlmsg_free(msg);
19731 	return -ENOBUFS;
19732 }
19733 EXPORT_SYMBOL(cfg80211_external_auth_request);
19734 
19735 void cfg80211_update_owe_info_event(struct net_device *netdev,
19736 				    struct cfg80211_update_owe_info *owe_info,
19737 				    gfp_t gfp)
19738 {
19739 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
19740 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19741 	struct sk_buff *msg;
19742 	void *hdr;
19743 
19744 	trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
19745 
19746 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19747 	if (!msg)
19748 		return;
19749 
19750 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
19751 	if (!hdr)
19752 		goto nla_put_failure;
19753 
19754 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19755 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19756 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
19757 		goto nla_put_failure;
19758 
19759 	if (!owe_info->ie_len ||
19760 	    nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
19761 		goto nla_put_failure;
19762 
19763 	genlmsg_end(msg, hdr);
19764 
19765 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19766 				NL80211_MCGRP_MLME, gfp);
19767 	return;
19768 
19769 nla_put_failure:
19770 	genlmsg_cancel(msg, hdr);
19771 	nlmsg_free(msg);
19772 }
19773 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
19774 
19775 /* initialisation/exit functions */
19776 
19777 int __init nl80211_init(void)
19778 {
19779 	int err;
19780 
19781 	err = genl_register_family(&nl80211_fam);
19782 	if (err)
19783 		return err;
19784 
19785 	err = netlink_register_notifier(&nl80211_netlink_notifier);
19786 	if (err)
19787 		goto err_out;
19788 
19789 	return 0;
19790  err_out:
19791 	genl_unregister_family(&nl80211_fam);
19792 	return err;
19793 }
19794 
19795 void nl80211_exit(void)
19796 {
19797 	netlink_unregister_notifier(&nl80211_netlink_notifier);
19798 	genl_unregister_family(&nl80211_fam);
19799 }
19800