xref: /openbmc/linux/net/wireless/nl80211.c (revision bc33f5e5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This is the new netlink-based wireless configuration interface.
4  *
5  * Copyright 2006-2010	Johannes Berg <johannes@sipsolutions.net>
6  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  * Copyright 2015-2017	Intel Deutschland GmbH
8  * Copyright (C) 2018-2022 Intel Corporation
9  */
10 
11 #include <linux/if.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/list.h>
16 #include <linux/if_ether.h>
17 #include <linux/ieee80211.h>
18 #include <linux/nl80211.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/netlink.h>
21 #include <linux/nospec.h>
22 #include <linux/etherdevice.h>
23 #include <linux/if_vlan.h>
24 #include <net/net_namespace.h>
25 #include <net/genetlink.h>
26 #include <net/cfg80211.h>
27 #include <net/sock.h>
28 #include <net/inet_connection_sock.h>
29 #include "core.h"
30 #include "nl80211.h"
31 #include "reg.h"
32 #include "rdev-ops.h"
33 
34 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
35 				   struct genl_info *info,
36 				   struct cfg80211_crypto_settings *settings,
37 				   int cipher_limit);
38 
39 /* the netlink family */
40 static struct genl_family nl80211_fam;
41 
42 /* multicast groups */
43 enum nl80211_multicast_groups {
44 	NL80211_MCGRP_CONFIG,
45 	NL80211_MCGRP_SCAN,
46 	NL80211_MCGRP_REGULATORY,
47 	NL80211_MCGRP_MLME,
48 	NL80211_MCGRP_VENDOR,
49 	NL80211_MCGRP_NAN,
50 	NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
51 };
52 
53 static const struct genl_multicast_group nl80211_mcgrps[] = {
54 	[NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
55 	[NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
56 	[NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
57 	[NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
58 	[NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
59 	[NL80211_MCGRP_NAN] = { .name = NL80211_MULTICAST_GROUP_NAN },
60 #ifdef CONFIG_NL80211_TESTMODE
61 	[NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
62 #endif
63 };
64 
65 /* returns ERR_PTR values */
66 static struct wireless_dev *
67 __cfg80211_wdev_from_attrs(struct cfg80211_registered_device *rdev,
68 			   struct net *netns, struct nlattr **attrs)
69 {
70 	struct wireless_dev *result = NULL;
71 	bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
72 	bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
73 	u64 wdev_id = 0;
74 	int wiphy_idx = -1;
75 	int ifidx = -1;
76 
77 	if (!have_ifidx && !have_wdev_id)
78 		return ERR_PTR(-EINVAL);
79 
80 	if (have_ifidx)
81 		ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
82 	if (have_wdev_id) {
83 		wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
84 		wiphy_idx = wdev_id >> 32;
85 	}
86 
87 	if (rdev) {
88 		struct wireless_dev *wdev;
89 
90 		lockdep_assert_held(&rdev->wiphy.mtx);
91 
92 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
93 			if (have_ifidx && wdev->netdev &&
94 			    wdev->netdev->ifindex == ifidx) {
95 				result = wdev;
96 				break;
97 			}
98 			if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
99 				result = wdev;
100 				break;
101 			}
102 		}
103 
104 		return result ?: ERR_PTR(-ENODEV);
105 	}
106 
107 	ASSERT_RTNL();
108 
109 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
110 		struct wireless_dev *wdev;
111 
112 		if (wiphy_net(&rdev->wiphy) != netns)
113 			continue;
114 
115 		if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
116 			continue;
117 
118 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
119 			if (have_ifidx && wdev->netdev &&
120 			    wdev->netdev->ifindex == ifidx) {
121 				result = wdev;
122 				break;
123 			}
124 			if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
125 				result = wdev;
126 				break;
127 			}
128 		}
129 
130 		if (result)
131 			break;
132 	}
133 
134 	if (result)
135 		return result;
136 	return ERR_PTR(-ENODEV);
137 }
138 
139 static struct cfg80211_registered_device *
140 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
141 {
142 	struct cfg80211_registered_device *rdev = NULL, *tmp;
143 	struct net_device *netdev;
144 
145 	ASSERT_RTNL();
146 
147 	if (!attrs[NL80211_ATTR_WIPHY] &&
148 	    !attrs[NL80211_ATTR_IFINDEX] &&
149 	    !attrs[NL80211_ATTR_WDEV])
150 		return ERR_PTR(-EINVAL);
151 
152 	if (attrs[NL80211_ATTR_WIPHY])
153 		rdev = cfg80211_rdev_by_wiphy_idx(
154 				nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
155 
156 	if (attrs[NL80211_ATTR_WDEV]) {
157 		u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
158 		struct wireless_dev *wdev;
159 		bool found = false;
160 
161 		tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
162 		if (tmp) {
163 			/* make sure wdev exists */
164 			list_for_each_entry(wdev, &tmp->wiphy.wdev_list, list) {
165 				if (wdev->identifier != (u32)wdev_id)
166 					continue;
167 				found = true;
168 				break;
169 			}
170 
171 			if (!found)
172 				tmp = NULL;
173 
174 			if (rdev && tmp != rdev)
175 				return ERR_PTR(-EINVAL);
176 			rdev = tmp;
177 		}
178 	}
179 
180 	if (attrs[NL80211_ATTR_IFINDEX]) {
181 		int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
182 
183 		netdev = __dev_get_by_index(netns, ifindex);
184 		if (netdev) {
185 			if (netdev->ieee80211_ptr)
186 				tmp = wiphy_to_rdev(
187 					netdev->ieee80211_ptr->wiphy);
188 			else
189 				tmp = NULL;
190 
191 			/* not wireless device -- return error */
192 			if (!tmp)
193 				return ERR_PTR(-EINVAL);
194 
195 			/* mismatch -- return error */
196 			if (rdev && tmp != rdev)
197 				return ERR_PTR(-EINVAL);
198 
199 			rdev = tmp;
200 		}
201 	}
202 
203 	if (!rdev)
204 		return ERR_PTR(-ENODEV);
205 
206 	if (netns != wiphy_net(&rdev->wiphy))
207 		return ERR_PTR(-ENODEV);
208 
209 	return rdev;
210 }
211 
212 /*
213  * This function returns a pointer to the driver
214  * that the genl_info item that is passed refers to.
215  *
216  * The result of this can be a PTR_ERR and hence must
217  * be checked with IS_ERR() for errors.
218  */
219 static struct cfg80211_registered_device *
220 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
221 {
222 	return __cfg80211_rdev_from_attrs(netns, info->attrs);
223 }
224 
225 static int validate_beacon_head(const struct nlattr *attr,
226 				struct netlink_ext_ack *extack)
227 {
228 	const u8 *data = nla_data(attr);
229 	unsigned int len = nla_len(attr);
230 	const struct element *elem;
231 	const struct ieee80211_mgmt *mgmt = (void *)data;
232 	unsigned int fixedlen, hdrlen;
233 	bool s1g_bcn;
234 
235 	if (len < offsetofend(typeof(*mgmt), frame_control))
236 		goto err;
237 
238 	s1g_bcn = ieee80211_is_s1g_beacon(mgmt->frame_control);
239 	if (s1g_bcn) {
240 		fixedlen = offsetof(struct ieee80211_ext,
241 				    u.s1g_beacon.variable);
242 		hdrlen = offsetof(struct ieee80211_ext, u.s1g_beacon);
243 	} else {
244 		fixedlen = offsetof(struct ieee80211_mgmt,
245 				    u.beacon.variable);
246 		hdrlen = offsetof(struct ieee80211_mgmt, u.beacon);
247 	}
248 
249 	if (len < fixedlen)
250 		goto err;
251 
252 	if (ieee80211_hdrlen(mgmt->frame_control) != hdrlen)
253 		goto err;
254 
255 	data += fixedlen;
256 	len -= fixedlen;
257 
258 	for_each_element(elem, data, len) {
259 		/* nothing */
260 	}
261 
262 	if (for_each_element_completed(elem, data, len))
263 		return 0;
264 
265 err:
266 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed beacon head");
267 	return -EINVAL;
268 }
269 
270 static int validate_ie_attr(const struct nlattr *attr,
271 			    struct netlink_ext_ack *extack)
272 {
273 	const u8 *data = nla_data(attr);
274 	unsigned int len = nla_len(attr);
275 	const struct element *elem;
276 
277 	for_each_element(elem, data, len) {
278 		/* nothing */
279 	}
280 
281 	if (for_each_element_completed(elem, data, len))
282 		return 0;
283 
284 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed information elements");
285 	return -EINVAL;
286 }
287 
288 static int validate_he_capa(const struct nlattr *attr,
289 			    struct netlink_ext_ack *extack)
290 {
291 	if (!ieee80211_he_capa_size_ok(nla_data(attr), nla_len(attr)))
292 		return -EINVAL;
293 
294 	return 0;
295 }
296 
297 /* policy for the attributes */
298 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR];
299 
300 static const struct nla_policy
301 nl80211_ftm_responder_policy[NL80211_FTM_RESP_ATTR_MAX + 1] = {
302 	[NL80211_FTM_RESP_ATTR_ENABLED] = { .type = NLA_FLAG, },
303 	[NL80211_FTM_RESP_ATTR_LCI] = { .type = NLA_BINARY,
304 					.len = U8_MAX },
305 	[NL80211_FTM_RESP_ATTR_CIVICLOC] = { .type = NLA_BINARY,
306 					     .len = U8_MAX },
307 };
308 
309 static const struct nla_policy
310 nl80211_pmsr_ftm_req_attr_policy[NL80211_PMSR_FTM_REQ_ATTR_MAX + 1] = {
311 	[NL80211_PMSR_FTM_REQ_ATTR_ASAP] = { .type = NLA_FLAG },
312 	[NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE] = { .type = NLA_U32 },
313 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP] =
314 		NLA_POLICY_MAX(NLA_U8, 15),
315 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD] = { .type = NLA_U16 },
316 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION] =
317 		NLA_POLICY_MAX(NLA_U8, 15),
318 	[NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST] =
319 		NLA_POLICY_MAX(NLA_U8, 31),
320 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES] = { .type = NLA_U8 },
321 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI] = { .type = NLA_FLAG },
322 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC] = { .type = NLA_FLAG },
323 	[NL80211_PMSR_FTM_REQ_ATTR_TRIGGER_BASED] = { .type = NLA_FLAG },
324 	[NL80211_PMSR_FTM_REQ_ATTR_NON_TRIGGER_BASED] = { .type = NLA_FLAG },
325 	[NL80211_PMSR_FTM_REQ_ATTR_LMR_FEEDBACK] = { .type = NLA_FLAG },
326 };
327 
328 static const struct nla_policy
329 nl80211_pmsr_req_data_policy[NL80211_PMSR_TYPE_MAX + 1] = {
330 	[NL80211_PMSR_TYPE_FTM] =
331 		NLA_POLICY_NESTED(nl80211_pmsr_ftm_req_attr_policy),
332 };
333 
334 static const struct nla_policy
335 nl80211_pmsr_req_attr_policy[NL80211_PMSR_REQ_ATTR_MAX + 1] = {
336 	[NL80211_PMSR_REQ_ATTR_DATA] =
337 		NLA_POLICY_NESTED(nl80211_pmsr_req_data_policy),
338 	[NL80211_PMSR_REQ_ATTR_GET_AP_TSF] = { .type = NLA_FLAG },
339 };
340 
341 static const struct nla_policy
342 nl80211_pmsr_peer_attr_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
343 	[NL80211_PMSR_PEER_ATTR_ADDR] = NLA_POLICY_ETH_ADDR,
344 	[NL80211_PMSR_PEER_ATTR_CHAN] = NLA_POLICY_NESTED(nl80211_policy),
345 	[NL80211_PMSR_PEER_ATTR_REQ] =
346 		NLA_POLICY_NESTED(nl80211_pmsr_req_attr_policy),
347 	[NL80211_PMSR_PEER_ATTR_RESP] = { .type = NLA_REJECT },
348 };
349 
350 static const struct nla_policy
351 nl80211_pmsr_attr_policy[NL80211_PMSR_ATTR_MAX + 1] = {
352 	[NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_REJECT },
353 	[NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_REJECT },
354 	[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
355 	[NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
356 	[NL80211_PMSR_ATTR_PEERS] =
357 		NLA_POLICY_NESTED_ARRAY(nl80211_pmsr_peer_attr_policy),
358 };
359 
360 static const struct nla_policy
361 he_obss_pd_policy[NL80211_HE_OBSS_PD_ATTR_MAX + 1] = {
362 	[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] =
363 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
364 	[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET] =
365 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
366 	[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET] =
367 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
368 	[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP] =
369 		NLA_POLICY_EXACT_LEN(8),
370 	[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP] =
371 		NLA_POLICY_EXACT_LEN(8),
372 	[NL80211_HE_OBSS_PD_ATTR_SR_CTRL] = { .type = NLA_U8 },
373 };
374 
375 static const struct nla_policy
376 he_bss_color_policy[NL80211_HE_BSS_COLOR_ATTR_MAX + 1] = {
377 	[NL80211_HE_BSS_COLOR_ATTR_COLOR] = NLA_POLICY_RANGE(NLA_U8, 1, 63),
378 	[NL80211_HE_BSS_COLOR_ATTR_DISABLED] = { .type = NLA_FLAG },
379 	[NL80211_HE_BSS_COLOR_ATTR_PARTIAL] = { .type = NLA_FLAG },
380 };
381 
382 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
383 	[NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
384 				    .len = NL80211_MAX_SUPP_RATES },
385 	[NL80211_TXRATE_HT] = { .type = NLA_BINARY,
386 				.len = NL80211_MAX_SUPP_HT_RATES },
387 	[NL80211_TXRATE_VHT] = NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_txrate_vht)),
388 	[NL80211_TXRATE_GI] = { .type = NLA_U8 },
389 	[NL80211_TXRATE_HE] = NLA_POLICY_EXACT_LEN(sizeof(struct nl80211_txrate_he)),
390 	[NL80211_TXRATE_HE_GI] =  NLA_POLICY_RANGE(NLA_U8,
391 						   NL80211_RATE_INFO_HE_GI_0_8,
392 						   NL80211_RATE_INFO_HE_GI_3_2),
393 	[NL80211_TXRATE_HE_LTF] = NLA_POLICY_RANGE(NLA_U8,
394 						   NL80211_RATE_INFO_HE_1XLTF,
395 						   NL80211_RATE_INFO_HE_4XLTF),
396 };
397 
398 static const struct nla_policy
399 nl80211_tid_config_attr_policy[NL80211_TID_CONFIG_ATTR_MAX + 1] = {
400 	[NL80211_TID_CONFIG_ATTR_VIF_SUPP] = { .type = NLA_U64 },
401 	[NL80211_TID_CONFIG_ATTR_PEER_SUPP] = { .type = NLA_U64 },
402 	[NL80211_TID_CONFIG_ATTR_OVERRIDE] = { .type = NLA_FLAG },
403 	[NL80211_TID_CONFIG_ATTR_TIDS] = NLA_POLICY_RANGE(NLA_U16, 1, 0xff),
404 	[NL80211_TID_CONFIG_ATTR_NOACK] =
405 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
406 	[NL80211_TID_CONFIG_ATTR_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
407 	[NL80211_TID_CONFIG_ATTR_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
408 	[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL] =
409 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
410 	[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL] =
411 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
412 	[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL] =
413 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
414 	[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE] =
415 			NLA_POLICY_MAX(NLA_U8, NL80211_TX_RATE_FIXED),
416 	[NL80211_TID_CONFIG_ATTR_TX_RATE] =
417 			NLA_POLICY_NESTED(nl80211_txattr_policy),
418 };
419 
420 static const struct nla_policy
421 nl80211_fils_discovery_policy[NL80211_FILS_DISCOVERY_ATTR_MAX + 1] = {
422 	[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] = NLA_POLICY_MAX(NLA_U32, 10000),
423 	[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] = NLA_POLICY_MAX(NLA_U32, 10000),
424 	[NL80211_FILS_DISCOVERY_ATTR_TMPL] =
425 			NLA_POLICY_RANGE(NLA_BINARY,
426 					 NL80211_FILS_DISCOVERY_TMPL_MIN_LEN,
427 					 IEEE80211_MAX_DATA_LEN),
428 };
429 
430 static const struct nla_policy
431 nl80211_unsol_bcast_probe_resp_policy[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1] = {
432 	[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] = NLA_POLICY_MAX(NLA_U32, 20),
433 	[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL] = { .type = NLA_BINARY,
434 						       .len = IEEE80211_MAX_DATA_LEN }
435 };
436 
437 static const struct nla_policy
438 sar_specs_policy[NL80211_SAR_ATTR_SPECS_MAX + 1] = {
439 	[NL80211_SAR_ATTR_SPECS_POWER] = { .type = NLA_S32 },
440 	[NL80211_SAR_ATTR_SPECS_RANGE_INDEX] = {.type = NLA_U32 },
441 };
442 
443 static const struct nla_policy
444 sar_policy[NL80211_SAR_ATTR_MAX + 1] = {
445 	[NL80211_SAR_ATTR_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_SAR_TYPE),
446 	[NL80211_SAR_ATTR_SPECS] = NLA_POLICY_NESTED_ARRAY(sar_specs_policy),
447 };
448 
449 static const struct nla_policy
450 nl80211_mbssid_config_policy[NL80211_MBSSID_CONFIG_ATTR_MAX + 1] = {
451 	[NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES] = NLA_POLICY_MIN(NLA_U8, 2),
452 	[NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY] =
453 						NLA_POLICY_MIN(NLA_U8, 1),
454 	[NL80211_MBSSID_CONFIG_ATTR_INDEX] = { .type = NLA_U8 },
455 	[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX] = { .type = NLA_U32 },
456 	[NL80211_MBSSID_CONFIG_ATTR_EMA] = { .type = NLA_FLAG },
457 };
458 
459 static const struct nla_policy
460 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
461 	[NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
462 	[NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
463 };
464 
465 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
466 	[0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
467 	[NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
468 	[NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
469 				      .len = 20-1 },
470 	[NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
471 
472 	[NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
473 	[NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
474 	[NL80211_ATTR_WIPHY_EDMG_CHANNELS] = NLA_POLICY_RANGE(NLA_U8,
475 						NL80211_EDMG_CHANNELS_MIN,
476 						NL80211_EDMG_CHANNELS_MAX),
477 	[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG] = NLA_POLICY_RANGE(NLA_U8,
478 						NL80211_EDMG_BW_CONFIG_MIN,
479 						NL80211_EDMG_BW_CONFIG_MAX),
480 
481 	[NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
482 	[NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
483 	[NL80211_ATTR_CENTER_FREQ1_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
484 	[NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
485 
486 	[NL80211_ATTR_WIPHY_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
487 	[NL80211_ATTR_WIPHY_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
488 	[NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
489 	[NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
490 	[NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
491 	[NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
492 
493 	[NL80211_ATTR_IFTYPE] = NLA_POLICY_MAX(NLA_U32, NL80211_IFTYPE_MAX),
494 	[NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
495 	[NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
496 
497 	[NL80211_ATTR_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
498 	[NL80211_ATTR_PREV_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
499 
500 	[NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
501 	[NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
502 				    .len = WLAN_MAX_KEY_LEN },
503 	[NL80211_ATTR_KEY_IDX] = NLA_POLICY_MAX(NLA_U8, 7),
504 	[NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
505 	[NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
506 	[NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
507 	[NL80211_ATTR_KEY_TYPE] =
508 		NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES),
509 
510 	[NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
511 	[NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
512 	[NL80211_ATTR_BEACON_HEAD] =
513 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_beacon_head,
514 				       IEEE80211_MAX_DATA_LEN),
515 	[NL80211_ATTR_BEACON_TAIL] =
516 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
517 				       IEEE80211_MAX_DATA_LEN),
518 	[NL80211_ATTR_STA_AID] =
519 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
520 	[NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
521 	[NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
522 	[NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
523 					       .len = NL80211_MAX_SUPP_RATES },
524 	[NL80211_ATTR_STA_PLINK_ACTION] =
525 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_ACTIONS - 1),
526 	[NL80211_ATTR_STA_TX_POWER_SETTING] =
527 		NLA_POLICY_RANGE(NLA_U8,
528 				 NL80211_TX_POWER_AUTOMATIC,
529 				 NL80211_TX_POWER_FIXED),
530 	[NL80211_ATTR_STA_TX_POWER] = { .type = NLA_S16 },
531 	[NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
532 	[NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
533 	[NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
534 				   .len = IEEE80211_MAX_MESH_ID_LEN },
535 	[NL80211_ATTR_MPATH_NEXT_HOP] = NLA_POLICY_ETH_ADDR_COMPAT,
536 
537 	/* allow 3 for NUL-termination, we used to declare this NLA_STRING */
538 	[NL80211_ATTR_REG_ALPHA2] = NLA_POLICY_RANGE(NLA_BINARY, 2, 3),
539 	[NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
540 
541 	[NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
542 	[NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
543 	[NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
544 	[NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
545 					   .len = NL80211_MAX_SUPP_RATES },
546 	[NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
547 
548 	[NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
549 	[NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
550 
551 	[NL80211_ATTR_HT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_HT_CAPABILITY_LEN),
552 
553 	[NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
554 	[NL80211_ATTR_IE] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
555 						   validate_ie_attr,
556 						   IEEE80211_MAX_DATA_LEN),
557 	[NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
558 	[NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
559 
560 	[NL80211_ATTR_SSID] = { .type = NLA_BINARY,
561 				.len = IEEE80211_MAX_SSID_LEN },
562 	[NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
563 	[NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
564 	[NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
565 	[NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
566 	[NL80211_ATTR_USE_MFP] = NLA_POLICY_RANGE(NLA_U32,
567 						  NL80211_MFP_NO,
568 						  NL80211_MFP_OPTIONAL),
569 	[NL80211_ATTR_STA_FLAGS2] =
570 		NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_sta_flag_update)),
571 	[NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
572 	[NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
573 	[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
574 	[NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
575 	[NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
576 	[NL80211_ATTR_STATUS_CODE] = { .type = NLA_U16 },
577 	[NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
578 	[NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
579 	[NL80211_ATTR_PID] = { .type = NLA_U32 },
580 	[NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
581 	[NL80211_ATTR_PMKID] = NLA_POLICY_EXACT_LEN_WARN(WLAN_PMKID_LEN),
582 	[NL80211_ATTR_DURATION] = { .type = NLA_U32 },
583 	[NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
584 	[NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
585 	[NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
586 				 .len = IEEE80211_MAX_DATA_LEN },
587 	[NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
588 	[NL80211_ATTR_PS_STATE] = NLA_POLICY_RANGE(NLA_U32,
589 						   NL80211_PS_DISABLED,
590 						   NL80211_PS_ENABLED),
591 	[NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
592 	[NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
593 	[NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
594 	[NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
595 	[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
596 	[NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
597 	[NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
598 	[NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
599 	[NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
600 	[NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
601 	[NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
602 	[NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
603 	[NL80211_ATTR_STA_PLINK_STATE] =
604 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_STATES - 1),
605 	[NL80211_ATTR_MEASUREMENT_DURATION] = { .type = NLA_U16 },
606 	[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY] = { .type = NLA_FLAG },
607 	[NL80211_ATTR_MESH_PEER_AID] =
608 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
609 	[NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
610 	[NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
611 	[NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
612 	[NL80211_ATTR_HIDDEN_SSID] =
613 		NLA_POLICY_RANGE(NLA_U32,
614 				 NL80211_HIDDEN_SSID_NOT_IN_USE,
615 				 NL80211_HIDDEN_SSID_ZERO_CONTENTS),
616 	[NL80211_ATTR_IE_PROBE_RESP] =
617 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
618 				       IEEE80211_MAX_DATA_LEN),
619 	[NL80211_ATTR_IE_ASSOC_RESP] =
620 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
621 				       IEEE80211_MAX_DATA_LEN),
622 	[NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
623 	[NL80211_ATTR_STA_WME] = NLA_POLICY_NESTED(nl80211_sta_wme_policy),
624 	[NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
625 	[NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
626 	[NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
627 	[NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
628 	[NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
629 	[NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
630 	[NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
631 	[NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
632 	[NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
633 	[NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
634 				      .len = IEEE80211_MAX_DATA_LEN },
635 	[NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
636 	[NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
637 	[NL80211_ATTR_HT_CAPABILITY_MASK] = {
638 		.len = NL80211_HT_CAPABILITY_LEN
639 	},
640 	[NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
641 	[NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
642 	[NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
643 	[NL80211_ATTR_WDEV] = { .type = NLA_U64 },
644 	[NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
645 
646 	/* need to include at least Auth Transaction and Status Code */
647 	[NL80211_ATTR_AUTH_DATA] = NLA_POLICY_MIN_LEN(4),
648 
649 	[NL80211_ATTR_VHT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_VHT_CAPABILITY_LEN),
650 	[NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
651 	[NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
652 	[NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
653 	[NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
654 		NLA_POLICY_RANGE(NLA_U32,
655 				 NL80211_MESH_POWER_UNKNOWN + 1,
656 				 NL80211_MESH_POWER_MAX),
657 	[NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
658 	[NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
659 	[NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
660 	[NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
661 	[NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
662 	[NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
663 	[NL80211_ATTR_VHT_CAPABILITY_MASK] = {
664 		.len = NL80211_VHT_CAPABILITY_LEN,
665 	},
666 	[NL80211_ATTR_MDID] = { .type = NLA_U16 },
667 	[NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
668 				  .len = IEEE80211_MAX_DATA_LEN },
669 	[NL80211_ATTR_CRIT_PROT_ID] = { .type = NLA_U16 },
670 	[NL80211_ATTR_MAX_CRIT_PROT_DURATION] =
671 		NLA_POLICY_MAX(NLA_U16, NL80211_CRIT_PROTO_MAX_DURATION),
672 	[NL80211_ATTR_PEER_AID] =
673 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
674 	[NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
675 	[NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
676 	[NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
677 	[NL80211_ATTR_CNTDWN_OFFS_BEACON] = { .type = NLA_BINARY },
678 	[NL80211_ATTR_CNTDWN_OFFS_PRESP] = { .type = NLA_BINARY },
679 	[NL80211_ATTR_STA_SUPPORTED_CHANNELS] = NLA_POLICY_MIN_LEN(2),
680 	/*
681 	 * The value of the Length field of the Supported Operating
682 	 * Classes element is between 2 and 253.
683 	 */
684 	[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] =
685 		NLA_POLICY_RANGE(NLA_BINARY, 2, 253),
686 	[NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
687 	[NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
688 	[NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
689 	[NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
690 	[NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
691 	[NL80211_ATTR_QOS_MAP] = NLA_POLICY_RANGE(NLA_BINARY,
692 						  IEEE80211_QOS_MAP_LEN_MIN,
693 						  IEEE80211_QOS_MAP_LEN_MAX),
694 	[NL80211_ATTR_MAC_HINT] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
695 	[NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
696 	[NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
697 	[NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
698 	[NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
699 	[NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
700 	[NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
701 	[NL80211_ATTR_USER_PRIO] =
702 		NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
703 	[NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
704 	[NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
705 	[NL80211_ATTR_OPER_CLASS] = { .type = NLA_U8 },
706 	[NL80211_ATTR_MAC_MASK] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
707 	[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
708 	[NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
709 	[NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
710 	[NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
711 	[NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
712 	[NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
713 	[NL80211_ATTR_STA_SUPPORT_P2P_PS] =
714 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
715 	[NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
716 		.len = VHT_MUMIMO_GROUPS_DATA_LEN
717 	},
718 	[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
719 	[NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
720 	[NL80211_ATTR_BANDS] = { .type = NLA_U32 },
721 	[NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
722 	[NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
723 				    .len = FILS_MAX_KEK_LEN },
724 	[NL80211_ATTR_FILS_NONCES] = NLA_POLICY_EXACT_LEN_WARN(2 * FILS_NONCE_LEN),
725 	[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
726 	[NL80211_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
727 	[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
728 	[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
729 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
730 	},
731 	[NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
732 	[NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
733 					     .len = FILS_ERP_MAX_USERNAME_LEN },
734 	[NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
735 					  .len = FILS_ERP_MAX_REALM_LEN },
736 	[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
737 	[NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
738 					.len = FILS_ERP_MAX_RRK_LEN },
739 	[NL80211_ATTR_FILS_CACHE_ID] = NLA_POLICY_EXACT_LEN_WARN(2),
740 	[NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
741 	[NL80211_ATTR_PMKR0_NAME] = NLA_POLICY_EXACT_LEN(WLAN_PMK_NAME_LEN),
742 	[NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
743 	[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
744 
745 	[NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
746 	[NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
747 	[NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
748 	[NL80211_ATTR_HE_CAPABILITY] =
749 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_he_capa,
750 				       NL80211_HE_MAX_CAPABILITY_LEN),
751 	[NL80211_ATTR_FTM_RESPONDER] =
752 		NLA_POLICY_NESTED(nl80211_ftm_responder_policy),
753 	[NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
754 	[NL80211_ATTR_PEER_MEASUREMENTS] =
755 		NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
756 	[NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
757 	[NL80211_ATTR_SAE_PASSWORD] = { .type = NLA_BINARY,
758 					.len = SAE_PASSWORD_MAX_LEN },
759 	[NL80211_ATTR_TWT_RESPONDER] = { .type = NLA_FLAG },
760 	[NL80211_ATTR_HE_OBSS_PD] = NLA_POLICY_NESTED(he_obss_pd_policy),
761 	[NL80211_ATTR_VLAN_ID] = NLA_POLICY_RANGE(NLA_U16, 1, VLAN_N_VID - 2),
762 	[NL80211_ATTR_HE_BSS_COLOR] = NLA_POLICY_NESTED(he_bss_color_policy),
763 	[NL80211_ATTR_TID_CONFIG] =
764 		NLA_POLICY_NESTED_ARRAY(nl80211_tid_config_attr_policy),
765 	[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH] = { .type = NLA_FLAG },
766 	[NL80211_ATTR_PMK_LIFETIME] = NLA_POLICY_MIN(NLA_U32, 1),
767 	[NL80211_ATTR_PMK_REAUTH_THRESHOLD] = NLA_POLICY_RANGE(NLA_U8, 1, 100),
768 	[NL80211_ATTR_RECEIVE_MULTICAST] = { .type = NLA_FLAG },
769 	[NL80211_ATTR_WIPHY_FREQ_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
770 	[NL80211_ATTR_SCAN_FREQ_KHZ] = { .type = NLA_NESTED },
771 	[NL80211_ATTR_HE_6GHZ_CAPABILITY] =
772 		NLA_POLICY_EXACT_LEN(sizeof(struct ieee80211_he_6ghz_capa)),
773 	[NL80211_ATTR_FILS_DISCOVERY] =
774 		NLA_POLICY_NESTED(nl80211_fils_discovery_policy),
775 	[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP] =
776 		NLA_POLICY_NESTED(nl80211_unsol_bcast_probe_resp_policy),
777 	[NL80211_ATTR_S1G_CAPABILITY] =
778 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
779 	[NL80211_ATTR_S1G_CAPABILITY_MASK] =
780 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
781 	[NL80211_ATTR_SAE_PWE] =
782 		NLA_POLICY_RANGE(NLA_U8, NL80211_SAE_PWE_HUNT_AND_PECK,
783 				 NL80211_SAE_PWE_BOTH),
784 	[NL80211_ATTR_RECONNECT_REQUESTED] = { .type = NLA_REJECT },
785 	[NL80211_ATTR_SAR_SPEC] = NLA_POLICY_NESTED(sar_policy),
786 	[NL80211_ATTR_DISABLE_HE] = { .type = NLA_FLAG },
787 	[NL80211_ATTR_OBSS_COLOR_BITMAP] = { .type = NLA_U64 },
788 	[NL80211_ATTR_COLOR_CHANGE_COUNT] = { .type = NLA_U8 },
789 	[NL80211_ATTR_COLOR_CHANGE_COLOR] = { .type = NLA_U8 },
790 	[NL80211_ATTR_COLOR_CHANGE_ELEMS] = NLA_POLICY_NESTED(nl80211_policy),
791 	[NL80211_ATTR_MBSSID_CONFIG] =
792 			NLA_POLICY_NESTED(nl80211_mbssid_config_policy),
793 	[NL80211_ATTR_MBSSID_ELEMS] = { .type = NLA_NESTED },
794 	[NL80211_ATTR_RADAR_BACKGROUND] = { .type = NLA_FLAG },
795 	[NL80211_ATTR_AP_SETTINGS_FLAGS] = { .type = NLA_U32 },
796 	[NL80211_ATTR_EHT_CAPABILITY] =
797 		NLA_POLICY_RANGE(NLA_BINARY,
798 				 NL80211_EHT_MIN_CAPABILITY_LEN,
799 				 NL80211_EHT_MAX_CAPABILITY_LEN),
800 	[NL80211_ATTR_DISABLE_EHT] = { .type = NLA_FLAG },
801 	[NL80211_ATTR_MLO_LINKS] =
802 		NLA_POLICY_NESTED_ARRAY(nl80211_policy),
803 	[NL80211_ATTR_MLO_LINK_ID] =
804 		NLA_POLICY_RANGE(NLA_U8, 0, IEEE80211_MLD_MAX_NUM_LINKS),
805 	[NL80211_ATTR_MLD_ADDR] = NLA_POLICY_EXACT_LEN(ETH_ALEN),
806 	[NL80211_ATTR_MLO_SUPPORT] = { .type = NLA_FLAG },
807 	[NL80211_ATTR_MAX_NUM_AKM_SUITES] = { .type = NLA_REJECT },
808 };
809 
810 /* policy for the key attributes */
811 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
812 	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
813 	[NL80211_KEY_IDX] = { .type = NLA_U8 },
814 	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
815 	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
816 	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
817 	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
818 	[NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
819 	[NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
820 	[NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
821 };
822 
823 /* policy for the key default flags */
824 static const struct nla_policy
825 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
826 	[NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
827 	[NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
828 };
829 
830 #ifdef CONFIG_PM
831 /* policy for WoWLAN attributes */
832 static const struct nla_policy
833 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
834 	[NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
835 	[NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
836 	[NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
837 	[NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
838 	[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
839 	[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
840 	[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
841 	[NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
842 	[NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
843 	[NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
844 };
845 
846 static const struct nla_policy
847 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
848 	[NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
849 	[NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
850 	[NL80211_WOWLAN_TCP_DST_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
851 	[NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
852 	[NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
853 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
854 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
855 		.len = sizeof(struct nl80211_wowlan_tcp_data_seq)
856 	},
857 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
858 		.len = sizeof(struct nl80211_wowlan_tcp_data_token)
859 	},
860 	[NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
861 	[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
862 	[NL80211_WOWLAN_TCP_WAKE_MASK] = NLA_POLICY_MIN_LEN(1),
863 };
864 #endif /* CONFIG_PM */
865 
866 /* policy for coalesce rule attributes */
867 static const struct nla_policy
868 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
869 	[NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
870 	[NL80211_ATTR_COALESCE_RULE_CONDITION] =
871 		NLA_POLICY_RANGE(NLA_U32,
872 				 NL80211_COALESCE_CONDITION_MATCH,
873 				 NL80211_COALESCE_CONDITION_NO_MATCH),
874 	[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
875 };
876 
877 /* policy for GTK rekey offload attributes */
878 static const struct nla_policy
879 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
880 	[NL80211_REKEY_DATA_KEK] = {
881 		.type = NLA_BINARY,
882 		.len = NL80211_KEK_EXT_LEN
883 	},
884 	[NL80211_REKEY_DATA_KCK] = {
885 		.type = NLA_BINARY,
886 		.len = NL80211_KCK_EXT_LEN
887 	},
888 	[NL80211_REKEY_DATA_REPLAY_CTR] = NLA_POLICY_EXACT_LEN(NL80211_REPLAY_CTR_LEN),
889 	[NL80211_REKEY_DATA_AKM] = { .type = NLA_U32 },
890 };
891 
892 static const struct nla_policy
893 nl80211_match_band_rssi_policy[NUM_NL80211_BANDS] = {
894 	[NL80211_BAND_2GHZ] = { .type = NLA_S32 },
895 	[NL80211_BAND_5GHZ] = { .type = NLA_S32 },
896 	[NL80211_BAND_6GHZ] = { .type = NLA_S32 },
897 	[NL80211_BAND_60GHZ] = { .type = NLA_S32 },
898 	[NL80211_BAND_LC]    = { .type = NLA_S32 },
899 };
900 
901 static const struct nla_policy
902 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
903 	[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
904 						 .len = IEEE80211_MAX_SSID_LEN },
905 	[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
906 	[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
907 	[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI] =
908 		NLA_POLICY_NESTED(nl80211_match_band_rssi_policy),
909 };
910 
911 static const struct nla_policy
912 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
913 	[NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
914 	[NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
915 };
916 
917 static const struct nla_policy
918 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
919 	[NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
920 	[NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
921 	[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
922 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
923 	},
924 };
925 
926 /* policy for NAN function attributes */
927 static const struct nla_policy
928 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
929 	[NL80211_NAN_FUNC_TYPE] =
930 		NLA_POLICY_MAX(NLA_U8, NL80211_NAN_FUNC_MAX_TYPE),
931 	[NL80211_NAN_FUNC_SERVICE_ID] = {
932 				    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
933 	[NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
934 	[NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
935 	[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
936 	[NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
937 	[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
938 	[NL80211_NAN_FUNC_FOLLOW_UP_DEST] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
939 	[NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
940 	[NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
941 	[NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
942 			.len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
943 	[NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
944 	[NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
945 	[NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
946 	[NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
947 	[NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
948 };
949 
950 /* policy for Service Response Filter attributes */
951 static const struct nla_policy
952 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
953 	[NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
954 	[NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
955 				 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
956 	[NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
957 	[NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
958 };
959 
960 /* policy for packet pattern attributes */
961 static const struct nla_policy
962 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
963 	[NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
964 	[NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
965 	[NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
966 };
967 
968 static int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
969 				     struct cfg80211_registered_device **rdev,
970 				     struct wireless_dev **wdev,
971 				     struct nlattr **attrbuf)
972 {
973 	int err;
974 
975 	if (!cb->args[0]) {
976 		struct nlattr **attrbuf_free = NULL;
977 
978 		if (!attrbuf) {
979 			attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
980 					  GFP_KERNEL);
981 			if (!attrbuf)
982 				return -ENOMEM;
983 			attrbuf_free = attrbuf;
984 		}
985 
986 		err = nlmsg_parse_deprecated(cb->nlh,
987 					     GENL_HDRLEN + nl80211_fam.hdrsize,
988 					     attrbuf, nl80211_fam.maxattr,
989 					     nl80211_policy, NULL);
990 		if (err) {
991 			kfree(attrbuf_free);
992 			return err;
993 		}
994 
995 		rtnl_lock();
996 		*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(cb->skb->sk),
997 						   attrbuf);
998 		kfree(attrbuf_free);
999 		if (IS_ERR(*wdev)) {
1000 			rtnl_unlock();
1001 			return PTR_ERR(*wdev);
1002 		}
1003 		*rdev = wiphy_to_rdev((*wdev)->wiphy);
1004 		mutex_lock(&(*rdev)->wiphy.mtx);
1005 		rtnl_unlock();
1006 		/* 0 is the first index - add 1 to parse only once */
1007 		cb->args[0] = (*rdev)->wiphy_idx + 1;
1008 		cb->args[1] = (*wdev)->identifier;
1009 	} else {
1010 		/* subtract the 1 again here */
1011 		struct wiphy *wiphy;
1012 		struct wireless_dev *tmp;
1013 
1014 		rtnl_lock();
1015 		wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
1016 		if (!wiphy) {
1017 			rtnl_unlock();
1018 			return -ENODEV;
1019 		}
1020 		*rdev = wiphy_to_rdev(wiphy);
1021 		*wdev = NULL;
1022 
1023 		list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
1024 			if (tmp->identifier == cb->args[1]) {
1025 				*wdev = tmp;
1026 				break;
1027 			}
1028 		}
1029 
1030 		if (!*wdev) {
1031 			rtnl_unlock();
1032 			return -ENODEV;
1033 		}
1034 		mutex_lock(&(*rdev)->wiphy.mtx);
1035 		rtnl_unlock();
1036 	}
1037 
1038 	return 0;
1039 }
1040 
1041 /* message building helper */
1042 void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1043 		     int flags, u8 cmd)
1044 {
1045 	/* since there is no private header just add the generic one */
1046 	return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
1047 }
1048 
1049 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
1050 				     const struct ieee80211_reg_rule *rule)
1051 {
1052 	int j;
1053 	struct nlattr *nl_wmm_rules =
1054 		nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
1055 
1056 	if (!nl_wmm_rules)
1057 		goto nla_put_failure;
1058 
1059 	for (j = 0; j < IEEE80211_NUM_ACS; j++) {
1060 		struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
1061 
1062 		if (!nl_wmm_rule)
1063 			goto nla_put_failure;
1064 
1065 		if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
1066 				rule->wmm_rule.client[j].cw_min) ||
1067 		    nla_put_u16(msg, NL80211_WMMR_CW_MAX,
1068 				rule->wmm_rule.client[j].cw_max) ||
1069 		    nla_put_u8(msg, NL80211_WMMR_AIFSN,
1070 			       rule->wmm_rule.client[j].aifsn) ||
1071 		    nla_put_u16(msg, NL80211_WMMR_TXOP,
1072 			        rule->wmm_rule.client[j].cot))
1073 			goto nla_put_failure;
1074 
1075 		nla_nest_end(msg, nl_wmm_rule);
1076 	}
1077 	nla_nest_end(msg, nl_wmm_rules);
1078 
1079 	return 0;
1080 
1081 nla_put_failure:
1082 	return -ENOBUFS;
1083 }
1084 
1085 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
1086 				   struct ieee80211_channel *chan,
1087 				   bool large)
1088 {
1089 	/* Some channels must be completely excluded from the
1090 	 * list to protect old user-space tools from breaking
1091 	 */
1092 	if (!large && chan->flags &
1093 	    (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
1094 		return 0;
1095 	if (!large && chan->freq_offset)
1096 		return 0;
1097 
1098 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
1099 			chan->center_freq))
1100 		goto nla_put_failure;
1101 
1102 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_OFFSET, chan->freq_offset))
1103 		goto nla_put_failure;
1104 
1105 	if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
1106 	    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
1107 		goto nla_put_failure;
1108 	if (chan->flags & IEEE80211_CHAN_NO_IR) {
1109 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
1110 			goto nla_put_failure;
1111 		if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
1112 			goto nla_put_failure;
1113 	}
1114 	if (chan->flags & IEEE80211_CHAN_RADAR) {
1115 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
1116 			goto nla_put_failure;
1117 		if (large) {
1118 			u32 time;
1119 
1120 			time = elapsed_jiffies_msecs(chan->dfs_state_entered);
1121 
1122 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
1123 					chan->dfs_state))
1124 				goto nla_put_failure;
1125 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
1126 					time))
1127 				goto nla_put_failure;
1128 			if (nla_put_u32(msg,
1129 					NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
1130 					chan->dfs_cac_ms))
1131 				goto nla_put_failure;
1132 		}
1133 	}
1134 
1135 	if (large) {
1136 		if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
1137 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
1138 			goto nla_put_failure;
1139 		if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
1140 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
1141 			goto nla_put_failure;
1142 		if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
1143 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
1144 			goto nla_put_failure;
1145 		if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
1146 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
1147 			goto nla_put_failure;
1148 		if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
1149 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
1150 			goto nla_put_failure;
1151 		if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
1152 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
1153 			goto nla_put_failure;
1154 		if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
1155 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
1156 			goto nla_put_failure;
1157 		if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
1158 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
1159 			goto nla_put_failure;
1160 		if ((chan->flags & IEEE80211_CHAN_NO_HE) &&
1161 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HE))
1162 			goto nla_put_failure;
1163 		if ((chan->flags & IEEE80211_CHAN_1MHZ) &&
1164 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_1MHZ))
1165 			goto nla_put_failure;
1166 		if ((chan->flags & IEEE80211_CHAN_2MHZ) &&
1167 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_2MHZ))
1168 			goto nla_put_failure;
1169 		if ((chan->flags & IEEE80211_CHAN_4MHZ) &&
1170 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_4MHZ))
1171 			goto nla_put_failure;
1172 		if ((chan->flags & IEEE80211_CHAN_8MHZ) &&
1173 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_8MHZ))
1174 			goto nla_put_failure;
1175 		if ((chan->flags & IEEE80211_CHAN_16MHZ) &&
1176 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_16MHZ))
1177 			goto nla_put_failure;
1178 		if ((chan->flags & IEEE80211_CHAN_NO_320MHZ) &&
1179 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_320MHZ))
1180 			goto nla_put_failure;
1181 		if ((chan->flags & IEEE80211_CHAN_NO_EHT) &&
1182 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_EHT))
1183 			goto nla_put_failure;
1184 	}
1185 
1186 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
1187 			DBM_TO_MBM(chan->max_power)))
1188 		goto nla_put_failure;
1189 
1190 	if (large) {
1191 		const struct ieee80211_reg_rule *rule =
1192 			freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1193 
1194 		if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
1195 			if (nl80211_msg_put_wmm_rules(msg, rule))
1196 				goto nla_put_failure;
1197 		}
1198 	}
1199 
1200 	return 0;
1201 
1202  nla_put_failure:
1203 	return -ENOBUFS;
1204 }
1205 
1206 static bool nl80211_put_txq_stats(struct sk_buff *msg,
1207 				  struct cfg80211_txq_stats *txqstats,
1208 				  int attrtype)
1209 {
1210 	struct nlattr *txqattr;
1211 
1212 #define PUT_TXQVAL_U32(attr, memb) do {					  \
1213 	if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&	  \
1214 	    nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
1215 		return false;						  \
1216 	} while (0)
1217 
1218 	txqattr = nla_nest_start_noflag(msg, attrtype);
1219 	if (!txqattr)
1220 		return false;
1221 
1222 	PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
1223 	PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
1224 	PUT_TXQVAL_U32(FLOWS, flows);
1225 	PUT_TXQVAL_U32(DROPS, drops);
1226 	PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
1227 	PUT_TXQVAL_U32(OVERLIMIT, overlimit);
1228 	PUT_TXQVAL_U32(OVERMEMORY, overmemory);
1229 	PUT_TXQVAL_U32(COLLISIONS, collisions);
1230 	PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
1231 	PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
1232 	PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
1233 	nla_nest_end(msg, txqattr);
1234 
1235 #undef PUT_TXQVAL_U32
1236 	return true;
1237 }
1238 
1239 /* netlink command implementations */
1240 
1241 /**
1242  * nl80211_link_id - return link ID
1243  * @attrs: attributes to look at
1244  *
1245  * Returns: the link ID or 0 if not given
1246  *
1247  * Note this function doesn't do any validation of the link
1248  * ID validity wrt. links that were actually added, so it must
1249  * be called only from ops with %NL80211_FLAG_MLO_VALID_LINK_ID
1250  * or if additional validation is done.
1251  */
1252 static unsigned int nl80211_link_id(struct nlattr **attrs)
1253 {
1254 	struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1255 
1256 	if (!linkid)
1257 		return 0;
1258 
1259 	return nla_get_u8(linkid);
1260 }
1261 
1262 static int nl80211_link_id_or_invalid(struct nlattr **attrs)
1263 {
1264 	struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1265 
1266 	if (!linkid)
1267 		return -1;
1268 
1269 	return nla_get_u8(linkid);
1270 }
1271 
1272 struct key_parse {
1273 	struct key_params p;
1274 	int idx;
1275 	int type;
1276 	bool def, defmgmt, defbeacon;
1277 	bool def_uni, def_multi;
1278 };
1279 
1280 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
1281 				 struct key_parse *k)
1282 {
1283 	struct nlattr *tb[NL80211_KEY_MAX + 1];
1284 	int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
1285 					      nl80211_key_policy,
1286 					      info->extack);
1287 	if (err)
1288 		return err;
1289 
1290 	k->def = !!tb[NL80211_KEY_DEFAULT];
1291 	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1292 	k->defbeacon = !!tb[NL80211_KEY_DEFAULT_BEACON];
1293 
1294 	if (k->def) {
1295 		k->def_uni = true;
1296 		k->def_multi = true;
1297 	}
1298 	if (k->defmgmt || k->defbeacon)
1299 		k->def_multi = true;
1300 
1301 	if (tb[NL80211_KEY_IDX])
1302 		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1303 
1304 	if (tb[NL80211_KEY_DATA]) {
1305 		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1306 		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1307 	}
1308 
1309 	if (tb[NL80211_KEY_SEQ]) {
1310 		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1311 		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1312 	}
1313 
1314 	if (tb[NL80211_KEY_CIPHER])
1315 		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1316 
1317 	if (tb[NL80211_KEY_TYPE])
1318 		k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1319 
1320 	if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1321 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1322 
1323 		err = nla_parse_nested_deprecated(kdt,
1324 						  NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1325 						  tb[NL80211_KEY_DEFAULT_TYPES],
1326 						  nl80211_key_default_policy,
1327 						  info->extack);
1328 		if (err)
1329 			return err;
1330 
1331 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1332 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1333 	}
1334 
1335 	if (tb[NL80211_KEY_MODE])
1336 		k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1337 
1338 	return 0;
1339 }
1340 
1341 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1342 {
1343 	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1344 		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1345 		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1346 	}
1347 
1348 	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1349 		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1350 		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1351 	}
1352 
1353 	if (info->attrs[NL80211_ATTR_KEY_IDX])
1354 		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1355 
1356 	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1357 		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1358 
1359 	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1360 	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1361 
1362 	if (k->def) {
1363 		k->def_uni = true;
1364 		k->def_multi = true;
1365 	}
1366 	if (k->defmgmt)
1367 		k->def_multi = true;
1368 
1369 	if (info->attrs[NL80211_ATTR_KEY_TYPE])
1370 		k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1371 
1372 	if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1373 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1374 		int err = nla_parse_nested_deprecated(kdt,
1375 						      NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1376 						      info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1377 						      nl80211_key_default_policy,
1378 						      info->extack);
1379 		if (err)
1380 			return err;
1381 
1382 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1383 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1384 	}
1385 
1386 	return 0;
1387 }
1388 
1389 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1390 {
1391 	int err;
1392 
1393 	memset(k, 0, sizeof(*k));
1394 	k->idx = -1;
1395 	k->type = -1;
1396 
1397 	if (info->attrs[NL80211_ATTR_KEY])
1398 		err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1399 	else
1400 		err = nl80211_parse_key_old(info, k);
1401 
1402 	if (err)
1403 		return err;
1404 
1405 	if ((k->def ? 1 : 0) + (k->defmgmt ? 1 : 0) +
1406 	    (k->defbeacon ? 1 : 0) > 1) {
1407 		GENL_SET_ERR_MSG(info,
1408 				 "key with multiple default flags is invalid");
1409 		return -EINVAL;
1410 	}
1411 
1412 	if (k->defmgmt || k->defbeacon) {
1413 		if (k->def_uni || !k->def_multi) {
1414 			GENL_SET_ERR_MSG(info,
1415 					 "defmgmt/defbeacon key must be mcast");
1416 			return -EINVAL;
1417 		}
1418 	}
1419 
1420 	if (k->idx != -1) {
1421 		if (k->defmgmt) {
1422 			if (k->idx < 4 || k->idx > 5) {
1423 				GENL_SET_ERR_MSG(info,
1424 						 "defmgmt key idx not 4 or 5");
1425 				return -EINVAL;
1426 			}
1427 		} else if (k->defbeacon) {
1428 			if (k->idx < 6 || k->idx > 7) {
1429 				GENL_SET_ERR_MSG(info,
1430 						 "defbeacon key idx not 6 or 7");
1431 				return -EINVAL;
1432 			}
1433 		} else if (k->def) {
1434 			if (k->idx < 0 || k->idx > 3) {
1435 				GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1436 				return -EINVAL;
1437 			}
1438 		} else {
1439 			if (k->idx < 0 || k->idx > 7) {
1440 				GENL_SET_ERR_MSG(info, "key idx not 0-7");
1441 				return -EINVAL;
1442 			}
1443 		}
1444 	}
1445 
1446 	return 0;
1447 }
1448 
1449 static struct cfg80211_cached_keys *
1450 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1451 		       struct genl_info *info, bool *no_ht)
1452 {
1453 	struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1454 	struct key_parse parse;
1455 	struct nlattr *key;
1456 	struct cfg80211_cached_keys *result;
1457 	int rem, err, def = 0;
1458 	bool have_key = false;
1459 
1460 	nla_for_each_nested(key, keys, rem) {
1461 		have_key = true;
1462 		break;
1463 	}
1464 
1465 	if (!have_key)
1466 		return NULL;
1467 
1468 	result = kzalloc(sizeof(*result), GFP_KERNEL);
1469 	if (!result)
1470 		return ERR_PTR(-ENOMEM);
1471 
1472 	result->def = -1;
1473 
1474 	nla_for_each_nested(key, keys, rem) {
1475 		memset(&parse, 0, sizeof(parse));
1476 		parse.idx = -1;
1477 
1478 		err = nl80211_parse_key_new(info, key, &parse);
1479 		if (err)
1480 			goto error;
1481 		err = -EINVAL;
1482 		if (!parse.p.key)
1483 			goto error;
1484 		if (parse.idx < 0 || parse.idx > 3) {
1485 			GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1486 			goto error;
1487 		}
1488 		if (parse.def) {
1489 			if (def) {
1490 				GENL_SET_ERR_MSG(info,
1491 						 "only one key can be default");
1492 				goto error;
1493 			}
1494 			def = 1;
1495 			result->def = parse.idx;
1496 			if (!parse.def_uni || !parse.def_multi)
1497 				goto error;
1498 		} else if (parse.defmgmt)
1499 			goto error;
1500 		err = cfg80211_validate_key_settings(rdev, &parse.p,
1501 						     parse.idx, false, NULL);
1502 		if (err)
1503 			goto error;
1504 		if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1505 		    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1506 			GENL_SET_ERR_MSG(info, "connect key must be WEP");
1507 			err = -EINVAL;
1508 			goto error;
1509 		}
1510 		result->params[parse.idx].cipher = parse.p.cipher;
1511 		result->params[parse.idx].key_len = parse.p.key_len;
1512 		result->params[parse.idx].key = result->data[parse.idx];
1513 		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1514 
1515 		/* must be WEP key if we got here */
1516 		if (no_ht)
1517 			*no_ht = true;
1518 	}
1519 
1520 	if (result->def < 0) {
1521 		err = -EINVAL;
1522 		GENL_SET_ERR_MSG(info, "need a default/TX key");
1523 		goto error;
1524 	}
1525 
1526 	return result;
1527  error:
1528 	kfree(result);
1529 	return ERR_PTR(err);
1530 }
1531 
1532 static int nl80211_key_allowed(struct wireless_dev *wdev)
1533 {
1534 	ASSERT_WDEV_LOCK(wdev);
1535 
1536 	switch (wdev->iftype) {
1537 	case NL80211_IFTYPE_AP:
1538 	case NL80211_IFTYPE_AP_VLAN:
1539 	case NL80211_IFTYPE_P2P_GO:
1540 	case NL80211_IFTYPE_MESH_POINT:
1541 		break;
1542 	case NL80211_IFTYPE_ADHOC:
1543 		if (wdev->u.ibss.current_bss)
1544 			return 0;
1545 		return -ENOLINK;
1546 	case NL80211_IFTYPE_STATION:
1547 	case NL80211_IFTYPE_P2P_CLIENT:
1548 		if (wdev->connected)
1549 			return 0;
1550 		return -ENOLINK;
1551 	case NL80211_IFTYPE_UNSPECIFIED:
1552 	case NL80211_IFTYPE_OCB:
1553 	case NL80211_IFTYPE_MONITOR:
1554 	case NL80211_IFTYPE_NAN:
1555 	case NL80211_IFTYPE_P2P_DEVICE:
1556 	case NL80211_IFTYPE_WDS:
1557 	case NUM_NL80211_IFTYPES:
1558 		return -EINVAL;
1559 	}
1560 
1561 	return 0;
1562 }
1563 
1564 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1565 							u32 freq)
1566 {
1567 	struct ieee80211_channel *chan;
1568 
1569 	chan = ieee80211_get_channel_khz(wiphy, freq);
1570 	if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1571 		return NULL;
1572 	return chan;
1573 }
1574 
1575 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1576 {
1577 	struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1578 	int i;
1579 
1580 	if (!nl_modes)
1581 		goto nla_put_failure;
1582 
1583 	i = 0;
1584 	while (ifmodes) {
1585 		if ((ifmodes & 1) && nla_put_flag(msg, i))
1586 			goto nla_put_failure;
1587 		ifmodes >>= 1;
1588 		i++;
1589 	}
1590 
1591 	nla_nest_end(msg, nl_modes);
1592 	return 0;
1593 
1594 nla_put_failure:
1595 	return -ENOBUFS;
1596 }
1597 
1598 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1599 					  struct sk_buff *msg,
1600 					  bool large)
1601 {
1602 	struct nlattr *nl_combis;
1603 	int i, j;
1604 
1605 	nl_combis = nla_nest_start_noflag(msg,
1606 					  NL80211_ATTR_INTERFACE_COMBINATIONS);
1607 	if (!nl_combis)
1608 		goto nla_put_failure;
1609 
1610 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1611 		const struct ieee80211_iface_combination *c;
1612 		struct nlattr *nl_combi, *nl_limits;
1613 
1614 		c = &wiphy->iface_combinations[i];
1615 
1616 		nl_combi = nla_nest_start_noflag(msg, i + 1);
1617 		if (!nl_combi)
1618 			goto nla_put_failure;
1619 
1620 		nl_limits = nla_nest_start_noflag(msg,
1621 						  NL80211_IFACE_COMB_LIMITS);
1622 		if (!nl_limits)
1623 			goto nla_put_failure;
1624 
1625 		for (j = 0; j < c->n_limits; j++) {
1626 			struct nlattr *nl_limit;
1627 
1628 			nl_limit = nla_nest_start_noflag(msg, j + 1);
1629 			if (!nl_limit)
1630 				goto nla_put_failure;
1631 			if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1632 					c->limits[j].max))
1633 				goto nla_put_failure;
1634 			if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1635 						c->limits[j].types))
1636 				goto nla_put_failure;
1637 			nla_nest_end(msg, nl_limit);
1638 		}
1639 
1640 		nla_nest_end(msg, nl_limits);
1641 
1642 		if (c->beacon_int_infra_match &&
1643 		    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1644 			goto nla_put_failure;
1645 		if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1646 				c->num_different_channels) ||
1647 		    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1648 				c->max_interfaces))
1649 			goto nla_put_failure;
1650 		if (large &&
1651 		    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1652 				c->radar_detect_widths) ||
1653 		     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1654 				c->radar_detect_regions)))
1655 			goto nla_put_failure;
1656 		if (c->beacon_int_min_gcd &&
1657 		    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1658 				c->beacon_int_min_gcd))
1659 			goto nla_put_failure;
1660 
1661 		nla_nest_end(msg, nl_combi);
1662 	}
1663 
1664 	nla_nest_end(msg, nl_combis);
1665 
1666 	return 0;
1667 nla_put_failure:
1668 	return -ENOBUFS;
1669 }
1670 
1671 #ifdef CONFIG_PM
1672 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1673 					struct sk_buff *msg)
1674 {
1675 	const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1676 	struct nlattr *nl_tcp;
1677 
1678 	if (!tcp)
1679 		return 0;
1680 
1681 	nl_tcp = nla_nest_start_noflag(msg,
1682 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1683 	if (!nl_tcp)
1684 		return -ENOBUFS;
1685 
1686 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1687 			tcp->data_payload_max))
1688 		return -ENOBUFS;
1689 
1690 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1691 			tcp->data_payload_max))
1692 		return -ENOBUFS;
1693 
1694 	if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1695 		return -ENOBUFS;
1696 
1697 	if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1698 				sizeof(*tcp->tok), tcp->tok))
1699 		return -ENOBUFS;
1700 
1701 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1702 			tcp->data_interval_max))
1703 		return -ENOBUFS;
1704 
1705 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1706 			tcp->wake_payload_max))
1707 		return -ENOBUFS;
1708 
1709 	nla_nest_end(msg, nl_tcp);
1710 	return 0;
1711 }
1712 
1713 static int nl80211_send_wowlan(struct sk_buff *msg,
1714 			       struct cfg80211_registered_device *rdev,
1715 			       bool large)
1716 {
1717 	struct nlattr *nl_wowlan;
1718 
1719 	if (!rdev->wiphy.wowlan)
1720 		return 0;
1721 
1722 	nl_wowlan = nla_nest_start_noflag(msg,
1723 					  NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1724 	if (!nl_wowlan)
1725 		return -ENOBUFS;
1726 
1727 	if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1728 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1729 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1730 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1731 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1732 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1733 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1734 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1735 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1736 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1737 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1738 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1739 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1740 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1741 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1742 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1743 		return -ENOBUFS;
1744 
1745 	if (rdev->wiphy.wowlan->n_patterns) {
1746 		struct nl80211_pattern_support pat = {
1747 			.max_patterns = rdev->wiphy.wowlan->n_patterns,
1748 			.min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1749 			.max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1750 			.max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1751 		};
1752 
1753 		if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1754 			    sizeof(pat), &pat))
1755 			return -ENOBUFS;
1756 	}
1757 
1758 	if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1759 	    nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1760 			rdev->wiphy.wowlan->max_nd_match_sets))
1761 		return -ENOBUFS;
1762 
1763 	if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1764 		return -ENOBUFS;
1765 
1766 	nla_nest_end(msg, nl_wowlan);
1767 
1768 	return 0;
1769 }
1770 #endif
1771 
1772 static int nl80211_send_coalesce(struct sk_buff *msg,
1773 				 struct cfg80211_registered_device *rdev)
1774 {
1775 	struct nl80211_coalesce_rule_support rule;
1776 
1777 	if (!rdev->wiphy.coalesce)
1778 		return 0;
1779 
1780 	rule.max_rules = rdev->wiphy.coalesce->n_rules;
1781 	rule.max_delay = rdev->wiphy.coalesce->max_delay;
1782 	rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1783 	rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1784 	rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1785 	rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1786 
1787 	if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1788 		return -ENOBUFS;
1789 
1790 	return 0;
1791 }
1792 
1793 static int
1794 nl80211_send_iftype_data(struct sk_buff *msg,
1795 			 const struct ieee80211_supported_band *sband,
1796 			 const struct ieee80211_sband_iftype_data *iftdata)
1797 {
1798 	const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1799 	const struct ieee80211_sta_eht_cap *eht_cap = &iftdata->eht_cap;
1800 
1801 	if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1802 				iftdata->types_mask))
1803 		return -ENOBUFS;
1804 
1805 	if (he_cap->has_he) {
1806 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1807 			    sizeof(he_cap->he_cap_elem.mac_cap_info),
1808 			    he_cap->he_cap_elem.mac_cap_info) ||
1809 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1810 			    sizeof(he_cap->he_cap_elem.phy_cap_info),
1811 			    he_cap->he_cap_elem.phy_cap_info) ||
1812 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1813 			    sizeof(he_cap->he_mcs_nss_supp),
1814 			    &he_cap->he_mcs_nss_supp) ||
1815 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1816 			    sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1817 			return -ENOBUFS;
1818 	}
1819 
1820 	if (eht_cap->has_eht && he_cap->has_he) {
1821 		u8 mcs_nss_size, ppe_thresh_size;
1822 		u16 ppe_thres_hdr;
1823 		bool is_ap;
1824 
1825 		is_ap = iftdata->types_mask & BIT(NL80211_IFTYPE_AP) ||
1826 			iftdata->types_mask & BIT(NL80211_IFTYPE_P2P_GO);
1827 
1828 		mcs_nss_size =
1829 			ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
1830 						   &eht_cap->eht_cap_elem,
1831 						   is_ap);
1832 
1833 		ppe_thres_hdr = get_unaligned_le16(&eht_cap->eht_ppe_thres[0]);
1834 		ppe_thresh_size =
1835 			ieee80211_eht_ppe_size(ppe_thres_hdr,
1836 					       eht_cap->eht_cap_elem.phy_cap_info);
1837 
1838 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MAC,
1839 			    sizeof(eht_cap->eht_cap_elem.mac_cap_info),
1840 			    eht_cap->eht_cap_elem.mac_cap_info) ||
1841 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY,
1842 			    sizeof(eht_cap->eht_cap_elem.phy_cap_info),
1843 			    eht_cap->eht_cap_elem.phy_cap_info) ||
1844 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MCS_SET,
1845 			    mcs_nss_size, &eht_cap->eht_mcs_nss_supp) ||
1846 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PPE,
1847 			    ppe_thresh_size, eht_cap->eht_ppe_thres))
1848 			return -ENOBUFS;
1849 	}
1850 
1851 	if (sband->band == NL80211_BAND_6GHZ &&
1852 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_6GHZ_CAPA,
1853 		    sizeof(iftdata->he_6ghz_capa),
1854 		    &iftdata->he_6ghz_capa))
1855 		return -ENOBUFS;
1856 
1857 	if (iftdata->vendor_elems.data && iftdata->vendor_elems.len &&
1858 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_VENDOR_ELEMS,
1859 		    iftdata->vendor_elems.len, iftdata->vendor_elems.data))
1860 		return -ENOBUFS;
1861 
1862 	return 0;
1863 }
1864 
1865 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1866 				      struct ieee80211_supported_band *sband,
1867 				      bool large)
1868 {
1869 	struct nlattr *nl_rates, *nl_rate;
1870 	struct ieee80211_rate *rate;
1871 	int i;
1872 
1873 	/* add HT info */
1874 	if (sband->ht_cap.ht_supported &&
1875 	    (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1876 		     sizeof(sband->ht_cap.mcs),
1877 		     &sband->ht_cap.mcs) ||
1878 	     nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1879 			 sband->ht_cap.cap) ||
1880 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1881 			sband->ht_cap.ampdu_factor) ||
1882 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1883 			sband->ht_cap.ampdu_density)))
1884 		return -ENOBUFS;
1885 
1886 	/* add VHT info */
1887 	if (sband->vht_cap.vht_supported &&
1888 	    (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1889 		     sizeof(sband->vht_cap.vht_mcs),
1890 		     &sband->vht_cap.vht_mcs) ||
1891 	     nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1892 			 sband->vht_cap.cap)))
1893 		return -ENOBUFS;
1894 
1895 	if (large && sband->n_iftype_data) {
1896 		struct nlattr *nl_iftype_data =
1897 			nla_nest_start_noflag(msg,
1898 					      NL80211_BAND_ATTR_IFTYPE_DATA);
1899 		int err;
1900 
1901 		if (!nl_iftype_data)
1902 			return -ENOBUFS;
1903 
1904 		for (i = 0; i < sband->n_iftype_data; i++) {
1905 			struct nlattr *iftdata;
1906 
1907 			iftdata = nla_nest_start_noflag(msg, i + 1);
1908 			if (!iftdata)
1909 				return -ENOBUFS;
1910 
1911 			err = nl80211_send_iftype_data(msg, sband,
1912 						       &sband->iftype_data[i]);
1913 			if (err)
1914 				return err;
1915 
1916 			nla_nest_end(msg, iftdata);
1917 		}
1918 
1919 		nla_nest_end(msg, nl_iftype_data);
1920 	}
1921 
1922 	/* add EDMG info */
1923 	if (large && sband->edmg_cap.channels &&
1924 	    (nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_CHANNELS,
1925 		       sband->edmg_cap.channels) ||
1926 	    nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_BW_CONFIG,
1927 		       sband->edmg_cap.bw_config)))
1928 
1929 		return -ENOBUFS;
1930 
1931 	/* add bitrates */
1932 	nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1933 	if (!nl_rates)
1934 		return -ENOBUFS;
1935 
1936 	for (i = 0; i < sband->n_bitrates; i++) {
1937 		nl_rate = nla_nest_start_noflag(msg, i);
1938 		if (!nl_rate)
1939 			return -ENOBUFS;
1940 
1941 		rate = &sband->bitrates[i];
1942 		if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1943 				rate->bitrate))
1944 			return -ENOBUFS;
1945 		if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1946 		    nla_put_flag(msg,
1947 				 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1948 			return -ENOBUFS;
1949 
1950 		nla_nest_end(msg, nl_rate);
1951 	}
1952 
1953 	nla_nest_end(msg, nl_rates);
1954 
1955 	return 0;
1956 }
1957 
1958 static int
1959 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1960 			 const struct ieee80211_txrx_stypes *mgmt_stypes)
1961 {
1962 	u16 stypes;
1963 	struct nlattr *nl_ftypes, *nl_ifs;
1964 	enum nl80211_iftype ift;
1965 	int i;
1966 
1967 	if (!mgmt_stypes)
1968 		return 0;
1969 
1970 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
1971 	if (!nl_ifs)
1972 		return -ENOBUFS;
1973 
1974 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1975 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1976 		if (!nl_ftypes)
1977 			return -ENOBUFS;
1978 		i = 0;
1979 		stypes = mgmt_stypes[ift].tx;
1980 		while (stypes) {
1981 			if ((stypes & 1) &&
1982 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1983 					(i << 4) | IEEE80211_FTYPE_MGMT))
1984 				return -ENOBUFS;
1985 			stypes >>= 1;
1986 			i++;
1987 		}
1988 		nla_nest_end(msg, nl_ftypes);
1989 	}
1990 
1991 	nla_nest_end(msg, nl_ifs);
1992 
1993 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
1994 	if (!nl_ifs)
1995 		return -ENOBUFS;
1996 
1997 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1998 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1999 		if (!nl_ftypes)
2000 			return -ENOBUFS;
2001 		i = 0;
2002 		stypes = mgmt_stypes[ift].rx;
2003 		while (stypes) {
2004 			if ((stypes & 1) &&
2005 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
2006 					(i << 4) | IEEE80211_FTYPE_MGMT))
2007 				return -ENOBUFS;
2008 			stypes >>= 1;
2009 			i++;
2010 		}
2011 		nla_nest_end(msg, nl_ftypes);
2012 	}
2013 	nla_nest_end(msg, nl_ifs);
2014 
2015 	return 0;
2016 }
2017 
2018 #define CMD(op, n)							\
2019 	 do {								\
2020 		if (rdev->ops->op) {					\
2021 			i++;						\
2022 			if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) 	\
2023 				goto nla_put_failure;			\
2024 		}							\
2025 	} while (0)
2026 
2027 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
2028 					struct sk_buff *msg)
2029 {
2030 	int i = 0;
2031 
2032 	/*
2033 	 * do *NOT* add anything into this function, new things need to be
2034 	 * advertised only to new versions of userspace that can deal with
2035 	 * the split (and they can't possibly care about new features...
2036 	 */
2037 	CMD(add_virtual_intf, NEW_INTERFACE);
2038 	CMD(change_virtual_intf, SET_INTERFACE);
2039 	CMD(add_key, NEW_KEY);
2040 	CMD(start_ap, START_AP);
2041 	CMD(add_station, NEW_STATION);
2042 	CMD(add_mpath, NEW_MPATH);
2043 	CMD(update_mesh_config, SET_MESH_CONFIG);
2044 	CMD(change_bss, SET_BSS);
2045 	CMD(auth, AUTHENTICATE);
2046 	CMD(assoc, ASSOCIATE);
2047 	CMD(deauth, DEAUTHENTICATE);
2048 	CMD(disassoc, DISASSOCIATE);
2049 	CMD(join_ibss, JOIN_IBSS);
2050 	CMD(join_mesh, JOIN_MESH);
2051 	CMD(set_pmksa, SET_PMKSA);
2052 	CMD(del_pmksa, DEL_PMKSA);
2053 	CMD(flush_pmksa, FLUSH_PMKSA);
2054 	if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
2055 		CMD(remain_on_channel, REMAIN_ON_CHANNEL);
2056 	CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
2057 	CMD(mgmt_tx, FRAME);
2058 	CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
2059 	if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
2060 		i++;
2061 		if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
2062 			goto nla_put_failure;
2063 	}
2064 	if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
2065 	    rdev->ops->join_mesh) {
2066 		i++;
2067 		if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
2068 			goto nla_put_failure;
2069 	}
2070 	if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2071 		CMD(tdls_mgmt, TDLS_MGMT);
2072 		CMD(tdls_oper, TDLS_OPER);
2073 	}
2074 	if (rdev->wiphy.max_sched_scan_reqs)
2075 		CMD(sched_scan_start, START_SCHED_SCAN);
2076 	CMD(probe_client, PROBE_CLIENT);
2077 	CMD(set_noack_map, SET_NOACK_MAP);
2078 	if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
2079 		i++;
2080 		if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
2081 			goto nla_put_failure;
2082 	}
2083 	CMD(start_p2p_device, START_P2P_DEVICE);
2084 	CMD(set_mcast_rate, SET_MCAST_RATE);
2085 #ifdef CONFIG_NL80211_TESTMODE
2086 	CMD(testmode_cmd, TESTMODE);
2087 #endif
2088 
2089 	if (rdev->ops->connect || rdev->ops->auth) {
2090 		i++;
2091 		if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
2092 			goto nla_put_failure;
2093 	}
2094 
2095 	if (rdev->ops->disconnect || rdev->ops->deauth) {
2096 		i++;
2097 		if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
2098 			goto nla_put_failure;
2099 	}
2100 
2101 	return i;
2102  nla_put_failure:
2103 	return -ENOBUFS;
2104 }
2105 
2106 static int
2107 nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
2108 			   struct sk_buff *msg)
2109 {
2110 	struct nlattr *ftm;
2111 
2112 	if (!cap->ftm.supported)
2113 		return 0;
2114 
2115 	ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
2116 	if (!ftm)
2117 		return -ENOBUFS;
2118 
2119 	if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
2120 		return -ENOBUFS;
2121 	if (cap->ftm.non_asap &&
2122 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
2123 		return -ENOBUFS;
2124 	if (cap->ftm.request_lci &&
2125 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
2126 		return -ENOBUFS;
2127 	if (cap->ftm.request_civicloc &&
2128 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
2129 		return -ENOBUFS;
2130 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
2131 			cap->ftm.preambles))
2132 		return -ENOBUFS;
2133 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
2134 			cap->ftm.bandwidths))
2135 		return -ENOBUFS;
2136 	if (cap->ftm.max_bursts_exponent >= 0 &&
2137 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
2138 			cap->ftm.max_bursts_exponent))
2139 		return -ENOBUFS;
2140 	if (cap->ftm.max_ftms_per_burst &&
2141 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
2142 			cap->ftm.max_ftms_per_burst))
2143 		return -ENOBUFS;
2144 	if (cap->ftm.trigger_based &&
2145 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_TRIGGER_BASED))
2146 		return -ENOBUFS;
2147 	if (cap->ftm.non_trigger_based &&
2148 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_TRIGGER_BASED))
2149 		return -ENOBUFS;
2150 
2151 	nla_nest_end(msg, ftm);
2152 	return 0;
2153 }
2154 
2155 static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
2156 				  struct sk_buff *msg)
2157 {
2158 	const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
2159 	struct nlattr *pmsr, *caps;
2160 
2161 	if (!cap)
2162 		return 0;
2163 
2164 	/*
2165 	 * we don't need to clean up anything here since the caller
2166 	 * will genlmsg_cancel() if we fail
2167 	 */
2168 
2169 	pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
2170 	if (!pmsr)
2171 		return -ENOBUFS;
2172 
2173 	if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
2174 		return -ENOBUFS;
2175 
2176 	if (cap->report_ap_tsf &&
2177 	    nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
2178 		return -ENOBUFS;
2179 
2180 	if (cap->randomize_mac_addr &&
2181 	    nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
2182 		return -ENOBUFS;
2183 
2184 	caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
2185 	if (!caps)
2186 		return -ENOBUFS;
2187 
2188 	if (nl80211_send_pmsr_ftm_capa(cap, msg))
2189 		return -ENOBUFS;
2190 
2191 	nla_nest_end(msg, caps);
2192 	nla_nest_end(msg, pmsr);
2193 
2194 	return 0;
2195 }
2196 
2197 static int
2198 nl80211_put_iftype_akm_suites(struct cfg80211_registered_device *rdev,
2199 			      struct sk_buff *msg)
2200 {
2201 	int i;
2202 	struct nlattr *nested, *nested_akms;
2203 	const struct wiphy_iftype_akm_suites *iftype_akms;
2204 
2205 	if (!rdev->wiphy.num_iftype_akm_suites ||
2206 	    !rdev->wiphy.iftype_akm_suites)
2207 		return 0;
2208 
2209 	nested = nla_nest_start(msg, NL80211_ATTR_IFTYPE_AKM_SUITES);
2210 	if (!nested)
2211 		return -ENOBUFS;
2212 
2213 	for (i = 0; i < rdev->wiphy.num_iftype_akm_suites; i++) {
2214 		nested_akms = nla_nest_start(msg, i + 1);
2215 		if (!nested_akms)
2216 			return -ENOBUFS;
2217 
2218 		iftype_akms = &rdev->wiphy.iftype_akm_suites[i];
2219 
2220 		if (nl80211_put_iftypes(msg, NL80211_IFTYPE_AKM_ATTR_IFTYPES,
2221 					iftype_akms->iftypes_mask))
2222 			return -ENOBUFS;
2223 
2224 		if (nla_put(msg, NL80211_IFTYPE_AKM_ATTR_SUITES,
2225 			    sizeof(u32) * iftype_akms->n_akm_suites,
2226 			    iftype_akms->akm_suites)) {
2227 			return -ENOBUFS;
2228 		}
2229 		nla_nest_end(msg, nested_akms);
2230 	}
2231 
2232 	nla_nest_end(msg, nested);
2233 
2234 	return 0;
2235 }
2236 
2237 static int
2238 nl80211_put_tid_config_support(struct cfg80211_registered_device *rdev,
2239 			       struct sk_buff *msg)
2240 {
2241 	struct nlattr *supp;
2242 
2243 	if (!rdev->wiphy.tid_config_support.vif &&
2244 	    !rdev->wiphy.tid_config_support.peer)
2245 		return 0;
2246 
2247 	supp = nla_nest_start(msg, NL80211_ATTR_TID_CONFIG);
2248 	if (!supp)
2249 		return -ENOSPC;
2250 
2251 	if (rdev->wiphy.tid_config_support.vif &&
2252 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_VIF_SUPP,
2253 			      rdev->wiphy.tid_config_support.vif,
2254 			      NL80211_TID_CONFIG_ATTR_PAD))
2255 		goto fail;
2256 
2257 	if (rdev->wiphy.tid_config_support.peer &&
2258 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_PEER_SUPP,
2259 			      rdev->wiphy.tid_config_support.peer,
2260 			      NL80211_TID_CONFIG_ATTR_PAD))
2261 		goto fail;
2262 
2263 	/* for now we just use the same value ... makes more sense */
2264 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_SHORT,
2265 		       rdev->wiphy.tid_config_support.max_retry))
2266 		goto fail;
2267 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_LONG,
2268 		       rdev->wiphy.tid_config_support.max_retry))
2269 		goto fail;
2270 
2271 	nla_nest_end(msg, supp);
2272 
2273 	return 0;
2274 fail:
2275 	nla_nest_cancel(msg, supp);
2276 	return -ENOBUFS;
2277 }
2278 
2279 static int
2280 nl80211_put_sar_specs(struct cfg80211_registered_device *rdev,
2281 		      struct sk_buff *msg)
2282 {
2283 	struct nlattr *sar_capa, *specs, *sub_freq_range;
2284 	u8 num_freq_ranges;
2285 	int i;
2286 
2287 	if (!rdev->wiphy.sar_capa)
2288 		return 0;
2289 
2290 	num_freq_ranges = rdev->wiphy.sar_capa->num_freq_ranges;
2291 
2292 	sar_capa = nla_nest_start(msg, NL80211_ATTR_SAR_SPEC);
2293 	if (!sar_capa)
2294 		return -ENOSPC;
2295 
2296 	if (nla_put_u32(msg, NL80211_SAR_ATTR_TYPE, rdev->wiphy.sar_capa->type))
2297 		goto fail;
2298 
2299 	specs = nla_nest_start(msg, NL80211_SAR_ATTR_SPECS);
2300 	if (!specs)
2301 		goto fail;
2302 
2303 	/* report supported freq_ranges */
2304 	for (i = 0; i < num_freq_ranges; i++) {
2305 		sub_freq_range = nla_nest_start(msg, i + 1);
2306 		if (!sub_freq_range)
2307 			goto fail;
2308 
2309 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_START_FREQ,
2310 				rdev->wiphy.sar_capa->freq_ranges[i].start_freq))
2311 			goto fail;
2312 
2313 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_END_FREQ,
2314 				rdev->wiphy.sar_capa->freq_ranges[i].end_freq))
2315 			goto fail;
2316 
2317 		nla_nest_end(msg, sub_freq_range);
2318 	}
2319 
2320 	nla_nest_end(msg, specs);
2321 	nla_nest_end(msg, sar_capa);
2322 
2323 	return 0;
2324 fail:
2325 	nla_nest_cancel(msg, sar_capa);
2326 	return -ENOBUFS;
2327 }
2328 
2329 static int nl80211_put_mbssid_support(struct wiphy *wiphy, struct sk_buff *msg)
2330 {
2331 	struct nlattr *config;
2332 
2333 	if (!wiphy->mbssid_max_interfaces)
2334 		return 0;
2335 
2336 	config = nla_nest_start(msg, NL80211_ATTR_MBSSID_CONFIG);
2337 	if (!config)
2338 		return -ENOBUFS;
2339 
2340 	if (nla_put_u8(msg, NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES,
2341 		       wiphy->mbssid_max_interfaces))
2342 		goto fail;
2343 
2344 	if (wiphy->ema_max_profile_periodicity &&
2345 	    nla_put_u8(msg,
2346 		       NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY,
2347 		       wiphy->ema_max_profile_periodicity))
2348 		goto fail;
2349 
2350 	nla_nest_end(msg, config);
2351 	return 0;
2352 
2353 fail:
2354 	nla_nest_cancel(msg, config);
2355 	return -ENOBUFS;
2356 }
2357 
2358 struct nl80211_dump_wiphy_state {
2359 	s64 filter_wiphy;
2360 	long start;
2361 	long split_start, band_start, chan_start, capa_start;
2362 	bool split;
2363 };
2364 
2365 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
2366 			      enum nl80211_commands cmd,
2367 			      struct sk_buff *msg, u32 portid, u32 seq,
2368 			      int flags, struct nl80211_dump_wiphy_state *state)
2369 {
2370 	void *hdr;
2371 	struct nlattr *nl_bands, *nl_band;
2372 	struct nlattr *nl_freqs, *nl_freq;
2373 	struct nlattr *nl_cmds;
2374 	enum nl80211_band band;
2375 	struct ieee80211_channel *chan;
2376 	int i;
2377 	const struct ieee80211_txrx_stypes *mgmt_stypes =
2378 				rdev->wiphy.mgmt_stypes;
2379 	u32 features;
2380 
2381 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2382 	if (!hdr)
2383 		return -ENOBUFS;
2384 
2385 	if (WARN_ON(!state))
2386 		return -EINVAL;
2387 
2388 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2389 	    nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
2390 			   wiphy_name(&rdev->wiphy)) ||
2391 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
2392 			cfg80211_rdev_list_generation))
2393 		goto nla_put_failure;
2394 
2395 	if (cmd != NL80211_CMD_NEW_WIPHY)
2396 		goto finish;
2397 
2398 	switch (state->split_start) {
2399 	case 0:
2400 		if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
2401 			       rdev->wiphy.retry_short) ||
2402 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
2403 			       rdev->wiphy.retry_long) ||
2404 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
2405 				rdev->wiphy.frag_threshold) ||
2406 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
2407 				rdev->wiphy.rts_threshold) ||
2408 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
2409 			       rdev->wiphy.coverage_class) ||
2410 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
2411 			       rdev->wiphy.max_scan_ssids) ||
2412 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
2413 			       rdev->wiphy.max_sched_scan_ssids) ||
2414 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
2415 				rdev->wiphy.max_scan_ie_len) ||
2416 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
2417 				rdev->wiphy.max_sched_scan_ie_len) ||
2418 		    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
2419 			       rdev->wiphy.max_match_sets))
2420 			goto nla_put_failure;
2421 
2422 		if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
2423 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
2424 			goto nla_put_failure;
2425 		if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
2426 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
2427 			goto nla_put_failure;
2428 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2429 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
2430 			goto nla_put_failure;
2431 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
2432 		    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
2433 			goto nla_put_failure;
2434 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2435 		    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
2436 			goto nla_put_failure;
2437 		if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
2438 		    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
2439 			goto nla_put_failure;
2440 		state->split_start++;
2441 		if (state->split)
2442 			break;
2443 		fallthrough;
2444 	case 1:
2445 		if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
2446 			    sizeof(u32) * rdev->wiphy.n_cipher_suites,
2447 			    rdev->wiphy.cipher_suites))
2448 			goto nla_put_failure;
2449 
2450 		if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
2451 			       rdev->wiphy.max_num_pmkids))
2452 			goto nla_put_failure;
2453 
2454 		if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
2455 		    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
2456 			goto nla_put_failure;
2457 
2458 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
2459 				rdev->wiphy.available_antennas_tx) ||
2460 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
2461 				rdev->wiphy.available_antennas_rx))
2462 			goto nla_put_failure;
2463 
2464 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
2465 		    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
2466 				rdev->wiphy.probe_resp_offload))
2467 			goto nla_put_failure;
2468 
2469 		if ((rdev->wiphy.available_antennas_tx ||
2470 		     rdev->wiphy.available_antennas_rx) &&
2471 		    rdev->ops->get_antenna) {
2472 			u32 tx_ant = 0, rx_ant = 0;
2473 			int res;
2474 
2475 			res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
2476 			if (!res) {
2477 				if (nla_put_u32(msg,
2478 						NL80211_ATTR_WIPHY_ANTENNA_TX,
2479 						tx_ant) ||
2480 				    nla_put_u32(msg,
2481 						NL80211_ATTR_WIPHY_ANTENNA_RX,
2482 						rx_ant))
2483 					goto nla_put_failure;
2484 			}
2485 		}
2486 
2487 		state->split_start++;
2488 		if (state->split)
2489 			break;
2490 		fallthrough;
2491 	case 2:
2492 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
2493 					rdev->wiphy.interface_modes))
2494 				goto nla_put_failure;
2495 		state->split_start++;
2496 		if (state->split)
2497 			break;
2498 		fallthrough;
2499 	case 3:
2500 		nl_bands = nla_nest_start_noflag(msg,
2501 						 NL80211_ATTR_WIPHY_BANDS);
2502 		if (!nl_bands)
2503 			goto nla_put_failure;
2504 
2505 		for (band = state->band_start;
2506 		     band < (state->split ?
2507 				NUM_NL80211_BANDS :
2508 				NL80211_BAND_60GHZ + 1);
2509 		     band++) {
2510 			struct ieee80211_supported_band *sband;
2511 
2512 			/* omit higher bands for ancient software */
2513 			if (band > NL80211_BAND_5GHZ && !state->split)
2514 				break;
2515 
2516 			sband = rdev->wiphy.bands[band];
2517 
2518 			if (!sband)
2519 				continue;
2520 
2521 			nl_band = nla_nest_start_noflag(msg, band);
2522 			if (!nl_band)
2523 				goto nla_put_failure;
2524 
2525 			switch (state->chan_start) {
2526 			case 0:
2527 				if (nl80211_send_band_rateinfo(msg, sband,
2528 							       state->split))
2529 					goto nla_put_failure;
2530 				state->chan_start++;
2531 				if (state->split)
2532 					break;
2533 				fallthrough;
2534 			default:
2535 				/* add frequencies */
2536 				nl_freqs = nla_nest_start_noflag(msg,
2537 								 NL80211_BAND_ATTR_FREQS);
2538 				if (!nl_freqs)
2539 					goto nla_put_failure;
2540 
2541 				for (i = state->chan_start - 1;
2542 				     i < sband->n_channels;
2543 				     i++) {
2544 					nl_freq = nla_nest_start_noflag(msg,
2545 									i);
2546 					if (!nl_freq)
2547 						goto nla_put_failure;
2548 
2549 					chan = &sband->channels[i];
2550 
2551 					if (nl80211_msg_put_channel(
2552 							msg, &rdev->wiphy, chan,
2553 							state->split))
2554 						goto nla_put_failure;
2555 
2556 					nla_nest_end(msg, nl_freq);
2557 					if (state->split)
2558 						break;
2559 				}
2560 				if (i < sband->n_channels)
2561 					state->chan_start = i + 2;
2562 				else
2563 					state->chan_start = 0;
2564 				nla_nest_end(msg, nl_freqs);
2565 			}
2566 
2567 			nla_nest_end(msg, nl_band);
2568 
2569 			if (state->split) {
2570 				/* start again here */
2571 				if (state->chan_start)
2572 					band--;
2573 				break;
2574 			}
2575 		}
2576 		nla_nest_end(msg, nl_bands);
2577 
2578 		if (band < NUM_NL80211_BANDS)
2579 			state->band_start = band + 1;
2580 		else
2581 			state->band_start = 0;
2582 
2583 		/* if bands & channels are done, continue outside */
2584 		if (state->band_start == 0 && state->chan_start == 0)
2585 			state->split_start++;
2586 		if (state->split)
2587 			break;
2588 		fallthrough;
2589 	case 4:
2590 		nl_cmds = nla_nest_start_noflag(msg,
2591 						NL80211_ATTR_SUPPORTED_COMMANDS);
2592 		if (!nl_cmds)
2593 			goto nla_put_failure;
2594 
2595 		i = nl80211_add_commands_unsplit(rdev, msg);
2596 		if (i < 0)
2597 			goto nla_put_failure;
2598 		if (state->split) {
2599 			CMD(crit_proto_start, CRIT_PROTOCOL_START);
2600 			CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2601 			if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2602 				CMD(channel_switch, CHANNEL_SWITCH);
2603 			CMD(set_qos_map, SET_QOS_MAP);
2604 			if (rdev->wiphy.features &
2605 					NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2606 				CMD(add_tx_ts, ADD_TX_TS);
2607 			CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2608 			CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2609 			CMD(update_ft_ies, UPDATE_FT_IES);
2610 			if (rdev->wiphy.sar_capa)
2611 				CMD(set_sar_specs, SET_SAR_SPECS);
2612 		}
2613 #undef CMD
2614 
2615 		nla_nest_end(msg, nl_cmds);
2616 		state->split_start++;
2617 		if (state->split)
2618 			break;
2619 		fallthrough;
2620 	case 5:
2621 		if (rdev->ops->remain_on_channel &&
2622 		    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2623 		    nla_put_u32(msg,
2624 				NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2625 				rdev->wiphy.max_remain_on_channel_duration))
2626 			goto nla_put_failure;
2627 
2628 		if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2629 		    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2630 			goto nla_put_failure;
2631 
2632 		state->split_start++;
2633 		if (state->split)
2634 			break;
2635 		fallthrough;
2636 	case 6:
2637 #ifdef CONFIG_PM
2638 		if (nl80211_send_wowlan(msg, rdev, state->split))
2639 			goto nla_put_failure;
2640 		state->split_start++;
2641 		if (state->split)
2642 			break;
2643 #else
2644 		state->split_start++;
2645 #endif
2646 		fallthrough;
2647 	case 7:
2648 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2649 					rdev->wiphy.software_iftypes))
2650 			goto nla_put_failure;
2651 
2652 		if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2653 						   state->split))
2654 			goto nla_put_failure;
2655 
2656 		state->split_start++;
2657 		if (state->split)
2658 			break;
2659 		fallthrough;
2660 	case 8:
2661 		if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2662 		    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2663 				rdev->wiphy.ap_sme_capa))
2664 			goto nla_put_failure;
2665 
2666 		features = rdev->wiphy.features;
2667 		/*
2668 		 * We can only add the per-channel limit information if the
2669 		 * dump is split, otherwise it makes it too big. Therefore
2670 		 * only advertise it in that case.
2671 		 */
2672 		if (state->split)
2673 			features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2674 		if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2675 			goto nla_put_failure;
2676 
2677 		if (rdev->wiphy.ht_capa_mod_mask &&
2678 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2679 			    sizeof(*rdev->wiphy.ht_capa_mod_mask),
2680 			    rdev->wiphy.ht_capa_mod_mask))
2681 			goto nla_put_failure;
2682 
2683 		if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2684 		    rdev->wiphy.max_acl_mac_addrs &&
2685 		    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2686 				rdev->wiphy.max_acl_mac_addrs))
2687 			goto nla_put_failure;
2688 
2689 		/*
2690 		 * Any information below this point is only available to
2691 		 * applications that can deal with it being split. This
2692 		 * helps ensure that newly added capabilities don't break
2693 		 * older tools by overrunning their buffers.
2694 		 *
2695 		 * We still increment split_start so that in the split
2696 		 * case we'll continue with more data in the next round,
2697 		 * but break unconditionally so unsplit data stops here.
2698 		 */
2699 		if (state->split)
2700 			state->split_start++;
2701 		else
2702 			state->split_start = 0;
2703 		break;
2704 	case 9:
2705 		if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2706 			goto nla_put_failure;
2707 
2708 		if (nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
2709 				rdev->wiphy.max_sched_scan_plans) ||
2710 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
2711 				rdev->wiphy.max_sched_scan_plan_interval) ||
2712 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
2713 				rdev->wiphy.max_sched_scan_plan_iterations))
2714 			goto nla_put_failure;
2715 
2716 		if (rdev->wiphy.extended_capabilities &&
2717 		    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2718 			     rdev->wiphy.extended_capabilities_len,
2719 			     rdev->wiphy.extended_capabilities) ||
2720 		     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2721 			     rdev->wiphy.extended_capabilities_len,
2722 			     rdev->wiphy.extended_capabilities_mask)))
2723 			goto nla_put_failure;
2724 
2725 		if (rdev->wiphy.vht_capa_mod_mask &&
2726 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2727 			    sizeof(*rdev->wiphy.vht_capa_mod_mask),
2728 			    rdev->wiphy.vht_capa_mod_mask))
2729 			goto nla_put_failure;
2730 
2731 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2732 			    rdev->wiphy.perm_addr))
2733 			goto nla_put_failure;
2734 
2735 		if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2736 		    nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2737 			    rdev->wiphy.addr_mask))
2738 			goto nla_put_failure;
2739 
2740 		if (rdev->wiphy.n_addresses > 1) {
2741 			void *attr;
2742 
2743 			attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2744 			if (!attr)
2745 				goto nla_put_failure;
2746 
2747 			for (i = 0; i < rdev->wiphy.n_addresses; i++)
2748 				if (nla_put(msg, i + 1, ETH_ALEN,
2749 					    rdev->wiphy.addresses[i].addr))
2750 					goto nla_put_failure;
2751 
2752 			nla_nest_end(msg, attr);
2753 		}
2754 
2755 		state->split_start++;
2756 		break;
2757 	case 10:
2758 		if (nl80211_send_coalesce(msg, rdev))
2759 			goto nla_put_failure;
2760 
2761 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2762 		    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2763 		     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2764 			goto nla_put_failure;
2765 
2766 		if (rdev->wiphy.max_ap_assoc_sta &&
2767 		    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2768 				rdev->wiphy.max_ap_assoc_sta))
2769 			goto nla_put_failure;
2770 
2771 		state->split_start++;
2772 		break;
2773 	case 11:
2774 		if (rdev->wiphy.n_vendor_commands) {
2775 			const struct nl80211_vendor_cmd_info *info;
2776 			struct nlattr *nested;
2777 
2778 			nested = nla_nest_start_noflag(msg,
2779 						       NL80211_ATTR_VENDOR_DATA);
2780 			if (!nested)
2781 				goto nla_put_failure;
2782 
2783 			for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2784 				info = &rdev->wiphy.vendor_commands[i].info;
2785 				if (nla_put(msg, i + 1, sizeof(*info), info))
2786 					goto nla_put_failure;
2787 			}
2788 			nla_nest_end(msg, nested);
2789 		}
2790 
2791 		if (rdev->wiphy.n_vendor_events) {
2792 			const struct nl80211_vendor_cmd_info *info;
2793 			struct nlattr *nested;
2794 
2795 			nested = nla_nest_start_noflag(msg,
2796 						       NL80211_ATTR_VENDOR_EVENTS);
2797 			if (!nested)
2798 				goto nla_put_failure;
2799 
2800 			for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2801 				info = &rdev->wiphy.vendor_events[i];
2802 				if (nla_put(msg, i + 1, sizeof(*info), info))
2803 					goto nla_put_failure;
2804 			}
2805 			nla_nest_end(msg, nested);
2806 		}
2807 		state->split_start++;
2808 		break;
2809 	case 12:
2810 		if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2811 		    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2812 			       rdev->wiphy.max_num_csa_counters))
2813 			goto nla_put_failure;
2814 
2815 		if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2816 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2817 			goto nla_put_failure;
2818 
2819 		if (rdev->wiphy.max_sched_scan_reqs &&
2820 		    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2821 				rdev->wiphy.max_sched_scan_reqs))
2822 			goto nla_put_failure;
2823 
2824 		if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2825 			    sizeof(rdev->wiphy.ext_features),
2826 			    rdev->wiphy.ext_features))
2827 			goto nla_put_failure;
2828 
2829 		if (rdev->wiphy.bss_select_support) {
2830 			struct nlattr *nested;
2831 			u32 bss_select_support = rdev->wiphy.bss_select_support;
2832 
2833 			nested = nla_nest_start_noflag(msg,
2834 						       NL80211_ATTR_BSS_SELECT);
2835 			if (!nested)
2836 				goto nla_put_failure;
2837 
2838 			i = 0;
2839 			while (bss_select_support) {
2840 				if ((bss_select_support & 1) &&
2841 				    nla_put_flag(msg, i))
2842 					goto nla_put_failure;
2843 				i++;
2844 				bss_select_support >>= 1;
2845 			}
2846 			nla_nest_end(msg, nested);
2847 		}
2848 
2849 		state->split_start++;
2850 		break;
2851 	case 13:
2852 		if (rdev->wiphy.num_iftype_ext_capab &&
2853 		    rdev->wiphy.iftype_ext_capab) {
2854 			struct nlattr *nested_ext_capab, *nested;
2855 
2856 			nested = nla_nest_start_noflag(msg,
2857 						       NL80211_ATTR_IFTYPE_EXT_CAPA);
2858 			if (!nested)
2859 				goto nla_put_failure;
2860 
2861 			for (i = state->capa_start;
2862 			     i < rdev->wiphy.num_iftype_ext_capab; i++) {
2863 				const struct wiphy_iftype_ext_capab *capab;
2864 
2865 				capab = &rdev->wiphy.iftype_ext_capab[i];
2866 
2867 				nested_ext_capab = nla_nest_start_noflag(msg,
2868 									 i);
2869 				if (!nested_ext_capab ||
2870 				    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2871 						capab->iftype) ||
2872 				    nla_put(msg, NL80211_ATTR_EXT_CAPA,
2873 					    capab->extended_capabilities_len,
2874 					    capab->extended_capabilities) ||
2875 				    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2876 					    capab->extended_capabilities_len,
2877 					    capab->extended_capabilities_mask))
2878 					goto nla_put_failure;
2879 
2880 				if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO &&
2881 				    (nla_put_u16(msg,
2882 						 NL80211_ATTR_EML_CAPABILITY,
2883 						 capab->eml_capabilities) ||
2884 				     nla_put_u16(msg,
2885 						 NL80211_ATTR_MLD_CAPA_AND_OPS,
2886 						 capab->mld_capa_and_ops)))
2887 					goto nla_put_failure;
2888 
2889 				nla_nest_end(msg, nested_ext_capab);
2890 				if (state->split)
2891 					break;
2892 			}
2893 			nla_nest_end(msg, nested);
2894 			if (i < rdev->wiphy.num_iftype_ext_capab) {
2895 				state->capa_start = i + 1;
2896 				break;
2897 			}
2898 		}
2899 
2900 		if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2901 				rdev->wiphy.nan_supported_bands))
2902 			goto nla_put_failure;
2903 
2904 		if (wiphy_ext_feature_isset(&rdev->wiphy,
2905 					    NL80211_EXT_FEATURE_TXQS)) {
2906 			struct cfg80211_txq_stats txqstats = {};
2907 			int res;
2908 
2909 			res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2910 			if (!res &&
2911 			    !nl80211_put_txq_stats(msg, &txqstats,
2912 						   NL80211_ATTR_TXQ_STATS))
2913 				goto nla_put_failure;
2914 
2915 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2916 					rdev->wiphy.txq_limit))
2917 				goto nla_put_failure;
2918 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2919 					rdev->wiphy.txq_memory_limit))
2920 				goto nla_put_failure;
2921 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2922 					rdev->wiphy.txq_quantum))
2923 				goto nla_put_failure;
2924 		}
2925 
2926 		state->split_start++;
2927 		break;
2928 	case 14:
2929 		if (nl80211_send_pmsr_capa(rdev, msg))
2930 			goto nla_put_failure;
2931 
2932 		state->split_start++;
2933 		break;
2934 	case 15:
2935 		if (rdev->wiphy.akm_suites &&
2936 		    nla_put(msg, NL80211_ATTR_AKM_SUITES,
2937 			    sizeof(u32) * rdev->wiphy.n_akm_suites,
2938 			    rdev->wiphy.akm_suites))
2939 			goto nla_put_failure;
2940 
2941 		if (nl80211_put_iftype_akm_suites(rdev, msg))
2942 			goto nla_put_failure;
2943 
2944 		if (nl80211_put_tid_config_support(rdev, msg))
2945 			goto nla_put_failure;
2946 		state->split_start++;
2947 		break;
2948 	case 16:
2949 		if (nl80211_put_sar_specs(rdev, msg))
2950 			goto nla_put_failure;
2951 
2952 		if (nl80211_put_mbssid_support(&rdev->wiphy, msg))
2953 			goto nla_put_failure;
2954 
2955 		if (nla_put_u16(msg, NL80211_ATTR_MAX_NUM_AKM_SUITES,
2956 				rdev->wiphy.max_num_akm_suites))
2957 			goto nla_put_failure;
2958 
2959 		if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO)
2960 			nla_put_flag(msg, NL80211_ATTR_MLO_SUPPORT);
2961 
2962 		/* done */
2963 		state->split_start = 0;
2964 		break;
2965 	}
2966  finish:
2967 	genlmsg_end(msg, hdr);
2968 	return 0;
2969 
2970  nla_put_failure:
2971 	genlmsg_cancel(msg, hdr);
2972 	return -EMSGSIZE;
2973 }
2974 
2975 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
2976 				    struct netlink_callback *cb,
2977 				    struct nl80211_dump_wiphy_state *state)
2978 {
2979 	struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
2980 	int ret;
2981 
2982 	if (!tb)
2983 		return -ENOMEM;
2984 
2985 	ret = nlmsg_parse_deprecated(cb->nlh,
2986 				     GENL_HDRLEN + nl80211_fam.hdrsize,
2987 				     tb, nl80211_fam.maxattr,
2988 				     nl80211_policy, NULL);
2989 	/* ignore parse errors for backward compatibility */
2990 	if (ret) {
2991 		ret = 0;
2992 		goto out;
2993 	}
2994 
2995 	state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
2996 	if (tb[NL80211_ATTR_WIPHY])
2997 		state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2998 	if (tb[NL80211_ATTR_WDEV])
2999 		state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
3000 	if (tb[NL80211_ATTR_IFINDEX]) {
3001 		struct net_device *netdev;
3002 		struct cfg80211_registered_device *rdev;
3003 		int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3004 
3005 		netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
3006 		if (!netdev) {
3007 			ret = -ENODEV;
3008 			goto out;
3009 		}
3010 		if (netdev->ieee80211_ptr) {
3011 			rdev = wiphy_to_rdev(
3012 				netdev->ieee80211_ptr->wiphy);
3013 			state->filter_wiphy = rdev->wiphy_idx;
3014 		}
3015 	}
3016 
3017 	ret = 0;
3018 out:
3019 	kfree(tb);
3020 	return ret;
3021 }
3022 
3023 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
3024 {
3025 	int idx = 0, ret;
3026 	struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
3027 	struct cfg80211_registered_device *rdev;
3028 
3029 	rtnl_lock();
3030 	if (!state) {
3031 		state = kzalloc(sizeof(*state), GFP_KERNEL);
3032 		if (!state) {
3033 			rtnl_unlock();
3034 			return -ENOMEM;
3035 		}
3036 		state->filter_wiphy = -1;
3037 		ret = nl80211_dump_wiphy_parse(skb, cb, state);
3038 		if (ret) {
3039 			kfree(state);
3040 			rtnl_unlock();
3041 			return ret;
3042 		}
3043 		cb->args[0] = (long)state;
3044 	}
3045 
3046 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3047 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3048 			continue;
3049 		if (++idx <= state->start)
3050 			continue;
3051 		if (state->filter_wiphy != -1 &&
3052 		    state->filter_wiphy != rdev->wiphy_idx)
3053 			continue;
3054 		/* attempt to fit multiple wiphy data chunks into the skb */
3055 		do {
3056 			ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
3057 						 skb,
3058 						 NETLINK_CB(cb->skb).portid,
3059 						 cb->nlh->nlmsg_seq,
3060 						 NLM_F_MULTI, state);
3061 			if (ret < 0) {
3062 				/*
3063 				 * If sending the wiphy data didn't fit (ENOBUFS
3064 				 * or EMSGSIZE returned), this SKB is still
3065 				 * empty (so it's not too big because another
3066 				 * wiphy dataset is already in the skb) and
3067 				 * we've not tried to adjust the dump allocation
3068 				 * yet ... then adjust the alloc size to be
3069 				 * bigger, and return 1 but with the empty skb.
3070 				 * This results in an empty message being RX'ed
3071 				 * in userspace, but that is ignored.
3072 				 *
3073 				 * We can then retry with the larger buffer.
3074 				 */
3075 				if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
3076 				    !skb->len && !state->split &&
3077 				    cb->min_dump_alloc < 4096) {
3078 					cb->min_dump_alloc = 4096;
3079 					state->split_start = 0;
3080 					rtnl_unlock();
3081 					return 1;
3082 				}
3083 				idx--;
3084 				break;
3085 			}
3086 		} while (state->split_start > 0);
3087 		break;
3088 	}
3089 	rtnl_unlock();
3090 
3091 	state->start = idx;
3092 
3093 	return skb->len;
3094 }
3095 
3096 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
3097 {
3098 	kfree((void *)cb->args[0]);
3099 	return 0;
3100 }
3101 
3102 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
3103 {
3104 	struct sk_buff *msg;
3105 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3106 	struct nl80211_dump_wiphy_state state = {};
3107 
3108 	msg = nlmsg_new(4096, GFP_KERNEL);
3109 	if (!msg)
3110 		return -ENOMEM;
3111 
3112 	if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
3113 			       info->snd_portid, info->snd_seq, 0,
3114 			       &state) < 0) {
3115 		nlmsg_free(msg);
3116 		return -ENOBUFS;
3117 	}
3118 
3119 	return genlmsg_reply(msg, info);
3120 }
3121 
3122 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
3123 	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
3124 	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
3125 	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
3126 	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
3127 	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
3128 };
3129 
3130 static int parse_txq_params(struct nlattr *tb[],
3131 			    struct ieee80211_txq_params *txq_params)
3132 {
3133 	u8 ac;
3134 
3135 	if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
3136 	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
3137 	    !tb[NL80211_TXQ_ATTR_AIFS])
3138 		return -EINVAL;
3139 
3140 	ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
3141 	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
3142 	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
3143 	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
3144 	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
3145 
3146 	if (ac >= NL80211_NUM_ACS)
3147 		return -EINVAL;
3148 	txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
3149 	return 0;
3150 }
3151 
3152 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
3153 {
3154 	/*
3155 	 * You can only set the channel explicitly for some interfaces,
3156 	 * most have their channel managed via their respective
3157 	 * "establish a connection" command (connect, join, ...)
3158 	 *
3159 	 * For AP/GO and mesh mode, the channel can be set with the
3160 	 * channel userspace API, but is only stored and passed to the
3161 	 * low-level driver when the AP starts or the mesh is joined.
3162 	 * This is for backward compatibility, userspace can also give
3163 	 * the channel in the start-ap or join-mesh commands instead.
3164 	 *
3165 	 * Monitors are special as they are normally slaved to
3166 	 * whatever else is going on, so they have their own special
3167 	 * operation to set the monitor channel if possible.
3168 	 */
3169 	return !wdev ||
3170 		wdev->iftype == NL80211_IFTYPE_AP ||
3171 		wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
3172 		wdev->iftype == NL80211_IFTYPE_MONITOR ||
3173 		wdev->iftype == NL80211_IFTYPE_P2P_GO;
3174 }
3175 
3176 int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
3177 			  struct genl_info *info,
3178 			  struct cfg80211_chan_def *chandef)
3179 {
3180 	struct netlink_ext_ack *extack = info->extack;
3181 	struct nlattr **attrs = info->attrs;
3182 	u32 control_freq;
3183 
3184 	if (!attrs[NL80211_ATTR_WIPHY_FREQ])
3185 		return -EINVAL;
3186 
3187 	control_freq = MHZ_TO_KHZ(
3188 			nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3189 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
3190 		control_freq +=
3191 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
3192 
3193 	memset(chandef, 0, sizeof(*chandef));
3194 	chandef->chan = ieee80211_get_channel_khz(&rdev->wiphy, control_freq);
3195 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
3196 	chandef->center_freq1 = KHZ_TO_MHZ(control_freq);
3197 	chandef->freq1_offset = control_freq % 1000;
3198 	chandef->center_freq2 = 0;
3199 
3200 	/* Primary channel not allowed */
3201 	if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED) {
3202 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
3203 				    "Channel is disabled");
3204 		return -EINVAL;
3205 	}
3206 
3207 	if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
3208 		enum nl80211_channel_type chantype;
3209 
3210 		chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
3211 
3212 		switch (chantype) {
3213 		case NL80211_CHAN_NO_HT:
3214 		case NL80211_CHAN_HT20:
3215 		case NL80211_CHAN_HT40PLUS:
3216 		case NL80211_CHAN_HT40MINUS:
3217 			cfg80211_chandef_create(chandef, chandef->chan,
3218 						chantype);
3219 			/* user input for center_freq is incorrect */
3220 			if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
3221 			    chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
3222 				NL_SET_ERR_MSG_ATTR(extack,
3223 						    attrs[NL80211_ATTR_CENTER_FREQ1],
3224 						    "bad center frequency 1");
3225 				return -EINVAL;
3226 			}
3227 			/* center_freq2 must be zero */
3228 			if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
3229 			    nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
3230 				NL_SET_ERR_MSG_ATTR(extack,
3231 						    attrs[NL80211_ATTR_CENTER_FREQ2],
3232 						    "center frequency 2 can't be used");
3233 				return -EINVAL;
3234 			}
3235 			break;
3236 		default:
3237 			NL_SET_ERR_MSG_ATTR(extack,
3238 					    attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3239 					    "invalid channel type");
3240 			return -EINVAL;
3241 		}
3242 	} else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
3243 		chandef->width =
3244 			nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
3245 		if (chandef->chan->band == NL80211_BAND_S1GHZ) {
3246 			/* User input error for channel width doesn't match channel  */
3247 			if (chandef->width != ieee80211_s1g_channel_width(chandef->chan)) {
3248 				NL_SET_ERR_MSG_ATTR(extack,
3249 						    attrs[NL80211_ATTR_CHANNEL_WIDTH],
3250 						    "bad channel width");
3251 				return -EINVAL;
3252 			}
3253 		}
3254 		if (attrs[NL80211_ATTR_CENTER_FREQ1]) {
3255 			chandef->center_freq1 =
3256 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
3257 			if (attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET])
3258 				chandef->freq1_offset = nla_get_u32(
3259 				      attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET]);
3260 			else
3261 				chandef->freq1_offset = 0;
3262 		}
3263 		if (attrs[NL80211_ATTR_CENTER_FREQ2])
3264 			chandef->center_freq2 =
3265 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
3266 	}
3267 
3268 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
3269 		chandef->edmg.channels =
3270 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
3271 
3272 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
3273 			chandef->edmg.bw_config =
3274 		     nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
3275 	} else {
3276 		chandef->edmg.bw_config = 0;
3277 		chandef->edmg.channels = 0;
3278 	}
3279 
3280 	if (!cfg80211_chandef_valid(chandef)) {
3281 		NL_SET_ERR_MSG(extack, "invalid channel definition");
3282 		return -EINVAL;
3283 	}
3284 
3285 	if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
3286 				     IEEE80211_CHAN_DISABLED)) {
3287 		NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
3288 		return -EINVAL;
3289 	}
3290 
3291 	if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
3292 	     chandef->width == NL80211_CHAN_WIDTH_10) &&
3293 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
3294 		NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
3295 		return -EINVAL;
3296 	}
3297 
3298 	return 0;
3299 }
3300 
3301 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
3302 				 struct net_device *dev,
3303 				 struct genl_info *info,
3304 				 int _link_id)
3305 {
3306 	struct cfg80211_chan_def chandef;
3307 	int result;
3308 	enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
3309 	struct wireless_dev *wdev = NULL;
3310 	int link_id = _link_id;
3311 
3312 	if (dev)
3313 		wdev = dev->ieee80211_ptr;
3314 	if (!nl80211_can_set_dev_channel(wdev))
3315 		return -EOPNOTSUPP;
3316 	if (wdev)
3317 		iftype = wdev->iftype;
3318 
3319 	if (link_id < 0) {
3320 		if (wdev && wdev->valid_links)
3321 			return -EINVAL;
3322 		link_id = 0;
3323 	}
3324 
3325 	result = nl80211_parse_chandef(rdev, info, &chandef);
3326 	if (result)
3327 		return result;
3328 
3329 	switch (iftype) {
3330 	case NL80211_IFTYPE_AP:
3331 	case NL80211_IFTYPE_P2P_GO:
3332 		if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
3333 						   iftype))
3334 			return -EINVAL;
3335 		if (wdev->links[link_id].ap.beacon_interval) {
3336 			struct ieee80211_channel *cur_chan;
3337 
3338 			if (!dev || !rdev->ops->set_ap_chanwidth ||
3339 			    !(rdev->wiphy.features &
3340 			      NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE))
3341 				return -EBUSY;
3342 
3343 			/* Only allow dynamic channel width changes */
3344 			cur_chan = wdev->links[link_id].ap.chandef.chan;
3345 			if (chandef.chan != cur_chan)
3346 				return -EBUSY;
3347 
3348 			result = rdev_set_ap_chanwidth(rdev, dev, link_id,
3349 						       &chandef);
3350 			if (result)
3351 				return result;
3352 			wdev->links[link_id].ap.chandef = chandef;
3353 		} else {
3354 			wdev->u.ap.preset_chandef = chandef;
3355 		}
3356 		return 0;
3357 	case NL80211_IFTYPE_MESH_POINT:
3358 		return cfg80211_set_mesh_channel(rdev, wdev, &chandef);
3359 	case NL80211_IFTYPE_MONITOR:
3360 		return cfg80211_set_monitor_channel(rdev, &chandef);
3361 	default:
3362 		break;
3363 	}
3364 
3365 	return -EINVAL;
3366 }
3367 
3368 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
3369 {
3370 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3371 	int link_id = nl80211_link_id_or_invalid(info->attrs);
3372 	struct net_device *netdev = info->user_ptr[1];
3373 	int ret;
3374 
3375 	wdev_lock(netdev->ieee80211_ptr);
3376 	ret = __nl80211_set_channel(rdev, netdev, info, link_id);
3377 	wdev_unlock(netdev->ieee80211_ptr);
3378 
3379 	return ret;
3380 }
3381 
3382 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
3383 {
3384 	struct cfg80211_registered_device *rdev = NULL;
3385 	struct net_device *netdev = NULL;
3386 	struct wireless_dev *wdev;
3387 	int result = 0, rem_txq_params = 0;
3388 	struct nlattr *nl_txq_params;
3389 	u32 changed;
3390 	u8 retry_short = 0, retry_long = 0;
3391 	u32 frag_threshold = 0, rts_threshold = 0;
3392 	u8 coverage_class = 0;
3393 	u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
3394 
3395 	rtnl_lock();
3396 	/*
3397 	 * Try to find the wiphy and netdev. Normally this
3398 	 * function shouldn't need the netdev, but this is
3399 	 * done for backward compatibility -- previously
3400 	 * setting the channel was done per wiphy, but now
3401 	 * it is per netdev. Previous userland like hostapd
3402 	 * also passed a netdev to set_wiphy, so that it is
3403 	 * possible to let that go to the right netdev!
3404 	 */
3405 
3406 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
3407 		int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
3408 
3409 		netdev = __dev_get_by_index(genl_info_net(info), ifindex);
3410 		if (netdev && netdev->ieee80211_ptr)
3411 			rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
3412 		else
3413 			netdev = NULL;
3414 	}
3415 
3416 	if (!netdev) {
3417 		rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
3418 						  info->attrs);
3419 		if (IS_ERR(rdev)) {
3420 			rtnl_unlock();
3421 			return PTR_ERR(rdev);
3422 		}
3423 		wdev = NULL;
3424 		netdev = NULL;
3425 		result = 0;
3426 	} else
3427 		wdev = netdev->ieee80211_ptr;
3428 
3429 	wiphy_lock(&rdev->wiphy);
3430 
3431 	/*
3432 	 * end workaround code, by now the rdev is available
3433 	 * and locked, and wdev may or may not be NULL.
3434 	 */
3435 
3436 	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
3437 		result = cfg80211_dev_rename(
3438 			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
3439 	rtnl_unlock();
3440 
3441 	if (result)
3442 		goto out;
3443 
3444 	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
3445 		struct ieee80211_txq_params txq_params;
3446 		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
3447 
3448 		if (!rdev->ops->set_txq_params) {
3449 			result = -EOPNOTSUPP;
3450 			goto out;
3451 		}
3452 
3453 		if (!netdev) {
3454 			result = -EINVAL;
3455 			goto out;
3456 		}
3457 
3458 		if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3459 		    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3460 			result = -EINVAL;
3461 			goto out;
3462 		}
3463 
3464 		if (!netif_running(netdev)) {
3465 			result = -ENETDOWN;
3466 			goto out;
3467 		}
3468 
3469 		nla_for_each_nested(nl_txq_params,
3470 				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
3471 				    rem_txq_params) {
3472 			result = nla_parse_nested_deprecated(tb,
3473 							     NL80211_TXQ_ATTR_MAX,
3474 							     nl_txq_params,
3475 							     txq_params_policy,
3476 							     info->extack);
3477 			if (result)
3478 				goto out;
3479 			result = parse_txq_params(tb, &txq_params);
3480 			if (result)
3481 				goto out;
3482 
3483 			txq_params.link_id =
3484 				nl80211_link_id_or_invalid(info->attrs);
3485 
3486 			wdev_lock(netdev->ieee80211_ptr);
3487 			if (txq_params.link_id >= 0 &&
3488 			    !(netdev->ieee80211_ptr->valid_links &
3489 			      BIT(txq_params.link_id)))
3490 				result = -ENOLINK;
3491 			else if (txq_params.link_id >= 0 &&
3492 				 !netdev->ieee80211_ptr->valid_links)
3493 				result = -EINVAL;
3494 			else
3495 				result = rdev_set_txq_params(rdev, netdev,
3496 							     &txq_params);
3497 			wdev_unlock(netdev->ieee80211_ptr);
3498 			if (result)
3499 				goto out;
3500 		}
3501 	}
3502 
3503 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3504 		int link_id = nl80211_link_id_or_invalid(info->attrs);
3505 
3506 		if (wdev) {
3507 			wdev_lock(wdev);
3508 			result = __nl80211_set_channel(
3509 				rdev,
3510 				nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
3511 				info, link_id);
3512 			wdev_unlock(wdev);
3513 		} else {
3514 			result = __nl80211_set_channel(rdev, netdev, info, link_id);
3515 		}
3516 
3517 		if (result)
3518 			goto out;
3519 	}
3520 
3521 	if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
3522 		struct wireless_dev *txp_wdev = wdev;
3523 		enum nl80211_tx_power_setting type;
3524 		int idx, mbm = 0;
3525 
3526 		if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
3527 			txp_wdev = NULL;
3528 
3529 		if (!rdev->ops->set_tx_power) {
3530 			result = -EOPNOTSUPP;
3531 			goto out;
3532 		}
3533 
3534 		idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
3535 		type = nla_get_u32(info->attrs[idx]);
3536 
3537 		if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
3538 		    (type != NL80211_TX_POWER_AUTOMATIC)) {
3539 			result = -EINVAL;
3540 			goto out;
3541 		}
3542 
3543 		if (type != NL80211_TX_POWER_AUTOMATIC) {
3544 			idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
3545 			mbm = nla_get_u32(info->attrs[idx]);
3546 		}
3547 
3548 		result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
3549 		if (result)
3550 			goto out;
3551 	}
3552 
3553 	if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
3554 	    info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
3555 		u32 tx_ant, rx_ant;
3556 
3557 		if ((!rdev->wiphy.available_antennas_tx &&
3558 		     !rdev->wiphy.available_antennas_rx) ||
3559 		    !rdev->ops->set_antenna) {
3560 			result = -EOPNOTSUPP;
3561 			goto out;
3562 		}
3563 
3564 		tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
3565 		rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
3566 
3567 		/* reject antenna configurations which don't match the
3568 		 * available antenna masks, except for the "all" mask */
3569 		if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
3570 		    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
3571 			result = -EINVAL;
3572 			goto out;
3573 		}
3574 
3575 		tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
3576 		rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
3577 
3578 		result = rdev_set_antenna(rdev, tx_ant, rx_ant);
3579 		if (result)
3580 			goto out;
3581 	}
3582 
3583 	changed = 0;
3584 
3585 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
3586 		retry_short = nla_get_u8(
3587 			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
3588 
3589 		changed |= WIPHY_PARAM_RETRY_SHORT;
3590 	}
3591 
3592 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
3593 		retry_long = nla_get_u8(
3594 			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
3595 
3596 		changed |= WIPHY_PARAM_RETRY_LONG;
3597 	}
3598 
3599 	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
3600 		frag_threshold = nla_get_u32(
3601 			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
3602 		if (frag_threshold < 256) {
3603 			result = -EINVAL;
3604 			goto out;
3605 		}
3606 
3607 		if (frag_threshold != (u32) -1) {
3608 			/*
3609 			 * Fragments (apart from the last one) are required to
3610 			 * have even length. Make the fragmentation code
3611 			 * simpler by stripping LSB should someone try to use
3612 			 * odd threshold value.
3613 			 */
3614 			frag_threshold &= ~0x1;
3615 		}
3616 		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
3617 	}
3618 
3619 	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
3620 		rts_threshold = nla_get_u32(
3621 			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
3622 		changed |= WIPHY_PARAM_RTS_THRESHOLD;
3623 	}
3624 
3625 	if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
3626 		if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3627 			result = -EINVAL;
3628 			goto out;
3629 		}
3630 
3631 		coverage_class = nla_get_u8(
3632 			info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
3633 		changed |= WIPHY_PARAM_COVERAGE_CLASS;
3634 	}
3635 
3636 	if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3637 		if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION)) {
3638 			result = -EOPNOTSUPP;
3639 			goto out;
3640 		}
3641 
3642 		changed |= WIPHY_PARAM_DYN_ACK;
3643 	}
3644 
3645 	if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3646 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3647 					     NL80211_EXT_FEATURE_TXQS)) {
3648 			result = -EOPNOTSUPP;
3649 			goto out;
3650 		}
3651 		txq_limit = nla_get_u32(
3652 			info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3653 		changed |= WIPHY_PARAM_TXQ_LIMIT;
3654 	}
3655 
3656 	if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3657 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3658 					     NL80211_EXT_FEATURE_TXQS)) {
3659 			result = -EOPNOTSUPP;
3660 			goto out;
3661 		}
3662 		txq_memory_limit = nla_get_u32(
3663 			info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3664 		changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3665 	}
3666 
3667 	if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3668 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3669 					     NL80211_EXT_FEATURE_TXQS)) {
3670 			result = -EOPNOTSUPP;
3671 			goto out;
3672 		}
3673 		txq_quantum = nla_get_u32(
3674 			info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3675 		changed |= WIPHY_PARAM_TXQ_QUANTUM;
3676 	}
3677 
3678 	if (changed) {
3679 		u8 old_retry_short, old_retry_long;
3680 		u32 old_frag_threshold, old_rts_threshold;
3681 		u8 old_coverage_class;
3682 		u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3683 
3684 		if (!rdev->ops->set_wiphy_params) {
3685 			result = -EOPNOTSUPP;
3686 			goto out;
3687 		}
3688 
3689 		old_retry_short = rdev->wiphy.retry_short;
3690 		old_retry_long = rdev->wiphy.retry_long;
3691 		old_frag_threshold = rdev->wiphy.frag_threshold;
3692 		old_rts_threshold = rdev->wiphy.rts_threshold;
3693 		old_coverage_class = rdev->wiphy.coverage_class;
3694 		old_txq_limit = rdev->wiphy.txq_limit;
3695 		old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3696 		old_txq_quantum = rdev->wiphy.txq_quantum;
3697 
3698 		if (changed & WIPHY_PARAM_RETRY_SHORT)
3699 			rdev->wiphy.retry_short = retry_short;
3700 		if (changed & WIPHY_PARAM_RETRY_LONG)
3701 			rdev->wiphy.retry_long = retry_long;
3702 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3703 			rdev->wiphy.frag_threshold = frag_threshold;
3704 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3705 			rdev->wiphy.rts_threshold = rts_threshold;
3706 		if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3707 			rdev->wiphy.coverage_class = coverage_class;
3708 		if (changed & WIPHY_PARAM_TXQ_LIMIT)
3709 			rdev->wiphy.txq_limit = txq_limit;
3710 		if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3711 			rdev->wiphy.txq_memory_limit = txq_memory_limit;
3712 		if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3713 			rdev->wiphy.txq_quantum = txq_quantum;
3714 
3715 		result = rdev_set_wiphy_params(rdev, changed);
3716 		if (result) {
3717 			rdev->wiphy.retry_short = old_retry_short;
3718 			rdev->wiphy.retry_long = old_retry_long;
3719 			rdev->wiphy.frag_threshold = old_frag_threshold;
3720 			rdev->wiphy.rts_threshold = old_rts_threshold;
3721 			rdev->wiphy.coverage_class = old_coverage_class;
3722 			rdev->wiphy.txq_limit = old_txq_limit;
3723 			rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3724 			rdev->wiphy.txq_quantum = old_txq_quantum;
3725 			goto out;
3726 		}
3727 	}
3728 
3729 	result = 0;
3730 
3731 out:
3732 	wiphy_unlock(&rdev->wiphy);
3733 	return result;
3734 }
3735 
3736 static int nl80211_send_chandef(struct sk_buff *msg,
3737 				const struct cfg80211_chan_def *chandef)
3738 {
3739 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3740 		return -EINVAL;
3741 
3742 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3743 			chandef->chan->center_freq))
3744 		return -ENOBUFS;
3745 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
3746 			chandef->chan->freq_offset))
3747 		return -ENOBUFS;
3748 	switch (chandef->width) {
3749 	case NL80211_CHAN_WIDTH_20_NOHT:
3750 	case NL80211_CHAN_WIDTH_20:
3751 	case NL80211_CHAN_WIDTH_40:
3752 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3753 				cfg80211_get_chandef_type(chandef)))
3754 			return -ENOBUFS;
3755 		break;
3756 	default:
3757 		break;
3758 	}
3759 	if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3760 		return -ENOBUFS;
3761 	if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3762 		return -ENOBUFS;
3763 	if (chandef->center_freq2 &&
3764 	    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3765 		return -ENOBUFS;
3766 	return 0;
3767 }
3768 
3769 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3770 			      struct cfg80211_registered_device *rdev,
3771 			      struct wireless_dev *wdev,
3772 			      enum nl80211_commands cmd)
3773 {
3774 	struct net_device *dev = wdev->netdev;
3775 	void *hdr;
3776 
3777 	WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3778 		cmd != NL80211_CMD_DEL_INTERFACE &&
3779 		cmd != NL80211_CMD_SET_INTERFACE);
3780 
3781 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3782 	if (!hdr)
3783 		return -1;
3784 
3785 	if (dev &&
3786 	    (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3787 	     nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3788 		goto nla_put_failure;
3789 
3790 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3791 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3792 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3793 			      NL80211_ATTR_PAD) ||
3794 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3795 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
3796 			rdev->devlist_generation ^
3797 			(cfg80211_rdev_list_generation << 2)) ||
3798 	    nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3799 		goto nla_put_failure;
3800 
3801 	if (rdev->ops->get_channel && !wdev->valid_links) {
3802 		struct cfg80211_chan_def chandef = {};
3803 		int ret;
3804 
3805 		ret = rdev_get_channel(rdev, wdev, 0, &chandef);
3806 		if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3807 			goto nla_put_failure;
3808 	}
3809 
3810 	if (rdev->ops->get_tx_power) {
3811 		int dbm, ret;
3812 
3813 		ret = rdev_get_tx_power(rdev, wdev, &dbm);
3814 		if (ret == 0 &&
3815 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3816 				DBM_TO_MBM(dbm)))
3817 			goto nla_put_failure;
3818 	}
3819 
3820 	wdev_lock(wdev);
3821 	switch (wdev->iftype) {
3822 	case NL80211_IFTYPE_AP:
3823 	case NL80211_IFTYPE_P2P_GO:
3824 		if (wdev->u.ap.ssid_len &&
3825 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.ap.ssid_len,
3826 			    wdev->u.ap.ssid))
3827 			goto nla_put_failure_locked;
3828 		break;
3829 	case NL80211_IFTYPE_STATION:
3830 	case NL80211_IFTYPE_P2P_CLIENT:
3831 		if (wdev->u.client.ssid_len &&
3832 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.client.ssid_len,
3833 			    wdev->u.client.ssid))
3834 			goto nla_put_failure_locked;
3835 		break;
3836 	case NL80211_IFTYPE_ADHOC:
3837 		if (wdev->u.ibss.ssid_len &&
3838 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.ibss.ssid_len,
3839 			    wdev->u.ibss.ssid))
3840 			goto nla_put_failure_locked;
3841 		break;
3842 	default:
3843 		/* nothing */
3844 		break;
3845 	}
3846 	wdev_unlock(wdev);
3847 
3848 	if (rdev->ops->get_txq_stats) {
3849 		struct cfg80211_txq_stats txqstats = {};
3850 		int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3851 
3852 		if (ret == 0 &&
3853 		    !nl80211_put_txq_stats(msg, &txqstats,
3854 					   NL80211_ATTR_TXQ_STATS))
3855 			goto nla_put_failure;
3856 	}
3857 
3858 	if (wdev->valid_links) {
3859 		unsigned int link_id;
3860 		struct nlattr *links = nla_nest_start(msg,
3861 						      NL80211_ATTR_MLO_LINKS);
3862 
3863 		if (!links)
3864 			goto nla_put_failure;
3865 
3866 		for_each_valid_link(wdev, link_id) {
3867 			struct nlattr *link = nla_nest_start(msg, link_id + 1);
3868 			struct cfg80211_chan_def chandef = {};
3869 			int ret;
3870 
3871 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
3872 				goto nla_put_failure;
3873 			if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
3874 				    wdev->links[link_id].addr))
3875 				goto nla_put_failure;
3876 
3877 			ret = rdev_get_channel(rdev, wdev, link_id, &chandef);
3878 			if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3879 				goto nla_put_failure;
3880 
3881 			nla_nest_end(msg, link);
3882 		}
3883 
3884 		nla_nest_end(msg, links);
3885 	}
3886 
3887 	genlmsg_end(msg, hdr);
3888 	return 0;
3889 
3890  nla_put_failure_locked:
3891 	wdev_unlock(wdev);
3892  nla_put_failure:
3893 	genlmsg_cancel(msg, hdr);
3894 	return -EMSGSIZE;
3895 }
3896 
3897 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3898 {
3899 	int wp_idx = 0;
3900 	int if_idx = 0;
3901 	int wp_start = cb->args[0];
3902 	int if_start = cb->args[1];
3903 	int filter_wiphy = -1;
3904 	struct cfg80211_registered_device *rdev;
3905 	struct wireless_dev *wdev;
3906 	int ret;
3907 
3908 	rtnl_lock();
3909 	if (!cb->args[2]) {
3910 		struct nl80211_dump_wiphy_state state = {
3911 			.filter_wiphy = -1,
3912 		};
3913 
3914 		ret = nl80211_dump_wiphy_parse(skb, cb, &state);
3915 		if (ret)
3916 			goto out_unlock;
3917 
3918 		filter_wiphy = state.filter_wiphy;
3919 
3920 		/*
3921 		 * if filtering, set cb->args[2] to +1 since 0 is the default
3922 		 * value needed to determine that parsing is necessary.
3923 		 */
3924 		if (filter_wiphy >= 0)
3925 			cb->args[2] = filter_wiphy + 1;
3926 		else
3927 			cb->args[2] = -1;
3928 	} else if (cb->args[2] > 0) {
3929 		filter_wiphy = cb->args[2] - 1;
3930 	}
3931 
3932 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3933 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3934 			continue;
3935 		if (wp_idx < wp_start) {
3936 			wp_idx++;
3937 			continue;
3938 		}
3939 
3940 		if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
3941 			continue;
3942 
3943 		if_idx = 0;
3944 
3945 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3946 			if (if_idx < if_start) {
3947 				if_idx++;
3948 				continue;
3949 			}
3950 			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
3951 					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
3952 					       rdev, wdev,
3953 					       NL80211_CMD_NEW_INTERFACE) < 0) {
3954 				goto out;
3955 			}
3956 			if_idx++;
3957 		}
3958 
3959 		wp_idx++;
3960 	}
3961  out:
3962 	cb->args[0] = wp_idx;
3963 	cb->args[1] = if_idx;
3964 
3965 	ret = skb->len;
3966  out_unlock:
3967 	rtnl_unlock();
3968 
3969 	return ret;
3970 }
3971 
3972 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
3973 {
3974 	struct sk_buff *msg;
3975 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3976 	struct wireless_dev *wdev = info->user_ptr[1];
3977 
3978 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3979 	if (!msg)
3980 		return -ENOMEM;
3981 
3982 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3983 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3984 		nlmsg_free(msg);
3985 		return -ENOBUFS;
3986 	}
3987 
3988 	return genlmsg_reply(msg, info);
3989 }
3990 
3991 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
3992 	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
3993 	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
3994 	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
3995 	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
3996 	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
3997 	[NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
3998 };
3999 
4000 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
4001 {
4002 	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
4003 	int flag;
4004 
4005 	*mntrflags = 0;
4006 
4007 	if (!nla)
4008 		return -EINVAL;
4009 
4010 	if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
4011 		return -EINVAL;
4012 
4013 	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
4014 		if (flags[flag])
4015 			*mntrflags |= (1<<flag);
4016 
4017 	*mntrflags |= MONITOR_FLAG_CHANGED;
4018 
4019 	return 0;
4020 }
4021 
4022 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
4023 				     enum nl80211_iftype type,
4024 				     struct genl_info *info,
4025 				     struct vif_params *params)
4026 {
4027 	bool change = false;
4028 	int err;
4029 
4030 	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
4031 		if (type != NL80211_IFTYPE_MONITOR)
4032 			return -EINVAL;
4033 
4034 		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
4035 					  &params->flags);
4036 		if (err)
4037 			return err;
4038 
4039 		change = true;
4040 	}
4041 
4042 	if (params->flags & MONITOR_FLAG_ACTIVE &&
4043 	    !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
4044 		return -EOPNOTSUPP;
4045 
4046 	if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
4047 		const u8 *mumimo_groups;
4048 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4049 
4050 		if (type != NL80211_IFTYPE_MONITOR)
4051 			return -EINVAL;
4052 
4053 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4054 			return -EOPNOTSUPP;
4055 
4056 		mumimo_groups =
4057 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
4058 
4059 		/* bits 0 and 63 are reserved and must be zero */
4060 		if ((mumimo_groups[0] & BIT(0)) ||
4061 		    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
4062 			return -EINVAL;
4063 
4064 		params->vht_mumimo_groups = mumimo_groups;
4065 		change = true;
4066 	}
4067 
4068 	if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
4069 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4070 
4071 		if (type != NL80211_IFTYPE_MONITOR)
4072 			return -EINVAL;
4073 
4074 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4075 			return -EOPNOTSUPP;
4076 
4077 		params->vht_mumimo_follow_addr =
4078 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
4079 		change = true;
4080 	}
4081 
4082 	return change ? 1 : 0;
4083 }
4084 
4085 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
4086 			       struct net_device *netdev, u8 use_4addr,
4087 			       enum nl80211_iftype iftype)
4088 {
4089 	if (!use_4addr) {
4090 		if (netdev && netif_is_bridge_port(netdev))
4091 			return -EBUSY;
4092 		return 0;
4093 	}
4094 
4095 	switch (iftype) {
4096 	case NL80211_IFTYPE_AP_VLAN:
4097 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
4098 			return 0;
4099 		break;
4100 	case NL80211_IFTYPE_STATION:
4101 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
4102 			return 0;
4103 		break;
4104 	default:
4105 		break;
4106 	}
4107 
4108 	return -EOPNOTSUPP;
4109 }
4110 
4111 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
4112 {
4113 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4114 	struct vif_params params;
4115 	int err;
4116 	enum nl80211_iftype otype, ntype;
4117 	struct net_device *dev = info->user_ptr[1];
4118 	bool change = false;
4119 
4120 	memset(&params, 0, sizeof(params));
4121 
4122 	otype = ntype = dev->ieee80211_ptr->iftype;
4123 
4124 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
4125 		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4126 		if (otype != ntype)
4127 			change = true;
4128 	}
4129 
4130 	if (info->attrs[NL80211_ATTR_MESH_ID]) {
4131 		struct wireless_dev *wdev = dev->ieee80211_ptr;
4132 
4133 		if (ntype != NL80211_IFTYPE_MESH_POINT)
4134 			return -EINVAL;
4135 		if (netif_running(dev))
4136 			return -EBUSY;
4137 
4138 		wdev_lock(wdev);
4139 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
4140 			     IEEE80211_MAX_MESH_ID_LEN);
4141 		wdev->u.mesh.id_up_len =
4142 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4143 		memcpy(wdev->u.mesh.id,
4144 		       nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4145 		       wdev->u.mesh.id_up_len);
4146 		wdev_unlock(wdev);
4147 	}
4148 
4149 	if (info->attrs[NL80211_ATTR_4ADDR]) {
4150 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4151 		change = true;
4152 		err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
4153 		if (err)
4154 			return err;
4155 	} else {
4156 		params.use_4addr = -1;
4157 	}
4158 
4159 	err = nl80211_parse_mon_options(rdev, ntype, info, &params);
4160 	if (err < 0)
4161 		return err;
4162 	if (err > 0)
4163 		change = true;
4164 
4165 	if (change)
4166 		err = cfg80211_change_iface(rdev, dev, ntype, &params);
4167 	else
4168 		err = 0;
4169 
4170 	if (!err && params.use_4addr != -1)
4171 		dev->ieee80211_ptr->use_4addr = params.use_4addr;
4172 
4173 	if (change && !err) {
4174 		struct wireless_dev *wdev = dev->ieee80211_ptr;
4175 
4176 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
4177 	}
4178 
4179 	return err;
4180 }
4181 
4182 static int _nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4183 {
4184 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4185 	struct vif_params params;
4186 	struct wireless_dev *wdev;
4187 	struct sk_buff *msg;
4188 	int err;
4189 	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
4190 
4191 	memset(&params, 0, sizeof(params));
4192 
4193 	if (!info->attrs[NL80211_ATTR_IFNAME])
4194 		return -EINVAL;
4195 
4196 	if (info->attrs[NL80211_ATTR_IFTYPE])
4197 		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4198 
4199 	if (!rdev->ops->add_virtual_intf)
4200 		return -EOPNOTSUPP;
4201 
4202 	if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
4203 	     rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
4204 	    info->attrs[NL80211_ATTR_MAC]) {
4205 		nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
4206 			   ETH_ALEN);
4207 		if (!is_valid_ether_addr(params.macaddr))
4208 			return -EADDRNOTAVAIL;
4209 	}
4210 
4211 	if (info->attrs[NL80211_ATTR_4ADDR]) {
4212 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4213 		err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
4214 		if (err)
4215 			return err;
4216 	}
4217 
4218 	if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
4219 		return -EOPNOTSUPP;
4220 
4221 	err = nl80211_parse_mon_options(rdev, type, info, &params);
4222 	if (err < 0)
4223 		return err;
4224 
4225 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4226 	if (!msg)
4227 		return -ENOMEM;
4228 
4229 	wdev = rdev_add_virtual_intf(rdev,
4230 				nla_data(info->attrs[NL80211_ATTR_IFNAME]),
4231 				NET_NAME_USER, type, &params);
4232 	if (WARN_ON(!wdev)) {
4233 		nlmsg_free(msg);
4234 		return -EPROTO;
4235 	} else if (IS_ERR(wdev)) {
4236 		nlmsg_free(msg);
4237 		return PTR_ERR(wdev);
4238 	}
4239 
4240 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
4241 		wdev->owner_nlportid = info->snd_portid;
4242 
4243 	switch (type) {
4244 	case NL80211_IFTYPE_MESH_POINT:
4245 		if (!info->attrs[NL80211_ATTR_MESH_ID])
4246 			break;
4247 		wdev_lock(wdev);
4248 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
4249 			     IEEE80211_MAX_MESH_ID_LEN);
4250 		wdev->u.mesh.id_up_len =
4251 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4252 		memcpy(wdev->u.mesh.id,
4253 		       nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4254 		       wdev->u.mesh.id_up_len);
4255 		wdev_unlock(wdev);
4256 		break;
4257 	case NL80211_IFTYPE_NAN:
4258 	case NL80211_IFTYPE_P2P_DEVICE:
4259 		/*
4260 		 * P2P Device and NAN do not have a netdev, so don't go
4261 		 * through the netdev notifier and must be added here
4262 		 */
4263 		cfg80211_init_wdev(wdev);
4264 		cfg80211_register_wdev(rdev, wdev);
4265 		break;
4266 	default:
4267 		break;
4268 	}
4269 
4270 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
4271 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
4272 		nlmsg_free(msg);
4273 		return -ENOBUFS;
4274 	}
4275 
4276 	return genlmsg_reply(msg, info);
4277 }
4278 
4279 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4280 {
4281 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4282 	int ret;
4283 
4284 	/* to avoid failing a new interface creation due to pending removal */
4285 	cfg80211_destroy_ifaces(rdev);
4286 
4287 	wiphy_lock(&rdev->wiphy);
4288 	ret = _nl80211_new_interface(skb, info);
4289 	wiphy_unlock(&rdev->wiphy);
4290 
4291 	return ret;
4292 }
4293 
4294 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
4295 {
4296 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4297 	struct wireless_dev *wdev = info->user_ptr[1];
4298 
4299 	if (!rdev->ops->del_virtual_intf)
4300 		return -EOPNOTSUPP;
4301 
4302 	/*
4303 	 * We hold RTNL, so this is safe, without RTNL opencount cannot
4304 	 * reach 0, and thus the rdev cannot be deleted.
4305 	 *
4306 	 * We need to do it for the dev_close(), since that will call
4307 	 * the netdev notifiers, and we need to acquire the mutex there
4308 	 * but don't know if we get there from here or from some other
4309 	 * place (e.g. "ip link set ... down").
4310 	 */
4311 	mutex_unlock(&rdev->wiphy.mtx);
4312 
4313 	/*
4314 	 * If we remove a wireless device without a netdev then clear
4315 	 * user_ptr[1] so that nl80211_post_doit won't dereference it
4316 	 * to check if it needs to do dev_put(). Otherwise it crashes
4317 	 * since the wdev has been freed, unlike with a netdev where
4318 	 * we need the dev_put() for the netdev to really be freed.
4319 	 */
4320 	if (!wdev->netdev)
4321 		info->user_ptr[1] = NULL;
4322 	else
4323 		dev_close(wdev->netdev);
4324 
4325 	mutex_lock(&rdev->wiphy.mtx);
4326 
4327 	return cfg80211_remove_virtual_intf(rdev, wdev);
4328 }
4329 
4330 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
4331 {
4332 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4333 	struct net_device *dev = info->user_ptr[1];
4334 	u16 noack_map;
4335 
4336 	if (!info->attrs[NL80211_ATTR_NOACK_MAP])
4337 		return -EINVAL;
4338 
4339 	if (!rdev->ops->set_noack_map)
4340 		return -EOPNOTSUPP;
4341 
4342 	noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
4343 
4344 	return rdev_set_noack_map(rdev, dev, noack_map);
4345 }
4346 
4347 static int nl80211_validate_key_link_id(struct genl_info *info,
4348 					struct wireless_dev *wdev,
4349 					int link_id, bool pairwise)
4350 {
4351 	if (pairwise) {
4352 		if (link_id != -1) {
4353 			GENL_SET_ERR_MSG(info,
4354 					 "link ID not allowed for pairwise key");
4355 			return -EINVAL;
4356 		}
4357 
4358 		return 0;
4359 	}
4360 
4361 	if (wdev->valid_links) {
4362 		if (link_id == -1) {
4363 			GENL_SET_ERR_MSG(info,
4364 					 "link ID must for MLO group key");
4365 			return -EINVAL;
4366 		}
4367 		if (!(wdev->valid_links & BIT(link_id))) {
4368 			GENL_SET_ERR_MSG(info, "invalid link ID for MLO group key");
4369 			return -EINVAL;
4370 		}
4371 	} else if (link_id != -1) {
4372 		GENL_SET_ERR_MSG(info, "link ID not allowed for non-MLO group key");
4373 		return -EINVAL;
4374 	}
4375 
4376 	return 0;
4377 }
4378 
4379 struct get_key_cookie {
4380 	struct sk_buff *msg;
4381 	int error;
4382 	int idx;
4383 };
4384 
4385 static void get_key_callback(void *c, struct key_params *params)
4386 {
4387 	struct nlattr *key;
4388 	struct get_key_cookie *cookie = c;
4389 
4390 	if ((params->key &&
4391 	     nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
4392 		     params->key_len, params->key)) ||
4393 	    (params->seq &&
4394 	     nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
4395 		     params->seq_len, params->seq)) ||
4396 	    (params->cipher &&
4397 	     nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
4398 			 params->cipher)))
4399 		goto nla_put_failure;
4400 
4401 	key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
4402 	if (!key)
4403 		goto nla_put_failure;
4404 
4405 	if ((params->key &&
4406 	     nla_put(cookie->msg, NL80211_KEY_DATA,
4407 		     params->key_len, params->key)) ||
4408 	    (params->seq &&
4409 	     nla_put(cookie->msg, NL80211_KEY_SEQ,
4410 		     params->seq_len, params->seq)) ||
4411 	    (params->cipher &&
4412 	     nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
4413 			 params->cipher)))
4414 		goto nla_put_failure;
4415 
4416 	if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
4417 		goto nla_put_failure;
4418 
4419 	nla_nest_end(cookie->msg, key);
4420 
4421 	return;
4422  nla_put_failure:
4423 	cookie->error = 1;
4424 }
4425 
4426 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
4427 {
4428 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4429 	int err;
4430 	struct net_device *dev = info->user_ptr[1];
4431 	u8 key_idx = 0;
4432 	const u8 *mac_addr = NULL;
4433 	bool pairwise;
4434 	struct get_key_cookie cookie = {
4435 		.error = 0,
4436 	};
4437 	void *hdr;
4438 	struct sk_buff *msg;
4439 	bool bigtk_support = false;
4440 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4441 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4442 
4443 	if (wiphy_ext_feature_isset(&rdev->wiphy,
4444 				    NL80211_EXT_FEATURE_BEACON_PROTECTION))
4445 		bigtk_support = true;
4446 
4447 	if ((wdev->iftype == NL80211_IFTYPE_STATION ||
4448 	     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
4449 	    wiphy_ext_feature_isset(&rdev->wiphy,
4450 				    NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
4451 		bigtk_support = true;
4452 
4453 	if (info->attrs[NL80211_ATTR_KEY_IDX]) {
4454 		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
4455 
4456 		if (key_idx >= 6 && key_idx <= 7 && !bigtk_support) {
4457 			GENL_SET_ERR_MSG(info, "BIGTK not supported");
4458 			return -EINVAL;
4459 		}
4460 	}
4461 
4462 	if (info->attrs[NL80211_ATTR_MAC])
4463 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4464 
4465 	pairwise = !!mac_addr;
4466 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
4467 		u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
4468 
4469 		if (kt != NL80211_KEYTYPE_GROUP &&
4470 		    kt != NL80211_KEYTYPE_PAIRWISE)
4471 			return -EINVAL;
4472 		pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
4473 	}
4474 
4475 	if (!rdev->ops->get_key)
4476 		return -EOPNOTSUPP;
4477 
4478 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4479 		return -ENOENT;
4480 
4481 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4482 	if (!msg)
4483 		return -ENOMEM;
4484 
4485 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
4486 			     NL80211_CMD_NEW_KEY);
4487 	if (!hdr)
4488 		goto nla_put_failure;
4489 
4490 	cookie.msg = msg;
4491 	cookie.idx = key_idx;
4492 
4493 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4494 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
4495 		goto nla_put_failure;
4496 	if (mac_addr &&
4497 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
4498 		goto nla_put_failure;
4499 
4500 	err = nl80211_validate_key_link_id(info, wdev, link_id, pairwise);
4501 	if (err)
4502 		goto free_msg;
4503 
4504 	err = rdev_get_key(rdev, dev, link_id, key_idx, pairwise, mac_addr,
4505 			   &cookie, get_key_callback);
4506 
4507 	if (err)
4508 		goto free_msg;
4509 
4510 	if (cookie.error)
4511 		goto nla_put_failure;
4512 
4513 	genlmsg_end(msg, hdr);
4514 	return genlmsg_reply(msg, info);
4515 
4516  nla_put_failure:
4517 	err = -ENOBUFS;
4518  free_msg:
4519 	nlmsg_free(msg);
4520 	return err;
4521 }
4522 
4523 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
4524 {
4525 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4526 	struct key_parse key;
4527 	int err;
4528 	struct net_device *dev = info->user_ptr[1];
4529 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4530 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4531 
4532 	err = nl80211_parse_key(info, &key);
4533 	if (err)
4534 		return err;
4535 
4536 	if (key.idx < 0)
4537 		return -EINVAL;
4538 
4539 	/* Only support setting default key and
4540 	 * Extended Key ID action NL80211_KEY_SET_TX.
4541 	 */
4542 	if (!key.def && !key.defmgmt && !key.defbeacon &&
4543 	    !(key.p.mode == NL80211_KEY_SET_TX))
4544 		return -EINVAL;
4545 
4546 	wdev_lock(wdev);
4547 
4548 	if (key.def) {
4549 		if (!rdev->ops->set_default_key) {
4550 			err = -EOPNOTSUPP;
4551 			goto out;
4552 		}
4553 
4554 		err = nl80211_key_allowed(wdev);
4555 		if (err)
4556 			goto out;
4557 
4558 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4559 		if (err)
4560 			goto out;
4561 
4562 		err = rdev_set_default_key(rdev, dev, link_id, key.idx,
4563 					   key.def_uni, key.def_multi);
4564 
4565 		if (err)
4566 			goto out;
4567 
4568 #ifdef CONFIG_CFG80211_WEXT
4569 		wdev->wext.default_key = key.idx;
4570 #endif
4571 	} else if (key.defmgmt) {
4572 		if (key.def_uni || !key.def_multi) {
4573 			err = -EINVAL;
4574 			goto out;
4575 		}
4576 
4577 		if (!rdev->ops->set_default_mgmt_key) {
4578 			err = -EOPNOTSUPP;
4579 			goto out;
4580 		}
4581 
4582 		err = nl80211_key_allowed(wdev);
4583 		if (err)
4584 			goto out;
4585 
4586 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4587 		if (err)
4588 			goto out;
4589 
4590 		err = rdev_set_default_mgmt_key(rdev, dev, link_id, key.idx);
4591 		if (err)
4592 			goto out;
4593 
4594 #ifdef CONFIG_CFG80211_WEXT
4595 		wdev->wext.default_mgmt_key = key.idx;
4596 #endif
4597 	} else if (key.defbeacon) {
4598 		if (key.def_uni || !key.def_multi) {
4599 			err = -EINVAL;
4600 			goto out;
4601 		}
4602 
4603 		if (!rdev->ops->set_default_beacon_key) {
4604 			err = -EOPNOTSUPP;
4605 			goto out;
4606 		}
4607 
4608 		err = nl80211_key_allowed(wdev);
4609 		if (err)
4610 			goto out;
4611 
4612 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4613 		if (err)
4614 			goto out;
4615 
4616 		err = rdev_set_default_beacon_key(rdev, dev, link_id, key.idx);
4617 		if (err)
4618 			goto out;
4619 	} else if (key.p.mode == NL80211_KEY_SET_TX &&
4620 		   wiphy_ext_feature_isset(&rdev->wiphy,
4621 					   NL80211_EXT_FEATURE_EXT_KEY_ID)) {
4622 		u8 *mac_addr = NULL;
4623 
4624 		if (info->attrs[NL80211_ATTR_MAC])
4625 			mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4626 
4627 		if (!mac_addr || key.idx < 0 || key.idx > 1) {
4628 			err = -EINVAL;
4629 			goto out;
4630 		}
4631 
4632 		err = nl80211_validate_key_link_id(info, wdev, link_id, true);
4633 		if (err)
4634 			goto out;
4635 
4636 		err = rdev_add_key(rdev, dev, link_id, key.idx,
4637 				   NL80211_KEYTYPE_PAIRWISE,
4638 				   mac_addr, &key.p);
4639 	} else {
4640 		err = -EINVAL;
4641 	}
4642  out:
4643 	wdev_unlock(wdev);
4644 
4645 	return err;
4646 }
4647 
4648 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
4649 {
4650 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4651 	int err;
4652 	struct net_device *dev = info->user_ptr[1];
4653 	struct key_parse key;
4654 	const u8 *mac_addr = NULL;
4655 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4656 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4657 
4658 	err = nl80211_parse_key(info, &key);
4659 	if (err)
4660 		return err;
4661 
4662 	if (!key.p.key) {
4663 		GENL_SET_ERR_MSG(info, "no key");
4664 		return -EINVAL;
4665 	}
4666 
4667 	if (info->attrs[NL80211_ATTR_MAC])
4668 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4669 
4670 	if (key.type == -1) {
4671 		if (mac_addr)
4672 			key.type = NL80211_KEYTYPE_PAIRWISE;
4673 		else
4674 			key.type = NL80211_KEYTYPE_GROUP;
4675 	}
4676 
4677 	/* for now */
4678 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4679 	    key.type != NL80211_KEYTYPE_GROUP) {
4680 		GENL_SET_ERR_MSG(info, "key type not pairwise or group");
4681 		return -EINVAL;
4682 	}
4683 
4684 	if (key.type == NL80211_KEYTYPE_GROUP &&
4685 	    info->attrs[NL80211_ATTR_VLAN_ID])
4686 		key.p.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
4687 
4688 	if (!rdev->ops->add_key)
4689 		return -EOPNOTSUPP;
4690 
4691 	if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
4692 					   key.type == NL80211_KEYTYPE_PAIRWISE,
4693 					   mac_addr)) {
4694 		GENL_SET_ERR_MSG(info, "key setting validation failed");
4695 		return -EINVAL;
4696 	}
4697 
4698 	wdev_lock(wdev);
4699 	err = nl80211_key_allowed(wdev);
4700 	if (err)
4701 		GENL_SET_ERR_MSG(info, "key not allowed");
4702 
4703 	if (!err)
4704 		err = nl80211_validate_key_link_id(info, wdev, link_id,
4705 				key.type == NL80211_KEYTYPE_PAIRWISE);
4706 
4707 	if (!err) {
4708 		err = rdev_add_key(rdev, dev, link_id, key.idx,
4709 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4710 				    mac_addr, &key.p);
4711 		if (err)
4712 			GENL_SET_ERR_MSG(info, "key addition failed");
4713 	}
4714 	wdev_unlock(wdev);
4715 
4716 	return err;
4717 }
4718 
4719 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
4720 {
4721 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4722 	int err;
4723 	struct net_device *dev = info->user_ptr[1];
4724 	u8 *mac_addr = NULL;
4725 	struct key_parse key;
4726 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4727 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4728 
4729 	err = nl80211_parse_key(info, &key);
4730 	if (err)
4731 		return err;
4732 
4733 	if (info->attrs[NL80211_ATTR_MAC])
4734 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4735 
4736 	if (key.type == -1) {
4737 		if (mac_addr)
4738 			key.type = NL80211_KEYTYPE_PAIRWISE;
4739 		else
4740 			key.type = NL80211_KEYTYPE_GROUP;
4741 	}
4742 
4743 	/* for now */
4744 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4745 	    key.type != NL80211_KEYTYPE_GROUP)
4746 		return -EINVAL;
4747 
4748 	if (!cfg80211_valid_key_idx(rdev, key.idx,
4749 				    key.type == NL80211_KEYTYPE_PAIRWISE))
4750 		return -EINVAL;
4751 
4752 	if (!rdev->ops->del_key)
4753 		return -EOPNOTSUPP;
4754 
4755 	wdev_lock(wdev);
4756 	err = nl80211_key_allowed(wdev);
4757 
4758 	if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
4759 	    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4760 		err = -ENOENT;
4761 
4762 	if (!err)
4763 		err = nl80211_validate_key_link_id(info, wdev, link_id,
4764 				key.type == NL80211_KEYTYPE_PAIRWISE);
4765 
4766 	if (!err)
4767 		err = rdev_del_key(rdev, dev, link_id, key.idx,
4768 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4769 				   mac_addr);
4770 
4771 #ifdef CONFIG_CFG80211_WEXT
4772 	if (!err) {
4773 		if (key.idx == wdev->wext.default_key)
4774 			wdev->wext.default_key = -1;
4775 		else if (key.idx == wdev->wext.default_mgmt_key)
4776 			wdev->wext.default_mgmt_key = -1;
4777 	}
4778 #endif
4779 	wdev_unlock(wdev);
4780 
4781 	return err;
4782 }
4783 
4784 /* This function returns an error or the number of nested attributes */
4785 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
4786 {
4787 	struct nlattr *attr;
4788 	int n_entries = 0, tmp;
4789 
4790 	nla_for_each_nested(attr, nl_attr, tmp) {
4791 		if (nla_len(attr) != ETH_ALEN)
4792 			return -EINVAL;
4793 
4794 		n_entries++;
4795 	}
4796 
4797 	return n_entries;
4798 }
4799 
4800 /*
4801  * This function parses ACL information and allocates memory for ACL data.
4802  * On successful return, the calling function is responsible to free the
4803  * ACL buffer returned by this function.
4804  */
4805 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
4806 						struct genl_info *info)
4807 {
4808 	enum nl80211_acl_policy acl_policy;
4809 	struct nlattr *attr;
4810 	struct cfg80211_acl_data *acl;
4811 	int i = 0, n_entries, tmp;
4812 
4813 	if (!wiphy->max_acl_mac_addrs)
4814 		return ERR_PTR(-EOPNOTSUPP);
4815 
4816 	if (!info->attrs[NL80211_ATTR_ACL_POLICY])
4817 		return ERR_PTR(-EINVAL);
4818 
4819 	acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
4820 	if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
4821 	    acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
4822 		return ERR_PTR(-EINVAL);
4823 
4824 	if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
4825 		return ERR_PTR(-EINVAL);
4826 
4827 	n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
4828 	if (n_entries < 0)
4829 		return ERR_PTR(n_entries);
4830 
4831 	if (n_entries > wiphy->max_acl_mac_addrs)
4832 		return ERR_PTR(-ENOTSUPP);
4833 
4834 	acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4835 	if (!acl)
4836 		return ERR_PTR(-ENOMEM);
4837 
4838 	nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4839 		memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4840 		i++;
4841 	}
4842 
4843 	acl->n_acl_entries = n_entries;
4844 	acl->acl_policy = acl_policy;
4845 
4846 	return acl;
4847 }
4848 
4849 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4850 {
4851 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4852 	struct net_device *dev = info->user_ptr[1];
4853 	struct cfg80211_acl_data *acl;
4854 	int err;
4855 
4856 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4857 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4858 		return -EOPNOTSUPP;
4859 
4860 	if (!dev->ieee80211_ptr->links[0].ap.beacon_interval)
4861 		return -EINVAL;
4862 
4863 	acl = parse_acl_data(&rdev->wiphy, info);
4864 	if (IS_ERR(acl))
4865 		return PTR_ERR(acl);
4866 
4867 	err = rdev_set_mac_acl(rdev, dev, acl);
4868 
4869 	kfree(acl);
4870 
4871 	return err;
4872 }
4873 
4874 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4875 			   u8 *rates, u8 rates_len)
4876 {
4877 	u8 i;
4878 	u32 mask = 0;
4879 
4880 	for (i = 0; i < rates_len; i++) {
4881 		int rate = (rates[i] & 0x7f) * 5;
4882 		int ridx;
4883 
4884 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4885 			struct ieee80211_rate *srate =
4886 				&sband->bitrates[ridx];
4887 			if (rate == srate->bitrate) {
4888 				mask |= 1 << ridx;
4889 				break;
4890 			}
4891 		}
4892 		if (ridx == sband->n_bitrates)
4893 			return 0; /* rate not found */
4894 	}
4895 
4896 	return mask;
4897 }
4898 
4899 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4900 			       u8 *rates, u8 rates_len,
4901 			       u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4902 {
4903 	u8 i;
4904 
4905 	memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4906 
4907 	for (i = 0; i < rates_len; i++) {
4908 		int ridx, rbit;
4909 
4910 		ridx = rates[i] / 8;
4911 		rbit = BIT(rates[i] % 8);
4912 
4913 		/* check validity */
4914 		if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4915 			return false;
4916 
4917 		/* check availability */
4918 		ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
4919 		if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
4920 			mcs[ridx] |= rbit;
4921 		else
4922 			return false;
4923 	}
4924 
4925 	return true;
4926 }
4927 
4928 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
4929 {
4930 	u16 mcs_mask = 0;
4931 
4932 	switch (vht_mcs_map) {
4933 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
4934 		break;
4935 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
4936 		mcs_mask = 0x00FF;
4937 		break;
4938 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
4939 		mcs_mask = 0x01FF;
4940 		break;
4941 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
4942 		mcs_mask = 0x03FF;
4943 		break;
4944 	default:
4945 		break;
4946 	}
4947 
4948 	return mcs_mask;
4949 }
4950 
4951 static void vht_build_mcs_mask(u16 vht_mcs_map,
4952 			       u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
4953 {
4954 	u8 nss;
4955 
4956 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
4957 		vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
4958 		vht_mcs_map >>= 2;
4959 	}
4960 }
4961 
4962 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
4963 			     struct nl80211_txrate_vht *txrate,
4964 			     u16 mcs[NL80211_VHT_NSS_MAX])
4965 {
4966 	u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4967 	u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
4968 	u8 i;
4969 
4970 	if (!sband->vht_cap.vht_supported)
4971 		return false;
4972 
4973 	memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
4974 
4975 	/* Build vht_mcs_mask from VHT capabilities */
4976 	vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
4977 
4978 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4979 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
4980 			mcs[i] = txrate->mcs[i];
4981 		else
4982 			return false;
4983 	}
4984 
4985 	return true;
4986 }
4987 
4988 static u16 he_mcs_map_to_mcs_mask(u8 he_mcs_map)
4989 {
4990 	switch (he_mcs_map) {
4991 	case IEEE80211_HE_MCS_NOT_SUPPORTED:
4992 		return 0;
4993 	case IEEE80211_HE_MCS_SUPPORT_0_7:
4994 		return 0x00FF;
4995 	case IEEE80211_HE_MCS_SUPPORT_0_9:
4996 		return 0x03FF;
4997 	case IEEE80211_HE_MCS_SUPPORT_0_11:
4998 		return 0xFFF;
4999 	default:
5000 		break;
5001 	}
5002 	return 0;
5003 }
5004 
5005 static void he_build_mcs_mask(u16 he_mcs_map,
5006 			      u16 he_mcs_mask[NL80211_HE_NSS_MAX])
5007 {
5008 	u8 nss;
5009 
5010 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
5011 		he_mcs_mask[nss] = he_mcs_map_to_mcs_mask(he_mcs_map & 0x03);
5012 		he_mcs_map >>= 2;
5013 	}
5014 }
5015 
5016 static u16 he_get_txmcsmap(struct genl_info *info, unsigned int link_id,
5017 			   const struct ieee80211_sta_he_cap *he_cap)
5018 {
5019 	struct net_device *dev = info->user_ptr[1];
5020 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5021 	struct cfg80211_chan_def *chandef;
5022 	__le16 tx_mcs;
5023 
5024 	chandef = wdev_chandef(wdev, link_id);
5025 	if (!chandef) {
5026 		/*
5027 		 * This is probably broken, but we never maintained
5028 		 * a chandef in these cases, so it always was.
5029 		 */
5030 		return le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
5031 	}
5032 
5033 	switch (chandef->width) {
5034 	case NL80211_CHAN_WIDTH_80P80:
5035 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80p80;
5036 		break;
5037 	case NL80211_CHAN_WIDTH_160:
5038 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_160;
5039 		break;
5040 	default:
5041 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80;
5042 		break;
5043 	}
5044 
5045 	return le16_to_cpu(tx_mcs);
5046 }
5047 
5048 static bool he_set_mcs_mask(struct genl_info *info,
5049 			    struct wireless_dev *wdev,
5050 			    struct ieee80211_supported_band *sband,
5051 			    struct nl80211_txrate_he *txrate,
5052 			    u16 mcs[NL80211_HE_NSS_MAX],
5053 			    unsigned int link_id)
5054 {
5055 	const struct ieee80211_sta_he_cap *he_cap;
5056 	u16 tx_mcs_mask[NL80211_HE_NSS_MAX] = {};
5057 	u16 tx_mcs_map = 0;
5058 	u8 i;
5059 
5060 	he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5061 	if (!he_cap)
5062 		return false;
5063 
5064 	memset(mcs, 0, sizeof(u16) * NL80211_HE_NSS_MAX);
5065 
5066 	tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5067 
5068 	/* Build he_mcs_mask from HE capabilities */
5069 	he_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
5070 
5071 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5072 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
5073 			mcs[i] = txrate->mcs[i];
5074 		else
5075 			return false;
5076 	}
5077 
5078 	return true;
5079 }
5080 
5081 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
5082 					 struct nlattr *attrs[],
5083 					 enum nl80211_attrs attr,
5084 					 struct cfg80211_bitrate_mask *mask,
5085 					 struct net_device *dev,
5086 					 bool default_all_enabled,
5087 					 unsigned int link_id)
5088 {
5089 	struct nlattr *tb[NL80211_TXRATE_MAX + 1];
5090 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5091 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5092 	int rem, i;
5093 	struct nlattr *tx_rates;
5094 	struct ieee80211_supported_band *sband;
5095 	u16 vht_tx_mcs_map, he_tx_mcs_map;
5096 
5097 	memset(mask, 0, sizeof(*mask));
5098 	/* Default to all rates enabled */
5099 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
5100 		const struct ieee80211_sta_he_cap *he_cap;
5101 
5102 		if (!default_all_enabled)
5103 			break;
5104 
5105 		sband = rdev->wiphy.bands[i];
5106 
5107 		if (!sband)
5108 			continue;
5109 
5110 		mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
5111 		memcpy(mask->control[i].ht_mcs,
5112 		       sband->ht_cap.mcs.rx_mask,
5113 		       sizeof(mask->control[i].ht_mcs));
5114 
5115 		if (sband->vht_cap.vht_supported) {
5116 			vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5117 			vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
5118 		}
5119 
5120 		he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5121 		if (!he_cap)
5122 			continue;
5123 
5124 		he_tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5125 		he_build_mcs_mask(he_tx_mcs_map, mask->control[i].he_mcs);
5126 
5127 		mask->control[i].he_gi = 0xFF;
5128 		mask->control[i].he_ltf = 0xFF;
5129 	}
5130 
5131 	/* if no rates are given set it back to the defaults */
5132 	if (!attrs[attr])
5133 		goto out;
5134 
5135 	/* The nested attribute uses enum nl80211_band as the index. This maps
5136 	 * directly to the enum nl80211_band values used in cfg80211.
5137 	 */
5138 	BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
5139 	nla_for_each_nested(tx_rates, attrs[attr], rem) {
5140 		enum nl80211_band band = nla_type(tx_rates);
5141 		int err;
5142 
5143 		if (band < 0 || band >= NUM_NL80211_BANDS)
5144 			return -EINVAL;
5145 		sband = rdev->wiphy.bands[band];
5146 		if (sband == NULL)
5147 			return -EINVAL;
5148 		err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
5149 						  tx_rates,
5150 						  nl80211_txattr_policy,
5151 						  info->extack);
5152 		if (err)
5153 			return err;
5154 		if (tb[NL80211_TXRATE_LEGACY]) {
5155 			mask->control[band].legacy = rateset_to_mask(
5156 				sband,
5157 				nla_data(tb[NL80211_TXRATE_LEGACY]),
5158 				nla_len(tb[NL80211_TXRATE_LEGACY]));
5159 			if ((mask->control[band].legacy == 0) &&
5160 			    nla_len(tb[NL80211_TXRATE_LEGACY]))
5161 				return -EINVAL;
5162 		}
5163 		if (tb[NL80211_TXRATE_HT]) {
5164 			if (!ht_rateset_to_mask(
5165 					sband,
5166 					nla_data(tb[NL80211_TXRATE_HT]),
5167 					nla_len(tb[NL80211_TXRATE_HT]),
5168 					mask->control[band].ht_mcs))
5169 				return -EINVAL;
5170 		}
5171 
5172 		if (tb[NL80211_TXRATE_VHT]) {
5173 			if (!vht_set_mcs_mask(
5174 					sband,
5175 					nla_data(tb[NL80211_TXRATE_VHT]),
5176 					mask->control[band].vht_mcs))
5177 				return -EINVAL;
5178 		}
5179 
5180 		if (tb[NL80211_TXRATE_GI]) {
5181 			mask->control[band].gi =
5182 				nla_get_u8(tb[NL80211_TXRATE_GI]);
5183 			if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
5184 				return -EINVAL;
5185 		}
5186 		if (tb[NL80211_TXRATE_HE] &&
5187 		    !he_set_mcs_mask(info, wdev, sband,
5188 				     nla_data(tb[NL80211_TXRATE_HE]),
5189 				     mask->control[band].he_mcs,
5190 				     link_id))
5191 			return -EINVAL;
5192 
5193 		if (tb[NL80211_TXRATE_HE_GI])
5194 			mask->control[band].he_gi =
5195 				nla_get_u8(tb[NL80211_TXRATE_HE_GI]);
5196 		if (tb[NL80211_TXRATE_HE_LTF])
5197 			mask->control[band].he_ltf =
5198 				nla_get_u8(tb[NL80211_TXRATE_HE_LTF]);
5199 
5200 		if (mask->control[band].legacy == 0) {
5201 			/* don't allow empty legacy rates if HT, VHT or HE
5202 			 * are not even supported.
5203 			 */
5204 			if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
5205 			      rdev->wiphy.bands[band]->vht_cap.vht_supported ||
5206 			      ieee80211_get_he_iftype_cap(sband, wdev->iftype)))
5207 				return -EINVAL;
5208 
5209 			for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5210 				if (mask->control[band].ht_mcs[i])
5211 					goto out;
5212 
5213 			for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5214 				if (mask->control[band].vht_mcs[i])
5215 					goto out;
5216 
5217 			for (i = 0; i < NL80211_HE_NSS_MAX; i++)
5218 				if (mask->control[band].he_mcs[i])
5219 					goto out;
5220 
5221 			/* legacy and mcs rates may not be both empty */
5222 			return -EINVAL;
5223 		}
5224 	}
5225 
5226 out:
5227 	return 0;
5228 }
5229 
5230 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
5231 				   enum nl80211_band band,
5232 				   struct cfg80211_bitrate_mask *beacon_rate)
5233 {
5234 	u32 count_ht, count_vht, count_he, i;
5235 	u32 rate = beacon_rate->control[band].legacy;
5236 
5237 	/* Allow only one rate */
5238 	if (hweight32(rate) > 1)
5239 		return -EINVAL;
5240 
5241 	count_ht = 0;
5242 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
5243 		if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
5244 			return -EINVAL;
5245 		} else if (beacon_rate->control[band].ht_mcs[i]) {
5246 			count_ht++;
5247 			if (count_ht > 1)
5248 				return -EINVAL;
5249 		}
5250 		if (count_ht && rate)
5251 			return -EINVAL;
5252 	}
5253 
5254 	count_vht = 0;
5255 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5256 		if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
5257 			return -EINVAL;
5258 		} else if (beacon_rate->control[band].vht_mcs[i]) {
5259 			count_vht++;
5260 			if (count_vht > 1)
5261 				return -EINVAL;
5262 		}
5263 		if (count_vht && rate)
5264 			return -EINVAL;
5265 	}
5266 
5267 	count_he = 0;
5268 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5269 		if (hweight16(beacon_rate->control[band].he_mcs[i]) > 1) {
5270 			return -EINVAL;
5271 		} else if (beacon_rate->control[band].he_mcs[i]) {
5272 			count_he++;
5273 			if (count_he > 1)
5274 				return -EINVAL;
5275 		}
5276 		if (count_he && rate)
5277 			return -EINVAL;
5278 	}
5279 
5280 	if ((count_ht && count_vht && count_he) ||
5281 	    (!rate && !count_ht && !count_vht && !count_he))
5282 		return -EINVAL;
5283 
5284 	if (rate &&
5285 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5286 				     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
5287 		return -EINVAL;
5288 	if (count_ht &&
5289 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5290 				     NL80211_EXT_FEATURE_BEACON_RATE_HT))
5291 		return -EINVAL;
5292 	if (count_vht &&
5293 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5294 				     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
5295 		return -EINVAL;
5296 	if (count_he &&
5297 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5298 				     NL80211_EXT_FEATURE_BEACON_RATE_HE))
5299 		return -EINVAL;
5300 
5301 	return 0;
5302 }
5303 
5304 static int nl80211_parse_mbssid_config(struct wiphy *wiphy,
5305 				       struct net_device *dev,
5306 				       struct nlattr *attrs,
5307 				       struct cfg80211_mbssid_config *config,
5308 				       u8 num_elems)
5309 {
5310 	struct nlattr *tb[NL80211_MBSSID_CONFIG_ATTR_MAX + 1];
5311 
5312 	if (!wiphy->mbssid_max_interfaces)
5313 		return -EOPNOTSUPP;
5314 
5315 	if (nla_parse_nested(tb, NL80211_MBSSID_CONFIG_ATTR_MAX, attrs, NULL,
5316 			     NULL) ||
5317 	    !tb[NL80211_MBSSID_CONFIG_ATTR_INDEX])
5318 		return -EINVAL;
5319 
5320 	config->ema = nla_get_flag(tb[NL80211_MBSSID_CONFIG_ATTR_EMA]);
5321 	if (config->ema) {
5322 		if (!wiphy->ema_max_profile_periodicity)
5323 			return -EOPNOTSUPP;
5324 
5325 		if (num_elems > wiphy->ema_max_profile_periodicity)
5326 			return -EINVAL;
5327 	}
5328 
5329 	config->index = nla_get_u8(tb[NL80211_MBSSID_CONFIG_ATTR_INDEX]);
5330 	if (config->index >= wiphy->mbssid_max_interfaces ||
5331 	    (!config->index && !num_elems))
5332 		return -EINVAL;
5333 
5334 	if (tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]) {
5335 		u32 tx_ifindex =
5336 			nla_get_u32(tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]);
5337 
5338 		if ((!config->index && tx_ifindex != dev->ifindex) ||
5339 		    (config->index && tx_ifindex == dev->ifindex))
5340 			return -EINVAL;
5341 
5342 		if (tx_ifindex != dev->ifindex) {
5343 			struct net_device *tx_netdev =
5344 				dev_get_by_index(wiphy_net(wiphy), tx_ifindex);
5345 
5346 			if (!tx_netdev || !tx_netdev->ieee80211_ptr ||
5347 			    tx_netdev->ieee80211_ptr->wiphy != wiphy ||
5348 			    tx_netdev->ieee80211_ptr->iftype !=
5349 							NL80211_IFTYPE_AP) {
5350 				dev_put(tx_netdev);
5351 				return -EINVAL;
5352 			}
5353 
5354 			config->tx_wdev = tx_netdev->ieee80211_ptr;
5355 		} else {
5356 			config->tx_wdev = dev->ieee80211_ptr;
5357 		}
5358 	} else if (!config->index) {
5359 		config->tx_wdev = dev->ieee80211_ptr;
5360 	} else {
5361 		return -EINVAL;
5362 	}
5363 
5364 	return 0;
5365 }
5366 
5367 static struct cfg80211_mbssid_elems *
5368 nl80211_parse_mbssid_elems(struct wiphy *wiphy, struct nlattr *attrs)
5369 {
5370 	struct nlattr *nl_elems;
5371 	struct cfg80211_mbssid_elems *elems;
5372 	int rem_elems;
5373 	u8 i = 0, num_elems = 0;
5374 
5375 	if (!wiphy->mbssid_max_interfaces)
5376 		return ERR_PTR(-EINVAL);
5377 
5378 	nla_for_each_nested(nl_elems, attrs, rem_elems)
5379 		num_elems++;
5380 
5381 	elems = kzalloc(struct_size(elems, elem, num_elems), GFP_KERNEL);
5382 	if (!elems)
5383 		return ERR_PTR(-ENOMEM);
5384 
5385 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5386 		elems->elem[i].data = nla_data(nl_elems);
5387 		elems->elem[i].len = nla_len(nl_elems);
5388 		i++;
5389 	}
5390 	elems->cnt = num_elems;
5391 	return elems;
5392 }
5393 
5394 static int nl80211_parse_he_bss_color(struct nlattr *attrs,
5395 				      struct cfg80211_he_bss_color *he_bss_color)
5396 {
5397 	struct nlattr *tb[NL80211_HE_BSS_COLOR_ATTR_MAX + 1];
5398 	int err;
5399 
5400 	err = nla_parse_nested(tb, NL80211_HE_BSS_COLOR_ATTR_MAX, attrs,
5401 			       he_bss_color_policy, NULL);
5402 	if (err)
5403 		return err;
5404 
5405 	if (!tb[NL80211_HE_BSS_COLOR_ATTR_COLOR])
5406 		return -EINVAL;
5407 
5408 	he_bss_color->color =
5409 		nla_get_u8(tb[NL80211_HE_BSS_COLOR_ATTR_COLOR]);
5410 	he_bss_color->enabled =
5411 		!nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_DISABLED]);
5412 	he_bss_color->partial =
5413 		nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_PARTIAL]);
5414 
5415 	return 0;
5416 }
5417 
5418 static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
5419 				struct nlattr *attrs[],
5420 				struct cfg80211_beacon_data *bcn)
5421 {
5422 	bool haveinfo = false;
5423 	int err;
5424 
5425 	memset(bcn, 0, sizeof(*bcn));
5426 
5427 	bcn->link_id = nl80211_link_id(attrs);
5428 
5429 	if (attrs[NL80211_ATTR_BEACON_HEAD]) {
5430 		bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
5431 		bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
5432 		if (!bcn->head_len)
5433 			return -EINVAL;
5434 		haveinfo = true;
5435 	}
5436 
5437 	if (attrs[NL80211_ATTR_BEACON_TAIL]) {
5438 		bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
5439 		bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
5440 		haveinfo = true;
5441 	}
5442 
5443 	if (!haveinfo)
5444 		return -EINVAL;
5445 
5446 	if (attrs[NL80211_ATTR_IE]) {
5447 		bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
5448 		bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
5449 	}
5450 
5451 	if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
5452 		bcn->proberesp_ies =
5453 			nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5454 		bcn->proberesp_ies_len =
5455 			nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5456 	}
5457 
5458 	if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
5459 		bcn->assocresp_ies =
5460 			nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5461 		bcn->assocresp_ies_len =
5462 			nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5463 	}
5464 
5465 	if (attrs[NL80211_ATTR_PROBE_RESP]) {
5466 		bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
5467 		bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
5468 	}
5469 
5470 	if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
5471 		struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
5472 
5473 		err = nla_parse_nested_deprecated(tb,
5474 						  NL80211_FTM_RESP_ATTR_MAX,
5475 						  attrs[NL80211_ATTR_FTM_RESPONDER],
5476 						  NULL, NULL);
5477 		if (err)
5478 			return err;
5479 
5480 		if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
5481 		    wiphy_ext_feature_isset(&rdev->wiphy,
5482 					    NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
5483 			bcn->ftm_responder = 1;
5484 		else
5485 			return -EOPNOTSUPP;
5486 
5487 		if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
5488 			bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
5489 			bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
5490 		}
5491 
5492 		if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
5493 			bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5494 			bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5495 		}
5496 	} else {
5497 		bcn->ftm_responder = -1;
5498 	}
5499 
5500 	if (attrs[NL80211_ATTR_HE_BSS_COLOR]) {
5501 		err = nl80211_parse_he_bss_color(attrs[NL80211_ATTR_HE_BSS_COLOR],
5502 						 &bcn->he_bss_color);
5503 		if (err)
5504 			return err;
5505 		bcn->he_bss_color_valid = true;
5506 	}
5507 
5508 	if (attrs[NL80211_ATTR_MBSSID_ELEMS]) {
5509 		struct cfg80211_mbssid_elems *mbssid =
5510 			nl80211_parse_mbssid_elems(&rdev->wiphy,
5511 						   attrs[NL80211_ATTR_MBSSID_ELEMS]);
5512 
5513 		if (IS_ERR(mbssid))
5514 			return PTR_ERR(mbssid);
5515 
5516 		bcn->mbssid_ies = mbssid;
5517 	}
5518 
5519 	return 0;
5520 }
5521 
5522 static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
5523 				    struct ieee80211_he_obss_pd *he_obss_pd)
5524 {
5525 	struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
5526 	int err;
5527 
5528 	err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
5529 			       he_obss_pd_policy, NULL);
5530 	if (err)
5531 		return err;
5532 
5533 	if (!tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL])
5534 		return -EINVAL;
5535 
5536 	he_obss_pd->sr_ctrl = nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL]);
5537 
5538 	if (tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET])
5539 		he_obss_pd->min_offset =
5540 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
5541 	if (tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
5542 		he_obss_pd->max_offset =
5543 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
5544 	if (tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET])
5545 		he_obss_pd->non_srg_max_offset =
5546 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET]);
5547 
5548 	if (he_obss_pd->min_offset > he_obss_pd->max_offset)
5549 		return -EINVAL;
5550 
5551 	if (tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP])
5552 		memcpy(he_obss_pd->bss_color_bitmap,
5553 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP]),
5554 		       sizeof(he_obss_pd->bss_color_bitmap));
5555 
5556 	if (tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP])
5557 		memcpy(he_obss_pd->partial_bssid_bitmap,
5558 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP]),
5559 		       sizeof(he_obss_pd->partial_bssid_bitmap));
5560 
5561 	he_obss_pd->enable = true;
5562 
5563 	return 0;
5564 }
5565 
5566 static int nl80211_parse_fils_discovery(struct cfg80211_registered_device *rdev,
5567 					struct nlattr *attrs,
5568 					struct cfg80211_ap_settings *params)
5569 {
5570 	struct nlattr *tb[NL80211_FILS_DISCOVERY_ATTR_MAX + 1];
5571 	int ret;
5572 	struct cfg80211_fils_discovery *fd = &params->fils_discovery;
5573 
5574 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5575 				     NL80211_EXT_FEATURE_FILS_DISCOVERY))
5576 		return -EINVAL;
5577 
5578 	ret = nla_parse_nested(tb, NL80211_FILS_DISCOVERY_ATTR_MAX, attrs,
5579 			       NULL, NULL);
5580 	if (ret)
5581 		return ret;
5582 
5583 	if (!tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] ||
5584 	    !tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] ||
5585 	    !tb[NL80211_FILS_DISCOVERY_ATTR_TMPL])
5586 		return -EINVAL;
5587 
5588 	fd->tmpl_len = nla_len(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5589 	fd->tmpl = nla_data(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5590 	fd->min_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN]);
5591 	fd->max_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX]);
5592 
5593 	return 0;
5594 }
5595 
5596 static int
5597 nl80211_parse_unsol_bcast_probe_resp(struct cfg80211_registered_device *rdev,
5598 				     struct nlattr *attrs,
5599 				     struct cfg80211_ap_settings *params)
5600 {
5601 	struct nlattr *tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1];
5602 	int ret;
5603 	struct cfg80211_unsol_bcast_probe_resp *presp =
5604 					&params->unsol_bcast_probe_resp;
5605 
5606 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5607 				     NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP))
5608 		return -EINVAL;
5609 
5610 	ret = nla_parse_nested(tb, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX,
5611 			       attrs, NULL, NULL);
5612 	if (ret)
5613 		return ret;
5614 
5615 	if (!tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] ||
5616 	    !tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL])
5617 		return -EINVAL;
5618 
5619 	presp->tmpl = nla_data(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5620 	presp->tmpl_len = nla_len(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5621 	presp->interval = nla_get_u32(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT]);
5622 	return 0;
5623 }
5624 
5625 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
5626 					    const struct element *rates)
5627 {
5628 	int i;
5629 
5630 	if (!rates)
5631 		return;
5632 
5633 	for (i = 0; i < rates->datalen; i++) {
5634 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
5635 			params->ht_required = true;
5636 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
5637 			params->vht_required = true;
5638 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HE_PHY)
5639 			params->he_required = true;
5640 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_SAE_H2E)
5641 			params->sae_h2e_required = true;
5642 	}
5643 }
5644 
5645 /*
5646  * Since the nl80211 API didn't include, from the beginning, attributes about
5647  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
5648  * benefit of drivers that rebuild IEs in the firmware.
5649  */
5650 static int nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
5651 {
5652 	const struct cfg80211_beacon_data *bcn = &params->beacon;
5653 	size_t ies_len = bcn->tail_len;
5654 	const u8 *ies = bcn->tail;
5655 	const struct element *rates;
5656 	const struct element *cap;
5657 
5658 	rates = cfg80211_find_elem(WLAN_EID_SUPP_RATES, ies, ies_len);
5659 	nl80211_check_ap_rate_selectors(params, rates);
5660 
5661 	rates = cfg80211_find_elem(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
5662 	nl80211_check_ap_rate_selectors(params, rates);
5663 
5664 	cap = cfg80211_find_elem(WLAN_EID_HT_CAPABILITY, ies, ies_len);
5665 	if (cap && cap->datalen >= sizeof(*params->ht_cap))
5666 		params->ht_cap = (void *)cap->data;
5667 	cap = cfg80211_find_elem(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
5668 	if (cap && cap->datalen >= sizeof(*params->vht_cap))
5669 		params->vht_cap = (void *)cap->data;
5670 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
5671 	if (cap && cap->datalen >= sizeof(*params->he_cap) + 1)
5672 		params->he_cap = (void *)(cap->data + 1);
5673 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ies, ies_len);
5674 	if (cap && cap->datalen >= sizeof(*params->he_oper) + 1)
5675 		params->he_oper = (void *)(cap->data + 1);
5676 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_CAPABILITY, ies, ies_len);
5677 	if (cap) {
5678 		if (!cap->datalen)
5679 			return -EINVAL;
5680 		params->eht_cap = (void *)(cap->data + 1);
5681 		if (!ieee80211_eht_capa_size_ok((const u8 *)params->he_cap,
5682 						(const u8 *)params->eht_cap,
5683 						cap->datalen - 1, true))
5684 			return -EINVAL;
5685 	}
5686 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_OPERATION, ies, ies_len);
5687 	if (cap) {
5688 		if (!cap->datalen)
5689 			return -EINVAL;
5690 		params->eht_oper = (void *)(cap->data + 1);
5691 		if (!ieee80211_eht_oper_size_ok((const u8 *)params->eht_oper,
5692 						cap->datalen - 1))
5693 			return -EINVAL;
5694 	}
5695 	return 0;
5696 }
5697 
5698 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
5699 				   struct cfg80211_ap_settings *params)
5700 {
5701 	struct wireless_dev *wdev;
5702 
5703 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
5704 		if (wdev->iftype != NL80211_IFTYPE_AP &&
5705 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)
5706 			continue;
5707 
5708 		if (!wdev->u.ap.preset_chandef.chan)
5709 			continue;
5710 
5711 		params->chandef = wdev->u.ap.preset_chandef;
5712 		return true;
5713 	}
5714 
5715 	return false;
5716 }
5717 
5718 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
5719 				    enum nl80211_auth_type auth_type,
5720 				    enum nl80211_commands cmd)
5721 {
5722 	if (auth_type > NL80211_AUTHTYPE_MAX)
5723 		return false;
5724 
5725 	switch (cmd) {
5726 	case NL80211_CMD_AUTHENTICATE:
5727 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5728 		    auth_type == NL80211_AUTHTYPE_SAE)
5729 			return false;
5730 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5731 					     NL80211_EXT_FEATURE_FILS_STA) &&
5732 		    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5733 		     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5734 		     auth_type == NL80211_AUTHTYPE_FILS_PK))
5735 			return false;
5736 		return true;
5737 	case NL80211_CMD_CONNECT:
5738 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5739 		    !wiphy_ext_feature_isset(&rdev->wiphy,
5740 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
5741 		    auth_type == NL80211_AUTHTYPE_SAE)
5742 			return false;
5743 
5744 		/* FILS with SK PFS or PK not supported yet */
5745 		if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5746 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5747 			return false;
5748 		if (!wiphy_ext_feature_isset(
5749 			    &rdev->wiphy,
5750 			    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
5751 		    auth_type == NL80211_AUTHTYPE_FILS_SK)
5752 			return false;
5753 		return true;
5754 	case NL80211_CMD_START_AP:
5755 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5756 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP) &&
5757 		    auth_type == NL80211_AUTHTYPE_SAE)
5758 			return false;
5759 		/* FILS not supported yet */
5760 		if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5761 		    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5762 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5763 			return false;
5764 		return true;
5765 	default:
5766 		return false;
5767 	}
5768 }
5769 
5770 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
5771 {
5772 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5773 	unsigned int link_id = nl80211_link_id(info->attrs);
5774 	struct net_device *dev = info->user_ptr[1];
5775 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5776 	struct cfg80211_ap_settings *params;
5777 	int err;
5778 
5779 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5780 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5781 		return -EOPNOTSUPP;
5782 
5783 	if (!rdev->ops->start_ap)
5784 		return -EOPNOTSUPP;
5785 
5786 	if (wdev->links[link_id].ap.beacon_interval)
5787 		return -EALREADY;
5788 
5789 	/* these are required for START_AP */
5790 	if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
5791 	    !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
5792 	    !info->attrs[NL80211_ATTR_BEACON_HEAD])
5793 		return -EINVAL;
5794 
5795 	params = kzalloc(sizeof(*params), GFP_KERNEL);
5796 	if (!params)
5797 		return -ENOMEM;
5798 
5799 	err = nl80211_parse_beacon(rdev, info->attrs, &params->beacon);
5800 	if (err)
5801 		goto out;
5802 
5803 	params->beacon_interval =
5804 		nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5805 	params->dtim_period =
5806 		nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
5807 
5808 	err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
5809 					   params->beacon_interval);
5810 	if (err)
5811 		goto out;
5812 
5813 	/*
5814 	 * In theory, some of these attributes should be required here
5815 	 * but since they were not used when the command was originally
5816 	 * added, keep them optional for old user space programs to let
5817 	 * them continue to work with drivers that do not need the
5818 	 * additional information -- drivers must check!
5819 	 */
5820 	if (info->attrs[NL80211_ATTR_SSID]) {
5821 		params->ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5822 		params->ssid_len =
5823 			nla_len(info->attrs[NL80211_ATTR_SSID]);
5824 		if (params->ssid_len == 0) {
5825 			err = -EINVAL;
5826 			goto out;
5827 		}
5828 
5829 		if (wdev->u.ap.ssid_len &&
5830 		    (wdev->u.ap.ssid_len != params->ssid_len ||
5831 		     memcmp(wdev->u.ap.ssid, params->ssid, params->ssid_len))) {
5832 			/* require identical SSID for MLO */
5833 			err = -EINVAL;
5834 			goto out;
5835 		}
5836 	} else if (wdev->valid_links) {
5837 		/* require SSID for MLO */
5838 		err = -EINVAL;
5839 		goto out;
5840 	}
5841 
5842 	if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
5843 		params->hidden_ssid = nla_get_u32(
5844 			info->attrs[NL80211_ATTR_HIDDEN_SSID]);
5845 
5846 	params->privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
5847 
5848 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5849 		params->auth_type = nla_get_u32(
5850 			info->attrs[NL80211_ATTR_AUTH_TYPE]);
5851 		if (!nl80211_valid_auth_type(rdev, params->auth_type,
5852 					     NL80211_CMD_START_AP)) {
5853 			err = -EINVAL;
5854 			goto out;
5855 		}
5856 	} else
5857 		params->auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5858 
5859 	err = nl80211_crypto_settings(rdev, info, &params->crypto,
5860 				      NL80211_MAX_NR_CIPHER_SUITES);
5861 	if (err)
5862 		goto out;
5863 
5864 	if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
5865 		if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER)) {
5866 			err = -EOPNOTSUPP;
5867 			goto out;
5868 		}
5869 		params->inactivity_timeout = nla_get_u16(
5870 			info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
5871 	}
5872 
5873 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
5874 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
5875 			err = -EINVAL;
5876 			goto out;
5877 		}
5878 		params->p2p_ctwindow =
5879 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
5880 		if (params->p2p_ctwindow != 0 &&
5881 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN)) {
5882 			err = -EINVAL;
5883 			goto out;
5884 		}
5885 	}
5886 
5887 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
5888 		u8 tmp;
5889 
5890 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
5891 			err = -EINVAL;
5892 			goto out;
5893 		}
5894 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
5895 		params->p2p_opp_ps = tmp;
5896 		if (params->p2p_opp_ps != 0 &&
5897 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS)) {
5898 			err = -EINVAL;
5899 			goto out;
5900 		}
5901 	}
5902 
5903 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5904 		err = nl80211_parse_chandef(rdev, info, &params->chandef);
5905 		if (err)
5906 			goto out;
5907 	} else if (wdev->valid_links) {
5908 		/* with MLD need to specify the channel configuration */
5909 		err = -EINVAL;
5910 		goto out;
5911 	} else if (wdev->u.ap.preset_chandef.chan) {
5912 		params->chandef = wdev->u.ap.preset_chandef;
5913 	} else if (!nl80211_get_ap_channel(rdev, params)) {
5914 		err = -EINVAL;
5915 		goto out;
5916 	}
5917 
5918 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params->chandef,
5919 					   wdev->iftype)) {
5920 		err = -EINVAL;
5921 		goto out;
5922 	}
5923 
5924 	wdev_lock(wdev);
5925 
5926 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
5927 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
5928 						    NL80211_ATTR_TX_RATES,
5929 						    &params->beacon_rate,
5930 						    dev, false, link_id);
5931 		if (err)
5932 			goto out_unlock;
5933 
5934 		err = validate_beacon_tx_rate(rdev, params->chandef.chan->band,
5935 					      &params->beacon_rate);
5936 		if (err)
5937 			goto out_unlock;
5938 	}
5939 
5940 	if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
5941 		params->smps_mode =
5942 			nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
5943 		switch (params->smps_mode) {
5944 		case NL80211_SMPS_OFF:
5945 			break;
5946 		case NL80211_SMPS_STATIC:
5947 			if (!(rdev->wiphy.features &
5948 			      NL80211_FEATURE_STATIC_SMPS)) {
5949 				err = -EINVAL;
5950 				goto out_unlock;
5951 			}
5952 			break;
5953 		case NL80211_SMPS_DYNAMIC:
5954 			if (!(rdev->wiphy.features &
5955 			      NL80211_FEATURE_DYNAMIC_SMPS)) {
5956 				err = -EINVAL;
5957 				goto out_unlock;
5958 			}
5959 			break;
5960 		default:
5961 			err = -EINVAL;
5962 			goto out_unlock;
5963 		}
5964 	} else {
5965 		params->smps_mode = NL80211_SMPS_OFF;
5966 	}
5967 
5968 	params->pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
5969 	if (params->pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
5970 		err = -EOPNOTSUPP;
5971 		goto out_unlock;
5972 	}
5973 
5974 	if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
5975 		params->acl = parse_acl_data(&rdev->wiphy, info);
5976 		if (IS_ERR(params->acl)) {
5977 			err = PTR_ERR(params->acl);
5978 			params->acl = NULL;
5979 			goto out_unlock;
5980 		}
5981 	}
5982 
5983 	params->twt_responder =
5984 		    nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
5985 
5986 	if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
5987 		err = nl80211_parse_he_obss_pd(
5988 					info->attrs[NL80211_ATTR_HE_OBSS_PD],
5989 					&params->he_obss_pd);
5990 		if (err)
5991 			goto out_unlock;
5992 	}
5993 
5994 	if (info->attrs[NL80211_ATTR_FILS_DISCOVERY]) {
5995 		err = nl80211_parse_fils_discovery(rdev,
5996 						   info->attrs[NL80211_ATTR_FILS_DISCOVERY],
5997 						   params);
5998 		if (err)
5999 			goto out_unlock;
6000 	}
6001 
6002 	if (info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP]) {
6003 		err = nl80211_parse_unsol_bcast_probe_resp(
6004 			rdev, info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP],
6005 			params);
6006 		if (err)
6007 			goto out_unlock;
6008 	}
6009 
6010 	if (info->attrs[NL80211_ATTR_MBSSID_CONFIG]) {
6011 		err = nl80211_parse_mbssid_config(&rdev->wiphy, dev,
6012 						  info->attrs[NL80211_ATTR_MBSSID_CONFIG],
6013 						  &params->mbssid_config,
6014 						  params->beacon.mbssid_ies ?
6015 							params->beacon.mbssid_ies->cnt :
6016 							0);
6017 		if (err)
6018 			goto out_unlock;
6019 	}
6020 
6021 	err = nl80211_calculate_ap_params(params);
6022 	if (err)
6023 		goto out_unlock;
6024 
6025 	if (info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS])
6026 		params->flags = nla_get_u32(
6027 			info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS]);
6028 	else if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
6029 		params->flags |= NL80211_AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
6030 
6031 	if (wdev->conn_owner_nlportid &&
6032 	    info->attrs[NL80211_ATTR_SOCKET_OWNER] &&
6033 	    wdev->conn_owner_nlportid != info->snd_portid) {
6034 		err = -EINVAL;
6035 		goto out_unlock;
6036 	}
6037 
6038 	/* FIXME: validate MLO/link-id against driver capabilities */
6039 
6040 	err = rdev_start_ap(rdev, dev, params);
6041 	if (!err) {
6042 		wdev->links[link_id].ap.beacon_interval = params->beacon_interval;
6043 		wdev->links[link_id].ap.chandef = params->chandef;
6044 		wdev->u.ap.ssid_len = params->ssid_len;
6045 		memcpy(wdev->u.ap.ssid, params->ssid,
6046 		       params->ssid_len);
6047 
6048 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
6049 			wdev->conn_owner_nlportid = info->snd_portid;
6050 	}
6051 out_unlock:
6052 	wdev_unlock(wdev);
6053 out:
6054 	kfree(params->acl);
6055 	kfree(params->beacon.mbssid_ies);
6056 	if (params->mbssid_config.tx_wdev &&
6057 	    params->mbssid_config.tx_wdev->netdev &&
6058 	    params->mbssid_config.tx_wdev->netdev != dev)
6059 		dev_put(params->mbssid_config.tx_wdev->netdev);
6060 	kfree(params);
6061 
6062 	return err;
6063 }
6064 
6065 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
6066 {
6067 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6068 	unsigned int link_id = nl80211_link_id(info->attrs);
6069 	struct net_device *dev = info->user_ptr[1];
6070 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6071 	struct cfg80211_beacon_data params;
6072 	int err;
6073 
6074 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6075 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6076 		return -EOPNOTSUPP;
6077 
6078 	if (!rdev->ops->change_beacon)
6079 		return -EOPNOTSUPP;
6080 
6081 	if (!wdev->links[link_id].ap.beacon_interval)
6082 		return -EINVAL;
6083 
6084 	err = nl80211_parse_beacon(rdev, info->attrs, &params);
6085 	if (err)
6086 		goto out;
6087 
6088 	wdev_lock(wdev);
6089 	err = rdev_change_beacon(rdev, dev, &params);
6090 	wdev_unlock(wdev);
6091 
6092 out:
6093 	kfree(params.mbssid_ies);
6094 	return err;
6095 }
6096 
6097 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
6098 {
6099 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6100 	unsigned int link_id = nl80211_link_id(info->attrs);
6101 	struct net_device *dev = info->user_ptr[1];
6102 
6103 	return cfg80211_stop_ap(rdev, dev, link_id, false);
6104 }
6105 
6106 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
6107 	[NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
6108 	[NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
6109 	[NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
6110 	[NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
6111 	[NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
6112 	[NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
6113 };
6114 
6115 static int parse_station_flags(struct genl_info *info,
6116 			       enum nl80211_iftype iftype,
6117 			       struct station_parameters *params)
6118 {
6119 	struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
6120 	struct nlattr *nla;
6121 	int flag;
6122 
6123 	/*
6124 	 * Try parsing the new attribute first so userspace
6125 	 * can specify both for older kernels.
6126 	 */
6127 	nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
6128 	if (nla) {
6129 		struct nl80211_sta_flag_update *sta_flags;
6130 
6131 		sta_flags = nla_data(nla);
6132 		params->sta_flags_mask = sta_flags->mask;
6133 		params->sta_flags_set = sta_flags->set;
6134 		params->sta_flags_set &= params->sta_flags_mask;
6135 		if ((params->sta_flags_mask |
6136 		     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
6137 			return -EINVAL;
6138 		return 0;
6139 	}
6140 
6141 	/* if present, parse the old attribute */
6142 
6143 	nla = info->attrs[NL80211_ATTR_STA_FLAGS];
6144 	if (!nla)
6145 		return 0;
6146 
6147 	if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
6148 		return -EINVAL;
6149 
6150 	/*
6151 	 * Only allow certain flags for interface types so that
6152 	 * other attributes are silently ignored. Remember that
6153 	 * this is backward compatibility code with old userspace
6154 	 * and shouldn't be hit in other cases anyway.
6155 	 */
6156 	switch (iftype) {
6157 	case NL80211_IFTYPE_AP:
6158 	case NL80211_IFTYPE_AP_VLAN:
6159 	case NL80211_IFTYPE_P2P_GO:
6160 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6161 					 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6162 					 BIT(NL80211_STA_FLAG_WME) |
6163 					 BIT(NL80211_STA_FLAG_MFP);
6164 		break;
6165 	case NL80211_IFTYPE_P2P_CLIENT:
6166 	case NL80211_IFTYPE_STATION:
6167 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6168 					 BIT(NL80211_STA_FLAG_TDLS_PEER);
6169 		break;
6170 	case NL80211_IFTYPE_MESH_POINT:
6171 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6172 					 BIT(NL80211_STA_FLAG_MFP) |
6173 					 BIT(NL80211_STA_FLAG_AUTHORIZED);
6174 		break;
6175 	default:
6176 		return -EINVAL;
6177 	}
6178 
6179 	for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
6180 		if (flags[flag]) {
6181 			params->sta_flags_set |= (1<<flag);
6182 
6183 			/* no longer support new API additions in old API */
6184 			if (flag > NL80211_STA_FLAG_MAX_OLD_API)
6185 				return -EINVAL;
6186 		}
6187 	}
6188 
6189 	return 0;
6190 }
6191 
6192 bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
6193 {
6194 	struct nlattr *rate;
6195 	u32 bitrate;
6196 	u16 bitrate_compat;
6197 	enum nl80211_rate_info rate_flg;
6198 
6199 	rate = nla_nest_start_noflag(msg, attr);
6200 	if (!rate)
6201 		return false;
6202 
6203 	/* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
6204 	bitrate = cfg80211_calculate_bitrate(info);
6205 	/* report 16-bit bitrate only if we can */
6206 	bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
6207 	if (bitrate > 0 &&
6208 	    nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
6209 		return false;
6210 	if (bitrate_compat > 0 &&
6211 	    nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
6212 		return false;
6213 
6214 	switch (info->bw) {
6215 	case RATE_INFO_BW_5:
6216 		rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
6217 		break;
6218 	case RATE_INFO_BW_10:
6219 		rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
6220 		break;
6221 	default:
6222 		WARN_ON(1);
6223 		fallthrough;
6224 	case RATE_INFO_BW_20:
6225 		rate_flg = 0;
6226 		break;
6227 	case RATE_INFO_BW_40:
6228 		rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
6229 		break;
6230 	case RATE_INFO_BW_80:
6231 		rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
6232 		break;
6233 	case RATE_INFO_BW_160:
6234 		rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
6235 		break;
6236 	case RATE_INFO_BW_HE_RU:
6237 		rate_flg = 0;
6238 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
6239 		break;
6240 	case RATE_INFO_BW_320:
6241 		rate_flg = NL80211_RATE_INFO_320_MHZ_WIDTH;
6242 		break;
6243 	case RATE_INFO_BW_EHT_RU:
6244 		rate_flg = 0;
6245 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_EHT_MCS));
6246 		break;
6247 	}
6248 
6249 	if (rate_flg && nla_put_flag(msg, rate_flg))
6250 		return false;
6251 
6252 	if (info->flags & RATE_INFO_FLAGS_MCS) {
6253 		if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
6254 			return false;
6255 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6256 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6257 			return false;
6258 	} else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
6259 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
6260 			return false;
6261 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
6262 			return false;
6263 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6264 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6265 			return false;
6266 	} else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
6267 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
6268 			return false;
6269 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
6270 			return false;
6271 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
6272 			return false;
6273 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
6274 			return false;
6275 		if (info->bw == RATE_INFO_BW_HE_RU &&
6276 		    nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
6277 			       info->he_ru_alloc))
6278 			return false;
6279 	} else if (info->flags & RATE_INFO_FLAGS_EHT_MCS) {
6280 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_MCS, info->mcs))
6281 			return false;
6282 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_NSS, info->nss))
6283 			return false;
6284 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_GI, info->eht_gi))
6285 			return false;
6286 		if (info->bw == RATE_INFO_BW_EHT_RU &&
6287 		    nla_put_u8(msg, NL80211_RATE_INFO_EHT_RU_ALLOC,
6288 			       info->eht_ru_alloc))
6289 			return false;
6290 	}
6291 
6292 	nla_nest_end(msg, rate);
6293 	return true;
6294 }
6295 
6296 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
6297 			       int id)
6298 {
6299 	void *attr;
6300 	int i = 0;
6301 
6302 	if (!mask)
6303 		return true;
6304 
6305 	attr = nla_nest_start_noflag(msg, id);
6306 	if (!attr)
6307 		return false;
6308 
6309 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
6310 		if (!(mask & BIT(i)))
6311 			continue;
6312 
6313 		if (nla_put_u8(msg, i, signal[i]))
6314 			return false;
6315 	}
6316 
6317 	nla_nest_end(msg, attr);
6318 
6319 	return true;
6320 }
6321 
6322 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
6323 				u32 seq, int flags,
6324 				struct cfg80211_registered_device *rdev,
6325 				struct net_device *dev,
6326 				const u8 *mac_addr, struct station_info *sinfo)
6327 {
6328 	void *hdr;
6329 	struct nlattr *sinfoattr, *bss_param;
6330 
6331 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
6332 	if (!hdr) {
6333 		cfg80211_sinfo_release_content(sinfo);
6334 		return -1;
6335 	}
6336 
6337 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6338 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
6339 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
6340 		goto nla_put_failure;
6341 
6342 	sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
6343 	if (!sinfoattr)
6344 		goto nla_put_failure;
6345 
6346 #define PUT_SINFO(attr, memb, type) do {				\
6347 	BUILD_BUG_ON(sizeof(type) == sizeof(u64));			\
6348 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
6349 	    nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,		\
6350 			     sinfo->memb))				\
6351 		goto nla_put_failure;					\
6352 	} while (0)
6353 #define PUT_SINFO_U64(attr, memb) do {					\
6354 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
6355 	    nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,		\
6356 			      sinfo->memb, NL80211_STA_INFO_PAD))	\
6357 		goto nla_put_failure;					\
6358 	} while (0)
6359 
6360 	PUT_SINFO(CONNECTED_TIME, connected_time, u32);
6361 	PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
6362 	PUT_SINFO_U64(ASSOC_AT_BOOTTIME, assoc_at);
6363 
6364 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
6365 			     BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
6366 	    nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
6367 			(u32)sinfo->rx_bytes))
6368 		goto nla_put_failure;
6369 
6370 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
6371 			     BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
6372 	    nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
6373 			(u32)sinfo->tx_bytes))
6374 		goto nla_put_failure;
6375 
6376 	PUT_SINFO_U64(RX_BYTES64, rx_bytes);
6377 	PUT_SINFO_U64(TX_BYTES64, tx_bytes);
6378 	PUT_SINFO(LLID, llid, u16);
6379 	PUT_SINFO(PLID, plid, u16);
6380 	PUT_SINFO(PLINK_STATE, plink_state, u8);
6381 	PUT_SINFO_U64(RX_DURATION, rx_duration);
6382 	PUT_SINFO_U64(TX_DURATION, tx_duration);
6383 
6384 	if (wiphy_ext_feature_isset(&rdev->wiphy,
6385 				    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
6386 		PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
6387 
6388 	switch (rdev->wiphy.signal_type) {
6389 	case CFG80211_SIGNAL_TYPE_MBM:
6390 		PUT_SINFO(SIGNAL, signal, u8);
6391 		PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
6392 		break;
6393 	default:
6394 		break;
6395 	}
6396 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
6397 		if (!nl80211_put_signal(msg, sinfo->chains,
6398 					sinfo->chain_signal,
6399 					NL80211_STA_INFO_CHAIN_SIGNAL))
6400 			goto nla_put_failure;
6401 	}
6402 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
6403 		if (!nl80211_put_signal(msg, sinfo->chains,
6404 					sinfo->chain_signal_avg,
6405 					NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
6406 			goto nla_put_failure;
6407 	}
6408 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
6409 		if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
6410 					  NL80211_STA_INFO_TX_BITRATE))
6411 			goto nla_put_failure;
6412 	}
6413 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
6414 		if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
6415 					  NL80211_STA_INFO_RX_BITRATE))
6416 			goto nla_put_failure;
6417 	}
6418 
6419 	PUT_SINFO(RX_PACKETS, rx_packets, u32);
6420 	PUT_SINFO(TX_PACKETS, tx_packets, u32);
6421 	PUT_SINFO(TX_RETRIES, tx_retries, u32);
6422 	PUT_SINFO(TX_FAILED, tx_failed, u32);
6423 	PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
6424 	PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
6425 	PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
6426 	PUT_SINFO(LOCAL_PM, local_pm, u32);
6427 	PUT_SINFO(PEER_PM, peer_pm, u32);
6428 	PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
6429 	PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
6430 	PUT_SINFO(CONNECTED_TO_AS, connected_to_as, u8);
6431 
6432 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
6433 		bss_param = nla_nest_start_noflag(msg,
6434 						  NL80211_STA_INFO_BSS_PARAM);
6435 		if (!bss_param)
6436 			goto nla_put_failure;
6437 
6438 		if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
6439 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
6440 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
6441 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
6442 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
6443 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
6444 		    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
6445 			       sinfo->bss_param.dtim_period) ||
6446 		    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
6447 				sinfo->bss_param.beacon_interval))
6448 			goto nla_put_failure;
6449 
6450 		nla_nest_end(msg, bss_param);
6451 	}
6452 	if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
6453 	    nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
6454 		    sizeof(struct nl80211_sta_flag_update),
6455 		    &sinfo->sta_flags))
6456 		goto nla_put_failure;
6457 
6458 	PUT_SINFO_U64(T_OFFSET, t_offset);
6459 	PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
6460 	PUT_SINFO_U64(BEACON_RX, rx_beacon);
6461 	PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
6462 	PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
6463 	PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
6464 	if (wiphy_ext_feature_isset(&rdev->wiphy,
6465 				    NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
6466 		PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
6467 		PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
6468 	}
6469 
6470 #undef PUT_SINFO
6471 #undef PUT_SINFO_U64
6472 
6473 	if (sinfo->pertid) {
6474 		struct nlattr *tidsattr;
6475 		int tid;
6476 
6477 		tidsattr = nla_nest_start_noflag(msg,
6478 						 NL80211_STA_INFO_TID_STATS);
6479 		if (!tidsattr)
6480 			goto nla_put_failure;
6481 
6482 		for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
6483 			struct cfg80211_tid_stats *tidstats;
6484 			struct nlattr *tidattr;
6485 
6486 			tidstats = &sinfo->pertid[tid];
6487 
6488 			if (!tidstats->filled)
6489 				continue;
6490 
6491 			tidattr = nla_nest_start_noflag(msg, tid + 1);
6492 			if (!tidattr)
6493 				goto nla_put_failure;
6494 
6495 #define PUT_TIDVAL_U64(attr, memb) do {					\
6496 	if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&	\
6497 	    nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,		\
6498 			      tidstats->memb, NL80211_TID_STATS_PAD))	\
6499 		goto nla_put_failure;					\
6500 	} while (0)
6501 
6502 			PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
6503 			PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
6504 			PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
6505 			PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
6506 
6507 #undef PUT_TIDVAL_U64
6508 			if ((tidstats->filled &
6509 			     BIT(NL80211_TID_STATS_TXQ_STATS)) &&
6510 			    !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
6511 						   NL80211_TID_STATS_TXQ_STATS))
6512 				goto nla_put_failure;
6513 
6514 			nla_nest_end(msg, tidattr);
6515 		}
6516 
6517 		nla_nest_end(msg, tidsattr);
6518 	}
6519 
6520 	nla_nest_end(msg, sinfoattr);
6521 
6522 	if (sinfo->assoc_req_ies_len &&
6523 	    nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
6524 		    sinfo->assoc_req_ies))
6525 		goto nla_put_failure;
6526 
6527 	cfg80211_sinfo_release_content(sinfo);
6528 	genlmsg_end(msg, hdr);
6529 	return 0;
6530 
6531  nla_put_failure:
6532 	cfg80211_sinfo_release_content(sinfo);
6533 	genlmsg_cancel(msg, hdr);
6534 	return -EMSGSIZE;
6535 }
6536 
6537 static int nl80211_dump_station(struct sk_buff *skb,
6538 				struct netlink_callback *cb)
6539 {
6540 	struct station_info sinfo;
6541 	struct cfg80211_registered_device *rdev;
6542 	struct wireless_dev *wdev;
6543 	u8 mac_addr[ETH_ALEN];
6544 	int sta_idx = cb->args[2];
6545 	int err;
6546 
6547 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
6548 	if (err)
6549 		return err;
6550 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
6551 	__acquire(&rdev->wiphy.mtx);
6552 
6553 	if (!wdev->netdev) {
6554 		err = -EINVAL;
6555 		goto out_err;
6556 	}
6557 
6558 	if (!rdev->ops->dump_station) {
6559 		err = -EOPNOTSUPP;
6560 		goto out_err;
6561 	}
6562 
6563 	while (1) {
6564 		memset(&sinfo, 0, sizeof(sinfo));
6565 		err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
6566 					mac_addr, &sinfo);
6567 		if (err == -ENOENT)
6568 			break;
6569 		if (err)
6570 			goto out_err;
6571 
6572 		if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
6573 				NETLINK_CB(cb->skb).portid,
6574 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
6575 				rdev, wdev->netdev, mac_addr,
6576 				&sinfo) < 0)
6577 			goto out;
6578 
6579 		sta_idx++;
6580 	}
6581 
6582  out:
6583 	cb->args[2] = sta_idx;
6584 	err = skb->len;
6585  out_err:
6586 	wiphy_unlock(&rdev->wiphy);
6587 
6588 	return err;
6589 }
6590 
6591 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
6592 {
6593 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6594 	struct net_device *dev = info->user_ptr[1];
6595 	struct station_info sinfo;
6596 	struct sk_buff *msg;
6597 	u8 *mac_addr = NULL;
6598 	int err;
6599 
6600 	memset(&sinfo, 0, sizeof(sinfo));
6601 
6602 	if (!info->attrs[NL80211_ATTR_MAC])
6603 		return -EINVAL;
6604 
6605 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6606 
6607 	if (!rdev->ops->get_station)
6608 		return -EOPNOTSUPP;
6609 
6610 	err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
6611 	if (err)
6612 		return err;
6613 
6614 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6615 	if (!msg) {
6616 		cfg80211_sinfo_release_content(&sinfo);
6617 		return -ENOMEM;
6618 	}
6619 
6620 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
6621 				 info->snd_portid, info->snd_seq, 0,
6622 				 rdev, dev, mac_addr, &sinfo) < 0) {
6623 		nlmsg_free(msg);
6624 		return -ENOBUFS;
6625 	}
6626 
6627 	return genlmsg_reply(msg, info);
6628 }
6629 
6630 int cfg80211_check_station_change(struct wiphy *wiphy,
6631 				  struct station_parameters *params,
6632 				  enum cfg80211_station_type statype)
6633 {
6634 	if (params->listen_interval != -1 &&
6635 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6636 		return -EINVAL;
6637 
6638 	if (params->support_p2p_ps != -1 &&
6639 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6640 		return -EINVAL;
6641 
6642 	if (params->aid &&
6643 	    !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
6644 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6645 		return -EINVAL;
6646 
6647 	/* When you run into this, adjust the code below for the new flag */
6648 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
6649 
6650 	switch (statype) {
6651 	case CFG80211_STA_MESH_PEER_KERNEL:
6652 	case CFG80211_STA_MESH_PEER_USER:
6653 		/*
6654 		 * No ignoring the TDLS flag here -- the userspace mesh
6655 		 * code doesn't have the bug of including TDLS in the
6656 		 * mask everywhere.
6657 		 */
6658 		if (params->sta_flags_mask &
6659 				~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6660 				  BIT(NL80211_STA_FLAG_MFP) |
6661 				  BIT(NL80211_STA_FLAG_AUTHORIZED)))
6662 			return -EINVAL;
6663 		break;
6664 	case CFG80211_STA_TDLS_PEER_SETUP:
6665 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6666 		if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6667 			return -EINVAL;
6668 		/* ignore since it can't change */
6669 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6670 		break;
6671 	default:
6672 		/* disallow mesh-specific things */
6673 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
6674 			return -EINVAL;
6675 		if (params->local_pm)
6676 			return -EINVAL;
6677 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6678 			return -EINVAL;
6679 	}
6680 
6681 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6682 	    statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
6683 		/* TDLS can't be set, ... */
6684 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
6685 			return -EINVAL;
6686 		/*
6687 		 * ... but don't bother the driver with it. This works around
6688 		 * a hostapd/wpa_supplicant issue -- it always includes the
6689 		 * TLDS_PEER flag in the mask even for AP mode.
6690 		 */
6691 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6692 	}
6693 
6694 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6695 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6696 		/* reject other things that can't change */
6697 		if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
6698 			return -EINVAL;
6699 		if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
6700 			return -EINVAL;
6701 		if (params->link_sta_params.supported_rates)
6702 			return -EINVAL;
6703 		if (params->ext_capab || params->link_sta_params.ht_capa ||
6704 		    params->link_sta_params.vht_capa ||
6705 		    params->link_sta_params.he_capa ||
6706 		    params->link_sta_params.eht_capa)
6707 			return -EINVAL;
6708 	}
6709 
6710 	if (statype != CFG80211_STA_AP_CLIENT &&
6711 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6712 		if (params->vlan)
6713 			return -EINVAL;
6714 	}
6715 
6716 	switch (statype) {
6717 	case CFG80211_STA_AP_MLME_CLIENT:
6718 		/* Use this only for authorizing/unauthorizing a station */
6719 		if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
6720 			return -EOPNOTSUPP;
6721 		break;
6722 	case CFG80211_STA_AP_CLIENT:
6723 	case CFG80211_STA_AP_CLIENT_UNASSOC:
6724 		/* accept only the listed bits */
6725 		if (params->sta_flags_mask &
6726 				~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6727 				  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6728 				  BIT(NL80211_STA_FLAG_ASSOCIATED) |
6729 				  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6730 				  BIT(NL80211_STA_FLAG_WME) |
6731 				  BIT(NL80211_STA_FLAG_MFP)))
6732 			return -EINVAL;
6733 
6734 		/* but authenticated/associated only if driver handles it */
6735 		if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
6736 		    params->sta_flags_mask &
6737 				(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6738 				 BIT(NL80211_STA_FLAG_ASSOCIATED)))
6739 			return -EINVAL;
6740 		break;
6741 	case CFG80211_STA_IBSS:
6742 	case CFG80211_STA_AP_STA:
6743 		/* reject any changes other than AUTHORIZED */
6744 		if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
6745 			return -EINVAL;
6746 		break;
6747 	case CFG80211_STA_TDLS_PEER_SETUP:
6748 		/* reject any changes other than AUTHORIZED or WME */
6749 		if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6750 					       BIT(NL80211_STA_FLAG_WME)))
6751 			return -EINVAL;
6752 		/* force (at least) rates when authorizing */
6753 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
6754 		    !params->link_sta_params.supported_rates)
6755 			return -EINVAL;
6756 		break;
6757 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6758 		/* reject any changes */
6759 		return -EINVAL;
6760 	case CFG80211_STA_MESH_PEER_KERNEL:
6761 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6762 			return -EINVAL;
6763 		break;
6764 	case CFG80211_STA_MESH_PEER_USER:
6765 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
6766 		    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
6767 			return -EINVAL;
6768 		break;
6769 	}
6770 
6771 	/*
6772 	 * Older kernel versions ignored this attribute entirely, so don't
6773 	 * reject attempts to update it but mark it as unused instead so the
6774 	 * driver won't look at the data.
6775 	 */
6776 	if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
6777 	    statype != CFG80211_STA_TDLS_PEER_SETUP)
6778 		params->link_sta_params.opmode_notif_used = false;
6779 
6780 	return 0;
6781 }
6782 EXPORT_SYMBOL(cfg80211_check_station_change);
6783 
6784 /*
6785  * Get vlan interface making sure it is running and on the right wiphy.
6786  */
6787 static struct net_device *get_vlan(struct genl_info *info,
6788 				   struct cfg80211_registered_device *rdev)
6789 {
6790 	struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
6791 	struct net_device *v;
6792 	int ret;
6793 
6794 	if (!vlanattr)
6795 		return NULL;
6796 
6797 	v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
6798 	if (!v)
6799 		return ERR_PTR(-ENODEV);
6800 
6801 	if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
6802 		ret = -EINVAL;
6803 		goto error;
6804 	}
6805 
6806 	if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
6807 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6808 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
6809 		ret = -EINVAL;
6810 		goto error;
6811 	}
6812 
6813 	if (!netif_running(v)) {
6814 		ret = -ENETDOWN;
6815 		goto error;
6816 	}
6817 
6818 	return v;
6819  error:
6820 	dev_put(v);
6821 	return ERR_PTR(ret);
6822 }
6823 
6824 static int nl80211_parse_sta_wme(struct genl_info *info,
6825 				 struct station_parameters *params)
6826 {
6827 	struct nlattr *tb[NL80211_STA_WME_MAX + 1];
6828 	struct nlattr *nla;
6829 	int err;
6830 
6831 	/* parse WME attributes if present */
6832 	if (!info->attrs[NL80211_ATTR_STA_WME])
6833 		return 0;
6834 
6835 	nla = info->attrs[NL80211_ATTR_STA_WME];
6836 	err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
6837 					  nl80211_sta_wme_policy,
6838 					  info->extack);
6839 	if (err)
6840 		return err;
6841 
6842 	if (tb[NL80211_STA_WME_UAPSD_QUEUES])
6843 		params->uapsd_queues = nla_get_u8(
6844 			tb[NL80211_STA_WME_UAPSD_QUEUES]);
6845 	if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
6846 		return -EINVAL;
6847 
6848 	if (tb[NL80211_STA_WME_MAX_SP])
6849 		params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
6850 
6851 	if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
6852 		return -EINVAL;
6853 
6854 	params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
6855 
6856 	return 0;
6857 }
6858 
6859 static int nl80211_parse_sta_channel_info(struct genl_info *info,
6860 				      struct station_parameters *params)
6861 {
6862 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
6863 		params->supported_channels =
6864 		     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
6865 		params->supported_channels_len =
6866 		     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
6867 		/*
6868 		 * Need to include at least one (first channel, number of
6869 		 * channels) tuple for each subband (checked in policy),
6870 		 * and must have proper tuples for the rest of the data as well.
6871 		 */
6872 		if (params->supported_channels_len % 2)
6873 			return -EINVAL;
6874 	}
6875 
6876 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
6877 		params->supported_oper_classes =
6878 		 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
6879 		params->supported_oper_classes_len =
6880 		  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
6881 	}
6882 	return 0;
6883 }
6884 
6885 static int nl80211_set_station_tdls(struct genl_info *info,
6886 				    struct station_parameters *params)
6887 {
6888 	int err;
6889 	/* Dummy STA entry gets updated once the peer capabilities are known */
6890 	if (info->attrs[NL80211_ATTR_PEER_AID])
6891 		params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
6892 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
6893 		params->link_sta_params.ht_capa =
6894 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
6895 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
6896 		params->link_sta_params.vht_capa =
6897 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
6898 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
6899 		params->link_sta_params.he_capa =
6900 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6901 		params->link_sta_params.he_capa_len =
6902 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6903 
6904 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
6905 			params->link_sta_params.eht_capa =
6906 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
6907 			params->link_sta_params.eht_capa_len =
6908 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
6909 
6910 			if (!ieee80211_eht_capa_size_ok((const u8 *)params->link_sta_params.he_capa,
6911 							(const u8 *)params->link_sta_params.eht_capa,
6912 							params->link_sta_params.eht_capa_len,
6913 							false))
6914 				return -EINVAL;
6915 		}
6916 	}
6917 
6918 	err = nl80211_parse_sta_channel_info(info, params);
6919 	if (err)
6920 		return err;
6921 
6922 	return nl80211_parse_sta_wme(info, params);
6923 }
6924 
6925 static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
6926 					     struct sta_txpwr *txpwr,
6927 					     bool *txpwr_set)
6928 {
6929 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6930 	int idx;
6931 
6932 	if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
6933 		if (!rdev->ops->set_tx_power ||
6934 		    !wiphy_ext_feature_isset(&rdev->wiphy,
6935 					 NL80211_EXT_FEATURE_STA_TX_PWR))
6936 			return -EOPNOTSUPP;
6937 
6938 		idx = NL80211_ATTR_STA_TX_POWER_SETTING;
6939 		txpwr->type = nla_get_u8(info->attrs[idx]);
6940 
6941 		if (txpwr->type == NL80211_TX_POWER_LIMITED) {
6942 			idx = NL80211_ATTR_STA_TX_POWER;
6943 
6944 			if (info->attrs[idx])
6945 				txpwr->power = nla_get_s16(info->attrs[idx]);
6946 			else
6947 				return -EINVAL;
6948 		}
6949 
6950 		*txpwr_set = true;
6951 	} else {
6952 		*txpwr_set = false;
6953 	}
6954 
6955 	return 0;
6956 }
6957 
6958 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
6959 {
6960 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6961 	struct net_device *dev = info->user_ptr[1];
6962 	struct station_parameters params;
6963 	u8 *mac_addr;
6964 	int err;
6965 
6966 	memset(&params, 0, sizeof(params));
6967 
6968 	if (!rdev->ops->change_station)
6969 		return -EOPNOTSUPP;
6970 
6971 	/*
6972 	 * AID and listen_interval properties can be set only for unassociated
6973 	 * station. Include these parameters here and will check them in
6974 	 * cfg80211_check_station_change().
6975 	 */
6976 	if (info->attrs[NL80211_ATTR_STA_AID])
6977 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
6978 
6979 	if (info->attrs[NL80211_ATTR_VLAN_ID])
6980 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
6981 
6982 	if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
6983 		params.listen_interval =
6984 		     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
6985 	else
6986 		params.listen_interval = -1;
6987 
6988 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
6989 		params.support_p2p_ps =
6990 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
6991 	else
6992 		params.support_p2p_ps = -1;
6993 
6994 	if (!info->attrs[NL80211_ATTR_MAC])
6995 		return -EINVAL;
6996 
6997 	params.link_sta_params.link_id =
6998 		nl80211_link_id_or_invalid(info->attrs);
6999 
7000 	if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7001 		/* If MLD_ADDR attribute is set then this is an MLD station
7002 		 * and the MLD_ADDR attribute holds the MLD address and the
7003 		 * MAC attribute holds for the LINK address.
7004 		 * In that case, the link_id is also expected to be valid.
7005 		 */
7006 		if (params.link_sta_params.link_id < 0)
7007 			return -EINVAL;
7008 
7009 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7010 		params.link_sta_params.mld_mac = mac_addr;
7011 		params.link_sta_params.link_mac =
7012 			nla_data(info->attrs[NL80211_ATTR_MAC]);
7013 		if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7014 			return -EINVAL;
7015 	} else {
7016 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7017 	}
7018 
7019 
7020 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
7021 		params.link_sta_params.supported_rates =
7022 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7023 		params.link_sta_params.supported_rates_len =
7024 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7025 	}
7026 
7027 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7028 		params.capability =
7029 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7030 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7031 	}
7032 
7033 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7034 		params.ext_capab =
7035 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7036 		params.ext_capab_len =
7037 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7038 	}
7039 
7040 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7041 		return -EINVAL;
7042 
7043 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7044 		params.plink_action =
7045 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7046 
7047 	if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
7048 		params.plink_state =
7049 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
7050 		if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
7051 			params.peer_aid = nla_get_u16(
7052 				info->attrs[NL80211_ATTR_MESH_PEER_AID]);
7053 		params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
7054 	}
7055 
7056 	if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
7057 		params.local_pm = nla_get_u32(
7058 			info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
7059 
7060 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7061 		params.link_sta_params.opmode_notif_used = true;
7062 		params.link_sta_params.opmode_notif =
7063 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7064 	}
7065 
7066 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7067 		params.link_sta_params.he_6ghz_capa =
7068 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7069 
7070 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7071 		params.airtime_weight =
7072 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7073 
7074 	if (params.airtime_weight &&
7075 	    !wiphy_ext_feature_isset(&rdev->wiphy,
7076 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7077 		return -EOPNOTSUPP;
7078 
7079 	err = nl80211_parse_sta_txpower_setting(info,
7080 						&params.link_sta_params.txpwr,
7081 						&params.link_sta_params.txpwr_set);
7082 	if (err)
7083 		return err;
7084 
7085 	/* Include parameters for TDLS peer (will check later) */
7086 	err = nl80211_set_station_tdls(info, &params);
7087 	if (err)
7088 		return err;
7089 
7090 	params.vlan = get_vlan(info, rdev);
7091 	if (IS_ERR(params.vlan))
7092 		return PTR_ERR(params.vlan);
7093 
7094 	switch (dev->ieee80211_ptr->iftype) {
7095 	case NL80211_IFTYPE_AP:
7096 	case NL80211_IFTYPE_AP_VLAN:
7097 	case NL80211_IFTYPE_P2P_GO:
7098 	case NL80211_IFTYPE_P2P_CLIENT:
7099 	case NL80211_IFTYPE_STATION:
7100 	case NL80211_IFTYPE_ADHOC:
7101 	case NL80211_IFTYPE_MESH_POINT:
7102 		break;
7103 	default:
7104 		err = -EOPNOTSUPP;
7105 		goto out_put_vlan;
7106 	}
7107 
7108 	/* driver will call cfg80211_check_station_change() */
7109 	wdev_lock(dev->ieee80211_ptr);
7110 	err = rdev_change_station(rdev, dev, mac_addr, &params);
7111 	wdev_unlock(dev->ieee80211_ptr);
7112 
7113  out_put_vlan:
7114 	dev_put(params.vlan);
7115 
7116 	return err;
7117 }
7118 
7119 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
7120 {
7121 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7122 	int err;
7123 	struct net_device *dev = info->user_ptr[1];
7124 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7125 	struct station_parameters params;
7126 	u8 *mac_addr = NULL;
7127 	u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
7128 			 BIT(NL80211_STA_FLAG_ASSOCIATED);
7129 
7130 	memset(&params, 0, sizeof(params));
7131 
7132 	if (!rdev->ops->add_station)
7133 		return -EOPNOTSUPP;
7134 
7135 	if (!info->attrs[NL80211_ATTR_MAC])
7136 		return -EINVAL;
7137 
7138 	if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
7139 		return -EINVAL;
7140 
7141 	if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
7142 		return -EINVAL;
7143 
7144 	if (!info->attrs[NL80211_ATTR_STA_AID] &&
7145 	    !info->attrs[NL80211_ATTR_PEER_AID])
7146 		return -EINVAL;
7147 
7148 	params.link_sta_params.link_id =
7149 		nl80211_link_id_or_invalid(info->attrs);
7150 
7151 	if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7152 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7153 		params.link_sta_params.mld_mac = mac_addr;
7154 		params.link_sta_params.link_mac =
7155 			nla_data(info->attrs[NL80211_ATTR_MAC]);
7156 		if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7157 			return -EINVAL;
7158 	} else {
7159 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7160 	}
7161 
7162 	params.link_sta_params.supported_rates =
7163 		nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7164 	params.link_sta_params.supported_rates_len =
7165 		nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7166 	params.listen_interval =
7167 		nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
7168 
7169 	if (info->attrs[NL80211_ATTR_VLAN_ID])
7170 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
7171 
7172 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
7173 		params.support_p2p_ps =
7174 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
7175 	} else {
7176 		/*
7177 		 * if not specified, assume it's supported for P2P GO interface,
7178 		 * and is NOT supported for AP interface
7179 		 */
7180 		params.support_p2p_ps =
7181 			dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
7182 	}
7183 
7184 	if (info->attrs[NL80211_ATTR_PEER_AID])
7185 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
7186 	else
7187 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
7188 
7189 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7190 		params.capability =
7191 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7192 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7193 	}
7194 
7195 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7196 		params.ext_capab =
7197 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7198 		params.ext_capab_len =
7199 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7200 	}
7201 
7202 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
7203 		params.link_sta_params.ht_capa =
7204 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
7205 
7206 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
7207 		params.link_sta_params.vht_capa =
7208 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
7209 
7210 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
7211 		params.link_sta_params.he_capa =
7212 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7213 		params.link_sta_params.he_capa_len =
7214 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7215 
7216 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
7217 			params.link_sta_params.eht_capa =
7218 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7219 			params.link_sta_params.eht_capa_len =
7220 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7221 
7222 			if (!ieee80211_eht_capa_size_ok((const u8 *)params.link_sta_params.he_capa,
7223 							(const u8 *)params.link_sta_params.eht_capa,
7224 							params.link_sta_params.eht_capa_len,
7225 							false))
7226 				return -EINVAL;
7227 		}
7228 	}
7229 
7230 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7231 		params.link_sta_params.he_6ghz_capa =
7232 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7233 
7234 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7235 		params.link_sta_params.opmode_notif_used = true;
7236 		params.link_sta_params.opmode_notif =
7237 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7238 	}
7239 
7240 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7241 		params.plink_action =
7242 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7243 
7244 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7245 		params.airtime_weight =
7246 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7247 
7248 	if (params.airtime_weight &&
7249 	    !wiphy_ext_feature_isset(&rdev->wiphy,
7250 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7251 		return -EOPNOTSUPP;
7252 
7253 	err = nl80211_parse_sta_txpower_setting(info,
7254 						&params.link_sta_params.txpwr,
7255 						&params.link_sta_params.txpwr_set);
7256 	if (err)
7257 		return err;
7258 
7259 	err = nl80211_parse_sta_channel_info(info, &params);
7260 	if (err)
7261 		return err;
7262 
7263 	err = nl80211_parse_sta_wme(info, &params);
7264 	if (err)
7265 		return err;
7266 
7267 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7268 		return -EINVAL;
7269 
7270 	/* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
7271 	 * as userspace might just pass through the capabilities from the IEs
7272 	 * directly, rather than enforcing this restriction and returning an
7273 	 * error in this case.
7274 	 */
7275 	if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
7276 		params.link_sta_params.ht_capa = NULL;
7277 		params.link_sta_params.vht_capa = NULL;
7278 
7279 		/* HE and EHT require WME */
7280 		if (params.link_sta_params.he_capa_len ||
7281 		    params.link_sta_params.he_6ghz_capa ||
7282 		    params.link_sta_params.eht_capa_len)
7283 			return -EINVAL;
7284 	}
7285 
7286 	/* Ensure that HT/VHT capabilities are not set for 6 GHz HE STA */
7287 	if (params.link_sta_params.he_6ghz_capa &&
7288 	    (params.link_sta_params.ht_capa || params.link_sta_params.vht_capa))
7289 		return -EINVAL;
7290 
7291 	/* When you run into this, adjust the code below for the new flag */
7292 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
7293 
7294 	switch (dev->ieee80211_ptr->iftype) {
7295 	case NL80211_IFTYPE_AP:
7296 	case NL80211_IFTYPE_AP_VLAN:
7297 	case NL80211_IFTYPE_P2P_GO:
7298 		/* ignore WME attributes if iface/sta is not capable */
7299 		if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
7300 		    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
7301 			params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7302 
7303 		/* TDLS peers cannot be added */
7304 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7305 		    info->attrs[NL80211_ATTR_PEER_AID])
7306 			return -EINVAL;
7307 		/* but don't bother the driver with it */
7308 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
7309 
7310 		/* allow authenticated/associated only if driver handles it */
7311 		if (!(rdev->wiphy.features &
7312 				NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
7313 		    params.sta_flags_mask & auth_assoc)
7314 			return -EINVAL;
7315 
7316 		/* Older userspace, or userspace wanting to be compatible with
7317 		 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
7318 		 * and assoc flags in the mask, but assumes the station will be
7319 		 * added as associated anyway since this was the required driver
7320 		 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
7321 		 * introduced.
7322 		 * In order to not bother drivers with this quirk in the API
7323 		 * set the flags in both the mask and set for new stations in
7324 		 * this case.
7325 		 */
7326 		if (!(params.sta_flags_mask & auth_assoc)) {
7327 			params.sta_flags_mask |= auth_assoc;
7328 			params.sta_flags_set |= auth_assoc;
7329 		}
7330 
7331 		/* must be last in here for error handling */
7332 		params.vlan = get_vlan(info, rdev);
7333 		if (IS_ERR(params.vlan))
7334 			return PTR_ERR(params.vlan);
7335 		break;
7336 	case NL80211_IFTYPE_MESH_POINT:
7337 		/* ignore uAPSD data */
7338 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7339 
7340 		/* associated is disallowed */
7341 		if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
7342 			return -EINVAL;
7343 		/* TDLS peers cannot be added */
7344 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7345 		    info->attrs[NL80211_ATTR_PEER_AID])
7346 			return -EINVAL;
7347 		break;
7348 	case NL80211_IFTYPE_STATION:
7349 	case NL80211_IFTYPE_P2P_CLIENT:
7350 		/* ignore uAPSD data */
7351 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7352 
7353 		/* these are disallowed */
7354 		if (params.sta_flags_mask &
7355 				(BIT(NL80211_STA_FLAG_ASSOCIATED) |
7356 				 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
7357 			return -EINVAL;
7358 		/* Only TDLS peers can be added */
7359 		if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
7360 			return -EINVAL;
7361 		/* Can only add if TDLS ... */
7362 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
7363 			return -EOPNOTSUPP;
7364 		/* ... with external setup is supported */
7365 		if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
7366 			return -EOPNOTSUPP;
7367 		/*
7368 		 * Older wpa_supplicant versions always mark the TDLS peer
7369 		 * as authorized, but it shouldn't yet be.
7370 		 */
7371 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
7372 		break;
7373 	default:
7374 		return -EOPNOTSUPP;
7375 	}
7376 
7377 	/* be aware of params.vlan when changing code here */
7378 
7379 	wdev_lock(dev->ieee80211_ptr);
7380 	if (wdev->valid_links) {
7381 		if (params.link_sta_params.link_id < 0) {
7382 			err = -EINVAL;
7383 			goto out;
7384 		}
7385 		if (!(wdev->valid_links & BIT(params.link_sta_params.link_id))) {
7386 			err = -ENOLINK;
7387 			goto out;
7388 		}
7389 	} else {
7390 		if (params.link_sta_params.link_id >= 0) {
7391 			err = -EINVAL;
7392 			goto out;
7393 		}
7394 	}
7395 	err = rdev_add_station(rdev, dev, mac_addr, &params);
7396 out:
7397 	wdev_unlock(dev->ieee80211_ptr);
7398 	dev_put(params.vlan);
7399 	return err;
7400 }
7401 
7402 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
7403 {
7404 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7405 	struct net_device *dev = info->user_ptr[1];
7406 	struct station_del_parameters params;
7407 	int ret;
7408 
7409 	memset(&params, 0, sizeof(params));
7410 
7411 	if (info->attrs[NL80211_ATTR_MAC])
7412 		params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
7413 
7414 	switch (dev->ieee80211_ptr->iftype) {
7415 	case NL80211_IFTYPE_AP:
7416 	case NL80211_IFTYPE_AP_VLAN:
7417 	case NL80211_IFTYPE_MESH_POINT:
7418 	case NL80211_IFTYPE_P2P_GO:
7419 		/* always accept these */
7420 		break;
7421 	case NL80211_IFTYPE_ADHOC:
7422 		/* conditionally accept */
7423 		if (wiphy_ext_feature_isset(&rdev->wiphy,
7424 					    NL80211_EXT_FEATURE_DEL_IBSS_STA))
7425 			break;
7426 		return -EINVAL;
7427 	default:
7428 		return -EINVAL;
7429 	}
7430 
7431 	if (!rdev->ops->del_station)
7432 		return -EOPNOTSUPP;
7433 
7434 	if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
7435 		params.subtype =
7436 			nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
7437 		if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
7438 		    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
7439 			return -EINVAL;
7440 	} else {
7441 		/* Default to Deauthentication frame */
7442 		params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
7443 	}
7444 
7445 	if (info->attrs[NL80211_ATTR_REASON_CODE]) {
7446 		params.reason_code =
7447 			nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7448 		if (params.reason_code == 0)
7449 			return -EINVAL; /* 0 is reserved */
7450 	} else {
7451 		/* Default to reason code 2 */
7452 		params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
7453 	}
7454 
7455 	wdev_lock(dev->ieee80211_ptr);
7456 	ret = rdev_del_station(rdev, dev, &params);
7457 	wdev_unlock(dev->ieee80211_ptr);
7458 
7459 	return ret;
7460 }
7461 
7462 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
7463 				int flags, struct net_device *dev,
7464 				u8 *dst, u8 *next_hop,
7465 				struct mpath_info *pinfo)
7466 {
7467 	void *hdr;
7468 	struct nlattr *pinfoattr;
7469 
7470 	hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
7471 	if (!hdr)
7472 		return -1;
7473 
7474 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7475 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7476 	    nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
7477 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
7478 		goto nla_put_failure;
7479 
7480 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
7481 	if (!pinfoattr)
7482 		goto nla_put_failure;
7483 	if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
7484 	    nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
7485 			pinfo->frame_qlen))
7486 		goto nla_put_failure;
7487 	if (((pinfo->filled & MPATH_INFO_SN) &&
7488 	     nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
7489 	    ((pinfo->filled & MPATH_INFO_METRIC) &&
7490 	     nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
7491 			 pinfo->metric)) ||
7492 	    ((pinfo->filled & MPATH_INFO_EXPTIME) &&
7493 	     nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
7494 			 pinfo->exptime)) ||
7495 	    ((pinfo->filled & MPATH_INFO_FLAGS) &&
7496 	     nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
7497 			pinfo->flags)) ||
7498 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
7499 	     nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
7500 			 pinfo->discovery_timeout)) ||
7501 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
7502 	     nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
7503 			pinfo->discovery_retries)) ||
7504 	    ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
7505 	     nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
7506 			pinfo->hop_count)) ||
7507 	    ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
7508 	     nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
7509 			 pinfo->path_change_count)))
7510 		goto nla_put_failure;
7511 
7512 	nla_nest_end(msg, pinfoattr);
7513 
7514 	genlmsg_end(msg, hdr);
7515 	return 0;
7516 
7517  nla_put_failure:
7518 	genlmsg_cancel(msg, hdr);
7519 	return -EMSGSIZE;
7520 }
7521 
7522 static int nl80211_dump_mpath(struct sk_buff *skb,
7523 			      struct netlink_callback *cb)
7524 {
7525 	struct mpath_info pinfo;
7526 	struct cfg80211_registered_device *rdev;
7527 	struct wireless_dev *wdev;
7528 	u8 dst[ETH_ALEN];
7529 	u8 next_hop[ETH_ALEN];
7530 	int path_idx = cb->args[2];
7531 	int err;
7532 
7533 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7534 	if (err)
7535 		return err;
7536 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
7537 	__acquire(&rdev->wiphy.mtx);
7538 
7539 	if (!rdev->ops->dump_mpath) {
7540 		err = -EOPNOTSUPP;
7541 		goto out_err;
7542 	}
7543 
7544 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7545 		err = -EOPNOTSUPP;
7546 		goto out_err;
7547 	}
7548 
7549 	while (1) {
7550 		err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
7551 				      next_hop, &pinfo);
7552 		if (err == -ENOENT)
7553 			break;
7554 		if (err)
7555 			goto out_err;
7556 
7557 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7558 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
7559 				       wdev->netdev, dst, next_hop,
7560 				       &pinfo) < 0)
7561 			goto out;
7562 
7563 		path_idx++;
7564 	}
7565 
7566  out:
7567 	cb->args[2] = path_idx;
7568 	err = skb->len;
7569  out_err:
7570 	wiphy_unlock(&rdev->wiphy);
7571 	return err;
7572 }
7573 
7574 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
7575 {
7576 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7577 	int err;
7578 	struct net_device *dev = info->user_ptr[1];
7579 	struct mpath_info pinfo;
7580 	struct sk_buff *msg;
7581 	u8 *dst = NULL;
7582 	u8 next_hop[ETH_ALEN];
7583 
7584 	memset(&pinfo, 0, sizeof(pinfo));
7585 
7586 	if (!info->attrs[NL80211_ATTR_MAC])
7587 		return -EINVAL;
7588 
7589 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7590 
7591 	if (!rdev->ops->get_mpath)
7592 		return -EOPNOTSUPP;
7593 
7594 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7595 		return -EOPNOTSUPP;
7596 
7597 	err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
7598 	if (err)
7599 		return err;
7600 
7601 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7602 	if (!msg)
7603 		return -ENOMEM;
7604 
7605 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7606 				 dev, dst, next_hop, &pinfo) < 0) {
7607 		nlmsg_free(msg);
7608 		return -ENOBUFS;
7609 	}
7610 
7611 	return genlmsg_reply(msg, info);
7612 }
7613 
7614 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
7615 {
7616 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7617 	struct net_device *dev = info->user_ptr[1];
7618 	u8 *dst = NULL;
7619 	u8 *next_hop = NULL;
7620 
7621 	if (!info->attrs[NL80211_ATTR_MAC])
7622 		return -EINVAL;
7623 
7624 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7625 		return -EINVAL;
7626 
7627 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7628 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7629 
7630 	if (!rdev->ops->change_mpath)
7631 		return -EOPNOTSUPP;
7632 
7633 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7634 		return -EOPNOTSUPP;
7635 
7636 	return rdev_change_mpath(rdev, dev, dst, next_hop);
7637 }
7638 
7639 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
7640 {
7641 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7642 	struct net_device *dev = info->user_ptr[1];
7643 	u8 *dst = NULL;
7644 	u8 *next_hop = NULL;
7645 
7646 	if (!info->attrs[NL80211_ATTR_MAC])
7647 		return -EINVAL;
7648 
7649 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7650 		return -EINVAL;
7651 
7652 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7653 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7654 
7655 	if (!rdev->ops->add_mpath)
7656 		return -EOPNOTSUPP;
7657 
7658 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7659 		return -EOPNOTSUPP;
7660 
7661 	return rdev_add_mpath(rdev, dev, dst, next_hop);
7662 }
7663 
7664 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
7665 {
7666 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7667 	struct net_device *dev = info->user_ptr[1];
7668 	u8 *dst = NULL;
7669 
7670 	if (info->attrs[NL80211_ATTR_MAC])
7671 		dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7672 
7673 	if (!rdev->ops->del_mpath)
7674 		return -EOPNOTSUPP;
7675 
7676 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7677 		return -EOPNOTSUPP;
7678 
7679 	return rdev_del_mpath(rdev, dev, dst);
7680 }
7681 
7682 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
7683 {
7684 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7685 	int err;
7686 	struct net_device *dev = info->user_ptr[1];
7687 	struct mpath_info pinfo;
7688 	struct sk_buff *msg;
7689 	u8 *dst = NULL;
7690 	u8 mpp[ETH_ALEN];
7691 
7692 	memset(&pinfo, 0, sizeof(pinfo));
7693 
7694 	if (!info->attrs[NL80211_ATTR_MAC])
7695 		return -EINVAL;
7696 
7697 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7698 
7699 	if (!rdev->ops->get_mpp)
7700 		return -EOPNOTSUPP;
7701 
7702 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7703 		return -EOPNOTSUPP;
7704 
7705 	err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
7706 	if (err)
7707 		return err;
7708 
7709 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7710 	if (!msg)
7711 		return -ENOMEM;
7712 
7713 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7714 			       dev, dst, mpp, &pinfo) < 0) {
7715 		nlmsg_free(msg);
7716 		return -ENOBUFS;
7717 	}
7718 
7719 	return genlmsg_reply(msg, info);
7720 }
7721 
7722 static int nl80211_dump_mpp(struct sk_buff *skb,
7723 			    struct netlink_callback *cb)
7724 {
7725 	struct mpath_info pinfo;
7726 	struct cfg80211_registered_device *rdev;
7727 	struct wireless_dev *wdev;
7728 	u8 dst[ETH_ALEN];
7729 	u8 mpp[ETH_ALEN];
7730 	int path_idx = cb->args[2];
7731 	int err;
7732 
7733 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7734 	if (err)
7735 		return err;
7736 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
7737 	__acquire(&rdev->wiphy.mtx);
7738 
7739 	if (!rdev->ops->dump_mpp) {
7740 		err = -EOPNOTSUPP;
7741 		goto out_err;
7742 	}
7743 
7744 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7745 		err = -EOPNOTSUPP;
7746 		goto out_err;
7747 	}
7748 
7749 	while (1) {
7750 		err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
7751 				    mpp, &pinfo);
7752 		if (err == -ENOENT)
7753 			break;
7754 		if (err)
7755 			goto out_err;
7756 
7757 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7758 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
7759 				       wdev->netdev, dst, mpp,
7760 				       &pinfo) < 0)
7761 			goto out;
7762 
7763 		path_idx++;
7764 	}
7765 
7766  out:
7767 	cb->args[2] = path_idx;
7768 	err = skb->len;
7769  out_err:
7770 	wiphy_unlock(&rdev->wiphy);
7771 	return err;
7772 }
7773 
7774 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
7775 {
7776 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7777 	struct net_device *dev = info->user_ptr[1];
7778 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7779 	struct bss_parameters params;
7780 	int err;
7781 
7782 	memset(&params, 0, sizeof(params));
7783 	/* default to not changing parameters */
7784 	params.use_cts_prot = -1;
7785 	params.use_short_preamble = -1;
7786 	params.use_short_slot_time = -1;
7787 	params.ap_isolate = -1;
7788 	params.ht_opmode = -1;
7789 	params.p2p_ctwindow = -1;
7790 	params.p2p_opp_ps = -1;
7791 
7792 	if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
7793 		params.use_cts_prot =
7794 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
7795 	if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
7796 		params.use_short_preamble =
7797 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
7798 	if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
7799 		params.use_short_slot_time =
7800 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
7801 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
7802 		params.basic_rates =
7803 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7804 		params.basic_rates_len =
7805 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7806 	}
7807 	if (info->attrs[NL80211_ATTR_AP_ISOLATE])
7808 		params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
7809 	if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
7810 		params.ht_opmode =
7811 			nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
7812 
7813 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
7814 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
7815 			return -EINVAL;
7816 		params.p2p_ctwindow =
7817 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
7818 		if (params.p2p_ctwindow != 0 &&
7819 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
7820 			return -EINVAL;
7821 	}
7822 
7823 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
7824 		u8 tmp;
7825 
7826 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
7827 			return -EINVAL;
7828 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
7829 		params.p2p_opp_ps = tmp;
7830 		if (params.p2p_opp_ps &&
7831 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
7832 			return -EINVAL;
7833 	}
7834 
7835 	if (!rdev->ops->change_bss)
7836 		return -EOPNOTSUPP;
7837 
7838 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
7839 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
7840 		return -EOPNOTSUPP;
7841 
7842 	wdev_lock(wdev);
7843 	err = rdev_change_bss(rdev, dev, &params);
7844 	wdev_unlock(wdev);
7845 
7846 	return err;
7847 }
7848 
7849 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
7850 {
7851 	char *data = NULL;
7852 	bool is_indoor;
7853 	enum nl80211_user_reg_hint_type user_reg_hint_type;
7854 	u32 owner_nlportid;
7855 
7856 	/*
7857 	 * You should only get this when cfg80211 hasn't yet initialized
7858 	 * completely when built-in to the kernel right between the time
7859 	 * window between nl80211_init() and regulatory_init(), if that is
7860 	 * even possible.
7861 	 */
7862 	if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
7863 		return -EINPROGRESS;
7864 
7865 	if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
7866 		user_reg_hint_type =
7867 		  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
7868 	else
7869 		user_reg_hint_type = NL80211_USER_REG_HINT_USER;
7870 
7871 	switch (user_reg_hint_type) {
7872 	case NL80211_USER_REG_HINT_USER:
7873 	case NL80211_USER_REG_HINT_CELL_BASE:
7874 		if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
7875 			return -EINVAL;
7876 
7877 		data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
7878 		return regulatory_hint_user(data, user_reg_hint_type);
7879 	case NL80211_USER_REG_HINT_INDOOR:
7880 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
7881 			owner_nlportid = info->snd_portid;
7882 			is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
7883 		} else {
7884 			owner_nlportid = 0;
7885 			is_indoor = true;
7886 		}
7887 
7888 		return regulatory_hint_indoor(is_indoor, owner_nlportid);
7889 	default:
7890 		return -EINVAL;
7891 	}
7892 }
7893 
7894 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
7895 {
7896 	return reg_reload_regdb();
7897 }
7898 
7899 static int nl80211_get_mesh_config(struct sk_buff *skb,
7900 				   struct genl_info *info)
7901 {
7902 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7903 	struct net_device *dev = info->user_ptr[1];
7904 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7905 	struct mesh_config cur_params;
7906 	int err = 0;
7907 	void *hdr;
7908 	struct nlattr *pinfoattr;
7909 	struct sk_buff *msg;
7910 
7911 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
7912 		return -EOPNOTSUPP;
7913 
7914 	if (!rdev->ops->get_mesh_config)
7915 		return -EOPNOTSUPP;
7916 
7917 	wdev_lock(wdev);
7918 	/* If not connected, get default parameters */
7919 	if (!wdev->u.mesh.id_len)
7920 		memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
7921 	else
7922 		err = rdev_get_mesh_config(rdev, dev, &cur_params);
7923 	wdev_unlock(wdev);
7924 
7925 	if (err)
7926 		return err;
7927 
7928 	/* Draw up a netlink message to send back */
7929 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7930 	if (!msg)
7931 		return -ENOMEM;
7932 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7933 			     NL80211_CMD_GET_MESH_CONFIG);
7934 	if (!hdr)
7935 		goto out;
7936 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
7937 	if (!pinfoattr)
7938 		goto nla_put_failure;
7939 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7940 	    nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
7941 			cur_params.dot11MeshRetryTimeout) ||
7942 	    nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
7943 			cur_params.dot11MeshConfirmTimeout) ||
7944 	    nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
7945 			cur_params.dot11MeshHoldingTimeout) ||
7946 	    nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
7947 			cur_params.dot11MeshMaxPeerLinks) ||
7948 	    nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
7949 		       cur_params.dot11MeshMaxRetries) ||
7950 	    nla_put_u8(msg, NL80211_MESHCONF_TTL,
7951 		       cur_params.dot11MeshTTL) ||
7952 	    nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
7953 		       cur_params.element_ttl) ||
7954 	    nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
7955 		       cur_params.auto_open_plinks) ||
7956 	    nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
7957 			cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
7958 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
7959 		       cur_params.dot11MeshHWMPmaxPREQretries) ||
7960 	    nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
7961 			cur_params.path_refresh_time) ||
7962 	    nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
7963 			cur_params.min_discovery_timeout) ||
7964 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
7965 			cur_params.dot11MeshHWMPactivePathTimeout) ||
7966 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
7967 			cur_params.dot11MeshHWMPpreqMinInterval) ||
7968 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
7969 			cur_params.dot11MeshHWMPperrMinInterval) ||
7970 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
7971 			cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
7972 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
7973 		       cur_params.dot11MeshHWMPRootMode) ||
7974 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
7975 			cur_params.dot11MeshHWMPRannInterval) ||
7976 	    nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
7977 		       cur_params.dot11MeshGateAnnouncementProtocol) ||
7978 	    nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
7979 		       cur_params.dot11MeshForwarding) ||
7980 	    nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
7981 			cur_params.rssi_threshold) ||
7982 	    nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
7983 			cur_params.ht_opmode) ||
7984 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
7985 			cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
7986 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
7987 			cur_params.dot11MeshHWMProotInterval) ||
7988 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
7989 			cur_params.dot11MeshHWMPconfirmationInterval) ||
7990 	    nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
7991 			cur_params.power_mode) ||
7992 	    nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
7993 			cur_params.dot11MeshAwakeWindowDuration) ||
7994 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
7995 			cur_params.plink_timeout) ||
7996 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
7997 		       cur_params.dot11MeshConnectedToMeshGate) ||
7998 	    nla_put_u8(msg, NL80211_MESHCONF_NOLEARN,
7999 		       cur_params.dot11MeshNolearn) ||
8000 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_AS,
8001 		       cur_params.dot11MeshConnectedToAuthServer))
8002 		goto nla_put_failure;
8003 	nla_nest_end(msg, pinfoattr);
8004 	genlmsg_end(msg, hdr);
8005 	return genlmsg_reply(msg, info);
8006 
8007  nla_put_failure:
8008  out:
8009 	nlmsg_free(msg);
8010 	return -ENOBUFS;
8011 }
8012 
8013 static const struct nla_policy
8014 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
8015 	[NL80211_MESHCONF_RETRY_TIMEOUT] =
8016 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8017 	[NL80211_MESHCONF_CONFIRM_TIMEOUT] =
8018 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8019 	[NL80211_MESHCONF_HOLDING_TIMEOUT] =
8020 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8021 	[NL80211_MESHCONF_MAX_PEER_LINKS] =
8022 		NLA_POLICY_RANGE(NLA_U16, 0, 255),
8023 	[NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
8024 	[NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8025 	[NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8026 	[NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
8027 	[NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
8028 		NLA_POLICY_RANGE(NLA_U32, 1, 255),
8029 	[NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
8030 	[NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
8031 	[NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
8032 	[NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
8033 	[NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
8034 		NLA_POLICY_MIN(NLA_U16, 1),
8035 	[NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
8036 		NLA_POLICY_MIN(NLA_U16, 1),
8037 	[NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
8038 		NLA_POLICY_MIN(NLA_U16, 1),
8039 	[NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
8040 	[NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
8041 		NLA_POLICY_MIN(NLA_U16, 1),
8042 	[NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
8043 	[NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
8044 	[NL80211_MESHCONF_RSSI_THRESHOLD] =
8045 		NLA_POLICY_RANGE(NLA_S32, -255, 0),
8046 	[NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
8047 	[NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
8048 	[NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
8049 		NLA_POLICY_MIN(NLA_U16, 1),
8050 	[NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
8051 		NLA_POLICY_MIN(NLA_U16, 1),
8052 	[NL80211_MESHCONF_POWER_MODE] =
8053 		NLA_POLICY_RANGE(NLA_U32,
8054 				 NL80211_MESH_POWER_ACTIVE,
8055 				 NL80211_MESH_POWER_MAX),
8056 	[NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
8057 	[NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
8058 	[NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8059 	[NL80211_MESHCONF_NOLEARN] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8060 	[NL80211_MESHCONF_CONNECTED_TO_AS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8061 };
8062 
8063 static const struct nla_policy
8064 	nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
8065 	[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
8066 	[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
8067 	[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
8068 	[NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
8069 	[NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
8070 	[NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
8071 	[NL80211_MESH_SETUP_IE] =
8072 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
8073 				       IEEE80211_MAX_DATA_LEN),
8074 	[NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
8075 };
8076 
8077 static int nl80211_parse_mesh_config(struct genl_info *info,
8078 				     struct mesh_config *cfg,
8079 				     u32 *mask_out)
8080 {
8081 	struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
8082 	u32 mask = 0;
8083 	u16 ht_opmode;
8084 
8085 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)	\
8086 do {									\
8087 	if (tb[attr]) {							\
8088 		cfg->param = fn(tb[attr]);				\
8089 		mask |= BIT((attr) - 1);				\
8090 	}								\
8091 } while (0)
8092 
8093 	if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
8094 		return -EINVAL;
8095 	if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
8096 		return -EINVAL;
8097 
8098 	/* This makes sure that there aren't more than 32 mesh config
8099 	 * parameters (otherwise our bitfield scheme would not work.) */
8100 	BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
8101 
8102 	/* Fill in the params struct */
8103 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
8104 				  NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
8105 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
8106 				  NL80211_MESHCONF_CONFIRM_TIMEOUT,
8107 				  nla_get_u16);
8108 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
8109 				  NL80211_MESHCONF_HOLDING_TIMEOUT,
8110 				  nla_get_u16);
8111 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
8112 				  NL80211_MESHCONF_MAX_PEER_LINKS,
8113 				  nla_get_u16);
8114 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
8115 				  NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
8116 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
8117 				  NL80211_MESHCONF_TTL, nla_get_u8);
8118 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
8119 				  NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
8120 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
8121 				  NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8122 				  nla_get_u8);
8123 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
8124 				  mask,
8125 				  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
8126 				  nla_get_u32);
8127 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
8128 				  NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
8129 				  nla_get_u8);
8130 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
8131 				  NL80211_MESHCONF_PATH_REFRESH_TIME,
8132 				  nla_get_u32);
8133 	if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
8134 	    (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
8135 		return -EINVAL;
8136 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
8137 				  NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
8138 				  nla_get_u16);
8139 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
8140 				  mask,
8141 				  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
8142 				  nla_get_u32);
8143 	if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
8144 	    (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
8145 	     cfg->dot11MeshHWMPactivePathTimeout > 65535))
8146 		return -EINVAL;
8147 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
8148 				  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
8149 				  nla_get_u16);
8150 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
8151 				  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
8152 				  nla_get_u16);
8153 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8154 				  dot11MeshHWMPnetDiameterTraversalTime, mask,
8155 				  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
8156 				  nla_get_u16);
8157 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
8158 				  NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
8159 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
8160 				  NL80211_MESHCONF_HWMP_RANN_INTERVAL,
8161 				  nla_get_u16);
8162 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
8163 				  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
8164 				  nla_get_u8);
8165 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
8166 				  NL80211_MESHCONF_FORWARDING, nla_get_u8);
8167 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
8168 				  NL80211_MESHCONF_RSSI_THRESHOLD,
8169 				  nla_get_s32);
8170 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
8171 				  NL80211_MESHCONF_CONNECTED_TO_GATE,
8172 				  nla_get_u8);
8173 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToAuthServer, mask,
8174 				  NL80211_MESHCONF_CONNECTED_TO_AS,
8175 				  nla_get_u8);
8176 	/*
8177 	 * Check HT operation mode based on
8178 	 * IEEE 802.11-2016 9.4.2.57 HT Operation element.
8179 	 */
8180 	if (tb[NL80211_MESHCONF_HT_OPMODE]) {
8181 		ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
8182 
8183 		if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
8184 				  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
8185 				  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
8186 			return -EINVAL;
8187 
8188 		/* NON_HT_STA bit is reserved, but some programs set it */
8189 		ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
8190 
8191 		cfg->ht_opmode = ht_opmode;
8192 		mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
8193 	}
8194 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8195 				  dot11MeshHWMPactivePathToRootTimeout, mask,
8196 				  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
8197 				  nla_get_u32);
8198 	if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
8199 	    (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
8200 	     cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
8201 		return -EINVAL;
8202 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
8203 				  NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
8204 				  nla_get_u16);
8205 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
8206 				  mask,
8207 				  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
8208 				  nla_get_u16);
8209 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
8210 				  NL80211_MESHCONF_POWER_MODE, nla_get_u32);
8211 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
8212 				  NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
8213 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
8214 				  NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
8215 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNolearn, mask,
8216 				  NL80211_MESHCONF_NOLEARN, nla_get_u8);
8217 	if (mask_out)
8218 		*mask_out = mask;
8219 
8220 	return 0;
8221 
8222 #undef FILL_IN_MESH_PARAM_IF_SET
8223 }
8224 
8225 static int nl80211_parse_mesh_setup(struct genl_info *info,
8226 				     struct mesh_setup *setup)
8227 {
8228 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8229 	struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
8230 
8231 	if (!info->attrs[NL80211_ATTR_MESH_SETUP])
8232 		return -EINVAL;
8233 	if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
8234 		return -EINVAL;
8235 
8236 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
8237 		setup->sync_method =
8238 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
8239 		 IEEE80211_SYNC_METHOD_VENDOR :
8240 		 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
8241 
8242 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
8243 		setup->path_sel_proto =
8244 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
8245 		 IEEE80211_PATH_PROTOCOL_VENDOR :
8246 		 IEEE80211_PATH_PROTOCOL_HWMP;
8247 
8248 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
8249 		setup->path_metric =
8250 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
8251 		 IEEE80211_PATH_METRIC_VENDOR :
8252 		 IEEE80211_PATH_METRIC_AIRTIME;
8253 
8254 	if (tb[NL80211_MESH_SETUP_IE]) {
8255 		struct nlattr *ieattr =
8256 			tb[NL80211_MESH_SETUP_IE];
8257 		setup->ie = nla_data(ieattr);
8258 		setup->ie_len = nla_len(ieattr);
8259 	}
8260 	if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
8261 	    !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
8262 		return -EINVAL;
8263 	setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
8264 	setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
8265 	setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
8266 	if (setup->is_secure)
8267 		setup->user_mpm = true;
8268 
8269 	if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
8270 		if (!setup->user_mpm)
8271 			return -EINVAL;
8272 		setup->auth_id =
8273 			nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
8274 	}
8275 
8276 	return 0;
8277 }
8278 
8279 static int nl80211_update_mesh_config(struct sk_buff *skb,
8280 				      struct genl_info *info)
8281 {
8282 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8283 	struct net_device *dev = info->user_ptr[1];
8284 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8285 	struct mesh_config cfg;
8286 	u32 mask;
8287 	int err;
8288 
8289 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
8290 		return -EOPNOTSUPP;
8291 
8292 	if (!rdev->ops->update_mesh_config)
8293 		return -EOPNOTSUPP;
8294 
8295 	err = nl80211_parse_mesh_config(info, &cfg, &mask);
8296 	if (err)
8297 		return err;
8298 
8299 	wdev_lock(wdev);
8300 	if (!wdev->u.mesh.id_len)
8301 		err = -ENOLINK;
8302 
8303 	if (!err)
8304 		err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
8305 
8306 	wdev_unlock(wdev);
8307 
8308 	return err;
8309 }
8310 
8311 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
8312 			      struct sk_buff *msg)
8313 {
8314 	struct nlattr *nl_reg_rules;
8315 	unsigned int i;
8316 
8317 	if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
8318 	    (regdom->dfs_region &&
8319 	     nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
8320 		goto nla_put_failure;
8321 
8322 	nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
8323 	if (!nl_reg_rules)
8324 		goto nla_put_failure;
8325 
8326 	for (i = 0; i < regdom->n_reg_rules; i++) {
8327 		struct nlattr *nl_reg_rule;
8328 		const struct ieee80211_reg_rule *reg_rule;
8329 		const struct ieee80211_freq_range *freq_range;
8330 		const struct ieee80211_power_rule *power_rule;
8331 		unsigned int max_bandwidth_khz;
8332 
8333 		reg_rule = &regdom->reg_rules[i];
8334 		freq_range = &reg_rule->freq_range;
8335 		power_rule = &reg_rule->power_rule;
8336 
8337 		nl_reg_rule = nla_nest_start_noflag(msg, i);
8338 		if (!nl_reg_rule)
8339 			goto nla_put_failure;
8340 
8341 		max_bandwidth_khz = freq_range->max_bandwidth_khz;
8342 		if (!max_bandwidth_khz)
8343 			max_bandwidth_khz = reg_get_max_bandwidth(regdom,
8344 								  reg_rule);
8345 
8346 		if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
8347 				reg_rule->flags) ||
8348 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
8349 				freq_range->start_freq_khz) ||
8350 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
8351 				freq_range->end_freq_khz) ||
8352 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
8353 				max_bandwidth_khz) ||
8354 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
8355 				power_rule->max_antenna_gain) ||
8356 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
8357 				power_rule->max_eirp) ||
8358 		    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
8359 				reg_rule->dfs_cac_ms))
8360 			goto nla_put_failure;
8361 
8362 		nla_nest_end(msg, nl_reg_rule);
8363 	}
8364 
8365 	nla_nest_end(msg, nl_reg_rules);
8366 	return 0;
8367 
8368 nla_put_failure:
8369 	return -EMSGSIZE;
8370 }
8371 
8372 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
8373 {
8374 	const struct ieee80211_regdomain *regdom = NULL;
8375 	struct cfg80211_registered_device *rdev;
8376 	struct wiphy *wiphy = NULL;
8377 	struct sk_buff *msg;
8378 	int err = -EMSGSIZE;
8379 	void *hdr;
8380 
8381 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8382 	if (!msg)
8383 		return -ENOBUFS;
8384 
8385 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8386 			     NL80211_CMD_GET_REG);
8387 	if (!hdr)
8388 		goto put_failure;
8389 
8390 	rtnl_lock();
8391 
8392 	if (info->attrs[NL80211_ATTR_WIPHY]) {
8393 		bool self_managed;
8394 
8395 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
8396 		if (IS_ERR(rdev)) {
8397 			err = PTR_ERR(rdev);
8398 			goto nla_put_failure;
8399 		}
8400 
8401 		wiphy = &rdev->wiphy;
8402 		self_managed = wiphy->regulatory_flags &
8403 			       REGULATORY_WIPHY_SELF_MANAGED;
8404 
8405 		rcu_read_lock();
8406 
8407 		regdom = get_wiphy_regdom(wiphy);
8408 
8409 		/* a self-managed-reg device must have a private regdom */
8410 		if (WARN_ON(!regdom && self_managed)) {
8411 			err = -EINVAL;
8412 			goto nla_put_failure_rcu;
8413 		}
8414 
8415 		if (regdom &&
8416 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8417 			goto nla_put_failure_rcu;
8418 	} else {
8419 		rcu_read_lock();
8420 	}
8421 
8422 	if (!wiphy && reg_last_request_cell_base() &&
8423 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8424 			NL80211_USER_REG_HINT_CELL_BASE))
8425 		goto nla_put_failure_rcu;
8426 
8427 	if (!regdom)
8428 		regdom = rcu_dereference(cfg80211_regdomain);
8429 
8430 	if (nl80211_put_regdom(regdom, msg))
8431 		goto nla_put_failure_rcu;
8432 
8433 	rcu_read_unlock();
8434 
8435 	genlmsg_end(msg, hdr);
8436 	rtnl_unlock();
8437 	return genlmsg_reply(msg, info);
8438 
8439 nla_put_failure_rcu:
8440 	rcu_read_unlock();
8441 nla_put_failure:
8442 	rtnl_unlock();
8443 put_failure:
8444 	nlmsg_free(msg);
8445 	return err;
8446 }
8447 
8448 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
8449 			       u32 seq, int flags, struct wiphy *wiphy,
8450 			       const struct ieee80211_regdomain *regdom)
8451 {
8452 	void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
8453 				   NL80211_CMD_GET_REG);
8454 
8455 	if (!hdr)
8456 		return -1;
8457 
8458 	genl_dump_check_consistent(cb, hdr);
8459 
8460 	if (nl80211_put_regdom(regdom, msg))
8461 		goto nla_put_failure;
8462 
8463 	if (!wiphy && reg_last_request_cell_base() &&
8464 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8465 			NL80211_USER_REG_HINT_CELL_BASE))
8466 		goto nla_put_failure;
8467 
8468 	if (wiphy &&
8469 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8470 		goto nla_put_failure;
8471 
8472 	if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
8473 	    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
8474 		goto nla_put_failure;
8475 
8476 	genlmsg_end(msg, hdr);
8477 	return 0;
8478 
8479 nla_put_failure:
8480 	genlmsg_cancel(msg, hdr);
8481 	return -EMSGSIZE;
8482 }
8483 
8484 static int nl80211_get_reg_dump(struct sk_buff *skb,
8485 				struct netlink_callback *cb)
8486 {
8487 	const struct ieee80211_regdomain *regdom = NULL;
8488 	struct cfg80211_registered_device *rdev;
8489 	int err, reg_idx, start = cb->args[2];
8490 
8491 	rcu_read_lock();
8492 
8493 	if (cfg80211_regdomain && start == 0) {
8494 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8495 					  NLM_F_MULTI, NULL,
8496 					  rcu_dereference(cfg80211_regdomain));
8497 		if (err < 0)
8498 			goto out_err;
8499 	}
8500 
8501 	/* the global regdom is idx 0 */
8502 	reg_idx = 1;
8503 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
8504 		regdom = get_wiphy_regdom(&rdev->wiphy);
8505 		if (!regdom)
8506 			continue;
8507 
8508 		if (++reg_idx <= start)
8509 			continue;
8510 
8511 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8512 					  NLM_F_MULTI, &rdev->wiphy, regdom);
8513 		if (err < 0) {
8514 			reg_idx--;
8515 			break;
8516 		}
8517 	}
8518 
8519 	cb->args[2] = reg_idx;
8520 	err = skb->len;
8521 out_err:
8522 	rcu_read_unlock();
8523 	return err;
8524 }
8525 
8526 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
8527 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
8528 	[NL80211_ATTR_REG_RULE_FLAGS]		= { .type = NLA_U32 },
8529 	[NL80211_ATTR_FREQ_RANGE_START]		= { .type = NLA_U32 },
8530 	[NL80211_ATTR_FREQ_RANGE_END]		= { .type = NLA_U32 },
8531 	[NL80211_ATTR_FREQ_RANGE_MAX_BW]	= { .type = NLA_U32 },
8532 	[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]	= { .type = NLA_U32 },
8533 	[NL80211_ATTR_POWER_RULE_MAX_EIRP]	= { .type = NLA_U32 },
8534 	[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
8535 };
8536 
8537 static int parse_reg_rule(struct nlattr *tb[],
8538 	struct ieee80211_reg_rule *reg_rule)
8539 {
8540 	struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
8541 	struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
8542 
8543 	if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
8544 		return -EINVAL;
8545 	if (!tb[NL80211_ATTR_FREQ_RANGE_START])
8546 		return -EINVAL;
8547 	if (!tb[NL80211_ATTR_FREQ_RANGE_END])
8548 		return -EINVAL;
8549 	if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
8550 		return -EINVAL;
8551 	if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
8552 		return -EINVAL;
8553 
8554 	reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
8555 
8556 	freq_range->start_freq_khz =
8557 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
8558 	freq_range->end_freq_khz =
8559 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
8560 	freq_range->max_bandwidth_khz =
8561 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
8562 
8563 	power_rule->max_eirp =
8564 		nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
8565 
8566 	if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
8567 		power_rule->max_antenna_gain =
8568 			nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
8569 
8570 	if (tb[NL80211_ATTR_DFS_CAC_TIME])
8571 		reg_rule->dfs_cac_ms =
8572 			nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
8573 
8574 	return 0;
8575 }
8576 
8577 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
8578 {
8579 	struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
8580 	struct nlattr *nl_reg_rule;
8581 	char *alpha2;
8582 	int rem_reg_rules, r;
8583 	u32 num_rules = 0, rule_idx = 0;
8584 	enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
8585 	struct ieee80211_regdomain *rd;
8586 
8587 	if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
8588 		return -EINVAL;
8589 
8590 	if (!info->attrs[NL80211_ATTR_REG_RULES])
8591 		return -EINVAL;
8592 
8593 	alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
8594 
8595 	if (info->attrs[NL80211_ATTR_DFS_REGION])
8596 		dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
8597 
8598 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8599 			    rem_reg_rules) {
8600 		num_rules++;
8601 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
8602 			return -EINVAL;
8603 	}
8604 
8605 	rtnl_lock();
8606 	if (!reg_is_valid_request(alpha2)) {
8607 		r = -EINVAL;
8608 		goto out;
8609 	}
8610 
8611 	rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
8612 	if (!rd) {
8613 		r = -ENOMEM;
8614 		goto out;
8615 	}
8616 
8617 	rd->n_reg_rules = num_rules;
8618 	rd->alpha2[0] = alpha2[0];
8619 	rd->alpha2[1] = alpha2[1];
8620 
8621 	/*
8622 	 * Disable DFS master mode if the DFS region was
8623 	 * not supported or known on this kernel.
8624 	 */
8625 	if (reg_supported_dfs_region(dfs_region))
8626 		rd->dfs_region = dfs_region;
8627 
8628 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8629 			    rem_reg_rules) {
8630 		r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
8631 						nl_reg_rule, reg_rule_policy,
8632 						info->extack);
8633 		if (r)
8634 			goto bad_reg;
8635 		r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
8636 		if (r)
8637 			goto bad_reg;
8638 
8639 		rule_idx++;
8640 
8641 		if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
8642 			r = -EINVAL;
8643 			goto bad_reg;
8644 		}
8645 	}
8646 
8647 	r = set_regdom(rd, REGD_SOURCE_CRDA);
8648 	/* set_regdom takes ownership of rd */
8649 	rd = NULL;
8650  bad_reg:
8651 	kfree(rd);
8652  out:
8653 	rtnl_unlock();
8654 	return r;
8655 }
8656 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
8657 
8658 static int validate_scan_freqs(struct nlattr *freqs)
8659 {
8660 	struct nlattr *attr1, *attr2;
8661 	int n_channels = 0, tmp1, tmp2;
8662 
8663 	nla_for_each_nested(attr1, freqs, tmp1)
8664 		if (nla_len(attr1) != sizeof(u32))
8665 			return 0;
8666 
8667 	nla_for_each_nested(attr1, freqs, tmp1) {
8668 		n_channels++;
8669 		/*
8670 		 * Some hardware has a limited channel list for
8671 		 * scanning, and it is pretty much nonsensical
8672 		 * to scan for a channel twice, so disallow that
8673 		 * and don't require drivers to check that the
8674 		 * channel list they get isn't longer than what
8675 		 * they can scan, as long as they can scan all
8676 		 * the channels they registered at once.
8677 		 */
8678 		nla_for_each_nested(attr2, freqs, tmp2)
8679 			if (attr1 != attr2 &&
8680 			    nla_get_u32(attr1) == nla_get_u32(attr2))
8681 				return 0;
8682 	}
8683 
8684 	return n_channels;
8685 }
8686 
8687 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
8688 {
8689 	return b < NUM_NL80211_BANDS && wiphy->bands[b];
8690 }
8691 
8692 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
8693 			    struct cfg80211_bss_selection *bss_select)
8694 {
8695 	struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
8696 	struct nlattr *nest;
8697 	int err;
8698 	bool found = false;
8699 	int i;
8700 
8701 	/* only process one nested attribute */
8702 	nest = nla_data(nla);
8703 	if (!nla_ok(nest, nla_len(nest)))
8704 		return -EINVAL;
8705 
8706 	err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
8707 					  nest, nl80211_bss_select_policy,
8708 					  NULL);
8709 	if (err)
8710 		return err;
8711 
8712 	/* only one attribute may be given */
8713 	for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
8714 		if (attr[i]) {
8715 			if (found)
8716 				return -EINVAL;
8717 			found = true;
8718 		}
8719 	}
8720 
8721 	bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
8722 
8723 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
8724 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
8725 
8726 	if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
8727 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
8728 		bss_select->param.band_pref =
8729 			nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
8730 		if (!is_band_valid(wiphy, bss_select->param.band_pref))
8731 			return -EINVAL;
8732 	}
8733 
8734 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
8735 		struct nl80211_bss_select_rssi_adjust *adj_param;
8736 
8737 		adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
8738 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
8739 		bss_select->param.adjust.band = adj_param->band;
8740 		bss_select->param.adjust.delta = adj_param->delta;
8741 		if (!is_band_valid(wiphy, bss_select->param.adjust.band))
8742 			return -EINVAL;
8743 	}
8744 
8745 	/* user-space did not provide behaviour attribute */
8746 	if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
8747 		return -EINVAL;
8748 
8749 	if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
8750 		return -EINVAL;
8751 
8752 	return 0;
8753 }
8754 
8755 int nl80211_parse_random_mac(struct nlattr **attrs,
8756 			     u8 *mac_addr, u8 *mac_addr_mask)
8757 {
8758 	int i;
8759 
8760 	if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
8761 		eth_zero_addr(mac_addr);
8762 		eth_zero_addr(mac_addr_mask);
8763 		mac_addr[0] = 0x2;
8764 		mac_addr_mask[0] = 0x3;
8765 
8766 		return 0;
8767 	}
8768 
8769 	/* need both or none */
8770 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
8771 		return -EINVAL;
8772 
8773 	memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
8774 	memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
8775 
8776 	/* don't allow or configure an mcast address */
8777 	if (!is_multicast_ether_addr(mac_addr_mask) ||
8778 	    is_multicast_ether_addr(mac_addr))
8779 		return -EINVAL;
8780 
8781 	/*
8782 	 * allow users to pass a MAC address that has bits set outside
8783 	 * of the mask, but don't bother drivers with having to deal
8784 	 * with such bits
8785 	 */
8786 	for (i = 0; i < ETH_ALEN; i++)
8787 		mac_addr[i] &= mac_addr_mask[i];
8788 
8789 	return 0;
8790 }
8791 
8792 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev,
8793 					      struct ieee80211_channel *chan)
8794 {
8795 	unsigned int link_id;
8796 	bool all_ok = true;
8797 
8798 	ASSERT_WDEV_LOCK(wdev);
8799 
8800 	if (!cfg80211_beaconing_iface_active(wdev))
8801 		return true;
8802 
8803 	/*
8804 	 * FIXME: check if we have a free HW resource/link for chan
8805 	 *
8806 	 * This, as well as the FIXME below, requires knowing the link
8807 	 * capabilities of the hardware.
8808 	 */
8809 
8810 	/* we cannot leave radar channels */
8811 	for_each_valid_link(wdev, link_id) {
8812 		struct cfg80211_chan_def *chandef;
8813 
8814 		chandef = wdev_chandef(wdev, link_id);
8815 		if (!chandef)
8816 			continue;
8817 
8818 		/*
8819 		 * FIXME: don't require all_ok, but rather check only the
8820 		 *	  correct HW resource/link onto which 'chan' falls,
8821 		 *	  as only that link leaves the channel for doing
8822 		 *	  the off-channel operation.
8823 		 */
8824 
8825 		if (chandef->chan->flags & IEEE80211_CHAN_RADAR)
8826 			all_ok = false;
8827 	}
8828 
8829 	if (all_ok)
8830 		return true;
8831 
8832 	return regulatory_pre_cac_allowed(wdev->wiphy);
8833 }
8834 
8835 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
8836 				    enum nl80211_ext_feature_index feat)
8837 {
8838 	if (!(flags & flag))
8839 		return true;
8840 	if (wiphy_ext_feature_isset(wiphy, feat))
8841 		return true;
8842 	return false;
8843 }
8844 
8845 static int
8846 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
8847 			 void *request, struct nlattr **attrs,
8848 			 bool is_sched_scan)
8849 {
8850 	u8 *mac_addr, *mac_addr_mask;
8851 	u32 *flags;
8852 	enum nl80211_feature_flags randomness_flag;
8853 
8854 	if (!attrs[NL80211_ATTR_SCAN_FLAGS])
8855 		return 0;
8856 
8857 	if (is_sched_scan) {
8858 		struct cfg80211_sched_scan_request *req = request;
8859 
8860 		randomness_flag = wdev ?
8861 				  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
8862 				  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
8863 		flags = &req->flags;
8864 		mac_addr = req->mac_addr;
8865 		mac_addr_mask = req->mac_addr_mask;
8866 	} else {
8867 		struct cfg80211_scan_request *req = request;
8868 
8869 		randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
8870 		flags = &req->flags;
8871 		mac_addr = req->mac_addr;
8872 		mac_addr_mask = req->mac_addr_mask;
8873 	}
8874 
8875 	*flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
8876 
8877 	if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
8878 	     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
8879 	    !nl80211_check_scan_feat(wiphy, *flags,
8880 				     NL80211_SCAN_FLAG_LOW_SPAN,
8881 				     NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
8882 	    !nl80211_check_scan_feat(wiphy, *flags,
8883 				     NL80211_SCAN_FLAG_LOW_POWER,
8884 				     NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
8885 	    !nl80211_check_scan_feat(wiphy, *flags,
8886 				     NL80211_SCAN_FLAG_HIGH_ACCURACY,
8887 				     NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
8888 	    !nl80211_check_scan_feat(wiphy, *flags,
8889 				     NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
8890 				     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
8891 	    !nl80211_check_scan_feat(wiphy, *flags,
8892 				     NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
8893 				     NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
8894 	    !nl80211_check_scan_feat(wiphy, *flags,
8895 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
8896 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
8897 	    !nl80211_check_scan_feat(wiphy, *flags,
8898 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
8899 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
8900 	    !nl80211_check_scan_feat(wiphy, *flags,
8901 				     NL80211_SCAN_FLAG_RANDOM_SN,
8902 				     NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
8903 	    !nl80211_check_scan_feat(wiphy, *flags,
8904 				     NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
8905 				     NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
8906 		return -EOPNOTSUPP;
8907 
8908 	if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
8909 		int err;
8910 
8911 		if (!(wiphy->features & randomness_flag) ||
8912 		    (wdev && wdev->connected))
8913 			return -EOPNOTSUPP;
8914 
8915 		err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
8916 		if (err)
8917 			return err;
8918 	}
8919 
8920 	return 0;
8921 }
8922 
8923 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
8924 {
8925 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8926 	struct wireless_dev *wdev = info->user_ptr[1];
8927 	struct cfg80211_scan_request *request;
8928 	struct nlattr *scan_freqs = NULL;
8929 	bool scan_freqs_khz = false;
8930 	struct nlattr *attr;
8931 	struct wiphy *wiphy;
8932 	int err, tmp, n_ssids = 0, n_channels, i;
8933 	size_t ie_len;
8934 
8935 	wiphy = &rdev->wiphy;
8936 
8937 	if (wdev->iftype == NL80211_IFTYPE_NAN)
8938 		return -EOPNOTSUPP;
8939 
8940 	if (!rdev->ops->scan)
8941 		return -EOPNOTSUPP;
8942 
8943 	if (rdev->scan_req || rdev->scan_msg)
8944 		return -EBUSY;
8945 
8946 	if (info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ]) {
8947 		if (!wiphy_ext_feature_isset(wiphy,
8948 					     NL80211_EXT_FEATURE_SCAN_FREQ_KHZ))
8949 			return -EOPNOTSUPP;
8950 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ];
8951 		scan_freqs_khz = true;
8952 	} else if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES])
8953 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQUENCIES];
8954 
8955 	if (scan_freqs) {
8956 		n_channels = validate_scan_freqs(scan_freqs);
8957 		if (!n_channels)
8958 			return -EINVAL;
8959 	} else {
8960 		n_channels = ieee80211_get_num_supported_channels(wiphy);
8961 	}
8962 
8963 	if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
8964 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
8965 			n_ssids++;
8966 
8967 	if (n_ssids > wiphy->max_scan_ssids)
8968 		return -EINVAL;
8969 
8970 	if (info->attrs[NL80211_ATTR_IE])
8971 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8972 	else
8973 		ie_len = 0;
8974 
8975 	if (ie_len > wiphy->max_scan_ie_len)
8976 		return -EINVAL;
8977 
8978 	request = kzalloc(sizeof(*request)
8979 			+ sizeof(*request->ssids) * n_ssids
8980 			+ sizeof(*request->channels) * n_channels
8981 			+ ie_len, GFP_KERNEL);
8982 	if (!request)
8983 		return -ENOMEM;
8984 
8985 	if (n_ssids)
8986 		request->ssids = (void *)&request->channels[n_channels];
8987 	request->n_ssids = n_ssids;
8988 	if (ie_len) {
8989 		if (n_ssids)
8990 			request->ie = (void *)(request->ssids + n_ssids);
8991 		else
8992 			request->ie = (void *)(request->channels + n_channels);
8993 	}
8994 
8995 	i = 0;
8996 	if (scan_freqs) {
8997 		/* user specified, bail out if channel not found */
8998 		nla_for_each_nested(attr, scan_freqs, tmp) {
8999 			struct ieee80211_channel *chan;
9000 			int freq = nla_get_u32(attr);
9001 
9002 			if (!scan_freqs_khz)
9003 				freq = MHZ_TO_KHZ(freq);
9004 
9005 			chan = ieee80211_get_channel_khz(wiphy, freq);
9006 			if (!chan) {
9007 				err = -EINVAL;
9008 				goto out_free;
9009 			}
9010 
9011 			/* ignore disabled channels */
9012 			if (chan->flags & IEEE80211_CHAN_DISABLED)
9013 				continue;
9014 
9015 			request->channels[i] = chan;
9016 			i++;
9017 		}
9018 	} else {
9019 		enum nl80211_band band;
9020 
9021 		/* all channels */
9022 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
9023 			int j;
9024 
9025 			if (!wiphy->bands[band])
9026 				continue;
9027 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9028 				struct ieee80211_channel *chan;
9029 
9030 				chan = &wiphy->bands[band]->channels[j];
9031 
9032 				if (chan->flags & IEEE80211_CHAN_DISABLED)
9033 					continue;
9034 
9035 				request->channels[i] = chan;
9036 				i++;
9037 			}
9038 		}
9039 	}
9040 
9041 	if (!i) {
9042 		err = -EINVAL;
9043 		goto out_free;
9044 	}
9045 
9046 	request->n_channels = i;
9047 
9048 	wdev_lock(wdev);
9049 	for (i = 0; i < request->n_channels; i++) {
9050 		struct ieee80211_channel *chan = request->channels[i];
9051 
9052 		/* if we can go off-channel to the target channel we're good */
9053 		if (cfg80211_off_channel_oper_allowed(wdev, chan))
9054 			continue;
9055 
9056 		if (!cfg80211_wdev_on_sub_chan(wdev, chan, true)) {
9057 			wdev_unlock(wdev);
9058 			err = -EBUSY;
9059 			goto out_free;
9060 		}
9061 	}
9062 	wdev_unlock(wdev);
9063 
9064 	i = 0;
9065 	if (n_ssids) {
9066 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
9067 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9068 				err = -EINVAL;
9069 				goto out_free;
9070 			}
9071 			request->ssids[i].ssid_len = nla_len(attr);
9072 			memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
9073 			i++;
9074 		}
9075 	}
9076 
9077 	if (info->attrs[NL80211_ATTR_IE]) {
9078 		request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9079 		memcpy((void *)request->ie,
9080 		       nla_data(info->attrs[NL80211_ATTR_IE]),
9081 		       request->ie_len);
9082 	}
9083 
9084 	for (i = 0; i < NUM_NL80211_BANDS; i++)
9085 		if (wiphy->bands[i])
9086 			request->rates[i] =
9087 				(1 << wiphy->bands[i]->n_bitrates) - 1;
9088 
9089 	if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
9090 		nla_for_each_nested(attr,
9091 				    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
9092 				    tmp) {
9093 			enum nl80211_band band = nla_type(attr);
9094 
9095 			if (band < 0 || band >= NUM_NL80211_BANDS) {
9096 				err = -EINVAL;
9097 				goto out_free;
9098 			}
9099 
9100 			if (!wiphy->bands[band])
9101 				continue;
9102 
9103 			err = ieee80211_get_ratemask(wiphy->bands[band],
9104 						     nla_data(attr),
9105 						     nla_len(attr),
9106 						     &request->rates[band]);
9107 			if (err)
9108 				goto out_free;
9109 		}
9110 	}
9111 
9112 	if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
9113 		request->duration =
9114 			nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
9115 		request->duration_mandatory =
9116 			nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
9117 	}
9118 
9119 	err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
9120 				       false);
9121 	if (err)
9122 		goto out_free;
9123 
9124 	request->no_cck =
9125 		nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
9126 
9127 	/* Initial implementation used NL80211_ATTR_MAC to set the specific
9128 	 * BSSID to scan for. This was problematic because that same attribute
9129 	 * was already used for another purpose (local random MAC address). The
9130 	 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
9131 	 * compatibility with older userspace components, also use the
9132 	 * NL80211_ATTR_MAC value here if it can be determined to be used for
9133 	 * the specific BSSID use case instead of the random MAC address
9134 	 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
9135 	 */
9136 	if (info->attrs[NL80211_ATTR_BSSID])
9137 		memcpy(request->bssid,
9138 		       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
9139 	else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
9140 		 info->attrs[NL80211_ATTR_MAC])
9141 		memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
9142 		       ETH_ALEN);
9143 	else
9144 		eth_broadcast_addr(request->bssid);
9145 
9146 	request->wdev = wdev;
9147 	request->wiphy = &rdev->wiphy;
9148 	request->scan_start = jiffies;
9149 
9150 	rdev->scan_req = request;
9151 	err = cfg80211_scan(rdev);
9152 
9153 	if (err)
9154 		goto out_free;
9155 
9156 	nl80211_send_scan_start(rdev, wdev);
9157 	dev_hold(wdev->netdev);
9158 
9159 	return 0;
9160 
9161  out_free:
9162 	rdev->scan_req = NULL;
9163 	kfree(request);
9164 
9165 	return err;
9166 }
9167 
9168 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
9169 {
9170 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9171 	struct wireless_dev *wdev = info->user_ptr[1];
9172 
9173 	if (!rdev->ops->abort_scan)
9174 		return -EOPNOTSUPP;
9175 
9176 	if (rdev->scan_msg)
9177 		return 0;
9178 
9179 	if (!rdev->scan_req)
9180 		return -ENOENT;
9181 
9182 	rdev_abort_scan(rdev, wdev);
9183 	return 0;
9184 }
9185 
9186 static int
9187 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
9188 			       struct cfg80211_sched_scan_request *request,
9189 			       struct nlattr **attrs)
9190 {
9191 	int tmp, err, i = 0;
9192 	struct nlattr *attr;
9193 
9194 	if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9195 		u32 interval;
9196 
9197 		/*
9198 		 * If scan plans are not specified,
9199 		 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
9200 		 * case one scan plan will be set with the specified scan
9201 		 * interval and infinite number of iterations.
9202 		 */
9203 		interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
9204 		if (!interval)
9205 			return -EINVAL;
9206 
9207 		request->scan_plans[0].interval =
9208 			DIV_ROUND_UP(interval, MSEC_PER_SEC);
9209 		if (!request->scan_plans[0].interval)
9210 			return -EINVAL;
9211 
9212 		if (request->scan_plans[0].interval >
9213 		    wiphy->max_sched_scan_plan_interval)
9214 			request->scan_plans[0].interval =
9215 				wiphy->max_sched_scan_plan_interval;
9216 
9217 		return 0;
9218 	}
9219 
9220 	nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
9221 		struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
9222 
9223 		if (WARN_ON(i >= n_plans))
9224 			return -EINVAL;
9225 
9226 		err = nla_parse_nested_deprecated(plan,
9227 						  NL80211_SCHED_SCAN_PLAN_MAX,
9228 						  attr, nl80211_plan_policy,
9229 						  NULL);
9230 		if (err)
9231 			return err;
9232 
9233 		if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
9234 			return -EINVAL;
9235 
9236 		request->scan_plans[i].interval =
9237 			nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
9238 		if (!request->scan_plans[i].interval ||
9239 		    request->scan_plans[i].interval >
9240 		    wiphy->max_sched_scan_plan_interval)
9241 			return -EINVAL;
9242 
9243 		if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
9244 			request->scan_plans[i].iterations =
9245 				nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
9246 			if (!request->scan_plans[i].iterations ||
9247 			    (request->scan_plans[i].iterations >
9248 			     wiphy->max_sched_scan_plan_iterations))
9249 				return -EINVAL;
9250 		} else if (i < n_plans - 1) {
9251 			/*
9252 			 * All scan plans but the last one must specify
9253 			 * a finite number of iterations
9254 			 */
9255 			return -EINVAL;
9256 		}
9257 
9258 		i++;
9259 	}
9260 
9261 	/*
9262 	 * The last scan plan must not specify the number of
9263 	 * iterations, it is supposed to run infinitely
9264 	 */
9265 	if (request->scan_plans[n_plans - 1].iterations)
9266 		return  -EINVAL;
9267 
9268 	return 0;
9269 }
9270 
9271 static int
9272 nl80211_parse_sched_scan_per_band_rssi(struct wiphy *wiphy,
9273 				       struct cfg80211_match_set *match_sets,
9274 				       struct nlattr *tb_band_rssi,
9275 				       s32 rssi_thold)
9276 {
9277 	struct nlattr *attr;
9278 	int i, tmp, ret = 0;
9279 
9280 	if (!wiphy_ext_feature_isset(wiphy,
9281 		    NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD)) {
9282 		if (tb_band_rssi)
9283 			ret = -EOPNOTSUPP;
9284 		else
9285 			for (i = 0; i < NUM_NL80211_BANDS; i++)
9286 				match_sets->per_band_rssi_thold[i] =
9287 					NL80211_SCAN_RSSI_THOLD_OFF;
9288 		return ret;
9289 	}
9290 
9291 	for (i = 0; i < NUM_NL80211_BANDS; i++)
9292 		match_sets->per_band_rssi_thold[i] = rssi_thold;
9293 
9294 	nla_for_each_nested(attr, tb_band_rssi, tmp) {
9295 		enum nl80211_band band = nla_type(attr);
9296 
9297 		if (band < 0 || band >= NUM_NL80211_BANDS)
9298 			return -EINVAL;
9299 
9300 		match_sets->per_band_rssi_thold[band] =	nla_get_s32(attr);
9301 	}
9302 
9303 	return 0;
9304 }
9305 
9306 static struct cfg80211_sched_scan_request *
9307 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
9308 			 struct nlattr **attrs, int max_match_sets)
9309 {
9310 	struct cfg80211_sched_scan_request *request;
9311 	struct nlattr *attr;
9312 	int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
9313 	enum nl80211_band band;
9314 	size_t ie_len;
9315 	struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
9316 	s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
9317 
9318 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9319 		n_channels = validate_scan_freqs(
9320 				attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
9321 		if (!n_channels)
9322 			return ERR_PTR(-EINVAL);
9323 	} else {
9324 		n_channels = ieee80211_get_num_supported_channels(wiphy);
9325 	}
9326 
9327 	if (attrs[NL80211_ATTR_SCAN_SSIDS])
9328 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9329 				    tmp)
9330 			n_ssids++;
9331 
9332 	if (n_ssids > wiphy->max_sched_scan_ssids)
9333 		return ERR_PTR(-EINVAL);
9334 
9335 	/*
9336 	 * First, count the number of 'real' matchsets. Due to an issue with
9337 	 * the old implementation, matchsets containing only the RSSI attribute
9338 	 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
9339 	 * RSSI for all matchsets, rather than their own matchset for reporting
9340 	 * all APs with a strong RSSI. This is needed to be compatible with
9341 	 * older userspace that treated a matchset with only the RSSI as the
9342 	 * global RSSI for all other matchsets - if there are other matchsets.
9343 	 */
9344 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9345 		nla_for_each_nested(attr,
9346 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9347 				    tmp) {
9348 			struct nlattr *rssi;
9349 
9350 			err = nla_parse_nested_deprecated(tb,
9351 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9352 							  attr,
9353 							  nl80211_match_policy,
9354 							  NULL);
9355 			if (err)
9356 				return ERR_PTR(err);
9357 
9358 			/* SSID and BSSID are mutually exclusive */
9359 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
9360 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
9361 				return ERR_PTR(-EINVAL);
9362 
9363 			/* add other standalone attributes here */
9364 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
9365 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
9366 				n_match_sets++;
9367 				continue;
9368 			}
9369 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9370 			if (rssi)
9371 				default_match_rssi = nla_get_s32(rssi);
9372 		}
9373 	}
9374 
9375 	/* However, if there's no other matchset, add the RSSI one */
9376 	if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
9377 		n_match_sets = 1;
9378 
9379 	if (n_match_sets > max_match_sets)
9380 		return ERR_PTR(-EINVAL);
9381 
9382 	if (attrs[NL80211_ATTR_IE])
9383 		ie_len = nla_len(attrs[NL80211_ATTR_IE]);
9384 	else
9385 		ie_len = 0;
9386 
9387 	if (ie_len > wiphy->max_sched_scan_ie_len)
9388 		return ERR_PTR(-EINVAL);
9389 
9390 	if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9391 		/*
9392 		 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
9393 		 * each scan plan already specifies its own interval
9394 		 */
9395 		if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9396 			return ERR_PTR(-EINVAL);
9397 
9398 		nla_for_each_nested(attr,
9399 				    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
9400 			n_plans++;
9401 	} else {
9402 		/*
9403 		 * The scan interval attribute is kept for backward
9404 		 * compatibility. If no scan plans are specified and sched scan
9405 		 * interval is specified, one scan plan will be set with this
9406 		 * scan interval and infinite number of iterations.
9407 		 */
9408 		if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9409 			return ERR_PTR(-EINVAL);
9410 
9411 		n_plans = 1;
9412 	}
9413 
9414 	if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
9415 		return ERR_PTR(-EINVAL);
9416 
9417 	if (!wiphy_ext_feature_isset(
9418 		    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
9419 	    (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
9420 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
9421 		return ERR_PTR(-EINVAL);
9422 
9423 	request = kzalloc(sizeof(*request)
9424 			+ sizeof(*request->ssids) * n_ssids
9425 			+ sizeof(*request->match_sets) * n_match_sets
9426 			+ sizeof(*request->scan_plans) * n_plans
9427 			+ sizeof(*request->channels) * n_channels
9428 			+ ie_len, GFP_KERNEL);
9429 	if (!request)
9430 		return ERR_PTR(-ENOMEM);
9431 
9432 	if (n_ssids)
9433 		request->ssids = (void *)&request->channels[n_channels];
9434 	request->n_ssids = n_ssids;
9435 	if (ie_len) {
9436 		if (n_ssids)
9437 			request->ie = (void *)(request->ssids + n_ssids);
9438 		else
9439 			request->ie = (void *)(request->channels + n_channels);
9440 	}
9441 
9442 	if (n_match_sets) {
9443 		if (request->ie)
9444 			request->match_sets = (void *)(request->ie + ie_len);
9445 		else if (n_ssids)
9446 			request->match_sets =
9447 				(void *)(request->ssids + n_ssids);
9448 		else
9449 			request->match_sets =
9450 				(void *)(request->channels + n_channels);
9451 	}
9452 	request->n_match_sets = n_match_sets;
9453 
9454 	if (n_match_sets)
9455 		request->scan_plans = (void *)(request->match_sets +
9456 					       n_match_sets);
9457 	else if (request->ie)
9458 		request->scan_plans = (void *)(request->ie + ie_len);
9459 	else if (n_ssids)
9460 		request->scan_plans = (void *)(request->ssids + n_ssids);
9461 	else
9462 		request->scan_plans = (void *)(request->channels + n_channels);
9463 
9464 	request->n_scan_plans = n_plans;
9465 
9466 	i = 0;
9467 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9468 		/* user specified, bail out if channel not found */
9469 		nla_for_each_nested(attr,
9470 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
9471 				    tmp) {
9472 			struct ieee80211_channel *chan;
9473 
9474 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
9475 
9476 			if (!chan) {
9477 				err = -EINVAL;
9478 				goto out_free;
9479 			}
9480 
9481 			/* ignore disabled channels */
9482 			if (chan->flags & IEEE80211_CHAN_DISABLED)
9483 				continue;
9484 
9485 			request->channels[i] = chan;
9486 			i++;
9487 		}
9488 	} else {
9489 		/* all channels */
9490 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
9491 			int j;
9492 
9493 			if (!wiphy->bands[band])
9494 				continue;
9495 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9496 				struct ieee80211_channel *chan;
9497 
9498 				chan = &wiphy->bands[band]->channels[j];
9499 
9500 				if (chan->flags & IEEE80211_CHAN_DISABLED)
9501 					continue;
9502 
9503 				request->channels[i] = chan;
9504 				i++;
9505 			}
9506 		}
9507 	}
9508 
9509 	if (!i) {
9510 		err = -EINVAL;
9511 		goto out_free;
9512 	}
9513 
9514 	request->n_channels = i;
9515 
9516 	i = 0;
9517 	if (n_ssids) {
9518 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9519 				    tmp) {
9520 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9521 				err = -EINVAL;
9522 				goto out_free;
9523 			}
9524 			request->ssids[i].ssid_len = nla_len(attr);
9525 			memcpy(request->ssids[i].ssid, nla_data(attr),
9526 			       nla_len(attr));
9527 			i++;
9528 		}
9529 	}
9530 
9531 	i = 0;
9532 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9533 		nla_for_each_nested(attr,
9534 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9535 				    tmp) {
9536 			struct nlattr *ssid, *bssid, *rssi;
9537 
9538 			err = nla_parse_nested_deprecated(tb,
9539 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9540 							  attr,
9541 							  nl80211_match_policy,
9542 							  NULL);
9543 			if (err)
9544 				goto out_free;
9545 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
9546 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
9547 
9548 			if (!ssid && !bssid) {
9549 				i++;
9550 				continue;
9551 			}
9552 
9553 			if (WARN_ON(i >= n_match_sets)) {
9554 				/* this indicates a programming error,
9555 				 * the loop above should have verified
9556 				 * things properly
9557 				 */
9558 				err = -EINVAL;
9559 				goto out_free;
9560 			}
9561 
9562 			if (ssid) {
9563 				memcpy(request->match_sets[i].ssid.ssid,
9564 				       nla_data(ssid), nla_len(ssid));
9565 				request->match_sets[i].ssid.ssid_len =
9566 					nla_len(ssid);
9567 			}
9568 			if (bssid)
9569 				memcpy(request->match_sets[i].bssid,
9570 				       nla_data(bssid), ETH_ALEN);
9571 
9572 			/* special attribute - old implementation w/a */
9573 			request->match_sets[i].rssi_thold = default_match_rssi;
9574 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9575 			if (rssi)
9576 				request->match_sets[i].rssi_thold =
9577 					nla_get_s32(rssi);
9578 
9579 			/* Parse per band RSSI attribute */
9580 			err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
9581 				&request->match_sets[i],
9582 				tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
9583 				request->match_sets[i].rssi_thold);
9584 			if (err)
9585 				goto out_free;
9586 
9587 			i++;
9588 		}
9589 
9590 		/* there was no other matchset, so the RSSI one is alone */
9591 		if (i == 0 && n_match_sets)
9592 			request->match_sets[0].rssi_thold = default_match_rssi;
9593 
9594 		request->min_rssi_thold = INT_MAX;
9595 		for (i = 0; i < n_match_sets; i++)
9596 			request->min_rssi_thold =
9597 				min(request->match_sets[i].rssi_thold,
9598 				    request->min_rssi_thold);
9599 	} else {
9600 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
9601 	}
9602 
9603 	if (ie_len) {
9604 		request->ie_len = ie_len;
9605 		memcpy((void *)request->ie,
9606 		       nla_data(attrs[NL80211_ATTR_IE]),
9607 		       request->ie_len);
9608 	}
9609 
9610 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
9611 	if (err)
9612 		goto out_free;
9613 
9614 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
9615 		request->delay =
9616 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
9617 
9618 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
9619 		request->relative_rssi = nla_get_s8(
9620 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
9621 		request->relative_rssi_set = true;
9622 	}
9623 
9624 	if (request->relative_rssi_set &&
9625 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
9626 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
9627 
9628 		rssi_adjust = nla_data(
9629 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
9630 		request->rssi_adjust.band = rssi_adjust->band;
9631 		request->rssi_adjust.delta = rssi_adjust->delta;
9632 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
9633 			err = -EINVAL;
9634 			goto out_free;
9635 		}
9636 	}
9637 
9638 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
9639 	if (err)
9640 		goto out_free;
9641 
9642 	request->scan_start = jiffies;
9643 
9644 	return request;
9645 
9646 out_free:
9647 	kfree(request);
9648 	return ERR_PTR(err);
9649 }
9650 
9651 static int nl80211_start_sched_scan(struct sk_buff *skb,
9652 				    struct genl_info *info)
9653 {
9654 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9655 	struct net_device *dev = info->user_ptr[1];
9656 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9657 	struct cfg80211_sched_scan_request *sched_scan_req;
9658 	bool want_multi;
9659 	int err;
9660 
9661 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
9662 		return -EOPNOTSUPP;
9663 
9664 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
9665 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
9666 	if (err)
9667 		return err;
9668 
9669 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
9670 						  info->attrs,
9671 						  rdev->wiphy.max_match_sets);
9672 
9673 	err = PTR_ERR_OR_ZERO(sched_scan_req);
9674 	if (err)
9675 		goto out_err;
9676 
9677 	/* leave request id zero for legacy request
9678 	 * or if driver does not support multi-scheduled scan
9679 	 */
9680 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1)
9681 		sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
9682 
9683 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
9684 	if (err)
9685 		goto out_free;
9686 
9687 	sched_scan_req->dev = dev;
9688 	sched_scan_req->wiphy = &rdev->wiphy;
9689 
9690 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9691 		sched_scan_req->owner_nlportid = info->snd_portid;
9692 
9693 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
9694 
9695 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
9696 	return 0;
9697 
9698 out_free:
9699 	kfree(sched_scan_req);
9700 out_err:
9701 	return err;
9702 }
9703 
9704 static int nl80211_stop_sched_scan(struct sk_buff *skb,
9705 				   struct genl_info *info)
9706 {
9707 	struct cfg80211_sched_scan_request *req;
9708 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9709 	u64 cookie;
9710 
9711 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
9712 		return -EOPNOTSUPP;
9713 
9714 	if (info->attrs[NL80211_ATTR_COOKIE]) {
9715 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9716 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
9717 	}
9718 
9719 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
9720 				     struct cfg80211_sched_scan_request,
9721 				     list);
9722 	if (!req || req->reqid ||
9723 	    (req->owner_nlportid &&
9724 	     req->owner_nlportid != info->snd_portid))
9725 		return -ENOENT;
9726 
9727 	return cfg80211_stop_sched_scan_req(rdev, req, false);
9728 }
9729 
9730 static int nl80211_start_radar_detection(struct sk_buff *skb,
9731 					 struct genl_info *info)
9732 {
9733 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9734 	struct net_device *dev = info->user_ptr[1];
9735 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9736 	struct wiphy *wiphy = wdev->wiphy;
9737 	struct cfg80211_chan_def chandef;
9738 	enum nl80211_dfs_regions dfs_region;
9739 	unsigned int cac_time_ms;
9740 	int err = -EINVAL;
9741 
9742 	flush_delayed_work(&rdev->dfs_update_channels_wk);
9743 
9744 	wiphy_lock(wiphy);
9745 
9746 	dfs_region = reg_get_dfs_region(wiphy);
9747 	if (dfs_region == NL80211_DFS_UNSET)
9748 		goto unlock;
9749 
9750 	err = nl80211_parse_chandef(rdev, info, &chandef);
9751 	if (err)
9752 		goto unlock;
9753 
9754 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
9755 	if (err < 0)
9756 		goto unlock;
9757 
9758 	if (err == 0) {
9759 		err = -EINVAL;
9760 		goto unlock;
9761 	}
9762 
9763 	if (!cfg80211_chandef_dfs_usable(wiphy, &chandef)) {
9764 		err = -EINVAL;
9765 		goto unlock;
9766 	}
9767 
9768 	if (nla_get_flag(info->attrs[NL80211_ATTR_RADAR_BACKGROUND])) {
9769 		err = cfg80211_start_background_radar_detection(rdev, wdev,
9770 								&chandef);
9771 		goto unlock;
9772 	}
9773 
9774 	if (netif_carrier_ok(dev)) {
9775 		err = -EBUSY;
9776 		goto unlock;
9777 	}
9778 
9779 	if (wdev->cac_started) {
9780 		err = -EBUSY;
9781 		goto unlock;
9782 	}
9783 
9784 	/* CAC start is offloaded to HW and can't be started manually */
9785 	if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD)) {
9786 		err = -EOPNOTSUPP;
9787 		goto unlock;
9788 	}
9789 
9790 	if (!rdev->ops->start_radar_detection) {
9791 		err = -EOPNOTSUPP;
9792 		goto unlock;
9793 	}
9794 
9795 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
9796 	if (WARN_ON(!cac_time_ms))
9797 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
9798 
9799 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
9800 	if (!err) {
9801 		wdev->links[0].ap.chandef = chandef;
9802 		wdev->cac_started = true;
9803 		wdev->cac_start_time = jiffies;
9804 		wdev->cac_time_ms = cac_time_ms;
9805 	}
9806 unlock:
9807 	wiphy_unlock(wiphy);
9808 
9809 	return err;
9810 }
9811 
9812 static int nl80211_notify_radar_detection(struct sk_buff *skb,
9813 					  struct genl_info *info)
9814 {
9815 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9816 	struct net_device *dev = info->user_ptr[1];
9817 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9818 	struct wiphy *wiphy = wdev->wiphy;
9819 	struct cfg80211_chan_def chandef;
9820 	enum nl80211_dfs_regions dfs_region;
9821 	int err;
9822 
9823 	dfs_region = reg_get_dfs_region(wiphy);
9824 	if (dfs_region == NL80211_DFS_UNSET) {
9825 		GENL_SET_ERR_MSG(info,
9826 				 "DFS Region is not set. Unexpected Radar indication");
9827 		return -EINVAL;
9828 	}
9829 
9830 	err = nl80211_parse_chandef(rdev, info, &chandef);
9831 	if (err) {
9832 		GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
9833 		return err;
9834 	}
9835 
9836 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
9837 	if (err < 0) {
9838 		GENL_SET_ERR_MSG(info, "chandef is invalid");
9839 		return err;
9840 	}
9841 
9842 	if (err == 0) {
9843 		GENL_SET_ERR_MSG(info,
9844 				 "Unexpected Radar indication for chandef/iftype");
9845 		return -EINVAL;
9846 	}
9847 
9848 	/* Do not process this notification if radar is already detected
9849 	 * by kernel on this channel, and return success.
9850 	 */
9851 	if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
9852 		return 0;
9853 
9854 	cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
9855 
9856 	cfg80211_sched_dfs_chan_update(rdev);
9857 
9858 	rdev->radar_chandef = chandef;
9859 
9860 	/* Propagate this notification to other radios as well */
9861 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
9862 
9863 	return 0;
9864 }
9865 
9866 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
9867 {
9868 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9869 	unsigned int link_id = nl80211_link_id(info->attrs);
9870 	struct net_device *dev = info->user_ptr[1];
9871 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9872 	struct cfg80211_csa_settings params;
9873 	struct nlattr **csa_attrs = NULL;
9874 	int err;
9875 	bool need_new_beacon = false;
9876 	bool need_handle_dfs_flag = true;
9877 	int len, i;
9878 	u32 cs_count;
9879 
9880 	if (!rdev->ops->channel_switch ||
9881 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
9882 		return -EOPNOTSUPP;
9883 
9884 	switch (dev->ieee80211_ptr->iftype) {
9885 	case NL80211_IFTYPE_AP:
9886 	case NL80211_IFTYPE_P2P_GO:
9887 		need_new_beacon = true;
9888 		/* For all modes except AP the handle_dfs flag needs to be
9889 		 * supplied to tell the kernel that userspace will handle radar
9890 		 * events when they happen. Otherwise a switch to a channel
9891 		 * requiring DFS will be rejected.
9892 		 */
9893 		need_handle_dfs_flag = false;
9894 
9895 		/* useless if AP is not running */
9896 		if (!wdev->links[link_id].ap.beacon_interval)
9897 			return -ENOTCONN;
9898 		break;
9899 	case NL80211_IFTYPE_ADHOC:
9900 		if (!wdev->u.ibss.ssid_len)
9901 			return -ENOTCONN;
9902 		break;
9903 	case NL80211_IFTYPE_MESH_POINT:
9904 		if (!wdev->u.mesh.id_len)
9905 			return -ENOTCONN;
9906 		break;
9907 	default:
9908 		return -EOPNOTSUPP;
9909 	}
9910 
9911 	memset(&params, 0, sizeof(params));
9912 	params.beacon_csa.ftm_responder = -1;
9913 
9914 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
9915 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
9916 		return -EINVAL;
9917 
9918 	/* only important for AP, IBSS and mesh create IEs internally */
9919 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
9920 		return -EINVAL;
9921 
9922 	/* Even though the attribute is u32, the specification says
9923 	 * u8, so let's make sure we don't overflow.
9924 	 */
9925 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
9926 	if (cs_count > 255)
9927 		return -EINVAL;
9928 
9929 	params.count = cs_count;
9930 
9931 	if (!need_new_beacon)
9932 		goto skip_beacons;
9933 
9934 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after);
9935 	if (err)
9936 		goto free;
9937 
9938 	csa_attrs = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*csa_attrs),
9939 			    GFP_KERNEL);
9940 	if (!csa_attrs) {
9941 		err = -ENOMEM;
9942 		goto free;
9943 	}
9944 
9945 	err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
9946 					  info->attrs[NL80211_ATTR_CSA_IES],
9947 					  nl80211_policy, info->extack);
9948 	if (err)
9949 		goto free;
9950 
9951 	err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa);
9952 	if (err)
9953 		goto free;
9954 
9955 	if (!csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
9956 		err = -EINVAL;
9957 		goto free;
9958 	}
9959 
9960 	len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
9961 	if (!len || (len % sizeof(u16))) {
9962 		err = -EINVAL;
9963 		goto free;
9964 	}
9965 
9966 	params.n_counter_offsets_beacon = len / sizeof(u16);
9967 	if (rdev->wiphy.max_num_csa_counters &&
9968 	    (params.n_counter_offsets_beacon >
9969 	     rdev->wiphy.max_num_csa_counters)) {
9970 		err = -EINVAL;
9971 		goto free;
9972 	}
9973 
9974 	params.counter_offsets_beacon =
9975 		nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
9976 
9977 	/* sanity checks - counters should fit and be the same */
9978 	for (i = 0; i < params.n_counter_offsets_beacon; i++) {
9979 		u16 offset = params.counter_offsets_beacon[i];
9980 
9981 		if (offset >= params.beacon_csa.tail_len) {
9982 			err = -EINVAL;
9983 			goto free;
9984 		}
9985 
9986 		if (params.beacon_csa.tail[offset] != params.count) {
9987 			err = -EINVAL;
9988 			goto free;
9989 		}
9990 	}
9991 
9992 	if (csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
9993 		len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
9994 		if (!len || (len % sizeof(u16))) {
9995 			err = -EINVAL;
9996 			goto free;
9997 		}
9998 
9999 		params.n_counter_offsets_presp = len / sizeof(u16);
10000 		if (rdev->wiphy.max_num_csa_counters &&
10001 		    (params.n_counter_offsets_presp >
10002 		     rdev->wiphy.max_num_csa_counters)) {
10003 			err = -EINVAL;
10004 			goto free;
10005 		}
10006 
10007 		params.counter_offsets_presp =
10008 			nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
10009 
10010 		/* sanity checks - counters should fit and be the same */
10011 		for (i = 0; i < params.n_counter_offsets_presp; i++) {
10012 			u16 offset = params.counter_offsets_presp[i];
10013 
10014 			if (offset >= params.beacon_csa.probe_resp_len) {
10015 				err = -EINVAL;
10016 				goto free;
10017 			}
10018 
10019 			if (params.beacon_csa.probe_resp[offset] !=
10020 			    params.count) {
10021 				err = -EINVAL;
10022 				goto free;
10023 			}
10024 		}
10025 	}
10026 
10027 skip_beacons:
10028 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
10029 	if (err)
10030 		goto free;
10031 
10032 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
10033 					   wdev->iftype)) {
10034 		err = -EINVAL;
10035 		goto free;
10036 	}
10037 
10038 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
10039 					    &params.chandef,
10040 					    wdev->iftype);
10041 	if (err < 0)
10042 		goto free;
10043 
10044 	if (err > 0) {
10045 		params.radar_required = true;
10046 		if (need_handle_dfs_flag &&
10047 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
10048 			err = -EINVAL;
10049 			goto free;
10050 		}
10051 	}
10052 
10053 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
10054 		params.block_tx = true;
10055 
10056 	wdev_lock(wdev);
10057 	err = rdev_channel_switch(rdev, dev, &params);
10058 	wdev_unlock(wdev);
10059 
10060 free:
10061 	kfree(params.beacon_after.mbssid_ies);
10062 	kfree(params.beacon_csa.mbssid_ies);
10063 	kfree(csa_attrs);
10064 	return err;
10065 }
10066 
10067 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
10068 			    u32 seq, int flags,
10069 			    struct cfg80211_registered_device *rdev,
10070 			    struct wireless_dev *wdev,
10071 			    struct cfg80211_internal_bss *intbss)
10072 {
10073 	struct cfg80211_bss *res = &intbss->pub;
10074 	const struct cfg80211_bss_ies *ies;
10075 	unsigned int link_id;
10076 	void *hdr;
10077 	struct nlattr *bss;
10078 
10079 	ASSERT_WDEV_LOCK(wdev);
10080 
10081 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
10082 			     NL80211_CMD_NEW_SCAN_RESULTS);
10083 	if (!hdr)
10084 		return -1;
10085 
10086 	genl_dump_check_consistent(cb, hdr);
10087 
10088 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
10089 		goto nla_put_failure;
10090 	if (wdev->netdev &&
10091 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
10092 		goto nla_put_failure;
10093 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
10094 			      NL80211_ATTR_PAD))
10095 		goto nla_put_failure;
10096 
10097 	bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
10098 	if (!bss)
10099 		goto nla_put_failure;
10100 	if ((!is_zero_ether_addr(res->bssid) &&
10101 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
10102 		goto nla_put_failure;
10103 
10104 	rcu_read_lock();
10105 	/* indicate whether we have probe response data or not */
10106 	if (rcu_access_pointer(res->proberesp_ies) &&
10107 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
10108 		goto fail_unlock_rcu;
10109 
10110 	/* this pointer prefers to be pointed to probe response data
10111 	 * but is always valid
10112 	 */
10113 	ies = rcu_dereference(res->ies);
10114 	if (ies) {
10115 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
10116 				      NL80211_BSS_PAD))
10117 			goto fail_unlock_rcu;
10118 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
10119 					ies->len, ies->data))
10120 			goto fail_unlock_rcu;
10121 	}
10122 
10123 	/* and this pointer is always (unless driver didn't know) beacon data */
10124 	ies = rcu_dereference(res->beacon_ies);
10125 	if (ies && ies->from_beacon) {
10126 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
10127 				      NL80211_BSS_PAD))
10128 			goto fail_unlock_rcu;
10129 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
10130 					ies->len, ies->data))
10131 			goto fail_unlock_rcu;
10132 	}
10133 	rcu_read_unlock();
10134 
10135 	if (res->beacon_interval &&
10136 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
10137 		goto nla_put_failure;
10138 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
10139 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
10140 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY_OFFSET,
10141 			res->channel->freq_offset) ||
10142 	    nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
10143 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
10144 			jiffies_to_msecs(jiffies - intbss->ts)))
10145 		goto nla_put_failure;
10146 
10147 	if (intbss->parent_tsf &&
10148 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
10149 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
10150 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
10151 		     intbss->parent_bssid)))
10152 		goto nla_put_failure;
10153 
10154 	if (intbss->ts_boottime &&
10155 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
10156 			      intbss->ts_boottime, NL80211_BSS_PAD))
10157 		goto nla_put_failure;
10158 
10159 	if (!nl80211_put_signal(msg, intbss->pub.chains,
10160 				intbss->pub.chain_signal,
10161 				NL80211_BSS_CHAIN_SIGNAL))
10162 		goto nla_put_failure;
10163 
10164 	switch (rdev->wiphy.signal_type) {
10165 	case CFG80211_SIGNAL_TYPE_MBM:
10166 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
10167 			goto nla_put_failure;
10168 		break;
10169 	case CFG80211_SIGNAL_TYPE_UNSPEC:
10170 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
10171 			goto nla_put_failure;
10172 		break;
10173 	default:
10174 		break;
10175 	}
10176 
10177 	switch (wdev->iftype) {
10178 	case NL80211_IFTYPE_P2P_CLIENT:
10179 	case NL80211_IFTYPE_STATION:
10180 		for_each_valid_link(wdev, link_id) {
10181 			if (intbss == wdev->links[link_id].client.current_bss &&
10182 			    (nla_put_u32(msg, NL80211_BSS_STATUS,
10183 					 NL80211_BSS_STATUS_ASSOCIATED) ||
10184 			     (wdev->valid_links &&
10185 			      (nla_put_u8(msg, NL80211_BSS_MLO_LINK_ID,
10186 					  link_id) ||
10187 			       nla_put(msg, NL80211_BSS_MLD_ADDR, ETH_ALEN,
10188 				       wdev->u.client.connected_addr)))))
10189 				goto nla_put_failure;
10190 		}
10191 		break;
10192 	case NL80211_IFTYPE_ADHOC:
10193 		if (intbss == wdev->u.ibss.current_bss &&
10194 		    nla_put_u32(msg, NL80211_BSS_STATUS,
10195 				NL80211_BSS_STATUS_IBSS_JOINED))
10196 			goto nla_put_failure;
10197 		break;
10198 	default:
10199 		break;
10200 	}
10201 
10202 	nla_nest_end(msg, bss);
10203 
10204 	genlmsg_end(msg, hdr);
10205 	return 0;
10206 
10207  fail_unlock_rcu:
10208 	rcu_read_unlock();
10209  nla_put_failure:
10210 	genlmsg_cancel(msg, hdr);
10211 	return -EMSGSIZE;
10212 }
10213 
10214 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
10215 {
10216 	struct cfg80211_registered_device *rdev;
10217 	struct cfg80211_internal_bss *scan;
10218 	struct wireless_dev *wdev;
10219 	int start = cb->args[2], idx = 0;
10220 	int err;
10221 
10222 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
10223 	if (err)
10224 		return err;
10225 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10226 	__acquire(&rdev->wiphy.mtx);
10227 
10228 	wdev_lock(wdev);
10229 	spin_lock_bh(&rdev->bss_lock);
10230 
10231 	/*
10232 	 * dump_scan will be called multiple times to break up the scan results
10233 	 * into multiple messages.  It is unlikely that any more bss-es will be
10234 	 * expired after the first call, so only call only call this on the
10235 	 * first dump_scan invocation.
10236 	 */
10237 	if (start == 0)
10238 		cfg80211_bss_expire(rdev);
10239 
10240 	cb->seq = rdev->bss_generation;
10241 
10242 	list_for_each_entry(scan, &rdev->bss_list, list) {
10243 		if (++idx <= start)
10244 			continue;
10245 		if (nl80211_send_bss(skb, cb,
10246 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10247 				rdev, wdev, scan) < 0) {
10248 			idx--;
10249 			break;
10250 		}
10251 	}
10252 
10253 	spin_unlock_bh(&rdev->bss_lock);
10254 	wdev_unlock(wdev);
10255 
10256 	cb->args[2] = idx;
10257 	wiphy_unlock(&rdev->wiphy);
10258 
10259 	return skb->len;
10260 }
10261 
10262 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
10263 			       int flags, struct net_device *dev,
10264 			       bool allow_radio_stats,
10265 			       struct survey_info *survey)
10266 {
10267 	void *hdr;
10268 	struct nlattr *infoattr;
10269 
10270 	/* skip radio stats if userspace didn't request them */
10271 	if (!survey->channel && !allow_radio_stats)
10272 		return 0;
10273 
10274 	hdr = nl80211hdr_put(msg, portid, seq, flags,
10275 			     NL80211_CMD_NEW_SURVEY_RESULTS);
10276 	if (!hdr)
10277 		return -ENOMEM;
10278 
10279 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
10280 		goto nla_put_failure;
10281 
10282 	infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
10283 	if (!infoattr)
10284 		goto nla_put_failure;
10285 
10286 	if (survey->channel &&
10287 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
10288 			survey->channel->center_freq))
10289 		goto nla_put_failure;
10290 
10291 	if (survey->channel && survey->channel->freq_offset &&
10292 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY_OFFSET,
10293 			survey->channel->freq_offset))
10294 		goto nla_put_failure;
10295 
10296 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
10297 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
10298 		goto nla_put_failure;
10299 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
10300 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
10301 		goto nla_put_failure;
10302 	if ((survey->filled & SURVEY_INFO_TIME) &&
10303 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
10304 			survey->time, NL80211_SURVEY_INFO_PAD))
10305 		goto nla_put_failure;
10306 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
10307 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
10308 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
10309 		goto nla_put_failure;
10310 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
10311 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
10312 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
10313 		goto nla_put_failure;
10314 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
10315 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
10316 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
10317 		goto nla_put_failure;
10318 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
10319 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
10320 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
10321 		goto nla_put_failure;
10322 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
10323 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
10324 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
10325 		goto nla_put_failure;
10326 	if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
10327 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
10328 			      survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
10329 		goto nla_put_failure;
10330 
10331 	nla_nest_end(msg, infoattr);
10332 
10333 	genlmsg_end(msg, hdr);
10334 	return 0;
10335 
10336  nla_put_failure:
10337 	genlmsg_cancel(msg, hdr);
10338 	return -EMSGSIZE;
10339 }
10340 
10341 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
10342 {
10343 	struct nlattr **attrbuf;
10344 	struct survey_info survey;
10345 	struct cfg80211_registered_device *rdev;
10346 	struct wireless_dev *wdev;
10347 	int survey_idx = cb->args[2];
10348 	int res;
10349 	bool radio_stats;
10350 
10351 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
10352 	if (!attrbuf)
10353 		return -ENOMEM;
10354 
10355 	res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, attrbuf);
10356 	if (res) {
10357 		kfree(attrbuf);
10358 		return res;
10359 	}
10360 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10361 	__acquire(&rdev->wiphy.mtx);
10362 
10363 	/* prepare_wdev_dump parsed the attributes */
10364 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
10365 
10366 	if (!wdev->netdev) {
10367 		res = -EINVAL;
10368 		goto out_err;
10369 	}
10370 
10371 	if (!rdev->ops->dump_survey) {
10372 		res = -EOPNOTSUPP;
10373 		goto out_err;
10374 	}
10375 
10376 	while (1) {
10377 		wdev_lock(wdev);
10378 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
10379 		wdev_unlock(wdev);
10380 		if (res == -ENOENT)
10381 			break;
10382 		if (res)
10383 			goto out_err;
10384 
10385 		/* don't send disabled channels, but do send non-channel data */
10386 		if (survey.channel &&
10387 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
10388 			survey_idx++;
10389 			continue;
10390 		}
10391 
10392 		if (nl80211_send_survey(skb,
10393 				NETLINK_CB(cb->skb).portid,
10394 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10395 				wdev->netdev, radio_stats, &survey) < 0)
10396 			goto out;
10397 		survey_idx++;
10398 	}
10399 
10400  out:
10401 	cb->args[2] = survey_idx;
10402 	res = skb->len;
10403  out_err:
10404 	kfree(attrbuf);
10405 	wiphy_unlock(&rdev->wiphy);
10406 	return res;
10407 }
10408 
10409 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
10410 {
10411 	return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
10412 				  NL80211_WPA_VERSION_2 |
10413 				  NL80211_WPA_VERSION_3));
10414 }
10415 
10416 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
10417 {
10418 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10419 	struct net_device *dev = info->user_ptr[1];
10420 	struct ieee80211_channel *chan;
10421 	const u8 *bssid, *ssid;
10422 	int err, ssid_len;
10423 	enum nl80211_auth_type auth_type;
10424 	struct key_parse key;
10425 	bool local_state_change;
10426 	struct cfg80211_auth_request req = {};
10427 	u32 freq;
10428 
10429 	if (!info->attrs[NL80211_ATTR_MAC])
10430 		return -EINVAL;
10431 
10432 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
10433 		return -EINVAL;
10434 
10435 	if (!info->attrs[NL80211_ATTR_SSID])
10436 		return -EINVAL;
10437 
10438 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10439 		return -EINVAL;
10440 
10441 	err = nl80211_parse_key(info, &key);
10442 	if (err)
10443 		return err;
10444 
10445 	if (key.idx >= 0) {
10446 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
10447 			return -EINVAL;
10448 		if (!key.p.key || !key.p.key_len)
10449 			return -EINVAL;
10450 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
10451 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
10452 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
10453 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
10454 			return -EINVAL;
10455 		if (key.idx > 3)
10456 			return -EINVAL;
10457 	} else {
10458 		key.p.key_len = 0;
10459 		key.p.key = NULL;
10460 	}
10461 
10462 	if (key.idx >= 0) {
10463 		int i;
10464 		bool ok = false;
10465 
10466 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
10467 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
10468 				ok = true;
10469 				break;
10470 			}
10471 		}
10472 		if (!ok)
10473 			return -EINVAL;
10474 	}
10475 
10476 	if (!rdev->ops->auth)
10477 		return -EOPNOTSUPP;
10478 
10479 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10480 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10481 		return -EOPNOTSUPP;
10482 
10483 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10484 	freq = MHZ_TO_KHZ(nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
10485 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10486 		freq +=
10487 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10488 
10489 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10490 	if (!chan)
10491 		return -EINVAL;
10492 
10493 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10494 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10495 
10496 	if (info->attrs[NL80211_ATTR_IE]) {
10497 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10498 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10499 	}
10500 
10501 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10502 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
10503 		return -EINVAL;
10504 
10505 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
10506 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
10507 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
10508 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
10509 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
10510 		return -EINVAL;
10511 
10512 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
10513 		if (auth_type != NL80211_AUTHTYPE_SAE &&
10514 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
10515 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
10516 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
10517 			return -EINVAL;
10518 		req.auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
10519 		req.auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
10520 	}
10521 
10522 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
10523 
10524 	/*
10525 	 * Since we no longer track auth state, ignore
10526 	 * requests to only change local state.
10527 	 */
10528 	if (local_state_change)
10529 		return 0;
10530 
10531 	req.auth_type = auth_type;
10532 	req.key = key.p.key;
10533 	req.key_len = key.p.key_len;
10534 	req.key_idx = key.idx;
10535 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
10536 	if (req.link_id >= 0) {
10537 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
10538 			return -EINVAL;
10539 		if (!info->attrs[NL80211_ATTR_MLD_ADDR])
10540 			return -EINVAL;
10541 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
10542 	}
10543 
10544 	req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
10545 				   IEEE80211_BSS_TYPE_ESS,
10546 				   IEEE80211_PRIVACY_ANY);
10547 	if (!req.bss)
10548 		return -ENOENT;
10549 
10550 	wdev_lock(dev->ieee80211_ptr);
10551 	err = cfg80211_mlme_auth(rdev, dev, &req);
10552 	wdev_unlock(dev->ieee80211_ptr);
10553 
10554 	cfg80211_put_bss(&rdev->wiphy, req.bss);
10555 
10556 	return err;
10557 }
10558 
10559 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
10560 				     struct genl_info *info)
10561 {
10562 	if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10563 		GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
10564 		return -EINVAL;
10565 	}
10566 
10567 	if (!rdev->ops->tx_control_port ||
10568 	    !wiphy_ext_feature_isset(&rdev->wiphy,
10569 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
10570 		return -EOPNOTSUPP;
10571 
10572 	return 0;
10573 }
10574 
10575 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
10576 				   struct genl_info *info,
10577 				   struct cfg80211_crypto_settings *settings,
10578 				   int cipher_limit)
10579 {
10580 	memset(settings, 0, sizeof(*settings));
10581 
10582 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
10583 
10584 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
10585 		u16 proto;
10586 
10587 		proto = nla_get_u16(
10588 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
10589 		settings->control_port_ethertype = cpu_to_be16(proto);
10590 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
10591 		    proto != ETH_P_PAE)
10592 			return -EINVAL;
10593 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
10594 			settings->control_port_no_encrypt = true;
10595 	} else
10596 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
10597 
10598 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
10599 		int r = validate_pae_over_nl80211(rdev, info);
10600 
10601 		if (r < 0)
10602 			return r;
10603 
10604 		settings->control_port_over_nl80211 = true;
10605 
10606 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH])
10607 			settings->control_port_no_preauth = true;
10608 	}
10609 
10610 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
10611 		void *data;
10612 		int len, i;
10613 
10614 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10615 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10616 		settings->n_ciphers_pairwise = len / sizeof(u32);
10617 
10618 		if (len % sizeof(u32))
10619 			return -EINVAL;
10620 
10621 		if (settings->n_ciphers_pairwise > cipher_limit)
10622 			return -EINVAL;
10623 
10624 		memcpy(settings->ciphers_pairwise, data, len);
10625 
10626 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
10627 			if (!cfg80211_supported_cipher_suite(
10628 					&rdev->wiphy,
10629 					settings->ciphers_pairwise[i]))
10630 				return -EINVAL;
10631 	}
10632 
10633 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
10634 		settings->cipher_group =
10635 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
10636 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
10637 						     settings->cipher_group))
10638 			return -EINVAL;
10639 	}
10640 
10641 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
10642 		settings->wpa_versions =
10643 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
10644 		if (!nl80211_valid_wpa_versions(settings->wpa_versions))
10645 			return -EINVAL;
10646 	}
10647 
10648 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
10649 		void *data;
10650 		int len;
10651 
10652 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
10653 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
10654 		settings->n_akm_suites = len / sizeof(u32);
10655 
10656 		if (len % sizeof(u32))
10657 			return -EINVAL;
10658 
10659 		if (settings->n_akm_suites > rdev->wiphy.max_num_akm_suites)
10660 			return -EINVAL;
10661 
10662 		memcpy(settings->akm_suites, data, len);
10663 	}
10664 
10665 	if (info->attrs[NL80211_ATTR_PMK]) {
10666 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
10667 			return -EINVAL;
10668 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10669 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK) &&
10670 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10671 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_AP_PSK))
10672 			return -EINVAL;
10673 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10674 	}
10675 
10676 	if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
10677 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10678 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
10679 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10680 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP))
10681 			return -EINVAL;
10682 		settings->sae_pwd =
10683 			nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10684 		settings->sae_pwd_len =
10685 			nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10686 	}
10687 
10688 	if (info->attrs[NL80211_ATTR_SAE_PWE])
10689 		settings->sae_pwe =
10690 			nla_get_u8(info->attrs[NL80211_ATTR_SAE_PWE]);
10691 	else
10692 		settings->sae_pwe = NL80211_SAE_PWE_UNSPECIFIED;
10693 
10694 	return 0;
10695 }
10696 
10697 static struct cfg80211_bss *nl80211_assoc_bss(struct cfg80211_registered_device *rdev,
10698 					      const u8 *ssid, int ssid_len,
10699 					      struct nlattr **attrs,
10700 					      const u8 **bssid_out)
10701 {
10702 	struct ieee80211_channel *chan;
10703 	struct cfg80211_bss *bss;
10704 	const u8 *bssid;
10705 	u32 freq;
10706 
10707 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_WIPHY_FREQ])
10708 		return ERR_PTR(-EINVAL);
10709 
10710 	bssid = nla_data(attrs[NL80211_ATTR_MAC]);
10711 
10712 	freq = MHZ_TO_KHZ(nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ]));
10713 	if (attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10714 		freq += nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10715 
10716 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10717 	if (!chan)
10718 		return ERR_PTR(-EINVAL);
10719 
10720 	bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid,
10721 			       ssid, ssid_len,
10722 			       IEEE80211_BSS_TYPE_ESS,
10723 			       IEEE80211_PRIVACY_ANY);
10724 	if (!bss)
10725 		return ERR_PTR(-ENOENT);
10726 
10727 	*bssid_out = bssid;
10728 	return bss;
10729 }
10730 
10731 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
10732 {
10733 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10734 	struct net_device *dev = info->user_ptr[1];
10735 	struct cfg80211_assoc_request req = {};
10736 	struct nlattr **attrs = NULL;
10737 	const u8 *bssid, *ssid;
10738 	unsigned int link_id;
10739 	int err, ssid_len;
10740 
10741 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
10742 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10743 		return -EPERM;
10744 
10745 	if (!info->attrs[NL80211_ATTR_SSID])
10746 		return -EINVAL;
10747 
10748 	if (!rdev->ops->assoc)
10749 		return -EOPNOTSUPP;
10750 
10751 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10752 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10753 		return -EOPNOTSUPP;
10754 
10755 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10756 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10757 
10758 	if (info->attrs[NL80211_ATTR_IE]) {
10759 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10760 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10761 
10762 		if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
10763 					   req.ie, req.ie_len)) {
10764 			GENL_SET_ERR_MSG(info,
10765 					 "non-inheritance makes no sense");
10766 			return -EINVAL;
10767 		}
10768 	}
10769 
10770 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
10771 		enum nl80211_mfp mfp =
10772 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
10773 		if (mfp == NL80211_MFP_REQUIRED)
10774 			req.use_mfp = true;
10775 		else if (mfp != NL80211_MFP_NO)
10776 			return -EINVAL;
10777 	}
10778 
10779 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
10780 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
10781 
10782 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
10783 		req.flags |= ASSOC_REQ_DISABLE_HT;
10784 
10785 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10786 		memcpy(&req.ht_capa_mask,
10787 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
10788 		       sizeof(req.ht_capa_mask));
10789 
10790 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
10791 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10792 			return -EINVAL;
10793 		memcpy(&req.ht_capa,
10794 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
10795 		       sizeof(req.ht_capa));
10796 	}
10797 
10798 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
10799 		req.flags |= ASSOC_REQ_DISABLE_VHT;
10800 
10801 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
10802 		req.flags |= ASSOC_REQ_DISABLE_HE;
10803 
10804 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
10805 		req.flags |= ASSOC_REQ_DISABLE_EHT;
10806 
10807 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10808 		memcpy(&req.vht_capa_mask,
10809 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
10810 		       sizeof(req.vht_capa_mask));
10811 
10812 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
10813 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10814 			return -EINVAL;
10815 		memcpy(&req.vht_capa,
10816 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
10817 		       sizeof(req.vht_capa));
10818 	}
10819 
10820 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
10821 		if (!((rdev->wiphy.features &
10822 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
10823 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
10824 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10825 					     NL80211_EXT_FEATURE_RRM))
10826 			return -EINVAL;
10827 		req.flags |= ASSOC_REQ_USE_RRM;
10828 	}
10829 
10830 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
10831 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
10832 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
10833 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
10834 			return -EINVAL;
10835 		req.fils_nonces =
10836 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
10837 	}
10838 
10839 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]) {
10840 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY])
10841 			return -EINVAL;
10842 		memcpy(&req.s1g_capa_mask,
10843 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]),
10844 		       sizeof(req.s1g_capa_mask));
10845 	}
10846 
10847 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY]) {
10848 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK])
10849 			return -EINVAL;
10850 		memcpy(&req.s1g_capa,
10851 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY]),
10852 		       sizeof(req.s1g_capa));
10853 	}
10854 
10855 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
10856 
10857 	if (info->attrs[NL80211_ATTR_MLO_LINKS]) {
10858 		unsigned int attrsize = NUM_NL80211_ATTR * sizeof(*attrs);
10859 		struct nlattr *link;
10860 		int rem = 0;
10861 
10862 		if (req.link_id < 0)
10863 			return -EINVAL;
10864 
10865 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
10866 			return -EINVAL;
10867 
10868 		if (info->attrs[NL80211_ATTR_MAC] ||
10869 		    info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
10870 		    !info->attrs[NL80211_ATTR_MLD_ADDR])
10871 			return -EINVAL;
10872 
10873 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
10874 
10875 		attrs = kzalloc(attrsize, GFP_KERNEL);
10876 		if (!attrs)
10877 			return -ENOMEM;
10878 
10879 		nla_for_each_nested(link,
10880 				    info->attrs[NL80211_ATTR_MLO_LINKS],
10881 				    rem) {
10882 			memset(attrs, 0, attrsize);
10883 
10884 			nla_parse_nested(attrs, NL80211_ATTR_MAX,
10885 					 link, NULL, NULL);
10886 
10887 			if (!attrs[NL80211_ATTR_MLO_LINK_ID]) {
10888 				err = -EINVAL;
10889 				goto free;
10890 			}
10891 
10892 			link_id = nla_get_u8(attrs[NL80211_ATTR_MLO_LINK_ID]);
10893 			/* cannot use the same link ID again */
10894 			if (req.links[link_id].bss) {
10895 				err = -EINVAL;
10896 				goto free;
10897 			}
10898 			req.links[link_id].bss =
10899 				nl80211_assoc_bss(rdev, ssid, ssid_len, attrs,
10900 						  &bssid);
10901 			if (IS_ERR(req.links[link_id].bss)) {
10902 				err = PTR_ERR(req.links[link_id].bss);
10903 				req.links[link_id].bss = NULL;
10904 				goto free;
10905 			}
10906 
10907 			if (attrs[NL80211_ATTR_IE]) {
10908 				req.links[link_id].elems =
10909 					nla_data(attrs[NL80211_ATTR_IE]);
10910 				req.links[link_id].elems_len =
10911 					nla_len(attrs[NL80211_ATTR_IE]);
10912 
10913 				if (cfg80211_find_elem(WLAN_EID_FRAGMENT,
10914 						       req.links[link_id].elems,
10915 						       req.links[link_id].elems_len)) {
10916 					GENL_SET_ERR_MSG(info,
10917 							 "cannot deal with fragmentation");
10918 					err = -EINVAL;
10919 					goto free;
10920 				}
10921 
10922 				if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
10923 							   req.links[link_id].elems,
10924 							   req.links[link_id].elems_len)) {
10925 					GENL_SET_ERR_MSG(info,
10926 							 "cannot deal with non-inheritance");
10927 					err = -EINVAL;
10928 					goto free;
10929 				}
10930 			}
10931 		}
10932 
10933 		if (!req.links[req.link_id].bss) {
10934 			err = -EINVAL;
10935 			goto free;
10936 		}
10937 
10938 		if (req.links[req.link_id].elems_len) {
10939 			GENL_SET_ERR_MSG(info,
10940 					 "cannot have per-link elems on assoc link");
10941 			err = -EINVAL;
10942 			goto free;
10943 		}
10944 
10945 		kfree(attrs);
10946 		attrs = NULL;
10947 	} else {
10948 		if (req.link_id >= 0)
10949 			return -EINVAL;
10950 
10951 		req.bss = nl80211_assoc_bss(rdev, ssid, ssid_len, info->attrs,
10952 					    &bssid);
10953 		if (IS_ERR(req.bss))
10954 			return PTR_ERR(req.bss);
10955 	}
10956 
10957 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
10958 	if (!err) {
10959 		wdev_lock(dev->ieee80211_ptr);
10960 
10961 		err = cfg80211_mlme_assoc(rdev, dev, &req);
10962 
10963 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10964 			dev->ieee80211_ptr->conn_owner_nlportid =
10965 				info->snd_portid;
10966 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
10967 			       bssid, ETH_ALEN);
10968 		}
10969 
10970 		wdev_unlock(dev->ieee80211_ptr);
10971 	}
10972 
10973 free:
10974 	for (link_id = 0; link_id < ARRAY_SIZE(req.links); link_id++)
10975 		cfg80211_put_bss(&rdev->wiphy, req.links[link_id].bss);
10976 	cfg80211_put_bss(&rdev->wiphy, req.bss);
10977 	kfree(attrs);
10978 
10979 	return err;
10980 }
10981 
10982 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
10983 {
10984 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10985 	struct net_device *dev = info->user_ptr[1];
10986 	const u8 *ie = NULL, *bssid;
10987 	int ie_len = 0, err;
10988 	u16 reason_code;
10989 	bool local_state_change;
10990 
10991 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
10992 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10993 		return -EPERM;
10994 
10995 	if (!info->attrs[NL80211_ATTR_MAC])
10996 		return -EINVAL;
10997 
10998 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
10999 		return -EINVAL;
11000 
11001 	if (!rdev->ops->deauth)
11002 		return -EOPNOTSUPP;
11003 
11004 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11005 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11006 		return -EOPNOTSUPP;
11007 
11008 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11009 
11010 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11011 	if (reason_code == 0) {
11012 		/* Reason Code 0 is reserved */
11013 		return -EINVAL;
11014 	}
11015 
11016 	if (info->attrs[NL80211_ATTR_IE]) {
11017 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11018 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11019 	}
11020 
11021 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11022 
11023 	wdev_lock(dev->ieee80211_ptr);
11024 	err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
11025 				   local_state_change);
11026 	wdev_unlock(dev->ieee80211_ptr);
11027 	return err;
11028 }
11029 
11030 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
11031 {
11032 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11033 	struct net_device *dev = info->user_ptr[1];
11034 	const u8 *ie = NULL, *bssid;
11035 	int ie_len = 0, err;
11036 	u16 reason_code;
11037 	bool local_state_change;
11038 
11039 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11040 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11041 		return -EPERM;
11042 
11043 	if (!info->attrs[NL80211_ATTR_MAC])
11044 		return -EINVAL;
11045 
11046 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11047 		return -EINVAL;
11048 
11049 	if (!rdev->ops->disassoc)
11050 		return -EOPNOTSUPP;
11051 
11052 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11053 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11054 		return -EOPNOTSUPP;
11055 
11056 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11057 
11058 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11059 	if (reason_code == 0) {
11060 		/* Reason Code 0 is reserved */
11061 		return -EINVAL;
11062 	}
11063 
11064 	if (info->attrs[NL80211_ATTR_IE]) {
11065 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11066 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11067 	}
11068 
11069 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11070 
11071 	wdev_lock(dev->ieee80211_ptr);
11072 	err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
11073 				     local_state_change);
11074 	wdev_unlock(dev->ieee80211_ptr);
11075 	return err;
11076 }
11077 
11078 static bool
11079 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
11080 			 int mcast_rate[NUM_NL80211_BANDS],
11081 			 int rateval)
11082 {
11083 	struct wiphy *wiphy = &rdev->wiphy;
11084 	bool found = false;
11085 	int band, i;
11086 
11087 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
11088 		struct ieee80211_supported_band *sband;
11089 
11090 		sband = wiphy->bands[band];
11091 		if (!sband)
11092 			continue;
11093 
11094 		for (i = 0; i < sband->n_bitrates; i++) {
11095 			if (sband->bitrates[i].bitrate == rateval) {
11096 				mcast_rate[band] = i + 1;
11097 				found = true;
11098 				break;
11099 			}
11100 		}
11101 	}
11102 
11103 	return found;
11104 }
11105 
11106 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
11107 {
11108 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11109 	struct net_device *dev = info->user_ptr[1];
11110 	struct cfg80211_ibss_params ibss;
11111 	struct wiphy *wiphy;
11112 	struct cfg80211_cached_keys *connkeys = NULL;
11113 	int err;
11114 
11115 	memset(&ibss, 0, sizeof(ibss));
11116 
11117 	if (!info->attrs[NL80211_ATTR_SSID] ||
11118 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11119 		return -EINVAL;
11120 
11121 	ibss.beacon_interval = 100;
11122 
11123 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
11124 		ibss.beacon_interval =
11125 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11126 
11127 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
11128 					   ibss.beacon_interval);
11129 	if (err)
11130 		return err;
11131 
11132 	if (!rdev->ops->join_ibss)
11133 		return -EOPNOTSUPP;
11134 
11135 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11136 		return -EOPNOTSUPP;
11137 
11138 	wiphy = &rdev->wiphy;
11139 
11140 	if (info->attrs[NL80211_ATTR_MAC]) {
11141 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11142 
11143 		if (!is_valid_ether_addr(ibss.bssid))
11144 			return -EINVAL;
11145 	}
11146 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11147 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11148 
11149 	if (info->attrs[NL80211_ATTR_IE]) {
11150 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11151 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11152 	}
11153 
11154 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
11155 	if (err)
11156 		return err;
11157 
11158 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
11159 				     NL80211_IFTYPE_ADHOC))
11160 		return -EINVAL;
11161 
11162 	switch (ibss.chandef.width) {
11163 	case NL80211_CHAN_WIDTH_5:
11164 	case NL80211_CHAN_WIDTH_10:
11165 	case NL80211_CHAN_WIDTH_20_NOHT:
11166 		break;
11167 	case NL80211_CHAN_WIDTH_20:
11168 	case NL80211_CHAN_WIDTH_40:
11169 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11170 			return -EINVAL;
11171 		break;
11172 	case NL80211_CHAN_WIDTH_80:
11173 	case NL80211_CHAN_WIDTH_80P80:
11174 	case NL80211_CHAN_WIDTH_160:
11175 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11176 			return -EINVAL;
11177 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
11178 					     NL80211_EXT_FEATURE_VHT_IBSS))
11179 			return -EINVAL;
11180 		break;
11181 	case NL80211_CHAN_WIDTH_320:
11182 		return -EINVAL;
11183 	default:
11184 		return -EINVAL;
11185 	}
11186 
11187 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
11188 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
11189 
11190 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11191 		u8 *rates =
11192 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11193 		int n_rates =
11194 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11195 		struct ieee80211_supported_band *sband =
11196 			wiphy->bands[ibss.chandef.chan->band];
11197 
11198 		err = ieee80211_get_ratemask(sband, rates, n_rates,
11199 					     &ibss.basic_rates);
11200 		if (err)
11201 			return err;
11202 	}
11203 
11204 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11205 		memcpy(&ibss.ht_capa_mask,
11206 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11207 		       sizeof(ibss.ht_capa_mask));
11208 
11209 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11210 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11211 			return -EINVAL;
11212 		memcpy(&ibss.ht_capa,
11213 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11214 		       sizeof(ibss.ht_capa));
11215 	}
11216 
11217 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11218 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
11219 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11220 		return -EINVAL;
11221 
11222 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11223 		bool no_ht = false;
11224 
11225 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
11226 		if (IS_ERR(connkeys))
11227 			return PTR_ERR(connkeys);
11228 
11229 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
11230 		    no_ht) {
11231 			kfree_sensitive(connkeys);
11232 			return -EINVAL;
11233 		}
11234 	}
11235 
11236 	ibss.control_port =
11237 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
11238 
11239 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11240 		int r = validate_pae_over_nl80211(rdev, info);
11241 
11242 		if (r < 0) {
11243 			kfree_sensitive(connkeys);
11244 			return r;
11245 		}
11246 
11247 		ibss.control_port_over_nl80211 = true;
11248 	}
11249 
11250 	ibss.userspace_handles_dfs =
11251 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11252 
11253 	wdev_lock(dev->ieee80211_ptr);
11254 	err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
11255 	if (err)
11256 		kfree_sensitive(connkeys);
11257 	else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
11258 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11259 	wdev_unlock(dev->ieee80211_ptr);
11260 
11261 	return err;
11262 }
11263 
11264 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
11265 {
11266 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11267 	struct net_device *dev = info->user_ptr[1];
11268 
11269 	if (!rdev->ops->leave_ibss)
11270 		return -EOPNOTSUPP;
11271 
11272 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11273 		return -EOPNOTSUPP;
11274 
11275 	return cfg80211_leave_ibss(rdev, dev, false);
11276 }
11277 
11278 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
11279 {
11280 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11281 	struct net_device *dev = info->user_ptr[1];
11282 	int mcast_rate[NUM_NL80211_BANDS];
11283 	u32 nla_rate;
11284 
11285 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
11286 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
11287 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
11288 		return -EOPNOTSUPP;
11289 
11290 	if (!rdev->ops->set_mcast_rate)
11291 		return -EOPNOTSUPP;
11292 
11293 	memset(mcast_rate, 0, sizeof(mcast_rate));
11294 
11295 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
11296 		return -EINVAL;
11297 
11298 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
11299 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
11300 		return -EINVAL;
11301 
11302 	return rdev_set_mcast_rate(rdev, dev, mcast_rate);
11303 }
11304 
11305 static struct sk_buff *
11306 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
11307 			    struct wireless_dev *wdev, int approxlen,
11308 			    u32 portid, u32 seq, enum nl80211_commands cmd,
11309 			    enum nl80211_attrs attr,
11310 			    const struct nl80211_vendor_cmd_info *info,
11311 			    gfp_t gfp)
11312 {
11313 	struct sk_buff *skb;
11314 	void *hdr;
11315 	struct nlattr *data;
11316 
11317 	skb = nlmsg_new(approxlen + 100, gfp);
11318 	if (!skb)
11319 		return NULL;
11320 
11321 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
11322 	if (!hdr) {
11323 		kfree_skb(skb);
11324 		return NULL;
11325 	}
11326 
11327 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11328 		goto nla_put_failure;
11329 
11330 	if (info) {
11331 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
11332 				info->vendor_id))
11333 			goto nla_put_failure;
11334 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
11335 				info->subcmd))
11336 			goto nla_put_failure;
11337 	}
11338 
11339 	if (wdev) {
11340 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
11341 				      wdev_id(wdev), NL80211_ATTR_PAD))
11342 			goto nla_put_failure;
11343 		if (wdev->netdev &&
11344 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
11345 				wdev->netdev->ifindex))
11346 			goto nla_put_failure;
11347 	}
11348 
11349 	data = nla_nest_start_noflag(skb, attr);
11350 	if (!data)
11351 		goto nla_put_failure;
11352 
11353 	((void **)skb->cb)[0] = rdev;
11354 	((void **)skb->cb)[1] = hdr;
11355 	((void **)skb->cb)[2] = data;
11356 
11357 	return skb;
11358 
11359  nla_put_failure:
11360 	kfree_skb(skb);
11361 	return NULL;
11362 }
11363 
11364 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
11365 					   struct wireless_dev *wdev,
11366 					   enum nl80211_commands cmd,
11367 					   enum nl80211_attrs attr,
11368 					   unsigned int portid,
11369 					   int vendor_event_idx,
11370 					   int approxlen, gfp_t gfp)
11371 {
11372 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11373 	const struct nl80211_vendor_cmd_info *info;
11374 
11375 	switch (cmd) {
11376 	case NL80211_CMD_TESTMODE:
11377 		if (WARN_ON(vendor_event_idx != -1))
11378 			return NULL;
11379 		info = NULL;
11380 		break;
11381 	case NL80211_CMD_VENDOR:
11382 		if (WARN_ON(vendor_event_idx < 0 ||
11383 			    vendor_event_idx >= wiphy->n_vendor_events))
11384 			return NULL;
11385 		info = &wiphy->vendor_events[vendor_event_idx];
11386 		break;
11387 	default:
11388 		WARN_ON(1);
11389 		return NULL;
11390 	}
11391 
11392 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
11393 					   cmd, attr, info, gfp);
11394 }
11395 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
11396 
11397 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
11398 {
11399 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
11400 	void *hdr = ((void **)skb->cb)[1];
11401 	struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
11402 	struct nlattr *data = ((void **)skb->cb)[2];
11403 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
11404 
11405 	/* clear CB data for netlink core to own from now on */
11406 	memset(skb->cb, 0, sizeof(skb->cb));
11407 
11408 	nla_nest_end(skb, data);
11409 	genlmsg_end(skb, hdr);
11410 
11411 	if (nlhdr->nlmsg_pid) {
11412 		genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
11413 				nlhdr->nlmsg_pid);
11414 	} else {
11415 		if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
11416 			mcgrp = NL80211_MCGRP_VENDOR;
11417 
11418 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11419 					skb, 0, mcgrp, gfp);
11420 	}
11421 }
11422 EXPORT_SYMBOL(__cfg80211_send_event_skb);
11423 
11424 #ifdef CONFIG_NL80211_TESTMODE
11425 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
11426 {
11427 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11428 	struct wireless_dev *wdev;
11429 	int err;
11430 
11431 	lockdep_assert_held(&rdev->wiphy.mtx);
11432 
11433 	wdev = __cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
11434 					  info->attrs);
11435 
11436 	if (!rdev->ops->testmode_cmd)
11437 		return -EOPNOTSUPP;
11438 
11439 	if (IS_ERR(wdev)) {
11440 		err = PTR_ERR(wdev);
11441 		if (err != -EINVAL)
11442 			return err;
11443 		wdev = NULL;
11444 	} else if (wdev->wiphy != &rdev->wiphy) {
11445 		return -EINVAL;
11446 	}
11447 
11448 	if (!info->attrs[NL80211_ATTR_TESTDATA])
11449 		return -EINVAL;
11450 
11451 	rdev->cur_cmd_info = info;
11452 	err = rdev_testmode_cmd(rdev, wdev,
11453 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
11454 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
11455 	rdev->cur_cmd_info = NULL;
11456 
11457 	return err;
11458 }
11459 
11460 static int nl80211_testmode_dump(struct sk_buff *skb,
11461 				 struct netlink_callback *cb)
11462 {
11463 	struct cfg80211_registered_device *rdev;
11464 	struct nlattr **attrbuf = NULL;
11465 	int err;
11466 	long phy_idx;
11467 	void *data = NULL;
11468 	int data_len = 0;
11469 
11470 	rtnl_lock();
11471 
11472 	if (cb->args[0]) {
11473 		/*
11474 		 * 0 is a valid index, but not valid for args[0],
11475 		 * so we need to offset by 1.
11476 		 */
11477 		phy_idx = cb->args[0] - 1;
11478 
11479 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
11480 		if (!rdev) {
11481 			err = -ENOENT;
11482 			goto out_err;
11483 		}
11484 	} else {
11485 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
11486 				  GFP_KERNEL);
11487 		if (!attrbuf) {
11488 			err = -ENOMEM;
11489 			goto out_err;
11490 		}
11491 
11492 		err = nlmsg_parse_deprecated(cb->nlh,
11493 					     GENL_HDRLEN + nl80211_fam.hdrsize,
11494 					     attrbuf, nl80211_fam.maxattr,
11495 					     nl80211_policy, NULL);
11496 		if (err)
11497 			goto out_err;
11498 
11499 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
11500 		if (IS_ERR(rdev)) {
11501 			err = PTR_ERR(rdev);
11502 			goto out_err;
11503 		}
11504 		phy_idx = rdev->wiphy_idx;
11505 
11506 		if (attrbuf[NL80211_ATTR_TESTDATA])
11507 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
11508 	}
11509 
11510 	if (cb->args[1]) {
11511 		data = nla_data((void *)cb->args[1]);
11512 		data_len = nla_len((void *)cb->args[1]);
11513 	}
11514 
11515 	if (!rdev->ops->testmode_dump) {
11516 		err = -EOPNOTSUPP;
11517 		goto out_err;
11518 	}
11519 
11520 	while (1) {
11521 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
11522 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
11523 					   NL80211_CMD_TESTMODE);
11524 		struct nlattr *tmdata;
11525 
11526 		if (!hdr)
11527 			break;
11528 
11529 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
11530 			genlmsg_cancel(skb, hdr);
11531 			break;
11532 		}
11533 
11534 		tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
11535 		if (!tmdata) {
11536 			genlmsg_cancel(skb, hdr);
11537 			break;
11538 		}
11539 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
11540 		nla_nest_end(skb, tmdata);
11541 
11542 		if (err == -ENOBUFS || err == -ENOENT) {
11543 			genlmsg_cancel(skb, hdr);
11544 			break;
11545 		} else if (err) {
11546 			genlmsg_cancel(skb, hdr);
11547 			goto out_err;
11548 		}
11549 
11550 		genlmsg_end(skb, hdr);
11551 	}
11552 
11553 	err = skb->len;
11554 	/* see above */
11555 	cb->args[0] = phy_idx + 1;
11556  out_err:
11557 	kfree(attrbuf);
11558 	rtnl_unlock();
11559 	return err;
11560 }
11561 #endif
11562 
11563 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
11564 {
11565 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11566 	struct net_device *dev = info->user_ptr[1];
11567 	struct cfg80211_connect_params connect;
11568 	struct wiphy *wiphy;
11569 	struct cfg80211_cached_keys *connkeys = NULL;
11570 	u32 freq = 0;
11571 	int err;
11572 
11573 	memset(&connect, 0, sizeof(connect));
11574 
11575 	if (!info->attrs[NL80211_ATTR_SSID] ||
11576 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11577 		return -EINVAL;
11578 
11579 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
11580 		connect.auth_type =
11581 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
11582 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
11583 					     NL80211_CMD_CONNECT))
11584 			return -EINVAL;
11585 	} else
11586 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
11587 
11588 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
11589 
11590 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
11591 	    !wiphy_ext_feature_isset(&rdev->wiphy,
11592 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
11593 		return -EINVAL;
11594 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
11595 
11596 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
11597 				      NL80211_MAX_NR_CIPHER_SUITES);
11598 	if (err)
11599 		return err;
11600 
11601 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11602 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11603 		return -EOPNOTSUPP;
11604 
11605 	wiphy = &rdev->wiphy;
11606 
11607 	connect.bg_scan_period = -1;
11608 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
11609 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
11610 		connect.bg_scan_period =
11611 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
11612 	}
11613 
11614 	if (info->attrs[NL80211_ATTR_MAC])
11615 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11616 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
11617 		connect.bssid_hint =
11618 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
11619 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11620 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11621 
11622 	if (info->attrs[NL80211_ATTR_IE]) {
11623 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11624 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11625 	}
11626 
11627 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
11628 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
11629 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
11630 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11631 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
11632 			return -EOPNOTSUPP;
11633 	} else {
11634 		connect.mfp = NL80211_MFP_NO;
11635 	}
11636 
11637 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
11638 		connect.prev_bssid =
11639 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
11640 
11641 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ])
11642 		freq = MHZ_TO_KHZ(nla_get_u32(
11643 					info->attrs[NL80211_ATTR_WIPHY_FREQ]));
11644 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
11645 		freq +=
11646 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
11647 
11648 	if (freq) {
11649 		connect.channel = nl80211_get_valid_chan(wiphy, freq);
11650 		if (!connect.channel)
11651 			return -EINVAL;
11652 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
11653 		freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
11654 		freq = MHZ_TO_KHZ(freq);
11655 		connect.channel_hint = nl80211_get_valid_chan(wiphy, freq);
11656 		if (!connect.channel_hint)
11657 			return -EINVAL;
11658 	}
11659 
11660 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
11661 		connect.edmg.channels =
11662 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
11663 
11664 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
11665 			connect.edmg.bw_config =
11666 				nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
11667 	}
11668 
11669 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11670 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
11671 		if (IS_ERR(connkeys))
11672 			return PTR_ERR(connkeys);
11673 	}
11674 
11675 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
11676 		connect.flags |= ASSOC_REQ_DISABLE_HT;
11677 
11678 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11679 		memcpy(&connect.ht_capa_mask,
11680 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11681 		       sizeof(connect.ht_capa_mask));
11682 
11683 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11684 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
11685 			kfree_sensitive(connkeys);
11686 			return -EINVAL;
11687 		}
11688 		memcpy(&connect.ht_capa,
11689 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11690 		       sizeof(connect.ht_capa));
11691 	}
11692 
11693 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
11694 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
11695 
11696 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
11697 		connect.flags |= ASSOC_REQ_DISABLE_HE;
11698 
11699 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
11700 		connect.flags |= ASSOC_REQ_DISABLE_EHT;
11701 
11702 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11703 		memcpy(&connect.vht_capa_mask,
11704 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
11705 		       sizeof(connect.vht_capa_mask));
11706 
11707 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
11708 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
11709 			kfree_sensitive(connkeys);
11710 			return -EINVAL;
11711 		}
11712 		memcpy(&connect.vht_capa,
11713 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
11714 		       sizeof(connect.vht_capa));
11715 	}
11716 
11717 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
11718 		if (!((rdev->wiphy.features &
11719 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
11720 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
11721 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11722 					     NL80211_EXT_FEATURE_RRM)) {
11723 			kfree_sensitive(connkeys);
11724 			return -EINVAL;
11725 		}
11726 		connect.flags |= ASSOC_REQ_USE_RRM;
11727 	}
11728 
11729 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
11730 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
11731 		kfree_sensitive(connkeys);
11732 		return -EOPNOTSUPP;
11733 	}
11734 
11735 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
11736 		/* bss selection makes no sense if bssid is set */
11737 		if (connect.bssid) {
11738 			kfree_sensitive(connkeys);
11739 			return -EINVAL;
11740 		}
11741 
11742 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
11743 				       wiphy, &connect.bss_select);
11744 		if (err) {
11745 			kfree_sensitive(connkeys);
11746 			return err;
11747 		}
11748 	}
11749 
11750 	if (wiphy_ext_feature_isset(&rdev->wiphy,
11751 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
11752 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
11753 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
11754 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
11755 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11756 		connect.fils_erp_username =
11757 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11758 		connect.fils_erp_username_len =
11759 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11760 		connect.fils_erp_realm =
11761 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11762 		connect.fils_erp_realm_len =
11763 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11764 		connect.fils_erp_next_seq_num =
11765 			nla_get_u16(
11766 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
11767 		connect.fils_erp_rrk =
11768 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
11769 		connect.fils_erp_rrk_len =
11770 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
11771 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
11772 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
11773 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
11774 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11775 		kfree_sensitive(connkeys);
11776 		return -EINVAL;
11777 	}
11778 
11779 	if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
11780 		if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
11781 			kfree_sensitive(connkeys);
11782 			GENL_SET_ERR_MSG(info,
11783 					 "external auth requires connection ownership");
11784 			return -EINVAL;
11785 		}
11786 		connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
11787 	}
11788 
11789 	if (nla_get_flag(info->attrs[NL80211_ATTR_MLO_SUPPORT]))
11790 		connect.flags |= CONNECT_REQ_MLO_SUPPORT;
11791 
11792 	wdev_lock(dev->ieee80211_ptr);
11793 
11794 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
11795 			       connect.prev_bssid);
11796 	if (err)
11797 		kfree_sensitive(connkeys);
11798 
11799 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
11800 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11801 		if (connect.bssid)
11802 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
11803 			       connect.bssid, ETH_ALEN);
11804 		else
11805 			eth_zero_addr(dev->ieee80211_ptr->disconnect_bssid);
11806 	}
11807 
11808 	wdev_unlock(dev->ieee80211_ptr);
11809 
11810 	return err;
11811 }
11812 
11813 static int nl80211_update_connect_params(struct sk_buff *skb,
11814 					 struct genl_info *info)
11815 {
11816 	struct cfg80211_connect_params connect = {};
11817 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11818 	struct net_device *dev = info->user_ptr[1];
11819 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11820 	bool fils_sk_offload;
11821 	u32 auth_type;
11822 	u32 changed = 0;
11823 	int ret;
11824 
11825 	if (!rdev->ops->update_connect_params)
11826 		return -EOPNOTSUPP;
11827 
11828 	if (info->attrs[NL80211_ATTR_IE]) {
11829 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11830 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11831 		changed |= UPDATE_ASSOC_IES;
11832 	}
11833 
11834 	fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
11835 						  NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
11836 
11837 	/*
11838 	 * when driver supports fils-sk offload all attributes must be
11839 	 * provided. So the else covers "fils-sk-not-all" and
11840 	 * "no-fils-sk-any".
11841 	 */
11842 	if (fils_sk_offload &&
11843 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
11844 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
11845 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
11846 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11847 		connect.fils_erp_username =
11848 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11849 		connect.fils_erp_username_len =
11850 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11851 		connect.fils_erp_realm =
11852 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11853 		connect.fils_erp_realm_len =
11854 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11855 		connect.fils_erp_next_seq_num =
11856 			nla_get_u16(
11857 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
11858 		connect.fils_erp_rrk =
11859 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
11860 		connect.fils_erp_rrk_len =
11861 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
11862 		changed |= UPDATE_FILS_ERP_INFO;
11863 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
11864 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
11865 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
11866 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11867 		return -EINVAL;
11868 	}
11869 
11870 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
11871 		auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
11872 		if (!nl80211_valid_auth_type(rdev, auth_type,
11873 					     NL80211_CMD_CONNECT))
11874 			return -EINVAL;
11875 
11876 		if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
11877 		    fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
11878 			return -EINVAL;
11879 
11880 		connect.auth_type = auth_type;
11881 		changed |= UPDATE_AUTH_TYPE;
11882 	}
11883 
11884 	wdev_lock(dev->ieee80211_ptr);
11885 	if (!wdev->connected)
11886 		ret = -ENOLINK;
11887 	else
11888 		ret = rdev_update_connect_params(rdev, dev, &connect, changed);
11889 	wdev_unlock(dev->ieee80211_ptr);
11890 
11891 	return ret;
11892 }
11893 
11894 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
11895 {
11896 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11897 	struct net_device *dev = info->user_ptr[1];
11898 	u16 reason;
11899 	int ret;
11900 
11901 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11902 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11903 		return -EPERM;
11904 
11905 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11906 		reason = WLAN_REASON_DEAUTH_LEAVING;
11907 	else
11908 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11909 
11910 	if (reason == 0)
11911 		return -EINVAL;
11912 
11913 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11914 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11915 		return -EOPNOTSUPP;
11916 
11917 	wdev_lock(dev->ieee80211_ptr);
11918 	ret = cfg80211_disconnect(rdev, dev, reason, true);
11919 	wdev_unlock(dev->ieee80211_ptr);
11920 	return ret;
11921 }
11922 
11923 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
11924 {
11925 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11926 	struct net *net;
11927 	int err;
11928 
11929 	if (info->attrs[NL80211_ATTR_PID]) {
11930 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
11931 
11932 		net = get_net_ns_by_pid(pid);
11933 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
11934 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
11935 
11936 		net = get_net_ns_by_fd(fd);
11937 	} else {
11938 		return -EINVAL;
11939 	}
11940 
11941 	if (IS_ERR(net))
11942 		return PTR_ERR(net);
11943 
11944 	err = 0;
11945 
11946 	/* check if anything to do */
11947 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
11948 		err = cfg80211_switch_netns(rdev, net);
11949 
11950 	put_net(net);
11951 	return err;
11952 }
11953 
11954 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
11955 {
11956 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11957 	int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
11958 			struct cfg80211_pmksa *pmksa) = NULL;
11959 	struct net_device *dev = info->user_ptr[1];
11960 	struct cfg80211_pmksa pmksa;
11961 
11962 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
11963 
11964 	if (!info->attrs[NL80211_ATTR_PMKID])
11965 		return -EINVAL;
11966 
11967 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
11968 
11969 	if (info->attrs[NL80211_ATTR_MAC]) {
11970 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11971 	} else if (info->attrs[NL80211_ATTR_SSID] &&
11972 		   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
11973 		   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
11974 		    info->attrs[NL80211_ATTR_PMK])) {
11975 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11976 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11977 		pmksa.cache_id =
11978 			nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
11979 	} else {
11980 		return -EINVAL;
11981 	}
11982 	if (info->attrs[NL80211_ATTR_PMK]) {
11983 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
11984 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
11985 	}
11986 
11987 	if (info->attrs[NL80211_ATTR_PMK_LIFETIME])
11988 		pmksa.pmk_lifetime =
11989 			nla_get_u32(info->attrs[NL80211_ATTR_PMK_LIFETIME]);
11990 
11991 	if (info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD])
11992 		pmksa.pmk_reauth_threshold =
11993 			nla_get_u8(
11994 				info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD]);
11995 
11996 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11997 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
11998 	    !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
11999 	      wiphy_ext_feature_isset(&rdev->wiphy,
12000 				      NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
12001 		return -EOPNOTSUPP;
12002 
12003 	switch (info->genlhdr->cmd) {
12004 	case NL80211_CMD_SET_PMKSA:
12005 		rdev_ops = rdev->ops->set_pmksa;
12006 		break;
12007 	case NL80211_CMD_DEL_PMKSA:
12008 		rdev_ops = rdev->ops->del_pmksa;
12009 		break;
12010 	default:
12011 		WARN_ON(1);
12012 		break;
12013 	}
12014 
12015 	if (!rdev_ops)
12016 		return -EOPNOTSUPP;
12017 
12018 	return rdev_ops(&rdev->wiphy, dev, &pmksa);
12019 }
12020 
12021 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
12022 {
12023 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12024 	struct net_device *dev = info->user_ptr[1];
12025 
12026 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12027 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
12028 		return -EOPNOTSUPP;
12029 
12030 	if (!rdev->ops->flush_pmksa)
12031 		return -EOPNOTSUPP;
12032 
12033 	return rdev_flush_pmksa(rdev, dev);
12034 }
12035 
12036 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
12037 {
12038 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12039 	struct net_device *dev = info->user_ptr[1];
12040 	u8 action_code, dialog_token;
12041 	u32 peer_capability = 0;
12042 	u16 status_code;
12043 	u8 *peer;
12044 	bool initiator;
12045 
12046 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12047 	    !rdev->ops->tdls_mgmt)
12048 		return -EOPNOTSUPP;
12049 
12050 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
12051 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
12052 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
12053 	    !info->attrs[NL80211_ATTR_IE] ||
12054 	    !info->attrs[NL80211_ATTR_MAC])
12055 		return -EINVAL;
12056 
12057 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12058 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
12059 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
12060 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
12061 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
12062 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
12063 		peer_capability =
12064 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
12065 
12066 	return rdev_tdls_mgmt(rdev, dev, peer, action_code,
12067 			      dialog_token, status_code, peer_capability,
12068 			      initiator,
12069 			      nla_data(info->attrs[NL80211_ATTR_IE]),
12070 			      nla_len(info->attrs[NL80211_ATTR_IE]));
12071 }
12072 
12073 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
12074 {
12075 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12076 	struct net_device *dev = info->user_ptr[1];
12077 	enum nl80211_tdls_operation operation;
12078 	u8 *peer;
12079 
12080 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12081 	    !rdev->ops->tdls_oper)
12082 		return -EOPNOTSUPP;
12083 
12084 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
12085 	    !info->attrs[NL80211_ATTR_MAC])
12086 		return -EINVAL;
12087 
12088 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
12089 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12090 
12091 	return rdev_tdls_oper(rdev, dev, peer, operation);
12092 }
12093 
12094 static int nl80211_remain_on_channel(struct sk_buff *skb,
12095 				     struct genl_info *info)
12096 {
12097 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12098 	unsigned int link_id = nl80211_link_id(info->attrs);
12099 	struct wireless_dev *wdev = info->user_ptr[1];
12100 	struct cfg80211_chan_def chandef;
12101 	struct sk_buff *msg;
12102 	void *hdr;
12103 	u64 cookie;
12104 	u32 duration;
12105 	int err;
12106 
12107 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
12108 	    !info->attrs[NL80211_ATTR_DURATION])
12109 		return -EINVAL;
12110 
12111 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12112 
12113 	if (!rdev->ops->remain_on_channel ||
12114 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
12115 		return -EOPNOTSUPP;
12116 
12117 	/*
12118 	 * We should be on that channel for at least a minimum amount of
12119 	 * time (10ms) but no longer than the driver supports.
12120 	 */
12121 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12122 	    duration > rdev->wiphy.max_remain_on_channel_duration)
12123 		return -EINVAL;
12124 
12125 	err = nl80211_parse_chandef(rdev, info, &chandef);
12126 	if (err)
12127 		return err;
12128 
12129 	wdev_lock(wdev);
12130 	if (!cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12131 		const struct cfg80211_chan_def *oper_chandef, *compat_chandef;
12132 
12133 		oper_chandef = wdev_chandef(wdev, link_id);
12134 
12135 		if (WARN_ON(!oper_chandef)) {
12136 			/* cannot happen since we must beacon to get here */
12137 			WARN_ON(1);
12138 			wdev_unlock(wdev);
12139 			return -EBUSY;
12140 		}
12141 
12142 		/* note: returns first one if identical chandefs */
12143 		compat_chandef = cfg80211_chandef_compatible(&chandef,
12144 							     oper_chandef);
12145 
12146 		if (compat_chandef != &chandef) {
12147 			wdev_unlock(wdev);
12148 			return -EBUSY;
12149 		}
12150 	}
12151 	wdev_unlock(wdev);
12152 
12153 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12154 	if (!msg)
12155 		return -ENOMEM;
12156 
12157 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12158 			     NL80211_CMD_REMAIN_ON_CHANNEL);
12159 	if (!hdr) {
12160 		err = -ENOBUFS;
12161 		goto free_msg;
12162 	}
12163 
12164 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
12165 				     duration, &cookie);
12166 
12167 	if (err)
12168 		goto free_msg;
12169 
12170 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12171 			      NL80211_ATTR_PAD))
12172 		goto nla_put_failure;
12173 
12174 	genlmsg_end(msg, hdr);
12175 
12176 	return genlmsg_reply(msg, info);
12177 
12178  nla_put_failure:
12179 	err = -ENOBUFS;
12180  free_msg:
12181 	nlmsg_free(msg);
12182 	return err;
12183 }
12184 
12185 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
12186 					    struct genl_info *info)
12187 {
12188 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12189 	struct wireless_dev *wdev = info->user_ptr[1];
12190 	u64 cookie;
12191 
12192 	if (!info->attrs[NL80211_ATTR_COOKIE])
12193 		return -EINVAL;
12194 
12195 	if (!rdev->ops->cancel_remain_on_channel)
12196 		return -EOPNOTSUPP;
12197 
12198 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12199 
12200 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
12201 }
12202 
12203 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
12204 				       struct genl_info *info)
12205 {
12206 	struct cfg80211_bitrate_mask mask;
12207 	unsigned int link_id = nl80211_link_id(info->attrs);
12208 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12209 	struct net_device *dev = info->user_ptr[1];
12210 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12211 	int err;
12212 
12213 	if (!rdev->ops->set_bitrate_mask)
12214 		return -EOPNOTSUPP;
12215 
12216 	wdev_lock(wdev);
12217 	err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
12218 					    NL80211_ATTR_TX_RATES, &mask,
12219 					    dev, true, link_id);
12220 	if (err)
12221 		goto out;
12222 
12223 	err = rdev_set_bitrate_mask(rdev, dev, link_id, NULL, &mask);
12224 out:
12225 	wdev_unlock(wdev);
12226 	return err;
12227 }
12228 
12229 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
12230 {
12231 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12232 	struct wireless_dev *wdev = info->user_ptr[1];
12233 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
12234 
12235 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
12236 		return -EINVAL;
12237 
12238 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
12239 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
12240 
12241 	switch (wdev->iftype) {
12242 	case NL80211_IFTYPE_STATION:
12243 	case NL80211_IFTYPE_ADHOC:
12244 	case NL80211_IFTYPE_P2P_CLIENT:
12245 	case NL80211_IFTYPE_AP:
12246 	case NL80211_IFTYPE_AP_VLAN:
12247 	case NL80211_IFTYPE_MESH_POINT:
12248 	case NL80211_IFTYPE_P2P_GO:
12249 	case NL80211_IFTYPE_P2P_DEVICE:
12250 		break;
12251 	case NL80211_IFTYPE_NAN:
12252 	default:
12253 		return -EOPNOTSUPP;
12254 	}
12255 
12256 	/* not much point in registering if we can't reply */
12257 	if (!rdev->ops->mgmt_tx)
12258 		return -EOPNOTSUPP;
12259 
12260 	if (info->attrs[NL80211_ATTR_RECEIVE_MULTICAST] &&
12261 	    !wiphy_ext_feature_isset(&rdev->wiphy,
12262 				     NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS)) {
12263 		GENL_SET_ERR_MSG(info,
12264 				 "multicast RX registrations are not supported");
12265 		return -EOPNOTSUPP;
12266 	}
12267 
12268 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
12269 					   nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12270 					   nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12271 					   info->attrs[NL80211_ATTR_RECEIVE_MULTICAST],
12272 					   info->extack);
12273 }
12274 
12275 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
12276 {
12277 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12278 	struct wireless_dev *wdev = info->user_ptr[1];
12279 	struct cfg80211_chan_def chandef;
12280 	int err;
12281 	void *hdr = NULL;
12282 	u64 cookie;
12283 	struct sk_buff *msg = NULL;
12284 	struct cfg80211_mgmt_tx_params params = {
12285 		.dont_wait_for_ack =
12286 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
12287 	};
12288 
12289 	if (!info->attrs[NL80211_ATTR_FRAME])
12290 		return -EINVAL;
12291 
12292 	if (!rdev->ops->mgmt_tx)
12293 		return -EOPNOTSUPP;
12294 
12295 	switch (wdev->iftype) {
12296 	case NL80211_IFTYPE_P2P_DEVICE:
12297 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
12298 			return -EINVAL;
12299 		break;
12300 	case NL80211_IFTYPE_STATION:
12301 	case NL80211_IFTYPE_ADHOC:
12302 	case NL80211_IFTYPE_P2P_CLIENT:
12303 	case NL80211_IFTYPE_AP:
12304 	case NL80211_IFTYPE_AP_VLAN:
12305 	case NL80211_IFTYPE_MESH_POINT:
12306 	case NL80211_IFTYPE_P2P_GO:
12307 		break;
12308 	case NL80211_IFTYPE_NAN:
12309 	default:
12310 		return -EOPNOTSUPP;
12311 	}
12312 
12313 	if (info->attrs[NL80211_ATTR_DURATION]) {
12314 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12315 			return -EINVAL;
12316 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12317 
12318 		/*
12319 		 * We should wait on the channel for at least a minimum amount
12320 		 * of time (10ms) but no longer than the driver supports.
12321 		 */
12322 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12323 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
12324 			return -EINVAL;
12325 	}
12326 
12327 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
12328 
12329 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12330 		return -EINVAL;
12331 
12332 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
12333 
12334 	/* get the channel if any has been specified, otherwise pass NULL to
12335 	 * the driver. The latter will use the current one
12336 	 */
12337 	chandef.chan = NULL;
12338 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
12339 		err = nl80211_parse_chandef(rdev, info, &chandef);
12340 		if (err)
12341 			return err;
12342 	}
12343 
12344 	if (!chandef.chan && params.offchan)
12345 		return -EINVAL;
12346 
12347 	wdev_lock(wdev);
12348 	if (params.offchan &&
12349 	    !cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12350 		wdev_unlock(wdev);
12351 		return -EBUSY;
12352 	}
12353 
12354 	params.link_id = nl80211_link_id_or_invalid(info->attrs);
12355 	/*
12356 	 * This now races due to the unlock, but we cannot check
12357 	 * the valid links for the _station_ anyway, so that's up
12358 	 * to the driver.
12359 	 */
12360 	if (params.link_id >= 0 &&
12361 	    !(wdev->valid_links & BIT(params.link_id))) {
12362 		wdev_unlock(wdev);
12363 		return -EINVAL;
12364 	}
12365 	wdev_unlock(wdev);
12366 
12367 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
12368 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
12369 
12370 	if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
12371 		int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12372 		int i;
12373 
12374 		if (len % sizeof(u16))
12375 			return -EINVAL;
12376 
12377 		params.n_csa_offsets = len / sizeof(u16);
12378 		params.csa_offsets =
12379 			nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12380 
12381 		/* check that all the offsets fit the frame */
12382 		for (i = 0; i < params.n_csa_offsets; i++) {
12383 			if (params.csa_offsets[i] >= params.len)
12384 				return -EINVAL;
12385 		}
12386 	}
12387 
12388 	if (!params.dont_wait_for_ack) {
12389 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12390 		if (!msg)
12391 			return -ENOMEM;
12392 
12393 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12394 				     NL80211_CMD_FRAME);
12395 		if (!hdr) {
12396 			err = -ENOBUFS;
12397 			goto free_msg;
12398 		}
12399 	}
12400 
12401 	params.chan = chandef.chan;
12402 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
12403 	if (err)
12404 		goto free_msg;
12405 
12406 	if (msg) {
12407 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12408 				      NL80211_ATTR_PAD))
12409 			goto nla_put_failure;
12410 
12411 		genlmsg_end(msg, hdr);
12412 		return genlmsg_reply(msg, info);
12413 	}
12414 
12415 	return 0;
12416 
12417  nla_put_failure:
12418 	err = -ENOBUFS;
12419  free_msg:
12420 	nlmsg_free(msg);
12421 	return err;
12422 }
12423 
12424 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
12425 {
12426 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12427 	struct wireless_dev *wdev = info->user_ptr[1];
12428 	u64 cookie;
12429 
12430 	if (!info->attrs[NL80211_ATTR_COOKIE])
12431 		return -EINVAL;
12432 
12433 	if (!rdev->ops->mgmt_tx_cancel_wait)
12434 		return -EOPNOTSUPP;
12435 
12436 	switch (wdev->iftype) {
12437 	case NL80211_IFTYPE_STATION:
12438 	case NL80211_IFTYPE_ADHOC:
12439 	case NL80211_IFTYPE_P2P_CLIENT:
12440 	case NL80211_IFTYPE_AP:
12441 	case NL80211_IFTYPE_AP_VLAN:
12442 	case NL80211_IFTYPE_P2P_GO:
12443 	case NL80211_IFTYPE_P2P_DEVICE:
12444 		break;
12445 	case NL80211_IFTYPE_NAN:
12446 	default:
12447 		return -EOPNOTSUPP;
12448 	}
12449 
12450 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12451 
12452 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
12453 }
12454 
12455 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
12456 {
12457 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12458 	struct wireless_dev *wdev;
12459 	struct net_device *dev = info->user_ptr[1];
12460 	u8 ps_state;
12461 	bool state;
12462 	int err;
12463 
12464 	if (!info->attrs[NL80211_ATTR_PS_STATE])
12465 		return -EINVAL;
12466 
12467 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
12468 
12469 	wdev = dev->ieee80211_ptr;
12470 
12471 	if (!rdev->ops->set_power_mgmt)
12472 		return -EOPNOTSUPP;
12473 
12474 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
12475 
12476 	if (state == wdev->ps)
12477 		return 0;
12478 
12479 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
12480 	if (!err)
12481 		wdev->ps = state;
12482 	return err;
12483 }
12484 
12485 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
12486 {
12487 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12488 	enum nl80211_ps_state ps_state;
12489 	struct wireless_dev *wdev;
12490 	struct net_device *dev = info->user_ptr[1];
12491 	struct sk_buff *msg;
12492 	void *hdr;
12493 	int err;
12494 
12495 	wdev = dev->ieee80211_ptr;
12496 
12497 	if (!rdev->ops->set_power_mgmt)
12498 		return -EOPNOTSUPP;
12499 
12500 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12501 	if (!msg)
12502 		return -ENOMEM;
12503 
12504 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12505 			     NL80211_CMD_GET_POWER_SAVE);
12506 	if (!hdr) {
12507 		err = -ENOBUFS;
12508 		goto free_msg;
12509 	}
12510 
12511 	if (wdev->ps)
12512 		ps_state = NL80211_PS_ENABLED;
12513 	else
12514 		ps_state = NL80211_PS_DISABLED;
12515 
12516 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
12517 		goto nla_put_failure;
12518 
12519 	genlmsg_end(msg, hdr);
12520 	return genlmsg_reply(msg, info);
12521 
12522  nla_put_failure:
12523 	err = -ENOBUFS;
12524  free_msg:
12525 	nlmsg_free(msg);
12526 	return err;
12527 }
12528 
12529 static const struct nla_policy
12530 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
12531 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
12532 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
12533 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
12534 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
12535 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
12536 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
12537 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
12538 };
12539 
12540 static int nl80211_set_cqm_txe(struct genl_info *info,
12541 			       u32 rate, u32 pkts, u32 intvl)
12542 {
12543 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12544 	struct net_device *dev = info->user_ptr[1];
12545 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12546 
12547 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
12548 		return -EINVAL;
12549 
12550 	if (!rdev->ops->set_cqm_txe_config)
12551 		return -EOPNOTSUPP;
12552 
12553 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12554 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12555 		return -EOPNOTSUPP;
12556 
12557 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
12558 }
12559 
12560 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
12561 				    struct net_device *dev)
12562 {
12563 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12564 	s32 last, low, high;
12565 	u32 hyst;
12566 	int i, n, low_index;
12567 	int err;
12568 
12569 	/* RSSI reporting disabled? */
12570 	if (!wdev->cqm_config)
12571 		return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
12572 
12573 	/*
12574 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
12575 	 * event has been received yet, we should receive an event after a
12576 	 * connection is established and enough beacons received to calculate
12577 	 * the average.
12578 	 */
12579 	if (!wdev->cqm_config->last_rssi_event_value &&
12580 	    wdev->links[0].client.current_bss &&
12581 	    rdev->ops->get_station) {
12582 		struct station_info sinfo = {};
12583 		u8 *mac_addr;
12584 
12585 		mac_addr = wdev->links[0].client.current_bss->pub.bssid;
12586 
12587 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
12588 		if (err)
12589 			return err;
12590 
12591 		cfg80211_sinfo_release_content(&sinfo);
12592 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
12593 			wdev->cqm_config->last_rssi_event_value =
12594 				(s8) sinfo.rx_beacon_signal_avg;
12595 	}
12596 
12597 	last = wdev->cqm_config->last_rssi_event_value;
12598 	hyst = wdev->cqm_config->rssi_hyst;
12599 	n = wdev->cqm_config->n_rssi_thresholds;
12600 
12601 	for (i = 0; i < n; i++) {
12602 		i = array_index_nospec(i, n);
12603 		if (last < wdev->cqm_config->rssi_thresholds[i])
12604 			break;
12605 	}
12606 
12607 	low_index = i - 1;
12608 	if (low_index >= 0) {
12609 		low_index = array_index_nospec(low_index, n);
12610 		low = wdev->cqm_config->rssi_thresholds[low_index] - hyst;
12611 	} else {
12612 		low = S32_MIN;
12613 	}
12614 	if (i < n) {
12615 		i = array_index_nospec(i, n);
12616 		high = wdev->cqm_config->rssi_thresholds[i] + hyst - 1;
12617 	} else {
12618 		high = S32_MAX;
12619 	}
12620 
12621 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
12622 }
12623 
12624 static int nl80211_set_cqm_rssi(struct genl_info *info,
12625 				const s32 *thresholds, int n_thresholds,
12626 				u32 hysteresis)
12627 {
12628 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12629 	struct net_device *dev = info->user_ptr[1];
12630 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12631 	int i, err;
12632 	s32 prev = S32_MIN;
12633 
12634 	/* Check all values negative and sorted */
12635 	for (i = 0; i < n_thresholds; i++) {
12636 		if (thresholds[i] > 0 || thresholds[i] <= prev)
12637 			return -EINVAL;
12638 
12639 		prev = thresholds[i];
12640 	}
12641 
12642 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12643 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12644 		return -EOPNOTSUPP;
12645 
12646 	wdev_lock(wdev);
12647 	cfg80211_cqm_config_free(wdev);
12648 	wdev_unlock(wdev);
12649 
12650 	if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
12651 		if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
12652 			return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
12653 
12654 		return rdev_set_cqm_rssi_config(rdev, dev,
12655 						thresholds[0], hysteresis);
12656 	}
12657 
12658 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
12659 				     NL80211_EXT_FEATURE_CQM_RSSI_LIST))
12660 		return -EOPNOTSUPP;
12661 
12662 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
12663 		n_thresholds = 0;
12664 
12665 	wdev_lock(wdev);
12666 	if (n_thresholds) {
12667 		struct cfg80211_cqm_config *cqm_config;
12668 
12669 		cqm_config = kzalloc(struct_size(cqm_config, rssi_thresholds,
12670 						 n_thresholds),
12671 				     GFP_KERNEL);
12672 		if (!cqm_config) {
12673 			err = -ENOMEM;
12674 			goto unlock;
12675 		}
12676 
12677 		cqm_config->rssi_hyst = hysteresis;
12678 		cqm_config->n_rssi_thresholds = n_thresholds;
12679 		memcpy(cqm_config->rssi_thresholds, thresholds,
12680 		       flex_array_size(cqm_config, rssi_thresholds,
12681 				       n_thresholds));
12682 
12683 		wdev->cqm_config = cqm_config;
12684 	}
12685 
12686 	err = cfg80211_cqm_rssi_update(rdev, dev);
12687 
12688 unlock:
12689 	wdev_unlock(wdev);
12690 
12691 	return err;
12692 }
12693 
12694 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
12695 {
12696 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
12697 	struct nlattr *cqm;
12698 	int err;
12699 
12700 	cqm = info->attrs[NL80211_ATTR_CQM];
12701 	if (!cqm)
12702 		return -EINVAL;
12703 
12704 	err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
12705 					  nl80211_attr_cqm_policy,
12706 					  info->extack);
12707 	if (err)
12708 		return err;
12709 
12710 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
12711 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
12712 		const s32 *thresholds =
12713 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
12714 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
12715 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
12716 
12717 		if (len % 4)
12718 			return -EINVAL;
12719 
12720 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
12721 					    hysteresis);
12722 	}
12723 
12724 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
12725 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
12726 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
12727 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
12728 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
12729 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
12730 
12731 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
12732 	}
12733 
12734 	return -EINVAL;
12735 }
12736 
12737 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
12738 {
12739 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12740 	struct net_device *dev = info->user_ptr[1];
12741 	struct ocb_setup setup = {};
12742 	int err;
12743 
12744 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
12745 	if (err)
12746 		return err;
12747 
12748 	return cfg80211_join_ocb(rdev, dev, &setup);
12749 }
12750 
12751 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
12752 {
12753 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12754 	struct net_device *dev = info->user_ptr[1];
12755 
12756 	return cfg80211_leave_ocb(rdev, dev);
12757 }
12758 
12759 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
12760 {
12761 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12762 	struct net_device *dev = info->user_ptr[1];
12763 	struct mesh_config cfg;
12764 	struct mesh_setup setup;
12765 	int err;
12766 
12767 	/* start with default */
12768 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
12769 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
12770 
12771 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
12772 		/* and parse parameters if given */
12773 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
12774 		if (err)
12775 			return err;
12776 	}
12777 
12778 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
12779 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
12780 		return -EINVAL;
12781 
12782 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
12783 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
12784 
12785 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
12786 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
12787 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
12788 			return -EINVAL;
12789 
12790 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
12791 		setup.beacon_interval =
12792 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
12793 
12794 		err = cfg80211_validate_beacon_int(rdev,
12795 						   NL80211_IFTYPE_MESH_POINT,
12796 						   setup.beacon_interval);
12797 		if (err)
12798 			return err;
12799 	}
12800 
12801 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
12802 		setup.dtim_period =
12803 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
12804 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
12805 			return -EINVAL;
12806 	}
12807 
12808 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
12809 		/* parse additional setup parameters if given */
12810 		err = nl80211_parse_mesh_setup(info, &setup);
12811 		if (err)
12812 			return err;
12813 	}
12814 
12815 	if (setup.user_mpm)
12816 		cfg.auto_open_plinks = false;
12817 
12818 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
12819 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
12820 		if (err)
12821 			return err;
12822 	} else {
12823 		/* __cfg80211_join_mesh() will sort it out */
12824 		setup.chandef.chan = NULL;
12825 	}
12826 
12827 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
12828 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
12829 		int n_rates =
12830 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
12831 		struct ieee80211_supported_band *sband;
12832 
12833 		if (!setup.chandef.chan)
12834 			return -EINVAL;
12835 
12836 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
12837 
12838 		err = ieee80211_get_ratemask(sband, rates, n_rates,
12839 					     &setup.basic_rates);
12840 		if (err)
12841 			return err;
12842 	}
12843 
12844 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
12845 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
12846 						    NL80211_ATTR_TX_RATES,
12847 						    &setup.beacon_rate,
12848 						    dev, false, 0);
12849 		if (err)
12850 			return err;
12851 
12852 		if (!setup.chandef.chan)
12853 			return -EINVAL;
12854 
12855 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
12856 					      &setup.beacon_rate);
12857 		if (err)
12858 			return err;
12859 	}
12860 
12861 	setup.userspace_handles_dfs =
12862 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
12863 
12864 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
12865 		int r = validate_pae_over_nl80211(rdev, info);
12866 
12867 		if (r < 0)
12868 			return r;
12869 
12870 		setup.control_port_over_nl80211 = true;
12871 	}
12872 
12873 	wdev_lock(dev->ieee80211_ptr);
12874 	err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
12875 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
12876 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
12877 	wdev_unlock(dev->ieee80211_ptr);
12878 
12879 	return err;
12880 }
12881 
12882 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
12883 {
12884 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12885 	struct net_device *dev = info->user_ptr[1];
12886 
12887 	return cfg80211_leave_mesh(rdev, dev);
12888 }
12889 
12890 #ifdef CONFIG_PM
12891 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
12892 					struct cfg80211_registered_device *rdev)
12893 {
12894 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
12895 	struct nlattr *nl_pats, *nl_pat;
12896 	int i, pat_len;
12897 
12898 	if (!wowlan->n_patterns)
12899 		return 0;
12900 
12901 	nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
12902 	if (!nl_pats)
12903 		return -ENOBUFS;
12904 
12905 	for (i = 0; i < wowlan->n_patterns; i++) {
12906 		nl_pat = nla_nest_start_noflag(msg, i + 1);
12907 		if (!nl_pat)
12908 			return -ENOBUFS;
12909 		pat_len = wowlan->patterns[i].pattern_len;
12910 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
12911 			    wowlan->patterns[i].mask) ||
12912 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
12913 			    wowlan->patterns[i].pattern) ||
12914 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
12915 				wowlan->patterns[i].pkt_offset))
12916 			return -ENOBUFS;
12917 		nla_nest_end(msg, nl_pat);
12918 	}
12919 	nla_nest_end(msg, nl_pats);
12920 
12921 	return 0;
12922 }
12923 
12924 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
12925 				   struct cfg80211_wowlan_tcp *tcp)
12926 {
12927 	struct nlattr *nl_tcp;
12928 
12929 	if (!tcp)
12930 		return 0;
12931 
12932 	nl_tcp = nla_nest_start_noflag(msg,
12933 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
12934 	if (!nl_tcp)
12935 		return -ENOBUFS;
12936 
12937 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
12938 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
12939 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
12940 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
12941 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
12942 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
12943 		    tcp->payload_len, tcp->payload) ||
12944 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
12945 			tcp->data_interval) ||
12946 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
12947 		    tcp->wake_len, tcp->wake_data) ||
12948 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
12949 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
12950 		return -ENOBUFS;
12951 
12952 	if (tcp->payload_seq.len &&
12953 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
12954 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
12955 		return -ENOBUFS;
12956 
12957 	if (tcp->payload_tok.len &&
12958 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
12959 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
12960 		    &tcp->payload_tok))
12961 		return -ENOBUFS;
12962 
12963 	nla_nest_end(msg, nl_tcp);
12964 
12965 	return 0;
12966 }
12967 
12968 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
12969 				  struct cfg80211_sched_scan_request *req)
12970 {
12971 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
12972 	int i;
12973 
12974 	if (!req)
12975 		return 0;
12976 
12977 	nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
12978 	if (!nd)
12979 		return -ENOBUFS;
12980 
12981 	if (req->n_scan_plans == 1 &&
12982 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
12983 			req->scan_plans[0].interval * 1000))
12984 		return -ENOBUFS;
12985 
12986 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
12987 		return -ENOBUFS;
12988 
12989 	if (req->relative_rssi_set) {
12990 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
12991 
12992 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
12993 			       req->relative_rssi))
12994 			return -ENOBUFS;
12995 
12996 		rssi_adjust.band = req->rssi_adjust.band;
12997 		rssi_adjust.delta = req->rssi_adjust.delta;
12998 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
12999 			    sizeof(rssi_adjust), &rssi_adjust))
13000 			return -ENOBUFS;
13001 	}
13002 
13003 	freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
13004 	if (!freqs)
13005 		return -ENOBUFS;
13006 
13007 	for (i = 0; i < req->n_channels; i++) {
13008 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
13009 			return -ENOBUFS;
13010 	}
13011 
13012 	nla_nest_end(msg, freqs);
13013 
13014 	if (req->n_match_sets) {
13015 		matches = nla_nest_start_noflag(msg,
13016 						NL80211_ATTR_SCHED_SCAN_MATCH);
13017 		if (!matches)
13018 			return -ENOBUFS;
13019 
13020 		for (i = 0; i < req->n_match_sets; i++) {
13021 			match = nla_nest_start_noflag(msg, i);
13022 			if (!match)
13023 				return -ENOBUFS;
13024 
13025 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
13026 				    req->match_sets[i].ssid.ssid_len,
13027 				    req->match_sets[i].ssid.ssid))
13028 				return -ENOBUFS;
13029 			nla_nest_end(msg, match);
13030 		}
13031 		nla_nest_end(msg, matches);
13032 	}
13033 
13034 	scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
13035 	if (!scan_plans)
13036 		return -ENOBUFS;
13037 
13038 	for (i = 0; i < req->n_scan_plans; i++) {
13039 		scan_plan = nla_nest_start_noflag(msg, i + 1);
13040 		if (!scan_plan)
13041 			return -ENOBUFS;
13042 
13043 		if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
13044 				req->scan_plans[i].interval) ||
13045 		    (req->scan_plans[i].iterations &&
13046 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
13047 				 req->scan_plans[i].iterations)))
13048 			return -ENOBUFS;
13049 		nla_nest_end(msg, scan_plan);
13050 	}
13051 	nla_nest_end(msg, scan_plans);
13052 
13053 	nla_nest_end(msg, nd);
13054 
13055 	return 0;
13056 }
13057 
13058 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
13059 {
13060 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13061 	struct sk_buff *msg;
13062 	void *hdr;
13063 	u32 size = NLMSG_DEFAULT_SIZE;
13064 
13065 	if (!rdev->wiphy.wowlan)
13066 		return -EOPNOTSUPP;
13067 
13068 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
13069 		/* adjust size to have room for all the data */
13070 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
13071 			rdev->wiphy.wowlan_config->tcp->payload_len +
13072 			rdev->wiphy.wowlan_config->tcp->wake_len +
13073 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
13074 	}
13075 
13076 	msg = nlmsg_new(size, GFP_KERNEL);
13077 	if (!msg)
13078 		return -ENOMEM;
13079 
13080 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13081 			     NL80211_CMD_GET_WOWLAN);
13082 	if (!hdr)
13083 		goto nla_put_failure;
13084 
13085 	if (rdev->wiphy.wowlan_config) {
13086 		struct nlattr *nl_wowlan;
13087 
13088 		nl_wowlan = nla_nest_start_noflag(msg,
13089 						  NL80211_ATTR_WOWLAN_TRIGGERS);
13090 		if (!nl_wowlan)
13091 			goto nla_put_failure;
13092 
13093 		if ((rdev->wiphy.wowlan_config->any &&
13094 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
13095 		    (rdev->wiphy.wowlan_config->disconnect &&
13096 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
13097 		    (rdev->wiphy.wowlan_config->magic_pkt &&
13098 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
13099 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
13100 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
13101 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
13102 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
13103 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
13104 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
13105 		    (rdev->wiphy.wowlan_config->rfkill_release &&
13106 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
13107 			goto nla_put_failure;
13108 
13109 		if (nl80211_send_wowlan_patterns(msg, rdev))
13110 			goto nla_put_failure;
13111 
13112 		if (nl80211_send_wowlan_tcp(msg,
13113 					    rdev->wiphy.wowlan_config->tcp))
13114 			goto nla_put_failure;
13115 
13116 		if (nl80211_send_wowlan_nd(
13117 			    msg,
13118 			    rdev->wiphy.wowlan_config->nd_config))
13119 			goto nla_put_failure;
13120 
13121 		nla_nest_end(msg, nl_wowlan);
13122 	}
13123 
13124 	genlmsg_end(msg, hdr);
13125 	return genlmsg_reply(msg, info);
13126 
13127 nla_put_failure:
13128 	nlmsg_free(msg);
13129 	return -ENOBUFS;
13130 }
13131 
13132 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
13133 				    struct nlattr *attr,
13134 				    struct cfg80211_wowlan *trig)
13135 {
13136 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
13137 	struct cfg80211_wowlan_tcp *cfg;
13138 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
13139 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
13140 	u32 size;
13141 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
13142 	int err, port;
13143 
13144 	if (!rdev->wiphy.wowlan->tcp)
13145 		return -EINVAL;
13146 
13147 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
13148 					  nl80211_wowlan_tcp_policy, NULL);
13149 	if (err)
13150 		return err;
13151 
13152 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
13153 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
13154 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
13155 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
13156 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
13157 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
13158 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
13159 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
13160 		return -EINVAL;
13161 
13162 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
13163 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
13164 		return -EINVAL;
13165 
13166 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
13167 			rdev->wiphy.wowlan->tcp->data_interval_max ||
13168 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
13169 		return -EINVAL;
13170 
13171 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
13172 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
13173 		return -EINVAL;
13174 
13175 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
13176 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
13177 		return -EINVAL;
13178 
13179 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
13180 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13181 
13182 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13183 		tokens_size = tokln - sizeof(*tok);
13184 
13185 		if (!tok->len || tokens_size % tok->len)
13186 			return -EINVAL;
13187 		if (!rdev->wiphy.wowlan->tcp->tok)
13188 			return -EINVAL;
13189 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
13190 			return -EINVAL;
13191 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
13192 			return -EINVAL;
13193 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
13194 			return -EINVAL;
13195 		if (tok->offset + tok->len > data_size)
13196 			return -EINVAL;
13197 	}
13198 
13199 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
13200 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
13201 		if (!rdev->wiphy.wowlan->tcp->seq)
13202 			return -EINVAL;
13203 		if (seq->len == 0 || seq->len > 4)
13204 			return -EINVAL;
13205 		if (seq->len + seq->offset > data_size)
13206 			return -EINVAL;
13207 	}
13208 
13209 	size = sizeof(*cfg);
13210 	size += data_size;
13211 	size += wake_size + wake_mask_size;
13212 	size += tokens_size;
13213 
13214 	cfg = kzalloc(size, GFP_KERNEL);
13215 	if (!cfg)
13216 		return -ENOMEM;
13217 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
13218 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
13219 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
13220 	       ETH_ALEN);
13221 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
13222 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
13223 	else
13224 		port = 0;
13225 #ifdef CONFIG_INET
13226 	/* allocate a socket and port for it and use it */
13227 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
13228 			    IPPROTO_TCP, &cfg->sock, 1);
13229 	if (err) {
13230 		kfree(cfg);
13231 		return err;
13232 	}
13233 	if (inet_csk_get_port(cfg->sock->sk, port)) {
13234 		sock_release(cfg->sock);
13235 		kfree(cfg);
13236 		return -EADDRINUSE;
13237 	}
13238 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
13239 #else
13240 	if (!port) {
13241 		kfree(cfg);
13242 		return -EINVAL;
13243 	}
13244 	cfg->src_port = port;
13245 #endif
13246 
13247 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
13248 	cfg->payload_len = data_size;
13249 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
13250 	memcpy((void *)cfg->payload,
13251 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
13252 	       data_size);
13253 	if (seq)
13254 		cfg->payload_seq = *seq;
13255 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
13256 	cfg->wake_len = wake_size;
13257 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
13258 	memcpy((void *)cfg->wake_data,
13259 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
13260 	       wake_size);
13261 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
13262 			 data_size + wake_size;
13263 	memcpy((void *)cfg->wake_mask,
13264 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
13265 	       wake_mask_size);
13266 	if (tok) {
13267 		cfg->tokens_size = tokens_size;
13268 		cfg->payload_tok = *tok;
13269 		memcpy(cfg->payload_tok.token_stream, tok->token_stream,
13270 		       tokens_size);
13271 	}
13272 
13273 	trig->tcp = cfg;
13274 
13275 	return 0;
13276 }
13277 
13278 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
13279 				   const struct wiphy_wowlan_support *wowlan,
13280 				   struct nlattr *attr,
13281 				   struct cfg80211_wowlan *trig)
13282 {
13283 	struct nlattr **tb;
13284 	int err;
13285 
13286 	tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
13287 	if (!tb)
13288 		return -ENOMEM;
13289 
13290 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
13291 		err = -EOPNOTSUPP;
13292 		goto out;
13293 	}
13294 
13295 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
13296 					  nl80211_policy, NULL);
13297 	if (err)
13298 		goto out;
13299 
13300 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
13301 						   wowlan->max_nd_match_sets);
13302 	err = PTR_ERR_OR_ZERO(trig->nd_config);
13303 	if (err)
13304 		trig->nd_config = NULL;
13305 
13306 out:
13307 	kfree(tb);
13308 	return err;
13309 }
13310 
13311 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
13312 {
13313 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13314 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
13315 	struct cfg80211_wowlan new_triggers = {};
13316 	struct cfg80211_wowlan *ntrig;
13317 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
13318 	int err, i;
13319 	bool prev_enabled = rdev->wiphy.wowlan_config;
13320 	bool regular = false;
13321 
13322 	if (!wowlan)
13323 		return -EOPNOTSUPP;
13324 
13325 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
13326 		cfg80211_rdev_free_wowlan(rdev);
13327 		rdev->wiphy.wowlan_config = NULL;
13328 		goto set_wakeup;
13329 	}
13330 
13331 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
13332 					  info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
13333 					  nl80211_wowlan_policy, info->extack);
13334 	if (err)
13335 		return err;
13336 
13337 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
13338 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
13339 			return -EINVAL;
13340 		new_triggers.any = true;
13341 	}
13342 
13343 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
13344 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
13345 			return -EINVAL;
13346 		new_triggers.disconnect = true;
13347 		regular = true;
13348 	}
13349 
13350 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
13351 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
13352 			return -EINVAL;
13353 		new_triggers.magic_pkt = true;
13354 		regular = true;
13355 	}
13356 
13357 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
13358 		return -EINVAL;
13359 
13360 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
13361 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
13362 			return -EINVAL;
13363 		new_triggers.gtk_rekey_failure = true;
13364 		regular = true;
13365 	}
13366 
13367 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
13368 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
13369 			return -EINVAL;
13370 		new_triggers.eap_identity_req = true;
13371 		regular = true;
13372 	}
13373 
13374 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
13375 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
13376 			return -EINVAL;
13377 		new_triggers.four_way_handshake = true;
13378 		regular = true;
13379 	}
13380 
13381 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
13382 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
13383 			return -EINVAL;
13384 		new_triggers.rfkill_release = true;
13385 		regular = true;
13386 	}
13387 
13388 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
13389 		struct nlattr *pat;
13390 		int n_patterns = 0;
13391 		int rem, pat_len, mask_len, pkt_offset;
13392 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13393 
13394 		regular = true;
13395 
13396 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13397 				    rem)
13398 			n_patterns++;
13399 		if (n_patterns > wowlan->n_patterns)
13400 			return -EINVAL;
13401 
13402 		new_triggers.patterns = kcalloc(n_patterns,
13403 						sizeof(new_triggers.patterns[0]),
13404 						GFP_KERNEL);
13405 		if (!new_triggers.patterns)
13406 			return -ENOMEM;
13407 
13408 		new_triggers.n_patterns = n_patterns;
13409 		i = 0;
13410 
13411 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13412 				    rem) {
13413 			u8 *mask_pat;
13414 
13415 			err = nla_parse_nested_deprecated(pat_tb,
13416 							  MAX_NL80211_PKTPAT,
13417 							  pat,
13418 							  nl80211_packet_pattern_policy,
13419 							  info->extack);
13420 			if (err)
13421 				goto error;
13422 
13423 			err = -EINVAL;
13424 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
13425 			    !pat_tb[NL80211_PKTPAT_PATTERN])
13426 				goto error;
13427 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13428 			mask_len = DIV_ROUND_UP(pat_len, 8);
13429 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13430 				goto error;
13431 			if (pat_len > wowlan->pattern_max_len ||
13432 			    pat_len < wowlan->pattern_min_len)
13433 				goto error;
13434 
13435 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
13436 				pkt_offset = 0;
13437 			else
13438 				pkt_offset = nla_get_u32(
13439 					pat_tb[NL80211_PKTPAT_OFFSET]);
13440 			if (pkt_offset > wowlan->max_pkt_offset)
13441 				goto error;
13442 			new_triggers.patterns[i].pkt_offset = pkt_offset;
13443 
13444 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13445 			if (!mask_pat) {
13446 				err = -ENOMEM;
13447 				goto error;
13448 			}
13449 			new_triggers.patterns[i].mask = mask_pat;
13450 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13451 			       mask_len);
13452 			mask_pat += mask_len;
13453 			new_triggers.patterns[i].pattern = mask_pat;
13454 			new_triggers.patterns[i].pattern_len = pat_len;
13455 			memcpy(mask_pat,
13456 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13457 			       pat_len);
13458 			i++;
13459 		}
13460 	}
13461 
13462 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
13463 		regular = true;
13464 		err = nl80211_parse_wowlan_tcp(
13465 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
13466 			&new_triggers);
13467 		if (err)
13468 			goto error;
13469 	}
13470 
13471 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
13472 		regular = true;
13473 		err = nl80211_parse_wowlan_nd(
13474 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
13475 			&new_triggers);
13476 		if (err)
13477 			goto error;
13478 	}
13479 
13480 	/* The 'any' trigger means the device continues operating more or less
13481 	 * as in its normal operation mode and wakes up the host on most of the
13482 	 * normal interrupts (like packet RX, ...)
13483 	 * It therefore makes little sense to combine with the more constrained
13484 	 * wakeup trigger modes.
13485 	 */
13486 	if (new_triggers.any && regular) {
13487 		err = -EINVAL;
13488 		goto error;
13489 	}
13490 
13491 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
13492 	if (!ntrig) {
13493 		err = -ENOMEM;
13494 		goto error;
13495 	}
13496 	cfg80211_rdev_free_wowlan(rdev);
13497 	rdev->wiphy.wowlan_config = ntrig;
13498 
13499  set_wakeup:
13500 	if (rdev->ops->set_wakeup &&
13501 	    prev_enabled != !!rdev->wiphy.wowlan_config)
13502 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
13503 
13504 	return 0;
13505  error:
13506 	for (i = 0; i < new_triggers.n_patterns; i++)
13507 		kfree(new_triggers.patterns[i].mask);
13508 	kfree(new_triggers.patterns);
13509 	if (new_triggers.tcp && new_triggers.tcp->sock)
13510 		sock_release(new_triggers.tcp->sock);
13511 	kfree(new_triggers.tcp);
13512 	kfree(new_triggers.nd_config);
13513 	return err;
13514 }
13515 #endif
13516 
13517 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
13518 				       struct cfg80211_registered_device *rdev)
13519 {
13520 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
13521 	int i, j, pat_len;
13522 	struct cfg80211_coalesce_rules *rule;
13523 
13524 	if (!rdev->coalesce->n_rules)
13525 		return 0;
13526 
13527 	nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
13528 	if (!nl_rules)
13529 		return -ENOBUFS;
13530 
13531 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
13532 		nl_rule = nla_nest_start_noflag(msg, i + 1);
13533 		if (!nl_rule)
13534 			return -ENOBUFS;
13535 
13536 		rule = &rdev->coalesce->rules[i];
13537 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
13538 				rule->delay))
13539 			return -ENOBUFS;
13540 
13541 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
13542 				rule->condition))
13543 			return -ENOBUFS;
13544 
13545 		nl_pats = nla_nest_start_noflag(msg,
13546 						NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
13547 		if (!nl_pats)
13548 			return -ENOBUFS;
13549 
13550 		for (j = 0; j < rule->n_patterns; j++) {
13551 			nl_pat = nla_nest_start_noflag(msg, j + 1);
13552 			if (!nl_pat)
13553 				return -ENOBUFS;
13554 			pat_len = rule->patterns[j].pattern_len;
13555 			if (nla_put(msg, NL80211_PKTPAT_MASK,
13556 				    DIV_ROUND_UP(pat_len, 8),
13557 				    rule->patterns[j].mask) ||
13558 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
13559 				    rule->patterns[j].pattern) ||
13560 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
13561 					rule->patterns[j].pkt_offset))
13562 				return -ENOBUFS;
13563 			nla_nest_end(msg, nl_pat);
13564 		}
13565 		nla_nest_end(msg, nl_pats);
13566 		nla_nest_end(msg, nl_rule);
13567 	}
13568 	nla_nest_end(msg, nl_rules);
13569 
13570 	return 0;
13571 }
13572 
13573 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
13574 {
13575 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13576 	struct sk_buff *msg;
13577 	void *hdr;
13578 
13579 	if (!rdev->wiphy.coalesce)
13580 		return -EOPNOTSUPP;
13581 
13582 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13583 	if (!msg)
13584 		return -ENOMEM;
13585 
13586 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13587 			     NL80211_CMD_GET_COALESCE);
13588 	if (!hdr)
13589 		goto nla_put_failure;
13590 
13591 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
13592 		goto nla_put_failure;
13593 
13594 	genlmsg_end(msg, hdr);
13595 	return genlmsg_reply(msg, info);
13596 
13597 nla_put_failure:
13598 	nlmsg_free(msg);
13599 	return -ENOBUFS;
13600 }
13601 
13602 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
13603 {
13604 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
13605 	int i, j;
13606 	struct cfg80211_coalesce_rules *rule;
13607 
13608 	if (!coalesce)
13609 		return;
13610 
13611 	for (i = 0; i < coalesce->n_rules; i++) {
13612 		rule = &coalesce->rules[i];
13613 		for (j = 0; j < rule->n_patterns; j++)
13614 			kfree(rule->patterns[j].mask);
13615 		kfree(rule->patterns);
13616 	}
13617 	kfree(coalesce->rules);
13618 	kfree(coalesce);
13619 	rdev->coalesce = NULL;
13620 }
13621 
13622 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
13623 				       struct nlattr *rule,
13624 				       struct cfg80211_coalesce_rules *new_rule)
13625 {
13626 	int err, i;
13627 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13628 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
13629 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
13630 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13631 
13632 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
13633 					  rule, nl80211_coalesce_policy, NULL);
13634 	if (err)
13635 		return err;
13636 
13637 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
13638 		new_rule->delay =
13639 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
13640 	if (new_rule->delay > coalesce->max_delay)
13641 		return -EINVAL;
13642 
13643 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
13644 		new_rule->condition =
13645 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
13646 
13647 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
13648 		return -EINVAL;
13649 
13650 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13651 			    rem)
13652 		n_patterns++;
13653 	if (n_patterns > coalesce->n_patterns)
13654 		return -EINVAL;
13655 
13656 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
13657 				     GFP_KERNEL);
13658 	if (!new_rule->patterns)
13659 		return -ENOMEM;
13660 
13661 	new_rule->n_patterns = n_patterns;
13662 	i = 0;
13663 
13664 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13665 			    rem) {
13666 		u8 *mask_pat;
13667 
13668 		err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
13669 						  pat,
13670 						  nl80211_packet_pattern_policy,
13671 						  NULL);
13672 		if (err)
13673 			return err;
13674 
13675 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
13676 		    !pat_tb[NL80211_PKTPAT_PATTERN])
13677 			return -EINVAL;
13678 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13679 		mask_len = DIV_ROUND_UP(pat_len, 8);
13680 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13681 			return -EINVAL;
13682 		if (pat_len > coalesce->pattern_max_len ||
13683 		    pat_len < coalesce->pattern_min_len)
13684 			return -EINVAL;
13685 
13686 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
13687 			pkt_offset = 0;
13688 		else
13689 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
13690 		if (pkt_offset > coalesce->max_pkt_offset)
13691 			return -EINVAL;
13692 		new_rule->patterns[i].pkt_offset = pkt_offset;
13693 
13694 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13695 		if (!mask_pat)
13696 			return -ENOMEM;
13697 
13698 		new_rule->patterns[i].mask = mask_pat;
13699 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13700 		       mask_len);
13701 
13702 		mask_pat += mask_len;
13703 		new_rule->patterns[i].pattern = mask_pat;
13704 		new_rule->patterns[i].pattern_len = pat_len;
13705 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13706 		       pat_len);
13707 		i++;
13708 	}
13709 
13710 	return 0;
13711 }
13712 
13713 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
13714 {
13715 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13716 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13717 	struct cfg80211_coalesce new_coalesce = {};
13718 	struct cfg80211_coalesce *n_coalesce;
13719 	int err, rem_rule, n_rules = 0, i, j;
13720 	struct nlattr *rule;
13721 	struct cfg80211_coalesce_rules *tmp_rule;
13722 
13723 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
13724 		return -EOPNOTSUPP;
13725 
13726 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
13727 		cfg80211_rdev_free_coalesce(rdev);
13728 		rdev_set_coalesce(rdev, NULL);
13729 		return 0;
13730 	}
13731 
13732 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
13733 			    rem_rule)
13734 		n_rules++;
13735 	if (n_rules > coalesce->n_rules)
13736 		return -EINVAL;
13737 
13738 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
13739 				     GFP_KERNEL);
13740 	if (!new_coalesce.rules)
13741 		return -ENOMEM;
13742 
13743 	new_coalesce.n_rules = n_rules;
13744 	i = 0;
13745 
13746 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
13747 			    rem_rule) {
13748 		err = nl80211_parse_coalesce_rule(rdev, rule,
13749 						  &new_coalesce.rules[i]);
13750 		if (err)
13751 			goto error;
13752 
13753 		i++;
13754 	}
13755 
13756 	err = rdev_set_coalesce(rdev, &new_coalesce);
13757 	if (err)
13758 		goto error;
13759 
13760 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
13761 	if (!n_coalesce) {
13762 		err = -ENOMEM;
13763 		goto error;
13764 	}
13765 	cfg80211_rdev_free_coalesce(rdev);
13766 	rdev->coalesce = n_coalesce;
13767 
13768 	return 0;
13769 error:
13770 	for (i = 0; i < new_coalesce.n_rules; i++) {
13771 		tmp_rule = &new_coalesce.rules[i];
13772 		for (j = 0; j < tmp_rule->n_patterns; j++)
13773 			kfree(tmp_rule->patterns[j].mask);
13774 		kfree(tmp_rule->patterns);
13775 	}
13776 	kfree(new_coalesce.rules);
13777 
13778 	return err;
13779 }
13780 
13781 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
13782 {
13783 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13784 	struct net_device *dev = info->user_ptr[1];
13785 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13786 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
13787 	struct cfg80211_gtk_rekey_data rekey_data = {};
13788 	int err;
13789 
13790 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
13791 		return -EINVAL;
13792 
13793 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
13794 					  info->attrs[NL80211_ATTR_REKEY_DATA],
13795 					  nl80211_rekey_policy, info->extack);
13796 	if (err)
13797 		return err;
13798 
13799 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
13800 	    !tb[NL80211_REKEY_DATA_KCK])
13801 		return -EINVAL;
13802 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN &&
13803 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
13804 	      nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KEK_EXT_LEN))
13805 		return -ERANGE;
13806 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN &&
13807 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
13808 	      nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KCK_EXT_LEN))
13809 		return -ERANGE;
13810 
13811 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
13812 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
13813 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
13814 	rekey_data.kek_len = nla_len(tb[NL80211_REKEY_DATA_KEK]);
13815 	rekey_data.kck_len = nla_len(tb[NL80211_REKEY_DATA_KCK]);
13816 	if (tb[NL80211_REKEY_DATA_AKM])
13817 		rekey_data.akm = nla_get_u32(tb[NL80211_REKEY_DATA_AKM]);
13818 
13819 	wdev_lock(wdev);
13820 	if (!wdev->connected) {
13821 		err = -ENOTCONN;
13822 		goto out;
13823 	}
13824 
13825 	if (!rdev->ops->set_rekey_data) {
13826 		err = -EOPNOTSUPP;
13827 		goto out;
13828 	}
13829 
13830 	err = rdev_set_rekey_data(rdev, dev, &rekey_data);
13831  out:
13832 	wdev_unlock(wdev);
13833 	return err;
13834 }
13835 
13836 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
13837 					     struct genl_info *info)
13838 {
13839 	struct net_device *dev = info->user_ptr[1];
13840 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13841 
13842 	if (wdev->iftype != NL80211_IFTYPE_AP &&
13843 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
13844 		return -EINVAL;
13845 
13846 	if (wdev->ap_unexpected_nlportid)
13847 		return -EBUSY;
13848 
13849 	wdev->ap_unexpected_nlportid = info->snd_portid;
13850 	return 0;
13851 }
13852 
13853 static int nl80211_probe_client(struct sk_buff *skb,
13854 				struct genl_info *info)
13855 {
13856 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13857 	struct net_device *dev = info->user_ptr[1];
13858 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13859 	struct sk_buff *msg;
13860 	void *hdr;
13861 	const u8 *addr;
13862 	u64 cookie;
13863 	int err;
13864 
13865 	if (wdev->iftype != NL80211_IFTYPE_AP &&
13866 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
13867 		return -EOPNOTSUPP;
13868 
13869 	if (!info->attrs[NL80211_ATTR_MAC])
13870 		return -EINVAL;
13871 
13872 	if (!rdev->ops->probe_client)
13873 		return -EOPNOTSUPP;
13874 
13875 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13876 	if (!msg)
13877 		return -ENOMEM;
13878 
13879 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13880 			     NL80211_CMD_PROBE_CLIENT);
13881 	if (!hdr) {
13882 		err = -ENOBUFS;
13883 		goto free_msg;
13884 	}
13885 
13886 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
13887 
13888 	err = rdev_probe_client(rdev, dev, addr, &cookie);
13889 	if (err)
13890 		goto free_msg;
13891 
13892 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
13893 			      NL80211_ATTR_PAD))
13894 		goto nla_put_failure;
13895 
13896 	genlmsg_end(msg, hdr);
13897 
13898 	return genlmsg_reply(msg, info);
13899 
13900  nla_put_failure:
13901 	err = -ENOBUFS;
13902  free_msg:
13903 	nlmsg_free(msg);
13904 	return err;
13905 }
13906 
13907 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
13908 {
13909 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13910 	struct cfg80211_beacon_registration *reg, *nreg;
13911 	int rv;
13912 
13913 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
13914 		return -EOPNOTSUPP;
13915 
13916 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
13917 	if (!nreg)
13918 		return -ENOMEM;
13919 
13920 	/* First, check if already registered. */
13921 	spin_lock_bh(&rdev->beacon_registrations_lock);
13922 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
13923 		if (reg->nlportid == info->snd_portid) {
13924 			rv = -EALREADY;
13925 			goto out_err;
13926 		}
13927 	}
13928 	/* Add it to the list */
13929 	nreg->nlportid = info->snd_portid;
13930 	list_add(&nreg->list, &rdev->beacon_registrations);
13931 
13932 	spin_unlock_bh(&rdev->beacon_registrations_lock);
13933 
13934 	return 0;
13935 out_err:
13936 	spin_unlock_bh(&rdev->beacon_registrations_lock);
13937 	kfree(nreg);
13938 	return rv;
13939 }
13940 
13941 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
13942 {
13943 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13944 	struct wireless_dev *wdev = info->user_ptr[1];
13945 	int err;
13946 
13947 	if (!rdev->ops->start_p2p_device)
13948 		return -EOPNOTSUPP;
13949 
13950 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
13951 		return -EOPNOTSUPP;
13952 
13953 	if (wdev_running(wdev))
13954 		return 0;
13955 
13956 	if (rfkill_blocked(rdev->wiphy.rfkill))
13957 		return -ERFKILL;
13958 
13959 	err = rdev_start_p2p_device(rdev, wdev);
13960 	if (err)
13961 		return err;
13962 
13963 	wdev->is_running = true;
13964 	rdev->opencount++;
13965 
13966 	return 0;
13967 }
13968 
13969 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
13970 {
13971 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13972 	struct wireless_dev *wdev = info->user_ptr[1];
13973 
13974 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
13975 		return -EOPNOTSUPP;
13976 
13977 	if (!rdev->ops->stop_p2p_device)
13978 		return -EOPNOTSUPP;
13979 
13980 	cfg80211_stop_p2p_device(rdev, wdev);
13981 
13982 	return 0;
13983 }
13984 
13985 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
13986 {
13987 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13988 	struct wireless_dev *wdev = info->user_ptr[1];
13989 	struct cfg80211_nan_conf conf = {};
13990 	int err;
13991 
13992 	if (wdev->iftype != NL80211_IFTYPE_NAN)
13993 		return -EOPNOTSUPP;
13994 
13995 	if (wdev_running(wdev))
13996 		return -EEXIST;
13997 
13998 	if (rfkill_blocked(rdev->wiphy.rfkill))
13999 		return -ERFKILL;
14000 
14001 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
14002 		return -EINVAL;
14003 
14004 	conf.master_pref =
14005 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14006 
14007 	if (info->attrs[NL80211_ATTR_BANDS]) {
14008 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14009 
14010 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14011 			return -EOPNOTSUPP;
14012 
14013 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14014 			return -EINVAL;
14015 
14016 		conf.bands = bands;
14017 	}
14018 
14019 	err = rdev_start_nan(rdev, wdev, &conf);
14020 	if (err)
14021 		return err;
14022 
14023 	wdev->is_running = true;
14024 	rdev->opencount++;
14025 
14026 	return 0;
14027 }
14028 
14029 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
14030 {
14031 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14032 	struct wireless_dev *wdev = info->user_ptr[1];
14033 
14034 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14035 		return -EOPNOTSUPP;
14036 
14037 	cfg80211_stop_nan(rdev, wdev);
14038 
14039 	return 0;
14040 }
14041 
14042 static int validate_nan_filter(struct nlattr *filter_attr)
14043 {
14044 	struct nlattr *attr;
14045 	int len = 0, n_entries = 0, rem;
14046 
14047 	nla_for_each_nested(attr, filter_attr, rem) {
14048 		len += nla_len(attr);
14049 		n_entries++;
14050 	}
14051 
14052 	if (len >= U8_MAX)
14053 		return -EINVAL;
14054 
14055 	return n_entries;
14056 }
14057 
14058 static int handle_nan_filter(struct nlattr *attr_filter,
14059 			     struct cfg80211_nan_func *func,
14060 			     bool tx)
14061 {
14062 	struct nlattr *attr;
14063 	int n_entries, rem, i;
14064 	struct cfg80211_nan_func_filter *filter;
14065 
14066 	n_entries = validate_nan_filter(attr_filter);
14067 	if (n_entries < 0)
14068 		return n_entries;
14069 
14070 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
14071 
14072 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
14073 	if (!filter)
14074 		return -ENOMEM;
14075 
14076 	i = 0;
14077 	nla_for_each_nested(attr, attr_filter, rem) {
14078 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
14079 		if (!filter[i].filter)
14080 			goto err;
14081 
14082 		filter[i].len = nla_len(attr);
14083 		i++;
14084 	}
14085 	if (tx) {
14086 		func->num_tx_filters = n_entries;
14087 		func->tx_filters = filter;
14088 	} else {
14089 		func->num_rx_filters = n_entries;
14090 		func->rx_filters = filter;
14091 	}
14092 
14093 	return 0;
14094 
14095 err:
14096 	i = 0;
14097 	nla_for_each_nested(attr, attr_filter, rem) {
14098 		kfree(filter[i].filter);
14099 		i++;
14100 	}
14101 	kfree(filter);
14102 	return -ENOMEM;
14103 }
14104 
14105 static int nl80211_nan_add_func(struct sk_buff *skb,
14106 				struct genl_info *info)
14107 {
14108 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14109 	struct wireless_dev *wdev = info->user_ptr[1];
14110 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
14111 	struct cfg80211_nan_func *func;
14112 	struct sk_buff *msg = NULL;
14113 	void *hdr = NULL;
14114 	int err = 0;
14115 
14116 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14117 		return -EOPNOTSUPP;
14118 
14119 	if (!wdev_running(wdev))
14120 		return -ENOTCONN;
14121 
14122 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
14123 		return -EINVAL;
14124 
14125 	err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
14126 					  info->attrs[NL80211_ATTR_NAN_FUNC],
14127 					  nl80211_nan_func_policy,
14128 					  info->extack);
14129 	if (err)
14130 		return err;
14131 
14132 	func = kzalloc(sizeof(*func), GFP_KERNEL);
14133 	if (!func)
14134 		return -ENOMEM;
14135 
14136 	func->cookie = cfg80211_assign_cookie(rdev);
14137 
14138 	if (!tb[NL80211_NAN_FUNC_TYPE]) {
14139 		err = -EINVAL;
14140 		goto out;
14141 	}
14142 
14143 
14144 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
14145 
14146 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
14147 		err = -EINVAL;
14148 		goto out;
14149 	}
14150 
14151 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
14152 	       sizeof(func->service_id));
14153 
14154 	func->close_range =
14155 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
14156 
14157 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
14158 		func->serv_spec_info_len =
14159 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
14160 		func->serv_spec_info =
14161 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
14162 				func->serv_spec_info_len,
14163 				GFP_KERNEL);
14164 		if (!func->serv_spec_info) {
14165 			err = -ENOMEM;
14166 			goto out;
14167 		}
14168 	}
14169 
14170 	if (tb[NL80211_NAN_FUNC_TTL])
14171 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
14172 
14173 	switch (func->type) {
14174 	case NL80211_NAN_FUNC_PUBLISH:
14175 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
14176 			err = -EINVAL;
14177 			goto out;
14178 		}
14179 
14180 		func->publish_type =
14181 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
14182 		func->publish_bcast =
14183 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
14184 
14185 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
14186 			func->publish_bcast) {
14187 			err = -EINVAL;
14188 			goto out;
14189 		}
14190 		break;
14191 	case NL80211_NAN_FUNC_SUBSCRIBE:
14192 		func->subscribe_active =
14193 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
14194 		break;
14195 	case NL80211_NAN_FUNC_FOLLOW_UP:
14196 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
14197 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
14198 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
14199 			err = -EINVAL;
14200 			goto out;
14201 		}
14202 
14203 		func->followup_id =
14204 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
14205 		func->followup_reqid =
14206 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
14207 		memcpy(func->followup_dest.addr,
14208 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
14209 		       sizeof(func->followup_dest.addr));
14210 		if (func->ttl) {
14211 			err = -EINVAL;
14212 			goto out;
14213 		}
14214 		break;
14215 	default:
14216 		err = -EINVAL;
14217 		goto out;
14218 	}
14219 
14220 	if (tb[NL80211_NAN_FUNC_SRF]) {
14221 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
14222 
14223 		err = nla_parse_nested_deprecated(srf_tb,
14224 						  NL80211_NAN_SRF_ATTR_MAX,
14225 						  tb[NL80211_NAN_FUNC_SRF],
14226 						  nl80211_nan_srf_policy,
14227 						  info->extack);
14228 		if (err)
14229 			goto out;
14230 
14231 		func->srf_include =
14232 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
14233 
14234 		if (srf_tb[NL80211_NAN_SRF_BF]) {
14235 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
14236 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
14237 				err = -EINVAL;
14238 				goto out;
14239 			}
14240 
14241 			func->srf_bf_len =
14242 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
14243 			func->srf_bf =
14244 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
14245 					func->srf_bf_len, GFP_KERNEL);
14246 			if (!func->srf_bf) {
14247 				err = -ENOMEM;
14248 				goto out;
14249 			}
14250 
14251 			func->srf_bf_idx =
14252 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
14253 		} else {
14254 			struct nlattr *attr, *mac_attr =
14255 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
14256 			int n_entries, rem, i = 0;
14257 
14258 			if (!mac_attr) {
14259 				err = -EINVAL;
14260 				goto out;
14261 			}
14262 
14263 			n_entries = validate_acl_mac_addrs(mac_attr);
14264 			if (n_entries <= 0) {
14265 				err = -EINVAL;
14266 				goto out;
14267 			}
14268 
14269 			func->srf_num_macs = n_entries;
14270 			func->srf_macs =
14271 				kcalloc(n_entries, sizeof(*func->srf_macs),
14272 					GFP_KERNEL);
14273 			if (!func->srf_macs) {
14274 				err = -ENOMEM;
14275 				goto out;
14276 			}
14277 
14278 			nla_for_each_nested(attr, mac_attr, rem)
14279 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
14280 				       sizeof(*func->srf_macs));
14281 		}
14282 	}
14283 
14284 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
14285 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
14286 					func, true);
14287 		if (err)
14288 			goto out;
14289 	}
14290 
14291 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
14292 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
14293 					func, false);
14294 		if (err)
14295 			goto out;
14296 	}
14297 
14298 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14299 	if (!msg) {
14300 		err = -ENOMEM;
14301 		goto out;
14302 	}
14303 
14304 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14305 			     NL80211_CMD_ADD_NAN_FUNCTION);
14306 	/* This can't really happen - we just allocated 4KB */
14307 	if (WARN_ON(!hdr)) {
14308 		err = -ENOMEM;
14309 		goto out;
14310 	}
14311 
14312 	err = rdev_add_nan_func(rdev, wdev, func);
14313 out:
14314 	if (err < 0) {
14315 		cfg80211_free_nan_func(func);
14316 		nlmsg_free(msg);
14317 		return err;
14318 	}
14319 
14320 	/* propagate the instance id and cookie to userspace  */
14321 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
14322 			      NL80211_ATTR_PAD))
14323 		goto nla_put_failure;
14324 
14325 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14326 	if (!func_attr)
14327 		goto nla_put_failure;
14328 
14329 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
14330 		       func->instance_id))
14331 		goto nla_put_failure;
14332 
14333 	nla_nest_end(msg, func_attr);
14334 
14335 	genlmsg_end(msg, hdr);
14336 	return genlmsg_reply(msg, info);
14337 
14338 nla_put_failure:
14339 	nlmsg_free(msg);
14340 	return -ENOBUFS;
14341 }
14342 
14343 static int nl80211_nan_del_func(struct sk_buff *skb,
14344 			       struct genl_info *info)
14345 {
14346 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14347 	struct wireless_dev *wdev = info->user_ptr[1];
14348 	u64 cookie;
14349 
14350 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14351 		return -EOPNOTSUPP;
14352 
14353 	if (!wdev_running(wdev))
14354 		return -ENOTCONN;
14355 
14356 	if (!info->attrs[NL80211_ATTR_COOKIE])
14357 		return -EINVAL;
14358 
14359 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
14360 
14361 	rdev_del_nan_func(rdev, wdev, cookie);
14362 
14363 	return 0;
14364 }
14365 
14366 static int nl80211_nan_change_config(struct sk_buff *skb,
14367 				     struct genl_info *info)
14368 {
14369 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14370 	struct wireless_dev *wdev = info->user_ptr[1];
14371 	struct cfg80211_nan_conf conf = {};
14372 	u32 changed = 0;
14373 
14374 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14375 		return -EOPNOTSUPP;
14376 
14377 	if (!wdev_running(wdev))
14378 		return -ENOTCONN;
14379 
14380 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
14381 		conf.master_pref =
14382 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14383 		if (conf.master_pref <= 1 || conf.master_pref == 255)
14384 			return -EINVAL;
14385 
14386 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
14387 	}
14388 
14389 	if (info->attrs[NL80211_ATTR_BANDS]) {
14390 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14391 
14392 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14393 			return -EOPNOTSUPP;
14394 
14395 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14396 			return -EINVAL;
14397 
14398 		conf.bands = bands;
14399 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
14400 	}
14401 
14402 	if (!changed)
14403 		return -EINVAL;
14404 
14405 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
14406 }
14407 
14408 void cfg80211_nan_match(struct wireless_dev *wdev,
14409 			struct cfg80211_nan_match_params *match, gfp_t gfp)
14410 {
14411 	struct wiphy *wiphy = wdev->wiphy;
14412 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14413 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
14414 	struct sk_buff *msg;
14415 	void *hdr;
14416 
14417 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
14418 		return;
14419 
14420 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14421 	if (!msg)
14422 		return;
14423 
14424 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
14425 	if (!hdr) {
14426 		nlmsg_free(msg);
14427 		return;
14428 	}
14429 
14430 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14431 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14432 					 wdev->netdev->ifindex)) ||
14433 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14434 			      NL80211_ATTR_PAD))
14435 		goto nla_put_failure;
14436 
14437 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
14438 			      NL80211_ATTR_PAD) ||
14439 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
14440 		goto nla_put_failure;
14441 
14442 	match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
14443 	if (!match_attr)
14444 		goto nla_put_failure;
14445 
14446 	local_func_attr = nla_nest_start_noflag(msg,
14447 						NL80211_NAN_MATCH_FUNC_LOCAL);
14448 	if (!local_func_attr)
14449 		goto nla_put_failure;
14450 
14451 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
14452 		goto nla_put_failure;
14453 
14454 	nla_nest_end(msg, local_func_attr);
14455 
14456 	peer_func_attr = nla_nest_start_noflag(msg,
14457 					       NL80211_NAN_MATCH_FUNC_PEER);
14458 	if (!peer_func_attr)
14459 		goto nla_put_failure;
14460 
14461 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
14462 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
14463 		goto nla_put_failure;
14464 
14465 	if (match->info && match->info_len &&
14466 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
14467 		    match->info))
14468 		goto nla_put_failure;
14469 
14470 	nla_nest_end(msg, peer_func_attr);
14471 	nla_nest_end(msg, match_attr);
14472 	genlmsg_end(msg, hdr);
14473 
14474 	if (!wdev->owner_nlportid)
14475 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14476 					msg, 0, NL80211_MCGRP_NAN, gfp);
14477 	else
14478 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14479 				wdev->owner_nlportid);
14480 
14481 	return;
14482 
14483 nla_put_failure:
14484 	nlmsg_free(msg);
14485 }
14486 EXPORT_SYMBOL(cfg80211_nan_match);
14487 
14488 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
14489 				  u8 inst_id,
14490 				  enum nl80211_nan_func_term_reason reason,
14491 				  u64 cookie, gfp_t gfp)
14492 {
14493 	struct wiphy *wiphy = wdev->wiphy;
14494 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14495 	struct sk_buff *msg;
14496 	struct nlattr *func_attr;
14497 	void *hdr;
14498 
14499 	if (WARN_ON(!inst_id))
14500 		return;
14501 
14502 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14503 	if (!msg)
14504 		return;
14505 
14506 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
14507 	if (!hdr) {
14508 		nlmsg_free(msg);
14509 		return;
14510 	}
14511 
14512 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14513 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14514 					 wdev->netdev->ifindex)) ||
14515 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14516 			      NL80211_ATTR_PAD))
14517 		goto nla_put_failure;
14518 
14519 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14520 			      NL80211_ATTR_PAD))
14521 		goto nla_put_failure;
14522 
14523 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14524 	if (!func_attr)
14525 		goto nla_put_failure;
14526 
14527 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
14528 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
14529 		goto nla_put_failure;
14530 
14531 	nla_nest_end(msg, func_attr);
14532 	genlmsg_end(msg, hdr);
14533 
14534 	if (!wdev->owner_nlportid)
14535 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14536 					msg, 0, NL80211_MCGRP_NAN, gfp);
14537 	else
14538 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14539 				wdev->owner_nlportid);
14540 
14541 	return;
14542 
14543 nla_put_failure:
14544 	nlmsg_free(msg);
14545 }
14546 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
14547 
14548 static int nl80211_get_protocol_features(struct sk_buff *skb,
14549 					 struct genl_info *info)
14550 {
14551 	void *hdr;
14552 	struct sk_buff *msg;
14553 
14554 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14555 	if (!msg)
14556 		return -ENOMEM;
14557 
14558 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14559 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
14560 	if (!hdr)
14561 		goto nla_put_failure;
14562 
14563 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
14564 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
14565 		goto nla_put_failure;
14566 
14567 	genlmsg_end(msg, hdr);
14568 	return genlmsg_reply(msg, info);
14569 
14570  nla_put_failure:
14571 	kfree_skb(msg);
14572 	return -ENOBUFS;
14573 }
14574 
14575 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
14576 {
14577 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14578 	struct cfg80211_update_ft_ies_params ft_params;
14579 	struct net_device *dev = info->user_ptr[1];
14580 
14581 	if (!rdev->ops->update_ft_ies)
14582 		return -EOPNOTSUPP;
14583 
14584 	if (!info->attrs[NL80211_ATTR_MDID] ||
14585 	    !info->attrs[NL80211_ATTR_IE])
14586 		return -EINVAL;
14587 
14588 	memset(&ft_params, 0, sizeof(ft_params));
14589 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
14590 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
14591 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
14592 
14593 	return rdev_update_ft_ies(rdev, dev, &ft_params);
14594 }
14595 
14596 static int nl80211_crit_protocol_start(struct sk_buff *skb,
14597 				       struct genl_info *info)
14598 {
14599 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14600 	struct wireless_dev *wdev = info->user_ptr[1];
14601 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
14602 	u16 duration;
14603 	int ret;
14604 
14605 	if (!rdev->ops->crit_proto_start)
14606 		return -EOPNOTSUPP;
14607 
14608 	if (WARN_ON(!rdev->ops->crit_proto_stop))
14609 		return -EINVAL;
14610 
14611 	if (rdev->crit_proto_nlportid)
14612 		return -EBUSY;
14613 
14614 	/* determine protocol if provided */
14615 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
14616 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
14617 
14618 	if (proto >= NUM_NL80211_CRIT_PROTO)
14619 		return -EINVAL;
14620 
14621 	/* timeout must be provided */
14622 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
14623 		return -EINVAL;
14624 
14625 	duration =
14626 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
14627 
14628 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
14629 	if (!ret)
14630 		rdev->crit_proto_nlportid = info->snd_portid;
14631 
14632 	return ret;
14633 }
14634 
14635 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
14636 				      struct genl_info *info)
14637 {
14638 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14639 	struct wireless_dev *wdev = info->user_ptr[1];
14640 
14641 	if (!rdev->ops->crit_proto_stop)
14642 		return -EOPNOTSUPP;
14643 
14644 	if (rdev->crit_proto_nlportid) {
14645 		rdev->crit_proto_nlportid = 0;
14646 		rdev_crit_proto_stop(rdev, wdev);
14647 	}
14648 	return 0;
14649 }
14650 
14651 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
14652 				       struct nlattr *attr,
14653 				       struct netlink_ext_ack *extack)
14654 {
14655 	if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
14656 		if (attr->nla_type & NLA_F_NESTED) {
14657 			NL_SET_ERR_MSG_ATTR(extack, attr,
14658 					    "unexpected nested data");
14659 			return -EINVAL;
14660 		}
14661 
14662 		return 0;
14663 	}
14664 
14665 	if (!(attr->nla_type & NLA_F_NESTED)) {
14666 		NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
14667 		return -EINVAL;
14668 	}
14669 
14670 	return nla_validate_nested(attr, vcmd->maxattr, vcmd->policy, extack);
14671 }
14672 
14673 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
14674 {
14675 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14676 	struct wireless_dev *wdev =
14677 		__cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
14678 					   info->attrs);
14679 	int i, err;
14680 	u32 vid, subcmd;
14681 
14682 	if (!rdev->wiphy.vendor_commands)
14683 		return -EOPNOTSUPP;
14684 
14685 	if (IS_ERR(wdev)) {
14686 		err = PTR_ERR(wdev);
14687 		if (err != -EINVAL)
14688 			return err;
14689 		wdev = NULL;
14690 	} else if (wdev->wiphy != &rdev->wiphy) {
14691 		return -EINVAL;
14692 	}
14693 
14694 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
14695 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
14696 		return -EINVAL;
14697 
14698 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
14699 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
14700 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
14701 		const struct wiphy_vendor_command *vcmd;
14702 		void *data = NULL;
14703 		int len = 0;
14704 
14705 		vcmd = &rdev->wiphy.vendor_commands[i];
14706 
14707 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
14708 			continue;
14709 
14710 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
14711 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
14712 			if (!wdev)
14713 				return -EINVAL;
14714 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
14715 			    !wdev->netdev)
14716 				return -EINVAL;
14717 
14718 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
14719 				if (!wdev_running(wdev))
14720 					return -ENETDOWN;
14721 			}
14722 		} else {
14723 			wdev = NULL;
14724 		}
14725 
14726 		if (!vcmd->doit)
14727 			return -EOPNOTSUPP;
14728 
14729 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
14730 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
14731 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
14732 
14733 			err = nl80211_vendor_check_policy(vcmd,
14734 					info->attrs[NL80211_ATTR_VENDOR_DATA],
14735 					info->extack);
14736 			if (err)
14737 				return err;
14738 		}
14739 
14740 		rdev->cur_cmd_info = info;
14741 		err = vcmd->doit(&rdev->wiphy, wdev, data, len);
14742 		rdev->cur_cmd_info = NULL;
14743 		return err;
14744 	}
14745 
14746 	return -EOPNOTSUPP;
14747 }
14748 
14749 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
14750 				       struct netlink_callback *cb,
14751 				       struct cfg80211_registered_device **rdev,
14752 				       struct wireless_dev **wdev)
14753 {
14754 	struct nlattr **attrbuf;
14755 	u32 vid, subcmd;
14756 	unsigned int i;
14757 	int vcmd_idx = -1;
14758 	int err;
14759 	void *data = NULL;
14760 	unsigned int data_len = 0;
14761 
14762 	if (cb->args[0]) {
14763 		/* subtract the 1 again here */
14764 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
14765 		struct wireless_dev *tmp;
14766 
14767 		if (!wiphy)
14768 			return -ENODEV;
14769 		*rdev = wiphy_to_rdev(wiphy);
14770 		*wdev = NULL;
14771 
14772 		if (cb->args[1]) {
14773 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
14774 				if (tmp->identifier == cb->args[1] - 1) {
14775 					*wdev = tmp;
14776 					break;
14777 				}
14778 			}
14779 		}
14780 
14781 		/* keep rtnl locked in successful case */
14782 		return 0;
14783 	}
14784 
14785 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
14786 	if (!attrbuf)
14787 		return -ENOMEM;
14788 
14789 	err = nlmsg_parse_deprecated(cb->nlh,
14790 				     GENL_HDRLEN + nl80211_fam.hdrsize,
14791 				     attrbuf, nl80211_fam.maxattr,
14792 				     nl80211_policy, NULL);
14793 	if (err)
14794 		goto out;
14795 
14796 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
14797 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
14798 		err = -EINVAL;
14799 		goto out;
14800 	}
14801 
14802 	*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(skb->sk), attrbuf);
14803 	if (IS_ERR(*wdev))
14804 		*wdev = NULL;
14805 
14806 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
14807 	if (IS_ERR(*rdev)) {
14808 		err = PTR_ERR(*rdev);
14809 		goto out;
14810 	}
14811 
14812 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
14813 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
14814 
14815 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
14816 		const struct wiphy_vendor_command *vcmd;
14817 
14818 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
14819 
14820 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
14821 			continue;
14822 
14823 		if (!vcmd->dumpit) {
14824 			err = -EOPNOTSUPP;
14825 			goto out;
14826 		}
14827 
14828 		vcmd_idx = i;
14829 		break;
14830 	}
14831 
14832 	if (vcmd_idx < 0) {
14833 		err = -EOPNOTSUPP;
14834 		goto out;
14835 	}
14836 
14837 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
14838 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
14839 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
14840 
14841 		err = nl80211_vendor_check_policy(
14842 				&(*rdev)->wiphy.vendor_commands[vcmd_idx],
14843 				attrbuf[NL80211_ATTR_VENDOR_DATA],
14844 				cb->extack);
14845 		if (err)
14846 			goto out;
14847 	}
14848 
14849 	/* 0 is the first index - add 1 to parse only once */
14850 	cb->args[0] = (*rdev)->wiphy_idx + 1;
14851 	/* add 1 to know if it was NULL */
14852 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
14853 	cb->args[2] = vcmd_idx;
14854 	cb->args[3] = (unsigned long)data;
14855 	cb->args[4] = data_len;
14856 
14857 	/* keep rtnl locked in successful case */
14858 	err = 0;
14859 out:
14860 	kfree(attrbuf);
14861 	return err;
14862 }
14863 
14864 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
14865 				   struct netlink_callback *cb)
14866 {
14867 	struct cfg80211_registered_device *rdev;
14868 	struct wireless_dev *wdev;
14869 	unsigned int vcmd_idx;
14870 	const struct wiphy_vendor_command *vcmd;
14871 	void *data;
14872 	int data_len;
14873 	int err;
14874 	struct nlattr *vendor_data;
14875 
14876 	rtnl_lock();
14877 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
14878 	if (err)
14879 		goto out;
14880 
14881 	vcmd_idx = cb->args[2];
14882 	data = (void *)cb->args[3];
14883 	data_len = cb->args[4];
14884 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
14885 
14886 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
14887 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
14888 		if (!wdev) {
14889 			err = -EINVAL;
14890 			goto out;
14891 		}
14892 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
14893 		    !wdev->netdev) {
14894 			err = -EINVAL;
14895 			goto out;
14896 		}
14897 
14898 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
14899 			if (!wdev_running(wdev)) {
14900 				err = -ENETDOWN;
14901 				goto out;
14902 			}
14903 		}
14904 	}
14905 
14906 	while (1) {
14907 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
14908 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
14909 					   NL80211_CMD_VENDOR);
14910 		if (!hdr)
14911 			break;
14912 
14913 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14914 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
14915 					       wdev_id(wdev),
14916 					       NL80211_ATTR_PAD))) {
14917 			genlmsg_cancel(skb, hdr);
14918 			break;
14919 		}
14920 
14921 		vendor_data = nla_nest_start_noflag(skb,
14922 						    NL80211_ATTR_VENDOR_DATA);
14923 		if (!vendor_data) {
14924 			genlmsg_cancel(skb, hdr);
14925 			break;
14926 		}
14927 
14928 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
14929 				   (unsigned long *)&cb->args[5]);
14930 		nla_nest_end(skb, vendor_data);
14931 
14932 		if (err == -ENOBUFS || err == -ENOENT) {
14933 			genlmsg_cancel(skb, hdr);
14934 			break;
14935 		} else if (err <= 0) {
14936 			genlmsg_cancel(skb, hdr);
14937 			goto out;
14938 		}
14939 
14940 		genlmsg_end(skb, hdr);
14941 	}
14942 
14943 	err = skb->len;
14944  out:
14945 	rtnl_unlock();
14946 	return err;
14947 }
14948 
14949 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
14950 					   enum nl80211_commands cmd,
14951 					   enum nl80211_attrs attr,
14952 					   int approxlen)
14953 {
14954 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14955 
14956 	if (WARN_ON(!rdev->cur_cmd_info))
14957 		return NULL;
14958 
14959 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
14960 					   rdev->cur_cmd_info->snd_portid,
14961 					   rdev->cur_cmd_info->snd_seq,
14962 					   cmd, attr, NULL, GFP_KERNEL);
14963 }
14964 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
14965 
14966 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
14967 {
14968 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
14969 	void *hdr = ((void **)skb->cb)[1];
14970 	struct nlattr *data = ((void **)skb->cb)[2];
14971 
14972 	/* clear CB data for netlink core to own from now on */
14973 	memset(skb->cb, 0, sizeof(skb->cb));
14974 
14975 	if (WARN_ON(!rdev->cur_cmd_info)) {
14976 		kfree_skb(skb);
14977 		return -EINVAL;
14978 	}
14979 
14980 	nla_nest_end(skb, data);
14981 	genlmsg_end(skb, hdr);
14982 	return genlmsg_reply(skb, rdev->cur_cmd_info);
14983 }
14984 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
14985 
14986 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
14987 {
14988 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14989 
14990 	if (WARN_ON(!rdev->cur_cmd_info))
14991 		return 0;
14992 
14993 	return rdev->cur_cmd_info->snd_portid;
14994 }
14995 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
14996 
14997 static int nl80211_set_qos_map(struct sk_buff *skb,
14998 			       struct genl_info *info)
14999 {
15000 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15001 	struct cfg80211_qos_map *qos_map = NULL;
15002 	struct net_device *dev = info->user_ptr[1];
15003 	u8 *pos, len, num_des, des_len, des;
15004 	int ret;
15005 
15006 	if (!rdev->ops->set_qos_map)
15007 		return -EOPNOTSUPP;
15008 
15009 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
15010 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
15011 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
15012 
15013 		if (len % 2)
15014 			return -EINVAL;
15015 
15016 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
15017 		if (!qos_map)
15018 			return -ENOMEM;
15019 
15020 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
15021 		if (num_des) {
15022 			des_len = num_des *
15023 				sizeof(struct cfg80211_dscp_exception);
15024 			memcpy(qos_map->dscp_exception, pos, des_len);
15025 			qos_map->num_des = num_des;
15026 			for (des = 0; des < num_des; des++) {
15027 				if (qos_map->dscp_exception[des].up > 7) {
15028 					kfree(qos_map);
15029 					return -EINVAL;
15030 				}
15031 			}
15032 			pos += des_len;
15033 		}
15034 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
15035 	}
15036 
15037 	wdev_lock(dev->ieee80211_ptr);
15038 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
15039 	if (!ret)
15040 		ret = rdev_set_qos_map(rdev, dev, qos_map);
15041 	wdev_unlock(dev->ieee80211_ptr);
15042 
15043 	kfree(qos_map);
15044 	return ret;
15045 }
15046 
15047 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
15048 {
15049 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15050 	struct net_device *dev = info->user_ptr[1];
15051 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15052 	const u8 *peer;
15053 	u8 tsid, up;
15054 	u16 admitted_time = 0;
15055 	int err;
15056 
15057 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
15058 		return -EOPNOTSUPP;
15059 
15060 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
15061 	    !info->attrs[NL80211_ATTR_USER_PRIO])
15062 		return -EINVAL;
15063 
15064 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15065 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
15066 
15067 	/* WMM uses TIDs 0-7 even for TSPEC */
15068 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
15069 		/* TODO: handle 802.11 TSPEC/admission control
15070 		 * need more attributes for that (e.g. BA session requirement);
15071 		 * change the WMM adminssion test above to allow both then
15072 		 */
15073 		return -EINVAL;
15074 	}
15075 
15076 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15077 
15078 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
15079 		admitted_time =
15080 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
15081 		if (!admitted_time)
15082 			return -EINVAL;
15083 	}
15084 
15085 	wdev_lock(wdev);
15086 	switch (wdev->iftype) {
15087 	case NL80211_IFTYPE_STATION:
15088 	case NL80211_IFTYPE_P2P_CLIENT:
15089 		if (wdev->connected)
15090 			break;
15091 		err = -ENOTCONN;
15092 		goto out;
15093 	default:
15094 		err = -EOPNOTSUPP;
15095 		goto out;
15096 	}
15097 
15098 	err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
15099 
15100  out:
15101 	wdev_unlock(wdev);
15102 	return err;
15103 }
15104 
15105 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
15106 {
15107 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15108 	struct net_device *dev = info->user_ptr[1];
15109 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15110 	const u8 *peer;
15111 	u8 tsid;
15112 	int err;
15113 
15114 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
15115 		return -EINVAL;
15116 
15117 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15118 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15119 
15120 	wdev_lock(wdev);
15121 	err = rdev_del_tx_ts(rdev, dev, tsid, peer);
15122 	wdev_unlock(wdev);
15123 
15124 	return err;
15125 }
15126 
15127 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
15128 				       struct genl_info *info)
15129 {
15130 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15131 	struct net_device *dev = info->user_ptr[1];
15132 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15133 	struct cfg80211_chan_def chandef = {};
15134 	const u8 *addr;
15135 	u8 oper_class;
15136 	int err;
15137 
15138 	if (!rdev->ops->tdls_channel_switch ||
15139 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15140 		return -EOPNOTSUPP;
15141 
15142 	switch (dev->ieee80211_ptr->iftype) {
15143 	case NL80211_IFTYPE_STATION:
15144 	case NL80211_IFTYPE_P2P_CLIENT:
15145 		break;
15146 	default:
15147 		return -EOPNOTSUPP;
15148 	}
15149 
15150 	if (!info->attrs[NL80211_ATTR_MAC] ||
15151 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
15152 		return -EINVAL;
15153 
15154 	err = nl80211_parse_chandef(rdev, info, &chandef);
15155 	if (err)
15156 		return err;
15157 
15158 	/*
15159 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
15160 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
15161 	 * specification is not defined for them.
15162 	 */
15163 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
15164 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
15165 	    chandef.width != NL80211_CHAN_WIDTH_20)
15166 		return -EINVAL;
15167 
15168 	/* we will be active on the TDLS link */
15169 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
15170 					   wdev->iftype))
15171 		return -EINVAL;
15172 
15173 	/* don't allow switching to DFS channels */
15174 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
15175 		return -EINVAL;
15176 
15177 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15178 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
15179 
15180 	wdev_lock(wdev);
15181 	err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
15182 	wdev_unlock(wdev);
15183 
15184 	return err;
15185 }
15186 
15187 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
15188 					      struct genl_info *info)
15189 {
15190 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15191 	struct net_device *dev = info->user_ptr[1];
15192 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15193 	const u8 *addr;
15194 
15195 	if (!rdev->ops->tdls_channel_switch ||
15196 	    !rdev->ops->tdls_cancel_channel_switch ||
15197 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15198 		return -EOPNOTSUPP;
15199 
15200 	switch (dev->ieee80211_ptr->iftype) {
15201 	case NL80211_IFTYPE_STATION:
15202 	case NL80211_IFTYPE_P2P_CLIENT:
15203 		break;
15204 	default:
15205 		return -EOPNOTSUPP;
15206 	}
15207 
15208 	if (!info->attrs[NL80211_ATTR_MAC])
15209 		return -EINVAL;
15210 
15211 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15212 
15213 	wdev_lock(wdev);
15214 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
15215 	wdev_unlock(wdev);
15216 
15217 	return 0;
15218 }
15219 
15220 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
15221 					    struct genl_info *info)
15222 {
15223 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15224 	struct net_device *dev = info->user_ptr[1];
15225 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15226 	const struct nlattr *nla;
15227 	bool enabled;
15228 
15229 	if (!rdev->ops->set_multicast_to_unicast)
15230 		return -EOPNOTSUPP;
15231 
15232 	if (wdev->iftype != NL80211_IFTYPE_AP &&
15233 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
15234 		return -EOPNOTSUPP;
15235 
15236 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
15237 	enabled = nla_get_flag(nla);
15238 
15239 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
15240 }
15241 
15242 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
15243 {
15244 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15245 	struct net_device *dev = info->user_ptr[1];
15246 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15247 	struct cfg80211_pmk_conf pmk_conf = {};
15248 	int ret;
15249 
15250 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15251 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15252 		return -EOPNOTSUPP;
15253 
15254 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15255 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15256 		return -EOPNOTSUPP;
15257 
15258 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
15259 		return -EINVAL;
15260 
15261 	wdev_lock(wdev);
15262 	if (!wdev->connected) {
15263 		ret = -ENOTCONN;
15264 		goto out;
15265 	}
15266 
15267 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15268 	if (memcmp(pmk_conf.aa, wdev->u.client.connected_addr, ETH_ALEN)) {
15269 		ret = -EINVAL;
15270 		goto out;
15271 	}
15272 
15273 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
15274 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
15275 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
15276 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
15277 		ret = -EINVAL;
15278 		goto out;
15279 	}
15280 
15281 	if (info->attrs[NL80211_ATTR_PMKR0_NAME])
15282 		pmk_conf.pmk_r0_name =
15283 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
15284 
15285 	ret = rdev_set_pmk(rdev, dev, &pmk_conf);
15286 out:
15287 	wdev_unlock(wdev);
15288 	return ret;
15289 }
15290 
15291 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
15292 {
15293 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15294 	struct net_device *dev = info->user_ptr[1];
15295 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15296 	const u8 *aa;
15297 	int ret;
15298 
15299 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15300 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15301 		return -EOPNOTSUPP;
15302 
15303 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15304 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15305 		return -EOPNOTSUPP;
15306 
15307 	if (!info->attrs[NL80211_ATTR_MAC])
15308 		return -EINVAL;
15309 
15310 	wdev_lock(wdev);
15311 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15312 	ret = rdev_del_pmk(rdev, dev, aa);
15313 	wdev_unlock(wdev);
15314 
15315 	return ret;
15316 }
15317 
15318 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
15319 {
15320 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15321 	struct net_device *dev = info->user_ptr[1];
15322 	struct cfg80211_external_auth_params params;
15323 
15324 	if (!rdev->ops->external_auth)
15325 		return -EOPNOTSUPP;
15326 
15327 	if (!info->attrs[NL80211_ATTR_SSID] &&
15328 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
15329 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
15330 		return -EINVAL;
15331 
15332 	if (!info->attrs[NL80211_ATTR_BSSID])
15333 		return -EINVAL;
15334 
15335 	if (!info->attrs[NL80211_ATTR_STATUS_CODE])
15336 		return -EINVAL;
15337 
15338 	memset(&params, 0, sizeof(params));
15339 
15340 	if (info->attrs[NL80211_ATTR_SSID]) {
15341 		params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
15342 		if (params.ssid.ssid_len == 0)
15343 			return -EINVAL;
15344 		memcpy(params.ssid.ssid,
15345 		       nla_data(info->attrs[NL80211_ATTR_SSID]),
15346 		       params.ssid.ssid_len);
15347 	}
15348 
15349 	memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
15350 	       ETH_ALEN);
15351 
15352 	params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15353 
15354 	if (info->attrs[NL80211_ATTR_PMKID])
15355 		params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
15356 
15357 	return rdev_external_auth(rdev, dev, &params);
15358 }
15359 
15360 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
15361 {
15362 	bool dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
15363 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15364 	struct net_device *dev = info->user_ptr[1];
15365 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15366 	const u8 *buf;
15367 	size_t len;
15368 	u8 *dest;
15369 	u16 proto;
15370 	bool noencrypt;
15371 	u64 cookie = 0;
15372 	int link_id;
15373 	int err;
15374 
15375 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15376 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
15377 		return -EOPNOTSUPP;
15378 
15379 	if (!rdev->ops->tx_control_port)
15380 		return -EOPNOTSUPP;
15381 
15382 	if (!info->attrs[NL80211_ATTR_FRAME] ||
15383 	    !info->attrs[NL80211_ATTR_MAC] ||
15384 	    !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
15385 		GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
15386 		return -EINVAL;
15387 	}
15388 
15389 	wdev_lock(wdev);
15390 
15391 	switch (wdev->iftype) {
15392 	case NL80211_IFTYPE_AP:
15393 	case NL80211_IFTYPE_P2P_GO:
15394 	case NL80211_IFTYPE_MESH_POINT:
15395 		break;
15396 	case NL80211_IFTYPE_ADHOC:
15397 		if (wdev->u.ibss.current_bss)
15398 			break;
15399 		err = -ENOTCONN;
15400 		goto out;
15401 	case NL80211_IFTYPE_STATION:
15402 	case NL80211_IFTYPE_P2P_CLIENT:
15403 		if (wdev->connected)
15404 			break;
15405 		err = -ENOTCONN;
15406 		goto out;
15407 	default:
15408 		err = -EOPNOTSUPP;
15409 		goto out;
15410 	}
15411 
15412 	wdev_unlock(wdev);
15413 
15414 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15415 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15416 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15417 	proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
15418 	noencrypt =
15419 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
15420 
15421 	link_id = nl80211_link_id_or_invalid(info->attrs);
15422 
15423 	err = rdev_tx_control_port(rdev, dev, buf, len,
15424 				   dest, cpu_to_be16(proto), noencrypt, link_id,
15425 				   dont_wait_for_ack ? NULL : &cookie);
15426 	if (!err && !dont_wait_for_ack)
15427 		nl_set_extack_cookie_u64(info->extack, cookie);
15428 	return err;
15429  out:
15430 	wdev_unlock(wdev);
15431 	return err;
15432 }
15433 
15434 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
15435 					   struct genl_info *info)
15436 {
15437 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15438 	struct net_device *dev = info->user_ptr[1];
15439 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15440 	struct cfg80211_ftm_responder_stats ftm_stats = {};
15441 	unsigned int link_id = nl80211_link_id(info->attrs);
15442 	struct sk_buff *msg;
15443 	void *hdr;
15444 	struct nlattr *ftm_stats_attr;
15445 	int err;
15446 
15447 	if (wdev->iftype != NL80211_IFTYPE_AP ||
15448 	    !wdev->links[link_id].ap.beacon_interval)
15449 		return -EOPNOTSUPP;
15450 
15451 	err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
15452 	if (err)
15453 		return err;
15454 
15455 	if (!ftm_stats.filled)
15456 		return -ENODATA;
15457 
15458 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15459 	if (!msg)
15460 		return -ENOMEM;
15461 
15462 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
15463 			     NL80211_CMD_GET_FTM_RESPONDER_STATS);
15464 	if (!hdr)
15465 		goto nla_put_failure;
15466 
15467 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15468 		goto nla_put_failure;
15469 
15470 	ftm_stats_attr = nla_nest_start_noflag(msg,
15471 					       NL80211_ATTR_FTM_RESPONDER_STATS);
15472 	if (!ftm_stats_attr)
15473 		goto nla_put_failure;
15474 
15475 #define SET_FTM(field, name, type)					 \
15476 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15477 	    nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,		 \
15478 			     ftm_stats.field))				 \
15479 		goto nla_put_failure; } while (0)
15480 #define SET_FTM_U64(field, name)					 \
15481 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15482 	    nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,		 \
15483 			      ftm_stats.field, NL80211_FTM_STATS_PAD))	 \
15484 		goto nla_put_failure; } while (0)
15485 
15486 	SET_FTM(success_num, SUCCESS_NUM, u32);
15487 	SET_FTM(partial_num, PARTIAL_NUM, u32);
15488 	SET_FTM(failed_num, FAILED_NUM, u32);
15489 	SET_FTM(asap_num, ASAP_NUM, u32);
15490 	SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
15491 	SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
15492 	SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
15493 	SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
15494 	SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
15495 #undef SET_FTM
15496 
15497 	nla_nest_end(msg, ftm_stats_attr);
15498 
15499 	genlmsg_end(msg, hdr);
15500 	return genlmsg_reply(msg, info);
15501 
15502 nla_put_failure:
15503 	nlmsg_free(msg);
15504 	return -ENOBUFS;
15505 }
15506 
15507 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
15508 {
15509 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15510 	struct cfg80211_update_owe_info owe_info;
15511 	struct net_device *dev = info->user_ptr[1];
15512 
15513 	if (!rdev->ops->update_owe_info)
15514 		return -EOPNOTSUPP;
15515 
15516 	if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
15517 	    !info->attrs[NL80211_ATTR_MAC])
15518 		return -EINVAL;
15519 
15520 	memset(&owe_info, 0, sizeof(owe_info));
15521 	owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15522 	nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
15523 
15524 	if (info->attrs[NL80211_ATTR_IE]) {
15525 		owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
15526 		owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
15527 	}
15528 
15529 	return rdev_update_owe_info(rdev, dev, &owe_info);
15530 }
15531 
15532 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
15533 {
15534 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15535 	struct net_device *dev = info->user_ptr[1];
15536 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15537 	struct station_info sinfo = {};
15538 	const u8 *buf;
15539 	size_t len;
15540 	u8 *dest;
15541 	int err;
15542 
15543 	if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
15544 		return -EOPNOTSUPP;
15545 
15546 	if (!info->attrs[NL80211_ATTR_MAC] ||
15547 	    !info->attrs[NL80211_ATTR_FRAME]) {
15548 		GENL_SET_ERR_MSG(info, "Frame or MAC missing");
15549 		return -EINVAL;
15550 	}
15551 
15552 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
15553 		return -EOPNOTSUPP;
15554 
15555 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15556 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15557 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15558 
15559 	if (len < sizeof(struct ethhdr))
15560 		return -EINVAL;
15561 
15562 	if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
15563 	    !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
15564 		return -EINVAL;
15565 
15566 	err = rdev_get_station(rdev, dev, dest, &sinfo);
15567 	if (err)
15568 		return err;
15569 
15570 	cfg80211_sinfo_release_content(&sinfo);
15571 
15572 	return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
15573 }
15574 
15575 static int parse_tid_conf(struct cfg80211_registered_device *rdev,
15576 			  struct nlattr *attrs[], struct net_device *dev,
15577 			  struct cfg80211_tid_cfg *tid_conf,
15578 			  struct genl_info *info, const u8 *peer,
15579 			  unsigned int link_id)
15580 {
15581 	struct netlink_ext_ack *extack = info->extack;
15582 	u64 mask;
15583 	int err;
15584 
15585 	if (!attrs[NL80211_TID_CONFIG_ATTR_TIDS])
15586 		return -EINVAL;
15587 
15588 	tid_conf->config_override =
15589 			nla_get_flag(attrs[NL80211_TID_CONFIG_ATTR_OVERRIDE]);
15590 	tid_conf->tids = nla_get_u16(attrs[NL80211_TID_CONFIG_ATTR_TIDS]);
15591 
15592 	if (tid_conf->config_override) {
15593 		if (rdev->ops->reset_tid_config) {
15594 			err = rdev_reset_tid_config(rdev, dev, peer,
15595 						    tid_conf->tids);
15596 			if (err)
15597 				return err;
15598 		} else {
15599 			return -EINVAL;
15600 		}
15601 	}
15602 
15603 	if (attrs[NL80211_TID_CONFIG_ATTR_NOACK]) {
15604 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_NOACK);
15605 		tid_conf->noack =
15606 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_NOACK]);
15607 	}
15608 
15609 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]) {
15610 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT);
15611 		tid_conf->retry_short =
15612 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]);
15613 
15614 		if (tid_conf->retry_short > rdev->wiphy.max_data_retry_count)
15615 			return -EINVAL;
15616 	}
15617 
15618 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]) {
15619 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
15620 		tid_conf->retry_long =
15621 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]);
15622 
15623 		if (tid_conf->retry_long > rdev->wiphy.max_data_retry_count)
15624 			return -EINVAL;
15625 	}
15626 
15627 	if (attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]) {
15628 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
15629 		tid_conf->ampdu =
15630 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]);
15631 	}
15632 
15633 	if (attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]) {
15634 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
15635 		tid_conf->rtscts =
15636 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]);
15637 	}
15638 
15639 	if (attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]) {
15640 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
15641 		tid_conf->amsdu =
15642 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]);
15643 	}
15644 
15645 	if (attrs[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE]) {
15646 		u32 idx = NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE, attr;
15647 
15648 		tid_conf->txrate_type = nla_get_u8(attrs[idx]);
15649 
15650 		if (tid_conf->txrate_type != NL80211_TX_RATE_AUTOMATIC) {
15651 			attr = NL80211_TID_CONFIG_ATTR_TX_RATE;
15652 			err = nl80211_parse_tx_bitrate_mask(info, attrs, attr,
15653 						    &tid_conf->txrate_mask, dev,
15654 						    true, link_id);
15655 			if (err)
15656 				return err;
15657 
15658 			tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE);
15659 		}
15660 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
15661 	}
15662 
15663 	if (peer)
15664 		mask = rdev->wiphy.tid_config_support.peer;
15665 	else
15666 		mask = rdev->wiphy.tid_config_support.vif;
15667 
15668 	if (tid_conf->mask & ~mask) {
15669 		NL_SET_ERR_MSG(extack, "unsupported TID configuration");
15670 		return -ENOTSUPP;
15671 	}
15672 
15673 	return 0;
15674 }
15675 
15676 static int nl80211_set_tid_config(struct sk_buff *skb,
15677 				  struct genl_info *info)
15678 {
15679 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15680 	struct nlattr *attrs[NL80211_TID_CONFIG_ATTR_MAX + 1];
15681 	unsigned int link_id = nl80211_link_id(info->attrs);
15682 	struct net_device *dev = info->user_ptr[1];
15683 	struct cfg80211_tid_config *tid_config;
15684 	struct nlattr *tid;
15685 	int conf_idx = 0, rem_conf;
15686 	int ret = -EINVAL;
15687 	u32 num_conf = 0;
15688 
15689 	if (!info->attrs[NL80211_ATTR_TID_CONFIG])
15690 		return -EINVAL;
15691 
15692 	if (!rdev->ops->set_tid_config)
15693 		return -EOPNOTSUPP;
15694 
15695 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
15696 			    rem_conf)
15697 		num_conf++;
15698 
15699 	tid_config = kzalloc(struct_size(tid_config, tid_conf, num_conf),
15700 			     GFP_KERNEL);
15701 	if (!tid_config)
15702 		return -ENOMEM;
15703 
15704 	tid_config->n_tid_conf = num_conf;
15705 
15706 	if (info->attrs[NL80211_ATTR_MAC])
15707 		tid_config->peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15708 
15709 	wdev_lock(dev->ieee80211_ptr);
15710 
15711 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
15712 			    rem_conf) {
15713 		ret = nla_parse_nested(attrs, NL80211_TID_CONFIG_ATTR_MAX,
15714 				       tid, NULL, NULL);
15715 
15716 		if (ret)
15717 			goto bad_tid_conf;
15718 
15719 		ret = parse_tid_conf(rdev, attrs, dev,
15720 				     &tid_config->tid_conf[conf_idx],
15721 				     info, tid_config->peer, link_id);
15722 		if (ret)
15723 			goto bad_tid_conf;
15724 
15725 		conf_idx++;
15726 	}
15727 
15728 	ret = rdev_set_tid_config(rdev, dev, tid_config);
15729 
15730 bad_tid_conf:
15731 	kfree(tid_config);
15732 	wdev_unlock(dev->ieee80211_ptr);
15733 	return ret;
15734 }
15735 
15736 static int nl80211_color_change(struct sk_buff *skb, struct genl_info *info)
15737 {
15738 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15739 	struct cfg80211_color_change_settings params = {};
15740 	struct net_device *dev = info->user_ptr[1];
15741 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15742 	struct nlattr **tb;
15743 	u16 offset;
15744 	int err;
15745 
15746 	if (!rdev->ops->color_change)
15747 		return -EOPNOTSUPP;
15748 
15749 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15750 				     NL80211_EXT_FEATURE_BSS_COLOR))
15751 		return -EOPNOTSUPP;
15752 
15753 	if (wdev->iftype != NL80211_IFTYPE_AP)
15754 		return -EOPNOTSUPP;
15755 
15756 	if (!info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT] ||
15757 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR] ||
15758 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS])
15759 		return -EINVAL;
15760 
15761 	params.count = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT]);
15762 	params.color = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR]);
15763 
15764 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_next);
15765 	if (err)
15766 		return err;
15767 
15768 	tb = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*tb), GFP_KERNEL);
15769 	if (!tb)
15770 		return -ENOMEM;
15771 
15772 	err = nla_parse_nested(tb, NL80211_ATTR_MAX,
15773 			       info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS],
15774 			       nl80211_policy, info->extack);
15775 	if (err)
15776 		goto out;
15777 
15778 	err = nl80211_parse_beacon(rdev, tb, &params.beacon_color_change);
15779 	if (err)
15780 		goto out;
15781 
15782 	if (!tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
15783 		err = -EINVAL;
15784 		goto out;
15785 	}
15786 
15787 	if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) != sizeof(u16)) {
15788 		err = -EINVAL;
15789 		goto out;
15790 	}
15791 
15792 	offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
15793 	if (offset >= params.beacon_color_change.tail_len) {
15794 		err = -EINVAL;
15795 		goto out;
15796 	}
15797 
15798 	if (params.beacon_color_change.tail[offset] != params.count) {
15799 		err = -EINVAL;
15800 		goto out;
15801 	}
15802 
15803 	params.counter_offset_beacon = offset;
15804 
15805 	if (tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
15806 		if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) !=
15807 		    sizeof(u16)) {
15808 			err = -EINVAL;
15809 			goto out;
15810 		}
15811 
15812 		offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
15813 		if (offset >= params.beacon_color_change.probe_resp_len) {
15814 			err = -EINVAL;
15815 			goto out;
15816 		}
15817 
15818 		if (params.beacon_color_change.probe_resp[offset] !=
15819 		    params.count) {
15820 			err = -EINVAL;
15821 			goto out;
15822 		}
15823 
15824 		params.counter_offset_presp = offset;
15825 	}
15826 
15827 	wdev_lock(wdev);
15828 	err = rdev_color_change(rdev, dev, &params);
15829 	wdev_unlock(wdev);
15830 
15831 out:
15832 	kfree(params.beacon_next.mbssid_ies);
15833 	kfree(params.beacon_color_change.mbssid_ies);
15834 	kfree(tb);
15835 	return err;
15836 }
15837 
15838 static int nl80211_set_fils_aad(struct sk_buff *skb,
15839 				struct genl_info *info)
15840 {
15841 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15842 	struct net_device *dev = info->user_ptr[1];
15843 	struct cfg80211_fils_aad fils_aad = {};
15844 	u8 *nonces;
15845 
15846 	if (!info->attrs[NL80211_ATTR_MAC] ||
15847 	    !info->attrs[NL80211_ATTR_FILS_KEK] ||
15848 	    !info->attrs[NL80211_ATTR_FILS_NONCES])
15849 		return -EINVAL;
15850 
15851 	fils_aad.macaddr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15852 	fils_aad.kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
15853 	fils_aad.kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
15854 	nonces = nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
15855 	fils_aad.snonce = nonces;
15856 	fils_aad.anonce = nonces + FILS_NONCE_LEN;
15857 
15858 	return rdev_set_fils_aad(rdev, dev, &fils_aad);
15859 }
15860 
15861 static int nl80211_add_link(struct sk_buff *skb, struct genl_info *info)
15862 {
15863 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15864 	unsigned int link_id = nl80211_link_id(info->attrs);
15865 	struct net_device *dev = info->user_ptr[1];
15866 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15867 	int ret;
15868 
15869 	if (!(wdev->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
15870 		return -EINVAL;
15871 
15872 	switch (wdev->iftype) {
15873 	case NL80211_IFTYPE_AP:
15874 		break;
15875 	default:
15876 		return -EINVAL;
15877 	}
15878 
15879 	if (!info->attrs[NL80211_ATTR_MAC] ||
15880 	    !is_valid_ether_addr(nla_data(info->attrs[NL80211_ATTR_MAC])))
15881 		return -EINVAL;
15882 
15883 	wdev_lock(wdev);
15884 	wdev->valid_links |= BIT(link_id);
15885 	ether_addr_copy(wdev->links[link_id].addr,
15886 			nla_data(info->attrs[NL80211_ATTR_MAC]));
15887 
15888 	ret = rdev_add_intf_link(rdev, wdev, link_id);
15889 	if (ret) {
15890 		wdev->valid_links &= ~BIT(link_id);
15891 		eth_zero_addr(wdev->links[link_id].addr);
15892 	}
15893 	wdev_unlock(wdev);
15894 
15895 	return ret;
15896 }
15897 
15898 static int nl80211_remove_link(struct sk_buff *skb, struct genl_info *info)
15899 {
15900 	unsigned int link_id = nl80211_link_id(info->attrs);
15901 	struct net_device *dev = info->user_ptr[1];
15902 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15903 
15904 	/* cannot remove if there's no link */
15905 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
15906 		return -EINVAL;
15907 
15908 	switch (wdev->iftype) {
15909 	case NL80211_IFTYPE_AP:
15910 		break;
15911 	default:
15912 		return -EINVAL;
15913 	}
15914 
15915 	wdev_lock(wdev);
15916 	cfg80211_remove_link(wdev, link_id);
15917 	wdev_unlock(wdev);
15918 
15919 	return 0;
15920 }
15921 
15922 static int
15923 nl80211_add_mod_link_station(struct sk_buff *skb, struct genl_info *info,
15924 			     bool add)
15925 {
15926 	struct link_station_parameters params = {};
15927 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15928 	struct net_device *dev = info->user_ptr[1];
15929 	int err;
15930 
15931 	if ((add && !rdev->ops->add_link_station) ||
15932 	    (!add && !rdev->ops->mod_link_station))
15933 		return -EOPNOTSUPP;
15934 
15935 	if (add && !info->attrs[NL80211_ATTR_MAC])
15936 		return -EINVAL;
15937 
15938 	if (!info->attrs[NL80211_ATTR_MLD_ADDR])
15939 		return -EINVAL;
15940 
15941 	if (add && !info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
15942 		return -EINVAL;
15943 
15944 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
15945 
15946 	if (info->attrs[NL80211_ATTR_MAC]) {
15947 		params.link_mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
15948 		if (!is_valid_ether_addr(params.link_mac))
15949 			return -EINVAL;
15950 	}
15951 
15952 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
15953 		return -EINVAL;
15954 
15955 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
15956 
15957 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
15958 		params.supported_rates =
15959 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
15960 		params.supported_rates_len =
15961 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
15962 	}
15963 
15964 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
15965 		params.ht_capa =
15966 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
15967 
15968 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
15969 		params.vht_capa =
15970 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
15971 
15972 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
15973 		params.he_capa =
15974 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
15975 		params.he_capa_len =
15976 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
15977 
15978 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
15979 			params.eht_capa =
15980 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
15981 			params.eht_capa_len =
15982 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
15983 
15984 			if (!ieee80211_eht_capa_size_ok((const u8 *)params.he_capa,
15985 							(const u8 *)params.eht_capa,
15986 							params.eht_capa_len,
15987 							false))
15988 				return -EINVAL;
15989 		}
15990 	}
15991 
15992 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
15993 		params.he_6ghz_capa =
15994 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
15995 
15996 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
15997 		params.opmode_notif_used = true;
15998 		params.opmode_notif =
15999 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
16000 	}
16001 
16002 	err = nl80211_parse_sta_txpower_setting(info, &params.txpwr,
16003 						&params.txpwr_set);
16004 	if (err)
16005 		return err;
16006 
16007 	wdev_lock(dev->ieee80211_ptr);
16008 	if (add)
16009 		err = rdev_add_link_station(rdev, dev, &params);
16010 	else
16011 		err = rdev_mod_link_station(rdev, dev, &params);
16012 	wdev_unlock(dev->ieee80211_ptr);
16013 
16014 	return err;
16015 }
16016 
16017 static int
16018 nl80211_add_link_station(struct sk_buff *skb, struct genl_info *info)
16019 {
16020 	return nl80211_add_mod_link_station(skb, info, true);
16021 }
16022 
16023 static int
16024 nl80211_modify_link_station(struct sk_buff *skb, struct genl_info *info)
16025 {
16026 	return nl80211_add_mod_link_station(skb, info, false);
16027 }
16028 
16029 static int
16030 nl80211_remove_link_station(struct sk_buff *skb, struct genl_info *info)
16031 {
16032 	struct link_station_del_parameters params = {};
16033 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16034 	struct net_device *dev = info->user_ptr[1];
16035 	int ret;
16036 
16037 	if (!rdev->ops->del_link_station)
16038 		return -EOPNOTSUPP;
16039 
16040 	if (!info->attrs[NL80211_ATTR_MLD_ADDR] ||
16041 	    !info->attrs[NL80211_ATTR_MLO_LINK_ID])
16042 		return -EINVAL;
16043 
16044 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
16045 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
16046 
16047 	wdev_lock(dev->ieee80211_ptr);
16048 	ret = rdev_del_link_station(rdev, dev, &params);
16049 	wdev_unlock(dev->ieee80211_ptr);
16050 
16051 	return ret;
16052 }
16053 
16054 #define NL80211_FLAG_NEED_WIPHY		0x01
16055 #define NL80211_FLAG_NEED_NETDEV	0x02
16056 #define NL80211_FLAG_NEED_RTNL		0x04
16057 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
16058 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
16059 					 NL80211_FLAG_CHECK_NETDEV_UP)
16060 #define NL80211_FLAG_NEED_WDEV		0x10
16061 /* If a netdev is associated, it must be UP, P2P must be started */
16062 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
16063 					 NL80211_FLAG_CHECK_NETDEV_UP)
16064 #define NL80211_FLAG_CLEAR_SKB		0x20
16065 #define NL80211_FLAG_NO_WIPHY_MTX	0x40
16066 #define NL80211_FLAG_MLO_VALID_LINK_ID	0x80
16067 #define NL80211_FLAG_MLO_UNSUPPORTED	0x100
16068 
16069 #define INTERNAL_FLAG_SELECTORS(__sel)			\
16070 	SELECTOR(__sel, NONE, 0) /* must be first */	\
16071 	SELECTOR(__sel, WIPHY,				\
16072 		 NL80211_FLAG_NEED_WIPHY)		\
16073 	SELECTOR(__sel, WDEV,				\
16074 		 NL80211_FLAG_NEED_WDEV)		\
16075 	SELECTOR(__sel, NETDEV,				\
16076 		 NL80211_FLAG_NEED_NETDEV)		\
16077 	SELECTOR(__sel, NETDEV_LINK,			\
16078 		 NL80211_FLAG_NEED_NETDEV |		\
16079 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16080 	SELECTOR(__sel, NETDEV_NO_MLO,			\
16081 		 NL80211_FLAG_NEED_NETDEV |		\
16082 		 NL80211_FLAG_MLO_UNSUPPORTED)	\
16083 	SELECTOR(__sel, WIPHY_RTNL,			\
16084 		 NL80211_FLAG_NEED_WIPHY |		\
16085 		 NL80211_FLAG_NEED_RTNL)		\
16086 	SELECTOR(__sel, WIPHY_RTNL_NOMTX,		\
16087 		 NL80211_FLAG_NEED_WIPHY |		\
16088 		 NL80211_FLAG_NEED_RTNL |		\
16089 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16090 	SELECTOR(__sel, WDEV_RTNL,			\
16091 		 NL80211_FLAG_NEED_WDEV |		\
16092 		 NL80211_FLAG_NEED_RTNL)		\
16093 	SELECTOR(__sel, NETDEV_RTNL,			\
16094 		 NL80211_FLAG_NEED_NETDEV |		\
16095 		 NL80211_FLAG_NEED_RTNL)		\
16096 	SELECTOR(__sel, NETDEV_UP,			\
16097 		 NL80211_FLAG_NEED_NETDEV_UP)		\
16098 	SELECTOR(__sel, NETDEV_UP_LINK,			\
16099 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16100 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16101 	SELECTOR(__sel, NETDEV_UP_NO_MLO,		\
16102 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16103 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16104 	SELECTOR(__sel, NETDEV_UP_NO_MLO_CLEAR,		\
16105 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16106 		 NL80211_FLAG_CLEAR_SKB |		\
16107 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16108 	SELECTOR(__sel, NETDEV_UP_NOTMX,		\
16109 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16110 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16111 	SELECTOR(__sel, NETDEV_UP_NOTMX_NOMLO,		\
16112 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16113 		 NL80211_FLAG_NO_WIPHY_MTX |		\
16114 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16115 	SELECTOR(__sel, NETDEV_UP_CLEAR,		\
16116 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16117 		 NL80211_FLAG_CLEAR_SKB)		\
16118 	SELECTOR(__sel, WDEV_UP,			\
16119 		 NL80211_FLAG_NEED_WDEV_UP)		\
16120 	SELECTOR(__sel, WDEV_UP_LINK,			\
16121 		 NL80211_FLAG_NEED_WDEV_UP |		\
16122 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16123 	SELECTOR(__sel, WDEV_UP_RTNL,			\
16124 		 NL80211_FLAG_NEED_WDEV_UP |		\
16125 		 NL80211_FLAG_NEED_RTNL)		\
16126 	SELECTOR(__sel, WIPHY_CLEAR,			\
16127 		 NL80211_FLAG_NEED_WIPHY |		\
16128 		 NL80211_FLAG_CLEAR_SKB)
16129 
16130 enum nl80211_internal_flags_selector {
16131 #define SELECTOR(_, name, value)	NL80211_IFL_SEL_##name,
16132 	INTERNAL_FLAG_SELECTORS(_)
16133 #undef SELECTOR
16134 };
16135 
16136 static u32 nl80211_internal_flags[] = {
16137 #define SELECTOR(_, name, value)	[NL80211_IFL_SEL_##name] = value,
16138 	INTERNAL_FLAG_SELECTORS(_)
16139 #undef SELECTOR
16140 };
16141 
16142 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
16143 			    struct genl_info *info)
16144 {
16145 	struct cfg80211_registered_device *rdev = NULL;
16146 	struct wireless_dev *wdev = NULL;
16147 	struct net_device *dev = NULL;
16148 	u32 internal_flags;
16149 	int err;
16150 
16151 	if (WARN_ON(ops->internal_flags >= ARRAY_SIZE(nl80211_internal_flags)))
16152 		return -EINVAL;
16153 
16154 	internal_flags = nl80211_internal_flags[ops->internal_flags];
16155 
16156 	rtnl_lock();
16157 	if (internal_flags & NL80211_FLAG_NEED_WIPHY) {
16158 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
16159 		if (IS_ERR(rdev)) {
16160 			err = PTR_ERR(rdev);
16161 			goto out_unlock;
16162 		}
16163 		info->user_ptr[0] = rdev;
16164 	} else if (internal_flags & NL80211_FLAG_NEED_NETDEV ||
16165 		   internal_flags & NL80211_FLAG_NEED_WDEV) {
16166 		wdev = __cfg80211_wdev_from_attrs(NULL, genl_info_net(info),
16167 						  info->attrs);
16168 		if (IS_ERR(wdev)) {
16169 			err = PTR_ERR(wdev);
16170 			goto out_unlock;
16171 		}
16172 
16173 		dev = wdev->netdev;
16174 		dev_hold(dev);
16175 		rdev = wiphy_to_rdev(wdev->wiphy);
16176 
16177 		if (internal_flags & NL80211_FLAG_NEED_NETDEV) {
16178 			if (!dev) {
16179 				err = -EINVAL;
16180 				goto out_unlock;
16181 			}
16182 
16183 			info->user_ptr[1] = dev;
16184 		} else {
16185 			info->user_ptr[1] = wdev;
16186 		}
16187 
16188 		if (internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
16189 		    !wdev_running(wdev)) {
16190 			err = -ENETDOWN;
16191 			goto out_unlock;
16192 		}
16193 
16194 		info->user_ptr[0] = rdev;
16195 	}
16196 
16197 	if (internal_flags & NL80211_FLAG_MLO_VALID_LINK_ID) {
16198 		struct nlattr *link_id = info->attrs[NL80211_ATTR_MLO_LINK_ID];
16199 
16200 		if (!wdev) {
16201 			err = -EINVAL;
16202 			goto out_unlock;
16203 		}
16204 
16205 		/* MLO -> require valid link ID */
16206 		if (wdev->valid_links &&
16207 		    (!link_id ||
16208 		     !(wdev->valid_links & BIT(nla_get_u8(link_id))))) {
16209 			err = -EINVAL;
16210 			goto out_unlock;
16211 		}
16212 
16213 		/* non-MLO -> no link ID attribute accepted */
16214 		if (!wdev->valid_links && link_id) {
16215 			err = -EINVAL;
16216 			goto out_unlock;
16217 		}
16218 	}
16219 
16220 	if (internal_flags & NL80211_FLAG_MLO_UNSUPPORTED) {
16221 		if (info->attrs[NL80211_ATTR_MLO_LINK_ID] ||
16222 		    (wdev && wdev->valid_links)) {
16223 			err = -EINVAL;
16224 			goto out_unlock;
16225 		}
16226 	}
16227 
16228 	if (rdev && !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16229 		wiphy_lock(&rdev->wiphy);
16230 		/* we keep the mutex locked until post_doit */
16231 		__release(&rdev->wiphy.mtx);
16232 	}
16233 	if (!(internal_flags & NL80211_FLAG_NEED_RTNL))
16234 		rtnl_unlock();
16235 
16236 	return 0;
16237 out_unlock:
16238 	rtnl_unlock();
16239 	dev_put(dev);
16240 	return err;
16241 }
16242 
16243 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
16244 			      struct genl_info *info)
16245 {
16246 	u32 internal_flags = nl80211_internal_flags[ops->internal_flags];
16247 
16248 	if (info->user_ptr[1]) {
16249 		if (internal_flags & NL80211_FLAG_NEED_WDEV) {
16250 			struct wireless_dev *wdev = info->user_ptr[1];
16251 
16252 			dev_put(wdev->netdev);
16253 		} else {
16254 			dev_put(info->user_ptr[1]);
16255 		}
16256 	}
16257 
16258 	if (info->user_ptr[0] &&
16259 	    !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16260 		struct cfg80211_registered_device *rdev = info->user_ptr[0];
16261 
16262 		/* we kept the mutex locked since pre_doit */
16263 		__acquire(&rdev->wiphy.mtx);
16264 		wiphy_unlock(&rdev->wiphy);
16265 	}
16266 
16267 	if (internal_flags & NL80211_FLAG_NEED_RTNL)
16268 		rtnl_unlock();
16269 
16270 	/* If needed, clear the netlink message payload from the SKB
16271 	 * as it might contain key data that shouldn't stick around on
16272 	 * the heap after the SKB is freed. The netlink message header
16273 	 * is still needed for further processing, so leave it intact.
16274 	 */
16275 	if (internal_flags & NL80211_FLAG_CLEAR_SKB) {
16276 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
16277 
16278 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
16279 	}
16280 }
16281 
16282 static int nl80211_set_sar_sub_specs(struct cfg80211_registered_device *rdev,
16283 				     struct cfg80211_sar_specs *sar_specs,
16284 				     struct nlattr *spec[], int index)
16285 {
16286 	u32 range_index, i;
16287 
16288 	if (!sar_specs || !spec)
16289 		return -EINVAL;
16290 
16291 	if (!spec[NL80211_SAR_ATTR_SPECS_POWER] ||
16292 	    !spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX])
16293 		return -EINVAL;
16294 
16295 	range_index = nla_get_u32(spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX]);
16296 
16297 	/* check if range_index exceeds num_freq_ranges */
16298 	if (range_index >= rdev->wiphy.sar_capa->num_freq_ranges)
16299 		return -EINVAL;
16300 
16301 	/* check if range_index duplicates */
16302 	for (i = 0; i < index; i++) {
16303 		if (sar_specs->sub_specs[i].freq_range_index == range_index)
16304 			return -EINVAL;
16305 	}
16306 
16307 	sar_specs->sub_specs[index].power =
16308 		nla_get_s32(spec[NL80211_SAR_ATTR_SPECS_POWER]);
16309 
16310 	sar_specs->sub_specs[index].freq_range_index = range_index;
16311 
16312 	return 0;
16313 }
16314 
16315 static int nl80211_set_sar_specs(struct sk_buff *skb, struct genl_info *info)
16316 {
16317 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16318 	struct nlattr *spec[NL80211_SAR_ATTR_SPECS_MAX + 1];
16319 	struct nlattr *tb[NL80211_SAR_ATTR_MAX + 1];
16320 	struct cfg80211_sar_specs *sar_spec;
16321 	enum nl80211_sar_type type;
16322 	struct nlattr *spec_list;
16323 	u32 specs;
16324 	int rem, err;
16325 
16326 	if (!rdev->wiphy.sar_capa || !rdev->ops->set_sar_specs)
16327 		return -EOPNOTSUPP;
16328 
16329 	if (!info->attrs[NL80211_ATTR_SAR_SPEC])
16330 		return -EINVAL;
16331 
16332 	nla_parse_nested(tb, NL80211_SAR_ATTR_MAX,
16333 			 info->attrs[NL80211_ATTR_SAR_SPEC],
16334 			 NULL, NULL);
16335 
16336 	if (!tb[NL80211_SAR_ATTR_TYPE] || !tb[NL80211_SAR_ATTR_SPECS])
16337 		return -EINVAL;
16338 
16339 	type = nla_get_u32(tb[NL80211_SAR_ATTR_TYPE]);
16340 	if (type != rdev->wiphy.sar_capa->type)
16341 		return -EINVAL;
16342 
16343 	specs = 0;
16344 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem)
16345 		specs++;
16346 
16347 	if (specs > rdev->wiphy.sar_capa->num_freq_ranges)
16348 		return -EINVAL;
16349 
16350 	sar_spec = kzalloc(struct_size(sar_spec, sub_specs, specs), GFP_KERNEL);
16351 	if (!sar_spec)
16352 		return -ENOMEM;
16353 
16354 	sar_spec->type = type;
16355 	specs = 0;
16356 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem) {
16357 		nla_parse_nested(spec, NL80211_SAR_ATTR_SPECS_MAX,
16358 				 spec_list, NULL, NULL);
16359 
16360 		switch (type) {
16361 		case NL80211_SAR_TYPE_POWER:
16362 			if (nl80211_set_sar_sub_specs(rdev, sar_spec,
16363 						      spec, specs)) {
16364 				err = -EINVAL;
16365 				goto error;
16366 			}
16367 			break;
16368 		default:
16369 			err = -EINVAL;
16370 			goto error;
16371 		}
16372 		specs++;
16373 	}
16374 
16375 	sar_spec->num_sub_specs = specs;
16376 
16377 	rdev->cur_cmd_info = info;
16378 	err = rdev_set_sar_specs(rdev, sar_spec);
16379 	rdev->cur_cmd_info = NULL;
16380 error:
16381 	kfree(sar_spec);
16382 	return err;
16383 }
16384 
16385 #define SELECTOR(__sel, name, value) \
16386 	((__sel) == (value)) ? NL80211_IFL_SEL_##name :
16387 int __missing_selector(void);
16388 #define IFLAGS(__val) INTERNAL_FLAG_SELECTORS(__val) __missing_selector()
16389 
16390 static const struct genl_ops nl80211_ops[] = {
16391 	{
16392 		.cmd = NL80211_CMD_GET_WIPHY,
16393 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16394 		.doit = nl80211_get_wiphy,
16395 		.dumpit = nl80211_dump_wiphy,
16396 		.done = nl80211_dump_wiphy_done,
16397 		/* can be retrieved by unprivileged users */
16398 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16399 	},
16400 };
16401 
16402 static const struct genl_small_ops nl80211_small_ops[] = {
16403 	{
16404 		.cmd = NL80211_CMD_SET_WIPHY,
16405 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16406 		.doit = nl80211_set_wiphy,
16407 		.flags = GENL_UNS_ADMIN_PERM,
16408 	},
16409 	{
16410 		.cmd = NL80211_CMD_GET_INTERFACE,
16411 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16412 		.doit = nl80211_get_interface,
16413 		.dumpit = nl80211_dump_interface,
16414 		/* can be retrieved by unprivileged users */
16415 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
16416 	},
16417 	{
16418 		.cmd = NL80211_CMD_SET_INTERFACE,
16419 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16420 		.doit = nl80211_set_interface,
16421 		.flags = GENL_UNS_ADMIN_PERM,
16422 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16423 					 NL80211_FLAG_NEED_RTNL),
16424 	},
16425 	{
16426 		.cmd = NL80211_CMD_NEW_INTERFACE,
16427 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16428 		.doit = nl80211_new_interface,
16429 		.flags = GENL_UNS_ADMIN_PERM,
16430 		.internal_flags =
16431 			IFLAGS(NL80211_FLAG_NEED_WIPHY |
16432 			       NL80211_FLAG_NEED_RTNL |
16433 			       /* we take the wiphy mutex later ourselves */
16434 			       NL80211_FLAG_NO_WIPHY_MTX),
16435 	},
16436 	{
16437 		.cmd = NL80211_CMD_DEL_INTERFACE,
16438 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16439 		.doit = nl80211_del_interface,
16440 		.flags = GENL_UNS_ADMIN_PERM,
16441 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
16442 					 NL80211_FLAG_NEED_RTNL),
16443 	},
16444 	{
16445 		.cmd = NL80211_CMD_GET_KEY,
16446 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16447 		.doit = nl80211_get_key,
16448 		.flags = GENL_UNS_ADMIN_PERM,
16449 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16450 	},
16451 	{
16452 		.cmd = NL80211_CMD_SET_KEY,
16453 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16454 		.doit = nl80211_set_key,
16455 		.flags = GENL_UNS_ADMIN_PERM,
16456 		/* cannot use NL80211_FLAG_MLO_VALID_LINK_ID, depends on key */
16457 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16458 					 NL80211_FLAG_CLEAR_SKB),
16459 	},
16460 	{
16461 		.cmd = NL80211_CMD_NEW_KEY,
16462 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16463 		.doit = nl80211_new_key,
16464 		.flags = GENL_UNS_ADMIN_PERM,
16465 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16466 					 NL80211_FLAG_CLEAR_SKB),
16467 	},
16468 	{
16469 		.cmd = NL80211_CMD_DEL_KEY,
16470 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16471 		.doit = nl80211_del_key,
16472 		.flags = GENL_UNS_ADMIN_PERM,
16473 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16474 	},
16475 	{
16476 		.cmd = NL80211_CMD_SET_BEACON,
16477 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16478 		.flags = GENL_UNS_ADMIN_PERM,
16479 		.doit = nl80211_set_beacon,
16480 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16481 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16482 	},
16483 	{
16484 		.cmd = NL80211_CMD_START_AP,
16485 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16486 		.flags = GENL_UNS_ADMIN_PERM,
16487 		.doit = nl80211_start_ap,
16488 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16489 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16490 	},
16491 	{
16492 		.cmd = NL80211_CMD_STOP_AP,
16493 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16494 		.flags = GENL_UNS_ADMIN_PERM,
16495 		.doit = nl80211_stop_ap,
16496 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16497 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16498 	},
16499 	{
16500 		.cmd = NL80211_CMD_GET_STATION,
16501 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16502 		.doit = nl80211_get_station,
16503 		.dumpit = nl80211_dump_station,
16504 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16505 	},
16506 	{
16507 		.cmd = NL80211_CMD_SET_STATION,
16508 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16509 		.doit = nl80211_set_station,
16510 		.flags = GENL_UNS_ADMIN_PERM,
16511 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16512 	},
16513 	{
16514 		.cmd = NL80211_CMD_NEW_STATION,
16515 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16516 		.doit = nl80211_new_station,
16517 		.flags = GENL_UNS_ADMIN_PERM,
16518 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16519 	},
16520 	{
16521 		.cmd = NL80211_CMD_DEL_STATION,
16522 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16523 		.doit = nl80211_del_station,
16524 		.flags = GENL_UNS_ADMIN_PERM,
16525 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16526 	},
16527 	{
16528 		.cmd = NL80211_CMD_GET_MPATH,
16529 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16530 		.doit = nl80211_get_mpath,
16531 		.dumpit = nl80211_dump_mpath,
16532 		.flags = GENL_UNS_ADMIN_PERM,
16533 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16534 	},
16535 	{
16536 		.cmd = NL80211_CMD_GET_MPP,
16537 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16538 		.doit = nl80211_get_mpp,
16539 		.dumpit = nl80211_dump_mpp,
16540 		.flags = GENL_UNS_ADMIN_PERM,
16541 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16542 	},
16543 	{
16544 		.cmd = NL80211_CMD_SET_MPATH,
16545 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16546 		.doit = nl80211_set_mpath,
16547 		.flags = GENL_UNS_ADMIN_PERM,
16548 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16549 	},
16550 	{
16551 		.cmd = NL80211_CMD_NEW_MPATH,
16552 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16553 		.doit = nl80211_new_mpath,
16554 		.flags = GENL_UNS_ADMIN_PERM,
16555 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16556 	},
16557 	{
16558 		.cmd = NL80211_CMD_DEL_MPATH,
16559 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16560 		.doit = nl80211_del_mpath,
16561 		.flags = GENL_UNS_ADMIN_PERM,
16562 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16563 	},
16564 	{
16565 		.cmd = NL80211_CMD_SET_BSS,
16566 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16567 		.doit = nl80211_set_bss,
16568 		.flags = GENL_UNS_ADMIN_PERM,
16569 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16570 	},
16571 	{
16572 		.cmd = NL80211_CMD_GET_REG,
16573 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16574 		.doit = nl80211_get_reg_do,
16575 		.dumpit = nl80211_get_reg_dump,
16576 		/* can be retrieved by unprivileged users */
16577 	},
16578 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
16579 	{
16580 		.cmd = NL80211_CMD_SET_REG,
16581 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16582 		.doit = nl80211_set_reg,
16583 		.flags = GENL_ADMIN_PERM,
16584 	},
16585 #endif
16586 	{
16587 		.cmd = NL80211_CMD_REQ_SET_REG,
16588 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16589 		.doit = nl80211_req_set_reg,
16590 		.flags = GENL_ADMIN_PERM,
16591 	},
16592 	{
16593 		.cmd = NL80211_CMD_RELOAD_REGDB,
16594 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16595 		.doit = nl80211_reload_regdb,
16596 		.flags = GENL_ADMIN_PERM,
16597 	},
16598 	{
16599 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
16600 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16601 		.doit = nl80211_get_mesh_config,
16602 		/* can be retrieved by unprivileged users */
16603 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16604 	},
16605 	{
16606 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
16607 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16608 		.doit = nl80211_update_mesh_config,
16609 		.flags = GENL_UNS_ADMIN_PERM,
16610 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16611 	},
16612 	{
16613 		.cmd = NL80211_CMD_TRIGGER_SCAN,
16614 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16615 		.doit = nl80211_trigger_scan,
16616 		.flags = GENL_UNS_ADMIN_PERM,
16617 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16618 	},
16619 	{
16620 		.cmd = NL80211_CMD_ABORT_SCAN,
16621 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16622 		.doit = nl80211_abort_scan,
16623 		.flags = GENL_UNS_ADMIN_PERM,
16624 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16625 	},
16626 	{
16627 		.cmd = NL80211_CMD_GET_SCAN,
16628 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16629 		.dumpit = nl80211_dump_scan,
16630 	},
16631 	{
16632 		.cmd = NL80211_CMD_START_SCHED_SCAN,
16633 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16634 		.doit = nl80211_start_sched_scan,
16635 		.flags = GENL_UNS_ADMIN_PERM,
16636 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16637 	},
16638 	{
16639 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
16640 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16641 		.doit = nl80211_stop_sched_scan,
16642 		.flags = GENL_UNS_ADMIN_PERM,
16643 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16644 	},
16645 	{
16646 		.cmd = NL80211_CMD_AUTHENTICATE,
16647 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16648 		.doit = nl80211_authenticate,
16649 		.flags = GENL_UNS_ADMIN_PERM,
16650 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16651 					 NL80211_FLAG_CLEAR_SKB),
16652 	},
16653 	{
16654 		.cmd = NL80211_CMD_ASSOCIATE,
16655 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16656 		.doit = nl80211_associate,
16657 		.flags = GENL_UNS_ADMIN_PERM,
16658 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16659 					 NL80211_FLAG_CLEAR_SKB),
16660 	},
16661 	{
16662 		.cmd = NL80211_CMD_DEAUTHENTICATE,
16663 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16664 		.doit = nl80211_deauthenticate,
16665 		.flags = GENL_UNS_ADMIN_PERM,
16666 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16667 	},
16668 	{
16669 		.cmd = NL80211_CMD_DISASSOCIATE,
16670 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16671 		.doit = nl80211_disassociate,
16672 		.flags = GENL_UNS_ADMIN_PERM,
16673 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16674 	},
16675 	{
16676 		.cmd = NL80211_CMD_JOIN_IBSS,
16677 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16678 		.doit = nl80211_join_ibss,
16679 		.flags = GENL_UNS_ADMIN_PERM,
16680 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16681 	},
16682 	{
16683 		.cmd = NL80211_CMD_LEAVE_IBSS,
16684 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16685 		.doit = nl80211_leave_ibss,
16686 		.flags = GENL_UNS_ADMIN_PERM,
16687 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16688 	},
16689 #ifdef CONFIG_NL80211_TESTMODE
16690 	{
16691 		.cmd = NL80211_CMD_TESTMODE,
16692 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16693 		.doit = nl80211_testmode_do,
16694 		.dumpit = nl80211_testmode_dump,
16695 		.flags = GENL_UNS_ADMIN_PERM,
16696 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16697 	},
16698 #endif
16699 	{
16700 		.cmd = NL80211_CMD_CONNECT,
16701 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16702 		.doit = nl80211_connect,
16703 		.flags = GENL_UNS_ADMIN_PERM,
16704 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16705 					 NL80211_FLAG_CLEAR_SKB),
16706 	},
16707 	{
16708 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
16709 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16710 		.doit = nl80211_update_connect_params,
16711 		.flags = GENL_ADMIN_PERM,
16712 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16713 					 NL80211_FLAG_CLEAR_SKB),
16714 	},
16715 	{
16716 		.cmd = NL80211_CMD_DISCONNECT,
16717 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16718 		.doit = nl80211_disconnect,
16719 		.flags = GENL_UNS_ADMIN_PERM,
16720 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16721 	},
16722 	{
16723 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
16724 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16725 		.doit = nl80211_wiphy_netns,
16726 		.flags = GENL_UNS_ADMIN_PERM,
16727 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
16728 					 NL80211_FLAG_NEED_RTNL |
16729 					 NL80211_FLAG_NO_WIPHY_MTX),
16730 	},
16731 	{
16732 		.cmd = NL80211_CMD_GET_SURVEY,
16733 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16734 		.dumpit = nl80211_dump_survey,
16735 	},
16736 	{
16737 		.cmd = NL80211_CMD_SET_PMKSA,
16738 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16739 		.doit = nl80211_setdel_pmksa,
16740 		.flags = GENL_UNS_ADMIN_PERM,
16741 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16742 					 NL80211_FLAG_CLEAR_SKB),
16743 	},
16744 	{
16745 		.cmd = NL80211_CMD_DEL_PMKSA,
16746 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16747 		.doit = nl80211_setdel_pmksa,
16748 		.flags = GENL_UNS_ADMIN_PERM,
16749 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16750 	},
16751 	{
16752 		.cmd = NL80211_CMD_FLUSH_PMKSA,
16753 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16754 		.doit = nl80211_flush_pmksa,
16755 		.flags = GENL_UNS_ADMIN_PERM,
16756 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16757 	},
16758 	{
16759 		.cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
16760 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16761 		.doit = nl80211_remain_on_channel,
16762 		.flags = GENL_UNS_ADMIN_PERM,
16763 		/* FIXME: requiring a link ID here is probably not good */
16764 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
16765 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16766 	},
16767 	{
16768 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
16769 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16770 		.doit = nl80211_cancel_remain_on_channel,
16771 		.flags = GENL_UNS_ADMIN_PERM,
16772 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16773 	},
16774 	{
16775 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
16776 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16777 		.doit = nl80211_set_tx_bitrate_mask,
16778 		.flags = GENL_UNS_ADMIN_PERM,
16779 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16780 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16781 	},
16782 	{
16783 		.cmd = NL80211_CMD_REGISTER_FRAME,
16784 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16785 		.doit = nl80211_register_mgmt,
16786 		.flags = GENL_UNS_ADMIN_PERM,
16787 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
16788 	},
16789 	{
16790 		.cmd = NL80211_CMD_FRAME,
16791 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16792 		.doit = nl80211_tx_mgmt,
16793 		.flags = GENL_UNS_ADMIN_PERM,
16794 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16795 	},
16796 	{
16797 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
16798 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16799 		.doit = nl80211_tx_mgmt_cancel_wait,
16800 		.flags = GENL_UNS_ADMIN_PERM,
16801 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16802 	},
16803 	{
16804 		.cmd = NL80211_CMD_SET_POWER_SAVE,
16805 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16806 		.doit = nl80211_set_power_save,
16807 		.flags = GENL_UNS_ADMIN_PERM,
16808 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16809 	},
16810 	{
16811 		.cmd = NL80211_CMD_GET_POWER_SAVE,
16812 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16813 		.doit = nl80211_get_power_save,
16814 		/* can be retrieved by unprivileged users */
16815 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16816 	},
16817 	{
16818 		.cmd = NL80211_CMD_SET_CQM,
16819 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16820 		.doit = nl80211_set_cqm,
16821 		.flags = GENL_UNS_ADMIN_PERM,
16822 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16823 	},
16824 	{
16825 		.cmd = NL80211_CMD_SET_CHANNEL,
16826 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16827 		.doit = nl80211_set_channel,
16828 		.flags = GENL_UNS_ADMIN_PERM,
16829 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16830 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16831 	},
16832 	{
16833 		.cmd = NL80211_CMD_JOIN_MESH,
16834 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16835 		.doit = nl80211_join_mesh,
16836 		.flags = GENL_UNS_ADMIN_PERM,
16837 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16838 	},
16839 	{
16840 		.cmd = NL80211_CMD_LEAVE_MESH,
16841 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16842 		.doit = nl80211_leave_mesh,
16843 		.flags = GENL_UNS_ADMIN_PERM,
16844 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16845 	},
16846 	{
16847 		.cmd = NL80211_CMD_JOIN_OCB,
16848 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16849 		.doit = nl80211_join_ocb,
16850 		.flags = GENL_UNS_ADMIN_PERM,
16851 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16852 	},
16853 	{
16854 		.cmd = NL80211_CMD_LEAVE_OCB,
16855 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16856 		.doit = nl80211_leave_ocb,
16857 		.flags = GENL_UNS_ADMIN_PERM,
16858 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16859 	},
16860 #ifdef CONFIG_PM
16861 	{
16862 		.cmd = NL80211_CMD_GET_WOWLAN,
16863 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16864 		.doit = nl80211_get_wowlan,
16865 		/* can be retrieved by unprivileged users */
16866 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16867 	},
16868 	{
16869 		.cmd = NL80211_CMD_SET_WOWLAN,
16870 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16871 		.doit = nl80211_set_wowlan,
16872 		.flags = GENL_UNS_ADMIN_PERM,
16873 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16874 	},
16875 #endif
16876 	{
16877 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
16878 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16879 		.doit = nl80211_set_rekey_data,
16880 		.flags = GENL_UNS_ADMIN_PERM,
16881 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16882 					 NL80211_FLAG_CLEAR_SKB),
16883 	},
16884 	{
16885 		.cmd = NL80211_CMD_TDLS_MGMT,
16886 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16887 		.doit = nl80211_tdls_mgmt,
16888 		.flags = GENL_UNS_ADMIN_PERM,
16889 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16890 	},
16891 	{
16892 		.cmd = NL80211_CMD_TDLS_OPER,
16893 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16894 		.doit = nl80211_tdls_oper,
16895 		.flags = GENL_UNS_ADMIN_PERM,
16896 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16897 	},
16898 	{
16899 		.cmd = NL80211_CMD_UNEXPECTED_FRAME,
16900 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16901 		.doit = nl80211_register_unexpected_frame,
16902 		.flags = GENL_UNS_ADMIN_PERM,
16903 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16904 	},
16905 	{
16906 		.cmd = NL80211_CMD_PROBE_CLIENT,
16907 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16908 		.doit = nl80211_probe_client,
16909 		.flags = GENL_UNS_ADMIN_PERM,
16910 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16911 	},
16912 	{
16913 		.cmd = NL80211_CMD_REGISTER_BEACONS,
16914 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16915 		.doit = nl80211_register_beacons,
16916 		.flags = GENL_UNS_ADMIN_PERM,
16917 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16918 	},
16919 	{
16920 		.cmd = NL80211_CMD_SET_NOACK_MAP,
16921 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16922 		.doit = nl80211_set_noack_map,
16923 		.flags = GENL_UNS_ADMIN_PERM,
16924 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16925 	},
16926 	{
16927 		.cmd = NL80211_CMD_START_P2P_DEVICE,
16928 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16929 		.doit = nl80211_start_p2p_device,
16930 		.flags = GENL_UNS_ADMIN_PERM,
16931 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
16932 					 NL80211_FLAG_NEED_RTNL),
16933 	},
16934 	{
16935 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
16936 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16937 		.doit = nl80211_stop_p2p_device,
16938 		.flags = GENL_UNS_ADMIN_PERM,
16939 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
16940 					 NL80211_FLAG_NEED_RTNL),
16941 	},
16942 	{
16943 		.cmd = NL80211_CMD_START_NAN,
16944 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16945 		.doit = nl80211_start_nan,
16946 		.flags = GENL_ADMIN_PERM,
16947 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
16948 					 NL80211_FLAG_NEED_RTNL),
16949 	},
16950 	{
16951 		.cmd = NL80211_CMD_STOP_NAN,
16952 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16953 		.doit = nl80211_stop_nan,
16954 		.flags = GENL_ADMIN_PERM,
16955 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
16956 					 NL80211_FLAG_NEED_RTNL),
16957 	},
16958 	{
16959 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
16960 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16961 		.doit = nl80211_nan_add_func,
16962 		.flags = GENL_ADMIN_PERM,
16963 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16964 	},
16965 	{
16966 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
16967 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16968 		.doit = nl80211_nan_del_func,
16969 		.flags = GENL_ADMIN_PERM,
16970 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16971 	},
16972 	{
16973 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
16974 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16975 		.doit = nl80211_nan_change_config,
16976 		.flags = GENL_ADMIN_PERM,
16977 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16978 	},
16979 	{
16980 		.cmd = NL80211_CMD_SET_MCAST_RATE,
16981 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16982 		.doit = nl80211_set_mcast_rate,
16983 		.flags = GENL_UNS_ADMIN_PERM,
16984 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16985 	},
16986 	{
16987 		.cmd = NL80211_CMD_SET_MAC_ACL,
16988 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16989 		.doit = nl80211_set_mac_acl,
16990 		.flags = GENL_UNS_ADMIN_PERM,
16991 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16992 					 NL80211_FLAG_MLO_UNSUPPORTED),
16993 	},
16994 	{
16995 		.cmd = NL80211_CMD_RADAR_DETECT,
16996 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16997 		.doit = nl80211_start_radar_detection,
16998 		.flags = GENL_UNS_ADMIN_PERM,
16999 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17000 					 NL80211_FLAG_NO_WIPHY_MTX |
17001 					 NL80211_FLAG_MLO_UNSUPPORTED),
17002 	},
17003 	{
17004 		.cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
17005 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17006 		.doit = nl80211_get_protocol_features,
17007 	},
17008 	{
17009 		.cmd = NL80211_CMD_UPDATE_FT_IES,
17010 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17011 		.doit = nl80211_update_ft_ies,
17012 		.flags = GENL_UNS_ADMIN_PERM,
17013 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17014 	},
17015 	{
17016 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
17017 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17018 		.doit = nl80211_crit_protocol_start,
17019 		.flags = GENL_UNS_ADMIN_PERM,
17020 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17021 	},
17022 	{
17023 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
17024 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17025 		.doit = nl80211_crit_protocol_stop,
17026 		.flags = GENL_UNS_ADMIN_PERM,
17027 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17028 	},
17029 	{
17030 		.cmd = NL80211_CMD_GET_COALESCE,
17031 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17032 		.doit = nl80211_get_coalesce,
17033 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17034 	},
17035 	{
17036 		.cmd = NL80211_CMD_SET_COALESCE,
17037 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17038 		.doit = nl80211_set_coalesce,
17039 		.flags = GENL_UNS_ADMIN_PERM,
17040 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17041 	},
17042 	{
17043 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
17044 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17045 		.doit = nl80211_channel_switch,
17046 		.flags = GENL_UNS_ADMIN_PERM,
17047 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17048 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17049 	},
17050 	{
17051 		.cmd = NL80211_CMD_VENDOR,
17052 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17053 		.doit = nl80211_vendor_cmd,
17054 		.dumpit = nl80211_vendor_cmd_dump,
17055 		.flags = GENL_UNS_ADMIN_PERM,
17056 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17057 					 NL80211_FLAG_CLEAR_SKB),
17058 	},
17059 	{
17060 		.cmd = NL80211_CMD_SET_QOS_MAP,
17061 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17062 		.doit = nl80211_set_qos_map,
17063 		.flags = GENL_UNS_ADMIN_PERM,
17064 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17065 	},
17066 	{
17067 		.cmd = NL80211_CMD_ADD_TX_TS,
17068 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17069 		.doit = nl80211_add_tx_ts,
17070 		.flags = GENL_UNS_ADMIN_PERM,
17071 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17072 					 NL80211_FLAG_MLO_UNSUPPORTED),
17073 	},
17074 	{
17075 		.cmd = NL80211_CMD_DEL_TX_TS,
17076 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17077 		.doit = nl80211_del_tx_ts,
17078 		.flags = GENL_UNS_ADMIN_PERM,
17079 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17080 	},
17081 	{
17082 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
17083 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17084 		.doit = nl80211_tdls_channel_switch,
17085 		.flags = GENL_UNS_ADMIN_PERM,
17086 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17087 	},
17088 	{
17089 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
17090 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17091 		.doit = nl80211_tdls_cancel_channel_switch,
17092 		.flags = GENL_UNS_ADMIN_PERM,
17093 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17094 	},
17095 	{
17096 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
17097 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17098 		.doit = nl80211_set_multicast_to_unicast,
17099 		.flags = GENL_UNS_ADMIN_PERM,
17100 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17101 	},
17102 	{
17103 		.cmd = NL80211_CMD_SET_PMK,
17104 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17105 		.doit = nl80211_set_pmk,
17106 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17107 					 NL80211_FLAG_CLEAR_SKB),
17108 	},
17109 	{
17110 		.cmd = NL80211_CMD_DEL_PMK,
17111 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17112 		.doit = nl80211_del_pmk,
17113 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17114 	},
17115 	{
17116 		.cmd = NL80211_CMD_EXTERNAL_AUTH,
17117 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17118 		.doit = nl80211_external_auth,
17119 		.flags = GENL_ADMIN_PERM,
17120 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17121 	},
17122 	{
17123 		.cmd = NL80211_CMD_CONTROL_PORT_FRAME,
17124 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17125 		.doit = nl80211_tx_control_port,
17126 		.flags = GENL_UNS_ADMIN_PERM,
17127 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17128 	},
17129 	{
17130 		.cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
17131 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17132 		.doit = nl80211_get_ftm_responder_stats,
17133 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17134 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17135 	},
17136 	{
17137 		.cmd = NL80211_CMD_PEER_MEASUREMENT_START,
17138 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17139 		.doit = nl80211_pmsr_start,
17140 		.flags = GENL_UNS_ADMIN_PERM,
17141 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17142 	},
17143 	{
17144 		.cmd = NL80211_CMD_NOTIFY_RADAR,
17145 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17146 		.doit = nl80211_notify_radar_detection,
17147 		.flags = GENL_UNS_ADMIN_PERM,
17148 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17149 	},
17150 	{
17151 		.cmd = NL80211_CMD_UPDATE_OWE_INFO,
17152 		.doit = nl80211_update_owe_info,
17153 		.flags = GENL_ADMIN_PERM,
17154 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17155 	},
17156 	{
17157 		.cmd = NL80211_CMD_PROBE_MESH_LINK,
17158 		.doit = nl80211_probe_mesh_link,
17159 		.flags = GENL_UNS_ADMIN_PERM,
17160 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17161 	},
17162 	{
17163 		.cmd = NL80211_CMD_SET_TID_CONFIG,
17164 		.doit = nl80211_set_tid_config,
17165 		.flags = GENL_UNS_ADMIN_PERM,
17166 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17167 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17168 	},
17169 	{
17170 		.cmd = NL80211_CMD_SET_SAR_SPECS,
17171 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17172 		.doit = nl80211_set_sar_specs,
17173 		.flags = GENL_UNS_ADMIN_PERM,
17174 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17175 					 NL80211_FLAG_NEED_RTNL),
17176 	},
17177 	{
17178 		.cmd = NL80211_CMD_COLOR_CHANGE_REQUEST,
17179 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17180 		.doit = nl80211_color_change,
17181 		.flags = GENL_UNS_ADMIN_PERM,
17182 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17183 	},
17184 	{
17185 		.cmd = NL80211_CMD_SET_FILS_AAD,
17186 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17187 		.doit = nl80211_set_fils_aad,
17188 		.flags = GENL_UNS_ADMIN_PERM,
17189 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17190 	},
17191 	{
17192 		.cmd = NL80211_CMD_ADD_LINK,
17193 		.doit = nl80211_add_link,
17194 		.flags = GENL_UNS_ADMIN_PERM,
17195 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17196 	},
17197 	{
17198 		.cmd = NL80211_CMD_REMOVE_LINK,
17199 		.doit = nl80211_remove_link,
17200 		.flags = GENL_UNS_ADMIN_PERM,
17201 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17202 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17203 	},
17204 	{
17205 		.cmd = NL80211_CMD_ADD_LINK_STA,
17206 		.doit = nl80211_add_link_station,
17207 		.flags = GENL_UNS_ADMIN_PERM,
17208 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17209 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17210 	},
17211 	{
17212 		.cmd = NL80211_CMD_MODIFY_LINK_STA,
17213 		.doit = nl80211_modify_link_station,
17214 		.flags = GENL_UNS_ADMIN_PERM,
17215 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17216 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17217 	},
17218 	{
17219 		.cmd = NL80211_CMD_REMOVE_LINK_STA,
17220 		.doit = nl80211_remove_link_station,
17221 		.flags = GENL_UNS_ADMIN_PERM,
17222 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17223 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17224 	},
17225 };
17226 
17227 static struct genl_family nl80211_fam __ro_after_init = {
17228 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
17229 	.hdrsize = 0,			/* no private header */
17230 	.version = 1,			/* no particular meaning now */
17231 	.maxattr = NL80211_ATTR_MAX,
17232 	.policy = nl80211_policy,
17233 	.netnsok = true,
17234 	.pre_doit = nl80211_pre_doit,
17235 	.post_doit = nl80211_post_doit,
17236 	.module = THIS_MODULE,
17237 	.ops = nl80211_ops,
17238 	.n_ops = ARRAY_SIZE(nl80211_ops),
17239 	.small_ops = nl80211_small_ops,
17240 	.n_small_ops = ARRAY_SIZE(nl80211_small_ops),
17241 	.resv_start_op = NL80211_CMD_REMOVE_LINK_STA + 1,
17242 	.mcgrps = nl80211_mcgrps,
17243 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
17244 	.parallel_ops = true,
17245 };
17246 
17247 /* notification functions */
17248 
17249 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
17250 			  enum nl80211_commands cmd)
17251 {
17252 	struct sk_buff *msg;
17253 	struct nl80211_dump_wiphy_state state = {};
17254 
17255 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
17256 		cmd != NL80211_CMD_DEL_WIPHY);
17257 
17258 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17259 	if (!msg)
17260 		return;
17261 
17262 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
17263 		nlmsg_free(msg);
17264 		return;
17265 	}
17266 
17267 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17268 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17269 }
17270 
17271 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
17272 				struct wireless_dev *wdev,
17273 				enum nl80211_commands cmd)
17274 {
17275 	struct sk_buff *msg;
17276 
17277 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17278 	if (!msg)
17279 		return;
17280 
17281 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
17282 		nlmsg_free(msg);
17283 		return;
17284 	}
17285 
17286 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17287 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17288 }
17289 
17290 static int nl80211_add_scan_req(struct sk_buff *msg,
17291 				struct cfg80211_registered_device *rdev)
17292 {
17293 	struct cfg80211_scan_request *req = rdev->scan_req;
17294 	struct nlattr *nest;
17295 	int i;
17296 	struct cfg80211_scan_info *info;
17297 
17298 	if (WARN_ON(!req))
17299 		return 0;
17300 
17301 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
17302 	if (!nest)
17303 		goto nla_put_failure;
17304 	for (i = 0; i < req->n_ssids; i++) {
17305 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
17306 			goto nla_put_failure;
17307 	}
17308 	nla_nest_end(msg, nest);
17309 
17310 	if (req->flags & NL80211_SCAN_FLAG_FREQ_KHZ) {
17311 		nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQ_KHZ);
17312 		if (!nest)
17313 			goto nla_put_failure;
17314 		for (i = 0; i < req->n_channels; i++) {
17315 			if (nla_put_u32(msg, i,
17316 				   ieee80211_channel_to_khz(req->channels[i])))
17317 				goto nla_put_failure;
17318 		}
17319 		nla_nest_end(msg, nest);
17320 	} else {
17321 		nest = nla_nest_start_noflag(msg,
17322 					     NL80211_ATTR_SCAN_FREQUENCIES);
17323 		if (!nest)
17324 			goto nla_put_failure;
17325 		for (i = 0; i < req->n_channels; i++) {
17326 			if (nla_put_u32(msg, i, req->channels[i]->center_freq))
17327 				goto nla_put_failure;
17328 		}
17329 		nla_nest_end(msg, nest);
17330 	}
17331 
17332 	if (req->ie &&
17333 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
17334 		goto nla_put_failure;
17335 
17336 	if (req->flags &&
17337 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
17338 		goto nla_put_failure;
17339 
17340 	info = rdev->int_scan_req ? &rdev->int_scan_req->info :
17341 		&rdev->scan_req->info;
17342 	if (info->scan_start_tsf &&
17343 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
17344 			       info->scan_start_tsf, NL80211_BSS_PAD) ||
17345 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
17346 		     info->tsf_bssid)))
17347 		goto nla_put_failure;
17348 
17349 	return 0;
17350  nla_put_failure:
17351 	return -ENOBUFS;
17352 }
17353 
17354 static int nl80211_prep_scan_msg(struct sk_buff *msg,
17355 				 struct cfg80211_registered_device *rdev,
17356 				 struct wireless_dev *wdev,
17357 				 u32 portid, u32 seq, int flags,
17358 				 u32 cmd)
17359 {
17360 	void *hdr;
17361 
17362 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
17363 	if (!hdr)
17364 		return -1;
17365 
17366 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17367 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
17368 					 wdev->netdev->ifindex)) ||
17369 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17370 			      NL80211_ATTR_PAD))
17371 		goto nla_put_failure;
17372 
17373 	/* ignore errors and send incomplete event anyway */
17374 	nl80211_add_scan_req(msg, rdev);
17375 
17376 	genlmsg_end(msg, hdr);
17377 	return 0;
17378 
17379  nla_put_failure:
17380 	genlmsg_cancel(msg, hdr);
17381 	return -EMSGSIZE;
17382 }
17383 
17384 static int
17385 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
17386 			    struct cfg80211_sched_scan_request *req, u32 cmd)
17387 {
17388 	void *hdr;
17389 
17390 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17391 	if (!hdr)
17392 		return -1;
17393 
17394 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
17395 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
17396 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
17397 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
17398 			      NL80211_ATTR_PAD))
17399 		goto nla_put_failure;
17400 
17401 	genlmsg_end(msg, hdr);
17402 	return 0;
17403 
17404  nla_put_failure:
17405 	genlmsg_cancel(msg, hdr);
17406 	return -EMSGSIZE;
17407 }
17408 
17409 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
17410 			     struct wireless_dev *wdev)
17411 {
17412 	struct sk_buff *msg;
17413 
17414 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17415 	if (!msg)
17416 		return;
17417 
17418 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17419 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
17420 		nlmsg_free(msg);
17421 		return;
17422 	}
17423 
17424 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17425 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17426 }
17427 
17428 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
17429 				       struct wireless_dev *wdev, bool aborted)
17430 {
17431 	struct sk_buff *msg;
17432 
17433 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17434 	if (!msg)
17435 		return NULL;
17436 
17437 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17438 				  aborted ? NL80211_CMD_SCAN_ABORTED :
17439 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
17440 		nlmsg_free(msg);
17441 		return NULL;
17442 	}
17443 
17444 	return msg;
17445 }
17446 
17447 /* send message created by nl80211_build_scan_msg() */
17448 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
17449 			   struct sk_buff *msg)
17450 {
17451 	if (!msg)
17452 		return;
17453 
17454 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17455 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17456 }
17457 
17458 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
17459 {
17460 	struct sk_buff *msg;
17461 
17462 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17463 	if (!msg)
17464 		return;
17465 
17466 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
17467 		nlmsg_free(msg);
17468 		return;
17469 	}
17470 
17471 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
17472 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17473 }
17474 
17475 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
17476 					  struct regulatory_request *request)
17477 {
17478 	/* Userspace can always count this one always being set */
17479 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
17480 		goto nla_put_failure;
17481 
17482 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
17483 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17484 			       NL80211_REGDOM_TYPE_WORLD))
17485 			goto nla_put_failure;
17486 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
17487 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17488 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
17489 			goto nla_put_failure;
17490 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
17491 		   request->intersect) {
17492 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17493 			       NL80211_REGDOM_TYPE_INTERSECTION))
17494 			goto nla_put_failure;
17495 	} else {
17496 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17497 			       NL80211_REGDOM_TYPE_COUNTRY) ||
17498 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
17499 				   request->alpha2))
17500 			goto nla_put_failure;
17501 	}
17502 
17503 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
17504 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
17505 
17506 		if (wiphy &&
17507 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
17508 			goto nla_put_failure;
17509 
17510 		if (wiphy &&
17511 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
17512 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
17513 			goto nla_put_failure;
17514 	}
17515 
17516 	return true;
17517 
17518 nla_put_failure:
17519 	return false;
17520 }
17521 
17522 /*
17523  * This can happen on global regulatory changes or device specific settings
17524  * based on custom regulatory domains.
17525  */
17526 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
17527 				     struct regulatory_request *request)
17528 {
17529 	struct sk_buff *msg;
17530 	void *hdr;
17531 
17532 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17533 	if (!msg)
17534 		return;
17535 
17536 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
17537 	if (!hdr)
17538 		goto nla_put_failure;
17539 
17540 	if (!nl80211_reg_change_event_fill(msg, request))
17541 		goto nla_put_failure;
17542 
17543 	genlmsg_end(msg, hdr);
17544 
17545 	rcu_read_lock();
17546 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
17547 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
17548 	rcu_read_unlock();
17549 
17550 	return;
17551 
17552 nla_put_failure:
17553 	nlmsg_free(msg);
17554 }
17555 
17556 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
17557 				    struct net_device *netdev,
17558 				    const u8 *buf, size_t len,
17559 				    enum nl80211_commands cmd, gfp_t gfp,
17560 				    int uapsd_queues, const u8 *req_ies,
17561 				    size_t req_ies_len, bool reconnect)
17562 {
17563 	struct sk_buff *msg;
17564 	void *hdr;
17565 
17566 	msg = nlmsg_new(100 + len + req_ies_len, gfp);
17567 	if (!msg)
17568 		return;
17569 
17570 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17571 	if (!hdr) {
17572 		nlmsg_free(msg);
17573 		return;
17574 	}
17575 
17576 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17577 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17578 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
17579 	    (req_ies &&
17580 	     nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
17581 		goto nla_put_failure;
17582 
17583 	if (reconnect && nla_put_flag(msg, NL80211_ATTR_RECONNECT_REQUESTED))
17584 		goto nla_put_failure;
17585 
17586 	if (uapsd_queues >= 0) {
17587 		struct nlattr *nla_wmm =
17588 			nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
17589 		if (!nla_wmm)
17590 			goto nla_put_failure;
17591 
17592 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
17593 			       uapsd_queues))
17594 			goto nla_put_failure;
17595 
17596 		nla_nest_end(msg, nla_wmm);
17597 	}
17598 
17599 	genlmsg_end(msg, hdr);
17600 
17601 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17602 				NL80211_MCGRP_MLME, gfp);
17603 	return;
17604 
17605  nla_put_failure:
17606 	nlmsg_free(msg);
17607 }
17608 
17609 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
17610 			  struct net_device *netdev, const u8 *buf,
17611 			  size_t len, gfp_t gfp)
17612 {
17613 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17614 				NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0,
17615 				false);
17616 }
17617 
17618 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
17619 			   struct net_device *netdev,
17620 			   struct cfg80211_rx_assoc_resp *data)
17621 {
17622 	nl80211_send_mlme_event(rdev, netdev, data->buf, data->len,
17623 				NL80211_CMD_ASSOCIATE, GFP_KERNEL,
17624 				data->uapsd_queues,
17625 				data->req_ies, data->req_ies_len, false);
17626 }
17627 
17628 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
17629 			 struct net_device *netdev, const u8 *buf,
17630 			 size_t len, bool reconnect, gfp_t gfp)
17631 {
17632 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17633 				NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0,
17634 				reconnect);
17635 }
17636 
17637 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
17638 			   struct net_device *netdev, const u8 *buf,
17639 			   size_t len, bool reconnect, gfp_t gfp)
17640 {
17641 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17642 				NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0,
17643 				reconnect);
17644 }
17645 
17646 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
17647 				  size_t len)
17648 {
17649 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17650 	struct wiphy *wiphy = wdev->wiphy;
17651 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17652 	const struct ieee80211_mgmt *mgmt = (void *)buf;
17653 	u32 cmd;
17654 
17655 	if (WARN_ON(len < 2))
17656 		return;
17657 
17658 	if (ieee80211_is_deauth(mgmt->frame_control)) {
17659 		cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
17660 	} else if (ieee80211_is_disassoc(mgmt->frame_control)) {
17661 		cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
17662 	} else if (ieee80211_is_beacon(mgmt->frame_control)) {
17663 		if (wdev->unprot_beacon_reported &&
17664 		    elapsed_jiffies_msecs(wdev->unprot_beacon_reported) < 10000)
17665 			return;
17666 		cmd = NL80211_CMD_UNPROT_BEACON;
17667 		wdev->unprot_beacon_reported = jiffies;
17668 	} else {
17669 		return;
17670 	}
17671 
17672 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
17673 	nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
17674 				NULL, 0, false);
17675 }
17676 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
17677 
17678 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
17679 				      struct net_device *netdev, int cmd,
17680 				      const u8 *addr, gfp_t gfp)
17681 {
17682 	struct sk_buff *msg;
17683 	void *hdr;
17684 
17685 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17686 	if (!msg)
17687 		return;
17688 
17689 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17690 	if (!hdr) {
17691 		nlmsg_free(msg);
17692 		return;
17693 	}
17694 
17695 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17696 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17697 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
17698 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
17699 		goto nla_put_failure;
17700 
17701 	genlmsg_end(msg, hdr);
17702 
17703 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17704 				NL80211_MCGRP_MLME, gfp);
17705 	return;
17706 
17707  nla_put_failure:
17708 	nlmsg_free(msg);
17709 }
17710 
17711 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
17712 			       struct net_device *netdev, const u8 *addr,
17713 			       gfp_t gfp)
17714 {
17715 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
17716 				  addr, gfp);
17717 }
17718 
17719 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
17720 				struct net_device *netdev, const u8 *addr,
17721 				gfp_t gfp)
17722 {
17723 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
17724 				  addr, gfp);
17725 }
17726 
17727 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
17728 				 struct net_device *netdev,
17729 				 struct cfg80211_connect_resp_params *cr,
17730 				 gfp_t gfp)
17731 {
17732 	struct sk_buff *msg;
17733 	void *hdr;
17734 	unsigned int link;
17735 	size_t link_info_size = 0;
17736 	const u8 *connected_addr = cr->valid_links ?
17737 				   cr->ap_mld_addr : cr->links[0].bssid;
17738 
17739 	if (cr->valid_links) {
17740 		for_each_valid_link(cr, link) {
17741 			/* Nested attribute header */
17742 			link_info_size += NLA_HDRLEN;
17743 			/* Link ID */
17744 			link_info_size += nla_total_size(sizeof(u8));
17745 			link_info_size += cr->links[link].addr ?
17746 					  nla_total_size(ETH_ALEN) : 0;
17747 			link_info_size += (cr->links[link].bssid ||
17748 					   cr->links[link].bss) ?
17749 					  nla_total_size(ETH_ALEN) : 0;
17750 		}
17751 	}
17752 
17753 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
17754 			cr->fils.kek_len + cr->fils.pmk_len +
17755 			(cr->fils.pmkid ? WLAN_PMKID_LEN : 0) + link_info_size,
17756 			gfp);
17757 	if (!msg)
17758 		return;
17759 
17760 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
17761 	if (!hdr) {
17762 		nlmsg_free(msg);
17763 		return;
17764 	}
17765 
17766 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17767 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17768 	    (connected_addr &&
17769 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr)) ||
17770 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
17771 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
17772 			cr->status) ||
17773 	    (cr->status < 0 &&
17774 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
17775 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
17776 			  cr->timeout_reason))) ||
17777 	    (cr->req_ie &&
17778 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
17779 	    (cr->resp_ie &&
17780 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
17781 		     cr->resp_ie)) ||
17782 	    (cr->fils.update_erp_next_seq_num &&
17783 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
17784 			 cr->fils.erp_next_seq_num)) ||
17785 	    (cr->status == WLAN_STATUS_SUCCESS &&
17786 	     ((cr->fils.kek &&
17787 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
17788 		       cr->fils.kek)) ||
17789 	      (cr->fils.pmk &&
17790 	       nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
17791 	      (cr->fils.pmkid &&
17792 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
17793 		goto nla_put_failure;
17794 
17795 	if (cr->valid_links) {
17796 		int i = 1;
17797 		struct nlattr *nested;
17798 
17799 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
17800 		if (!nested)
17801 			goto nla_put_failure;
17802 
17803 		for_each_valid_link(cr, link) {
17804 			struct nlattr *nested_mlo_links;
17805 			const u8 *bssid = cr->links[link].bss ?
17806 					  cr->links[link].bss->bssid :
17807 					  cr->links[link].bssid;
17808 
17809 			nested_mlo_links = nla_nest_start(msg, i);
17810 			if (!nested_mlo_links)
17811 				goto nla_put_failure;
17812 
17813 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
17814 			    (bssid &&
17815 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
17816 			    (cr->links[link].addr &&
17817 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
17818 				     cr->links[link].addr)))
17819 				goto nla_put_failure;
17820 
17821 			nla_nest_end(msg, nested_mlo_links);
17822 			i++;
17823 		}
17824 		nla_nest_end(msg, nested);
17825 	}
17826 
17827 	genlmsg_end(msg, hdr);
17828 
17829 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17830 				NL80211_MCGRP_MLME, gfp);
17831 	return;
17832 
17833  nla_put_failure:
17834 	nlmsg_free(msg);
17835 }
17836 
17837 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
17838 			 struct net_device *netdev,
17839 			 struct cfg80211_roam_info *info, gfp_t gfp)
17840 {
17841 	struct sk_buff *msg;
17842 	void *hdr;
17843 	size_t link_info_size = 0;
17844 	unsigned int link;
17845 	const u8 *connected_addr = info->ap_mld_addr ?
17846 				   info->ap_mld_addr :
17847 				   (info->links[0].bss ?
17848 				    info->links[0].bss->bssid :
17849 				    info->links[0].bssid);
17850 
17851 	if (info->valid_links) {
17852 		for_each_valid_link(info, link) {
17853 			/* Nested attribute header */
17854 			link_info_size += NLA_HDRLEN;
17855 			/* Link ID */
17856 			link_info_size += nla_total_size(sizeof(u8));
17857 			link_info_size += info->links[link].addr ?
17858 					  nla_total_size(ETH_ALEN) : 0;
17859 			link_info_size += (info->links[link].bssid ||
17860 					   info->links[link].bss) ?
17861 					  nla_total_size(ETH_ALEN) : 0;
17862 		}
17863 	}
17864 
17865 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
17866 			info->fils.kek_len + info->fils.pmk_len +
17867 			(info->fils.pmkid ? WLAN_PMKID_LEN : 0) +
17868 			link_info_size, gfp);
17869 	if (!msg)
17870 		return;
17871 
17872 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
17873 	if (!hdr) {
17874 		nlmsg_free(msg);
17875 		return;
17876 	}
17877 
17878 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17879 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17880 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr) ||
17881 	    (info->req_ie &&
17882 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
17883 		     info->req_ie)) ||
17884 	    (info->resp_ie &&
17885 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
17886 		     info->resp_ie)) ||
17887 	    (info->fils.update_erp_next_seq_num &&
17888 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
17889 			 info->fils.erp_next_seq_num)) ||
17890 	    (info->fils.kek &&
17891 	     nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
17892 		     info->fils.kek)) ||
17893 	    (info->fils.pmk &&
17894 	     nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
17895 	    (info->fils.pmkid &&
17896 	     nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
17897 		goto nla_put_failure;
17898 
17899 	if (info->valid_links) {
17900 		int i = 1;
17901 		struct nlattr *nested;
17902 
17903 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
17904 		if (!nested)
17905 			goto nla_put_failure;
17906 
17907 		for_each_valid_link(info, link) {
17908 			struct nlattr *nested_mlo_links;
17909 			const u8 *bssid = info->links[link].bss ?
17910 					  info->links[link].bss->bssid :
17911 					  info->links[link].bssid;
17912 
17913 			nested_mlo_links = nla_nest_start(msg, i);
17914 			if (!nested_mlo_links)
17915 				goto nla_put_failure;
17916 
17917 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
17918 			    (bssid &&
17919 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
17920 			    (info->links[link].addr &&
17921 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
17922 				     info->links[link].addr)))
17923 				goto nla_put_failure;
17924 
17925 			nla_nest_end(msg, nested_mlo_links);
17926 			i++;
17927 		}
17928 		nla_nest_end(msg, nested);
17929 	}
17930 
17931 	genlmsg_end(msg, hdr);
17932 
17933 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17934 				NL80211_MCGRP_MLME, gfp);
17935 	return;
17936 
17937  nla_put_failure:
17938 	nlmsg_free(msg);
17939 }
17940 
17941 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
17942 				  struct net_device *netdev, const u8 *bssid)
17943 {
17944 	struct sk_buff *msg;
17945 	void *hdr;
17946 
17947 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17948 	if (!msg)
17949 		return;
17950 
17951 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
17952 	if (!hdr) {
17953 		nlmsg_free(msg);
17954 		return;
17955 	}
17956 
17957 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17958 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17959 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
17960 		goto nla_put_failure;
17961 
17962 	genlmsg_end(msg, hdr);
17963 
17964 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17965 				NL80211_MCGRP_MLME, GFP_KERNEL);
17966 	return;
17967 
17968  nla_put_failure:
17969 	nlmsg_free(msg);
17970 }
17971 
17972 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
17973 			       struct net_device *netdev, u16 reason,
17974 			       const u8 *ie, size_t ie_len, bool from_ap)
17975 {
17976 	struct sk_buff *msg;
17977 	void *hdr;
17978 
17979 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
17980 	if (!msg)
17981 		return;
17982 
17983 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
17984 	if (!hdr) {
17985 		nlmsg_free(msg);
17986 		return;
17987 	}
17988 
17989 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17990 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17991 	    (reason &&
17992 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
17993 	    (from_ap &&
17994 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
17995 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
17996 		goto nla_put_failure;
17997 
17998 	genlmsg_end(msg, hdr);
17999 
18000 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18001 				NL80211_MCGRP_MLME, GFP_KERNEL);
18002 	return;
18003 
18004  nla_put_failure:
18005 	nlmsg_free(msg);
18006 }
18007 
18008 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
18009 			     struct net_device *netdev, const u8 *bssid,
18010 			     gfp_t gfp)
18011 {
18012 	struct sk_buff *msg;
18013 	void *hdr;
18014 
18015 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18016 	if (!msg)
18017 		return;
18018 
18019 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
18020 	if (!hdr) {
18021 		nlmsg_free(msg);
18022 		return;
18023 	}
18024 
18025 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18026 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18027 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18028 		goto nla_put_failure;
18029 
18030 	genlmsg_end(msg, hdr);
18031 
18032 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18033 				NL80211_MCGRP_MLME, gfp);
18034 	return;
18035 
18036  nla_put_failure:
18037 	nlmsg_free(msg);
18038 }
18039 
18040 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
18041 					const u8 *ie, u8 ie_len,
18042 					int sig_dbm, gfp_t gfp)
18043 {
18044 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18045 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18046 	struct sk_buff *msg;
18047 	void *hdr;
18048 
18049 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
18050 		return;
18051 
18052 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
18053 
18054 	msg = nlmsg_new(100 + ie_len, gfp);
18055 	if (!msg)
18056 		return;
18057 
18058 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
18059 	if (!hdr) {
18060 		nlmsg_free(msg);
18061 		return;
18062 	}
18063 
18064 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18065 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18066 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
18067 	    (ie_len && ie &&
18068 	     nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
18069 	    (sig_dbm &&
18070 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
18071 		goto nla_put_failure;
18072 
18073 	genlmsg_end(msg, hdr);
18074 
18075 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18076 				NL80211_MCGRP_MLME, gfp);
18077 	return;
18078 
18079  nla_put_failure:
18080 	nlmsg_free(msg);
18081 }
18082 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
18083 
18084 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
18085 				 struct net_device *netdev, const u8 *addr,
18086 				 enum nl80211_key_type key_type, int key_id,
18087 				 const u8 *tsc, gfp_t gfp)
18088 {
18089 	struct sk_buff *msg;
18090 	void *hdr;
18091 
18092 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18093 	if (!msg)
18094 		return;
18095 
18096 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
18097 	if (!hdr) {
18098 		nlmsg_free(msg);
18099 		return;
18100 	}
18101 
18102 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18103 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18104 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
18105 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
18106 	    (key_id != -1 &&
18107 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
18108 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
18109 		goto nla_put_failure;
18110 
18111 	genlmsg_end(msg, hdr);
18112 
18113 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18114 				NL80211_MCGRP_MLME, gfp);
18115 	return;
18116 
18117  nla_put_failure:
18118 	nlmsg_free(msg);
18119 }
18120 
18121 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
18122 				    struct ieee80211_channel *channel_before,
18123 				    struct ieee80211_channel *channel_after)
18124 {
18125 	struct sk_buff *msg;
18126 	void *hdr;
18127 	struct nlattr *nl_freq;
18128 
18129 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
18130 	if (!msg)
18131 		return;
18132 
18133 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
18134 	if (!hdr) {
18135 		nlmsg_free(msg);
18136 		return;
18137 	}
18138 
18139 	/*
18140 	 * Since we are applying the beacon hint to a wiphy we know its
18141 	 * wiphy_idx is valid
18142 	 */
18143 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
18144 		goto nla_put_failure;
18145 
18146 	/* Before */
18147 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
18148 	if (!nl_freq)
18149 		goto nla_put_failure;
18150 
18151 	if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
18152 		goto nla_put_failure;
18153 	nla_nest_end(msg, nl_freq);
18154 
18155 	/* After */
18156 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
18157 	if (!nl_freq)
18158 		goto nla_put_failure;
18159 
18160 	if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
18161 		goto nla_put_failure;
18162 	nla_nest_end(msg, nl_freq);
18163 
18164 	genlmsg_end(msg, hdr);
18165 
18166 	rcu_read_lock();
18167 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
18168 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
18169 	rcu_read_unlock();
18170 
18171 	return;
18172 
18173 nla_put_failure:
18174 	nlmsg_free(msg);
18175 }
18176 
18177 static void nl80211_send_remain_on_chan_event(
18178 	int cmd, struct cfg80211_registered_device *rdev,
18179 	struct wireless_dev *wdev, u64 cookie,
18180 	struct ieee80211_channel *chan,
18181 	unsigned int duration, gfp_t gfp)
18182 {
18183 	struct sk_buff *msg;
18184 	void *hdr;
18185 
18186 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18187 	if (!msg)
18188 		return;
18189 
18190 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18191 	if (!hdr) {
18192 		nlmsg_free(msg);
18193 		return;
18194 	}
18195 
18196 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18197 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18198 					 wdev->netdev->ifindex)) ||
18199 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18200 			      NL80211_ATTR_PAD) ||
18201 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
18202 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
18203 			NL80211_CHAN_NO_HT) ||
18204 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
18205 			      NL80211_ATTR_PAD))
18206 		goto nla_put_failure;
18207 
18208 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
18209 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
18210 		goto nla_put_failure;
18211 
18212 	genlmsg_end(msg, hdr);
18213 
18214 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18215 				NL80211_MCGRP_MLME, gfp);
18216 	return;
18217 
18218  nla_put_failure:
18219 	nlmsg_free(msg);
18220 }
18221 
18222 void cfg80211_assoc_comeback(struct net_device *netdev,
18223 			     const u8 *ap_addr, u32 timeout)
18224 {
18225 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
18226 	struct wiphy *wiphy = wdev->wiphy;
18227 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18228 	struct sk_buff *msg;
18229 	void *hdr;
18230 
18231 	trace_cfg80211_assoc_comeback(wdev, ap_addr, timeout);
18232 
18233 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18234 	if (!msg)
18235 		return;
18236 
18237 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ASSOC_COMEBACK);
18238 	if (!hdr) {
18239 		nlmsg_free(msg);
18240 		return;
18241 	}
18242 
18243 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18244 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18245 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ap_addr) ||
18246 	    nla_put_u32(msg, NL80211_ATTR_TIMEOUT, timeout))
18247 		goto nla_put_failure;
18248 
18249 	genlmsg_end(msg, hdr);
18250 
18251 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18252 				NL80211_MCGRP_MLME, GFP_KERNEL);
18253 	return;
18254 
18255  nla_put_failure:
18256 	nlmsg_free(msg);
18257 }
18258 EXPORT_SYMBOL(cfg80211_assoc_comeback);
18259 
18260 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
18261 			       struct ieee80211_channel *chan,
18262 			       unsigned int duration, gfp_t gfp)
18263 {
18264 	struct wiphy *wiphy = wdev->wiphy;
18265 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18266 
18267 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
18268 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
18269 					  rdev, wdev, cookie, chan,
18270 					  duration, gfp);
18271 }
18272 EXPORT_SYMBOL(cfg80211_ready_on_channel);
18273 
18274 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
18275 					struct ieee80211_channel *chan,
18276 					gfp_t gfp)
18277 {
18278 	struct wiphy *wiphy = wdev->wiphy;
18279 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18280 
18281 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
18282 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
18283 					  rdev, wdev, cookie, chan, 0, gfp);
18284 }
18285 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
18286 
18287 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
18288 					struct ieee80211_channel *chan,
18289 					gfp_t gfp)
18290 {
18291 	struct wiphy *wiphy = wdev->wiphy;
18292 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18293 
18294 	trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
18295 	nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
18296 					  rdev, wdev, cookie, chan, 0, gfp);
18297 }
18298 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
18299 
18300 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
18301 		      struct station_info *sinfo, gfp_t gfp)
18302 {
18303 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18304 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18305 	struct sk_buff *msg;
18306 
18307 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
18308 
18309 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18310 	if (!msg)
18311 		return;
18312 
18313 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
18314 				 rdev, dev, mac_addr, sinfo) < 0) {
18315 		nlmsg_free(msg);
18316 		return;
18317 	}
18318 
18319 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18320 				NL80211_MCGRP_MLME, gfp);
18321 }
18322 EXPORT_SYMBOL(cfg80211_new_sta);
18323 
18324 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
18325 			    struct station_info *sinfo, gfp_t gfp)
18326 {
18327 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18328 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18329 	struct sk_buff *msg;
18330 	struct station_info empty_sinfo = {};
18331 
18332 	if (!sinfo)
18333 		sinfo = &empty_sinfo;
18334 
18335 	trace_cfg80211_del_sta(dev, mac_addr);
18336 
18337 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18338 	if (!msg) {
18339 		cfg80211_sinfo_release_content(sinfo);
18340 		return;
18341 	}
18342 
18343 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
18344 				 rdev, dev, mac_addr, sinfo) < 0) {
18345 		nlmsg_free(msg);
18346 		return;
18347 	}
18348 
18349 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18350 				NL80211_MCGRP_MLME, gfp);
18351 }
18352 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
18353 
18354 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
18355 			  enum nl80211_connect_failed_reason reason,
18356 			  gfp_t gfp)
18357 {
18358 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18359 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18360 	struct sk_buff *msg;
18361 	void *hdr;
18362 
18363 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
18364 	if (!msg)
18365 		return;
18366 
18367 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
18368 	if (!hdr) {
18369 		nlmsg_free(msg);
18370 		return;
18371 	}
18372 
18373 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18374 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
18375 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
18376 		goto nla_put_failure;
18377 
18378 	genlmsg_end(msg, hdr);
18379 
18380 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18381 				NL80211_MCGRP_MLME, gfp);
18382 	return;
18383 
18384  nla_put_failure:
18385 	nlmsg_free(msg);
18386 }
18387 EXPORT_SYMBOL(cfg80211_conn_failed);
18388 
18389 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
18390 				       const u8 *addr, gfp_t gfp)
18391 {
18392 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18393 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18394 	struct sk_buff *msg;
18395 	void *hdr;
18396 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
18397 
18398 	if (!nlportid)
18399 		return false;
18400 
18401 	msg = nlmsg_new(100, gfp);
18402 	if (!msg)
18403 		return true;
18404 
18405 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18406 	if (!hdr) {
18407 		nlmsg_free(msg);
18408 		return true;
18409 	}
18410 
18411 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18412 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18413 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
18414 		goto nla_put_failure;
18415 
18416 	genlmsg_end(msg, hdr);
18417 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18418 	return true;
18419 
18420  nla_put_failure:
18421 	nlmsg_free(msg);
18422 	return true;
18423 }
18424 
18425 bool cfg80211_rx_spurious_frame(struct net_device *dev,
18426 				const u8 *addr, gfp_t gfp)
18427 {
18428 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18429 	bool ret;
18430 
18431 	trace_cfg80211_rx_spurious_frame(dev, addr);
18432 
18433 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18434 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
18435 		trace_cfg80211_return_bool(false);
18436 		return false;
18437 	}
18438 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
18439 					 addr, gfp);
18440 	trace_cfg80211_return_bool(ret);
18441 	return ret;
18442 }
18443 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
18444 
18445 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
18446 					const u8 *addr, gfp_t gfp)
18447 {
18448 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18449 	bool ret;
18450 
18451 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
18452 
18453 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18454 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
18455 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
18456 		trace_cfg80211_return_bool(false);
18457 		return false;
18458 	}
18459 	ret = __nl80211_unexpected_frame(dev,
18460 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
18461 					 addr, gfp);
18462 	trace_cfg80211_return_bool(ret);
18463 	return ret;
18464 }
18465 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
18466 
18467 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
18468 		      struct wireless_dev *wdev, u32 nlportid,
18469 		      struct cfg80211_rx_info *info, gfp_t gfp)
18470 {
18471 	struct net_device *netdev = wdev->netdev;
18472 	struct sk_buff *msg;
18473 	void *hdr;
18474 
18475 	msg = nlmsg_new(100 + info->len, gfp);
18476 	if (!msg)
18477 		return -ENOMEM;
18478 
18479 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
18480 	if (!hdr) {
18481 		nlmsg_free(msg);
18482 		return -ENOMEM;
18483 	}
18484 
18485 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18486 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18487 					netdev->ifindex)) ||
18488 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18489 			      NL80211_ATTR_PAD) ||
18490 	    (info->have_link_id &&
18491 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, info->link_id)) ||
18492 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, KHZ_TO_MHZ(info->freq)) ||
18493 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET, info->freq % 1000) ||
18494 	    (info->sig_dbm &&
18495 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, info->sig_dbm)) ||
18496 	    nla_put(msg, NL80211_ATTR_FRAME, info->len, info->buf) ||
18497 	    (info->flags &&
18498 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, info->flags)) ||
18499 	    (info->rx_tstamp && nla_put_u64_64bit(msg,
18500 						  NL80211_ATTR_RX_HW_TIMESTAMP,
18501 						  info->rx_tstamp,
18502 						  NL80211_ATTR_PAD)) ||
18503 	    (info->ack_tstamp && nla_put_u64_64bit(msg,
18504 						   NL80211_ATTR_TX_HW_TIMESTAMP,
18505 						   info->ack_tstamp,
18506 						   NL80211_ATTR_PAD)))
18507 		goto nla_put_failure;
18508 
18509 	genlmsg_end(msg, hdr);
18510 
18511 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18512 
18513  nla_put_failure:
18514 	nlmsg_free(msg);
18515 	return -ENOBUFS;
18516 }
18517 
18518 static void nl80211_frame_tx_status(struct wireless_dev *wdev,
18519 				    struct cfg80211_tx_status *status,
18520 				    gfp_t gfp, enum nl80211_commands command)
18521 {
18522 	struct wiphy *wiphy = wdev->wiphy;
18523 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18524 	struct net_device *netdev = wdev->netdev;
18525 	struct sk_buff *msg;
18526 	void *hdr;
18527 
18528 	if (command == NL80211_CMD_FRAME_TX_STATUS)
18529 		trace_cfg80211_mgmt_tx_status(wdev, status->cookie,
18530 					      status->ack);
18531 	else
18532 		trace_cfg80211_control_port_tx_status(wdev, status->cookie,
18533 						      status->ack);
18534 
18535 	msg = nlmsg_new(100 + status->len, gfp);
18536 	if (!msg)
18537 		return;
18538 
18539 	hdr = nl80211hdr_put(msg, 0, 0, 0, command);
18540 	if (!hdr) {
18541 		nlmsg_free(msg);
18542 		return;
18543 	}
18544 
18545 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18546 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18547 				   netdev->ifindex)) ||
18548 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18549 			      NL80211_ATTR_PAD) ||
18550 	    nla_put(msg, NL80211_ATTR_FRAME, status->len, status->buf) ||
18551 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, status->cookie,
18552 			      NL80211_ATTR_PAD) ||
18553 	    (status->ack && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
18554 	    (status->tx_tstamp &&
18555 	     nla_put_u64_64bit(msg, NL80211_ATTR_TX_HW_TIMESTAMP,
18556 			       status->tx_tstamp, NL80211_ATTR_PAD)) ||
18557 	    (status->ack_tstamp &&
18558 	     nla_put_u64_64bit(msg, NL80211_ATTR_RX_HW_TIMESTAMP,
18559 			       status->ack_tstamp, NL80211_ATTR_PAD)))
18560 		goto nla_put_failure;
18561 
18562 	genlmsg_end(msg, hdr);
18563 
18564 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18565 				NL80211_MCGRP_MLME, gfp);
18566 	return;
18567 
18568 nla_put_failure:
18569 	nlmsg_free(msg);
18570 }
18571 
18572 void cfg80211_control_port_tx_status(struct wireless_dev *wdev, u64 cookie,
18573 				     const u8 *buf, size_t len, bool ack,
18574 				     gfp_t gfp)
18575 {
18576 	struct cfg80211_tx_status status = {
18577 		.cookie = cookie,
18578 		.buf = buf,
18579 		.len = len,
18580 		.ack = ack
18581 	};
18582 
18583 	nl80211_frame_tx_status(wdev, &status, gfp,
18584 				NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS);
18585 }
18586 EXPORT_SYMBOL(cfg80211_control_port_tx_status);
18587 
18588 void cfg80211_mgmt_tx_status_ext(struct wireless_dev *wdev,
18589 				 struct cfg80211_tx_status *status, gfp_t gfp)
18590 {
18591 	nl80211_frame_tx_status(wdev, status, gfp, NL80211_CMD_FRAME_TX_STATUS);
18592 }
18593 EXPORT_SYMBOL(cfg80211_mgmt_tx_status_ext);
18594 
18595 static int __nl80211_rx_control_port(struct net_device *dev,
18596 				     struct sk_buff *skb,
18597 				     bool unencrypted, gfp_t gfp)
18598 {
18599 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18600 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18601 	struct ethhdr *ehdr = eth_hdr(skb);
18602 	const u8 *addr = ehdr->h_source;
18603 	u16 proto = be16_to_cpu(skb->protocol);
18604 	struct sk_buff *msg;
18605 	void *hdr;
18606 	struct nlattr *frame;
18607 
18608 	u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
18609 
18610 	if (!nlportid)
18611 		return -ENOENT;
18612 
18613 	msg = nlmsg_new(100 + skb->len, gfp);
18614 	if (!msg)
18615 		return -ENOMEM;
18616 
18617 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
18618 	if (!hdr) {
18619 		nlmsg_free(msg);
18620 		return -ENOBUFS;
18621 	}
18622 
18623 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18624 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18625 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18626 			      NL80211_ATTR_PAD) ||
18627 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
18628 	    nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
18629 	    (unencrypted && nla_put_flag(msg,
18630 					 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
18631 		goto nla_put_failure;
18632 
18633 	frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
18634 	if (!frame)
18635 		goto nla_put_failure;
18636 
18637 	skb_copy_bits(skb, 0, nla_data(frame), skb->len);
18638 	genlmsg_end(msg, hdr);
18639 
18640 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18641 
18642  nla_put_failure:
18643 	nlmsg_free(msg);
18644 	return -ENOBUFS;
18645 }
18646 
18647 bool cfg80211_rx_control_port(struct net_device *dev,
18648 			      struct sk_buff *skb, bool unencrypted)
18649 {
18650 	int ret;
18651 
18652 	trace_cfg80211_rx_control_port(dev, skb, unencrypted);
18653 	ret = __nl80211_rx_control_port(dev, skb, unencrypted, GFP_ATOMIC);
18654 	trace_cfg80211_return_bool(ret == 0);
18655 	return ret == 0;
18656 }
18657 EXPORT_SYMBOL(cfg80211_rx_control_port);
18658 
18659 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
18660 					    const char *mac, gfp_t gfp)
18661 {
18662 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18663 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18664 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18665 	void **cb;
18666 
18667 	if (!msg)
18668 		return NULL;
18669 
18670 	cb = (void **)msg->cb;
18671 
18672 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
18673 	if (!cb[0]) {
18674 		nlmsg_free(msg);
18675 		return NULL;
18676 	}
18677 
18678 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18679 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
18680 		goto nla_put_failure;
18681 
18682 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
18683 		goto nla_put_failure;
18684 
18685 	cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
18686 	if (!cb[1])
18687 		goto nla_put_failure;
18688 
18689 	cb[2] = rdev;
18690 
18691 	return msg;
18692  nla_put_failure:
18693 	nlmsg_free(msg);
18694 	return NULL;
18695 }
18696 
18697 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
18698 {
18699 	void **cb = (void **)msg->cb;
18700 	struct cfg80211_registered_device *rdev = cb[2];
18701 
18702 	nla_nest_end(msg, cb[1]);
18703 	genlmsg_end(msg, cb[0]);
18704 
18705 	memset(msg->cb, 0, sizeof(msg->cb));
18706 
18707 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18708 				NL80211_MCGRP_MLME, gfp);
18709 }
18710 
18711 void cfg80211_cqm_rssi_notify(struct net_device *dev,
18712 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
18713 			      s32 rssi_level, gfp_t gfp)
18714 {
18715 	struct sk_buff *msg;
18716 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18717 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18718 
18719 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
18720 
18721 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
18722 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
18723 		return;
18724 
18725 	if (wdev->cqm_config) {
18726 		wdev->cqm_config->last_rssi_event_value = rssi_level;
18727 
18728 		cfg80211_cqm_rssi_update(rdev, dev);
18729 
18730 		if (rssi_level == 0)
18731 			rssi_level = wdev->cqm_config->last_rssi_event_value;
18732 	}
18733 
18734 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
18735 	if (!msg)
18736 		return;
18737 
18738 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
18739 			rssi_event))
18740 		goto nla_put_failure;
18741 
18742 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
18743 				      rssi_level))
18744 		goto nla_put_failure;
18745 
18746 	cfg80211_send_cqm(msg, gfp);
18747 
18748 	return;
18749 
18750  nla_put_failure:
18751 	nlmsg_free(msg);
18752 }
18753 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
18754 
18755 void cfg80211_cqm_txe_notify(struct net_device *dev,
18756 			     const u8 *peer, u32 num_packets,
18757 			     u32 rate, u32 intvl, gfp_t gfp)
18758 {
18759 	struct sk_buff *msg;
18760 
18761 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
18762 	if (!msg)
18763 		return;
18764 
18765 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
18766 		goto nla_put_failure;
18767 
18768 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
18769 		goto nla_put_failure;
18770 
18771 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
18772 		goto nla_put_failure;
18773 
18774 	cfg80211_send_cqm(msg, gfp);
18775 	return;
18776 
18777  nla_put_failure:
18778 	nlmsg_free(msg);
18779 }
18780 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
18781 
18782 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
18783 				 const u8 *peer, u32 num_packets, gfp_t gfp)
18784 {
18785 	struct sk_buff *msg;
18786 
18787 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
18788 
18789 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
18790 	if (!msg)
18791 		return;
18792 
18793 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
18794 		goto nla_put_failure;
18795 
18796 	cfg80211_send_cqm(msg, gfp);
18797 	return;
18798 
18799  nla_put_failure:
18800 	nlmsg_free(msg);
18801 }
18802 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
18803 
18804 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
18805 {
18806 	struct sk_buff *msg;
18807 
18808 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
18809 	if (!msg)
18810 		return;
18811 
18812 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
18813 		goto nla_put_failure;
18814 
18815 	cfg80211_send_cqm(msg, gfp);
18816 	return;
18817 
18818  nla_put_failure:
18819 	nlmsg_free(msg);
18820 }
18821 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
18822 
18823 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
18824 				     struct net_device *netdev, const u8 *bssid,
18825 				     const u8 *replay_ctr, gfp_t gfp)
18826 {
18827 	struct sk_buff *msg;
18828 	struct nlattr *rekey_attr;
18829 	void *hdr;
18830 
18831 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18832 	if (!msg)
18833 		return;
18834 
18835 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
18836 	if (!hdr) {
18837 		nlmsg_free(msg);
18838 		return;
18839 	}
18840 
18841 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18842 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18843 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18844 		goto nla_put_failure;
18845 
18846 	rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
18847 	if (!rekey_attr)
18848 		goto nla_put_failure;
18849 
18850 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
18851 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
18852 		goto nla_put_failure;
18853 
18854 	nla_nest_end(msg, rekey_attr);
18855 
18856 	genlmsg_end(msg, hdr);
18857 
18858 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18859 				NL80211_MCGRP_MLME, gfp);
18860 	return;
18861 
18862  nla_put_failure:
18863 	nlmsg_free(msg);
18864 }
18865 
18866 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
18867 			       const u8 *replay_ctr, gfp_t gfp)
18868 {
18869 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18870 	struct wiphy *wiphy = wdev->wiphy;
18871 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18872 
18873 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
18874 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
18875 }
18876 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
18877 
18878 static void
18879 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
18880 			       struct net_device *netdev, int index,
18881 			       const u8 *bssid, bool preauth, gfp_t gfp)
18882 {
18883 	struct sk_buff *msg;
18884 	struct nlattr *attr;
18885 	void *hdr;
18886 
18887 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18888 	if (!msg)
18889 		return;
18890 
18891 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
18892 	if (!hdr) {
18893 		nlmsg_free(msg);
18894 		return;
18895 	}
18896 
18897 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18898 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
18899 		goto nla_put_failure;
18900 
18901 	attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
18902 	if (!attr)
18903 		goto nla_put_failure;
18904 
18905 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
18906 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
18907 	    (preauth &&
18908 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
18909 		goto nla_put_failure;
18910 
18911 	nla_nest_end(msg, attr);
18912 
18913 	genlmsg_end(msg, hdr);
18914 
18915 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18916 				NL80211_MCGRP_MLME, gfp);
18917 	return;
18918 
18919  nla_put_failure:
18920 	nlmsg_free(msg);
18921 }
18922 
18923 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
18924 				     const u8 *bssid, bool preauth, gfp_t gfp)
18925 {
18926 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18927 	struct wiphy *wiphy = wdev->wiphy;
18928 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18929 
18930 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
18931 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
18932 }
18933 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
18934 
18935 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
18936 				     struct net_device *netdev,
18937 				     unsigned int link_id,
18938 				     struct cfg80211_chan_def *chandef,
18939 				     gfp_t gfp,
18940 				     enum nl80211_commands notif,
18941 				     u8 count, bool quiet)
18942 {
18943 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
18944 	struct sk_buff *msg;
18945 	void *hdr;
18946 
18947 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18948 	if (!msg)
18949 		return;
18950 
18951 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
18952 	if (!hdr) {
18953 		nlmsg_free(msg);
18954 		return;
18955 	}
18956 
18957 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
18958 		goto nla_put_failure;
18959 
18960 	if (wdev->valid_links &&
18961 	    nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
18962 		goto nla_put_failure;
18963 
18964 	if (nl80211_send_chandef(msg, chandef))
18965 		goto nla_put_failure;
18966 
18967 	if (notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) {
18968 		if (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count))
18969 			goto nla_put_failure;
18970 		if (quiet &&
18971 		    nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX))
18972 			goto nla_put_failure;
18973 	}
18974 
18975 	genlmsg_end(msg, hdr);
18976 
18977 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18978 				NL80211_MCGRP_MLME, gfp);
18979 	return;
18980 
18981  nla_put_failure:
18982 	nlmsg_free(msg);
18983 }
18984 
18985 void cfg80211_ch_switch_notify(struct net_device *dev,
18986 			       struct cfg80211_chan_def *chandef,
18987 			       unsigned int link_id)
18988 {
18989 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18990 	struct wiphy *wiphy = wdev->wiphy;
18991 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18992 
18993 	ASSERT_WDEV_LOCK(wdev);
18994 	WARN_INVALID_LINK_ID(wdev, link_id);
18995 
18996 	trace_cfg80211_ch_switch_notify(dev, chandef, link_id);
18997 
18998 	switch (wdev->iftype) {
18999 	case NL80211_IFTYPE_STATION:
19000 	case NL80211_IFTYPE_P2P_CLIENT:
19001 		if (!WARN_ON(!wdev->links[link_id].client.current_bss))
19002 			cfg80211_update_assoc_bss_entry(wdev, link_id,
19003 							chandef->chan);
19004 		break;
19005 	case NL80211_IFTYPE_MESH_POINT:
19006 		wdev->u.mesh.chandef = *chandef;
19007 		wdev->u.mesh.preset_chandef = *chandef;
19008 		break;
19009 	case NL80211_IFTYPE_AP:
19010 	case NL80211_IFTYPE_P2P_GO:
19011 		wdev->links[link_id].ap.chandef = *chandef;
19012 		break;
19013 	case NL80211_IFTYPE_ADHOC:
19014 		wdev->u.ibss.chandef = *chandef;
19015 		break;
19016 	default:
19017 		WARN_ON(1);
19018 		break;
19019 	}
19020 
19021 	cfg80211_sched_dfs_chan_update(rdev);
19022 
19023 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19024 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0, false);
19025 }
19026 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
19027 
19028 void cfg80211_ch_switch_started_notify(struct net_device *dev,
19029 				       struct cfg80211_chan_def *chandef,
19030 				       unsigned int link_id, u8 count,
19031 				       bool quiet)
19032 {
19033 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19034 	struct wiphy *wiphy = wdev->wiphy;
19035 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19036 
19037 	ASSERT_WDEV_LOCK(wdev);
19038 	WARN_INVALID_LINK_ID(wdev, link_id);
19039 
19040 	trace_cfg80211_ch_switch_started_notify(dev, chandef, link_id);
19041 
19042 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19043 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY,
19044 				 count, quiet);
19045 }
19046 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
19047 
19048 int cfg80211_bss_color_notify(struct net_device *dev, gfp_t gfp,
19049 			      enum nl80211_commands cmd, u8 count,
19050 			      u64 color_bitmap)
19051 {
19052 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19053 	struct wiphy *wiphy = wdev->wiphy;
19054 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19055 	struct sk_buff *msg;
19056 	void *hdr;
19057 
19058 	ASSERT_WDEV_LOCK(wdev);
19059 
19060 	trace_cfg80211_bss_color_notify(dev, cmd, count, color_bitmap);
19061 
19062 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19063 	if (!msg)
19064 		return -ENOMEM;
19065 
19066 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
19067 	if (!hdr)
19068 		goto nla_put_failure;
19069 
19070 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19071 		goto nla_put_failure;
19072 
19073 	if (cmd == NL80211_CMD_COLOR_CHANGE_STARTED &&
19074 	    nla_put_u32(msg, NL80211_ATTR_COLOR_CHANGE_COUNT, count))
19075 		goto nla_put_failure;
19076 
19077 	if (cmd == NL80211_CMD_OBSS_COLOR_COLLISION &&
19078 	    nla_put_u64_64bit(msg, NL80211_ATTR_OBSS_COLOR_BITMAP,
19079 			      color_bitmap, NL80211_ATTR_PAD))
19080 		goto nla_put_failure;
19081 
19082 	genlmsg_end(msg, hdr);
19083 
19084 	return genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
19085 				       msg, 0, NL80211_MCGRP_MLME, gfp);
19086 
19087 nla_put_failure:
19088 	nlmsg_free(msg);
19089 	return -EINVAL;
19090 }
19091 EXPORT_SYMBOL(cfg80211_bss_color_notify);
19092 
19093 void
19094 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
19095 		     const struct cfg80211_chan_def *chandef,
19096 		     enum nl80211_radar_event event,
19097 		     struct net_device *netdev, gfp_t gfp)
19098 {
19099 	struct sk_buff *msg;
19100 	void *hdr;
19101 
19102 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19103 	if (!msg)
19104 		return;
19105 
19106 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
19107 	if (!hdr) {
19108 		nlmsg_free(msg);
19109 		return;
19110 	}
19111 
19112 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19113 		goto nla_put_failure;
19114 
19115 	/* NOP and radar events don't need a netdev parameter */
19116 	if (netdev) {
19117 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
19118 
19119 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19120 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19121 				      NL80211_ATTR_PAD))
19122 			goto nla_put_failure;
19123 	}
19124 
19125 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
19126 		goto nla_put_failure;
19127 
19128 	if (nl80211_send_chandef(msg, chandef))
19129 		goto nla_put_failure;
19130 
19131 	genlmsg_end(msg, hdr);
19132 
19133 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19134 				NL80211_MCGRP_MLME, gfp);
19135 	return;
19136 
19137  nla_put_failure:
19138 	nlmsg_free(msg);
19139 }
19140 
19141 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
19142 				       struct sta_opmode_info *sta_opmode,
19143 				       gfp_t gfp)
19144 {
19145 	struct sk_buff *msg;
19146 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19147 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19148 	void *hdr;
19149 
19150 	if (WARN_ON(!mac))
19151 		return;
19152 
19153 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19154 	if (!msg)
19155 		return;
19156 
19157 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
19158 	if (!hdr) {
19159 		nlmsg_free(msg);
19160 		return;
19161 	}
19162 
19163 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19164 		goto nla_put_failure;
19165 
19166 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19167 		goto nla_put_failure;
19168 
19169 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
19170 		goto nla_put_failure;
19171 
19172 	if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
19173 	    nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
19174 		goto nla_put_failure;
19175 
19176 	if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
19177 	    nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
19178 		goto nla_put_failure;
19179 
19180 	if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
19181 	    nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
19182 		goto nla_put_failure;
19183 
19184 	genlmsg_end(msg, hdr);
19185 
19186 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19187 				NL80211_MCGRP_MLME, gfp);
19188 
19189 	return;
19190 
19191 nla_put_failure:
19192 	nlmsg_free(msg);
19193 }
19194 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
19195 
19196 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
19197 			   u64 cookie, bool acked, s32 ack_signal,
19198 			   bool is_valid_ack_signal, gfp_t gfp)
19199 {
19200 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19201 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19202 	struct sk_buff *msg;
19203 	void *hdr;
19204 
19205 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
19206 
19207 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19208 
19209 	if (!msg)
19210 		return;
19211 
19212 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
19213 	if (!hdr) {
19214 		nlmsg_free(msg);
19215 		return;
19216 	}
19217 
19218 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19219 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19220 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
19221 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
19222 			      NL80211_ATTR_PAD) ||
19223 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
19224 	    (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
19225 						ack_signal)))
19226 		goto nla_put_failure;
19227 
19228 	genlmsg_end(msg, hdr);
19229 
19230 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19231 				NL80211_MCGRP_MLME, gfp);
19232 	return;
19233 
19234  nla_put_failure:
19235 	nlmsg_free(msg);
19236 }
19237 EXPORT_SYMBOL(cfg80211_probe_status);
19238 
19239 void cfg80211_report_obss_beacon_khz(struct wiphy *wiphy, const u8 *frame,
19240 				     size_t len, int freq, int sig_dbm)
19241 {
19242 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19243 	struct sk_buff *msg;
19244 	void *hdr;
19245 	struct cfg80211_beacon_registration *reg;
19246 
19247 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
19248 
19249 	spin_lock_bh(&rdev->beacon_registrations_lock);
19250 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
19251 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
19252 		if (!msg) {
19253 			spin_unlock_bh(&rdev->beacon_registrations_lock);
19254 			return;
19255 		}
19256 
19257 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
19258 		if (!hdr)
19259 			goto nla_put_failure;
19260 
19261 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19262 		    (freq &&
19263 		     (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
19264 				  KHZ_TO_MHZ(freq)) ||
19265 		      nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
19266 				  freq % 1000))) ||
19267 		    (sig_dbm &&
19268 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
19269 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
19270 			goto nla_put_failure;
19271 
19272 		genlmsg_end(msg, hdr);
19273 
19274 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
19275 	}
19276 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19277 	return;
19278 
19279  nla_put_failure:
19280 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19281 	nlmsg_free(msg);
19282 }
19283 EXPORT_SYMBOL(cfg80211_report_obss_beacon_khz);
19284 
19285 #ifdef CONFIG_PM
19286 static int cfg80211_net_detect_results(struct sk_buff *msg,
19287 				       struct cfg80211_wowlan_wakeup *wakeup)
19288 {
19289 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
19290 	struct nlattr *nl_results, *nl_match, *nl_freqs;
19291 	int i, j;
19292 
19293 	nl_results = nla_nest_start_noflag(msg,
19294 					   NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
19295 	if (!nl_results)
19296 		return -EMSGSIZE;
19297 
19298 	for (i = 0; i < nd->n_matches; i++) {
19299 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
19300 
19301 		nl_match = nla_nest_start_noflag(msg, i);
19302 		if (!nl_match)
19303 			break;
19304 
19305 		/* The SSID attribute is optional in nl80211, but for
19306 		 * simplicity reasons it's always present in the
19307 		 * cfg80211 structure.  If a driver can't pass the
19308 		 * SSID, that needs to be changed.  A zero length SSID
19309 		 * is still a valid SSID (wildcard), so it cannot be
19310 		 * used for this purpose.
19311 		 */
19312 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
19313 			    match->ssid.ssid)) {
19314 			nla_nest_cancel(msg, nl_match);
19315 			goto out;
19316 		}
19317 
19318 		if (match->n_channels) {
19319 			nl_freqs = nla_nest_start_noflag(msg,
19320 							 NL80211_ATTR_SCAN_FREQUENCIES);
19321 			if (!nl_freqs) {
19322 				nla_nest_cancel(msg, nl_match);
19323 				goto out;
19324 			}
19325 
19326 			for (j = 0; j < match->n_channels; j++) {
19327 				if (nla_put_u32(msg, j, match->channels[j])) {
19328 					nla_nest_cancel(msg, nl_freqs);
19329 					nla_nest_cancel(msg, nl_match);
19330 					goto out;
19331 				}
19332 			}
19333 
19334 			nla_nest_end(msg, nl_freqs);
19335 		}
19336 
19337 		nla_nest_end(msg, nl_match);
19338 	}
19339 
19340 out:
19341 	nla_nest_end(msg, nl_results);
19342 	return 0;
19343 }
19344 
19345 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
19346 				   struct cfg80211_wowlan_wakeup *wakeup,
19347 				   gfp_t gfp)
19348 {
19349 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19350 	struct sk_buff *msg;
19351 	void *hdr;
19352 	int size = 200;
19353 
19354 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
19355 
19356 	if (wakeup)
19357 		size += wakeup->packet_present_len;
19358 
19359 	msg = nlmsg_new(size, gfp);
19360 	if (!msg)
19361 		return;
19362 
19363 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
19364 	if (!hdr)
19365 		goto free_msg;
19366 
19367 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19368 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19369 			      NL80211_ATTR_PAD))
19370 		goto free_msg;
19371 
19372 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
19373 					wdev->netdev->ifindex))
19374 		goto free_msg;
19375 
19376 	if (wakeup) {
19377 		struct nlattr *reasons;
19378 
19379 		reasons = nla_nest_start_noflag(msg,
19380 						NL80211_ATTR_WOWLAN_TRIGGERS);
19381 		if (!reasons)
19382 			goto free_msg;
19383 
19384 		if (wakeup->disconnect &&
19385 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
19386 			goto free_msg;
19387 		if (wakeup->magic_pkt &&
19388 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
19389 			goto free_msg;
19390 		if (wakeup->gtk_rekey_failure &&
19391 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
19392 			goto free_msg;
19393 		if (wakeup->eap_identity_req &&
19394 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
19395 			goto free_msg;
19396 		if (wakeup->four_way_handshake &&
19397 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
19398 			goto free_msg;
19399 		if (wakeup->rfkill_release &&
19400 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
19401 			goto free_msg;
19402 
19403 		if (wakeup->pattern_idx >= 0 &&
19404 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
19405 				wakeup->pattern_idx))
19406 			goto free_msg;
19407 
19408 		if (wakeup->tcp_match &&
19409 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
19410 			goto free_msg;
19411 
19412 		if (wakeup->tcp_connlost &&
19413 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
19414 			goto free_msg;
19415 
19416 		if (wakeup->tcp_nomoretokens &&
19417 		    nla_put_flag(msg,
19418 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
19419 			goto free_msg;
19420 
19421 		if (wakeup->packet) {
19422 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
19423 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
19424 
19425 			if (!wakeup->packet_80211) {
19426 				pkt_attr =
19427 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
19428 				len_attr =
19429 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
19430 			}
19431 
19432 			if (wakeup->packet_len &&
19433 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
19434 				goto free_msg;
19435 
19436 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
19437 				    wakeup->packet))
19438 				goto free_msg;
19439 		}
19440 
19441 		if (wakeup->net_detect &&
19442 		    cfg80211_net_detect_results(msg, wakeup))
19443 				goto free_msg;
19444 
19445 		nla_nest_end(msg, reasons);
19446 	}
19447 
19448 	genlmsg_end(msg, hdr);
19449 
19450 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19451 				NL80211_MCGRP_MLME, gfp);
19452 	return;
19453 
19454  free_msg:
19455 	nlmsg_free(msg);
19456 }
19457 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
19458 #endif
19459 
19460 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
19461 				enum nl80211_tdls_operation oper,
19462 				u16 reason_code, gfp_t gfp)
19463 {
19464 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19465 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19466 	struct sk_buff *msg;
19467 	void *hdr;
19468 
19469 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
19470 					 reason_code);
19471 
19472 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19473 	if (!msg)
19474 		return;
19475 
19476 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
19477 	if (!hdr) {
19478 		nlmsg_free(msg);
19479 		return;
19480 	}
19481 
19482 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19483 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19484 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
19485 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
19486 	    (reason_code > 0 &&
19487 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
19488 		goto nla_put_failure;
19489 
19490 	genlmsg_end(msg, hdr);
19491 
19492 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19493 				NL80211_MCGRP_MLME, gfp);
19494 	return;
19495 
19496  nla_put_failure:
19497 	nlmsg_free(msg);
19498 }
19499 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
19500 
19501 static int nl80211_netlink_notify(struct notifier_block * nb,
19502 				  unsigned long state,
19503 				  void *_notify)
19504 {
19505 	struct netlink_notify *notify = _notify;
19506 	struct cfg80211_registered_device *rdev;
19507 	struct wireless_dev *wdev;
19508 	struct cfg80211_beacon_registration *reg, *tmp;
19509 
19510 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
19511 		return NOTIFY_DONE;
19512 
19513 	rcu_read_lock();
19514 
19515 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
19516 		struct cfg80211_sched_scan_request *sched_scan_req;
19517 
19518 		list_for_each_entry_rcu(sched_scan_req,
19519 					&rdev->sched_scan_req_list,
19520 					list) {
19521 			if (sched_scan_req->owner_nlportid == notify->portid) {
19522 				sched_scan_req->nl_owner_dead = true;
19523 				schedule_work(&rdev->sched_scan_stop_wk);
19524 			}
19525 		}
19526 
19527 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
19528 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
19529 
19530 			if (wdev->owner_nlportid == notify->portid) {
19531 				wdev->nl_owner_dead = true;
19532 				schedule_work(&rdev->destroy_work);
19533 			} else if (wdev->conn_owner_nlportid == notify->portid) {
19534 				schedule_work(&wdev->disconnect_wk);
19535 			}
19536 
19537 			cfg80211_release_pmsr(wdev, notify->portid);
19538 		}
19539 
19540 		spin_lock_bh(&rdev->beacon_registrations_lock);
19541 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
19542 					 list) {
19543 			if (reg->nlportid == notify->portid) {
19544 				list_del(&reg->list);
19545 				kfree(reg);
19546 				break;
19547 			}
19548 		}
19549 		spin_unlock_bh(&rdev->beacon_registrations_lock);
19550 	}
19551 
19552 	rcu_read_unlock();
19553 
19554 	/*
19555 	 * It is possible that the user space process that is controlling the
19556 	 * indoor setting disappeared, so notify the regulatory core.
19557 	 */
19558 	regulatory_netlink_notify(notify->portid);
19559 	return NOTIFY_OK;
19560 }
19561 
19562 static struct notifier_block nl80211_netlink_notifier = {
19563 	.notifier_call = nl80211_netlink_notify,
19564 };
19565 
19566 void cfg80211_ft_event(struct net_device *netdev,
19567 		       struct cfg80211_ft_event_params *ft_event)
19568 {
19569 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
19570 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19571 	struct sk_buff *msg;
19572 	void *hdr;
19573 
19574 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
19575 
19576 	if (!ft_event->target_ap)
19577 		return;
19578 
19579 	msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
19580 			GFP_KERNEL);
19581 	if (!msg)
19582 		return;
19583 
19584 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
19585 	if (!hdr)
19586 		goto out;
19587 
19588 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19589 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19590 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
19591 		goto out;
19592 
19593 	if (ft_event->ies &&
19594 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
19595 		goto out;
19596 	if (ft_event->ric_ies &&
19597 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
19598 		    ft_event->ric_ies))
19599 		goto out;
19600 
19601 	genlmsg_end(msg, hdr);
19602 
19603 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19604 				NL80211_MCGRP_MLME, GFP_KERNEL);
19605 	return;
19606  out:
19607 	nlmsg_free(msg);
19608 }
19609 EXPORT_SYMBOL(cfg80211_ft_event);
19610 
19611 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
19612 {
19613 	struct cfg80211_registered_device *rdev;
19614 	struct sk_buff *msg;
19615 	void *hdr;
19616 	u32 nlportid;
19617 
19618 	rdev = wiphy_to_rdev(wdev->wiphy);
19619 	if (!rdev->crit_proto_nlportid)
19620 		return;
19621 
19622 	nlportid = rdev->crit_proto_nlportid;
19623 	rdev->crit_proto_nlportid = 0;
19624 
19625 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19626 	if (!msg)
19627 		return;
19628 
19629 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
19630 	if (!hdr)
19631 		goto nla_put_failure;
19632 
19633 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19634 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19635 			      NL80211_ATTR_PAD))
19636 		goto nla_put_failure;
19637 
19638 	genlmsg_end(msg, hdr);
19639 
19640 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
19641 	return;
19642 
19643  nla_put_failure:
19644 	nlmsg_free(msg);
19645 }
19646 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
19647 
19648 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
19649 {
19650 	struct wiphy *wiphy = wdev->wiphy;
19651 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19652 	struct sk_buff *msg;
19653 	void *hdr;
19654 
19655 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
19656 	if (!msg)
19657 		return;
19658 
19659 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
19660 	if (!hdr)
19661 		goto out;
19662 
19663 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19664 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
19665 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19666 			      NL80211_ATTR_PAD))
19667 		goto out;
19668 
19669 	genlmsg_end(msg, hdr);
19670 
19671 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
19672 				NL80211_MCGRP_MLME, GFP_KERNEL);
19673 	return;
19674  out:
19675 	nlmsg_free(msg);
19676 }
19677 
19678 int cfg80211_external_auth_request(struct net_device *dev,
19679 				   struct cfg80211_external_auth_params *params,
19680 				   gfp_t gfp)
19681 {
19682 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19683 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19684 	struct sk_buff *msg;
19685 	void *hdr;
19686 
19687 	if (!wdev->conn_owner_nlportid)
19688 		return -EINVAL;
19689 
19690 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19691 	if (!msg)
19692 		return -ENOMEM;
19693 
19694 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
19695 	if (!hdr)
19696 		goto nla_put_failure;
19697 
19698 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19699 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19700 	    nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
19701 	    nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
19702 			params->action) ||
19703 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
19704 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
19705 		    params->ssid.ssid))
19706 		goto nla_put_failure;
19707 
19708 	genlmsg_end(msg, hdr);
19709 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
19710 			wdev->conn_owner_nlportid);
19711 	return 0;
19712 
19713  nla_put_failure:
19714 	nlmsg_free(msg);
19715 	return -ENOBUFS;
19716 }
19717 EXPORT_SYMBOL(cfg80211_external_auth_request);
19718 
19719 void cfg80211_update_owe_info_event(struct net_device *netdev,
19720 				    struct cfg80211_update_owe_info *owe_info,
19721 				    gfp_t gfp)
19722 {
19723 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
19724 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19725 	struct sk_buff *msg;
19726 	void *hdr;
19727 
19728 	trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
19729 
19730 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19731 	if (!msg)
19732 		return;
19733 
19734 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
19735 	if (!hdr)
19736 		goto nla_put_failure;
19737 
19738 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19739 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19740 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
19741 		goto nla_put_failure;
19742 
19743 	if (!owe_info->ie_len ||
19744 	    nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
19745 		goto nla_put_failure;
19746 
19747 	genlmsg_end(msg, hdr);
19748 
19749 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19750 				NL80211_MCGRP_MLME, gfp);
19751 	return;
19752 
19753 nla_put_failure:
19754 	genlmsg_cancel(msg, hdr);
19755 	nlmsg_free(msg);
19756 }
19757 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
19758 
19759 /* initialisation/exit functions */
19760 
19761 int __init nl80211_init(void)
19762 {
19763 	int err;
19764 
19765 	err = genl_register_family(&nl80211_fam);
19766 	if (err)
19767 		return err;
19768 
19769 	err = netlink_register_notifier(&nl80211_netlink_notifier);
19770 	if (err)
19771 		goto err_out;
19772 
19773 	return 0;
19774  err_out:
19775 	genl_unregister_family(&nl80211_fam);
19776 	return err;
19777 }
19778 
19779 void nl80211_exit(void)
19780 {
19781 	netlink_unregister_notifier(&nl80211_netlink_notifier);
19782 	genl_unregister_family(&nl80211_fam);
19783 }
19784