xref: /linux/net/wireless/nl80211.c (revision ab475966455ce285c2c9978a3e3bfe97d75ff8d4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This is the new netlink-based wireless configuration interface.
4  *
5  * Copyright 2006-2010	Johannes Berg <johannes@sipsolutions.net>
6  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  * Copyright 2015-2017	Intel Deutschland GmbH
8  * Copyright (C) 2018-2023 Intel Corporation
9  */
10 
11 #include <linux/if.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/list.h>
16 #include <linux/if_ether.h>
17 #include <linux/ieee80211.h>
18 #include <linux/nl80211.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/netlink.h>
21 #include <linux/nospec.h>
22 #include <linux/etherdevice.h>
23 #include <linux/if_vlan.h>
24 #include <net/net_namespace.h>
25 #include <net/genetlink.h>
26 #include <net/cfg80211.h>
27 #include <net/sock.h>
28 #include <net/inet_connection_sock.h>
29 #include "core.h"
30 #include "nl80211.h"
31 #include "reg.h"
32 #include "rdev-ops.h"
33 
34 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
35 				   struct genl_info *info,
36 				   struct cfg80211_crypto_settings *settings,
37 				   int cipher_limit);
38 
39 /* the netlink family */
40 static struct genl_family nl80211_fam;
41 
42 /* multicast groups */
43 enum nl80211_multicast_groups {
44 	NL80211_MCGRP_CONFIG,
45 	NL80211_MCGRP_SCAN,
46 	NL80211_MCGRP_REGULATORY,
47 	NL80211_MCGRP_MLME,
48 	NL80211_MCGRP_VENDOR,
49 	NL80211_MCGRP_NAN,
50 	NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
51 };
52 
53 static const struct genl_multicast_group nl80211_mcgrps[] = {
54 	[NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
55 	[NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
56 	[NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
57 	[NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
58 	[NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
59 	[NL80211_MCGRP_NAN] = { .name = NL80211_MULTICAST_GROUP_NAN },
60 #ifdef CONFIG_NL80211_TESTMODE
61 	[NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
62 #endif
63 };
64 
65 /* returns ERR_PTR values */
66 static struct wireless_dev *
67 __cfg80211_wdev_from_attrs(struct cfg80211_registered_device *rdev,
68 			   struct net *netns, struct nlattr **attrs)
69 {
70 	struct wireless_dev *result = NULL;
71 	bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
72 	bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
73 	u64 wdev_id = 0;
74 	int wiphy_idx = -1;
75 	int ifidx = -1;
76 
77 	if (!have_ifidx && !have_wdev_id)
78 		return ERR_PTR(-EINVAL);
79 
80 	if (have_ifidx)
81 		ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
82 	if (have_wdev_id) {
83 		wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
84 		wiphy_idx = wdev_id >> 32;
85 	}
86 
87 	if (rdev) {
88 		struct wireless_dev *wdev;
89 
90 		lockdep_assert_held(&rdev->wiphy.mtx);
91 
92 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
93 			if (have_ifidx && wdev->netdev &&
94 			    wdev->netdev->ifindex == ifidx) {
95 				result = wdev;
96 				break;
97 			}
98 			if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
99 				result = wdev;
100 				break;
101 			}
102 		}
103 
104 		return result ?: ERR_PTR(-ENODEV);
105 	}
106 
107 	ASSERT_RTNL();
108 
109 	for_each_rdev(rdev) {
110 		struct wireless_dev *wdev;
111 
112 		if (wiphy_net(&rdev->wiphy) != netns)
113 			continue;
114 
115 		if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
116 			continue;
117 
118 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
119 			if (have_ifidx && wdev->netdev &&
120 			    wdev->netdev->ifindex == ifidx) {
121 				result = wdev;
122 				break;
123 			}
124 			if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
125 				result = wdev;
126 				break;
127 			}
128 		}
129 
130 		if (result)
131 			break;
132 	}
133 
134 	if (result)
135 		return result;
136 	return ERR_PTR(-ENODEV);
137 }
138 
139 static struct cfg80211_registered_device *
140 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
141 {
142 	struct cfg80211_registered_device *rdev = NULL, *tmp;
143 	struct net_device *netdev;
144 
145 	ASSERT_RTNL();
146 
147 	if (!attrs[NL80211_ATTR_WIPHY] &&
148 	    !attrs[NL80211_ATTR_IFINDEX] &&
149 	    !attrs[NL80211_ATTR_WDEV])
150 		return ERR_PTR(-EINVAL);
151 
152 	if (attrs[NL80211_ATTR_WIPHY])
153 		rdev = cfg80211_rdev_by_wiphy_idx(
154 				nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
155 
156 	if (attrs[NL80211_ATTR_WDEV]) {
157 		u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
158 		struct wireless_dev *wdev;
159 		bool found = false;
160 
161 		tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
162 		if (tmp) {
163 			/* make sure wdev exists */
164 			list_for_each_entry(wdev, &tmp->wiphy.wdev_list, list) {
165 				if (wdev->identifier != (u32)wdev_id)
166 					continue;
167 				found = true;
168 				break;
169 			}
170 
171 			if (!found)
172 				tmp = NULL;
173 
174 			if (rdev && tmp != rdev)
175 				return ERR_PTR(-EINVAL);
176 			rdev = tmp;
177 		}
178 	}
179 
180 	if (attrs[NL80211_ATTR_IFINDEX]) {
181 		int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
182 
183 		netdev = __dev_get_by_index(netns, ifindex);
184 		if (netdev) {
185 			if (netdev->ieee80211_ptr)
186 				tmp = wiphy_to_rdev(
187 					netdev->ieee80211_ptr->wiphy);
188 			else
189 				tmp = NULL;
190 
191 			/* not wireless device -- return error */
192 			if (!tmp)
193 				return ERR_PTR(-EINVAL);
194 
195 			/* mismatch -- return error */
196 			if (rdev && tmp != rdev)
197 				return ERR_PTR(-EINVAL);
198 
199 			rdev = tmp;
200 		}
201 	}
202 
203 	if (!rdev)
204 		return ERR_PTR(-ENODEV);
205 
206 	if (netns != wiphy_net(&rdev->wiphy))
207 		return ERR_PTR(-ENODEV);
208 
209 	return rdev;
210 }
211 
212 /*
213  * This function returns a pointer to the driver
214  * that the genl_info item that is passed refers to.
215  *
216  * The result of this can be a PTR_ERR and hence must
217  * be checked with IS_ERR() for errors.
218  */
219 static struct cfg80211_registered_device *
220 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
221 {
222 	return __cfg80211_rdev_from_attrs(netns, info->attrs);
223 }
224 
225 static int validate_beacon_head(const struct nlattr *attr,
226 				struct netlink_ext_ack *extack)
227 {
228 	const u8 *data = nla_data(attr);
229 	unsigned int len = nla_len(attr);
230 	const struct element *elem;
231 	const struct ieee80211_mgmt *mgmt = (void *)data;
232 	unsigned int fixedlen, hdrlen;
233 	bool s1g_bcn;
234 
235 	if (len < offsetofend(typeof(*mgmt), frame_control))
236 		goto err;
237 
238 	s1g_bcn = ieee80211_is_s1g_beacon(mgmt->frame_control);
239 	if (s1g_bcn) {
240 		fixedlen = offsetof(struct ieee80211_ext,
241 				    u.s1g_beacon.variable);
242 		hdrlen = offsetof(struct ieee80211_ext, u.s1g_beacon);
243 	} else {
244 		fixedlen = offsetof(struct ieee80211_mgmt,
245 				    u.beacon.variable);
246 		hdrlen = offsetof(struct ieee80211_mgmt, u.beacon);
247 	}
248 
249 	if (len < fixedlen)
250 		goto err;
251 
252 	if (ieee80211_hdrlen(mgmt->frame_control) != hdrlen)
253 		goto err;
254 
255 	data += fixedlen;
256 	len -= fixedlen;
257 
258 	for_each_element(elem, data, len) {
259 		/* nothing */
260 	}
261 
262 	if (for_each_element_completed(elem, data, len))
263 		return 0;
264 
265 err:
266 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed beacon head");
267 	return -EINVAL;
268 }
269 
270 static int validate_ie_attr(const struct nlattr *attr,
271 			    struct netlink_ext_ack *extack)
272 {
273 	const u8 *data = nla_data(attr);
274 	unsigned int len = nla_len(attr);
275 	const struct element *elem;
276 
277 	for_each_element(elem, data, len) {
278 		/* nothing */
279 	}
280 
281 	if (for_each_element_completed(elem, data, len))
282 		return 0;
283 
284 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed information elements");
285 	return -EINVAL;
286 }
287 
288 static int validate_he_capa(const struct nlattr *attr,
289 			    struct netlink_ext_ack *extack)
290 {
291 	if (!ieee80211_he_capa_size_ok(nla_data(attr), nla_len(attr)))
292 		return -EINVAL;
293 
294 	return 0;
295 }
296 
297 /* policy for the attributes */
298 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR];
299 
300 static const struct nla_policy
301 nl80211_ftm_responder_policy[NL80211_FTM_RESP_ATTR_MAX + 1] = {
302 	[NL80211_FTM_RESP_ATTR_ENABLED] = { .type = NLA_FLAG, },
303 	[NL80211_FTM_RESP_ATTR_LCI] = { .type = NLA_BINARY,
304 					.len = U8_MAX },
305 	[NL80211_FTM_RESP_ATTR_CIVICLOC] = { .type = NLA_BINARY,
306 					     .len = U8_MAX },
307 };
308 
309 static const struct nla_policy
310 nl80211_pmsr_ftm_req_attr_policy[NL80211_PMSR_FTM_REQ_ATTR_MAX + 1] = {
311 	[NL80211_PMSR_FTM_REQ_ATTR_ASAP] = { .type = NLA_FLAG },
312 	[NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE] = { .type = NLA_U32 },
313 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP] =
314 		NLA_POLICY_MAX(NLA_U8, 15),
315 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD] = { .type = NLA_U16 },
316 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION] =
317 		NLA_POLICY_MAX(NLA_U8, 15),
318 	[NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST] =
319 		NLA_POLICY_MAX(NLA_U8, 31),
320 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES] = { .type = NLA_U8 },
321 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI] = { .type = NLA_FLAG },
322 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC] = { .type = NLA_FLAG },
323 	[NL80211_PMSR_FTM_REQ_ATTR_TRIGGER_BASED] = { .type = NLA_FLAG },
324 	[NL80211_PMSR_FTM_REQ_ATTR_NON_TRIGGER_BASED] = { .type = NLA_FLAG },
325 	[NL80211_PMSR_FTM_REQ_ATTR_LMR_FEEDBACK] = { .type = NLA_FLAG },
326 	[NL80211_PMSR_FTM_REQ_ATTR_BSS_COLOR] = { .type = NLA_U8 },
327 };
328 
329 static const struct nla_policy
330 nl80211_pmsr_req_data_policy[NL80211_PMSR_TYPE_MAX + 1] = {
331 	[NL80211_PMSR_TYPE_FTM] =
332 		NLA_POLICY_NESTED(nl80211_pmsr_ftm_req_attr_policy),
333 };
334 
335 static const struct nla_policy
336 nl80211_pmsr_req_attr_policy[NL80211_PMSR_REQ_ATTR_MAX + 1] = {
337 	[NL80211_PMSR_REQ_ATTR_DATA] =
338 		NLA_POLICY_NESTED(nl80211_pmsr_req_data_policy),
339 	[NL80211_PMSR_REQ_ATTR_GET_AP_TSF] = { .type = NLA_FLAG },
340 };
341 
342 static const struct nla_policy
343 nl80211_pmsr_peer_attr_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
344 	[NL80211_PMSR_PEER_ATTR_ADDR] = NLA_POLICY_ETH_ADDR,
345 	[NL80211_PMSR_PEER_ATTR_CHAN] = NLA_POLICY_NESTED(nl80211_policy),
346 	[NL80211_PMSR_PEER_ATTR_REQ] =
347 		NLA_POLICY_NESTED(nl80211_pmsr_req_attr_policy),
348 	[NL80211_PMSR_PEER_ATTR_RESP] = { .type = NLA_REJECT },
349 };
350 
351 static const struct nla_policy
352 nl80211_pmsr_attr_policy[NL80211_PMSR_ATTR_MAX + 1] = {
353 	[NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_REJECT },
354 	[NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_REJECT },
355 	[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
356 	[NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
357 	[NL80211_PMSR_ATTR_PEERS] =
358 		NLA_POLICY_NESTED_ARRAY(nl80211_pmsr_peer_attr_policy),
359 };
360 
361 static const struct nla_policy
362 he_obss_pd_policy[NL80211_HE_OBSS_PD_ATTR_MAX + 1] = {
363 	[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] =
364 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
365 	[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET] =
366 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
367 	[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET] =
368 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
369 	[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP] =
370 		NLA_POLICY_EXACT_LEN(8),
371 	[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP] =
372 		NLA_POLICY_EXACT_LEN(8),
373 	[NL80211_HE_OBSS_PD_ATTR_SR_CTRL] = { .type = NLA_U8 },
374 };
375 
376 static const struct nla_policy
377 he_bss_color_policy[NL80211_HE_BSS_COLOR_ATTR_MAX + 1] = {
378 	[NL80211_HE_BSS_COLOR_ATTR_COLOR] = NLA_POLICY_RANGE(NLA_U8, 1, 63),
379 	[NL80211_HE_BSS_COLOR_ATTR_DISABLED] = { .type = NLA_FLAG },
380 	[NL80211_HE_BSS_COLOR_ATTR_PARTIAL] = { .type = NLA_FLAG },
381 };
382 
383 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
384 	[NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
385 				    .len = NL80211_MAX_SUPP_RATES },
386 	[NL80211_TXRATE_HT] = { .type = NLA_BINARY,
387 				.len = NL80211_MAX_SUPP_HT_RATES },
388 	[NL80211_TXRATE_VHT] = NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_txrate_vht)),
389 	[NL80211_TXRATE_GI] = { .type = NLA_U8 },
390 	[NL80211_TXRATE_HE] = NLA_POLICY_EXACT_LEN(sizeof(struct nl80211_txrate_he)),
391 	[NL80211_TXRATE_HE_GI] =  NLA_POLICY_RANGE(NLA_U8,
392 						   NL80211_RATE_INFO_HE_GI_0_8,
393 						   NL80211_RATE_INFO_HE_GI_3_2),
394 	[NL80211_TXRATE_HE_LTF] = NLA_POLICY_RANGE(NLA_U8,
395 						   NL80211_RATE_INFO_HE_1XLTF,
396 						   NL80211_RATE_INFO_HE_4XLTF),
397 };
398 
399 static const struct nla_policy
400 nl80211_tid_config_attr_policy[NL80211_TID_CONFIG_ATTR_MAX + 1] = {
401 	[NL80211_TID_CONFIG_ATTR_VIF_SUPP] = { .type = NLA_U64 },
402 	[NL80211_TID_CONFIG_ATTR_PEER_SUPP] = { .type = NLA_U64 },
403 	[NL80211_TID_CONFIG_ATTR_OVERRIDE] = { .type = NLA_FLAG },
404 	[NL80211_TID_CONFIG_ATTR_TIDS] = NLA_POLICY_RANGE(NLA_U16, 1, 0xff),
405 	[NL80211_TID_CONFIG_ATTR_NOACK] =
406 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
407 	[NL80211_TID_CONFIG_ATTR_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
408 	[NL80211_TID_CONFIG_ATTR_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
409 	[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL] =
410 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
411 	[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL] =
412 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
413 	[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL] =
414 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
415 	[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE] =
416 			NLA_POLICY_MAX(NLA_U8, NL80211_TX_RATE_FIXED),
417 	[NL80211_TID_CONFIG_ATTR_TX_RATE] =
418 			NLA_POLICY_NESTED(nl80211_txattr_policy),
419 };
420 
421 static const struct nla_policy
422 nl80211_fils_discovery_policy[NL80211_FILS_DISCOVERY_ATTR_MAX + 1] = {
423 	[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] = NLA_POLICY_MAX(NLA_U32, 10000),
424 	[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] = NLA_POLICY_MAX(NLA_U32, 10000),
425 	[NL80211_FILS_DISCOVERY_ATTR_TMPL] =
426 			NLA_POLICY_RANGE(NLA_BINARY,
427 					 NL80211_FILS_DISCOVERY_TMPL_MIN_LEN,
428 					 IEEE80211_MAX_DATA_LEN),
429 };
430 
431 static const struct nla_policy
432 nl80211_unsol_bcast_probe_resp_policy[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1] = {
433 	[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] = NLA_POLICY_MAX(NLA_U32, 20),
434 	[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL] = { .type = NLA_BINARY,
435 						       .len = IEEE80211_MAX_DATA_LEN }
436 };
437 
438 static const struct nla_policy
439 sar_specs_policy[NL80211_SAR_ATTR_SPECS_MAX + 1] = {
440 	[NL80211_SAR_ATTR_SPECS_POWER] = { .type = NLA_S32 },
441 	[NL80211_SAR_ATTR_SPECS_RANGE_INDEX] = {.type = NLA_U32 },
442 };
443 
444 static const struct nla_policy
445 sar_policy[NL80211_SAR_ATTR_MAX + 1] = {
446 	[NL80211_SAR_ATTR_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_SAR_TYPE),
447 	[NL80211_SAR_ATTR_SPECS] = NLA_POLICY_NESTED_ARRAY(sar_specs_policy),
448 };
449 
450 static const struct nla_policy
451 nl80211_mbssid_config_policy[NL80211_MBSSID_CONFIG_ATTR_MAX + 1] = {
452 	[NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES] = NLA_POLICY_MIN(NLA_U8, 2),
453 	[NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY] =
454 						NLA_POLICY_MIN(NLA_U8, 1),
455 	[NL80211_MBSSID_CONFIG_ATTR_INDEX] = { .type = NLA_U8 },
456 	[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX] = { .type = NLA_U32 },
457 	[NL80211_MBSSID_CONFIG_ATTR_EMA] = { .type = NLA_FLAG },
458 };
459 
460 static const struct nla_policy
461 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
462 	[NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
463 	[NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
464 };
465 
466 static const struct netlink_range_validation nl80211_punct_bitmap_range = {
467 	.min = 0,
468 	.max = 0xffff,
469 };
470 
471 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
472 	[0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
473 	[NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
474 	[NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
475 				      .len = 20-1 },
476 	[NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
477 
478 	[NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
479 	[NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
480 	[NL80211_ATTR_WIPHY_EDMG_CHANNELS] = NLA_POLICY_RANGE(NLA_U8,
481 						NL80211_EDMG_CHANNELS_MIN,
482 						NL80211_EDMG_CHANNELS_MAX),
483 	[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG] = NLA_POLICY_RANGE(NLA_U8,
484 						NL80211_EDMG_BW_CONFIG_MIN,
485 						NL80211_EDMG_BW_CONFIG_MAX),
486 
487 	[NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
488 	[NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
489 	[NL80211_ATTR_CENTER_FREQ1_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
490 	[NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
491 
492 	[NL80211_ATTR_WIPHY_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
493 	[NL80211_ATTR_WIPHY_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
494 	[NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
495 	[NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
496 	[NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
497 	[NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
498 
499 	[NL80211_ATTR_IFTYPE] = NLA_POLICY_MAX(NLA_U32, NL80211_IFTYPE_MAX),
500 	[NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
501 	[NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
502 
503 	[NL80211_ATTR_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
504 	[NL80211_ATTR_PREV_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
505 
506 	[NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
507 	[NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
508 				    .len = WLAN_MAX_KEY_LEN },
509 	[NL80211_ATTR_KEY_IDX] = NLA_POLICY_MAX(NLA_U8, 7),
510 	[NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
511 	[NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
512 	[NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
513 	[NL80211_ATTR_KEY_TYPE] =
514 		NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES),
515 
516 	[NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
517 	[NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
518 	[NL80211_ATTR_BEACON_HEAD] =
519 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_beacon_head,
520 				       IEEE80211_MAX_DATA_LEN),
521 	[NL80211_ATTR_BEACON_TAIL] =
522 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
523 				       IEEE80211_MAX_DATA_LEN),
524 	[NL80211_ATTR_STA_AID] =
525 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
526 	[NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
527 	[NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
528 	[NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
529 					       .len = NL80211_MAX_SUPP_RATES },
530 	[NL80211_ATTR_STA_PLINK_ACTION] =
531 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_ACTIONS - 1),
532 	[NL80211_ATTR_STA_TX_POWER_SETTING] =
533 		NLA_POLICY_RANGE(NLA_U8,
534 				 NL80211_TX_POWER_AUTOMATIC,
535 				 NL80211_TX_POWER_FIXED),
536 	[NL80211_ATTR_STA_TX_POWER] = { .type = NLA_S16 },
537 	[NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
538 	[NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
539 	[NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
540 				   .len = IEEE80211_MAX_MESH_ID_LEN },
541 	[NL80211_ATTR_MPATH_NEXT_HOP] = NLA_POLICY_ETH_ADDR_COMPAT,
542 
543 	/* allow 3 for NUL-termination, we used to declare this NLA_STRING */
544 	[NL80211_ATTR_REG_ALPHA2] = NLA_POLICY_RANGE(NLA_BINARY, 2, 3),
545 	[NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
546 
547 	[NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
548 	[NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
549 	[NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
550 	[NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
551 					   .len = NL80211_MAX_SUPP_RATES },
552 	[NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
553 
554 	[NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
555 	[NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
556 
557 	[NL80211_ATTR_HT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_HT_CAPABILITY_LEN),
558 
559 	[NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
560 	[NL80211_ATTR_IE] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
561 						   validate_ie_attr,
562 						   IEEE80211_MAX_DATA_LEN),
563 	[NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
564 	[NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
565 
566 	[NL80211_ATTR_SSID] = { .type = NLA_BINARY,
567 				.len = IEEE80211_MAX_SSID_LEN },
568 	[NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
569 	[NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
570 	[NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
571 	[NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
572 	[NL80211_ATTR_USE_MFP] = NLA_POLICY_RANGE(NLA_U32,
573 						  NL80211_MFP_NO,
574 						  NL80211_MFP_OPTIONAL),
575 	[NL80211_ATTR_STA_FLAGS2] =
576 		NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_sta_flag_update)),
577 	[NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
578 	[NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
579 	[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
580 	[NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
581 	[NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
582 	[NL80211_ATTR_STATUS_CODE] = { .type = NLA_U16 },
583 	[NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
584 	[NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
585 	[NL80211_ATTR_PID] = { .type = NLA_U32 },
586 	[NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
587 	[NL80211_ATTR_PMKID] = NLA_POLICY_EXACT_LEN_WARN(WLAN_PMKID_LEN),
588 	[NL80211_ATTR_DURATION] = { .type = NLA_U32 },
589 	[NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
590 	[NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
591 	[NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
592 				 .len = IEEE80211_MAX_DATA_LEN },
593 	[NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
594 	[NL80211_ATTR_PS_STATE] = NLA_POLICY_RANGE(NLA_U32,
595 						   NL80211_PS_DISABLED,
596 						   NL80211_PS_ENABLED),
597 	[NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
598 	[NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
599 	[NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
600 	[NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
601 	[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
602 	[NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
603 	[NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
604 	[NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
605 	[NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
606 	[NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
607 	[NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
608 	[NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
609 	[NL80211_ATTR_STA_PLINK_STATE] =
610 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_STATES - 1),
611 	[NL80211_ATTR_MEASUREMENT_DURATION] = { .type = NLA_U16 },
612 	[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY] = { .type = NLA_FLAG },
613 	[NL80211_ATTR_MESH_PEER_AID] =
614 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
615 	[NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
616 	[NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
617 	[NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
618 	[NL80211_ATTR_HIDDEN_SSID] =
619 		NLA_POLICY_RANGE(NLA_U32,
620 				 NL80211_HIDDEN_SSID_NOT_IN_USE,
621 				 NL80211_HIDDEN_SSID_ZERO_CONTENTS),
622 	[NL80211_ATTR_IE_PROBE_RESP] =
623 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
624 				       IEEE80211_MAX_DATA_LEN),
625 	[NL80211_ATTR_IE_ASSOC_RESP] =
626 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
627 				       IEEE80211_MAX_DATA_LEN),
628 	[NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
629 	[NL80211_ATTR_STA_WME] = NLA_POLICY_NESTED(nl80211_sta_wme_policy),
630 	[NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
631 	[NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
632 	[NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
633 	[NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
634 	[NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
635 	[NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
636 	[NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
637 	[NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
638 	[NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
639 	[NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
640 				      .len = IEEE80211_MAX_DATA_LEN },
641 	[NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
642 	[NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
643 	[NL80211_ATTR_HT_CAPABILITY_MASK] = {
644 		.len = NL80211_HT_CAPABILITY_LEN
645 	},
646 	[NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
647 	[NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
648 	[NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
649 	[NL80211_ATTR_WDEV] = { .type = NLA_U64 },
650 	[NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
651 
652 	/* need to include at least Auth Transaction and Status Code */
653 	[NL80211_ATTR_AUTH_DATA] = NLA_POLICY_MIN_LEN(4),
654 
655 	[NL80211_ATTR_VHT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_VHT_CAPABILITY_LEN),
656 	[NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
657 	[NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
658 	[NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
659 	[NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
660 		NLA_POLICY_RANGE(NLA_U32,
661 				 NL80211_MESH_POWER_UNKNOWN + 1,
662 				 NL80211_MESH_POWER_MAX),
663 	[NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
664 	[NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
665 	[NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
666 	[NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
667 	[NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
668 	[NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
669 	[NL80211_ATTR_VHT_CAPABILITY_MASK] = {
670 		.len = NL80211_VHT_CAPABILITY_LEN,
671 	},
672 	[NL80211_ATTR_MDID] = { .type = NLA_U16 },
673 	[NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
674 				  .len = IEEE80211_MAX_DATA_LEN },
675 	[NL80211_ATTR_CRIT_PROT_ID] = { .type = NLA_U16 },
676 	[NL80211_ATTR_MAX_CRIT_PROT_DURATION] =
677 		NLA_POLICY_MAX(NLA_U16, NL80211_CRIT_PROTO_MAX_DURATION),
678 	[NL80211_ATTR_PEER_AID] =
679 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
680 	[NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
681 	[NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
682 	[NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
683 	[NL80211_ATTR_CNTDWN_OFFS_BEACON] = { .type = NLA_BINARY },
684 	[NL80211_ATTR_CNTDWN_OFFS_PRESP] = { .type = NLA_BINARY },
685 	[NL80211_ATTR_STA_SUPPORTED_CHANNELS] = NLA_POLICY_MIN_LEN(2),
686 	/*
687 	 * The value of the Length field of the Supported Operating
688 	 * Classes element is between 2 and 253.
689 	 */
690 	[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] =
691 		NLA_POLICY_RANGE(NLA_BINARY, 2, 253),
692 	[NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
693 	[NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
694 	[NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
695 	[NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
696 	[NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
697 	[NL80211_ATTR_QOS_MAP] = NLA_POLICY_RANGE(NLA_BINARY,
698 						  IEEE80211_QOS_MAP_LEN_MIN,
699 						  IEEE80211_QOS_MAP_LEN_MAX),
700 	[NL80211_ATTR_MAC_HINT] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
701 	[NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
702 	[NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
703 	[NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
704 	[NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
705 	[NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
706 	[NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
707 	[NL80211_ATTR_USER_PRIO] =
708 		NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
709 	[NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
710 	[NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
711 	[NL80211_ATTR_OPER_CLASS] = { .type = NLA_U8 },
712 	[NL80211_ATTR_MAC_MASK] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
713 	[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
714 	[NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
715 	[NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
716 	[NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
717 	[NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
718 	[NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
719 	[NL80211_ATTR_STA_SUPPORT_P2P_PS] =
720 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
721 	[NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
722 		.len = VHT_MUMIMO_GROUPS_DATA_LEN
723 	},
724 	[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
725 	[NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
726 	[NL80211_ATTR_BANDS] = { .type = NLA_U32 },
727 	[NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
728 	[NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
729 				    .len = FILS_MAX_KEK_LEN },
730 	[NL80211_ATTR_FILS_NONCES] = NLA_POLICY_EXACT_LEN_WARN(2 * FILS_NONCE_LEN),
731 	[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
732 	[NL80211_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
733 	[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
734 	[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
735 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
736 	},
737 	[NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
738 	[NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
739 					     .len = FILS_ERP_MAX_USERNAME_LEN },
740 	[NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
741 					  .len = FILS_ERP_MAX_REALM_LEN },
742 	[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
743 	[NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
744 					.len = FILS_ERP_MAX_RRK_LEN },
745 	[NL80211_ATTR_FILS_CACHE_ID] = NLA_POLICY_EXACT_LEN_WARN(2),
746 	[NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
747 	[NL80211_ATTR_PMKR0_NAME] = NLA_POLICY_EXACT_LEN(WLAN_PMK_NAME_LEN),
748 	[NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
749 	[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
750 
751 	[NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
752 	[NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
753 	[NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
754 	[NL80211_ATTR_HE_CAPABILITY] =
755 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_he_capa,
756 				       NL80211_HE_MAX_CAPABILITY_LEN),
757 	[NL80211_ATTR_FTM_RESPONDER] =
758 		NLA_POLICY_NESTED(nl80211_ftm_responder_policy),
759 	[NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
760 	[NL80211_ATTR_PEER_MEASUREMENTS] =
761 		NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
762 	[NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
763 	[NL80211_ATTR_SAE_PASSWORD] = { .type = NLA_BINARY,
764 					.len = SAE_PASSWORD_MAX_LEN },
765 	[NL80211_ATTR_TWT_RESPONDER] = { .type = NLA_FLAG },
766 	[NL80211_ATTR_HE_OBSS_PD] = NLA_POLICY_NESTED(he_obss_pd_policy),
767 	[NL80211_ATTR_VLAN_ID] = NLA_POLICY_RANGE(NLA_U16, 1, VLAN_N_VID - 2),
768 	[NL80211_ATTR_HE_BSS_COLOR] = NLA_POLICY_NESTED(he_bss_color_policy),
769 	[NL80211_ATTR_TID_CONFIG] =
770 		NLA_POLICY_NESTED_ARRAY(nl80211_tid_config_attr_policy),
771 	[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH] = { .type = NLA_FLAG },
772 	[NL80211_ATTR_PMK_LIFETIME] = NLA_POLICY_MIN(NLA_U32, 1),
773 	[NL80211_ATTR_PMK_REAUTH_THRESHOLD] = NLA_POLICY_RANGE(NLA_U8, 1, 100),
774 	[NL80211_ATTR_RECEIVE_MULTICAST] = { .type = NLA_FLAG },
775 	[NL80211_ATTR_WIPHY_FREQ_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
776 	[NL80211_ATTR_SCAN_FREQ_KHZ] = { .type = NLA_NESTED },
777 	[NL80211_ATTR_HE_6GHZ_CAPABILITY] =
778 		NLA_POLICY_EXACT_LEN(sizeof(struct ieee80211_he_6ghz_capa)),
779 	[NL80211_ATTR_FILS_DISCOVERY] =
780 		NLA_POLICY_NESTED(nl80211_fils_discovery_policy),
781 	[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP] =
782 		NLA_POLICY_NESTED(nl80211_unsol_bcast_probe_resp_policy),
783 	[NL80211_ATTR_S1G_CAPABILITY] =
784 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
785 	[NL80211_ATTR_S1G_CAPABILITY_MASK] =
786 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
787 	[NL80211_ATTR_SAE_PWE] =
788 		NLA_POLICY_RANGE(NLA_U8, NL80211_SAE_PWE_HUNT_AND_PECK,
789 				 NL80211_SAE_PWE_BOTH),
790 	[NL80211_ATTR_RECONNECT_REQUESTED] = { .type = NLA_REJECT },
791 	[NL80211_ATTR_SAR_SPEC] = NLA_POLICY_NESTED(sar_policy),
792 	[NL80211_ATTR_DISABLE_HE] = { .type = NLA_FLAG },
793 	[NL80211_ATTR_OBSS_COLOR_BITMAP] = { .type = NLA_U64 },
794 	[NL80211_ATTR_COLOR_CHANGE_COUNT] = { .type = NLA_U8 },
795 	[NL80211_ATTR_COLOR_CHANGE_COLOR] = { .type = NLA_U8 },
796 	[NL80211_ATTR_COLOR_CHANGE_ELEMS] = NLA_POLICY_NESTED(nl80211_policy),
797 	[NL80211_ATTR_MBSSID_CONFIG] =
798 			NLA_POLICY_NESTED(nl80211_mbssid_config_policy),
799 	[NL80211_ATTR_MBSSID_ELEMS] = { .type = NLA_NESTED },
800 	[NL80211_ATTR_RADAR_BACKGROUND] = { .type = NLA_FLAG },
801 	[NL80211_ATTR_AP_SETTINGS_FLAGS] = { .type = NLA_U32 },
802 	[NL80211_ATTR_EHT_CAPABILITY] =
803 		NLA_POLICY_RANGE(NLA_BINARY,
804 				 NL80211_EHT_MIN_CAPABILITY_LEN,
805 				 NL80211_EHT_MAX_CAPABILITY_LEN),
806 	[NL80211_ATTR_DISABLE_EHT] = { .type = NLA_FLAG },
807 	[NL80211_ATTR_MLO_LINKS] =
808 		NLA_POLICY_NESTED_ARRAY(nl80211_policy),
809 	[NL80211_ATTR_MLO_LINK_ID] =
810 		NLA_POLICY_RANGE(NLA_U8, 0, IEEE80211_MLD_MAX_NUM_LINKS),
811 	[NL80211_ATTR_MLD_ADDR] = NLA_POLICY_EXACT_LEN(ETH_ALEN),
812 	[NL80211_ATTR_MLO_SUPPORT] = { .type = NLA_FLAG },
813 	[NL80211_ATTR_MAX_NUM_AKM_SUITES] = { .type = NLA_REJECT },
814 	[NL80211_ATTR_PUNCT_BITMAP] =
815 		NLA_POLICY_FULL_RANGE(NLA_U32, &nl80211_punct_bitmap_range),
816 
817 	[NL80211_ATTR_MAX_HW_TIMESTAMP_PEERS] = { .type = NLA_U16 },
818 	[NL80211_ATTR_HW_TIMESTAMP_ENABLED] = { .type = NLA_FLAG },
819 	[NL80211_ATTR_EMA_RNR_ELEMS] = { .type = NLA_NESTED },
820 	[NL80211_ATTR_MLO_LINK_DISABLED] = { .type = NLA_FLAG },
821 };
822 
823 /* policy for the key attributes */
824 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
825 	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
826 	[NL80211_KEY_IDX] = { .type = NLA_U8 },
827 	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
828 	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
829 	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
830 	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
831 	[NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
832 	[NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
833 	[NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
834 };
835 
836 /* policy for the key default flags */
837 static const struct nla_policy
838 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
839 	[NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
840 	[NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
841 };
842 
843 #ifdef CONFIG_PM
844 /* policy for WoWLAN attributes */
845 static const struct nla_policy
846 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
847 	[NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
848 	[NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
849 	[NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
850 	[NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
851 	[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
852 	[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
853 	[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
854 	[NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
855 	[NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
856 	[NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
857 };
858 
859 static const struct nla_policy
860 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
861 	[NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
862 	[NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
863 	[NL80211_WOWLAN_TCP_DST_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
864 	[NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
865 	[NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
866 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
867 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
868 		.len = sizeof(struct nl80211_wowlan_tcp_data_seq)
869 	},
870 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
871 		.len = sizeof(struct nl80211_wowlan_tcp_data_token)
872 	},
873 	[NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
874 	[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
875 	[NL80211_WOWLAN_TCP_WAKE_MASK] = NLA_POLICY_MIN_LEN(1),
876 };
877 #endif /* CONFIG_PM */
878 
879 /* policy for coalesce rule attributes */
880 static const struct nla_policy
881 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
882 	[NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
883 	[NL80211_ATTR_COALESCE_RULE_CONDITION] =
884 		NLA_POLICY_RANGE(NLA_U32,
885 				 NL80211_COALESCE_CONDITION_MATCH,
886 				 NL80211_COALESCE_CONDITION_NO_MATCH),
887 	[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
888 };
889 
890 /* policy for GTK rekey offload attributes */
891 static const struct nla_policy
892 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
893 	[NL80211_REKEY_DATA_KEK] = {
894 		.type = NLA_BINARY,
895 		.len = NL80211_KEK_EXT_LEN
896 	},
897 	[NL80211_REKEY_DATA_KCK] = {
898 		.type = NLA_BINARY,
899 		.len = NL80211_KCK_EXT_LEN_32
900 	},
901 	[NL80211_REKEY_DATA_REPLAY_CTR] = NLA_POLICY_EXACT_LEN(NL80211_REPLAY_CTR_LEN),
902 	[NL80211_REKEY_DATA_AKM] = { .type = NLA_U32 },
903 };
904 
905 static const struct nla_policy
906 nl80211_match_band_rssi_policy[NUM_NL80211_BANDS] = {
907 	[NL80211_BAND_2GHZ] = { .type = NLA_S32 },
908 	[NL80211_BAND_5GHZ] = { .type = NLA_S32 },
909 	[NL80211_BAND_6GHZ] = { .type = NLA_S32 },
910 	[NL80211_BAND_60GHZ] = { .type = NLA_S32 },
911 	[NL80211_BAND_LC]    = { .type = NLA_S32 },
912 };
913 
914 static const struct nla_policy
915 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
916 	[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
917 						 .len = IEEE80211_MAX_SSID_LEN },
918 	[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
919 	[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
920 	[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI] =
921 		NLA_POLICY_NESTED(nl80211_match_band_rssi_policy),
922 };
923 
924 static const struct nla_policy
925 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
926 	[NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
927 	[NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
928 };
929 
930 static const struct nla_policy
931 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
932 	[NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
933 	[NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
934 	[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
935 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
936 	},
937 };
938 
939 /* policy for NAN function attributes */
940 static const struct nla_policy
941 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
942 	[NL80211_NAN_FUNC_TYPE] =
943 		NLA_POLICY_MAX(NLA_U8, NL80211_NAN_FUNC_MAX_TYPE),
944 	[NL80211_NAN_FUNC_SERVICE_ID] = {
945 				    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
946 	[NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
947 	[NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
948 	[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
949 	[NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
950 	[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
951 	[NL80211_NAN_FUNC_FOLLOW_UP_DEST] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
952 	[NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
953 	[NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
954 	[NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
955 			.len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
956 	[NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
957 	[NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
958 	[NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
959 	[NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
960 	[NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
961 };
962 
963 /* policy for Service Response Filter attributes */
964 static const struct nla_policy
965 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
966 	[NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
967 	[NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
968 				 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
969 	[NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
970 	[NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
971 };
972 
973 /* policy for packet pattern attributes */
974 static const struct nla_policy
975 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
976 	[NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
977 	[NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
978 	[NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
979 };
980 
981 static int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
982 				     struct cfg80211_registered_device **rdev,
983 				     struct wireless_dev **wdev,
984 				     struct nlattr **attrbuf)
985 {
986 	int err;
987 
988 	if (!cb->args[0]) {
989 		struct nlattr **attrbuf_free = NULL;
990 
991 		if (!attrbuf) {
992 			attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
993 					  GFP_KERNEL);
994 			if (!attrbuf)
995 				return -ENOMEM;
996 			attrbuf_free = attrbuf;
997 		}
998 
999 		err = nlmsg_parse_deprecated(cb->nlh,
1000 					     GENL_HDRLEN + nl80211_fam.hdrsize,
1001 					     attrbuf, nl80211_fam.maxattr,
1002 					     nl80211_policy, NULL);
1003 		if (err) {
1004 			kfree(attrbuf_free);
1005 			return err;
1006 		}
1007 
1008 		rtnl_lock();
1009 		*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(cb->skb->sk),
1010 						   attrbuf);
1011 		kfree(attrbuf_free);
1012 		if (IS_ERR(*wdev)) {
1013 			rtnl_unlock();
1014 			return PTR_ERR(*wdev);
1015 		}
1016 		*rdev = wiphy_to_rdev((*wdev)->wiphy);
1017 		mutex_lock(&(*rdev)->wiphy.mtx);
1018 		rtnl_unlock();
1019 		/* 0 is the first index - add 1 to parse only once */
1020 		cb->args[0] = (*rdev)->wiphy_idx + 1;
1021 		cb->args[1] = (*wdev)->identifier;
1022 	} else {
1023 		/* subtract the 1 again here */
1024 		struct wiphy *wiphy;
1025 		struct wireless_dev *tmp;
1026 
1027 		rtnl_lock();
1028 		wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
1029 		if (!wiphy) {
1030 			rtnl_unlock();
1031 			return -ENODEV;
1032 		}
1033 		*rdev = wiphy_to_rdev(wiphy);
1034 		*wdev = NULL;
1035 
1036 		list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
1037 			if (tmp->identifier == cb->args[1]) {
1038 				*wdev = tmp;
1039 				break;
1040 			}
1041 		}
1042 
1043 		if (!*wdev) {
1044 			rtnl_unlock();
1045 			return -ENODEV;
1046 		}
1047 		mutex_lock(&(*rdev)->wiphy.mtx);
1048 		rtnl_unlock();
1049 	}
1050 
1051 	return 0;
1052 }
1053 
1054 /* message building helper */
1055 void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1056 		     int flags, u8 cmd)
1057 {
1058 	/* since there is no private header just add the generic one */
1059 	return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
1060 }
1061 
1062 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
1063 				     const struct ieee80211_reg_rule *rule)
1064 {
1065 	int j;
1066 	struct nlattr *nl_wmm_rules =
1067 		nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
1068 
1069 	if (!nl_wmm_rules)
1070 		goto nla_put_failure;
1071 
1072 	for (j = 0; j < IEEE80211_NUM_ACS; j++) {
1073 		struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
1074 
1075 		if (!nl_wmm_rule)
1076 			goto nla_put_failure;
1077 
1078 		if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
1079 				rule->wmm_rule.client[j].cw_min) ||
1080 		    nla_put_u16(msg, NL80211_WMMR_CW_MAX,
1081 				rule->wmm_rule.client[j].cw_max) ||
1082 		    nla_put_u8(msg, NL80211_WMMR_AIFSN,
1083 			       rule->wmm_rule.client[j].aifsn) ||
1084 		    nla_put_u16(msg, NL80211_WMMR_TXOP,
1085 			        rule->wmm_rule.client[j].cot))
1086 			goto nla_put_failure;
1087 
1088 		nla_nest_end(msg, nl_wmm_rule);
1089 	}
1090 	nla_nest_end(msg, nl_wmm_rules);
1091 
1092 	return 0;
1093 
1094 nla_put_failure:
1095 	return -ENOBUFS;
1096 }
1097 
1098 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
1099 				   struct ieee80211_channel *chan,
1100 				   bool large)
1101 {
1102 	/* Some channels must be completely excluded from the
1103 	 * list to protect old user-space tools from breaking
1104 	 */
1105 	if (!large && chan->flags &
1106 	    (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
1107 		return 0;
1108 	if (!large && chan->freq_offset)
1109 		return 0;
1110 
1111 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
1112 			chan->center_freq))
1113 		goto nla_put_failure;
1114 
1115 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_OFFSET, chan->freq_offset))
1116 		goto nla_put_failure;
1117 
1118 	if ((chan->flags & IEEE80211_CHAN_PSD) &&
1119 	    nla_put_s8(msg, NL80211_FREQUENCY_ATTR_PSD, chan->psd))
1120 		goto nla_put_failure;
1121 
1122 	if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
1123 	    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
1124 		goto nla_put_failure;
1125 	if (chan->flags & IEEE80211_CHAN_NO_IR) {
1126 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
1127 			goto nla_put_failure;
1128 		if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
1129 			goto nla_put_failure;
1130 	}
1131 	if (chan->flags & IEEE80211_CHAN_RADAR) {
1132 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
1133 			goto nla_put_failure;
1134 		if (large) {
1135 			u32 time;
1136 
1137 			time = elapsed_jiffies_msecs(chan->dfs_state_entered);
1138 
1139 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
1140 					chan->dfs_state))
1141 				goto nla_put_failure;
1142 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
1143 					time))
1144 				goto nla_put_failure;
1145 			if (nla_put_u32(msg,
1146 					NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
1147 					chan->dfs_cac_ms))
1148 				goto nla_put_failure;
1149 		}
1150 	}
1151 
1152 	if (large) {
1153 		if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
1154 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
1155 			goto nla_put_failure;
1156 		if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
1157 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
1158 			goto nla_put_failure;
1159 		if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
1160 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
1161 			goto nla_put_failure;
1162 		if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
1163 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
1164 			goto nla_put_failure;
1165 		if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
1166 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
1167 			goto nla_put_failure;
1168 		if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
1169 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
1170 			goto nla_put_failure;
1171 		if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
1172 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
1173 			goto nla_put_failure;
1174 		if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
1175 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
1176 			goto nla_put_failure;
1177 		if ((chan->flags & IEEE80211_CHAN_NO_HE) &&
1178 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HE))
1179 			goto nla_put_failure;
1180 		if ((chan->flags & IEEE80211_CHAN_1MHZ) &&
1181 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_1MHZ))
1182 			goto nla_put_failure;
1183 		if ((chan->flags & IEEE80211_CHAN_2MHZ) &&
1184 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_2MHZ))
1185 			goto nla_put_failure;
1186 		if ((chan->flags & IEEE80211_CHAN_4MHZ) &&
1187 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_4MHZ))
1188 			goto nla_put_failure;
1189 		if ((chan->flags & IEEE80211_CHAN_8MHZ) &&
1190 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_8MHZ))
1191 			goto nla_put_failure;
1192 		if ((chan->flags & IEEE80211_CHAN_16MHZ) &&
1193 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_16MHZ))
1194 			goto nla_put_failure;
1195 		if ((chan->flags & IEEE80211_CHAN_NO_320MHZ) &&
1196 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_320MHZ))
1197 			goto nla_put_failure;
1198 		if ((chan->flags & IEEE80211_CHAN_NO_EHT) &&
1199 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_EHT))
1200 			goto nla_put_failure;
1201 	}
1202 
1203 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
1204 			DBM_TO_MBM(chan->max_power)))
1205 		goto nla_put_failure;
1206 
1207 	if (large) {
1208 		const struct ieee80211_reg_rule *rule =
1209 			freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1210 
1211 		if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
1212 			if (nl80211_msg_put_wmm_rules(msg, rule))
1213 				goto nla_put_failure;
1214 		}
1215 	}
1216 
1217 	return 0;
1218 
1219  nla_put_failure:
1220 	return -ENOBUFS;
1221 }
1222 
1223 static bool nl80211_put_txq_stats(struct sk_buff *msg,
1224 				  struct cfg80211_txq_stats *txqstats,
1225 				  int attrtype)
1226 {
1227 	struct nlattr *txqattr;
1228 
1229 #define PUT_TXQVAL_U32(attr, memb) do {					  \
1230 	if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&	  \
1231 	    nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
1232 		return false;						  \
1233 	} while (0)
1234 
1235 	txqattr = nla_nest_start_noflag(msg, attrtype);
1236 	if (!txqattr)
1237 		return false;
1238 
1239 	PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
1240 	PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
1241 	PUT_TXQVAL_U32(FLOWS, flows);
1242 	PUT_TXQVAL_U32(DROPS, drops);
1243 	PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
1244 	PUT_TXQVAL_U32(OVERLIMIT, overlimit);
1245 	PUT_TXQVAL_U32(OVERMEMORY, overmemory);
1246 	PUT_TXQVAL_U32(COLLISIONS, collisions);
1247 	PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
1248 	PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
1249 	PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
1250 	nla_nest_end(msg, txqattr);
1251 
1252 #undef PUT_TXQVAL_U32
1253 	return true;
1254 }
1255 
1256 /* netlink command implementations */
1257 
1258 /**
1259  * nl80211_link_id - return link ID
1260  * @attrs: attributes to look at
1261  *
1262  * Returns: the link ID or 0 if not given
1263  *
1264  * Note this function doesn't do any validation of the link
1265  * ID validity wrt. links that were actually added, so it must
1266  * be called only from ops with %NL80211_FLAG_MLO_VALID_LINK_ID
1267  * or if additional validation is done.
1268  */
1269 static unsigned int nl80211_link_id(struct nlattr **attrs)
1270 {
1271 	struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1272 
1273 	if (!linkid)
1274 		return 0;
1275 
1276 	return nla_get_u8(linkid);
1277 }
1278 
1279 static int nl80211_link_id_or_invalid(struct nlattr **attrs)
1280 {
1281 	struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1282 
1283 	if (!linkid)
1284 		return -1;
1285 
1286 	return nla_get_u8(linkid);
1287 }
1288 
1289 struct key_parse {
1290 	struct key_params p;
1291 	int idx;
1292 	int type;
1293 	bool def, defmgmt, defbeacon;
1294 	bool def_uni, def_multi;
1295 };
1296 
1297 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
1298 				 struct key_parse *k)
1299 {
1300 	struct nlattr *tb[NL80211_KEY_MAX + 1];
1301 	int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
1302 					      nl80211_key_policy,
1303 					      info->extack);
1304 	if (err)
1305 		return err;
1306 
1307 	k->def = !!tb[NL80211_KEY_DEFAULT];
1308 	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1309 	k->defbeacon = !!tb[NL80211_KEY_DEFAULT_BEACON];
1310 
1311 	if (k->def) {
1312 		k->def_uni = true;
1313 		k->def_multi = true;
1314 	}
1315 	if (k->defmgmt || k->defbeacon)
1316 		k->def_multi = true;
1317 
1318 	if (tb[NL80211_KEY_IDX])
1319 		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1320 
1321 	if (tb[NL80211_KEY_DATA]) {
1322 		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1323 		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1324 	}
1325 
1326 	if (tb[NL80211_KEY_SEQ]) {
1327 		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1328 		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1329 	}
1330 
1331 	if (tb[NL80211_KEY_CIPHER])
1332 		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1333 
1334 	if (tb[NL80211_KEY_TYPE])
1335 		k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1336 
1337 	if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1338 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1339 
1340 		err = nla_parse_nested_deprecated(kdt,
1341 						  NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1342 						  tb[NL80211_KEY_DEFAULT_TYPES],
1343 						  nl80211_key_default_policy,
1344 						  info->extack);
1345 		if (err)
1346 			return err;
1347 
1348 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1349 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1350 	}
1351 
1352 	if (tb[NL80211_KEY_MODE])
1353 		k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1354 
1355 	return 0;
1356 }
1357 
1358 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1359 {
1360 	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1361 		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1362 		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1363 	}
1364 
1365 	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1366 		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1367 		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1368 	}
1369 
1370 	if (info->attrs[NL80211_ATTR_KEY_IDX])
1371 		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1372 
1373 	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1374 		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1375 
1376 	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1377 	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1378 
1379 	if (k->def) {
1380 		k->def_uni = true;
1381 		k->def_multi = true;
1382 	}
1383 	if (k->defmgmt)
1384 		k->def_multi = true;
1385 
1386 	if (info->attrs[NL80211_ATTR_KEY_TYPE])
1387 		k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1388 
1389 	if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1390 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1391 		int err = nla_parse_nested_deprecated(kdt,
1392 						      NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1393 						      info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1394 						      nl80211_key_default_policy,
1395 						      info->extack);
1396 		if (err)
1397 			return err;
1398 
1399 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1400 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1401 	}
1402 
1403 	return 0;
1404 }
1405 
1406 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1407 {
1408 	int err;
1409 
1410 	memset(k, 0, sizeof(*k));
1411 	k->idx = -1;
1412 	k->type = -1;
1413 
1414 	if (info->attrs[NL80211_ATTR_KEY])
1415 		err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1416 	else
1417 		err = nl80211_parse_key_old(info, k);
1418 
1419 	if (err)
1420 		return err;
1421 
1422 	if ((k->def ? 1 : 0) + (k->defmgmt ? 1 : 0) +
1423 	    (k->defbeacon ? 1 : 0) > 1) {
1424 		GENL_SET_ERR_MSG(info,
1425 				 "key with multiple default flags is invalid");
1426 		return -EINVAL;
1427 	}
1428 
1429 	if (k->defmgmt || k->defbeacon) {
1430 		if (k->def_uni || !k->def_multi) {
1431 			GENL_SET_ERR_MSG(info,
1432 					 "defmgmt/defbeacon key must be mcast");
1433 			return -EINVAL;
1434 		}
1435 	}
1436 
1437 	if (k->idx != -1) {
1438 		if (k->defmgmt) {
1439 			if (k->idx < 4 || k->idx > 5) {
1440 				GENL_SET_ERR_MSG(info,
1441 						 "defmgmt key idx not 4 or 5");
1442 				return -EINVAL;
1443 			}
1444 		} else if (k->defbeacon) {
1445 			if (k->idx < 6 || k->idx > 7) {
1446 				GENL_SET_ERR_MSG(info,
1447 						 "defbeacon key idx not 6 or 7");
1448 				return -EINVAL;
1449 			}
1450 		} else if (k->def) {
1451 			if (k->idx < 0 || k->idx > 3) {
1452 				GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1453 				return -EINVAL;
1454 			}
1455 		} else {
1456 			if (k->idx < 0 || k->idx > 7) {
1457 				GENL_SET_ERR_MSG(info, "key idx not 0-7");
1458 				return -EINVAL;
1459 			}
1460 		}
1461 	}
1462 
1463 	return 0;
1464 }
1465 
1466 static struct cfg80211_cached_keys *
1467 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1468 		       struct genl_info *info, bool *no_ht)
1469 {
1470 	struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1471 	struct key_parse parse;
1472 	struct nlattr *key;
1473 	struct cfg80211_cached_keys *result;
1474 	int rem, err, def = 0;
1475 	bool have_key = false;
1476 
1477 	nla_for_each_nested(key, keys, rem) {
1478 		have_key = true;
1479 		break;
1480 	}
1481 
1482 	if (!have_key)
1483 		return NULL;
1484 
1485 	result = kzalloc(sizeof(*result), GFP_KERNEL);
1486 	if (!result)
1487 		return ERR_PTR(-ENOMEM);
1488 
1489 	result->def = -1;
1490 
1491 	nla_for_each_nested(key, keys, rem) {
1492 		memset(&parse, 0, sizeof(parse));
1493 		parse.idx = -1;
1494 
1495 		err = nl80211_parse_key_new(info, key, &parse);
1496 		if (err)
1497 			goto error;
1498 		err = -EINVAL;
1499 		if (!parse.p.key)
1500 			goto error;
1501 		if (parse.idx < 0 || parse.idx > 3) {
1502 			GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1503 			goto error;
1504 		}
1505 		if (parse.def) {
1506 			if (def) {
1507 				GENL_SET_ERR_MSG(info,
1508 						 "only one key can be default");
1509 				goto error;
1510 			}
1511 			def = 1;
1512 			result->def = parse.idx;
1513 			if (!parse.def_uni || !parse.def_multi)
1514 				goto error;
1515 		} else if (parse.defmgmt)
1516 			goto error;
1517 		err = cfg80211_validate_key_settings(rdev, &parse.p,
1518 						     parse.idx, false, NULL);
1519 		if (err)
1520 			goto error;
1521 		if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1522 		    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1523 			GENL_SET_ERR_MSG(info, "connect key must be WEP");
1524 			err = -EINVAL;
1525 			goto error;
1526 		}
1527 		result->params[parse.idx].cipher = parse.p.cipher;
1528 		result->params[parse.idx].key_len = parse.p.key_len;
1529 		result->params[parse.idx].key = result->data[parse.idx];
1530 		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1531 
1532 		/* must be WEP key if we got here */
1533 		if (no_ht)
1534 			*no_ht = true;
1535 	}
1536 
1537 	if (result->def < 0) {
1538 		err = -EINVAL;
1539 		GENL_SET_ERR_MSG(info, "need a default/TX key");
1540 		goto error;
1541 	}
1542 
1543 	return result;
1544  error:
1545 	kfree(result);
1546 	return ERR_PTR(err);
1547 }
1548 
1549 static int nl80211_key_allowed(struct wireless_dev *wdev)
1550 {
1551 	lockdep_assert_wiphy(wdev->wiphy);
1552 
1553 	switch (wdev->iftype) {
1554 	case NL80211_IFTYPE_AP:
1555 	case NL80211_IFTYPE_AP_VLAN:
1556 	case NL80211_IFTYPE_P2P_GO:
1557 	case NL80211_IFTYPE_MESH_POINT:
1558 		break;
1559 	case NL80211_IFTYPE_ADHOC:
1560 		if (wdev->u.ibss.current_bss)
1561 			return 0;
1562 		return -ENOLINK;
1563 	case NL80211_IFTYPE_STATION:
1564 	case NL80211_IFTYPE_P2P_CLIENT:
1565 		if (wdev->connected)
1566 			return 0;
1567 		return -ENOLINK;
1568 	case NL80211_IFTYPE_NAN:
1569 		if (wiphy_ext_feature_isset(wdev->wiphy,
1570 					    NL80211_EXT_FEATURE_SECURE_NAN))
1571 			return 0;
1572 		return -EINVAL;
1573 	case NL80211_IFTYPE_UNSPECIFIED:
1574 	case NL80211_IFTYPE_OCB:
1575 	case NL80211_IFTYPE_MONITOR:
1576 	case NL80211_IFTYPE_P2P_DEVICE:
1577 	case NL80211_IFTYPE_WDS:
1578 	case NUM_NL80211_IFTYPES:
1579 		return -EINVAL;
1580 	}
1581 
1582 	return 0;
1583 }
1584 
1585 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1586 							u32 freq)
1587 {
1588 	struct ieee80211_channel *chan;
1589 
1590 	chan = ieee80211_get_channel_khz(wiphy, freq);
1591 	if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1592 		return NULL;
1593 	return chan;
1594 }
1595 
1596 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1597 {
1598 	struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1599 	int i;
1600 
1601 	if (!nl_modes)
1602 		goto nla_put_failure;
1603 
1604 	i = 0;
1605 	while (ifmodes) {
1606 		if ((ifmodes & 1) && nla_put_flag(msg, i))
1607 			goto nla_put_failure;
1608 		ifmodes >>= 1;
1609 		i++;
1610 	}
1611 
1612 	nla_nest_end(msg, nl_modes);
1613 	return 0;
1614 
1615 nla_put_failure:
1616 	return -ENOBUFS;
1617 }
1618 
1619 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1620 					  struct sk_buff *msg,
1621 					  bool large)
1622 {
1623 	struct nlattr *nl_combis;
1624 	int i, j;
1625 
1626 	nl_combis = nla_nest_start_noflag(msg,
1627 					  NL80211_ATTR_INTERFACE_COMBINATIONS);
1628 	if (!nl_combis)
1629 		goto nla_put_failure;
1630 
1631 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1632 		const struct ieee80211_iface_combination *c;
1633 		struct nlattr *nl_combi, *nl_limits;
1634 
1635 		c = &wiphy->iface_combinations[i];
1636 
1637 		nl_combi = nla_nest_start_noflag(msg, i + 1);
1638 		if (!nl_combi)
1639 			goto nla_put_failure;
1640 
1641 		nl_limits = nla_nest_start_noflag(msg,
1642 						  NL80211_IFACE_COMB_LIMITS);
1643 		if (!nl_limits)
1644 			goto nla_put_failure;
1645 
1646 		for (j = 0; j < c->n_limits; j++) {
1647 			struct nlattr *nl_limit;
1648 
1649 			nl_limit = nla_nest_start_noflag(msg, j + 1);
1650 			if (!nl_limit)
1651 				goto nla_put_failure;
1652 			if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1653 					c->limits[j].max))
1654 				goto nla_put_failure;
1655 			if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1656 						c->limits[j].types))
1657 				goto nla_put_failure;
1658 			nla_nest_end(msg, nl_limit);
1659 		}
1660 
1661 		nla_nest_end(msg, nl_limits);
1662 
1663 		if (c->beacon_int_infra_match &&
1664 		    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1665 			goto nla_put_failure;
1666 		if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1667 				c->num_different_channels) ||
1668 		    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1669 				c->max_interfaces))
1670 			goto nla_put_failure;
1671 		if (large &&
1672 		    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1673 				c->radar_detect_widths) ||
1674 		     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1675 				c->radar_detect_regions)))
1676 			goto nla_put_failure;
1677 		if (c->beacon_int_min_gcd &&
1678 		    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1679 				c->beacon_int_min_gcd))
1680 			goto nla_put_failure;
1681 
1682 		nla_nest_end(msg, nl_combi);
1683 	}
1684 
1685 	nla_nest_end(msg, nl_combis);
1686 
1687 	return 0;
1688 nla_put_failure:
1689 	return -ENOBUFS;
1690 }
1691 
1692 #ifdef CONFIG_PM
1693 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1694 					struct sk_buff *msg)
1695 {
1696 	const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1697 	struct nlattr *nl_tcp;
1698 
1699 	if (!tcp)
1700 		return 0;
1701 
1702 	nl_tcp = nla_nest_start_noflag(msg,
1703 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1704 	if (!nl_tcp)
1705 		return -ENOBUFS;
1706 
1707 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1708 			tcp->data_payload_max))
1709 		return -ENOBUFS;
1710 
1711 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1712 			tcp->data_payload_max))
1713 		return -ENOBUFS;
1714 
1715 	if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1716 		return -ENOBUFS;
1717 
1718 	if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1719 				sizeof(*tcp->tok), tcp->tok))
1720 		return -ENOBUFS;
1721 
1722 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1723 			tcp->data_interval_max))
1724 		return -ENOBUFS;
1725 
1726 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1727 			tcp->wake_payload_max))
1728 		return -ENOBUFS;
1729 
1730 	nla_nest_end(msg, nl_tcp);
1731 	return 0;
1732 }
1733 
1734 static int nl80211_send_wowlan(struct sk_buff *msg,
1735 			       struct cfg80211_registered_device *rdev,
1736 			       bool large)
1737 {
1738 	struct nlattr *nl_wowlan;
1739 
1740 	if (!rdev->wiphy.wowlan)
1741 		return 0;
1742 
1743 	nl_wowlan = nla_nest_start_noflag(msg,
1744 					  NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1745 	if (!nl_wowlan)
1746 		return -ENOBUFS;
1747 
1748 	if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1749 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1750 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1751 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1752 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1753 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1754 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1755 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1756 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1757 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1758 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1759 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1760 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1761 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1762 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1763 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1764 		return -ENOBUFS;
1765 
1766 	if (rdev->wiphy.wowlan->n_patterns) {
1767 		struct nl80211_pattern_support pat = {
1768 			.max_patterns = rdev->wiphy.wowlan->n_patterns,
1769 			.min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1770 			.max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1771 			.max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1772 		};
1773 
1774 		if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1775 			    sizeof(pat), &pat))
1776 			return -ENOBUFS;
1777 	}
1778 
1779 	if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1780 	    nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1781 			rdev->wiphy.wowlan->max_nd_match_sets))
1782 		return -ENOBUFS;
1783 
1784 	if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1785 		return -ENOBUFS;
1786 
1787 	nla_nest_end(msg, nl_wowlan);
1788 
1789 	return 0;
1790 }
1791 #endif
1792 
1793 static int nl80211_send_coalesce(struct sk_buff *msg,
1794 				 struct cfg80211_registered_device *rdev)
1795 {
1796 	struct nl80211_coalesce_rule_support rule;
1797 
1798 	if (!rdev->wiphy.coalesce)
1799 		return 0;
1800 
1801 	rule.max_rules = rdev->wiphy.coalesce->n_rules;
1802 	rule.max_delay = rdev->wiphy.coalesce->max_delay;
1803 	rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1804 	rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1805 	rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1806 	rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1807 
1808 	if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1809 		return -ENOBUFS;
1810 
1811 	return 0;
1812 }
1813 
1814 static int
1815 nl80211_send_iftype_data(struct sk_buff *msg,
1816 			 const struct ieee80211_supported_band *sband,
1817 			 const struct ieee80211_sband_iftype_data *iftdata)
1818 {
1819 	const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1820 	const struct ieee80211_sta_eht_cap *eht_cap = &iftdata->eht_cap;
1821 
1822 	if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1823 				iftdata->types_mask))
1824 		return -ENOBUFS;
1825 
1826 	if (he_cap->has_he) {
1827 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1828 			    sizeof(he_cap->he_cap_elem.mac_cap_info),
1829 			    he_cap->he_cap_elem.mac_cap_info) ||
1830 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1831 			    sizeof(he_cap->he_cap_elem.phy_cap_info),
1832 			    he_cap->he_cap_elem.phy_cap_info) ||
1833 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1834 			    sizeof(he_cap->he_mcs_nss_supp),
1835 			    &he_cap->he_mcs_nss_supp) ||
1836 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1837 			    sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1838 			return -ENOBUFS;
1839 	}
1840 
1841 	if (eht_cap->has_eht && he_cap->has_he) {
1842 		u8 mcs_nss_size, ppe_thresh_size;
1843 		u16 ppe_thres_hdr;
1844 		bool is_ap;
1845 
1846 		is_ap = iftdata->types_mask & BIT(NL80211_IFTYPE_AP) ||
1847 			iftdata->types_mask & BIT(NL80211_IFTYPE_P2P_GO);
1848 
1849 		mcs_nss_size =
1850 			ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
1851 						   &eht_cap->eht_cap_elem,
1852 						   is_ap);
1853 
1854 		ppe_thres_hdr = get_unaligned_le16(&eht_cap->eht_ppe_thres[0]);
1855 		ppe_thresh_size =
1856 			ieee80211_eht_ppe_size(ppe_thres_hdr,
1857 					       eht_cap->eht_cap_elem.phy_cap_info);
1858 
1859 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MAC,
1860 			    sizeof(eht_cap->eht_cap_elem.mac_cap_info),
1861 			    eht_cap->eht_cap_elem.mac_cap_info) ||
1862 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY,
1863 			    sizeof(eht_cap->eht_cap_elem.phy_cap_info),
1864 			    eht_cap->eht_cap_elem.phy_cap_info) ||
1865 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MCS_SET,
1866 			    mcs_nss_size, &eht_cap->eht_mcs_nss_supp) ||
1867 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PPE,
1868 			    ppe_thresh_size, eht_cap->eht_ppe_thres))
1869 			return -ENOBUFS;
1870 	}
1871 
1872 	if (sband->band == NL80211_BAND_6GHZ &&
1873 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_6GHZ_CAPA,
1874 		    sizeof(iftdata->he_6ghz_capa),
1875 		    &iftdata->he_6ghz_capa))
1876 		return -ENOBUFS;
1877 
1878 	if (iftdata->vendor_elems.data && iftdata->vendor_elems.len &&
1879 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_VENDOR_ELEMS,
1880 		    iftdata->vendor_elems.len, iftdata->vendor_elems.data))
1881 		return -ENOBUFS;
1882 
1883 	return 0;
1884 }
1885 
1886 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1887 				      struct ieee80211_supported_band *sband,
1888 				      bool large)
1889 {
1890 	struct nlattr *nl_rates, *nl_rate;
1891 	struct ieee80211_rate *rate;
1892 	int i;
1893 
1894 	/* add HT info */
1895 	if (sband->ht_cap.ht_supported &&
1896 	    (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1897 		     sizeof(sband->ht_cap.mcs),
1898 		     &sband->ht_cap.mcs) ||
1899 	     nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1900 			 sband->ht_cap.cap) ||
1901 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1902 			sband->ht_cap.ampdu_factor) ||
1903 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1904 			sband->ht_cap.ampdu_density)))
1905 		return -ENOBUFS;
1906 
1907 	/* add VHT info */
1908 	if (sband->vht_cap.vht_supported &&
1909 	    (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1910 		     sizeof(sband->vht_cap.vht_mcs),
1911 		     &sband->vht_cap.vht_mcs) ||
1912 	     nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1913 			 sband->vht_cap.cap)))
1914 		return -ENOBUFS;
1915 
1916 	if (large && sband->n_iftype_data) {
1917 		struct nlattr *nl_iftype_data =
1918 			nla_nest_start_noflag(msg,
1919 					      NL80211_BAND_ATTR_IFTYPE_DATA);
1920 		const struct ieee80211_sband_iftype_data *iftd;
1921 		int err;
1922 
1923 		if (!nl_iftype_data)
1924 			return -ENOBUFS;
1925 
1926 		for_each_sband_iftype_data(sband, i, iftd) {
1927 			struct nlattr *iftdata;
1928 
1929 			iftdata = nla_nest_start_noflag(msg, i + 1);
1930 			if (!iftdata)
1931 				return -ENOBUFS;
1932 
1933 			err = nl80211_send_iftype_data(msg, sband, iftd);
1934 			if (err)
1935 				return err;
1936 
1937 			nla_nest_end(msg, iftdata);
1938 		}
1939 
1940 		nla_nest_end(msg, nl_iftype_data);
1941 	}
1942 
1943 	/* add EDMG info */
1944 	if (large && sband->edmg_cap.channels &&
1945 	    (nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_CHANNELS,
1946 		       sband->edmg_cap.channels) ||
1947 	    nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_BW_CONFIG,
1948 		       sband->edmg_cap.bw_config)))
1949 
1950 		return -ENOBUFS;
1951 
1952 	/* add bitrates */
1953 	nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1954 	if (!nl_rates)
1955 		return -ENOBUFS;
1956 
1957 	for (i = 0; i < sband->n_bitrates; i++) {
1958 		nl_rate = nla_nest_start_noflag(msg, i);
1959 		if (!nl_rate)
1960 			return -ENOBUFS;
1961 
1962 		rate = &sband->bitrates[i];
1963 		if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1964 				rate->bitrate))
1965 			return -ENOBUFS;
1966 		if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1967 		    nla_put_flag(msg,
1968 				 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1969 			return -ENOBUFS;
1970 
1971 		nla_nest_end(msg, nl_rate);
1972 	}
1973 
1974 	nla_nest_end(msg, nl_rates);
1975 
1976 	/* S1G capabilities */
1977 	if (sband->band == NL80211_BAND_S1GHZ && sband->s1g_cap.s1g &&
1978 	    (nla_put(msg, NL80211_BAND_ATTR_S1G_CAPA,
1979 		     sizeof(sband->s1g_cap.cap),
1980 		     sband->s1g_cap.cap) ||
1981 	     nla_put(msg, NL80211_BAND_ATTR_S1G_MCS_NSS_SET,
1982 		     sizeof(sband->s1g_cap.nss_mcs),
1983 		     sband->s1g_cap.nss_mcs)))
1984 		return -ENOBUFS;
1985 
1986 	return 0;
1987 }
1988 
1989 static int
1990 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1991 			 const struct ieee80211_txrx_stypes *mgmt_stypes)
1992 {
1993 	u16 stypes;
1994 	struct nlattr *nl_ftypes, *nl_ifs;
1995 	enum nl80211_iftype ift;
1996 	int i;
1997 
1998 	if (!mgmt_stypes)
1999 		return 0;
2000 
2001 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
2002 	if (!nl_ifs)
2003 		return -ENOBUFS;
2004 
2005 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
2006 		nl_ftypes = nla_nest_start_noflag(msg, ift);
2007 		if (!nl_ftypes)
2008 			return -ENOBUFS;
2009 		i = 0;
2010 		stypes = mgmt_stypes[ift].tx;
2011 		while (stypes) {
2012 			if ((stypes & 1) &&
2013 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
2014 					(i << 4) | IEEE80211_FTYPE_MGMT))
2015 				return -ENOBUFS;
2016 			stypes >>= 1;
2017 			i++;
2018 		}
2019 		nla_nest_end(msg, nl_ftypes);
2020 	}
2021 
2022 	nla_nest_end(msg, nl_ifs);
2023 
2024 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
2025 	if (!nl_ifs)
2026 		return -ENOBUFS;
2027 
2028 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
2029 		nl_ftypes = nla_nest_start_noflag(msg, ift);
2030 		if (!nl_ftypes)
2031 			return -ENOBUFS;
2032 		i = 0;
2033 		stypes = mgmt_stypes[ift].rx;
2034 		while (stypes) {
2035 			if ((stypes & 1) &&
2036 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
2037 					(i << 4) | IEEE80211_FTYPE_MGMT))
2038 				return -ENOBUFS;
2039 			stypes >>= 1;
2040 			i++;
2041 		}
2042 		nla_nest_end(msg, nl_ftypes);
2043 	}
2044 	nla_nest_end(msg, nl_ifs);
2045 
2046 	return 0;
2047 }
2048 
2049 #define CMD(op, n)							\
2050 	 do {								\
2051 		if (rdev->ops->op) {					\
2052 			i++;						\
2053 			if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) 	\
2054 				goto nla_put_failure;			\
2055 		}							\
2056 	} while (0)
2057 
2058 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
2059 					struct sk_buff *msg)
2060 {
2061 	int i = 0;
2062 
2063 	/*
2064 	 * do *NOT* add anything into this function, new things need to be
2065 	 * advertised only to new versions of userspace that can deal with
2066 	 * the split (and they can't possibly care about new features...
2067 	 */
2068 	CMD(add_virtual_intf, NEW_INTERFACE);
2069 	CMD(change_virtual_intf, SET_INTERFACE);
2070 	CMD(add_key, NEW_KEY);
2071 	CMD(start_ap, START_AP);
2072 	CMD(add_station, NEW_STATION);
2073 	CMD(add_mpath, NEW_MPATH);
2074 	CMD(update_mesh_config, SET_MESH_CONFIG);
2075 	CMD(change_bss, SET_BSS);
2076 	CMD(auth, AUTHENTICATE);
2077 	CMD(assoc, ASSOCIATE);
2078 	CMD(deauth, DEAUTHENTICATE);
2079 	CMD(disassoc, DISASSOCIATE);
2080 	CMD(join_ibss, JOIN_IBSS);
2081 	CMD(join_mesh, JOIN_MESH);
2082 	CMD(set_pmksa, SET_PMKSA);
2083 	CMD(del_pmksa, DEL_PMKSA);
2084 	CMD(flush_pmksa, FLUSH_PMKSA);
2085 	if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
2086 		CMD(remain_on_channel, REMAIN_ON_CHANNEL);
2087 	CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
2088 	CMD(mgmt_tx, FRAME);
2089 	CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
2090 	if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
2091 		i++;
2092 		if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
2093 			goto nla_put_failure;
2094 	}
2095 	if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
2096 	    rdev->ops->join_mesh) {
2097 		i++;
2098 		if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
2099 			goto nla_put_failure;
2100 	}
2101 	if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2102 		CMD(tdls_mgmt, TDLS_MGMT);
2103 		CMD(tdls_oper, TDLS_OPER);
2104 	}
2105 	if (rdev->wiphy.max_sched_scan_reqs)
2106 		CMD(sched_scan_start, START_SCHED_SCAN);
2107 	CMD(probe_client, PROBE_CLIENT);
2108 	CMD(set_noack_map, SET_NOACK_MAP);
2109 	if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
2110 		i++;
2111 		if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
2112 			goto nla_put_failure;
2113 	}
2114 	CMD(start_p2p_device, START_P2P_DEVICE);
2115 	CMD(set_mcast_rate, SET_MCAST_RATE);
2116 #ifdef CONFIG_NL80211_TESTMODE
2117 	CMD(testmode_cmd, TESTMODE);
2118 #endif
2119 
2120 	if (rdev->ops->connect || rdev->ops->auth) {
2121 		i++;
2122 		if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
2123 			goto nla_put_failure;
2124 	}
2125 
2126 	if (rdev->ops->disconnect || rdev->ops->deauth) {
2127 		i++;
2128 		if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
2129 			goto nla_put_failure;
2130 	}
2131 
2132 	return i;
2133  nla_put_failure:
2134 	return -ENOBUFS;
2135 }
2136 
2137 static int
2138 nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
2139 			   struct sk_buff *msg)
2140 {
2141 	struct nlattr *ftm;
2142 
2143 	if (!cap->ftm.supported)
2144 		return 0;
2145 
2146 	ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
2147 	if (!ftm)
2148 		return -ENOBUFS;
2149 
2150 	if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
2151 		return -ENOBUFS;
2152 	if (cap->ftm.non_asap &&
2153 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
2154 		return -ENOBUFS;
2155 	if (cap->ftm.request_lci &&
2156 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
2157 		return -ENOBUFS;
2158 	if (cap->ftm.request_civicloc &&
2159 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
2160 		return -ENOBUFS;
2161 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
2162 			cap->ftm.preambles))
2163 		return -ENOBUFS;
2164 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
2165 			cap->ftm.bandwidths))
2166 		return -ENOBUFS;
2167 	if (cap->ftm.max_bursts_exponent >= 0 &&
2168 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
2169 			cap->ftm.max_bursts_exponent))
2170 		return -ENOBUFS;
2171 	if (cap->ftm.max_ftms_per_burst &&
2172 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
2173 			cap->ftm.max_ftms_per_burst))
2174 		return -ENOBUFS;
2175 	if (cap->ftm.trigger_based &&
2176 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_TRIGGER_BASED))
2177 		return -ENOBUFS;
2178 	if (cap->ftm.non_trigger_based &&
2179 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_TRIGGER_BASED))
2180 		return -ENOBUFS;
2181 
2182 	nla_nest_end(msg, ftm);
2183 	return 0;
2184 }
2185 
2186 static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
2187 				  struct sk_buff *msg)
2188 {
2189 	const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
2190 	struct nlattr *pmsr, *caps;
2191 
2192 	if (!cap)
2193 		return 0;
2194 
2195 	/*
2196 	 * we don't need to clean up anything here since the caller
2197 	 * will genlmsg_cancel() if we fail
2198 	 */
2199 
2200 	pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
2201 	if (!pmsr)
2202 		return -ENOBUFS;
2203 
2204 	if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
2205 		return -ENOBUFS;
2206 
2207 	if (cap->report_ap_tsf &&
2208 	    nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
2209 		return -ENOBUFS;
2210 
2211 	if (cap->randomize_mac_addr &&
2212 	    nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
2213 		return -ENOBUFS;
2214 
2215 	caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
2216 	if (!caps)
2217 		return -ENOBUFS;
2218 
2219 	if (nl80211_send_pmsr_ftm_capa(cap, msg))
2220 		return -ENOBUFS;
2221 
2222 	nla_nest_end(msg, caps);
2223 	nla_nest_end(msg, pmsr);
2224 
2225 	return 0;
2226 }
2227 
2228 static int
2229 nl80211_put_iftype_akm_suites(struct cfg80211_registered_device *rdev,
2230 			      struct sk_buff *msg)
2231 {
2232 	int i;
2233 	struct nlattr *nested, *nested_akms;
2234 	const struct wiphy_iftype_akm_suites *iftype_akms;
2235 
2236 	if (!rdev->wiphy.num_iftype_akm_suites ||
2237 	    !rdev->wiphy.iftype_akm_suites)
2238 		return 0;
2239 
2240 	nested = nla_nest_start(msg, NL80211_ATTR_IFTYPE_AKM_SUITES);
2241 	if (!nested)
2242 		return -ENOBUFS;
2243 
2244 	for (i = 0; i < rdev->wiphy.num_iftype_akm_suites; i++) {
2245 		nested_akms = nla_nest_start(msg, i + 1);
2246 		if (!nested_akms)
2247 			return -ENOBUFS;
2248 
2249 		iftype_akms = &rdev->wiphy.iftype_akm_suites[i];
2250 
2251 		if (nl80211_put_iftypes(msg, NL80211_IFTYPE_AKM_ATTR_IFTYPES,
2252 					iftype_akms->iftypes_mask))
2253 			return -ENOBUFS;
2254 
2255 		if (nla_put(msg, NL80211_IFTYPE_AKM_ATTR_SUITES,
2256 			    sizeof(u32) * iftype_akms->n_akm_suites,
2257 			    iftype_akms->akm_suites)) {
2258 			return -ENOBUFS;
2259 		}
2260 		nla_nest_end(msg, nested_akms);
2261 	}
2262 
2263 	nla_nest_end(msg, nested);
2264 
2265 	return 0;
2266 }
2267 
2268 static int
2269 nl80211_put_tid_config_support(struct cfg80211_registered_device *rdev,
2270 			       struct sk_buff *msg)
2271 {
2272 	struct nlattr *supp;
2273 
2274 	if (!rdev->wiphy.tid_config_support.vif &&
2275 	    !rdev->wiphy.tid_config_support.peer)
2276 		return 0;
2277 
2278 	supp = nla_nest_start(msg, NL80211_ATTR_TID_CONFIG);
2279 	if (!supp)
2280 		return -ENOSPC;
2281 
2282 	if (rdev->wiphy.tid_config_support.vif &&
2283 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_VIF_SUPP,
2284 			      rdev->wiphy.tid_config_support.vif,
2285 			      NL80211_TID_CONFIG_ATTR_PAD))
2286 		goto fail;
2287 
2288 	if (rdev->wiphy.tid_config_support.peer &&
2289 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_PEER_SUPP,
2290 			      rdev->wiphy.tid_config_support.peer,
2291 			      NL80211_TID_CONFIG_ATTR_PAD))
2292 		goto fail;
2293 
2294 	/* for now we just use the same value ... makes more sense */
2295 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_SHORT,
2296 		       rdev->wiphy.tid_config_support.max_retry))
2297 		goto fail;
2298 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_LONG,
2299 		       rdev->wiphy.tid_config_support.max_retry))
2300 		goto fail;
2301 
2302 	nla_nest_end(msg, supp);
2303 
2304 	return 0;
2305 fail:
2306 	nla_nest_cancel(msg, supp);
2307 	return -ENOBUFS;
2308 }
2309 
2310 static int
2311 nl80211_put_sar_specs(struct cfg80211_registered_device *rdev,
2312 		      struct sk_buff *msg)
2313 {
2314 	struct nlattr *sar_capa, *specs, *sub_freq_range;
2315 	u8 num_freq_ranges;
2316 	int i;
2317 
2318 	if (!rdev->wiphy.sar_capa)
2319 		return 0;
2320 
2321 	num_freq_ranges = rdev->wiphy.sar_capa->num_freq_ranges;
2322 
2323 	sar_capa = nla_nest_start(msg, NL80211_ATTR_SAR_SPEC);
2324 	if (!sar_capa)
2325 		return -ENOSPC;
2326 
2327 	if (nla_put_u32(msg, NL80211_SAR_ATTR_TYPE, rdev->wiphy.sar_capa->type))
2328 		goto fail;
2329 
2330 	specs = nla_nest_start(msg, NL80211_SAR_ATTR_SPECS);
2331 	if (!specs)
2332 		goto fail;
2333 
2334 	/* report supported freq_ranges */
2335 	for (i = 0; i < num_freq_ranges; i++) {
2336 		sub_freq_range = nla_nest_start(msg, i + 1);
2337 		if (!sub_freq_range)
2338 			goto fail;
2339 
2340 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_START_FREQ,
2341 				rdev->wiphy.sar_capa->freq_ranges[i].start_freq))
2342 			goto fail;
2343 
2344 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_END_FREQ,
2345 				rdev->wiphy.sar_capa->freq_ranges[i].end_freq))
2346 			goto fail;
2347 
2348 		nla_nest_end(msg, sub_freq_range);
2349 	}
2350 
2351 	nla_nest_end(msg, specs);
2352 	nla_nest_end(msg, sar_capa);
2353 
2354 	return 0;
2355 fail:
2356 	nla_nest_cancel(msg, sar_capa);
2357 	return -ENOBUFS;
2358 }
2359 
2360 static int nl80211_put_mbssid_support(struct wiphy *wiphy, struct sk_buff *msg)
2361 {
2362 	struct nlattr *config;
2363 
2364 	if (!wiphy->mbssid_max_interfaces)
2365 		return 0;
2366 
2367 	config = nla_nest_start(msg, NL80211_ATTR_MBSSID_CONFIG);
2368 	if (!config)
2369 		return -ENOBUFS;
2370 
2371 	if (nla_put_u8(msg, NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES,
2372 		       wiphy->mbssid_max_interfaces))
2373 		goto fail;
2374 
2375 	if (wiphy->ema_max_profile_periodicity &&
2376 	    nla_put_u8(msg,
2377 		       NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY,
2378 		       wiphy->ema_max_profile_periodicity))
2379 		goto fail;
2380 
2381 	nla_nest_end(msg, config);
2382 	return 0;
2383 
2384 fail:
2385 	nla_nest_cancel(msg, config);
2386 	return -ENOBUFS;
2387 }
2388 
2389 struct nl80211_dump_wiphy_state {
2390 	s64 filter_wiphy;
2391 	long start;
2392 	long split_start, band_start, chan_start, capa_start;
2393 	bool split;
2394 };
2395 
2396 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
2397 			      enum nl80211_commands cmd,
2398 			      struct sk_buff *msg, u32 portid, u32 seq,
2399 			      int flags, struct nl80211_dump_wiphy_state *state)
2400 {
2401 	void *hdr;
2402 	struct nlattr *nl_bands, *nl_band;
2403 	struct nlattr *nl_freqs, *nl_freq;
2404 	struct nlattr *nl_cmds;
2405 	enum nl80211_band band;
2406 	struct ieee80211_channel *chan;
2407 	int i;
2408 	const struct ieee80211_txrx_stypes *mgmt_stypes =
2409 				rdev->wiphy.mgmt_stypes;
2410 	u32 features;
2411 
2412 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2413 	if (!hdr)
2414 		return -ENOBUFS;
2415 
2416 	if (WARN_ON(!state))
2417 		return -EINVAL;
2418 
2419 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2420 	    nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
2421 			   wiphy_name(&rdev->wiphy)) ||
2422 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
2423 			cfg80211_rdev_list_generation))
2424 		goto nla_put_failure;
2425 
2426 	if (cmd != NL80211_CMD_NEW_WIPHY)
2427 		goto finish;
2428 
2429 	switch (state->split_start) {
2430 	case 0:
2431 		if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
2432 			       rdev->wiphy.retry_short) ||
2433 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
2434 			       rdev->wiphy.retry_long) ||
2435 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
2436 				rdev->wiphy.frag_threshold) ||
2437 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
2438 				rdev->wiphy.rts_threshold) ||
2439 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
2440 			       rdev->wiphy.coverage_class) ||
2441 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
2442 			       rdev->wiphy.max_scan_ssids) ||
2443 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
2444 			       rdev->wiphy.max_sched_scan_ssids) ||
2445 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
2446 				rdev->wiphy.max_scan_ie_len) ||
2447 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
2448 				rdev->wiphy.max_sched_scan_ie_len) ||
2449 		    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
2450 			       rdev->wiphy.max_match_sets))
2451 			goto nla_put_failure;
2452 
2453 		if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
2454 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
2455 			goto nla_put_failure;
2456 		if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
2457 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
2458 			goto nla_put_failure;
2459 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2460 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
2461 			goto nla_put_failure;
2462 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
2463 		    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
2464 			goto nla_put_failure;
2465 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2466 		    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
2467 			goto nla_put_failure;
2468 		if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
2469 		    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
2470 			goto nla_put_failure;
2471 		state->split_start++;
2472 		if (state->split)
2473 			break;
2474 		fallthrough;
2475 	case 1:
2476 		if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
2477 			    sizeof(u32) * rdev->wiphy.n_cipher_suites,
2478 			    rdev->wiphy.cipher_suites))
2479 			goto nla_put_failure;
2480 
2481 		if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
2482 			       rdev->wiphy.max_num_pmkids))
2483 			goto nla_put_failure;
2484 
2485 		if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
2486 		    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
2487 			goto nla_put_failure;
2488 
2489 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
2490 				rdev->wiphy.available_antennas_tx) ||
2491 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
2492 				rdev->wiphy.available_antennas_rx))
2493 			goto nla_put_failure;
2494 
2495 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
2496 		    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
2497 				rdev->wiphy.probe_resp_offload))
2498 			goto nla_put_failure;
2499 
2500 		if ((rdev->wiphy.available_antennas_tx ||
2501 		     rdev->wiphy.available_antennas_rx) &&
2502 		    rdev->ops->get_antenna) {
2503 			u32 tx_ant = 0, rx_ant = 0;
2504 			int res;
2505 
2506 			res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
2507 			if (!res) {
2508 				if (nla_put_u32(msg,
2509 						NL80211_ATTR_WIPHY_ANTENNA_TX,
2510 						tx_ant) ||
2511 				    nla_put_u32(msg,
2512 						NL80211_ATTR_WIPHY_ANTENNA_RX,
2513 						rx_ant))
2514 					goto nla_put_failure;
2515 			}
2516 		}
2517 
2518 		state->split_start++;
2519 		if (state->split)
2520 			break;
2521 		fallthrough;
2522 	case 2:
2523 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
2524 					rdev->wiphy.interface_modes))
2525 				goto nla_put_failure;
2526 		state->split_start++;
2527 		if (state->split)
2528 			break;
2529 		fallthrough;
2530 	case 3:
2531 		nl_bands = nla_nest_start_noflag(msg,
2532 						 NL80211_ATTR_WIPHY_BANDS);
2533 		if (!nl_bands)
2534 			goto nla_put_failure;
2535 
2536 		for (band = state->band_start;
2537 		     band < (state->split ?
2538 				NUM_NL80211_BANDS :
2539 				NL80211_BAND_60GHZ + 1);
2540 		     band++) {
2541 			struct ieee80211_supported_band *sband;
2542 
2543 			/* omit higher bands for ancient software */
2544 			if (band > NL80211_BAND_5GHZ && !state->split)
2545 				break;
2546 
2547 			sband = rdev->wiphy.bands[band];
2548 
2549 			if (!sband)
2550 				continue;
2551 
2552 			nl_band = nla_nest_start_noflag(msg, band);
2553 			if (!nl_band)
2554 				goto nla_put_failure;
2555 
2556 			switch (state->chan_start) {
2557 			case 0:
2558 				if (nl80211_send_band_rateinfo(msg, sband,
2559 							       state->split))
2560 					goto nla_put_failure;
2561 				state->chan_start++;
2562 				if (state->split)
2563 					break;
2564 				fallthrough;
2565 			default:
2566 				/* add frequencies */
2567 				nl_freqs = nla_nest_start_noflag(msg,
2568 								 NL80211_BAND_ATTR_FREQS);
2569 				if (!nl_freqs)
2570 					goto nla_put_failure;
2571 
2572 				for (i = state->chan_start - 1;
2573 				     i < sband->n_channels;
2574 				     i++) {
2575 					nl_freq = nla_nest_start_noflag(msg,
2576 									i);
2577 					if (!nl_freq)
2578 						goto nla_put_failure;
2579 
2580 					chan = &sband->channels[i];
2581 
2582 					if (nl80211_msg_put_channel(
2583 							msg, &rdev->wiphy, chan,
2584 							state->split))
2585 						goto nla_put_failure;
2586 
2587 					nla_nest_end(msg, nl_freq);
2588 					if (state->split)
2589 						break;
2590 				}
2591 				if (i < sband->n_channels)
2592 					state->chan_start = i + 2;
2593 				else
2594 					state->chan_start = 0;
2595 				nla_nest_end(msg, nl_freqs);
2596 			}
2597 
2598 			nla_nest_end(msg, nl_band);
2599 
2600 			if (state->split) {
2601 				/* start again here */
2602 				if (state->chan_start)
2603 					band--;
2604 				break;
2605 			}
2606 		}
2607 		nla_nest_end(msg, nl_bands);
2608 
2609 		if (band < NUM_NL80211_BANDS)
2610 			state->band_start = band + 1;
2611 		else
2612 			state->band_start = 0;
2613 
2614 		/* if bands & channels are done, continue outside */
2615 		if (state->band_start == 0 && state->chan_start == 0)
2616 			state->split_start++;
2617 		if (state->split)
2618 			break;
2619 		fallthrough;
2620 	case 4:
2621 		nl_cmds = nla_nest_start_noflag(msg,
2622 						NL80211_ATTR_SUPPORTED_COMMANDS);
2623 		if (!nl_cmds)
2624 			goto nla_put_failure;
2625 
2626 		i = nl80211_add_commands_unsplit(rdev, msg);
2627 		if (i < 0)
2628 			goto nla_put_failure;
2629 		if (state->split) {
2630 			CMD(crit_proto_start, CRIT_PROTOCOL_START);
2631 			CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2632 			if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2633 				CMD(channel_switch, CHANNEL_SWITCH);
2634 			CMD(set_qos_map, SET_QOS_MAP);
2635 			if (rdev->wiphy.features &
2636 					NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2637 				CMD(add_tx_ts, ADD_TX_TS);
2638 			CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2639 			CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2640 			CMD(update_ft_ies, UPDATE_FT_IES);
2641 			if (rdev->wiphy.sar_capa)
2642 				CMD(set_sar_specs, SET_SAR_SPECS);
2643 		}
2644 #undef CMD
2645 
2646 		nla_nest_end(msg, nl_cmds);
2647 		state->split_start++;
2648 		if (state->split)
2649 			break;
2650 		fallthrough;
2651 	case 5:
2652 		if (rdev->ops->remain_on_channel &&
2653 		    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2654 		    nla_put_u32(msg,
2655 				NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2656 				rdev->wiphy.max_remain_on_channel_duration))
2657 			goto nla_put_failure;
2658 
2659 		if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2660 		    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2661 			goto nla_put_failure;
2662 
2663 		state->split_start++;
2664 		if (state->split)
2665 			break;
2666 		fallthrough;
2667 	case 6:
2668 #ifdef CONFIG_PM
2669 		if (nl80211_send_wowlan(msg, rdev, state->split))
2670 			goto nla_put_failure;
2671 		state->split_start++;
2672 		if (state->split)
2673 			break;
2674 #else
2675 		state->split_start++;
2676 #endif
2677 		fallthrough;
2678 	case 7:
2679 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2680 					rdev->wiphy.software_iftypes))
2681 			goto nla_put_failure;
2682 
2683 		if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2684 						   state->split))
2685 			goto nla_put_failure;
2686 
2687 		state->split_start++;
2688 		if (state->split)
2689 			break;
2690 		fallthrough;
2691 	case 8:
2692 		if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2693 		    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2694 				rdev->wiphy.ap_sme_capa))
2695 			goto nla_put_failure;
2696 
2697 		features = rdev->wiphy.features;
2698 		/*
2699 		 * We can only add the per-channel limit information if the
2700 		 * dump is split, otherwise it makes it too big. Therefore
2701 		 * only advertise it in that case.
2702 		 */
2703 		if (state->split)
2704 			features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2705 		if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2706 			goto nla_put_failure;
2707 
2708 		if (rdev->wiphy.ht_capa_mod_mask &&
2709 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2710 			    sizeof(*rdev->wiphy.ht_capa_mod_mask),
2711 			    rdev->wiphy.ht_capa_mod_mask))
2712 			goto nla_put_failure;
2713 
2714 		if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2715 		    rdev->wiphy.max_acl_mac_addrs &&
2716 		    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2717 				rdev->wiphy.max_acl_mac_addrs))
2718 			goto nla_put_failure;
2719 
2720 		/*
2721 		 * Any information below this point is only available to
2722 		 * applications that can deal with it being split. This
2723 		 * helps ensure that newly added capabilities don't break
2724 		 * older tools by overrunning their buffers.
2725 		 *
2726 		 * We still increment split_start so that in the split
2727 		 * case we'll continue with more data in the next round,
2728 		 * but break unconditionally so unsplit data stops here.
2729 		 */
2730 		if (state->split)
2731 			state->split_start++;
2732 		else
2733 			state->split_start = 0;
2734 		break;
2735 	case 9:
2736 		if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2737 			goto nla_put_failure;
2738 
2739 		if (nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
2740 				rdev->wiphy.max_sched_scan_plans) ||
2741 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
2742 				rdev->wiphy.max_sched_scan_plan_interval) ||
2743 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
2744 				rdev->wiphy.max_sched_scan_plan_iterations))
2745 			goto nla_put_failure;
2746 
2747 		if (rdev->wiphy.extended_capabilities &&
2748 		    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2749 			     rdev->wiphy.extended_capabilities_len,
2750 			     rdev->wiphy.extended_capabilities) ||
2751 		     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2752 			     rdev->wiphy.extended_capabilities_len,
2753 			     rdev->wiphy.extended_capabilities_mask)))
2754 			goto nla_put_failure;
2755 
2756 		if (rdev->wiphy.vht_capa_mod_mask &&
2757 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2758 			    sizeof(*rdev->wiphy.vht_capa_mod_mask),
2759 			    rdev->wiphy.vht_capa_mod_mask))
2760 			goto nla_put_failure;
2761 
2762 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2763 			    rdev->wiphy.perm_addr))
2764 			goto nla_put_failure;
2765 
2766 		if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2767 		    nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2768 			    rdev->wiphy.addr_mask))
2769 			goto nla_put_failure;
2770 
2771 		if (rdev->wiphy.n_addresses > 1) {
2772 			void *attr;
2773 
2774 			attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2775 			if (!attr)
2776 				goto nla_put_failure;
2777 
2778 			for (i = 0; i < rdev->wiphy.n_addresses; i++)
2779 				if (nla_put(msg, i + 1, ETH_ALEN,
2780 					    rdev->wiphy.addresses[i].addr))
2781 					goto nla_put_failure;
2782 
2783 			nla_nest_end(msg, attr);
2784 		}
2785 
2786 		state->split_start++;
2787 		break;
2788 	case 10:
2789 		if (nl80211_send_coalesce(msg, rdev))
2790 			goto nla_put_failure;
2791 
2792 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2793 		    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2794 		     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2795 			goto nla_put_failure;
2796 
2797 		if (rdev->wiphy.max_ap_assoc_sta &&
2798 		    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2799 				rdev->wiphy.max_ap_assoc_sta))
2800 			goto nla_put_failure;
2801 
2802 		state->split_start++;
2803 		break;
2804 	case 11:
2805 		if (rdev->wiphy.n_vendor_commands) {
2806 			const struct nl80211_vendor_cmd_info *info;
2807 			struct nlattr *nested;
2808 
2809 			nested = nla_nest_start_noflag(msg,
2810 						       NL80211_ATTR_VENDOR_DATA);
2811 			if (!nested)
2812 				goto nla_put_failure;
2813 
2814 			for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2815 				info = &rdev->wiphy.vendor_commands[i].info;
2816 				if (nla_put(msg, i + 1, sizeof(*info), info))
2817 					goto nla_put_failure;
2818 			}
2819 			nla_nest_end(msg, nested);
2820 		}
2821 
2822 		if (rdev->wiphy.n_vendor_events) {
2823 			const struct nl80211_vendor_cmd_info *info;
2824 			struct nlattr *nested;
2825 
2826 			nested = nla_nest_start_noflag(msg,
2827 						       NL80211_ATTR_VENDOR_EVENTS);
2828 			if (!nested)
2829 				goto nla_put_failure;
2830 
2831 			for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2832 				info = &rdev->wiphy.vendor_events[i];
2833 				if (nla_put(msg, i + 1, sizeof(*info), info))
2834 					goto nla_put_failure;
2835 			}
2836 			nla_nest_end(msg, nested);
2837 		}
2838 		state->split_start++;
2839 		break;
2840 	case 12:
2841 		if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2842 		    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2843 			       rdev->wiphy.max_num_csa_counters))
2844 			goto nla_put_failure;
2845 
2846 		if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2847 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2848 			goto nla_put_failure;
2849 
2850 		if (rdev->wiphy.max_sched_scan_reqs &&
2851 		    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2852 				rdev->wiphy.max_sched_scan_reqs))
2853 			goto nla_put_failure;
2854 
2855 		if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2856 			    sizeof(rdev->wiphy.ext_features),
2857 			    rdev->wiphy.ext_features))
2858 			goto nla_put_failure;
2859 
2860 		if (rdev->wiphy.bss_select_support) {
2861 			struct nlattr *nested;
2862 			u32 bss_select_support = rdev->wiphy.bss_select_support;
2863 
2864 			nested = nla_nest_start_noflag(msg,
2865 						       NL80211_ATTR_BSS_SELECT);
2866 			if (!nested)
2867 				goto nla_put_failure;
2868 
2869 			i = 0;
2870 			while (bss_select_support) {
2871 				if ((bss_select_support & 1) &&
2872 				    nla_put_flag(msg, i))
2873 					goto nla_put_failure;
2874 				i++;
2875 				bss_select_support >>= 1;
2876 			}
2877 			nla_nest_end(msg, nested);
2878 		}
2879 
2880 		state->split_start++;
2881 		break;
2882 	case 13:
2883 		if (rdev->wiphy.num_iftype_ext_capab &&
2884 		    rdev->wiphy.iftype_ext_capab) {
2885 			struct nlattr *nested_ext_capab, *nested;
2886 
2887 			nested = nla_nest_start_noflag(msg,
2888 						       NL80211_ATTR_IFTYPE_EXT_CAPA);
2889 			if (!nested)
2890 				goto nla_put_failure;
2891 
2892 			for (i = state->capa_start;
2893 			     i < rdev->wiphy.num_iftype_ext_capab; i++) {
2894 				const struct wiphy_iftype_ext_capab *capab;
2895 
2896 				capab = &rdev->wiphy.iftype_ext_capab[i];
2897 
2898 				nested_ext_capab = nla_nest_start_noflag(msg,
2899 									 i);
2900 				if (!nested_ext_capab ||
2901 				    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2902 						capab->iftype) ||
2903 				    nla_put(msg, NL80211_ATTR_EXT_CAPA,
2904 					    capab->extended_capabilities_len,
2905 					    capab->extended_capabilities) ||
2906 				    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2907 					    capab->extended_capabilities_len,
2908 					    capab->extended_capabilities_mask))
2909 					goto nla_put_failure;
2910 
2911 				if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO &&
2912 				    (nla_put_u16(msg,
2913 						 NL80211_ATTR_EML_CAPABILITY,
2914 						 capab->eml_capabilities) ||
2915 				     nla_put_u16(msg,
2916 						 NL80211_ATTR_MLD_CAPA_AND_OPS,
2917 						 capab->mld_capa_and_ops)))
2918 					goto nla_put_failure;
2919 
2920 				nla_nest_end(msg, nested_ext_capab);
2921 				if (state->split)
2922 					break;
2923 			}
2924 			nla_nest_end(msg, nested);
2925 			if (i < rdev->wiphy.num_iftype_ext_capab) {
2926 				state->capa_start = i + 1;
2927 				break;
2928 			}
2929 		}
2930 
2931 		if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2932 				rdev->wiphy.nan_supported_bands))
2933 			goto nla_put_failure;
2934 
2935 		if (wiphy_ext_feature_isset(&rdev->wiphy,
2936 					    NL80211_EXT_FEATURE_TXQS)) {
2937 			struct cfg80211_txq_stats txqstats = {};
2938 			int res;
2939 
2940 			res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2941 			if (!res &&
2942 			    !nl80211_put_txq_stats(msg, &txqstats,
2943 						   NL80211_ATTR_TXQ_STATS))
2944 				goto nla_put_failure;
2945 
2946 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2947 					rdev->wiphy.txq_limit))
2948 				goto nla_put_failure;
2949 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2950 					rdev->wiphy.txq_memory_limit))
2951 				goto nla_put_failure;
2952 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2953 					rdev->wiphy.txq_quantum))
2954 				goto nla_put_failure;
2955 		}
2956 
2957 		state->split_start++;
2958 		break;
2959 	case 14:
2960 		if (nl80211_send_pmsr_capa(rdev, msg))
2961 			goto nla_put_failure;
2962 
2963 		state->split_start++;
2964 		break;
2965 	case 15:
2966 		if (rdev->wiphy.akm_suites &&
2967 		    nla_put(msg, NL80211_ATTR_AKM_SUITES,
2968 			    sizeof(u32) * rdev->wiphy.n_akm_suites,
2969 			    rdev->wiphy.akm_suites))
2970 			goto nla_put_failure;
2971 
2972 		if (nl80211_put_iftype_akm_suites(rdev, msg))
2973 			goto nla_put_failure;
2974 
2975 		if (nl80211_put_tid_config_support(rdev, msg))
2976 			goto nla_put_failure;
2977 		state->split_start++;
2978 		break;
2979 	case 16:
2980 		if (nl80211_put_sar_specs(rdev, msg))
2981 			goto nla_put_failure;
2982 
2983 		if (nl80211_put_mbssid_support(&rdev->wiphy, msg))
2984 			goto nla_put_failure;
2985 
2986 		if (nla_put_u16(msg, NL80211_ATTR_MAX_NUM_AKM_SUITES,
2987 				rdev->wiphy.max_num_akm_suites))
2988 			goto nla_put_failure;
2989 
2990 		if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO)
2991 			nla_put_flag(msg, NL80211_ATTR_MLO_SUPPORT);
2992 
2993 		if (rdev->wiphy.hw_timestamp_max_peers &&
2994 		    nla_put_u16(msg, NL80211_ATTR_MAX_HW_TIMESTAMP_PEERS,
2995 				rdev->wiphy.hw_timestamp_max_peers))
2996 			goto nla_put_failure;
2997 
2998 		/* done */
2999 		state->split_start = 0;
3000 		break;
3001 	}
3002  finish:
3003 	genlmsg_end(msg, hdr);
3004 	return 0;
3005 
3006  nla_put_failure:
3007 	genlmsg_cancel(msg, hdr);
3008 	return -EMSGSIZE;
3009 }
3010 
3011 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
3012 				    struct netlink_callback *cb,
3013 				    struct nl80211_dump_wiphy_state *state)
3014 {
3015 	struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
3016 	int ret;
3017 
3018 	if (!tb)
3019 		return -ENOMEM;
3020 
3021 	ret = nlmsg_parse_deprecated(cb->nlh,
3022 				     GENL_HDRLEN + nl80211_fam.hdrsize,
3023 				     tb, nl80211_fam.maxattr,
3024 				     nl80211_policy, NULL);
3025 	/* ignore parse errors for backward compatibility */
3026 	if (ret) {
3027 		ret = 0;
3028 		goto out;
3029 	}
3030 
3031 	state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
3032 	if (tb[NL80211_ATTR_WIPHY])
3033 		state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
3034 	if (tb[NL80211_ATTR_WDEV])
3035 		state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
3036 	if (tb[NL80211_ATTR_IFINDEX]) {
3037 		struct net_device *netdev;
3038 		struct cfg80211_registered_device *rdev;
3039 		int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3040 
3041 		netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
3042 		if (!netdev) {
3043 			ret = -ENODEV;
3044 			goto out;
3045 		}
3046 		if (netdev->ieee80211_ptr) {
3047 			rdev = wiphy_to_rdev(
3048 				netdev->ieee80211_ptr->wiphy);
3049 			state->filter_wiphy = rdev->wiphy_idx;
3050 		}
3051 	}
3052 
3053 	ret = 0;
3054 out:
3055 	kfree(tb);
3056 	return ret;
3057 }
3058 
3059 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
3060 {
3061 	int idx = 0, ret;
3062 	struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
3063 	struct cfg80211_registered_device *rdev;
3064 
3065 	rtnl_lock();
3066 	if (!state) {
3067 		state = kzalloc(sizeof(*state), GFP_KERNEL);
3068 		if (!state) {
3069 			rtnl_unlock();
3070 			return -ENOMEM;
3071 		}
3072 		state->filter_wiphy = -1;
3073 		ret = nl80211_dump_wiphy_parse(skb, cb, state);
3074 		if (ret) {
3075 			kfree(state);
3076 			rtnl_unlock();
3077 			return ret;
3078 		}
3079 		cb->args[0] = (long)state;
3080 	}
3081 
3082 	for_each_rdev(rdev) {
3083 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3084 			continue;
3085 		if (++idx <= state->start)
3086 			continue;
3087 		if (state->filter_wiphy != -1 &&
3088 		    state->filter_wiphy != rdev->wiphy_idx)
3089 			continue;
3090 		wiphy_lock(&rdev->wiphy);
3091 		/* attempt to fit multiple wiphy data chunks into the skb */
3092 		do {
3093 			ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
3094 						 skb,
3095 						 NETLINK_CB(cb->skb).portid,
3096 						 cb->nlh->nlmsg_seq,
3097 						 NLM_F_MULTI, state);
3098 			if (ret < 0) {
3099 				/*
3100 				 * If sending the wiphy data didn't fit (ENOBUFS
3101 				 * or EMSGSIZE returned), this SKB is still
3102 				 * empty (so it's not too big because another
3103 				 * wiphy dataset is already in the skb) and
3104 				 * we've not tried to adjust the dump allocation
3105 				 * yet ... then adjust the alloc size to be
3106 				 * bigger, and return 1 but with the empty skb.
3107 				 * This results in an empty message being RX'ed
3108 				 * in userspace, but that is ignored.
3109 				 *
3110 				 * We can then retry with the larger buffer.
3111 				 */
3112 				if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
3113 				    !skb->len && !state->split &&
3114 				    cb->min_dump_alloc < 4096) {
3115 					cb->min_dump_alloc = 4096;
3116 					state->split_start = 0;
3117 					wiphy_unlock(&rdev->wiphy);
3118 					rtnl_unlock();
3119 					return 1;
3120 				}
3121 				idx--;
3122 				break;
3123 			}
3124 		} while (state->split_start > 0);
3125 		wiphy_unlock(&rdev->wiphy);
3126 		break;
3127 	}
3128 	rtnl_unlock();
3129 
3130 	state->start = idx;
3131 
3132 	return skb->len;
3133 }
3134 
3135 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
3136 {
3137 	kfree((void *)cb->args[0]);
3138 	return 0;
3139 }
3140 
3141 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
3142 {
3143 	struct sk_buff *msg;
3144 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3145 	struct nl80211_dump_wiphy_state state = {};
3146 
3147 	msg = nlmsg_new(4096, GFP_KERNEL);
3148 	if (!msg)
3149 		return -ENOMEM;
3150 
3151 	if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
3152 			       info->snd_portid, info->snd_seq, 0,
3153 			       &state) < 0) {
3154 		nlmsg_free(msg);
3155 		return -ENOBUFS;
3156 	}
3157 
3158 	return genlmsg_reply(msg, info);
3159 }
3160 
3161 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
3162 	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
3163 	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
3164 	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
3165 	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
3166 	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
3167 };
3168 
3169 static int parse_txq_params(struct nlattr *tb[],
3170 			    struct ieee80211_txq_params *txq_params)
3171 {
3172 	u8 ac;
3173 
3174 	if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
3175 	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
3176 	    !tb[NL80211_TXQ_ATTR_AIFS])
3177 		return -EINVAL;
3178 
3179 	ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
3180 	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
3181 	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
3182 	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
3183 	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
3184 
3185 	if (ac >= NL80211_NUM_ACS)
3186 		return -EINVAL;
3187 	txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
3188 	return 0;
3189 }
3190 
3191 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
3192 {
3193 	/*
3194 	 * You can only set the channel explicitly for some interfaces,
3195 	 * most have their channel managed via their respective
3196 	 * "establish a connection" command (connect, join, ...)
3197 	 *
3198 	 * For AP/GO and mesh mode, the channel can be set with the
3199 	 * channel userspace API, but is only stored and passed to the
3200 	 * low-level driver when the AP starts or the mesh is joined.
3201 	 * This is for backward compatibility, userspace can also give
3202 	 * the channel in the start-ap or join-mesh commands instead.
3203 	 *
3204 	 * Monitors are special as they are normally slaved to
3205 	 * whatever else is going on, so they have their own special
3206 	 * operation to set the monitor channel if possible.
3207 	 */
3208 	return !wdev ||
3209 		wdev->iftype == NL80211_IFTYPE_AP ||
3210 		wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
3211 		wdev->iftype == NL80211_IFTYPE_MONITOR ||
3212 		wdev->iftype == NL80211_IFTYPE_P2P_GO;
3213 }
3214 
3215 static int nl80211_parse_punct_bitmap(struct cfg80211_registered_device *rdev,
3216 				      struct genl_info *info,
3217 				      const struct cfg80211_chan_def *chandef,
3218 				      u16 *punct_bitmap)
3219 {
3220 	if (!wiphy_ext_feature_isset(&rdev->wiphy, NL80211_EXT_FEATURE_PUNCT))
3221 		return -EINVAL;
3222 
3223 	*punct_bitmap = nla_get_u32(info->attrs[NL80211_ATTR_PUNCT_BITMAP]);
3224 	if (!cfg80211_valid_disable_subchannel_bitmap(punct_bitmap, chandef))
3225 		return -EINVAL;
3226 
3227 	return 0;
3228 }
3229 
3230 int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
3231 			  struct genl_info *info,
3232 			  struct cfg80211_chan_def *chandef)
3233 {
3234 	struct netlink_ext_ack *extack = info->extack;
3235 	struct nlattr **attrs = info->attrs;
3236 	u32 control_freq;
3237 
3238 	if (!attrs[NL80211_ATTR_WIPHY_FREQ]) {
3239 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
3240 				    "Frequency is missing");
3241 		return -EINVAL;
3242 	}
3243 
3244 	control_freq = MHZ_TO_KHZ(
3245 			nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3246 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
3247 		control_freq +=
3248 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
3249 
3250 	memset(chandef, 0, sizeof(*chandef));
3251 	chandef->chan = ieee80211_get_channel_khz(&rdev->wiphy, control_freq);
3252 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
3253 	chandef->center_freq1 = KHZ_TO_MHZ(control_freq);
3254 	chandef->freq1_offset = control_freq % 1000;
3255 	chandef->center_freq2 = 0;
3256 
3257 	/* Primary channel not allowed */
3258 	if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED) {
3259 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
3260 				    "Channel is disabled");
3261 		return -EINVAL;
3262 	}
3263 
3264 	if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
3265 		enum nl80211_channel_type chantype;
3266 
3267 		chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
3268 
3269 		switch (chantype) {
3270 		case NL80211_CHAN_NO_HT:
3271 		case NL80211_CHAN_HT20:
3272 		case NL80211_CHAN_HT40PLUS:
3273 		case NL80211_CHAN_HT40MINUS:
3274 			cfg80211_chandef_create(chandef, chandef->chan,
3275 						chantype);
3276 			/* user input for center_freq is incorrect */
3277 			if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
3278 			    chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
3279 				NL_SET_ERR_MSG_ATTR(extack,
3280 						    attrs[NL80211_ATTR_CENTER_FREQ1],
3281 						    "bad center frequency 1");
3282 				return -EINVAL;
3283 			}
3284 			/* center_freq2 must be zero */
3285 			if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
3286 			    nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
3287 				NL_SET_ERR_MSG_ATTR(extack,
3288 						    attrs[NL80211_ATTR_CENTER_FREQ2],
3289 						    "center frequency 2 can't be used");
3290 				return -EINVAL;
3291 			}
3292 			break;
3293 		default:
3294 			NL_SET_ERR_MSG_ATTR(extack,
3295 					    attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3296 					    "invalid channel type");
3297 			return -EINVAL;
3298 		}
3299 	} else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
3300 		chandef->width =
3301 			nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
3302 		if (chandef->chan->band == NL80211_BAND_S1GHZ) {
3303 			/* User input error for channel width doesn't match channel  */
3304 			if (chandef->width != ieee80211_s1g_channel_width(chandef->chan)) {
3305 				NL_SET_ERR_MSG_ATTR(extack,
3306 						    attrs[NL80211_ATTR_CHANNEL_WIDTH],
3307 						    "bad channel width");
3308 				return -EINVAL;
3309 			}
3310 		}
3311 		if (attrs[NL80211_ATTR_CENTER_FREQ1]) {
3312 			chandef->center_freq1 =
3313 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
3314 			if (attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET])
3315 				chandef->freq1_offset = nla_get_u32(
3316 				      attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET]);
3317 			else
3318 				chandef->freq1_offset = 0;
3319 		}
3320 		if (attrs[NL80211_ATTR_CENTER_FREQ2])
3321 			chandef->center_freq2 =
3322 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
3323 	}
3324 
3325 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
3326 		chandef->edmg.channels =
3327 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
3328 
3329 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
3330 			chandef->edmg.bw_config =
3331 		     nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
3332 	} else {
3333 		chandef->edmg.bw_config = 0;
3334 		chandef->edmg.channels = 0;
3335 	}
3336 
3337 	if (!cfg80211_chandef_valid(chandef)) {
3338 		NL_SET_ERR_MSG(extack, "invalid channel definition");
3339 		return -EINVAL;
3340 	}
3341 
3342 	if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
3343 				     IEEE80211_CHAN_DISABLED)) {
3344 		NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
3345 		return -EINVAL;
3346 	}
3347 
3348 	if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
3349 	     chandef->width == NL80211_CHAN_WIDTH_10) &&
3350 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
3351 		NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
3352 		return -EINVAL;
3353 	}
3354 
3355 	return 0;
3356 }
3357 
3358 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
3359 				 struct net_device *dev,
3360 				 struct genl_info *info,
3361 				 int _link_id)
3362 {
3363 	struct cfg80211_chan_def chandef;
3364 	int result;
3365 	enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
3366 	struct wireless_dev *wdev = NULL;
3367 	int link_id = _link_id;
3368 
3369 	if (dev)
3370 		wdev = dev->ieee80211_ptr;
3371 	if (!nl80211_can_set_dev_channel(wdev))
3372 		return -EOPNOTSUPP;
3373 	if (wdev)
3374 		iftype = wdev->iftype;
3375 
3376 	if (link_id < 0) {
3377 		if (wdev && wdev->valid_links)
3378 			return -EINVAL;
3379 		link_id = 0;
3380 	}
3381 
3382 	result = nl80211_parse_chandef(rdev, info, &chandef);
3383 	if (result)
3384 		return result;
3385 
3386 	switch (iftype) {
3387 	case NL80211_IFTYPE_AP:
3388 	case NL80211_IFTYPE_P2P_GO:
3389 		if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
3390 						   iftype))
3391 			return -EINVAL;
3392 		if (wdev->links[link_id].ap.beacon_interval) {
3393 			struct ieee80211_channel *cur_chan;
3394 
3395 			if (!dev || !rdev->ops->set_ap_chanwidth ||
3396 			    !(rdev->wiphy.features &
3397 			      NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE))
3398 				return -EBUSY;
3399 
3400 			/* Only allow dynamic channel width changes */
3401 			cur_chan = wdev->links[link_id].ap.chandef.chan;
3402 			if (chandef.chan != cur_chan)
3403 				return -EBUSY;
3404 
3405 			result = rdev_set_ap_chanwidth(rdev, dev, link_id,
3406 						       &chandef);
3407 			if (result)
3408 				return result;
3409 			wdev->links[link_id].ap.chandef = chandef;
3410 		} else {
3411 			wdev->u.ap.preset_chandef = chandef;
3412 		}
3413 		return 0;
3414 	case NL80211_IFTYPE_MESH_POINT:
3415 		return cfg80211_set_mesh_channel(rdev, wdev, &chandef);
3416 	case NL80211_IFTYPE_MONITOR:
3417 		return cfg80211_set_monitor_channel(rdev, &chandef);
3418 	default:
3419 		break;
3420 	}
3421 
3422 	return -EINVAL;
3423 }
3424 
3425 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
3426 {
3427 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3428 	int link_id = nl80211_link_id_or_invalid(info->attrs);
3429 	struct net_device *netdev = info->user_ptr[1];
3430 
3431 	return __nl80211_set_channel(rdev, netdev, info, link_id);
3432 }
3433 
3434 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
3435 {
3436 	struct cfg80211_registered_device *rdev = NULL;
3437 	struct net_device *netdev = NULL;
3438 	struct wireless_dev *wdev;
3439 	int result = 0, rem_txq_params = 0;
3440 	struct nlattr *nl_txq_params;
3441 	u32 changed;
3442 	u8 retry_short = 0, retry_long = 0;
3443 	u32 frag_threshold = 0, rts_threshold = 0;
3444 	u8 coverage_class = 0;
3445 	u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
3446 
3447 	rtnl_lock();
3448 	/*
3449 	 * Try to find the wiphy and netdev. Normally this
3450 	 * function shouldn't need the netdev, but this is
3451 	 * done for backward compatibility -- previously
3452 	 * setting the channel was done per wiphy, but now
3453 	 * it is per netdev. Previous userland like hostapd
3454 	 * also passed a netdev to set_wiphy, so that it is
3455 	 * possible to let that go to the right netdev!
3456 	 */
3457 
3458 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
3459 		int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
3460 
3461 		netdev = __dev_get_by_index(genl_info_net(info), ifindex);
3462 		if (netdev && netdev->ieee80211_ptr)
3463 			rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
3464 		else
3465 			netdev = NULL;
3466 	}
3467 
3468 	if (!netdev) {
3469 		rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
3470 						  info->attrs);
3471 		if (IS_ERR(rdev)) {
3472 			rtnl_unlock();
3473 			return PTR_ERR(rdev);
3474 		}
3475 		wdev = NULL;
3476 		netdev = NULL;
3477 		result = 0;
3478 	} else
3479 		wdev = netdev->ieee80211_ptr;
3480 
3481 	wiphy_lock(&rdev->wiphy);
3482 
3483 	/*
3484 	 * end workaround code, by now the rdev is available
3485 	 * and locked, and wdev may or may not be NULL.
3486 	 */
3487 
3488 	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
3489 		result = cfg80211_dev_rename(
3490 			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
3491 	rtnl_unlock();
3492 
3493 	if (result)
3494 		goto out;
3495 
3496 	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
3497 		struct ieee80211_txq_params txq_params;
3498 		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
3499 
3500 		if (!rdev->ops->set_txq_params) {
3501 			result = -EOPNOTSUPP;
3502 			goto out;
3503 		}
3504 
3505 		if (!netdev) {
3506 			result = -EINVAL;
3507 			goto out;
3508 		}
3509 
3510 		if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3511 		    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3512 			result = -EINVAL;
3513 			goto out;
3514 		}
3515 
3516 		if (!netif_running(netdev)) {
3517 			result = -ENETDOWN;
3518 			goto out;
3519 		}
3520 
3521 		nla_for_each_nested(nl_txq_params,
3522 				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
3523 				    rem_txq_params) {
3524 			result = nla_parse_nested_deprecated(tb,
3525 							     NL80211_TXQ_ATTR_MAX,
3526 							     nl_txq_params,
3527 							     txq_params_policy,
3528 							     info->extack);
3529 			if (result)
3530 				goto out;
3531 			result = parse_txq_params(tb, &txq_params);
3532 			if (result)
3533 				goto out;
3534 
3535 			txq_params.link_id =
3536 				nl80211_link_id_or_invalid(info->attrs);
3537 
3538 			if (txq_params.link_id >= 0 &&
3539 			    !(netdev->ieee80211_ptr->valid_links &
3540 			      BIT(txq_params.link_id)))
3541 				result = -ENOLINK;
3542 			else if (txq_params.link_id >= 0 &&
3543 				 !netdev->ieee80211_ptr->valid_links)
3544 				result = -EINVAL;
3545 			else
3546 				result = rdev_set_txq_params(rdev, netdev,
3547 							     &txq_params);
3548 			if (result)
3549 				goto out;
3550 		}
3551 	}
3552 
3553 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3554 		int link_id = nl80211_link_id_or_invalid(info->attrs);
3555 
3556 		if (wdev) {
3557 			result = __nl80211_set_channel(
3558 				rdev,
3559 				nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
3560 				info, link_id);
3561 		} else {
3562 			result = __nl80211_set_channel(rdev, netdev, info, link_id);
3563 		}
3564 
3565 		if (result)
3566 			goto out;
3567 	}
3568 
3569 	if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
3570 		struct wireless_dev *txp_wdev = wdev;
3571 		enum nl80211_tx_power_setting type;
3572 		int idx, mbm = 0;
3573 
3574 		if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
3575 			txp_wdev = NULL;
3576 
3577 		if (!rdev->ops->set_tx_power) {
3578 			result = -EOPNOTSUPP;
3579 			goto out;
3580 		}
3581 
3582 		idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
3583 		type = nla_get_u32(info->attrs[idx]);
3584 
3585 		if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
3586 		    (type != NL80211_TX_POWER_AUTOMATIC)) {
3587 			result = -EINVAL;
3588 			goto out;
3589 		}
3590 
3591 		if (type != NL80211_TX_POWER_AUTOMATIC) {
3592 			idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
3593 			mbm = nla_get_u32(info->attrs[idx]);
3594 		}
3595 
3596 		result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
3597 		if (result)
3598 			goto out;
3599 	}
3600 
3601 	if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
3602 	    info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
3603 		u32 tx_ant, rx_ant;
3604 
3605 		if ((!rdev->wiphy.available_antennas_tx &&
3606 		     !rdev->wiphy.available_antennas_rx) ||
3607 		    !rdev->ops->set_antenna) {
3608 			result = -EOPNOTSUPP;
3609 			goto out;
3610 		}
3611 
3612 		tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
3613 		rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
3614 
3615 		/* reject antenna configurations which don't match the
3616 		 * available antenna masks, except for the "all" mask */
3617 		if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
3618 		    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
3619 			result = -EINVAL;
3620 			goto out;
3621 		}
3622 
3623 		tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
3624 		rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
3625 
3626 		result = rdev_set_antenna(rdev, tx_ant, rx_ant);
3627 		if (result)
3628 			goto out;
3629 	}
3630 
3631 	changed = 0;
3632 
3633 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
3634 		retry_short = nla_get_u8(
3635 			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
3636 
3637 		changed |= WIPHY_PARAM_RETRY_SHORT;
3638 	}
3639 
3640 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
3641 		retry_long = nla_get_u8(
3642 			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
3643 
3644 		changed |= WIPHY_PARAM_RETRY_LONG;
3645 	}
3646 
3647 	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
3648 		frag_threshold = nla_get_u32(
3649 			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
3650 		if (frag_threshold < 256) {
3651 			result = -EINVAL;
3652 			goto out;
3653 		}
3654 
3655 		if (frag_threshold != (u32) -1) {
3656 			/*
3657 			 * Fragments (apart from the last one) are required to
3658 			 * have even length. Make the fragmentation code
3659 			 * simpler by stripping LSB should someone try to use
3660 			 * odd threshold value.
3661 			 */
3662 			frag_threshold &= ~0x1;
3663 		}
3664 		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
3665 	}
3666 
3667 	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
3668 		rts_threshold = nla_get_u32(
3669 			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
3670 		changed |= WIPHY_PARAM_RTS_THRESHOLD;
3671 	}
3672 
3673 	if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
3674 		if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3675 			result = -EINVAL;
3676 			goto out;
3677 		}
3678 
3679 		coverage_class = nla_get_u8(
3680 			info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
3681 		changed |= WIPHY_PARAM_COVERAGE_CLASS;
3682 	}
3683 
3684 	if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3685 		if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION)) {
3686 			result = -EOPNOTSUPP;
3687 			goto out;
3688 		}
3689 
3690 		changed |= WIPHY_PARAM_DYN_ACK;
3691 	}
3692 
3693 	if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3694 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3695 					     NL80211_EXT_FEATURE_TXQS)) {
3696 			result = -EOPNOTSUPP;
3697 			goto out;
3698 		}
3699 		txq_limit = nla_get_u32(
3700 			info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3701 		changed |= WIPHY_PARAM_TXQ_LIMIT;
3702 	}
3703 
3704 	if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3705 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3706 					     NL80211_EXT_FEATURE_TXQS)) {
3707 			result = -EOPNOTSUPP;
3708 			goto out;
3709 		}
3710 		txq_memory_limit = nla_get_u32(
3711 			info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3712 		changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3713 	}
3714 
3715 	if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3716 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3717 					     NL80211_EXT_FEATURE_TXQS)) {
3718 			result = -EOPNOTSUPP;
3719 			goto out;
3720 		}
3721 		txq_quantum = nla_get_u32(
3722 			info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3723 		changed |= WIPHY_PARAM_TXQ_QUANTUM;
3724 	}
3725 
3726 	if (changed) {
3727 		u8 old_retry_short, old_retry_long;
3728 		u32 old_frag_threshold, old_rts_threshold;
3729 		u8 old_coverage_class;
3730 		u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3731 
3732 		if (!rdev->ops->set_wiphy_params) {
3733 			result = -EOPNOTSUPP;
3734 			goto out;
3735 		}
3736 
3737 		old_retry_short = rdev->wiphy.retry_short;
3738 		old_retry_long = rdev->wiphy.retry_long;
3739 		old_frag_threshold = rdev->wiphy.frag_threshold;
3740 		old_rts_threshold = rdev->wiphy.rts_threshold;
3741 		old_coverage_class = rdev->wiphy.coverage_class;
3742 		old_txq_limit = rdev->wiphy.txq_limit;
3743 		old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3744 		old_txq_quantum = rdev->wiphy.txq_quantum;
3745 
3746 		if (changed & WIPHY_PARAM_RETRY_SHORT)
3747 			rdev->wiphy.retry_short = retry_short;
3748 		if (changed & WIPHY_PARAM_RETRY_LONG)
3749 			rdev->wiphy.retry_long = retry_long;
3750 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3751 			rdev->wiphy.frag_threshold = frag_threshold;
3752 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3753 			rdev->wiphy.rts_threshold = rts_threshold;
3754 		if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3755 			rdev->wiphy.coverage_class = coverage_class;
3756 		if (changed & WIPHY_PARAM_TXQ_LIMIT)
3757 			rdev->wiphy.txq_limit = txq_limit;
3758 		if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3759 			rdev->wiphy.txq_memory_limit = txq_memory_limit;
3760 		if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3761 			rdev->wiphy.txq_quantum = txq_quantum;
3762 
3763 		result = rdev_set_wiphy_params(rdev, changed);
3764 		if (result) {
3765 			rdev->wiphy.retry_short = old_retry_short;
3766 			rdev->wiphy.retry_long = old_retry_long;
3767 			rdev->wiphy.frag_threshold = old_frag_threshold;
3768 			rdev->wiphy.rts_threshold = old_rts_threshold;
3769 			rdev->wiphy.coverage_class = old_coverage_class;
3770 			rdev->wiphy.txq_limit = old_txq_limit;
3771 			rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3772 			rdev->wiphy.txq_quantum = old_txq_quantum;
3773 			goto out;
3774 		}
3775 	}
3776 
3777 	result = 0;
3778 
3779 out:
3780 	wiphy_unlock(&rdev->wiphy);
3781 	return result;
3782 }
3783 
3784 int nl80211_send_chandef(struct sk_buff *msg, const struct cfg80211_chan_def *chandef)
3785 {
3786 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3787 		return -EINVAL;
3788 
3789 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3790 			chandef->chan->center_freq))
3791 		return -ENOBUFS;
3792 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
3793 			chandef->chan->freq_offset))
3794 		return -ENOBUFS;
3795 	switch (chandef->width) {
3796 	case NL80211_CHAN_WIDTH_20_NOHT:
3797 	case NL80211_CHAN_WIDTH_20:
3798 	case NL80211_CHAN_WIDTH_40:
3799 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3800 				cfg80211_get_chandef_type(chandef)))
3801 			return -ENOBUFS;
3802 		break;
3803 	default:
3804 		break;
3805 	}
3806 	if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3807 		return -ENOBUFS;
3808 	if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3809 		return -ENOBUFS;
3810 	if (chandef->center_freq2 &&
3811 	    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3812 		return -ENOBUFS;
3813 	return 0;
3814 }
3815 EXPORT_SYMBOL(nl80211_send_chandef);
3816 
3817 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3818 			      struct cfg80211_registered_device *rdev,
3819 			      struct wireless_dev *wdev,
3820 			      enum nl80211_commands cmd)
3821 {
3822 	struct net_device *dev = wdev->netdev;
3823 	void *hdr;
3824 
3825 	WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3826 		cmd != NL80211_CMD_DEL_INTERFACE &&
3827 		cmd != NL80211_CMD_SET_INTERFACE);
3828 
3829 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3830 	if (!hdr)
3831 		return -1;
3832 
3833 	if (dev &&
3834 	    (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3835 	     nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3836 		goto nla_put_failure;
3837 
3838 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3839 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3840 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3841 			      NL80211_ATTR_PAD) ||
3842 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3843 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
3844 			rdev->devlist_generation ^
3845 			(cfg80211_rdev_list_generation << 2)) ||
3846 	    nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3847 		goto nla_put_failure;
3848 
3849 	if (rdev->ops->get_channel && !wdev->valid_links) {
3850 		struct cfg80211_chan_def chandef = {};
3851 		int ret;
3852 
3853 		ret = rdev_get_channel(rdev, wdev, 0, &chandef);
3854 		if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3855 			goto nla_put_failure;
3856 	}
3857 
3858 	if (rdev->ops->get_tx_power) {
3859 		int dbm, ret;
3860 
3861 		ret = rdev_get_tx_power(rdev, wdev, &dbm);
3862 		if (ret == 0 &&
3863 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3864 				DBM_TO_MBM(dbm)))
3865 			goto nla_put_failure;
3866 	}
3867 
3868 	switch (wdev->iftype) {
3869 	case NL80211_IFTYPE_AP:
3870 	case NL80211_IFTYPE_P2P_GO:
3871 		if (wdev->u.ap.ssid_len &&
3872 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.ap.ssid_len,
3873 			    wdev->u.ap.ssid))
3874 			goto nla_put_failure;
3875 		break;
3876 	case NL80211_IFTYPE_STATION:
3877 	case NL80211_IFTYPE_P2P_CLIENT:
3878 		if (wdev->u.client.ssid_len &&
3879 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.client.ssid_len,
3880 			    wdev->u.client.ssid))
3881 			goto nla_put_failure;
3882 		break;
3883 	case NL80211_IFTYPE_ADHOC:
3884 		if (wdev->u.ibss.ssid_len &&
3885 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.ibss.ssid_len,
3886 			    wdev->u.ibss.ssid))
3887 			goto nla_put_failure;
3888 		break;
3889 	default:
3890 		/* nothing */
3891 		break;
3892 	}
3893 
3894 	if (rdev->ops->get_txq_stats) {
3895 		struct cfg80211_txq_stats txqstats = {};
3896 		int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3897 
3898 		if (ret == 0 &&
3899 		    !nl80211_put_txq_stats(msg, &txqstats,
3900 					   NL80211_ATTR_TXQ_STATS))
3901 			goto nla_put_failure;
3902 	}
3903 
3904 	if (wdev->valid_links) {
3905 		unsigned int link_id;
3906 		struct nlattr *links = nla_nest_start(msg,
3907 						      NL80211_ATTR_MLO_LINKS);
3908 
3909 		if (!links)
3910 			goto nla_put_failure;
3911 
3912 		for_each_valid_link(wdev, link_id) {
3913 			struct nlattr *link = nla_nest_start(msg, link_id + 1);
3914 			struct cfg80211_chan_def chandef = {};
3915 			int ret;
3916 
3917 			if (!link)
3918 				goto nla_put_failure;
3919 
3920 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
3921 				goto nla_put_failure;
3922 			if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
3923 				    wdev->links[link_id].addr))
3924 				goto nla_put_failure;
3925 
3926 			ret = rdev_get_channel(rdev, wdev, link_id, &chandef);
3927 			if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3928 				goto nla_put_failure;
3929 
3930 			nla_nest_end(msg, link);
3931 		}
3932 
3933 		nla_nest_end(msg, links);
3934 	}
3935 
3936 	genlmsg_end(msg, hdr);
3937 	return 0;
3938 
3939  nla_put_failure:
3940 	genlmsg_cancel(msg, hdr);
3941 	return -EMSGSIZE;
3942 }
3943 
3944 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3945 {
3946 	int wp_idx = 0;
3947 	int if_idx = 0;
3948 	int wp_start = cb->args[0];
3949 	int if_start = cb->args[1];
3950 	int filter_wiphy = -1;
3951 	struct cfg80211_registered_device *rdev;
3952 	struct wireless_dev *wdev;
3953 	int ret;
3954 
3955 	rtnl_lock();
3956 	if (!cb->args[2]) {
3957 		struct nl80211_dump_wiphy_state state = {
3958 			.filter_wiphy = -1,
3959 		};
3960 
3961 		ret = nl80211_dump_wiphy_parse(skb, cb, &state);
3962 		if (ret)
3963 			goto out_unlock;
3964 
3965 		filter_wiphy = state.filter_wiphy;
3966 
3967 		/*
3968 		 * if filtering, set cb->args[2] to +1 since 0 is the default
3969 		 * value needed to determine that parsing is necessary.
3970 		 */
3971 		if (filter_wiphy >= 0)
3972 			cb->args[2] = filter_wiphy + 1;
3973 		else
3974 			cb->args[2] = -1;
3975 	} else if (cb->args[2] > 0) {
3976 		filter_wiphy = cb->args[2] - 1;
3977 	}
3978 
3979 	for_each_rdev(rdev) {
3980 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3981 			continue;
3982 		if (wp_idx < wp_start) {
3983 			wp_idx++;
3984 			continue;
3985 		}
3986 
3987 		if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
3988 			continue;
3989 
3990 		if_idx = 0;
3991 
3992 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3993 			if (if_idx < if_start) {
3994 				if_idx++;
3995 				continue;
3996 			}
3997 			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
3998 					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
3999 					       rdev, wdev,
4000 					       NL80211_CMD_NEW_INTERFACE) < 0) {
4001 				goto out;
4002 			}
4003 			if_idx++;
4004 		}
4005 
4006 		wp_idx++;
4007 	}
4008  out:
4009 	cb->args[0] = wp_idx;
4010 	cb->args[1] = if_idx;
4011 
4012 	ret = skb->len;
4013  out_unlock:
4014 	rtnl_unlock();
4015 
4016 	return ret;
4017 }
4018 
4019 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
4020 {
4021 	struct sk_buff *msg;
4022 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4023 	struct wireless_dev *wdev = info->user_ptr[1];
4024 
4025 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4026 	if (!msg)
4027 		return -ENOMEM;
4028 
4029 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
4030 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
4031 		nlmsg_free(msg);
4032 		return -ENOBUFS;
4033 	}
4034 
4035 	return genlmsg_reply(msg, info);
4036 }
4037 
4038 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
4039 	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
4040 	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
4041 	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
4042 	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
4043 	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
4044 	[NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
4045 };
4046 
4047 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
4048 {
4049 	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
4050 	int flag;
4051 
4052 	*mntrflags = 0;
4053 
4054 	if (!nla)
4055 		return -EINVAL;
4056 
4057 	if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
4058 		return -EINVAL;
4059 
4060 	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
4061 		if (flags[flag])
4062 			*mntrflags |= (1<<flag);
4063 
4064 	*mntrflags |= MONITOR_FLAG_CHANGED;
4065 
4066 	return 0;
4067 }
4068 
4069 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
4070 				     enum nl80211_iftype type,
4071 				     struct genl_info *info,
4072 				     struct vif_params *params)
4073 {
4074 	bool change = false;
4075 	int err;
4076 
4077 	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
4078 		if (type != NL80211_IFTYPE_MONITOR)
4079 			return -EINVAL;
4080 
4081 		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
4082 					  &params->flags);
4083 		if (err)
4084 			return err;
4085 
4086 		change = true;
4087 	}
4088 
4089 	if (params->flags & MONITOR_FLAG_ACTIVE &&
4090 	    !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
4091 		return -EOPNOTSUPP;
4092 
4093 	if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
4094 		const u8 *mumimo_groups;
4095 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4096 
4097 		if (type != NL80211_IFTYPE_MONITOR)
4098 			return -EINVAL;
4099 
4100 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4101 			return -EOPNOTSUPP;
4102 
4103 		mumimo_groups =
4104 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
4105 
4106 		/* bits 0 and 63 are reserved and must be zero */
4107 		if ((mumimo_groups[0] & BIT(0)) ||
4108 		    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
4109 			return -EINVAL;
4110 
4111 		params->vht_mumimo_groups = mumimo_groups;
4112 		change = true;
4113 	}
4114 
4115 	if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
4116 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4117 
4118 		if (type != NL80211_IFTYPE_MONITOR)
4119 			return -EINVAL;
4120 
4121 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4122 			return -EOPNOTSUPP;
4123 
4124 		params->vht_mumimo_follow_addr =
4125 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
4126 		change = true;
4127 	}
4128 
4129 	return change ? 1 : 0;
4130 }
4131 
4132 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
4133 			       struct net_device *netdev, u8 use_4addr,
4134 			       enum nl80211_iftype iftype)
4135 {
4136 	if (!use_4addr) {
4137 		if (netdev && netif_is_bridge_port(netdev))
4138 			return -EBUSY;
4139 		return 0;
4140 	}
4141 
4142 	switch (iftype) {
4143 	case NL80211_IFTYPE_AP_VLAN:
4144 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
4145 			return 0;
4146 		break;
4147 	case NL80211_IFTYPE_STATION:
4148 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
4149 			return 0;
4150 		break;
4151 	default:
4152 		break;
4153 	}
4154 
4155 	return -EOPNOTSUPP;
4156 }
4157 
4158 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
4159 {
4160 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4161 	struct vif_params params;
4162 	int err;
4163 	enum nl80211_iftype otype, ntype;
4164 	struct net_device *dev = info->user_ptr[1];
4165 	bool change = false;
4166 
4167 	memset(&params, 0, sizeof(params));
4168 
4169 	otype = ntype = dev->ieee80211_ptr->iftype;
4170 
4171 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
4172 		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4173 		if (otype != ntype)
4174 			change = true;
4175 	}
4176 
4177 	if (info->attrs[NL80211_ATTR_MESH_ID]) {
4178 		struct wireless_dev *wdev = dev->ieee80211_ptr;
4179 
4180 		if (ntype != NL80211_IFTYPE_MESH_POINT)
4181 			return -EINVAL;
4182 		if (netif_running(dev))
4183 			return -EBUSY;
4184 
4185 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
4186 			     IEEE80211_MAX_MESH_ID_LEN);
4187 		wdev->u.mesh.id_up_len =
4188 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4189 		memcpy(wdev->u.mesh.id,
4190 		       nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4191 		       wdev->u.mesh.id_up_len);
4192 	}
4193 
4194 	if (info->attrs[NL80211_ATTR_4ADDR]) {
4195 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4196 		change = true;
4197 		err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
4198 		if (err)
4199 			return err;
4200 	} else {
4201 		params.use_4addr = -1;
4202 	}
4203 
4204 	err = nl80211_parse_mon_options(rdev, ntype, info, &params);
4205 	if (err < 0)
4206 		return err;
4207 	if (err > 0)
4208 		change = true;
4209 
4210 	if (change)
4211 		err = cfg80211_change_iface(rdev, dev, ntype, &params);
4212 	else
4213 		err = 0;
4214 
4215 	if (!err && params.use_4addr != -1)
4216 		dev->ieee80211_ptr->use_4addr = params.use_4addr;
4217 
4218 	if (change && !err) {
4219 		struct wireless_dev *wdev = dev->ieee80211_ptr;
4220 
4221 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
4222 	}
4223 
4224 	return err;
4225 }
4226 
4227 static int _nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4228 {
4229 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4230 	struct vif_params params;
4231 	struct wireless_dev *wdev;
4232 	struct sk_buff *msg;
4233 	int err;
4234 	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
4235 
4236 	memset(&params, 0, sizeof(params));
4237 
4238 	if (!info->attrs[NL80211_ATTR_IFNAME])
4239 		return -EINVAL;
4240 
4241 	if (info->attrs[NL80211_ATTR_IFTYPE])
4242 		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4243 
4244 	if (!rdev->ops->add_virtual_intf)
4245 		return -EOPNOTSUPP;
4246 
4247 	if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
4248 	     rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
4249 	    info->attrs[NL80211_ATTR_MAC]) {
4250 		nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
4251 			   ETH_ALEN);
4252 		if (!is_valid_ether_addr(params.macaddr))
4253 			return -EADDRNOTAVAIL;
4254 	}
4255 
4256 	if (info->attrs[NL80211_ATTR_4ADDR]) {
4257 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4258 		err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
4259 		if (err)
4260 			return err;
4261 	}
4262 
4263 	if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
4264 		return -EOPNOTSUPP;
4265 
4266 	err = nl80211_parse_mon_options(rdev, type, info, &params);
4267 	if (err < 0)
4268 		return err;
4269 
4270 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4271 	if (!msg)
4272 		return -ENOMEM;
4273 
4274 	wdev = rdev_add_virtual_intf(rdev,
4275 				nla_data(info->attrs[NL80211_ATTR_IFNAME]),
4276 				NET_NAME_USER, type, &params);
4277 	if (WARN_ON(!wdev)) {
4278 		nlmsg_free(msg);
4279 		return -EPROTO;
4280 	} else if (IS_ERR(wdev)) {
4281 		nlmsg_free(msg);
4282 		return PTR_ERR(wdev);
4283 	}
4284 
4285 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
4286 		wdev->owner_nlportid = info->snd_portid;
4287 
4288 	switch (type) {
4289 	case NL80211_IFTYPE_MESH_POINT:
4290 		if (!info->attrs[NL80211_ATTR_MESH_ID])
4291 			break;
4292 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
4293 			     IEEE80211_MAX_MESH_ID_LEN);
4294 		wdev->u.mesh.id_up_len =
4295 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4296 		memcpy(wdev->u.mesh.id,
4297 		       nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4298 		       wdev->u.mesh.id_up_len);
4299 		break;
4300 	case NL80211_IFTYPE_NAN:
4301 	case NL80211_IFTYPE_P2P_DEVICE:
4302 		/*
4303 		 * P2P Device and NAN do not have a netdev, so don't go
4304 		 * through the netdev notifier and must be added here
4305 		 */
4306 		cfg80211_init_wdev(wdev);
4307 		cfg80211_register_wdev(rdev, wdev);
4308 		break;
4309 	default:
4310 		break;
4311 	}
4312 
4313 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
4314 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
4315 		nlmsg_free(msg);
4316 		return -ENOBUFS;
4317 	}
4318 
4319 	return genlmsg_reply(msg, info);
4320 }
4321 
4322 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4323 {
4324 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4325 	int ret;
4326 
4327 	/* to avoid failing a new interface creation due to pending removal */
4328 	cfg80211_destroy_ifaces(rdev);
4329 
4330 	wiphy_lock(&rdev->wiphy);
4331 	ret = _nl80211_new_interface(skb, info);
4332 	wiphy_unlock(&rdev->wiphy);
4333 
4334 	return ret;
4335 }
4336 
4337 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
4338 {
4339 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4340 	struct wireless_dev *wdev = info->user_ptr[1];
4341 
4342 	if (!rdev->ops->del_virtual_intf)
4343 		return -EOPNOTSUPP;
4344 
4345 	/*
4346 	 * We hold RTNL, so this is safe, without RTNL opencount cannot
4347 	 * reach 0, and thus the rdev cannot be deleted.
4348 	 *
4349 	 * We need to do it for the dev_close(), since that will call
4350 	 * the netdev notifiers, and we need to acquire the mutex there
4351 	 * but don't know if we get there from here or from some other
4352 	 * place (e.g. "ip link set ... down").
4353 	 */
4354 	mutex_unlock(&rdev->wiphy.mtx);
4355 
4356 	/*
4357 	 * If we remove a wireless device without a netdev then clear
4358 	 * user_ptr[1] so that nl80211_post_doit won't dereference it
4359 	 * to check if it needs to do dev_put(). Otherwise it crashes
4360 	 * since the wdev has been freed, unlike with a netdev where
4361 	 * we need the dev_put() for the netdev to really be freed.
4362 	 */
4363 	if (!wdev->netdev)
4364 		info->user_ptr[1] = NULL;
4365 	else
4366 		dev_close(wdev->netdev);
4367 
4368 	mutex_lock(&rdev->wiphy.mtx);
4369 
4370 	return cfg80211_remove_virtual_intf(rdev, wdev);
4371 }
4372 
4373 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
4374 {
4375 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4376 	struct net_device *dev = info->user_ptr[1];
4377 	u16 noack_map;
4378 
4379 	if (!info->attrs[NL80211_ATTR_NOACK_MAP])
4380 		return -EINVAL;
4381 
4382 	if (!rdev->ops->set_noack_map)
4383 		return -EOPNOTSUPP;
4384 
4385 	noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
4386 
4387 	return rdev_set_noack_map(rdev, dev, noack_map);
4388 }
4389 
4390 static int nl80211_validate_key_link_id(struct genl_info *info,
4391 					struct wireless_dev *wdev,
4392 					int link_id, bool pairwise)
4393 {
4394 	if (pairwise) {
4395 		if (link_id != -1) {
4396 			GENL_SET_ERR_MSG(info,
4397 					 "link ID not allowed for pairwise key");
4398 			return -EINVAL;
4399 		}
4400 
4401 		return 0;
4402 	}
4403 
4404 	if (wdev->valid_links) {
4405 		if (link_id == -1) {
4406 			GENL_SET_ERR_MSG(info,
4407 					 "link ID must for MLO group key");
4408 			return -EINVAL;
4409 		}
4410 		if (!(wdev->valid_links & BIT(link_id))) {
4411 			GENL_SET_ERR_MSG(info, "invalid link ID for MLO group key");
4412 			return -EINVAL;
4413 		}
4414 	} else if (link_id != -1) {
4415 		GENL_SET_ERR_MSG(info, "link ID not allowed for non-MLO group key");
4416 		return -EINVAL;
4417 	}
4418 
4419 	return 0;
4420 }
4421 
4422 struct get_key_cookie {
4423 	struct sk_buff *msg;
4424 	int error;
4425 	int idx;
4426 };
4427 
4428 static void get_key_callback(void *c, struct key_params *params)
4429 {
4430 	struct nlattr *key;
4431 	struct get_key_cookie *cookie = c;
4432 
4433 	if ((params->key &&
4434 	     nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
4435 		     params->key_len, params->key)) ||
4436 	    (params->seq &&
4437 	     nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
4438 		     params->seq_len, params->seq)) ||
4439 	    (params->cipher &&
4440 	     nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
4441 			 params->cipher)))
4442 		goto nla_put_failure;
4443 
4444 	key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
4445 	if (!key)
4446 		goto nla_put_failure;
4447 
4448 	if ((params->key &&
4449 	     nla_put(cookie->msg, NL80211_KEY_DATA,
4450 		     params->key_len, params->key)) ||
4451 	    (params->seq &&
4452 	     nla_put(cookie->msg, NL80211_KEY_SEQ,
4453 		     params->seq_len, params->seq)) ||
4454 	    (params->cipher &&
4455 	     nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
4456 			 params->cipher)))
4457 		goto nla_put_failure;
4458 
4459 	if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
4460 		goto nla_put_failure;
4461 
4462 	nla_nest_end(cookie->msg, key);
4463 
4464 	return;
4465  nla_put_failure:
4466 	cookie->error = 1;
4467 }
4468 
4469 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
4470 {
4471 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4472 	int err;
4473 	struct net_device *dev = info->user_ptr[1];
4474 	u8 key_idx = 0;
4475 	const u8 *mac_addr = NULL;
4476 	bool pairwise;
4477 	struct get_key_cookie cookie = {
4478 		.error = 0,
4479 	};
4480 	void *hdr;
4481 	struct sk_buff *msg;
4482 	bool bigtk_support = false;
4483 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4484 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4485 
4486 	if (wiphy_ext_feature_isset(&rdev->wiphy,
4487 				    NL80211_EXT_FEATURE_BEACON_PROTECTION))
4488 		bigtk_support = true;
4489 
4490 	if ((wdev->iftype == NL80211_IFTYPE_STATION ||
4491 	     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
4492 	    wiphy_ext_feature_isset(&rdev->wiphy,
4493 				    NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
4494 		bigtk_support = true;
4495 
4496 	if (info->attrs[NL80211_ATTR_KEY_IDX]) {
4497 		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
4498 
4499 		if (key_idx >= 6 && key_idx <= 7 && !bigtk_support) {
4500 			GENL_SET_ERR_MSG(info, "BIGTK not supported");
4501 			return -EINVAL;
4502 		}
4503 	}
4504 
4505 	if (info->attrs[NL80211_ATTR_MAC])
4506 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4507 
4508 	pairwise = !!mac_addr;
4509 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
4510 		u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
4511 
4512 		if (kt != NL80211_KEYTYPE_GROUP &&
4513 		    kt != NL80211_KEYTYPE_PAIRWISE)
4514 			return -EINVAL;
4515 		pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
4516 	}
4517 
4518 	if (!rdev->ops->get_key)
4519 		return -EOPNOTSUPP;
4520 
4521 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4522 		return -ENOENT;
4523 
4524 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4525 	if (!msg)
4526 		return -ENOMEM;
4527 
4528 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
4529 			     NL80211_CMD_NEW_KEY);
4530 	if (!hdr)
4531 		goto nla_put_failure;
4532 
4533 	cookie.msg = msg;
4534 	cookie.idx = key_idx;
4535 
4536 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4537 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
4538 		goto nla_put_failure;
4539 	if (mac_addr &&
4540 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
4541 		goto nla_put_failure;
4542 
4543 	err = nl80211_validate_key_link_id(info, wdev, link_id, pairwise);
4544 	if (err)
4545 		goto free_msg;
4546 
4547 	err = rdev_get_key(rdev, dev, link_id, key_idx, pairwise, mac_addr,
4548 			   &cookie, get_key_callback);
4549 
4550 	if (err)
4551 		goto free_msg;
4552 
4553 	if (cookie.error)
4554 		goto nla_put_failure;
4555 
4556 	genlmsg_end(msg, hdr);
4557 	return genlmsg_reply(msg, info);
4558 
4559  nla_put_failure:
4560 	err = -ENOBUFS;
4561  free_msg:
4562 	nlmsg_free(msg);
4563 	return err;
4564 }
4565 
4566 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
4567 {
4568 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4569 	struct key_parse key;
4570 	int err;
4571 	struct net_device *dev = info->user_ptr[1];
4572 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4573 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4574 
4575 	err = nl80211_parse_key(info, &key);
4576 	if (err)
4577 		return err;
4578 
4579 	if (key.idx < 0)
4580 		return -EINVAL;
4581 
4582 	/* Only support setting default key and
4583 	 * Extended Key ID action NL80211_KEY_SET_TX.
4584 	 */
4585 	if (!key.def && !key.defmgmt && !key.defbeacon &&
4586 	    !(key.p.mode == NL80211_KEY_SET_TX))
4587 		return -EINVAL;
4588 
4589 	if (key.def) {
4590 		if (!rdev->ops->set_default_key)
4591 			return -EOPNOTSUPP;
4592 
4593 		err = nl80211_key_allowed(wdev);
4594 		if (err)
4595 			return err;
4596 
4597 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4598 		if (err)
4599 			return err;
4600 
4601 		err = rdev_set_default_key(rdev, dev, link_id, key.idx,
4602 					   key.def_uni, key.def_multi);
4603 
4604 		if (err)
4605 			return err;
4606 
4607 #ifdef CONFIG_CFG80211_WEXT
4608 		wdev->wext.default_key = key.idx;
4609 #endif
4610 		return 0;
4611 	} else if (key.defmgmt) {
4612 		if (key.def_uni || !key.def_multi)
4613 			return -EINVAL;
4614 
4615 		if (!rdev->ops->set_default_mgmt_key)
4616 			return -EOPNOTSUPP;
4617 
4618 		err = nl80211_key_allowed(wdev);
4619 		if (err)
4620 			return err;
4621 
4622 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4623 		if (err)
4624 			return err;
4625 
4626 		err = rdev_set_default_mgmt_key(rdev, dev, link_id, key.idx);
4627 		if (err)
4628 			return err;
4629 
4630 #ifdef CONFIG_CFG80211_WEXT
4631 		wdev->wext.default_mgmt_key = key.idx;
4632 #endif
4633 		return 0;
4634 	} else if (key.defbeacon) {
4635 		if (key.def_uni || !key.def_multi)
4636 			return -EINVAL;
4637 
4638 		if (!rdev->ops->set_default_beacon_key)
4639 			return -EOPNOTSUPP;
4640 
4641 		err = nl80211_key_allowed(wdev);
4642 		if (err)
4643 			return err;
4644 
4645 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4646 		if (err)
4647 			return err;
4648 
4649 		return rdev_set_default_beacon_key(rdev, dev, link_id, key.idx);
4650 	} else if (key.p.mode == NL80211_KEY_SET_TX &&
4651 		   wiphy_ext_feature_isset(&rdev->wiphy,
4652 					   NL80211_EXT_FEATURE_EXT_KEY_ID)) {
4653 		u8 *mac_addr = NULL;
4654 
4655 		if (info->attrs[NL80211_ATTR_MAC])
4656 			mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4657 
4658 		if (!mac_addr || key.idx < 0 || key.idx > 1)
4659 			return -EINVAL;
4660 
4661 		err = nl80211_validate_key_link_id(info, wdev, link_id, true);
4662 		if (err)
4663 			return err;
4664 
4665 		return rdev_add_key(rdev, dev, link_id, key.idx,
4666 				    NL80211_KEYTYPE_PAIRWISE,
4667 				    mac_addr, &key.p);
4668 	}
4669 
4670 	return -EINVAL;
4671 }
4672 
4673 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
4674 {
4675 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4676 	int err;
4677 	struct net_device *dev = info->user_ptr[1];
4678 	struct key_parse key;
4679 	const u8 *mac_addr = NULL;
4680 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4681 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4682 
4683 	err = nl80211_parse_key(info, &key);
4684 	if (err)
4685 		return err;
4686 
4687 	if (!key.p.key) {
4688 		GENL_SET_ERR_MSG(info, "no key");
4689 		return -EINVAL;
4690 	}
4691 
4692 	if (info->attrs[NL80211_ATTR_MAC])
4693 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4694 
4695 	if (key.type == -1) {
4696 		if (mac_addr)
4697 			key.type = NL80211_KEYTYPE_PAIRWISE;
4698 		else
4699 			key.type = NL80211_KEYTYPE_GROUP;
4700 	}
4701 
4702 	/* for now */
4703 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4704 	    key.type != NL80211_KEYTYPE_GROUP) {
4705 		GENL_SET_ERR_MSG(info, "key type not pairwise or group");
4706 		return -EINVAL;
4707 	}
4708 
4709 	if (key.type == NL80211_KEYTYPE_GROUP &&
4710 	    info->attrs[NL80211_ATTR_VLAN_ID])
4711 		key.p.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
4712 
4713 	if (!rdev->ops->add_key)
4714 		return -EOPNOTSUPP;
4715 
4716 	if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
4717 					   key.type == NL80211_KEYTYPE_PAIRWISE,
4718 					   mac_addr)) {
4719 		GENL_SET_ERR_MSG(info, "key setting validation failed");
4720 		return -EINVAL;
4721 	}
4722 
4723 	err = nl80211_key_allowed(wdev);
4724 	if (err)
4725 		GENL_SET_ERR_MSG(info, "key not allowed");
4726 
4727 	if (!err)
4728 		err = nl80211_validate_key_link_id(info, wdev, link_id,
4729 				key.type == NL80211_KEYTYPE_PAIRWISE);
4730 
4731 	if (!err) {
4732 		err = rdev_add_key(rdev, dev, link_id, key.idx,
4733 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4734 				    mac_addr, &key.p);
4735 		if (err)
4736 			GENL_SET_ERR_MSG(info, "key addition failed");
4737 	}
4738 
4739 	return err;
4740 }
4741 
4742 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
4743 {
4744 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4745 	int err;
4746 	struct net_device *dev = info->user_ptr[1];
4747 	u8 *mac_addr = NULL;
4748 	struct key_parse key;
4749 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4750 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4751 
4752 	err = nl80211_parse_key(info, &key);
4753 	if (err)
4754 		return err;
4755 
4756 	if (info->attrs[NL80211_ATTR_MAC])
4757 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4758 
4759 	if (key.type == -1) {
4760 		if (mac_addr)
4761 			key.type = NL80211_KEYTYPE_PAIRWISE;
4762 		else
4763 			key.type = NL80211_KEYTYPE_GROUP;
4764 	}
4765 
4766 	/* for now */
4767 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4768 	    key.type != NL80211_KEYTYPE_GROUP)
4769 		return -EINVAL;
4770 
4771 	if (!cfg80211_valid_key_idx(rdev, key.idx,
4772 				    key.type == NL80211_KEYTYPE_PAIRWISE))
4773 		return -EINVAL;
4774 
4775 	if (!rdev->ops->del_key)
4776 		return -EOPNOTSUPP;
4777 
4778 	err = nl80211_key_allowed(wdev);
4779 
4780 	if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
4781 	    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4782 		err = -ENOENT;
4783 
4784 	if (!err)
4785 		err = nl80211_validate_key_link_id(info, wdev, link_id,
4786 				key.type == NL80211_KEYTYPE_PAIRWISE);
4787 
4788 	if (!err)
4789 		err = rdev_del_key(rdev, dev, link_id, key.idx,
4790 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4791 				   mac_addr);
4792 
4793 #ifdef CONFIG_CFG80211_WEXT
4794 	if (!err) {
4795 		if (key.idx == wdev->wext.default_key)
4796 			wdev->wext.default_key = -1;
4797 		else if (key.idx == wdev->wext.default_mgmt_key)
4798 			wdev->wext.default_mgmt_key = -1;
4799 	}
4800 #endif
4801 
4802 	return err;
4803 }
4804 
4805 /* This function returns an error or the number of nested attributes */
4806 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
4807 {
4808 	struct nlattr *attr;
4809 	int n_entries = 0, tmp;
4810 
4811 	nla_for_each_nested(attr, nl_attr, tmp) {
4812 		if (nla_len(attr) != ETH_ALEN)
4813 			return -EINVAL;
4814 
4815 		n_entries++;
4816 	}
4817 
4818 	return n_entries;
4819 }
4820 
4821 /*
4822  * This function parses ACL information and allocates memory for ACL data.
4823  * On successful return, the calling function is responsible to free the
4824  * ACL buffer returned by this function.
4825  */
4826 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
4827 						struct genl_info *info)
4828 {
4829 	enum nl80211_acl_policy acl_policy;
4830 	struct nlattr *attr;
4831 	struct cfg80211_acl_data *acl;
4832 	int i = 0, n_entries, tmp;
4833 
4834 	if (!wiphy->max_acl_mac_addrs)
4835 		return ERR_PTR(-EOPNOTSUPP);
4836 
4837 	if (!info->attrs[NL80211_ATTR_ACL_POLICY])
4838 		return ERR_PTR(-EINVAL);
4839 
4840 	acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
4841 	if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
4842 	    acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
4843 		return ERR_PTR(-EINVAL);
4844 
4845 	if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
4846 		return ERR_PTR(-EINVAL);
4847 
4848 	n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
4849 	if (n_entries < 0)
4850 		return ERR_PTR(n_entries);
4851 
4852 	if (n_entries > wiphy->max_acl_mac_addrs)
4853 		return ERR_PTR(-ENOTSUPP);
4854 
4855 	acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4856 	if (!acl)
4857 		return ERR_PTR(-ENOMEM);
4858 	acl->n_acl_entries = n_entries;
4859 
4860 	nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4861 		memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4862 		i++;
4863 	}
4864 	acl->acl_policy = acl_policy;
4865 
4866 	return acl;
4867 }
4868 
4869 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4870 {
4871 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4872 	struct net_device *dev = info->user_ptr[1];
4873 	struct cfg80211_acl_data *acl;
4874 	int err;
4875 
4876 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4877 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4878 		return -EOPNOTSUPP;
4879 
4880 	if (!dev->ieee80211_ptr->links[0].ap.beacon_interval)
4881 		return -EINVAL;
4882 
4883 	acl = parse_acl_data(&rdev->wiphy, info);
4884 	if (IS_ERR(acl))
4885 		return PTR_ERR(acl);
4886 
4887 	err = rdev_set_mac_acl(rdev, dev, acl);
4888 
4889 	kfree(acl);
4890 
4891 	return err;
4892 }
4893 
4894 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4895 			   u8 *rates, u8 rates_len)
4896 {
4897 	u8 i;
4898 	u32 mask = 0;
4899 
4900 	for (i = 0; i < rates_len; i++) {
4901 		int rate = (rates[i] & 0x7f) * 5;
4902 		int ridx;
4903 
4904 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4905 			struct ieee80211_rate *srate =
4906 				&sband->bitrates[ridx];
4907 			if (rate == srate->bitrate) {
4908 				mask |= 1 << ridx;
4909 				break;
4910 			}
4911 		}
4912 		if (ridx == sband->n_bitrates)
4913 			return 0; /* rate not found */
4914 	}
4915 
4916 	return mask;
4917 }
4918 
4919 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4920 			       u8 *rates, u8 rates_len,
4921 			       u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4922 {
4923 	u8 i;
4924 
4925 	memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4926 
4927 	for (i = 0; i < rates_len; i++) {
4928 		int ridx, rbit;
4929 
4930 		ridx = rates[i] / 8;
4931 		rbit = BIT(rates[i] % 8);
4932 
4933 		/* check validity */
4934 		if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4935 			return false;
4936 
4937 		/* check availability */
4938 		ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
4939 		if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
4940 			mcs[ridx] |= rbit;
4941 		else
4942 			return false;
4943 	}
4944 
4945 	return true;
4946 }
4947 
4948 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
4949 {
4950 	u16 mcs_mask = 0;
4951 
4952 	switch (vht_mcs_map) {
4953 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
4954 		break;
4955 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
4956 		mcs_mask = 0x00FF;
4957 		break;
4958 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
4959 		mcs_mask = 0x01FF;
4960 		break;
4961 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
4962 		mcs_mask = 0x03FF;
4963 		break;
4964 	default:
4965 		break;
4966 	}
4967 
4968 	return mcs_mask;
4969 }
4970 
4971 static void vht_build_mcs_mask(u16 vht_mcs_map,
4972 			       u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
4973 {
4974 	u8 nss;
4975 
4976 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
4977 		vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
4978 		vht_mcs_map >>= 2;
4979 	}
4980 }
4981 
4982 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
4983 			     struct nl80211_txrate_vht *txrate,
4984 			     u16 mcs[NL80211_VHT_NSS_MAX])
4985 {
4986 	u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4987 	u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
4988 	u8 i;
4989 
4990 	if (!sband->vht_cap.vht_supported)
4991 		return false;
4992 
4993 	memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
4994 
4995 	/* Build vht_mcs_mask from VHT capabilities */
4996 	vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
4997 
4998 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4999 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
5000 			mcs[i] = txrate->mcs[i];
5001 		else
5002 			return false;
5003 	}
5004 
5005 	return true;
5006 }
5007 
5008 static u16 he_mcs_map_to_mcs_mask(u8 he_mcs_map)
5009 {
5010 	switch (he_mcs_map) {
5011 	case IEEE80211_HE_MCS_NOT_SUPPORTED:
5012 		return 0;
5013 	case IEEE80211_HE_MCS_SUPPORT_0_7:
5014 		return 0x00FF;
5015 	case IEEE80211_HE_MCS_SUPPORT_0_9:
5016 		return 0x03FF;
5017 	case IEEE80211_HE_MCS_SUPPORT_0_11:
5018 		return 0xFFF;
5019 	default:
5020 		break;
5021 	}
5022 	return 0;
5023 }
5024 
5025 static void he_build_mcs_mask(u16 he_mcs_map,
5026 			      u16 he_mcs_mask[NL80211_HE_NSS_MAX])
5027 {
5028 	u8 nss;
5029 
5030 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
5031 		he_mcs_mask[nss] = he_mcs_map_to_mcs_mask(he_mcs_map & 0x03);
5032 		he_mcs_map >>= 2;
5033 	}
5034 }
5035 
5036 static u16 he_get_txmcsmap(struct genl_info *info, unsigned int link_id,
5037 			   const struct ieee80211_sta_he_cap *he_cap)
5038 {
5039 	struct net_device *dev = info->user_ptr[1];
5040 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5041 	struct cfg80211_chan_def *chandef;
5042 	__le16 tx_mcs;
5043 
5044 	chandef = wdev_chandef(wdev, link_id);
5045 	if (!chandef) {
5046 		/*
5047 		 * This is probably broken, but we never maintained
5048 		 * a chandef in these cases, so it always was.
5049 		 */
5050 		return le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
5051 	}
5052 
5053 	switch (chandef->width) {
5054 	case NL80211_CHAN_WIDTH_80P80:
5055 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80p80;
5056 		break;
5057 	case NL80211_CHAN_WIDTH_160:
5058 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_160;
5059 		break;
5060 	default:
5061 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80;
5062 		break;
5063 	}
5064 
5065 	return le16_to_cpu(tx_mcs);
5066 }
5067 
5068 static bool he_set_mcs_mask(struct genl_info *info,
5069 			    struct wireless_dev *wdev,
5070 			    struct ieee80211_supported_band *sband,
5071 			    struct nl80211_txrate_he *txrate,
5072 			    u16 mcs[NL80211_HE_NSS_MAX],
5073 			    unsigned int link_id)
5074 {
5075 	const struct ieee80211_sta_he_cap *he_cap;
5076 	u16 tx_mcs_mask[NL80211_HE_NSS_MAX] = {};
5077 	u16 tx_mcs_map = 0;
5078 	u8 i;
5079 
5080 	he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5081 	if (!he_cap)
5082 		return false;
5083 
5084 	memset(mcs, 0, sizeof(u16) * NL80211_HE_NSS_MAX);
5085 
5086 	tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5087 
5088 	/* Build he_mcs_mask from HE capabilities */
5089 	he_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
5090 
5091 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5092 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
5093 			mcs[i] = txrate->mcs[i];
5094 		else
5095 			return false;
5096 	}
5097 
5098 	return true;
5099 }
5100 
5101 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
5102 					 struct nlattr *attrs[],
5103 					 enum nl80211_attrs attr,
5104 					 struct cfg80211_bitrate_mask *mask,
5105 					 struct net_device *dev,
5106 					 bool default_all_enabled,
5107 					 unsigned int link_id)
5108 {
5109 	struct nlattr *tb[NL80211_TXRATE_MAX + 1];
5110 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5111 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5112 	int rem, i;
5113 	struct nlattr *tx_rates;
5114 	struct ieee80211_supported_band *sband;
5115 	u16 vht_tx_mcs_map, he_tx_mcs_map;
5116 
5117 	memset(mask, 0, sizeof(*mask));
5118 	/* Default to all rates enabled */
5119 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
5120 		const struct ieee80211_sta_he_cap *he_cap;
5121 
5122 		if (!default_all_enabled)
5123 			break;
5124 
5125 		sband = rdev->wiphy.bands[i];
5126 
5127 		if (!sband)
5128 			continue;
5129 
5130 		mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
5131 		memcpy(mask->control[i].ht_mcs,
5132 		       sband->ht_cap.mcs.rx_mask,
5133 		       sizeof(mask->control[i].ht_mcs));
5134 
5135 		if (sband->vht_cap.vht_supported) {
5136 			vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5137 			vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
5138 		}
5139 
5140 		he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5141 		if (!he_cap)
5142 			continue;
5143 
5144 		he_tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5145 		he_build_mcs_mask(he_tx_mcs_map, mask->control[i].he_mcs);
5146 
5147 		mask->control[i].he_gi = 0xFF;
5148 		mask->control[i].he_ltf = 0xFF;
5149 	}
5150 
5151 	/* if no rates are given set it back to the defaults */
5152 	if (!attrs[attr])
5153 		goto out;
5154 
5155 	/* The nested attribute uses enum nl80211_band as the index. This maps
5156 	 * directly to the enum nl80211_band values used in cfg80211.
5157 	 */
5158 	BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
5159 	nla_for_each_nested(tx_rates, attrs[attr], rem) {
5160 		enum nl80211_band band = nla_type(tx_rates);
5161 		int err;
5162 
5163 		if (band < 0 || band >= NUM_NL80211_BANDS)
5164 			return -EINVAL;
5165 		sband = rdev->wiphy.bands[band];
5166 		if (sband == NULL)
5167 			return -EINVAL;
5168 		err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
5169 						  tx_rates,
5170 						  nl80211_txattr_policy,
5171 						  info->extack);
5172 		if (err)
5173 			return err;
5174 		if (tb[NL80211_TXRATE_LEGACY]) {
5175 			mask->control[band].legacy = rateset_to_mask(
5176 				sband,
5177 				nla_data(tb[NL80211_TXRATE_LEGACY]),
5178 				nla_len(tb[NL80211_TXRATE_LEGACY]));
5179 			if ((mask->control[band].legacy == 0) &&
5180 			    nla_len(tb[NL80211_TXRATE_LEGACY]))
5181 				return -EINVAL;
5182 		}
5183 		if (tb[NL80211_TXRATE_HT]) {
5184 			if (!ht_rateset_to_mask(
5185 					sband,
5186 					nla_data(tb[NL80211_TXRATE_HT]),
5187 					nla_len(tb[NL80211_TXRATE_HT]),
5188 					mask->control[band].ht_mcs))
5189 				return -EINVAL;
5190 		}
5191 
5192 		if (tb[NL80211_TXRATE_VHT]) {
5193 			if (!vht_set_mcs_mask(
5194 					sband,
5195 					nla_data(tb[NL80211_TXRATE_VHT]),
5196 					mask->control[band].vht_mcs))
5197 				return -EINVAL;
5198 		}
5199 
5200 		if (tb[NL80211_TXRATE_GI]) {
5201 			mask->control[band].gi =
5202 				nla_get_u8(tb[NL80211_TXRATE_GI]);
5203 			if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
5204 				return -EINVAL;
5205 		}
5206 		if (tb[NL80211_TXRATE_HE] &&
5207 		    !he_set_mcs_mask(info, wdev, sband,
5208 				     nla_data(tb[NL80211_TXRATE_HE]),
5209 				     mask->control[band].he_mcs,
5210 				     link_id))
5211 			return -EINVAL;
5212 
5213 		if (tb[NL80211_TXRATE_HE_GI])
5214 			mask->control[band].he_gi =
5215 				nla_get_u8(tb[NL80211_TXRATE_HE_GI]);
5216 		if (tb[NL80211_TXRATE_HE_LTF])
5217 			mask->control[band].he_ltf =
5218 				nla_get_u8(tb[NL80211_TXRATE_HE_LTF]);
5219 
5220 		if (mask->control[band].legacy == 0) {
5221 			/* don't allow empty legacy rates if HT, VHT or HE
5222 			 * are not even supported.
5223 			 */
5224 			if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
5225 			      rdev->wiphy.bands[band]->vht_cap.vht_supported ||
5226 			      ieee80211_get_he_iftype_cap(sband, wdev->iftype)))
5227 				return -EINVAL;
5228 
5229 			for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5230 				if (mask->control[band].ht_mcs[i])
5231 					goto out;
5232 
5233 			for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5234 				if (mask->control[band].vht_mcs[i])
5235 					goto out;
5236 
5237 			for (i = 0; i < NL80211_HE_NSS_MAX; i++)
5238 				if (mask->control[band].he_mcs[i])
5239 					goto out;
5240 
5241 			/* legacy and mcs rates may not be both empty */
5242 			return -EINVAL;
5243 		}
5244 	}
5245 
5246 out:
5247 	return 0;
5248 }
5249 
5250 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
5251 				   enum nl80211_band band,
5252 				   struct cfg80211_bitrate_mask *beacon_rate)
5253 {
5254 	u32 count_ht, count_vht, count_he, i;
5255 	u32 rate = beacon_rate->control[band].legacy;
5256 
5257 	/* Allow only one rate */
5258 	if (hweight32(rate) > 1)
5259 		return -EINVAL;
5260 
5261 	count_ht = 0;
5262 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
5263 		if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
5264 			return -EINVAL;
5265 		} else if (beacon_rate->control[band].ht_mcs[i]) {
5266 			count_ht++;
5267 			if (count_ht > 1)
5268 				return -EINVAL;
5269 		}
5270 		if (count_ht && rate)
5271 			return -EINVAL;
5272 	}
5273 
5274 	count_vht = 0;
5275 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5276 		if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
5277 			return -EINVAL;
5278 		} else if (beacon_rate->control[band].vht_mcs[i]) {
5279 			count_vht++;
5280 			if (count_vht > 1)
5281 				return -EINVAL;
5282 		}
5283 		if (count_vht && rate)
5284 			return -EINVAL;
5285 	}
5286 
5287 	count_he = 0;
5288 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5289 		if (hweight16(beacon_rate->control[band].he_mcs[i]) > 1) {
5290 			return -EINVAL;
5291 		} else if (beacon_rate->control[band].he_mcs[i]) {
5292 			count_he++;
5293 			if (count_he > 1)
5294 				return -EINVAL;
5295 		}
5296 		if (count_he && rate)
5297 			return -EINVAL;
5298 	}
5299 
5300 	if ((count_ht && count_vht && count_he) ||
5301 	    (!rate && !count_ht && !count_vht && !count_he))
5302 		return -EINVAL;
5303 
5304 	if (rate &&
5305 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5306 				     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
5307 		return -EINVAL;
5308 	if (count_ht &&
5309 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5310 				     NL80211_EXT_FEATURE_BEACON_RATE_HT))
5311 		return -EINVAL;
5312 	if (count_vht &&
5313 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5314 				     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
5315 		return -EINVAL;
5316 	if (count_he &&
5317 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5318 				     NL80211_EXT_FEATURE_BEACON_RATE_HE))
5319 		return -EINVAL;
5320 
5321 	return 0;
5322 }
5323 
5324 static int nl80211_parse_mbssid_config(struct wiphy *wiphy,
5325 				       struct net_device *dev,
5326 				       struct nlattr *attrs,
5327 				       struct cfg80211_mbssid_config *config,
5328 				       u8 num_elems)
5329 {
5330 	struct nlattr *tb[NL80211_MBSSID_CONFIG_ATTR_MAX + 1];
5331 
5332 	if (!wiphy->mbssid_max_interfaces)
5333 		return -EOPNOTSUPP;
5334 
5335 	if (nla_parse_nested(tb, NL80211_MBSSID_CONFIG_ATTR_MAX, attrs, NULL,
5336 			     NULL) ||
5337 	    !tb[NL80211_MBSSID_CONFIG_ATTR_INDEX])
5338 		return -EINVAL;
5339 
5340 	config->ema = nla_get_flag(tb[NL80211_MBSSID_CONFIG_ATTR_EMA]);
5341 	if (config->ema) {
5342 		if (!wiphy->ema_max_profile_periodicity)
5343 			return -EOPNOTSUPP;
5344 
5345 		if (num_elems > wiphy->ema_max_profile_periodicity)
5346 			return -EINVAL;
5347 	}
5348 
5349 	config->index = nla_get_u8(tb[NL80211_MBSSID_CONFIG_ATTR_INDEX]);
5350 	if (config->index >= wiphy->mbssid_max_interfaces ||
5351 	    (!config->index && !num_elems))
5352 		return -EINVAL;
5353 
5354 	if (tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]) {
5355 		u32 tx_ifindex =
5356 			nla_get_u32(tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]);
5357 
5358 		if ((!config->index && tx_ifindex != dev->ifindex) ||
5359 		    (config->index && tx_ifindex == dev->ifindex))
5360 			return -EINVAL;
5361 
5362 		if (tx_ifindex != dev->ifindex) {
5363 			struct net_device *tx_netdev =
5364 				dev_get_by_index(wiphy_net(wiphy), tx_ifindex);
5365 
5366 			if (!tx_netdev || !tx_netdev->ieee80211_ptr ||
5367 			    tx_netdev->ieee80211_ptr->wiphy != wiphy ||
5368 			    tx_netdev->ieee80211_ptr->iftype !=
5369 							NL80211_IFTYPE_AP) {
5370 				dev_put(tx_netdev);
5371 				return -EINVAL;
5372 			}
5373 
5374 			config->tx_wdev = tx_netdev->ieee80211_ptr;
5375 		} else {
5376 			config->tx_wdev = dev->ieee80211_ptr;
5377 		}
5378 	} else if (!config->index) {
5379 		config->tx_wdev = dev->ieee80211_ptr;
5380 	} else {
5381 		return -EINVAL;
5382 	}
5383 
5384 	return 0;
5385 }
5386 
5387 static struct cfg80211_mbssid_elems *
5388 nl80211_parse_mbssid_elems(struct wiphy *wiphy, struct nlattr *attrs)
5389 {
5390 	struct nlattr *nl_elems;
5391 	struct cfg80211_mbssid_elems *elems;
5392 	int rem_elems;
5393 	u8 i = 0, num_elems = 0;
5394 
5395 	if (!wiphy->mbssid_max_interfaces)
5396 		return ERR_PTR(-EINVAL);
5397 
5398 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5399 		if (num_elems >= 255)
5400 			return ERR_PTR(-EINVAL);
5401 		num_elems++;
5402 	}
5403 
5404 	elems = kzalloc(struct_size(elems, elem, num_elems), GFP_KERNEL);
5405 	if (!elems)
5406 		return ERR_PTR(-ENOMEM);
5407 	elems->cnt = num_elems;
5408 
5409 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5410 		elems->elem[i].data = nla_data(nl_elems);
5411 		elems->elem[i].len = nla_len(nl_elems);
5412 		i++;
5413 	}
5414 	return elems;
5415 }
5416 
5417 static struct cfg80211_rnr_elems *
5418 nl80211_parse_rnr_elems(struct wiphy *wiphy, struct nlattr *attrs,
5419 			struct netlink_ext_ack *extack)
5420 {
5421 	struct nlattr *nl_elems;
5422 	struct cfg80211_rnr_elems *elems;
5423 	int rem_elems;
5424 	u8 i = 0, num_elems = 0;
5425 
5426 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5427 		int ret;
5428 
5429 		ret = validate_ie_attr(nl_elems, extack);
5430 		if (ret)
5431 			return ERR_PTR(ret);
5432 
5433 		num_elems++;
5434 	}
5435 
5436 	elems = kzalloc(struct_size(elems, elem, num_elems), GFP_KERNEL);
5437 	if (!elems)
5438 		return ERR_PTR(-ENOMEM);
5439 	elems->cnt = num_elems;
5440 
5441 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5442 		elems->elem[i].data = nla_data(nl_elems);
5443 		elems->elem[i].len = nla_len(nl_elems);
5444 		i++;
5445 	}
5446 	return elems;
5447 }
5448 
5449 static int nl80211_parse_he_bss_color(struct nlattr *attrs,
5450 				      struct cfg80211_he_bss_color *he_bss_color)
5451 {
5452 	struct nlattr *tb[NL80211_HE_BSS_COLOR_ATTR_MAX + 1];
5453 	int err;
5454 
5455 	err = nla_parse_nested(tb, NL80211_HE_BSS_COLOR_ATTR_MAX, attrs,
5456 			       he_bss_color_policy, NULL);
5457 	if (err)
5458 		return err;
5459 
5460 	if (!tb[NL80211_HE_BSS_COLOR_ATTR_COLOR])
5461 		return -EINVAL;
5462 
5463 	he_bss_color->color =
5464 		nla_get_u8(tb[NL80211_HE_BSS_COLOR_ATTR_COLOR]);
5465 	he_bss_color->enabled =
5466 		!nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_DISABLED]);
5467 	he_bss_color->partial =
5468 		nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_PARTIAL]);
5469 
5470 	return 0;
5471 }
5472 
5473 static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
5474 				struct nlattr *attrs[],
5475 				struct cfg80211_beacon_data *bcn,
5476 				struct netlink_ext_ack *extack)
5477 {
5478 	bool haveinfo = false;
5479 	int err;
5480 
5481 	memset(bcn, 0, sizeof(*bcn));
5482 
5483 	bcn->link_id = nl80211_link_id(attrs);
5484 
5485 	if (attrs[NL80211_ATTR_BEACON_HEAD]) {
5486 		bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
5487 		bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
5488 		if (!bcn->head_len)
5489 			return -EINVAL;
5490 		haveinfo = true;
5491 	}
5492 
5493 	if (attrs[NL80211_ATTR_BEACON_TAIL]) {
5494 		bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
5495 		bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
5496 		haveinfo = true;
5497 	}
5498 
5499 	if (!haveinfo)
5500 		return -EINVAL;
5501 
5502 	if (attrs[NL80211_ATTR_IE]) {
5503 		bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
5504 		bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
5505 	}
5506 
5507 	if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
5508 		bcn->proberesp_ies =
5509 			nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5510 		bcn->proberesp_ies_len =
5511 			nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5512 	}
5513 
5514 	if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
5515 		bcn->assocresp_ies =
5516 			nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5517 		bcn->assocresp_ies_len =
5518 			nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5519 	}
5520 
5521 	if (attrs[NL80211_ATTR_PROBE_RESP]) {
5522 		bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
5523 		bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
5524 	}
5525 
5526 	if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
5527 		struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
5528 
5529 		err = nla_parse_nested_deprecated(tb,
5530 						  NL80211_FTM_RESP_ATTR_MAX,
5531 						  attrs[NL80211_ATTR_FTM_RESPONDER],
5532 						  NULL, NULL);
5533 		if (err)
5534 			return err;
5535 
5536 		if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
5537 		    wiphy_ext_feature_isset(&rdev->wiphy,
5538 					    NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
5539 			bcn->ftm_responder = 1;
5540 		else
5541 			return -EOPNOTSUPP;
5542 
5543 		if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
5544 			bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
5545 			bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
5546 		}
5547 
5548 		if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
5549 			bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5550 			bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5551 		}
5552 	} else {
5553 		bcn->ftm_responder = -1;
5554 	}
5555 
5556 	if (attrs[NL80211_ATTR_HE_BSS_COLOR]) {
5557 		err = nl80211_parse_he_bss_color(attrs[NL80211_ATTR_HE_BSS_COLOR],
5558 						 &bcn->he_bss_color);
5559 		if (err)
5560 			return err;
5561 		bcn->he_bss_color_valid = true;
5562 	}
5563 
5564 	if (attrs[NL80211_ATTR_MBSSID_ELEMS]) {
5565 		struct cfg80211_mbssid_elems *mbssid =
5566 			nl80211_parse_mbssid_elems(&rdev->wiphy,
5567 						   attrs[NL80211_ATTR_MBSSID_ELEMS]);
5568 
5569 		if (IS_ERR(mbssid))
5570 			return PTR_ERR(mbssid);
5571 
5572 		bcn->mbssid_ies = mbssid;
5573 
5574 		if (bcn->mbssid_ies && attrs[NL80211_ATTR_EMA_RNR_ELEMS]) {
5575 			struct cfg80211_rnr_elems *rnr =
5576 				nl80211_parse_rnr_elems(&rdev->wiphy,
5577 							attrs[NL80211_ATTR_EMA_RNR_ELEMS],
5578 							extack);
5579 
5580 			if (IS_ERR(rnr))
5581 				return PTR_ERR(rnr);
5582 
5583 			if (rnr && rnr->cnt < bcn->mbssid_ies->cnt)
5584 				return -EINVAL;
5585 
5586 			bcn->rnr_ies = rnr;
5587 		}
5588 	}
5589 
5590 	return 0;
5591 }
5592 
5593 static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
5594 				    struct ieee80211_he_obss_pd *he_obss_pd)
5595 {
5596 	struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
5597 	int err;
5598 
5599 	err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
5600 			       he_obss_pd_policy, NULL);
5601 	if (err)
5602 		return err;
5603 
5604 	if (!tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL])
5605 		return -EINVAL;
5606 
5607 	he_obss_pd->sr_ctrl = nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL]);
5608 
5609 	if (tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET])
5610 		he_obss_pd->min_offset =
5611 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
5612 	if (tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
5613 		he_obss_pd->max_offset =
5614 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
5615 	if (tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET])
5616 		he_obss_pd->non_srg_max_offset =
5617 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET]);
5618 
5619 	if (he_obss_pd->min_offset > he_obss_pd->max_offset)
5620 		return -EINVAL;
5621 
5622 	if (tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP])
5623 		memcpy(he_obss_pd->bss_color_bitmap,
5624 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP]),
5625 		       sizeof(he_obss_pd->bss_color_bitmap));
5626 
5627 	if (tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP])
5628 		memcpy(he_obss_pd->partial_bssid_bitmap,
5629 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP]),
5630 		       sizeof(he_obss_pd->partial_bssid_bitmap));
5631 
5632 	he_obss_pd->enable = true;
5633 
5634 	return 0;
5635 }
5636 
5637 static int nl80211_parse_fils_discovery(struct cfg80211_registered_device *rdev,
5638 					struct nlattr *attrs,
5639 					struct cfg80211_fils_discovery *fd)
5640 {
5641 	struct nlattr *tb[NL80211_FILS_DISCOVERY_ATTR_MAX + 1];
5642 	int ret;
5643 
5644 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5645 				     NL80211_EXT_FEATURE_FILS_DISCOVERY))
5646 		return -EINVAL;
5647 
5648 	ret = nla_parse_nested(tb, NL80211_FILS_DISCOVERY_ATTR_MAX, attrs,
5649 			       NULL, NULL);
5650 	if (ret)
5651 		return ret;
5652 
5653 	if (!tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] &&
5654 	    !tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] &&
5655 	    !tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]) {
5656 		fd->update = true;
5657 		return 0;
5658 	}
5659 
5660 	if (!tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] ||
5661 	    !tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] ||
5662 	    !tb[NL80211_FILS_DISCOVERY_ATTR_TMPL])
5663 		return -EINVAL;
5664 
5665 	fd->tmpl_len = nla_len(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5666 	fd->tmpl = nla_data(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5667 	fd->min_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN]);
5668 	fd->max_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX]);
5669 	fd->update = true;
5670 	return 0;
5671 }
5672 
5673 static int
5674 nl80211_parse_unsol_bcast_probe_resp(struct cfg80211_registered_device *rdev,
5675 				     struct nlattr *attrs,
5676 				     struct cfg80211_unsol_bcast_probe_resp *presp)
5677 {
5678 	struct nlattr *tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1];
5679 	int ret;
5680 
5681 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5682 				     NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP))
5683 		return -EINVAL;
5684 
5685 	ret = nla_parse_nested(tb, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX,
5686 			       attrs, NULL, NULL);
5687 	if (ret)
5688 		return ret;
5689 
5690 	if (!tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] &&
5691 	    !tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]) {
5692 		presp->update = true;
5693 		return 0;
5694 	}
5695 
5696 	if (!tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] ||
5697 	    !tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL])
5698 		return -EINVAL;
5699 
5700 	presp->tmpl = nla_data(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5701 	presp->tmpl_len = nla_len(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5702 	presp->interval = nla_get_u32(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT]);
5703 	presp->update = true;
5704 	return 0;
5705 }
5706 
5707 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
5708 					    const struct element *rates)
5709 {
5710 	int i;
5711 
5712 	if (!rates)
5713 		return;
5714 
5715 	for (i = 0; i < rates->datalen; i++) {
5716 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
5717 			params->ht_required = true;
5718 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
5719 			params->vht_required = true;
5720 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HE_PHY)
5721 			params->he_required = true;
5722 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_SAE_H2E)
5723 			params->sae_h2e_required = true;
5724 	}
5725 }
5726 
5727 /*
5728  * Since the nl80211 API didn't include, from the beginning, attributes about
5729  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
5730  * benefit of drivers that rebuild IEs in the firmware.
5731  */
5732 static int nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
5733 {
5734 	const struct cfg80211_beacon_data *bcn = &params->beacon;
5735 	size_t ies_len = bcn->tail_len;
5736 	const u8 *ies = bcn->tail;
5737 	const struct element *rates;
5738 	const struct element *cap;
5739 
5740 	rates = cfg80211_find_elem(WLAN_EID_SUPP_RATES, ies, ies_len);
5741 	nl80211_check_ap_rate_selectors(params, rates);
5742 
5743 	rates = cfg80211_find_elem(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
5744 	nl80211_check_ap_rate_selectors(params, rates);
5745 
5746 	cap = cfg80211_find_elem(WLAN_EID_HT_CAPABILITY, ies, ies_len);
5747 	if (cap && cap->datalen >= sizeof(*params->ht_cap))
5748 		params->ht_cap = (void *)cap->data;
5749 	cap = cfg80211_find_elem(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
5750 	if (cap && cap->datalen >= sizeof(*params->vht_cap))
5751 		params->vht_cap = (void *)cap->data;
5752 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
5753 	if (cap && cap->datalen >= sizeof(*params->he_cap) + 1)
5754 		params->he_cap = (void *)(cap->data + 1);
5755 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ies, ies_len);
5756 	if (cap && cap->datalen >= sizeof(*params->he_oper) + 1)
5757 		params->he_oper = (void *)(cap->data + 1);
5758 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_CAPABILITY, ies, ies_len);
5759 	if (cap) {
5760 		if (!cap->datalen)
5761 			return -EINVAL;
5762 		params->eht_cap = (void *)(cap->data + 1);
5763 		if (!ieee80211_eht_capa_size_ok((const u8 *)params->he_cap,
5764 						(const u8 *)params->eht_cap,
5765 						cap->datalen - 1, true))
5766 			return -EINVAL;
5767 	}
5768 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_OPERATION, ies, ies_len);
5769 	if (cap) {
5770 		if (!cap->datalen)
5771 			return -EINVAL;
5772 		params->eht_oper = (void *)(cap->data + 1);
5773 		if (!ieee80211_eht_oper_size_ok((const u8 *)params->eht_oper,
5774 						cap->datalen - 1))
5775 			return -EINVAL;
5776 	}
5777 	return 0;
5778 }
5779 
5780 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
5781 				   struct cfg80211_ap_settings *params)
5782 {
5783 	struct wireless_dev *wdev;
5784 
5785 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
5786 		if (wdev->iftype != NL80211_IFTYPE_AP &&
5787 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)
5788 			continue;
5789 
5790 		if (!wdev->u.ap.preset_chandef.chan)
5791 			continue;
5792 
5793 		params->chandef = wdev->u.ap.preset_chandef;
5794 		return true;
5795 	}
5796 
5797 	return false;
5798 }
5799 
5800 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
5801 				    enum nl80211_auth_type auth_type,
5802 				    enum nl80211_commands cmd)
5803 {
5804 	if (auth_type > NL80211_AUTHTYPE_MAX)
5805 		return false;
5806 
5807 	switch (cmd) {
5808 	case NL80211_CMD_AUTHENTICATE:
5809 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5810 		    auth_type == NL80211_AUTHTYPE_SAE)
5811 			return false;
5812 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5813 					     NL80211_EXT_FEATURE_FILS_STA) &&
5814 		    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5815 		     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5816 		     auth_type == NL80211_AUTHTYPE_FILS_PK))
5817 			return false;
5818 		return true;
5819 	case NL80211_CMD_CONNECT:
5820 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5821 		    !wiphy_ext_feature_isset(&rdev->wiphy,
5822 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
5823 		    auth_type == NL80211_AUTHTYPE_SAE)
5824 			return false;
5825 
5826 		/* FILS with SK PFS or PK not supported yet */
5827 		if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5828 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5829 			return false;
5830 		if (!wiphy_ext_feature_isset(
5831 			    &rdev->wiphy,
5832 			    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
5833 		    auth_type == NL80211_AUTHTYPE_FILS_SK)
5834 			return false;
5835 		return true;
5836 	case NL80211_CMD_START_AP:
5837 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5838 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP) &&
5839 		    auth_type == NL80211_AUTHTYPE_SAE)
5840 			return false;
5841 		/* FILS not supported yet */
5842 		if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5843 		    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5844 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5845 			return false;
5846 		return true;
5847 	default:
5848 		return false;
5849 	}
5850 }
5851 
5852 static void nl80211_send_ap_started(struct wireless_dev *wdev,
5853 				    unsigned int link_id)
5854 {
5855 	struct wiphy *wiphy = wdev->wiphy;
5856 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
5857 	struct sk_buff *msg;
5858 	void *hdr;
5859 
5860 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5861 	if (!msg)
5862 		return;
5863 
5864 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_START_AP);
5865 	if (!hdr)
5866 		goto out;
5867 
5868 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
5869 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
5870 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
5871 			      NL80211_ATTR_PAD) ||
5872 	    (wdev->u.ap.ssid_len &&
5873 	     nla_put(msg, NL80211_ATTR_SSID, wdev->u.ap.ssid_len,
5874 		     wdev->u.ap.ssid)) ||
5875 	    (wdev->valid_links &&
5876 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)))
5877 		goto out;
5878 
5879 	genlmsg_end(msg, hdr);
5880 
5881 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
5882 				NL80211_MCGRP_MLME, GFP_KERNEL);
5883 	return;
5884 out:
5885 	nlmsg_free(msg);
5886 }
5887 
5888 static int nl80211_validate_ap_phy_operation(struct cfg80211_ap_settings *params)
5889 {
5890 	struct ieee80211_channel *channel = params->chandef.chan;
5891 
5892 	if ((params->he_cap ||  params->he_oper) &&
5893 	    (channel->flags & IEEE80211_CHAN_NO_HE))
5894 		return -EOPNOTSUPP;
5895 
5896 	if ((params->eht_cap || params->eht_oper) &&
5897 	    (channel->flags & IEEE80211_CHAN_NO_EHT))
5898 		return -EOPNOTSUPP;
5899 
5900 	return 0;
5901 }
5902 
5903 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
5904 {
5905 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5906 	unsigned int link_id = nl80211_link_id(info->attrs);
5907 	struct net_device *dev = info->user_ptr[1];
5908 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5909 	struct cfg80211_ap_settings *params;
5910 	int err;
5911 
5912 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5913 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5914 		return -EOPNOTSUPP;
5915 
5916 	if (!rdev->ops->start_ap)
5917 		return -EOPNOTSUPP;
5918 
5919 	if (wdev->links[link_id].ap.beacon_interval)
5920 		return -EALREADY;
5921 
5922 	/* these are required for START_AP */
5923 	if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
5924 	    !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
5925 	    !info->attrs[NL80211_ATTR_BEACON_HEAD])
5926 		return -EINVAL;
5927 
5928 	params = kzalloc(sizeof(*params), GFP_KERNEL);
5929 	if (!params)
5930 		return -ENOMEM;
5931 
5932 	err = nl80211_parse_beacon(rdev, info->attrs, &params->beacon,
5933 				   info->extack);
5934 	if (err)
5935 		goto out;
5936 
5937 	params->beacon_interval =
5938 		nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5939 	params->dtim_period =
5940 		nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
5941 
5942 	err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
5943 					   params->beacon_interval);
5944 	if (err)
5945 		goto out;
5946 
5947 	/*
5948 	 * In theory, some of these attributes should be required here
5949 	 * but since they were not used when the command was originally
5950 	 * added, keep them optional for old user space programs to let
5951 	 * them continue to work with drivers that do not need the
5952 	 * additional information -- drivers must check!
5953 	 */
5954 	if (info->attrs[NL80211_ATTR_SSID]) {
5955 		params->ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5956 		params->ssid_len =
5957 			nla_len(info->attrs[NL80211_ATTR_SSID]);
5958 		if (params->ssid_len == 0) {
5959 			err = -EINVAL;
5960 			goto out;
5961 		}
5962 
5963 		if (wdev->u.ap.ssid_len &&
5964 		    (wdev->u.ap.ssid_len != params->ssid_len ||
5965 		     memcmp(wdev->u.ap.ssid, params->ssid, params->ssid_len))) {
5966 			/* require identical SSID for MLO */
5967 			err = -EINVAL;
5968 			goto out;
5969 		}
5970 	} else if (wdev->valid_links) {
5971 		/* require SSID for MLO */
5972 		err = -EINVAL;
5973 		goto out;
5974 	}
5975 
5976 	if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
5977 		params->hidden_ssid = nla_get_u32(
5978 			info->attrs[NL80211_ATTR_HIDDEN_SSID]);
5979 
5980 	params->privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
5981 
5982 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5983 		params->auth_type = nla_get_u32(
5984 			info->attrs[NL80211_ATTR_AUTH_TYPE]);
5985 		if (!nl80211_valid_auth_type(rdev, params->auth_type,
5986 					     NL80211_CMD_START_AP)) {
5987 			err = -EINVAL;
5988 			goto out;
5989 		}
5990 	} else
5991 		params->auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5992 
5993 	err = nl80211_crypto_settings(rdev, info, &params->crypto,
5994 				      NL80211_MAX_NR_CIPHER_SUITES);
5995 	if (err)
5996 		goto out;
5997 
5998 	if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
5999 		if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER)) {
6000 			err = -EOPNOTSUPP;
6001 			goto out;
6002 		}
6003 		params->inactivity_timeout = nla_get_u16(
6004 			info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
6005 	}
6006 
6007 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
6008 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
6009 			err = -EINVAL;
6010 			goto out;
6011 		}
6012 		params->p2p_ctwindow =
6013 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
6014 		if (params->p2p_ctwindow != 0 &&
6015 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN)) {
6016 			err = -EINVAL;
6017 			goto out;
6018 		}
6019 	}
6020 
6021 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
6022 		u8 tmp;
6023 
6024 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
6025 			err = -EINVAL;
6026 			goto out;
6027 		}
6028 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
6029 		params->p2p_opp_ps = tmp;
6030 		if (params->p2p_opp_ps != 0 &&
6031 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS)) {
6032 			err = -EINVAL;
6033 			goto out;
6034 		}
6035 	}
6036 
6037 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6038 		err = nl80211_parse_chandef(rdev, info, &params->chandef);
6039 		if (err)
6040 			goto out;
6041 	} else if (wdev->valid_links) {
6042 		/* with MLD need to specify the channel configuration */
6043 		err = -EINVAL;
6044 		goto out;
6045 	} else if (wdev->u.ap.preset_chandef.chan) {
6046 		params->chandef = wdev->u.ap.preset_chandef;
6047 	} else if (!nl80211_get_ap_channel(rdev, params)) {
6048 		err = -EINVAL;
6049 		goto out;
6050 	}
6051 
6052 	if (info->attrs[NL80211_ATTR_PUNCT_BITMAP]) {
6053 		err = nl80211_parse_punct_bitmap(rdev, info,
6054 						 &params->chandef,
6055 						 &params->punct_bitmap);
6056 		if (err)
6057 			goto out;
6058 	}
6059 
6060 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params->chandef,
6061 					   wdev->iftype)) {
6062 		err = -EINVAL;
6063 		goto out;
6064 	}
6065 
6066 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
6067 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
6068 						    NL80211_ATTR_TX_RATES,
6069 						    &params->beacon_rate,
6070 						    dev, false, link_id);
6071 		if (err)
6072 			goto out;
6073 
6074 		err = validate_beacon_tx_rate(rdev, params->chandef.chan->band,
6075 					      &params->beacon_rate);
6076 		if (err)
6077 			goto out;
6078 	}
6079 
6080 	if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
6081 		params->smps_mode =
6082 			nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
6083 		switch (params->smps_mode) {
6084 		case NL80211_SMPS_OFF:
6085 			break;
6086 		case NL80211_SMPS_STATIC:
6087 			if (!(rdev->wiphy.features &
6088 			      NL80211_FEATURE_STATIC_SMPS)) {
6089 				err = -EINVAL;
6090 				goto out;
6091 			}
6092 			break;
6093 		case NL80211_SMPS_DYNAMIC:
6094 			if (!(rdev->wiphy.features &
6095 			      NL80211_FEATURE_DYNAMIC_SMPS)) {
6096 				err = -EINVAL;
6097 				goto out;
6098 			}
6099 			break;
6100 		default:
6101 			err = -EINVAL;
6102 			goto out;
6103 		}
6104 	} else {
6105 		params->smps_mode = NL80211_SMPS_OFF;
6106 	}
6107 
6108 	params->pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
6109 	if (params->pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
6110 		err = -EOPNOTSUPP;
6111 		goto out;
6112 	}
6113 
6114 	if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
6115 		params->acl = parse_acl_data(&rdev->wiphy, info);
6116 		if (IS_ERR(params->acl)) {
6117 			err = PTR_ERR(params->acl);
6118 			params->acl = NULL;
6119 			goto out;
6120 		}
6121 	}
6122 
6123 	params->twt_responder =
6124 		    nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
6125 
6126 	if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
6127 		err = nl80211_parse_he_obss_pd(
6128 					info->attrs[NL80211_ATTR_HE_OBSS_PD],
6129 					&params->he_obss_pd);
6130 		if (err)
6131 			goto out;
6132 	}
6133 
6134 	if (info->attrs[NL80211_ATTR_FILS_DISCOVERY]) {
6135 		err = nl80211_parse_fils_discovery(rdev,
6136 						   info->attrs[NL80211_ATTR_FILS_DISCOVERY],
6137 						   &params->fils_discovery);
6138 		if (err)
6139 			goto out;
6140 	}
6141 
6142 	if (info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP]) {
6143 		err = nl80211_parse_unsol_bcast_probe_resp(
6144 			rdev, info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP],
6145 			&params->unsol_bcast_probe_resp);
6146 		if (err)
6147 			goto out;
6148 	}
6149 
6150 	if (info->attrs[NL80211_ATTR_MBSSID_CONFIG]) {
6151 		err = nl80211_parse_mbssid_config(&rdev->wiphy, dev,
6152 						  info->attrs[NL80211_ATTR_MBSSID_CONFIG],
6153 						  &params->mbssid_config,
6154 						  params->beacon.mbssid_ies ?
6155 							params->beacon.mbssid_ies->cnt :
6156 							0);
6157 		if (err)
6158 			goto out;
6159 	}
6160 
6161 	if (!params->mbssid_config.ema && params->beacon.rnr_ies) {
6162 		err = -EINVAL;
6163 		goto out;
6164 	}
6165 
6166 	err = nl80211_calculate_ap_params(params);
6167 	if (err)
6168 		goto out;
6169 
6170 	err = nl80211_validate_ap_phy_operation(params);
6171 	if (err)
6172 		goto out;
6173 
6174 	if (info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS])
6175 		params->flags = nla_get_u32(
6176 			info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS]);
6177 	else if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
6178 		params->flags |= NL80211_AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
6179 
6180 	if (wdev->conn_owner_nlportid &&
6181 	    info->attrs[NL80211_ATTR_SOCKET_OWNER] &&
6182 	    wdev->conn_owner_nlportid != info->snd_portid) {
6183 		err = -EINVAL;
6184 		goto out;
6185 	}
6186 
6187 	/* FIXME: validate MLO/link-id against driver capabilities */
6188 
6189 	err = rdev_start_ap(rdev, dev, params);
6190 	if (!err) {
6191 		wdev->links[link_id].ap.beacon_interval = params->beacon_interval;
6192 		wdev->links[link_id].ap.chandef = params->chandef;
6193 		wdev->u.ap.ssid_len = params->ssid_len;
6194 		memcpy(wdev->u.ap.ssid, params->ssid,
6195 		       params->ssid_len);
6196 
6197 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
6198 			wdev->conn_owner_nlportid = info->snd_portid;
6199 
6200 		nl80211_send_ap_started(wdev, link_id);
6201 	}
6202 out:
6203 	kfree(params->acl);
6204 	kfree(params->beacon.mbssid_ies);
6205 	if (params->mbssid_config.tx_wdev &&
6206 	    params->mbssid_config.tx_wdev->netdev &&
6207 	    params->mbssid_config.tx_wdev->netdev != dev)
6208 		dev_put(params->mbssid_config.tx_wdev->netdev);
6209 	kfree(params->beacon.rnr_ies);
6210 	kfree(params);
6211 
6212 	return err;
6213 }
6214 
6215 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
6216 {
6217 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6218 	unsigned int link_id = nl80211_link_id(info->attrs);
6219 	struct net_device *dev = info->user_ptr[1];
6220 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6221 	struct cfg80211_ap_update *params;
6222 	struct nlattr *attr;
6223 	int err;
6224 
6225 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6226 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6227 		return -EOPNOTSUPP;
6228 
6229 	if (!rdev->ops->change_beacon)
6230 		return -EOPNOTSUPP;
6231 
6232 	if (!wdev->links[link_id].ap.beacon_interval)
6233 		return -EINVAL;
6234 
6235 	params = kzalloc(sizeof(*params), GFP_KERNEL);
6236 	if (!params)
6237 		return -ENOMEM;
6238 
6239 	err = nl80211_parse_beacon(rdev, info->attrs, &params->beacon,
6240 				   info->extack);
6241 	if (err)
6242 		goto out;
6243 
6244 	attr = info->attrs[NL80211_ATTR_FILS_DISCOVERY];
6245 	if (attr) {
6246 		err = nl80211_parse_fils_discovery(rdev, attr,
6247 						   &params->fils_discovery);
6248 		if (err)
6249 			goto out;
6250 	}
6251 
6252 	attr = info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP];
6253 	if (attr) {
6254 		err = nl80211_parse_unsol_bcast_probe_resp(rdev, attr,
6255 							   &params->unsol_bcast_probe_resp);
6256 		if (err)
6257 			goto out;
6258 	}
6259 
6260 	err = rdev_change_beacon(rdev, dev, params);
6261 
6262 out:
6263 	kfree(params->beacon.mbssid_ies);
6264 	kfree(params->beacon.rnr_ies);
6265 	kfree(params);
6266 	return err;
6267 }
6268 
6269 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
6270 {
6271 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6272 	unsigned int link_id = nl80211_link_id(info->attrs);
6273 	struct net_device *dev = info->user_ptr[1];
6274 
6275 	return cfg80211_stop_ap(rdev, dev, link_id, false);
6276 }
6277 
6278 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
6279 	[NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
6280 	[NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
6281 	[NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
6282 	[NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
6283 	[NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
6284 	[NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
6285 };
6286 
6287 static int parse_station_flags(struct genl_info *info,
6288 			       enum nl80211_iftype iftype,
6289 			       struct station_parameters *params)
6290 {
6291 	struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
6292 	struct nlattr *nla;
6293 	int flag;
6294 
6295 	/*
6296 	 * Try parsing the new attribute first so userspace
6297 	 * can specify both for older kernels.
6298 	 */
6299 	nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
6300 	if (nla) {
6301 		struct nl80211_sta_flag_update *sta_flags;
6302 
6303 		sta_flags = nla_data(nla);
6304 		params->sta_flags_mask = sta_flags->mask;
6305 		params->sta_flags_set = sta_flags->set;
6306 		params->sta_flags_set &= params->sta_flags_mask;
6307 		if ((params->sta_flags_mask |
6308 		     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
6309 			return -EINVAL;
6310 		return 0;
6311 	}
6312 
6313 	/* if present, parse the old attribute */
6314 
6315 	nla = info->attrs[NL80211_ATTR_STA_FLAGS];
6316 	if (!nla)
6317 		return 0;
6318 
6319 	if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
6320 		return -EINVAL;
6321 
6322 	/*
6323 	 * Only allow certain flags for interface types so that
6324 	 * other attributes are silently ignored. Remember that
6325 	 * this is backward compatibility code with old userspace
6326 	 * and shouldn't be hit in other cases anyway.
6327 	 */
6328 	switch (iftype) {
6329 	case NL80211_IFTYPE_AP:
6330 	case NL80211_IFTYPE_AP_VLAN:
6331 	case NL80211_IFTYPE_P2P_GO:
6332 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6333 					 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6334 					 BIT(NL80211_STA_FLAG_WME) |
6335 					 BIT(NL80211_STA_FLAG_MFP);
6336 		break;
6337 	case NL80211_IFTYPE_P2P_CLIENT:
6338 	case NL80211_IFTYPE_STATION:
6339 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6340 					 BIT(NL80211_STA_FLAG_TDLS_PEER);
6341 		break;
6342 	case NL80211_IFTYPE_MESH_POINT:
6343 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6344 					 BIT(NL80211_STA_FLAG_MFP) |
6345 					 BIT(NL80211_STA_FLAG_AUTHORIZED);
6346 		break;
6347 	default:
6348 		return -EINVAL;
6349 	}
6350 
6351 	for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
6352 		if (flags[flag]) {
6353 			params->sta_flags_set |= (1<<flag);
6354 
6355 			/* no longer support new API additions in old API */
6356 			if (flag > NL80211_STA_FLAG_MAX_OLD_API)
6357 				return -EINVAL;
6358 		}
6359 	}
6360 
6361 	return 0;
6362 }
6363 
6364 bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
6365 {
6366 	struct nlattr *rate;
6367 	u32 bitrate;
6368 	u16 bitrate_compat;
6369 	enum nl80211_rate_info rate_flg;
6370 
6371 	rate = nla_nest_start_noflag(msg, attr);
6372 	if (!rate)
6373 		return false;
6374 
6375 	/* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
6376 	bitrate = cfg80211_calculate_bitrate(info);
6377 	/* report 16-bit bitrate only if we can */
6378 	bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
6379 	if (bitrate > 0 &&
6380 	    nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
6381 		return false;
6382 	if (bitrate_compat > 0 &&
6383 	    nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
6384 		return false;
6385 
6386 	switch (info->bw) {
6387 	case RATE_INFO_BW_1:
6388 		rate_flg = NL80211_RATE_INFO_1_MHZ_WIDTH;
6389 		break;
6390 	case RATE_INFO_BW_2:
6391 		rate_flg = NL80211_RATE_INFO_2_MHZ_WIDTH;
6392 		break;
6393 	case RATE_INFO_BW_4:
6394 		rate_flg = NL80211_RATE_INFO_4_MHZ_WIDTH;
6395 		break;
6396 	case RATE_INFO_BW_5:
6397 		rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
6398 		break;
6399 	case RATE_INFO_BW_8:
6400 		rate_flg = NL80211_RATE_INFO_8_MHZ_WIDTH;
6401 		break;
6402 	case RATE_INFO_BW_10:
6403 		rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
6404 		break;
6405 	case RATE_INFO_BW_16:
6406 		rate_flg = NL80211_RATE_INFO_16_MHZ_WIDTH;
6407 		break;
6408 	default:
6409 		WARN_ON(1);
6410 		fallthrough;
6411 	case RATE_INFO_BW_20:
6412 		rate_flg = 0;
6413 		break;
6414 	case RATE_INFO_BW_40:
6415 		rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
6416 		break;
6417 	case RATE_INFO_BW_80:
6418 		rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
6419 		break;
6420 	case RATE_INFO_BW_160:
6421 		rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
6422 		break;
6423 	case RATE_INFO_BW_HE_RU:
6424 		rate_flg = 0;
6425 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
6426 		break;
6427 	case RATE_INFO_BW_320:
6428 		rate_flg = NL80211_RATE_INFO_320_MHZ_WIDTH;
6429 		break;
6430 	case RATE_INFO_BW_EHT_RU:
6431 		rate_flg = 0;
6432 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_EHT_MCS));
6433 		break;
6434 	}
6435 
6436 	if (rate_flg && nla_put_flag(msg, rate_flg))
6437 		return false;
6438 
6439 	if (info->flags & RATE_INFO_FLAGS_MCS) {
6440 		if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
6441 			return false;
6442 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6443 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6444 			return false;
6445 	} else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
6446 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
6447 			return false;
6448 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
6449 			return false;
6450 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6451 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6452 			return false;
6453 	} else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
6454 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
6455 			return false;
6456 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
6457 			return false;
6458 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
6459 			return false;
6460 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
6461 			return false;
6462 		if (info->bw == RATE_INFO_BW_HE_RU &&
6463 		    nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
6464 			       info->he_ru_alloc))
6465 			return false;
6466 	} else if (info->flags & RATE_INFO_FLAGS_S1G_MCS) {
6467 		if (nla_put_u8(msg, NL80211_RATE_INFO_S1G_MCS, info->mcs))
6468 			return false;
6469 		if (nla_put_u8(msg, NL80211_RATE_INFO_S1G_NSS, info->nss))
6470 			return false;
6471 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6472 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6473 			return false;
6474 	} else if (info->flags & RATE_INFO_FLAGS_EHT_MCS) {
6475 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_MCS, info->mcs))
6476 			return false;
6477 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_NSS, info->nss))
6478 			return false;
6479 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_GI, info->eht_gi))
6480 			return false;
6481 		if (info->bw == RATE_INFO_BW_EHT_RU &&
6482 		    nla_put_u8(msg, NL80211_RATE_INFO_EHT_RU_ALLOC,
6483 			       info->eht_ru_alloc))
6484 			return false;
6485 	}
6486 
6487 	nla_nest_end(msg, rate);
6488 	return true;
6489 }
6490 
6491 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
6492 			       int id)
6493 {
6494 	void *attr;
6495 	int i = 0;
6496 
6497 	if (!mask)
6498 		return true;
6499 
6500 	attr = nla_nest_start_noflag(msg, id);
6501 	if (!attr)
6502 		return false;
6503 
6504 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
6505 		if (!(mask & BIT(i)))
6506 			continue;
6507 
6508 		if (nla_put_u8(msg, i, signal[i]))
6509 			return false;
6510 	}
6511 
6512 	nla_nest_end(msg, attr);
6513 
6514 	return true;
6515 }
6516 
6517 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
6518 				u32 seq, int flags,
6519 				struct cfg80211_registered_device *rdev,
6520 				struct net_device *dev,
6521 				const u8 *mac_addr, struct station_info *sinfo)
6522 {
6523 	void *hdr;
6524 	struct nlattr *sinfoattr, *bss_param;
6525 
6526 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
6527 	if (!hdr) {
6528 		cfg80211_sinfo_release_content(sinfo);
6529 		return -1;
6530 	}
6531 
6532 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6533 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
6534 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
6535 		goto nla_put_failure;
6536 
6537 	sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
6538 	if (!sinfoattr)
6539 		goto nla_put_failure;
6540 
6541 #define PUT_SINFO(attr, memb, type) do {				\
6542 	BUILD_BUG_ON(sizeof(type) == sizeof(u64));			\
6543 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
6544 	    nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,		\
6545 			     sinfo->memb))				\
6546 		goto nla_put_failure;					\
6547 	} while (0)
6548 #define PUT_SINFO_U64(attr, memb) do {					\
6549 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
6550 	    nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,		\
6551 			      sinfo->memb, NL80211_STA_INFO_PAD))	\
6552 		goto nla_put_failure;					\
6553 	} while (0)
6554 
6555 	PUT_SINFO(CONNECTED_TIME, connected_time, u32);
6556 	PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
6557 	PUT_SINFO_U64(ASSOC_AT_BOOTTIME, assoc_at);
6558 
6559 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
6560 			     BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
6561 	    nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
6562 			(u32)sinfo->rx_bytes))
6563 		goto nla_put_failure;
6564 
6565 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
6566 			     BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
6567 	    nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
6568 			(u32)sinfo->tx_bytes))
6569 		goto nla_put_failure;
6570 
6571 	PUT_SINFO_U64(RX_BYTES64, rx_bytes);
6572 	PUT_SINFO_U64(TX_BYTES64, tx_bytes);
6573 	PUT_SINFO(LLID, llid, u16);
6574 	PUT_SINFO(PLID, plid, u16);
6575 	PUT_SINFO(PLINK_STATE, plink_state, u8);
6576 	PUT_SINFO_U64(RX_DURATION, rx_duration);
6577 	PUT_SINFO_U64(TX_DURATION, tx_duration);
6578 
6579 	if (wiphy_ext_feature_isset(&rdev->wiphy,
6580 				    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
6581 		PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
6582 
6583 	switch (rdev->wiphy.signal_type) {
6584 	case CFG80211_SIGNAL_TYPE_MBM:
6585 		PUT_SINFO(SIGNAL, signal, u8);
6586 		PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
6587 		break;
6588 	default:
6589 		break;
6590 	}
6591 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
6592 		if (!nl80211_put_signal(msg, sinfo->chains,
6593 					sinfo->chain_signal,
6594 					NL80211_STA_INFO_CHAIN_SIGNAL))
6595 			goto nla_put_failure;
6596 	}
6597 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
6598 		if (!nl80211_put_signal(msg, sinfo->chains,
6599 					sinfo->chain_signal_avg,
6600 					NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
6601 			goto nla_put_failure;
6602 	}
6603 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
6604 		if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
6605 					  NL80211_STA_INFO_TX_BITRATE))
6606 			goto nla_put_failure;
6607 	}
6608 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
6609 		if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
6610 					  NL80211_STA_INFO_RX_BITRATE))
6611 			goto nla_put_failure;
6612 	}
6613 
6614 	PUT_SINFO(RX_PACKETS, rx_packets, u32);
6615 	PUT_SINFO(TX_PACKETS, tx_packets, u32);
6616 	PUT_SINFO(TX_RETRIES, tx_retries, u32);
6617 	PUT_SINFO(TX_FAILED, tx_failed, u32);
6618 	PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
6619 	PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
6620 	PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
6621 	PUT_SINFO(LOCAL_PM, local_pm, u32);
6622 	PUT_SINFO(PEER_PM, peer_pm, u32);
6623 	PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
6624 	PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
6625 	PUT_SINFO(CONNECTED_TO_AS, connected_to_as, u8);
6626 
6627 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
6628 		bss_param = nla_nest_start_noflag(msg,
6629 						  NL80211_STA_INFO_BSS_PARAM);
6630 		if (!bss_param)
6631 			goto nla_put_failure;
6632 
6633 		if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
6634 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
6635 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
6636 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
6637 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
6638 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
6639 		    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
6640 			       sinfo->bss_param.dtim_period) ||
6641 		    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
6642 				sinfo->bss_param.beacon_interval))
6643 			goto nla_put_failure;
6644 
6645 		nla_nest_end(msg, bss_param);
6646 	}
6647 	if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
6648 	    nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
6649 		    sizeof(struct nl80211_sta_flag_update),
6650 		    &sinfo->sta_flags))
6651 		goto nla_put_failure;
6652 
6653 	PUT_SINFO_U64(T_OFFSET, t_offset);
6654 	PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
6655 	PUT_SINFO_U64(BEACON_RX, rx_beacon);
6656 	PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
6657 	PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
6658 	PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
6659 	if (wiphy_ext_feature_isset(&rdev->wiphy,
6660 				    NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
6661 		PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
6662 		PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
6663 	}
6664 
6665 #undef PUT_SINFO
6666 #undef PUT_SINFO_U64
6667 
6668 	if (sinfo->pertid) {
6669 		struct nlattr *tidsattr;
6670 		int tid;
6671 
6672 		tidsattr = nla_nest_start_noflag(msg,
6673 						 NL80211_STA_INFO_TID_STATS);
6674 		if (!tidsattr)
6675 			goto nla_put_failure;
6676 
6677 		for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
6678 			struct cfg80211_tid_stats *tidstats;
6679 			struct nlattr *tidattr;
6680 
6681 			tidstats = &sinfo->pertid[tid];
6682 
6683 			if (!tidstats->filled)
6684 				continue;
6685 
6686 			tidattr = nla_nest_start_noflag(msg, tid + 1);
6687 			if (!tidattr)
6688 				goto nla_put_failure;
6689 
6690 #define PUT_TIDVAL_U64(attr, memb) do {					\
6691 	if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&	\
6692 	    nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,		\
6693 			      tidstats->memb, NL80211_TID_STATS_PAD))	\
6694 		goto nla_put_failure;					\
6695 	} while (0)
6696 
6697 			PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
6698 			PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
6699 			PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
6700 			PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
6701 
6702 #undef PUT_TIDVAL_U64
6703 			if ((tidstats->filled &
6704 			     BIT(NL80211_TID_STATS_TXQ_STATS)) &&
6705 			    !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
6706 						   NL80211_TID_STATS_TXQ_STATS))
6707 				goto nla_put_failure;
6708 
6709 			nla_nest_end(msg, tidattr);
6710 		}
6711 
6712 		nla_nest_end(msg, tidsattr);
6713 	}
6714 
6715 	nla_nest_end(msg, sinfoattr);
6716 
6717 	if (sinfo->assoc_req_ies_len &&
6718 	    nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
6719 		    sinfo->assoc_req_ies))
6720 		goto nla_put_failure;
6721 
6722 	if (sinfo->assoc_resp_ies_len &&
6723 	    nla_put(msg, NL80211_ATTR_RESP_IE, sinfo->assoc_resp_ies_len,
6724 		    sinfo->assoc_resp_ies))
6725 		goto nla_put_failure;
6726 
6727 	if (sinfo->mlo_params_valid) {
6728 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
6729 			       sinfo->assoc_link_id))
6730 			goto nla_put_failure;
6731 
6732 		if (!is_zero_ether_addr(sinfo->mld_addr) &&
6733 		    nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
6734 			    sinfo->mld_addr))
6735 			goto nla_put_failure;
6736 	}
6737 
6738 	cfg80211_sinfo_release_content(sinfo);
6739 	genlmsg_end(msg, hdr);
6740 	return 0;
6741 
6742  nla_put_failure:
6743 	cfg80211_sinfo_release_content(sinfo);
6744 	genlmsg_cancel(msg, hdr);
6745 	return -EMSGSIZE;
6746 }
6747 
6748 static int nl80211_dump_station(struct sk_buff *skb,
6749 				struct netlink_callback *cb)
6750 {
6751 	struct station_info sinfo;
6752 	struct cfg80211_registered_device *rdev;
6753 	struct wireless_dev *wdev;
6754 	u8 mac_addr[ETH_ALEN];
6755 	int sta_idx = cb->args[2];
6756 	int err;
6757 
6758 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
6759 	if (err)
6760 		return err;
6761 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
6762 	__acquire(&rdev->wiphy.mtx);
6763 
6764 	if (!wdev->netdev) {
6765 		err = -EINVAL;
6766 		goto out_err;
6767 	}
6768 
6769 	if (!rdev->ops->dump_station) {
6770 		err = -EOPNOTSUPP;
6771 		goto out_err;
6772 	}
6773 
6774 	while (1) {
6775 		memset(&sinfo, 0, sizeof(sinfo));
6776 		err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
6777 					mac_addr, &sinfo);
6778 		if (err == -ENOENT)
6779 			break;
6780 		if (err)
6781 			goto out_err;
6782 
6783 		if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
6784 				NETLINK_CB(cb->skb).portid,
6785 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
6786 				rdev, wdev->netdev, mac_addr,
6787 				&sinfo) < 0)
6788 			goto out;
6789 
6790 		sta_idx++;
6791 	}
6792 
6793  out:
6794 	cb->args[2] = sta_idx;
6795 	err = skb->len;
6796  out_err:
6797 	wiphy_unlock(&rdev->wiphy);
6798 
6799 	return err;
6800 }
6801 
6802 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
6803 {
6804 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6805 	struct net_device *dev = info->user_ptr[1];
6806 	struct station_info sinfo;
6807 	struct sk_buff *msg;
6808 	u8 *mac_addr = NULL;
6809 	int err;
6810 
6811 	memset(&sinfo, 0, sizeof(sinfo));
6812 
6813 	if (!info->attrs[NL80211_ATTR_MAC])
6814 		return -EINVAL;
6815 
6816 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6817 
6818 	if (!rdev->ops->get_station)
6819 		return -EOPNOTSUPP;
6820 
6821 	err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
6822 	if (err)
6823 		return err;
6824 
6825 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6826 	if (!msg) {
6827 		cfg80211_sinfo_release_content(&sinfo);
6828 		return -ENOMEM;
6829 	}
6830 
6831 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
6832 				 info->snd_portid, info->snd_seq, 0,
6833 				 rdev, dev, mac_addr, &sinfo) < 0) {
6834 		nlmsg_free(msg);
6835 		return -ENOBUFS;
6836 	}
6837 
6838 	return genlmsg_reply(msg, info);
6839 }
6840 
6841 int cfg80211_check_station_change(struct wiphy *wiphy,
6842 				  struct station_parameters *params,
6843 				  enum cfg80211_station_type statype)
6844 {
6845 	if (params->listen_interval != -1 &&
6846 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6847 		return -EINVAL;
6848 
6849 	if (params->support_p2p_ps != -1 &&
6850 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6851 		return -EINVAL;
6852 
6853 	if (params->aid &&
6854 	    !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
6855 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6856 		return -EINVAL;
6857 
6858 	/* When you run into this, adjust the code below for the new flag */
6859 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
6860 
6861 	switch (statype) {
6862 	case CFG80211_STA_MESH_PEER_KERNEL:
6863 	case CFG80211_STA_MESH_PEER_USER:
6864 		/*
6865 		 * No ignoring the TDLS flag here -- the userspace mesh
6866 		 * code doesn't have the bug of including TDLS in the
6867 		 * mask everywhere.
6868 		 */
6869 		if (params->sta_flags_mask &
6870 				~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6871 				  BIT(NL80211_STA_FLAG_MFP) |
6872 				  BIT(NL80211_STA_FLAG_AUTHORIZED)))
6873 			return -EINVAL;
6874 		break;
6875 	case CFG80211_STA_TDLS_PEER_SETUP:
6876 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6877 		if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6878 			return -EINVAL;
6879 		/* ignore since it can't change */
6880 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6881 		break;
6882 	default:
6883 		/* disallow mesh-specific things */
6884 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
6885 			return -EINVAL;
6886 		if (params->local_pm)
6887 			return -EINVAL;
6888 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6889 			return -EINVAL;
6890 	}
6891 
6892 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6893 	    statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
6894 		/* TDLS can't be set, ... */
6895 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
6896 			return -EINVAL;
6897 		/*
6898 		 * ... but don't bother the driver with it. This works around
6899 		 * a hostapd/wpa_supplicant issue -- it always includes the
6900 		 * TLDS_PEER flag in the mask even for AP mode.
6901 		 */
6902 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6903 	}
6904 
6905 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6906 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6907 		/* reject other things that can't change */
6908 		if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
6909 			return -EINVAL;
6910 		if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
6911 			return -EINVAL;
6912 		if (params->link_sta_params.supported_rates)
6913 			return -EINVAL;
6914 		if (params->ext_capab || params->link_sta_params.ht_capa ||
6915 		    params->link_sta_params.vht_capa ||
6916 		    params->link_sta_params.he_capa ||
6917 		    params->link_sta_params.eht_capa)
6918 			return -EINVAL;
6919 	}
6920 
6921 	if (statype != CFG80211_STA_AP_CLIENT &&
6922 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6923 		if (params->vlan)
6924 			return -EINVAL;
6925 	}
6926 
6927 	switch (statype) {
6928 	case CFG80211_STA_AP_MLME_CLIENT:
6929 		/* Use this only for authorizing/unauthorizing a station */
6930 		if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
6931 			return -EOPNOTSUPP;
6932 		break;
6933 	case CFG80211_STA_AP_CLIENT:
6934 	case CFG80211_STA_AP_CLIENT_UNASSOC:
6935 		/* accept only the listed bits */
6936 		if (params->sta_flags_mask &
6937 				~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6938 				  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6939 				  BIT(NL80211_STA_FLAG_ASSOCIATED) |
6940 				  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6941 				  BIT(NL80211_STA_FLAG_WME) |
6942 				  BIT(NL80211_STA_FLAG_MFP)))
6943 			return -EINVAL;
6944 
6945 		/* but authenticated/associated only if driver handles it */
6946 		if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
6947 		    params->sta_flags_mask &
6948 				(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6949 				 BIT(NL80211_STA_FLAG_ASSOCIATED)))
6950 			return -EINVAL;
6951 		break;
6952 	case CFG80211_STA_IBSS:
6953 	case CFG80211_STA_AP_STA:
6954 		/* reject any changes other than AUTHORIZED */
6955 		if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
6956 			return -EINVAL;
6957 		break;
6958 	case CFG80211_STA_TDLS_PEER_SETUP:
6959 		/* reject any changes other than AUTHORIZED or WME */
6960 		if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6961 					       BIT(NL80211_STA_FLAG_WME)))
6962 			return -EINVAL;
6963 		/* force (at least) rates when authorizing */
6964 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
6965 		    !params->link_sta_params.supported_rates)
6966 			return -EINVAL;
6967 		break;
6968 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6969 		/* reject any changes */
6970 		return -EINVAL;
6971 	case CFG80211_STA_MESH_PEER_KERNEL:
6972 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6973 			return -EINVAL;
6974 		break;
6975 	case CFG80211_STA_MESH_PEER_USER:
6976 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
6977 		    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
6978 			return -EINVAL;
6979 		break;
6980 	}
6981 
6982 	/*
6983 	 * Older kernel versions ignored this attribute entirely, so don't
6984 	 * reject attempts to update it but mark it as unused instead so the
6985 	 * driver won't look at the data.
6986 	 */
6987 	if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
6988 	    statype != CFG80211_STA_TDLS_PEER_SETUP)
6989 		params->link_sta_params.opmode_notif_used = false;
6990 
6991 	return 0;
6992 }
6993 EXPORT_SYMBOL(cfg80211_check_station_change);
6994 
6995 /*
6996  * Get vlan interface making sure it is running and on the right wiphy.
6997  */
6998 static struct net_device *get_vlan(struct genl_info *info,
6999 				   struct cfg80211_registered_device *rdev)
7000 {
7001 	struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
7002 	struct net_device *v;
7003 	int ret;
7004 
7005 	if (!vlanattr)
7006 		return NULL;
7007 
7008 	v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
7009 	if (!v)
7010 		return ERR_PTR(-ENODEV);
7011 
7012 	if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
7013 		ret = -EINVAL;
7014 		goto error;
7015 	}
7016 
7017 	if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
7018 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
7019 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
7020 		ret = -EINVAL;
7021 		goto error;
7022 	}
7023 
7024 	if (!netif_running(v)) {
7025 		ret = -ENETDOWN;
7026 		goto error;
7027 	}
7028 
7029 	return v;
7030  error:
7031 	dev_put(v);
7032 	return ERR_PTR(ret);
7033 }
7034 
7035 static int nl80211_parse_sta_wme(struct genl_info *info,
7036 				 struct station_parameters *params)
7037 {
7038 	struct nlattr *tb[NL80211_STA_WME_MAX + 1];
7039 	struct nlattr *nla;
7040 	int err;
7041 
7042 	/* parse WME attributes if present */
7043 	if (!info->attrs[NL80211_ATTR_STA_WME])
7044 		return 0;
7045 
7046 	nla = info->attrs[NL80211_ATTR_STA_WME];
7047 	err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
7048 					  nl80211_sta_wme_policy,
7049 					  info->extack);
7050 	if (err)
7051 		return err;
7052 
7053 	if (tb[NL80211_STA_WME_UAPSD_QUEUES])
7054 		params->uapsd_queues = nla_get_u8(
7055 			tb[NL80211_STA_WME_UAPSD_QUEUES]);
7056 	if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
7057 		return -EINVAL;
7058 
7059 	if (tb[NL80211_STA_WME_MAX_SP])
7060 		params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
7061 
7062 	if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
7063 		return -EINVAL;
7064 
7065 	params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
7066 
7067 	return 0;
7068 }
7069 
7070 static int nl80211_parse_sta_channel_info(struct genl_info *info,
7071 				      struct station_parameters *params)
7072 {
7073 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
7074 		params->supported_channels =
7075 		     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
7076 		params->supported_channels_len =
7077 		     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
7078 		/*
7079 		 * Need to include at least one (first channel, number of
7080 		 * channels) tuple for each subband (checked in policy),
7081 		 * and must have proper tuples for the rest of the data as well.
7082 		 */
7083 		if (params->supported_channels_len % 2)
7084 			return -EINVAL;
7085 	}
7086 
7087 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
7088 		params->supported_oper_classes =
7089 		 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
7090 		params->supported_oper_classes_len =
7091 		  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
7092 	}
7093 	return 0;
7094 }
7095 
7096 static int nl80211_set_station_tdls(struct genl_info *info,
7097 				    struct station_parameters *params)
7098 {
7099 	int err;
7100 	/* Dummy STA entry gets updated once the peer capabilities are known */
7101 	if (info->attrs[NL80211_ATTR_PEER_AID])
7102 		params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
7103 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
7104 		params->link_sta_params.ht_capa =
7105 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
7106 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
7107 		params->link_sta_params.vht_capa =
7108 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
7109 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
7110 		params->link_sta_params.he_capa =
7111 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7112 		params->link_sta_params.he_capa_len =
7113 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7114 
7115 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
7116 			params->link_sta_params.eht_capa =
7117 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7118 			params->link_sta_params.eht_capa_len =
7119 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7120 
7121 			if (!ieee80211_eht_capa_size_ok((const u8 *)params->link_sta_params.he_capa,
7122 							(const u8 *)params->link_sta_params.eht_capa,
7123 							params->link_sta_params.eht_capa_len,
7124 							false))
7125 				return -EINVAL;
7126 		}
7127 	}
7128 
7129 	err = nl80211_parse_sta_channel_info(info, params);
7130 	if (err)
7131 		return err;
7132 
7133 	return nl80211_parse_sta_wme(info, params);
7134 }
7135 
7136 static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
7137 					     struct sta_txpwr *txpwr,
7138 					     bool *txpwr_set)
7139 {
7140 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7141 	int idx;
7142 
7143 	if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
7144 		if (!rdev->ops->set_tx_power ||
7145 		    !wiphy_ext_feature_isset(&rdev->wiphy,
7146 					 NL80211_EXT_FEATURE_STA_TX_PWR))
7147 			return -EOPNOTSUPP;
7148 
7149 		idx = NL80211_ATTR_STA_TX_POWER_SETTING;
7150 		txpwr->type = nla_get_u8(info->attrs[idx]);
7151 
7152 		if (txpwr->type == NL80211_TX_POWER_LIMITED) {
7153 			idx = NL80211_ATTR_STA_TX_POWER;
7154 
7155 			if (info->attrs[idx])
7156 				txpwr->power = nla_get_s16(info->attrs[idx]);
7157 			else
7158 				return -EINVAL;
7159 		}
7160 
7161 		*txpwr_set = true;
7162 	} else {
7163 		*txpwr_set = false;
7164 	}
7165 
7166 	return 0;
7167 }
7168 
7169 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
7170 {
7171 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7172 	struct net_device *dev = info->user_ptr[1];
7173 	struct station_parameters params;
7174 	u8 *mac_addr;
7175 	int err;
7176 
7177 	memset(&params, 0, sizeof(params));
7178 
7179 	if (!rdev->ops->change_station)
7180 		return -EOPNOTSUPP;
7181 
7182 	/*
7183 	 * AID and listen_interval properties can be set only for unassociated
7184 	 * station. Include these parameters here and will check them in
7185 	 * cfg80211_check_station_change().
7186 	 */
7187 	if (info->attrs[NL80211_ATTR_STA_AID])
7188 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
7189 
7190 	if (info->attrs[NL80211_ATTR_VLAN_ID])
7191 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
7192 
7193 	if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
7194 		params.listen_interval =
7195 		     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
7196 	else
7197 		params.listen_interval = -1;
7198 
7199 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
7200 		params.support_p2p_ps =
7201 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
7202 	else
7203 		params.support_p2p_ps = -1;
7204 
7205 	if (!info->attrs[NL80211_ATTR_MAC])
7206 		return -EINVAL;
7207 
7208 	params.link_sta_params.link_id =
7209 		nl80211_link_id_or_invalid(info->attrs);
7210 
7211 	if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7212 		/* If MLD_ADDR attribute is set then this is an MLD station
7213 		 * and the MLD_ADDR attribute holds the MLD address and the
7214 		 * MAC attribute holds for the LINK address.
7215 		 * In that case, the link_id is also expected to be valid.
7216 		 */
7217 		if (params.link_sta_params.link_id < 0)
7218 			return -EINVAL;
7219 
7220 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7221 		params.link_sta_params.mld_mac = mac_addr;
7222 		params.link_sta_params.link_mac =
7223 			nla_data(info->attrs[NL80211_ATTR_MAC]);
7224 		if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7225 			return -EINVAL;
7226 	} else {
7227 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7228 	}
7229 
7230 
7231 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
7232 		params.link_sta_params.supported_rates =
7233 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7234 		params.link_sta_params.supported_rates_len =
7235 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7236 	}
7237 
7238 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7239 		params.capability =
7240 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7241 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7242 	}
7243 
7244 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7245 		params.ext_capab =
7246 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7247 		params.ext_capab_len =
7248 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7249 	}
7250 
7251 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7252 		return -EINVAL;
7253 
7254 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7255 		params.plink_action =
7256 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7257 
7258 	if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
7259 		params.plink_state =
7260 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
7261 		if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
7262 			params.peer_aid = nla_get_u16(
7263 				info->attrs[NL80211_ATTR_MESH_PEER_AID]);
7264 		params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
7265 	}
7266 
7267 	if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
7268 		params.local_pm = nla_get_u32(
7269 			info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
7270 
7271 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7272 		params.link_sta_params.opmode_notif_used = true;
7273 		params.link_sta_params.opmode_notif =
7274 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7275 	}
7276 
7277 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7278 		params.link_sta_params.he_6ghz_capa =
7279 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7280 
7281 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7282 		params.airtime_weight =
7283 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7284 
7285 	if (params.airtime_weight &&
7286 	    !wiphy_ext_feature_isset(&rdev->wiphy,
7287 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7288 		return -EOPNOTSUPP;
7289 
7290 	err = nl80211_parse_sta_txpower_setting(info,
7291 						&params.link_sta_params.txpwr,
7292 						&params.link_sta_params.txpwr_set);
7293 	if (err)
7294 		return err;
7295 
7296 	/* Include parameters for TDLS peer (will check later) */
7297 	err = nl80211_set_station_tdls(info, &params);
7298 	if (err)
7299 		return err;
7300 
7301 	params.vlan = get_vlan(info, rdev);
7302 	if (IS_ERR(params.vlan))
7303 		return PTR_ERR(params.vlan);
7304 
7305 	switch (dev->ieee80211_ptr->iftype) {
7306 	case NL80211_IFTYPE_AP:
7307 	case NL80211_IFTYPE_AP_VLAN:
7308 	case NL80211_IFTYPE_P2P_GO:
7309 	case NL80211_IFTYPE_P2P_CLIENT:
7310 	case NL80211_IFTYPE_STATION:
7311 	case NL80211_IFTYPE_ADHOC:
7312 	case NL80211_IFTYPE_MESH_POINT:
7313 		break;
7314 	default:
7315 		err = -EOPNOTSUPP;
7316 		goto out_put_vlan;
7317 	}
7318 
7319 	/* driver will call cfg80211_check_station_change() */
7320 	err = rdev_change_station(rdev, dev, mac_addr, &params);
7321 
7322  out_put_vlan:
7323 	dev_put(params.vlan);
7324 
7325 	return err;
7326 }
7327 
7328 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
7329 {
7330 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7331 	int err;
7332 	struct net_device *dev = info->user_ptr[1];
7333 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7334 	struct station_parameters params;
7335 	u8 *mac_addr = NULL;
7336 	u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
7337 			 BIT(NL80211_STA_FLAG_ASSOCIATED);
7338 
7339 	memset(&params, 0, sizeof(params));
7340 
7341 	if (!rdev->ops->add_station)
7342 		return -EOPNOTSUPP;
7343 
7344 	if (!info->attrs[NL80211_ATTR_MAC])
7345 		return -EINVAL;
7346 
7347 	if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
7348 		return -EINVAL;
7349 
7350 	if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
7351 		return -EINVAL;
7352 
7353 	if (!info->attrs[NL80211_ATTR_STA_AID] &&
7354 	    !info->attrs[NL80211_ATTR_PEER_AID])
7355 		return -EINVAL;
7356 
7357 	params.link_sta_params.link_id =
7358 		nl80211_link_id_or_invalid(info->attrs);
7359 
7360 	if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7361 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7362 		params.link_sta_params.mld_mac = mac_addr;
7363 		params.link_sta_params.link_mac =
7364 			nla_data(info->attrs[NL80211_ATTR_MAC]);
7365 		if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7366 			return -EINVAL;
7367 	} else {
7368 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7369 	}
7370 
7371 	params.link_sta_params.supported_rates =
7372 		nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7373 	params.link_sta_params.supported_rates_len =
7374 		nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7375 	params.listen_interval =
7376 		nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
7377 
7378 	if (info->attrs[NL80211_ATTR_VLAN_ID])
7379 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
7380 
7381 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
7382 		params.support_p2p_ps =
7383 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
7384 	} else {
7385 		/*
7386 		 * if not specified, assume it's supported for P2P GO interface,
7387 		 * and is NOT supported for AP interface
7388 		 */
7389 		params.support_p2p_ps =
7390 			dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
7391 	}
7392 
7393 	if (info->attrs[NL80211_ATTR_PEER_AID])
7394 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
7395 	else
7396 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
7397 
7398 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7399 		params.capability =
7400 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7401 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7402 	}
7403 
7404 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7405 		params.ext_capab =
7406 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7407 		params.ext_capab_len =
7408 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7409 	}
7410 
7411 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
7412 		params.link_sta_params.ht_capa =
7413 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
7414 
7415 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
7416 		params.link_sta_params.vht_capa =
7417 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
7418 
7419 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
7420 		params.link_sta_params.he_capa =
7421 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7422 		params.link_sta_params.he_capa_len =
7423 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7424 
7425 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
7426 			params.link_sta_params.eht_capa =
7427 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7428 			params.link_sta_params.eht_capa_len =
7429 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7430 
7431 			if (!ieee80211_eht_capa_size_ok((const u8 *)params.link_sta_params.he_capa,
7432 							(const u8 *)params.link_sta_params.eht_capa,
7433 							params.link_sta_params.eht_capa_len,
7434 							false))
7435 				return -EINVAL;
7436 		}
7437 	}
7438 
7439 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7440 		params.link_sta_params.he_6ghz_capa =
7441 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7442 
7443 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7444 		params.link_sta_params.opmode_notif_used = true;
7445 		params.link_sta_params.opmode_notif =
7446 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7447 	}
7448 
7449 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7450 		params.plink_action =
7451 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7452 
7453 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7454 		params.airtime_weight =
7455 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7456 
7457 	if (params.airtime_weight &&
7458 	    !wiphy_ext_feature_isset(&rdev->wiphy,
7459 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7460 		return -EOPNOTSUPP;
7461 
7462 	err = nl80211_parse_sta_txpower_setting(info,
7463 						&params.link_sta_params.txpwr,
7464 						&params.link_sta_params.txpwr_set);
7465 	if (err)
7466 		return err;
7467 
7468 	err = nl80211_parse_sta_channel_info(info, &params);
7469 	if (err)
7470 		return err;
7471 
7472 	err = nl80211_parse_sta_wme(info, &params);
7473 	if (err)
7474 		return err;
7475 
7476 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7477 		return -EINVAL;
7478 
7479 	/* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
7480 	 * as userspace might just pass through the capabilities from the IEs
7481 	 * directly, rather than enforcing this restriction and returning an
7482 	 * error in this case.
7483 	 */
7484 	if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
7485 		params.link_sta_params.ht_capa = NULL;
7486 		params.link_sta_params.vht_capa = NULL;
7487 
7488 		/* HE and EHT require WME */
7489 		if (params.link_sta_params.he_capa_len ||
7490 		    params.link_sta_params.he_6ghz_capa ||
7491 		    params.link_sta_params.eht_capa_len)
7492 			return -EINVAL;
7493 	}
7494 
7495 	/* Ensure that HT/VHT capabilities are not set for 6 GHz HE STA */
7496 	if (params.link_sta_params.he_6ghz_capa &&
7497 	    (params.link_sta_params.ht_capa || params.link_sta_params.vht_capa))
7498 		return -EINVAL;
7499 
7500 	/* When you run into this, adjust the code below for the new flag */
7501 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
7502 
7503 	switch (dev->ieee80211_ptr->iftype) {
7504 	case NL80211_IFTYPE_AP:
7505 	case NL80211_IFTYPE_AP_VLAN:
7506 	case NL80211_IFTYPE_P2P_GO:
7507 		/* ignore WME attributes if iface/sta is not capable */
7508 		if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
7509 		    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
7510 			params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7511 
7512 		/* TDLS peers cannot be added */
7513 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7514 		    info->attrs[NL80211_ATTR_PEER_AID])
7515 			return -EINVAL;
7516 		/* but don't bother the driver with it */
7517 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
7518 
7519 		/* allow authenticated/associated only if driver handles it */
7520 		if (!(rdev->wiphy.features &
7521 				NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
7522 		    params.sta_flags_mask & auth_assoc)
7523 			return -EINVAL;
7524 
7525 		/* Older userspace, or userspace wanting to be compatible with
7526 		 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
7527 		 * and assoc flags in the mask, but assumes the station will be
7528 		 * added as associated anyway since this was the required driver
7529 		 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
7530 		 * introduced.
7531 		 * In order to not bother drivers with this quirk in the API
7532 		 * set the flags in both the mask and set for new stations in
7533 		 * this case.
7534 		 */
7535 		if (!(params.sta_flags_mask & auth_assoc)) {
7536 			params.sta_flags_mask |= auth_assoc;
7537 			params.sta_flags_set |= auth_assoc;
7538 		}
7539 
7540 		/* must be last in here for error handling */
7541 		params.vlan = get_vlan(info, rdev);
7542 		if (IS_ERR(params.vlan))
7543 			return PTR_ERR(params.vlan);
7544 		break;
7545 	case NL80211_IFTYPE_MESH_POINT:
7546 		/* ignore uAPSD data */
7547 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7548 
7549 		/* associated is disallowed */
7550 		if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
7551 			return -EINVAL;
7552 		/* TDLS peers cannot be added */
7553 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7554 		    info->attrs[NL80211_ATTR_PEER_AID])
7555 			return -EINVAL;
7556 		break;
7557 	case NL80211_IFTYPE_STATION:
7558 	case NL80211_IFTYPE_P2P_CLIENT:
7559 		/* ignore uAPSD data */
7560 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7561 
7562 		/* these are disallowed */
7563 		if (params.sta_flags_mask &
7564 				(BIT(NL80211_STA_FLAG_ASSOCIATED) |
7565 				 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
7566 			return -EINVAL;
7567 		/* Only TDLS peers can be added */
7568 		if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
7569 			return -EINVAL;
7570 		/* Can only add if TDLS ... */
7571 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
7572 			return -EOPNOTSUPP;
7573 		/* ... with external setup is supported */
7574 		if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
7575 			return -EOPNOTSUPP;
7576 		/*
7577 		 * Older wpa_supplicant versions always mark the TDLS peer
7578 		 * as authorized, but it shouldn't yet be.
7579 		 */
7580 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
7581 		break;
7582 	default:
7583 		return -EOPNOTSUPP;
7584 	}
7585 
7586 	/* be aware of params.vlan when changing code here */
7587 
7588 	if (wdev->valid_links) {
7589 		if (params.link_sta_params.link_id < 0) {
7590 			err = -EINVAL;
7591 			goto out;
7592 		}
7593 		if (!(wdev->valid_links & BIT(params.link_sta_params.link_id))) {
7594 			err = -ENOLINK;
7595 			goto out;
7596 		}
7597 	} else {
7598 		if (params.link_sta_params.link_id >= 0) {
7599 			err = -EINVAL;
7600 			goto out;
7601 		}
7602 	}
7603 	err = rdev_add_station(rdev, dev, mac_addr, &params);
7604 out:
7605 	dev_put(params.vlan);
7606 	return err;
7607 }
7608 
7609 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
7610 {
7611 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7612 	struct net_device *dev = info->user_ptr[1];
7613 	struct station_del_parameters params;
7614 
7615 	memset(&params, 0, sizeof(params));
7616 
7617 	if (info->attrs[NL80211_ATTR_MAC])
7618 		params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
7619 
7620 	switch (dev->ieee80211_ptr->iftype) {
7621 	case NL80211_IFTYPE_AP:
7622 	case NL80211_IFTYPE_AP_VLAN:
7623 	case NL80211_IFTYPE_MESH_POINT:
7624 	case NL80211_IFTYPE_P2P_GO:
7625 		/* always accept these */
7626 		break;
7627 	case NL80211_IFTYPE_ADHOC:
7628 		/* conditionally accept */
7629 		if (wiphy_ext_feature_isset(&rdev->wiphy,
7630 					    NL80211_EXT_FEATURE_DEL_IBSS_STA))
7631 			break;
7632 		return -EINVAL;
7633 	default:
7634 		return -EINVAL;
7635 	}
7636 
7637 	if (!rdev->ops->del_station)
7638 		return -EOPNOTSUPP;
7639 
7640 	if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
7641 		params.subtype =
7642 			nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
7643 		if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
7644 		    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
7645 			return -EINVAL;
7646 	} else {
7647 		/* Default to Deauthentication frame */
7648 		params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
7649 	}
7650 
7651 	if (info->attrs[NL80211_ATTR_REASON_CODE]) {
7652 		params.reason_code =
7653 			nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7654 		if (params.reason_code == 0)
7655 			return -EINVAL; /* 0 is reserved */
7656 	} else {
7657 		/* Default to reason code 2 */
7658 		params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
7659 	}
7660 
7661 	return rdev_del_station(rdev, dev, &params);
7662 }
7663 
7664 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
7665 				int flags, struct net_device *dev,
7666 				u8 *dst, u8 *next_hop,
7667 				struct mpath_info *pinfo)
7668 {
7669 	void *hdr;
7670 	struct nlattr *pinfoattr;
7671 
7672 	hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
7673 	if (!hdr)
7674 		return -1;
7675 
7676 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7677 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7678 	    nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
7679 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
7680 		goto nla_put_failure;
7681 
7682 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
7683 	if (!pinfoattr)
7684 		goto nla_put_failure;
7685 	if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
7686 	    nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
7687 			pinfo->frame_qlen))
7688 		goto nla_put_failure;
7689 	if (((pinfo->filled & MPATH_INFO_SN) &&
7690 	     nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
7691 	    ((pinfo->filled & MPATH_INFO_METRIC) &&
7692 	     nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
7693 			 pinfo->metric)) ||
7694 	    ((pinfo->filled & MPATH_INFO_EXPTIME) &&
7695 	     nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
7696 			 pinfo->exptime)) ||
7697 	    ((pinfo->filled & MPATH_INFO_FLAGS) &&
7698 	     nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
7699 			pinfo->flags)) ||
7700 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
7701 	     nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
7702 			 pinfo->discovery_timeout)) ||
7703 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
7704 	     nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
7705 			pinfo->discovery_retries)) ||
7706 	    ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
7707 	     nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
7708 			pinfo->hop_count)) ||
7709 	    ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
7710 	     nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
7711 			 pinfo->path_change_count)))
7712 		goto nla_put_failure;
7713 
7714 	nla_nest_end(msg, pinfoattr);
7715 
7716 	genlmsg_end(msg, hdr);
7717 	return 0;
7718 
7719  nla_put_failure:
7720 	genlmsg_cancel(msg, hdr);
7721 	return -EMSGSIZE;
7722 }
7723 
7724 static int nl80211_dump_mpath(struct sk_buff *skb,
7725 			      struct netlink_callback *cb)
7726 {
7727 	struct mpath_info pinfo;
7728 	struct cfg80211_registered_device *rdev;
7729 	struct wireless_dev *wdev;
7730 	u8 dst[ETH_ALEN];
7731 	u8 next_hop[ETH_ALEN];
7732 	int path_idx = cb->args[2];
7733 	int err;
7734 
7735 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7736 	if (err)
7737 		return err;
7738 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
7739 	__acquire(&rdev->wiphy.mtx);
7740 
7741 	if (!rdev->ops->dump_mpath) {
7742 		err = -EOPNOTSUPP;
7743 		goto out_err;
7744 	}
7745 
7746 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7747 		err = -EOPNOTSUPP;
7748 		goto out_err;
7749 	}
7750 
7751 	while (1) {
7752 		err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
7753 				      next_hop, &pinfo);
7754 		if (err == -ENOENT)
7755 			break;
7756 		if (err)
7757 			goto out_err;
7758 
7759 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7760 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
7761 				       wdev->netdev, dst, next_hop,
7762 				       &pinfo) < 0)
7763 			goto out;
7764 
7765 		path_idx++;
7766 	}
7767 
7768  out:
7769 	cb->args[2] = path_idx;
7770 	err = skb->len;
7771  out_err:
7772 	wiphy_unlock(&rdev->wiphy);
7773 	return err;
7774 }
7775 
7776 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
7777 {
7778 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7779 	int err;
7780 	struct net_device *dev = info->user_ptr[1];
7781 	struct mpath_info pinfo;
7782 	struct sk_buff *msg;
7783 	u8 *dst = NULL;
7784 	u8 next_hop[ETH_ALEN];
7785 
7786 	memset(&pinfo, 0, sizeof(pinfo));
7787 
7788 	if (!info->attrs[NL80211_ATTR_MAC])
7789 		return -EINVAL;
7790 
7791 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7792 
7793 	if (!rdev->ops->get_mpath)
7794 		return -EOPNOTSUPP;
7795 
7796 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7797 		return -EOPNOTSUPP;
7798 
7799 	err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
7800 	if (err)
7801 		return err;
7802 
7803 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7804 	if (!msg)
7805 		return -ENOMEM;
7806 
7807 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7808 				 dev, dst, next_hop, &pinfo) < 0) {
7809 		nlmsg_free(msg);
7810 		return -ENOBUFS;
7811 	}
7812 
7813 	return genlmsg_reply(msg, info);
7814 }
7815 
7816 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
7817 {
7818 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7819 	struct net_device *dev = info->user_ptr[1];
7820 	u8 *dst = NULL;
7821 	u8 *next_hop = NULL;
7822 
7823 	if (!info->attrs[NL80211_ATTR_MAC])
7824 		return -EINVAL;
7825 
7826 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7827 		return -EINVAL;
7828 
7829 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7830 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7831 
7832 	if (!rdev->ops->change_mpath)
7833 		return -EOPNOTSUPP;
7834 
7835 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7836 		return -EOPNOTSUPP;
7837 
7838 	return rdev_change_mpath(rdev, dev, dst, next_hop);
7839 }
7840 
7841 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
7842 {
7843 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7844 	struct net_device *dev = info->user_ptr[1];
7845 	u8 *dst = NULL;
7846 	u8 *next_hop = NULL;
7847 
7848 	if (!info->attrs[NL80211_ATTR_MAC])
7849 		return -EINVAL;
7850 
7851 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7852 		return -EINVAL;
7853 
7854 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7855 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7856 
7857 	if (!rdev->ops->add_mpath)
7858 		return -EOPNOTSUPP;
7859 
7860 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7861 		return -EOPNOTSUPP;
7862 
7863 	return rdev_add_mpath(rdev, dev, dst, next_hop);
7864 }
7865 
7866 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
7867 {
7868 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7869 	struct net_device *dev = info->user_ptr[1];
7870 	u8 *dst = NULL;
7871 
7872 	if (info->attrs[NL80211_ATTR_MAC])
7873 		dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7874 
7875 	if (!rdev->ops->del_mpath)
7876 		return -EOPNOTSUPP;
7877 
7878 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7879 		return -EOPNOTSUPP;
7880 
7881 	return rdev_del_mpath(rdev, dev, dst);
7882 }
7883 
7884 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
7885 {
7886 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7887 	int err;
7888 	struct net_device *dev = info->user_ptr[1];
7889 	struct mpath_info pinfo;
7890 	struct sk_buff *msg;
7891 	u8 *dst = NULL;
7892 	u8 mpp[ETH_ALEN];
7893 
7894 	memset(&pinfo, 0, sizeof(pinfo));
7895 
7896 	if (!info->attrs[NL80211_ATTR_MAC])
7897 		return -EINVAL;
7898 
7899 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7900 
7901 	if (!rdev->ops->get_mpp)
7902 		return -EOPNOTSUPP;
7903 
7904 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7905 		return -EOPNOTSUPP;
7906 
7907 	err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
7908 	if (err)
7909 		return err;
7910 
7911 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7912 	if (!msg)
7913 		return -ENOMEM;
7914 
7915 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7916 			       dev, dst, mpp, &pinfo) < 0) {
7917 		nlmsg_free(msg);
7918 		return -ENOBUFS;
7919 	}
7920 
7921 	return genlmsg_reply(msg, info);
7922 }
7923 
7924 static int nl80211_dump_mpp(struct sk_buff *skb,
7925 			    struct netlink_callback *cb)
7926 {
7927 	struct mpath_info pinfo;
7928 	struct cfg80211_registered_device *rdev;
7929 	struct wireless_dev *wdev;
7930 	u8 dst[ETH_ALEN];
7931 	u8 mpp[ETH_ALEN];
7932 	int path_idx = cb->args[2];
7933 	int err;
7934 
7935 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7936 	if (err)
7937 		return err;
7938 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
7939 	__acquire(&rdev->wiphy.mtx);
7940 
7941 	if (!rdev->ops->dump_mpp) {
7942 		err = -EOPNOTSUPP;
7943 		goto out_err;
7944 	}
7945 
7946 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7947 		err = -EOPNOTSUPP;
7948 		goto out_err;
7949 	}
7950 
7951 	while (1) {
7952 		err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
7953 				    mpp, &pinfo);
7954 		if (err == -ENOENT)
7955 			break;
7956 		if (err)
7957 			goto out_err;
7958 
7959 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7960 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
7961 				       wdev->netdev, dst, mpp,
7962 				       &pinfo) < 0)
7963 			goto out;
7964 
7965 		path_idx++;
7966 	}
7967 
7968  out:
7969 	cb->args[2] = path_idx;
7970 	err = skb->len;
7971  out_err:
7972 	wiphy_unlock(&rdev->wiphy);
7973 	return err;
7974 }
7975 
7976 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
7977 {
7978 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7979 	struct net_device *dev = info->user_ptr[1];
7980 	struct bss_parameters params;
7981 
7982 	memset(&params, 0, sizeof(params));
7983 	params.link_id = nl80211_link_id_or_invalid(info->attrs);
7984 	/* default to not changing parameters */
7985 	params.use_cts_prot = -1;
7986 	params.use_short_preamble = -1;
7987 	params.use_short_slot_time = -1;
7988 	params.ap_isolate = -1;
7989 	params.ht_opmode = -1;
7990 	params.p2p_ctwindow = -1;
7991 	params.p2p_opp_ps = -1;
7992 
7993 	if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
7994 		params.use_cts_prot =
7995 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
7996 	if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
7997 		params.use_short_preamble =
7998 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
7999 	if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
8000 		params.use_short_slot_time =
8001 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
8002 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8003 		params.basic_rates =
8004 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8005 		params.basic_rates_len =
8006 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8007 	}
8008 	if (info->attrs[NL80211_ATTR_AP_ISOLATE])
8009 		params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
8010 	if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
8011 		params.ht_opmode =
8012 			nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
8013 
8014 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
8015 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
8016 			return -EINVAL;
8017 		params.p2p_ctwindow =
8018 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
8019 		if (params.p2p_ctwindow != 0 &&
8020 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
8021 			return -EINVAL;
8022 	}
8023 
8024 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
8025 		u8 tmp;
8026 
8027 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
8028 			return -EINVAL;
8029 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
8030 		params.p2p_opp_ps = tmp;
8031 		if (params.p2p_opp_ps &&
8032 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
8033 			return -EINVAL;
8034 	}
8035 
8036 	if (!rdev->ops->change_bss)
8037 		return -EOPNOTSUPP;
8038 
8039 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
8040 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
8041 		return -EOPNOTSUPP;
8042 
8043 	return rdev_change_bss(rdev, dev, &params);
8044 }
8045 
8046 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
8047 {
8048 	char *data = NULL;
8049 	bool is_indoor;
8050 	enum nl80211_user_reg_hint_type user_reg_hint_type;
8051 	u32 owner_nlportid;
8052 
8053 	/*
8054 	 * You should only get this when cfg80211 hasn't yet initialized
8055 	 * completely when built-in to the kernel right between the time
8056 	 * window between nl80211_init() and regulatory_init(), if that is
8057 	 * even possible.
8058 	 */
8059 	if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
8060 		return -EINPROGRESS;
8061 
8062 	if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
8063 		user_reg_hint_type =
8064 		  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
8065 	else
8066 		user_reg_hint_type = NL80211_USER_REG_HINT_USER;
8067 
8068 	switch (user_reg_hint_type) {
8069 	case NL80211_USER_REG_HINT_USER:
8070 	case NL80211_USER_REG_HINT_CELL_BASE:
8071 		if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
8072 			return -EINVAL;
8073 
8074 		data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
8075 		return regulatory_hint_user(data, user_reg_hint_type);
8076 	case NL80211_USER_REG_HINT_INDOOR:
8077 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
8078 			owner_nlportid = info->snd_portid;
8079 			is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
8080 		} else {
8081 			owner_nlportid = 0;
8082 			is_indoor = true;
8083 		}
8084 
8085 		return regulatory_hint_indoor(is_indoor, owner_nlportid);
8086 	default:
8087 		return -EINVAL;
8088 	}
8089 }
8090 
8091 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
8092 {
8093 	return reg_reload_regdb();
8094 }
8095 
8096 static int nl80211_get_mesh_config(struct sk_buff *skb,
8097 				   struct genl_info *info)
8098 {
8099 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8100 	struct net_device *dev = info->user_ptr[1];
8101 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8102 	struct mesh_config cur_params;
8103 	int err = 0;
8104 	void *hdr;
8105 	struct nlattr *pinfoattr;
8106 	struct sk_buff *msg;
8107 
8108 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
8109 		return -EOPNOTSUPP;
8110 
8111 	if (!rdev->ops->get_mesh_config)
8112 		return -EOPNOTSUPP;
8113 
8114 	/* If not connected, get default parameters */
8115 	if (!wdev->u.mesh.id_len)
8116 		memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
8117 	else
8118 		err = rdev_get_mesh_config(rdev, dev, &cur_params);
8119 
8120 	if (err)
8121 		return err;
8122 
8123 	/* Draw up a netlink message to send back */
8124 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8125 	if (!msg)
8126 		return -ENOMEM;
8127 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8128 			     NL80211_CMD_GET_MESH_CONFIG);
8129 	if (!hdr)
8130 		goto out;
8131 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
8132 	if (!pinfoattr)
8133 		goto nla_put_failure;
8134 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8135 	    nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
8136 			cur_params.dot11MeshRetryTimeout) ||
8137 	    nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
8138 			cur_params.dot11MeshConfirmTimeout) ||
8139 	    nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
8140 			cur_params.dot11MeshHoldingTimeout) ||
8141 	    nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
8142 			cur_params.dot11MeshMaxPeerLinks) ||
8143 	    nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
8144 		       cur_params.dot11MeshMaxRetries) ||
8145 	    nla_put_u8(msg, NL80211_MESHCONF_TTL,
8146 		       cur_params.dot11MeshTTL) ||
8147 	    nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
8148 		       cur_params.element_ttl) ||
8149 	    nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8150 		       cur_params.auto_open_plinks) ||
8151 	    nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
8152 			cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
8153 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
8154 		       cur_params.dot11MeshHWMPmaxPREQretries) ||
8155 	    nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
8156 			cur_params.path_refresh_time) ||
8157 	    nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
8158 			cur_params.min_discovery_timeout) ||
8159 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
8160 			cur_params.dot11MeshHWMPactivePathTimeout) ||
8161 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
8162 			cur_params.dot11MeshHWMPpreqMinInterval) ||
8163 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
8164 			cur_params.dot11MeshHWMPperrMinInterval) ||
8165 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
8166 			cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
8167 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
8168 		       cur_params.dot11MeshHWMPRootMode) ||
8169 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
8170 			cur_params.dot11MeshHWMPRannInterval) ||
8171 	    nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
8172 		       cur_params.dot11MeshGateAnnouncementProtocol) ||
8173 	    nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
8174 		       cur_params.dot11MeshForwarding) ||
8175 	    nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
8176 			cur_params.rssi_threshold) ||
8177 	    nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
8178 			cur_params.ht_opmode) ||
8179 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
8180 			cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
8181 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
8182 			cur_params.dot11MeshHWMProotInterval) ||
8183 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
8184 			cur_params.dot11MeshHWMPconfirmationInterval) ||
8185 	    nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
8186 			cur_params.power_mode) ||
8187 	    nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
8188 			cur_params.dot11MeshAwakeWindowDuration) ||
8189 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
8190 			cur_params.plink_timeout) ||
8191 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
8192 		       cur_params.dot11MeshConnectedToMeshGate) ||
8193 	    nla_put_u8(msg, NL80211_MESHCONF_NOLEARN,
8194 		       cur_params.dot11MeshNolearn) ||
8195 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_AS,
8196 		       cur_params.dot11MeshConnectedToAuthServer))
8197 		goto nla_put_failure;
8198 	nla_nest_end(msg, pinfoattr);
8199 	genlmsg_end(msg, hdr);
8200 	return genlmsg_reply(msg, info);
8201 
8202  nla_put_failure:
8203  out:
8204 	nlmsg_free(msg);
8205 	return -ENOBUFS;
8206 }
8207 
8208 static const struct nla_policy
8209 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
8210 	[NL80211_MESHCONF_RETRY_TIMEOUT] =
8211 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8212 	[NL80211_MESHCONF_CONFIRM_TIMEOUT] =
8213 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8214 	[NL80211_MESHCONF_HOLDING_TIMEOUT] =
8215 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8216 	[NL80211_MESHCONF_MAX_PEER_LINKS] =
8217 		NLA_POLICY_RANGE(NLA_U16, 0, 255),
8218 	[NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
8219 	[NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8220 	[NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8221 	[NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
8222 	[NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
8223 		NLA_POLICY_RANGE(NLA_U32, 1, 255),
8224 	[NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
8225 	[NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
8226 	[NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
8227 	[NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
8228 	[NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
8229 		NLA_POLICY_MIN(NLA_U16, 1),
8230 	[NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
8231 		NLA_POLICY_MIN(NLA_U16, 1),
8232 	[NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
8233 		NLA_POLICY_MIN(NLA_U16, 1),
8234 	[NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
8235 	[NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
8236 		NLA_POLICY_MIN(NLA_U16, 1),
8237 	[NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
8238 	[NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
8239 	[NL80211_MESHCONF_RSSI_THRESHOLD] =
8240 		NLA_POLICY_RANGE(NLA_S32, -255, 0),
8241 	[NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
8242 	[NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
8243 	[NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
8244 		NLA_POLICY_MIN(NLA_U16, 1),
8245 	[NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
8246 		NLA_POLICY_MIN(NLA_U16, 1),
8247 	[NL80211_MESHCONF_POWER_MODE] =
8248 		NLA_POLICY_RANGE(NLA_U32,
8249 				 NL80211_MESH_POWER_ACTIVE,
8250 				 NL80211_MESH_POWER_MAX),
8251 	[NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
8252 	[NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
8253 	[NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8254 	[NL80211_MESHCONF_NOLEARN] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8255 	[NL80211_MESHCONF_CONNECTED_TO_AS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8256 };
8257 
8258 static const struct nla_policy
8259 	nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
8260 	[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
8261 	[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
8262 	[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
8263 	[NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
8264 	[NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
8265 	[NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
8266 	[NL80211_MESH_SETUP_IE] =
8267 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
8268 				       IEEE80211_MAX_DATA_LEN),
8269 	[NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
8270 };
8271 
8272 static int nl80211_parse_mesh_config(struct genl_info *info,
8273 				     struct mesh_config *cfg,
8274 				     u32 *mask_out)
8275 {
8276 	struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
8277 	u32 mask = 0;
8278 	u16 ht_opmode;
8279 
8280 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)	\
8281 do {									\
8282 	if (tb[attr]) {							\
8283 		cfg->param = fn(tb[attr]);				\
8284 		mask |= BIT((attr) - 1);				\
8285 	}								\
8286 } while (0)
8287 
8288 	if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
8289 		return -EINVAL;
8290 	if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
8291 		return -EINVAL;
8292 
8293 	/* This makes sure that there aren't more than 32 mesh config
8294 	 * parameters (otherwise our bitfield scheme would not work.) */
8295 	BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
8296 
8297 	/* Fill in the params struct */
8298 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
8299 				  NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
8300 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
8301 				  NL80211_MESHCONF_CONFIRM_TIMEOUT,
8302 				  nla_get_u16);
8303 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
8304 				  NL80211_MESHCONF_HOLDING_TIMEOUT,
8305 				  nla_get_u16);
8306 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
8307 				  NL80211_MESHCONF_MAX_PEER_LINKS,
8308 				  nla_get_u16);
8309 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
8310 				  NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
8311 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
8312 				  NL80211_MESHCONF_TTL, nla_get_u8);
8313 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
8314 				  NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
8315 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
8316 				  NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8317 				  nla_get_u8);
8318 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
8319 				  mask,
8320 				  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
8321 				  nla_get_u32);
8322 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
8323 				  NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
8324 				  nla_get_u8);
8325 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
8326 				  NL80211_MESHCONF_PATH_REFRESH_TIME,
8327 				  nla_get_u32);
8328 	if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
8329 	    (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
8330 		return -EINVAL;
8331 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
8332 				  NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
8333 				  nla_get_u16);
8334 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
8335 				  mask,
8336 				  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
8337 				  nla_get_u32);
8338 	if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
8339 	    (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
8340 	     cfg->dot11MeshHWMPactivePathTimeout > 65535))
8341 		return -EINVAL;
8342 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
8343 				  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
8344 				  nla_get_u16);
8345 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
8346 				  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
8347 				  nla_get_u16);
8348 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8349 				  dot11MeshHWMPnetDiameterTraversalTime, mask,
8350 				  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
8351 				  nla_get_u16);
8352 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
8353 				  NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
8354 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
8355 				  NL80211_MESHCONF_HWMP_RANN_INTERVAL,
8356 				  nla_get_u16);
8357 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
8358 				  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
8359 				  nla_get_u8);
8360 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
8361 				  NL80211_MESHCONF_FORWARDING, nla_get_u8);
8362 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
8363 				  NL80211_MESHCONF_RSSI_THRESHOLD,
8364 				  nla_get_s32);
8365 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
8366 				  NL80211_MESHCONF_CONNECTED_TO_GATE,
8367 				  nla_get_u8);
8368 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToAuthServer, mask,
8369 				  NL80211_MESHCONF_CONNECTED_TO_AS,
8370 				  nla_get_u8);
8371 	/*
8372 	 * Check HT operation mode based on
8373 	 * IEEE 802.11-2016 9.4.2.57 HT Operation element.
8374 	 */
8375 	if (tb[NL80211_MESHCONF_HT_OPMODE]) {
8376 		ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
8377 
8378 		if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
8379 				  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
8380 				  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
8381 			return -EINVAL;
8382 
8383 		/* NON_HT_STA bit is reserved, but some programs set it */
8384 		ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
8385 
8386 		cfg->ht_opmode = ht_opmode;
8387 		mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
8388 	}
8389 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8390 				  dot11MeshHWMPactivePathToRootTimeout, mask,
8391 				  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
8392 				  nla_get_u32);
8393 	if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
8394 	    (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
8395 	     cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
8396 		return -EINVAL;
8397 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
8398 				  NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
8399 				  nla_get_u16);
8400 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
8401 				  mask,
8402 				  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
8403 				  nla_get_u16);
8404 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
8405 				  NL80211_MESHCONF_POWER_MODE, nla_get_u32);
8406 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
8407 				  NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
8408 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
8409 				  NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
8410 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNolearn, mask,
8411 				  NL80211_MESHCONF_NOLEARN, nla_get_u8);
8412 	if (mask_out)
8413 		*mask_out = mask;
8414 
8415 	return 0;
8416 
8417 #undef FILL_IN_MESH_PARAM_IF_SET
8418 }
8419 
8420 static int nl80211_parse_mesh_setup(struct genl_info *info,
8421 				     struct mesh_setup *setup)
8422 {
8423 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8424 	struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
8425 
8426 	if (!info->attrs[NL80211_ATTR_MESH_SETUP])
8427 		return -EINVAL;
8428 	if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
8429 		return -EINVAL;
8430 
8431 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
8432 		setup->sync_method =
8433 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
8434 		 IEEE80211_SYNC_METHOD_VENDOR :
8435 		 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
8436 
8437 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
8438 		setup->path_sel_proto =
8439 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
8440 		 IEEE80211_PATH_PROTOCOL_VENDOR :
8441 		 IEEE80211_PATH_PROTOCOL_HWMP;
8442 
8443 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
8444 		setup->path_metric =
8445 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
8446 		 IEEE80211_PATH_METRIC_VENDOR :
8447 		 IEEE80211_PATH_METRIC_AIRTIME;
8448 
8449 	if (tb[NL80211_MESH_SETUP_IE]) {
8450 		struct nlattr *ieattr =
8451 			tb[NL80211_MESH_SETUP_IE];
8452 		setup->ie = nla_data(ieattr);
8453 		setup->ie_len = nla_len(ieattr);
8454 	}
8455 	if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
8456 	    !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
8457 		return -EINVAL;
8458 	setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
8459 	setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
8460 	setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
8461 	if (setup->is_secure)
8462 		setup->user_mpm = true;
8463 
8464 	if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
8465 		if (!setup->user_mpm)
8466 			return -EINVAL;
8467 		setup->auth_id =
8468 			nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
8469 	}
8470 
8471 	return 0;
8472 }
8473 
8474 static int nl80211_update_mesh_config(struct sk_buff *skb,
8475 				      struct genl_info *info)
8476 {
8477 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8478 	struct net_device *dev = info->user_ptr[1];
8479 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8480 	struct mesh_config cfg = {};
8481 	u32 mask;
8482 	int err;
8483 
8484 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
8485 		return -EOPNOTSUPP;
8486 
8487 	if (!rdev->ops->update_mesh_config)
8488 		return -EOPNOTSUPP;
8489 
8490 	err = nl80211_parse_mesh_config(info, &cfg, &mask);
8491 	if (err)
8492 		return err;
8493 
8494 	if (!wdev->u.mesh.id_len)
8495 		err = -ENOLINK;
8496 
8497 	if (!err)
8498 		err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
8499 
8500 	return err;
8501 }
8502 
8503 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
8504 			      struct sk_buff *msg)
8505 {
8506 	struct nlattr *nl_reg_rules;
8507 	unsigned int i;
8508 
8509 	if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
8510 	    (regdom->dfs_region &&
8511 	     nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
8512 		goto nla_put_failure;
8513 
8514 	nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
8515 	if (!nl_reg_rules)
8516 		goto nla_put_failure;
8517 
8518 	for (i = 0; i < regdom->n_reg_rules; i++) {
8519 		struct nlattr *nl_reg_rule;
8520 		const struct ieee80211_reg_rule *reg_rule;
8521 		const struct ieee80211_freq_range *freq_range;
8522 		const struct ieee80211_power_rule *power_rule;
8523 		unsigned int max_bandwidth_khz;
8524 
8525 		reg_rule = &regdom->reg_rules[i];
8526 		freq_range = &reg_rule->freq_range;
8527 		power_rule = &reg_rule->power_rule;
8528 
8529 		nl_reg_rule = nla_nest_start_noflag(msg, i);
8530 		if (!nl_reg_rule)
8531 			goto nla_put_failure;
8532 
8533 		max_bandwidth_khz = freq_range->max_bandwidth_khz;
8534 		if (!max_bandwidth_khz)
8535 			max_bandwidth_khz = reg_get_max_bandwidth(regdom,
8536 								  reg_rule);
8537 
8538 		if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
8539 				reg_rule->flags) ||
8540 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
8541 				freq_range->start_freq_khz) ||
8542 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
8543 				freq_range->end_freq_khz) ||
8544 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
8545 				max_bandwidth_khz) ||
8546 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
8547 				power_rule->max_antenna_gain) ||
8548 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
8549 				power_rule->max_eirp) ||
8550 		    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
8551 				reg_rule->dfs_cac_ms))
8552 			goto nla_put_failure;
8553 
8554 		if ((reg_rule->flags & NL80211_RRF_PSD) &&
8555 		    nla_put_s8(msg, NL80211_ATTR_POWER_RULE_PSD,
8556 			       reg_rule->psd))
8557 			goto nla_put_failure;
8558 
8559 		nla_nest_end(msg, nl_reg_rule);
8560 	}
8561 
8562 	nla_nest_end(msg, nl_reg_rules);
8563 	return 0;
8564 
8565 nla_put_failure:
8566 	return -EMSGSIZE;
8567 }
8568 
8569 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
8570 {
8571 	const struct ieee80211_regdomain *regdom = NULL;
8572 	struct cfg80211_registered_device *rdev;
8573 	struct wiphy *wiphy = NULL;
8574 	struct sk_buff *msg;
8575 	int err = -EMSGSIZE;
8576 	void *hdr;
8577 
8578 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8579 	if (!msg)
8580 		return -ENOBUFS;
8581 
8582 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8583 			     NL80211_CMD_GET_REG);
8584 	if (!hdr)
8585 		goto put_failure;
8586 
8587 	rtnl_lock();
8588 
8589 	if (info->attrs[NL80211_ATTR_WIPHY]) {
8590 		bool self_managed;
8591 
8592 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
8593 		if (IS_ERR(rdev)) {
8594 			err = PTR_ERR(rdev);
8595 			goto nla_put_failure;
8596 		}
8597 
8598 		wiphy = &rdev->wiphy;
8599 		self_managed = wiphy->regulatory_flags &
8600 			       REGULATORY_WIPHY_SELF_MANAGED;
8601 
8602 		rcu_read_lock();
8603 
8604 		regdom = get_wiphy_regdom(wiphy);
8605 
8606 		/* a self-managed-reg device must have a private regdom */
8607 		if (WARN_ON(!regdom && self_managed)) {
8608 			err = -EINVAL;
8609 			goto nla_put_failure_rcu;
8610 		}
8611 
8612 		if (regdom &&
8613 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8614 			goto nla_put_failure_rcu;
8615 	} else {
8616 		rcu_read_lock();
8617 	}
8618 
8619 	if (!wiphy && reg_last_request_cell_base() &&
8620 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8621 			NL80211_USER_REG_HINT_CELL_BASE))
8622 		goto nla_put_failure_rcu;
8623 
8624 	if (!regdom)
8625 		regdom = rcu_dereference(cfg80211_regdomain);
8626 
8627 	if (nl80211_put_regdom(regdom, msg))
8628 		goto nla_put_failure_rcu;
8629 
8630 	rcu_read_unlock();
8631 
8632 	genlmsg_end(msg, hdr);
8633 	rtnl_unlock();
8634 	return genlmsg_reply(msg, info);
8635 
8636 nla_put_failure_rcu:
8637 	rcu_read_unlock();
8638 nla_put_failure:
8639 	rtnl_unlock();
8640 put_failure:
8641 	nlmsg_free(msg);
8642 	return err;
8643 }
8644 
8645 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
8646 			       u32 seq, int flags, struct wiphy *wiphy,
8647 			       const struct ieee80211_regdomain *regdom)
8648 {
8649 	void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
8650 				   NL80211_CMD_GET_REG);
8651 
8652 	if (!hdr)
8653 		return -1;
8654 
8655 	genl_dump_check_consistent(cb, hdr);
8656 
8657 	if (nl80211_put_regdom(regdom, msg))
8658 		goto nla_put_failure;
8659 
8660 	if (!wiphy && reg_last_request_cell_base() &&
8661 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8662 			NL80211_USER_REG_HINT_CELL_BASE))
8663 		goto nla_put_failure;
8664 
8665 	if (wiphy &&
8666 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8667 		goto nla_put_failure;
8668 
8669 	if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
8670 	    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
8671 		goto nla_put_failure;
8672 
8673 	genlmsg_end(msg, hdr);
8674 	return 0;
8675 
8676 nla_put_failure:
8677 	genlmsg_cancel(msg, hdr);
8678 	return -EMSGSIZE;
8679 }
8680 
8681 static int nl80211_get_reg_dump(struct sk_buff *skb,
8682 				struct netlink_callback *cb)
8683 {
8684 	const struct ieee80211_regdomain *regdom = NULL;
8685 	struct cfg80211_registered_device *rdev;
8686 	int err, reg_idx, start = cb->args[2];
8687 
8688 	rcu_read_lock();
8689 
8690 	if (cfg80211_regdomain && start == 0) {
8691 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8692 					  NLM_F_MULTI, NULL,
8693 					  rcu_dereference(cfg80211_regdomain));
8694 		if (err < 0)
8695 			goto out_err;
8696 	}
8697 
8698 	/* the global regdom is idx 0 */
8699 	reg_idx = 1;
8700 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
8701 		regdom = get_wiphy_regdom(&rdev->wiphy);
8702 		if (!regdom)
8703 			continue;
8704 
8705 		if (++reg_idx <= start)
8706 			continue;
8707 
8708 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8709 					  NLM_F_MULTI, &rdev->wiphy, regdom);
8710 		if (err < 0) {
8711 			reg_idx--;
8712 			break;
8713 		}
8714 	}
8715 
8716 	cb->args[2] = reg_idx;
8717 	err = skb->len;
8718 out_err:
8719 	rcu_read_unlock();
8720 	return err;
8721 }
8722 
8723 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
8724 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
8725 	[NL80211_ATTR_REG_RULE_FLAGS]		= { .type = NLA_U32 },
8726 	[NL80211_ATTR_FREQ_RANGE_START]		= { .type = NLA_U32 },
8727 	[NL80211_ATTR_FREQ_RANGE_END]		= { .type = NLA_U32 },
8728 	[NL80211_ATTR_FREQ_RANGE_MAX_BW]	= { .type = NLA_U32 },
8729 	[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]	= { .type = NLA_U32 },
8730 	[NL80211_ATTR_POWER_RULE_MAX_EIRP]	= { .type = NLA_U32 },
8731 	[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
8732 };
8733 
8734 static int parse_reg_rule(struct nlattr *tb[],
8735 	struct ieee80211_reg_rule *reg_rule)
8736 {
8737 	struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
8738 	struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
8739 
8740 	if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
8741 		return -EINVAL;
8742 	if (!tb[NL80211_ATTR_FREQ_RANGE_START])
8743 		return -EINVAL;
8744 	if (!tb[NL80211_ATTR_FREQ_RANGE_END])
8745 		return -EINVAL;
8746 	if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
8747 		return -EINVAL;
8748 	if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
8749 		return -EINVAL;
8750 
8751 	reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
8752 
8753 	freq_range->start_freq_khz =
8754 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
8755 	freq_range->end_freq_khz =
8756 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
8757 	freq_range->max_bandwidth_khz =
8758 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
8759 
8760 	power_rule->max_eirp =
8761 		nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
8762 
8763 	if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
8764 		power_rule->max_antenna_gain =
8765 			nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
8766 
8767 	if (tb[NL80211_ATTR_DFS_CAC_TIME])
8768 		reg_rule->dfs_cac_ms =
8769 			nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
8770 
8771 	return 0;
8772 }
8773 
8774 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
8775 {
8776 	struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
8777 	struct nlattr *nl_reg_rule;
8778 	char *alpha2;
8779 	int rem_reg_rules, r;
8780 	u32 num_rules = 0, rule_idx = 0;
8781 	enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
8782 	struct ieee80211_regdomain *rd;
8783 
8784 	if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
8785 		return -EINVAL;
8786 
8787 	if (!info->attrs[NL80211_ATTR_REG_RULES])
8788 		return -EINVAL;
8789 
8790 	alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
8791 
8792 	if (info->attrs[NL80211_ATTR_DFS_REGION])
8793 		dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
8794 
8795 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8796 			    rem_reg_rules) {
8797 		num_rules++;
8798 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
8799 			return -EINVAL;
8800 	}
8801 
8802 	rtnl_lock();
8803 	if (!reg_is_valid_request(alpha2)) {
8804 		r = -EINVAL;
8805 		goto out;
8806 	}
8807 
8808 	rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
8809 	if (!rd) {
8810 		r = -ENOMEM;
8811 		goto out;
8812 	}
8813 
8814 	rd->n_reg_rules = num_rules;
8815 	rd->alpha2[0] = alpha2[0];
8816 	rd->alpha2[1] = alpha2[1];
8817 
8818 	/*
8819 	 * Disable DFS master mode if the DFS region was
8820 	 * not supported or known on this kernel.
8821 	 */
8822 	if (reg_supported_dfs_region(dfs_region))
8823 		rd->dfs_region = dfs_region;
8824 
8825 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8826 			    rem_reg_rules) {
8827 		r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
8828 						nl_reg_rule, reg_rule_policy,
8829 						info->extack);
8830 		if (r)
8831 			goto bad_reg;
8832 		r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
8833 		if (r)
8834 			goto bad_reg;
8835 
8836 		rule_idx++;
8837 
8838 		if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
8839 			r = -EINVAL;
8840 			goto bad_reg;
8841 		}
8842 	}
8843 
8844 	r = set_regdom(rd, REGD_SOURCE_CRDA);
8845 	/* set_regdom takes ownership of rd */
8846 	rd = NULL;
8847  bad_reg:
8848 	kfree(rd);
8849  out:
8850 	rtnl_unlock();
8851 	return r;
8852 }
8853 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
8854 
8855 static int validate_scan_freqs(struct nlattr *freqs)
8856 {
8857 	struct nlattr *attr1, *attr2;
8858 	int n_channels = 0, tmp1, tmp2;
8859 
8860 	nla_for_each_nested(attr1, freqs, tmp1)
8861 		if (nla_len(attr1) != sizeof(u32))
8862 			return 0;
8863 
8864 	nla_for_each_nested(attr1, freqs, tmp1) {
8865 		n_channels++;
8866 		/*
8867 		 * Some hardware has a limited channel list for
8868 		 * scanning, and it is pretty much nonsensical
8869 		 * to scan for a channel twice, so disallow that
8870 		 * and don't require drivers to check that the
8871 		 * channel list they get isn't longer than what
8872 		 * they can scan, as long as they can scan all
8873 		 * the channels they registered at once.
8874 		 */
8875 		nla_for_each_nested(attr2, freqs, tmp2)
8876 			if (attr1 != attr2 &&
8877 			    nla_get_u32(attr1) == nla_get_u32(attr2))
8878 				return 0;
8879 	}
8880 
8881 	return n_channels;
8882 }
8883 
8884 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
8885 {
8886 	return b < NUM_NL80211_BANDS && wiphy->bands[b];
8887 }
8888 
8889 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
8890 			    struct cfg80211_bss_selection *bss_select)
8891 {
8892 	struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
8893 	struct nlattr *nest;
8894 	int err;
8895 	bool found = false;
8896 	int i;
8897 
8898 	/* only process one nested attribute */
8899 	nest = nla_data(nla);
8900 	if (!nla_ok(nest, nla_len(nest)))
8901 		return -EINVAL;
8902 
8903 	err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
8904 					  nest, nl80211_bss_select_policy,
8905 					  NULL);
8906 	if (err)
8907 		return err;
8908 
8909 	/* only one attribute may be given */
8910 	for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
8911 		if (attr[i]) {
8912 			if (found)
8913 				return -EINVAL;
8914 			found = true;
8915 		}
8916 	}
8917 
8918 	bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
8919 
8920 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
8921 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
8922 
8923 	if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
8924 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
8925 		bss_select->param.band_pref =
8926 			nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
8927 		if (!is_band_valid(wiphy, bss_select->param.band_pref))
8928 			return -EINVAL;
8929 	}
8930 
8931 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
8932 		struct nl80211_bss_select_rssi_adjust *adj_param;
8933 
8934 		adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
8935 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
8936 		bss_select->param.adjust.band = adj_param->band;
8937 		bss_select->param.adjust.delta = adj_param->delta;
8938 		if (!is_band_valid(wiphy, bss_select->param.adjust.band))
8939 			return -EINVAL;
8940 	}
8941 
8942 	/* user-space did not provide behaviour attribute */
8943 	if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
8944 		return -EINVAL;
8945 
8946 	if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
8947 		return -EINVAL;
8948 
8949 	return 0;
8950 }
8951 
8952 int nl80211_parse_random_mac(struct nlattr **attrs,
8953 			     u8 *mac_addr, u8 *mac_addr_mask)
8954 {
8955 	int i;
8956 
8957 	if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
8958 		eth_zero_addr(mac_addr);
8959 		eth_zero_addr(mac_addr_mask);
8960 		mac_addr[0] = 0x2;
8961 		mac_addr_mask[0] = 0x3;
8962 
8963 		return 0;
8964 	}
8965 
8966 	/* need both or none */
8967 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
8968 		return -EINVAL;
8969 
8970 	memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
8971 	memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
8972 
8973 	/* don't allow or configure an mcast address */
8974 	if (!is_multicast_ether_addr(mac_addr_mask) ||
8975 	    is_multicast_ether_addr(mac_addr))
8976 		return -EINVAL;
8977 
8978 	/*
8979 	 * allow users to pass a MAC address that has bits set outside
8980 	 * of the mask, but don't bother drivers with having to deal
8981 	 * with such bits
8982 	 */
8983 	for (i = 0; i < ETH_ALEN; i++)
8984 		mac_addr[i] &= mac_addr_mask[i];
8985 
8986 	return 0;
8987 }
8988 
8989 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev,
8990 					      struct ieee80211_channel *chan)
8991 {
8992 	unsigned int link_id;
8993 	bool all_ok = true;
8994 
8995 	lockdep_assert_wiphy(wdev->wiphy);
8996 
8997 	if (!cfg80211_beaconing_iface_active(wdev))
8998 		return true;
8999 
9000 	/*
9001 	 * FIXME: check if we have a free HW resource/link for chan
9002 	 *
9003 	 * This, as well as the FIXME below, requires knowing the link
9004 	 * capabilities of the hardware.
9005 	 */
9006 
9007 	/* we cannot leave radar channels */
9008 	for_each_valid_link(wdev, link_id) {
9009 		struct cfg80211_chan_def *chandef;
9010 
9011 		chandef = wdev_chandef(wdev, link_id);
9012 		if (!chandef || !chandef->chan)
9013 			continue;
9014 
9015 		/*
9016 		 * FIXME: don't require all_ok, but rather check only the
9017 		 *	  correct HW resource/link onto which 'chan' falls,
9018 		 *	  as only that link leaves the channel for doing
9019 		 *	  the off-channel operation.
9020 		 */
9021 
9022 		if (chandef->chan->flags & IEEE80211_CHAN_RADAR)
9023 			all_ok = false;
9024 	}
9025 
9026 	if (all_ok)
9027 		return true;
9028 
9029 	return regulatory_pre_cac_allowed(wdev->wiphy);
9030 }
9031 
9032 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
9033 				    enum nl80211_ext_feature_index feat)
9034 {
9035 	if (!(flags & flag))
9036 		return true;
9037 	if (wiphy_ext_feature_isset(wiphy, feat))
9038 		return true;
9039 	return false;
9040 }
9041 
9042 static int
9043 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
9044 			 void *request, struct nlattr **attrs,
9045 			 bool is_sched_scan)
9046 {
9047 	u8 *mac_addr, *mac_addr_mask;
9048 	u32 *flags;
9049 	enum nl80211_feature_flags randomness_flag;
9050 
9051 	if (!attrs[NL80211_ATTR_SCAN_FLAGS])
9052 		return 0;
9053 
9054 	if (is_sched_scan) {
9055 		struct cfg80211_sched_scan_request *req = request;
9056 
9057 		randomness_flag = wdev ?
9058 				  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
9059 				  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
9060 		flags = &req->flags;
9061 		mac_addr = req->mac_addr;
9062 		mac_addr_mask = req->mac_addr_mask;
9063 	} else {
9064 		struct cfg80211_scan_request *req = request;
9065 
9066 		randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
9067 		flags = &req->flags;
9068 		mac_addr = req->mac_addr;
9069 		mac_addr_mask = req->mac_addr_mask;
9070 	}
9071 
9072 	*flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
9073 
9074 	if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
9075 	     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
9076 	    !nl80211_check_scan_feat(wiphy, *flags,
9077 				     NL80211_SCAN_FLAG_LOW_SPAN,
9078 				     NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
9079 	    !nl80211_check_scan_feat(wiphy, *flags,
9080 				     NL80211_SCAN_FLAG_LOW_POWER,
9081 				     NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
9082 	    !nl80211_check_scan_feat(wiphy, *flags,
9083 				     NL80211_SCAN_FLAG_HIGH_ACCURACY,
9084 				     NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
9085 	    !nl80211_check_scan_feat(wiphy, *flags,
9086 				     NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
9087 				     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
9088 	    !nl80211_check_scan_feat(wiphy, *flags,
9089 				     NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
9090 				     NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
9091 	    !nl80211_check_scan_feat(wiphy, *flags,
9092 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
9093 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
9094 	    !nl80211_check_scan_feat(wiphy, *flags,
9095 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
9096 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
9097 	    !nl80211_check_scan_feat(wiphy, *flags,
9098 				     NL80211_SCAN_FLAG_RANDOM_SN,
9099 				     NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
9100 	    !nl80211_check_scan_feat(wiphy, *flags,
9101 				     NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
9102 				     NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
9103 		return -EOPNOTSUPP;
9104 
9105 	if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
9106 		int err;
9107 
9108 		if (!(wiphy->features & randomness_flag) ||
9109 		    (wdev && wdev->connected))
9110 			return -EOPNOTSUPP;
9111 
9112 		err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
9113 		if (err)
9114 			return err;
9115 	}
9116 
9117 	return 0;
9118 }
9119 
9120 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
9121 {
9122 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9123 	struct wireless_dev *wdev = info->user_ptr[1];
9124 	struct cfg80211_scan_request *request;
9125 	struct nlattr *scan_freqs = NULL;
9126 	bool scan_freqs_khz = false;
9127 	struct nlattr *attr;
9128 	struct wiphy *wiphy;
9129 	int err, tmp, n_ssids = 0, n_channels, i;
9130 	size_t ie_len, size;
9131 
9132 	wiphy = &rdev->wiphy;
9133 
9134 	if (wdev->iftype == NL80211_IFTYPE_NAN)
9135 		return -EOPNOTSUPP;
9136 
9137 	if (!rdev->ops->scan)
9138 		return -EOPNOTSUPP;
9139 
9140 	if (rdev->scan_req || rdev->scan_msg)
9141 		return -EBUSY;
9142 
9143 	if (info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ]) {
9144 		if (!wiphy_ext_feature_isset(wiphy,
9145 					     NL80211_EXT_FEATURE_SCAN_FREQ_KHZ))
9146 			return -EOPNOTSUPP;
9147 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ];
9148 		scan_freqs_khz = true;
9149 	} else if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES])
9150 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQUENCIES];
9151 
9152 	if (scan_freqs) {
9153 		n_channels = validate_scan_freqs(scan_freqs);
9154 		if (!n_channels)
9155 			return -EINVAL;
9156 	} else {
9157 		n_channels = ieee80211_get_num_supported_channels(wiphy);
9158 	}
9159 
9160 	if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
9161 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
9162 			n_ssids++;
9163 
9164 	if (n_ssids > wiphy->max_scan_ssids)
9165 		return -EINVAL;
9166 
9167 	if (info->attrs[NL80211_ATTR_IE])
9168 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9169 	else
9170 		ie_len = 0;
9171 
9172 	if (ie_len > wiphy->max_scan_ie_len)
9173 		return -EINVAL;
9174 
9175 	size = struct_size(request, channels, n_channels);
9176 	size = size_add(size, array_size(sizeof(*request->ssids), n_ssids));
9177 	size = size_add(size, ie_len);
9178 	request = kzalloc(size, GFP_KERNEL);
9179 	if (!request)
9180 		return -ENOMEM;
9181 
9182 	if (n_ssids)
9183 		request->ssids = (void *)&request->channels[n_channels];
9184 	request->n_ssids = n_ssids;
9185 	if (ie_len) {
9186 		if (n_ssids)
9187 			request->ie = (void *)(request->ssids + n_ssids);
9188 		else
9189 			request->ie = (void *)(request->channels + n_channels);
9190 	}
9191 
9192 	i = 0;
9193 	if (scan_freqs) {
9194 		/* user specified, bail out if channel not found */
9195 		nla_for_each_nested(attr, scan_freqs, tmp) {
9196 			struct ieee80211_channel *chan;
9197 			int freq = nla_get_u32(attr);
9198 
9199 			if (!scan_freqs_khz)
9200 				freq = MHZ_TO_KHZ(freq);
9201 
9202 			chan = ieee80211_get_channel_khz(wiphy, freq);
9203 			if (!chan) {
9204 				err = -EINVAL;
9205 				goto out_free;
9206 			}
9207 
9208 			/* ignore disabled channels */
9209 			if (chan->flags & IEEE80211_CHAN_DISABLED)
9210 				continue;
9211 
9212 			request->channels[i] = chan;
9213 			i++;
9214 		}
9215 	} else {
9216 		enum nl80211_band band;
9217 
9218 		/* all channels */
9219 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
9220 			int j;
9221 
9222 			if (!wiphy->bands[band])
9223 				continue;
9224 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9225 				struct ieee80211_channel *chan;
9226 
9227 				chan = &wiphy->bands[band]->channels[j];
9228 
9229 				if (chan->flags & IEEE80211_CHAN_DISABLED)
9230 					continue;
9231 
9232 				request->channels[i] = chan;
9233 				i++;
9234 			}
9235 		}
9236 	}
9237 
9238 	if (!i) {
9239 		err = -EINVAL;
9240 		goto out_free;
9241 	}
9242 
9243 	request->n_channels = i;
9244 
9245 	for (i = 0; i < request->n_channels; i++) {
9246 		struct ieee80211_channel *chan = request->channels[i];
9247 
9248 		/* if we can go off-channel to the target channel we're good */
9249 		if (cfg80211_off_channel_oper_allowed(wdev, chan))
9250 			continue;
9251 
9252 		if (!cfg80211_wdev_on_sub_chan(wdev, chan, true)) {
9253 			err = -EBUSY;
9254 			goto out_free;
9255 		}
9256 	}
9257 
9258 	i = 0;
9259 	if (n_ssids) {
9260 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
9261 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9262 				err = -EINVAL;
9263 				goto out_free;
9264 			}
9265 			request->ssids[i].ssid_len = nla_len(attr);
9266 			memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
9267 			i++;
9268 		}
9269 	}
9270 
9271 	if (info->attrs[NL80211_ATTR_IE]) {
9272 		request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9273 		memcpy((void *)request->ie,
9274 		       nla_data(info->attrs[NL80211_ATTR_IE]),
9275 		       request->ie_len);
9276 	}
9277 
9278 	for (i = 0; i < NUM_NL80211_BANDS; i++)
9279 		if (wiphy->bands[i])
9280 			request->rates[i] =
9281 				(1 << wiphy->bands[i]->n_bitrates) - 1;
9282 
9283 	if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
9284 		nla_for_each_nested(attr,
9285 				    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
9286 				    tmp) {
9287 			enum nl80211_band band = nla_type(attr);
9288 
9289 			if (band < 0 || band >= NUM_NL80211_BANDS) {
9290 				err = -EINVAL;
9291 				goto out_free;
9292 			}
9293 
9294 			if (!wiphy->bands[band])
9295 				continue;
9296 
9297 			err = ieee80211_get_ratemask(wiphy->bands[band],
9298 						     nla_data(attr),
9299 						     nla_len(attr),
9300 						     &request->rates[band]);
9301 			if (err)
9302 				goto out_free;
9303 		}
9304 	}
9305 
9306 	if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
9307 		request->duration =
9308 			nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
9309 		request->duration_mandatory =
9310 			nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
9311 	}
9312 
9313 	err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
9314 				       false);
9315 	if (err)
9316 		goto out_free;
9317 
9318 	request->no_cck =
9319 		nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
9320 
9321 	/* Initial implementation used NL80211_ATTR_MAC to set the specific
9322 	 * BSSID to scan for. This was problematic because that same attribute
9323 	 * was already used for another purpose (local random MAC address). The
9324 	 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
9325 	 * compatibility with older userspace components, also use the
9326 	 * NL80211_ATTR_MAC value here if it can be determined to be used for
9327 	 * the specific BSSID use case instead of the random MAC address
9328 	 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
9329 	 */
9330 	if (info->attrs[NL80211_ATTR_BSSID])
9331 		memcpy(request->bssid,
9332 		       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
9333 	else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
9334 		 info->attrs[NL80211_ATTR_MAC])
9335 		memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
9336 		       ETH_ALEN);
9337 	else
9338 		eth_broadcast_addr(request->bssid);
9339 
9340 	request->wdev = wdev;
9341 	request->wiphy = &rdev->wiphy;
9342 	request->scan_start = jiffies;
9343 
9344 	rdev->scan_req = request;
9345 	err = cfg80211_scan(rdev);
9346 
9347 	if (err)
9348 		goto out_free;
9349 
9350 	nl80211_send_scan_start(rdev, wdev);
9351 	dev_hold(wdev->netdev);
9352 
9353 	return 0;
9354 
9355  out_free:
9356 	rdev->scan_req = NULL;
9357 	kfree(request);
9358 
9359 	return err;
9360 }
9361 
9362 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
9363 {
9364 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9365 	struct wireless_dev *wdev = info->user_ptr[1];
9366 
9367 	if (!rdev->ops->abort_scan)
9368 		return -EOPNOTSUPP;
9369 
9370 	if (rdev->scan_msg)
9371 		return 0;
9372 
9373 	if (!rdev->scan_req)
9374 		return -ENOENT;
9375 
9376 	rdev_abort_scan(rdev, wdev);
9377 	return 0;
9378 }
9379 
9380 static int
9381 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
9382 			       struct cfg80211_sched_scan_request *request,
9383 			       struct nlattr **attrs)
9384 {
9385 	int tmp, err, i = 0;
9386 	struct nlattr *attr;
9387 
9388 	if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9389 		u32 interval;
9390 
9391 		/*
9392 		 * If scan plans are not specified,
9393 		 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
9394 		 * case one scan plan will be set with the specified scan
9395 		 * interval and infinite number of iterations.
9396 		 */
9397 		interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
9398 		if (!interval)
9399 			return -EINVAL;
9400 
9401 		request->scan_plans[0].interval =
9402 			DIV_ROUND_UP(interval, MSEC_PER_SEC);
9403 		if (!request->scan_plans[0].interval)
9404 			return -EINVAL;
9405 
9406 		if (request->scan_plans[0].interval >
9407 		    wiphy->max_sched_scan_plan_interval)
9408 			request->scan_plans[0].interval =
9409 				wiphy->max_sched_scan_plan_interval;
9410 
9411 		return 0;
9412 	}
9413 
9414 	nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
9415 		struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
9416 
9417 		if (WARN_ON(i >= n_plans))
9418 			return -EINVAL;
9419 
9420 		err = nla_parse_nested_deprecated(plan,
9421 						  NL80211_SCHED_SCAN_PLAN_MAX,
9422 						  attr, nl80211_plan_policy,
9423 						  NULL);
9424 		if (err)
9425 			return err;
9426 
9427 		if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
9428 			return -EINVAL;
9429 
9430 		request->scan_plans[i].interval =
9431 			nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
9432 		if (!request->scan_plans[i].interval ||
9433 		    request->scan_plans[i].interval >
9434 		    wiphy->max_sched_scan_plan_interval)
9435 			return -EINVAL;
9436 
9437 		if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
9438 			request->scan_plans[i].iterations =
9439 				nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
9440 			if (!request->scan_plans[i].iterations ||
9441 			    (request->scan_plans[i].iterations >
9442 			     wiphy->max_sched_scan_plan_iterations))
9443 				return -EINVAL;
9444 		} else if (i < n_plans - 1) {
9445 			/*
9446 			 * All scan plans but the last one must specify
9447 			 * a finite number of iterations
9448 			 */
9449 			return -EINVAL;
9450 		}
9451 
9452 		i++;
9453 	}
9454 
9455 	/*
9456 	 * The last scan plan must not specify the number of
9457 	 * iterations, it is supposed to run infinitely
9458 	 */
9459 	if (request->scan_plans[n_plans - 1].iterations)
9460 		return  -EINVAL;
9461 
9462 	return 0;
9463 }
9464 
9465 static int
9466 nl80211_parse_sched_scan_per_band_rssi(struct wiphy *wiphy,
9467 				       struct cfg80211_match_set *match_sets,
9468 				       struct nlattr *tb_band_rssi,
9469 				       s32 rssi_thold)
9470 {
9471 	struct nlattr *attr;
9472 	int i, tmp, ret = 0;
9473 
9474 	if (!wiphy_ext_feature_isset(wiphy,
9475 		    NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD)) {
9476 		if (tb_band_rssi)
9477 			ret = -EOPNOTSUPP;
9478 		else
9479 			for (i = 0; i < NUM_NL80211_BANDS; i++)
9480 				match_sets->per_band_rssi_thold[i] =
9481 					NL80211_SCAN_RSSI_THOLD_OFF;
9482 		return ret;
9483 	}
9484 
9485 	for (i = 0; i < NUM_NL80211_BANDS; i++)
9486 		match_sets->per_band_rssi_thold[i] = rssi_thold;
9487 
9488 	nla_for_each_nested(attr, tb_band_rssi, tmp) {
9489 		enum nl80211_band band = nla_type(attr);
9490 
9491 		if (band < 0 || band >= NUM_NL80211_BANDS)
9492 			return -EINVAL;
9493 
9494 		match_sets->per_band_rssi_thold[band] =	nla_get_s32(attr);
9495 	}
9496 
9497 	return 0;
9498 }
9499 
9500 static struct cfg80211_sched_scan_request *
9501 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
9502 			 struct nlattr **attrs, int max_match_sets)
9503 {
9504 	struct cfg80211_sched_scan_request *request;
9505 	struct nlattr *attr;
9506 	int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
9507 	enum nl80211_band band;
9508 	size_t ie_len, size;
9509 	struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
9510 	s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
9511 
9512 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9513 		n_channels = validate_scan_freqs(
9514 				attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
9515 		if (!n_channels)
9516 			return ERR_PTR(-EINVAL);
9517 	} else {
9518 		n_channels = ieee80211_get_num_supported_channels(wiphy);
9519 	}
9520 
9521 	if (attrs[NL80211_ATTR_SCAN_SSIDS])
9522 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9523 				    tmp)
9524 			n_ssids++;
9525 
9526 	if (n_ssids > wiphy->max_sched_scan_ssids)
9527 		return ERR_PTR(-EINVAL);
9528 
9529 	/*
9530 	 * First, count the number of 'real' matchsets. Due to an issue with
9531 	 * the old implementation, matchsets containing only the RSSI attribute
9532 	 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
9533 	 * RSSI for all matchsets, rather than their own matchset for reporting
9534 	 * all APs with a strong RSSI. This is needed to be compatible with
9535 	 * older userspace that treated a matchset with only the RSSI as the
9536 	 * global RSSI for all other matchsets - if there are other matchsets.
9537 	 */
9538 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9539 		nla_for_each_nested(attr,
9540 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9541 				    tmp) {
9542 			struct nlattr *rssi;
9543 
9544 			err = nla_parse_nested_deprecated(tb,
9545 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9546 							  attr,
9547 							  nl80211_match_policy,
9548 							  NULL);
9549 			if (err)
9550 				return ERR_PTR(err);
9551 
9552 			/* SSID and BSSID are mutually exclusive */
9553 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
9554 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
9555 				return ERR_PTR(-EINVAL);
9556 
9557 			/* add other standalone attributes here */
9558 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
9559 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
9560 				n_match_sets++;
9561 				continue;
9562 			}
9563 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9564 			if (rssi)
9565 				default_match_rssi = nla_get_s32(rssi);
9566 		}
9567 	}
9568 
9569 	/* However, if there's no other matchset, add the RSSI one */
9570 	if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
9571 		n_match_sets = 1;
9572 
9573 	if (n_match_sets > max_match_sets)
9574 		return ERR_PTR(-EINVAL);
9575 
9576 	if (attrs[NL80211_ATTR_IE])
9577 		ie_len = nla_len(attrs[NL80211_ATTR_IE]);
9578 	else
9579 		ie_len = 0;
9580 
9581 	if (ie_len > wiphy->max_sched_scan_ie_len)
9582 		return ERR_PTR(-EINVAL);
9583 
9584 	if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9585 		/*
9586 		 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
9587 		 * each scan plan already specifies its own interval
9588 		 */
9589 		if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9590 			return ERR_PTR(-EINVAL);
9591 
9592 		nla_for_each_nested(attr,
9593 				    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
9594 			n_plans++;
9595 	} else {
9596 		/*
9597 		 * The scan interval attribute is kept for backward
9598 		 * compatibility. If no scan plans are specified and sched scan
9599 		 * interval is specified, one scan plan will be set with this
9600 		 * scan interval and infinite number of iterations.
9601 		 */
9602 		if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9603 			return ERR_PTR(-EINVAL);
9604 
9605 		n_plans = 1;
9606 	}
9607 
9608 	if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
9609 		return ERR_PTR(-EINVAL);
9610 
9611 	if (!wiphy_ext_feature_isset(
9612 		    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
9613 	    (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
9614 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
9615 		return ERR_PTR(-EINVAL);
9616 
9617 	size = struct_size(request, channels, n_channels);
9618 	size = size_add(size, array_size(sizeof(*request->ssids), n_ssids));
9619 	size = size_add(size, array_size(sizeof(*request->match_sets),
9620 					 n_match_sets));
9621 	size = size_add(size, array_size(sizeof(*request->scan_plans),
9622 					 n_plans));
9623 	size = size_add(size, ie_len);
9624 	request = kzalloc(size, GFP_KERNEL);
9625 	if (!request)
9626 		return ERR_PTR(-ENOMEM);
9627 
9628 	if (n_ssids)
9629 		request->ssids = (void *)&request->channels[n_channels];
9630 	request->n_ssids = n_ssids;
9631 	if (ie_len) {
9632 		if (n_ssids)
9633 			request->ie = (void *)(request->ssids + n_ssids);
9634 		else
9635 			request->ie = (void *)(request->channels + n_channels);
9636 	}
9637 
9638 	if (n_match_sets) {
9639 		if (request->ie)
9640 			request->match_sets = (void *)(request->ie + ie_len);
9641 		else if (n_ssids)
9642 			request->match_sets =
9643 				(void *)(request->ssids + n_ssids);
9644 		else
9645 			request->match_sets =
9646 				(void *)(request->channels + n_channels);
9647 	}
9648 	request->n_match_sets = n_match_sets;
9649 
9650 	if (n_match_sets)
9651 		request->scan_plans = (void *)(request->match_sets +
9652 					       n_match_sets);
9653 	else if (request->ie)
9654 		request->scan_plans = (void *)(request->ie + ie_len);
9655 	else if (n_ssids)
9656 		request->scan_plans = (void *)(request->ssids + n_ssids);
9657 	else
9658 		request->scan_plans = (void *)(request->channels + n_channels);
9659 
9660 	request->n_scan_plans = n_plans;
9661 
9662 	i = 0;
9663 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9664 		/* user specified, bail out if channel not found */
9665 		nla_for_each_nested(attr,
9666 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
9667 				    tmp) {
9668 			struct ieee80211_channel *chan;
9669 
9670 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
9671 
9672 			if (!chan) {
9673 				err = -EINVAL;
9674 				goto out_free;
9675 			}
9676 
9677 			/* ignore disabled channels */
9678 			if (chan->flags & IEEE80211_CHAN_DISABLED)
9679 				continue;
9680 
9681 			request->channels[i] = chan;
9682 			i++;
9683 		}
9684 	} else {
9685 		/* all channels */
9686 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
9687 			int j;
9688 
9689 			if (!wiphy->bands[band])
9690 				continue;
9691 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9692 				struct ieee80211_channel *chan;
9693 
9694 				chan = &wiphy->bands[band]->channels[j];
9695 
9696 				if (chan->flags & IEEE80211_CHAN_DISABLED)
9697 					continue;
9698 
9699 				request->channels[i] = chan;
9700 				i++;
9701 			}
9702 		}
9703 	}
9704 
9705 	if (!i) {
9706 		err = -EINVAL;
9707 		goto out_free;
9708 	}
9709 
9710 	request->n_channels = i;
9711 
9712 	i = 0;
9713 	if (n_ssids) {
9714 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9715 				    tmp) {
9716 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9717 				err = -EINVAL;
9718 				goto out_free;
9719 			}
9720 			request->ssids[i].ssid_len = nla_len(attr);
9721 			memcpy(request->ssids[i].ssid, nla_data(attr),
9722 			       nla_len(attr));
9723 			i++;
9724 		}
9725 	}
9726 
9727 	i = 0;
9728 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9729 		nla_for_each_nested(attr,
9730 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9731 				    tmp) {
9732 			struct nlattr *ssid, *bssid, *rssi;
9733 
9734 			err = nla_parse_nested_deprecated(tb,
9735 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9736 							  attr,
9737 							  nl80211_match_policy,
9738 							  NULL);
9739 			if (err)
9740 				goto out_free;
9741 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
9742 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
9743 
9744 			if (!ssid && !bssid) {
9745 				i++;
9746 				continue;
9747 			}
9748 
9749 			if (WARN_ON(i >= n_match_sets)) {
9750 				/* this indicates a programming error,
9751 				 * the loop above should have verified
9752 				 * things properly
9753 				 */
9754 				err = -EINVAL;
9755 				goto out_free;
9756 			}
9757 
9758 			if (ssid) {
9759 				memcpy(request->match_sets[i].ssid.ssid,
9760 				       nla_data(ssid), nla_len(ssid));
9761 				request->match_sets[i].ssid.ssid_len =
9762 					nla_len(ssid);
9763 			}
9764 			if (bssid)
9765 				memcpy(request->match_sets[i].bssid,
9766 				       nla_data(bssid), ETH_ALEN);
9767 
9768 			/* special attribute - old implementation w/a */
9769 			request->match_sets[i].rssi_thold = default_match_rssi;
9770 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9771 			if (rssi)
9772 				request->match_sets[i].rssi_thold =
9773 					nla_get_s32(rssi);
9774 
9775 			/* Parse per band RSSI attribute */
9776 			err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
9777 				&request->match_sets[i],
9778 				tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
9779 				request->match_sets[i].rssi_thold);
9780 			if (err)
9781 				goto out_free;
9782 
9783 			i++;
9784 		}
9785 
9786 		/* there was no other matchset, so the RSSI one is alone */
9787 		if (i == 0 && n_match_sets)
9788 			request->match_sets[0].rssi_thold = default_match_rssi;
9789 
9790 		request->min_rssi_thold = INT_MAX;
9791 		for (i = 0; i < n_match_sets; i++)
9792 			request->min_rssi_thold =
9793 				min(request->match_sets[i].rssi_thold,
9794 				    request->min_rssi_thold);
9795 	} else {
9796 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
9797 	}
9798 
9799 	if (ie_len) {
9800 		request->ie_len = ie_len;
9801 		memcpy((void *)request->ie,
9802 		       nla_data(attrs[NL80211_ATTR_IE]),
9803 		       request->ie_len);
9804 	}
9805 
9806 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
9807 	if (err)
9808 		goto out_free;
9809 
9810 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
9811 		request->delay =
9812 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
9813 
9814 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
9815 		request->relative_rssi = nla_get_s8(
9816 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
9817 		request->relative_rssi_set = true;
9818 	}
9819 
9820 	if (request->relative_rssi_set &&
9821 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
9822 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
9823 
9824 		rssi_adjust = nla_data(
9825 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
9826 		request->rssi_adjust.band = rssi_adjust->band;
9827 		request->rssi_adjust.delta = rssi_adjust->delta;
9828 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
9829 			err = -EINVAL;
9830 			goto out_free;
9831 		}
9832 	}
9833 
9834 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
9835 	if (err)
9836 		goto out_free;
9837 
9838 	request->scan_start = jiffies;
9839 
9840 	return request;
9841 
9842 out_free:
9843 	kfree(request);
9844 	return ERR_PTR(err);
9845 }
9846 
9847 static int nl80211_start_sched_scan(struct sk_buff *skb,
9848 				    struct genl_info *info)
9849 {
9850 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9851 	struct net_device *dev = info->user_ptr[1];
9852 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9853 	struct cfg80211_sched_scan_request *sched_scan_req;
9854 	bool want_multi;
9855 	int err;
9856 
9857 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
9858 		return -EOPNOTSUPP;
9859 
9860 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
9861 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
9862 	if (err)
9863 		return err;
9864 
9865 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
9866 						  info->attrs,
9867 						  rdev->wiphy.max_match_sets);
9868 
9869 	err = PTR_ERR_OR_ZERO(sched_scan_req);
9870 	if (err)
9871 		goto out_err;
9872 
9873 	/* leave request id zero for legacy request
9874 	 * or if driver does not support multi-scheduled scan
9875 	 */
9876 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1)
9877 		sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
9878 
9879 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
9880 	if (err)
9881 		goto out_free;
9882 
9883 	sched_scan_req->dev = dev;
9884 	sched_scan_req->wiphy = &rdev->wiphy;
9885 
9886 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9887 		sched_scan_req->owner_nlportid = info->snd_portid;
9888 
9889 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
9890 
9891 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
9892 	return 0;
9893 
9894 out_free:
9895 	kfree(sched_scan_req);
9896 out_err:
9897 	return err;
9898 }
9899 
9900 static int nl80211_stop_sched_scan(struct sk_buff *skb,
9901 				   struct genl_info *info)
9902 {
9903 	struct cfg80211_sched_scan_request *req;
9904 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9905 	u64 cookie;
9906 
9907 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
9908 		return -EOPNOTSUPP;
9909 
9910 	if (info->attrs[NL80211_ATTR_COOKIE]) {
9911 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9912 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
9913 	}
9914 
9915 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
9916 				     struct cfg80211_sched_scan_request,
9917 				     list);
9918 	if (!req || req->reqid ||
9919 	    (req->owner_nlportid &&
9920 	     req->owner_nlportid != info->snd_portid))
9921 		return -ENOENT;
9922 
9923 	return cfg80211_stop_sched_scan_req(rdev, req, false);
9924 }
9925 
9926 static int nl80211_start_radar_detection(struct sk_buff *skb,
9927 					 struct genl_info *info)
9928 {
9929 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9930 	struct net_device *dev = info->user_ptr[1];
9931 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9932 	struct wiphy *wiphy = wdev->wiphy;
9933 	struct cfg80211_chan_def chandef;
9934 	enum nl80211_dfs_regions dfs_region;
9935 	unsigned int cac_time_ms;
9936 	int err = -EINVAL;
9937 
9938 	flush_delayed_work(&rdev->dfs_update_channels_wk);
9939 
9940 	wiphy_lock(wiphy);
9941 
9942 	dfs_region = reg_get_dfs_region(wiphy);
9943 	if (dfs_region == NL80211_DFS_UNSET)
9944 		goto unlock;
9945 
9946 	err = nl80211_parse_chandef(rdev, info, &chandef);
9947 	if (err)
9948 		goto unlock;
9949 
9950 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
9951 	if (err < 0)
9952 		goto unlock;
9953 
9954 	if (err == 0) {
9955 		err = -EINVAL;
9956 		goto unlock;
9957 	}
9958 
9959 	if (!cfg80211_chandef_dfs_usable(wiphy, &chandef)) {
9960 		err = -EINVAL;
9961 		goto unlock;
9962 	}
9963 
9964 	if (nla_get_flag(info->attrs[NL80211_ATTR_RADAR_BACKGROUND])) {
9965 		err = cfg80211_start_background_radar_detection(rdev, wdev,
9966 								&chandef);
9967 		goto unlock;
9968 	}
9969 
9970 	if (netif_carrier_ok(dev)) {
9971 		err = -EBUSY;
9972 		goto unlock;
9973 	}
9974 
9975 	if (wdev->cac_started) {
9976 		err = -EBUSY;
9977 		goto unlock;
9978 	}
9979 
9980 	/* CAC start is offloaded to HW and can't be started manually */
9981 	if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD)) {
9982 		err = -EOPNOTSUPP;
9983 		goto unlock;
9984 	}
9985 
9986 	if (!rdev->ops->start_radar_detection) {
9987 		err = -EOPNOTSUPP;
9988 		goto unlock;
9989 	}
9990 
9991 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
9992 	if (WARN_ON(!cac_time_ms))
9993 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
9994 
9995 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
9996 	if (!err) {
9997 		wdev->links[0].ap.chandef = chandef;
9998 		wdev->cac_started = true;
9999 		wdev->cac_start_time = jiffies;
10000 		wdev->cac_time_ms = cac_time_ms;
10001 	}
10002 unlock:
10003 	wiphy_unlock(wiphy);
10004 
10005 	return err;
10006 }
10007 
10008 static int nl80211_notify_radar_detection(struct sk_buff *skb,
10009 					  struct genl_info *info)
10010 {
10011 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10012 	struct net_device *dev = info->user_ptr[1];
10013 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10014 	struct wiphy *wiphy = wdev->wiphy;
10015 	struct cfg80211_chan_def chandef;
10016 	enum nl80211_dfs_regions dfs_region;
10017 	int err;
10018 
10019 	dfs_region = reg_get_dfs_region(wiphy);
10020 	if (dfs_region == NL80211_DFS_UNSET) {
10021 		GENL_SET_ERR_MSG(info,
10022 				 "DFS Region is not set. Unexpected Radar indication");
10023 		return -EINVAL;
10024 	}
10025 
10026 	err = nl80211_parse_chandef(rdev, info, &chandef);
10027 	if (err) {
10028 		GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
10029 		return err;
10030 	}
10031 
10032 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
10033 	if (err < 0) {
10034 		GENL_SET_ERR_MSG(info, "chandef is invalid");
10035 		return err;
10036 	}
10037 
10038 	if (err == 0) {
10039 		GENL_SET_ERR_MSG(info,
10040 				 "Unexpected Radar indication for chandef/iftype");
10041 		return -EINVAL;
10042 	}
10043 
10044 	/* Do not process this notification if radar is already detected
10045 	 * by kernel on this channel, and return success.
10046 	 */
10047 	if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
10048 		return 0;
10049 
10050 	cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
10051 
10052 	cfg80211_sched_dfs_chan_update(rdev);
10053 
10054 	rdev->radar_chandef = chandef;
10055 
10056 	/* Propagate this notification to other radios as well */
10057 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
10058 
10059 	return 0;
10060 }
10061 
10062 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
10063 {
10064 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10065 	unsigned int link_id = nl80211_link_id(info->attrs);
10066 	struct net_device *dev = info->user_ptr[1];
10067 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10068 	struct cfg80211_csa_settings params;
10069 	struct nlattr **csa_attrs = NULL;
10070 	int err;
10071 	bool need_new_beacon = false;
10072 	bool need_handle_dfs_flag = true;
10073 	int len, i;
10074 	u32 cs_count;
10075 
10076 	if (!rdev->ops->channel_switch ||
10077 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
10078 		return -EOPNOTSUPP;
10079 
10080 	switch (dev->ieee80211_ptr->iftype) {
10081 	case NL80211_IFTYPE_AP:
10082 	case NL80211_IFTYPE_P2P_GO:
10083 		need_new_beacon = true;
10084 		/* For all modes except AP the handle_dfs flag needs to be
10085 		 * supplied to tell the kernel that userspace will handle radar
10086 		 * events when they happen. Otherwise a switch to a channel
10087 		 * requiring DFS will be rejected.
10088 		 */
10089 		need_handle_dfs_flag = false;
10090 
10091 		/* useless if AP is not running */
10092 		if (!wdev->links[link_id].ap.beacon_interval)
10093 			return -ENOTCONN;
10094 		break;
10095 	case NL80211_IFTYPE_ADHOC:
10096 		if (!wdev->u.ibss.ssid_len)
10097 			return -ENOTCONN;
10098 		break;
10099 	case NL80211_IFTYPE_MESH_POINT:
10100 		if (!wdev->u.mesh.id_len)
10101 			return -ENOTCONN;
10102 		break;
10103 	default:
10104 		return -EOPNOTSUPP;
10105 	}
10106 
10107 	memset(&params, 0, sizeof(params));
10108 	params.beacon_csa.ftm_responder = -1;
10109 
10110 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
10111 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
10112 		return -EINVAL;
10113 
10114 	/* only important for AP, IBSS and mesh create IEs internally */
10115 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
10116 		return -EINVAL;
10117 
10118 	/* Even though the attribute is u32, the specification says
10119 	 * u8, so let's make sure we don't overflow.
10120 	 */
10121 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
10122 	if (cs_count > 255)
10123 		return -EINVAL;
10124 
10125 	params.count = cs_count;
10126 
10127 	if (!need_new_beacon)
10128 		goto skip_beacons;
10129 
10130 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after,
10131 				   info->extack);
10132 	if (err)
10133 		goto free;
10134 
10135 	csa_attrs = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*csa_attrs),
10136 			    GFP_KERNEL);
10137 	if (!csa_attrs) {
10138 		err = -ENOMEM;
10139 		goto free;
10140 	}
10141 
10142 	err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
10143 					  info->attrs[NL80211_ATTR_CSA_IES],
10144 					  nl80211_policy, info->extack);
10145 	if (err)
10146 		goto free;
10147 
10148 	err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa,
10149 				   info->extack);
10150 	if (err)
10151 		goto free;
10152 
10153 	if (!csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
10154 		err = -EINVAL;
10155 		goto free;
10156 	}
10157 
10158 	len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
10159 	if (!len || (len % sizeof(u16))) {
10160 		err = -EINVAL;
10161 		goto free;
10162 	}
10163 
10164 	params.n_counter_offsets_beacon = len / sizeof(u16);
10165 	if (rdev->wiphy.max_num_csa_counters &&
10166 	    (params.n_counter_offsets_beacon >
10167 	     rdev->wiphy.max_num_csa_counters)) {
10168 		err = -EINVAL;
10169 		goto free;
10170 	}
10171 
10172 	params.counter_offsets_beacon =
10173 		nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
10174 
10175 	/* sanity checks - counters should fit and be the same */
10176 	for (i = 0; i < params.n_counter_offsets_beacon; i++) {
10177 		u16 offset = params.counter_offsets_beacon[i];
10178 
10179 		if (offset >= params.beacon_csa.tail_len) {
10180 			err = -EINVAL;
10181 			goto free;
10182 		}
10183 
10184 		if (params.beacon_csa.tail[offset] != params.count) {
10185 			err = -EINVAL;
10186 			goto free;
10187 		}
10188 	}
10189 
10190 	if (csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
10191 		len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
10192 		if (!len || (len % sizeof(u16))) {
10193 			err = -EINVAL;
10194 			goto free;
10195 		}
10196 
10197 		params.n_counter_offsets_presp = len / sizeof(u16);
10198 		if (rdev->wiphy.max_num_csa_counters &&
10199 		    (params.n_counter_offsets_presp >
10200 		     rdev->wiphy.max_num_csa_counters)) {
10201 			err = -EINVAL;
10202 			goto free;
10203 		}
10204 
10205 		params.counter_offsets_presp =
10206 			nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
10207 
10208 		/* sanity checks - counters should fit and be the same */
10209 		for (i = 0; i < params.n_counter_offsets_presp; i++) {
10210 			u16 offset = params.counter_offsets_presp[i];
10211 
10212 			if (offset >= params.beacon_csa.probe_resp_len) {
10213 				err = -EINVAL;
10214 				goto free;
10215 			}
10216 
10217 			if (params.beacon_csa.probe_resp[offset] !=
10218 			    params.count) {
10219 				err = -EINVAL;
10220 				goto free;
10221 			}
10222 		}
10223 	}
10224 
10225 skip_beacons:
10226 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
10227 	if (err)
10228 		goto free;
10229 
10230 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
10231 					   wdev->iftype)) {
10232 		err = -EINVAL;
10233 		goto free;
10234 	}
10235 
10236 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
10237 					    &params.chandef,
10238 					    wdev->iftype);
10239 	if (err < 0)
10240 		goto free;
10241 
10242 	if (err > 0) {
10243 		params.radar_required = true;
10244 		if (need_handle_dfs_flag &&
10245 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
10246 			err = -EINVAL;
10247 			goto free;
10248 		}
10249 	}
10250 
10251 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
10252 		params.block_tx = true;
10253 
10254 	if (info->attrs[NL80211_ATTR_PUNCT_BITMAP]) {
10255 		err = nl80211_parse_punct_bitmap(rdev, info,
10256 						 &params.chandef,
10257 						 &params.punct_bitmap);
10258 		if (err)
10259 			goto free;
10260 	}
10261 
10262 	err = rdev_channel_switch(rdev, dev, &params);
10263 
10264 free:
10265 	kfree(params.beacon_after.mbssid_ies);
10266 	kfree(params.beacon_csa.mbssid_ies);
10267 	kfree(params.beacon_after.rnr_ies);
10268 	kfree(params.beacon_csa.rnr_ies);
10269 	kfree(csa_attrs);
10270 	return err;
10271 }
10272 
10273 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
10274 			    u32 seq, int flags,
10275 			    struct cfg80211_registered_device *rdev,
10276 			    struct wireless_dev *wdev,
10277 			    struct cfg80211_internal_bss *intbss)
10278 {
10279 	struct cfg80211_bss *res = &intbss->pub;
10280 	const struct cfg80211_bss_ies *ies;
10281 	unsigned int link_id;
10282 	void *hdr;
10283 	struct nlattr *bss;
10284 
10285 	lockdep_assert_wiphy(wdev->wiphy);
10286 
10287 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
10288 			     NL80211_CMD_NEW_SCAN_RESULTS);
10289 	if (!hdr)
10290 		return -1;
10291 
10292 	genl_dump_check_consistent(cb, hdr);
10293 
10294 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
10295 		goto nla_put_failure;
10296 	if (wdev->netdev &&
10297 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
10298 		goto nla_put_failure;
10299 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
10300 			      NL80211_ATTR_PAD))
10301 		goto nla_put_failure;
10302 
10303 	bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
10304 	if (!bss)
10305 		goto nla_put_failure;
10306 	if ((!is_zero_ether_addr(res->bssid) &&
10307 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
10308 		goto nla_put_failure;
10309 
10310 	rcu_read_lock();
10311 	/* indicate whether we have probe response data or not */
10312 	if (rcu_access_pointer(res->proberesp_ies) &&
10313 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
10314 		goto fail_unlock_rcu;
10315 
10316 	/* this pointer prefers to be pointed to probe response data
10317 	 * but is always valid
10318 	 */
10319 	ies = rcu_dereference(res->ies);
10320 	if (ies) {
10321 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
10322 				      NL80211_BSS_PAD))
10323 			goto fail_unlock_rcu;
10324 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
10325 					ies->len, ies->data))
10326 			goto fail_unlock_rcu;
10327 	}
10328 
10329 	/* and this pointer is always (unless driver didn't know) beacon data */
10330 	ies = rcu_dereference(res->beacon_ies);
10331 	if (ies && ies->from_beacon) {
10332 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
10333 				      NL80211_BSS_PAD))
10334 			goto fail_unlock_rcu;
10335 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
10336 					ies->len, ies->data))
10337 			goto fail_unlock_rcu;
10338 	}
10339 	rcu_read_unlock();
10340 
10341 	if (res->beacon_interval &&
10342 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
10343 		goto nla_put_failure;
10344 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
10345 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
10346 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY_OFFSET,
10347 			res->channel->freq_offset) ||
10348 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
10349 			jiffies_to_msecs(jiffies - intbss->ts)))
10350 		goto nla_put_failure;
10351 
10352 	if (intbss->parent_tsf &&
10353 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
10354 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
10355 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
10356 		     intbss->parent_bssid)))
10357 		goto nla_put_failure;
10358 
10359 	if (intbss->ts_boottime &&
10360 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
10361 			      intbss->ts_boottime, NL80211_BSS_PAD))
10362 		goto nla_put_failure;
10363 
10364 	if (!nl80211_put_signal(msg, intbss->pub.chains,
10365 				intbss->pub.chain_signal,
10366 				NL80211_BSS_CHAIN_SIGNAL))
10367 		goto nla_put_failure;
10368 
10369 	switch (rdev->wiphy.signal_type) {
10370 	case CFG80211_SIGNAL_TYPE_MBM:
10371 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
10372 			goto nla_put_failure;
10373 		break;
10374 	case CFG80211_SIGNAL_TYPE_UNSPEC:
10375 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
10376 			goto nla_put_failure;
10377 		break;
10378 	default:
10379 		break;
10380 	}
10381 
10382 	switch (wdev->iftype) {
10383 	case NL80211_IFTYPE_P2P_CLIENT:
10384 	case NL80211_IFTYPE_STATION:
10385 		for_each_valid_link(wdev, link_id) {
10386 			if (intbss == wdev->links[link_id].client.current_bss &&
10387 			    (nla_put_u32(msg, NL80211_BSS_STATUS,
10388 					 NL80211_BSS_STATUS_ASSOCIATED) ||
10389 			     (wdev->valid_links &&
10390 			      (nla_put_u8(msg, NL80211_BSS_MLO_LINK_ID,
10391 					  link_id) ||
10392 			       nla_put(msg, NL80211_BSS_MLD_ADDR, ETH_ALEN,
10393 				       wdev->u.client.connected_addr)))))
10394 				goto nla_put_failure;
10395 		}
10396 		break;
10397 	case NL80211_IFTYPE_ADHOC:
10398 		if (intbss == wdev->u.ibss.current_bss &&
10399 		    nla_put_u32(msg, NL80211_BSS_STATUS,
10400 				NL80211_BSS_STATUS_IBSS_JOINED))
10401 			goto nla_put_failure;
10402 		break;
10403 	default:
10404 		break;
10405 	}
10406 
10407 	nla_nest_end(msg, bss);
10408 
10409 	genlmsg_end(msg, hdr);
10410 	return 0;
10411 
10412  fail_unlock_rcu:
10413 	rcu_read_unlock();
10414  nla_put_failure:
10415 	genlmsg_cancel(msg, hdr);
10416 	return -EMSGSIZE;
10417 }
10418 
10419 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
10420 {
10421 	struct cfg80211_registered_device *rdev;
10422 	struct cfg80211_internal_bss *scan;
10423 	struct wireless_dev *wdev;
10424 	int start = cb->args[2], idx = 0;
10425 	int err;
10426 
10427 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
10428 	if (err)
10429 		return err;
10430 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10431 	__acquire(&rdev->wiphy.mtx);
10432 
10433 	spin_lock_bh(&rdev->bss_lock);
10434 
10435 	/*
10436 	 * dump_scan will be called multiple times to break up the scan results
10437 	 * into multiple messages.  It is unlikely that any more bss-es will be
10438 	 * expired after the first call, so only call only call this on the
10439 	 * first dump_scan invocation.
10440 	 */
10441 	if (start == 0)
10442 		cfg80211_bss_expire(rdev);
10443 
10444 	cb->seq = rdev->bss_generation;
10445 
10446 	list_for_each_entry(scan, &rdev->bss_list, list) {
10447 		if (++idx <= start)
10448 			continue;
10449 		if (nl80211_send_bss(skb, cb,
10450 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10451 				rdev, wdev, scan) < 0) {
10452 			idx--;
10453 			break;
10454 		}
10455 	}
10456 
10457 	spin_unlock_bh(&rdev->bss_lock);
10458 
10459 	cb->args[2] = idx;
10460 	wiphy_unlock(&rdev->wiphy);
10461 
10462 	return skb->len;
10463 }
10464 
10465 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
10466 			       int flags, struct net_device *dev,
10467 			       bool allow_radio_stats,
10468 			       struct survey_info *survey)
10469 {
10470 	void *hdr;
10471 	struct nlattr *infoattr;
10472 
10473 	/* skip radio stats if userspace didn't request them */
10474 	if (!survey->channel && !allow_radio_stats)
10475 		return 0;
10476 
10477 	hdr = nl80211hdr_put(msg, portid, seq, flags,
10478 			     NL80211_CMD_NEW_SURVEY_RESULTS);
10479 	if (!hdr)
10480 		return -ENOMEM;
10481 
10482 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
10483 		goto nla_put_failure;
10484 
10485 	infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
10486 	if (!infoattr)
10487 		goto nla_put_failure;
10488 
10489 	if (survey->channel &&
10490 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
10491 			survey->channel->center_freq))
10492 		goto nla_put_failure;
10493 
10494 	if (survey->channel && survey->channel->freq_offset &&
10495 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY_OFFSET,
10496 			survey->channel->freq_offset))
10497 		goto nla_put_failure;
10498 
10499 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
10500 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
10501 		goto nla_put_failure;
10502 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
10503 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
10504 		goto nla_put_failure;
10505 	if ((survey->filled & SURVEY_INFO_TIME) &&
10506 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
10507 			survey->time, NL80211_SURVEY_INFO_PAD))
10508 		goto nla_put_failure;
10509 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
10510 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
10511 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
10512 		goto nla_put_failure;
10513 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
10514 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
10515 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
10516 		goto nla_put_failure;
10517 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
10518 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
10519 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
10520 		goto nla_put_failure;
10521 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
10522 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
10523 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
10524 		goto nla_put_failure;
10525 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
10526 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
10527 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
10528 		goto nla_put_failure;
10529 	if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
10530 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
10531 			      survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
10532 		goto nla_put_failure;
10533 
10534 	nla_nest_end(msg, infoattr);
10535 
10536 	genlmsg_end(msg, hdr);
10537 	return 0;
10538 
10539  nla_put_failure:
10540 	genlmsg_cancel(msg, hdr);
10541 	return -EMSGSIZE;
10542 }
10543 
10544 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
10545 {
10546 	struct nlattr **attrbuf;
10547 	struct survey_info survey;
10548 	struct cfg80211_registered_device *rdev;
10549 	struct wireless_dev *wdev;
10550 	int survey_idx = cb->args[2];
10551 	int res;
10552 	bool radio_stats;
10553 
10554 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
10555 	if (!attrbuf)
10556 		return -ENOMEM;
10557 
10558 	res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, attrbuf);
10559 	if (res) {
10560 		kfree(attrbuf);
10561 		return res;
10562 	}
10563 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10564 	__acquire(&rdev->wiphy.mtx);
10565 
10566 	/* prepare_wdev_dump parsed the attributes */
10567 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
10568 
10569 	if (!wdev->netdev) {
10570 		res = -EINVAL;
10571 		goto out_err;
10572 	}
10573 
10574 	if (!rdev->ops->dump_survey) {
10575 		res = -EOPNOTSUPP;
10576 		goto out_err;
10577 	}
10578 
10579 	while (1) {
10580 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
10581 		if (res == -ENOENT)
10582 			break;
10583 		if (res)
10584 			goto out_err;
10585 
10586 		/* don't send disabled channels, but do send non-channel data */
10587 		if (survey.channel &&
10588 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
10589 			survey_idx++;
10590 			continue;
10591 		}
10592 
10593 		if (nl80211_send_survey(skb,
10594 				NETLINK_CB(cb->skb).portid,
10595 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10596 				wdev->netdev, radio_stats, &survey) < 0)
10597 			goto out;
10598 		survey_idx++;
10599 	}
10600 
10601  out:
10602 	cb->args[2] = survey_idx;
10603 	res = skb->len;
10604  out_err:
10605 	kfree(attrbuf);
10606 	wiphy_unlock(&rdev->wiphy);
10607 	return res;
10608 }
10609 
10610 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
10611 {
10612 	return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
10613 				  NL80211_WPA_VERSION_2 |
10614 				  NL80211_WPA_VERSION_3));
10615 }
10616 
10617 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
10618 {
10619 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10620 	struct net_device *dev = info->user_ptr[1];
10621 	struct ieee80211_channel *chan;
10622 	const u8 *bssid, *ssid;
10623 	int err, ssid_len;
10624 	enum nl80211_auth_type auth_type;
10625 	struct key_parse key;
10626 	bool local_state_change;
10627 	struct cfg80211_auth_request req = {};
10628 	u32 freq;
10629 
10630 	if (!info->attrs[NL80211_ATTR_MAC])
10631 		return -EINVAL;
10632 
10633 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
10634 		return -EINVAL;
10635 
10636 	if (!info->attrs[NL80211_ATTR_SSID])
10637 		return -EINVAL;
10638 
10639 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10640 		return -EINVAL;
10641 
10642 	err = nl80211_parse_key(info, &key);
10643 	if (err)
10644 		return err;
10645 
10646 	if (key.idx >= 0) {
10647 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
10648 			return -EINVAL;
10649 		if (!key.p.key || !key.p.key_len)
10650 			return -EINVAL;
10651 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
10652 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
10653 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
10654 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
10655 			return -EINVAL;
10656 		if (key.idx > 3)
10657 			return -EINVAL;
10658 	} else {
10659 		key.p.key_len = 0;
10660 		key.p.key = NULL;
10661 	}
10662 
10663 	if (key.idx >= 0) {
10664 		int i;
10665 		bool ok = false;
10666 
10667 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
10668 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
10669 				ok = true;
10670 				break;
10671 			}
10672 		}
10673 		if (!ok)
10674 			return -EINVAL;
10675 	}
10676 
10677 	if (!rdev->ops->auth)
10678 		return -EOPNOTSUPP;
10679 
10680 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10681 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10682 		return -EOPNOTSUPP;
10683 
10684 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10685 	freq = MHZ_TO_KHZ(nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
10686 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10687 		freq +=
10688 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10689 
10690 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10691 	if (!chan)
10692 		return -EINVAL;
10693 
10694 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10695 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10696 
10697 	if (info->attrs[NL80211_ATTR_IE]) {
10698 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10699 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10700 	}
10701 
10702 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10703 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
10704 		return -EINVAL;
10705 
10706 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
10707 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
10708 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
10709 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
10710 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
10711 		return -EINVAL;
10712 
10713 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
10714 		if (auth_type != NL80211_AUTHTYPE_SAE &&
10715 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
10716 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
10717 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
10718 			return -EINVAL;
10719 		req.auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
10720 		req.auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
10721 	}
10722 
10723 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
10724 
10725 	/*
10726 	 * Since we no longer track auth state, ignore
10727 	 * requests to only change local state.
10728 	 */
10729 	if (local_state_change)
10730 		return 0;
10731 
10732 	req.auth_type = auth_type;
10733 	req.key = key.p.key;
10734 	req.key_len = key.p.key_len;
10735 	req.key_idx = key.idx;
10736 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
10737 	if (req.link_id >= 0) {
10738 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
10739 			return -EINVAL;
10740 		if (!info->attrs[NL80211_ATTR_MLD_ADDR])
10741 			return -EINVAL;
10742 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
10743 		if (!is_valid_ether_addr(req.ap_mld_addr))
10744 			return -EINVAL;
10745 	}
10746 
10747 	req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
10748 				   IEEE80211_BSS_TYPE_ESS,
10749 				   IEEE80211_PRIVACY_ANY);
10750 	if (!req.bss)
10751 		return -ENOENT;
10752 
10753 	err = cfg80211_mlme_auth(rdev, dev, &req);
10754 
10755 	cfg80211_put_bss(&rdev->wiphy, req.bss);
10756 
10757 	return err;
10758 }
10759 
10760 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
10761 				     struct genl_info *info)
10762 {
10763 	if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10764 		GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
10765 		return -EINVAL;
10766 	}
10767 
10768 	if (!rdev->ops->tx_control_port ||
10769 	    !wiphy_ext_feature_isset(&rdev->wiphy,
10770 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
10771 		return -EOPNOTSUPP;
10772 
10773 	return 0;
10774 }
10775 
10776 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
10777 				   struct genl_info *info,
10778 				   struct cfg80211_crypto_settings *settings,
10779 				   int cipher_limit)
10780 {
10781 	memset(settings, 0, sizeof(*settings));
10782 
10783 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
10784 
10785 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
10786 		u16 proto;
10787 
10788 		proto = nla_get_u16(
10789 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
10790 		settings->control_port_ethertype = cpu_to_be16(proto);
10791 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
10792 		    proto != ETH_P_PAE)
10793 			return -EINVAL;
10794 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
10795 			settings->control_port_no_encrypt = true;
10796 	} else
10797 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
10798 
10799 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
10800 		int r = validate_pae_over_nl80211(rdev, info);
10801 
10802 		if (r < 0)
10803 			return r;
10804 
10805 		settings->control_port_over_nl80211 = true;
10806 
10807 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH])
10808 			settings->control_port_no_preauth = true;
10809 	}
10810 
10811 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
10812 		void *data;
10813 		int len, i;
10814 
10815 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10816 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10817 		settings->n_ciphers_pairwise = len / sizeof(u32);
10818 
10819 		if (len % sizeof(u32))
10820 			return -EINVAL;
10821 
10822 		if (settings->n_ciphers_pairwise > cipher_limit)
10823 			return -EINVAL;
10824 
10825 		memcpy(settings->ciphers_pairwise, data, len);
10826 
10827 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
10828 			if (!cfg80211_supported_cipher_suite(
10829 					&rdev->wiphy,
10830 					settings->ciphers_pairwise[i]))
10831 				return -EINVAL;
10832 	}
10833 
10834 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
10835 		settings->cipher_group =
10836 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
10837 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
10838 						     settings->cipher_group))
10839 			return -EINVAL;
10840 	}
10841 
10842 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
10843 		settings->wpa_versions =
10844 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
10845 		if (!nl80211_valid_wpa_versions(settings->wpa_versions))
10846 			return -EINVAL;
10847 	}
10848 
10849 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
10850 		void *data;
10851 		int len;
10852 
10853 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
10854 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
10855 		settings->n_akm_suites = len / sizeof(u32);
10856 
10857 		if (len % sizeof(u32))
10858 			return -EINVAL;
10859 
10860 		if (settings->n_akm_suites > rdev->wiphy.max_num_akm_suites)
10861 			return -EINVAL;
10862 
10863 		memcpy(settings->akm_suites, data, len);
10864 	}
10865 
10866 	if (info->attrs[NL80211_ATTR_PMK]) {
10867 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
10868 			return -EINVAL;
10869 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10870 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK) &&
10871 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10872 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_AP_PSK))
10873 			return -EINVAL;
10874 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10875 	}
10876 
10877 	if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
10878 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10879 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
10880 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10881 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP))
10882 			return -EINVAL;
10883 		settings->sae_pwd =
10884 			nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10885 		settings->sae_pwd_len =
10886 			nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10887 	}
10888 
10889 	if (info->attrs[NL80211_ATTR_SAE_PWE])
10890 		settings->sae_pwe =
10891 			nla_get_u8(info->attrs[NL80211_ATTR_SAE_PWE]);
10892 	else
10893 		settings->sae_pwe = NL80211_SAE_PWE_UNSPECIFIED;
10894 
10895 	return 0;
10896 }
10897 
10898 static struct cfg80211_bss *nl80211_assoc_bss(struct cfg80211_registered_device *rdev,
10899 					      const u8 *ssid, int ssid_len,
10900 					      struct nlattr **attrs)
10901 {
10902 	struct ieee80211_channel *chan;
10903 	struct cfg80211_bss *bss;
10904 	const u8 *bssid;
10905 	u32 freq;
10906 
10907 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_WIPHY_FREQ])
10908 		return ERR_PTR(-EINVAL);
10909 
10910 	bssid = nla_data(attrs[NL80211_ATTR_MAC]);
10911 
10912 	freq = MHZ_TO_KHZ(nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ]));
10913 	if (attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10914 		freq += nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10915 
10916 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10917 	if (!chan)
10918 		return ERR_PTR(-EINVAL);
10919 
10920 	bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid,
10921 			       ssid, ssid_len,
10922 			       IEEE80211_BSS_TYPE_ESS,
10923 			       IEEE80211_PRIVACY_ANY);
10924 	if (!bss)
10925 		return ERR_PTR(-ENOENT);
10926 
10927 	return bss;
10928 }
10929 
10930 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
10931 {
10932 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10933 	struct net_device *dev = info->user_ptr[1];
10934 	struct cfg80211_assoc_request req = {};
10935 	struct nlattr **attrs = NULL;
10936 	const u8 *ap_addr, *ssid;
10937 	unsigned int link_id;
10938 	int err, ssid_len;
10939 
10940 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
10941 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10942 		return -EPERM;
10943 
10944 	if (!info->attrs[NL80211_ATTR_SSID])
10945 		return -EINVAL;
10946 
10947 	if (!rdev->ops->assoc)
10948 		return -EOPNOTSUPP;
10949 
10950 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10951 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10952 		return -EOPNOTSUPP;
10953 
10954 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10955 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10956 
10957 	if (info->attrs[NL80211_ATTR_IE]) {
10958 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10959 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10960 
10961 		if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
10962 					   req.ie, req.ie_len)) {
10963 			NL_SET_ERR_MSG_ATTR(info->extack,
10964 					    info->attrs[NL80211_ATTR_IE],
10965 					    "non-inheritance makes no sense");
10966 			return -EINVAL;
10967 		}
10968 	}
10969 
10970 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
10971 		enum nl80211_mfp mfp =
10972 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
10973 		if (mfp == NL80211_MFP_REQUIRED)
10974 			req.use_mfp = true;
10975 		else if (mfp != NL80211_MFP_NO)
10976 			return -EINVAL;
10977 	}
10978 
10979 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
10980 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
10981 
10982 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
10983 		req.flags |= ASSOC_REQ_DISABLE_HT;
10984 
10985 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10986 		memcpy(&req.ht_capa_mask,
10987 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
10988 		       sizeof(req.ht_capa_mask));
10989 
10990 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
10991 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10992 			return -EINVAL;
10993 		memcpy(&req.ht_capa,
10994 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
10995 		       sizeof(req.ht_capa));
10996 	}
10997 
10998 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
10999 		req.flags |= ASSOC_REQ_DISABLE_VHT;
11000 
11001 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
11002 		req.flags |= ASSOC_REQ_DISABLE_HE;
11003 
11004 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
11005 		req.flags |= ASSOC_REQ_DISABLE_EHT;
11006 
11007 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11008 		memcpy(&req.vht_capa_mask,
11009 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
11010 		       sizeof(req.vht_capa_mask));
11011 
11012 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
11013 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11014 			return -EINVAL;
11015 		memcpy(&req.vht_capa,
11016 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
11017 		       sizeof(req.vht_capa));
11018 	}
11019 
11020 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
11021 		if (!((rdev->wiphy.features &
11022 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
11023 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
11024 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11025 					     NL80211_EXT_FEATURE_RRM))
11026 			return -EINVAL;
11027 		req.flags |= ASSOC_REQ_USE_RRM;
11028 	}
11029 
11030 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
11031 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
11032 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
11033 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
11034 			return -EINVAL;
11035 		req.fils_nonces =
11036 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
11037 	}
11038 
11039 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]) {
11040 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY])
11041 			return -EINVAL;
11042 		memcpy(&req.s1g_capa_mask,
11043 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]),
11044 		       sizeof(req.s1g_capa_mask));
11045 	}
11046 
11047 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY]) {
11048 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK])
11049 			return -EINVAL;
11050 		memcpy(&req.s1g_capa,
11051 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY]),
11052 		       sizeof(req.s1g_capa));
11053 	}
11054 
11055 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
11056 
11057 	if (info->attrs[NL80211_ATTR_MLO_LINKS]) {
11058 		unsigned int attrsize = NUM_NL80211_ATTR * sizeof(*attrs);
11059 		struct nlattr *link;
11060 		int rem = 0;
11061 
11062 		if (req.link_id < 0)
11063 			return -EINVAL;
11064 
11065 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
11066 			return -EINVAL;
11067 
11068 		if (info->attrs[NL80211_ATTR_MAC] ||
11069 		    info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
11070 		    !info->attrs[NL80211_ATTR_MLD_ADDR])
11071 			return -EINVAL;
11072 
11073 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
11074 		ap_addr = req.ap_mld_addr;
11075 
11076 		attrs = kzalloc(attrsize, GFP_KERNEL);
11077 		if (!attrs)
11078 			return -ENOMEM;
11079 
11080 		nla_for_each_nested(link,
11081 				    info->attrs[NL80211_ATTR_MLO_LINKS],
11082 				    rem) {
11083 			memset(attrs, 0, attrsize);
11084 
11085 			nla_parse_nested(attrs, NL80211_ATTR_MAX,
11086 					 link, NULL, NULL);
11087 
11088 			if (!attrs[NL80211_ATTR_MLO_LINK_ID]) {
11089 				err = -EINVAL;
11090 				NL_SET_BAD_ATTR(info->extack, link);
11091 				goto free;
11092 			}
11093 
11094 			link_id = nla_get_u8(attrs[NL80211_ATTR_MLO_LINK_ID]);
11095 			/* cannot use the same link ID again */
11096 			if (req.links[link_id].bss) {
11097 				err = -EINVAL;
11098 				NL_SET_BAD_ATTR(info->extack, link);
11099 				goto free;
11100 			}
11101 			req.links[link_id].bss =
11102 				nl80211_assoc_bss(rdev, ssid, ssid_len, attrs);
11103 			if (IS_ERR(req.links[link_id].bss)) {
11104 				err = PTR_ERR(req.links[link_id].bss);
11105 				req.links[link_id].bss = NULL;
11106 				NL_SET_ERR_MSG_ATTR(info->extack,
11107 						    link, "Error fetching BSS for link");
11108 				goto free;
11109 			}
11110 
11111 			if (attrs[NL80211_ATTR_IE]) {
11112 				req.links[link_id].elems =
11113 					nla_data(attrs[NL80211_ATTR_IE]);
11114 				req.links[link_id].elems_len =
11115 					nla_len(attrs[NL80211_ATTR_IE]);
11116 
11117 				if (cfg80211_find_elem(WLAN_EID_FRAGMENT,
11118 						       req.links[link_id].elems,
11119 						       req.links[link_id].elems_len)) {
11120 					NL_SET_ERR_MSG_ATTR(info->extack,
11121 							    attrs[NL80211_ATTR_IE],
11122 							    "cannot deal with fragmentation");
11123 					err = -EINVAL;
11124 					goto free;
11125 				}
11126 
11127 				if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
11128 							   req.links[link_id].elems,
11129 							   req.links[link_id].elems_len)) {
11130 					NL_SET_ERR_MSG_ATTR(info->extack,
11131 							    attrs[NL80211_ATTR_IE],
11132 							    "cannot deal with non-inheritance");
11133 					err = -EINVAL;
11134 					goto free;
11135 				}
11136 			}
11137 
11138 			req.links[link_id].disabled =
11139 				nla_get_flag(attrs[NL80211_ATTR_MLO_LINK_DISABLED]);
11140 		}
11141 
11142 		if (!req.links[req.link_id].bss) {
11143 			err = -EINVAL;
11144 			goto free;
11145 		}
11146 
11147 		if (req.links[req.link_id].elems_len) {
11148 			GENL_SET_ERR_MSG(info,
11149 					 "cannot have per-link elems on assoc link");
11150 			err = -EINVAL;
11151 			goto free;
11152 		}
11153 
11154 		if (req.links[req.link_id].disabled) {
11155 			GENL_SET_ERR_MSG(info,
11156 					 "cannot have assoc link disabled");
11157 			err = -EINVAL;
11158 			goto free;
11159 		}
11160 
11161 		kfree(attrs);
11162 		attrs = NULL;
11163 	} else {
11164 		if (req.link_id >= 0)
11165 			return -EINVAL;
11166 
11167 		req.bss = nl80211_assoc_bss(rdev, ssid, ssid_len, info->attrs);
11168 		if (IS_ERR(req.bss))
11169 			return PTR_ERR(req.bss);
11170 		ap_addr = req.bss->bssid;
11171 	}
11172 
11173 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
11174 	if (!err) {
11175 		struct nlattr *link;
11176 		int rem = 0;
11177 
11178 		err = cfg80211_mlme_assoc(rdev, dev, &req);
11179 
11180 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
11181 			dev->ieee80211_ptr->conn_owner_nlportid =
11182 				info->snd_portid;
11183 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
11184 			       ap_addr, ETH_ALEN);
11185 		}
11186 
11187 		/* Report error from first problematic link */
11188 		if (info->attrs[NL80211_ATTR_MLO_LINKS]) {
11189 			nla_for_each_nested(link,
11190 					    info->attrs[NL80211_ATTR_MLO_LINKS],
11191 					    rem) {
11192 				struct nlattr *link_id_attr =
11193 					nla_find_nested(link, NL80211_ATTR_MLO_LINK_ID);
11194 
11195 				if (!link_id_attr)
11196 					continue;
11197 
11198 				link_id = nla_get_u8(link_id_attr);
11199 
11200 				if (link_id == req.link_id)
11201 					continue;
11202 
11203 				if (!req.links[link_id].error ||
11204 				    WARN_ON(req.links[link_id].error > 0))
11205 					continue;
11206 
11207 				WARN_ON(err >= 0);
11208 
11209 				NL_SET_BAD_ATTR(info->extack, link);
11210 				err = req.links[link_id].error;
11211 				break;
11212 			}
11213 		}
11214 	}
11215 
11216 free:
11217 	for (link_id = 0; link_id < ARRAY_SIZE(req.links); link_id++)
11218 		cfg80211_put_bss(&rdev->wiphy, req.links[link_id].bss);
11219 	cfg80211_put_bss(&rdev->wiphy, req.bss);
11220 	kfree(attrs);
11221 
11222 	return err;
11223 }
11224 
11225 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
11226 {
11227 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11228 	struct net_device *dev = info->user_ptr[1];
11229 	const u8 *ie = NULL, *bssid;
11230 	int ie_len = 0;
11231 	u16 reason_code;
11232 	bool local_state_change;
11233 
11234 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11235 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11236 		return -EPERM;
11237 
11238 	if (!info->attrs[NL80211_ATTR_MAC])
11239 		return -EINVAL;
11240 
11241 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11242 		return -EINVAL;
11243 
11244 	if (!rdev->ops->deauth)
11245 		return -EOPNOTSUPP;
11246 
11247 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11248 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11249 		return -EOPNOTSUPP;
11250 
11251 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11252 
11253 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11254 	if (reason_code == 0) {
11255 		/* Reason Code 0 is reserved */
11256 		return -EINVAL;
11257 	}
11258 
11259 	if (info->attrs[NL80211_ATTR_IE]) {
11260 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11261 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11262 	}
11263 
11264 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11265 
11266 	return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
11267 				    local_state_change);
11268 }
11269 
11270 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
11271 {
11272 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11273 	struct net_device *dev = info->user_ptr[1];
11274 	const u8 *ie = NULL, *bssid;
11275 	int ie_len = 0;
11276 	u16 reason_code;
11277 	bool local_state_change;
11278 
11279 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11280 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11281 		return -EPERM;
11282 
11283 	if (!info->attrs[NL80211_ATTR_MAC])
11284 		return -EINVAL;
11285 
11286 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11287 		return -EINVAL;
11288 
11289 	if (!rdev->ops->disassoc)
11290 		return -EOPNOTSUPP;
11291 
11292 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11293 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11294 		return -EOPNOTSUPP;
11295 
11296 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11297 
11298 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11299 	if (reason_code == 0) {
11300 		/* Reason Code 0 is reserved */
11301 		return -EINVAL;
11302 	}
11303 
11304 	if (info->attrs[NL80211_ATTR_IE]) {
11305 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11306 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11307 	}
11308 
11309 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11310 
11311 	return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
11312 				      local_state_change);
11313 }
11314 
11315 static bool
11316 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
11317 			 int mcast_rate[NUM_NL80211_BANDS],
11318 			 int rateval)
11319 {
11320 	struct wiphy *wiphy = &rdev->wiphy;
11321 	bool found = false;
11322 	int band, i;
11323 
11324 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
11325 		struct ieee80211_supported_band *sband;
11326 
11327 		sband = wiphy->bands[band];
11328 		if (!sband)
11329 			continue;
11330 
11331 		for (i = 0; i < sband->n_bitrates; i++) {
11332 			if (sband->bitrates[i].bitrate == rateval) {
11333 				mcast_rate[band] = i + 1;
11334 				found = true;
11335 				break;
11336 			}
11337 		}
11338 	}
11339 
11340 	return found;
11341 }
11342 
11343 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
11344 {
11345 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11346 	struct net_device *dev = info->user_ptr[1];
11347 	struct cfg80211_ibss_params ibss;
11348 	struct wiphy *wiphy;
11349 	struct cfg80211_cached_keys *connkeys = NULL;
11350 	int err;
11351 
11352 	memset(&ibss, 0, sizeof(ibss));
11353 
11354 	if (!info->attrs[NL80211_ATTR_SSID] ||
11355 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11356 		return -EINVAL;
11357 
11358 	ibss.beacon_interval = 100;
11359 
11360 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
11361 		ibss.beacon_interval =
11362 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11363 
11364 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
11365 					   ibss.beacon_interval);
11366 	if (err)
11367 		return err;
11368 
11369 	if (!rdev->ops->join_ibss)
11370 		return -EOPNOTSUPP;
11371 
11372 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11373 		return -EOPNOTSUPP;
11374 
11375 	wiphy = &rdev->wiphy;
11376 
11377 	if (info->attrs[NL80211_ATTR_MAC]) {
11378 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11379 
11380 		if (!is_valid_ether_addr(ibss.bssid))
11381 			return -EINVAL;
11382 	}
11383 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11384 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11385 
11386 	if (info->attrs[NL80211_ATTR_IE]) {
11387 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11388 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11389 	}
11390 
11391 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
11392 	if (err)
11393 		return err;
11394 
11395 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
11396 				     NL80211_IFTYPE_ADHOC))
11397 		return -EINVAL;
11398 
11399 	switch (ibss.chandef.width) {
11400 	case NL80211_CHAN_WIDTH_5:
11401 	case NL80211_CHAN_WIDTH_10:
11402 	case NL80211_CHAN_WIDTH_20_NOHT:
11403 		break;
11404 	case NL80211_CHAN_WIDTH_20:
11405 	case NL80211_CHAN_WIDTH_40:
11406 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11407 			return -EINVAL;
11408 		break;
11409 	case NL80211_CHAN_WIDTH_80:
11410 	case NL80211_CHAN_WIDTH_80P80:
11411 	case NL80211_CHAN_WIDTH_160:
11412 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11413 			return -EINVAL;
11414 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
11415 					     NL80211_EXT_FEATURE_VHT_IBSS))
11416 			return -EINVAL;
11417 		break;
11418 	case NL80211_CHAN_WIDTH_320:
11419 		return -EINVAL;
11420 	default:
11421 		return -EINVAL;
11422 	}
11423 
11424 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
11425 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
11426 
11427 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11428 		u8 *rates =
11429 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11430 		int n_rates =
11431 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11432 		struct ieee80211_supported_band *sband =
11433 			wiphy->bands[ibss.chandef.chan->band];
11434 
11435 		err = ieee80211_get_ratemask(sband, rates, n_rates,
11436 					     &ibss.basic_rates);
11437 		if (err)
11438 			return err;
11439 	}
11440 
11441 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11442 		memcpy(&ibss.ht_capa_mask,
11443 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11444 		       sizeof(ibss.ht_capa_mask));
11445 
11446 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11447 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11448 			return -EINVAL;
11449 		memcpy(&ibss.ht_capa,
11450 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11451 		       sizeof(ibss.ht_capa));
11452 	}
11453 
11454 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11455 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
11456 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11457 		return -EINVAL;
11458 
11459 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11460 		bool no_ht = false;
11461 
11462 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
11463 		if (IS_ERR(connkeys))
11464 			return PTR_ERR(connkeys);
11465 
11466 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
11467 		    no_ht) {
11468 			kfree_sensitive(connkeys);
11469 			return -EINVAL;
11470 		}
11471 	}
11472 
11473 	ibss.control_port =
11474 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
11475 
11476 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11477 		int r = validate_pae_over_nl80211(rdev, info);
11478 
11479 		if (r < 0) {
11480 			kfree_sensitive(connkeys);
11481 			return r;
11482 		}
11483 
11484 		ibss.control_port_over_nl80211 = true;
11485 	}
11486 
11487 	ibss.userspace_handles_dfs =
11488 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11489 
11490 	err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
11491 	if (err)
11492 		kfree_sensitive(connkeys);
11493 	else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
11494 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11495 
11496 	return err;
11497 }
11498 
11499 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
11500 {
11501 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11502 	struct net_device *dev = info->user_ptr[1];
11503 
11504 	if (!rdev->ops->leave_ibss)
11505 		return -EOPNOTSUPP;
11506 
11507 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11508 		return -EOPNOTSUPP;
11509 
11510 	return cfg80211_leave_ibss(rdev, dev, false);
11511 }
11512 
11513 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
11514 {
11515 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11516 	struct net_device *dev = info->user_ptr[1];
11517 	int mcast_rate[NUM_NL80211_BANDS];
11518 	u32 nla_rate;
11519 
11520 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
11521 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
11522 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
11523 		return -EOPNOTSUPP;
11524 
11525 	if (!rdev->ops->set_mcast_rate)
11526 		return -EOPNOTSUPP;
11527 
11528 	memset(mcast_rate, 0, sizeof(mcast_rate));
11529 
11530 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
11531 		return -EINVAL;
11532 
11533 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
11534 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
11535 		return -EINVAL;
11536 
11537 	return rdev_set_mcast_rate(rdev, dev, mcast_rate);
11538 }
11539 
11540 static struct sk_buff *
11541 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
11542 			    struct wireless_dev *wdev, int approxlen,
11543 			    u32 portid, u32 seq, enum nl80211_commands cmd,
11544 			    enum nl80211_attrs attr,
11545 			    const struct nl80211_vendor_cmd_info *info,
11546 			    gfp_t gfp)
11547 {
11548 	struct sk_buff *skb;
11549 	void *hdr;
11550 	struct nlattr *data;
11551 
11552 	skb = nlmsg_new(approxlen + 100, gfp);
11553 	if (!skb)
11554 		return NULL;
11555 
11556 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
11557 	if (!hdr) {
11558 		kfree_skb(skb);
11559 		return NULL;
11560 	}
11561 
11562 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11563 		goto nla_put_failure;
11564 
11565 	if (info) {
11566 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
11567 				info->vendor_id))
11568 			goto nla_put_failure;
11569 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
11570 				info->subcmd))
11571 			goto nla_put_failure;
11572 	}
11573 
11574 	if (wdev) {
11575 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
11576 				      wdev_id(wdev), NL80211_ATTR_PAD))
11577 			goto nla_put_failure;
11578 		if (wdev->netdev &&
11579 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
11580 				wdev->netdev->ifindex))
11581 			goto nla_put_failure;
11582 	}
11583 
11584 	data = nla_nest_start_noflag(skb, attr);
11585 	if (!data)
11586 		goto nla_put_failure;
11587 
11588 	((void **)skb->cb)[0] = rdev;
11589 	((void **)skb->cb)[1] = hdr;
11590 	((void **)skb->cb)[2] = data;
11591 
11592 	return skb;
11593 
11594  nla_put_failure:
11595 	kfree_skb(skb);
11596 	return NULL;
11597 }
11598 
11599 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
11600 					   struct wireless_dev *wdev,
11601 					   enum nl80211_commands cmd,
11602 					   enum nl80211_attrs attr,
11603 					   unsigned int portid,
11604 					   int vendor_event_idx,
11605 					   int approxlen, gfp_t gfp)
11606 {
11607 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11608 	const struct nl80211_vendor_cmd_info *info;
11609 
11610 	switch (cmd) {
11611 	case NL80211_CMD_TESTMODE:
11612 		if (WARN_ON(vendor_event_idx != -1))
11613 			return NULL;
11614 		info = NULL;
11615 		break;
11616 	case NL80211_CMD_VENDOR:
11617 		if (WARN_ON(vendor_event_idx < 0 ||
11618 			    vendor_event_idx >= wiphy->n_vendor_events))
11619 			return NULL;
11620 		info = &wiphy->vendor_events[vendor_event_idx];
11621 		break;
11622 	default:
11623 		WARN_ON(1);
11624 		return NULL;
11625 	}
11626 
11627 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
11628 					   cmd, attr, info, gfp);
11629 }
11630 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
11631 
11632 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
11633 {
11634 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
11635 	void *hdr = ((void **)skb->cb)[1];
11636 	struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
11637 	struct nlattr *data = ((void **)skb->cb)[2];
11638 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
11639 
11640 	/* clear CB data for netlink core to own from now on */
11641 	memset(skb->cb, 0, sizeof(skb->cb));
11642 
11643 	nla_nest_end(skb, data);
11644 	genlmsg_end(skb, hdr);
11645 
11646 	if (nlhdr->nlmsg_pid) {
11647 		genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
11648 				nlhdr->nlmsg_pid);
11649 	} else {
11650 		if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
11651 			mcgrp = NL80211_MCGRP_VENDOR;
11652 
11653 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11654 					skb, 0, mcgrp, gfp);
11655 	}
11656 }
11657 EXPORT_SYMBOL(__cfg80211_send_event_skb);
11658 
11659 #ifdef CONFIG_NL80211_TESTMODE
11660 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
11661 {
11662 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11663 	struct wireless_dev *wdev;
11664 	int err;
11665 
11666 	lockdep_assert_held(&rdev->wiphy.mtx);
11667 
11668 	wdev = __cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
11669 					  info->attrs);
11670 
11671 	if (!rdev->ops->testmode_cmd)
11672 		return -EOPNOTSUPP;
11673 
11674 	if (IS_ERR(wdev)) {
11675 		err = PTR_ERR(wdev);
11676 		if (err != -EINVAL)
11677 			return err;
11678 		wdev = NULL;
11679 	} else if (wdev->wiphy != &rdev->wiphy) {
11680 		return -EINVAL;
11681 	}
11682 
11683 	if (!info->attrs[NL80211_ATTR_TESTDATA])
11684 		return -EINVAL;
11685 
11686 	rdev->cur_cmd_info = info;
11687 	err = rdev_testmode_cmd(rdev, wdev,
11688 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
11689 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
11690 	rdev->cur_cmd_info = NULL;
11691 
11692 	return err;
11693 }
11694 
11695 static int nl80211_testmode_dump(struct sk_buff *skb,
11696 				 struct netlink_callback *cb)
11697 {
11698 	struct cfg80211_registered_device *rdev;
11699 	struct nlattr **attrbuf = NULL;
11700 	int err;
11701 	long phy_idx;
11702 	void *data = NULL;
11703 	int data_len = 0;
11704 
11705 	rtnl_lock();
11706 
11707 	if (cb->args[0]) {
11708 		/*
11709 		 * 0 is a valid index, but not valid for args[0],
11710 		 * so we need to offset by 1.
11711 		 */
11712 		phy_idx = cb->args[0] - 1;
11713 
11714 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
11715 		if (!rdev) {
11716 			err = -ENOENT;
11717 			goto out_err;
11718 		}
11719 	} else {
11720 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
11721 				  GFP_KERNEL);
11722 		if (!attrbuf) {
11723 			err = -ENOMEM;
11724 			goto out_err;
11725 		}
11726 
11727 		err = nlmsg_parse_deprecated(cb->nlh,
11728 					     GENL_HDRLEN + nl80211_fam.hdrsize,
11729 					     attrbuf, nl80211_fam.maxattr,
11730 					     nl80211_policy, NULL);
11731 		if (err)
11732 			goto out_err;
11733 
11734 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
11735 		if (IS_ERR(rdev)) {
11736 			err = PTR_ERR(rdev);
11737 			goto out_err;
11738 		}
11739 		phy_idx = rdev->wiphy_idx;
11740 
11741 		if (attrbuf[NL80211_ATTR_TESTDATA])
11742 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
11743 	}
11744 
11745 	if (cb->args[1]) {
11746 		data = nla_data((void *)cb->args[1]);
11747 		data_len = nla_len((void *)cb->args[1]);
11748 	}
11749 
11750 	if (!rdev->ops->testmode_dump) {
11751 		err = -EOPNOTSUPP;
11752 		goto out_err;
11753 	}
11754 
11755 	while (1) {
11756 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
11757 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
11758 					   NL80211_CMD_TESTMODE);
11759 		struct nlattr *tmdata;
11760 
11761 		if (!hdr)
11762 			break;
11763 
11764 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
11765 			genlmsg_cancel(skb, hdr);
11766 			break;
11767 		}
11768 
11769 		tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
11770 		if (!tmdata) {
11771 			genlmsg_cancel(skb, hdr);
11772 			break;
11773 		}
11774 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
11775 		nla_nest_end(skb, tmdata);
11776 
11777 		if (err == -ENOBUFS || err == -ENOENT) {
11778 			genlmsg_cancel(skb, hdr);
11779 			break;
11780 		} else if (err) {
11781 			genlmsg_cancel(skb, hdr);
11782 			goto out_err;
11783 		}
11784 
11785 		genlmsg_end(skb, hdr);
11786 	}
11787 
11788 	err = skb->len;
11789 	/* see above */
11790 	cb->args[0] = phy_idx + 1;
11791  out_err:
11792 	kfree(attrbuf);
11793 	rtnl_unlock();
11794 	return err;
11795 }
11796 #endif
11797 
11798 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
11799 {
11800 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11801 	struct net_device *dev = info->user_ptr[1];
11802 	struct cfg80211_connect_params connect;
11803 	struct wiphy *wiphy;
11804 	struct cfg80211_cached_keys *connkeys = NULL;
11805 	u32 freq = 0;
11806 	int err;
11807 
11808 	memset(&connect, 0, sizeof(connect));
11809 
11810 	if (!info->attrs[NL80211_ATTR_SSID] ||
11811 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11812 		return -EINVAL;
11813 
11814 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
11815 		connect.auth_type =
11816 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
11817 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
11818 					     NL80211_CMD_CONNECT))
11819 			return -EINVAL;
11820 	} else
11821 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
11822 
11823 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
11824 
11825 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
11826 	    !wiphy_ext_feature_isset(&rdev->wiphy,
11827 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
11828 		return -EINVAL;
11829 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
11830 
11831 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
11832 				      NL80211_MAX_NR_CIPHER_SUITES);
11833 	if (err)
11834 		return err;
11835 
11836 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11837 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11838 		return -EOPNOTSUPP;
11839 
11840 	wiphy = &rdev->wiphy;
11841 
11842 	connect.bg_scan_period = -1;
11843 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
11844 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
11845 		connect.bg_scan_period =
11846 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
11847 	}
11848 
11849 	if (info->attrs[NL80211_ATTR_MAC])
11850 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11851 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
11852 		connect.bssid_hint =
11853 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
11854 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11855 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11856 
11857 	if (info->attrs[NL80211_ATTR_IE]) {
11858 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11859 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11860 	}
11861 
11862 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
11863 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
11864 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
11865 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11866 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
11867 			return -EOPNOTSUPP;
11868 	} else {
11869 		connect.mfp = NL80211_MFP_NO;
11870 	}
11871 
11872 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
11873 		connect.prev_bssid =
11874 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
11875 
11876 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ])
11877 		freq = MHZ_TO_KHZ(nla_get_u32(
11878 					info->attrs[NL80211_ATTR_WIPHY_FREQ]));
11879 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
11880 		freq +=
11881 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
11882 
11883 	if (freq) {
11884 		connect.channel = nl80211_get_valid_chan(wiphy, freq);
11885 		if (!connect.channel)
11886 			return -EINVAL;
11887 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
11888 		freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
11889 		freq = MHZ_TO_KHZ(freq);
11890 		connect.channel_hint = nl80211_get_valid_chan(wiphy, freq);
11891 		if (!connect.channel_hint)
11892 			return -EINVAL;
11893 	}
11894 
11895 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
11896 		connect.edmg.channels =
11897 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
11898 
11899 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
11900 			connect.edmg.bw_config =
11901 				nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
11902 	}
11903 
11904 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11905 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
11906 		if (IS_ERR(connkeys))
11907 			return PTR_ERR(connkeys);
11908 	}
11909 
11910 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
11911 		connect.flags |= ASSOC_REQ_DISABLE_HT;
11912 
11913 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11914 		memcpy(&connect.ht_capa_mask,
11915 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11916 		       sizeof(connect.ht_capa_mask));
11917 
11918 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11919 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
11920 			kfree_sensitive(connkeys);
11921 			return -EINVAL;
11922 		}
11923 		memcpy(&connect.ht_capa,
11924 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11925 		       sizeof(connect.ht_capa));
11926 	}
11927 
11928 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
11929 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
11930 
11931 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
11932 		connect.flags |= ASSOC_REQ_DISABLE_HE;
11933 
11934 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
11935 		connect.flags |= ASSOC_REQ_DISABLE_EHT;
11936 
11937 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11938 		memcpy(&connect.vht_capa_mask,
11939 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
11940 		       sizeof(connect.vht_capa_mask));
11941 
11942 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
11943 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
11944 			kfree_sensitive(connkeys);
11945 			return -EINVAL;
11946 		}
11947 		memcpy(&connect.vht_capa,
11948 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
11949 		       sizeof(connect.vht_capa));
11950 	}
11951 
11952 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
11953 		if (!((rdev->wiphy.features &
11954 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
11955 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
11956 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11957 					     NL80211_EXT_FEATURE_RRM)) {
11958 			kfree_sensitive(connkeys);
11959 			return -EINVAL;
11960 		}
11961 		connect.flags |= ASSOC_REQ_USE_RRM;
11962 	}
11963 
11964 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
11965 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
11966 		kfree_sensitive(connkeys);
11967 		return -EOPNOTSUPP;
11968 	}
11969 
11970 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
11971 		/* bss selection makes no sense if bssid is set */
11972 		if (connect.bssid) {
11973 			kfree_sensitive(connkeys);
11974 			return -EINVAL;
11975 		}
11976 
11977 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
11978 				       wiphy, &connect.bss_select);
11979 		if (err) {
11980 			kfree_sensitive(connkeys);
11981 			return err;
11982 		}
11983 	}
11984 
11985 	if (wiphy_ext_feature_isset(&rdev->wiphy,
11986 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
11987 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
11988 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
11989 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
11990 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11991 		connect.fils_erp_username =
11992 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11993 		connect.fils_erp_username_len =
11994 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11995 		connect.fils_erp_realm =
11996 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11997 		connect.fils_erp_realm_len =
11998 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11999 		connect.fils_erp_next_seq_num =
12000 			nla_get_u16(
12001 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
12002 		connect.fils_erp_rrk =
12003 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12004 		connect.fils_erp_rrk_len =
12005 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12006 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
12007 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
12008 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
12009 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12010 		kfree_sensitive(connkeys);
12011 		return -EINVAL;
12012 	}
12013 
12014 	if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
12015 		if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
12016 			kfree_sensitive(connkeys);
12017 			GENL_SET_ERR_MSG(info,
12018 					 "external auth requires connection ownership");
12019 			return -EINVAL;
12020 		}
12021 		connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
12022 	}
12023 
12024 	if (nla_get_flag(info->attrs[NL80211_ATTR_MLO_SUPPORT]))
12025 		connect.flags |= CONNECT_REQ_MLO_SUPPORT;
12026 
12027 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
12028 			       connect.prev_bssid);
12029 	if (err)
12030 		kfree_sensitive(connkeys);
12031 
12032 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
12033 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
12034 		if (connect.bssid)
12035 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
12036 			       connect.bssid, ETH_ALEN);
12037 		else
12038 			eth_zero_addr(dev->ieee80211_ptr->disconnect_bssid);
12039 	}
12040 
12041 	return err;
12042 }
12043 
12044 static int nl80211_update_connect_params(struct sk_buff *skb,
12045 					 struct genl_info *info)
12046 {
12047 	struct cfg80211_connect_params connect = {};
12048 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12049 	struct net_device *dev = info->user_ptr[1];
12050 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12051 	bool fils_sk_offload;
12052 	u32 auth_type;
12053 	u32 changed = 0;
12054 
12055 	if (!rdev->ops->update_connect_params)
12056 		return -EOPNOTSUPP;
12057 
12058 	if (info->attrs[NL80211_ATTR_IE]) {
12059 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
12060 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
12061 		changed |= UPDATE_ASSOC_IES;
12062 	}
12063 
12064 	fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
12065 						  NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
12066 
12067 	/*
12068 	 * when driver supports fils-sk offload all attributes must be
12069 	 * provided. So the else covers "fils-sk-not-all" and
12070 	 * "no-fils-sk-any".
12071 	 */
12072 	if (fils_sk_offload &&
12073 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
12074 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
12075 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
12076 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12077 		connect.fils_erp_username =
12078 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12079 		connect.fils_erp_username_len =
12080 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12081 		connect.fils_erp_realm =
12082 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12083 		connect.fils_erp_realm_len =
12084 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12085 		connect.fils_erp_next_seq_num =
12086 			nla_get_u16(
12087 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
12088 		connect.fils_erp_rrk =
12089 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12090 		connect.fils_erp_rrk_len =
12091 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12092 		changed |= UPDATE_FILS_ERP_INFO;
12093 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
12094 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
12095 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
12096 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12097 		return -EINVAL;
12098 	}
12099 
12100 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
12101 		auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
12102 		if (!nl80211_valid_auth_type(rdev, auth_type,
12103 					     NL80211_CMD_CONNECT))
12104 			return -EINVAL;
12105 
12106 		if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
12107 		    fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
12108 			return -EINVAL;
12109 
12110 		connect.auth_type = auth_type;
12111 		changed |= UPDATE_AUTH_TYPE;
12112 	}
12113 
12114 	if (!wdev->connected)
12115 		return -ENOLINK;
12116 
12117 	return rdev_update_connect_params(rdev, dev, &connect, changed);
12118 }
12119 
12120 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
12121 {
12122 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12123 	struct net_device *dev = info->user_ptr[1];
12124 	u16 reason;
12125 
12126 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
12127 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
12128 		return -EPERM;
12129 
12130 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
12131 		reason = WLAN_REASON_DEAUTH_LEAVING;
12132 	else
12133 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
12134 
12135 	if (reason == 0)
12136 		return -EINVAL;
12137 
12138 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12139 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
12140 		return -EOPNOTSUPP;
12141 
12142 	return cfg80211_disconnect(rdev, dev, reason, true);
12143 }
12144 
12145 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
12146 {
12147 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12148 	struct net *net;
12149 	int err;
12150 
12151 	if (info->attrs[NL80211_ATTR_PID]) {
12152 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
12153 
12154 		net = get_net_ns_by_pid(pid);
12155 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
12156 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
12157 
12158 		net = get_net_ns_by_fd(fd);
12159 	} else {
12160 		return -EINVAL;
12161 	}
12162 
12163 	if (IS_ERR(net))
12164 		return PTR_ERR(net);
12165 
12166 	err = 0;
12167 
12168 	/* check if anything to do */
12169 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
12170 		err = cfg80211_switch_netns(rdev, net);
12171 
12172 	put_net(net);
12173 	return err;
12174 }
12175 
12176 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
12177 {
12178 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12179 	int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
12180 			struct cfg80211_pmksa *pmksa) = NULL;
12181 	struct net_device *dev = info->user_ptr[1];
12182 	struct cfg80211_pmksa pmksa;
12183 
12184 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
12185 
12186 	if (!info->attrs[NL80211_ATTR_PMKID])
12187 		return -EINVAL;
12188 
12189 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
12190 
12191 	if (info->attrs[NL80211_ATTR_MAC]) {
12192 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
12193 	} else if (info->attrs[NL80211_ATTR_SSID] &&
12194 		   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
12195 		   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
12196 		    info->attrs[NL80211_ATTR_PMK])) {
12197 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
12198 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
12199 		pmksa.cache_id =
12200 			nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
12201 	} else {
12202 		return -EINVAL;
12203 	}
12204 	if (info->attrs[NL80211_ATTR_PMK]) {
12205 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
12206 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
12207 	}
12208 
12209 	if (info->attrs[NL80211_ATTR_PMK_LIFETIME])
12210 		pmksa.pmk_lifetime =
12211 			nla_get_u32(info->attrs[NL80211_ATTR_PMK_LIFETIME]);
12212 
12213 	if (info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD])
12214 		pmksa.pmk_reauth_threshold =
12215 			nla_get_u8(
12216 				info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD]);
12217 
12218 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12219 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
12220 	    !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
12221 	      wiphy_ext_feature_isset(&rdev->wiphy,
12222 				      NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
12223 		return -EOPNOTSUPP;
12224 
12225 	switch (info->genlhdr->cmd) {
12226 	case NL80211_CMD_SET_PMKSA:
12227 		rdev_ops = rdev->ops->set_pmksa;
12228 		break;
12229 	case NL80211_CMD_DEL_PMKSA:
12230 		rdev_ops = rdev->ops->del_pmksa;
12231 		break;
12232 	default:
12233 		WARN_ON(1);
12234 		break;
12235 	}
12236 
12237 	if (!rdev_ops)
12238 		return -EOPNOTSUPP;
12239 
12240 	return rdev_ops(&rdev->wiphy, dev, &pmksa);
12241 }
12242 
12243 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
12244 {
12245 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12246 	struct net_device *dev = info->user_ptr[1];
12247 
12248 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12249 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
12250 		return -EOPNOTSUPP;
12251 
12252 	if (!rdev->ops->flush_pmksa)
12253 		return -EOPNOTSUPP;
12254 
12255 	return rdev_flush_pmksa(rdev, dev);
12256 }
12257 
12258 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
12259 {
12260 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12261 	struct net_device *dev = info->user_ptr[1];
12262 	u8 action_code, dialog_token;
12263 	u32 peer_capability = 0;
12264 	u16 status_code;
12265 	u8 *peer;
12266 	int link_id;
12267 	bool initiator;
12268 
12269 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12270 	    !rdev->ops->tdls_mgmt)
12271 		return -EOPNOTSUPP;
12272 
12273 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
12274 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
12275 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
12276 	    !info->attrs[NL80211_ATTR_IE] ||
12277 	    !info->attrs[NL80211_ATTR_MAC])
12278 		return -EINVAL;
12279 
12280 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12281 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
12282 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
12283 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
12284 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
12285 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
12286 		peer_capability =
12287 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
12288 	link_id = nl80211_link_id_or_invalid(info->attrs);
12289 
12290 	return rdev_tdls_mgmt(rdev, dev, peer, link_id, action_code,
12291 			      dialog_token, status_code, peer_capability,
12292 			      initiator,
12293 			      nla_data(info->attrs[NL80211_ATTR_IE]),
12294 			      nla_len(info->attrs[NL80211_ATTR_IE]));
12295 }
12296 
12297 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
12298 {
12299 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12300 	struct net_device *dev = info->user_ptr[1];
12301 	enum nl80211_tdls_operation operation;
12302 	u8 *peer;
12303 
12304 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12305 	    !rdev->ops->tdls_oper)
12306 		return -EOPNOTSUPP;
12307 
12308 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
12309 	    !info->attrs[NL80211_ATTR_MAC])
12310 		return -EINVAL;
12311 
12312 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
12313 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12314 
12315 	return rdev_tdls_oper(rdev, dev, peer, operation);
12316 }
12317 
12318 static int nl80211_remain_on_channel(struct sk_buff *skb,
12319 				     struct genl_info *info)
12320 {
12321 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12322 	unsigned int link_id = nl80211_link_id(info->attrs);
12323 	struct wireless_dev *wdev = info->user_ptr[1];
12324 	struct cfg80211_chan_def chandef;
12325 	struct sk_buff *msg;
12326 	void *hdr;
12327 	u64 cookie;
12328 	u32 duration;
12329 	int err;
12330 
12331 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
12332 	    !info->attrs[NL80211_ATTR_DURATION])
12333 		return -EINVAL;
12334 
12335 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12336 
12337 	if (!rdev->ops->remain_on_channel ||
12338 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
12339 		return -EOPNOTSUPP;
12340 
12341 	/*
12342 	 * We should be on that channel for at least a minimum amount of
12343 	 * time (10ms) but no longer than the driver supports.
12344 	 */
12345 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12346 	    duration > rdev->wiphy.max_remain_on_channel_duration)
12347 		return -EINVAL;
12348 
12349 	err = nl80211_parse_chandef(rdev, info, &chandef);
12350 	if (err)
12351 		return err;
12352 
12353 	if (!cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12354 		const struct cfg80211_chan_def *oper_chandef, *compat_chandef;
12355 
12356 		oper_chandef = wdev_chandef(wdev, link_id);
12357 
12358 		if (WARN_ON(!oper_chandef)) {
12359 			/* cannot happen since we must beacon to get here */
12360 			WARN_ON(1);
12361 			return -EBUSY;
12362 		}
12363 
12364 		/* note: returns first one if identical chandefs */
12365 		compat_chandef = cfg80211_chandef_compatible(&chandef,
12366 							     oper_chandef);
12367 
12368 		if (compat_chandef != &chandef)
12369 			return -EBUSY;
12370 	}
12371 
12372 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12373 	if (!msg)
12374 		return -ENOMEM;
12375 
12376 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12377 			     NL80211_CMD_REMAIN_ON_CHANNEL);
12378 	if (!hdr) {
12379 		err = -ENOBUFS;
12380 		goto free_msg;
12381 	}
12382 
12383 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
12384 				     duration, &cookie);
12385 
12386 	if (err)
12387 		goto free_msg;
12388 
12389 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12390 			      NL80211_ATTR_PAD))
12391 		goto nla_put_failure;
12392 
12393 	genlmsg_end(msg, hdr);
12394 
12395 	return genlmsg_reply(msg, info);
12396 
12397  nla_put_failure:
12398 	err = -ENOBUFS;
12399  free_msg:
12400 	nlmsg_free(msg);
12401 	return err;
12402 }
12403 
12404 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
12405 					    struct genl_info *info)
12406 {
12407 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12408 	struct wireless_dev *wdev = info->user_ptr[1];
12409 	u64 cookie;
12410 
12411 	if (!info->attrs[NL80211_ATTR_COOKIE])
12412 		return -EINVAL;
12413 
12414 	if (!rdev->ops->cancel_remain_on_channel)
12415 		return -EOPNOTSUPP;
12416 
12417 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12418 
12419 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
12420 }
12421 
12422 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
12423 				       struct genl_info *info)
12424 {
12425 	struct cfg80211_bitrate_mask mask;
12426 	unsigned int link_id = nl80211_link_id(info->attrs);
12427 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12428 	struct net_device *dev = info->user_ptr[1];
12429 	int err;
12430 
12431 	if (!rdev->ops->set_bitrate_mask)
12432 		return -EOPNOTSUPP;
12433 
12434 	err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
12435 					    NL80211_ATTR_TX_RATES, &mask,
12436 					    dev, true, link_id);
12437 	if (err)
12438 		return err;
12439 
12440 	return rdev_set_bitrate_mask(rdev, dev, link_id, NULL, &mask);
12441 }
12442 
12443 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
12444 {
12445 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12446 	struct wireless_dev *wdev = info->user_ptr[1];
12447 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
12448 
12449 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
12450 		return -EINVAL;
12451 
12452 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
12453 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
12454 
12455 	switch (wdev->iftype) {
12456 	case NL80211_IFTYPE_STATION:
12457 	case NL80211_IFTYPE_ADHOC:
12458 	case NL80211_IFTYPE_P2P_CLIENT:
12459 	case NL80211_IFTYPE_AP:
12460 	case NL80211_IFTYPE_AP_VLAN:
12461 	case NL80211_IFTYPE_MESH_POINT:
12462 	case NL80211_IFTYPE_P2P_GO:
12463 	case NL80211_IFTYPE_P2P_DEVICE:
12464 		break;
12465 	case NL80211_IFTYPE_NAN:
12466 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12467 					     NL80211_EXT_FEATURE_SECURE_NAN))
12468 			return -EOPNOTSUPP;
12469 		break;
12470 	default:
12471 		return -EOPNOTSUPP;
12472 	}
12473 
12474 	/* not much point in registering if we can't reply */
12475 	if (!rdev->ops->mgmt_tx)
12476 		return -EOPNOTSUPP;
12477 
12478 	if (info->attrs[NL80211_ATTR_RECEIVE_MULTICAST] &&
12479 	    !wiphy_ext_feature_isset(&rdev->wiphy,
12480 				     NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS)) {
12481 		GENL_SET_ERR_MSG(info,
12482 				 "multicast RX registrations are not supported");
12483 		return -EOPNOTSUPP;
12484 	}
12485 
12486 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
12487 					   nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12488 					   nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12489 					   info->attrs[NL80211_ATTR_RECEIVE_MULTICAST],
12490 					   info->extack);
12491 }
12492 
12493 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
12494 {
12495 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12496 	struct wireless_dev *wdev = info->user_ptr[1];
12497 	struct cfg80211_chan_def chandef;
12498 	int err;
12499 	void *hdr = NULL;
12500 	u64 cookie;
12501 	struct sk_buff *msg = NULL;
12502 	struct cfg80211_mgmt_tx_params params = {
12503 		.dont_wait_for_ack =
12504 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
12505 	};
12506 
12507 	if (!info->attrs[NL80211_ATTR_FRAME])
12508 		return -EINVAL;
12509 
12510 	if (!rdev->ops->mgmt_tx)
12511 		return -EOPNOTSUPP;
12512 
12513 	switch (wdev->iftype) {
12514 	case NL80211_IFTYPE_P2P_DEVICE:
12515 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
12516 			return -EINVAL;
12517 		break;
12518 	case NL80211_IFTYPE_STATION:
12519 	case NL80211_IFTYPE_ADHOC:
12520 	case NL80211_IFTYPE_P2P_CLIENT:
12521 	case NL80211_IFTYPE_AP:
12522 	case NL80211_IFTYPE_AP_VLAN:
12523 	case NL80211_IFTYPE_MESH_POINT:
12524 	case NL80211_IFTYPE_P2P_GO:
12525 		break;
12526 	case NL80211_IFTYPE_NAN:
12527 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12528 					     NL80211_EXT_FEATURE_SECURE_NAN))
12529 			return -EOPNOTSUPP;
12530 		break;
12531 	default:
12532 		return -EOPNOTSUPP;
12533 	}
12534 
12535 	if (info->attrs[NL80211_ATTR_DURATION]) {
12536 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12537 			return -EINVAL;
12538 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12539 
12540 		/*
12541 		 * We should wait on the channel for at least a minimum amount
12542 		 * of time (10ms) but no longer than the driver supports.
12543 		 */
12544 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12545 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
12546 			return -EINVAL;
12547 	}
12548 
12549 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
12550 
12551 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12552 		return -EINVAL;
12553 
12554 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
12555 
12556 	/* get the channel if any has been specified, otherwise pass NULL to
12557 	 * the driver. The latter will use the current one
12558 	 */
12559 	chandef.chan = NULL;
12560 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
12561 		err = nl80211_parse_chandef(rdev, info, &chandef);
12562 		if (err)
12563 			return err;
12564 	}
12565 
12566 	if (!chandef.chan && params.offchan)
12567 		return -EINVAL;
12568 
12569 	if (params.offchan &&
12570 	    !cfg80211_off_channel_oper_allowed(wdev, chandef.chan))
12571 		return -EBUSY;
12572 
12573 	params.link_id = nl80211_link_id_or_invalid(info->attrs);
12574 	/*
12575 	 * This now races due to the unlock, but we cannot check
12576 	 * the valid links for the _station_ anyway, so that's up
12577 	 * to the driver.
12578 	 */
12579 	if (params.link_id >= 0 &&
12580 	    !(wdev->valid_links & BIT(params.link_id)))
12581 		return -EINVAL;
12582 
12583 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
12584 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
12585 
12586 	if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
12587 		int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12588 		int i;
12589 
12590 		if (len % sizeof(u16))
12591 			return -EINVAL;
12592 
12593 		params.n_csa_offsets = len / sizeof(u16);
12594 		params.csa_offsets =
12595 			nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12596 
12597 		/* check that all the offsets fit the frame */
12598 		for (i = 0; i < params.n_csa_offsets; i++) {
12599 			if (params.csa_offsets[i] >= params.len)
12600 				return -EINVAL;
12601 		}
12602 	}
12603 
12604 	if (!params.dont_wait_for_ack) {
12605 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12606 		if (!msg)
12607 			return -ENOMEM;
12608 
12609 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12610 				     NL80211_CMD_FRAME);
12611 		if (!hdr) {
12612 			err = -ENOBUFS;
12613 			goto free_msg;
12614 		}
12615 	}
12616 
12617 	params.chan = chandef.chan;
12618 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
12619 	if (err)
12620 		goto free_msg;
12621 
12622 	if (msg) {
12623 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12624 				      NL80211_ATTR_PAD))
12625 			goto nla_put_failure;
12626 
12627 		genlmsg_end(msg, hdr);
12628 		return genlmsg_reply(msg, info);
12629 	}
12630 
12631 	return 0;
12632 
12633  nla_put_failure:
12634 	err = -ENOBUFS;
12635  free_msg:
12636 	nlmsg_free(msg);
12637 	return err;
12638 }
12639 
12640 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
12641 {
12642 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12643 	struct wireless_dev *wdev = info->user_ptr[1];
12644 	u64 cookie;
12645 
12646 	if (!info->attrs[NL80211_ATTR_COOKIE])
12647 		return -EINVAL;
12648 
12649 	if (!rdev->ops->mgmt_tx_cancel_wait)
12650 		return -EOPNOTSUPP;
12651 
12652 	switch (wdev->iftype) {
12653 	case NL80211_IFTYPE_STATION:
12654 	case NL80211_IFTYPE_ADHOC:
12655 	case NL80211_IFTYPE_P2P_CLIENT:
12656 	case NL80211_IFTYPE_AP:
12657 	case NL80211_IFTYPE_AP_VLAN:
12658 	case NL80211_IFTYPE_P2P_GO:
12659 	case NL80211_IFTYPE_P2P_DEVICE:
12660 		break;
12661 	case NL80211_IFTYPE_NAN:
12662 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12663 					     NL80211_EXT_FEATURE_SECURE_NAN))
12664 			return -EOPNOTSUPP;
12665 		break;
12666 	default:
12667 		return -EOPNOTSUPP;
12668 	}
12669 
12670 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12671 
12672 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
12673 }
12674 
12675 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
12676 {
12677 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12678 	struct wireless_dev *wdev;
12679 	struct net_device *dev = info->user_ptr[1];
12680 	u8 ps_state;
12681 	bool state;
12682 	int err;
12683 
12684 	if (!info->attrs[NL80211_ATTR_PS_STATE])
12685 		return -EINVAL;
12686 
12687 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
12688 
12689 	wdev = dev->ieee80211_ptr;
12690 
12691 	if (!rdev->ops->set_power_mgmt)
12692 		return -EOPNOTSUPP;
12693 
12694 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
12695 
12696 	if (state == wdev->ps)
12697 		return 0;
12698 
12699 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
12700 	if (!err)
12701 		wdev->ps = state;
12702 	return err;
12703 }
12704 
12705 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
12706 {
12707 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12708 	enum nl80211_ps_state ps_state;
12709 	struct wireless_dev *wdev;
12710 	struct net_device *dev = info->user_ptr[1];
12711 	struct sk_buff *msg;
12712 	void *hdr;
12713 	int err;
12714 
12715 	wdev = dev->ieee80211_ptr;
12716 
12717 	if (!rdev->ops->set_power_mgmt)
12718 		return -EOPNOTSUPP;
12719 
12720 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12721 	if (!msg)
12722 		return -ENOMEM;
12723 
12724 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12725 			     NL80211_CMD_GET_POWER_SAVE);
12726 	if (!hdr) {
12727 		err = -ENOBUFS;
12728 		goto free_msg;
12729 	}
12730 
12731 	if (wdev->ps)
12732 		ps_state = NL80211_PS_ENABLED;
12733 	else
12734 		ps_state = NL80211_PS_DISABLED;
12735 
12736 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
12737 		goto nla_put_failure;
12738 
12739 	genlmsg_end(msg, hdr);
12740 	return genlmsg_reply(msg, info);
12741 
12742  nla_put_failure:
12743 	err = -ENOBUFS;
12744  free_msg:
12745 	nlmsg_free(msg);
12746 	return err;
12747 }
12748 
12749 static const struct nla_policy
12750 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
12751 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
12752 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
12753 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
12754 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
12755 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
12756 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
12757 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
12758 };
12759 
12760 static int nl80211_set_cqm_txe(struct genl_info *info,
12761 			       u32 rate, u32 pkts, u32 intvl)
12762 {
12763 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12764 	struct net_device *dev = info->user_ptr[1];
12765 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12766 
12767 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
12768 		return -EINVAL;
12769 
12770 	if (!rdev->ops->set_cqm_txe_config)
12771 		return -EOPNOTSUPP;
12772 
12773 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12774 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12775 		return -EOPNOTSUPP;
12776 
12777 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
12778 }
12779 
12780 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
12781 				    struct net_device *dev,
12782 				    struct cfg80211_cqm_config *cqm_config)
12783 {
12784 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12785 	s32 last, low, high;
12786 	u32 hyst;
12787 	int i, n, low_index;
12788 	int err;
12789 
12790 	/* RSSI reporting disabled? */
12791 	if (!cqm_config)
12792 		return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
12793 
12794 	/*
12795 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
12796 	 * event has been received yet, we should receive an event after a
12797 	 * connection is established and enough beacons received to calculate
12798 	 * the average.
12799 	 */
12800 	if (!cqm_config->last_rssi_event_value &&
12801 	    wdev->links[0].client.current_bss &&
12802 	    rdev->ops->get_station) {
12803 		struct station_info sinfo = {};
12804 		u8 *mac_addr;
12805 
12806 		mac_addr = wdev->links[0].client.current_bss->pub.bssid;
12807 
12808 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
12809 		if (err)
12810 			return err;
12811 
12812 		cfg80211_sinfo_release_content(&sinfo);
12813 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
12814 			cqm_config->last_rssi_event_value =
12815 				(s8) sinfo.rx_beacon_signal_avg;
12816 	}
12817 
12818 	last = cqm_config->last_rssi_event_value;
12819 	hyst = cqm_config->rssi_hyst;
12820 	n = cqm_config->n_rssi_thresholds;
12821 
12822 	for (i = 0; i < n; i++) {
12823 		i = array_index_nospec(i, n);
12824 		if (last < cqm_config->rssi_thresholds[i])
12825 			break;
12826 	}
12827 
12828 	low_index = i - 1;
12829 	if (low_index >= 0) {
12830 		low_index = array_index_nospec(low_index, n);
12831 		low = cqm_config->rssi_thresholds[low_index] - hyst;
12832 	} else {
12833 		low = S32_MIN;
12834 	}
12835 	if (i < n) {
12836 		i = array_index_nospec(i, n);
12837 		high = cqm_config->rssi_thresholds[i] + hyst - 1;
12838 	} else {
12839 		high = S32_MAX;
12840 	}
12841 
12842 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
12843 }
12844 
12845 static int nl80211_set_cqm_rssi(struct genl_info *info,
12846 				const s32 *thresholds, int n_thresholds,
12847 				u32 hysteresis)
12848 {
12849 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12850 	struct cfg80211_cqm_config *cqm_config = NULL, *old;
12851 	struct net_device *dev = info->user_ptr[1];
12852 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12853 	s32 prev = S32_MIN;
12854 	int i, err;
12855 
12856 	/* Check all values negative and sorted */
12857 	for (i = 0; i < n_thresholds; i++) {
12858 		if (thresholds[i] > 0 || thresholds[i] <= prev)
12859 			return -EINVAL;
12860 
12861 		prev = thresholds[i];
12862 	}
12863 
12864 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12865 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12866 		return -EOPNOTSUPP;
12867 
12868 	if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
12869 		if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
12870 			return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
12871 
12872 		return rdev_set_cqm_rssi_config(rdev, dev,
12873 						thresholds[0], hysteresis);
12874 	}
12875 
12876 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
12877 				     NL80211_EXT_FEATURE_CQM_RSSI_LIST))
12878 		return -EOPNOTSUPP;
12879 
12880 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
12881 		n_thresholds = 0;
12882 
12883 	old = wiphy_dereference(wdev->wiphy, wdev->cqm_config);
12884 
12885 	if (n_thresholds) {
12886 		cqm_config = kzalloc(struct_size(cqm_config, rssi_thresholds,
12887 						 n_thresholds),
12888 				     GFP_KERNEL);
12889 		if (!cqm_config)
12890 			return -ENOMEM;
12891 
12892 		cqm_config->rssi_hyst = hysteresis;
12893 		cqm_config->n_rssi_thresholds = n_thresholds;
12894 		memcpy(cqm_config->rssi_thresholds, thresholds,
12895 		       flex_array_size(cqm_config, rssi_thresholds,
12896 				       n_thresholds));
12897 
12898 		rcu_assign_pointer(wdev->cqm_config, cqm_config);
12899 	} else {
12900 		RCU_INIT_POINTER(wdev->cqm_config, NULL);
12901 	}
12902 
12903 	err = cfg80211_cqm_rssi_update(rdev, dev, cqm_config);
12904 	if (err) {
12905 		rcu_assign_pointer(wdev->cqm_config, old);
12906 		kfree_rcu(cqm_config, rcu_head);
12907 	} else {
12908 		kfree_rcu(old, rcu_head);
12909 	}
12910 
12911 	return err;
12912 }
12913 
12914 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
12915 {
12916 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
12917 	struct nlattr *cqm;
12918 	int err;
12919 
12920 	cqm = info->attrs[NL80211_ATTR_CQM];
12921 	if (!cqm)
12922 		return -EINVAL;
12923 
12924 	err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
12925 					  nl80211_attr_cqm_policy,
12926 					  info->extack);
12927 	if (err)
12928 		return err;
12929 
12930 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
12931 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
12932 		const s32 *thresholds =
12933 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
12934 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
12935 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
12936 
12937 		if (len % 4)
12938 			return -EINVAL;
12939 
12940 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
12941 					    hysteresis);
12942 	}
12943 
12944 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
12945 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
12946 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
12947 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
12948 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
12949 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
12950 
12951 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
12952 	}
12953 
12954 	return -EINVAL;
12955 }
12956 
12957 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
12958 {
12959 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12960 	struct net_device *dev = info->user_ptr[1];
12961 	struct ocb_setup setup = {};
12962 	int err;
12963 
12964 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
12965 	if (err)
12966 		return err;
12967 
12968 	return cfg80211_join_ocb(rdev, dev, &setup);
12969 }
12970 
12971 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
12972 {
12973 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12974 	struct net_device *dev = info->user_ptr[1];
12975 
12976 	return cfg80211_leave_ocb(rdev, dev);
12977 }
12978 
12979 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
12980 {
12981 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12982 	struct net_device *dev = info->user_ptr[1];
12983 	struct mesh_config cfg;
12984 	struct mesh_setup setup;
12985 	int err;
12986 
12987 	/* start with default */
12988 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
12989 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
12990 
12991 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
12992 		/* and parse parameters if given */
12993 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
12994 		if (err)
12995 			return err;
12996 	}
12997 
12998 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
12999 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
13000 		return -EINVAL;
13001 
13002 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
13003 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
13004 
13005 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
13006 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
13007 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
13008 			return -EINVAL;
13009 
13010 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
13011 		setup.beacon_interval =
13012 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
13013 
13014 		err = cfg80211_validate_beacon_int(rdev,
13015 						   NL80211_IFTYPE_MESH_POINT,
13016 						   setup.beacon_interval);
13017 		if (err)
13018 			return err;
13019 	}
13020 
13021 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
13022 		setup.dtim_period =
13023 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
13024 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
13025 			return -EINVAL;
13026 	}
13027 
13028 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
13029 		/* parse additional setup parameters if given */
13030 		err = nl80211_parse_mesh_setup(info, &setup);
13031 		if (err)
13032 			return err;
13033 	}
13034 
13035 	if (setup.user_mpm)
13036 		cfg.auto_open_plinks = false;
13037 
13038 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
13039 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
13040 		if (err)
13041 			return err;
13042 	} else {
13043 		/* __cfg80211_join_mesh() will sort it out */
13044 		setup.chandef.chan = NULL;
13045 	}
13046 
13047 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
13048 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
13049 		int n_rates =
13050 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
13051 		struct ieee80211_supported_band *sband;
13052 
13053 		if (!setup.chandef.chan)
13054 			return -EINVAL;
13055 
13056 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
13057 
13058 		err = ieee80211_get_ratemask(sband, rates, n_rates,
13059 					     &setup.basic_rates);
13060 		if (err)
13061 			return err;
13062 	}
13063 
13064 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
13065 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
13066 						    NL80211_ATTR_TX_RATES,
13067 						    &setup.beacon_rate,
13068 						    dev, false, 0);
13069 		if (err)
13070 			return err;
13071 
13072 		if (!setup.chandef.chan)
13073 			return -EINVAL;
13074 
13075 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
13076 					      &setup.beacon_rate);
13077 		if (err)
13078 			return err;
13079 	}
13080 
13081 	setup.userspace_handles_dfs =
13082 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
13083 
13084 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
13085 		int r = validate_pae_over_nl80211(rdev, info);
13086 
13087 		if (r < 0)
13088 			return r;
13089 
13090 		setup.control_port_over_nl80211 = true;
13091 	}
13092 
13093 	err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
13094 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
13095 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
13096 
13097 	return err;
13098 }
13099 
13100 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
13101 {
13102 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13103 	struct net_device *dev = info->user_ptr[1];
13104 
13105 	return cfg80211_leave_mesh(rdev, dev);
13106 }
13107 
13108 #ifdef CONFIG_PM
13109 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
13110 					struct cfg80211_registered_device *rdev)
13111 {
13112 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
13113 	struct nlattr *nl_pats, *nl_pat;
13114 	int i, pat_len;
13115 
13116 	if (!wowlan->n_patterns)
13117 		return 0;
13118 
13119 	nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
13120 	if (!nl_pats)
13121 		return -ENOBUFS;
13122 
13123 	for (i = 0; i < wowlan->n_patterns; i++) {
13124 		nl_pat = nla_nest_start_noflag(msg, i + 1);
13125 		if (!nl_pat)
13126 			return -ENOBUFS;
13127 		pat_len = wowlan->patterns[i].pattern_len;
13128 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
13129 			    wowlan->patterns[i].mask) ||
13130 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
13131 			    wowlan->patterns[i].pattern) ||
13132 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
13133 				wowlan->patterns[i].pkt_offset))
13134 			return -ENOBUFS;
13135 		nla_nest_end(msg, nl_pat);
13136 	}
13137 	nla_nest_end(msg, nl_pats);
13138 
13139 	return 0;
13140 }
13141 
13142 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
13143 				   struct cfg80211_wowlan_tcp *tcp)
13144 {
13145 	struct nlattr *nl_tcp;
13146 
13147 	if (!tcp)
13148 		return 0;
13149 
13150 	nl_tcp = nla_nest_start_noflag(msg,
13151 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
13152 	if (!nl_tcp)
13153 		return -ENOBUFS;
13154 
13155 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
13156 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
13157 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
13158 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
13159 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
13160 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
13161 		    tcp->payload_len, tcp->payload) ||
13162 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
13163 			tcp->data_interval) ||
13164 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
13165 		    tcp->wake_len, tcp->wake_data) ||
13166 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
13167 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
13168 		return -ENOBUFS;
13169 
13170 	if (tcp->payload_seq.len &&
13171 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
13172 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
13173 		return -ENOBUFS;
13174 
13175 	if (tcp->payload_tok.len &&
13176 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
13177 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
13178 		    &tcp->payload_tok))
13179 		return -ENOBUFS;
13180 
13181 	nla_nest_end(msg, nl_tcp);
13182 
13183 	return 0;
13184 }
13185 
13186 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
13187 				  struct cfg80211_sched_scan_request *req)
13188 {
13189 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
13190 	int i;
13191 
13192 	if (!req)
13193 		return 0;
13194 
13195 	nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
13196 	if (!nd)
13197 		return -ENOBUFS;
13198 
13199 	if (req->n_scan_plans == 1 &&
13200 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
13201 			req->scan_plans[0].interval * 1000))
13202 		return -ENOBUFS;
13203 
13204 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
13205 		return -ENOBUFS;
13206 
13207 	if (req->relative_rssi_set) {
13208 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
13209 
13210 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
13211 			       req->relative_rssi))
13212 			return -ENOBUFS;
13213 
13214 		rssi_adjust.band = req->rssi_adjust.band;
13215 		rssi_adjust.delta = req->rssi_adjust.delta;
13216 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
13217 			    sizeof(rssi_adjust), &rssi_adjust))
13218 			return -ENOBUFS;
13219 	}
13220 
13221 	freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
13222 	if (!freqs)
13223 		return -ENOBUFS;
13224 
13225 	for (i = 0; i < req->n_channels; i++) {
13226 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
13227 			return -ENOBUFS;
13228 	}
13229 
13230 	nla_nest_end(msg, freqs);
13231 
13232 	if (req->n_match_sets) {
13233 		matches = nla_nest_start_noflag(msg,
13234 						NL80211_ATTR_SCHED_SCAN_MATCH);
13235 		if (!matches)
13236 			return -ENOBUFS;
13237 
13238 		for (i = 0; i < req->n_match_sets; i++) {
13239 			match = nla_nest_start_noflag(msg, i);
13240 			if (!match)
13241 				return -ENOBUFS;
13242 
13243 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
13244 				    req->match_sets[i].ssid.ssid_len,
13245 				    req->match_sets[i].ssid.ssid))
13246 				return -ENOBUFS;
13247 			nla_nest_end(msg, match);
13248 		}
13249 		nla_nest_end(msg, matches);
13250 	}
13251 
13252 	scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
13253 	if (!scan_plans)
13254 		return -ENOBUFS;
13255 
13256 	for (i = 0; i < req->n_scan_plans; i++) {
13257 		scan_plan = nla_nest_start_noflag(msg, i + 1);
13258 		if (!scan_plan)
13259 			return -ENOBUFS;
13260 
13261 		if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
13262 				req->scan_plans[i].interval) ||
13263 		    (req->scan_plans[i].iterations &&
13264 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
13265 				 req->scan_plans[i].iterations)))
13266 			return -ENOBUFS;
13267 		nla_nest_end(msg, scan_plan);
13268 	}
13269 	nla_nest_end(msg, scan_plans);
13270 
13271 	nla_nest_end(msg, nd);
13272 
13273 	return 0;
13274 }
13275 
13276 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
13277 {
13278 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13279 	struct sk_buff *msg;
13280 	void *hdr;
13281 	u32 size = NLMSG_DEFAULT_SIZE;
13282 
13283 	if (!rdev->wiphy.wowlan)
13284 		return -EOPNOTSUPP;
13285 
13286 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
13287 		/* adjust size to have room for all the data */
13288 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
13289 			rdev->wiphy.wowlan_config->tcp->payload_len +
13290 			rdev->wiphy.wowlan_config->tcp->wake_len +
13291 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
13292 	}
13293 
13294 	msg = nlmsg_new(size, GFP_KERNEL);
13295 	if (!msg)
13296 		return -ENOMEM;
13297 
13298 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13299 			     NL80211_CMD_GET_WOWLAN);
13300 	if (!hdr)
13301 		goto nla_put_failure;
13302 
13303 	if (rdev->wiphy.wowlan_config) {
13304 		struct nlattr *nl_wowlan;
13305 
13306 		nl_wowlan = nla_nest_start_noflag(msg,
13307 						  NL80211_ATTR_WOWLAN_TRIGGERS);
13308 		if (!nl_wowlan)
13309 			goto nla_put_failure;
13310 
13311 		if ((rdev->wiphy.wowlan_config->any &&
13312 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
13313 		    (rdev->wiphy.wowlan_config->disconnect &&
13314 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
13315 		    (rdev->wiphy.wowlan_config->magic_pkt &&
13316 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
13317 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
13318 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
13319 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
13320 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
13321 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
13322 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
13323 		    (rdev->wiphy.wowlan_config->rfkill_release &&
13324 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
13325 			goto nla_put_failure;
13326 
13327 		if (nl80211_send_wowlan_patterns(msg, rdev))
13328 			goto nla_put_failure;
13329 
13330 		if (nl80211_send_wowlan_tcp(msg,
13331 					    rdev->wiphy.wowlan_config->tcp))
13332 			goto nla_put_failure;
13333 
13334 		if (nl80211_send_wowlan_nd(
13335 			    msg,
13336 			    rdev->wiphy.wowlan_config->nd_config))
13337 			goto nla_put_failure;
13338 
13339 		nla_nest_end(msg, nl_wowlan);
13340 	}
13341 
13342 	genlmsg_end(msg, hdr);
13343 	return genlmsg_reply(msg, info);
13344 
13345 nla_put_failure:
13346 	nlmsg_free(msg);
13347 	return -ENOBUFS;
13348 }
13349 
13350 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
13351 				    struct nlattr *attr,
13352 				    struct cfg80211_wowlan *trig)
13353 {
13354 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
13355 	struct cfg80211_wowlan_tcp *cfg;
13356 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
13357 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
13358 	u32 size;
13359 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
13360 	int err, port;
13361 
13362 	if (!rdev->wiphy.wowlan->tcp)
13363 		return -EINVAL;
13364 
13365 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
13366 					  nl80211_wowlan_tcp_policy, NULL);
13367 	if (err)
13368 		return err;
13369 
13370 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
13371 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
13372 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
13373 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
13374 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
13375 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
13376 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
13377 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
13378 		return -EINVAL;
13379 
13380 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
13381 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
13382 		return -EINVAL;
13383 
13384 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
13385 			rdev->wiphy.wowlan->tcp->data_interval_max ||
13386 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
13387 		return -EINVAL;
13388 
13389 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
13390 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
13391 		return -EINVAL;
13392 
13393 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
13394 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
13395 		return -EINVAL;
13396 
13397 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
13398 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13399 
13400 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13401 		tokens_size = tokln - sizeof(*tok);
13402 
13403 		if (!tok->len || tokens_size % tok->len)
13404 			return -EINVAL;
13405 		if (!rdev->wiphy.wowlan->tcp->tok)
13406 			return -EINVAL;
13407 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
13408 			return -EINVAL;
13409 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
13410 			return -EINVAL;
13411 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
13412 			return -EINVAL;
13413 		if (tok->offset + tok->len > data_size)
13414 			return -EINVAL;
13415 	}
13416 
13417 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
13418 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
13419 		if (!rdev->wiphy.wowlan->tcp->seq)
13420 			return -EINVAL;
13421 		if (seq->len == 0 || seq->len > 4)
13422 			return -EINVAL;
13423 		if (seq->len + seq->offset > data_size)
13424 			return -EINVAL;
13425 	}
13426 
13427 	size = sizeof(*cfg);
13428 	size += data_size;
13429 	size += wake_size + wake_mask_size;
13430 	size += tokens_size;
13431 
13432 	cfg = kzalloc(size, GFP_KERNEL);
13433 	if (!cfg)
13434 		return -ENOMEM;
13435 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
13436 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
13437 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
13438 	       ETH_ALEN);
13439 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
13440 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
13441 	else
13442 		port = 0;
13443 #ifdef CONFIG_INET
13444 	/* allocate a socket and port for it and use it */
13445 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
13446 			    IPPROTO_TCP, &cfg->sock, 1);
13447 	if (err) {
13448 		kfree(cfg);
13449 		return err;
13450 	}
13451 	if (inet_csk_get_port(cfg->sock->sk, port)) {
13452 		sock_release(cfg->sock);
13453 		kfree(cfg);
13454 		return -EADDRINUSE;
13455 	}
13456 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
13457 #else
13458 	if (!port) {
13459 		kfree(cfg);
13460 		return -EINVAL;
13461 	}
13462 	cfg->src_port = port;
13463 #endif
13464 
13465 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
13466 	cfg->payload_len = data_size;
13467 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
13468 	memcpy((void *)cfg->payload,
13469 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
13470 	       data_size);
13471 	if (seq)
13472 		cfg->payload_seq = *seq;
13473 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
13474 	cfg->wake_len = wake_size;
13475 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
13476 	memcpy((void *)cfg->wake_data,
13477 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
13478 	       wake_size);
13479 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
13480 			 data_size + wake_size;
13481 	memcpy((void *)cfg->wake_mask,
13482 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
13483 	       wake_mask_size);
13484 	if (tok) {
13485 		cfg->tokens_size = tokens_size;
13486 		cfg->payload_tok = *tok;
13487 		memcpy(cfg->payload_tok.token_stream, tok->token_stream,
13488 		       tokens_size);
13489 	}
13490 
13491 	trig->tcp = cfg;
13492 
13493 	return 0;
13494 }
13495 
13496 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
13497 				   const struct wiphy_wowlan_support *wowlan,
13498 				   struct nlattr *attr,
13499 				   struct cfg80211_wowlan *trig)
13500 {
13501 	struct nlattr **tb;
13502 	int err;
13503 
13504 	tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
13505 	if (!tb)
13506 		return -ENOMEM;
13507 
13508 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
13509 		err = -EOPNOTSUPP;
13510 		goto out;
13511 	}
13512 
13513 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
13514 					  nl80211_policy, NULL);
13515 	if (err)
13516 		goto out;
13517 
13518 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
13519 						   wowlan->max_nd_match_sets);
13520 	err = PTR_ERR_OR_ZERO(trig->nd_config);
13521 	if (err)
13522 		trig->nd_config = NULL;
13523 
13524 out:
13525 	kfree(tb);
13526 	return err;
13527 }
13528 
13529 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
13530 {
13531 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13532 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
13533 	struct cfg80211_wowlan new_triggers = {};
13534 	struct cfg80211_wowlan *ntrig;
13535 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
13536 	int err, i;
13537 	bool prev_enabled = rdev->wiphy.wowlan_config;
13538 	bool regular = false;
13539 
13540 	if (!wowlan)
13541 		return -EOPNOTSUPP;
13542 
13543 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
13544 		cfg80211_rdev_free_wowlan(rdev);
13545 		rdev->wiphy.wowlan_config = NULL;
13546 		goto set_wakeup;
13547 	}
13548 
13549 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
13550 					  info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
13551 					  nl80211_wowlan_policy, info->extack);
13552 	if (err)
13553 		return err;
13554 
13555 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
13556 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
13557 			return -EINVAL;
13558 		new_triggers.any = true;
13559 	}
13560 
13561 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
13562 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
13563 			return -EINVAL;
13564 		new_triggers.disconnect = true;
13565 		regular = true;
13566 	}
13567 
13568 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
13569 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
13570 			return -EINVAL;
13571 		new_triggers.magic_pkt = true;
13572 		regular = true;
13573 	}
13574 
13575 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
13576 		return -EINVAL;
13577 
13578 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
13579 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
13580 			return -EINVAL;
13581 		new_triggers.gtk_rekey_failure = true;
13582 		regular = true;
13583 	}
13584 
13585 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
13586 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
13587 			return -EINVAL;
13588 		new_triggers.eap_identity_req = true;
13589 		regular = true;
13590 	}
13591 
13592 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
13593 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
13594 			return -EINVAL;
13595 		new_triggers.four_way_handshake = true;
13596 		regular = true;
13597 	}
13598 
13599 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
13600 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
13601 			return -EINVAL;
13602 		new_triggers.rfkill_release = true;
13603 		regular = true;
13604 	}
13605 
13606 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
13607 		struct nlattr *pat;
13608 		int n_patterns = 0;
13609 		int rem, pat_len, mask_len, pkt_offset;
13610 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13611 
13612 		regular = true;
13613 
13614 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13615 				    rem)
13616 			n_patterns++;
13617 		if (n_patterns > wowlan->n_patterns)
13618 			return -EINVAL;
13619 
13620 		new_triggers.patterns = kcalloc(n_patterns,
13621 						sizeof(new_triggers.patterns[0]),
13622 						GFP_KERNEL);
13623 		if (!new_triggers.patterns)
13624 			return -ENOMEM;
13625 
13626 		new_triggers.n_patterns = n_patterns;
13627 		i = 0;
13628 
13629 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13630 				    rem) {
13631 			u8 *mask_pat;
13632 
13633 			err = nla_parse_nested_deprecated(pat_tb,
13634 							  MAX_NL80211_PKTPAT,
13635 							  pat,
13636 							  nl80211_packet_pattern_policy,
13637 							  info->extack);
13638 			if (err)
13639 				goto error;
13640 
13641 			err = -EINVAL;
13642 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
13643 			    !pat_tb[NL80211_PKTPAT_PATTERN])
13644 				goto error;
13645 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13646 			mask_len = DIV_ROUND_UP(pat_len, 8);
13647 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13648 				goto error;
13649 			if (pat_len > wowlan->pattern_max_len ||
13650 			    pat_len < wowlan->pattern_min_len)
13651 				goto error;
13652 
13653 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
13654 				pkt_offset = 0;
13655 			else
13656 				pkt_offset = nla_get_u32(
13657 					pat_tb[NL80211_PKTPAT_OFFSET]);
13658 			if (pkt_offset > wowlan->max_pkt_offset)
13659 				goto error;
13660 			new_triggers.patterns[i].pkt_offset = pkt_offset;
13661 
13662 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13663 			if (!mask_pat) {
13664 				err = -ENOMEM;
13665 				goto error;
13666 			}
13667 			new_triggers.patterns[i].mask = mask_pat;
13668 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13669 			       mask_len);
13670 			mask_pat += mask_len;
13671 			new_triggers.patterns[i].pattern = mask_pat;
13672 			new_triggers.patterns[i].pattern_len = pat_len;
13673 			memcpy(mask_pat,
13674 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13675 			       pat_len);
13676 			i++;
13677 		}
13678 	}
13679 
13680 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
13681 		regular = true;
13682 		err = nl80211_parse_wowlan_tcp(
13683 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
13684 			&new_triggers);
13685 		if (err)
13686 			goto error;
13687 	}
13688 
13689 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
13690 		regular = true;
13691 		err = nl80211_parse_wowlan_nd(
13692 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
13693 			&new_triggers);
13694 		if (err)
13695 			goto error;
13696 	}
13697 
13698 	/* The 'any' trigger means the device continues operating more or less
13699 	 * as in its normal operation mode and wakes up the host on most of the
13700 	 * normal interrupts (like packet RX, ...)
13701 	 * It therefore makes little sense to combine with the more constrained
13702 	 * wakeup trigger modes.
13703 	 */
13704 	if (new_triggers.any && regular) {
13705 		err = -EINVAL;
13706 		goto error;
13707 	}
13708 
13709 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
13710 	if (!ntrig) {
13711 		err = -ENOMEM;
13712 		goto error;
13713 	}
13714 	cfg80211_rdev_free_wowlan(rdev);
13715 	rdev->wiphy.wowlan_config = ntrig;
13716 
13717  set_wakeup:
13718 	if (rdev->ops->set_wakeup &&
13719 	    prev_enabled != !!rdev->wiphy.wowlan_config)
13720 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
13721 
13722 	return 0;
13723  error:
13724 	for (i = 0; i < new_triggers.n_patterns; i++)
13725 		kfree(new_triggers.patterns[i].mask);
13726 	kfree(new_triggers.patterns);
13727 	if (new_triggers.tcp && new_triggers.tcp->sock)
13728 		sock_release(new_triggers.tcp->sock);
13729 	kfree(new_triggers.tcp);
13730 	kfree(new_triggers.nd_config);
13731 	return err;
13732 }
13733 #endif
13734 
13735 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
13736 				       struct cfg80211_registered_device *rdev)
13737 {
13738 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
13739 	int i, j, pat_len;
13740 	struct cfg80211_coalesce_rules *rule;
13741 
13742 	if (!rdev->coalesce->n_rules)
13743 		return 0;
13744 
13745 	nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
13746 	if (!nl_rules)
13747 		return -ENOBUFS;
13748 
13749 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
13750 		nl_rule = nla_nest_start_noflag(msg, i + 1);
13751 		if (!nl_rule)
13752 			return -ENOBUFS;
13753 
13754 		rule = &rdev->coalesce->rules[i];
13755 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
13756 				rule->delay))
13757 			return -ENOBUFS;
13758 
13759 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
13760 				rule->condition))
13761 			return -ENOBUFS;
13762 
13763 		nl_pats = nla_nest_start_noflag(msg,
13764 						NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
13765 		if (!nl_pats)
13766 			return -ENOBUFS;
13767 
13768 		for (j = 0; j < rule->n_patterns; j++) {
13769 			nl_pat = nla_nest_start_noflag(msg, j + 1);
13770 			if (!nl_pat)
13771 				return -ENOBUFS;
13772 			pat_len = rule->patterns[j].pattern_len;
13773 			if (nla_put(msg, NL80211_PKTPAT_MASK,
13774 				    DIV_ROUND_UP(pat_len, 8),
13775 				    rule->patterns[j].mask) ||
13776 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
13777 				    rule->patterns[j].pattern) ||
13778 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
13779 					rule->patterns[j].pkt_offset))
13780 				return -ENOBUFS;
13781 			nla_nest_end(msg, nl_pat);
13782 		}
13783 		nla_nest_end(msg, nl_pats);
13784 		nla_nest_end(msg, nl_rule);
13785 	}
13786 	nla_nest_end(msg, nl_rules);
13787 
13788 	return 0;
13789 }
13790 
13791 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
13792 {
13793 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13794 	struct sk_buff *msg;
13795 	void *hdr;
13796 
13797 	if (!rdev->wiphy.coalesce)
13798 		return -EOPNOTSUPP;
13799 
13800 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13801 	if (!msg)
13802 		return -ENOMEM;
13803 
13804 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13805 			     NL80211_CMD_GET_COALESCE);
13806 	if (!hdr)
13807 		goto nla_put_failure;
13808 
13809 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
13810 		goto nla_put_failure;
13811 
13812 	genlmsg_end(msg, hdr);
13813 	return genlmsg_reply(msg, info);
13814 
13815 nla_put_failure:
13816 	nlmsg_free(msg);
13817 	return -ENOBUFS;
13818 }
13819 
13820 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
13821 {
13822 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
13823 	int i, j;
13824 	struct cfg80211_coalesce_rules *rule;
13825 
13826 	if (!coalesce)
13827 		return;
13828 
13829 	for (i = 0; i < coalesce->n_rules; i++) {
13830 		rule = &coalesce->rules[i];
13831 		for (j = 0; j < rule->n_patterns; j++)
13832 			kfree(rule->patterns[j].mask);
13833 		kfree(rule->patterns);
13834 	}
13835 	kfree(coalesce->rules);
13836 	kfree(coalesce);
13837 	rdev->coalesce = NULL;
13838 }
13839 
13840 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
13841 				       struct nlattr *rule,
13842 				       struct cfg80211_coalesce_rules *new_rule)
13843 {
13844 	int err, i;
13845 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13846 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
13847 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
13848 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13849 
13850 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
13851 					  rule, nl80211_coalesce_policy, NULL);
13852 	if (err)
13853 		return err;
13854 
13855 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
13856 		new_rule->delay =
13857 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
13858 	if (new_rule->delay > coalesce->max_delay)
13859 		return -EINVAL;
13860 
13861 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
13862 		new_rule->condition =
13863 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
13864 
13865 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
13866 		return -EINVAL;
13867 
13868 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13869 			    rem)
13870 		n_patterns++;
13871 	if (n_patterns > coalesce->n_patterns)
13872 		return -EINVAL;
13873 
13874 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
13875 				     GFP_KERNEL);
13876 	if (!new_rule->patterns)
13877 		return -ENOMEM;
13878 
13879 	new_rule->n_patterns = n_patterns;
13880 	i = 0;
13881 
13882 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13883 			    rem) {
13884 		u8 *mask_pat;
13885 
13886 		err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
13887 						  pat,
13888 						  nl80211_packet_pattern_policy,
13889 						  NULL);
13890 		if (err)
13891 			return err;
13892 
13893 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
13894 		    !pat_tb[NL80211_PKTPAT_PATTERN])
13895 			return -EINVAL;
13896 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13897 		mask_len = DIV_ROUND_UP(pat_len, 8);
13898 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13899 			return -EINVAL;
13900 		if (pat_len > coalesce->pattern_max_len ||
13901 		    pat_len < coalesce->pattern_min_len)
13902 			return -EINVAL;
13903 
13904 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
13905 			pkt_offset = 0;
13906 		else
13907 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
13908 		if (pkt_offset > coalesce->max_pkt_offset)
13909 			return -EINVAL;
13910 		new_rule->patterns[i].pkt_offset = pkt_offset;
13911 
13912 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13913 		if (!mask_pat)
13914 			return -ENOMEM;
13915 
13916 		new_rule->patterns[i].mask = mask_pat;
13917 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13918 		       mask_len);
13919 
13920 		mask_pat += mask_len;
13921 		new_rule->patterns[i].pattern = mask_pat;
13922 		new_rule->patterns[i].pattern_len = pat_len;
13923 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13924 		       pat_len);
13925 		i++;
13926 	}
13927 
13928 	return 0;
13929 }
13930 
13931 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
13932 {
13933 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13934 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13935 	struct cfg80211_coalesce new_coalesce = {};
13936 	struct cfg80211_coalesce *n_coalesce;
13937 	int err, rem_rule, n_rules = 0, i, j;
13938 	struct nlattr *rule;
13939 	struct cfg80211_coalesce_rules *tmp_rule;
13940 
13941 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
13942 		return -EOPNOTSUPP;
13943 
13944 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
13945 		cfg80211_rdev_free_coalesce(rdev);
13946 		rdev_set_coalesce(rdev, NULL);
13947 		return 0;
13948 	}
13949 
13950 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
13951 			    rem_rule)
13952 		n_rules++;
13953 	if (n_rules > coalesce->n_rules)
13954 		return -EINVAL;
13955 
13956 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
13957 				     GFP_KERNEL);
13958 	if (!new_coalesce.rules)
13959 		return -ENOMEM;
13960 
13961 	new_coalesce.n_rules = n_rules;
13962 	i = 0;
13963 
13964 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
13965 			    rem_rule) {
13966 		err = nl80211_parse_coalesce_rule(rdev, rule,
13967 						  &new_coalesce.rules[i]);
13968 		if (err)
13969 			goto error;
13970 
13971 		i++;
13972 	}
13973 
13974 	err = rdev_set_coalesce(rdev, &new_coalesce);
13975 	if (err)
13976 		goto error;
13977 
13978 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
13979 	if (!n_coalesce) {
13980 		err = -ENOMEM;
13981 		goto error;
13982 	}
13983 	cfg80211_rdev_free_coalesce(rdev);
13984 	rdev->coalesce = n_coalesce;
13985 
13986 	return 0;
13987 error:
13988 	for (i = 0; i < new_coalesce.n_rules; i++) {
13989 		tmp_rule = &new_coalesce.rules[i];
13990 		for (j = 0; j < tmp_rule->n_patterns; j++)
13991 			kfree(tmp_rule->patterns[j].mask);
13992 		kfree(tmp_rule->patterns);
13993 	}
13994 	kfree(new_coalesce.rules);
13995 
13996 	return err;
13997 }
13998 
13999 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
14000 {
14001 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14002 	struct net_device *dev = info->user_ptr[1];
14003 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14004 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
14005 	struct cfg80211_gtk_rekey_data rekey_data = {};
14006 	int err;
14007 
14008 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
14009 		return -EINVAL;
14010 
14011 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
14012 					  info->attrs[NL80211_ATTR_REKEY_DATA],
14013 					  nl80211_rekey_policy, info->extack);
14014 	if (err)
14015 		return err;
14016 
14017 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
14018 	    !tb[NL80211_REKEY_DATA_KCK])
14019 		return -EINVAL;
14020 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN &&
14021 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
14022 	      nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KEK_EXT_LEN))
14023 		return -ERANGE;
14024 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN &&
14025 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
14026 	      nla_len(tb[NL80211_REKEY_DATA_KCK]) == NL80211_KCK_EXT_LEN) &&
14027 	     !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KCK_32 &&
14028 	       nla_len(tb[NL80211_REKEY_DATA_KCK]) == NL80211_KCK_EXT_LEN_32))
14029 		return -ERANGE;
14030 
14031 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
14032 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
14033 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
14034 	rekey_data.kek_len = nla_len(tb[NL80211_REKEY_DATA_KEK]);
14035 	rekey_data.kck_len = nla_len(tb[NL80211_REKEY_DATA_KCK]);
14036 	if (tb[NL80211_REKEY_DATA_AKM])
14037 		rekey_data.akm = nla_get_u32(tb[NL80211_REKEY_DATA_AKM]);
14038 
14039 	if (!wdev->connected)
14040 		return -ENOTCONN;
14041 
14042 	if (!rdev->ops->set_rekey_data)
14043 		return -EOPNOTSUPP;
14044 
14045 	return rdev_set_rekey_data(rdev, dev, &rekey_data);
14046 }
14047 
14048 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
14049 					     struct genl_info *info)
14050 {
14051 	struct net_device *dev = info->user_ptr[1];
14052 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14053 
14054 	if (wdev->iftype != NL80211_IFTYPE_AP &&
14055 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
14056 		return -EINVAL;
14057 
14058 	if (wdev->ap_unexpected_nlportid)
14059 		return -EBUSY;
14060 
14061 	wdev->ap_unexpected_nlportid = info->snd_portid;
14062 	return 0;
14063 }
14064 
14065 static int nl80211_probe_client(struct sk_buff *skb,
14066 				struct genl_info *info)
14067 {
14068 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14069 	struct net_device *dev = info->user_ptr[1];
14070 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14071 	struct sk_buff *msg;
14072 	void *hdr;
14073 	const u8 *addr;
14074 	u64 cookie;
14075 	int err;
14076 
14077 	if (wdev->iftype != NL80211_IFTYPE_AP &&
14078 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
14079 		return -EOPNOTSUPP;
14080 
14081 	if (!info->attrs[NL80211_ATTR_MAC])
14082 		return -EINVAL;
14083 
14084 	if (!rdev->ops->probe_client)
14085 		return -EOPNOTSUPP;
14086 
14087 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14088 	if (!msg)
14089 		return -ENOMEM;
14090 
14091 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14092 			     NL80211_CMD_PROBE_CLIENT);
14093 	if (!hdr) {
14094 		err = -ENOBUFS;
14095 		goto free_msg;
14096 	}
14097 
14098 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
14099 
14100 	err = rdev_probe_client(rdev, dev, addr, &cookie);
14101 	if (err)
14102 		goto free_msg;
14103 
14104 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14105 			      NL80211_ATTR_PAD))
14106 		goto nla_put_failure;
14107 
14108 	genlmsg_end(msg, hdr);
14109 
14110 	return genlmsg_reply(msg, info);
14111 
14112  nla_put_failure:
14113 	err = -ENOBUFS;
14114  free_msg:
14115 	nlmsg_free(msg);
14116 	return err;
14117 }
14118 
14119 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
14120 {
14121 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14122 	struct cfg80211_beacon_registration *reg, *nreg;
14123 	int rv;
14124 
14125 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
14126 		return -EOPNOTSUPP;
14127 
14128 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
14129 	if (!nreg)
14130 		return -ENOMEM;
14131 
14132 	/* First, check if already registered. */
14133 	spin_lock_bh(&rdev->beacon_registrations_lock);
14134 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
14135 		if (reg->nlportid == info->snd_portid) {
14136 			rv = -EALREADY;
14137 			goto out_err;
14138 		}
14139 	}
14140 	/* Add it to the list */
14141 	nreg->nlportid = info->snd_portid;
14142 	list_add(&nreg->list, &rdev->beacon_registrations);
14143 
14144 	spin_unlock_bh(&rdev->beacon_registrations_lock);
14145 
14146 	return 0;
14147 out_err:
14148 	spin_unlock_bh(&rdev->beacon_registrations_lock);
14149 	kfree(nreg);
14150 	return rv;
14151 }
14152 
14153 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
14154 {
14155 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14156 	struct wireless_dev *wdev = info->user_ptr[1];
14157 	int err;
14158 
14159 	if (!rdev->ops->start_p2p_device)
14160 		return -EOPNOTSUPP;
14161 
14162 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
14163 		return -EOPNOTSUPP;
14164 
14165 	if (wdev_running(wdev))
14166 		return 0;
14167 
14168 	if (rfkill_blocked(rdev->wiphy.rfkill))
14169 		return -ERFKILL;
14170 
14171 	err = rdev_start_p2p_device(rdev, wdev);
14172 	if (err)
14173 		return err;
14174 
14175 	wdev->is_running = true;
14176 	rdev->opencount++;
14177 
14178 	return 0;
14179 }
14180 
14181 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
14182 {
14183 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14184 	struct wireless_dev *wdev = info->user_ptr[1];
14185 
14186 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
14187 		return -EOPNOTSUPP;
14188 
14189 	if (!rdev->ops->stop_p2p_device)
14190 		return -EOPNOTSUPP;
14191 
14192 	cfg80211_stop_p2p_device(rdev, wdev);
14193 
14194 	return 0;
14195 }
14196 
14197 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
14198 {
14199 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14200 	struct wireless_dev *wdev = info->user_ptr[1];
14201 	struct cfg80211_nan_conf conf = {};
14202 	int err;
14203 
14204 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14205 		return -EOPNOTSUPP;
14206 
14207 	if (wdev_running(wdev))
14208 		return -EEXIST;
14209 
14210 	if (rfkill_blocked(rdev->wiphy.rfkill))
14211 		return -ERFKILL;
14212 
14213 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
14214 		return -EINVAL;
14215 
14216 	conf.master_pref =
14217 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14218 
14219 	if (info->attrs[NL80211_ATTR_BANDS]) {
14220 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14221 
14222 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14223 			return -EOPNOTSUPP;
14224 
14225 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14226 			return -EINVAL;
14227 
14228 		conf.bands = bands;
14229 	}
14230 
14231 	err = rdev_start_nan(rdev, wdev, &conf);
14232 	if (err)
14233 		return err;
14234 
14235 	wdev->is_running = true;
14236 	rdev->opencount++;
14237 
14238 	return 0;
14239 }
14240 
14241 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
14242 {
14243 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14244 	struct wireless_dev *wdev = info->user_ptr[1];
14245 
14246 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14247 		return -EOPNOTSUPP;
14248 
14249 	cfg80211_stop_nan(rdev, wdev);
14250 
14251 	return 0;
14252 }
14253 
14254 static int validate_nan_filter(struct nlattr *filter_attr)
14255 {
14256 	struct nlattr *attr;
14257 	int len = 0, n_entries = 0, rem;
14258 
14259 	nla_for_each_nested(attr, filter_attr, rem) {
14260 		len += nla_len(attr);
14261 		n_entries++;
14262 	}
14263 
14264 	if (len >= U8_MAX)
14265 		return -EINVAL;
14266 
14267 	return n_entries;
14268 }
14269 
14270 static int handle_nan_filter(struct nlattr *attr_filter,
14271 			     struct cfg80211_nan_func *func,
14272 			     bool tx)
14273 {
14274 	struct nlattr *attr;
14275 	int n_entries, rem, i;
14276 	struct cfg80211_nan_func_filter *filter;
14277 
14278 	n_entries = validate_nan_filter(attr_filter);
14279 	if (n_entries < 0)
14280 		return n_entries;
14281 
14282 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
14283 
14284 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
14285 	if (!filter)
14286 		return -ENOMEM;
14287 
14288 	i = 0;
14289 	nla_for_each_nested(attr, attr_filter, rem) {
14290 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
14291 		if (!filter[i].filter)
14292 			goto err;
14293 
14294 		filter[i].len = nla_len(attr);
14295 		i++;
14296 	}
14297 	if (tx) {
14298 		func->num_tx_filters = n_entries;
14299 		func->tx_filters = filter;
14300 	} else {
14301 		func->num_rx_filters = n_entries;
14302 		func->rx_filters = filter;
14303 	}
14304 
14305 	return 0;
14306 
14307 err:
14308 	i = 0;
14309 	nla_for_each_nested(attr, attr_filter, rem) {
14310 		kfree(filter[i].filter);
14311 		i++;
14312 	}
14313 	kfree(filter);
14314 	return -ENOMEM;
14315 }
14316 
14317 static int nl80211_nan_add_func(struct sk_buff *skb,
14318 				struct genl_info *info)
14319 {
14320 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14321 	struct wireless_dev *wdev = info->user_ptr[1];
14322 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
14323 	struct cfg80211_nan_func *func;
14324 	struct sk_buff *msg = NULL;
14325 	void *hdr = NULL;
14326 	int err = 0;
14327 
14328 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14329 		return -EOPNOTSUPP;
14330 
14331 	if (!wdev_running(wdev))
14332 		return -ENOTCONN;
14333 
14334 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
14335 		return -EINVAL;
14336 
14337 	err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
14338 					  info->attrs[NL80211_ATTR_NAN_FUNC],
14339 					  nl80211_nan_func_policy,
14340 					  info->extack);
14341 	if (err)
14342 		return err;
14343 
14344 	func = kzalloc(sizeof(*func), GFP_KERNEL);
14345 	if (!func)
14346 		return -ENOMEM;
14347 
14348 	func->cookie = cfg80211_assign_cookie(rdev);
14349 
14350 	if (!tb[NL80211_NAN_FUNC_TYPE]) {
14351 		err = -EINVAL;
14352 		goto out;
14353 	}
14354 
14355 
14356 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
14357 
14358 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
14359 		err = -EINVAL;
14360 		goto out;
14361 	}
14362 
14363 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
14364 	       sizeof(func->service_id));
14365 
14366 	func->close_range =
14367 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
14368 
14369 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
14370 		func->serv_spec_info_len =
14371 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
14372 		func->serv_spec_info =
14373 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
14374 				func->serv_spec_info_len,
14375 				GFP_KERNEL);
14376 		if (!func->serv_spec_info) {
14377 			err = -ENOMEM;
14378 			goto out;
14379 		}
14380 	}
14381 
14382 	if (tb[NL80211_NAN_FUNC_TTL])
14383 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
14384 
14385 	switch (func->type) {
14386 	case NL80211_NAN_FUNC_PUBLISH:
14387 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
14388 			err = -EINVAL;
14389 			goto out;
14390 		}
14391 
14392 		func->publish_type =
14393 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
14394 		func->publish_bcast =
14395 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
14396 
14397 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
14398 			func->publish_bcast) {
14399 			err = -EINVAL;
14400 			goto out;
14401 		}
14402 		break;
14403 	case NL80211_NAN_FUNC_SUBSCRIBE:
14404 		func->subscribe_active =
14405 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
14406 		break;
14407 	case NL80211_NAN_FUNC_FOLLOW_UP:
14408 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
14409 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
14410 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
14411 			err = -EINVAL;
14412 			goto out;
14413 		}
14414 
14415 		func->followup_id =
14416 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
14417 		func->followup_reqid =
14418 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
14419 		memcpy(func->followup_dest.addr,
14420 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
14421 		       sizeof(func->followup_dest.addr));
14422 		if (func->ttl) {
14423 			err = -EINVAL;
14424 			goto out;
14425 		}
14426 		break;
14427 	default:
14428 		err = -EINVAL;
14429 		goto out;
14430 	}
14431 
14432 	if (tb[NL80211_NAN_FUNC_SRF]) {
14433 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
14434 
14435 		err = nla_parse_nested_deprecated(srf_tb,
14436 						  NL80211_NAN_SRF_ATTR_MAX,
14437 						  tb[NL80211_NAN_FUNC_SRF],
14438 						  nl80211_nan_srf_policy,
14439 						  info->extack);
14440 		if (err)
14441 			goto out;
14442 
14443 		func->srf_include =
14444 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
14445 
14446 		if (srf_tb[NL80211_NAN_SRF_BF]) {
14447 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
14448 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
14449 				err = -EINVAL;
14450 				goto out;
14451 			}
14452 
14453 			func->srf_bf_len =
14454 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
14455 			func->srf_bf =
14456 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
14457 					func->srf_bf_len, GFP_KERNEL);
14458 			if (!func->srf_bf) {
14459 				err = -ENOMEM;
14460 				goto out;
14461 			}
14462 
14463 			func->srf_bf_idx =
14464 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
14465 		} else {
14466 			struct nlattr *attr, *mac_attr =
14467 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
14468 			int n_entries, rem, i = 0;
14469 
14470 			if (!mac_attr) {
14471 				err = -EINVAL;
14472 				goto out;
14473 			}
14474 
14475 			n_entries = validate_acl_mac_addrs(mac_attr);
14476 			if (n_entries <= 0) {
14477 				err = -EINVAL;
14478 				goto out;
14479 			}
14480 
14481 			func->srf_num_macs = n_entries;
14482 			func->srf_macs =
14483 				kcalloc(n_entries, sizeof(*func->srf_macs),
14484 					GFP_KERNEL);
14485 			if (!func->srf_macs) {
14486 				err = -ENOMEM;
14487 				goto out;
14488 			}
14489 
14490 			nla_for_each_nested(attr, mac_attr, rem)
14491 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
14492 				       sizeof(*func->srf_macs));
14493 		}
14494 	}
14495 
14496 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
14497 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
14498 					func, true);
14499 		if (err)
14500 			goto out;
14501 	}
14502 
14503 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
14504 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
14505 					func, false);
14506 		if (err)
14507 			goto out;
14508 	}
14509 
14510 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14511 	if (!msg) {
14512 		err = -ENOMEM;
14513 		goto out;
14514 	}
14515 
14516 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14517 			     NL80211_CMD_ADD_NAN_FUNCTION);
14518 	/* This can't really happen - we just allocated 4KB */
14519 	if (WARN_ON(!hdr)) {
14520 		err = -ENOMEM;
14521 		goto out;
14522 	}
14523 
14524 	err = rdev_add_nan_func(rdev, wdev, func);
14525 out:
14526 	if (err < 0) {
14527 		cfg80211_free_nan_func(func);
14528 		nlmsg_free(msg);
14529 		return err;
14530 	}
14531 
14532 	/* propagate the instance id and cookie to userspace  */
14533 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
14534 			      NL80211_ATTR_PAD))
14535 		goto nla_put_failure;
14536 
14537 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14538 	if (!func_attr)
14539 		goto nla_put_failure;
14540 
14541 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
14542 		       func->instance_id))
14543 		goto nla_put_failure;
14544 
14545 	nla_nest_end(msg, func_attr);
14546 
14547 	genlmsg_end(msg, hdr);
14548 	return genlmsg_reply(msg, info);
14549 
14550 nla_put_failure:
14551 	nlmsg_free(msg);
14552 	return -ENOBUFS;
14553 }
14554 
14555 static int nl80211_nan_del_func(struct sk_buff *skb,
14556 			       struct genl_info *info)
14557 {
14558 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14559 	struct wireless_dev *wdev = info->user_ptr[1];
14560 	u64 cookie;
14561 
14562 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14563 		return -EOPNOTSUPP;
14564 
14565 	if (!wdev_running(wdev))
14566 		return -ENOTCONN;
14567 
14568 	if (!info->attrs[NL80211_ATTR_COOKIE])
14569 		return -EINVAL;
14570 
14571 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
14572 
14573 	rdev_del_nan_func(rdev, wdev, cookie);
14574 
14575 	return 0;
14576 }
14577 
14578 static int nl80211_nan_change_config(struct sk_buff *skb,
14579 				     struct genl_info *info)
14580 {
14581 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14582 	struct wireless_dev *wdev = info->user_ptr[1];
14583 	struct cfg80211_nan_conf conf = {};
14584 	u32 changed = 0;
14585 
14586 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14587 		return -EOPNOTSUPP;
14588 
14589 	if (!wdev_running(wdev))
14590 		return -ENOTCONN;
14591 
14592 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
14593 		conf.master_pref =
14594 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14595 		if (conf.master_pref <= 1 || conf.master_pref == 255)
14596 			return -EINVAL;
14597 
14598 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
14599 	}
14600 
14601 	if (info->attrs[NL80211_ATTR_BANDS]) {
14602 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14603 
14604 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14605 			return -EOPNOTSUPP;
14606 
14607 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14608 			return -EINVAL;
14609 
14610 		conf.bands = bands;
14611 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
14612 	}
14613 
14614 	if (!changed)
14615 		return -EINVAL;
14616 
14617 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
14618 }
14619 
14620 void cfg80211_nan_match(struct wireless_dev *wdev,
14621 			struct cfg80211_nan_match_params *match, gfp_t gfp)
14622 {
14623 	struct wiphy *wiphy = wdev->wiphy;
14624 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14625 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
14626 	struct sk_buff *msg;
14627 	void *hdr;
14628 
14629 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
14630 		return;
14631 
14632 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14633 	if (!msg)
14634 		return;
14635 
14636 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
14637 	if (!hdr) {
14638 		nlmsg_free(msg);
14639 		return;
14640 	}
14641 
14642 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14643 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14644 					 wdev->netdev->ifindex)) ||
14645 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14646 			      NL80211_ATTR_PAD))
14647 		goto nla_put_failure;
14648 
14649 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
14650 			      NL80211_ATTR_PAD) ||
14651 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
14652 		goto nla_put_failure;
14653 
14654 	match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
14655 	if (!match_attr)
14656 		goto nla_put_failure;
14657 
14658 	local_func_attr = nla_nest_start_noflag(msg,
14659 						NL80211_NAN_MATCH_FUNC_LOCAL);
14660 	if (!local_func_attr)
14661 		goto nla_put_failure;
14662 
14663 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
14664 		goto nla_put_failure;
14665 
14666 	nla_nest_end(msg, local_func_attr);
14667 
14668 	peer_func_attr = nla_nest_start_noflag(msg,
14669 					       NL80211_NAN_MATCH_FUNC_PEER);
14670 	if (!peer_func_attr)
14671 		goto nla_put_failure;
14672 
14673 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
14674 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
14675 		goto nla_put_failure;
14676 
14677 	if (match->info && match->info_len &&
14678 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
14679 		    match->info))
14680 		goto nla_put_failure;
14681 
14682 	nla_nest_end(msg, peer_func_attr);
14683 	nla_nest_end(msg, match_attr);
14684 	genlmsg_end(msg, hdr);
14685 
14686 	if (!wdev->owner_nlportid)
14687 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14688 					msg, 0, NL80211_MCGRP_NAN, gfp);
14689 	else
14690 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14691 				wdev->owner_nlportid);
14692 
14693 	return;
14694 
14695 nla_put_failure:
14696 	nlmsg_free(msg);
14697 }
14698 EXPORT_SYMBOL(cfg80211_nan_match);
14699 
14700 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
14701 				  u8 inst_id,
14702 				  enum nl80211_nan_func_term_reason reason,
14703 				  u64 cookie, gfp_t gfp)
14704 {
14705 	struct wiphy *wiphy = wdev->wiphy;
14706 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14707 	struct sk_buff *msg;
14708 	struct nlattr *func_attr;
14709 	void *hdr;
14710 
14711 	if (WARN_ON(!inst_id))
14712 		return;
14713 
14714 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14715 	if (!msg)
14716 		return;
14717 
14718 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
14719 	if (!hdr) {
14720 		nlmsg_free(msg);
14721 		return;
14722 	}
14723 
14724 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14725 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14726 					 wdev->netdev->ifindex)) ||
14727 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14728 			      NL80211_ATTR_PAD))
14729 		goto nla_put_failure;
14730 
14731 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14732 			      NL80211_ATTR_PAD))
14733 		goto nla_put_failure;
14734 
14735 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14736 	if (!func_attr)
14737 		goto nla_put_failure;
14738 
14739 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
14740 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
14741 		goto nla_put_failure;
14742 
14743 	nla_nest_end(msg, func_attr);
14744 	genlmsg_end(msg, hdr);
14745 
14746 	if (!wdev->owner_nlportid)
14747 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14748 					msg, 0, NL80211_MCGRP_NAN, gfp);
14749 	else
14750 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14751 				wdev->owner_nlportid);
14752 
14753 	return;
14754 
14755 nla_put_failure:
14756 	nlmsg_free(msg);
14757 }
14758 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
14759 
14760 static int nl80211_get_protocol_features(struct sk_buff *skb,
14761 					 struct genl_info *info)
14762 {
14763 	void *hdr;
14764 	struct sk_buff *msg;
14765 
14766 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14767 	if (!msg)
14768 		return -ENOMEM;
14769 
14770 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14771 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
14772 	if (!hdr)
14773 		goto nla_put_failure;
14774 
14775 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
14776 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
14777 		goto nla_put_failure;
14778 
14779 	genlmsg_end(msg, hdr);
14780 	return genlmsg_reply(msg, info);
14781 
14782  nla_put_failure:
14783 	kfree_skb(msg);
14784 	return -ENOBUFS;
14785 }
14786 
14787 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
14788 {
14789 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14790 	struct cfg80211_update_ft_ies_params ft_params;
14791 	struct net_device *dev = info->user_ptr[1];
14792 
14793 	if (!rdev->ops->update_ft_ies)
14794 		return -EOPNOTSUPP;
14795 
14796 	if (!info->attrs[NL80211_ATTR_MDID] ||
14797 	    !info->attrs[NL80211_ATTR_IE])
14798 		return -EINVAL;
14799 
14800 	memset(&ft_params, 0, sizeof(ft_params));
14801 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
14802 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
14803 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
14804 
14805 	return rdev_update_ft_ies(rdev, dev, &ft_params);
14806 }
14807 
14808 static int nl80211_crit_protocol_start(struct sk_buff *skb,
14809 				       struct genl_info *info)
14810 {
14811 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14812 	struct wireless_dev *wdev = info->user_ptr[1];
14813 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
14814 	u16 duration;
14815 	int ret;
14816 
14817 	if (!rdev->ops->crit_proto_start)
14818 		return -EOPNOTSUPP;
14819 
14820 	if (WARN_ON(!rdev->ops->crit_proto_stop))
14821 		return -EINVAL;
14822 
14823 	if (rdev->crit_proto_nlportid)
14824 		return -EBUSY;
14825 
14826 	/* determine protocol if provided */
14827 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
14828 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
14829 
14830 	if (proto >= NUM_NL80211_CRIT_PROTO)
14831 		return -EINVAL;
14832 
14833 	/* timeout must be provided */
14834 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
14835 		return -EINVAL;
14836 
14837 	duration =
14838 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
14839 
14840 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
14841 	if (!ret)
14842 		rdev->crit_proto_nlportid = info->snd_portid;
14843 
14844 	return ret;
14845 }
14846 
14847 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
14848 				      struct genl_info *info)
14849 {
14850 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14851 	struct wireless_dev *wdev = info->user_ptr[1];
14852 
14853 	if (!rdev->ops->crit_proto_stop)
14854 		return -EOPNOTSUPP;
14855 
14856 	if (rdev->crit_proto_nlportid) {
14857 		rdev->crit_proto_nlportid = 0;
14858 		rdev_crit_proto_stop(rdev, wdev);
14859 	}
14860 	return 0;
14861 }
14862 
14863 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
14864 				       struct nlattr *attr,
14865 				       struct netlink_ext_ack *extack)
14866 {
14867 	if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
14868 		if (attr->nla_type & NLA_F_NESTED) {
14869 			NL_SET_ERR_MSG_ATTR(extack, attr,
14870 					    "unexpected nested data");
14871 			return -EINVAL;
14872 		}
14873 
14874 		return 0;
14875 	}
14876 
14877 	if (!(attr->nla_type & NLA_F_NESTED)) {
14878 		NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
14879 		return -EINVAL;
14880 	}
14881 
14882 	return nla_validate_nested(attr, vcmd->maxattr, vcmd->policy, extack);
14883 }
14884 
14885 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
14886 {
14887 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14888 	struct wireless_dev *wdev =
14889 		__cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
14890 					   info->attrs);
14891 	int i, err;
14892 	u32 vid, subcmd;
14893 
14894 	if (!rdev->wiphy.vendor_commands)
14895 		return -EOPNOTSUPP;
14896 
14897 	if (IS_ERR(wdev)) {
14898 		err = PTR_ERR(wdev);
14899 		if (err != -EINVAL)
14900 			return err;
14901 		wdev = NULL;
14902 	} else if (wdev->wiphy != &rdev->wiphy) {
14903 		return -EINVAL;
14904 	}
14905 
14906 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
14907 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
14908 		return -EINVAL;
14909 
14910 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
14911 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
14912 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
14913 		const struct wiphy_vendor_command *vcmd;
14914 		void *data = NULL;
14915 		int len = 0;
14916 
14917 		vcmd = &rdev->wiphy.vendor_commands[i];
14918 
14919 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
14920 			continue;
14921 
14922 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
14923 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
14924 			if (!wdev)
14925 				return -EINVAL;
14926 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
14927 			    !wdev->netdev)
14928 				return -EINVAL;
14929 
14930 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
14931 				if (!wdev_running(wdev))
14932 					return -ENETDOWN;
14933 			}
14934 		} else {
14935 			wdev = NULL;
14936 		}
14937 
14938 		if (!vcmd->doit)
14939 			return -EOPNOTSUPP;
14940 
14941 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
14942 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
14943 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
14944 
14945 			err = nl80211_vendor_check_policy(vcmd,
14946 					info->attrs[NL80211_ATTR_VENDOR_DATA],
14947 					info->extack);
14948 			if (err)
14949 				return err;
14950 		}
14951 
14952 		rdev->cur_cmd_info = info;
14953 		err = vcmd->doit(&rdev->wiphy, wdev, data, len);
14954 		rdev->cur_cmd_info = NULL;
14955 		return err;
14956 	}
14957 
14958 	return -EOPNOTSUPP;
14959 }
14960 
14961 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
14962 				       struct netlink_callback *cb,
14963 				       struct cfg80211_registered_device **rdev,
14964 				       struct wireless_dev **wdev)
14965 {
14966 	struct nlattr **attrbuf;
14967 	u32 vid, subcmd;
14968 	unsigned int i;
14969 	int vcmd_idx = -1;
14970 	int err;
14971 	void *data = NULL;
14972 	unsigned int data_len = 0;
14973 
14974 	if (cb->args[0]) {
14975 		/* subtract the 1 again here */
14976 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
14977 		struct wireless_dev *tmp;
14978 
14979 		if (!wiphy)
14980 			return -ENODEV;
14981 		*rdev = wiphy_to_rdev(wiphy);
14982 		*wdev = NULL;
14983 
14984 		if (cb->args[1]) {
14985 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
14986 				if (tmp->identifier == cb->args[1] - 1) {
14987 					*wdev = tmp;
14988 					break;
14989 				}
14990 			}
14991 		}
14992 
14993 		/* keep rtnl locked in successful case */
14994 		return 0;
14995 	}
14996 
14997 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
14998 	if (!attrbuf)
14999 		return -ENOMEM;
15000 
15001 	err = nlmsg_parse_deprecated(cb->nlh,
15002 				     GENL_HDRLEN + nl80211_fam.hdrsize,
15003 				     attrbuf, nl80211_fam.maxattr,
15004 				     nl80211_policy, NULL);
15005 	if (err)
15006 		goto out;
15007 
15008 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
15009 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
15010 		err = -EINVAL;
15011 		goto out;
15012 	}
15013 
15014 	*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(skb->sk), attrbuf);
15015 	if (IS_ERR(*wdev))
15016 		*wdev = NULL;
15017 
15018 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
15019 	if (IS_ERR(*rdev)) {
15020 		err = PTR_ERR(*rdev);
15021 		goto out;
15022 	}
15023 
15024 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
15025 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
15026 
15027 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
15028 		const struct wiphy_vendor_command *vcmd;
15029 
15030 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
15031 
15032 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
15033 			continue;
15034 
15035 		if (!vcmd->dumpit) {
15036 			err = -EOPNOTSUPP;
15037 			goto out;
15038 		}
15039 
15040 		vcmd_idx = i;
15041 		break;
15042 	}
15043 
15044 	if (vcmd_idx < 0) {
15045 		err = -EOPNOTSUPP;
15046 		goto out;
15047 	}
15048 
15049 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
15050 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
15051 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
15052 
15053 		err = nl80211_vendor_check_policy(
15054 				&(*rdev)->wiphy.vendor_commands[vcmd_idx],
15055 				attrbuf[NL80211_ATTR_VENDOR_DATA],
15056 				cb->extack);
15057 		if (err)
15058 			goto out;
15059 	}
15060 
15061 	/* 0 is the first index - add 1 to parse only once */
15062 	cb->args[0] = (*rdev)->wiphy_idx + 1;
15063 	/* add 1 to know if it was NULL */
15064 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
15065 	cb->args[2] = vcmd_idx;
15066 	cb->args[3] = (unsigned long)data;
15067 	cb->args[4] = data_len;
15068 
15069 	/* keep rtnl locked in successful case */
15070 	err = 0;
15071 out:
15072 	kfree(attrbuf);
15073 	return err;
15074 }
15075 
15076 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
15077 				   struct netlink_callback *cb)
15078 {
15079 	struct cfg80211_registered_device *rdev;
15080 	struct wireless_dev *wdev;
15081 	unsigned int vcmd_idx;
15082 	const struct wiphy_vendor_command *vcmd;
15083 	void *data;
15084 	int data_len;
15085 	int err;
15086 	struct nlattr *vendor_data;
15087 
15088 	rtnl_lock();
15089 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
15090 	if (err)
15091 		goto out;
15092 
15093 	vcmd_idx = cb->args[2];
15094 	data = (void *)cb->args[3];
15095 	data_len = cb->args[4];
15096 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
15097 
15098 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
15099 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
15100 		if (!wdev) {
15101 			err = -EINVAL;
15102 			goto out;
15103 		}
15104 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
15105 		    !wdev->netdev) {
15106 			err = -EINVAL;
15107 			goto out;
15108 		}
15109 
15110 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
15111 			if (!wdev_running(wdev)) {
15112 				err = -ENETDOWN;
15113 				goto out;
15114 			}
15115 		}
15116 	}
15117 
15118 	while (1) {
15119 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
15120 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
15121 					   NL80211_CMD_VENDOR);
15122 		if (!hdr)
15123 			break;
15124 
15125 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15126 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
15127 					       wdev_id(wdev),
15128 					       NL80211_ATTR_PAD))) {
15129 			genlmsg_cancel(skb, hdr);
15130 			break;
15131 		}
15132 
15133 		vendor_data = nla_nest_start_noflag(skb,
15134 						    NL80211_ATTR_VENDOR_DATA);
15135 		if (!vendor_data) {
15136 			genlmsg_cancel(skb, hdr);
15137 			break;
15138 		}
15139 
15140 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
15141 				   (unsigned long *)&cb->args[5]);
15142 		nla_nest_end(skb, vendor_data);
15143 
15144 		if (err == -ENOBUFS || err == -ENOENT) {
15145 			genlmsg_cancel(skb, hdr);
15146 			break;
15147 		} else if (err <= 0) {
15148 			genlmsg_cancel(skb, hdr);
15149 			goto out;
15150 		}
15151 
15152 		genlmsg_end(skb, hdr);
15153 	}
15154 
15155 	err = skb->len;
15156  out:
15157 	rtnl_unlock();
15158 	return err;
15159 }
15160 
15161 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
15162 					   enum nl80211_commands cmd,
15163 					   enum nl80211_attrs attr,
15164 					   int approxlen)
15165 {
15166 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15167 
15168 	if (WARN_ON(!rdev->cur_cmd_info))
15169 		return NULL;
15170 
15171 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
15172 					   rdev->cur_cmd_info->snd_portid,
15173 					   rdev->cur_cmd_info->snd_seq,
15174 					   cmd, attr, NULL, GFP_KERNEL);
15175 }
15176 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
15177 
15178 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
15179 {
15180 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
15181 	void *hdr = ((void **)skb->cb)[1];
15182 	struct nlattr *data = ((void **)skb->cb)[2];
15183 
15184 	/* clear CB data for netlink core to own from now on */
15185 	memset(skb->cb, 0, sizeof(skb->cb));
15186 
15187 	if (WARN_ON(!rdev->cur_cmd_info)) {
15188 		kfree_skb(skb);
15189 		return -EINVAL;
15190 	}
15191 
15192 	nla_nest_end(skb, data);
15193 	genlmsg_end(skb, hdr);
15194 	return genlmsg_reply(skb, rdev->cur_cmd_info);
15195 }
15196 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
15197 
15198 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
15199 {
15200 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15201 
15202 	if (WARN_ON(!rdev->cur_cmd_info))
15203 		return 0;
15204 
15205 	return rdev->cur_cmd_info->snd_portid;
15206 }
15207 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
15208 
15209 static int nl80211_set_qos_map(struct sk_buff *skb,
15210 			       struct genl_info *info)
15211 {
15212 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15213 	struct cfg80211_qos_map *qos_map = NULL;
15214 	struct net_device *dev = info->user_ptr[1];
15215 	u8 *pos, len, num_des, des_len, des;
15216 	int ret;
15217 
15218 	if (!rdev->ops->set_qos_map)
15219 		return -EOPNOTSUPP;
15220 
15221 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
15222 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
15223 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
15224 
15225 		if (len % 2)
15226 			return -EINVAL;
15227 
15228 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
15229 		if (!qos_map)
15230 			return -ENOMEM;
15231 
15232 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
15233 		if (num_des) {
15234 			des_len = num_des *
15235 				sizeof(struct cfg80211_dscp_exception);
15236 			memcpy(qos_map->dscp_exception, pos, des_len);
15237 			qos_map->num_des = num_des;
15238 			for (des = 0; des < num_des; des++) {
15239 				if (qos_map->dscp_exception[des].up > 7) {
15240 					kfree(qos_map);
15241 					return -EINVAL;
15242 				}
15243 			}
15244 			pos += des_len;
15245 		}
15246 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
15247 	}
15248 
15249 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
15250 	if (!ret)
15251 		ret = rdev_set_qos_map(rdev, dev, qos_map);
15252 
15253 	kfree(qos_map);
15254 	return ret;
15255 }
15256 
15257 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
15258 {
15259 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15260 	struct net_device *dev = info->user_ptr[1];
15261 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15262 	const u8 *peer;
15263 	u8 tsid, up;
15264 	u16 admitted_time = 0;
15265 
15266 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
15267 		return -EOPNOTSUPP;
15268 
15269 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
15270 	    !info->attrs[NL80211_ATTR_USER_PRIO])
15271 		return -EINVAL;
15272 
15273 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15274 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
15275 
15276 	/* WMM uses TIDs 0-7 even for TSPEC */
15277 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
15278 		/* TODO: handle 802.11 TSPEC/admission control
15279 		 * need more attributes for that (e.g. BA session requirement);
15280 		 * change the WMM adminssion test above to allow both then
15281 		 */
15282 		return -EINVAL;
15283 	}
15284 
15285 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15286 
15287 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
15288 		admitted_time =
15289 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
15290 		if (!admitted_time)
15291 			return -EINVAL;
15292 	}
15293 
15294 	switch (wdev->iftype) {
15295 	case NL80211_IFTYPE_STATION:
15296 	case NL80211_IFTYPE_P2P_CLIENT:
15297 		if (wdev->connected)
15298 			break;
15299 		return -ENOTCONN;
15300 	default:
15301 		return -EOPNOTSUPP;
15302 	}
15303 
15304 	return rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
15305 }
15306 
15307 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
15308 {
15309 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15310 	struct net_device *dev = info->user_ptr[1];
15311 	const u8 *peer;
15312 	u8 tsid;
15313 
15314 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
15315 		return -EINVAL;
15316 
15317 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15318 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15319 
15320 	return rdev_del_tx_ts(rdev, dev, tsid, peer);
15321 }
15322 
15323 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
15324 				       struct genl_info *info)
15325 {
15326 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15327 	struct net_device *dev = info->user_ptr[1];
15328 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15329 	struct cfg80211_chan_def chandef = {};
15330 	const u8 *addr;
15331 	u8 oper_class;
15332 	int err;
15333 
15334 	if (!rdev->ops->tdls_channel_switch ||
15335 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15336 		return -EOPNOTSUPP;
15337 
15338 	switch (dev->ieee80211_ptr->iftype) {
15339 	case NL80211_IFTYPE_STATION:
15340 	case NL80211_IFTYPE_P2P_CLIENT:
15341 		break;
15342 	default:
15343 		return -EOPNOTSUPP;
15344 	}
15345 
15346 	if (!info->attrs[NL80211_ATTR_MAC] ||
15347 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
15348 		return -EINVAL;
15349 
15350 	err = nl80211_parse_chandef(rdev, info, &chandef);
15351 	if (err)
15352 		return err;
15353 
15354 	/*
15355 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
15356 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
15357 	 * specification is not defined for them.
15358 	 */
15359 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
15360 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
15361 	    chandef.width != NL80211_CHAN_WIDTH_20)
15362 		return -EINVAL;
15363 
15364 	/* we will be active on the TDLS link */
15365 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
15366 					   wdev->iftype))
15367 		return -EINVAL;
15368 
15369 	/* don't allow switching to DFS channels */
15370 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
15371 		return -EINVAL;
15372 
15373 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15374 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
15375 
15376 	return rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
15377 }
15378 
15379 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
15380 					      struct genl_info *info)
15381 {
15382 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15383 	struct net_device *dev = info->user_ptr[1];
15384 	const u8 *addr;
15385 
15386 	if (!rdev->ops->tdls_channel_switch ||
15387 	    !rdev->ops->tdls_cancel_channel_switch ||
15388 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15389 		return -EOPNOTSUPP;
15390 
15391 	switch (dev->ieee80211_ptr->iftype) {
15392 	case NL80211_IFTYPE_STATION:
15393 	case NL80211_IFTYPE_P2P_CLIENT:
15394 		break;
15395 	default:
15396 		return -EOPNOTSUPP;
15397 	}
15398 
15399 	if (!info->attrs[NL80211_ATTR_MAC])
15400 		return -EINVAL;
15401 
15402 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15403 
15404 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
15405 
15406 	return 0;
15407 }
15408 
15409 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
15410 					    struct genl_info *info)
15411 {
15412 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15413 	struct net_device *dev = info->user_ptr[1];
15414 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15415 	const struct nlattr *nla;
15416 	bool enabled;
15417 
15418 	if (!rdev->ops->set_multicast_to_unicast)
15419 		return -EOPNOTSUPP;
15420 
15421 	if (wdev->iftype != NL80211_IFTYPE_AP &&
15422 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
15423 		return -EOPNOTSUPP;
15424 
15425 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
15426 	enabled = nla_get_flag(nla);
15427 
15428 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
15429 }
15430 
15431 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
15432 {
15433 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15434 	struct net_device *dev = info->user_ptr[1];
15435 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15436 	struct cfg80211_pmk_conf pmk_conf = {};
15437 
15438 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15439 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15440 		return -EOPNOTSUPP;
15441 
15442 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15443 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15444 		return -EOPNOTSUPP;
15445 
15446 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
15447 		return -EINVAL;
15448 
15449 	if (!wdev->connected)
15450 		return -ENOTCONN;
15451 
15452 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15453 	if (memcmp(pmk_conf.aa, wdev->u.client.connected_addr, ETH_ALEN))
15454 		return -EINVAL;
15455 
15456 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
15457 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
15458 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
15459 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192)
15460 		return -EINVAL;
15461 
15462 	if (info->attrs[NL80211_ATTR_PMKR0_NAME])
15463 		pmk_conf.pmk_r0_name =
15464 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
15465 
15466 	return rdev_set_pmk(rdev, dev, &pmk_conf);
15467 }
15468 
15469 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
15470 {
15471 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15472 	struct net_device *dev = info->user_ptr[1];
15473 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15474 	const u8 *aa;
15475 
15476 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15477 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15478 		return -EOPNOTSUPP;
15479 
15480 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15481 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15482 		return -EOPNOTSUPP;
15483 
15484 	if (!info->attrs[NL80211_ATTR_MAC])
15485 		return -EINVAL;
15486 
15487 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15488 	return rdev_del_pmk(rdev, dev, aa);
15489 }
15490 
15491 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
15492 {
15493 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15494 	struct net_device *dev = info->user_ptr[1];
15495 	struct cfg80211_external_auth_params params;
15496 
15497 	if (!rdev->ops->external_auth)
15498 		return -EOPNOTSUPP;
15499 
15500 	if (!info->attrs[NL80211_ATTR_SSID] &&
15501 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
15502 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
15503 		return -EINVAL;
15504 
15505 	if (!info->attrs[NL80211_ATTR_BSSID])
15506 		return -EINVAL;
15507 
15508 	if (!info->attrs[NL80211_ATTR_STATUS_CODE])
15509 		return -EINVAL;
15510 
15511 	memset(&params, 0, sizeof(params));
15512 
15513 	if (info->attrs[NL80211_ATTR_SSID]) {
15514 		params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
15515 		if (params.ssid.ssid_len == 0)
15516 			return -EINVAL;
15517 		memcpy(params.ssid.ssid,
15518 		       nla_data(info->attrs[NL80211_ATTR_SSID]),
15519 		       params.ssid.ssid_len);
15520 	}
15521 
15522 	memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
15523 	       ETH_ALEN);
15524 
15525 	params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15526 
15527 	if (info->attrs[NL80211_ATTR_PMKID])
15528 		params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
15529 
15530 	return rdev_external_auth(rdev, dev, &params);
15531 }
15532 
15533 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
15534 {
15535 	bool dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
15536 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15537 	struct net_device *dev = info->user_ptr[1];
15538 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15539 	const u8 *buf;
15540 	size_t len;
15541 	u8 *dest;
15542 	u16 proto;
15543 	bool noencrypt;
15544 	u64 cookie = 0;
15545 	int link_id;
15546 	int err;
15547 
15548 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15549 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
15550 		return -EOPNOTSUPP;
15551 
15552 	if (!rdev->ops->tx_control_port)
15553 		return -EOPNOTSUPP;
15554 
15555 	if (!info->attrs[NL80211_ATTR_FRAME] ||
15556 	    !info->attrs[NL80211_ATTR_MAC] ||
15557 	    !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
15558 		GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
15559 		return -EINVAL;
15560 	}
15561 
15562 	switch (wdev->iftype) {
15563 	case NL80211_IFTYPE_AP:
15564 	case NL80211_IFTYPE_P2P_GO:
15565 	case NL80211_IFTYPE_MESH_POINT:
15566 		break;
15567 	case NL80211_IFTYPE_ADHOC:
15568 		if (wdev->u.ibss.current_bss)
15569 			break;
15570 		return -ENOTCONN;
15571 	case NL80211_IFTYPE_STATION:
15572 	case NL80211_IFTYPE_P2P_CLIENT:
15573 		if (wdev->connected)
15574 			break;
15575 		return -ENOTCONN;
15576 	default:
15577 		return -EOPNOTSUPP;
15578 	}
15579 
15580 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15581 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15582 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15583 	proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
15584 	noencrypt =
15585 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
15586 
15587 	link_id = nl80211_link_id_or_invalid(info->attrs);
15588 
15589 	err = rdev_tx_control_port(rdev, dev, buf, len,
15590 				   dest, cpu_to_be16(proto), noencrypt, link_id,
15591 				   dont_wait_for_ack ? NULL : &cookie);
15592 	if (!err && !dont_wait_for_ack)
15593 		nl_set_extack_cookie_u64(info->extack, cookie);
15594 	return err;
15595 }
15596 
15597 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
15598 					   struct genl_info *info)
15599 {
15600 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15601 	struct net_device *dev = info->user_ptr[1];
15602 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15603 	struct cfg80211_ftm_responder_stats ftm_stats = {};
15604 	unsigned int link_id = nl80211_link_id(info->attrs);
15605 	struct sk_buff *msg;
15606 	void *hdr;
15607 	struct nlattr *ftm_stats_attr;
15608 	int err;
15609 
15610 	if (wdev->iftype != NL80211_IFTYPE_AP ||
15611 	    !wdev->links[link_id].ap.beacon_interval)
15612 		return -EOPNOTSUPP;
15613 
15614 	err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
15615 	if (err)
15616 		return err;
15617 
15618 	if (!ftm_stats.filled)
15619 		return -ENODATA;
15620 
15621 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15622 	if (!msg)
15623 		return -ENOMEM;
15624 
15625 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
15626 			     NL80211_CMD_GET_FTM_RESPONDER_STATS);
15627 	if (!hdr)
15628 		goto nla_put_failure;
15629 
15630 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15631 		goto nla_put_failure;
15632 
15633 	ftm_stats_attr = nla_nest_start_noflag(msg,
15634 					       NL80211_ATTR_FTM_RESPONDER_STATS);
15635 	if (!ftm_stats_attr)
15636 		goto nla_put_failure;
15637 
15638 #define SET_FTM(field, name, type)					 \
15639 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15640 	    nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,		 \
15641 			     ftm_stats.field))				 \
15642 		goto nla_put_failure; } while (0)
15643 #define SET_FTM_U64(field, name)					 \
15644 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15645 	    nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,		 \
15646 			      ftm_stats.field, NL80211_FTM_STATS_PAD))	 \
15647 		goto nla_put_failure; } while (0)
15648 
15649 	SET_FTM(success_num, SUCCESS_NUM, u32);
15650 	SET_FTM(partial_num, PARTIAL_NUM, u32);
15651 	SET_FTM(failed_num, FAILED_NUM, u32);
15652 	SET_FTM(asap_num, ASAP_NUM, u32);
15653 	SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
15654 	SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
15655 	SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
15656 	SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
15657 	SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
15658 #undef SET_FTM
15659 
15660 	nla_nest_end(msg, ftm_stats_attr);
15661 
15662 	genlmsg_end(msg, hdr);
15663 	return genlmsg_reply(msg, info);
15664 
15665 nla_put_failure:
15666 	nlmsg_free(msg);
15667 	return -ENOBUFS;
15668 }
15669 
15670 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
15671 {
15672 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15673 	struct cfg80211_update_owe_info owe_info;
15674 	struct net_device *dev = info->user_ptr[1];
15675 
15676 	if (!rdev->ops->update_owe_info)
15677 		return -EOPNOTSUPP;
15678 
15679 	if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
15680 	    !info->attrs[NL80211_ATTR_MAC])
15681 		return -EINVAL;
15682 
15683 	memset(&owe_info, 0, sizeof(owe_info));
15684 	owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15685 	nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
15686 
15687 	if (info->attrs[NL80211_ATTR_IE]) {
15688 		owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
15689 		owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
15690 	}
15691 
15692 	return rdev_update_owe_info(rdev, dev, &owe_info);
15693 }
15694 
15695 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
15696 {
15697 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15698 	struct net_device *dev = info->user_ptr[1];
15699 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15700 	struct station_info sinfo = {};
15701 	const u8 *buf;
15702 	size_t len;
15703 	u8 *dest;
15704 	int err;
15705 
15706 	if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
15707 		return -EOPNOTSUPP;
15708 
15709 	if (!info->attrs[NL80211_ATTR_MAC] ||
15710 	    !info->attrs[NL80211_ATTR_FRAME]) {
15711 		GENL_SET_ERR_MSG(info, "Frame or MAC missing");
15712 		return -EINVAL;
15713 	}
15714 
15715 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
15716 		return -EOPNOTSUPP;
15717 
15718 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15719 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15720 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15721 
15722 	if (len < sizeof(struct ethhdr))
15723 		return -EINVAL;
15724 
15725 	if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
15726 	    !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
15727 		return -EINVAL;
15728 
15729 	err = rdev_get_station(rdev, dev, dest, &sinfo);
15730 	if (err)
15731 		return err;
15732 
15733 	cfg80211_sinfo_release_content(&sinfo);
15734 
15735 	return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
15736 }
15737 
15738 static int parse_tid_conf(struct cfg80211_registered_device *rdev,
15739 			  struct nlattr *attrs[], struct net_device *dev,
15740 			  struct cfg80211_tid_cfg *tid_conf,
15741 			  struct genl_info *info, const u8 *peer,
15742 			  unsigned int link_id)
15743 {
15744 	struct netlink_ext_ack *extack = info->extack;
15745 	u64 mask;
15746 	int err;
15747 
15748 	if (!attrs[NL80211_TID_CONFIG_ATTR_TIDS])
15749 		return -EINVAL;
15750 
15751 	tid_conf->config_override =
15752 			nla_get_flag(attrs[NL80211_TID_CONFIG_ATTR_OVERRIDE]);
15753 	tid_conf->tids = nla_get_u16(attrs[NL80211_TID_CONFIG_ATTR_TIDS]);
15754 
15755 	if (tid_conf->config_override) {
15756 		if (rdev->ops->reset_tid_config) {
15757 			err = rdev_reset_tid_config(rdev, dev, peer,
15758 						    tid_conf->tids);
15759 			if (err)
15760 				return err;
15761 		} else {
15762 			return -EINVAL;
15763 		}
15764 	}
15765 
15766 	if (attrs[NL80211_TID_CONFIG_ATTR_NOACK]) {
15767 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_NOACK);
15768 		tid_conf->noack =
15769 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_NOACK]);
15770 	}
15771 
15772 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]) {
15773 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT);
15774 		tid_conf->retry_short =
15775 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]);
15776 
15777 		if (tid_conf->retry_short > rdev->wiphy.max_data_retry_count)
15778 			return -EINVAL;
15779 	}
15780 
15781 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]) {
15782 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
15783 		tid_conf->retry_long =
15784 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]);
15785 
15786 		if (tid_conf->retry_long > rdev->wiphy.max_data_retry_count)
15787 			return -EINVAL;
15788 	}
15789 
15790 	if (attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]) {
15791 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
15792 		tid_conf->ampdu =
15793 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]);
15794 	}
15795 
15796 	if (attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]) {
15797 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
15798 		tid_conf->rtscts =
15799 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]);
15800 	}
15801 
15802 	if (attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]) {
15803 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
15804 		tid_conf->amsdu =
15805 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]);
15806 	}
15807 
15808 	if (attrs[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE]) {
15809 		u32 idx = NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE, attr;
15810 
15811 		tid_conf->txrate_type = nla_get_u8(attrs[idx]);
15812 
15813 		if (tid_conf->txrate_type != NL80211_TX_RATE_AUTOMATIC) {
15814 			attr = NL80211_TID_CONFIG_ATTR_TX_RATE;
15815 			err = nl80211_parse_tx_bitrate_mask(info, attrs, attr,
15816 						    &tid_conf->txrate_mask, dev,
15817 						    true, link_id);
15818 			if (err)
15819 				return err;
15820 
15821 			tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE);
15822 		}
15823 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
15824 	}
15825 
15826 	if (peer)
15827 		mask = rdev->wiphy.tid_config_support.peer;
15828 	else
15829 		mask = rdev->wiphy.tid_config_support.vif;
15830 
15831 	if (tid_conf->mask & ~mask) {
15832 		NL_SET_ERR_MSG(extack, "unsupported TID configuration");
15833 		return -ENOTSUPP;
15834 	}
15835 
15836 	return 0;
15837 }
15838 
15839 static int nl80211_set_tid_config(struct sk_buff *skb,
15840 				  struct genl_info *info)
15841 {
15842 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15843 	struct nlattr *attrs[NL80211_TID_CONFIG_ATTR_MAX + 1];
15844 	unsigned int link_id = nl80211_link_id(info->attrs);
15845 	struct net_device *dev = info->user_ptr[1];
15846 	struct cfg80211_tid_config *tid_config;
15847 	struct nlattr *tid;
15848 	int conf_idx = 0, rem_conf;
15849 	int ret = -EINVAL;
15850 	u32 num_conf = 0;
15851 
15852 	if (!info->attrs[NL80211_ATTR_TID_CONFIG])
15853 		return -EINVAL;
15854 
15855 	if (!rdev->ops->set_tid_config)
15856 		return -EOPNOTSUPP;
15857 
15858 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
15859 			    rem_conf)
15860 		num_conf++;
15861 
15862 	tid_config = kzalloc(struct_size(tid_config, tid_conf, num_conf),
15863 			     GFP_KERNEL);
15864 	if (!tid_config)
15865 		return -ENOMEM;
15866 
15867 	tid_config->n_tid_conf = num_conf;
15868 
15869 	if (info->attrs[NL80211_ATTR_MAC])
15870 		tid_config->peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15871 
15872 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
15873 			    rem_conf) {
15874 		ret = nla_parse_nested(attrs, NL80211_TID_CONFIG_ATTR_MAX,
15875 				       tid, NULL, NULL);
15876 
15877 		if (ret)
15878 			goto bad_tid_conf;
15879 
15880 		ret = parse_tid_conf(rdev, attrs, dev,
15881 				     &tid_config->tid_conf[conf_idx],
15882 				     info, tid_config->peer, link_id);
15883 		if (ret)
15884 			goto bad_tid_conf;
15885 
15886 		conf_idx++;
15887 	}
15888 
15889 	ret = rdev_set_tid_config(rdev, dev, tid_config);
15890 
15891 bad_tid_conf:
15892 	kfree(tid_config);
15893 	return ret;
15894 }
15895 
15896 static int nl80211_color_change(struct sk_buff *skb, struct genl_info *info)
15897 {
15898 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15899 	struct cfg80211_color_change_settings params = {};
15900 	struct net_device *dev = info->user_ptr[1];
15901 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15902 	struct nlattr **tb;
15903 	u16 offset;
15904 	int err;
15905 
15906 	if (!rdev->ops->color_change)
15907 		return -EOPNOTSUPP;
15908 
15909 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15910 				     NL80211_EXT_FEATURE_BSS_COLOR))
15911 		return -EOPNOTSUPP;
15912 
15913 	if (wdev->iftype != NL80211_IFTYPE_AP)
15914 		return -EOPNOTSUPP;
15915 
15916 	if (!info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT] ||
15917 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR] ||
15918 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS])
15919 		return -EINVAL;
15920 
15921 	params.count = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT]);
15922 	params.color = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR]);
15923 
15924 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_next,
15925 				   info->extack);
15926 	if (err)
15927 		return err;
15928 
15929 	tb = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*tb), GFP_KERNEL);
15930 	if (!tb)
15931 		return -ENOMEM;
15932 
15933 	err = nla_parse_nested(tb, NL80211_ATTR_MAX,
15934 			       info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS],
15935 			       nl80211_policy, info->extack);
15936 	if (err)
15937 		goto out;
15938 
15939 	err = nl80211_parse_beacon(rdev, tb, &params.beacon_color_change,
15940 				   info->extack);
15941 	if (err)
15942 		goto out;
15943 
15944 	if (!tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
15945 		err = -EINVAL;
15946 		goto out;
15947 	}
15948 
15949 	if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) != sizeof(u16)) {
15950 		err = -EINVAL;
15951 		goto out;
15952 	}
15953 
15954 	offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
15955 	if (offset >= params.beacon_color_change.tail_len) {
15956 		err = -EINVAL;
15957 		goto out;
15958 	}
15959 
15960 	if (params.beacon_color_change.tail[offset] != params.count) {
15961 		err = -EINVAL;
15962 		goto out;
15963 	}
15964 
15965 	params.counter_offset_beacon = offset;
15966 
15967 	if (tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
15968 		if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) !=
15969 		    sizeof(u16)) {
15970 			err = -EINVAL;
15971 			goto out;
15972 		}
15973 
15974 		offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
15975 		if (offset >= params.beacon_color_change.probe_resp_len) {
15976 			err = -EINVAL;
15977 			goto out;
15978 		}
15979 
15980 		if (params.beacon_color_change.probe_resp[offset] !=
15981 		    params.count) {
15982 			err = -EINVAL;
15983 			goto out;
15984 		}
15985 
15986 		params.counter_offset_presp = offset;
15987 	}
15988 
15989 	err = rdev_color_change(rdev, dev, &params);
15990 
15991 out:
15992 	kfree(params.beacon_next.mbssid_ies);
15993 	kfree(params.beacon_color_change.mbssid_ies);
15994 	kfree(params.beacon_next.rnr_ies);
15995 	kfree(params.beacon_color_change.rnr_ies);
15996 	kfree(tb);
15997 	return err;
15998 }
15999 
16000 static int nl80211_set_fils_aad(struct sk_buff *skb,
16001 				struct genl_info *info)
16002 {
16003 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16004 	struct net_device *dev = info->user_ptr[1];
16005 	struct cfg80211_fils_aad fils_aad = {};
16006 	u8 *nonces;
16007 
16008 	if (!info->attrs[NL80211_ATTR_MAC] ||
16009 	    !info->attrs[NL80211_ATTR_FILS_KEK] ||
16010 	    !info->attrs[NL80211_ATTR_FILS_NONCES])
16011 		return -EINVAL;
16012 
16013 	fils_aad.macaddr = nla_data(info->attrs[NL80211_ATTR_MAC]);
16014 	fils_aad.kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
16015 	fils_aad.kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
16016 	nonces = nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
16017 	fils_aad.snonce = nonces;
16018 	fils_aad.anonce = nonces + FILS_NONCE_LEN;
16019 
16020 	return rdev_set_fils_aad(rdev, dev, &fils_aad);
16021 }
16022 
16023 static int nl80211_add_link(struct sk_buff *skb, struct genl_info *info)
16024 {
16025 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16026 	unsigned int link_id = nl80211_link_id(info->attrs);
16027 	struct net_device *dev = info->user_ptr[1];
16028 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16029 	int ret;
16030 
16031 	if (!(wdev->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
16032 		return -EINVAL;
16033 
16034 	switch (wdev->iftype) {
16035 	case NL80211_IFTYPE_AP:
16036 		break;
16037 	default:
16038 		return -EINVAL;
16039 	}
16040 
16041 	if (!info->attrs[NL80211_ATTR_MAC] ||
16042 	    !is_valid_ether_addr(nla_data(info->attrs[NL80211_ATTR_MAC])))
16043 		return -EINVAL;
16044 
16045 	wdev->valid_links |= BIT(link_id);
16046 	ether_addr_copy(wdev->links[link_id].addr,
16047 			nla_data(info->attrs[NL80211_ATTR_MAC]));
16048 
16049 	ret = rdev_add_intf_link(rdev, wdev, link_id);
16050 	if (ret) {
16051 		wdev->valid_links &= ~BIT(link_id);
16052 		eth_zero_addr(wdev->links[link_id].addr);
16053 	}
16054 
16055 	return ret;
16056 }
16057 
16058 static int nl80211_remove_link(struct sk_buff *skb, struct genl_info *info)
16059 {
16060 	unsigned int link_id = nl80211_link_id(info->attrs);
16061 	struct net_device *dev = info->user_ptr[1];
16062 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16063 
16064 	/* cannot remove if there's no link */
16065 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
16066 		return -EINVAL;
16067 
16068 	switch (wdev->iftype) {
16069 	case NL80211_IFTYPE_AP:
16070 		break;
16071 	default:
16072 		return -EINVAL;
16073 	}
16074 
16075 	cfg80211_remove_link(wdev, link_id);
16076 
16077 	return 0;
16078 }
16079 
16080 static int
16081 nl80211_add_mod_link_station(struct sk_buff *skb, struct genl_info *info,
16082 			     bool add)
16083 {
16084 	struct link_station_parameters params = {};
16085 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16086 	struct net_device *dev = info->user_ptr[1];
16087 	int err;
16088 
16089 	if ((add && !rdev->ops->add_link_station) ||
16090 	    (!add && !rdev->ops->mod_link_station))
16091 		return -EOPNOTSUPP;
16092 
16093 	if (add && !info->attrs[NL80211_ATTR_MAC])
16094 		return -EINVAL;
16095 
16096 	if (!info->attrs[NL80211_ATTR_MLD_ADDR])
16097 		return -EINVAL;
16098 
16099 	if (add && !info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
16100 		return -EINVAL;
16101 
16102 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
16103 
16104 	if (info->attrs[NL80211_ATTR_MAC]) {
16105 		params.link_mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
16106 		if (!is_valid_ether_addr(params.link_mac))
16107 			return -EINVAL;
16108 	}
16109 
16110 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
16111 		return -EINVAL;
16112 
16113 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
16114 
16115 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
16116 		params.supported_rates =
16117 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
16118 		params.supported_rates_len =
16119 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
16120 	}
16121 
16122 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
16123 		params.ht_capa =
16124 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
16125 
16126 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
16127 		params.vht_capa =
16128 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
16129 
16130 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
16131 		params.he_capa =
16132 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
16133 		params.he_capa_len =
16134 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
16135 
16136 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
16137 			params.eht_capa =
16138 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
16139 			params.eht_capa_len =
16140 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
16141 
16142 			if (!ieee80211_eht_capa_size_ok((const u8 *)params.he_capa,
16143 							(const u8 *)params.eht_capa,
16144 							params.eht_capa_len,
16145 							false))
16146 				return -EINVAL;
16147 		}
16148 	}
16149 
16150 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
16151 		params.he_6ghz_capa =
16152 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
16153 
16154 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
16155 		params.opmode_notif_used = true;
16156 		params.opmode_notif =
16157 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
16158 	}
16159 
16160 	err = nl80211_parse_sta_txpower_setting(info, &params.txpwr,
16161 						&params.txpwr_set);
16162 	if (err)
16163 		return err;
16164 
16165 	if (add)
16166 		return rdev_add_link_station(rdev, dev, &params);
16167 
16168 	return rdev_mod_link_station(rdev, dev, &params);
16169 }
16170 
16171 static int
16172 nl80211_add_link_station(struct sk_buff *skb, struct genl_info *info)
16173 {
16174 	return nl80211_add_mod_link_station(skb, info, true);
16175 }
16176 
16177 static int
16178 nl80211_modify_link_station(struct sk_buff *skb, struct genl_info *info)
16179 {
16180 	return nl80211_add_mod_link_station(skb, info, false);
16181 }
16182 
16183 static int
16184 nl80211_remove_link_station(struct sk_buff *skb, struct genl_info *info)
16185 {
16186 	struct link_station_del_parameters params = {};
16187 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16188 	struct net_device *dev = info->user_ptr[1];
16189 
16190 	if (!rdev->ops->del_link_station)
16191 		return -EOPNOTSUPP;
16192 
16193 	if (!info->attrs[NL80211_ATTR_MLD_ADDR] ||
16194 	    !info->attrs[NL80211_ATTR_MLO_LINK_ID])
16195 		return -EINVAL;
16196 
16197 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
16198 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
16199 
16200 	return rdev_del_link_station(rdev, dev, &params);
16201 }
16202 
16203 static int nl80211_set_hw_timestamp(struct sk_buff *skb,
16204 				    struct genl_info *info)
16205 {
16206 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16207 	struct net_device *dev = info->user_ptr[1];
16208 	struct cfg80211_set_hw_timestamp hwts = {};
16209 
16210 	if (!rdev->wiphy.hw_timestamp_max_peers)
16211 		return -EOPNOTSUPP;
16212 
16213 	if (!info->attrs[NL80211_ATTR_MAC] &&
16214 	    rdev->wiphy.hw_timestamp_max_peers != CFG80211_HW_TIMESTAMP_ALL_PEERS)
16215 		return -EOPNOTSUPP;
16216 
16217 	if (info->attrs[NL80211_ATTR_MAC])
16218 		hwts.macaddr = nla_data(info->attrs[NL80211_ATTR_MAC]);
16219 
16220 	hwts.enable =
16221 		nla_get_flag(info->attrs[NL80211_ATTR_HW_TIMESTAMP_ENABLED]);
16222 
16223 	return rdev_set_hw_timestamp(rdev, dev, &hwts);
16224 }
16225 
16226 #define NL80211_FLAG_NEED_WIPHY		0x01
16227 #define NL80211_FLAG_NEED_NETDEV	0x02
16228 #define NL80211_FLAG_NEED_RTNL		0x04
16229 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
16230 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
16231 					 NL80211_FLAG_CHECK_NETDEV_UP)
16232 #define NL80211_FLAG_NEED_WDEV		0x10
16233 /* If a netdev is associated, it must be UP, P2P must be started */
16234 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
16235 					 NL80211_FLAG_CHECK_NETDEV_UP)
16236 #define NL80211_FLAG_CLEAR_SKB		0x20
16237 #define NL80211_FLAG_NO_WIPHY_MTX	0x40
16238 #define NL80211_FLAG_MLO_VALID_LINK_ID	0x80
16239 #define NL80211_FLAG_MLO_UNSUPPORTED	0x100
16240 
16241 #define INTERNAL_FLAG_SELECTORS(__sel)			\
16242 	SELECTOR(__sel, NONE, 0) /* must be first */	\
16243 	SELECTOR(__sel, WIPHY,				\
16244 		 NL80211_FLAG_NEED_WIPHY)		\
16245 	SELECTOR(__sel, WDEV,				\
16246 		 NL80211_FLAG_NEED_WDEV)		\
16247 	SELECTOR(__sel, NETDEV,				\
16248 		 NL80211_FLAG_NEED_NETDEV)		\
16249 	SELECTOR(__sel, NETDEV_LINK,			\
16250 		 NL80211_FLAG_NEED_NETDEV |		\
16251 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16252 	SELECTOR(__sel, NETDEV_NO_MLO,			\
16253 		 NL80211_FLAG_NEED_NETDEV |		\
16254 		 NL80211_FLAG_MLO_UNSUPPORTED)	\
16255 	SELECTOR(__sel, WIPHY_RTNL,			\
16256 		 NL80211_FLAG_NEED_WIPHY |		\
16257 		 NL80211_FLAG_NEED_RTNL)		\
16258 	SELECTOR(__sel, WIPHY_RTNL_NOMTX,		\
16259 		 NL80211_FLAG_NEED_WIPHY |		\
16260 		 NL80211_FLAG_NEED_RTNL |		\
16261 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16262 	SELECTOR(__sel, WDEV_RTNL,			\
16263 		 NL80211_FLAG_NEED_WDEV |		\
16264 		 NL80211_FLAG_NEED_RTNL)		\
16265 	SELECTOR(__sel, NETDEV_RTNL,			\
16266 		 NL80211_FLAG_NEED_NETDEV |		\
16267 		 NL80211_FLAG_NEED_RTNL)		\
16268 	SELECTOR(__sel, NETDEV_UP,			\
16269 		 NL80211_FLAG_NEED_NETDEV_UP)		\
16270 	SELECTOR(__sel, NETDEV_UP_LINK,			\
16271 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16272 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16273 	SELECTOR(__sel, NETDEV_UP_NO_MLO,		\
16274 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16275 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16276 	SELECTOR(__sel, NETDEV_UP_NO_MLO_CLEAR,		\
16277 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16278 		 NL80211_FLAG_CLEAR_SKB |		\
16279 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16280 	SELECTOR(__sel, NETDEV_UP_NOTMX,		\
16281 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16282 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16283 	SELECTOR(__sel, NETDEV_UP_NOTMX_NOMLO,		\
16284 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16285 		 NL80211_FLAG_NO_WIPHY_MTX |		\
16286 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16287 	SELECTOR(__sel, NETDEV_UP_CLEAR,		\
16288 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16289 		 NL80211_FLAG_CLEAR_SKB)		\
16290 	SELECTOR(__sel, WDEV_UP,			\
16291 		 NL80211_FLAG_NEED_WDEV_UP)		\
16292 	SELECTOR(__sel, WDEV_UP_LINK,			\
16293 		 NL80211_FLAG_NEED_WDEV_UP |		\
16294 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16295 	SELECTOR(__sel, WDEV_UP_RTNL,			\
16296 		 NL80211_FLAG_NEED_WDEV_UP |		\
16297 		 NL80211_FLAG_NEED_RTNL)		\
16298 	SELECTOR(__sel, WIPHY_CLEAR,			\
16299 		 NL80211_FLAG_NEED_WIPHY |		\
16300 		 NL80211_FLAG_CLEAR_SKB)
16301 
16302 enum nl80211_internal_flags_selector {
16303 #define SELECTOR(_, name, value)	NL80211_IFL_SEL_##name,
16304 	INTERNAL_FLAG_SELECTORS(_)
16305 #undef SELECTOR
16306 };
16307 
16308 static u32 nl80211_internal_flags[] = {
16309 #define SELECTOR(_, name, value)	[NL80211_IFL_SEL_##name] = value,
16310 	INTERNAL_FLAG_SELECTORS(_)
16311 #undef SELECTOR
16312 };
16313 
16314 static int nl80211_pre_doit(const struct genl_split_ops *ops,
16315 			    struct sk_buff *skb,
16316 			    struct genl_info *info)
16317 {
16318 	struct cfg80211_registered_device *rdev = NULL;
16319 	struct wireless_dev *wdev = NULL;
16320 	struct net_device *dev = NULL;
16321 	u32 internal_flags;
16322 	int err;
16323 
16324 	if (WARN_ON(ops->internal_flags >= ARRAY_SIZE(nl80211_internal_flags)))
16325 		return -EINVAL;
16326 
16327 	internal_flags = nl80211_internal_flags[ops->internal_flags];
16328 
16329 	rtnl_lock();
16330 	if (internal_flags & NL80211_FLAG_NEED_WIPHY) {
16331 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
16332 		if (IS_ERR(rdev)) {
16333 			err = PTR_ERR(rdev);
16334 			goto out_unlock;
16335 		}
16336 		info->user_ptr[0] = rdev;
16337 	} else if (internal_flags & NL80211_FLAG_NEED_NETDEV ||
16338 		   internal_flags & NL80211_FLAG_NEED_WDEV) {
16339 		wdev = __cfg80211_wdev_from_attrs(NULL, genl_info_net(info),
16340 						  info->attrs);
16341 		if (IS_ERR(wdev)) {
16342 			err = PTR_ERR(wdev);
16343 			goto out_unlock;
16344 		}
16345 
16346 		dev = wdev->netdev;
16347 		dev_hold(dev);
16348 		rdev = wiphy_to_rdev(wdev->wiphy);
16349 
16350 		if (internal_flags & NL80211_FLAG_NEED_NETDEV) {
16351 			if (!dev) {
16352 				err = -EINVAL;
16353 				goto out_unlock;
16354 			}
16355 
16356 			info->user_ptr[1] = dev;
16357 		} else {
16358 			info->user_ptr[1] = wdev;
16359 		}
16360 
16361 		if (internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
16362 		    !wdev_running(wdev)) {
16363 			err = -ENETDOWN;
16364 			goto out_unlock;
16365 		}
16366 
16367 		info->user_ptr[0] = rdev;
16368 	}
16369 
16370 	if (internal_flags & NL80211_FLAG_MLO_VALID_LINK_ID) {
16371 		struct nlattr *link_id = info->attrs[NL80211_ATTR_MLO_LINK_ID];
16372 
16373 		if (!wdev) {
16374 			err = -EINVAL;
16375 			goto out_unlock;
16376 		}
16377 
16378 		/* MLO -> require valid link ID */
16379 		if (wdev->valid_links &&
16380 		    (!link_id ||
16381 		     !(wdev->valid_links & BIT(nla_get_u8(link_id))))) {
16382 			err = -EINVAL;
16383 			goto out_unlock;
16384 		}
16385 
16386 		/* non-MLO -> no link ID attribute accepted */
16387 		if (!wdev->valid_links && link_id) {
16388 			err = -EINVAL;
16389 			goto out_unlock;
16390 		}
16391 	}
16392 
16393 	if (internal_flags & NL80211_FLAG_MLO_UNSUPPORTED) {
16394 		if (info->attrs[NL80211_ATTR_MLO_LINK_ID] ||
16395 		    (wdev && wdev->valid_links)) {
16396 			err = -EINVAL;
16397 			goto out_unlock;
16398 		}
16399 	}
16400 
16401 	if (rdev && !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16402 		wiphy_lock(&rdev->wiphy);
16403 		/* we keep the mutex locked until post_doit */
16404 		__release(&rdev->wiphy.mtx);
16405 	}
16406 	if (!(internal_flags & NL80211_FLAG_NEED_RTNL))
16407 		rtnl_unlock();
16408 
16409 	return 0;
16410 out_unlock:
16411 	rtnl_unlock();
16412 	dev_put(dev);
16413 	return err;
16414 }
16415 
16416 static void nl80211_post_doit(const struct genl_split_ops *ops,
16417 			      struct sk_buff *skb,
16418 			      struct genl_info *info)
16419 {
16420 	u32 internal_flags = nl80211_internal_flags[ops->internal_flags];
16421 
16422 	if (info->user_ptr[1]) {
16423 		if (internal_flags & NL80211_FLAG_NEED_WDEV) {
16424 			struct wireless_dev *wdev = info->user_ptr[1];
16425 
16426 			dev_put(wdev->netdev);
16427 		} else {
16428 			dev_put(info->user_ptr[1]);
16429 		}
16430 	}
16431 
16432 	if (info->user_ptr[0] &&
16433 	    !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16434 		struct cfg80211_registered_device *rdev = info->user_ptr[0];
16435 
16436 		/* we kept the mutex locked since pre_doit */
16437 		__acquire(&rdev->wiphy.mtx);
16438 		wiphy_unlock(&rdev->wiphy);
16439 	}
16440 
16441 	if (internal_flags & NL80211_FLAG_NEED_RTNL)
16442 		rtnl_unlock();
16443 
16444 	/* If needed, clear the netlink message payload from the SKB
16445 	 * as it might contain key data that shouldn't stick around on
16446 	 * the heap after the SKB is freed. The netlink message header
16447 	 * is still needed for further processing, so leave it intact.
16448 	 */
16449 	if (internal_flags & NL80211_FLAG_CLEAR_SKB) {
16450 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
16451 
16452 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
16453 	}
16454 }
16455 
16456 static int nl80211_set_sar_sub_specs(struct cfg80211_registered_device *rdev,
16457 				     struct cfg80211_sar_specs *sar_specs,
16458 				     struct nlattr *spec[], int index)
16459 {
16460 	u32 range_index, i;
16461 
16462 	if (!sar_specs || !spec)
16463 		return -EINVAL;
16464 
16465 	if (!spec[NL80211_SAR_ATTR_SPECS_POWER] ||
16466 	    !spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX])
16467 		return -EINVAL;
16468 
16469 	range_index = nla_get_u32(spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX]);
16470 
16471 	/* check if range_index exceeds num_freq_ranges */
16472 	if (range_index >= rdev->wiphy.sar_capa->num_freq_ranges)
16473 		return -EINVAL;
16474 
16475 	/* check if range_index duplicates */
16476 	for (i = 0; i < index; i++) {
16477 		if (sar_specs->sub_specs[i].freq_range_index == range_index)
16478 			return -EINVAL;
16479 	}
16480 
16481 	sar_specs->sub_specs[index].power =
16482 		nla_get_s32(spec[NL80211_SAR_ATTR_SPECS_POWER]);
16483 
16484 	sar_specs->sub_specs[index].freq_range_index = range_index;
16485 
16486 	return 0;
16487 }
16488 
16489 static int nl80211_set_sar_specs(struct sk_buff *skb, struct genl_info *info)
16490 {
16491 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16492 	struct nlattr *spec[NL80211_SAR_ATTR_SPECS_MAX + 1];
16493 	struct nlattr *tb[NL80211_SAR_ATTR_MAX + 1];
16494 	struct cfg80211_sar_specs *sar_spec;
16495 	enum nl80211_sar_type type;
16496 	struct nlattr *spec_list;
16497 	u32 specs;
16498 	int rem, err;
16499 
16500 	if (!rdev->wiphy.sar_capa || !rdev->ops->set_sar_specs)
16501 		return -EOPNOTSUPP;
16502 
16503 	if (!info->attrs[NL80211_ATTR_SAR_SPEC])
16504 		return -EINVAL;
16505 
16506 	nla_parse_nested(tb, NL80211_SAR_ATTR_MAX,
16507 			 info->attrs[NL80211_ATTR_SAR_SPEC],
16508 			 NULL, NULL);
16509 
16510 	if (!tb[NL80211_SAR_ATTR_TYPE] || !tb[NL80211_SAR_ATTR_SPECS])
16511 		return -EINVAL;
16512 
16513 	type = nla_get_u32(tb[NL80211_SAR_ATTR_TYPE]);
16514 	if (type != rdev->wiphy.sar_capa->type)
16515 		return -EINVAL;
16516 
16517 	specs = 0;
16518 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem)
16519 		specs++;
16520 
16521 	if (specs > rdev->wiphy.sar_capa->num_freq_ranges)
16522 		return -EINVAL;
16523 
16524 	sar_spec = kzalloc(struct_size(sar_spec, sub_specs, specs), GFP_KERNEL);
16525 	if (!sar_spec)
16526 		return -ENOMEM;
16527 
16528 	sar_spec->type = type;
16529 	specs = 0;
16530 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem) {
16531 		nla_parse_nested(spec, NL80211_SAR_ATTR_SPECS_MAX,
16532 				 spec_list, NULL, NULL);
16533 
16534 		switch (type) {
16535 		case NL80211_SAR_TYPE_POWER:
16536 			if (nl80211_set_sar_sub_specs(rdev, sar_spec,
16537 						      spec, specs)) {
16538 				err = -EINVAL;
16539 				goto error;
16540 			}
16541 			break;
16542 		default:
16543 			err = -EINVAL;
16544 			goto error;
16545 		}
16546 		specs++;
16547 	}
16548 
16549 	sar_spec->num_sub_specs = specs;
16550 
16551 	rdev->cur_cmd_info = info;
16552 	err = rdev_set_sar_specs(rdev, sar_spec);
16553 	rdev->cur_cmd_info = NULL;
16554 error:
16555 	kfree(sar_spec);
16556 	return err;
16557 }
16558 
16559 #define SELECTOR(__sel, name, value) \
16560 	((__sel) == (value)) ? NL80211_IFL_SEL_##name :
16561 int __missing_selector(void);
16562 #define IFLAGS(__val) INTERNAL_FLAG_SELECTORS(__val) __missing_selector()
16563 
16564 static const struct genl_ops nl80211_ops[] = {
16565 	{
16566 		.cmd = NL80211_CMD_GET_WIPHY,
16567 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16568 		.doit = nl80211_get_wiphy,
16569 		.dumpit = nl80211_dump_wiphy,
16570 		.done = nl80211_dump_wiphy_done,
16571 		/* can be retrieved by unprivileged users */
16572 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16573 	},
16574 };
16575 
16576 static const struct genl_small_ops nl80211_small_ops[] = {
16577 	{
16578 		.cmd = NL80211_CMD_SET_WIPHY,
16579 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16580 		.doit = nl80211_set_wiphy,
16581 		.flags = GENL_UNS_ADMIN_PERM,
16582 	},
16583 	{
16584 		.cmd = NL80211_CMD_GET_INTERFACE,
16585 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16586 		.doit = nl80211_get_interface,
16587 		.dumpit = nl80211_dump_interface,
16588 		/* can be retrieved by unprivileged users */
16589 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
16590 	},
16591 	{
16592 		.cmd = NL80211_CMD_SET_INTERFACE,
16593 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16594 		.doit = nl80211_set_interface,
16595 		.flags = GENL_UNS_ADMIN_PERM,
16596 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16597 					 NL80211_FLAG_NEED_RTNL),
16598 	},
16599 	{
16600 		.cmd = NL80211_CMD_NEW_INTERFACE,
16601 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16602 		.doit = nl80211_new_interface,
16603 		.flags = GENL_UNS_ADMIN_PERM,
16604 		.internal_flags =
16605 			IFLAGS(NL80211_FLAG_NEED_WIPHY |
16606 			       NL80211_FLAG_NEED_RTNL |
16607 			       /* we take the wiphy mutex later ourselves */
16608 			       NL80211_FLAG_NO_WIPHY_MTX),
16609 	},
16610 	{
16611 		.cmd = NL80211_CMD_DEL_INTERFACE,
16612 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16613 		.doit = nl80211_del_interface,
16614 		.flags = GENL_UNS_ADMIN_PERM,
16615 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
16616 					 NL80211_FLAG_NEED_RTNL),
16617 	},
16618 	{
16619 		.cmd = NL80211_CMD_GET_KEY,
16620 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16621 		.doit = nl80211_get_key,
16622 		.flags = GENL_UNS_ADMIN_PERM,
16623 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16624 	},
16625 	{
16626 		.cmd = NL80211_CMD_SET_KEY,
16627 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16628 		.doit = nl80211_set_key,
16629 		.flags = GENL_UNS_ADMIN_PERM,
16630 		/* cannot use NL80211_FLAG_MLO_VALID_LINK_ID, depends on key */
16631 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16632 					 NL80211_FLAG_CLEAR_SKB),
16633 	},
16634 	{
16635 		.cmd = NL80211_CMD_NEW_KEY,
16636 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16637 		.doit = nl80211_new_key,
16638 		.flags = GENL_UNS_ADMIN_PERM,
16639 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16640 					 NL80211_FLAG_CLEAR_SKB),
16641 	},
16642 	{
16643 		.cmd = NL80211_CMD_DEL_KEY,
16644 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16645 		.doit = nl80211_del_key,
16646 		.flags = GENL_UNS_ADMIN_PERM,
16647 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16648 	},
16649 	{
16650 		.cmd = NL80211_CMD_SET_BEACON,
16651 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16652 		.flags = GENL_UNS_ADMIN_PERM,
16653 		.doit = nl80211_set_beacon,
16654 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16655 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16656 	},
16657 	{
16658 		.cmd = NL80211_CMD_START_AP,
16659 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16660 		.flags = GENL_UNS_ADMIN_PERM,
16661 		.doit = nl80211_start_ap,
16662 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16663 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16664 	},
16665 	{
16666 		.cmd = NL80211_CMD_STOP_AP,
16667 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16668 		.flags = GENL_UNS_ADMIN_PERM,
16669 		.doit = nl80211_stop_ap,
16670 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16671 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16672 	},
16673 	{
16674 		.cmd = NL80211_CMD_GET_STATION,
16675 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16676 		.doit = nl80211_get_station,
16677 		.dumpit = nl80211_dump_station,
16678 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16679 	},
16680 	{
16681 		.cmd = NL80211_CMD_SET_STATION,
16682 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16683 		.doit = nl80211_set_station,
16684 		.flags = GENL_UNS_ADMIN_PERM,
16685 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16686 	},
16687 	{
16688 		.cmd = NL80211_CMD_NEW_STATION,
16689 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16690 		.doit = nl80211_new_station,
16691 		.flags = GENL_UNS_ADMIN_PERM,
16692 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16693 	},
16694 	{
16695 		.cmd = NL80211_CMD_DEL_STATION,
16696 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16697 		.doit = nl80211_del_station,
16698 		.flags = GENL_UNS_ADMIN_PERM,
16699 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16700 	},
16701 	{
16702 		.cmd = NL80211_CMD_GET_MPATH,
16703 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16704 		.doit = nl80211_get_mpath,
16705 		.dumpit = nl80211_dump_mpath,
16706 		.flags = GENL_UNS_ADMIN_PERM,
16707 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16708 	},
16709 	{
16710 		.cmd = NL80211_CMD_GET_MPP,
16711 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16712 		.doit = nl80211_get_mpp,
16713 		.dumpit = nl80211_dump_mpp,
16714 		.flags = GENL_UNS_ADMIN_PERM,
16715 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16716 	},
16717 	{
16718 		.cmd = NL80211_CMD_SET_MPATH,
16719 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16720 		.doit = nl80211_set_mpath,
16721 		.flags = GENL_UNS_ADMIN_PERM,
16722 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16723 	},
16724 	{
16725 		.cmd = NL80211_CMD_NEW_MPATH,
16726 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16727 		.doit = nl80211_new_mpath,
16728 		.flags = GENL_UNS_ADMIN_PERM,
16729 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16730 	},
16731 	{
16732 		.cmd = NL80211_CMD_DEL_MPATH,
16733 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16734 		.doit = nl80211_del_mpath,
16735 		.flags = GENL_UNS_ADMIN_PERM,
16736 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16737 	},
16738 	{
16739 		.cmd = NL80211_CMD_SET_BSS,
16740 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16741 		.doit = nl80211_set_bss,
16742 		.flags = GENL_UNS_ADMIN_PERM,
16743 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16744 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16745 	},
16746 	{
16747 		.cmd = NL80211_CMD_GET_REG,
16748 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16749 		.doit = nl80211_get_reg_do,
16750 		.dumpit = nl80211_get_reg_dump,
16751 		/* can be retrieved by unprivileged users */
16752 	},
16753 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
16754 	{
16755 		.cmd = NL80211_CMD_SET_REG,
16756 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16757 		.doit = nl80211_set_reg,
16758 		.flags = GENL_ADMIN_PERM,
16759 	},
16760 #endif
16761 	{
16762 		.cmd = NL80211_CMD_REQ_SET_REG,
16763 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16764 		.doit = nl80211_req_set_reg,
16765 		.flags = GENL_ADMIN_PERM,
16766 	},
16767 	{
16768 		.cmd = NL80211_CMD_RELOAD_REGDB,
16769 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16770 		.doit = nl80211_reload_regdb,
16771 		.flags = GENL_ADMIN_PERM,
16772 	},
16773 	{
16774 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
16775 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16776 		.doit = nl80211_get_mesh_config,
16777 		/* can be retrieved by unprivileged users */
16778 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16779 	},
16780 	{
16781 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
16782 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16783 		.doit = nl80211_update_mesh_config,
16784 		.flags = GENL_UNS_ADMIN_PERM,
16785 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16786 	},
16787 	{
16788 		.cmd = NL80211_CMD_TRIGGER_SCAN,
16789 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16790 		.doit = nl80211_trigger_scan,
16791 		.flags = GENL_UNS_ADMIN_PERM,
16792 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16793 	},
16794 	{
16795 		.cmd = NL80211_CMD_ABORT_SCAN,
16796 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16797 		.doit = nl80211_abort_scan,
16798 		.flags = GENL_UNS_ADMIN_PERM,
16799 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16800 	},
16801 	{
16802 		.cmd = NL80211_CMD_GET_SCAN,
16803 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16804 		.dumpit = nl80211_dump_scan,
16805 	},
16806 	{
16807 		.cmd = NL80211_CMD_START_SCHED_SCAN,
16808 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16809 		.doit = nl80211_start_sched_scan,
16810 		.flags = GENL_UNS_ADMIN_PERM,
16811 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16812 	},
16813 	{
16814 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
16815 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16816 		.doit = nl80211_stop_sched_scan,
16817 		.flags = GENL_UNS_ADMIN_PERM,
16818 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16819 	},
16820 	{
16821 		.cmd = NL80211_CMD_AUTHENTICATE,
16822 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16823 		.doit = nl80211_authenticate,
16824 		.flags = GENL_UNS_ADMIN_PERM,
16825 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16826 					 NL80211_FLAG_CLEAR_SKB),
16827 	},
16828 	{
16829 		.cmd = NL80211_CMD_ASSOCIATE,
16830 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16831 		.doit = nl80211_associate,
16832 		.flags = GENL_UNS_ADMIN_PERM,
16833 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16834 					 NL80211_FLAG_CLEAR_SKB),
16835 	},
16836 	{
16837 		.cmd = NL80211_CMD_DEAUTHENTICATE,
16838 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16839 		.doit = nl80211_deauthenticate,
16840 		.flags = GENL_UNS_ADMIN_PERM,
16841 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16842 	},
16843 	{
16844 		.cmd = NL80211_CMD_DISASSOCIATE,
16845 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16846 		.doit = nl80211_disassociate,
16847 		.flags = GENL_UNS_ADMIN_PERM,
16848 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16849 	},
16850 	{
16851 		.cmd = NL80211_CMD_JOIN_IBSS,
16852 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16853 		.doit = nl80211_join_ibss,
16854 		.flags = GENL_UNS_ADMIN_PERM,
16855 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16856 	},
16857 	{
16858 		.cmd = NL80211_CMD_LEAVE_IBSS,
16859 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16860 		.doit = nl80211_leave_ibss,
16861 		.flags = GENL_UNS_ADMIN_PERM,
16862 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16863 	},
16864 #ifdef CONFIG_NL80211_TESTMODE
16865 	{
16866 		.cmd = NL80211_CMD_TESTMODE,
16867 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16868 		.doit = nl80211_testmode_do,
16869 		.dumpit = nl80211_testmode_dump,
16870 		.flags = GENL_UNS_ADMIN_PERM,
16871 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16872 	},
16873 #endif
16874 	{
16875 		.cmd = NL80211_CMD_CONNECT,
16876 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16877 		.doit = nl80211_connect,
16878 		.flags = GENL_UNS_ADMIN_PERM,
16879 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16880 					 NL80211_FLAG_CLEAR_SKB),
16881 	},
16882 	{
16883 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
16884 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16885 		.doit = nl80211_update_connect_params,
16886 		.flags = GENL_ADMIN_PERM,
16887 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16888 					 NL80211_FLAG_CLEAR_SKB),
16889 	},
16890 	{
16891 		.cmd = NL80211_CMD_DISCONNECT,
16892 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16893 		.doit = nl80211_disconnect,
16894 		.flags = GENL_UNS_ADMIN_PERM,
16895 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16896 	},
16897 	{
16898 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
16899 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16900 		.doit = nl80211_wiphy_netns,
16901 		.flags = GENL_UNS_ADMIN_PERM,
16902 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
16903 					 NL80211_FLAG_NEED_RTNL |
16904 					 NL80211_FLAG_NO_WIPHY_MTX),
16905 	},
16906 	{
16907 		.cmd = NL80211_CMD_GET_SURVEY,
16908 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16909 		.dumpit = nl80211_dump_survey,
16910 	},
16911 	{
16912 		.cmd = NL80211_CMD_SET_PMKSA,
16913 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16914 		.doit = nl80211_setdel_pmksa,
16915 		.flags = GENL_UNS_ADMIN_PERM,
16916 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16917 					 NL80211_FLAG_CLEAR_SKB),
16918 	},
16919 	{
16920 		.cmd = NL80211_CMD_DEL_PMKSA,
16921 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16922 		.doit = nl80211_setdel_pmksa,
16923 		.flags = GENL_UNS_ADMIN_PERM,
16924 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16925 	},
16926 	{
16927 		.cmd = NL80211_CMD_FLUSH_PMKSA,
16928 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16929 		.doit = nl80211_flush_pmksa,
16930 		.flags = GENL_UNS_ADMIN_PERM,
16931 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16932 	},
16933 	{
16934 		.cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
16935 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16936 		.doit = nl80211_remain_on_channel,
16937 		.flags = GENL_UNS_ADMIN_PERM,
16938 		/* FIXME: requiring a link ID here is probably not good */
16939 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
16940 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16941 	},
16942 	{
16943 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
16944 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16945 		.doit = nl80211_cancel_remain_on_channel,
16946 		.flags = GENL_UNS_ADMIN_PERM,
16947 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16948 	},
16949 	{
16950 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
16951 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16952 		.doit = nl80211_set_tx_bitrate_mask,
16953 		.flags = GENL_UNS_ADMIN_PERM,
16954 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16955 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16956 	},
16957 	{
16958 		.cmd = NL80211_CMD_REGISTER_FRAME,
16959 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16960 		.doit = nl80211_register_mgmt,
16961 		.flags = GENL_UNS_ADMIN_PERM,
16962 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
16963 	},
16964 	{
16965 		.cmd = NL80211_CMD_FRAME,
16966 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16967 		.doit = nl80211_tx_mgmt,
16968 		.flags = GENL_UNS_ADMIN_PERM,
16969 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16970 	},
16971 	{
16972 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
16973 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16974 		.doit = nl80211_tx_mgmt_cancel_wait,
16975 		.flags = GENL_UNS_ADMIN_PERM,
16976 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16977 	},
16978 	{
16979 		.cmd = NL80211_CMD_SET_POWER_SAVE,
16980 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16981 		.doit = nl80211_set_power_save,
16982 		.flags = GENL_UNS_ADMIN_PERM,
16983 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16984 	},
16985 	{
16986 		.cmd = NL80211_CMD_GET_POWER_SAVE,
16987 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16988 		.doit = nl80211_get_power_save,
16989 		/* can be retrieved by unprivileged users */
16990 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16991 	},
16992 	{
16993 		.cmd = NL80211_CMD_SET_CQM,
16994 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16995 		.doit = nl80211_set_cqm,
16996 		.flags = GENL_UNS_ADMIN_PERM,
16997 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16998 	},
16999 	{
17000 		.cmd = NL80211_CMD_SET_CHANNEL,
17001 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17002 		.doit = nl80211_set_channel,
17003 		.flags = GENL_UNS_ADMIN_PERM,
17004 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17005 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17006 	},
17007 	{
17008 		.cmd = NL80211_CMD_JOIN_MESH,
17009 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17010 		.doit = nl80211_join_mesh,
17011 		.flags = GENL_UNS_ADMIN_PERM,
17012 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17013 	},
17014 	{
17015 		.cmd = NL80211_CMD_LEAVE_MESH,
17016 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17017 		.doit = nl80211_leave_mesh,
17018 		.flags = GENL_UNS_ADMIN_PERM,
17019 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17020 	},
17021 	{
17022 		.cmd = NL80211_CMD_JOIN_OCB,
17023 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17024 		.doit = nl80211_join_ocb,
17025 		.flags = GENL_UNS_ADMIN_PERM,
17026 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17027 	},
17028 	{
17029 		.cmd = NL80211_CMD_LEAVE_OCB,
17030 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17031 		.doit = nl80211_leave_ocb,
17032 		.flags = GENL_UNS_ADMIN_PERM,
17033 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17034 	},
17035 #ifdef CONFIG_PM
17036 	{
17037 		.cmd = NL80211_CMD_GET_WOWLAN,
17038 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17039 		.doit = nl80211_get_wowlan,
17040 		/* can be retrieved by unprivileged users */
17041 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17042 	},
17043 	{
17044 		.cmd = NL80211_CMD_SET_WOWLAN,
17045 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17046 		.doit = nl80211_set_wowlan,
17047 		.flags = GENL_UNS_ADMIN_PERM,
17048 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17049 	},
17050 #endif
17051 	{
17052 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
17053 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17054 		.doit = nl80211_set_rekey_data,
17055 		.flags = GENL_UNS_ADMIN_PERM,
17056 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17057 					 NL80211_FLAG_CLEAR_SKB),
17058 	},
17059 	{
17060 		.cmd = NL80211_CMD_TDLS_MGMT,
17061 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17062 		.doit = nl80211_tdls_mgmt,
17063 		.flags = GENL_UNS_ADMIN_PERM,
17064 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17065 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17066 	},
17067 	{
17068 		.cmd = NL80211_CMD_TDLS_OPER,
17069 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17070 		.doit = nl80211_tdls_oper,
17071 		.flags = GENL_UNS_ADMIN_PERM,
17072 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17073 	},
17074 	{
17075 		.cmd = NL80211_CMD_UNEXPECTED_FRAME,
17076 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17077 		.doit = nl80211_register_unexpected_frame,
17078 		.flags = GENL_UNS_ADMIN_PERM,
17079 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17080 	},
17081 	{
17082 		.cmd = NL80211_CMD_PROBE_CLIENT,
17083 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17084 		.doit = nl80211_probe_client,
17085 		.flags = GENL_UNS_ADMIN_PERM,
17086 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17087 	},
17088 	{
17089 		.cmd = NL80211_CMD_REGISTER_BEACONS,
17090 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17091 		.doit = nl80211_register_beacons,
17092 		.flags = GENL_UNS_ADMIN_PERM,
17093 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17094 	},
17095 	{
17096 		.cmd = NL80211_CMD_SET_NOACK_MAP,
17097 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17098 		.doit = nl80211_set_noack_map,
17099 		.flags = GENL_UNS_ADMIN_PERM,
17100 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17101 	},
17102 	{
17103 		.cmd = NL80211_CMD_START_P2P_DEVICE,
17104 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17105 		.doit = nl80211_start_p2p_device,
17106 		.flags = GENL_UNS_ADMIN_PERM,
17107 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
17108 					 NL80211_FLAG_NEED_RTNL),
17109 	},
17110 	{
17111 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
17112 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17113 		.doit = nl80211_stop_p2p_device,
17114 		.flags = GENL_UNS_ADMIN_PERM,
17115 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17116 					 NL80211_FLAG_NEED_RTNL),
17117 	},
17118 	{
17119 		.cmd = NL80211_CMD_START_NAN,
17120 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17121 		.doit = nl80211_start_nan,
17122 		.flags = GENL_ADMIN_PERM,
17123 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
17124 					 NL80211_FLAG_NEED_RTNL),
17125 	},
17126 	{
17127 		.cmd = NL80211_CMD_STOP_NAN,
17128 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17129 		.doit = nl80211_stop_nan,
17130 		.flags = GENL_ADMIN_PERM,
17131 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17132 					 NL80211_FLAG_NEED_RTNL),
17133 	},
17134 	{
17135 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
17136 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17137 		.doit = nl80211_nan_add_func,
17138 		.flags = GENL_ADMIN_PERM,
17139 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17140 	},
17141 	{
17142 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
17143 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17144 		.doit = nl80211_nan_del_func,
17145 		.flags = GENL_ADMIN_PERM,
17146 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17147 	},
17148 	{
17149 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
17150 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17151 		.doit = nl80211_nan_change_config,
17152 		.flags = GENL_ADMIN_PERM,
17153 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17154 	},
17155 	{
17156 		.cmd = NL80211_CMD_SET_MCAST_RATE,
17157 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17158 		.doit = nl80211_set_mcast_rate,
17159 		.flags = GENL_UNS_ADMIN_PERM,
17160 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17161 	},
17162 	{
17163 		.cmd = NL80211_CMD_SET_MAC_ACL,
17164 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17165 		.doit = nl80211_set_mac_acl,
17166 		.flags = GENL_UNS_ADMIN_PERM,
17167 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17168 					 NL80211_FLAG_MLO_UNSUPPORTED),
17169 	},
17170 	{
17171 		.cmd = NL80211_CMD_RADAR_DETECT,
17172 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17173 		.doit = nl80211_start_radar_detection,
17174 		.flags = GENL_UNS_ADMIN_PERM,
17175 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17176 					 NL80211_FLAG_NO_WIPHY_MTX |
17177 					 NL80211_FLAG_MLO_UNSUPPORTED),
17178 	},
17179 	{
17180 		.cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
17181 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17182 		.doit = nl80211_get_protocol_features,
17183 	},
17184 	{
17185 		.cmd = NL80211_CMD_UPDATE_FT_IES,
17186 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17187 		.doit = nl80211_update_ft_ies,
17188 		.flags = GENL_UNS_ADMIN_PERM,
17189 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17190 	},
17191 	{
17192 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
17193 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17194 		.doit = nl80211_crit_protocol_start,
17195 		.flags = GENL_UNS_ADMIN_PERM,
17196 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17197 	},
17198 	{
17199 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
17200 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17201 		.doit = nl80211_crit_protocol_stop,
17202 		.flags = GENL_UNS_ADMIN_PERM,
17203 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17204 	},
17205 	{
17206 		.cmd = NL80211_CMD_GET_COALESCE,
17207 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17208 		.doit = nl80211_get_coalesce,
17209 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17210 	},
17211 	{
17212 		.cmd = NL80211_CMD_SET_COALESCE,
17213 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17214 		.doit = nl80211_set_coalesce,
17215 		.flags = GENL_UNS_ADMIN_PERM,
17216 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17217 	},
17218 	{
17219 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
17220 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17221 		.doit = nl80211_channel_switch,
17222 		.flags = GENL_UNS_ADMIN_PERM,
17223 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17224 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17225 	},
17226 	{
17227 		.cmd = NL80211_CMD_VENDOR,
17228 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17229 		.doit = nl80211_vendor_cmd,
17230 		.dumpit = nl80211_vendor_cmd_dump,
17231 		.flags = GENL_UNS_ADMIN_PERM,
17232 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17233 					 NL80211_FLAG_CLEAR_SKB),
17234 	},
17235 	{
17236 		.cmd = NL80211_CMD_SET_QOS_MAP,
17237 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17238 		.doit = nl80211_set_qos_map,
17239 		.flags = GENL_UNS_ADMIN_PERM,
17240 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17241 	},
17242 	{
17243 		.cmd = NL80211_CMD_ADD_TX_TS,
17244 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17245 		.doit = nl80211_add_tx_ts,
17246 		.flags = GENL_UNS_ADMIN_PERM,
17247 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17248 					 NL80211_FLAG_MLO_UNSUPPORTED),
17249 	},
17250 	{
17251 		.cmd = NL80211_CMD_DEL_TX_TS,
17252 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17253 		.doit = nl80211_del_tx_ts,
17254 		.flags = GENL_UNS_ADMIN_PERM,
17255 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17256 	},
17257 	{
17258 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
17259 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17260 		.doit = nl80211_tdls_channel_switch,
17261 		.flags = GENL_UNS_ADMIN_PERM,
17262 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17263 	},
17264 	{
17265 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
17266 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17267 		.doit = nl80211_tdls_cancel_channel_switch,
17268 		.flags = GENL_UNS_ADMIN_PERM,
17269 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17270 	},
17271 	{
17272 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
17273 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17274 		.doit = nl80211_set_multicast_to_unicast,
17275 		.flags = GENL_UNS_ADMIN_PERM,
17276 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17277 	},
17278 	{
17279 		.cmd = NL80211_CMD_SET_PMK,
17280 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17281 		.doit = nl80211_set_pmk,
17282 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17283 					 NL80211_FLAG_CLEAR_SKB),
17284 	},
17285 	{
17286 		.cmd = NL80211_CMD_DEL_PMK,
17287 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17288 		.doit = nl80211_del_pmk,
17289 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17290 	},
17291 	{
17292 		.cmd = NL80211_CMD_EXTERNAL_AUTH,
17293 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17294 		.doit = nl80211_external_auth,
17295 		.flags = GENL_ADMIN_PERM,
17296 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17297 	},
17298 	{
17299 		.cmd = NL80211_CMD_CONTROL_PORT_FRAME,
17300 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17301 		.doit = nl80211_tx_control_port,
17302 		.flags = GENL_UNS_ADMIN_PERM,
17303 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17304 	},
17305 	{
17306 		.cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
17307 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17308 		.doit = nl80211_get_ftm_responder_stats,
17309 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17310 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17311 	},
17312 	{
17313 		.cmd = NL80211_CMD_PEER_MEASUREMENT_START,
17314 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17315 		.doit = nl80211_pmsr_start,
17316 		.flags = GENL_UNS_ADMIN_PERM,
17317 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17318 	},
17319 	{
17320 		.cmd = NL80211_CMD_NOTIFY_RADAR,
17321 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17322 		.doit = nl80211_notify_radar_detection,
17323 		.flags = GENL_UNS_ADMIN_PERM,
17324 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17325 	},
17326 	{
17327 		.cmd = NL80211_CMD_UPDATE_OWE_INFO,
17328 		.doit = nl80211_update_owe_info,
17329 		.flags = GENL_ADMIN_PERM,
17330 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17331 	},
17332 	{
17333 		.cmd = NL80211_CMD_PROBE_MESH_LINK,
17334 		.doit = nl80211_probe_mesh_link,
17335 		.flags = GENL_UNS_ADMIN_PERM,
17336 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17337 	},
17338 	{
17339 		.cmd = NL80211_CMD_SET_TID_CONFIG,
17340 		.doit = nl80211_set_tid_config,
17341 		.flags = GENL_UNS_ADMIN_PERM,
17342 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17343 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17344 	},
17345 	{
17346 		.cmd = NL80211_CMD_SET_SAR_SPECS,
17347 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17348 		.doit = nl80211_set_sar_specs,
17349 		.flags = GENL_UNS_ADMIN_PERM,
17350 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17351 					 NL80211_FLAG_NEED_RTNL),
17352 	},
17353 	{
17354 		.cmd = NL80211_CMD_COLOR_CHANGE_REQUEST,
17355 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17356 		.doit = nl80211_color_change,
17357 		.flags = GENL_UNS_ADMIN_PERM,
17358 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17359 	},
17360 	{
17361 		.cmd = NL80211_CMD_SET_FILS_AAD,
17362 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17363 		.doit = nl80211_set_fils_aad,
17364 		.flags = GENL_UNS_ADMIN_PERM,
17365 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17366 	},
17367 	{
17368 		.cmd = NL80211_CMD_ADD_LINK,
17369 		.doit = nl80211_add_link,
17370 		.flags = GENL_UNS_ADMIN_PERM,
17371 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17372 	},
17373 	{
17374 		.cmd = NL80211_CMD_REMOVE_LINK,
17375 		.doit = nl80211_remove_link,
17376 		.flags = GENL_UNS_ADMIN_PERM,
17377 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17378 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17379 	},
17380 	{
17381 		.cmd = NL80211_CMD_ADD_LINK_STA,
17382 		.doit = nl80211_add_link_station,
17383 		.flags = GENL_UNS_ADMIN_PERM,
17384 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17385 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17386 	},
17387 	{
17388 		.cmd = NL80211_CMD_MODIFY_LINK_STA,
17389 		.doit = nl80211_modify_link_station,
17390 		.flags = GENL_UNS_ADMIN_PERM,
17391 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17392 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17393 	},
17394 	{
17395 		.cmd = NL80211_CMD_REMOVE_LINK_STA,
17396 		.doit = nl80211_remove_link_station,
17397 		.flags = GENL_UNS_ADMIN_PERM,
17398 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17399 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17400 	},
17401 	{
17402 		.cmd = NL80211_CMD_SET_HW_TIMESTAMP,
17403 		.doit = nl80211_set_hw_timestamp,
17404 		.flags = GENL_UNS_ADMIN_PERM,
17405 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17406 	},
17407 };
17408 
17409 static struct genl_family nl80211_fam __ro_after_init = {
17410 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
17411 	.hdrsize = 0,			/* no private header */
17412 	.version = 1,			/* no particular meaning now */
17413 	.maxattr = NL80211_ATTR_MAX,
17414 	.policy = nl80211_policy,
17415 	.netnsok = true,
17416 	.pre_doit = nl80211_pre_doit,
17417 	.post_doit = nl80211_post_doit,
17418 	.module = THIS_MODULE,
17419 	.ops = nl80211_ops,
17420 	.n_ops = ARRAY_SIZE(nl80211_ops),
17421 	.small_ops = nl80211_small_ops,
17422 	.n_small_ops = ARRAY_SIZE(nl80211_small_ops),
17423 	.resv_start_op = NL80211_CMD_REMOVE_LINK_STA + 1,
17424 	.mcgrps = nl80211_mcgrps,
17425 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
17426 	.parallel_ops = true,
17427 };
17428 
17429 /* notification functions */
17430 
17431 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
17432 			  enum nl80211_commands cmd)
17433 {
17434 	struct sk_buff *msg;
17435 	struct nl80211_dump_wiphy_state state = {};
17436 
17437 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
17438 		cmd != NL80211_CMD_DEL_WIPHY);
17439 
17440 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17441 	if (!msg)
17442 		return;
17443 
17444 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
17445 		nlmsg_free(msg);
17446 		return;
17447 	}
17448 
17449 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17450 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17451 }
17452 
17453 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
17454 				struct wireless_dev *wdev,
17455 				enum nl80211_commands cmd)
17456 {
17457 	struct sk_buff *msg;
17458 
17459 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17460 	if (!msg)
17461 		return;
17462 
17463 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
17464 		nlmsg_free(msg);
17465 		return;
17466 	}
17467 
17468 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17469 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17470 }
17471 
17472 static int nl80211_add_scan_req(struct sk_buff *msg,
17473 				struct cfg80211_registered_device *rdev)
17474 {
17475 	struct cfg80211_scan_request *req = rdev->scan_req;
17476 	struct nlattr *nest;
17477 	int i;
17478 	struct cfg80211_scan_info *info;
17479 
17480 	if (WARN_ON(!req))
17481 		return 0;
17482 
17483 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
17484 	if (!nest)
17485 		goto nla_put_failure;
17486 	for (i = 0; i < req->n_ssids; i++) {
17487 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
17488 			goto nla_put_failure;
17489 	}
17490 	nla_nest_end(msg, nest);
17491 
17492 	if (req->flags & NL80211_SCAN_FLAG_FREQ_KHZ) {
17493 		nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQ_KHZ);
17494 		if (!nest)
17495 			goto nla_put_failure;
17496 		for (i = 0; i < req->n_channels; i++) {
17497 			if (nla_put_u32(msg, i,
17498 				   ieee80211_channel_to_khz(req->channels[i])))
17499 				goto nla_put_failure;
17500 		}
17501 		nla_nest_end(msg, nest);
17502 	} else {
17503 		nest = nla_nest_start_noflag(msg,
17504 					     NL80211_ATTR_SCAN_FREQUENCIES);
17505 		if (!nest)
17506 			goto nla_put_failure;
17507 		for (i = 0; i < req->n_channels; i++) {
17508 			if (nla_put_u32(msg, i, req->channels[i]->center_freq))
17509 				goto nla_put_failure;
17510 		}
17511 		nla_nest_end(msg, nest);
17512 	}
17513 
17514 	if (req->ie &&
17515 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
17516 		goto nla_put_failure;
17517 
17518 	if (req->flags &&
17519 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
17520 		goto nla_put_failure;
17521 
17522 	info = rdev->int_scan_req ? &rdev->int_scan_req->info :
17523 		&rdev->scan_req->info;
17524 	if (info->scan_start_tsf &&
17525 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
17526 			       info->scan_start_tsf, NL80211_BSS_PAD) ||
17527 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
17528 		     info->tsf_bssid)))
17529 		goto nla_put_failure;
17530 
17531 	return 0;
17532  nla_put_failure:
17533 	return -ENOBUFS;
17534 }
17535 
17536 static int nl80211_prep_scan_msg(struct sk_buff *msg,
17537 				 struct cfg80211_registered_device *rdev,
17538 				 struct wireless_dev *wdev,
17539 				 u32 portid, u32 seq, int flags,
17540 				 u32 cmd)
17541 {
17542 	void *hdr;
17543 
17544 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
17545 	if (!hdr)
17546 		return -1;
17547 
17548 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17549 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
17550 					 wdev->netdev->ifindex)) ||
17551 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17552 			      NL80211_ATTR_PAD))
17553 		goto nla_put_failure;
17554 
17555 	/* ignore errors and send incomplete event anyway */
17556 	nl80211_add_scan_req(msg, rdev);
17557 
17558 	genlmsg_end(msg, hdr);
17559 	return 0;
17560 
17561  nla_put_failure:
17562 	genlmsg_cancel(msg, hdr);
17563 	return -EMSGSIZE;
17564 }
17565 
17566 static int
17567 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
17568 			    struct cfg80211_sched_scan_request *req, u32 cmd)
17569 {
17570 	void *hdr;
17571 
17572 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17573 	if (!hdr)
17574 		return -1;
17575 
17576 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
17577 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
17578 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
17579 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
17580 			      NL80211_ATTR_PAD))
17581 		goto nla_put_failure;
17582 
17583 	genlmsg_end(msg, hdr);
17584 	return 0;
17585 
17586  nla_put_failure:
17587 	genlmsg_cancel(msg, hdr);
17588 	return -EMSGSIZE;
17589 }
17590 
17591 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
17592 			     struct wireless_dev *wdev)
17593 {
17594 	struct sk_buff *msg;
17595 
17596 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17597 	if (!msg)
17598 		return;
17599 
17600 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17601 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
17602 		nlmsg_free(msg);
17603 		return;
17604 	}
17605 
17606 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17607 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17608 }
17609 
17610 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
17611 				       struct wireless_dev *wdev, bool aborted)
17612 {
17613 	struct sk_buff *msg;
17614 
17615 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17616 	if (!msg)
17617 		return NULL;
17618 
17619 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17620 				  aborted ? NL80211_CMD_SCAN_ABORTED :
17621 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
17622 		nlmsg_free(msg);
17623 		return NULL;
17624 	}
17625 
17626 	return msg;
17627 }
17628 
17629 /* send message created by nl80211_build_scan_msg() */
17630 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
17631 			   struct sk_buff *msg)
17632 {
17633 	if (!msg)
17634 		return;
17635 
17636 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17637 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17638 }
17639 
17640 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
17641 {
17642 	struct sk_buff *msg;
17643 
17644 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17645 	if (!msg)
17646 		return;
17647 
17648 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
17649 		nlmsg_free(msg);
17650 		return;
17651 	}
17652 
17653 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
17654 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17655 }
17656 
17657 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
17658 					  struct regulatory_request *request)
17659 {
17660 	/* Userspace can always count this one always being set */
17661 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
17662 		goto nla_put_failure;
17663 
17664 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
17665 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17666 			       NL80211_REGDOM_TYPE_WORLD))
17667 			goto nla_put_failure;
17668 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
17669 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17670 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
17671 			goto nla_put_failure;
17672 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
17673 		   request->intersect) {
17674 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17675 			       NL80211_REGDOM_TYPE_INTERSECTION))
17676 			goto nla_put_failure;
17677 	} else {
17678 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17679 			       NL80211_REGDOM_TYPE_COUNTRY) ||
17680 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
17681 				   request->alpha2))
17682 			goto nla_put_failure;
17683 	}
17684 
17685 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
17686 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
17687 
17688 		if (wiphy &&
17689 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
17690 			goto nla_put_failure;
17691 
17692 		if (wiphy &&
17693 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
17694 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
17695 			goto nla_put_failure;
17696 	}
17697 
17698 	return true;
17699 
17700 nla_put_failure:
17701 	return false;
17702 }
17703 
17704 /*
17705  * This can happen on global regulatory changes or device specific settings
17706  * based on custom regulatory domains.
17707  */
17708 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
17709 				     struct regulatory_request *request)
17710 {
17711 	struct sk_buff *msg;
17712 	void *hdr;
17713 
17714 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17715 	if (!msg)
17716 		return;
17717 
17718 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
17719 	if (!hdr)
17720 		goto nla_put_failure;
17721 
17722 	if (!nl80211_reg_change_event_fill(msg, request))
17723 		goto nla_put_failure;
17724 
17725 	genlmsg_end(msg, hdr);
17726 
17727 	rcu_read_lock();
17728 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
17729 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
17730 	rcu_read_unlock();
17731 
17732 	return;
17733 
17734 nla_put_failure:
17735 	nlmsg_free(msg);
17736 }
17737 
17738 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
17739 				    struct net_device *netdev,
17740 				    const u8 *buf, size_t len,
17741 				    enum nl80211_commands cmd, gfp_t gfp,
17742 				    int uapsd_queues, const u8 *req_ies,
17743 				    size_t req_ies_len, bool reconnect)
17744 {
17745 	struct sk_buff *msg;
17746 	void *hdr;
17747 
17748 	msg = nlmsg_new(100 + len + req_ies_len, gfp);
17749 	if (!msg)
17750 		return;
17751 
17752 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17753 	if (!hdr) {
17754 		nlmsg_free(msg);
17755 		return;
17756 	}
17757 
17758 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17759 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17760 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
17761 	    (req_ies &&
17762 	     nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
17763 		goto nla_put_failure;
17764 
17765 	if (reconnect && nla_put_flag(msg, NL80211_ATTR_RECONNECT_REQUESTED))
17766 		goto nla_put_failure;
17767 
17768 	if (uapsd_queues >= 0) {
17769 		struct nlattr *nla_wmm =
17770 			nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
17771 		if (!nla_wmm)
17772 			goto nla_put_failure;
17773 
17774 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
17775 			       uapsd_queues))
17776 			goto nla_put_failure;
17777 
17778 		nla_nest_end(msg, nla_wmm);
17779 	}
17780 
17781 	genlmsg_end(msg, hdr);
17782 
17783 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17784 				NL80211_MCGRP_MLME, gfp);
17785 	return;
17786 
17787  nla_put_failure:
17788 	nlmsg_free(msg);
17789 }
17790 
17791 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
17792 			  struct net_device *netdev, const u8 *buf,
17793 			  size_t len, gfp_t gfp)
17794 {
17795 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17796 				NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0,
17797 				false);
17798 }
17799 
17800 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
17801 			   struct net_device *netdev,
17802 			   struct cfg80211_rx_assoc_resp_data *data)
17803 {
17804 	nl80211_send_mlme_event(rdev, netdev, data->buf, data->len,
17805 				NL80211_CMD_ASSOCIATE, GFP_KERNEL,
17806 				data->uapsd_queues,
17807 				data->req_ies, data->req_ies_len, false);
17808 }
17809 
17810 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
17811 			 struct net_device *netdev, const u8 *buf,
17812 			 size_t len, bool reconnect, gfp_t gfp)
17813 {
17814 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17815 				NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0,
17816 				reconnect);
17817 }
17818 
17819 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
17820 			   struct net_device *netdev, const u8 *buf,
17821 			   size_t len, bool reconnect, gfp_t gfp)
17822 {
17823 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17824 				NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0,
17825 				reconnect);
17826 }
17827 
17828 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
17829 				  size_t len)
17830 {
17831 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17832 	struct wiphy *wiphy = wdev->wiphy;
17833 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17834 	const struct ieee80211_mgmt *mgmt = (void *)buf;
17835 	u32 cmd;
17836 
17837 	if (WARN_ON(len < 2))
17838 		return;
17839 
17840 	if (ieee80211_is_deauth(mgmt->frame_control)) {
17841 		cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
17842 	} else if (ieee80211_is_disassoc(mgmt->frame_control)) {
17843 		cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
17844 	} else if (ieee80211_is_beacon(mgmt->frame_control)) {
17845 		if (wdev->unprot_beacon_reported &&
17846 		    elapsed_jiffies_msecs(wdev->unprot_beacon_reported) < 10000)
17847 			return;
17848 		cmd = NL80211_CMD_UNPROT_BEACON;
17849 		wdev->unprot_beacon_reported = jiffies;
17850 	} else {
17851 		return;
17852 	}
17853 
17854 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
17855 	nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
17856 				NULL, 0, false);
17857 }
17858 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
17859 
17860 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
17861 				      struct net_device *netdev, int cmd,
17862 				      const u8 *addr, gfp_t gfp)
17863 {
17864 	struct sk_buff *msg;
17865 	void *hdr;
17866 
17867 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17868 	if (!msg)
17869 		return;
17870 
17871 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17872 	if (!hdr) {
17873 		nlmsg_free(msg);
17874 		return;
17875 	}
17876 
17877 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17878 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17879 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
17880 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
17881 		goto nla_put_failure;
17882 
17883 	genlmsg_end(msg, hdr);
17884 
17885 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17886 				NL80211_MCGRP_MLME, gfp);
17887 	return;
17888 
17889  nla_put_failure:
17890 	nlmsg_free(msg);
17891 }
17892 
17893 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
17894 			       struct net_device *netdev, const u8 *addr,
17895 			       gfp_t gfp)
17896 {
17897 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
17898 				  addr, gfp);
17899 }
17900 
17901 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
17902 				struct net_device *netdev, const u8 *addr,
17903 				gfp_t gfp)
17904 {
17905 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
17906 				  addr, gfp);
17907 }
17908 
17909 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
17910 				 struct net_device *netdev,
17911 				 struct cfg80211_connect_resp_params *cr,
17912 				 gfp_t gfp)
17913 {
17914 	struct sk_buff *msg;
17915 	void *hdr;
17916 	unsigned int link;
17917 	size_t link_info_size = 0;
17918 	const u8 *connected_addr = cr->valid_links ?
17919 				   cr->ap_mld_addr : cr->links[0].bssid;
17920 
17921 	if (cr->valid_links) {
17922 		for_each_valid_link(cr, link) {
17923 			/* Nested attribute header */
17924 			link_info_size += NLA_HDRLEN;
17925 			/* Link ID */
17926 			link_info_size += nla_total_size(sizeof(u8));
17927 			link_info_size += cr->links[link].addr ?
17928 					  nla_total_size(ETH_ALEN) : 0;
17929 			link_info_size += (cr->links[link].bssid ||
17930 					   cr->links[link].bss) ?
17931 					  nla_total_size(ETH_ALEN) : 0;
17932 			link_info_size += nla_total_size(sizeof(u16));
17933 		}
17934 	}
17935 
17936 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
17937 			cr->fils.kek_len + cr->fils.pmk_len +
17938 			(cr->fils.pmkid ? WLAN_PMKID_LEN : 0) + link_info_size,
17939 			gfp);
17940 	if (!msg)
17941 		return;
17942 
17943 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
17944 	if (!hdr) {
17945 		nlmsg_free(msg);
17946 		return;
17947 	}
17948 
17949 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17950 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17951 	    (connected_addr &&
17952 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr)) ||
17953 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
17954 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
17955 			cr->status) ||
17956 	    (cr->status < 0 &&
17957 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
17958 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
17959 			  cr->timeout_reason))) ||
17960 	    (cr->req_ie &&
17961 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
17962 	    (cr->resp_ie &&
17963 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
17964 		     cr->resp_ie)) ||
17965 	    (cr->fils.update_erp_next_seq_num &&
17966 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
17967 			 cr->fils.erp_next_seq_num)) ||
17968 	    (cr->status == WLAN_STATUS_SUCCESS &&
17969 	     ((cr->fils.kek &&
17970 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
17971 		       cr->fils.kek)) ||
17972 	      (cr->fils.pmk &&
17973 	       nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
17974 	      (cr->fils.pmkid &&
17975 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
17976 		goto nla_put_failure;
17977 
17978 	if (cr->valid_links) {
17979 		int i = 1;
17980 		struct nlattr *nested;
17981 
17982 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
17983 		if (!nested)
17984 			goto nla_put_failure;
17985 
17986 		for_each_valid_link(cr, link) {
17987 			struct nlattr *nested_mlo_links;
17988 			const u8 *bssid = cr->links[link].bss ?
17989 					  cr->links[link].bss->bssid :
17990 					  cr->links[link].bssid;
17991 
17992 			nested_mlo_links = nla_nest_start(msg, i);
17993 			if (!nested_mlo_links)
17994 				goto nla_put_failure;
17995 
17996 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
17997 			    (bssid &&
17998 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
17999 			    (cr->links[link].addr &&
18000 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
18001 				     cr->links[link].addr)) ||
18002 			    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
18003 					cr->links[link].status))
18004 				goto nla_put_failure;
18005 
18006 			nla_nest_end(msg, nested_mlo_links);
18007 			i++;
18008 		}
18009 		nla_nest_end(msg, nested);
18010 	}
18011 
18012 	genlmsg_end(msg, hdr);
18013 
18014 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18015 				NL80211_MCGRP_MLME, gfp);
18016 	return;
18017 
18018  nla_put_failure:
18019 	nlmsg_free(msg);
18020 }
18021 
18022 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
18023 			 struct net_device *netdev,
18024 			 struct cfg80211_roam_info *info, gfp_t gfp)
18025 {
18026 	struct sk_buff *msg;
18027 	void *hdr;
18028 	size_t link_info_size = 0;
18029 	unsigned int link;
18030 	const u8 *connected_addr = info->ap_mld_addr ?
18031 				   info->ap_mld_addr :
18032 				   (info->links[0].bss ?
18033 				    info->links[0].bss->bssid :
18034 				    info->links[0].bssid);
18035 
18036 	if (info->valid_links) {
18037 		for_each_valid_link(info, link) {
18038 			/* Nested attribute header */
18039 			link_info_size += NLA_HDRLEN;
18040 			/* Link ID */
18041 			link_info_size += nla_total_size(sizeof(u8));
18042 			link_info_size += info->links[link].addr ?
18043 					  nla_total_size(ETH_ALEN) : 0;
18044 			link_info_size += (info->links[link].bssid ||
18045 					   info->links[link].bss) ?
18046 					  nla_total_size(ETH_ALEN) : 0;
18047 		}
18048 	}
18049 
18050 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
18051 			info->fils.kek_len + info->fils.pmk_len +
18052 			(info->fils.pmkid ? WLAN_PMKID_LEN : 0) +
18053 			link_info_size, gfp);
18054 	if (!msg)
18055 		return;
18056 
18057 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
18058 	if (!hdr) {
18059 		nlmsg_free(msg);
18060 		return;
18061 	}
18062 
18063 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18064 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18065 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr) ||
18066 	    (info->req_ie &&
18067 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
18068 		     info->req_ie)) ||
18069 	    (info->resp_ie &&
18070 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
18071 		     info->resp_ie)) ||
18072 	    (info->fils.update_erp_next_seq_num &&
18073 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
18074 			 info->fils.erp_next_seq_num)) ||
18075 	    (info->fils.kek &&
18076 	     nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
18077 		     info->fils.kek)) ||
18078 	    (info->fils.pmk &&
18079 	     nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
18080 	    (info->fils.pmkid &&
18081 	     nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
18082 		goto nla_put_failure;
18083 
18084 	if (info->valid_links) {
18085 		int i = 1;
18086 		struct nlattr *nested;
18087 
18088 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18089 		if (!nested)
18090 			goto nla_put_failure;
18091 
18092 		for_each_valid_link(info, link) {
18093 			struct nlattr *nested_mlo_links;
18094 			const u8 *bssid = info->links[link].bss ?
18095 					  info->links[link].bss->bssid :
18096 					  info->links[link].bssid;
18097 
18098 			nested_mlo_links = nla_nest_start(msg, i);
18099 			if (!nested_mlo_links)
18100 				goto nla_put_failure;
18101 
18102 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
18103 			    (bssid &&
18104 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
18105 			    (info->links[link].addr &&
18106 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
18107 				     info->links[link].addr)))
18108 				goto nla_put_failure;
18109 
18110 			nla_nest_end(msg, nested_mlo_links);
18111 			i++;
18112 		}
18113 		nla_nest_end(msg, nested);
18114 	}
18115 
18116 	genlmsg_end(msg, hdr);
18117 
18118 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18119 				NL80211_MCGRP_MLME, gfp);
18120 	return;
18121 
18122  nla_put_failure:
18123 	nlmsg_free(msg);
18124 }
18125 
18126 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
18127 				  struct net_device *netdev, const u8 *peer_addr,
18128 				  const u8 *td_bitmap, u8 td_bitmap_len)
18129 {
18130 	struct sk_buff *msg;
18131 	void *hdr;
18132 
18133 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18134 	if (!msg)
18135 		return;
18136 
18137 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
18138 	if (!hdr) {
18139 		nlmsg_free(msg);
18140 		return;
18141 	}
18142 
18143 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18144 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18145 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer_addr))
18146 		goto nla_put_failure;
18147 
18148 	if ((td_bitmap_len > 0) && td_bitmap)
18149 		if (nla_put(msg, NL80211_ATTR_TD_BITMAP,
18150 			    td_bitmap_len, td_bitmap))
18151 			goto nla_put_failure;
18152 
18153 	genlmsg_end(msg, hdr);
18154 
18155 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18156 				NL80211_MCGRP_MLME, GFP_KERNEL);
18157 	return;
18158 
18159  nla_put_failure:
18160 	nlmsg_free(msg);
18161 }
18162 
18163 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
18164 			       struct net_device *netdev, u16 reason,
18165 			       const u8 *ie, size_t ie_len, bool from_ap)
18166 {
18167 	struct sk_buff *msg;
18168 	void *hdr;
18169 
18170 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
18171 	if (!msg)
18172 		return;
18173 
18174 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
18175 	if (!hdr) {
18176 		nlmsg_free(msg);
18177 		return;
18178 	}
18179 
18180 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18181 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18182 	    (reason &&
18183 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
18184 	    (from_ap &&
18185 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
18186 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
18187 		goto nla_put_failure;
18188 
18189 	genlmsg_end(msg, hdr);
18190 
18191 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18192 				NL80211_MCGRP_MLME, GFP_KERNEL);
18193 	return;
18194 
18195  nla_put_failure:
18196 	nlmsg_free(msg);
18197 }
18198 
18199 void cfg80211_links_removed(struct net_device *dev, u16 link_mask)
18200 {
18201 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18202 	struct wiphy *wiphy = wdev->wiphy;
18203 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18204 	struct sk_buff *msg;
18205 	struct nlattr *links;
18206 	void *hdr;
18207 
18208 	lockdep_assert_wiphy(wdev->wiphy);
18209 	trace_cfg80211_links_removed(dev, link_mask);
18210 
18211 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
18212 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
18213 		return;
18214 
18215 	if (WARN_ON(!wdev->valid_links || !link_mask ||
18216 		    (wdev->valid_links & link_mask) != link_mask ||
18217 		    wdev->valid_links == link_mask))
18218 		return;
18219 
18220 	cfg80211_wdev_release_link_bsses(wdev, link_mask);
18221 	wdev->valid_links &= ~link_mask;
18222 
18223 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18224 	if (!msg)
18225 		return;
18226 
18227 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_LINKS_REMOVED);
18228 	if (!hdr) {
18229 		nlmsg_free(msg);
18230 		return;
18231 	}
18232 
18233 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18234 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
18235 		goto nla_put_failure;
18236 
18237 	links = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18238 	if (!links)
18239 		goto nla_put_failure;
18240 
18241 	while (link_mask) {
18242 		struct nlattr *link;
18243 		int link_id = __ffs(link_mask);
18244 
18245 		link = nla_nest_start(msg, link_id + 1);
18246 		if (!link)
18247 			goto nla_put_failure;
18248 
18249 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
18250 			goto nla_put_failure;
18251 
18252 		nla_nest_end(msg, link);
18253 		link_mask &= ~(1 << link_id);
18254 	}
18255 
18256 	nla_nest_end(msg, links);
18257 
18258 	genlmsg_end(msg, hdr);
18259 
18260 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18261 				NL80211_MCGRP_MLME, GFP_KERNEL);
18262 	return;
18263 
18264  nla_put_failure:
18265 	nlmsg_free(msg);
18266 }
18267 EXPORT_SYMBOL(cfg80211_links_removed);
18268 
18269 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
18270 			     struct net_device *netdev, const u8 *bssid,
18271 			     gfp_t gfp)
18272 {
18273 	struct sk_buff *msg;
18274 	void *hdr;
18275 
18276 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18277 	if (!msg)
18278 		return;
18279 
18280 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
18281 	if (!hdr) {
18282 		nlmsg_free(msg);
18283 		return;
18284 	}
18285 
18286 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18287 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18288 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18289 		goto nla_put_failure;
18290 
18291 	genlmsg_end(msg, hdr);
18292 
18293 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18294 				NL80211_MCGRP_MLME, gfp);
18295 	return;
18296 
18297  nla_put_failure:
18298 	nlmsg_free(msg);
18299 }
18300 
18301 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
18302 					const u8 *ie, u8 ie_len,
18303 					int sig_dbm, gfp_t gfp)
18304 {
18305 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18306 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18307 	struct sk_buff *msg;
18308 	void *hdr;
18309 
18310 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
18311 		return;
18312 
18313 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
18314 
18315 	msg = nlmsg_new(100 + ie_len, gfp);
18316 	if (!msg)
18317 		return;
18318 
18319 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
18320 	if (!hdr) {
18321 		nlmsg_free(msg);
18322 		return;
18323 	}
18324 
18325 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18326 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18327 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
18328 	    (ie_len && ie &&
18329 	     nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
18330 	    (sig_dbm &&
18331 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
18332 		goto nla_put_failure;
18333 
18334 	genlmsg_end(msg, hdr);
18335 
18336 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18337 				NL80211_MCGRP_MLME, gfp);
18338 	return;
18339 
18340  nla_put_failure:
18341 	nlmsg_free(msg);
18342 }
18343 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
18344 
18345 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
18346 				 struct net_device *netdev, const u8 *addr,
18347 				 enum nl80211_key_type key_type, int key_id,
18348 				 const u8 *tsc, gfp_t gfp)
18349 {
18350 	struct sk_buff *msg;
18351 	void *hdr;
18352 
18353 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18354 	if (!msg)
18355 		return;
18356 
18357 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
18358 	if (!hdr) {
18359 		nlmsg_free(msg);
18360 		return;
18361 	}
18362 
18363 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18364 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18365 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
18366 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
18367 	    (key_id != -1 &&
18368 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
18369 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
18370 		goto nla_put_failure;
18371 
18372 	genlmsg_end(msg, hdr);
18373 
18374 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18375 				NL80211_MCGRP_MLME, gfp);
18376 	return;
18377 
18378  nla_put_failure:
18379 	nlmsg_free(msg);
18380 }
18381 
18382 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
18383 				    struct ieee80211_channel *channel_before,
18384 				    struct ieee80211_channel *channel_after)
18385 {
18386 	struct sk_buff *msg;
18387 	void *hdr;
18388 	struct nlattr *nl_freq;
18389 
18390 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
18391 	if (!msg)
18392 		return;
18393 
18394 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
18395 	if (!hdr) {
18396 		nlmsg_free(msg);
18397 		return;
18398 	}
18399 
18400 	/*
18401 	 * Since we are applying the beacon hint to a wiphy we know its
18402 	 * wiphy_idx is valid
18403 	 */
18404 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
18405 		goto nla_put_failure;
18406 
18407 	/* Before */
18408 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
18409 	if (!nl_freq)
18410 		goto nla_put_failure;
18411 
18412 	if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
18413 		goto nla_put_failure;
18414 	nla_nest_end(msg, nl_freq);
18415 
18416 	/* After */
18417 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
18418 	if (!nl_freq)
18419 		goto nla_put_failure;
18420 
18421 	if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
18422 		goto nla_put_failure;
18423 	nla_nest_end(msg, nl_freq);
18424 
18425 	genlmsg_end(msg, hdr);
18426 
18427 	rcu_read_lock();
18428 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
18429 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
18430 	rcu_read_unlock();
18431 
18432 	return;
18433 
18434 nla_put_failure:
18435 	nlmsg_free(msg);
18436 }
18437 
18438 static void nl80211_send_remain_on_chan_event(
18439 	int cmd, struct cfg80211_registered_device *rdev,
18440 	struct wireless_dev *wdev, u64 cookie,
18441 	struct ieee80211_channel *chan,
18442 	unsigned int duration, gfp_t gfp)
18443 {
18444 	struct sk_buff *msg;
18445 	void *hdr;
18446 
18447 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18448 	if (!msg)
18449 		return;
18450 
18451 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18452 	if (!hdr) {
18453 		nlmsg_free(msg);
18454 		return;
18455 	}
18456 
18457 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18458 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18459 					 wdev->netdev->ifindex)) ||
18460 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18461 			      NL80211_ATTR_PAD) ||
18462 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
18463 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
18464 			NL80211_CHAN_NO_HT) ||
18465 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
18466 			      NL80211_ATTR_PAD))
18467 		goto nla_put_failure;
18468 
18469 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
18470 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
18471 		goto nla_put_failure;
18472 
18473 	genlmsg_end(msg, hdr);
18474 
18475 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18476 				NL80211_MCGRP_MLME, gfp);
18477 	return;
18478 
18479  nla_put_failure:
18480 	nlmsg_free(msg);
18481 }
18482 
18483 void cfg80211_assoc_comeback(struct net_device *netdev,
18484 			     const u8 *ap_addr, u32 timeout)
18485 {
18486 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
18487 	struct wiphy *wiphy = wdev->wiphy;
18488 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18489 	struct sk_buff *msg;
18490 	void *hdr;
18491 
18492 	trace_cfg80211_assoc_comeback(wdev, ap_addr, timeout);
18493 
18494 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18495 	if (!msg)
18496 		return;
18497 
18498 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ASSOC_COMEBACK);
18499 	if (!hdr) {
18500 		nlmsg_free(msg);
18501 		return;
18502 	}
18503 
18504 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18505 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18506 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ap_addr) ||
18507 	    nla_put_u32(msg, NL80211_ATTR_TIMEOUT, timeout))
18508 		goto nla_put_failure;
18509 
18510 	genlmsg_end(msg, hdr);
18511 
18512 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18513 				NL80211_MCGRP_MLME, GFP_KERNEL);
18514 	return;
18515 
18516  nla_put_failure:
18517 	nlmsg_free(msg);
18518 }
18519 EXPORT_SYMBOL(cfg80211_assoc_comeback);
18520 
18521 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
18522 			       struct ieee80211_channel *chan,
18523 			       unsigned int duration, gfp_t gfp)
18524 {
18525 	struct wiphy *wiphy = wdev->wiphy;
18526 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18527 
18528 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
18529 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
18530 					  rdev, wdev, cookie, chan,
18531 					  duration, gfp);
18532 }
18533 EXPORT_SYMBOL(cfg80211_ready_on_channel);
18534 
18535 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
18536 					struct ieee80211_channel *chan,
18537 					gfp_t gfp)
18538 {
18539 	struct wiphy *wiphy = wdev->wiphy;
18540 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18541 
18542 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
18543 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
18544 					  rdev, wdev, cookie, chan, 0, gfp);
18545 }
18546 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
18547 
18548 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
18549 					struct ieee80211_channel *chan,
18550 					gfp_t gfp)
18551 {
18552 	struct wiphy *wiphy = wdev->wiphy;
18553 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18554 
18555 	trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
18556 	nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
18557 					  rdev, wdev, cookie, chan, 0, gfp);
18558 }
18559 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
18560 
18561 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
18562 		      struct station_info *sinfo, gfp_t gfp)
18563 {
18564 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18565 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18566 	struct sk_buff *msg;
18567 
18568 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
18569 
18570 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18571 	if (!msg)
18572 		return;
18573 
18574 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
18575 				 rdev, dev, mac_addr, sinfo) < 0) {
18576 		nlmsg_free(msg);
18577 		return;
18578 	}
18579 
18580 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18581 				NL80211_MCGRP_MLME, gfp);
18582 }
18583 EXPORT_SYMBOL(cfg80211_new_sta);
18584 
18585 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
18586 			    struct station_info *sinfo, gfp_t gfp)
18587 {
18588 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18589 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18590 	struct sk_buff *msg;
18591 	struct station_info empty_sinfo = {};
18592 
18593 	if (!sinfo)
18594 		sinfo = &empty_sinfo;
18595 
18596 	trace_cfg80211_del_sta(dev, mac_addr);
18597 
18598 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18599 	if (!msg) {
18600 		cfg80211_sinfo_release_content(sinfo);
18601 		return;
18602 	}
18603 
18604 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
18605 				 rdev, dev, mac_addr, sinfo) < 0) {
18606 		nlmsg_free(msg);
18607 		return;
18608 	}
18609 
18610 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18611 				NL80211_MCGRP_MLME, gfp);
18612 }
18613 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
18614 
18615 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
18616 			  enum nl80211_connect_failed_reason reason,
18617 			  gfp_t gfp)
18618 {
18619 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18620 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18621 	struct sk_buff *msg;
18622 	void *hdr;
18623 
18624 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
18625 	if (!msg)
18626 		return;
18627 
18628 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
18629 	if (!hdr) {
18630 		nlmsg_free(msg);
18631 		return;
18632 	}
18633 
18634 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18635 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
18636 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
18637 		goto nla_put_failure;
18638 
18639 	genlmsg_end(msg, hdr);
18640 
18641 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18642 				NL80211_MCGRP_MLME, gfp);
18643 	return;
18644 
18645  nla_put_failure:
18646 	nlmsg_free(msg);
18647 }
18648 EXPORT_SYMBOL(cfg80211_conn_failed);
18649 
18650 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
18651 				       const u8 *addr, gfp_t gfp)
18652 {
18653 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18654 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18655 	struct sk_buff *msg;
18656 	void *hdr;
18657 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
18658 
18659 	if (!nlportid)
18660 		return false;
18661 
18662 	msg = nlmsg_new(100, gfp);
18663 	if (!msg)
18664 		return true;
18665 
18666 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18667 	if (!hdr) {
18668 		nlmsg_free(msg);
18669 		return true;
18670 	}
18671 
18672 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18673 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18674 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
18675 		goto nla_put_failure;
18676 
18677 	genlmsg_end(msg, hdr);
18678 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18679 	return true;
18680 
18681  nla_put_failure:
18682 	nlmsg_free(msg);
18683 	return true;
18684 }
18685 
18686 bool cfg80211_rx_spurious_frame(struct net_device *dev,
18687 				const u8 *addr, gfp_t gfp)
18688 {
18689 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18690 	bool ret;
18691 
18692 	trace_cfg80211_rx_spurious_frame(dev, addr);
18693 
18694 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18695 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
18696 		trace_cfg80211_return_bool(false);
18697 		return false;
18698 	}
18699 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
18700 					 addr, gfp);
18701 	trace_cfg80211_return_bool(ret);
18702 	return ret;
18703 }
18704 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
18705 
18706 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
18707 					const u8 *addr, gfp_t gfp)
18708 {
18709 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18710 	bool ret;
18711 
18712 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
18713 
18714 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18715 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
18716 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
18717 		trace_cfg80211_return_bool(false);
18718 		return false;
18719 	}
18720 	ret = __nl80211_unexpected_frame(dev,
18721 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
18722 					 addr, gfp);
18723 	trace_cfg80211_return_bool(ret);
18724 	return ret;
18725 }
18726 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
18727 
18728 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
18729 		      struct wireless_dev *wdev, u32 nlportid,
18730 		      struct cfg80211_rx_info *info, gfp_t gfp)
18731 {
18732 	struct net_device *netdev = wdev->netdev;
18733 	struct sk_buff *msg;
18734 	void *hdr;
18735 
18736 	msg = nlmsg_new(100 + info->len, gfp);
18737 	if (!msg)
18738 		return -ENOMEM;
18739 
18740 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
18741 	if (!hdr) {
18742 		nlmsg_free(msg);
18743 		return -ENOMEM;
18744 	}
18745 
18746 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18747 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18748 					netdev->ifindex)) ||
18749 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18750 			      NL80211_ATTR_PAD) ||
18751 	    (info->have_link_id &&
18752 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, info->link_id)) ||
18753 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, KHZ_TO_MHZ(info->freq)) ||
18754 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET, info->freq % 1000) ||
18755 	    (info->sig_dbm &&
18756 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, info->sig_dbm)) ||
18757 	    nla_put(msg, NL80211_ATTR_FRAME, info->len, info->buf) ||
18758 	    (info->flags &&
18759 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, info->flags)) ||
18760 	    (info->rx_tstamp && nla_put_u64_64bit(msg,
18761 						  NL80211_ATTR_RX_HW_TIMESTAMP,
18762 						  info->rx_tstamp,
18763 						  NL80211_ATTR_PAD)) ||
18764 	    (info->ack_tstamp && nla_put_u64_64bit(msg,
18765 						   NL80211_ATTR_TX_HW_TIMESTAMP,
18766 						   info->ack_tstamp,
18767 						   NL80211_ATTR_PAD)))
18768 		goto nla_put_failure;
18769 
18770 	genlmsg_end(msg, hdr);
18771 
18772 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18773 
18774  nla_put_failure:
18775 	nlmsg_free(msg);
18776 	return -ENOBUFS;
18777 }
18778 
18779 static void nl80211_frame_tx_status(struct wireless_dev *wdev,
18780 				    struct cfg80211_tx_status *status,
18781 				    gfp_t gfp, enum nl80211_commands command)
18782 {
18783 	struct wiphy *wiphy = wdev->wiphy;
18784 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18785 	struct net_device *netdev = wdev->netdev;
18786 	struct sk_buff *msg;
18787 	void *hdr;
18788 
18789 	if (command == NL80211_CMD_FRAME_TX_STATUS)
18790 		trace_cfg80211_mgmt_tx_status(wdev, status->cookie,
18791 					      status->ack);
18792 	else
18793 		trace_cfg80211_control_port_tx_status(wdev, status->cookie,
18794 						      status->ack);
18795 
18796 	msg = nlmsg_new(100 + status->len, gfp);
18797 	if (!msg)
18798 		return;
18799 
18800 	hdr = nl80211hdr_put(msg, 0, 0, 0, command);
18801 	if (!hdr) {
18802 		nlmsg_free(msg);
18803 		return;
18804 	}
18805 
18806 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18807 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18808 				   netdev->ifindex)) ||
18809 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18810 			      NL80211_ATTR_PAD) ||
18811 	    nla_put(msg, NL80211_ATTR_FRAME, status->len, status->buf) ||
18812 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, status->cookie,
18813 			      NL80211_ATTR_PAD) ||
18814 	    (status->ack && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
18815 	    (status->tx_tstamp &&
18816 	     nla_put_u64_64bit(msg, NL80211_ATTR_TX_HW_TIMESTAMP,
18817 			       status->tx_tstamp, NL80211_ATTR_PAD)) ||
18818 	    (status->ack_tstamp &&
18819 	     nla_put_u64_64bit(msg, NL80211_ATTR_RX_HW_TIMESTAMP,
18820 			       status->ack_tstamp, NL80211_ATTR_PAD)))
18821 		goto nla_put_failure;
18822 
18823 	genlmsg_end(msg, hdr);
18824 
18825 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18826 				NL80211_MCGRP_MLME, gfp);
18827 	return;
18828 
18829 nla_put_failure:
18830 	nlmsg_free(msg);
18831 }
18832 
18833 void cfg80211_control_port_tx_status(struct wireless_dev *wdev, u64 cookie,
18834 				     const u8 *buf, size_t len, bool ack,
18835 				     gfp_t gfp)
18836 {
18837 	struct cfg80211_tx_status status = {
18838 		.cookie = cookie,
18839 		.buf = buf,
18840 		.len = len,
18841 		.ack = ack
18842 	};
18843 
18844 	nl80211_frame_tx_status(wdev, &status, gfp,
18845 				NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS);
18846 }
18847 EXPORT_SYMBOL(cfg80211_control_port_tx_status);
18848 
18849 void cfg80211_mgmt_tx_status_ext(struct wireless_dev *wdev,
18850 				 struct cfg80211_tx_status *status, gfp_t gfp)
18851 {
18852 	nl80211_frame_tx_status(wdev, status, gfp, NL80211_CMD_FRAME_TX_STATUS);
18853 }
18854 EXPORT_SYMBOL(cfg80211_mgmt_tx_status_ext);
18855 
18856 static int __nl80211_rx_control_port(struct net_device *dev,
18857 				     struct sk_buff *skb,
18858 				     bool unencrypted,
18859 				     int link_id,
18860 				     gfp_t gfp)
18861 {
18862 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18863 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18864 	struct ethhdr *ehdr = eth_hdr(skb);
18865 	const u8 *addr = ehdr->h_source;
18866 	u16 proto = be16_to_cpu(skb->protocol);
18867 	struct sk_buff *msg;
18868 	void *hdr;
18869 	struct nlattr *frame;
18870 
18871 	u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
18872 
18873 	if (!nlportid)
18874 		return -ENOENT;
18875 
18876 	msg = nlmsg_new(100 + skb->len, gfp);
18877 	if (!msg)
18878 		return -ENOMEM;
18879 
18880 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
18881 	if (!hdr) {
18882 		nlmsg_free(msg);
18883 		return -ENOBUFS;
18884 	}
18885 
18886 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18887 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18888 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18889 			      NL80211_ATTR_PAD) ||
18890 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
18891 	    nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
18892 	    (link_id >= 0 &&
18893 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
18894 	    (unencrypted && nla_put_flag(msg,
18895 					 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
18896 		goto nla_put_failure;
18897 
18898 	frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
18899 	if (!frame)
18900 		goto nla_put_failure;
18901 
18902 	skb_copy_bits(skb, 0, nla_data(frame), skb->len);
18903 	genlmsg_end(msg, hdr);
18904 
18905 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18906 
18907  nla_put_failure:
18908 	nlmsg_free(msg);
18909 	return -ENOBUFS;
18910 }
18911 
18912 bool cfg80211_rx_control_port(struct net_device *dev, struct sk_buff *skb,
18913 			      bool unencrypted, int link_id)
18914 {
18915 	int ret;
18916 
18917 	trace_cfg80211_rx_control_port(dev, skb, unencrypted, link_id);
18918 	ret = __nl80211_rx_control_port(dev, skb, unencrypted, link_id,
18919 					GFP_ATOMIC);
18920 	trace_cfg80211_return_bool(ret == 0);
18921 	return ret == 0;
18922 }
18923 EXPORT_SYMBOL(cfg80211_rx_control_port);
18924 
18925 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
18926 					    const char *mac, gfp_t gfp)
18927 {
18928 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18929 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18930 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18931 	void **cb;
18932 
18933 	if (!msg)
18934 		return NULL;
18935 
18936 	cb = (void **)msg->cb;
18937 
18938 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
18939 	if (!cb[0]) {
18940 		nlmsg_free(msg);
18941 		return NULL;
18942 	}
18943 
18944 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18945 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
18946 		goto nla_put_failure;
18947 
18948 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
18949 		goto nla_put_failure;
18950 
18951 	cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
18952 	if (!cb[1])
18953 		goto nla_put_failure;
18954 
18955 	cb[2] = rdev;
18956 
18957 	return msg;
18958  nla_put_failure:
18959 	nlmsg_free(msg);
18960 	return NULL;
18961 }
18962 
18963 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
18964 {
18965 	void **cb = (void **)msg->cb;
18966 	struct cfg80211_registered_device *rdev = cb[2];
18967 
18968 	nla_nest_end(msg, cb[1]);
18969 	genlmsg_end(msg, cb[0]);
18970 
18971 	memset(msg->cb, 0, sizeof(msg->cb));
18972 
18973 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18974 				NL80211_MCGRP_MLME, gfp);
18975 }
18976 
18977 void cfg80211_cqm_rssi_notify(struct net_device *dev,
18978 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
18979 			      s32 rssi_level, gfp_t gfp)
18980 {
18981 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18982 	struct cfg80211_cqm_config *cqm_config;
18983 
18984 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
18985 
18986 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
18987 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
18988 		return;
18989 
18990 	rcu_read_lock();
18991 	cqm_config = rcu_dereference(wdev->cqm_config);
18992 	if (cqm_config) {
18993 		cqm_config->last_rssi_event_value = rssi_level;
18994 		cqm_config->last_rssi_event_type = rssi_event;
18995 		wiphy_work_queue(wdev->wiphy, &wdev->cqm_rssi_work);
18996 	}
18997 	rcu_read_unlock();
18998 }
18999 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
19000 
19001 void cfg80211_cqm_rssi_notify_work(struct wiphy *wiphy, struct wiphy_work *work)
19002 {
19003 	struct wireless_dev *wdev = container_of(work, struct wireless_dev,
19004 						 cqm_rssi_work);
19005 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19006 	enum nl80211_cqm_rssi_threshold_event rssi_event;
19007 	struct cfg80211_cqm_config *cqm_config;
19008 	struct sk_buff *msg;
19009 	s32 rssi_level;
19010 
19011 	cqm_config = wiphy_dereference(wdev->wiphy, wdev->cqm_config);
19012 	if (!wdev->cqm_config)
19013 		return;
19014 
19015 	cfg80211_cqm_rssi_update(rdev, wdev->netdev, cqm_config);
19016 
19017 	rssi_level = cqm_config->last_rssi_event_value;
19018 	rssi_event = cqm_config->last_rssi_event_type;
19019 
19020 	msg = cfg80211_prepare_cqm(wdev->netdev, NULL, GFP_KERNEL);
19021 	if (!msg)
19022 		return;
19023 
19024 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
19025 			rssi_event))
19026 		goto nla_put_failure;
19027 
19028 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
19029 				      rssi_level))
19030 		goto nla_put_failure;
19031 
19032 	cfg80211_send_cqm(msg, GFP_KERNEL);
19033 
19034 	return;
19035 
19036  nla_put_failure:
19037 	nlmsg_free(msg);
19038 }
19039 
19040 void cfg80211_cqm_txe_notify(struct net_device *dev,
19041 			     const u8 *peer, u32 num_packets,
19042 			     u32 rate, u32 intvl, gfp_t gfp)
19043 {
19044 	struct sk_buff *msg;
19045 
19046 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
19047 	if (!msg)
19048 		return;
19049 
19050 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
19051 		goto nla_put_failure;
19052 
19053 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
19054 		goto nla_put_failure;
19055 
19056 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
19057 		goto nla_put_failure;
19058 
19059 	cfg80211_send_cqm(msg, gfp);
19060 	return;
19061 
19062  nla_put_failure:
19063 	nlmsg_free(msg);
19064 }
19065 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
19066 
19067 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
19068 				 const u8 *peer, u32 num_packets, gfp_t gfp)
19069 {
19070 	struct sk_buff *msg;
19071 
19072 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
19073 
19074 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
19075 	if (!msg)
19076 		return;
19077 
19078 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
19079 		goto nla_put_failure;
19080 
19081 	cfg80211_send_cqm(msg, gfp);
19082 	return;
19083 
19084  nla_put_failure:
19085 	nlmsg_free(msg);
19086 }
19087 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
19088 
19089 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
19090 {
19091 	struct sk_buff *msg;
19092 
19093 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
19094 	if (!msg)
19095 		return;
19096 
19097 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
19098 		goto nla_put_failure;
19099 
19100 	cfg80211_send_cqm(msg, gfp);
19101 	return;
19102 
19103  nla_put_failure:
19104 	nlmsg_free(msg);
19105 }
19106 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
19107 
19108 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
19109 				     struct net_device *netdev, const u8 *bssid,
19110 				     const u8 *replay_ctr, gfp_t gfp)
19111 {
19112 	struct sk_buff *msg;
19113 	struct nlattr *rekey_attr;
19114 	void *hdr;
19115 
19116 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19117 	if (!msg)
19118 		return;
19119 
19120 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
19121 	if (!hdr) {
19122 		nlmsg_free(msg);
19123 		return;
19124 	}
19125 
19126 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19127 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19128 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
19129 		goto nla_put_failure;
19130 
19131 	rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
19132 	if (!rekey_attr)
19133 		goto nla_put_failure;
19134 
19135 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
19136 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
19137 		goto nla_put_failure;
19138 
19139 	nla_nest_end(msg, rekey_attr);
19140 
19141 	genlmsg_end(msg, hdr);
19142 
19143 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19144 				NL80211_MCGRP_MLME, gfp);
19145 	return;
19146 
19147  nla_put_failure:
19148 	nlmsg_free(msg);
19149 }
19150 
19151 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
19152 			       const u8 *replay_ctr, gfp_t gfp)
19153 {
19154 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19155 	struct wiphy *wiphy = wdev->wiphy;
19156 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19157 
19158 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
19159 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
19160 }
19161 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
19162 
19163 static void
19164 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
19165 			       struct net_device *netdev, int index,
19166 			       const u8 *bssid, bool preauth, gfp_t gfp)
19167 {
19168 	struct sk_buff *msg;
19169 	struct nlattr *attr;
19170 	void *hdr;
19171 
19172 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19173 	if (!msg)
19174 		return;
19175 
19176 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
19177 	if (!hdr) {
19178 		nlmsg_free(msg);
19179 		return;
19180 	}
19181 
19182 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19183 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
19184 		goto nla_put_failure;
19185 
19186 	attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
19187 	if (!attr)
19188 		goto nla_put_failure;
19189 
19190 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
19191 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
19192 	    (preauth &&
19193 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
19194 		goto nla_put_failure;
19195 
19196 	nla_nest_end(msg, attr);
19197 
19198 	genlmsg_end(msg, hdr);
19199 
19200 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19201 				NL80211_MCGRP_MLME, gfp);
19202 	return;
19203 
19204  nla_put_failure:
19205 	nlmsg_free(msg);
19206 }
19207 
19208 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
19209 				     const u8 *bssid, bool preauth, gfp_t gfp)
19210 {
19211 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19212 	struct wiphy *wiphy = wdev->wiphy;
19213 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19214 
19215 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
19216 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
19217 }
19218 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
19219 
19220 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
19221 				     struct net_device *netdev,
19222 				     unsigned int link_id,
19223 				     struct cfg80211_chan_def *chandef,
19224 				     gfp_t gfp,
19225 				     enum nl80211_commands notif,
19226 				     u8 count, bool quiet, u16 punct_bitmap)
19227 {
19228 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
19229 	struct sk_buff *msg;
19230 	void *hdr;
19231 
19232 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19233 	if (!msg)
19234 		return;
19235 
19236 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
19237 	if (!hdr) {
19238 		nlmsg_free(msg);
19239 		return;
19240 	}
19241 
19242 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
19243 		goto nla_put_failure;
19244 
19245 	if (wdev->valid_links &&
19246 	    nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
19247 		goto nla_put_failure;
19248 
19249 	if (nl80211_send_chandef(msg, chandef))
19250 		goto nla_put_failure;
19251 
19252 	if (notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) {
19253 		if (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count))
19254 			goto nla_put_failure;
19255 		if (quiet &&
19256 		    nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX))
19257 			goto nla_put_failure;
19258 	}
19259 
19260 	if (nla_put_u32(msg, NL80211_ATTR_PUNCT_BITMAP, punct_bitmap))
19261 		goto nla_put_failure;
19262 
19263 	genlmsg_end(msg, hdr);
19264 
19265 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19266 				NL80211_MCGRP_MLME, gfp);
19267 	return;
19268 
19269  nla_put_failure:
19270 	nlmsg_free(msg);
19271 }
19272 
19273 void cfg80211_ch_switch_notify(struct net_device *dev,
19274 			       struct cfg80211_chan_def *chandef,
19275 			       unsigned int link_id, u16 punct_bitmap)
19276 {
19277 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19278 	struct wiphy *wiphy = wdev->wiphy;
19279 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19280 
19281 	lockdep_assert_wiphy(wdev->wiphy);
19282 	WARN_INVALID_LINK_ID(wdev, link_id);
19283 
19284 	trace_cfg80211_ch_switch_notify(dev, chandef, link_id, punct_bitmap);
19285 
19286 	switch (wdev->iftype) {
19287 	case NL80211_IFTYPE_STATION:
19288 	case NL80211_IFTYPE_P2P_CLIENT:
19289 		if (!WARN_ON(!wdev->links[link_id].client.current_bss))
19290 			cfg80211_update_assoc_bss_entry(wdev, link_id,
19291 							chandef->chan);
19292 		break;
19293 	case NL80211_IFTYPE_MESH_POINT:
19294 		wdev->u.mesh.chandef = *chandef;
19295 		wdev->u.mesh.preset_chandef = *chandef;
19296 		break;
19297 	case NL80211_IFTYPE_AP:
19298 	case NL80211_IFTYPE_P2P_GO:
19299 		wdev->links[link_id].ap.chandef = *chandef;
19300 		break;
19301 	case NL80211_IFTYPE_ADHOC:
19302 		wdev->u.ibss.chandef = *chandef;
19303 		break;
19304 	default:
19305 		WARN_ON(1);
19306 		break;
19307 	}
19308 
19309 	cfg80211_sched_dfs_chan_update(rdev);
19310 
19311 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19312 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0, false,
19313 				 punct_bitmap);
19314 }
19315 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
19316 
19317 void cfg80211_ch_switch_started_notify(struct net_device *dev,
19318 				       struct cfg80211_chan_def *chandef,
19319 				       unsigned int link_id, u8 count,
19320 				       bool quiet, u16 punct_bitmap)
19321 {
19322 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19323 	struct wiphy *wiphy = wdev->wiphy;
19324 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19325 
19326 	lockdep_assert_wiphy(wdev->wiphy);
19327 	WARN_INVALID_LINK_ID(wdev, link_id);
19328 
19329 	trace_cfg80211_ch_switch_started_notify(dev, chandef, link_id,
19330 						punct_bitmap);
19331 
19332 
19333 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19334 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY,
19335 				 count, quiet, punct_bitmap);
19336 }
19337 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
19338 
19339 int cfg80211_bss_color_notify(struct net_device *dev,
19340 			      enum nl80211_commands cmd, u8 count,
19341 			      u64 color_bitmap)
19342 {
19343 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19344 	struct wiphy *wiphy = wdev->wiphy;
19345 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19346 	struct sk_buff *msg;
19347 	void *hdr;
19348 
19349 	lockdep_assert_wiphy(wdev->wiphy);
19350 
19351 	trace_cfg80211_bss_color_notify(dev, cmd, count, color_bitmap);
19352 
19353 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
19354 	if (!msg)
19355 		return -ENOMEM;
19356 
19357 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
19358 	if (!hdr)
19359 		goto nla_put_failure;
19360 
19361 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19362 		goto nla_put_failure;
19363 
19364 	if (cmd == NL80211_CMD_COLOR_CHANGE_STARTED &&
19365 	    nla_put_u32(msg, NL80211_ATTR_COLOR_CHANGE_COUNT, count))
19366 		goto nla_put_failure;
19367 
19368 	if (cmd == NL80211_CMD_OBSS_COLOR_COLLISION &&
19369 	    nla_put_u64_64bit(msg, NL80211_ATTR_OBSS_COLOR_BITMAP,
19370 			      color_bitmap, NL80211_ATTR_PAD))
19371 		goto nla_put_failure;
19372 
19373 	genlmsg_end(msg, hdr);
19374 
19375 	return genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
19376 				       msg, 0, NL80211_MCGRP_MLME, GFP_KERNEL);
19377 
19378 nla_put_failure:
19379 	nlmsg_free(msg);
19380 	return -EINVAL;
19381 }
19382 EXPORT_SYMBOL(cfg80211_bss_color_notify);
19383 
19384 void
19385 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
19386 		     const struct cfg80211_chan_def *chandef,
19387 		     enum nl80211_radar_event event,
19388 		     struct net_device *netdev, gfp_t gfp)
19389 {
19390 	struct sk_buff *msg;
19391 	void *hdr;
19392 
19393 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19394 	if (!msg)
19395 		return;
19396 
19397 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
19398 	if (!hdr) {
19399 		nlmsg_free(msg);
19400 		return;
19401 	}
19402 
19403 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19404 		goto nla_put_failure;
19405 
19406 	/* NOP and radar events don't need a netdev parameter */
19407 	if (netdev) {
19408 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
19409 
19410 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19411 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19412 				      NL80211_ATTR_PAD))
19413 			goto nla_put_failure;
19414 	}
19415 
19416 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
19417 		goto nla_put_failure;
19418 
19419 	if (nl80211_send_chandef(msg, chandef))
19420 		goto nla_put_failure;
19421 
19422 	genlmsg_end(msg, hdr);
19423 
19424 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19425 				NL80211_MCGRP_MLME, gfp);
19426 	return;
19427 
19428  nla_put_failure:
19429 	nlmsg_free(msg);
19430 }
19431 
19432 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
19433 				       struct sta_opmode_info *sta_opmode,
19434 				       gfp_t gfp)
19435 {
19436 	struct sk_buff *msg;
19437 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19438 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19439 	void *hdr;
19440 
19441 	if (WARN_ON(!mac))
19442 		return;
19443 
19444 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19445 	if (!msg)
19446 		return;
19447 
19448 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
19449 	if (!hdr) {
19450 		nlmsg_free(msg);
19451 		return;
19452 	}
19453 
19454 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19455 		goto nla_put_failure;
19456 
19457 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19458 		goto nla_put_failure;
19459 
19460 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
19461 		goto nla_put_failure;
19462 
19463 	if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
19464 	    nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
19465 		goto nla_put_failure;
19466 
19467 	if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
19468 	    nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
19469 		goto nla_put_failure;
19470 
19471 	if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
19472 	    nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
19473 		goto nla_put_failure;
19474 
19475 	genlmsg_end(msg, hdr);
19476 
19477 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19478 				NL80211_MCGRP_MLME, gfp);
19479 
19480 	return;
19481 
19482 nla_put_failure:
19483 	nlmsg_free(msg);
19484 }
19485 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
19486 
19487 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
19488 			   u64 cookie, bool acked, s32 ack_signal,
19489 			   bool is_valid_ack_signal, gfp_t gfp)
19490 {
19491 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19492 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19493 	struct sk_buff *msg;
19494 	void *hdr;
19495 
19496 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
19497 
19498 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19499 
19500 	if (!msg)
19501 		return;
19502 
19503 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
19504 	if (!hdr) {
19505 		nlmsg_free(msg);
19506 		return;
19507 	}
19508 
19509 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19510 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19511 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
19512 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
19513 			      NL80211_ATTR_PAD) ||
19514 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
19515 	    (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
19516 						ack_signal)))
19517 		goto nla_put_failure;
19518 
19519 	genlmsg_end(msg, hdr);
19520 
19521 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19522 				NL80211_MCGRP_MLME, gfp);
19523 	return;
19524 
19525  nla_put_failure:
19526 	nlmsg_free(msg);
19527 }
19528 EXPORT_SYMBOL(cfg80211_probe_status);
19529 
19530 void cfg80211_report_obss_beacon_khz(struct wiphy *wiphy, const u8 *frame,
19531 				     size_t len, int freq, int sig_dbm)
19532 {
19533 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19534 	struct sk_buff *msg;
19535 	void *hdr;
19536 	struct cfg80211_beacon_registration *reg;
19537 
19538 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
19539 
19540 	spin_lock_bh(&rdev->beacon_registrations_lock);
19541 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
19542 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
19543 		if (!msg) {
19544 			spin_unlock_bh(&rdev->beacon_registrations_lock);
19545 			return;
19546 		}
19547 
19548 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
19549 		if (!hdr)
19550 			goto nla_put_failure;
19551 
19552 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19553 		    (freq &&
19554 		     (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
19555 				  KHZ_TO_MHZ(freq)) ||
19556 		      nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
19557 				  freq % 1000))) ||
19558 		    (sig_dbm &&
19559 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
19560 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
19561 			goto nla_put_failure;
19562 
19563 		genlmsg_end(msg, hdr);
19564 
19565 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
19566 	}
19567 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19568 	return;
19569 
19570  nla_put_failure:
19571 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19572 	nlmsg_free(msg);
19573 }
19574 EXPORT_SYMBOL(cfg80211_report_obss_beacon_khz);
19575 
19576 #ifdef CONFIG_PM
19577 static int cfg80211_net_detect_results(struct sk_buff *msg,
19578 				       struct cfg80211_wowlan_wakeup *wakeup)
19579 {
19580 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
19581 	struct nlattr *nl_results, *nl_match, *nl_freqs;
19582 	int i, j;
19583 
19584 	nl_results = nla_nest_start_noflag(msg,
19585 					   NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
19586 	if (!nl_results)
19587 		return -EMSGSIZE;
19588 
19589 	for (i = 0; i < nd->n_matches; i++) {
19590 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
19591 
19592 		nl_match = nla_nest_start_noflag(msg, i);
19593 		if (!nl_match)
19594 			break;
19595 
19596 		/* The SSID attribute is optional in nl80211, but for
19597 		 * simplicity reasons it's always present in the
19598 		 * cfg80211 structure.  If a driver can't pass the
19599 		 * SSID, that needs to be changed.  A zero length SSID
19600 		 * is still a valid SSID (wildcard), so it cannot be
19601 		 * used for this purpose.
19602 		 */
19603 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
19604 			    match->ssid.ssid)) {
19605 			nla_nest_cancel(msg, nl_match);
19606 			goto out;
19607 		}
19608 
19609 		if (match->n_channels) {
19610 			nl_freqs = nla_nest_start_noflag(msg,
19611 							 NL80211_ATTR_SCAN_FREQUENCIES);
19612 			if (!nl_freqs) {
19613 				nla_nest_cancel(msg, nl_match);
19614 				goto out;
19615 			}
19616 
19617 			for (j = 0; j < match->n_channels; j++) {
19618 				if (nla_put_u32(msg, j, match->channels[j])) {
19619 					nla_nest_cancel(msg, nl_freqs);
19620 					nla_nest_cancel(msg, nl_match);
19621 					goto out;
19622 				}
19623 			}
19624 
19625 			nla_nest_end(msg, nl_freqs);
19626 		}
19627 
19628 		nla_nest_end(msg, nl_match);
19629 	}
19630 
19631 out:
19632 	nla_nest_end(msg, nl_results);
19633 	return 0;
19634 }
19635 
19636 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
19637 				   struct cfg80211_wowlan_wakeup *wakeup,
19638 				   gfp_t gfp)
19639 {
19640 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19641 	struct sk_buff *msg;
19642 	void *hdr;
19643 	int size = 200;
19644 
19645 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
19646 
19647 	if (wakeup)
19648 		size += wakeup->packet_present_len;
19649 
19650 	msg = nlmsg_new(size, gfp);
19651 	if (!msg)
19652 		return;
19653 
19654 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
19655 	if (!hdr)
19656 		goto free_msg;
19657 
19658 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19659 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19660 			      NL80211_ATTR_PAD))
19661 		goto free_msg;
19662 
19663 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
19664 					wdev->netdev->ifindex))
19665 		goto free_msg;
19666 
19667 	if (wakeup) {
19668 		struct nlattr *reasons;
19669 
19670 		reasons = nla_nest_start_noflag(msg,
19671 						NL80211_ATTR_WOWLAN_TRIGGERS);
19672 		if (!reasons)
19673 			goto free_msg;
19674 
19675 		if (wakeup->disconnect &&
19676 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
19677 			goto free_msg;
19678 		if (wakeup->magic_pkt &&
19679 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
19680 			goto free_msg;
19681 		if (wakeup->gtk_rekey_failure &&
19682 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
19683 			goto free_msg;
19684 		if (wakeup->eap_identity_req &&
19685 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
19686 			goto free_msg;
19687 		if (wakeup->four_way_handshake &&
19688 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
19689 			goto free_msg;
19690 		if (wakeup->rfkill_release &&
19691 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
19692 			goto free_msg;
19693 
19694 		if (wakeup->pattern_idx >= 0 &&
19695 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
19696 				wakeup->pattern_idx))
19697 			goto free_msg;
19698 
19699 		if (wakeup->tcp_match &&
19700 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
19701 			goto free_msg;
19702 
19703 		if (wakeup->tcp_connlost &&
19704 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
19705 			goto free_msg;
19706 
19707 		if (wakeup->tcp_nomoretokens &&
19708 		    nla_put_flag(msg,
19709 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
19710 			goto free_msg;
19711 
19712 		if (wakeup->packet) {
19713 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
19714 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
19715 
19716 			if (!wakeup->packet_80211) {
19717 				pkt_attr =
19718 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
19719 				len_attr =
19720 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
19721 			}
19722 
19723 			if (wakeup->packet_len &&
19724 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
19725 				goto free_msg;
19726 
19727 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
19728 				    wakeup->packet))
19729 				goto free_msg;
19730 		}
19731 
19732 		if (wakeup->net_detect &&
19733 		    cfg80211_net_detect_results(msg, wakeup))
19734 				goto free_msg;
19735 
19736 		nla_nest_end(msg, reasons);
19737 	}
19738 
19739 	genlmsg_end(msg, hdr);
19740 
19741 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19742 				NL80211_MCGRP_MLME, gfp);
19743 	return;
19744 
19745  free_msg:
19746 	nlmsg_free(msg);
19747 }
19748 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
19749 #endif
19750 
19751 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
19752 				enum nl80211_tdls_operation oper,
19753 				u16 reason_code, gfp_t gfp)
19754 {
19755 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19756 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19757 	struct sk_buff *msg;
19758 	void *hdr;
19759 
19760 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
19761 					 reason_code);
19762 
19763 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19764 	if (!msg)
19765 		return;
19766 
19767 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
19768 	if (!hdr) {
19769 		nlmsg_free(msg);
19770 		return;
19771 	}
19772 
19773 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19774 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19775 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
19776 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
19777 	    (reason_code > 0 &&
19778 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
19779 		goto nla_put_failure;
19780 
19781 	genlmsg_end(msg, hdr);
19782 
19783 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19784 				NL80211_MCGRP_MLME, gfp);
19785 	return;
19786 
19787  nla_put_failure:
19788 	nlmsg_free(msg);
19789 }
19790 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
19791 
19792 static int nl80211_netlink_notify(struct notifier_block * nb,
19793 				  unsigned long state,
19794 				  void *_notify)
19795 {
19796 	struct netlink_notify *notify = _notify;
19797 	struct cfg80211_registered_device *rdev;
19798 	struct wireless_dev *wdev;
19799 	struct cfg80211_beacon_registration *reg, *tmp;
19800 
19801 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
19802 		return NOTIFY_DONE;
19803 
19804 	rcu_read_lock();
19805 
19806 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
19807 		struct cfg80211_sched_scan_request *sched_scan_req;
19808 
19809 		list_for_each_entry_rcu(sched_scan_req,
19810 					&rdev->sched_scan_req_list,
19811 					list) {
19812 			if (sched_scan_req->owner_nlportid == notify->portid) {
19813 				sched_scan_req->nl_owner_dead = true;
19814 				wiphy_work_queue(&rdev->wiphy,
19815 						 &rdev->sched_scan_stop_wk);
19816 			}
19817 		}
19818 
19819 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
19820 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
19821 
19822 			if (wdev->owner_nlportid == notify->portid) {
19823 				wdev->nl_owner_dead = true;
19824 				schedule_work(&rdev->destroy_work);
19825 			} else if (wdev->conn_owner_nlportid == notify->portid) {
19826 				schedule_work(&wdev->disconnect_wk);
19827 			}
19828 
19829 			cfg80211_release_pmsr(wdev, notify->portid);
19830 		}
19831 
19832 		spin_lock_bh(&rdev->beacon_registrations_lock);
19833 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
19834 					 list) {
19835 			if (reg->nlportid == notify->portid) {
19836 				list_del(&reg->list);
19837 				kfree(reg);
19838 				break;
19839 			}
19840 		}
19841 		spin_unlock_bh(&rdev->beacon_registrations_lock);
19842 	}
19843 
19844 	rcu_read_unlock();
19845 
19846 	/*
19847 	 * It is possible that the user space process that is controlling the
19848 	 * indoor setting disappeared, so notify the regulatory core.
19849 	 */
19850 	regulatory_netlink_notify(notify->portid);
19851 	return NOTIFY_OK;
19852 }
19853 
19854 static struct notifier_block nl80211_netlink_notifier = {
19855 	.notifier_call = nl80211_netlink_notify,
19856 };
19857 
19858 void cfg80211_ft_event(struct net_device *netdev,
19859 		       struct cfg80211_ft_event_params *ft_event)
19860 {
19861 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
19862 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19863 	struct sk_buff *msg;
19864 	void *hdr;
19865 
19866 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
19867 
19868 	if (!ft_event->target_ap)
19869 		return;
19870 
19871 	msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
19872 			GFP_KERNEL);
19873 	if (!msg)
19874 		return;
19875 
19876 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
19877 	if (!hdr)
19878 		goto out;
19879 
19880 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19881 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19882 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
19883 		goto out;
19884 
19885 	if (ft_event->ies &&
19886 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
19887 		goto out;
19888 	if (ft_event->ric_ies &&
19889 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
19890 		    ft_event->ric_ies))
19891 		goto out;
19892 
19893 	genlmsg_end(msg, hdr);
19894 
19895 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19896 				NL80211_MCGRP_MLME, GFP_KERNEL);
19897 	return;
19898  out:
19899 	nlmsg_free(msg);
19900 }
19901 EXPORT_SYMBOL(cfg80211_ft_event);
19902 
19903 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
19904 {
19905 	struct cfg80211_registered_device *rdev;
19906 	struct sk_buff *msg;
19907 	void *hdr;
19908 	u32 nlportid;
19909 
19910 	rdev = wiphy_to_rdev(wdev->wiphy);
19911 	if (!rdev->crit_proto_nlportid)
19912 		return;
19913 
19914 	nlportid = rdev->crit_proto_nlportid;
19915 	rdev->crit_proto_nlportid = 0;
19916 
19917 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19918 	if (!msg)
19919 		return;
19920 
19921 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
19922 	if (!hdr)
19923 		goto nla_put_failure;
19924 
19925 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19926 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19927 			      NL80211_ATTR_PAD))
19928 		goto nla_put_failure;
19929 
19930 	genlmsg_end(msg, hdr);
19931 
19932 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
19933 	return;
19934 
19935  nla_put_failure:
19936 	nlmsg_free(msg);
19937 }
19938 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
19939 
19940 void nl80211_send_ap_stopped(struct wireless_dev *wdev, unsigned int link_id)
19941 {
19942 	struct wiphy *wiphy = wdev->wiphy;
19943 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19944 	struct sk_buff *msg;
19945 	void *hdr;
19946 
19947 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
19948 	if (!msg)
19949 		return;
19950 
19951 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
19952 	if (!hdr)
19953 		goto out;
19954 
19955 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19956 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
19957 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19958 			      NL80211_ATTR_PAD) ||
19959 	    (wdev->valid_links &&
19960 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)))
19961 		goto out;
19962 
19963 	genlmsg_end(msg, hdr);
19964 
19965 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
19966 				NL80211_MCGRP_MLME, GFP_KERNEL);
19967 	return;
19968  out:
19969 	nlmsg_free(msg);
19970 }
19971 
19972 int cfg80211_external_auth_request(struct net_device *dev,
19973 				   struct cfg80211_external_auth_params *params,
19974 				   gfp_t gfp)
19975 {
19976 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19977 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19978 	struct sk_buff *msg;
19979 	void *hdr;
19980 
19981 	if (!wdev->conn_owner_nlportid)
19982 		return -EINVAL;
19983 
19984 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19985 	if (!msg)
19986 		return -ENOMEM;
19987 
19988 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
19989 	if (!hdr)
19990 		goto nla_put_failure;
19991 
19992 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19993 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19994 	    nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
19995 	    nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
19996 			params->action) ||
19997 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
19998 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
19999 		    params->ssid.ssid) ||
20000 	    (!is_zero_ether_addr(params->mld_addr) &&
20001 	     nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN, params->mld_addr)))
20002 		goto nla_put_failure;
20003 
20004 	genlmsg_end(msg, hdr);
20005 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
20006 			wdev->conn_owner_nlportid);
20007 	return 0;
20008 
20009  nla_put_failure:
20010 	nlmsg_free(msg);
20011 	return -ENOBUFS;
20012 }
20013 EXPORT_SYMBOL(cfg80211_external_auth_request);
20014 
20015 void cfg80211_update_owe_info_event(struct net_device *netdev,
20016 				    struct cfg80211_update_owe_info *owe_info,
20017 				    gfp_t gfp)
20018 {
20019 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
20020 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
20021 	struct sk_buff *msg;
20022 	void *hdr;
20023 
20024 	trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
20025 
20026 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
20027 	if (!msg)
20028 		return;
20029 
20030 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
20031 	if (!hdr)
20032 		goto nla_put_failure;
20033 
20034 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20035 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
20036 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
20037 		goto nla_put_failure;
20038 
20039 	if (!owe_info->ie_len ||
20040 	    nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
20041 		goto nla_put_failure;
20042 
20043 	if (owe_info->assoc_link_id != -1) {
20044 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
20045 			       owe_info->assoc_link_id))
20046 			goto nla_put_failure;
20047 
20048 		if (!is_zero_ether_addr(owe_info->peer_mld_addr) &&
20049 		    nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
20050 			    owe_info->peer_mld_addr))
20051 			goto nla_put_failure;
20052 	}
20053 
20054 	genlmsg_end(msg, hdr);
20055 
20056 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
20057 				NL80211_MCGRP_MLME, gfp);
20058 	return;
20059 
20060 nla_put_failure:
20061 	genlmsg_cancel(msg, hdr);
20062 	nlmsg_free(msg);
20063 }
20064 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
20065 
20066 /* initialisation/exit functions */
20067 
20068 int __init nl80211_init(void)
20069 {
20070 	int err;
20071 
20072 	err = genl_register_family(&nl80211_fam);
20073 	if (err)
20074 		return err;
20075 
20076 	err = netlink_register_notifier(&nl80211_netlink_notifier);
20077 	if (err)
20078 		goto err_out;
20079 
20080 	return 0;
20081  err_out:
20082 	genl_unregister_family(&nl80211_fam);
20083 	return err;
20084 }
20085 
20086 void nl80211_exit(void)
20087 {
20088 	netlink_unregister_notifier(&nl80211_netlink_notifier);
20089 	genl_unregister_family(&nl80211_fam);
20090 }
20091