xref: /linux/net/wireless/nl80211.c (revision 8137a49e1567726eb10fcf55ad141ac19804ca6b)
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-2021 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 /* policy for the attributes */
289 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR];
290 
291 static const struct nla_policy
292 nl80211_ftm_responder_policy[NL80211_FTM_RESP_ATTR_MAX + 1] = {
293 	[NL80211_FTM_RESP_ATTR_ENABLED] = { .type = NLA_FLAG, },
294 	[NL80211_FTM_RESP_ATTR_LCI] = { .type = NLA_BINARY,
295 					.len = U8_MAX },
296 	[NL80211_FTM_RESP_ATTR_CIVICLOC] = { .type = NLA_BINARY,
297 					     .len = U8_MAX },
298 };
299 
300 static const struct nla_policy
301 nl80211_pmsr_ftm_req_attr_policy[NL80211_PMSR_FTM_REQ_ATTR_MAX + 1] = {
302 	[NL80211_PMSR_FTM_REQ_ATTR_ASAP] = { .type = NLA_FLAG },
303 	[NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE] = { .type = NLA_U32 },
304 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP] =
305 		NLA_POLICY_MAX(NLA_U8, 15),
306 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD] = { .type = NLA_U16 },
307 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION] =
308 		NLA_POLICY_MAX(NLA_U8, 15),
309 	[NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST] =
310 		NLA_POLICY_MAX(NLA_U8, 31),
311 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES] = { .type = NLA_U8 },
312 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI] = { .type = NLA_FLAG },
313 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC] = { .type = NLA_FLAG },
314 	[NL80211_PMSR_FTM_REQ_ATTR_TRIGGER_BASED] = { .type = NLA_FLAG },
315 	[NL80211_PMSR_FTM_REQ_ATTR_NON_TRIGGER_BASED] = { .type = NLA_FLAG },
316 	[NL80211_PMSR_FTM_REQ_ATTR_LMR_FEEDBACK] = { .type = NLA_FLAG },
317 };
318 
319 static const struct nla_policy
320 nl80211_pmsr_req_data_policy[NL80211_PMSR_TYPE_MAX + 1] = {
321 	[NL80211_PMSR_TYPE_FTM] =
322 		NLA_POLICY_NESTED(nl80211_pmsr_ftm_req_attr_policy),
323 };
324 
325 static const struct nla_policy
326 nl80211_pmsr_req_attr_policy[NL80211_PMSR_REQ_ATTR_MAX + 1] = {
327 	[NL80211_PMSR_REQ_ATTR_DATA] =
328 		NLA_POLICY_NESTED(nl80211_pmsr_req_data_policy),
329 	[NL80211_PMSR_REQ_ATTR_GET_AP_TSF] = { .type = NLA_FLAG },
330 };
331 
332 static const struct nla_policy
333 nl80211_pmsr_peer_attr_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
334 	[NL80211_PMSR_PEER_ATTR_ADDR] = NLA_POLICY_ETH_ADDR,
335 	[NL80211_PMSR_PEER_ATTR_CHAN] = NLA_POLICY_NESTED(nl80211_policy),
336 	[NL80211_PMSR_PEER_ATTR_REQ] =
337 		NLA_POLICY_NESTED(nl80211_pmsr_req_attr_policy),
338 	[NL80211_PMSR_PEER_ATTR_RESP] = { .type = NLA_REJECT },
339 };
340 
341 static const struct nla_policy
342 nl80211_pmsr_attr_policy[NL80211_PMSR_ATTR_MAX + 1] = {
343 	[NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_REJECT },
344 	[NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_REJECT },
345 	[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
346 	[NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
347 	[NL80211_PMSR_ATTR_PEERS] =
348 		NLA_POLICY_NESTED_ARRAY(nl80211_pmsr_peer_attr_policy),
349 };
350 
351 static const struct nla_policy
352 he_obss_pd_policy[NL80211_HE_OBSS_PD_ATTR_MAX + 1] = {
353 	[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] =
354 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
355 	[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET] =
356 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
357 	[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET] =
358 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
359 	[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP] =
360 		NLA_POLICY_EXACT_LEN(8),
361 	[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP] =
362 		NLA_POLICY_EXACT_LEN(8),
363 	[NL80211_HE_OBSS_PD_ATTR_SR_CTRL] = { .type = NLA_U8 },
364 };
365 
366 static const struct nla_policy
367 he_bss_color_policy[NL80211_HE_BSS_COLOR_ATTR_MAX + 1] = {
368 	[NL80211_HE_BSS_COLOR_ATTR_COLOR] = NLA_POLICY_RANGE(NLA_U8, 1, 63),
369 	[NL80211_HE_BSS_COLOR_ATTR_DISABLED] = { .type = NLA_FLAG },
370 	[NL80211_HE_BSS_COLOR_ATTR_PARTIAL] = { .type = NLA_FLAG },
371 };
372 
373 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
374 	[NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
375 				    .len = NL80211_MAX_SUPP_RATES },
376 	[NL80211_TXRATE_HT] = { .type = NLA_BINARY,
377 				.len = NL80211_MAX_SUPP_HT_RATES },
378 	[NL80211_TXRATE_VHT] = NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_txrate_vht)),
379 	[NL80211_TXRATE_GI] = { .type = NLA_U8 },
380 	[NL80211_TXRATE_HE] = NLA_POLICY_EXACT_LEN(sizeof(struct nl80211_txrate_he)),
381 	[NL80211_TXRATE_HE_GI] =  NLA_POLICY_RANGE(NLA_U8,
382 						   NL80211_RATE_INFO_HE_GI_0_8,
383 						   NL80211_RATE_INFO_HE_GI_3_2),
384 	[NL80211_TXRATE_HE_LTF] = NLA_POLICY_RANGE(NLA_U8,
385 						   NL80211_RATE_INFO_HE_1XLTF,
386 						   NL80211_RATE_INFO_HE_4XLTF),
387 };
388 
389 static const struct nla_policy
390 nl80211_tid_config_attr_policy[NL80211_TID_CONFIG_ATTR_MAX + 1] = {
391 	[NL80211_TID_CONFIG_ATTR_VIF_SUPP] = { .type = NLA_U64 },
392 	[NL80211_TID_CONFIG_ATTR_PEER_SUPP] = { .type = NLA_U64 },
393 	[NL80211_TID_CONFIG_ATTR_OVERRIDE] = { .type = NLA_FLAG },
394 	[NL80211_TID_CONFIG_ATTR_TIDS] = NLA_POLICY_RANGE(NLA_U16, 1, 0xff),
395 	[NL80211_TID_CONFIG_ATTR_NOACK] =
396 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
397 	[NL80211_TID_CONFIG_ATTR_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
398 	[NL80211_TID_CONFIG_ATTR_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
399 	[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL] =
400 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
401 	[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL] =
402 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
403 	[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL] =
404 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
405 	[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE] =
406 			NLA_POLICY_MAX(NLA_U8, NL80211_TX_RATE_FIXED),
407 	[NL80211_TID_CONFIG_ATTR_TX_RATE] =
408 			NLA_POLICY_NESTED(nl80211_txattr_policy),
409 };
410 
411 static const struct nla_policy
412 nl80211_fils_discovery_policy[NL80211_FILS_DISCOVERY_ATTR_MAX + 1] = {
413 	[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] = NLA_POLICY_MAX(NLA_U32, 10000),
414 	[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] = NLA_POLICY_MAX(NLA_U32, 10000),
415 	[NL80211_FILS_DISCOVERY_ATTR_TMPL] =
416 			NLA_POLICY_RANGE(NLA_BINARY,
417 					 NL80211_FILS_DISCOVERY_TMPL_MIN_LEN,
418 					 IEEE80211_MAX_DATA_LEN),
419 };
420 
421 static const struct nla_policy
422 nl80211_unsol_bcast_probe_resp_policy[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1] = {
423 	[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] = NLA_POLICY_MAX(NLA_U32, 20),
424 	[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL] = { .type = NLA_BINARY,
425 						       .len = IEEE80211_MAX_DATA_LEN }
426 };
427 
428 static const struct nla_policy
429 sar_specs_policy[NL80211_SAR_ATTR_SPECS_MAX + 1] = {
430 	[NL80211_SAR_ATTR_SPECS_POWER] = { .type = NLA_S32 },
431 	[NL80211_SAR_ATTR_SPECS_RANGE_INDEX] = {.type = NLA_U32 },
432 };
433 
434 static const struct nla_policy
435 sar_policy[NL80211_SAR_ATTR_MAX + 1] = {
436 	[NL80211_SAR_ATTR_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_SAR_TYPE),
437 	[NL80211_SAR_ATTR_SPECS] = NLA_POLICY_NESTED_ARRAY(sar_specs_policy),
438 };
439 
440 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
441 	[0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
442 	[NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
443 	[NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
444 				      .len = 20-1 },
445 	[NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
446 
447 	[NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
448 	[NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
449 	[NL80211_ATTR_WIPHY_EDMG_CHANNELS] = NLA_POLICY_RANGE(NLA_U8,
450 						NL80211_EDMG_CHANNELS_MIN,
451 						NL80211_EDMG_CHANNELS_MAX),
452 	[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG] = NLA_POLICY_RANGE(NLA_U8,
453 						NL80211_EDMG_BW_CONFIG_MIN,
454 						NL80211_EDMG_BW_CONFIG_MAX),
455 
456 	[NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
457 	[NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
458 	[NL80211_ATTR_CENTER_FREQ1_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
459 	[NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
460 
461 	[NL80211_ATTR_WIPHY_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
462 	[NL80211_ATTR_WIPHY_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
463 	[NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
464 	[NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
465 	[NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
466 	[NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
467 
468 	[NL80211_ATTR_IFTYPE] = NLA_POLICY_MAX(NLA_U32, NL80211_IFTYPE_MAX),
469 	[NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
470 	[NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
471 
472 	[NL80211_ATTR_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
473 	[NL80211_ATTR_PREV_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
474 
475 	[NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
476 	[NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
477 				    .len = WLAN_MAX_KEY_LEN },
478 	[NL80211_ATTR_KEY_IDX] = NLA_POLICY_MAX(NLA_U8, 7),
479 	[NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
480 	[NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
481 	[NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
482 	[NL80211_ATTR_KEY_TYPE] =
483 		NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES),
484 
485 	[NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
486 	[NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
487 	[NL80211_ATTR_BEACON_HEAD] =
488 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_beacon_head,
489 				       IEEE80211_MAX_DATA_LEN),
490 	[NL80211_ATTR_BEACON_TAIL] =
491 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
492 				       IEEE80211_MAX_DATA_LEN),
493 	[NL80211_ATTR_STA_AID] =
494 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
495 	[NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
496 	[NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
497 	[NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
498 					       .len = NL80211_MAX_SUPP_RATES },
499 	[NL80211_ATTR_STA_PLINK_ACTION] =
500 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_ACTIONS - 1),
501 	[NL80211_ATTR_STA_TX_POWER_SETTING] =
502 		NLA_POLICY_RANGE(NLA_U8,
503 				 NL80211_TX_POWER_AUTOMATIC,
504 				 NL80211_TX_POWER_FIXED),
505 	[NL80211_ATTR_STA_TX_POWER] = { .type = NLA_S16 },
506 	[NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
507 	[NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
508 	[NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
509 				   .len = IEEE80211_MAX_MESH_ID_LEN },
510 	[NL80211_ATTR_MPATH_NEXT_HOP] = NLA_POLICY_ETH_ADDR_COMPAT,
511 
512 	[NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
513 	[NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
514 
515 	[NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
516 	[NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
517 	[NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
518 	[NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
519 					   .len = NL80211_MAX_SUPP_RATES },
520 	[NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
521 
522 	[NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
523 	[NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
524 
525 	[NL80211_ATTR_HT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_HT_CAPABILITY_LEN),
526 
527 	[NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
528 	[NL80211_ATTR_IE] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
529 						   validate_ie_attr,
530 						   IEEE80211_MAX_DATA_LEN),
531 	[NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
532 	[NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
533 
534 	[NL80211_ATTR_SSID] = { .type = NLA_BINARY,
535 				.len = IEEE80211_MAX_SSID_LEN },
536 	[NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
537 	[NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
538 	[NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
539 	[NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
540 	[NL80211_ATTR_USE_MFP] = NLA_POLICY_RANGE(NLA_U32,
541 						  NL80211_MFP_NO,
542 						  NL80211_MFP_OPTIONAL),
543 	[NL80211_ATTR_STA_FLAGS2] = {
544 		.len = sizeof(struct nl80211_sta_flag_update),
545 	},
546 	[NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
547 	[NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
548 	[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
549 	[NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
550 	[NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
551 	[NL80211_ATTR_STATUS_CODE] = { .type = NLA_U16 },
552 	[NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
553 	[NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
554 	[NL80211_ATTR_PID] = { .type = NLA_U32 },
555 	[NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
556 	[NL80211_ATTR_PMKID] = NLA_POLICY_EXACT_LEN_WARN(WLAN_PMKID_LEN),
557 	[NL80211_ATTR_DURATION] = { .type = NLA_U32 },
558 	[NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
559 	[NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
560 	[NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
561 				 .len = IEEE80211_MAX_DATA_LEN },
562 	[NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
563 	[NL80211_ATTR_PS_STATE] = NLA_POLICY_RANGE(NLA_U32,
564 						   NL80211_PS_DISABLED,
565 						   NL80211_PS_ENABLED),
566 	[NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
567 	[NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
568 	[NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
569 	[NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
570 	[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
571 	[NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
572 	[NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
573 	[NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
574 	[NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
575 	[NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
576 	[NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
577 	[NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
578 	[NL80211_ATTR_STA_PLINK_STATE] =
579 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_STATES - 1),
580 	[NL80211_ATTR_MEASUREMENT_DURATION] = { .type = NLA_U16 },
581 	[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY] = { .type = NLA_FLAG },
582 	[NL80211_ATTR_MESH_PEER_AID] =
583 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
584 	[NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
585 	[NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
586 	[NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
587 	[NL80211_ATTR_HIDDEN_SSID] =
588 		NLA_POLICY_RANGE(NLA_U32,
589 				 NL80211_HIDDEN_SSID_NOT_IN_USE,
590 				 NL80211_HIDDEN_SSID_ZERO_CONTENTS),
591 	[NL80211_ATTR_IE_PROBE_RESP] =
592 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
593 				       IEEE80211_MAX_DATA_LEN),
594 	[NL80211_ATTR_IE_ASSOC_RESP] =
595 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
596 				       IEEE80211_MAX_DATA_LEN),
597 	[NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
598 	[NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
599 	[NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
600 	[NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
601 	[NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
602 	[NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
603 	[NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
604 	[NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
605 	[NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
606 	[NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
607 	[NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
608 				      .len = IEEE80211_MAX_DATA_LEN },
609 	[NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
610 	[NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
611 	[NL80211_ATTR_HT_CAPABILITY_MASK] = {
612 		.len = NL80211_HT_CAPABILITY_LEN
613 	},
614 	[NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
615 	[NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
616 	[NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
617 	[NL80211_ATTR_WDEV] = { .type = NLA_U64 },
618 	[NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
619 
620 	/* need to include at least Auth Transaction and Status Code */
621 	[NL80211_ATTR_AUTH_DATA] = NLA_POLICY_MIN_LEN(4),
622 
623 	[NL80211_ATTR_VHT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_VHT_CAPABILITY_LEN),
624 	[NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
625 	[NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
626 	[NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
627 	[NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
628 		NLA_POLICY_RANGE(NLA_U32,
629 				 NL80211_MESH_POWER_UNKNOWN + 1,
630 				 NL80211_MESH_POWER_MAX),
631 	[NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
632 	[NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
633 	[NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
634 	[NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
635 	[NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
636 	[NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
637 	[NL80211_ATTR_VHT_CAPABILITY_MASK] = {
638 		.len = NL80211_VHT_CAPABILITY_LEN,
639 	},
640 	[NL80211_ATTR_MDID] = { .type = NLA_U16 },
641 	[NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
642 				  .len = IEEE80211_MAX_DATA_LEN },
643 	[NL80211_ATTR_CRIT_PROT_ID] = { .type = NLA_U16 },
644 	[NL80211_ATTR_MAX_CRIT_PROT_DURATION] =
645 		NLA_POLICY_MAX(NLA_U16, NL80211_CRIT_PROTO_MAX_DURATION),
646 	[NL80211_ATTR_PEER_AID] =
647 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
648 	[NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
649 	[NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
650 	[NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
651 	[NL80211_ATTR_CNTDWN_OFFS_BEACON] = { .type = NLA_BINARY },
652 	[NL80211_ATTR_CNTDWN_OFFS_PRESP] = { .type = NLA_BINARY },
653 	[NL80211_ATTR_STA_SUPPORTED_CHANNELS] = NLA_POLICY_MIN_LEN(2),
654 	/*
655 	 * The value of the Length field of the Supported Operating
656 	 * Classes element is between 2 and 253.
657 	 */
658 	[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] =
659 		NLA_POLICY_RANGE(NLA_BINARY, 2, 253),
660 	[NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
661 	[NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
662 	[NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
663 	[NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
664 	[NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
665 	[NL80211_ATTR_QOS_MAP] = NLA_POLICY_RANGE(NLA_BINARY,
666 						  IEEE80211_QOS_MAP_LEN_MIN,
667 						  IEEE80211_QOS_MAP_LEN_MAX),
668 	[NL80211_ATTR_MAC_HINT] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
669 	[NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
670 	[NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
671 	[NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
672 	[NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
673 	[NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
674 	[NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
675 	[NL80211_ATTR_USER_PRIO] =
676 		NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
677 	[NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
678 	[NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
679 	[NL80211_ATTR_OPER_CLASS] = { .type = NLA_U8 },
680 	[NL80211_ATTR_MAC_MASK] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
681 	[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
682 	[NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
683 	[NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
684 	[NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
685 	[NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
686 	[NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
687 	[NL80211_ATTR_STA_SUPPORT_P2P_PS] =
688 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
689 	[NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
690 		.len = VHT_MUMIMO_GROUPS_DATA_LEN
691 	},
692 	[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
693 	[NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
694 	[NL80211_ATTR_BANDS] = { .type = NLA_U32 },
695 	[NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
696 	[NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
697 				    .len = FILS_MAX_KEK_LEN },
698 	[NL80211_ATTR_FILS_NONCES] = NLA_POLICY_EXACT_LEN_WARN(2 * FILS_NONCE_LEN),
699 	[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
700 	[NL80211_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
701 	[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
702 	[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
703 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
704 	},
705 	[NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
706 	[NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
707 					     .len = FILS_ERP_MAX_USERNAME_LEN },
708 	[NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
709 					  .len = FILS_ERP_MAX_REALM_LEN },
710 	[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
711 	[NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
712 					.len = FILS_ERP_MAX_RRK_LEN },
713 	[NL80211_ATTR_FILS_CACHE_ID] = NLA_POLICY_EXACT_LEN_WARN(2),
714 	[NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
715 	[NL80211_ATTR_PMKR0_NAME] = NLA_POLICY_EXACT_LEN(WLAN_PMK_NAME_LEN),
716 	[NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
717 	[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
718 
719 	[NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
720 	[NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
721 	[NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
722 	[NL80211_ATTR_HE_CAPABILITY] =
723 		NLA_POLICY_RANGE(NLA_BINARY,
724 				 NL80211_HE_MIN_CAPABILITY_LEN,
725 				 NL80211_HE_MAX_CAPABILITY_LEN),
726 	[NL80211_ATTR_FTM_RESPONDER] =
727 		NLA_POLICY_NESTED(nl80211_ftm_responder_policy),
728 	[NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
729 	[NL80211_ATTR_PEER_MEASUREMENTS] =
730 		NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
731 	[NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
732 	[NL80211_ATTR_SAE_PASSWORD] = { .type = NLA_BINARY,
733 					.len = SAE_PASSWORD_MAX_LEN },
734 	[NL80211_ATTR_TWT_RESPONDER] = { .type = NLA_FLAG },
735 	[NL80211_ATTR_HE_OBSS_PD] = NLA_POLICY_NESTED(he_obss_pd_policy),
736 	[NL80211_ATTR_VLAN_ID] = NLA_POLICY_RANGE(NLA_U16, 1, VLAN_N_VID - 2),
737 	[NL80211_ATTR_HE_BSS_COLOR] = NLA_POLICY_NESTED(he_bss_color_policy),
738 	[NL80211_ATTR_TID_CONFIG] =
739 		NLA_POLICY_NESTED_ARRAY(nl80211_tid_config_attr_policy),
740 	[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH] = { .type = NLA_FLAG },
741 	[NL80211_ATTR_PMK_LIFETIME] = NLA_POLICY_MIN(NLA_U32, 1),
742 	[NL80211_ATTR_PMK_REAUTH_THRESHOLD] = NLA_POLICY_RANGE(NLA_U8, 1, 100),
743 	[NL80211_ATTR_RECEIVE_MULTICAST] = { .type = NLA_FLAG },
744 	[NL80211_ATTR_WIPHY_FREQ_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
745 	[NL80211_ATTR_SCAN_FREQ_KHZ] = { .type = NLA_NESTED },
746 	[NL80211_ATTR_HE_6GHZ_CAPABILITY] =
747 		NLA_POLICY_EXACT_LEN(sizeof(struct ieee80211_he_6ghz_capa)),
748 	[NL80211_ATTR_FILS_DISCOVERY] =
749 		NLA_POLICY_NESTED(nl80211_fils_discovery_policy),
750 	[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP] =
751 		NLA_POLICY_NESTED(nl80211_unsol_bcast_probe_resp_policy),
752 	[NL80211_ATTR_S1G_CAPABILITY] =
753 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
754 	[NL80211_ATTR_S1G_CAPABILITY_MASK] =
755 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
756 	[NL80211_ATTR_SAE_PWE] =
757 		NLA_POLICY_RANGE(NLA_U8, NL80211_SAE_PWE_HUNT_AND_PECK,
758 				 NL80211_SAE_PWE_BOTH),
759 	[NL80211_ATTR_RECONNECT_REQUESTED] = { .type = NLA_REJECT },
760 	[NL80211_ATTR_SAR_SPEC] = NLA_POLICY_NESTED(sar_policy),
761 	[NL80211_ATTR_DISABLE_HE] = { .type = NLA_FLAG },
762 };
763 
764 /* policy for the key attributes */
765 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
766 	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
767 	[NL80211_KEY_IDX] = { .type = NLA_U8 },
768 	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
769 	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
770 	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
771 	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
772 	[NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
773 	[NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
774 	[NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
775 };
776 
777 /* policy for the key default flags */
778 static const struct nla_policy
779 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
780 	[NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
781 	[NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
782 };
783 
784 #ifdef CONFIG_PM
785 /* policy for WoWLAN attributes */
786 static const struct nla_policy
787 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
788 	[NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
789 	[NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
790 	[NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
791 	[NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
792 	[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
793 	[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
794 	[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
795 	[NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
796 	[NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
797 	[NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
798 };
799 
800 static const struct nla_policy
801 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
802 	[NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
803 	[NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
804 	[NL80211_WOWLAN_TCP_DST_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
805 	[NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
806 	[NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
807 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
808 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
809 		.len = sizeof(struct nl80211_wowlan_tcp_data_seq)
810 	},
811 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
812 		.len = sizeof(struct nl80211_wowlan_tcp_data_token)
813 	},
814 	[NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
815 	[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
816 	[NL80211_WOWLAN_TCP_WAKE_MASK] = NLA_POLICY_MIN_LEN(1),
817 };
818 #endif /* CONFIG_PM */
819 
820 /* policy for coalesce rule attributes */
821 static const struct nla_policy
822 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
823 	[NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
824 	[NL80211_ATTR_COALESCE_RULE_CONDITION] =
825 		NLA_POLICY_RANGE(NLA_U32,
826 				 NL80211_COALESCE_CONDITION_MATCH,
827 				 NL80211_COALESCE_CONDITION_NO_MATCH),
828 	[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
829 };
830 
831 /* policy for GTK rekey offload attributes */
832 static const struct nla_policy
833 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
834 	[NL80211_REKEY_DATA_KEK] = {
835 		.type = NLA_BINARY,
836 		.len = NL80211_KEK_EXT_LEN
837 	},
838 	[NL80211_REKEY_DATA_KCK] = {
839 		.type = NLA_BINARY,
840 		.len = NL80211_KCK_EXT_LEN
841 	},
842 	[NL80211_REKEY_DATA_REPLAY_CTR] = NLA_POLICY_EXACT_LEN(NL80211_REPLAY_CTR_LEN),
843 	[NL80211_REKEY_DATA_AKM] = { .type = NLA_U32 },
844 };
845 
846 static const struct nla_policy
847 nl80211_match_band_rssi_policy[NUM_NL80211_BANDS] = {
848 	[NL80211_BAND_2GHZ] = { .type = NLA_S32 },
849 	[NL80211_BAND_5GHZ] = { .type = NLA_S32 },
850 	[NL80211_BAND_6GHZ] = { .type = NLA_S32 },
851 	[NL80211_BAND_60GHZ] = { .type = NLA_S32 },
852 };
853 
854 static const struct nla_policy
855 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
856 	[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
857 						 .len = IEEE80211_MAX_SSID_LEN },
858 	[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
859 	[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
860 	[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI] =
861 		NLA_POLICY_NESTED(nl80211_match_band_rssi_policy),
862 };
863 
864 static const struct nla_policy
865 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
866 	[NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
867 	[NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
868 };
869 
870 static const struct nla_policy
871 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
872 	[NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
873 	[NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
874 	[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
875 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
876 	},
877 };
878 
879 /* policy for NAN function attributes */
880 static const struct nla_policy
881 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
882 	[NL80211_NAN_FUNC_TYPE] =
883 		NLA_POLICY_MAX(NLA_U8, NL80211_NAN_FUNC_MAX_TYPE),
884 	[NL80211_NAN_FUNC_SERVICE_ID] = {
885 				    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
886 	[NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
887 	[NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
888 	[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
889 	[NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
890 	[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
891 	[NL80211_NAN_FUNC_FOLLOW_UP_DEST] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
892 	[NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
893 	[NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
894 	[NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
895 			.len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
896 	[NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
897 	[NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
898 	[NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
899 	[NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
900 	[NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
901 };
902 
903 /* policy for Service Response Filter attributes */
904 static const struct nla_policy
905 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
906 	[NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
907 	[NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
908 				 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
909 	[NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
910 	[NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
911 };
912 
913 /* policy for packet pattern attributes */
914 static const struct nla_policy
915 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
916 	[NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
917 	[NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
918 	[NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
919 };
920 
921 int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
922 			      struct cfg80211_registered_device **rdev,
923 			      struct wireless_dev **wdev)
924 {
925 	int err;
926 
927 	if (!cb->args[0]) {
928 		struct nlattr **attrbuf;
929 
930 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
931 				  GFP_KERNEL);
932 		if (!attrbuf)
933 			return -ENOMEM;
934 
935 		err = nlmsg_parse_deprecated(cb->nlh,
936 					     GENL_HDRLEN + nl80211_fam.hdrsize,
937 					     attrbuf, nl80211_fam.maxattr,
938 					     nl80211_policy, NULL);
939 		if (err) {
940 			kfree(attrbuf);
941 			return err;
942 		}
943 
944 		rtnl_lock();
945 		*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(cb->skb->sk),
946 						   attrbuf);
947 		kfree(attrbuf);
948 		if (IS_ERR(*wdev)) {
949 			rtnl_unlock();
950 			return PTR_ERR(*wdev);
951 		}
952 		*rdev = wiphy_to_rdev((*wdev)->wiphy);
953 		mutex_lock(&(*rdev)->wiphy.mtx);
954 		rtnl_unlock();
955 		/* 0 is the first index - add 1 to parse only once */
956 		cb->args[0] = (*rdev)->wiphy_idx + 1;
957 		cb->args[1] = (*wdev)->identifier;
958 	} else {
959 		/* subtract the 1 again here */
960 		struct wiphy *wiphy;
961 		struct wireless_dev *tmp;
962 
963 		rtnl_lock();
964 		wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
965 		if (!wiphy) {
966 			rtnl_unlock();
967 			return -ENODEV;
968 		}
969 		*rdev = wiphy_to_rdev(wiphy);
970 		*wdev = NULL;
971 
972 		list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
973 			if (tmp->identifier == cb->args[1]) {
974 				*wdev = tmp;
975 				break;
976 			}
977 		}
978 
979 		if (!*wdev) {
980 			rtnl_unlock();
981 			return -ENODEV;
982 		}
983 		mutex_lock(&(*rdev)->wiphy.mtx);
984 		rtnl_unlock();
985 	}
986 
987 	return 0;
988 }
989 
990 /* message building helper */
991 void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
992 		     int flags, u8 cmd)
993 {
994 	/* since there is no private header just add the generic one */
995 	return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
996 }
997 
998 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
999 				     const struct ieee80211_reg_rule *rule)
1000 {
1001 	int j;
1002 	struct nlattr *nl_wmm_rules =
1003 		nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
1004 
1005 	if (!nl_wmm_rules)
1006 		goto nla_put_failure;
1007 
1008 	for (j = 0; j < IEEE80211_NUM_ACS; j++) {
1009 		struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
1010 
1011 		if (!nl_wmm_rule)
1012 			goto nla_put_failure;
1013 
1014 		if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
1015 				rule->wmm_rule.client[j].cw_min) ||
1016 		    nla_put_u16(msg, NL80211_WMMR_CW_MAX,
1017 				rule->wmm_rule.client[j].cw_max) ||
1018 		    nla_put_u8(msg, NL80211_WMMR_AIFSN,
1019 			       rule->wmm_rule.client[j].aifsn) ||
1020 		    nla_put_u16(msg, NL80211_WMMR_TXOP,
1021 			        rule->wmm_rule.client[j].cot))
1022 			goto nla_put_failure;
1023 
1024 		nla_nest_end(msg, nl_wmm_rule);
1025 	}
1026 	nla_nest_end(msg, nl_wmm_rules);
1027 
1028 	return 0;
1029 
1030 nla_put_failure:
1031 	return -ENOBUFS;
1032 }
1033 
1034 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
1035 				   struct ieee80211_channel *chan,
1036 				   bool large)
1037 {
1038 	/* Some channels must be completely excluded from the
1039 	 * list to protect old user-space tools from breaking
1040 	 */
1041 	if (!large && chan->flags &
1042 	    (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
1043 		return 0;
1044 	if (!large && chan->freq_offset)
1045 		return 0;
1046 
1047 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
1048 			chan->center_freq))
1049 		goto nla_put_failure;
1050 
1051 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_OFFSET, chan->freq_offset))
1052 		goto nla_put_failure;
1053 
1054 	if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
1055 	    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
1056 		goto nla_put_failure;
1057 	if (chan->flags & IEEE80211_CHAN_NO_IR) {
1058 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
1059 			goto nla_put_failure;
1060 		if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
1061 			goto nla_put_failure;
1062 	}
1063 	if (chan->flags & IEEE80211_CHAN_RADAR) {
1064 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
1065 			goto nla_put_failure;
1066 		if (large) {
1067 			u32 time;
1068 
1069 			time = elapsed_jiffies_msecs(chan->dfs_state_entered);
1070 
1071 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
1072 					chan->dfs_state))
1073 				goto nla_put_failure;
1074 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
1075 					time))
1076 				goto nla_put_failure;
1077 			if (nla_put_u32(msg,
1078 					NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
1079 					chan->dfs_cac_ms))
1080 				goto nla_put_failure;
1081 		}
1082 	}
1083 
1084 	if (large) {
1085 		if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
1086 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
1087 			goto nla_put_failure;
1088 		if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
1089 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
1090 			goto nla_put_failure;
1091 		if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
1092 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
1093 			goto nla_put_failure;
1094 		if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
1095 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
1096 			goto nla_put_failure;
1097 		if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
1098 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
1099 			goto nla_put_failure;
1100 		if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
1101 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
1102 			goto nla_put_failure;
1103 		if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
1104 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
1105 			goto nla_put_failure;
1106 		if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
1107 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
1108 			goto nla_put_failure;
1109 		if ((chan->flags & IEEE80211_CHAN_NO_HE) &&
1110 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HE))
1111 			goto nla_put_failure;
1112 		if ((chan->flags & IEEE80211_CHAN_1MHZ) &&
1113 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_1MHZ))
1114 			goto nla_put_failure;
1115 		if ((chan->flags & IEEE80211_CHAN_2MHZ) &&
1116 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_2MHZ))
1117 			goto nla_put_failure;
1118 		if ((chan->flags & IEEE80211_CHAN_4MHZ) &&
1119 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_4MHZ))
1120 			goto nla_put_failure;
1121 		if ((chan->flags & IEEE80211_CHAN_8MHZ) &&
1122 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_8MHZ))
1123 			goto nla_put_failure;
1124 		if ((chan->flags & IEEE80211_CHAN_16MHZ) &&
1125 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_16MHZ))
1126 			goto nla_put_failure;
1127 	}
1128 
1129 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
1130 			DBM_TO_MBM(chan->max_power)))
1131 		goto nla_put_failure;
1132 
1133 	if (large) {
1134 		const struct ieee80211_reg_rule *rule =
1135 			freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1136 
1137 		if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
1138 			if (nl80211_msg_put_wmm_rules(msg, rule))
1139 				goto nla_put_failure;
1140 		}
1141 	}
1142 
1143 	return 0;
1144 
1145  nla_put_failure:
1146 	return -ENOBUFS;
1147 }
1148 
1149 static bool nl80211_put_txq_stats(struct sk_buff *msg,
1150 				  struct cfg80211_txq_stats *txqstats,
1151 				  int attrtype)
1152 {
1153 	struct nlattr *txqattr;
1154 
1155 #define PUT_TXQVAL_U32(attr, memb) do {					  \
1156 	if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&	  \
1157 	    nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
1158 		return false;						  \
1159 	} while (0)
1160 
1161 	txqattr = nla_nest_start_noflag(msg, attrtype);
1162 	if (!txqattr)
1163 		return false;
1164 
1165 	PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
1166 	PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
1167 	PUT_TXQVAL_U32(FLOWS, flows);
1168 	PUT_TXQVAL_U32(DROPS, drops);
1169 	PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
1170 	PUT_TXQVAL_U32(OVERLIMIT, overlimit);
1171 	PUT_TXQVAL_U32(OVERMEMORY, overmemory);
1172 	PUT_TXQVAL_U32(COLLISIONS, collisions);
1173 	PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
1174 	PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
1175 	PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
1176 	nla_nest_end(msg, txqattr);
1177 
1178 #undef PUT_TXQVAL_U32
1179 	return true;
1180 }
1181 
1182 /* netlink command implementations */
1183 
1184 struct key_parse {
1185 	struct key_params p;
1186 	int idx;
1187 	int type;
1188 	bool def, defmgmt, defbeacon;
1189 	bool def_uni, def_multi;
1190 };
1191 
1192 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
1193 				 struct key_parse *k)
1194 {
1195 	struct nlattr *tb[NL80211_KEY_MAX + 1];
1196 	int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
1197 					      nl80211_key_policy,
1198 					      info->extack);
1199 	if (err)
1200 		return err;
1201 
1202 	k->def = !!tb[NL80211_KEY_DEFAULT];
1203 	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1204 	k->defbeacon = !!tb[NL80211_KEY_DEFAULT_BEACON];
1205 
1206 	if (k->def) {
1207 		k->def_uni = true;
1208 		k->def_multi = true;
1209 	}
1210 	if (k->defmgmt || k->defbeacon)
1211 		k->def_multi = true;
1212 
1213 	if (tb[NL80211_KEY_IDX])
1214 		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1215 
1216 	if (tb[NL80211_KEY_DATA]) {
1217 		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1218 		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1219 	}
1220 
1221 	if (tb[NL80211_KEY_SEQ]) {
1222 		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1223 		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1224 	}
1225 
1226 	if (tb[NL80211_KEY_CIPHER])
1227 		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1228 
1229 	if (tb[NL80211_KEY_TYPE])
1230 		k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1231 
1232 	if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1233 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1234 
1235 		err = nla_parse_nested_deprecated(kdt,
1236 						  NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1237 						  tb[NL80211_KEY_DEFAULT_TYPES],
1238 						  nl80211_key_default_policy,
1239 						  info->extack);
1240 		if (err)
1241 			return err;
1242 
1243 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1244 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1245 	}
1246 
1247 	if (tb[NL80211_KEY_MODE])
1248 		k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1249 
1250 	return 0;
1251 }
1252 
1253 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1254 {
1255 	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1256 		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1257 		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1258 	}
1259 
1260 	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1261 		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1262 		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1263 	}
1264 
1265 	if (info->attrs[NL80211_ATTR_KEY_IDX])
1266 		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1267 
1268 	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1269 		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1270 
1271 	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1272 	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1273 
1274 	if (k->def) {
1275 		k->def_uni = true;
1276 		k->def_multi = true;
1277 	}
1278 	if (k->defmgmt)
1279 		k->def_multi = true;
1280 
1281 	if (info->attrs[NL80211_ATTR_KEY_TYPE])
1282 		k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1283 
1284 	if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1285 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1286 		int err = nla_parse_nested_deprecated(kdt,
1287 						      NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1288 						      info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1289 						      nl80211_key_default_policy,
1290 						      info->extack);
1291 		if (err)
1292 			return err;
1293 
1294 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1295 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1296 	}
1297 
1298 	return 0;
1299 }
1300 
1301 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1302 {
1303 	int err;
1304 
1305 	memset(k, 0, sizeof(*k));
1306 	k->idx = -1;
1307 	k->type = -1;
1308 
1309 	if (info->attrs[NL80211_ATTR_KEY])
1310 		err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1311 	else
1312 		err = nl80211_parse_key_old(info, k);
1313 
1314 	if (err)
1315 		return err;
1316 
1317 	if ((k->def ? 1 : 0) + (k->defmgmt ? 1 : 0) +
1318 	    (k->defbeacon ? 1 : 0) > 1) {
1319 		GENL_SET_ERR_MSG(info,
1320 				 "key with multiple default flags is invalid");
1321 		return -EINVAL;
1322 	}
1323 
1324 	if (k->defmgmt || k->defbeacon) {
1325 		if (k->def_uni || !k->def_multi) {
1326 			GENL_SET_ERR_MSG(info,
1327 					 "defmgmt/defbeacon key must be mcast");
1328 			return -EINVAL;
1329 		}
1330 	}
1331 
1332 	if (k->idx != -1) {
1333 		if (k->defmgmt) {
1334 			if (k->idx < 4 || k->idx > 5) {
1335 				GENL_SET_ERR_MSG(info,
1336 						 "defmgmt key idx not 4 or 5");
1337 				return -EINVAL;
1338 			}
1339 		} else if (k->defbeacon) {
1340 			if (k->idx < 6 || k->idx > 7) {
1341 				GENL_SET_ERR_MSG(info,
1342 						 "defbeacon key idx not 6 or 7");
1343 				return -EINVAL;
1344 			}
1345 		} else if (k->def) {
1346 			if (k->idx < 0 || k->idx > 3) {
1347 				GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1348 				return -EINVAL;
1349 			}
1350 		} else {
1351 			if (k->idx < 0 || k->idx > 7) {
1352 				GENL_SET_ERR_MSG(info, "key idx not 0-7");
1353 				return -EINVAL;
1354 			}
1355 		}
1356 	}
1357 
1358 	return 0;
1359 }
1360 
1361 static struct cfg80211_cached_keys *
1362 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1363 		       struct genl_info *info, bool *no_ht)
1364 {
1365 	struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1366 	struct key_parse parse;
1367 	struct nlattr *key;
1368 	struct cfg80211_cached_keys *result;
1369 	int rem, err, def = 0;
1370 	bool have_key = false;
1371 
1372 	nla_for_each_nested(key, keys, rem) {
1373 		have_key = true;
1374 		break;
1375 	}
1376 
1377 	if (!have_key)
1378 		return NULL;
1379 
1380 	result = kzalloc(sizeof(*result), GFP_KERNEL);
1381 	if (!result)
1382 		return ERR_PTR(-ENOMEM);
1383 
1384 	result->def = -1;
1385 
1386 	nla_for_each_nested(key, keys, rem) {
1387 		memset(&parse, 0, sizeof(parse));
1388 		parse.idx = -1;
1389 
1390 		err = nl80211_parse_key_new(info, key, &parse);
1391 		if (err)
1392 			goto error;
1393 		err = -EINVAL;
1394 		if (!parse.p.key)
1395 			goto error;
1396 		if (parse.idx < 0 || parse.idx > 3) {
1397 			GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1398 			goto error;
1399 		}
1400 		if (parse.def) {
1401 			if (def) {
1402 				GENL_SET_ERR_MSG(info,
1403 						 "only one key can be default");
1404 				goto error;
1405 			}
1406 			def = 1;
1407 			result->def = parse.idx;
1408 			if (!parse.def_uni || !parse.def_multi)
1409 				goto error;
1410 		} else if (parse.defmgmt)
1411 			goto error;
1412 		err = cfg80211_validate_key_settings(rdev, &parse.p,
1413 						     parse.idx, false, NULL);
1414 		if (err)
1415 			goto error;
1416 		if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1417 		    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1418 			GENL_SET_ERR_MSG(info, "connect key must be WEP");
1419 			err = -EINVAL;
1420 			goto error;
1421 		}
1422 		result->params[parse.idx].cipher = parse.p.cipher;
1423 		result->params[parse.idx].key_len = parse.p.key_len;
1424 		result->params[parse.idx].key = result->data[parse.idx];
1425 		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1426 
1427 		/* must be WEP key if we got here */
1428 		if (no_ht)
1429 			*no_ht = true;
1430 	}
1431 
1432 	if (result->def < 0) {
1433 		err = -EINVAL;
1434 		GENL_SET_ERR_MSG(info, "need a default/TX key");
1435 		goto error;
1436 	}
1437 
1438 	return result;
1439  error:
1440 	kfree(result);
1441 	return ERR_PTR(err);
1442 }
1443 
1444 static int nl80211_key_allowed(struct wireless_dev *wdev)
1445 {
1446 	ASSERT_WDEV_LOCK(wdev);
1447 
1448 	switch (wdev->iftype) {
1449 	case NL80211_IFTYPE_AP:
1450 	case NL80211_IFTYPE_AP_VLAN:
1451 	case NL80211_IFTYPE_P2P_GO:
1452 	case NL80211_IFTYPE_MESH_POINT:
1453 		break;
1454 	case NL80211_IFTYPE_ADHOC:
1455 	case NL80211_IFTYPE_STATION:
1456 	case NL80211_IFTYPE_P2P_CLIENT:
1457 		if (!wdev->current_bss)
1458 			return -ENOLINK;
1459 		break;
1460 	case NL80211_IFTYPE_UNSPECIFIED:
1461 	case NL80211_IFTYPE_OCB:
1462 	case NL80211_IFTYPE_MONITOR:
1463 	case NL80211_IFTYPE_NAN:
1464 	case NL80211_IFTYPE_P2P_DEVICE:
1465 	case NL80211_IFTYPE_WDS:
1466 	case NUM_NL80211_IFTYPES:
1467 		return -EINVAL;
1468 	}
1469 
1470 	return 0;
1471 }
1472 
1473 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1474 							u32 freq)
1475 {
1476 	struct ieee80211_channel *chan;
1477 
1478 	chan = ieee80211_get_channel_khz(wiphy, freq);
1479 	if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1480 		return NULL;
1481 	return chan;
1482 }
1483 
1484 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1485 {
1486 	struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1487 	int i;
1488 
1489 	if (!nl_modes)
1490 		goto nla_put_failure;
1491 
1492 	i = 0;
1493 	while (ifmodes) {
1494 		if ((ifmodes & 1) && nla_put_flag(msg, i))
1495 			goto nla_put_failure;
1496 		ifmodes >>= 1;
1497 		i++;
1498 	}
1499 
1500 	nla_nest_end(msg, nl_modes);
1501 	return 0;
1502 
1503 nla_put_failure:
1504 	return -ENOBUFS;
1505 }
1506 
1507 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1508 					  struct sk_buff *msg,
1509 					  bool large)
1510 {
1511 	struct nlattr *nl_combis;
1512 	int i, j;
1513 
1514 	nl_combis = nla_nest_start_noflag(msg,
1515 					  NL80211_ATTR_INTERFACE_COMBINATIONS);
1516 	if (!nl_combis)
1517 		goto nla_put_failure;
1518 
1519 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1520 		const struct ieee80211_iface_combination *c;
1521 		struct nlattr *nl_combi, *nl_limits;
1522 
1523 		c = &wiphy->iface_combinations[i];
1524 
1525 		nl_combi = nla_nest_start_noflag(msg, i + 1);
1526 		if (!nl_combi)
1527 			goto nla_put_failure;
1528 
1529 		nl_limits = nla_nest_start_noflag(msg,
1530 						  NL80211_IFACE_COMB_LIMITS);
1531 		if (!nl_limits)
1532 			goto nla_put_failure;
1533 
1534 		for (j = 0; j < c->n_limits; j++) {
1535 			struct nlattr *nl_limit;
1536 
1537 			nl_limit = nla_nest_start_noflag(msg, j + 1);
1538 			if (!nl_limit)
1539 				goto nla_put_failure;
1540 			if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1541 					c->limits[j].max))
1542 				goto nla_put_failure;
1543 			if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1544 						c->limits[j].types))
1545 				goto nla_put_failure;
1546 			nla_nest_end(msg, nl_limit);
1547 		}
1548 
1549 		nla_nest_end(msg, nl_limits);
1550 
1551 		if (c->beacon_int_infra_match &&
1552 		    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1553 			goto nla_put_failure;
1554 		if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1555 				c->num_different_channels) ||
1556 		    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1557 				c->max_interfaces))
1558 			goto nla_put_failure;
1559 		if (large &&
1560 		    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1561 				c->radar_detect_widths) ||
1562 		     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1563 				c->radar_detect_regions)))
1564 			goto nla_put_failure;
1565 		if (c->beacon_int_min_gcd &&
1566 		    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1567 				c->beacon_int_min_gcd))
1568 			goto nla_put_failure;
1569 
1570 		nla_nest_end(msg, nl_combi);
1571 	}
1572 
1573 	nla_nest_end(msg, nl_combis);
1574 
1575 	return 0;
1576 nla_put_failure:
1577 	return -ENOBUFS;
1578 }
1579 
1580 #ifdef CONFIG_PM
1581 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1582 					struct sk_buff *msg)
1583 {
1584 	const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1585 	struct nlattr *nl_tcp;
1586 
1587 	if (!tcp)
1588 		return 0;
1589 
1590 	nl_tcp = nla_nest_start_noflag(msg,
1591 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1592 	if (!nl_tcp)
1593 		return -ENOBUFS;
1594 
1595 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1596 			tcp->data_payload_max))
1597 		return -ENOBUFS;
1598 
1599 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1600 			tcp->data_payload_max))
1601 		return -ENOBUFS;
1602 
1603 	if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1604 		return -ENOBUFS;
1605 
1606 	if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1607 				sizeof(*tcp->tok), tcp->tok))
1608 		return -ENOBUFS;
1609 
1610 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1611 			tcp->data_interval_max))
1612 		return -ENOBUFS;
1613 
1614 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1615 			tcp->wake_payload_max))
1616 		return -ENOBUFS;
1617 
1618 	nla_nest_end(msg, nl_tcp);
1619 	return 0;
1620 }
1621 
1622 static int nl80211_send_wowlan(struct sk_buff *msg,
1623 			       struct cfg80211_registered_device *rdev,
1624 			       bool large)
1625 {
1626 	struct nlattr *nl_wowlan;
1627 
1628 	if (!rdev->wiphy.wowlan)
1629 		return 0;
1630 
1631 	nl_wowlan = nla_nest_start_noflag(msg,
1632 					  NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1633 	if (!nl_wowlan)
1634 		return -ENOBUFS;
1635 
1636 	if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1637 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1638 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1639 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1640 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1641 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1642 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1643 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1644 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1645 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1646 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1647 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1648 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1649 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1650 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1651 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1652 		return -ENOBUFS;
1653 
1654 	if (rdev->wiphy.wowlan->n_patterns) {
1655 		struct nl80211_pattern_support pat = {
1656 			.max_patterns = rdev->wiphy.wowlan->n_patterns,
1657 			.min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1658 			.max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1659 			.max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1660 		};
1661 
1662 		if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1663 			    sizeof(pat), &pat))
1664 			return -ENOBUFS;
1665 	}
1666 
1667 	if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1668 	    nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1669 			rdev->wiphy.wowlan->max_nd_match_sets))
1670 		return -ENOBUFS;
1671 
1672 	if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1673 		return -ENOBUFS;
1674 
1675 	nla_nest_end(msg, nl_wowlan);
1676 
1677 	return 0;
1678 }
1679 #endif
1680 
1681 static int nl80211_send_coalesce(struct sk_buff *msg,
1682 				 struct cfg80211_registered_device *rdev)
1683 {
1684 	struct nl80211_coalesce_rule_support rule;
1685 
1686 	if (!rdev->wiphy.coalesce)
1687 		return 0;
1688 
1689 	rule.max_rules = rdev->wiphy.coalesce->n_rules;
1690 	rule.max_delay = rdev->wiphy.coalesce->max_delay;
1691 	rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1692 	rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1693 	rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1694 	rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1695 
1696 	if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1697 		return -ENOBUFS;
1698 
1699 	return 0;
1700 }
1701 
1702 static int
1703 nl80211_send_iftype_data(struct sk_buff *msg,
1704 			 const struct ieee80211_supported_band *sband,
1705 			 const struct ieee80211_sband_iftype_data *iftdata)
1706 {
1707 	const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1708 
1709 	if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1710 				iftdata->types_mask))
1711 		return -ENOBUFS;
1712 
1713 	if (he_cap->has_he) {
1714 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1715 			    sizeof(he_cap->he_cap_elem.mac_cap_info),
1716 			    he_cap->he_cap_elem.mac_cap_info) ||
1717 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1718 			    sizeof(he_cap->he_cap_elem.phy_cap_info),
1719 			    he_cap->he_cap_elem.phy_cap_info) ||
1720 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1721 			    sizeof(he_cap->he_mcs_nss_supp),
1722 			    &he_cap->he_mcs_nss_supp) ||
1723 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1724 			    sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1725 			return -ENOBUFS;
1726 	}
1727 
1728 	if (sband->band == NL80211_BAND_6GHZ &&
1729 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_6GHZ_CAPA,
1730 		    sizeof(iftdata->he_6ghz_capa),
1731 		    &iftdata->he_6ghz_capa))
1732 		return -ENOBUFS;
1733 
1734 	if (iftdata->vendor_elems.data && iftdata->vendor_elems.len &&
1735 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_VENDOR_ELEMS,
1736 		    iftdata->vendor_elems.len, iftdata->vendor_elems.data))
1737 		return -ENOBUFS;
1738 
1739 	return 0;
1740 }
1741 
1742 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1743 				      struct ieee80211_supported_band *sband,
1744 				      bool large)
1745 {
1746 	struct nlattr *nl_rates, *nl_rate;
1747 	struct ieee80211_rate *rate;
1748 	int i;
1749 
1750 	/* add HT info */
1751 	if (sband->ht_cap.ht_supported &&
1752 	    (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1753 		     sizeof(sband->ht_cap.mcs),
1754 		     &sband->ht_cap.mcs) ||
1755 	     nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1756 			 sband->ht_cap.cap) ||
1757 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1758 			sband->ht_cap.ampdu_factor) ||
1759 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1760 			sband->ht_cap.ampdu_density)))
1761 		return -ENOBUFS;
1762 
1763 	/* add VHT info */
1764 	if (sband->vht_cap.vht_supported &&
1765 	    (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1766 		     sizeof(sband->vht_cap.vht_mcs),
1767 		     &sband->vht_cap.vht_mcs) ||
1768 	     nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1769 			 sband->vht_cap.cap)))
1770 		return -ENOBUFS;
1771 
1772 	if (large && sband->n_iftype_data) {
1773 		struct nlattr *nl_iftype_data =
1774 			nla_nest_start_noflag(msg,
1775 					      NL80211_BAND_ATTR_IFTYPE_DATA);
1776 		int err;
1777 
1778 		if (!nl_iftype_data)
1779 			return -ENOBUFS;
1780 
1781 		for (i = 0; i < sband->n_iftype_data; i++) {
1782 			struct nlattr *iftdata;
1783 
1784 			iftdata = nla_nest_start_noflag(msg, i + 1);
1785 			if (!iftdata)
1786 				return -ENOBUFS;
1787 
1788 			err = nl80211_send_iftype_data(msg, sband,
1789 						       &sband->iftype_data[i]);
1790 			if (err)
1791 				return err;
1792 
1793 			nla_nest_end(msg, iftdata);
1794 		}
1795 
1796 		nla_nest_end(msg, nl_iftype_data);
1797 	}
1798 
1799 	/* add EDMG info */
1800 	if (large && sband->edmg_cap.channels &&
1801 	    (nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_CHANNELS,
1802 		       sband->edmg_cap.channels) ||
1803 	    nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_BW_CONFIG,
1804 		       sband->edmg_cap.bw_config)))
1805 
1806 		return -ENOBUFS;
1807 
1808 	/* add bitrates */
1809 	nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1810 	if (!nl_rates)
1811 		return -ENOBUFS;
1812 
1813 	for (i = 0; i < sband->n_bitrates; i++) {
1814 		nl_rate = nla_nest_start_noflag(msg, i);
1815 		if (!nl_rate)
1816 			return -ENOBUFS;
1817 
1818 		rate = &sband->bitrates[i];
1819 		if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1820 				rate->bitrate))
1821 			return -ENOBUFS;
1822 		if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1823 		    nla_put_flag(msg,
1824 				 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1825 			return -ENOBUFS;
1826 
1827 		nla_nest_end(msg, nl_rate);
1828 	}
1829 
1830 	nla_nest_end(msg, nl_rates);
1831 
1832 	return 0;
1833 }
1834 
1835 static int
1836 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1837 			 const struct ieee80211_txrx_stypes *mgmt_stypes)
1838 {
1839 	u16 stypes;
1840 	struct nlattr *nl_ftypes, *nl_ifs;
1841 	enum nl80211_iftype ift;
1842 	int i;
1843 
1844 	if (!mgmt_stypes)
1845 		return 0;
1846 
1847 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
1848 	if (!nl_ifs)
1849 		return -ENOBUFS;
1850 
1851 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1852 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1853 		if (!nl_ftypes)
1854 			return -ENOBUFS;
1855 		i = 0;
1856 		stypes = mgmt_stypes[ift].tx;
1857 		while (stypes) {
1858 			if ((stypes & 1) &&
1859 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1860 					(i << 4) | IEEE80211_FTYPE_MGMT))
1861 				return -ENOBUFS;
1862 			stypes >>= 1;
1863 			i++;
1864 		}
1865 		nla_nest_end(msg, nl_ftypes);
1866 	}
1867 
1868 	nla_nest_end(msg, nl_ifs);
1869 
1870 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
1871 	if (!nl_ifs)
1872 		return -ENOBUFS;
1873 
1874 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1875 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1876 		if (!nl_ftypes)
1877 			return -ENOBUFS;
1878 		i = 0;
1879 		stypes = mgmt_stypes[ift].rx;
1880 		while (stypes) {
1881 			if ((stypes & 1) &&
1882 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1883 					(i << 4) | IEEE80211_FTYPE_MGMT))
1884 				return -ENOBUFS;
1885 			stypes >>= 1;
1886 			i++;
1887 		}
1888 		nla_nest_end(msg, nl_ftypes);
1889 	}
1890 	nla_nest_end(msg, nl_ifs);
1891 
1892 	return 0;
1893 }
1894 
1895 #define CMD(op, n)							\
1896 	 do {								\
1897 		if (rdev->ops->op) {					\
1898 			i++;						\
1899 			if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) 	\
1900 				goto nla_put_failure;			\
1901 		}							\
1902 	} while (0)
1903 
1904 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
1905 					struct sk_buff *msg)
1906 {
1907 	int i = 0;
1908 
1909 	/*
1910 	 * do *NOT* add anything into this function, new things need to be
1911 	 * advertised only to new versions of userspace that can deal with
1912 	 * the split (and they can't possibly care about new features...
1913 	 */
1914 	CMD(add_virtual_intf, NEW_INTERFACE);
1915 	CMD(change_virtual_intf, SET_INTERFACE);
1916 	CMD(add_key, NEW_KEY);
1917 	CMD(start_ap, START_AP);
1918 	CMD(add_station, NEW_STATION);
1919 	CMD(add_mpath, NEW_MPATH);
1920 	CMD(update_mesh_config, SET_MESH_CONFIG);
1921 	CMD(change_bss, SET_BSS);
1922 	CMD(auth, AUTHENTICATE);
1923 	CMD(assoc, ASSOCIATE);
1924 	CMD(deauth, DEAUTHENTICATE);
1925 	CMD(disassoc, DISASSOCIATE);
1926 	CMD(join_ibss, JOIN_IBSS);
1927 	CMD(join_mesh, JOIN_MESH);
1928 	CMD(set_pmksa, SET_PMKSA);
1929 	CMD(del_pmksa, DEL_PMKSA);
1930 	CMD(flush_pmksa, FLUSH_PMKSA);
1931 	if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1932 		CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1933 	CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1934 	CMD(mgmt_tx, FRAME);
1935 	CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1936 	if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1937 		i++;
1938 		if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1939 			goto nla_put_failure;
1940 	}
1941 	if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1942 	    rdev->ops->join_mesh) {
1943 		i++;
1944 		if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1945 			goto nla_put_failure;
1946 	}
1947 	if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1948 		CMD(tdls_mgmt, TDLS_MGMT);
1949 		CMD(tdls_oper, TDLS_OPER);
1950 	}
1951 	if (rdev->wiphy.max_sched_scan_reqs)
1952 		CMD(sched_scan_start, START_SCHED_SCAN);
1953 	CMD(probe_client, PROBE_CLIENT);
1954 	CMD(set_noack_map, SET_NOACK_MAP);
1955 	if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1956 		i++;
1957 		if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1958 			goto nla_put_failure;
1959 	}
1960 	CMD(start_p2p_device, START_P2P_DEVICE);
1961 	CMD(set_mcast_rate, SET_MCAST_RATE);
1962 #ifdef CONFIG_NL80211_TESTMODE
1963 	CMD(testmode_cmd, TESTMODE);
1964 #endif
1965 
1966 	if (rdev->ops->connect || rdev->ops->auth) {
1967 		i++;
1968 		if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1969 			goto nla_put_failure;
1970 	}
1971 
1972 	if (rdev->ops->disconnect || rdev->ops->deauth) {
1973 		i++;
1974 		if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1975 			goto nla_put_failure;
1976 	}
1977 
1978 	return i;
1979  nla_put_failure:
1980 	return -ENOBUFS;
1981 }
1982 
1983 static int
1984 nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
1985 			   struct sk_buff *msg)
1986 {
1987 	struct nlattr *ftm;
1988 
1989 	if (!cap->ftm.supported)
1990 		return 0;
1991 
1992 	ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
1993 	if (!ftm)
1994 		return -ENOBUFS;
1995 
1996 	if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
1997 		return -ENOBUFS;
1998 	if (cap->ftm.non_asap &&
1999 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
2000 		return -ENOBUFS;
2001 	if (cap->ftm.request_lci &&
2002 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
2003 		return -ENOBUFS;
2004 	if (cap->ftm.request_civicloc &&
2005 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
2006 		return -ENOBUFS;
2007 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
2008 			cap->ftm.preambles))
2009 		return -ENOBUFS;
2010 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
2011 			cap->ftm.bandwidths))
2012 		return -ENOBUFS;
2013 	if (cap->ftm.max_bursts_exponent >= 0 &&
2014 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
2015 			cap->ftm.max_bursts_exponent))
2016 		return -ENOBUFS;
2017 	if (cap->ftm.max_ftms_per_burst &&
2018 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
2019 			cap->ftm.max_ftms_per_burst))
2020 		return -ENOBUFS;
2021 	if (cap->ftm.trigger_based &&
2022 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_TRIGGER_BASED))
2023 		return -ENOBUFS;
2024 	if (cap->ftm.non_trigger_based &&
2025 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_TRIGGER_BASED))
2026 		return -ENOBUFS;
2027 
2028 	nla_nest_end(msg, ftm);
2029 	return 0;
2030 }
2031 
2032 static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
2033 				  struct sk_buff *msg)
2034 {
2035 	const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
2036 	struct nlattr *pmsr, *caps;
2037 
2038 	if (!cap)
2039 		return 0;
2040 
2041 	/*
2042 	 * we don't need to clean up anything here since the caller
2043 	 * will genlmsg_cancel() if we fail
2044 	 */
2045 
2046 	pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
2047 	if (!pmsr)
2048 		return -ENOBUFS;
2049 
2050 	if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
2051 		return -ENOBUFS;
2052 
2053 	if (cap->report_ap_tsf &&
2054 	    nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
2055 		return -ENOBUFS;
2056 
2057 	if (cap->randomize_mac_addr &&
2058 	    nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
2059 		return -ENOBUFS;
2060 
2061 	caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
2062 	if (!caps)
2063 		return -ENOBUFS;
2064 
2065 	if (nl80211_send_pmsr_ftm_capa(cap, msg))
2066 		return -ENOBUFS;
2067 
2068 	nla_nest_end(msg, caps);
2069 	nla_nest_end(msg, pmsr);
2070 
2071 	return 0;
2072 }
2073 
2074 static int
2075 nl80211_put_iftype_akm_suites(struct cfg80211_registered_device *rdev,
2076 			      struct sk_buff *msg)
2077 {
2078 	int i;
2079 	struct nlattr *nested, *nested_akms;
2080 	const struct wiphy_iftype_akm_suites *iftype_akms;
2081 
2082 	if (!rdev->wiphy.num_iftype_akm_suites ||
2083 	    !rdev->wiphy.iftype_akm_suites)
2084 		return 0;
2085 
2086 	nested = nla_nest_start(msg, NL80211_ATTR_IFTYPE_AKM_SUITES);
2087 	if (!nested)
2088 		return -ENOBUFS;
2089 
2090 	for (i = 0; i < rdev->wiphy.num_iftype_akm_suites; i++) {
2091 		nested_akms = nla_nest_start(msg, i + 1);
2092 		if (!nested_akms)
2093 			return -ENOBUFS;
2094 
2095 		iftype_akms = &rdev->wiphy.iftype_akm_suites[i];
2096 
2097 		if (nl80211_put_iftypes(msg, NL80211_IFTYPE_AKM_ATTR_IFTYPES,
2098 					iftype_akms->iftypes_mask))
2099 			return -ENOBUFS;
2100 
2101 		if (nla_put(msg, NL80211_IFTYPE_AKM_ATTR_SUITES,
2102 			    sizeof(u32) * iftype_akms->n_akm_suites,
2103 			    iftype_akms->akm_suites)) {
2104 			return -ENOBUFS;
2105 		}
2106 		nla_nest_end(msg, nested_akms);
2107 	}
2108 
2109 	nla_nest_end(msg, nested);
2110 
2111 	return 0;
2112 }
2113 
2114 static int
2115 nl80211_put_tid_config_support(struct cfg80211_registered_device *rdev,
2116 			       struct sk_buff *msg)
2117 {
2118 	struct nlattr *supp;
2119 
2120 	if (!rdev->wiphy.tid_config_support.vif &&
2121 	    !rdev->wiphy.tid_config_support.peer)
2122 		return 0;
2123 
2124 	supp = nla_nest_start(msg, NL80211_ATTR_TID_CONFIG);
2125 	if (!supp)
2126 		return -ENOSPC;
2127 
2128 	if (rdev->wiphy.tid_config_support.vif &&
2129 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_VIF_SUPP,
2130 			      rdev->wiphy.tid_config_support.vif,
2131 			      NL80211_TID_CONFIG_ATTR_PAD))
2132 		goto fail;
2133 
2134 	if (rdev->wiphy.tid_config_support.peer &&
2135 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_PEER_SUPP,
2136 			      rdev->wiphy.tid_config_support.peer,
2137 			      NL80211_TID_CONFIG_ATTR_PAD))
2138 		goto fail;
2139 
2140 	/* for now we just use the same value ... makes more sense */
2141 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_SHORT,
2142 		       rdev->wiphy.tid_config_support.max_retry))
2143 		goto fail;
2144 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_LONG,
2145 		       rdev->wiphy.tid_config_support.max_retry))
2146 		goto fail;
2147 
2148 	nla_nest_end(msg, supp);
2149 
2150 	return 0;
2151 fail:
2152 	nla_nest_cancel(msg, supp);
2153 	return -ENOBUFS;
2154 }
2155 
2156 static int
2157 nl80211_put_sar_specs(struct cfg80211_registered_device *rdev,
2158 		      struct sk_buff *msg)
2159 {
2160 	struct nlattr *sar_capa, *specs, *sub_freq_range;
2161 	u8 num_freq_ranges;
2162 	int i;
2163 
2164 	if (!rdev->wiphy.sar_capa)
2165 		return 0;
2166 
2167 	num_freq_ranges = rdev->wiphy.sar_capa->num_freq_ranges;
2168 
2169 	sar_capa = nla_nest_start(msg, NL80211_ATTR_SAR_SPEC);
2170 	if (!sar_capa)
2171 		return -ENOSPC;
2172 
2173 	if (nla_put_u32(msg, NL80211_SAR_ATTR_TYPE, rdev->wiphy.sar_capa->type))
2174 		goto fail;
2175 
2176 	specs = nla_nest_start(msg, NL80211_SAR_ATTR_SPECS);
2177 	if (!specs)
2178 		goto fail;
2179 
2180 	/* report supported freq_ranges */
2181 	for (i = 0; i < num_freq_ranges; i++) {
2182 		sub_freq_range = nla_nest_start(msg, i + 1);
2183 		if (!sub_freq_range)
2184 			goto fail;
2185 
2186 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_START_FREQ,
2187 				rdev->wiphy.sar_capa->freq_ranges[i].start_freq))
2188 			goto fail;
2189 
2190 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_END_FREQ,
2191 				rdev->wiphy.sar_capa->freq_ranges[i].end_freq))
2192 			goto fail;
2193 
2194 		nla_nest_end(msg, sub_freq_range);
2195 	}
2196 
2197 	nla_nest_end(msg, specs);
2198 	nla_nest_end(msg, sar_capa);
2199 
2200 	return 0;
2201 fail:
2202 	nla_nest_cancel(msg, sar_capa);
2203 	return -ENOBUFS;
2204 }
2205 
2206 struct nl80211_dump_wiphy_state {
2207 	s64 filter_wiphy;
2208 	long start;
2209 	long split_start, band_start, chan_start, capa_start;
2210 	bool split;
2211 };
2212 
2213 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
2214 			      enum nl80211_commands cmd,
2215 			      struct sk_buff *msg, u32 portid, u32 seq,
2216 			      int flags, struct nl80211_dump_wiphy_state *state)
2217 {
2218 	void *hdr;
2219 	struct nlattr *nl_bands, *nl_band;
2220 	struct nlattr *nl_freqs, *nl_freq;
2221 	struct nlattr *nl_cmds;
2222 	enum nl80211_band band;
2223 	struct ieee80211_channel *chan;
2224 	int i;
2225 	const struct ieee80211_txrx_stypes *mgmt_stypes =
2226 				rdev->wiphy.mgmt_stypes;
2227 	u32 features;
2228 
2229 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2230 	if (!hdr)
2231 		return -ENOBUFS;
2232 
2233 	if (WARN_ON(!state))
2234 		return -EINVAL;
2235 
2236 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2237 	    nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
2238 			   wiphy_name(&rdev->wiphy)) ||
2239 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
2240 			cfg80211_rdev_list_generation))
2241 		goto nla_put_failure;
2242 
2243 	if (cmd != NL80211_CMD_NEW_WIPHY)
2244 		goto finish;
2245 
2246 	switch (state->split_start) {
2247 	case 0:
2248 		if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
2249 			       rdev->wiphy.retry_short) ||
2250 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
2251 			       rdev->wiphy.retry_long) ||
2252 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
2253 				rdev->wiphy.frag_threshold) ||
2254 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
2255 				rdev->wiphy.rts_threshold) ||
2256 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
2257 			       rdev->wiphy.coverage_class) ||
2258 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
2259 			       rdev->wiphy.max_scan_ssids) ||
2260 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
2261 			       rdev->wiphy.max_sched_scan_ssids) ||
2262 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
2263 				rdev->wiphy.max_scan_ie_len) ||
2264 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
2265 				rdev->wiphy.max_sched_scan_ie_len) ||
2266 		    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
2267 			       rdev->wiphy.max_match_sets))
2268 			goto nla_put_failure;
2269 
2270 		if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
2271 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
2272 			goto nla_put_failure;
2273 		if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
2274 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
2275 			goto nla_put_failure;
2276 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2277 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
2278 			goto nla_put_failure;
2279 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
2280 		    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
2281 			goto nla_put_failure;
2282 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2283 		    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
2284 			goto nla_put_failure;
2285 		if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
2286 		    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
2287 			goto nla_put_failure;
2288 		state->split_start++;
2289 		if (state->split)
2290 			break;
2291 		fallthrough;
2292 	case 1:
2293 		if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
2294 			    sizeof(u32) * rdev->wiphy.n_cipher_suites,
2295 			    rdev->wiphy.cipher_suites))
2296 			goto nla_put_failure;
2297 
2298 		if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
2299 			       rdev->wiphy.max_num_pmkids))
2300 			goto nla_put_failure;
2301 
2302 		if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
2303 		    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
2304 			goto nla_put_failure;
2305 
2306 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
2307 				rdev->wiphy.available_antennas_tx) ||
2308 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
2309 				rdev->wiphy.available_antennas_rx))
2310 			goto nla_put_failure;
2311 
2312 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
2313 		    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
2314 				rdev->wiphy.probe_resp_offload))
2315 			goto nla_put_failure;
2316 
2317 		if ((rdev->wiphy.available_antennas_tx ||
2318 		     rdev->wiphy.available_antennas_rx) &&
2319 		    rdev->ops->get_antenna) {
2320 			u32 tx_ant = 0, rx_ant = 0;
2321 			int res;
2322 
2323 			res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
2324 			if (!res) {
2325 				if (nla_put_u32(msg,
2326 						NL80211_ATTR_WIPHY_ANTENNA_TX,
2327 						tx_ant) ||
2328 				    nla_put_u32(msg,
2329 						NL80211_ATTR_WIPHY_ANTENNA_RX,
2330 						rx_ant))
2331 					goto nla_put_failure;
2332 			}
2333 		}
2334 
2335 		state->split_start++;
2336 		if (state->split)
2337 			break;
2338 		fallthrough;
2339 	case 2:
2340 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
2341 					rdev->wiphy.interface_modes))
2342 				goto nla_put_failure;
2343 		state->split_start++;
2344 		if (state->split)
2345 			break;
2346 		fallthrough;
2347 	case 3:
2348 		nl_bands = nla_nest_start_noflag(msg,
2349 						 NL80211_ATTR_WIPHY_BANDS);
2350 		if (!nl_bands)
2351 			goto nla_put_failure;
2352 
2353 		for (band = state->band_start;
2354 		     band < NUM_NL80211_BANDS; band++) {
2355 			struct ieee80211_supported_band *sband;
2356 
2357 			/* omit higher bands for ancient software */
2358 			if (band > NL80211_BAND_5GHZ && !state->split)
2359 				break;
2360 
2361 			sband = rdev->wiphy.bands[band];
2362 
2363 			if (!sband)
2364 				continue;
2365 
2366 			nl_band = nla_nest_start_noflag(msg, band);
2367 			if (!nl_band)
2368 				goto nla_put_failure;
2369 
2370 			switch (state->chan_start) {
2371 			case 0:
2372 				if (nl80211_send_band_rateinfo(msg, sband,
2373 							       state->split))
2374 					goto nla_put_failure;
2375 				state->chan_start++;
2376 				if (state->split)
2377 					break;
2378 				fallthrough;
2379 			default:
2380 				/* add frequencies */
2381 				nl_freqs = nla_nest_start_noflag(msg,
2382 								 NL80211_BAND_ATTR_FREQS);
2383 				if (!nl_freqs)
2384 					goto nla_put_failure;
2385 
2386 				for (i = state->chan_start - 1;
2387 				     i < sband->n_channels;
2388 				     i++) {
2389 					nl_freq = nla_nest_start_noflag(msg,
2390 									i);
2391 					if (!nl_freq)
2392 						goto nla_put_failure;
2393 
2394 					chan = &sband->channels[i];
2395 
2396 					if (nl80211_msg_put_channel(
2397 							msg, &rdev->wiphy, chan,
2398 							state->split))
2399 						goto nla_put_failure;
2400 
2401 					nla_nest_end(msg, nl_freq);
2402 					if (state->split)
2403 						break;
2404 				}
2405 				if (i < sband->n_channels)
2406 					state->chan_start = i + 2;
2407 				else
2408 					state->chan_start = 0;
2409 				nla_nest_end(msg, nl_freqs);
2410 			}
2411 
2412 			nla_nest_end(msg, nl_band);
2413 
2414 			if (state->split) {
2415 				/* start again here */
2416 				if (state->chan_start)
2417 					band--;
2418 				break;
2419 			}
2420 		}
2421 		nla_nest_end(msg, nl_bands);
2422 
2423 		if (band < NUM_NL80211_BANDS)
2424 			state->band_start = band + 1;
2425 		else
2426 			state->band_start = 0;
2427 
2428 		/* if bands & channels are done, continue outside */
2429 		if (state->band_start == 0 && state->chan_start == 0)
2430 			state->split_start++;
2431 		if (state->split)
2432 			break;
2433 		fallthrough;
2434 	case 4:
2435 		nl_cmds = nla_nest_start_noflag(msg,
2436 						NL80211_ATTR_SUPPORTED_COMMANDS);
2437 		if (!nl_cmds)
2438 			goto nla_put_failure;
2439 
2440 		i = nl80211_add_commands_unsplit(rdev, msg);
2441 		if (i < 0)
2442 			goto nla_put_failure;
2443 		if (state->split) {
2444 			CMD(crit_proto_start, CRIT_PROTOCOL_START);
2445 			CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2446 			if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2447 				CMD(channel_switch, CHANNEL_SWITCH);
2448 			CMD(set_qos_map, SET_QOS_MAP);
2449 			if (rdev->wiphy.features &
2450 					NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2451 				CMD(add_tx_ts, ADD_TX_TS);
2452 			CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2453 			CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2454 			CMD(update_ft_ies, UPDATE_FT_IES);
2455 			if (rdev->wiphy.sar_capa)
2456 				CMD(set_sar_specs, SET_SAR_SPECS);
2457 		}
2458 #undef CMD
2459 
2460 		nla_nest_end(msg, nl_cmds);
2461 		state->split_start++;
2462 		if (state->split)
2463 			break;
2464 		fallthrough;
2465 	case 5:
2466 		if (rdev->ops->remain_on_channel &&
2467 		    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2468 		    nla_put_u32(msg,
2469 				NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2470 				rdev->wiphy.max_remain_on_channel_duration))
2471 			goto nla_put_failure;
2472 
2473 		if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2474 		    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2475 			goto nla_put_failure;
2476 
2477 		state->split_start++;
2478 		if (state->split)
2479 			break;
2480 		fallthrough;
2481 	case 6:
2482 #ifdef CONFIG_PM
2483 		if (nl80211_send_wowlan(msg, rdev, state->split))
2484 			goto nla_put_failure;
2485 		state->split_start++;
2486 		if (state->split)
2487 			break;
2488 #else
2489 		state->split_start++;
2490 #endif
2491 		fallthrough;
2492 	case 7:
2493 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2494 					rdev->wiphy.software_iftypes))
2495 			goto nla_put_failure;
2496 
2497 		if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2498 						   state->split))
2499 			goto nla_put_failure;
2500 
2501 		state->split_start++;
2502 		if (state->split)
2503 			break;
2504 		fallthrough;
2505 	case 8:
2506 		if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2507 		    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2508 				rdev->wiphy.ap_sme_capa))
2509 			goto nla_put_failure;
2510 
2511 		features = rdev->wiphy.features;
2512 		/*
2513 		 * We can only add the per-channel limit information if the
2514 		 * dump is split, otherwise it makes it too big. Therefore
2515 		 * only advertise it in that case.
2516 		 */
2517 		if (state->split)
2518 			features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2519 		if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2520 			goto nla_put_failure;
2521 
2522 		if (rdev->wiphy.ht_capa_mod_mask &&
2523 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2524 			    sizeof(*rdev->wiphy.ht_capa_mod_mask),
2525 			    rdev->wiphy.ht_capa_mod_mask))
2526 			goto nla_put_failure;
2527 
2528 		if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2529 		    rdev->wiphy.max_acl_mac_addrs &&
2530 		    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2531 				rdev->wiphy.max_acl_mac_addrs))
2532 			goto nla_put_failure;
2533 
2534 		/*
2535 		 * Any information below this point is only available to
2536 		 * applications that can deal with it being split. This
2537 		 * helps ensure that newly added capabilities don't break
2538 		 * older tools by overrunning their buffers.
2539 		 *
2540 		 * We still increment split_start so that in the split
2541 		 * case we'll continue with more data in the next round,
2542 		 * but break unconditionally so unsplit data stops here.
2543 		 */
2544 		if (state->split)
2545 			state->split_start++;
2546 		else
2547 			state->split_start = 0;
2548 		break;
2549 	case 9:
2550 		if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2551 			goto nla_put_failure;
2552 
2553 		if (nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
2554 				rdev->wiphy.max_sched_scan_plans) ||
2555 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
2556 				rdev->wiphy.max_sched_scan_plan_interval) ||
2557 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
2558 				rdev->wiphy.max_sched_scan_plan_iterations))
2559 			goto nla_put_failure;
2560 
2561 		if (rdev->wiphy.extended_capabilities &&
2562 		    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2563 			     rdev->wiphy.extended_capabilities_len,
2564 			     rdev->wiphy.extended_capabilities) ||
2565 		     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2566 			     rdev->wiphy.extended_capabilities_len,
2567 			     rdev->wiphy.extended_capabilities_mask)))
2568 			goto nla_put_failure;
2569 
2570 		if (rdev->wiphy.vht_capa_mod_mask &&
2571 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2572 			    sizeof(*rdev->wiphy.vht_capa_mod_mask),
2573 			    rdev->wiphy.vht_capa_mod_mask))
2574 			goto nla_put_failure;
2575 
2576 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2577 			    rdev->wiphy.perm_addr))
2578 			goto nla_put_failure;
2579 
2580 		if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2581 		    nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2582 			    rdev->wiphy.addr_mask))
2583 			goto nla_put_failure;
2584 
2585 		if (rdev->wiphy.n_addresses > 1) {
2586 			void *attr;
2587 
2588 			attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2589 			if (!attr)
2590 				goto nla_put_failure;
2591 
2592 			for (i = 0; i < rdev->wiphy.n_addresses; i++)
2593 				if (nla_put(msg, i + 1, ETH_ALEN,
2594 					    rdev->wiphy.addresses[i].addr))
2595 					goto nla_put_failure;
2596 
2597 			nla_nest_end(msg, attr);
2598 		}
2599 
2600 		state->split_start++;
2601 		break;
2602 	case 10:
2603 		if (nl80211_send_coalesce(msg, rdev))
2604 			goto nla_put_failure;
2605 
2606 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2607 		    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2608 		     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2609 			goto nla_put_failure;
2610 
2611 		if (rdev->wiphy.max_ap_assoc_sta &&
2612 		    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2613 				rdev->wiphy.max_ap_assoc_sta))
2614 			goto nla_put_failure;
2615 
2616 		state->split_start++;
2617 		break;
2618 	case 11:
2619 		if (rdev->wiphy.n_vendor_commands) {
2620 			const struct nl80211_vendor_cmd_info *info;
2621 			struct nlattr *nested;
2622 
2623 			nested = nla_nest_start_noflag(msg,
2624 						       NL80211_ATTR_VENDOR_DATA);
2625 			if (!nested)
2626 				goto nla_put_failure;
2627 
2628 			for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2629 				info = &rdev->wiphy.vendor_commands[i].info;
2630 				if (nla_put(msg, i + 1, sizeof(*info), info))
2631 					goto nla_put_failure;
2632 			}
2633 			nla_nest_end(msg, nested);
2634 		}
2635 
2636 		if (rdev->wiphy.n_vendor_events) {
2637 			const struct nl80211_vendor_cmd_info *info;
2638 			struct nlattr *nested;
2639 
2640 			nested = nla_nest_start_noflag(msg,
2641 						       NL80211_ATTR_VENDOR_EVENTS);
2642 			if (!nested)
2643 				goto nla_put_failure;
2644 
2645 			for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2646 				info = &rdev->wiphy.vendor_events[i];
2647 				if (nla_put(msg, i + 1, sizeof(*info), info))
2648 					goto nla_put_failure;
2649 			}
2650 			nla_nest_end(msg, nested);
2651 		}
2652 		state->split_start++;
2653 		break;
2654 	case 12:
2655 		if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2656 		    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2657 			       rdev->wiphy.max_num_csa_counters))
2658 			goto nla_put_failure;
2659 
2660 		if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2661 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2662 			goto nla_put_failure;
2663 
2664 		if (rdev->wiphy.max_sched_scan_reqs &&
2665 		    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2666 				rdev->wiphy.max_sched_scan_reqs))
2667 			goto nla_put_failure;
2668 
2669 		if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2670 			    sizeof(rdev->wiphy.ext_features),
2671 			    rdev->wiphy.ext_features))
2672 			goto nla_put_failure;
2673 
2674 		if (rdev->wiphy.bss_select_support) {
2675 			struct nlattr *nested;
2676 			u32 bss_select_support = rdev->wiphy.bss_select_support;
2677 
2678 			nested = nla_nest_start_noflag(msg,
2679 						       NL80211_ATTR_BSS_SELECT);
2680 			if (!nested)
2681 				goto nla_put_failure;
2682 
2683 			i = 0;
2684 			while (bss_select_support) {
2685 				if ((bss_select_support & 1) &&
2686 				    nla_put_flag(msg, i))
2687 					goto nla_put_failure;
2688 				i++;
2689 				bss_select_support >>= 1;
2690 			}
2691 			nla_nest_end(msg, nested);
2692 		}
2693 
2694 		state->split_start++;
2695 		break;
2696 	case 13:
2697 		if (rdev->wiphy.num_iftype_ext_capab &&
2698 		    rdev->wiphy.iftype_ext_capab) {
2699 			struct nlattr *nested_ext_capab, *nested;
2700 
2701 			nested = nla_nest_start_noflag(msg,
2702 						       NL80211_ATTR_IFTYPE_EXT_CAPA);
2703 			if (!nested)
2704 				goto nla_put_failure;
2705 
2706 			for (i = state->capa_start;
2707 			     i < rdev->wiphy.num_iftype_ext_capab; i++) {
2708 				const struct wiphy_iftype_ext_capab *capab;
2709 
2710 				capab = &rdev->wiphy.iftype_ext_capab[i];
2711 
2712 				nested_ext_capab = nla_nest_start_noflag(msg,
2713 									 i);
2714 				if (!nested_ext_capab ||
2715 				    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2716 						capab->iftype) ||
2717 				    nla_put(msg, NL80211_ATTR_EXT_CAPA,
2718 					    capab->extended_capabilities_len,
2719 					    capab->extended_capabilities) ||
2720 				    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2721 					    capab->extended_capabilities_len,
2722 					    capab->extended_capabilities_mask))
2723 					goto nla_put_failure;
2724 
2725 				nla_nest_end(msg, nested_ext_capab);
2726 				if (state->split)
2727 					break;
2728 			}
2729 			nla_nest_end(msg, nested);
2730 			if (i < rdev->wiphy.num_iftype_ext_capab) {
2731 				state->capa_start = i + 1;
2732 				break;
2733 			}
2734 		}
2735 
2736 		if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2737 				rdev->wiphy.nan_supported_bands))
2738 			goto nla_put_failure;
2739 
2740 		if (wiphy_ext_feature_isset(&rdev->wiphy,
2741 					    NL80211_EXT_FEATURE_TXQS)) {
2742 			struct cfg80211_txq_stats txqstats = {};
2743 			int res;
2744 
2745 			res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2746 			if (!res &&
2747 			    !nl80211_put_txq_stats(msg, &txqstats,
2748 						   NL80211_ATTR_TXQ_STATS))
2749 				goto nla_put_failure;
2750 
2751 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2752 					rdev->wiphy.txq_limit))
2753 				goto nla_put_failure;
2754 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2755 					rdev->wiphy.txq_memory_limit))
2756 				goto nla_put_failure;
2757 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2758 					rdev->wiphy.txq_quantum))
2759 				goto nla_put_failure;
2760 		}
2761 
2762 		state->split_start++;
2763 		break;
2764 	case 14:
2765 		if (nl80211_send_pmsr_capa(rdev, msg))
2766 			goto nla_put_failure;
2767 
2768 		state->split_start++;
2769 		break;
2770 	case 15:
2771 		if (rdev->wiphy.akm_suites &&
2772 		    nla_put(msg, NL80211_ATTR_AKM_SUITES,
2773 			    sizeof(u32) * rdev->wiphy.n_akm_suites,
2774 			    rdev->wiphy.akm_suites))
2775 			goto nla_put_failure;
2776 
2777 		if (nl80211_put_iftype_akm_suites(rdev, msg))
2778 			goto nla_put_failure;
2779 
2780 		if (nl80211_put_tid_config_support(rdev, msg))
2781 			goto nla_put_failure;
2782 		state->split_start++;
2783 		break;
2784 	case 16:
2785 		if (nl80211_put_sar_specs(rdev, msg))
2786 			goto nla_put_failure;
2787 
2788 		/* done */
2789 		state->split_start = 0;
2790 		break;
2791 	}
2792  finish:
2793 	genlmsg_end(msg, hdr);
2794 	return 0;
2795 
2796  nla_put_failure:
2797 	genlmsg_cancel(msg, hdr);
2798 	return -EMSGSIZE;
2799 }
2800 
2801 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
2802 				    struct netlink_callback *cb,
2803 				    struct nl80211_dump_wiphy_state *state)
2804 {
2805 	struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
2806 	int ret;
2807 
2808 	if (!tb)
2809 		return -ENOMEM;
2810 
2811 	ret = nlmsg_parse_deprecated(cb->nlh,
2812 				     GENL_HDRLEN + nl80211_fam.hdrsize,
2813 				     tb, nl80211_fam.maxattr,
2814 				     nl80211_policy, NULL);
2815 	/* ignore parse errors for backward compatibility */
2816 	if (ret) {
2817 		ret = 0;
2818 		goto out;
2819 	}
2820 
2821 	state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
2822 	if (tb[NL80211_ATTR_WIPHY])
2823 		state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2824 	if (tb[NL80211_ATTR_WDEV])
2825 		state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
2826 	if (tb[NL80211_ATTR_IFINDEX]) {
2827 		struct net_device *netdev;
2828 		struct cfg80211_registered_device *rdev;
2829 		int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2830 
2831 		netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
2832 		if (!netdev) {
2833 			ret = -ENODEV;
2834 			goto out;
2835 		}
2836 		if (netdev->ieee80211_ptr) {
2837 			rdev = wiphy_to_rdev(
2838 				netdev->ieee80211_ptr->wiphy);
2839 			state->filter_wiphy = rdev->wiphy_idx;
2840 		}
2841 	}
2842 
2843 	ret = 0;
2844 out:
2845 	kfree(tb);
2846 	return ret;
2847 }
2848 
2849 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
2850 {
2851 	int idx = 0, ret;
2852 	struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
2853 	struct cfg80211_registered_device *rdev;
2854 
2855 	rtnl_lock();
2856 	if (!state) {
2857 		state = kzalloc(sizeof(*state), GFP_KERNEL);
2858 		if (!state) {
2859 			rtnl_unlock();
2860 			return -ENOMEM;
2861 		}
2862 		state->filter_wiphy = -1;
2863 		ret = nl80211_dump_wiphy_parse(skb, cb, state);
2864 		if (ret) {
2865 			kfree(state);
2866 			rtnl_unlock();
2867 			return ret;
2868 		}
2869 		cb->args[0] = (long)state;
2870 	}
2871 
2872 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2873 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2874 			continue;
2875 		if (++idx <= state->start)
2876 			continue;
2877 		if (state->filter_wiphy != -1 &&
2878 		    state->filter_wiphy != rdev->wiphy_idx)
2879 			continue;
2880 		/* attempt to fit multiple wiphy data chunks into the skb */
2881 		do {
2882 			ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
2883 						 skb,
2884 						 NETLINK_CB(cb->skb).portid,
2885 						 cb->nlh->nlmsg_seq,
2886 						 NLM_F_MULTI, state);
2887 			if (ret < 0) {
2888 				/*
2889 				 * If sending the wiphy data didn't fit (ENOBUFS
2890 				 * or EMSGSIZE returned), this SKB is still
2891 				 * empty (so it's not too big because another
2892 				 * wiphy dataset is already in the skb) and
2893 				 * we've not tried to adjust the dump allocation
2894 				 * yet ... then adjust the alloc size to be
2895 				 * bigger, and return 1 but with the empty skb.
2896 				 * This results in an empty message being RX'ed
2897 				 * in userspace, but that is ignored.
2898 				 *
2899 				 * We can then retry with the larger buffer.
2900 				 */
2901 				if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
2902 				    !skb->len && !state->split &&
2903 				    cb->min_dump_alloc < 4096) {
2904 					cb->min_dump_alloc = 4096;
2905 					state->split_start = 0;
2906 					rtnl_unlock();
2907 					return 1;
2908 				}
2909 				idx--;
2910 				break;
2911 			}
2912 		} while (state->split_start > 0);
2913 		break;
2914 	}
2915 	rtnl_unlock();
2916 
2917 	state->start = idx;
2918 
2919 	return skb->len;
2920 }
2921 
2922 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
2923 {
2924 	kfree((void *)cb->args[0]);
2925 	return 0;
2926 }
2927 
2928 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
2929 {
2930 	struct sk_buff *msg;
2931 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2932 	struct nl80211_dump_wiphy_state state = {};
2933 
2934 	msg = nlmsg_new(4096, GFP_KERNEL);
2935 	if (!msg)
2936 		return -ENOMEM;
2937 
2938 	if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
2939 			       info->snd_portid, info->snd_seq, 0,
2940 			       &state) < 0) {
2941 		nlmsg_free(msg);
2942 		return -ENOBUFS;
2943 	}
2944 
2945 	return genlmsg_reply(msg, info);
2946 }
2947 
2948 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
2949 	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
2950 	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
2951 	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
2952 	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
2953 	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
2954 };
2955 
2956 static int parse_txq_params(struct nlattr *tb[],
2957 			    struct ieee80211_txq_params *txq_params)
2958 {
2959 	u8 ac;
2960 
2961 	if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
2962 	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
2963 	    !tb[NL80211_TXQ_ATTR_AIFS])
2964 		return -EINVAL;
2965 
2966 	ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
2967 	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
2968 	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
2969 	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
2970 	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
2971 
2972 	if (ac >= NL80211_NUM_ACS)
2973 		return -EINVAL;
2974 	txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
2975 	return 0;
2976 }
2977 
2978 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
2979 {
2980 	/*
2981 	 * You can only set the channel explicitly for some interfaces,
2982 	 * most have their channel managed via their respective
2983 	 * "establish a connection" command (connect, join, ...)
2984 	 *
2985 	 * For AP/GO and mesh mode, the channel can be set with the
2986 	 * channel userspace API, but is only stored and passed to the
2987 	 * low-level driver when the AP starts or the mesh is joined.
2988 	 * This is for backward compatibility, userspace can also give
2989 	 * the channel in the start-ap or join-mesh commands instead.
2990 	 *
2991 	 * Monitors are special as they are normally slaved to
2992 	 * whatever else is going on, so they have their own special
2993 	 * operation to set the monitor channel if possible.
2994 	 */
2995 	return !wdev ||
2996 		wdev->iftype == NL80211_IFTYPE_AP ||
2997 		wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
2998 		wdev->iftype == NL80211_IFTYPE_MONITOR ||
2999 		wdev->iftype == NL80211_IFTYPE_P2P_GO;
3000 }
3001 
3002 int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
3003 			  struct genl_info *info,
3004 			  struct cfg80211_chan_def *chandef)
3005 {
3006 	struct netlink_ext_ack *extack = info->extack;
3007 	struct nlattr **attrs = info->attrs;
3008 	u32 control_freq;
3009 
3010 	if (!attrs[NL80211_ATTR_WIPHY_FREQ])
3011 		return -EINVAL;
3012 
3013 	control_freq = MHZ_TO_KHZ(
3014 			nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3015 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
3016 		control_freq +=
3017 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
3018 
3019 	memset(chandef, 0, sizeof(*chandef));
3020 	chandef->chan = ieee80211_get_channel_khz(&rdev->wiphy, control_freq);
3021 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
3022 	chandef->center_freq1 = KHZ_TO_MHZ(control_freq);
3023 	chandef->freq1_offset = control_freq % 1000;
3024 	chandef->center_freq2 = 0;
3025 
3026 	/* Primary channel not allowed */
3027 	if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED) {
3028 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
3029 				    "Channel is disabled");
3030 		return -EINVAL;
3031 	}
3032 
3033 	if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
3034 		enum nl80211_channel_type chantype;
3035 
3036 		chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
3037 
3038 		switch (chantype) {
3039 		case NL80211_CHAN_NO_HT:
3040 		case NL80211_CHAN_HT20:
3041 		case NL80211_CHAN_HT40PLUS:
3042 		case NL80211_CHAN_HT40MINUS:
3043 			cfg80211_chandef_create(chandef, chandef->chan,
3044 						chantype);
3045 			/* user input for center_freq is incorrect */
3046 			if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
3047 			    chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
3048 				NL_SET_ERR_MSG_ATTR(extack,
3049 						    attrs[NL80211_ATTR_CENTER_FREQ1],
3050 						    "bad center frequency 1");
3051 				return -EINVAL;
3052 			}
3053 			/* center_freq2 must be zero */
3054 			if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
3055 			    nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
3056 				NL_SET_ERR_MSG_ATTR(extack,
3057 						    attrs[NL80211_ATTR_CENTER_FREQ2],
3058 						    "center frequency 2 can't be used");
3059 				return -EINVAL;
3060 			}
3061 			break;
3062 		default:
3063 			NL_SET_ERR_MSG_ATTR(extack,
3064 					    attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3065 					    "invalid channel type");
3066 			return -EINVAL;
3067 		}
3068 	} else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
3069 		chandef->width =
3070 			nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
3071 		if (attrs[NL80211_ATTR_CENTER_FREQ1]) {
3072 			chandef->center_freq1 =
3073 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
3074 			if (attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET])
3075 				chandef->freq1_offset = nla_get_u32(
3076 				      attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET]);
3077 			else
3078 				chandef->freq1_offset = 0;
3079 		}
3080 		if (attrs[NL80211_ATTR_CENTER_FREQ2])
3081 			chandef->center_freq2 =
3082 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
3083 	}
3084 
3085 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
3086 		chandef->edmg.channels =
3087 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
3088 
3089 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
3090 			chandef->edmg.bw_config =
3091 		     nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
3092 	} else {
3093 		chandef->edmg.bw_config = 0;
3094 		chandef->edmg.channels = 0;
3095 	}
3096 
3097 	if (!cfg80211_chandef_valid(chandef)) {
3098 		NL_SET_ERR_MSG(extack, "invalid channel definition");
3099 		return -EINVAL;
3100 	}
3101 
3102 	if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
3103 				     IEEE80211_CHAN_DISABLED)) {
3104 		NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
3105 		return -EINVAL;
3106 	}
3107 
3108 	if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
3109 	     chandef->width == NL80211_CHAN_WIDTH_10) &&
3110 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
3111 		NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
3112 		return -EINVAL;
3113 	}
3114 
3115 	return 0;
3116 }
3117 
3118 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
3119 				 struct net_device *dev,
3120 				 struct genl_info *info)
3121 {
3122 	struct cfg80211_chan_def chandef;
3123 	int result;
3124 	enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
3125 	struct wireless_dev *wdev = NULL;
3126 
3127 	if (dev)
3128 		wdev = dev->ieee80211_ptr;
3129 	if (!nl80211_can_set_dev_channel(wdev))
3130 		return -EOPNOTSUPP;
3131 	if (wdev)
3132 		iftype = wdev->iftype;
3133 
3134 	result = nl80211_parse_chandef(rdev, info, &chandef);
3135 	if (result)
3136 		return result;
3137 
3138 	switch (iftype) {
3139 	case NL80211_IFTYPE_AP:
3140 	case NL80211_IFTYPE_P2P_GO:
3141 		if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
3142 						   iftype)) {
3143 			result = -EINVAL;
3144 			break;
3145 		}
3146 		if (wdev->beacon_interval) {
3147 			if (!dev || !rdev->ops->set_ap_chanwidth ||
3148 			    !(rdev->wiphy.features &
3149 			      NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
3150 				result = -EBUSY;
3151 				break;
3152 			}
3153 
3154 			/* Only allow dynamic channel width changes */
3155 			if (chandef.chan != wdev->preset_chandef.chan) {
3156 				result = -EBUSY;
3157 				break;
3158 			}
3159 			result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
3160 			if (result)
3161 				break;
3162 		}
3163 		wdev->preset_chandef = chandef;
3164 		result = 0;
3165 		break;
3166 	case NL80211_IFTYPE_MESH_POINT:
3167 		result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
3168 		break;
3169 	case NL80211_IFTYPE_MONITOR:
3170 		result = cfg80211_set_monitor_channel(rdev, &chandef);
3171 		break;
3172 	default:
3173 		result = -EINVAL;
3174 	}
3175 
3176 	return result;
3177 }
3178 
3179 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
3180 {
3181 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3182 	struct net_device *netdev = info->user_ptr[1];
3183 
3184 	return __nl80211_set_channel(rdev, netdev, info);
3185 }
3186 
3187 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
3188 {
3189 	struct cfg80211_registered_device *rdev = NULL;
3190 	struct net_device *netdev = NULL;
3191 	struct wireless_dev *wdev;
3192 	int result = 0, rem_txq_params = 0;
3193 	struct nlattr *nl_txq_params;
3194 	u32 changed;
3195 	u8 retry_short = 0, retry_long = 0;
3196 	u32 frag_threshold = 0, rts_threshold = 0;
3197 	u8 coverage_class = 0;
3198 	u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
3199 
3200 	rtnl_lock();
3201 	/*
3202 	 * Try to find the wiphy and netdev. Normally this
3203 	 * function shouldn't need the netdev, but this is
3204 	 * done for backward compatibility -- previously
3205 	 * setting the channel was done per wiphy, but now
3206 	 * it is per netdev. Previous userland like hostapd
3207 	 * also passed a netdev to set_wiphy, so that it is
3208 	 * possible to let that go to the right netdev!
3209 	 */
3210 
3211 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
3212 		int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
3213 
3214 		netdev = __dev_get_by_index(genl_info_net(info), ifindex);
3215 		if (netdev && netdev->ieee80211_ptr)
3216 			rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
3217 		else
3218 			netdev = NULL;
3219 	}
3220 
3221 	if (!netdev) {
3222 		rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
3223 						  info->attrs);
3224 		if (IS_ERR(rdev)) {
3225 			rtnl_unlock();
3226 			return PTR_ERR(rdev);
3227 		}
3228 		wdev = NULL;
3229 		netdev = NULL;
3230 		result = 0;
3231 	} else
3232 		wdev = netdev->ieee80211_ptr;
3233 
3234 	wiphy_lock(&rdev->wiphy);
3235 
3236 	/*
3237 	 * end workaround code, by now the rdev is available
3238 	 * and locked, and wdev may or may not be NULL.
3239 	 */
3240 
3241 	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
3242 		result = cfg80211_dev_rename(
3243 			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
3244 	rtnl_unlock();
3245 
3246 	if (result)
3247 		goto out;
3248 
3249 	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
3250 		struct ieee80211_txq_params txq_params;
3251 		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
3252 
3253 		if (!rdev->ops->set_txq_params) {
3254 			result = -EOPNOTSUPP;
3255 			goto out;
3256 		}
3257 
3258 		if (!netdev) {
3259 			result = -EINVAL;
3260 			goto out;
3261 		}
3262 
3263 		if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3264 		    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3265 			result = -EINVAL;
3266 			goto out;
3267 		}
3268 
3269 		if (!netif_running(netdev)) {
3270 			result = -ENETDOWN;
3271 			goto out;
3272 		}
3273 
3274 		nla_for_each_nested(nl_txq_params,
3275 				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
3276 				    rem_txq_params) {
3277 			result = nla_parse_nested_deprecated(tb,
3278 							     NL80211_TXQ_ATTR_MAX,
3279 							     nl_txq_params,
3280 							     txq_params_policy,
3281 							     info->extack);
3282 			if (result)
3283 				goto out;
3284 			result = parse_txq_params(tb, &txq_params);
3285 			if (result)
3286 				goto out;
3287 
3288 			result = rdev_set_txq_params(rdev, netdev,
3289 						     &txq_params);
3290 			if (result)
3291 				goto out;
3292 		}
3293 	}
3294 
3295 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3296 		result = __nl80211_set_channel(
3297 			rdev,
3298 			nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
3299 			info);
3300 		if (result)
3301 			goto out;
3302 	}
3303 
3304 	if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
3305 		struct wireless_dev *txp_wdev = wdev;
3306 		enum nl80211_tx_power_setting type;
3307 		int idx, mbm = 0;
3308 
3309 		if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
3310 			txp_wdev = NULL;
3311 
3312 		if (!rdev->ops->set_tx_power) {
3313 			result = -EOPNOTSUPP;
3314 			goto out;
3315 		}
3316 
3317 		idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
3318 		type = nla_get_u32(info->attrs[idx]);
3319 
3320 		if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
3321 		    (type != NL80211_TX_POWER_AUTOMATIC)) {
3322 			result = -EINVAL;
3323 			goto out;
3324 		}
3325 
3326 		if (type != NL80211_TX_POWER_AUTOMATIC) {
3327 			idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
3328 			mbm = nla_get_u32(info->attrs[idx]);
3329 		}
3330 
3331 		result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
3332 		if (result)
3333 			goto out;
3334 	}
3335 
3336 	if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
3337 	    info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
3338 		u32 tx_ant, rx_ant;
3339 
3340 		if ((!rdev->wiphy.available_antennas_tx &&
3341 		     !rdev->wiphy.available_antennas_rx) ||
3342 		    !rdev->ops->set_antenna) {
3343 			result = -EOPNOTSUPP;
3344 			goto out;
3345 		}
3346 
3347 		tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
3348 		rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
3349 
3350 		/* reject antenna configurations which don't match the
3351 		 * available antenna masks, except for the "all" mask */
3352 		if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
3353 		    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
3354 			result = -EINVAL;
3355 			goto out;
3356 		}
3357 
3358 		tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
3359 		rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
3360 
3361 		result = rdev_set_antenna(rdev, tx_ant, rx_ant);
3362 		if (result)
3363 			goto out;
3364 	}
3365 
3366 	changed = 0;
3367 
3368 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
3369 		retry_short = nla_get_u8(
3370 			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
3371 
3372 		changed |= WIPHY_PARAM_RETRY_SHORT;
3373 	}
3374 
3375 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
3376 		retry_long = nla_get_u8(
3377 			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
3378 
3379 		changed |= WIPHY_PARAM_RETRY_LONG;
3380 	}
3381 
3382 	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
3383 		frag_threshold = nla_get_u32(
3384 			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
3385 		if (frag_threshold < 256) {
3386 			result = -EINVAL;
3387 			goto out;
3388 		}
3389 
3390 		if (frag_threshold != (u32) -1) {
3391 			/*
3392 			 * Fragments (apart from the last one) are required to
3393 			 * have even length. Make the fragmentation code
3394 			 * simpler by stripping LSB should someone try to use
3395 			 * odd threshold value.
3396 			 */
3397 			frag_threshold &= ~0x1;
3398 		}
3399 		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
3400 	}
3401 
3402 	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
3403 		rts_threshold = nla_get_u32(
3404 			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
3405 		changed |= WIPHY_PARAM_RTS_THRESHOLD;
3406 	}
3407 
3408 	if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
3409 		if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3410 			result = -EINVAL;
3411 			goto out;
3412 		}
3413 
3414 		coverage_class = nla_get_u8(
3415 			info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
3416 		changed |= WIPHY_PARAM_COVERAGE_CLASS;
3417 	}
3418 
3419 	if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3420 		if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION)) {
3421 			result = -EOPNOTSUPP;
3422 			goto out;
3423 		}
3424 
3425 		changed |= WIPHY_PARAM_DYN_ACK;
3426 	}
3427 
3428 	if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3429 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3430 					     NL80211_EXT_FEATURE_TXQS)) {
3431 			result = -EOPNOTSUPP;
3432 			goto out;
3433 		}
3434 		txq_limit = nla_get_u32(
3435 			info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3436 		changed |= WIPHY_PARAM_TXQ_LIMIT;
3437 	}
3438 
3439 	if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3440 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3441 					     NL80211_EXT_FEATURE_TXQS)) {
3442 			result = -EOPNOTSUPP;
3443 			goto out;
3444 		}
3445 		txq_memory_limit = nla_get_u32(
3446 			info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3447 		changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3448 	}
3449 
3450 	if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3451 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3452 					     NL80211_EXT_FEATURE_TXQS)) {
3453 			result = -EOPNOTSUPP;
3454 			goto out;
3455 		}
3456 		txq_quantum = nla_get_u32(
3457 			info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3458 		changed |= WIPHY_PARAM_TXQ_QUANTUM;
3459 	}
3460 
3461 	if (changed) {
3462 		u8 old_retry_short, old_retry_long;
3463 		u32 old_frag_threshold, old_rts_threshold;
3464 		u8 old_coverage_class;
3465 		u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3466 
3467 		if (!rdev->ops->set_wiphy_params) {
3468 			result = -EOPNOTSUPP;
3469 			goto out;
3470 		}
3471 
3472 		old_retry_short = rdev->wiphy.retry_short;
3473 		old_retry_long = rdev->wiphy.retry_long;
3474 		old_frag_threshold = rdev->wiphy.frag_threshold;
3475 		old_rts_threshold = rdev->wiphy.rts_threshold;
3476 		old_coverage_class = rdev->wiphy.coverage_class;
3477 		old_txq_limit = rdev->wiphy.txq_limit;
3478 		old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3479 		old_txq_quantum = rdev->wiphy.txq_quantum;
3480 
3481 		if (changed & WIPHY_PARAM_RETRY_SHORT)
3482 			rdev->wiphy.retry_short = retry_short;
3483 		if (changed & WIPHY_PARAM_RETRY_LONG)
3484 			rdev->wiphy.retry_long = retry_long;
3485 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3486 			rdev->wiphy.frag_threshold = frag_threshold;
3487 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3488 			rdev->wiphy.rts_threshold = rts_threshold;
3489 		if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3490 			rdev->wiphy.coverage_class = coverage_class;
3491 		if (changed & WIPHY_PARAM_TXQ_LIMIT)
3492 			rdev->wiphy.txq_limit = txq_limit;
3493 		if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3494 			rdev->wiphy.txq_memory_limit = txq_memory_limit;
3495 		if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3496 			rdev->wiphy.txq_quantum = txq_quantum;
3497 
3498 		result = rdev_set_wiphy_params(rdev, changed);
3499 		if (result) {
3500 			rdev->wiphy.retry_short = old_retry_short;
3501 			rdev->wiphy.retry_long = old_retry_long;
3502 			rdev->wiphy.frag_threshold = old_frag_threshold;
3503 			rdev->wiphy.rts_threshold = old_rts_threshold;
3504 			rdev->wiphy.coverage_class = old_coverage_class;
3505 			rdev->wiphy.txq_limit = old_txq_limit;
3506 			rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3507 			rdev->wiphy.txq_quantum = old_txq_quantum;
3508 			goto out;
3509 		}
3510 	}
3511 
3512 	result = 0;
3513 
3514 out:
3515 	wiphy_unlock(&rdev->wiphy);
3516 	return result;
3517 }
3518 
3519 static int nl80211_send_chandef(struct sk_buff *msg,
3520 				const struct cfg80211_chan_def *chandef)
3521 {
3522 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3523 		return -EINVAL;
3524 
3525 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3526 			chandef->chan->center_freq))
3527 		return -ENOBUFS;
3528 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
3529 			chandef->chan->freq_offset))
3530 		return -ENOBUFS;
3531 	switch (chandef->width) {
3532 	case NL80211_CHAN_WIDTH_20_NOHT:
3533 	case NL80211_CHAN_WIDTH_20:
3534 	case NL80211_CHAN_WIDTH_40:
3535 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3536 				cfg80211_get_chandef_type(chandef)))
3537 			return -ENOBUFS;
3538 		break;
3539 	default:
3540 		break;
3541 	}
3542 	if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3543 		return -ENOBUFS;
3544 	if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3545 		return -ENOBUFS;
3546 	if (chandef->center_freq2 &&
3547 	    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3548 		return -ENOBUFS;
3549 	return 0;
3550 }
3551 
3552 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3553 			      struct cfg80211_registered_device *rdev,
3554 			      struct wireless_dev *wdev,
3555 			      enum nl80211_commands cmd)
3556 {
3557 	struct net_device *dev = wdev->netdev;
3558 	void *hdr;
3559 
3560 	WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3561 		cmd != NL80211_CMD_DEL_INTERFACE &&
3562 		cmd != NL80211_CMD_SET_INTERFACE);
3563 
3564 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3565 	if (!hdr)
3566 		return -1;
3567 
3568 	if (dev &&
3569 	    (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3570 	     nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3571 		goto nla_put_failure;
3572 
3573 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3574 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3575 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3576 			      NL80211_ATTR_PAD) ||
3577 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3578 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
3579 			rdev->devlist_generation ^
3580 			(cfg80211_rdev_list_generation << 2)) ||
3581 	    nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3582 		goto nla_put_failure;
3583 
3584 	if (rdev->ops->get_channel) {
3585 		int ret;
3586 		struct cfg80211_chan_def chandef = {};
3587 
3588 		ret = rdev_get_channel(rdev, wdev, &chandef);
3589 		if (ret == 0) {
3590 			if (nl80211_send_chandef(msg, &chandef))
3591 				goto nla_put_failure;
3592 		}
3593 	}
3594 
3595 	if (rdev->ops->get_tx_power) {
3596 		int dbm, ret;
3597 
3598 		ret = rdev_get_tx_power(rdev, wdev, &dbm);
3599 		if (ret == 0 &&
3600 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3601 				DBM_TO_MBM(dbm)))
3602 			goto nla_put_failure;
3603 	}
3604 
3605 	wdev_lock(wdev);
3606 	switch (wdev->iftype) {
3607 	case NL80211_IFTYPE_AP:
3608 		if (wdev->ssid_len &&
3609 		    nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
3610 			goto nla_put_failure_locked;
3611 		break;
3612 	case NL80211_IFTYPE_STATION:
3613 	case NL80211_IFTYPE_P2P_CLIENT:
3614 	case NL80211_IFTYPE_ADHOC: {
3615 		const u8 *ssid_ie;
3616 		if (!wdev->current_bss)
3617 			break;
3618 		rcu_read_lock();
3619 		ssid_ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
3620 					       WLAN_EID_SSID);
3621 		if (ssid_ie &&
3622 		    nla_put(msg, NL80211_ATTR_SSID, ssid_ie[1], ssid_ie + 2))
3623 			goto nla_put_failure_rcu_locked;
3624 		rcu_read_unlock();
3625 		break;
3626 		}
3627 	default:
3628 		/* nothing */
3629 		break;
3630 	}
3631 	wdev_unlock(wdev);
3632 
3633 	if (rdev->ops->get_txq_stats) {
3634 		struct cfg80211_txq_stats txqstats = {};
3635 		int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3636 
3637 		if (ret == 0 &&
3638 		    !nl80211_put_txq_stats(msg, &txqstats,
3639 					   NL80211_ATTR_TXQ_STATS))
3640 			goto nla_put_failure;
3641 	}
3642 
3643 	genlmsg_end(msg, hdr);
3644 	return 0;
3645 
3646  nla_put_failure_rcu_locked:
3647 	rcu_read_unlock();
3648  nla_put_failure_locked:
3649 	wdev_unlock(wdev);
3650  nla_put_failure:
3651 	genlmsg_cancel(msg, hdr);
3652 	return -EMSGSIZE;
3653 }
3654 
3655 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3656 {
3657 	int wp_idx = 0;
3658 	int if_idx = 0;
3659 	int wp_start = cb->args[0];
3660 	int if_start = cb->args[1];
3661 	int filter_wiphy = -1;
3662 	struct cfg80211_registered_device *rdev;
3663 	struct wireless_dev *wdev;
3664 	int ret;
3665 
3666 	rtnl_lock();
3667 	if (!cb->args[2]) {
3668 		struct nl80211_dump_wiphy_state state = {
3669 			.filter_wiphy = -1,
3670 		};
3671 
3672 		ret = nl80211_dump_wiphy_parse(skb, cb, &state);
3673 		if (ret)
3674 			goto out_unlock;
3675 
3676 		filter_wiphy = state.filter_wiphy;
3677 
3678 		/*
3679 		 * if filtering, set cb->args[2] to +1 since 0 is the default
3680 		 * value needed to determine that parsing is necessary.
3681 		 */
3682 		if (filter_wiphy >= 0)
3683 			cb->args[2] = filter_wiphy + 1;
3684 		else
3685 			cb->args[2] = -1;
3686 	} else if (cb->args[2] > 0) {
3687 		filter_wiphy = cb->args[2] - 1;
3688 	}
3689 
3690 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3691 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3692 			continue;
3693 		if (wp_idx < wp_start) {
3694 			wp_idx++;
3695 			continue;
3696 		}
3697 
3698 		if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
3699 			continue;
3700 
3701 		if_idx = 0;
3702 
3703 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3704 			if (if_idx < if_start) {
3705 				if_idx++;
3706 				continue;
3707 			}
3708 			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
3709 					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
3710 					       rdev, wdev,
3711 					       NL80211_CMD_NEW_INTERFACE) < 0) {
3712 				goto out;
3713 			}
3714 			if_idx++;
3715 		}
3716 
3717 		wp_idx++;
3718 	}
3719  out:
3720 	cb->args[0] = wp_idx;
3721 	cb->args[1] = if_idx;
3722 
3723 	ret = skb->len;
3724  out_unlock:
3725 	rtnl_unlock();
3726 
3727 	return ret;
3728 }
3729 
3730 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
3731 {
3732 	struct sk_buff *msg;
3733 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3734 	struct wireless_dev *wdev = info->user_ptr[1];
3735 
3736 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3737 	if (!msg)
3738 		return -ENOMEM;
3739 
3740 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3741 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3742 		nlmsg_free(msg);
3743 		return -ENOBUFS;
3744 	}
3745 
3746 	return genlmsg_reply(msg, info);
3747 }
3748 
3749 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
3750 	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
3751 	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
3752 	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
3753 	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
3754 	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
3755 	[NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
3756 };
3757 
3758 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
3759 {
3760 	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
3761 	int flag;
3762 
3763 	*mntrflags = 0;
3764 
3765 	if (!nla)
3766 		return -EINVAL;
3767 
3768 	if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
3769 		return -EINVAL;
3770 
3771 	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
3772 		if (flags[flag])
3773 			*mntrflags |= (1<<flag);
3774 
3775 	*mntrflags |= MONITOR_FLAG_CHANGED;
3776 
3777 	return 0;
3778 }
3779 
3780 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
3781 				     enum nl80211_iftype type,
3782 				     struct genl_info *info,
3783 				     struct vif_params *params)
3784 {
3785 	bool change = false;
3786 	int err;
3787 
3788 	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
3789 		if (type != NL80211_IFTYPE_MONITOR)
3790 			return -EINVAL;
3791 
3792 		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
3793 					  &params->flags);
3794 		if (err)
3795 			return err;
3796 
3797 		change = true;
3798 	}
3799 
3800 	if (params->flags & MONITOR_FLAG_ACTIVE &&
3801 	    !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
3802 		return -EOPNOTSUPP;
3803 
3804 	if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
3805 		const u8 *mumimo_groups;
3806 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3807 
3808 		if (type != NL80211_IFTYPE_MONITOR)
3809 			return -EINVAL;
3810 
3811 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3812 			return -EOPNOTSUPP;
3813 
3814 		mumimo_groups =
3815 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
3816 
3817 		/* bits 0 and 63 are reserved and must be zero */
3818 		if ((mumimo_groups[0] & BIT(0)) ||
3819 		    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
3820 			return -EINVAL;
3821 
3822 		params->vht_mumimo_groups = mumimo_groups;
3823 		change = true;
3824 	}
3825 
3826 	if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
3827 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3828 
3829 		if (type != NL80211_IFTYPE_MONITOR)
3830 			return -EINVAL;
3831 
3832 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3833 			return -EOPNOTSUPP;
3834 
3835 		params->vht_mumimo_follow_addr =
3836 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
3837 		change = true;
3838 	}
3839 
3840 	return change ? 1 : 0;
3841 }
3842 
3843 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
3844 			       struct net_device *netdev, u8 use_4addr,
3845 			       enum nl80211_iftype iftype)
3846 {
3847 	if (!use_4addr) {
3848 		if (netdev && netif_is_bridge_port(netdev))
3849 			return -EBUSY;
3850 		return 0;
3851 	}
3852 
3853 	switch (iftype) {
3854 	case NL80211_IFTYPE_AP_VLAN:
3855 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
3856 			return 0;
3857 		break;
3858 	case NL80211_IFTYPE_STATION:
3859 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
3860 			return 0;
3861 		break;
3862 	default:
3863 		break;
3864 	}
3865 
3866 	return -EOPNOTSUPP;
3867 }
3868 
3869 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
3870 {
3871 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3872 	struct vif_params params;
3873 	int err;
3874 	enum nl80211_iftype otype, ntype;
3875 	struct net_device *dev = info->user_ptr[1];
3876 	bool change = false;
3877 
3878 	memset(&params, 0, sizeof(params));
3879 
3880 	otype = ntype = dev->ieee80211_ptr->iftype;
3881 
3882 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
3883 		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3884 		if (otype != ntype)
3885 			change = true;
3886 	}
3887 
3888 	if (info->attrs[NL80211_ATTR_MESH_ID]) {
3889 		struct wireless_dev *wdev = dev->ieee80211_ptr;
3890 
3891 		if (ntype != NL80211_IFTYPE_MESH_POINT)
3892 			return -EINVAL;
3893 		if (netif_running(dev))
3894 			return -EBUSY;
3895 
3896 		wdev_lock(wdev);
3897 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3898 			     IEEE80211_MAX_MESH_ID_LEN);
3899 		wdev->mesh_id_up_len =
3900 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3901 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3902 		       wdev->mesh_id_up_len);
3903 		wdev_unlock(wdev);
3904 	}
3905 
3906 	if (info->attrs[NL80211_ATTR_4ADDR]) {
3907 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3908 		change = true;
3909 		err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
3910 		if (err)
3911 			return err;
3912 	} else {
3913 		params.use_4addr = -1;
3914 	}
3915 
3916 	err = nl80211_parse_mon_options(rdev, ntype, info, &params);
3917 	if (err < 0)
3918 		return err;
3919 	if (err > 0)
3920 		change = true;
3921 
3922 	if (change)
3923 		err = cfg80211_change_iface(rdev, dev, ntype, &params);
3924 	else
3925 		err = 0;
3926 
3927 	if (!err && params.use_4addr != -1)
3928 		dev->ieee80211_ptr->use_4addr = params.use_4addr;
3929 
3930 	if (change && !err) {
3931 		struct wireless_dev *wdev = dev->ieee80211_ptr;
3932 
3933 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
3934 	}
3935 
3936 	return err;
3937 }
3938 
3939 static int _nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
3940 {
3941 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3942 	struct vif_params params;
3943 	struct wireless_dev *wdev;
3944 	struct sk_buff *msg;
3945 	int err;
3946 	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
3947 
3948 	memset(&params, 0, sizeof(params));
3949 
3950 	if (!info->attrs[NL80211_ATTR_IFNAME])
3951 		return -EINVAL;
3952 
3953 	if (info->attrs[NL80211_ATTR_IFTYPE])
3954 		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3955 
3956 	if (!rdev->ops->add_virtual_intf)
3957 		return -EOPNOTSUPP;
3958 
3959 	if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
3960 	     rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
3961 	    info->attrs[NL80211_ATTR_MAC]) {
3962 		nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
3963 			   ETH_ALEN);
3964 		if (!is_valid_ether_addr(params.macaddr))
3965 			return -EADDRNOTAVAIL;
3966 	}
3967 
3968 	if (info->attrs[NL80211_ATTR_4ADDR]) {
3969 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3970 		err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
3971 		if (err)
3972 			return err;
3973 	}
3974 
3975 	if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
3976 		return -EOPNOTSUPP;
3977 
3978 	err = nl80211_parse_mon_options(rdev, type, info, &params);
3979 	if (err < 0)
3980 		return err;
3981 
3982 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3983 	if (!msg)
3984 		return -ENOMEM;
3985 
3986 	wdev = rdev_add_virtual_intf(rdev,
3987 				nla_data(info->attrs[NL80211_ATTR_IFNAME]),
3988 				NET_NAME_USER, type, &params);
3989 	if (WARN_ON(!wdev)) {
3990 		nlmsg_free(msg);
3991 		return -EPROTO;
3992 	} else if (IS_ERR(wdev)) {
3993 		nlmsg_free(msg);
3994 		return PTR_ERR(wdev);
3995 	}
3996 
3997 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
3998 		wdev->owner_nlportid = info->snd_portid;
3999 
4000 	switch (type) {
4001 	case NL80211_IFTYPE_MESH_POINT:
4002 		if (!info->attrs[NL80211_ATTR_MESH_ID])
4003 			break;
4004 		wdev_lock(wdev);
4005 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
4006 			     IEEE80211_MAX_MESH_ID_LEN);
4007 		wdev->mesh_id_up_len =
4008 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4009 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4010 		       wdev->mesh_id_up_len);
4011 		wdev_unlock(wdev);
4012 		break;
4013 	case NL80211_IFTYPE_NAN:
4014 	case NL80211_IFTYPE_P2P_DEVICE:
4015 		/*
4016 		 * P2P Device and NAN do not have a netdev, so don't go
4017 		 * through the netdev notifier and must be added here
4018 		 */
4019 		cfg80211_init_wdev(wdev);
4020 		cfg80211_register_wdev(rdev, wdev);
4021 		break;
4022 	default:
4023 		break;
4024 	}
4025 
4026 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
4027 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
4028 		nlmsg_free(msg);
4029 		return -ENOBUFS;
4030 	}
4031 
4032 	return genlmsg_reply(msg, info);
4033 }
4034 
4035 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4036 {
4037 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4038 	int ret;
4039 
4040 	/* to avoid failing a new interface creation due to pending removal */
4041 	cfg80211_destroy_ifaces(rdev);
4042 
4043 	wiphy_lock(&rdev->wiphy);
4044 	ret = _nl80211_new_interface(skb, info);
4045 	wiphy_unlock(&rdev->wiphy);
4046 
4047 	return ret;
4048 }
4049 
4050 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
4051 {
4052 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4053 	struct wireless_dev *wdev = info->user_ptr[1];
4054 
4055 	if (!rdev->ops->del_virtual_intf)
4056 		return -EOPNOTSUPP;
4057 
4058 	/*
4059 	 * We hold RTNL, so this is safe, without RTNL opencount cannot
4060 	 * reach 0, and thus the rdev cannot be deleted.
4061 	 *
4062 	 * We need to do it for the dev_close(), since that will call
4063 	 * the netdev notifiers, and we need to acquire the mutex there
4064 	 * but don't know if we get there from here or from some other
4065 	 * place (e.g. "ip link set ... down").
4066 	 */
4067 	mutex_unlock(&rdev->wiphy.mtx);
4068 
4069 	/*
4070 	 * If we remove a wireless device without a netdev then clear
4071 	 * user_ptr[1] so that nl80211_post_doit won't dereference it
4072 	 * to check if it needs to do dev_put(). Otherwise it crashes
4073 	 * since the wdev has been freed, unlike with a netdev where
4074 	 * we need the dev_put() for the netdev to really be freed.
4075 	 */
4076 	if (!wdev->netdev)
4077 		info->user_ptr[1] = NULL;
4078 	else
4079 		dev_close(wdev->netdev);
4080 
4081 	mutex_lock(&rdev->wiphy.mtx);
4082 
4083 	return rdev_del_virtual_intf(rdev, wdev);
4084 }
4085 
4086 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
4087 {
4088 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4089 	struct net_device *dev = info->user_ptr[1];
4090 	u16 noack_map;
4091 
4092 	if (!info->attrs[NL80211_ATTR_NOACK_MAP])
4093 		return -EINVAL;
4094 
4095 	if (!rdev->ops->set_noack_map)
4096 		return -EOPNOTSUPP;
4097 
4098 	noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
4099 
4100 	return rdev_set_noack_map(rdev, dev, noack_map);
4101 }
4102 
4103 struct get_key_cookie {
4104 	struct sk_buff *msg;
4105 	int error;
4106 	int idx;
4107 };
4108 
4109 static void get_key_callback(void *c, struct key_params *params)
4110 {
4111 	struct nlattr *key;
4112 	struct get_key_cookie *cookie = c;
4113 
4114 	if ((params->key &&
4115 	     nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
4116 		     params->key_len, params->key)) ||
4117 	    (params->seq &&
4118 	     nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
4119 		     params->seq_len, params->seq)) ||
4120 	    (params->cipher &&
4121 	     nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
4122 			 params->cipher)))
4123 		goto nla_put_failure;
4124 
4125 	key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
4126 	if (!key)
4127 		goto nla_put_failure;
4128 
4129 	if ((params->key &&
4130 	     nla_put(cookie->msg, NL80211_KEY_DATA,
4131 		     params->key_len, params->key)) ||
4132 	    (params->seq &&
4133 	     nla_put(cookie->msg, NL80211_KEY_SEQ,
4134 		     params->seq_len, params->seq)) ||
4135 	    (params->cipher &&
4136 	     nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
4137 			 params->cipher)))
4138 		goto nla_put_failure;
4139 
4140 	if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
4141 		goto nla_put_failure;
4142 
4143 	nla_nest_end(cookie->msg, key);
4144 
4145 	return;
4146  nla_put_failure:
4147 	cookie->error = 1;
4148 }
4149 
4150 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
4151 {
4152 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4153 	int err;
4154 	struct net_device *dev = info->user_ptr[1];
4155 	u8 key_idx = 0;
4156 	const u8 *mac_addr = NULL;
4157 	bool pairwise;
4158 	struct get_key_cookie cookie = {
4159 		.error = 0,
4160 	};
4161 	void *hdr;
4162 	struct sk_buff *msg;
4163 	bool bigtk_support = false;
4164 
4165 	if (wiphy_ext_feature_isset(&rdev->wiphy,
4166 				    NL80211_EXT_FEATURE_BEACON_PROTECTION))
4167 		bigtk_support = true;
4168 
4169 	if ((dev->ieee80211_ptr->iftype == NL80211_IFTYPE_STATION ||
4170 	     dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
4171 	    wiphy_ext_feature_isset(&rdev->wiphy,
4172 				    NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
4173 		bigtk_support = true;
4174 
4175 	if (info->attrs[NL80211_ATTR_KEY_IDX]) {
4176 		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
4177 
4178 		if (key_idx >= 6 && key_idx <= 7 && !bigtk_support) {
4179 			GENL_SET_ERR_MSG(info, "BIGTK not supported");
4180 			return -EINVAL;
4181 		}
4182 	}
4183 
4184 	if (info->attrs[NL80211_ATTR_MAC])
4185 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4186 
4187 	pairwise = !!mac_addr;
4188 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
4189 		u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
4190 
4191 		if (kt != NL80211_KEYTYPE_GROUP &&
4192 		    kt != NL80211_KEYTYPE_PAIRWISE)
4193 			return -EINVAL;
4194 		pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
4195 	}
4196 
4197 	if (!rdev->ops->get_key)
4198 		return -EOPNOTSUPP;
4199 
4200 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4201 		return -ENOENT;
4202 
4203 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4204 	if (!msg)
4205 		return -ENOMEM;
4206 
4207 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
4208 			     NL80211_CMD_NEW_KEY);
4209 	if (!hdr)
4210 		goto nla_put_failure;
4211 
4212 	cookie.msg = msg;
4213 	cookie.idx = key_idx;
4214 
4215 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4216 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
4217 		goto nla_put_failure;
4218 	if (mac_addr &&
4219 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
4220 		goto nla_put_failure;
4221 
4222 	err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
4223 			   get_key_callback);
4224 
4225 	if (err)
4226 		goto free_msg;
4227 
4228 	if (cookie.error)
4229 		goto nla_put_failure;
4230 
4231 	genlmsg_end(msg, hdr);
4232 	return genlmsg_reply(msg, info);
4233 
4234  nla_put_failure:
4235 	err = -ENOBUFS;
4236  free_msg:
4237 	nlmsg_free(msg);
4238 	return err;
4239 }
4240 
4241 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
4242 {
4243 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4244 	struct key_parse key;
4245 	int err;
4246 	struct net_device *dev = info->user_ptr[1];
4247 
4248 	err = nl80211_parse_key(info, &key);
4249 	if (err)
4250 		return err;
4251 
4252 	if (key.idx < 0)
4253 		return -EINVAL;
4254 
4255 	/* Only support setting default key and
4256 	 * Extended Key ID action NL80211_KEY_SET_TX.
4257 	 */
4258 	if (!key.def && !key.defmgmt && !key.defbeacon &&
4259 	    !(key.p.mode == NL80211_KEY_SET_TX))
4260 		return -EINVAL;
4261 
4262 	wdev_lock(dev->ieee80211_ptr);
4263 
4264 	if (key.def) {
4265 		if (!rdev->ops->set_default_key) {
4266 			err = -EOPNOTSUPP;
4267 			goto out;
4268 		}
4269 
4270 		err = nl80211_key_allowed(dev->ieee80211_ptr);
4271 		if (err)
4272 			goto out;
4273 
4274 		err = rdev_set_default_key(rdev, dev, key.idx,
4275 						 key.def_uni, key.def_multi);
4276 
4277 		if (err)
4278 			goto out;
4279 
4280 #ifdef CONFIG_CFG80211_WEXT
4281 		dev->ieee80211_ptr->wext.default_key = key.idx;
4282 #endif
4283 	} else if (key.defmgmt) {
4284 		if (key.def_uni || !key.def_multi) {
4285 			err = -EINVAL;
4286 			goto out;
4287 		}
4288 
4289 		if (!rdev->ops->set_default_mgmt_key) {
4290 			err = -EOPNOTSUPP;
4291 			goto out;
4292 		}
4293 
4294 		err = nl80211_key_allowed(dev->ieee80211_ptr);
4295 		if (err)
4296 			goto out;
4297 
4298 		err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
4299 		if (err)
4300 			goto out;
4301 
4302 #ifdef CONFIG_CFG80211_WEXT
4303 		dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
4304 #endif
4305 	} else if (key.defbeacon) {
4306 		if (key.def_uni || !key.def_multi) {
4307 			err = -EINVAL;
4308 			goto out;
4309 		}
4310 
4311 		if (!rdev->ops->set_default_beacon_key) {
4312 			err = -EOPNOTSUPP;
4313 			goto out;
4314 		}
4315 
4316 		err = nl80211_key_allowed(dev->ieee80211_ptr);
4317 		if (err)
4318 			goto out;
4319 
4320 		err = rdev_set_default_beacon_key(rdev, dev, key.idx);
4321 		if (err)
4322 			goto out;
4323 	} else if (key.p.mode == NL80211_KEY_SET_TX &&
4324 		   wiphy_ext_feature_isset(&rdev->wiphy,
4325 					   NL80211_EXT_FEATURE_EXT_KEY_ID)) {
4326 		u8 *mac_addr = NULL;
4327 
4328 		if (info->attrs[NL80211_ATTR_MAC])
4329 			mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4330 
4331 		if (!mac_addr || key.idx < 0 || key.idx > 1) {
4332 			err = -EINVAL;
4333 			goto out;
4334 		}
4335 
4336 		err = rdev_add_key(rdev, dev, key.idx,
4337 				   NL80211_KEYTYPE_PAIRWISE,
4338 				   mac_addr, &key.p);
4339 	} else {
4340 		err = -EINVAL;
4341 	}
4342  out:
4343 	wdev_unlock(dev->ieee80211_ptr);
4344 
4345 	return err;
4346 }
4347 
4348 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
4349 {
4350 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4351 	int err;
4352 	struct net_device *dev = info->user_ptr[1];
4353 	struct key_parse key;
4354 	const u8 *mac_addr = NULL;
4355 
4356 	err = nl80211_parse_key(info, &key);
4357 	if (err)
4358 		return err;
4359 
4360 	if (!key.p.key) {
4361 		GENL_SET_ERR_MSG(info, "no key");
4362 		return -EINVAL;
4363 	}
4364 
4365 	if (info->attrs[NL80211_ATTR_MAC])
4366 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4367 
4368 	if (key.type == -1) {
4369 		if (mac_addr)
4370 			key.type = NL80211_KEYTYPE_PAIRWISE;
4371 		else
4372 			key.type = NL80211_KEYTYPE_GROUP;
4373 	}
4374 
4375 	/* for now */
4376 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4377 	    key.type != NL80211_KEYTYPE_GROUP) {
4378 		GENL_SET_ERR_MSG(info, "key type not pairwise or group");
4379 		return -EINVAL;
4380 	}
4381 
4382 	if (key.type == NL80211_KEYTYPE_GROUP &&
4383 	    info->attrs[NL80211_ATTR_VLAN_ID])
4384 		key.p.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
4385 
4386 	if (!rdev->ops->add_key)
4387 		return -EOPNOTSUPP;
4388 
4389 	if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
4390 					   key.type == NL80211_KEYTYPE_PAIRWISE,
4391 					   mac_addr)) {
4392 		GENL_SET_ERR_MSG(info, "key setting validation failed");
4393 		return -EINVAL;
4394 	}
4395 
4396 	wdev_lock(dev->ieee80211_ptr);
4397 	err = nl80211_key_allowed(dev->ieee80211_ptr);
4398 	if (err)
4399 		GENL_SET_ERR_MSG(info, "key not allowed");
4400 	if (!err) {
4401 		err = rdev_add_key(rdev, dev, key.idx,
4402 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4403 				    mac_addr, &key.p);
4404 		if (err)
4405 			GENL_SET_ERR_MSG(info, "key addition failed");
4406 	}
4407 	wdev_unlock(dev->ieee80211_ptr);
4408 
4409 	return err;
4410 }
4411 
4412 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
4413 {
4414 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4415 	int err;
4416 	struct net_device *dev = info->user_ptr[1];
4417 	u8 *mac_addr = NULL;
4418 	struct key_parse key;
4419 
4420 	err = nl80211_parse_key(info, &key);
4421 	if (err)
4422 		return err;
4423 
4424 	if (info->attrs[NL80211_ATTR_MAC])
4425 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4426 
4427 	if (key.type == -1) {
4428 		if (mac_addr)
4429 			key.type = NL80211_KEYTYPE_PAIRWISE;
4430 		else
4431 			key.type = NL80211_KEYTYPE_GROUP;
4432 	}
4433 
4434 	/* for now */
4435 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4436 	    key.type != NL80211_KEYTYPE_GROUP)
4437 		return -EINVAL;
4438 
4439 	if (!cfg80211_valid_key_idx(rdev, key.idx,
4440 				    key.type == NL80211_KEYTYPE_PAIRWISE))
4441 		return -EINVAL;
4442 
4443 	if (!rdev->ops->del_key)
4444 		return -EOPNOTSUPP;
4445 
4446 	wdev_lock(dev->ieee80211_ptr);
4447 	err = nl80211_key_allowed(dev->ieee80211_ptr);
4448 
4449 	if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
4450 	    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4451 		err = -ENOENT;
4452 
4453 	if (!err)
4454 		err = rdev_del_key(rdev, dev, key.idx,
4455 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4456 				   mac_addr);
4457 
4458 #ifdef CONFIG_CFG80211_WEXT
4459 	if (!err) {
4460 		if (key.idx == dev->ieee80211_ptr->wext.default_key)
4461 			dev->ieee80211_ptr->wext.default_key = -1;
4462 		else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
4463 			dev->ieee80211_ptr->wext.default_mgmt_key = -1;
4464 	}
4465 #endif
4466 	wdev_unlock(dev->ieee80211_ptr);
4467 
4468 	return err;
4469 }
4470 
4471 /* This function returns an error or the number of nested attributes */
4472 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
4473 {
4474 	struct nlattr *attr;
4475 	int n_entries = 0, tmp;
4476 
4477 	nla_for_each_nested(attr, nl_attr, tmp) {
4478 		if (nla_len(attr) != ETH_ALEN)
4479 			return -EINVAL;
4480 
4481 		n_entries++;
4482 	}
4483 
4484 	return n_entries;
4485 }
4486 
4487 /*
4488  * This function parses ACL information and allocates memory for ACL data.
4489  * On successful return, the calling function is responsible to free the
4490  * ACL buffer returned by this function.
4491  */
4492 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
4493 						struct genl_info *info)
4494 {
4495 	enum nl80211_acl_policy acl_policy;
4496 	struct nlattr *attr;
4497 	struct cfg80211_acl_data *acl;
4498 	int i = 0, n_entries, tmp;
4499 
4500 	if (!wiphy->max_acl_mac_addrs)
4501 		return ERR_PTR(-EOPNOTSUPP);
4502 
4503 	if (!info->attrs[NL80211_ATTR_ACL_POLICY])
4504 		return ERR_PTR(-EINVAL);
4505 
4506 	acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
4507 	if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
4508 	    acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
4509 		return ERR_PTR(-EINVAL);
4510 
4511 	if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
4512 		return ERR_PTR(-EINVAL);
4513 
4514 	n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
4515 	if (n_entries < 0)
4516 		return ERR_PTR(n_entries);
4517 
4518 	if (n_entries > wiphy->max_acl_mac_addrs)
4519 		return ERR_PTR(-ENOTSUPP);
4520 
4521 	acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4522 	if (!acl)
4523 		return ERR_PTR(-ENOMEM);
4524 
4525 	nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4526 		memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4527 		i++;
4528 	}
4529 
4530 	acl->n_acl_entries = n_entries;
4531 	acl->acl_policy = acl_policy;
4532 
4533 	return acl;
4534 }
4535 
4536 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4537 {
4538 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4539 	struct net_device *dev = info->user_ptr[1];
4540 	struct cfg80211_acl_data *acl;
4541 	int err;
4542 
4543 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4544 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4545 		return -EOPNOTSUPP;
4546 
4547 	if (!dev->ieee80211_ptr->beacon_interval)
4548 		return -EINVAL;
4549 
4550 	acl = parse_acl_data(&rdev->wiphy, info);
4551 	if (IS_ERR(acl))
4552 		return PTR_ERR(acl);
4553 
4554 	err = rdev_set_mac_acl(rdev, dev, acl);
4555 
4556 	kfree(acl);
4557 
4558 	return err;
4559 }
4560 
4561 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4562 			   u8 *rates, u8 rates_len)
4563 {
4564 	u8 i;
4565 	u32 mask = 0;
4566 
4567 	for (i = 0; i < rates_len; i++) {
4568 		int rate = (rates[i] & 0x7f) * 5;
4569 		int ridx;
4570 
4571 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4572 			struct ieee80211_rate *srate =
4573 				&sband->bitrates[ridx];
4574 			if (rate == srate->bitrate) {
4575 				mask |= 1 << ridx;
4576 				break;
4577 			}
4578 		}
4579 		if (ridx == sband->n_bitrates)
4580 			return 0; /* rate not found */
4581 	}
4582 
4583 	return mask;
4584 }
4585 
4586 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4587 			       u8 *rates, u8 rates_len,
4588 			       u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4589 {
4590 	u8 i;
4591 
4592 	memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4593 
4594 	for (i = 0; i < rates_len; i++) {
4595 		int ridx, rbit;
4596 
4597 		ridx = rates[i] / 8;
4598 		rbit = BIT(rates[i] % 8);
4599 
4600 		/* check validity */
4601 		if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4602 			return false;
4603 
4604 		/* check availability */
4605 		ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
4606 		if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
4607 			mcs[ridx] |= rbit;
4608 		else
4609 			return false;
4610 	}
4611 
4612 	return true;
4613 }
4614 
4615 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
4616 {
4617 	u16 mcs_mask = 0;
4618 
4619 	switch (vht_mcs_map) {
4620 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
4621 		break;
4622 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
4623 		mcs_mask = 0x00FF;
4624 		break;
4625 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
4626 		mcs_mask = 0x01FF;
4627 		break;
4628 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
4629 		mcs_mask = 0x03FF;
4630 		break;
4631 	default:
4632 		break;
4633 	}
4634 
4635 	return mcs_mask;
4636 }
4637 
4638 static void vht_build_mcs_mask(u16 vht_mcs_map,
4639 			       u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
4640 {
4641 	u8 nss;
4642 
4643 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
4644 		vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
4645 		vht_mcs_map >>= 2;
4646 	}
4647 }
4648 
4649 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
4650 			     struct nl80211_txrate_vht *txrate,
4651 			     u16 mcs[NL80211_VHT_NSS_MAX])
4652 {
4653 	u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4654 	u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
4655 	u8 i;
4656 
4657 	if (!sband->vht_cap.vht_supported)
4658 		return false;
4659 
4660 	memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
4661 
4662 	/* Build vht_mcs_mask from VHT capabilities */
4663 	vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
4664 
4665 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4666 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
4667 			mcs[i] = txrate->mcs[i];
4668 		else
4669 			return false;
4670 	}
4671 
4672 	return true;
4673 }
4674 
4675 static u16 he_mcs_map_to_mcs_mask(u8 he_mcs_map)
4676 {
4677 	switch (he_mcs_map) {
4678 	case IEEE80211_HE_MCS_NOT_SUPPORTED:
4679 		return 0;
4680 	case IEEE80211_HE_MCS_SUPPORT_0_7:
4681 		return 0x00FF;
4682 	case IEEE80211_HE_MCS_SUPPORT_0_9:
4683 		return 0x03FF;
4684 	case IEEE80211_HE_MCS_SUPPORT_0_11:
4685 		return 0xFFF;
4686 	default:
4687 		break;
4688 	}
4689 	return 0;
4690 }
4691 
4692 static void he_build_mcs_mask(u16 he_mcs_map,
4693 			      u16 he_mcs_mask[NL80211_HE_NSS_MAX])
4694 {
4695 	u8 nss;
4696 
4697 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
4698 		he_mcs_mask[nss] = he_mcs_map_to_mcs_mask(he_mcs_map & 0x03);
4699 		he_mcs_map >>= 2;
4700 	}
4701 }
4702 
4703 static u16 he_get_txmcsmap(struct genl_info *info,
4704 			   const struct ieee80211_sta_he_cap *he_cap)
4705 {
4706 	struct net_device *dev = info->user_ptr[1];
4707 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4708 	__le16	tx_mcs;
4709 
4710 	switch (wdev->chandef.width) {
4711 	case NL80211_CHAN_WIDTH_80P80:
4712 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80p80;
4713 		break;
4714 	case NL80211_CHAN_WIDTH_160:
4715 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_160;
4716 		break;
4717 	default:
4718 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80;
4719 		break;
4720 	}
4721 	return le16_to_cpu(tx_mcs);
4722 }
4723 
4724 static bool he_set_mcs_mask(struct genl_info *info,
4725 			    struct wireless_dev *wdev,
4726 			    struct ieee80211_supported_band *sband,
4727 			    struct nl80211_txrate_he *txrate,
4728 			    u16 mcs[NL80211_HE_NSS_MAX])
4729 {
4730 	const struct ieee80211_sta_he_cap *he_cap;
4731 	u16 tx_mcs_mask[NL80211_HE_NSS_MAX] = {};
4732 	u16 tx_mcs_map = 0;
4733 	u8 i;
4734 
4735 	he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
4736 	if (!he_cap)
4737 		return false;
4738 
4739 	memset(mcs, 0, sizeof(u16) * NL80211_HE_NSS_MAX);
4740 
4741 	tx_mcs_map = he_get_txmcsmap(info, he_cap);
4742 
4743 	/* Build he_mcs_mask from HE capabilities */
4744 	he_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
4745 
4746 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
4747 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
4748 			mcs[i] = txrate->mcs[i];
4749 		else
4750 			return false;
4751 	}
4752 
4753 	return true;
4754 }
4755 
4756 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
4757 					 struct nlattr *attrs[],
4758 					 enum nl80211_attrs attr,
4759 					 struct cfg80211_bitrate_mask *mask,
4760 					 struct net_device *dev,
4761 					 bool default_all_enabled)
4762 {
4763 	struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4764 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4765 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4766 	int rem, i;
4767 	struct nlattr *tx_rates;
4768 	struct ieee80211_supported_band *sband;
4769 	u16 vht_tx_mcs_map, he_tx_mcs_map;
4770 
4771 	memset(mask, 0, sizeof(*mask));
4772 	/* Default to all rates enabled */
4773 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
4774 		const struct ieee80211_sta_he_cap *he_cap;
4775 
4776 		if (!default_all_enabled)
4777 			break;
4778 
4779 		sband = rdev->wiphy.bands[i];
4780 
4781 		if (!sband)
4782 			continue;
4783 
4784 		mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
4785 		memcpy(mask->control[i].ht_mcs,
4786 		       sband->ht_cap.mcs.rx_mask,
4787 		       sizeof(mask->control[i].ht_mcs));
4788 
4789 		if (sband->vht_cap.vht_supported) {
4790 			vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4791 			vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
4792 		}
4793 
4794 		he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
4795 		if (!he_cap)
4796 			continue;
4797 
4798 		he_tx_mcs_map = he_get_txmcsmap(info, he_cap);
4799 		he_build_mcs_mask(he_tx_mcs_map, mask->control[i].he_mcs);
4800 
4801 		mask->control[i].he_gi = 0xFF;
4802 		mask->control[i].he_ltf = 0xFF;
4803 	}
4804 
4805 	/* if no rates are given set it back to the defaults */
4806 	if (!attrs[attr])
4807 		goto out;
4808 
4809 	/* The nested attribute uses enum nl80211_band as the index. This maps
4810 	 * directly to the enum nl80211_band values used in cfg80211.
4811 	 */
4812 	BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
4813 	nla_for_each_nested(tx_rates, attrs[attr], rem) {
4814 		enum nl80211_band band = nla_type(tx_rates);
4815 		int err;
4816 
4817 		if (band < 0 || band >= NUM_NL80211_BANDS)
4818 			return -EINVAL;
4819 		sband = rdev->wiphy.bands[band];
4820 		if (sband == NULL)
4821 			return -EINVAL;
4822 		err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
4823 						  tx_rates,
4824 						  nl80211_txattr_policy,
4825 						  info->extack);
4826 		if (err)
4827 			return err;
4828 		if (tb[NL80211_TXRATE_LEGACY]) {
4829 			mask->control[band].legacy = rateset_to_mask(
4830 				sband,
4831 				nla_data(tb[NL80211_TXRATE_LEGACY]),
4832 				nla_len(tb[NL80211_TXRATE_LEGACY]));
4833 			if ((mask->control[band].legacy == 0) &&
4834 			    nla_len(tb[NL80211_TXRATE_LEGACY]))
4835 				return -EINVAL;
4836 		}
4837 		if (tb[NL80211_TXRATE_HT]) {
4838 			if (!ht_rateset_to_mask(
4839 					sband,
4840 					nla_data(tb[NL80211_TXRATE_HT]),
4841 					nla_len(tb[NL80211_TXRATE_HT]),
4842 					mask->control[band].ht_mcs))
4843 				return -EINVAL;
4844 		}
4845 
4846 		if (tb[NL80211_TXRATE_VHT]) {
4847 			if (!vht_set_mcs_mask(
4848 					sband,
4849 					nla_data(tb[NL80211_TXRATE_VHT]),
4850 					mask->control[band].vht_mcs))
4851 				return -EINVAL;
4852 		}
4853 
4854 		if (tb[NL80211_TXRATE_GI]) {
4855 			mask->control[band].gi =
4856 				nla_get_u8(tb[NL80211_TXRATE_GI]);
4857 			if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
4858 				return -EINVAL;
4859 		}
4860 		if (tb[NL80211_TXRATE_HE] &&
4861 		    !he_set_mcs_mask(info, wdev, sband,
4862 				     nla_data(tb[NL80211_TXRATE_HE]),
4863 				     mask->control[band].he_mcs))
4864 			return -EINVAL;
4865 
4866 		if (tb[NL80211_TXRATE_HE_GI])
4867 			mask->control[band].he_gi =
4868 				nla_get_u8(tb[NL80211_TXRATE_HE_GI]);
4869 		if (tb[NL80211_TXRATE_HE_LTF])
4870 			mask->control[band].he_ltf =
4871 				nla_get_u8(tb[NL80211_TXRATE_HE_LTF]);
4872 
4873 		if (mask->control[band].legacy == 0) {
4874 			/* don't allow empty legacy rates if HT, VHT or HE
4875 			 * are not even supported.
4876 			 */
4877 			if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
4878 			      rdev->wiphy.bands[band]->vht_cap.vht_supported ||
4879 			      ieee80211_get_he_iftype_cap(sband, wdev->iftype)))
4880 				return -EINVAL;
4881 
4882 			for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4883 				if (mask->control[band].ht_mcs[i])
4884 					goto out;
4885 
4886 			for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4887 				if (mask->control[band].vht_mcs[i])
4888 					goto out;
4889 
4890 			for (i = 0; i < NL80211_HE_NSS_MAX; i++)
4891 				if (mask->control[band].he_mcs[i])
4892 					goto out;
4893 
4894 			/* legacy and mcs rates may not be both empty */
4895 			return -EINVAL;
4896 		}
4897 	}
4898 
4899 out:
4900 	return 0;
4901 }
4902 
4903 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
4904 				   enum nl80211_band band,
4905 				   struct cfg80211_bitrate_mask *beacon_rate)
4906 {
4907 	u32 count_ht, count_vht, count_he, i;
4908 	u32 rate = beacon_rate->control[band].legacy;
4909 
4910 	/* Allow only one rate */
4911 	if (hweight32(rate) > 1)
4912 		return -EINVAL;
4913 
4914 	count_ht = 0;
4915 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4916 		if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
4917 			return -EINVAL;
4918 		} else if (beacon_rate->control[band].ht_mcs[i]) {
4919 			count_ht++;
4920 			if (count_ht > 1)
4921 				return -EINVAL;
4922 		}
4923 		if (count_ht && rate)
4924 			return -EINVAL;
4925 	}
4926 
4927 	count_vht = 0;
4928 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4929 		if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
4930 			return -EINVAL;
4931 		} else if (beacon_rate->control[band].vht_mcs[i]) {
4932 			count_vht++;
4933 			if (count_vht > 1)
4934 				return -EINVAL;
4935 		}
4936 		if (count_vht && rate)
4937 			return -EINVAL;
4938 	}
4939 
4940 	count_he = 0;
4941 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
4942 		if (hweight16(beacon_rate->control[band].he_mcs[i]) > 1) {
4943 			return -EINVAL;
4944 		} else if (beacon_rate->control[band].he_mcs[i]) {
4945 			count_he++;
4946 			if (count_he > 1)
4947 				return -EINVAL;
4948 		}
4949 		if (count_he && rate)
4950 			return -EINVAL;
4951 	}
4952 
4953 	if ((count_ht && count_vht && count_he) ||
4954 	    (!rate && !count_ht && !count_vht && !count_he))
4955 		return -EINVAL;
4956 
4957 	if (rate &&
4958 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4959 				     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
4960 		return -EINVAL;
4961 	if (count_ht &&
4962 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4963 				     NL80211_EXT_FEATURE_BEACON_RATE_HT))
4964 		return -EINVAL;
4965 	if (count_vht &&
4966 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4967 				     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
4968 		return -EINVAL;
4969 	if (count_he &&
4970 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4971 				     NL80211_EXT_FEATURE_BEACON_RATE_HE))
4972 		return -EINVAL;
4973 
4974 	return 0;
4975 }
4976 
4977 static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
4978 				struct nlattr *attrs[],
4979 				struct cfg80211_beacon_data *bcn)
4980 {
4981 	bool haveinfo = false;
4982 	int err;
4983 
4984 	memset(bcn, 0, sizeof(*bcn));
4985 
4986 	if (attrs[NL80211_ATTR_BEACON_HEAD]) {
4987 		bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
4988 		bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
4989 		if (!bcn->head_len)
4990 			return -EINVAL;
4991 		haveinfo = true;
4992 	}
4993 
4994 	if (attrs[NL80211_ATTR_BEACON_TAIL]) {
4995 		bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
4996 		bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
4997 		haveinfo = true;
4998 	}
4999 
5000 	if (!haveinfo)
5001 		return -EINVAL;
5002 
5003 	if (attrs[NL80211_ATTR_IE]) {
5004 		bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
5005 		bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
5006 	}
5007 
5008 	if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
5009 		bcn->proberesp_ies =
5010 			nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5011 		bcn->proberesp_ies_len =
5012 			nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5013 	}
5014 
5015 	if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
5016 		bcn->assocresp_ies =
5017 			nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5018 		bcn->assocresp_ies_len =
5019 			nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5020 	}
5021 
5022 	if (attrs[NL80211_ATTR_PROBE_RESP]) {
5023 		bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
5024 		bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
5025 	}
5026 
5027 	if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
5028 		struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
5029 
5030 		err = nla_parse_nested_deprecated(tb,
5031 						  NL80211_FTM_RESP_ATTR_MAX,
5032 						  attrs[NL80211_ATTR_FTM_RESPONDER],
5033 						  NULL, NULL);
5034 		if (err)
5035 			return err;
5036 
5037 		if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
5038 		    wiphy_ext_feature_isset(&rdev->wiphy,
5039 					    NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
5040 			bcn->ftm_responder = 1;
5041 		else
5042 			return -EOPNOTSUPP;
5043 
5044 		if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
5045 			bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
5046 			bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
5047 		}
5048 
5049 		if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
5050 			bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5051 			bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5052 		}
5053 	} else {
5054 		bcn->ftm_responder = -1;
5055 	}
5056 
5057 	return 0;
5058 }
5059 
5060 static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
5061 				    struct ieee80211_he_obss_pd *he_obss_pd)
5062 {
5063 	struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
5064 	int err;
5065 
5066 	err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
5067 			       he_obss_pd_policy, NULL);
5068 	if (err)
5069 		return err;
5070 
5071 	if (!tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL])
5072 		return -EINVAL;
5073 
5074 	he_obss_pd->sr_ctrl = nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL]);
5075 
5076 	if (tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET])
5077 		he_obss_pd->min_offset =
5078 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
5079 	if (tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
5080 		he_obss_pd->max_offset =
5081 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
5082 	if (tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET])
5083 		he_obss_pd->non_srg_max_offset =
5084 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET]);
5085 
5086 	if (he_obss_pd->min_offset > he_obss_pd->max_offset)
5087 		return -EINVAL;
5088 
5089 	if (tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP])
5090 		memcpy(he_obss_pd->bss_color_bitmap,
5091 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP]),
5092 		       sizeof(he_obss_pd->bss_color_bitmap));
5093 
5094 	if (tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP])
5095 		memcpy(he_obss_pd->partial_bssid_bitmap,
5096 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP]),
5097 		       sizeof(he_obss_pd->partial_bssid_bitmap));
5098 
5099 	he_obss_pd->enable = true;
5100 
5101 	return 0;
5102 }
5103 
5104 static int nl80211_parse_he_bss_color(struct nlattr *attrs,
5105 				      struct cfg80211_he_bss_color *he_bss_color)
5106 {
5107 	struct nlattr *tb[NL80211_HE_BSS_COLOR_ATTR_MAX + 1];
5108 	int err;
5109 
5110 	err = nla_parse_nested(tb, NL80211_HE_BSS_COLOR_ATTR_MAX, attrs,
5111 			       he_bss_color_policy, NULL);
5112 	if (err)
5113 		return err;
5114 
5115 	if (!tb[NL80211_HE_BSS_COLOR_ATTR_COLOR])
5116 		return -EINVAL;
5117 
5118 	he_bss_color->color =
5119 		nla_get_u8(tb[NL80211_HE_BSS_COLOR_ATTR_COLOR]);
5120 	he_bss_color->enabled =
5121 		!nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_DISABLED]);
5122 	he_bss_color->partial =
5123 		nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_PARTIAL]);
5124 
5125 	return 0;
5126 }
5127 
5128 static int nl80211_parse_fils_discovery(struct cfg80211_registered_device *rdev,
5129 					struct nlattr *attrs,
5130 					struct cfg80211_ap_settings *params)
5131 {
5132 	struct nlattr *tb[NL80211_FILS_DISCOVERY_ATTR_MAX + 1];
5133 	int ret;
5134 	struct cfg80211_fils_discovery *fd = &params->fils_discovery;
5135 
5136 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5137 				     NL80211_EXT_FEATURE_FILS_DISCOVERY))
5138 		return -EINVAL;
5139 
5140 	ret = nla_parse_nested(tb, NL80211_FILS_DISCOVERY_ATTR_MAX, attrs,
5141 			       NULL, NULL);
5142 	if (ret)
5143 		return ret;
5144 
5145 	if (!tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] ||
5146 	    !tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] ||
5147 	    !tb[NL80211_FILS_DISCOVERY_ATTR_TMPL])
5148 		return -EINVAL;
5149 
5150 	fd->tmpl_len = nla_len(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5151 	fd->tmpl = nla_data(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5152 	fd->min_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN]);
5153 	fd->max_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX]);
5154 
5155 	return 0;
5156 }
5157 
5158 static int
5159 nl80211_parse_unsol_bcast_probe_resp(struct cfg80211_registered_device *rdev,
5160 				     struct nlattr *attrs,
5161 				     struct cfg80211_ap_settings *params)
5162 {
5163 	struct nlattr *tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1];
5164 	int ret;
5165 	struct cfg80211_unsol_bcast_probe_resp *presp =
5166 					&params->unsol_bcast_probe_resp;
5167 
5168 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5169 				     NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP))
5170 		return -EINVAL;
5171 
5172 	ret = nla_parse_nested(tb, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX,
5173 			       attrs, NULL, NULL);
5174 	if (ret)
5175 		return ret;
5176 
5177 	if (!tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] ||
5178 	    !tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL])
5179 		return -EINVAL;
5180 
5181 	presp->tmpl = nla_data(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5182 	presp->tmpl_len = nla_len(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5183 	presp->interval = nla_get_u32(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT]);
5184 	return 0;
5185 }
5186 
5187 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
5188 					    const u8 *rates)
5189 {
5190 	int i;
5191 
5192 	if (!rates)
5193 		return;
5194 
5195 	for (i = 0; i < rates[1]; i++) {
5196 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
5197 			params->ht_required = true;
5198 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
5199 			params->vht_required = true;
5200 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HE_PHY)
5201 			params->he_required = true;
5202 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_SAE_H2E)
5203 			params->sae_h2e_required = true;
5204 	}
5205 }
5206 
5207 /*
5208  * Since the nl80211 API didn't include, from the beginning, attributes about
5209  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
5210  * benefit of drivers that rebuild IEs in the firmware.
5211  */
5212 static void nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
5213 {
5214 	const struct cfg80211_beacon_data *bcn = &params->beacon;
5215 	size_t ies_len = bcn->tail_len;
5216 	const u8 *ies = bcn->tail;
5217 	const u8 *rates;
5218 	const u8 *cap;
5219 
5220 	rates = cfg80211_find_ie(WLAN_EID_SUPP_RATES, ies, ies_len);
5221 	nl80211_check_ap_rate_selectors(params, rates);
5222 
5223 	rates = cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
5224 	nl80211_check_ap_rate_selectors(params, rates);
5225 
5226 	cap = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies, ies_len);
5227 	if (cap && cap[1] >= sizeof(*params->ht_cap))
5228 		params->ht_cap = (void *)(cap + 2);
5229 	cap = cfg80211_find_ie(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
5230 	if (cap && cap[1] >= sizeof(*params->vht_cap))
5231 		params->vht_cap = (void *)(cap + 2);
5232 	cap = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
5233 	if (cap && cap[1] >= sizeof(*params->he_cap) + 1)
5234 		params->he_cap = (void *)(cap + 3);
5235 	cap = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_OPERATION, ies, ies_len);
5236 	if (cap && cap[1] >= sizeof(*params->he_oper) + 1)
5237 		params->he_oper = (void *)(cap + 3);
5238 }
5239 
5240 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
5241 				   struct cfg80211_ap_settings *params)
5242 {
5243 	struct wireless_dev *wdev;
5244 	bool ret = false;
5245 
5246 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
5247 		if (wdev->iftype != NL80211_IFTYPE_AP &&
5248 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)
5249 			continue;
5250 
5251 		if (!wdev->preset_chandef.chan)
5252 			continue;
5253 
5254 		params->chandef = wdev->preset_chandef;
5255 		ret = true;
5256 		break;
5257 	}
5258 
5259 	return ret;
5260 }
5261 
5262 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
5263 				    enum nl80211_auth_type auth_type,
5264 				    enum nl80211_commands cmd)
5265 {
5266 	if (auth_type > NL80211_AUTHTYPE_MAX)
5267 		return false;
5268 
5269 	switch (cmd) {
5270 	case NL80211_CMD_AUTHENTICATE:
5271 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5272 		    auth_type == NL80211_AUTHTYPE_SAE)
5273 			return false;
5274 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5275 					     NL80211_EXT_FEATURE_FILS_STA) &&
5276 		    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5277 		     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5278 		     auth_type == NL80211_AUTHTYPE_FILS_PK))
5279 			return false;
5280 		return true;
5281 	case NL80211_CMD_CONNECT:
5282 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5283 		    !wiphy_ext_feature_isset(&rdev->wiphy,
5284 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
5285 		    auth_type == NL80211_AUTHTYPE_SAE)
5286 			return false;
5287 
5288 		/* FILS with SK PFS or PK not supported yet */
5289 		if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5290 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5291 			return false;
5292 		if (!wiphy_ext_feature_isset(
5293 			    &rdev->wiphy,
5294 			    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
5295 		    auth_type == NL80211_AUTHTYPE_FILS_SK)
5296 			return false;
5297 		return true;
5298 	case NL80211_CMD_START_AP:
5299 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5300 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP) &&
5301 		    auth_type == NL80211_AUTHTYPE_SAE)
5302 			return false;
5303 		/* FILS not supported yet */
5304 		if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5305 		    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5306 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5307 			return false;
5308 		return true;
5309 	default:
5310 		return false;
5311 	}
5312 }
5313 
5314 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
5315 {
5316 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5317 	struct net_device *dev = info->user_ptr[1];
5318 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5319 	struct cfg80211_ap_settings params;
5320 	int err;
5321 
5322 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5323 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5324 		return -EOPNOTSUPP;
5325 
5326 	if (!rdev->ops->start_ap)
5327 		return -EOPNOTSUPP;
5328 
5329 	if (wdev->beacon_interval)
5330 		return -EALREADY;
5331 
5332 	memset(&params, 0, sizeof(params));
5333 
5334 	/* these are required for START_AP */
5335 	if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
5336 	    !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
5337 	    !info->attrs[NL80211_ATTR_BEACON_HEAD])
5338 		return -EINVAL;
5339 
5340 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon);
5341 	if (err)
5342 		return err;
5343 
5344 	params.beacon_interval =
5345 		nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5346 	params.dtim_period =
5347 		nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
5348 
5349 	err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
5350 					   params.beacon_interval);
5351 	if (err)
5352 		return err;
5353 
5354 	/*
5355 	 * In theory, some of these attributes should be required here
5356 	 * but since they were not used when the command was originally
5357 	 * added, keep them optional for old user space programs to let
5358 	 * them continue to work with drivers that do not need the
5359 	 * additional information -- drivers must check!
5360 	 */
5361 	if (info->attrs[NL80211_ATTR_SSID]) {
5362 		params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5363 		params.ssid_len =
5364 			nla_len(info->attrs[NL80211_ATTR_SSID]);
5365 		if (params.ssid_len == 0)
5366 			return -EINVAL;
5367 	}
5368 
5369 	if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
5370 		params.hidden_ssid = nla_get_u32(
5371 			info->attrs[NL80211_ATTR_HIDDEN_SSID]);
5372 
5373 	params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
5374 
5375 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5376 		params.auth_type = nla_get_u32(
5377 			info->attrs[NL80211_ATTR_AUTH_TYPE]);
5378 		if (!nl80211_valid_auth_type(rdev, params.auth_type,
5379 					     NL80211_CMD_START_AP))
5380 			return -EINVAL;
5381 	} else
5382 		params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5383 
5384 	err = nl80211_crypto_settings(rdev, info, &params.crypto,
5385 				      NL80211_MAX_NR_CIPHER_SUITES);
5386 	if (err)
5387 		return err;
5388 
5389 	if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
5390 		if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
5391 			return -EOPNOTSUPP;
5392 		params.inactivity_timeout = nla_get_u16(
5393 			info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
5394 	}
5395 
5396 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
5397 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5398 			return -EINVAL;
5399 		params.p2p_ctwindow =
5400 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
5401 		if (params.p2p_ctwindow != 0 &&
5402 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
5403 			return -EINVAL;
5404 	}
5405 
5406 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
5407 		u8 tmp;
5408 
5409 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5410 			return -EINVAL;
5411 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
5412 		params.p2p_opp_ps = tmp;
5413 		if (params.p2p_opp_ps != 0 &&
5414 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
5415 			return -EINVAL;
5416 	}
5417 
5418 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5419 		err = nl80211_parse_chandef(rdev, info, &params.chandef);
5420 		if (err)
5421 			return err;
5422 	} else if (wdev->preset_chandef.chan) {
5423 		params.chandef = wdev->preset_chandef;
5424 	} else if (!nl80211_get_ap_channel(rdev, &params))
5425 		return -EINVAL;
5426 
5427 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
5428 					   wdev->iftype))
5429 		return -EINVAL;
5430 
5431 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
5432 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
5433 						    NL80211_ATTR_TX_RATES,
5434 						    &params.beacon_rate,
5435 						    dev, false);
5436 		if (err)
5437 			return err;
5438 
5439 		err = validate_beacon_tx_rate(rdev, params.chandef.chan->band,
5440 					      &params.beacon_rate);
5441 		if (err)
5442 			return err;
5443 	}
5444 
5445 	if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
5446 		params.smps_mode =
5447 			nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
5448 		switch (params.smps_mode) {
5449 		case NL80211_SMPS_OFF:
5450 			break;
5451 		case NL80211_SMPS_STATIC:
5452 			if (!(rdev->wiphy.features &
5453 			      NL80211_FEATURE_STATIC_SMPS))
5454 				return -EINVAL;
5455 			break;
5456 		case NL80211_SMPS_DYNAMIC:
5457 			if (!(rdev->wiphy.features &
5458 			      NL80211_FEATURE_DYNAMIC_SMPS))
5459 				return -EINVAL;
5460 			break;
5461 		default:
5462 			return -EINVAL;
5463 		}
5464 	} else {
5465 		params.smps_mode = NL80211_SMPS_OFF;
5466 	}
5467 
5468 	params.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
5469 	if (params.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ])
5470 		return -EOPNOTSUPP;
5471 
5472 	if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
5473 		params.acl = parse_acl_data(&rdev->wiphy, info);
5474 		if (IS_ERR(params.acl))
5475 			return PTR_ERR(params.acl);
5476 	}
5477 
5478 	params.twt_responder =
5479 		    nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
5480 
5481 	if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
5482 		err = nl80211_parse_he_obss_pd(
5483 					info->attrs[NL80211_ATTR_HE_OBSS_PD],
5484 					&params.he_obss_pd);
5485 		if (err)
5486 			goto out;
5487 	}
5488 
5489 	if (info->attrs[NL80211_ATTR_HE_BSS_COLOR]) {
5490 		err = nl80211_parse_he_bss_color(
5491 					info->attrs[NL80211_ATTR_HE_BSS_COLOR],
5492 					&params.he_bss_color);
5493 		if (err)
5494 			goto out;
5495 	}
5496 
5497 	if (info->attrs[NL80211_ATTR_FILS_DISCOVERY]) {
5498 		err = nl80211_parse_fils_discovery(rdev,
5499 						   info->attrs[NL80211_ATTR_FILS_DISCOVERY],
5500 						   &params);
5501 		if (err)
5502 			goto out;
5503 	}
5504 
5505 	if (info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP]) {
5506 		err = nl80211_parse_unsol_bcast_probe_resp(
5507 			rdev, info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP],
5508 			&params);
5509 		if (err)
5510 			goto out;
5511 	}
5512 
5513 	nl80211_calculate_ap_params(&params);
5514 
5515 	if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
5516 		params.flags |= AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
5517 
5518 	wdev_lock(wdev);
5519 	err = rdev_start_ap(rdev, dev, &params);
5520 	if (!err) {
5521 		wdev->preset_chandef = params.chandef;
5522 		wdev->beacon_interval = params.beacon_interval;
5523 		wdev->chandef = params.chandef;
5524 		wdev->ssid_len = params.ssid_len;
5525 		memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
5526 
5527 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
5528 			wdev->conn_owner_nlportid = info->snd_portid;
5529 	}
5530 	wdev_unlock(wdev);
5531 
5532 out:
5533 	kfree(params.acl);
5534 
5535 	return err;
5536 }
5537 
5538 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
5539 {
5540 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5541 	struct net_device *dev = info->user_ptr[1];
5542 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5543 	struct cfg80211_beacon_data params;
5544 	int err;
5545 
5546 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5547 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5548 		return -EOPNOTSUPP;
5549 
5550 	if (!rdev->ops->change_beacon)
5551 		return -EOPNOTSUPP;
5552 
5553 	if (!wdev->beacon_interval)
5554 		return -EINVAL;
5555 
5556 	err = nl80211_parse_beacon(rdev, info->attrs, &params);
5557 	if (err)
5558 		return err;
5559 
5560 	wdev_lock(wdev);
5561 	err = rdev_change_beacon(rdev, dev, &params);
5562 	wdev_unlock(wdev);
5563 
5564 	return err;
5565 }
5566 
5567 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
5568 {
5569 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5570 	struct net_device *dev = info->user_ptr[1];
5571 
5572 	return cfg80211_stop_ap(rdev, dev, false);
5573 }
5574 
5575 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
5576 	[NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
5577 	[NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
5578 	[NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
5579 	[NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
5580 	[NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
5581 	[NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
5582 };
5583 
5584 static int parse_station_flags(struct genl_info *info,
5585 			       enum nl80211_iftype iftype,
5586 			       struct station_parameters *params)
5587 {
5588 	struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
5589 	struct nlattr *nla;
5590 	int flag;
5591 
5592 	/*
5593 	 * Try parsing the new attribute first so userspace
5594 	 * can specify both for older kernels.
5595 	 */
5596 	nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
5597 	if (nla) {
5598 		struct nl80211_sta_flag_update *sta_flags;
5599 
5600 		sta_flags = nla_data(nla);
5601 		params->sta_flags_mask = sta_flags->mask;
5602 		params->sta_flags_set = sta_flags->set;
5603 		params->sta_flags_set &= params->sta_flags_mask;
5604 		if ((params->sta_flags_mask |
5605 		     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
5606 			return -EINVAL;
5607 		return 0;
5608 	}
5609 
5610 	/* if present, parse the old attribute */
5611 
5612 	nla = info->attrs[NL80211_ATTR_STA_FLAGS];
5613 	if (!nla)
5614 		return 0;
5615 
5616 	if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
5617 		return -EINVAL;
5618 
5619 	/*
5620 	 * Only allow certain flags for interface types so that
5621 	 * other attributes are silently ignored. Remember that
5622 	 * this is backward compatibility code with old userspace
5623 	 * and shouldn't be hit in other cases anyway.
5624 	 */
5625 	switch (iftype) {
5626 	case NL80211_IFTYPE_AP:
5627 	case NL80211_IFTYPE_AP_VLAN:
5628 	case NL80211_IFTYPE_P2P_GO:
5629 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
5630 					 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
5631 					 BIT(NL80211_STA_FLAG_WME) |
5632 					 BIT(NL80211_STA_FLAG_MFP);
5633 		break;
5634 	case NL80211_IFTYPE_P2P_CLIENT:
5635 	case NL80211_IFTYPE_STATION:
5636 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
5637 					 BIT(NL80211_STA_FLAG_TDLS_PEER);
5638 		break;
5639 	case NL80211_IFTYPE_MESH_POINT:
5640 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5641 					 BIT(NL80211_STA_FLAG_MFP) |
5642 					 BIT(NL80211_STA_FLAG_AUTHORIZED);
5643 		break;
5644 	default:
5645 		return -EINVAL;
5646 	}
5647 
5648 	for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
5649 		if (flags[flag]) {
5650 			params->sta_flags_set |= (1<<flag);
5651 
5652 			/* no longer support new API additions in old API */
5653 			if (flag > NL80211_STA_FLAG_MAX_OLD_API)
5654 				return -EINVAL;
5655 		}
5656 	}
5657 
5658 	return 0;
5659 }
5660 
5661 bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
5662 {
5663 	struct nlattr *rate;
5664 	u32 bitrate;
5665 	u16 bitrate_compat;
5666 	enum nl80211_rate_info rate_flg;
5667 
5668 	rate = nla_nest_start_noflag(msg, attr);
5669 	if (!rate)
5670 		return false;
5671 
5672 	/* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
5673 	bitrate = cfg80211_calculate_bitrate(info);
5674 	/* report 16-bit bitrate only if we can */
5675 	bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
5676 	if (bitrate > 0 &&
5677 	    nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
5678 		return false;
5679 	if (bitrate_compat > 0 &&
5680 	    nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
5681 		return false;
5682 
5683 	switch (info->bw) {
5684 	case RATE_INFO_BW_5:
5685 		rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
5686 		break;
5687 	case RATE_INFO_BW_10:
5688 		rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
5689 		break;
5690 	default:
5691 		WARN_ON(1);
5692 		fallthrough;
5693 	case RATE_INFO_BW_20:
5694 		rate_flg = 0;
5695 		break;
5696 	case RATE_INFO_BW_40:
5697 		rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
5698 		break;
5699 	case RATE_INFO_BW_80:
5700 		rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
5701 		break;
5702 	case RATE_INFO_BW_160:
5703 		rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
5704 		break;
5705 	case RATE_INFO_BW_HE_RU:
5706 		rate_flg = 0;
5707 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
5708 	}
5709 
5710 	if (rate_flg && nla_put_flag(msg, rate_flg))
5711 		return false;
5712 
5713 	if (info->flags & RATE_INFO_FLAGS_MCS) {
5714 		if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
5715 			return false;
5716 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
5717 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
5718 			return false;
5719 	} else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
5720 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
5721 			return false;
5722 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
5723 			return false;
5724 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
5725 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
5726 			return false;
5727 	} else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
5728 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
5729 			return false;
5730 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
5731 			return false;
5732 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
5733 			return false;
5734 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
5735 			return false;
5736 		if (info->bw == RATE_INFO_BW_HE_RU &&
5737 		    nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
5738 			       info->he_ru_alloc))
5739 			return false;
5740 	}
5741 
5742 	nla_nest_end(msg, rate);
5743 	return true;
5744 }
5745 
5746 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
5747 			       int id)
5748 {
5749 	void *attr;
5750 	int i = 0;
5751 
5752 	if (!mask)
5753 		return true;
5754 
5755 	attr = nla_nest_start_noflag(msg, id);
5756 	if (!attr)
5757 		return false;
5758 
5759 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
5760 		if (!(mask & BIT(i)))
5761 			continue;
5762 
5763 		if (nla_put_u8(msg, i, signal[i]))
5764 			return false;
5765 	}
5766 
5767 	nla_nest_end(msg, attr);
5768 
5769 	return true;
5770 }
5771 
5772 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
5773 				u32 seq, int flags,
5774 				struct cfg80211_registered_device *rdev,
5775 				struct net_device *dev,
5776 				const u8 *mac_addr, struct station_info *sinfo)
5777 {
5778 	void *hdr;
5779 	struct nlattr *sinfoattr, *bss_param;
5780 
5781 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
5782 	if (!hdr) {
5783 		cfg80211_sinfo_release_content(sinfo);
5784 		return -1;
5785 	}
5786 
5787 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5788 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
5789 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
5790 		goto nla_put_failure;
5791 
5792 	sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
5793 	if (!sinfoattr)
5794 		goto nla_put_failure;
5795 
5796 #define PUT_SINFO(attr, memb, type) do {				\
5797 	BUILD_BUG_ON(sizeof(type) == sizeof(u64));			\
5798 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
5799 	    nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,		\
5800 			     sinfo->memb))				\
5801 		goto nla_put_failure;					\
5802 	} while (0)
5803 #define PUT_SINFO_U64(attr, memb) do {					\
5804 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
5805 	    nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,		\
5806 			      sinfo->memb, NL80211_STA_INFO_PAD))	\
5807 		goto nla_put_failure;					\
5808 	} while (0)
5809 
5810 	PUT_SINFO(CONNECTED_TIME, connected_time, u32);
5811 	PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
5812 	PUT_SINFO_U64(ASSOC_AT_BOOTTIME, assoc_at);
5813 
5814 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
5815 			     BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
5816 	    nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
5817 			(u32)sinfo->rx_bytes))
5818 		goto nla_put_failure;
5819 
5820 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
5821 			     BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
5822 	    nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
5823 			(u32)sinfo->tx_bytes))
5824 		goto nla_put_failure;
5825 
5826 	PUT_SINFO_U64(RX_BYTES64, rx_bytes);
5827 	PUT_SINFO_U64(TX_BYTES64, tx_bytes);
5828 	PUT_SINFO(LLID, llid, u16);
5829 	PUT_SINFO(PLID, plid, u16);
5830 	PUT_SINFO(PLINK_STATE, plink_state, u8);
5831 	PUT_SINFO_U64(RX_DURATION, rx_duration);
5832 	PUT_SINFO_U64(TX_DURATION, tx_duration);
5833 
5834 	if (wiphy_ext_feature_isset(&rdev->wiphy,
5835 				    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5836 		PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
5837 
5838 	switch (rdev->wiphy.signal_type) {
5839 	case CFG80211_SIGNAL_TYPE_MBM:
5840 		PUT_SINFO(SIGNAL, signal, u8);
5841 		PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
5842 		break;
5843 	default:
5844 		break;
5845 	}
5846 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
5847 		if (!nl80211_put_signal(msg, sinfo->chains,
5848 					sinfo->chain_signal,
5849 					NL80211_STA_INFO_CHAIN_SIGNAL))
5850 			goto nla_put_failure;
5851 	}
5852 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
5853 		if (!nl80211_put_signal(msg, sinfo->chains,
5854 					sinfo->chain_signal_avg,
5855 					NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
5856 			goto nla_put_failure;
5857 	}
5858 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
5859 		if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
5860 					  NL80211_STA_INFO_TX_BITRATE))
5861 			goto nla_put_failure;
5862 	}
5863 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
5864 		if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
5865 					  NL80211_STA_INFO_RX_BITRATE))
5866 			goto nla_put_failure;
5867 	}
5868 
5869 	PUT_SINFO(RX_PACKETS, rx_packets, u32);
5870 	PUT_SINFO(TX_PACKETS, tx_packets, u32);
5871 	PUT_SINFO(TX_RETRIES, tx_retries, u32);
5872 	PUT_SINFO(TX_FAILED, tx_failed, u32);
5873 	PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
5874 	PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
5875 	PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
5876 	PUT_SINFO(LOCAL_PM, local_pm, u32);
5877 	PUT_SINFO(PEER_PM, peer_pm, u32);
5878 	PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
5879 	PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
5880 	PUT_SINFO(CONNECTED_TO_AS, connected_to_as, u8);
5881 
5882 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
5883 		bss_param = nla_nest_start_noflag(msg,
5884 						  NL80211_STA_INFO_BSS_PARAM);
5885 		if (!bss_param)
5886 			goto nla_put_failure;
5887 
5888 		if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
5889 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
5890 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
5891 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
5892 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
5893 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
5894 		    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
5895 			       sinfo->bss_param.dtim_period) ||
5896 		    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
5897 				sinfo->bss_param.beacon_interval))
5898 			goto nla_put_failure;
5899 
5900 		nla_nest_end(msg, bss_param);
5901 	}
5902 	if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
5903 	    nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
5904 		    sizeof(struct nl80211_sta_flag_update),
5905 		    &sinfo->sta_flags))
5906 		goto nla_put_failure;
5907 
5908 	PUT_SINFO_U64(T_OFFSET, t_offset);
5909 	PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
5910 	PUT_SINFO_U64(BEACON_RX, rx_beacon);
5911 	PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
5912 	PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
5913 	PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
5914 	if (wiphy_ext_feature_isset(&rdev->wiphy,
5915 				    NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
5916 		PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
5917 		PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
5918 	}
5919 
5920 #undef PUT_SINFO
5921 #undef PUT_SINFO_U64
5922 
5923 	if (sinfo->pertid) {
5924 		struct nlattr *tidsattr;
5925 		int tid;
5926 
5927 		tidsattr = nla_nest_start_noflag(msg,
5928 						 NL80211_STA_INFO_TID_STATS);
5929 		if (!tidsattr)
5930 			goto nla_put_failure;
5931 
5932 		for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
5933 			struct cfg80211_tid_stats *tidstats;
5934 			struct nlattr *tidattr;
5935 
5936 			tidstats = &sinfo->pertid[tid];
5937 
5938 			if (!tidstats->filled)
5939 				continue;
5940 
5941 			tidattr = nla_nest_start_noflag(msg, tid + 1);
5942 			if (!tidattr)
5943 				goto nla_put_failure;
5944 
5945 #define PUT_TIDVAL_U64(attr, memb) do {					\
5946 	if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&	\
5947 	    nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,		\
5948 			      tidstats->memb, NL80211_TID_STATS_PAD))	\
5949 		goto nla_put_failure;					\
5950 	} while (0)
5951 
5952 			PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
5953 			PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
5954 			PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
5955 			PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
5956 
5957 #undef PUT_TIDVAL_U64
5958 			if ((tidstats->filled &
5959 			     BIT(NL80211_TID_STATS_TXQ_STATS)) &&
5960 			    !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
5961 						   NL80211_TID_STATS_TXQ_STATS))
5962 				goto nla_put_failure;
5963 
5964 			nla_nest_end(msg, tidattr);
5965 		}
5966 
5967 		nla_nest_end(msg, tidsattr);
5968 	}
5969 
5970 	nla_nest_end(msg, sinfoattr);
5971 
5972 	if (sinfo->assoc_req_ies_len &&
5973 	    nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
5974 		    sinfo->assoc_req_ies))
5975 		goto nla_put_failure;
5976 
5977 	cfg80211_sinfo_release_content(sinfo);
5978 	genlmsg_end(msg, hdr);
5979 	return 0;
5980 
5981  nla_put_failure:
5982 	cfg80211_sinfo_release_content(sinfo);
5983 	genlmsg_cancel(msg, hdr);
5984 	return -EMSGSIZE;
5985 }
5986 
5987 static int nl80211_dump_station(struct sk_buff *skb,
5988 				struct netlink_callback *cb)
5989 {
5990 	struct station_info sinfo;
5991 	struct cfg80211_registered_device *rdev;
5992 	struct wireless_dev *wdev;
5993 	u8 mac_addr[ETH_ALEN];
5994 	int sta_idx = cb->args[2];
5995 	int err;
5996 
5997 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
5998 	if (err)
5999 		return err;
6000 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
6001 	__acquire(&rdev->wiphy.mtx);
6002 
6003 	if (!wdev->netdev) {
6004 		err = -EINVAL;
6005 		goto out_err;
6006 	}
6007 
6008 	if (!rdev->ops->dump_station) {
6009 		err = -EOPNOTSUPP;
6010 		goto out_err;
6011 	}
6012 
6013 	while (1) {
6014 		memset(&sinfo, 0, sizeof(sinfo));
6015 		err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
6016 					mac_addr, &sinfo);
6017 		if (err == -ENOENT)
6018 			break;
6019 		if (err)
6020 			goto out_err;
6021 
6022 		if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
6023 				NETLINK_CB(cb->skb).portid,
6024 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
6025 				rdev, wdev->netdev, mac_addr,
6026 				&sinfo) < 0)
6027 			goto out;
6028 
6029 		sta_idx++;
6030 	}
6031 
6032  out:
6033 	cb->args[2] = sta_idx;
6034 	err = skb->len;
6035  out_err:
6036 	wiphy_unlock(&rdev->wiphy);
6037 
6038 	return err;
6039 }
6040 
6041 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
6042 {
6043 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6044 	struct net_device *dev = info->user_ptr[1];
6045 	struct station_info sinfo;
6046 	struct sk_buff *msg;
6047 	u8 *mac_addr = NULL;
6048 	int err;
6049 
6050 	memset(&sinfo, 0, sizeof(sinfo));
6051 
6052 	if (!info->attrs[NL80211_ATTR_MAC])
6053 		return -EINVAL;
6054 
6055 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6056 
6057 	if (!rdev->ops->get_station)
6058 		return -EOPNOTSUPP;
6059 
6060 	err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
6061 	if (err)
6062 		return err;
6063 
6064 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6065 	if (!msg) {
6066 		cfg80211_sinfo_release_content(&sinfo);
6067 		return -ENOMEM;
6068 	}
6069 
6070 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
6071 				 info->snd_portid, info->snd_seq, 0,
6072 				 rdev, dev, mac_addr, &sinfo) < 0) {
6073 		nlmsg_free(msg);
6074 		return -ENOBUFS;
6075 	}
6076 
6077 	return genlmsg_reply(msg, info);
6078 }
6079 
6080 int cfg80211_check_station_change(struct wiphy *wiphy,
6081 				  struct station_parameters *params,
6082 				  enum cfg80211_station_type statype)
6083 {
6084 	if (params->listen_interval != -1 &&
6085 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6086 		return -EINVAL;
6087 
6088 	if (params->support_p2p_ps != -1 &&
6089 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6090 		return -EINVAL;
6091 
6092 	if (params->aid &&
6093 	    !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
6094 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6095 		return -EINVAL;
6096 
6097 	/* When you run into this, adjust the code below for the new flag */
6098 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
6099 
6100 	switch (statype) {
6101 	case CFG80211_STA_MESH_PEER_KERNEL:
6102 	case CFG80211_STA_MESH_PEER_USER:
6103 		/*
6104 		 * No ignoring the TDLS flag here -- the userspace mesh
6105 		 * code doesn't have the bug of including TDLS in the
6106 		 * mask everywhere.
6107 		 */
6108 		if (params->sta_flags_mask &
6109 				~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6110 				  BIT(NL80211_STA_FLAG_MFP) |
6111 				  BIT(NL80211_STA_FLAG_AUTHORIZED)))
6112 			return -EINVAL;
6113 		break;
6114 	case CFG80211_STA_TDLS_PEER_SETUP:
6115 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6116 		if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6117 			return -EINVAL;
6118 		/* ignore since it can't change */
6119 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6120 		break;
6121 	default:
6122 		/* disallow mesh-specific things */
6123 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
6124 			return -EINVAL;
6125 		if (params->local_pm)
6126 			return -EINVAL;
6127 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6128 			return -EINVAL;
6129 	}
6130 
6131 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6132 	    statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
6133 		/* TDLS can't be set, ... */
6134 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
6135 			return -EINVAL;
6136 		/*
6137 		 * ... but don't bother the driver with it. This works around
6138 		 * a hostapd/wpa_supplicant issue -- it always includes the
6139 		 * TLDS_PEER flag in the mask even for AP mode.
6140 		 */
6141 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6142 	}
6143 
6144 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6145 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6146 		/* reject other things that can't change */
6147 		if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
6148 			return -EINVAL;
6149 		if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
6150 			return -EINVAL;
6151 		if (params->supported_rates)
6152 			return -EINVAL;
6153 		if (params->ext_capab || params->ht_capa || params->vht_capa ||
6154 		    params->he_capa)
6155 			return -EINVAL;
6156 	}
6157 
6158 	if (statype != CFG80211_STA_AP_CLIENT &&
6159 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6160 		if (params->vlan)
6161 			return -EINVAL;
6162 	}
6163 
6164 	switch (statype) {
6165 	case CFG80211_STA_AP_MLME_CLIENT:
6166 		/* Use this only for authorizing/unauthorizing a station */
6167 		if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
6168 			return -EOPNOTSUPP;
6169 		break;
6170 	case CFG80211_STA_AP_CLIENT:
6171 	case CFG80211_STA_AP_CLIENT_UNASSOC:
6172 		/* accept only the listed bits */
6173 		if (params->sta_flags_mask &
6174 				~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6175 				  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6176 				  BIT(NL80211_STA_FLAG_ASSOCIATED) |
6177 				  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6178 				  BIT(NL80211_STA_FLAG_WME) |
6179 				  BIT(NL80211_STA_FLAG_MFP)))
6180 			return -EINVAL;
6181 
6182 		/* but authenticated/associated only if driver handles it */
6183 		if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
6184 		    params->sta_flags_mask &
6185 				(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6186 				 BIT(NL80211_STA_FLAG_ASSOCIATED)))
6187 			return -EINVAL;
6188 		break;
6189 	case CFG80211_STA_IBSS:
6190 	case CFG80211_STA_AP_STA:
6191 		/* reject any changes other than AUTHORIZED */
6192 		if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
6193 			return -EINVAL;
6194 		break;
6195 	case CFG80211_STA_TDLS_PEER_SETUP:
6196 		/* reject any changes other than AUTHORIZED or WME */
6197 		if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6198 					       BIT(NL80211_STA_FLAG_WME)))
6199 			return -EINVAL;
6200 		/* force (at least) rates when authorizing */
6201 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
6202 		    !params->supported_rates)
6203 			return -EINVAL;
6204 		break;
6205 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6206 		/* reject any changes */
6207 		return -EINVAL;
6208 	case CFG80211_STA_MESH_PEER_KERNEL:
6209 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6210 			return -EINVAL;
6211 		break;
6212 	case CFG80211_STA_MESH_PEER_USER:
6213 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
6214 		    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
6215 			return -EINVAL;
6216 		break;
6217 	}
6218 
6219 	/*
6220 	 * Older kernel versions ignored this attribute entirely, so don't
6221 	 * reject attempts to update it but mark it as unused instead so the
6222 	 * driver won't look at the data.
6223 	 */
6224 	if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
6225 	    statype != CFG80211_STA_TDLS_PEER_SETUP)
6226 		params->opmode_notif_used = false;
6227 
6228 	return 0;
6229 }
6230 EXPORT_SYMBOL(cfg80211_check_station_change);
6231 
6232 /*
6233  * Get vlan interface making sure it is running and on the right wiphy.
6234  */
6235 static struct net_device *get_vlan(struct genl_info *info,
6236 				   struct cfg80211_registered_device *rdev)
6237 {
6238 	struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
6239 	struct net_device *v;
6240 	int ret;
6241 
6242 	if (!vlanattr)
6243 		return NULL;
6244 
6245 	v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
6246 	if (!v)
6247 		return ERR_PTR(-ENODEV);
6248 
6249 	if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
6250 		ret = -EINVAL;
6251 		goto error;
6252 	}
6253 
6254 	if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
6255 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6256 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
6257 		ret = -EINVAL;
6258 		goto error;
6259 	}
6260 
6261 	if (!netif_running(v)) {
6262 		ret = -ENETDOWN;
6263 		goto error;
6264 	}
6265 
6266 	return v;
6267  error:
6268 	dev_put(v);
6269 	return ERR_PTR(ret);
6270 }
6271 
6272 static const struct nla_policy
6273 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
6274 	[NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
6275 	[NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
6276 };
6277 
6278 static int nl80211_parse_sta_wme(struct genl_info *info,
6279 				 struct station_parameters *params)
6280 {
6281 	struct nlattr *tb[NL80211_STA_WME_MAX + 1];
6282 	struct nlattr *nla;
6283 	int err;
6284 
6285 	/* parse WME attributes if present */
6286 	if (!info->attrs[NL80211_ATTR_STA_WME])
6287 		return 0;
6288 
6289 	nla = info->attrs[NL80211_ATTR_STA_WME];
6290 	err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
6291 					  nl80211_sta_wme_policy,
6292 					  info->extack);
6293 	if (err)
6294 		return err;
6295 
6296 	if (tb[NL80211_STA_WME_UAPSD_QUEUES])
6297 		params->uapsd_queues = nla_get_u8(
6298 			tb[NL80211_STA_WME_UAPSD_QUEUES]);
6299 	if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
6300 		return -EINVAL;
6301 
6302 	if (tb[NL80211_STA_WME_MAX_SP])
6303 		params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
6304 
6305 	if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
6306 		return -EINVAL;
6307 
6308 	params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
6309 
6310 	return 0;
6311 }
6312 
6313 static int nl80211_parse_sta_channel_info(struct genl_info *info,
6314 				      struct station_parameters *params)
6315 {
6316 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
6317 		params->supported_channels =
6318 		     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
6319 		params->supported_channels_len =
6320 		     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
6321 		/*
6322 		 * Need to include at least one (first channel, number of
6323 		 * channels) tuple for each subband (checked in policy),
6324 		 * and must have proper tuples for the rest of the data as well.
6325 		 */
6326 		if (params->supported_channels_len % 2)
6327 			return -EINVAL;
6328 	}
6329 
6330 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
6331 		params->supported_oper_classes =
6332 		 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
6333 		params->supported_oper_classes_len =
6334 		  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
6335 	}
6336 	return 0;
6337 }
6338 
6339 static int nl80211_set_station_tdls(struct genl_info *info,
6340 				    struct station_parameters *params)
6341 {
6342 	int err;
6343 	/* Dummy STA entry gets updated once the peer capabilities are known */
6344 	if (info->attrs[NL80211_ATTR_PEER_AID])
6345 		params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
6346 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
6347 		params->ht_capa =
6348 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
6349 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
6350 		params->vht_capa =
6351 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
6352 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
6353 		params->he_capa =
6354 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6355 		params->he_capa_len =
6356 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6357 	}
6358 
6359 	err = nl80211_parse_sta_channel_info(info, params);
6360 	if (err)
6361 		return err;
6362 
6363 	return nl80211_parse_sta_wme(info, params);
6364 }
6365 
6366 static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
6367 					     struct station_parameters *params)
6368 {
6369 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6370 	int idx;
6371 
6372 	if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
6373 		if (!rdev->ops->set_tx_power ||
6374 		    !wiphy_ext_feature_isset(&rdev->wiphy,
6375 					 NL80211_EXT_FEATURE_STA_TX_PWR))
6376 			return -EOPNOTSUPP;
6377 
6378 		idx = NL80211_ATTR_STA_TX_POWER_SETTING;
6379 		params->txpwr.type = nla_get_u8(info->attrs[idx]);
6380 
6381 		if (params->txpwr.type == NL80211_TX_POWER_LIMITED) {
6382 			idx = NL80211_ATTR_STA_TX_POWER;
6383 
6384 			if (info->attrs[idx])
6385 				params->txpwr.power =
6386 					nla_get_s16(info->attrs[idx]);
6387 			else
6388 				return -EINVAL;
6389 		}
6390 		params->sta_modify_mask |= STATION_PARAM_APPLY_STA_TXPOWER;
6391 	}
6392 
6393 	return 0;
6394 }
6395 
6396 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
6397 {
6398 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6399 	struct net_device *dev = info->user_ptr[1];
6400 	struct station_parameters params;
6401 	u8 *mac_addr;
6402 	int err;
6403 
6404 	memset(&params, 0, sizeof(params));
6405 
6406 	if (!rdev->ops->change_station)
6407 		return -EOPNOTSUPP;
6408 
6409 	/*
6410 	 * AID and listen_interval properties can be set only for unassociated
6411 	 * station. Include these parameters here and will check them in
6412 	 * cfg80211_check_station_change().
6413 	 */
6414 	if (info->attrs[NL80211_ATTR_STA_AID])
6415 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
6416 
6417 	if (info->attrs[NL80211_ATTR_VLAN_ID])
6418 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
6419 
6420 	if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
6421 		params.listen_interval =
6422 		     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
6423 	else
6424 		params.listen_interval = -1;
6425 
6426 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
6427 		params.support_p2p_ps =
6428 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
6429 	else
6430 		params.support_p2p_ps = -1;
6431 
6432 	if (!info->attrs[NL80211_ATTR_MAC])
6433 		return -EINVAL;
6434 
6435 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6436 
6437 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
6438 		params.supported_rates =
6439 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
6440 		params.supported_rates_len =
6441 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
6442 	}
6443 
6444 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
6445 		params.capability =
6446 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
6447 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
6448 	}
6449 
6450 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
6451 		params.ext_capab =
6452 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
6453 		params.ext_capab_len =
6454 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
6455 	}
6456 
6457 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
6458 		return -EINVAL;
6459 
6460 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
6461 		params.plink_action =
6462 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
6463 
6464 	if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
6465 		params.plink_state =
6466 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
6467 		if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
6468 			params.peer_aid = nla_get_u16(
6469 				info->attrs[NL80211_ATTR_MESH_PEER_AID]);
6470 		params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
6471 	}
6472 
6473 	if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
6474 		params.local_pm = nla_get_u32(
6475 			info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
6476 
6477 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
6478 		params.opmode_notif_used = true;
6479 		params.opmode_notif =
6480 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
6481 	}
6482 
6483 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
6484 		params.he_6ghz_capa =
6485 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
6486 
6487 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
6488 		params.airtime_weight =
6489 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
6490 
6491 	if (params.airtime_weight &&
6492 	    !wiphy_ext_feature_isset(&rdev->wiphy,
6493 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
6494 		return -EOPNOTSUPP;
6495 
6496 	err = nl80211_parse_sta_txpower_setting(info, &params);
6497 	if (err)
6498 		return err;
6499 
6500 	/* Include parameters for TDLS peer (will check later) */
6501 	err = nl80211_set_station_tdls(info, &params);
6502 	if (err)
6503 		return err;
6504 
6505 	params.vlan = get_vlan(info, rdev);
6506 	if (IS_ERR(params.vlan))
6507 		return PTR_ERR(params.vlan);
6508 
6509 	switch (dev->ieee80211_ptr->iftype) {
6510 	case NL80211_IFTYPE_AP:
6511 	case NL80211_IFTYPE_AP_VLAN:
6512 	case NL80211_IFTYPE_P2P_GO:
6513 	case NL80211_IFTYPE_P2P_CLIENT:
6514 	case NL80211_IFTYPE_STATION:
6515 	case NL80211_IFTYPE_ADHOC:
6516 	case NL80211_IFTYPE_MESH_POINT:
6517 		break;
6518 	default:
6519 		err = -EOPNOTSUPP;
6520 		goto out_put_vlan;
6521 	}
6522 
6523 	/* driver will call cfg80211_check_station_change() */
6524 	err = rdev_change_station(rdev, dev, mac_addr, &params);
6525 
6526  out_put_vlan:
6527 	if (params.vlan)
6528 		dev_put(params.vlan);
6529 
6530 	return err;
6531 }
6532 
6533 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
6534 {
6535 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6536 	int err;
6537 	struct net_device *dev = info->user_ptr[1];
6538 	struct station_parameters params;
6539 	u8 *mac_addr = NULL;
6540 	u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6541 			 BIT(NL80211_STA_FLAG_ASSOCIATED);
6542 
6543 	memset(&params, 0, sizeof(params));
6544 
6545 	if (!rdev->ops->add_station)
6546 		return -EOPNOTSUPP;
6547 
6548 	if (!info->attrs[NL80211_ATTR_MAC])
6549 		return -EINVAL;
6550 
6551 	if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
6552 		return -EINVAL;
6553 
6554 	if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
6555 		return -EINVAL;
6556 
6557 	if (!info->attrs[NL80211_ATTR_STA_AID] &&
6558 	    !info->attrs[NL80211_ATTR_PEER_AID])
6559 		return -EINVAL;
6560 
6561 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6562 	params.supported_rates =
6563 		nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
6564 	params.supported_rates_len =
6565 		nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
6566 	params.listen_interval =
6567 		nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
6568 
6569 	if (info->attrs[NL80211_ATTR_VLAN_ID])
6570 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
6571 
6572 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
6573 		params.support_p2p_ps =
6574 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
6575 	} else {
6576 		/*
6577 		 * if not specified, assume it's supported for P2P GO interface,
6578 		 * and is NOT supported for AP interface
6579 		 */
6580 		params.support_p2p_ps =
6581 			dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
6582 	}
6583 
6584 	if (info->attrs[NL80211_ATTR_PEER_AID])
6585 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
6586 	else
6587 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
6588 
6589 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
6590 		params.capability =
6591 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
6592 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
6593 	}
6594 
6595 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
6596 		params.ext_capab =
6597 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
6598 		params.ext_capab_len =
6599 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
6600 	}
6601 
6602 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
6603 		params.ht_capa =
6604 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
6605 
6606 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
6607 		params.vht_capa =
6608 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
6609 
6610 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
6611 		params.he_capa =
6612 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6613 		params.he_capa_len =
6614 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6615 	}
6616 
6617 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
6618 		params.he_6ghz_capa =
6619 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
6620 
6621 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
6622 		params.opmode_notif_used = true;
6623 		params.opmode_notif =
6624 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
6625 	}
6626 
6627 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
6628 		params.plink_action =
6629 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
6630 
6631 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
6632 		params.airtime_weight =
6633 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
6634 
6635 	if (params.airtime_weight &&
6636 	    !wiphy_ext_feature_isset(&rdev->wiphy,
6637 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
6638 		return -EOPNOTSUPP;
6639 
6640 	err = nl80211_parse_sta_txpower_setting(info, &params);
6641 	if (err)
6642 		return err;
6643 
6644 	err = nl80211_parse_sta_channel_info(info, &params);
6645 	if (err)
6646 		return err;
6647 
6648 	err = nl80211_parse_sta_wme(info, &params);
6649 	if (err)
6650 		return err;
6651 
6652 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
6653 		return -EINVAL;
6654 
6655 	/* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
6656 	 * as userspace might just pass through the capabilities from the IEs
6657 	 * directly, rather than enforcing this restriction and returning an
6658 	 * error in this case.
6659 	 */
6660 	if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
6661 		params.ht_capa = NULL;
6662 		params.vht_capa = NULL;
6663 
6664 		/* HE requires WME */
6665 		if (params.he_capa_len || params.he_6ghz_capa)
6666 			return -EINVAL;
6667 	}
6668 
6669 	/* Ensure that HT/VHT capabilities are not set for 6 GHz HE STA */
6670 	if (params.he_6ghz_capa && (params.ht_capa || params.vht_capa))
6671 		return -EINVAL;
6672 
6673 	/* When you run into this, adjust the code below for the new flag */
6674 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
6675 
6676 	switch (dev->ieee80211_ptr->iftype) {
6677 	case NL80211_IFTYPE_AP:
6678 	case NL80211_IFTYPE_AP_VLAN:
6679 	case NL80211_IFTYPE_P2P_GO:
6680 		/* ignore WME attributes if iface/sta is not capable */
6681 		if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
6682 		    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
6683 			params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6684 
6685 		/* TDLS peers cannot be added */
6686 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
6687 		    info->attrs[NL80211_ATTR_PEER_AID])
6688 			return -EINVAL;
6689 		/* but don't bother the driver with it */
6690 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6691 
6692 		/* allow authenticated/associated only if driver handles it */
6693 		if (!(rdev->wiphy.features &
6694 				NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
6695 		    params.sta_flags_mask & auth_assoc)
6696 			return -EINVAL;
6697 
6698 		/* Older userspace, or userspace wanting to be compatible with
6699 		 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
6700 		 * and assoc flags in the mask, but assumes the station will be
6701 		 * added as associated anyway since this was the required driver
6702 		 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
6703 		 * introduced.
6704 		 * In order to not bother drivers with this quirk in the API
6705 		 * set the flags in both the mask and set for new stations in
6706 		 * this case.
6707 		 */
6708 		if (!(params.sta_flags_mask & auth_assoc)) {
6709 			params.sta_flags_mask |= auth_assoc;
6710 			params.sta_flags_set |= auth_assoc;
6711 		}
6712 
6713 		/* must be last in here for error handling */
6714 		params.vlan = get_vlan(info, rdev);
6715 		if (IS_ERR(params.vlan))
6716 			return PTR_ERR(params.vlan);
6717 		break;
6718 	case NL80211_IFTYPE_MESH_POINT:
6719 		/* ignore uAPSD data */
6720 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6721 
6722 		/* associated is disallowed */
6723 		if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
6724 			return -EINVAL;
6725 		/* TDLS peers cannot be added */
6726 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
6727 		    info->attrs[NL80211_ATTR_PEER_AID])
6728 			return -EINVAL;
6729 		break;
6730 	case NL80211_IFTYPE_STATION:
6731 	case NL80211_IFTYPE_P2P_CLIENT:
6732 		/* ignore uAPSD data */
6733 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6734 
6735 		/* these are disallowed */
6736 		if (params.sta_flags_mask &
6737 				(BIT(NL80211_STA_FLAG_ASSOCIATED) |
6738 				 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
6739 			return -EINVAL;
6740 		/* Only TDLS peers can be added */
6741 		if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6742 			return -EINVAL;
6743 		/* Can only add if TDLS ... */
6744 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
6745 			return -EOPNOTSUPP;
6746 		/* ... with external setup is supported */
6747 		if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
6748 			return -EOPNOTSUPP;
6749 		/*
6750 		 * Older wpa_supplicant versions always mark the TDLS peer
6751 		 * as authorized, but it shouldn't yet be.
6752 		 */
6753 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
6754 		break;
6755 	default:
6756 		return -EOPNOTSUPP;
6757 	}
6758 
6759 	/* be aware of params.vlan when changing code here */
6760 
6761 	err = rdev_add_station(rdev, dev, mac_addr, &params);
6762 
6763 	if (params.vlan)
6764 		dev_put(params.vlan);
6765 	return err;
6766 }
6767 
6768 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
6769 {
6770 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6771 	struct net_device *dev = info->user_ptr[1];
6772 	struct station_del_parameters params;
6773 
6774 	memset(&params, 0, sizeof(params));
6775 
6776 	if (info->attrs[NL80211_ATTR_MAC])
6777 		params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
6778 
6779 	switch (dev->ieee80211_ptr->iftype) {
6780 	case NL80211_IFTYPE_AP:
6781 	case NL80211_IFTYPE_AP_VLAN:
6782 	case NL80211_IFTYPE_MESH_POINT:
6783 	case NL80211_IFTYPE_P2P_GO:
6784 		/* always accept these */
6785 		break;
6786 	case NL80211_IFTYPE_ADHOC:
6787 		/* conditionally accept */
6788 		if (wiphy_ext_feature_isset(&rdev->wiphy,
6789 					    NL80211_EXT_FEATURE_DEL_IBSS_STA))
6790 			break;
6791 		return -EINVAL;
6792 	default:
6793 		return -EINVAL;
6794 	}
6795 
6796 	if (!rdev->ops->del_station)
6797 		return -EOPNOTSUPP;
6798 
6799 	if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
6800 		params.subtype =
6801 			nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
6802 		if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
6803 		    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
6804 			return -EINVAL;
6805 	} else {
6806 		/* Default to Deauthentication frame */
6807 		params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
6808 	}
6809 
6810 	if (info->attrs[NL80211_ATTR_REASON_CODE]) {
6811 		params.reason_code =
6812 			nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6813 		if (params.reason_code == 0)
6814 			return -EINVAL; /* 0 is reserved */
6815 	} else {
6816 		/* Default to reason code 2 */
6817 		params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
6818 	}
6819 
6820 	return rdev_del_station(rdev, dev, &params);
6821 }
6822 
6823 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
6824 				int flags, struct net_device *dev,
6825 				u8 *dst, u8 *next_hop,
6826 				struct mpath_info *pinfo)
6827 {
6828 	void *hdr;
6829 	struct nlattr *pinfoattr;
6830 
6831 	hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
6832 	if (!hdr)
6833 		return -1;
6834 
6835 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6836 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
6837 	    nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
6838 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
6839 		goto nla_put_failure;
6840 
6841 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
6842 	if (!pinfoattr)
6843 		goto nla_put_failure;
6844 	if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
6845 	    nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
6846 			pinfo->frame_qlen))
6847 		goto nla_put_failure;
6848 	if (((pinfo->filled & MPATH_INFO_SN) &&
6849 	     nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
6850 	    ((pinfo->filled & MPATH_INFO_METRIC) &&
6851 	     nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
6852 			 pinfo->metric)) ||
6853 	    ((pinfo->filled & MPATH_INFO_EXPTIME) &&
6854 	     nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
6855 			 pinfo->exptime)) ||
6856 	    ((pinfo->filled & MPATH_INFO_FLAGS) &&
6857 	     nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
6858 			pinfo->flags)) ||
6859 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
6860 	     nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
6861 			 pinfo->discovery_timeout)) ||
6862 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
6863 	     nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
6864 			pinfo->discovery_retries)) ||
6865 	    ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
6866 	     nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
6867 			pinfo->hop_count)) ||
6868 	    ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
6869 	     nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
6870 			 pinfo->path_change_count)))
6871 		goto nla_put_failure;
6872 
6873 	nla_nest_end(msg, pinfoattr);
6874 
6875 	genlmsg_end(msg, hdr);
6876 	return 0;
6877 
6878  nla_put_failure:
6879 	genlmsg_cancel(msg, hdr);
6880 	return -EMSGSIZE;
6881 }
6882 
6883 static int nl80211_dump_mpath(struct sk_buff *skb,
6884 			      struct netlink_callback *cb)
6885 {
6886 	struct mpath_info pinfo;
6887 	struct cfg80211_registered_device *rdev;
6888 	struct wireless_dev *wdev;
6889 	u8 dst[ETH_ALEN];
6890 	u8 next_hop[ETH_ALEN];
6891 	int path_idx = cb->args[2];
6892 	int err;
6893 
6894 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
6895 	if (err)
6896 		return err;
6897 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
6898 	__acquire(&rdev->wiphy.mtx);
6899 
6900 	if (!rdev->ops->dump_mpath) {
6901 		err = -EOPNOTSUPP;
6902 		goto out_err;
6903 	}
6904 
6905 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
6906 		err = -EOPNOTSUPP;
6907 		goto out_err;
6908 	}
6909 
6910 	while (1) {
6911 		err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
6912 				      next_hop, &pinfo);
6913 		if (err == -ENOENT)
6914 			break;
6915 		if (err)
6916 			goto out_err;
6917 
6918 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
6919 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
6920 				       wdev->netdev, dst, next_hop,
6921 				       &pinfo) < 0)
6922 			goto out;
6923 
6924 		path_idx++;
6925 	}
6926 
6927  out:
6928 	cb->args[2] = path_idx;
6929 	err = skb->len;
6930  out_err:
6931 	wiphy_unlock(&rdev->wiphy);
6932 	return err;
6933 }
6934 
6935 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
6936 {
6937 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6938 	int err;
6939 	struct net_device *dev = info->user_ptr[1];
6940 	struct mpath_info pinfo;
6941 	struct sk_buff *msg;
6942 	u8 *dst = NULL;
6943 	u8 next_hop[ETH_ALEN];
6944 
6945 	memset(&pinfo, 0, sizeof(pinfo));
6946 
6947 	if (!info->attrs[NL80211_ATTR_MAC])
6948 		return -EINVAL;
6949 
6950 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6951 
6952 	if (!rdev->ops->get_mpath)
6953 		return -EOPNOTSUPP;
6954 
6955 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6956 		return -EOPNOTSUPP;
6957 
6958 	err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
6959 	if (err)
6960 		return err;
6961 
6962 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6963 	if (!msg)
6964 		return -ENOMEM;
6965 
6966 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
6967 				 dev, dst, next_hop, &pinfo) < 0) {
6968 		nlmsg_free(msg);
6969 		return -ENOBUFS;
6970 	}
6971 
6972 	return genlmsg_reply(msg, info);
6973 }
6974 
6975 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
6976 {
6977 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6978 	struct net_device *dev = info->user_ptr[1];
6979 	u8 *dst = NULL;
6980 	u8 *next_hop = NULL;
6981 
6982 	if (!info->attrs[NL80211_ATTR_MAC])
6983 		return -EINVAL;
6984 
6985 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
6986 		return -EINVAL;
6987 
6988 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6989 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
6990 
6991 	if (!rdev->ops->change_mpath)
6992 		return -EOPNOTSUPP;
6993 
6994 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6995 		return -EOPNOTSUPP;
6996 
6997 	return rdev_change_mpath(rdev, dev, dst, next_hop);
6998 }
6999 
7000 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
7001 {
7002 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7003 	struct net_device *dev = info->user_ptr[1];
7004 	u8 *dst = NULL;
7005 	u8 *next_hop = NULL;
7006 
7007 	if (!info->attrs[NL80211_ATTR_MAC])
7008 		return -EINVAL;
7009 
7010 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7011 		return -EINVAL;
7012 
7013 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7014 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7015 
7016 	if (!rdev->ops->add_mpath)
7017 		return -EOPNOTSUPP;
7018 
7019 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7020 		return -EOPNOTSUPP;
7021 
7022 	return rdev_add_mpath(rdev, dev, dst, next_hop);
7023 }
7024 
7025 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
7026 {
7027 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7028 	struct net_device *dev = info->user_ptr[1];
7029 	u8 *dst = NULL;
7030 
7031 	if (info->attrs[NL80211_ATTR_MAC])
7032 		dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7033 
7034 	if (!rdev->ops->del_mpath)
7035 		return -EOPNOTSUPP;
7036 
7037 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7038 		return -EOPNOTSUPP;
7039 
7040 	return rdev_del_mpath(rdev, dev, dst);
7041 }
7042 
7043 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
7044 {
7045 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7046 	int err;
7047 	struct net_device *dev = info->user_ptr[1];
7048 	struct mpath_info pinfo;
7049 	struct sk_buff *msg;
7050 	u8 *dst = NULL;
7051 	u8 mpp[ETH_ALEN];
7052 
7053 	memset(&pinfo, 0, sizeof(pinfo));
7054 
7055 	if (!info->attrs[NL80211_ATTR_MAC])
7056 		return -EINVAL;
7057 
7058 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7059 
7060 	if (!rdev->ops->get_mpp)
7061 		return -EOPNOTSUPP;
7062 
7063 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7064 		return -EOPNOTSUPP;
7065 
7066 	err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
7067 	if (err)
7068 		return err;
7069 
7070 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7071 	if (!msg)
7072 		return -ENOMEM;
7073 
7074 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7075 			       dev, dst, mpp, &pinfo) < 0) {
7076 		nlmsg_free(msg);
7077 		return -ENOBUFS;
7078 	}
7079 
7080 	return genlmsg_reply(msg, info);
7081 }
7082 
7083 static int nl80211_dump_mpp(struct sk_buff *skb,
7084 			    struct netlink_callback *cb)
7085 {
7086 	struct mpath_info pinfo;
7087 	struct cfg80211_registered_device *rdev;
7088 	struct wireless_dev *wdev;
7089 	u8 dst[ETH_ALEN];
7090 	u8 mpp[ETH_ALEN];
7091 	int path_idx = cb->args[2];
7092 	int err;
7093 
7094 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
7095 	if (err)
7096 		return err;
7097 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
7098 	__acquire(&rdev->wiphy.mtx);
7099 
7100 	if (!rdev->ops->dump_mpp) {
7101 		err = -EOPNOTSUPP;
7102 		goto out_err;
7103 	}
7104 
7105 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7106 		err = -EOPNOTSUPP;
7107 		goto out_err;
7108 	}
7109 
7110 	while (1) {
7111 		err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
7112 				    mpp, &pinfo);
7113 		if (err == -ENOENT)
7114 			break;
7115 		if (err)
7116 			goto out_err;
7117 
7118 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7119 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
7120 				       wdev->netdev, dst, mpp,
7121 				       &pinfo) < 0)
7122 			goto out;
7123 
7124 		path_idx++;
7125 	}
7126 
7127  out:
7128 	cb->args[2] = path_idx;
7129 	err = skb->len;
7130  out_err:
7131 	wiphy_unlock(&rdev->wiphy);
7132 	return err;
7133 }
7134 
7135 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
7136 {
7137 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7138 	struct net_device *dev = info->user_ptr[1];
7139 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7140 	struct bss_parameters params;
7141 	int err;
7142 
7143 	memset(&params, 0, sizeof(params));
7144 	/* default to not changing parameters */
7145 	params.use_cts_prot = -1;
7146 	params.use_short_preamble = -1;
7147 	params.use_short_slot_time = -1;
7148 	params.ap_isolate = -1;
7149 	params.ht_opmode = -1;
7150 	params.p2p_ctwindow = -1;
7151 	params.p2p_opp_ps = -1;
7152 
7153 	if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
7154 		params.use_cts_prot =
7155 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
7156 	if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
7157 		params.use_short_preamble =
7158 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
7159 	if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
7160 		params.use_short_slot_time =
7161 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
7162 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
7163 		params.basic_rates =
7164 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7165 		params.basic_rates_len =
7166 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7167 	}
7168 	if (info->attrs[NL80211_ATTR_AP_ISOLATE])
7169 		params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
7170 	if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
7171 		params.ht_opmode =
7172 			nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
7173 
7174 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
7175 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
7176 			return -EINVAL;
7177 		params.p2p_ctwindow =
7178 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
7179 		if (params.p2p_ctwindow != 0 &&
7180 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
7181 			return -EINVAL;
7182 	}
7183 
7184 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
7185 		u8 tmp;
7186 
7187 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
7188 			return -EINVAL;
7189 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
7190 		params.p2p_opp_ps = tmp;
7191 		if (params.p2p_opp_ps &&
7192 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
7193 			return -EINVAL;
7194 	}
7195 
7196 	if (!rdev->ops->change_bss)
7197 		return -EOPNOTSUPP;
7198 
7199 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
7200 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
7201 		return -EOPNOTSUPP;
7202 
7203 	wdev_lock(wdev);
7204 	err = rdev_change_bss(rdev, dev, &params);
7205 	wdev_unlock(wdev);
7206 
7207 	return err;
7208 }
7209 
7210 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
7211 {
7212 	char *data = NULL;
7213 	bool is_indoor;
7214 	enum nl80211_user_reg_hint_type user_reg_hint_type;
7215 	u32 owner_nlportid;
7216 
7217 	/*
7218 	 * You should only get this when cfg80211 hasn't yet initialized
7219 	 * completely when built-in to the kernel right between the time
7220 	 * window between nl80211_init() and regulatory_init(), if that is
7221 	 * even possible.
7222 	 */
7223 	if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
7224 		return -EINPROGRESS;
7225 
7226 	if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
7227 		user_reg_hint_type =
7228 		  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
7229 	else
7230 		user_reg_hint_type = NL80211_USER_REG_HINT_USER;
7231 
7232 	switch (user_reg_hint_type) {
7233 	case NL80211_USER_REG_HINT_USER:
7234 	case NL80211_USER_REG_HINT_CELL_BASE:
7235 		if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
7236 			return -EINVAL;
7237 
7238 		data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
7239 		return regulatory_hint_user(data, user_reg_hint_type);
7240 	case NL80211_USER_REG_HINT_INDOOR:
7241 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
7242 			owner_nlportid = info->snd_portid;
7243 			is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
7244 		} else {
7245 			owner_nlportid = 0;
7246 			is_indoor = true;
7247 		}
7248 
7249 		return regulatory_hint_indoor(is_indoor, owner_nlportid);
7250 	default:
7251 		return -EINVAL;
7252 	}
7253 }
7254 
7255 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
7256 {
7257 	return reg_reload_regdb();
7258 }
7259 
7260 static int nl80211_get_mesh_config(struct sk_buff *skb,
7261 				   struct genl_info *info)
7262 {
7263 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7264 	struct net_device *dev = info->user_ptr[1];
7265 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7266 	struct mesh_config cur_params;
7267 	int err = 0;
7268 	void *hdr;
7269 	struct nlattr *pinfoattr;
7270 	struct sk_buff *msg;
7271 
7272 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
7273 		return -EOPNOTSUPP;
7274 
7275 	if (!rdev->ops->get_mesh_config)
7276 		return -EOPNOTSUPP;
7277 
7278 	wdev_lock(wdev);
7279 	/* If not connected, get default parameters */
7280 	if (!wdev->mesh_id_len)
7281 		memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
7282 	else
7283 		err = rdev_get_mesh_config(rdev, dev, &cur_params);
7284 	wdev_unlock(wdev);
7285 
7286 	if (err)
7287 		return err;
7288 
7289 	/* Draw up a netlink message to send back */
7290 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7291 	if (!msg)
7292 		return -ENOMEM;
7293 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7294 			     NL80211_CMD_GET_MESH_CONFIG);
7295 	if (!hdr)
7296 		goto out;
7297 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
7298 	if (!pinfoattr)
7299 		goto nla_put_failure;
7300 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7301 	    nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
7302 			cur_params.dot11MeshRetryTimeout) ||
7303 	    nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
7304 			cur_params.dot11MeshConfirmTimeout) ||
7305 	    nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
7306 			cur_params.dot11MeshHoldingTimeout) ||
7307 	    nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
7308 			cur_params.dot11MeshMaxPeerLinks) ||
7309 	    nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
7310 		       cur_params.dot11MeshMaxRetries) ||
7311 	    nla_put_u8(msg, NL80211_MESHCONF_TTL,
7312 		       cur_params.dot11MeshTTL) ||
7313 	    nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
7314 		       cur_params.element_ttl) ||
7315 	    nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
7316 		       cur_params.auto_open_plinks) ||
7317 	    nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
7318 			cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
7319 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
7320 		       cur_params.dot11MeshHWMPmaxPREQretries) ||
7321 	    nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
7322 			cur_params.path_refresh_time) ||
7323 	    nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
7324 			cur_params.min_discovery_timeout) ||
7325 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
7326 			cur_params.dot11MeshHWMPactivePathTimeout) ||
7327 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
7328 			cur_params.dot11MeshHWMPpreqMinInterval) ||
7329 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
7330 			cur_params.dot11MeshHWMPperrMinInterval) ||
7331 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
7332 			cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
7333 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
7334 		       cur_params.dot11MeshHWMPRootMode) ||
7335 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
7336 			cur_params.dot11MeshHWMPRannInterval) ||
7337 	    nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
7338 		       cur_params.dot11MeshGateAnnouncementProtocol) ||
7339 	    nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
7340 		       cur_params.dot11MeshForwarding) ||
7341 	    nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
7342 			cur_params.rssi_threshold) ||
7343 	    nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
7344 			cur_params.ht_opmode) ||
7345 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
7346 			cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
7347 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
7348 			cur_params.dot11MeshHWMProotInterval) ||
7349 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
7350 			cur_params.dot11MeshHWMPconfirmationInterval) ||
7351 	    nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
7352 			cur_params.power_mode) ||
7353 	    nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
7354 			cur_params.dot11MeshAwakeWindowDuration) ||
7355 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
7356 			cur_params.plink_timeout) ||
7357 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
7358 		       cur_params.dot11MeshConnectedToMeshGate) ||
7359 	    nla_put_u8(msg, NL80211_MESHCONF_NOLEARN,
7360 		       cur_params.dot11MeshNolearn) ||
7361 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_AS,
7362 		       cur_params.dot11MeshConnectedToAuthServer))
7363 		goto nla_put_failure;
7364 	nla_nest_end(msg, pinfoattr);
7365 	genlmsg_end(msg, hdr);
7366 	return genlmsg_reply(msg, info);
7367 
7368  nla_put_failure:
7369  out:
7370 	nlmsg_free(msg);
7371 	return -ENOBUFS;
7372 }
7373 
7374 static const struct nla_policy
7375 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
7376 	[NL80211_MESHCONF_RETRY_TIMEOUT] =
7377 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
7378 	[NL80211_MESHCONF_CONFIRM_TIMEOUT] =
7379 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
7380 	[NL80211_MESHCONF_HOLDING_TIMEOUT] =
7381 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
7382 	[NL80211_MESHCONF_MAX_PEER_LINKS] =
7383 		NLA_POLICY_RANGE(NLA_U16, 0, 255),
7384 	[NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
7385 	[NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
7386 	[NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
7387 	[NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
7388 	[NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
7389 		NLA_POLICY_RANGE(NLA_U32, 1, 255),
7390 	[NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
7391 	[NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
7392 	[NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
7393 	[NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
7394 	[NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
7395 		NLA_POLICY_MIN(NLA_U16, 1),
7396 	[NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
7397 		NLA_POLICY_MIN(NLA_U16, 1),
7398 	[NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
7399 		NLA_POLICY_MIN(NLA_U16, 1),
7400 	[NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
7401 	[NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
7402 		NLA_POLICY_MIN(NLA_U16, 1),
7403 	[NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
7404 	[NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
7405 	[NL80211_MESHCONF_RSSI_THRESHOLD] =
7406 		NLA_POLICY_RANGE(NLA_S32, -255, 0),
7407 	[NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
7408 	[NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
7409 	[NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
7410 		NLA_POLICY_MIN(NLA_U16, 1),
7411 	[NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
7412 		NLA_POLICY_MIN(NLA_U16, 1),
7413 	[NL80211_MESHCONF_POWER_MODE] =
7414 		NLA_POLICY_RANGE(NLA_U32,
7415 				 NL80211_MESH_POWER_ACTIVE,
7416 				 NL80211_MESH_POWER_MAX),
7417 	[NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
7418 	[NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
7419 	[NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
7420 	[NL80211_MESHCONF_NOLEARN] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
7421 	[NL80211_MESHCONF_CONNECTED_TO_AS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
7422 };
7423 
7424 static const struct nla_policy
7425 	nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
7426 	[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
7427 	[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
7428 	[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
7429 	[NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
7430 	[NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
7431 	[NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
7432 	[NL80211_MESH_SETUP_IE] =
7433 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
7434 				       IEEE80211_MAX_DATA_LEN),
7435 	[NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
7436 };
7437 
7438 static int nl80211_parse_mesh_config(struct genl_info *info,
7439 				     struct mesh_config *cfg,
7440 				     u32 *mask_out)
7441 {
7442 	struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
7443 	u32 mask = 0;
7444 	u16 ht_opmode;
7445 
7446 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)	\
7447 do {									\
7448 	if (tb[attr]) {							\
7449 		cfg->param = fn(tb[attr]);				\
7450 		mask |= BIT((attr) - 1);				\
7451 	}								\
7452 } while (0)
7453 
7454 	if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
7455 		return -EINVAL;
7456 	if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
7457 		return -EINVAL;
7458 
7459 	/* This makes sure that there aren't more than 32 mesh config
7460 	 * parameters (otherwise our bitfield scheme would not work.) */
7461 	BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
7462 
7463 	/* Fill in the params struct */
7464 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
7465 				  NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
7466 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
7467 				  NL80211_MESHCONF_CONFIRM_TIMEOUT,
7468 				  nla_get_u16);
7469 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
7470 				  NL80211_MESHCONF_HOLDING_TIMEOUT,
7471 				  nla_get_u16);
7472 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
7473 				  NL80211_MESHCONF_MAX_PEER_LINKS,
7474 				  nla_get_u16);
7475 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
7476 				  NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
7477 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
7478 				  NL80211_MESHCONF_TTL, nla_get_u8);
7479 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
7480 				  NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
7481 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
7482 				  NL80211_MESHCONF_AUTO_OPEN_PLINKS,
7483 				  nla_get_u8);
7484 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
7485 				  mask,
7486 				  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
7487 				  nla_get_u32);
7488 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
7489 				  NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
7490 				  nla_get_u8);
7491 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
7492 				  NL80211_MESHCONF_PATH_REFRESH_TIME,
7493 				  nla_get_u32);
7494 	if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
7495 	    (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
7496 		return -EINVAL;
7497 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
7498 				  NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
7499 				  nla_get_u16);
7500 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
7501 				  mask,
7502 				  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
7503 				  nla_get_u32);
7504 	if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
7505 	    (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
7506 	     cfg->dot11MeshHWMPactivePathTimeout > 65535))
7507 		return -EINVAL;
7508 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
7509 				  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
7510 				  nla_get_u16);
7511 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
7512 				  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
7513 				  nla_get_u16);
7514 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
7515 				  dot11MeshHWMPnetDiameterTraversalTime, mask,
7516 				  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
7517 				  nla_get_u16);
7518 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
7519 				  NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
7520 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
7521 				  NL80211_MESHCONF_HWMP_RANN_INTERVAL,
7522 				  nla_get_u16);
7523 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
7524 				  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
7525 				  nla_get_u8);
7526 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
7527 				  NL80211_MESHCONF_FORWARDING, nla_get_u8);
7528 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
7529 				  NL80211_MESHCONF_RSSI_THRESHOLD,
7530 				  nla_get_s32);
7531 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
7532 				  NL80211_MESHCONF_CONNECTED_TO_GATE,
7533 				  nla_get_u8);
7534 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToAuthServer, mask,
7535 				  NL80211_MESHCONF_CONNECTED_TO_AS,
7536 				  nla_get_u8);
7537 	/*
7538 	 * Check HT operation mode based on
7539 	 * IEEE 802.11-2016 9.4.2.57 HT Operation element.
7540 	 */
7541 	if (tb[NL80211_MESHCONF_HT_OPMODE]) {
7542 		ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
7543 
7544 		if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
7545 				  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
7546 				  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
7547 			return -EINVAL;
7548 
7549 		/* NON_HT_STA bit is reserved, but some programs set it */
7550 		ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
7551 
7552 		cfg->ht_opmode = ht_opmode;
7553 		mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
7554 	}
7555 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
7556 				  dot11MeshHWMPactivePathToRootTimeout, mask,
7557 				  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
7558 				  nla_get_u32);
7559 	if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
7560 	    (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
7561 	     cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
7562 		return -EINVAL;
7563 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
7564 				  NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
7565 				  nla_get_u16);
7566 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
7567 				  mask,
7568 				  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
7569 				  nla_get_u16);
7570 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
7571 				  NL80211_MESHCONF_POWER_MODE, nla_get_u32);
7572 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
7573 				  NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
7574 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
7575 				  NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
7576 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNolearn, mask,
7577 				  NL80211_MESHCONF_NOLEARN, nla_get_u8);
7578 	if (mask_out)
7579 		*mask_out = mask;
7580 
7581 	return 0;
7582 
7583 #undef FILL_IN_MESH_PARAM_IF_SET
7584 }
7585 
7586 static int nl80211_parse_mesh_setup(struct genl_info *info,
7587 				     struct mesh_setup *setup)
7588 {
7589 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7590 	struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
7591 
7592 	if (!info->attrs[NL80211_ATTR_MESH_SETUP])
7593 		return -EINVAL;
7594 	if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
7595 		return -EINVAL;
7596 
7597 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
7598 		setup->sync_method =
7599 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
7600 		 IEEE80211_SYNC_METHOD_VENDOR :
7601 		 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
7602 
7603 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
7604 		setup->path_sel_proto =
7605 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
7606 		 IEEE80211_PATH_PROTOCOL_VENDOR :
7607 		 IEEE80211_PATH_PROTOCOL_HWMP;
7608 
7609 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
7610 		setup->path_metric =
7611 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
7612 		 IEEE80211_PATH_METRIC_VENDOR :
7613 		 IEEE80211_PATH_METRIC_AIRTIME;
7614 
7615 	if (tb[NL80211_MESH_SETUP_IE]) {
7616 		struct nlattr *ieattr =
7617 			tb[NL80211_MESH_SETUP_IE];
7618 		setup->ie = nla_data(ieattr);
7619 		setup->ie_len = nla_len(ieattr);
7620 	}
7621 	if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
7622 	    !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
7623 		return -EINVAL;
7624 	setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
7625 	setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
7626 	setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
7627 	if (setup->is_secure)
7628 		setup->user_mpm = true;
7629 
7630 	if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
7631 		if (!setup->user_mpm)
7632 			return -EINVAL;
7633 		setup->auth_id =
7634 			nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
7635 	}
7636 
7637 	return 0;
7638 }
7639 
7640 static int nl80211_update_mesh_config(struct sk_buff *skb,
7641 				      struct genl_info *info)
7642 {
7643 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7644 	struct net_device *dev = info->user_ptr[1];
7645 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7646 	struct mesh_config cfg;
7647 	u32 mask;
7648 	int err;
7649 
7650 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
7651 		return -EOPNOTSUPP;
7652 
7653 	if (!rdev->ops->update_mesh_config)
7654 		return -EOPNOTSUPP;
7655 
7656 	err = nl80211_parse_mesh_config(info, &cfg, &mask);
7657 	if (err)
7658 		return err;
7659 
7660 	wdev_lock(wdev);
7661 	if (!wdev->mesh_id_len)
7662 		err = -ENOLINK;
7663 
7664 	if (!err)
7665 		err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
7666 
7667 	wdev_unlock(wdev);
7668 
7669 	return err;
7670 }
7671 
7672 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
7673 			      struct sk_buff *msg)
7674 {
7675 	struct nlattr *nl_reg_rules;
7676 	unsigned int i;
7677 
7678 	if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
7679 	    (regdom->dfs_region &&
7680 	     nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
7681 		goto nla_put_failure;
7682 
7683 	nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
7684 	if (!nl_reg_rules)
7685 		goto nla_put_failure;
7686 
7687 	for (i = 0; i < regdom->n_reg_rules; i++) {
7688 		struct nlattr *nl_reg_rule;
7689 		const struct ieee80211_reg_rule *reg_rule;
7690 		const struct ieee80211_freq_range *freq_range;
7691 		const struct ieee80211_power_rule *power_rule;
7692 		unsigned int max_bandwidth_khz;
7693 
7694 		reg_rule = &regdom->reg_rules[i];
7695 		freq_range = &reg_rule->freq_range;
7696 		power_rule = &reg_rule->power_rule;
7697 
7698 		nl_reg_rule = nla_nest_start_noflag(msg, i);
7699 		if (!nl_reg_rule)
7700 			goto nla_put_failure;
7701 
7702 		max_bandwidth_khz = freq_range->max_bandwidth_khz;
7703 		if (!max_bandwidth_khz)
7704 			max_bandwidth_khz = reg_get_max_bandwidth(regdom,
7705 								  reg_rule);
7706 
7707 		if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
7708 				reg_rule->flags) ||
7709 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
7710 				freq_range->start_freq_khz) ||
7711 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
7712 				freq_range->end_freq_khz) ||
7713 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
7714 				max_bandwidth_khz) ||
7715 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
7716 				power_rule->max_antenna_gain) ||
7717 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
7718 				power_rule->max_eirp) ||
7719 		    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
7720 				reg_rule->dfs_cac_ms))
7721 			goto nla_put_failure;
7722 
7723 		nla_nest_end(msg, nl_reg_rule);
7724 	}
7725 
7726 	nla_nest_end(msg, nl_reg_rules);
7727 	return 0;
7728 
7729 nla_put_failure:
7730 	return -EMSGSIZE;
7731 }
7732 
7733 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
7734 {
7735 	const struct ieee80211_regdomain *regdom = NULL;
7736 	struct cfg80211_registered_device *rdev;
7737 	struct wiphy *wiphy = NULL;
7738 	struct sk_buff *msg;
7739 	void *hdr;
7740 
7741 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7742 	if (!msg)
7743 		return -ENOBUFS;
7744 
7745 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7746 			     NL80211_CMD_GET_REG);
7747 	if (!hdr)
7748 		goto put_failure;
7749 
7750 	rtnl_lock();
7751 
7752 	if (info->attrs[NL80211_ATTR_WIPHY]) {
7753 		bool self_managed;
7754 
7755 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
7756 		if (IS_ERR(rdev)) {
7757 			nlmsg_free(msg);
7758 			rtnl_unlock();
7759 			return PTR_ERR(rdev);
7760 		}
7761 
7762 		wiphy = &rdev->wiphy;
7763 		self_managed = wiphy->regulatory_flags &
7764 			       REGULATORY_WIPHY_SELF_MANAGED;
7765 		regdom = get_wiphy_regdom(wiphy);
7766 
7767 		/* a self-managed-reg device must have a private regdom */
7768 		if (WARN_ON(!regdom && self_managed)) {
7769 			nlmsg_free(msg);
7770 			rtnl_unlock();
7771 			return -EINVAL;
7772 		}
7773 
7774 		if (regdom &&
7775 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7776 			goto nla_put_failure;
7777 	}
7778 
7779 	if (!wiphy && reg_last_request_cell_base() &&
7780 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
7781 			NL80211_USER_REG_HINT_CELL_BASE))
7782 		goto nla_put_failure;
7783 
7784 	rcu_read_lock();
7785 
7786 	if (!regdom)
7787 		regdom = rcu_dereference(cfg80211_regdomain);
7788 
7789 	if (nl80211_put_regdom(regdom, msg))
7790 		goto nla_put_failure_rcu;
7791 
7792 	rcu_read_unlock();
7793 
7794 	genlmsg_end(msg, hdr);
7795 	rtnl_unlock();
7796 	return genlmsg_reply(msg, info);
7797 
7798 nla_put_failure_rcu:
7799 	rcu_read_unlock();
7800 nla_put_failure:
7801 	rtnl_unlock();
7802 put_failure:
7803 	nlmsg_free(msg);
7804 	return -EMSGSIZE;
7805 }
7806 
7807 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
7808 			       u32 seq, int flags, struct wiphy *wiphy,
7809 			       const struct ieee80211_regdomain *regdom)
7810 {
7811 	void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
7812 				   NL80211_CMD_GET_REG);
7813 
7814 	if (!hdr)
7815 		return -1;
7816 
7817 	genl_dump_check_consistent(cb, hdr);
7818 
7819 	if (nl80211_put_regdom(regdom, msg))
7820 		goto nla_put_failure;
7821 
7822 	if (!wiphy && reg_last_request_cell_base() &&
7823 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
7824 			NL80211_USER_REG_HINT_CELL_BASE))
7825 		goto nla_put_failure;
7826 
7827 	if (wiphy &&
7828 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7829 		goto nla_put_failure;
7830 
7831 	if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
7832 	    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
7833 		goto nla_put_failure;
7834 
7835 	genlmsg_end(msg, hdr);
7836 	return 0;
7837 
7838 nla_put_failure:
7839 	genlmsg_cancel(msg, hdr);
7840 	return -EMSGSIZE;
7841 }
7842 
7843 static int nl80211_get_reg_dump(struct sk_buff *skb,
7844 				struct netlink_callback *cb)
7845 {
7846 	const struct ieee80211_regdomain *regdom = NULL;
7847 	struct cfg80211_registered_device *rdev;
7848 	int err, reg_idx, start = cb->args[2];
7849 
7850 	rtnl_lock();
7851 
7852 	if (cfg80211_regdomain && start == 0) {
7853 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7854 					  NLM_F_MULTI, NULL,
7855 					  rtnl_dereference(cfg80211_regdomain));
7856 		if (err < 0)
7857 			goto out_err;
7858 	}
7859 
7860 	/* the global regdom is idx 0 */
7861 	reg_idx = 1;
7862 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
7863 		regdom = get_wiphy_regdom(&rdev->wiphy);
7864 		if (!regdom)
7865 			continue;
7866 
7867 		if (++reg_idx <= start)
7868 			continue;
7869 
7870 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7871 					  NLM_F_MULTI, &rdev->wiphy, regdom);
7872 		if (err < 0) {
7873 			reg_idx--;
7874 			break;
7875 		}
7876 	}
7877 
7878 	cb->args[2] = reg_idx;
7879 	err = skb->len;
7880 out_err:
7881 	rtnl_unlock();
7882 	return err;
7883 }
7884 
7885 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
7886 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
7887 	[NL80211_ATTR_REG_RULE_FLAGS]		= { .type = NLA_U32 },
7888 	[NL80211_ATTR_FREQ_RANGE_START]		= { .type = NLA_U32 },
7889 	[NL80211_ATTR_FREQ_RANGE_END]		= { .type = NLA_U32 },
7890 	[NL80211_ATTR_FREQ_RANGE_MAX_BW]	= { .type = NLA_U32 },
7891 	[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]	= { .type = NLA_U32 },
7892 	[NL80211_ATTR_POWER_RULE_MAX_EIRP]	= { .type = NLA_U32 },
7893 	[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
7894 };
7895 
7896 static int parse_reg_rule(struct nlattr *tb[],
7897 	struct ieee80211_reg_rule *reg_rule)
7898 {
7899 	struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
7900 	struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
7901 
7902 	if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
7903 		return -EINVAL;
7904 	if (!tb[NL80211_ATTR_FREQ_RANGE_START])
7905 		return -EINVAL;
7906 	if (!tb[NL80211_ATTR_FREQ_RANGE_END])
7907 		return -EINVAL;
7908 	if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
7909 		return -EINVAL;
7910 	if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
7911 		return -EINVAL;
7912 
7913 	reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
7914 
7915 	freq_range->start_freq_khz =
7916 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
7917 	freq_range->end_freq_khz =
7918 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
7919 	freq_range->max_bandwidth_khz =
7920 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
7921 
7922 	power_rule->max_eirp =
7923 		nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
7924 
7925 	if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
7926 		power_rule->max_antenna_gain =
7927 			nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
7928 
7929 	if (tb[NL80211_ATTR_DFS_CAC_TIME])
7930 		reg_rule->dfs_cac_ms =
7931 			nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
7932 
7933 	return 0;
7934 }
7935 
7936 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
7937 {
7938 	struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
7939 	struct nlattr *nl_reg_rule;
7940 	char *alpha2;
7941 	int rem_reg_rules, r;
7942 	u32 num_rules = 0, rule_idx = 0;
7943 	enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
7944 	struct ieee80211_regdomain *rd;
7945 
7946 	if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
7947 		return -EINVAL;
7948 
7949 	if (!info->attrs[NL80211_ATTR_REG_RULES])
7950 		return -EINVAL;
7951 
7952 	alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
7953 
7954 	if (info->attrs[NL80211_ATTR_DFS_REGION])
7955 		dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
7956 
7957 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7958 			    rem_reg_rules) {
7959 		num_rules++;
7960 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
7961 			return -EINVAL;
7962 	}
7963 
7964 	rtnl_lock();
7965 	if (!reg_is_valid_request(alpha2)) {
7966 		r = -EINVAL;
7967 		goto out;
7968 	}
7969 
7970 	rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
7971 	if (!rd) {
7972 		r = -ENOMEM;
7973 		goto out;
7974 	}
7975 
7976 	rd->n_reg_rules = num_rules;
7977 	rd->alpha2[0] = alpha2[0];
7978 	rd->alpha2[1] = alpha2[1];
7979 
7980 	/*
7981 	 * Disable DFS master mode if the DFS region was
7982 	 * not supported or known on this kernel.
7983 	 */
7984 	if (reg_supported_dfs_region(dfs_region))
7985 		rd->dfs_region = dfs_region;
7986 
7987 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7988 			    rem_reg_rules) {
7989 		r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
7990 						nl_reg_rule, reg_rule_policy,
7991 						info->extack);
7992 		if (r)
7993 			goto bad_reg;
7994 		r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
7995 		if (r)
7996 			goto bad_reg;
7997 
7998 		rule_idx++;
7999 
8000 		if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
8001 			r = -EINVAL;
8002 			goto bad_reg;
8003 		}
8004 	}
8005 
8006 	r = set_regdom(rd, REGD_SOURCE_CRDA);
8007 	/* set_regdom takes ownership of rd */
8008 	rd = NULL;
8009  bad_reg:
8010 	kfree(rd);
8011  out:
8012 	rtnl_unlock();
8013 	return r;
8014 }
8015 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
8016 
8017 static int validate_scan_freqs(struct nlattr *freqs)
8018 {
8019 	struct nlattr *attr1, *attr2;
8020 	int n_channels = 0, tmp1, tmp2;
8021 
8022 	nla_for_each_nested(attr1, freqs, tmp1)
8023 		if (nla_len(attr1) != sizeof(u32))
8024 			return 0;
8025 
8026 	nla_for_each_nested(attr1, freqs, tmp1) {
8027 		n_channels++;
8028 		/*
8029 		 * Some hardware has a limited channel list for
8030 		 * scanning, and it is pretty much nonsensical
8031 		 * to scan for a channel twice, so disallow that
8032 		 * and don't require drivers to check that the
8033 		 * channel list they get isn't longer than what
8034 		 * they can scan, as long as they can scan all
8035 		 * the channels they registered at once.
8036 		 */
8037 		nla_for_each_nested(attr2, freqs, tmp2)
8038 			if (attr1 != attr2 &&
8039 			    nla_get_u32(attr1) == nla_get_u32(attr2))
8040 				return 0;
8041 	}
8042 
8043 	return n_channels;
8044 }
8045 
8046 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
8047 {
8048 	return b < NUM_NL80211_BANDS && wiphy->bands[b];
8049 }
8050 
8051 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
8052 			    struct cfg80211_bss_selection *bss_select)
8053 {
8054 	struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
8055 	struct nlattr *nest;
8056 	int err;
8057 	bool found = false;
8058 	int i;
8059 
8060 	/* only process one nested attribute */
8061 	nest = nla_data(nla);
8062 	if (!nla_ok(nest, nla_len(nest)))
8063 		return -EINVAL;
8064 
8065 	err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
8066 					  nest, nl80211_bss_select_policy,
8067 					  NULL);
8068 	if (err)
8069 		return err;
8070 
8071 	/* only one attribute may be given */
8072 	for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
8073 		if (attr[i]) {
8074 			if (found)
8075 				return -EINVAL;
8076 			found = true;
8077 		}
8078 	}
8079 
8080 	bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
8081 
8082 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
8083 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
8084 
8085 	if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
8086 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
8087 		bss_select->param.band_pref =
8088 			nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
8089 		if (!is_band_valid(wiphy, bss_select->param.band_pref))
8090 			return -EINVAL;
8091 	}
8092 
8093 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
8094 		struct nl80211_bss_select_rssi_adjust *adj_param;
8095 
8096 		adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
8097 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
8098 		bss_select->param.adjust.band = adj_param->band;
8099 		bss_select->param.adjust.delta = adj_param->delta;
8100 		if (!is_band_valid(wiphy, bss_select->param.adjust.band))
8101 			return -EINVAL;
8102 	}
8103 
8104 	/* user-space did not provide behaviour attribute */
8105 	if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
8106 		return -EINVAL;
8107 
8108 	if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
8109 		return -EINVAL;
8110 
8111 	return 0;
8112 }
8113 
8114 int nl80211_parse_random_mac(struct nlattr **attrs,
8115 			     u8 *mac_addr, u8 *mac_addr_mask)
8116 {
8117 	int i;
8118 
8119 	if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
8120 		eth_zero_addr(mac_addr);
8121 		eth_zero_addr(mac_addr_mask);
8122 		mac_addr[0] = 0x2;
8123 		mac_addr_mask[0] = 0x3;
8124 
8125 		return 0;
8126 	}
8127 
8128 	/* need both or none */
8129 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
8130 		return -EINVAL;
8131 
8132 	memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
8133 	memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
8134 
8135 	/* don't allow or configure an mcast address */
8136 	if (!is_multicast_ether_addr(mac_addr_mask) ||
8137 	    is_multicast_ether_addr(mac_addr))
8138 		return -EINVAL;
8139 
8140 	/*
8141 	 * allow users to pass a MAC address that has bits set outside
8142 	 * of the mask, but don't bother drivers with having to deal
8143 	 * with such bits
8144 	 */
8145 	for (i = 0; i < ETH_ALEN; i++)
8146 		mac_addr[i] &= mac_addr_mask[i];
8147 
8148 	return 0;
8149 }
8150 
8151 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev)
8152 {
8153 	ASSERT_WDEV_LOCK(wdev);
8154 
8155 	if (!cfg80211_beaconing_iface_active(wdev))
8156 		return true;
8157 
8158 	if (!(wdev->chandef.chan->flags & IEEE80211_CHAN_RADAR))
8159 		return true;
8160 
8161 	return regulatory_pre_cac_allowed(wdev->wiphy);
8162 }
8163 
8164 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
8165 				    enum nl80211_ext_feature_index feat)
8166 {
8167 	if (!(flags & flag))
8168 		return true;
8169 	if (wiphy_ext_feature_isset(wiphy, feat))
8170 		return true;
8171 	return false;
8172 }
8173 
8174 static int
8175 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
8176 			 void *request, struct nlattr **attrs,
8177 			 bool is_sched_scan)
8178 {
8179 	u8 *mac_addr, *mac_addr_mask;
8180 	u32 *flags;
8181 	enum nl80211_feature_flags randomness_flag;
8182 
8183 	if (!attrs[NL80211_ATTR_SCAN_FLAGS])
8184 		return 0;
8185 
8186 	if (is_sched_scan) {
8187 		struct cfg80211_sched_scan_request *req = request;
8188 
8189 		randomness_flag = wdev ?
8190 				  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
8191 				  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
8192 		flags = &req->flags;
8193 		mac_addr = req->mac_addr;
8194 		mac_addr_mask = req->mac_addr_mask;
8195 	} else {
8196 		struct cfg80211_scan_request *req = request;
8197 
8198 		randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
8199 		flags = &req->flags;
8200 		mac_addr = req->mac_addr;
8201 		mac_addr_mask = req->mac_addr_mask;
8202 	}
8203 
8204 	*flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
8205 
8206 	if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
8207 	     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
8208 	    !nl80211_check_scan_feat(wiphy, *flags,
8209 				     NL80211_SCAN_FLAG_LOW_SPAN,
8210 				     NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
8211 	    !nl80211_check_scan_feat(wiphy, *flags,
8212 				     NL80211_SCAN_FLAG_LOW_POWER,
8213 				     NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
8214 	    !nl80211_check_scan_feat(wiphy, *flags,
8215 				     NL80211_SCAN_FLAG_HIGH_ACCURACY,
8216 				     NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
8217 	    !nl80211_check_scan_feat(wiphy, *flags,
8218 				     NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
8219 				     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
8220 	    !nl80211_check_scan_feat(wiphy, *flags,
8221 				     NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
8222 				     NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
8223 	    !nl80211_check_scan_feat(wiphy, *flags,
8224 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
8225 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
8226 	    !nl80211_check_scan_feat(wiphy, *flags,
8227 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
8228 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
8229 	    !nl80211_check_scan_feat(wiphy, *flags,
8230 				     NL80211_SCAN_FLAG_RANDOM_SN,
8231 				     NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
8232 	    !nl80211_check_scan_feat(wiphy, *flags,
8233 				     NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
8234 				     NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
8235 		return -EOPNOTSUPP;
8236 
8237 	if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
8238 		int err;
8239 
8240 		if (!(wiphy->features & randomness_flag) ||
8241 		    (wdev && wdev->current_bss))
8242 			return -EOPNOTSUPP;
8243 
8244 		err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
8245 		if (err)
8246 			return err;
8247 	}
8248 
8249 	return 0;
8250 }
8251 
8252 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
8253 {
8254 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8255 	struct wireless_dev *wdev = info->user_ptr[1];
8256 	struct cfg80211_scan_request *request;
8257 	struct nlattr *scan_freqs = NULL;
8258 	bool scan_freqs_khz = false;
8259 	struct nlattr *attr;
8260 	struct wiphy *wiphy;
8261 	int err, tmp, n_ssids = 0, n_channels, i;
8262 	size_t ie_len;
8263 
8264 	wiphy = &rdev->wiphy;
8265 
8266 	if (wdev->iftype == NL80211_IFTYPE_NAN)
8267 		return -EOPNOTSUPP;
8268 
8269 	if (!rdev->ops->scan)
8270 		return -EOPNOTSUPP;
8271 
8272 	if (rdev->scan_req || rdev->scan_msg)
8273 		return -EBUSY;
8274 
8275 	if (info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ]) {
8276 		if (!wiphy_ext_feature_isset(wiphy,
8277 					     NL80211_EXT_FEATURE_SCAN_FREQ_KHZ))
8278 			return -EOPNOTSUPP;
8279 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ];
8280 		scan_freqs_khz = true;
8281 	} else if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES])
8282 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQUENCIES];
8283 
8284 	if (scan_freqs) {
8285 		n_channels = validate_scan_freqs(scan_freqs);
8286 		if (!n_channels)
8287 			return -EINVAL;
8288 	} else {
8289 		n_channels = ieee80211_get_num_supported_channels(wiphy);
8290 	}
8291 
8292 	if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
8293 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
8294 			n_ssids++;
8295 
8296 	if (n_ssids > wiphy->max_scan_ssids)
8297 		return -EINVAL;
8298 
8299 	if (info->attrs[NL80211_ATTR_IE])
8300 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8301 	else
8302 		ie_len = 0;
8303 
8304 	if (ie_len > wiphy->max_scan_ie_len)
8305 		return -EINVAL;
8306 
8307 	request = kzalloc(sizeof(*request)
8308 			+ sizeof(*request->ssids) * n_ssids
8309 			+ sizeof(*request->channels) * n_channels
8310 			+ ie_len, GFP_KERNEL);
8311 	if (!request)
8312 		return -ENOMEM;
8313 
8314 	if (n_ssids)
8315 		request->ssids = (void *)&request->channels[n_channels];
8316 	request->n_ssids = n_ssids;
8317 	if (ie_len) {
8318 		if (n_ssids)
8319 			request->ie = (void *)(request->ssids + n_ssids);
8320 		else
8321 			request->ie = (void *)(request->channels + n_channels);
8322 	}
8323 
8324 	i = 0;
8325 	if (scan_freqs) {
8326 		/* user specified, bail out if channel not found */
8327 		nla_for_each_nested(attr, scan_freqs, tmp) {
8328 			struct ieee80211_channel *chan;
8329 			int freq = nla_get_u32(attr);
8330 
8331 			if (!scan_freqs_khz)
8332 				freq = MHZ_TO_KHZ(freq);
8333 
8334 			chan = ieee80211_get_channel_khz(wiphy, freq);
8335 			if (!chan) {
8336 				err = -EINVAL;
8337 				goto out_free;
8338 			}
8339 
8340 			/* ignore disabled channels */
8341 			if (chan->flags & IEEE80211_CHAN_DISABLED)
8342 				continue;
8343 
8344 			request->channels[i] = chan;
8345 			i++;
8346 		}
8347 	} else {
8348 		enum nl80211_band band;
8349 
8350 		/* all channels */
8351 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
8352 			int j;
8353 
8354 			if (!wiphy->bands[band])
8355 				continue;
8356 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
8357 				struct ieee80211_channel *chan;
8358 
8359 				chan = &wiphy->bands[band]->channels[j];
8360 
8361 				if (chan->flags & IEEE80211_CHAN_DISABLED)
8362 					continue;
8363 
8364 				request->channels[i] = chan;
8365 				i++;
8366 			}
8367 		}
8368 	}
8369 
8370 	if (!i) {
8371 		err = -EINVAL;
8372 		goto out_free;
8373 	}
8374 
8375 	request->n_channels = i;
8376 
8377 	wdev_lock(wdev);
8378 	if (!cfg80211_off_channel_oper_allowed(wdev)) {
8379 		struct ieee80211_channel *chan;
8380 
8381 		if (request->n_channels != 1) {
8382 			wdev_unlock(wdev);
8383 			err = -EBUSY;
8384 			goto out_free;
8385 		}
8386 
8387 		chan = request->channels[0];
8388 		if (chan->center_freq != wdev->chandef.chan->center_freq) {
8389 			wdev_unlock(wdev);
8390 			err = -EBUSY;
8391 			goto out_free;
8392 		}
8393 	}
8394 	wdev_unlock(wdev);
8395 
8396 	i = 0;
8397 	if (n_ssids) {
8398 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
8399 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
8400 				err = -EINVAL;
8401 				goto out_free;
8402 			}
8403 			request->ssids[i].ssid_len = nla_len(attr);
8404 			memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
8405 			i++;
8406 		}
8407 	}
8408 
8409 	if (info->attrs[NL80211_ATTR_IE]) {
8410 		request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8411 		memcpy((void *)request->ie,
8412 		       nla_data(info->attrs[NL80211_ATTR_IE]),
8413 		       request->ie_len);
8414 	}
8415 
8416 	for (i = 0; i < NUM_NL80211_BANDS; i++)
8417 		if (wiphy->bands[i])
8418 			request->rates[i] =
8419 				(1 << wiphy->bands[i]->n_bitrates) - 1;
8420 
8421 	if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
8422 		nla_for_each_nested(attr,
8423 				    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
8424 				    tmp) {
8425 			enum nl80211_band band = nla_type(attr);
8426 
8427 			if (band < 0 || band >= NUM_NL80211_BANDS) {
8428 				err = -EINVAL;
8429 				goto out_free;
8430 			}
8431 
8432 			if (!wiphy->bands[band])
8433 				continue;
8434 
8435 			err = ieee80211_get_ratemask(wiphy->bands[band],
8436 						     nla_data(attr),
8437 						     nla_len(attr),
8438 						     &request->rates[band]);
8439 			if (err)
8440 				goto out_free;
8441 		}
8442 	}
8443 
8444 	if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
8445 		request->duration =
8446 			nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
8447 		request->duration_mandatory =
8448 			nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
8449 	}
8450 
8451 	err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
8452 				       false);
8453 	if (err)
8454 		goto out_free;
8455 
8456 	request->no_cck =
8457 		nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
8458 
8459 	/* Initial implementation used NL80211_ATTR_MAC to set the specific
8460 	 * BSSID to scan for. This was problematic because that same attribute
8461 	 * was already used for another purpose (local random MAC address). The
8462 	 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
8463 	 * compatibility with older userspace components, also use the
8464 	 * NL80211_ATTR_MAC value here if it can be determined to be used for
8465 	 * the specific BSSID use case instead of the random MAC address
8466 	 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
8467 	 */
8468 	if (info->attrs[NL80211_ATTR_BSSID])
8469 		memcpy(request->bssid,
8470 		       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
8471 	else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
8472 		 info->attrs[NL80211_ATTR_MAC])
8473 		memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
8474 		       ETH_ALEN);
8475 	else
8476 		eth_broadcast_addr(request->bssid);
8477 
8478 	request->wdev = wdev;
8479 	request->wiphy = &rdev->wiphy;
8480 	request->scan_start = jiffies;
8481 
8482 	rdev->scan_req = request;
8483 	err = cfg80211_scan(rdev);
8484 
8485 	if (err)
8486 		goto out_free;
8487 
8488 	nl80211_send_scan_start(rdev, wdev);
8489 	if (wdev->netdev)
8490 		dev_hold(wdev->netdev);
8491 
8492 	return 0;
8493 
8494  out_free:
8495 	rdev->scan_req = NULL;
8496 	kfree(request);
8497 
8498 	return err;
8499 }
8500 
8501 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
8502 {
8503 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8504 	struct wireless_dev *wdev = info->user_ptr[1];
8505 
8506 	if (!rdev->ops->abort_scan)
8507 		return -EOPNOTSUPP;
8508 
8509 	if (rdev->scan_msg)
8510 		return 0;
8511 
8512 	if (!rdev->scan_req)
8513 		return -ENOENT;
8514 
8515 	rdev_abort_scan(rdev, wdev);
8516 	return 0;
8517 }
8518 
8519 static int
8520 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
8521 			       struct cfg80211_sched_scan_request *request,
8522 			       struct nlattr **attrs)
8523 {
8524 	int tmp, err, i = 0;
8525 	struct nlattr *attr;
8526 
8527 	if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
8528 		u32 interval;
8529 
8530 		/*
8531 		 * If scan plans are not specified,
8532 		 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
8533 		 * case one scan plan will be set with the specified scan
8534 		 * interval and infinite number of iterations.
8535 		 */
8536 		interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
8537 		if (!interval)
8538 			return -EINVAL;
8539 
8540 		request->scan_plans[0].interval =
8541 			DIV_ROUND_UP(interval, MSEC_PER_SEC);
8542 		if (!request->scan_plans[0].interval)
8543 			return -EINVAL;
8544 
8545 		if (request->scan_plans[0].interval >
8546 		    wiphy->max_sched_scan_plan_interval)
8547 			request->scan_plans[0].interval =
8548 				wiphy->max_sched_scan_plan_interval;
8549 
8550 		return 0;
8551 	}
8552 
8553 	nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
8554 		struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
8555 
8556 		if (WARN_ON(i >= n_plans))
8557 			return -EINVAL;
8558 
8559 		err = nla_parse_nested_deprecated(plan,
8560 						  NL80211_SCHED_SCAN_PLAN_MAX,
8561 						  attr, nl80211_plan_policy,
8562 						  NULL);
8563 		if (err)
8564 			return err;
8565 
8566 		if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
8567 			return -EINVAL;
8568 
8569 		request->scan_plans[i].interval =
8570 			nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
8571 		if (!request->scan_plans[i].interval ||
8572 		    request->scan_plans[i].interval >
8573 		    wiphy->max_sched_scan_plan_interval)
8574 			return -EINVAL;
8575 
8576 		if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
8577 			request->scan_plans[i].iterations =
8578 				nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
8579 			if (!request->scan_plans[i].iterations ||
8580 			    (request->scan_plans[i].iterations >
8581 			     wiphy->max_sched_scan_plan_iterations))
8582 				return -EINVAL;
8583 		} else if (i < n_plans - 1) {
8584 			/*
8585 			 * All scan plans but the last one must specify
8586 			 * a finite number of iterations
8587 			 */
8588 			return -EINVAL;
8589 		}
8590 
8591 		i++;
8592 	}
8593 
8594 	/*
8595 	 * The last scan plan must not specify the number of
8596 	 * iterations, it is supposed to run infinitely
8597 	 */
8598 	if (request->scan_plans[n_plans - 1].iterations)
8599 		return  -EINVAL;
8600 
8601 	return 0;
8602 }
8603 
8604 static int
8605 nl80211_parse_sched_scan_per_band_rssi(struct wiphy *wiphy,
8606 				       struct cfg80211_match_set *match_sets,
8607 				       struct nlattr *tb_band_rssi,
8608 				       s32 rssi_thold)
8609 {
8610 	struct nlattr *attr;
8611 	int i, tmp, ret = 0;
8612 
8613 	if (!wiphy_ext_feature_isset(wiphy,
8614 		    NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD)) {
8615 		if (tb_band_rssi)
8616 			ret = -EOPNOTSUPP;
8617 		else
8618 			for (i = 0; i < NUM_NL80211_BANDS; i++)
8619 				match_sets->per_band_rssi_thold[i] =
8620 					NL80211_SCAN_RSSI_THOLD_OFF;
8621 		return ret;
8622 	}
8623 
8624 	for (i = 0; i < NUM_NL80211_BANDS; i++)
8625 		match_sets->per_band_rssi_thold[i] = rssi_thold;
8626 
8627 	nla_for_each_nested(attr, tb_band_rssi, tmp) {
8628 		enum nl80211_band band = nla_type(attr);
8629 
8630 		if (band < 0 || band >= NUM_NL80211_BANDS)
8631 			return -EINVAL;
8632 
8633 		match_sets->per_band_rssi_thold[band] =	nla_get_s32(attr);
8634 	}
8635 
8636 	return 0;
8637 }
8638 
8639 static struct cfg80211_sched_scan_request *
8640 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
8641 			 struct nlattr **attrs, int max_match_sets)
8642 {
8643 	struct cfg80211_sched_scan_request *request;
8644 	struct nlattr *attr;
8645 	int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
8646 	enum nl80211_band band;
8647 	size_t ie_len;
8648 	struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
8649 	s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
8650 
8651 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
8652 		n_channels = validate_scan_freqs(
8653 				attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
8654 		if (!n_channels)
8655 			return ERR_PTR(-EINVAL);
8656 	} else {
8657 		n_channels = ieee80211_get_num_supported_channels(wiphy);
8658 	}
8659 
8660 	if (attrs[NL80211_ATTR_SCAN_SSIDS])
8661 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
8662 				    tmp)
8663 			n_ssids++;
8664 
8665 	if (n_ssids > wiphy->max_sched_scan_ssids)
8666 		return ERR_PTR(-EINVAL);
8667 
8668 	/*
8669 	 * First, count the number of 'real' matchsets. Due to an issue with
8670 	 * the old implementation, matchsets containing only the RSSI attribute
8671 	 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
8672 	 * RSSI for all matchsets, rather than their own matchset for reporting
8673 	 * all APs with a strong RSSI. This is needed to be compatible with
8674 	 * older userspace that treated a matchset with only the RSSI as the
8675 	 * global RSSI for all other matchsets - if there are other matchsets.
8676 	 */
8677 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
8678 		nla_for_each_nested(attr,
8679 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
8680 				    tmp) {
8681 			struct nlattr *rssi;
8682 
8683 			err = nla_parse_nested_deprecated(tb,
8684 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
8685 							  attr,
8686 							  nl80211_match_policy,
8687 							  NULL);
8688 			if (err)
8689 				return ERR_PTR(err);
8690 
8691 			/* SSID and BSSID are mutually exclusive */
8692 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
8693 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
8694 				return ERR_PTR(-EINVAL);
8695 
8696 			/* add other standalone attributes here */
8697 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
8698 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
8699 				n_match_sets++;
8700 				continue;
8701 			}
8702 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
8703 			if (rssi)
8704 				default_match_rssi = nla_get_s32(rssi);
8705 		}
8706 	}
8707 
8708 	/* However, if there's no other matchset, add the RSSI one */
8709 	if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
8710 		n_match_sets = 1;
8711 
8712 	if (n_match_sets > max_match_sets)
8713 		return ERR_PTR(-EINVAL);
8714 
8715 	if (attrs[NL80211_ATTR_IE])
8716 		ie_len = nla_len(attrs[NL80211_ATTR_IE]);
8717 	else
8718 		ie_len = 0;
8719 
8720 	if (ie_len > wiphy->max_sched_scan_ie_len)
8721 		return ERR_PTR(-EINVAL);
8722 
8723 	if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
8724 		/*
8725 		 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
8726 		 * each scan plan already specifies its own interval
8727 		 */
8728 		if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
8729 			return ERR_PTR(-EINVAL);
8730 
8731 		nla_for_each_nested(attr,
8732 				    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
8733 			n_plans++;
8734 	} else {
8735 		/*
8736 		 * The scan interval attribute is kept for backward
8737 		 * compatibility. If no scan plans are specified and sched scan
8738 		 * interval is specified, one scan plan will be set with this
8739 		 * scan interval and infinite number of iterations.
8740 		 */
8741 		if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
8742 			return ERR_PTR(-EINVAL);
8743 
8744 		n_plans = 1;
8745 	}
8746 
8747 	if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
8748 		return ERR_PTR(-EINVAL);
8749 
8750 	if (!wiphy_ext_feature_isset(
8751 		    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
8752 	    (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
8753 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
8754 		return ERR_PTR(-EINVAL);
8755 
8756 	request = kzalloc(sizeof(*request)
8757 			+ sizeof(*request->ssids) * n_ssids
8758 			+ sizeof(*request->match_sets) * n_match_sets
8759 			+ sizeof(*request->scan_plans) * n_plans
8760 			+ sizeof(*request->channels) * n_channels
8761 			+ ie_len, GFP_KERNEL);
8762 	if (!request)
8763 		return ERR_PTR(-ENOMEM);
8764 
8765 	if (n_ssids)
8766 		request->ssids = (void *)&request->channels[n_channels];
8767 	request->n_ssids = n_ssids;
8768 	if (ie_len) {
8769 		if (n_ssids)
8770 			request->ie = (void *)(request->ssids + n_ssids);
8771 		else
8772 			request->ie = (void *)(request->channels + n_channels);
8773 	}
8774 
8775 	if (n_match_sets) {
8776 		if (request->ie)
8777 			request->match_sets = (void *)(request->ie + ie_len);
8778 		else if (n_ssids)
8779 			request->match_sets =
8780 				(void *)(request->ssids + n_ssids);
8781 		else
8782 			request->match_sets =
8783 				(void *)(request->channels + n_channels);
8784 	}
8785 	request->n_match_sets = n_match_sets;
8786 
8787 	if (n_match_sets)
8788 		request->scan_plans = (void *)(request->match_sets +
8789 					       n_match_sets);
8790 	else if (request->ie)
8791 		request->scan_plans = (void *)(request->ie + ie_len);
8792 	else if (n_ssids)
8793 		request->scan_plans = (void *)(request->ssids + n_ssids);
8794 	else
8795 		request->scan_plans = (void *)(request->channels + n_channels);
8796 
8797 	request->n_scan_plans = n_plans;
8798 
8799 	i = 0;
8800 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
8801 		/* user specified, bail out if channel not found */
8802 		nla_for_each_nested(attr,
8803 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
8804 				    tmp) {
8805 			struct ieee80211_channel *chan;
8806 
8807 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
8808 
8809 			if (!chan) {
8810 				err = -EINVAL;
8811 				goto out_free;
8812 			}
8813 
8814 			/* ignore disabled channels */
8815 			if (chan->flags & IEEE80211_CHAN_DISABLED)
8816 				continue;
8817 
8818 			request->channels[i] = chan;
8819 			i++;
8820 		}
8821 	} else {
8822 		/* all channels */
8823 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
8824 			int j;
8825 
8826 			if (!wiphy->bands[band])
8827 				continue;
8828 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
8829 				struct ieee80211_channel *chan;
8830 
8831 				chan = &wiphy->bands[band]->channels[j];
8832 
8833 				if (chan->flags & IEEE80211_CHAN_DISABLED)
8834 					continue;
8835 
8836 				request->channels[i] = chan;
8837 				i++;
8838 			}
8839 		}
8840 	}
8841 
8842 	if (!i) {
8843 		err = -EINVAL;
8844 		goto out_free;
8845 	}
8846 
8847 	request->n_channels = i;
8848 
8849 	i = 0;
8850 	if (n_ssids) {
8851 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
8852 				    tmp) {
8853 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
8854 				err = -EINVAL;
8855 				goto out_free;
8856 			}
8857 			request->ssids[i].ssid_len = nla_len(attr);
8858 			memcpy(request->ssids[i].ssid, nla_data(attr),
8859 			       nla_len(attr));
8860 			i++;
8861 		}
8862 	}
8863 
8864 	i = 0;
8865 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
8866 		nla_for_each_nested(attr,
8867 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
8868 				    tmp) {
8869 			struct nlattr *ssid, *bssid, *rssi;
8870 
8871 			err = nla_parse_nested_deprecated(tb,
8872 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
8873 							  attr,
8874 							  nl80211_match_policy,
8875 							  NULL);
8876 			if (err)
8877 				goto out_free;
8878 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
8879 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
8880 
8881 			if (!ssid && !bssid) {
8882 				i++;
8883 				continue;
8884 			}
8885 
8886 			if (WARN_ON(i >= n_match_sets)) {
8887 				/* this indicates a programming error,
8888 				 * the loop above should have verified
8889 				 * things properly
8890 				 */
8891 				err = -EINVAL;
8892 				goto out_free;
8893 			}
8894 
8895 			if (ssid) {
8896 				memcpy(request->match_sets[i].ssid.ssid,
8897 				       nla_data(ssid), nla_len(ssid));
8898 				request->match_sets[i].ssid.ssid_len =
8899 					nla_len(ssid);
8900 			}
8901 			if (bssid)
8902 				memcpy(request->match_sets[i].bssid,
8903 				       nla_data(bssid), ETH_ALEN);
8904 
8905 			/* special attribute - old implementation w/a */
8906 			request->match_sets[i].rssi_thold = default_match_rssi;
8907 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
8908 			if (rssi)
8909 				request->match_sets[i].rssi_thold =
8910 					nla_get_s32(rssi);
8911 
8912 			/* Parse per band RSSI attribute */
8913 			err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
8914 				&request->match_sets[i],
8915 				tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
8916 				request->match_sets[i].rssi_thold);
8917 			if (err)
8918 				goto out_free;
8919 
8920 			i++;
8921 		}
8922 
8923 		/* there was no other matchset, so the RSSI one is alone */
8924 		if (i == 0 && n_match_sets)
8925 			request->match_sets[0].rssi_thold = default_match_rssi;
8926 
8927 		request->min_rssi_thold = INT_MAX;
8928 		for (i = 0; i < n_match_sets; i++)
8929 			request->min_rssi_thold =
8930 				min(request->match_sets[i].rssi_thold,
8931 				    request->min_rssi_thold);
8932 	} else {
8933 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
8934 	}
8935 
8936 	if (ie_len) {
8937 		request->ie_len = ie_len;
8938 		memcpy((void *)request->ie,
8939 		       nla_data(attrs[NL80211_ATTR_IE]),
8940 		       request->ie_len);
8941 	}
8942 
8943 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
8944 	if (err)
8945 		goto out_free;
8946 
8947 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
8948 		request->delay =
8949 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
8950 
8951 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
8952 		request->relative_rssi = nla_get_s8(
8953 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
8954 		request->relative_rssi_set = true;
8955 	}
8956 
8957 	if (request->relative_rssi_set &&
8958 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
8959 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
8960 
8961 		rssi_adjust = nla_data(
8962 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
8963 		request->rssi_adjust.band = rssi_adjust->band;
8964 		request->rssi_adjust.delta = rssi_adjust->delta;
8965 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
8966 			err = -EINVAL;
8967 			goto out_free;
8968 		}
8969 	}
8970 
8971 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
8972 	if (err)
8973 		goto out_free;
8974 
8975 	request->scan_start = jiffies;
8976 
8977 	return request;
8978 
8979 out_free:
8980 	kfree(request);
8981 	return ERR_PTR(err);
8982 }
8983 
8984 static int nl80211_start_sched_scan(struct sk_buff *skb,
8985 				    struct genl_info *info)
8986 {
8987 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8988 	struct net_device *dev = info->user_ptr[1];
8989 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8990 	struct cfg80211_sched_scan_request *sched_scan_req;
8991 	bool want_multi;
8992 	int err;
8993 
8994 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
8995 		return -EOPNOTSUPP;
8996 
8997 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
8998 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
8999 	if (err)
9000 		return err;
9001 
9002 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
9003 						  info->attrs,
9004 						  rdev->wiphy.max_match_sets);
9005 
9006 	err = PTR_ERR_OR_ZERO(sched_scan_req);
9007 	if (err)
9008 		goto out_err;
9009 
9010 	/* leave request id zero for legacy request
9011 	 * or if driver does not support multi-scheduled scan
9012 	 */
9013 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1)
9014 		sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
9015 
9016 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
9017 	if (err)
9018 		goto out_free;
9019 
9020 	sched_scan_req->dev = dev;
9021 	sched_scan_req->wiphy = &rdev->wiphy;
9022 
9023 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9024 		sched_scan_req->owner_nlportid = info->snd_portid;
9025 
9026 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
9027 
9028 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
9029 	return 0;
9030 
9031 out_free:
9032 	kfree(sched_scan_req);
9033 out_err:
9034 	return err;
9035 }
9036 
9037 static int nl80211_stop_sched_scan(struct sk_buff *skb,
9038 				   struct genl_info *info)
9039 {
9040 	struct cfg80211_sched_scan_request *req;
9041 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9042 	u64 cookie;
9043 
9044 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
9045 		return -EOPNOTSUPP;
9046 
9047 	if (info->attrs[NL80211_ATTR_COOKIE]) {
9048 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9049 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
9050 	}
9051 
9052 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
9053 				     struct cfg80211_sched_scan_request,
9054 				     list);
9055 	if (!req || req->reqid ||
9056 	    (req->owner_nlportid &&
9057 	     req->owner_nlportid != info->snd_portid))
9058 		return -ENOENT;
9059 
9060 	return cfg80211_stop_sched_scan_req(rdev, req, false);
9061 }
9062 
9063 static int nl80211_start_radar_detection(struct sk_buff *skb,
9064 					 struct genl_info *info)
9065 {
9066 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9067 	struct net_device *dev = info->user_ptr[1];
9068 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9069 	struct wiphy *wiphy = wdev->wiphy;
9070 	struct cfg80211_chan_def chandef;
9071 	enum nl80211_dfs_regions dfs_region;
9072 	unsigned int cac_time_ms;
9073 	int err;
9074 
9075 	dfs_region = reg_get_dfs_region(wiphy);
9076 	if (dfs_region == NL80211_DFS_UNSET)
9077 		return -EINVAL;
9078 
9079 	err = nl80211_parse_chandef(rdev, info, &chandef);
9080 	if (err)
9081 		return err;
9082 
9083 	if (netif_carrier_ok(dev))
9084 		return -EBUSY;
9085 
9086 	if (wdev->cac_started)
9087 		return -EBUSY;
9088 
9089 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
9090 	if (err < 0)
9091 		return err;
9092 
9093 	if (err == 0)
9094 		return -EINVAL;
9095 
9096 	if (!cfg80211_chandef_dfs_usable(wiphy, &chandef))
9097 		return -EINVAL;
9098 
9099 	/* CAC start is offloaded to HW and can't be started manually */
9100 	if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD))
9101 		return -EOPNOTSUPP;
9102 
9103 	if (!rdev->ops->start_radar_detection)
9104 		return -EOPNOTSUPP;
9105 
9106 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
9107 	if (WARN_ON(!cac_time_ms))
9108 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
9109 
9110 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
9111 	if (!err) {
9112 		wdev->chandef = chandef;
9113 		wdev->cac_started = true;
9114 		wdev->cac_start_time = jiffies;
9115 		wdev->cac_time_ms = cac_time_ms;
9116 	}
9117 	return err;
9118 }
9119 
9120 static int nl80211_notify_radar_detection(struct sk_buff *skb,
9121 					  struct genl_info *info)
9122 {
9123 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9124 	struct net_device *dev = info->user_ptr[1];
9125 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9126 	struct wiphy *wiphy = wdev->wiphy;
9127 	struct cfg80211_chan_def chandef;
9128 	enum nl80211_dfs_regions dfs_region;
9129 	int err;
9130 
9131 	dfs_region = reg_get_dfs_region(wiphy);
9132 	if (dfs_region == NL80211_DFS_UNSET) {
9133 		GENL_SET_ERR_MSG(info,
9134 				 "DFS Region is not set. Unexpected Radar indication");
9135 		return -EINVAL;
9136 	}
9137 
9138 	err = nl80211_parse_chandef(rdev, info, &chandef);
9139 	if (err) {
9140 		GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
9141 		return err;
9142 	}
9143 
9144 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
9145 	if (err < 0) {
9146 		GENL_SET_ERR_MSG(info, "chandef is invalid");
9147 		return err;
9148 	}
9149 
9150 	if (err == 0) {
9151 		GENL_SET_ERR_MSG(info,
9152 				 "Unexpected Radar indication for chandef/iftype");
9153 		return -EINVAL;
9154 	}
9155 
9156 	/* Do not process this notification if radar is already detected
9157 	 * by kernel on this channel, and return success.
9158 	 */
9159 	if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
9160 		return 0;
9161 
9162 	cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
9163 
9164 	cfg80211_sched_dfs_chan_update(rdev);
9165 
9166 	rdev->radar_chandef = chandef;
9167 
9168 	/* Propagate this notification to other radios as well */
9169 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
9170 
9171 	return 0;
9172 }
9173 
9174 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
9175 {
9176 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9177 	struct net_device *dev = info->user_ptr[1];
9178 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9179 	struct cfg80211_csa_settings params;
9180 	struct nlattr **csa_attrs = NULL;
9181 	int err;
9182 	bool need_new_beacon = false;
9183 	bool need_handle_dfs_flag = true;
9184 	int len, i;
9185 	u32 cs_count;
9186 
9187 	if (!rdev->ops->channel_switch ||
9188 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
9189 		return -EOPNOTSUPP;
9190 
9191 	switch (dev->ieee80211_ptr->iftype) {
9192 	case NL80211_IFTYPE_AP:
9193 	case NL80211_IFTYPE_P2P_GO:
9194 		need_new_beacon = true;
9195 		/* For all modes except AP the handle_dfs flag needs to be
9196 		 * supplied to tell the kernel that userspace will handle radar
9197 		 * events when they happen. Otherwise a switch to a channel
9198 		 * requiring DFS will be rejected.
9199 		 */
9200 		need_handle_dfs_flag = false;
9201 
9202 		/* useless if AP is not running */
9203 		if (!wdev->beacon_interval)
9204 			return -ENOTCONN;
9205 		break;
9206 	case NL80211_IFTYPE_ADHOC:
9207 		if (!wdev->ssid_len)
9208 			return -ENOTCONN;
9209 		break;
9210 	case NL80211_IFTYPE_MESH_POINT:
9211 		if (!wdev->mesh_id_len)
9212 			return -ENOTCONN;
9213 		break;
9214 	default:
9215 		return -EOPNOTSUPP;
9216 	}
9217 
9218 	memset(&params, 0, sizeof(params));
9219 	params.beacon_csa.ftm_responder = -1;
9220 
9221 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
9222 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
9223 		return -EINVAL;
9224 
9225 	/* only important for AP, IBSS and mesh create IEs internally */
9226 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
9227 		return -EINVAL;
9228 
9229 	/* Even though the attribute is u32, the specification says
9230 	 * u8, so let's make sure we don't overflow.
9231 	 */
9232 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
9233 	if (cs_count > 255)
9234 		return -EINVAL;
9235 
9236 	params.count = cs_count;
9237 
9238 	if (!need_new_beacon)
9239 		goto skip_beacons;
9240 
9241 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after);
9242 	if (err)
9243 		return err;
9244 
9245 	csa_attrs = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*csa_attrs),
9246 			    GFP_KERNEL);
9247 	if (!csa_attrs)
9248 		return -ENOMEM;
9249 
9250 	err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
9251 					  info->attrs[NL80211_ATTR_CSA_IES],
9252 					  nl80211_policy, info->extack);
9253 	if (err)
9254 		goto free;
9255 
9256 	err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa);
9257 	if (err)
9258 		goto free;
9259 
9260 	if (!csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
9261 		err = -EINVAL;
9262 		goto free;
9263 	}
9264 
9265 	len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
9266 	if (!len || (len % sizeof(u16))) {
9267 		err = -EINVAL;
9268 		goto free;
9269 	}
9270 
9271 	params.n_counter_offsets_beacon = len / sizeof(u16);
9272 	if (rdev->wiphy.max_num_csa_counters &&
9273 	    (params.n_counter_offsets_beacon >
9274 	     rdev->wiphy.max_num_csa_counters)) {
9275 		err = -EINVAL;
9276 		goto free;
9277 	}
9278 
9279 	params.counter_offsets_beacon =
9280 		nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
9281 
9282 	/* sanity checks - counters should fit and be the same */
9283 	for (i = 0; i < params.n_counter_offsets_beacon; i++) {
9284 		u16 offset = params.counter_offsets_beacon[i];
9285 
9286 		if (offset >= params.beacon_csa.tail_len) {
9287 			err = -EINVAL;
9288 			goto free;
9289 		}
9290 
9291 		if (params.beacon_csa.tail[offset] != params.count) {
9292 			err = -EINVAL;
9293 			goto free;
9294 		}
9295 	}
9296 
9297 	if (csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
9298 		len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
9299 		if (!len || (len % sizeof(u16))) {
9300 			err = -EINVAL;
9301 			goto free;
9302 		}
9303 
9304 		params.n_counter_offsets_presp = len / sizeof(u16);
9305 		if (rdev->wiphy.max_num_csa_counters &&
9306 		    (params.n_counter_offsets_presp >
9307 		     rdev->wiphy.max_num_csa_counters)) {
9308 			err = -EINVAL;
9309 			goto free;
9310 		}
9311 
9312 		params.counter_offsets_presp =
9313 			nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
9314 
9315 		/* sanity checks - counters should fit and be the same */
9316 		for (i = 0; i < params.n_counter_offsets_presp; i++) {
9317 			u16 offset = params.counter_offsets_presp[i];
9318 
9319 			if (offset >= params.beacon_csa.probe_resp_len) {
9320 				err = -EINVAL;
9321 				goto free;
9322 			}
9323 
9324 			if (params.beacon_csa.probe_resp[offset] !=
9325 			    params.count) {
9326 				err = -EINVAL;
9327 				goto free;
9328 			}
9329 		}
9330 	}
9331 
9332 skip_beacons:
9333 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
9334 	if (err)
9335 		goto free;
9336 
9337 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
9338 					   wdev->iftype)) {
9339 		err = -EINVAL;
9340 		goto free;
9341 	}
9342 
9343 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
9344 					    &params.chandef,
9345 					    wdev->iftype);
9346 	if (err < 0)
9347 		goto free;
9348 
9349 	if (err > 0) {
9350 		params.radar_required = true;
9351 		if (need_handle_dfs_flag &&
9352 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
9353 			err = -EINVAL;
9354 			goto free;
9355 		}
9356 	}
9357 
9358 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
9359 		params.block_tx = true;
9360 
9361 	wdev_lock(wdev);
9362 	err = rdev_channel_switch(rdev, dev, &params);
9363 	wdev_unlock(wdev);
9364 
9365 free:
9366 	kfree(csa_attrs);
9367 	return err;
9368 }
9369 
9370 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
9371 			    u32 seq, int flags,
9372 			    struct cfg80211_registered_device *rdev,
9373 			    struct wireless_dev *wdev,
9374 			    struct cfg80211_internal_bss *intbss)
9375 {
9376 	struct cfg80211_bss *res = &intbss->pub;
9377 	const struct cfg80211_bss_ies *ies;
9378 	void *hdr;
9379 	struct nlattr *bss;
9380 
9381 	ASSERT_WDEV_LOCK(wdev);
9382 
9383 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
9384 			     NL80211_CMD_NEW_SCAN_RESULTS);
9385 	if (!hdr)
9386 		return -1;
9387 
9388 	genl_dump_check_consistent(cb, hdr);
9389 
9390 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
9391 		goto nla_put_failure;
9392 	if (wdev->netdev &&
9393 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
9394 		goto nla_put_failure;
9395 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
9396 			      NL80211_ATTR_PAD))
9397 		goto nla_put_failure;
9398 
9399 	bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
9400 	if (!bss)
9401 		goto nla_put_failure;
9402 	if ((!is_zero_ether_addr(res->bssid) &&
9403 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
9404 		goto nla_put_failure;
9405 
9406 	rcu_read_lock();
9407 	/* indicate whether we have probe response data or not */
9408 	if (rcu_access_pointer(res->proberesp_ies) &&
9409 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
9410 		goto fail_unlock_rcu;
9411 
9412 	/* this pointer prefers to be pointed to probe response data
9413 	 * but is always valid
9414 	 */
9415 	ies = rcu_dereference(res->ies);
9416 	if (ies) {
9417 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
9418 				      NL80211_BSS_PAD))
9419 			goto fail_unlock_rcu;
9420 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
9421 					ies->len, ies->data))
9422 			goto fail_unlock_rcu;
9423 	}
9424 
9425 	/* and this pointer is always (unless driver didn't know) beacon data */
9426 	ies = rcu_dereference(res->beacon_ies);
9427 	if (ies && ies->from_beacon) {
9428 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
9429 				      NL80211_BSS_PAD))
9430 			goto fail_unlock_rcu;
9431 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
9432 					ies->len, ies->data))
9433 			goto fail_unlock_rcu;
9434 	}
9435 	rcu_read_unlock();
9436 
9437 	if (res->beacon_interval &&
9438 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
9439 		goto nla_put_failure;
9440 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
9441 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
9442 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY_OFFSET,
9443 			res->channel->freq_offset) ||
9444 	    nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
9445 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
9446 			jiffies_to_msecs(jiffies - intbss->ts)))
9447 		goto nla_put_failure;
9448 
9449 	if (intbss->parent_tsf &&
9450 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
9451 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
9452 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
9453 		     intbss->parent_bssid)))
9454 		goto nla_put_failure;
9455 
9456 	if (intbss->ts_boottime &&
9457 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
9458 			      intbss->ts_boottime, NL80211_BSS_PAD))
9459 		goto nla_put_failure;
9460 
9461 	if (!nl80211_put_signal(msg, intbss->pub.chains,
9462 				intbss->pub.chain_signal,
9463 				NL80211_BSS_CHAIN_SIGNAL))
9464 		goto nla_put_failure;
9465 
9466 	switch (rdev->wiphy.signal_type) {
9467 	case CFG80211_SIGNAL_TYPE_MBM:
9468 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
9469 			goto nla_put_failure;
9470 		break;
9471 	case CFG80211_SIGNAL_TYPE_UNSPEC:
9472 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
9473 			goto nla_put_failure;
9474 		break;
9475 	default:
9476 		break;
9477 	}
9478 
9479 	switch (wdev->iftype) {
9480 	case NL80211_IFTYPE_P2P_CLIENT:
9481 	case NL80211_IFTYPE_STATION:
9482 		if (intbss == wdev->current_bss &&
9483 		    nla_put_u32(msg, NL80211_BSS_STATUS,
9484 				NL80211_BSS_STATUS_ASSOCIATED))
9485 			goto nla_put_failure;
9486 		break;
9487 	case NL80211_IFTYPE_ADHOC:
9488 		if (intbss == wdev->current_bss &&
9489 		    nla_put_u32(msg, NL80211_BSS_STATUS,
9490 				NL80211_BSS_STATUS_IBSS_JOINED))
9491 			goto nla_put_failure;
9492 		break;
9493 	default:
9494 		break;
9495 	}
9496 
9497 	nla_nest_end(msg, bss);
9498 
9499 	genlmsg_end(msg, hdr);
9500 	return 0;
9501 
9502  fail_unlock_rcu:
9503 	rcu_read_unlock();
9504  nla_put_failure:
9505 	genlmsg_cancel(msg, hdr);
9506 	return -EMSGSIZE;
9507 }
9508 
9509 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
9510 {
9511 	struct cfg80211_registered_device *rdev;
9512 	struct cfg80211_internal_bss *scan;
9513 	struct wireless_dev *wdev;
9514 	int start = cb->args[2], idx = 0;
9515 	int err;
9516 
9517 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
9518 	if (err)
9519 		return err;
9520 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
9521 	__acquire(&rdev->wiphy.mtx);
9522 
9523 	wdev_lock(wdev);
9524 	spin_lock_bh(&rdev->bss_lock);
9525 
9526 	/*
9527 	 * dump_scan will be called multiple times to break up the scan results
9528 	 * into multiple messages.  It is unlikely that any more bss-es will be
9529 	 * expired after the first call, so only call only call this on the
9530 	 * first dump_scan invocation.
9531 	 */
9532 	if (start == 0)
9533 		cfg80211_bss_expire(rdev);
9534 
9535 	cb->seq = rdev->bss_generation;
9536 
9537 	list_for_each_entry(scan, &rdev->bss_list, list) {
9538 		if (++idx <= start)
9539 			continue;
9540 		if (nl80211_send_bss(skb, cb,
9541 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
9542 				rdev, wdev, scan) < 0) {
9543 			idx--;
9544 			break;
9545 		}
9546 	}
9547 
9548 	spin_unlock_bh(&rdev->bss_lock);
9549 	wdev_unlock(wdev);
9550 
9551 	cb->args[2] = idx;
9552 	wiphy_unlock(&rdev->wiphy);
9553 
9554 	return skb->len;
9555 }
9556 
9557 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
9558 			       int flags, struct net_device *dev,
9559 			       bool allow_radio_stats,
9560 			       struct survey_info *survey)
9561 {
9562 	void *hdr;
9563 	struct nlattr *infoattr;
9564 
9565 	/* skip radio stats if userspace didn't request them */
9566 	if (!survey->channel && !allow_radio_stats)
9567 		return 0;
9568 
9569 	hdr = nl80211hdr_put(msg, portid, seq, flags,
9570 			     NL80211_CMD_NEW_SURVEY_RESULTS);
9571 	if (!hdr)
9572 		return -ENOMEM;
9573 
9574 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
9575 		goto nla_put_failure;
9576 
9577 	infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
9578 	if (!infoattr)
9579 		goto nla_put_failure;
9580 
9581 	if (survey->channel &&
9582 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
9583 			survey->channel->center_freq))
9584 		goto nla_put_failure;
9585 
9586 	if (survey->channel && survey->channel->freq_offset &&
9587 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY_OFFSET,
9588 			survey->channel->freq_offset))
9589 		goto nla_put_failure;
9590 
9591 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
9592 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
9593 		goto nla_put_failure;
9594 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
9595 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
9596 		goto nla_put_failure;
9597 	if ((survey->filled & SURVEY_INFO_TIME) &&
9598 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
9599 			survey->time, NL80211_SURVEY_INFO_PAD))
9600 		goto nla_put_failure;
9601 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
9602 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
9603 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
9604 		goto nla_put_failure;
9605 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
9606 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
9607 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
9608 		goto nla_put_failure;
9609 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
9610 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
9611 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
9612 		goto nla_put_failure;
9613 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
9614 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
9615 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
9616 		goto nla_put_failure;
9617 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
9618 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
9619 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
9620 		goto nla_put_failure;
9621 	if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
9622 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
9623 			      survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
9624 		goto nla_put_failure;
9625 
9626 	nla_nest_end(msg, infoattr);
9627 
9628 	genlmsg_end(msg, hdr);
9629 	return 0;
9630 
9631  nla_put_failure:
9632 	genlmsg_cancel(msg, hdr);
9633 	return -EMSGSIZE;
9634 }
9635 
9636 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
9637 {
9638 	struct nlattr **attrbuf;
9639 	struct survey_info survey;
9640 	struct cfg80211_registered_device *rdev;
9641 	struct wireless_dev *wdev;
9642 	int survey_idx = cb->args[2];
9643 	int res;
9644 	bool radio_stats;
9645 
9646 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
9647 	if (!attrbuf)
9648 		return -ENOMEM;
9649 
9650 	res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
9651 	if (res) {
9652 		kfree(attrbuf);
9653 		return res;
9654 	}
9655 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
9656 	__acquire(&rdev->wiphy.mtx);
9657 
9658 	/* prepare_wdev_dump parsed the attributes */
9659 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
9660 
9661 	if (!wdev->netdev) {
9662 		res = -EINVAL;
9663 		goto out_err;
9664 	}
9665 
9666 	if (!rdev->ops->dump_survey) {
9667 		res = -EOPNOTSUPP;
9668 		goto out_err;
9669 	}
9670 
9671 	while (1) {
9672 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
9673 		if (res == -ENOENT)
9674 			break;
9675 		if (res)
9676 			goto out_err;
9677 
9678 		/* don't send disabled channels, but do send non-channel data */
9679 		if (survey.channel &&
9680 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
9681 			survey_idx++;
9682 			continue;
9683 		}
9684 
9685 		if (nl80211_send_survey(skb,
9686 				NETLINK_CB(cb->skb).portid,
9687 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
9688 				wdev->netdev, radio_stats, &survey) < 0)
9689 			goto out;
9690 		survey_idx++;
9691 	}
9692 
9693  out:
9694 	cb->args[2] = survey_idx;
9695 	res = skb->len;
9696  out_err:
9697 	kfree(attrbuf);
9698 	wiphy_unlock(&rdev->wiphy);
9699 	return res;
9700 }
9701 
9702 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
9703 {
9704 	return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
9705 				  NL80211_WPA_VERSION_2 |
9706 				  NL80211_WPA_VERSION_3));
9707 }
9708 
9709 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
9710 {
9711 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9712 	struct net_device *dev = info->user_ptr[1];
9713 	struct ieee80211_channel *chan;
9714 	const u8 *bssid, *ssid, *ie = NULL, *auth_data = NULL;
9715 	int err, ssid_len, ie_len = 0, auth_data_len = 0;
9716 	enum nl80211_auth_type auth_type;
9717 	struct key_parse key;
9718 	bool local_state_change;
9719 	u32 freq;
9720 
9721 	if (!info->attrs[NL80211_ATTR_MAC])
9722 		return -EINVAL;
9723 
9724 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
9725 		return -EINVAL;
9726 
9727 	if (!info->attrs[NL80211_ATTR_SSID])
9728 		return -EINVAL;
9729 
9730 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
9731 		return -EINVAL;
9732 
9733 	err = nl80211_parse_key(info, &key);
9734 	if (err)
9735 		return err;
9736 
9737 	if (key.idx >= 0) {
9738 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
9739 			return -EINVAL;
9740 		if (!key.p.key || !key.p.key_len)
9741 			return -EINVAL;
9742 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
9743 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
9744 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
9745 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
9746 			return -EINVAL;
9747 		if (key.idx > 3)
9748 			return -EINVAL;
9749 	} else {
9750 		key.p.key_len = 0;
9751 		key.p.key = NULL;
9752 	}
9753 
9754 	if (key.idx >= 0) {
9755 		int i;
9756 		bool ok = false;
9757 
9758 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
9759 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
9760 				ok = true;
9761 				break;
9762 			}
9763 		}
9764 		if (!ok)
9765 			return -EINVAL;
9766 	}
9767 
9768 	if (!rdev->ops->auth)
9769 		return -EOPNOTSUPP;
9770 
9771 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9772 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9773 		return -EOPNOTSUPP;
9774 
9775 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9776 	freq = MHZ_TO_KHZ(nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
9777 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
9778 		freq +=
9779 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
9780 
9781 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
9782 	if (!chan)
9783 		return -EINVAL;
9784 
9785 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9786 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9787 
9788 	if (info->attrs[NL80211_ATTR_IE]) {
9789 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9790 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9791 	}
9792 
9793 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
9794 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
9795 		return -EINVAL;
9796 
9797 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
9798 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
9799 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
9800 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
9801 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
9802 		return -EINVAL;
9803 
9804 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
9805 		if (auth_type != NL80211_AUTHTYPE_SAE &&
9806 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
9807 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
9808 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
9809 			return -EINVAL;
9810 		auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
9811 		auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
9812 	}
9813 
9814 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9815 
9816 	/*
9817 	 * Since we no longer track auth state, ignore
9818 	 * requests to only change local state.
9819 	 */
9820 	if (local_state_change)
9821 		return 0;
9822 
9823 	wdev_lock(dev->ieee80211_ptr);
9824 	err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
9825 				 ssid, ssid_len, ie, ie_len,
9826 				 key.p.key, key.p.key_len, key.idx,
9827 				 auth_data, auth_data_len);
9828 	wdev_unlock(dev->ieee80211_ptr);
9829 	return err;
9830 }
9831 
9832 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
9833 				     struct genl_info *info)
9834 {
9835 	if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9836 		GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
9837 		return -EINVAL;
9838 	}
9839 
9840 	if (!rdev->ops->tx_control_port ||
9841 	    !wiphy_ext_feature_isset(&rdev->wiphy,
9842 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
9843 		return -EOPNOTSUPP;
9844 
9845 	return 0;
9846 }
9847 
9848 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
9849 				   struct genl_info *info,
9850 				   struct cfg80211_crypto_settings *settings,
9851 				   int cipher_limit)
9852 {
9853 	memset(settings, 0, sizeof(*settings));
9854 
9855 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
9856 
9857 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
9858 		u16 proto;
9859 
9860 		proto = nla_get_u16(
9861 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
9862 		settings->control_port_ethertype = cpu_to_be16(proto);
9863 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
9864 		    proto != ETH_P_PAE)
9865 			return -EINVAL;
9866 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
9867 			settings->control_port_no_encrypt = true;
9868 	} else
9869 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
9870 
9871 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
9872 		int r = validate_pae_over_nl80211(rdev, info);
9873 
9874 		if (r < 0)
9875 			return r;
9876 
9877 		settings->control_port_over_nl80211 = true;
9878 
9879 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH])
9880 			settings->control_port_no_preauth = true;
9881 	}
9882 
9883 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
9884 		void *data;
9885 		int len, i;
9886 
9887 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9888 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9889 		settings->n_ciphers_pairwise = len / sizeof(u32);
9890 
9891 		if (len % sizeof(u32))
9892 			return -EINVAL;
9893 
9894 		if (settings->n_ciphers_pairwise > cipher_limit)
9895 			return -EINVAL;
9896 
9897 		memcpy(settings->ciphers_pairwise, data, len);
9898 
9899 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
9900 			if (!cfg80211_supported_cipher_suite(
9901 					&rdev->wiphy,
9902 					settings->ciphers_pairwise[i]))
9903 				return -EINVAL;
9904 	}
9905 
9906 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
9907 		settings->cipher_group =
9908 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
9909 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
9910 						     settings->cipher_group))
9911 			return -EINVAL;
9912 	}
9913 
9914 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
9915 		settings->wpa_versions =
9916 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
9917 		if (!nl80211_valid_wpa_versions(settings->wpa_versions))
9918 			return -EINVAL;
9919 	}
9920 
9921 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
9922 		void *data;
9923 		int len;
9924 
9925 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
9926 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
9927 		settings->n_akm_suites = len / sizeof(u32);
9928 
9929 		if (len % sizeof(u32))
9930 			return -EINVAL;
9931 
9932 		if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
9933 			return -EINVAL;
9934 
9935 		memcpy(settings->akm_suites, data, len);
9936 	}
9937 
9938 	if (info->attrs[NL80211_ATTR_PMK]) {
9939 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
9940 			return -EINVAL;
9941 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9942 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK) &&
9943 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9944 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_AP_PSK))
9945 			return -EINVAL;
9946 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
9947 	}
9948 
9949 	if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
9950 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9951 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
9952 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9953 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP))
9954 			return -EINVAL;
9955 		settings->sae_pwd =
9956 			nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9957 		settings->sae_pwd_len =
9958 			nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9959 	}
9960 
9961 	if (info->attrs[NL80211_ATTR_SAE_PWE])
9962 		settings->sae_pwe =
9963 			nla_get_u8(info->attrs[NL80211_ATTR_SAE_PWE]);
9964 	else
9965 		settings->sae_pwe = NL80211_SAE_PWE_UNSPECIFIED;
9966 
9967 	return 0;
9968 }
9969 
9970 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
9971 {
9972 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9973 	struct net_device *dev = info->user_ptr[1];
9974 	struct ieee80211_channel *chan;
9975 	struct cfg80211_assoc_request req = {};
9976 	const u8 *bssid, *ssid;
9977 	int err, ssid_len = 0;
9978 	u32 freq;
9979 
9980 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9981 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9982 		return -EPERM;
9983 
9984 	if (!info->attrs[NL80211_ATTR_MAC] ||
9985 	    !info->attrs[NL80211_ATTR_SSID] ||
9986 	    !info->attrs[NL80211_ATTR_WIPHY_FREQ])
9987 		return -EINVAL;
9988 
9989 	if (!rdev->ops->assoc)
9990 		return -EOPNOTSUPP;
9991 
9992 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9993 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9994 		return -EOPNOTSUPP;
9995 
9996 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9997 
9998 	freq = MHZ_TO_KHZ(nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
9999 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10000 		freq +=
10001 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10002 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10003 	if (!chan)
10004 		return -EINVAL;
10005 
10006 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10007 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10008 
10009 	if (info->attrs[NL80211_ATTR_IE]) {
10010 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10011 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10012 	}
10013 
10014 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
10015 		enum nl80211_mfp mfp =
10016 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
10017 		if (mfp == NL80211_MFP_REQUIRED)
10018 			req.use_mfp = true;
10019 		else if (mfp != NL80211_MFP_NO)
10020 			return -EINVAL;
10021 	}
10022 
10023 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
10024 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
10025 
10026 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
10027 		req.flags |= ASSOC_REQ_DISABLE_HT;
10028 
10029 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10030 		memcpy(&req.ht_capa_mask,
10031 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
10032 		       sizeof(req.ht_capa_mask));
10033 
10034 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
10035 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10036 			return -EINVAL;
10037 		memcpy(&req.ht_capa,
10038 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
10039 		       sizeof(req.ht_capa));
10040 	}
10041 
10042 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
10043 		req.flags |= ASSOC_REQ_DISABLE_VHT;
10044 
10045 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
10046 		req.flags |= ASSOC_REQ_DISABLE_HE;
10047 
10048 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10049 		memcpy(&req.vht_capa_mask,
10050 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
10051 		       sizeof(req.vht_capa_mask));
10052 
10053 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
10054 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10055 			return -EINVAL;
10056 		memcpy(&req.vht_capa,
10057 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
10058 		       sizeof(req.vht_capa));
10059 	}
10060 
10061 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
10062 		if (!((rdev->wiphy.features &
10063 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
10064 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
10065 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10066 					     NL80211_EXT_FEATURE_RRM))
10067 			return -EINVAL;
10068 		req.flags |= ASSOC_REQ_USE_RRM;
10069 	}
10070 
10071 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
10072 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
10073 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
10074 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
10075 			return -EINVAL;
10076 		req.fils_nonces =
10077 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
10078 	}
10079 
10080 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]) {
10081 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY])
10082 			return -EINVAL;
10083 		memcpy(&req.s1g_capa_mask,
10084 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]),
10085 		       sizeof(req.s1g_capa_mask));
10086 	}
10087 
10088 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY]) {
10089 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK])
10090 			return -EINVAL;
10091 		memcpy(&req.s1g_capa,
10092 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY]),
10093 		       sizeof(req.s1g_capa));
10094 	}
10095 
10096 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
10097 	if (!err) {
10098 		wdev_lock(dev->ieee80211_ptr);
10099 
10100 		err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
10101 					  ssid, ssid_len, &req);
10102 
10103 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10104 			dev->ieee80211_ptr->conn_owner_nlportid =
10105 				info->snd_portid;
10106 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
10107 			       bssid, ETH_ALEN);
10108 		}
10109 
10110 		wdev_unlock(dev->ieee80211_ptr);
10111 	}
10112 
10113 	return err;
10114 }
10115 
10116 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
10117 {
10118 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10119 	struct net_device *dev = info->user_ptr[1];
10120 	const u8 *ie = NULL, *bssid;
10121 	int ie_len = 0, err;
10122 	u16 reason_code;
10123 	bool local_state_change;
10124 
10125 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
10126 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10127 		return -EPERM;
10128 
10129 	if (!info->attrs[NL80211_ATTR_MAC])
10130 		return -EINVAL;
10131 
10132 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
10133 		return -EINVAL;
10134 
10135 	if (!rdev->ops->deauth)
10136 		return -EOPNOTSUPP;
10137 
10138 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10139 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10140 		return -EOPNOTSUPP;
10141 
10142 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10143 
10144 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
10145 	if (reason_code == 0) {
10146 		/* Reason Code 0 is reserved */
10147 		return -EINVAL;
10148 	}
10149 
10150 	if (info->attrs[NL80211_ATTR_IE]) {
10151 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10152 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10153 	}
10154 
10155 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
10156 
10157 	wdev_lock(dev->ieee80211_ptr);
10158 	err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
10159 				   local_state_change);
10160 	wdev_unlock(dev->ieee80211_ptr);
10161 	return err;
10162 }
10163 
10164 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
10165 {
10166 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10167 	struct net_device *dev = info->user_ptr[1];
10168 	const u8 *ie = NULL, *bssid;
10169 	int ie_len = 0, err;
10170 	u16 reason_code;
10171 	bool local_state_change;
10172 
10173 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
10174 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10175 		return -EPERM;
10176 
10177 	if (!info->attrs[NL80211_ATTR_MAC])
10178 		return -EINVAL;
10179 
10180 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
10181 		return -EINVAL;
10182 
10183 	if (!rdev->ops->disassoc)
10184 		return -EOPNOTSUPP;
10185 
10186 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10187 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10188 		return -EOPNOTSUPP;
10189 
10190 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10191 
10192 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
10193 	if (reason_code == 0) {
10194 		/* Reason Code 0 is reserved */
10195 		return -EINVAL;
10196 	}
10197 
10198 	if (info->attrs[NL80211_ATTR_IE]) {
10199 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10200 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10201 	}
10202 
10203 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
10204 
10205 	wdev_lock(dev->ieee80211_ptr);
10206 	err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
10207 				     local_state_change);
10208 	wdev_unlock(dev->ieee80211_ptr);
10209 	return err;
10210 }
10211 
10212 static bool
10213 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
10214 			 int mcast_rate[NUM_NL80211_BANDS],
10215 			 int rateval)
10216 {
10217 	struct wiphy *wiphy = &rdev->wiphy;
10218 	bool found = false;
10219 	int band, i;
10220 
10221 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
10222 		struct ieee80211_supported_band *sband;
10223 
10224 		sband = wiphy->bands[band];
10225 		if (!sband)
10226 			continue;
10227 
10228 		for (i = 0; i < sband->n_bitrates; i++) {
10229 			if (sband->bitrates[i].bitrate == rateval) {
10230 				mcast_rate[band] = i + 1;
10231 				found = true;
10232 				break;
10233 			}
10234 		}
10235 	}
10236 
10237 	return found;
10238 }
10239 
10240 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
10241 {
10242 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10243 	struct net_device *dev = info->user_ptr[1];
10244 	struct cfg80211_ibss_params ibss;
10245 	struct wiphy *wiphy;
10246 	struct cfg80211_cached_keys *connkeys = NULL;
10247 	int err;
10248 
10249 	memset(&ibss, 0, sizeof(ibss));
10250 
10251 	if (!info->attrs[NL80211_ATTR_SSID] ||
10252 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
10253 		return -EINVAL;
10254 
10255 	ibss.beacon_interval = 100;
10256 
10257 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
10258 		ibss.beacon_interval =
10259 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
10260 
10261 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
10262 					   ibss.beacon_interval);
10263 	if (err)
10264 		return err;
10265 
10266 	if (!rdev->ops->join_ibss)
10267 		return -EOPNOTSUPP;
10268 
10269 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
10270 		return -EOPNOTSUPP;
10271 
10272 	wiphy = &rdev->wiphy;
10273 
10274 	if (info->attrs[NL80211_ATTR_MAC]) {
10275 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10276 
10277 		if (!is_valid_ether_addr(ibss.bssid))
10278 			return -EINVAL;
10279 	}
10280 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10281 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10282 
10283 	if (info->attrs[NL80211_ATTR_IE]) {
10284 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10285 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10286 	}
10287 
10288 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
10289 	if (err)
10290 		return err;
10291 
10292 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
10293 				     NL80211_IFTYPE_ADHOC))
10294 		return -EINVAL;
10295 
10296 	switch (ibss.chandef.width) {
10297 	case NL80211_CHAN_WIDTH_5:
10298 	case NL80211_CHAN_WIDTH_10:
10299 	case NL80211_CHAN_WIDTH_20_NOHT:
10300 		break;
10301 	case NL80211_CHAN_WIDTH_20:
10302 	case NL80211_CHAN_WIDTH_40:
10303 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
10304 			return -EINVAL;
10305 		break;
10306 	case NL80211_CHAN_WIDTH_80:
10307 	case NL80211_CHAN_WIDTH_80P80:
10308 	case NL80211_CHAN_WIDTH_160:
10309 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
10310 			return -EINVAL;
10311 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10312 					     NL80211_EXT_FEATURE_VHT_IBSS))
10313 			return -EINVAL;
10314 		break;
10315 	default:
10316 		return -EINVAL;
10317 	}
10318 
10319 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
10320 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
10321 
10322 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
10323 		u8 *rates =
10324 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
10325 		int n_rates =
10326 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
10327 		struct ieee80211_supported_band *sband =
10328 			wiphy->bands[ibss.chandef.chan->band];
10329 
10330 		err = ieee80211_get_ratemask(sband, rates, n_rates,
10331 					     &ibss.basic_rates);
10332 		if (err)
10333 			return err;
10334 	}
10335 
10336 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10337 		memcpy(&ibss.ht_capa_mask,
10338 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
10339 		       sizeof(ibss.ht_capa_mask));
10340 
10341 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
10342 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10343 			return -EINVAL;
10344 		memcpy(&ibss.ht_capa,
10345 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
10346 		       sizeof(ibss.ht_capa));
10347 	}
10348 
10349 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
10350 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
10351 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
10352 		return -EINVAL;
10353 
10354 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
10355 		bool no_ht = false;
10356 
10357 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
10358 		if (IS_ERR(connkeys))
10359 			return PTR_ERR(connkeys);
10360 
10361 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
10362 		    no_ht) {
10363 			kfree_sensitive(connkeys);
10364 			return -EINVAL;
10365 		}
10366 	}
10367 
10368 	ibss.control_port =
10369 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
10370 
10371 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
10372 		int r = validate_pae_over_nl80211(rdev, info);
10373 
10374 		if (r < 0) {
10375 			kfree_sensitive(connkeys);
10376 			return r;
10377 		}
10378 
10379 		ibss.control_port_over_nl80211 = true;
10380 	}
10381 
10382 	ibss.userspace_handles_dfs =
10383 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
10384 
10385 	wdev_lock(dev->ieee80211_ptr);
10386 	err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
10387 	if (err)
10388 		kfree_sensitive(connkeys);
10389 	else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
10390 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
10391 	wdev_unlock(dev->ieee80211_ptr);
10392 
10393 	return err;
10394 }
10395 
10396 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
10397 {
10398 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10399 	struct net_device *dev = info->user_ptr[1];
10400 
10401 	if (!rdev->ops->leave_ibss)
10402 		return -EOPNOTSUPP;
10403 
10404 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
10405 		return -EOPNOTSUPP;
10406 
10407 	return cfg80211_leave_ibss(rdev, dev, false);
10408 }
10409 
10410 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
10411 {
10412 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10413 	struct net_device *dev = info->user_ptr[1];
10414 	int mcast_rate[NUM_NL80211_BANDS];
10415 	u32 nla_rate;
10416 	int err;
10417 
10418 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
10419 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
10420 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
10421 		return -EOPNOTSUPP;
10422 
10423 	if (!rdev->ops->set_mcast_rate)
10424 		return -EOPNOTSUPP;
10425 
10426 	memset(mcast_rate, 0, sizeof(mcast_rate));
10427 
10428 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
10429 		return -EINVAL;
10430 
10431 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
10432 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
10433 		return -EINVAL;
10434 
10435 	err = rdev_set_mcast_rate(rdev, dev, mcast_rate);
10436 
10437 	return err;
10438 }
10439 
10440 static struct sk_buff *
10441 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
10442 			    struct wireless_dev *wdev, int approxlen,
10443 			    u32 portid, u32 seq, enum nl80211_commands cmd,
10444 			    enum nl80211_attrs attr,
10445 			    const struct nl80211_vendor_cmd_info *info,
10446 			    gfp_t gfp)
10447 {
10448 	struct sk_buff *skb;
10449 	void *hdr;
10450 	struct nlattr *data;
10451 
10452 	skb = nlmsg_new(approxlen + 100, gfp);
10453 	if (!skb)
10454 		return NULL;
10455 
10456 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
10457 	if (!hdr) {
10458 		kfree_skb(skb);
10459 		return NULL;
10460 	}
10461 
10462 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
10463 		goto nla_put_failure;
10464 
10465 	if (info) {
10466 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
10467 				info->vendor_id))
10468 			goto nla_put_failure;
10469 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
10470 				info->subcmd))
10471 			goto nla_put_failure;
10472 	}
10473 
10474 	if (wdev) {
10475 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
10476 				      wdev_id(wdev), NL80211_ATTR_PAD))
10477 			goto nla_put_failure;
10478 		if (wdev->netdev &&
10479 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
10480 				wdev->netdev->ifindex))
10481 			goto nla_put_failure;
10482 	}
10483 
10484 	data = nla_nest_start_noflag(skb, attr);
10485 	if (!data)
10486 		goto nla_put_failure;
10487 
10488 	((void **)skb->cb)[0] = rdev;
10489 	((void **)skb->cb)[1] = hdr;
10490 	((void **)skb->cb)[2] = data;
10491 
10492 	return skb;
10493 
10494  nla_put_failure:
10495 	kfree_skb(skb);
10496 	return NULL;
10497 }
10498 
10499 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
10500 					   struct wireless_dev *wdev,
10501 					   enum nl80211_commands cmd,
10502 					   enum nl80211_attrs attr,
10503 					   unsigned int portid,
10504 					   int vendor_event_idx,
10505 					   int approxlen, gfp_t gfp)
10506 {
10507 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
10508 	const struct nl80211_vendor_cmd_info *info;
10509 
10510 	switch (cmd) {
10511 	case NL80211_CMD_TESTMODE:
10512 		if (WARN_ON(vendor_event_idx != -1))
10513 			return NULL;
10514 		info = NULL;
10515 		break;
10516 	case NL80211_CMD_VENDOR:
10517 		if (WARN_ON(vendor_event_idx < 0 ||
10518 			    vendor_event_idx >= wiphy->n_vendor_events))
10519 			return NULL;
10520 		info = &wiphy->vendor_events[vendor_event_idx];
10521 		break;
10522 	default:
10523 		WARN_ON(1);
10524 		return NULL;
10525 	}
10526 
10527 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
10528 					   cmd, attr, info, gfp);
10529 }
10530 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
10531 
10532 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
10533 {
10534 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
10535 	void *hdr = ((void **)skb->cb)[1];
10536 	struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
10537 	struct nlattr *data = ((void **)skb->cb)[2];
10538 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
10539 
10540 	/* clear CB data for netlink core to own from now on */
10541 	memset(skb->cb, 0, sizeof(skb->cb));
10542 
10543 	nla_nest_end(skb, data);
10544 	genlmsg_end(skb, hdr);
10545 
10546 	if (nlhdr->nlmsg_pid) {
10547 		genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
10548 				nlhdr->nlmsg_pid);
10549 	} else {
10550 		if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
10551 			mcgrp = NL80211_MCGRP_VENDOR;
10552 
10553 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
10554 					skb, 0, mcgrp, gfp);
10555 	}
10556 }
10557 EXPORT_SYMBOL(__cfg80211_send_event_skb);
10558 
10559 #ifdef CONFIG_NL80211_TESTMODE
10560 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
10561 {
10562 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10563 	struct wireless_dev *wdev;
10564 	int err;
10565 
10566 	lockdep_assert_held(&rdev->wiphy.mtx);
10567 
10568 	wdev = __cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
10569 					  info->attrs);
10570 
10571 	if (!rdev->ops->testmode_cmd)
10572 		return -EOPNOTSUPP;
10573 
10574 	if (IS_ERR(wdev)) {
10575 		err = PTR_ERR(wdev);
10576 		if (err != -EINVAL)
10577 			return err;
10578 		wdev = NULL;
10579 	} else if (wdev->wiphy != &rdev->wiphy) {
10580 		return -EINVAL;
10581 	}
10582 
10583 	if (!info->attrs[NL80211_ATTR_TESTDATA])
10584 		return -EINVAL;
10585 
10586 	rdev->cur_cmd_info = info;
10587 	err = rdev_testmode_cmd(rdev, wdev,
10588 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
10589 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
10590 	rdev->cur_cmd_info = NULL;
10591 
10592 	return err;
10593 }
10594 
10595 static int nl80211_testmode_dump(struct sk_buff *skb,
10596 				 struct netlink_callback *cb)
10597 {
10598 	struct cfg80211_registered_device *rdev;
10599 	struct nlattr **attrbuf = NULL;
10600 	int err;
10601 	long phy_idx;
10602 	void *data = NULL;
10603 	int data_len = 0;
10604 
10605 	rtnl_lock();
10606 
10607 	if (cb->args[0]) {
10608 		/*
10609 		 * 0 is a valid index, but not valid for args[0],
10610 		 * so we need to offset by 1.
10611 		 */
10612 		phy_idx = cb->args[0] - 1;
10613 
10614 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
10615 		if (!rdev) {
10616 			err = -ENOENT;
10617 			goto out_err;
10618 		}
10619 	} else {
10620 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
10621 				  GFP_KERNEL);
10622 		if (!attrbuf) {
10623 			err = -ENOMEM;
10624 			goto out_err;
10625 		}
10626 
10627 		err = nlmsg_parse_deprecated(cb->nlh,
10628 					     GENL_HDRLEN + nl80211_fam.hdrsize,
10629 					     attrbuf, nl80211_fam.maxattr,
10630 					     nl80211_policy, NULL);
10631 		if (err)
10632 			goto out_err;
10633 
10634 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
10635 		if (IS_ERR(rdev)) {
10636 			err = PTR_ERR(rdev);
10637 			goto out_err;
10638 		}
10639 		phy_idx = rdev->wiphy_idx;
10640 
10641 		if (attrbuf[NL80211_ATTR_TESTDATA])
10642 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
10643 	}
10644 
10645 	if (cb->args[1]) {
10646 		data = nla_data((void *)cb->args[1]);
10647 		data_len = nla_len((void *)cb->args[1]);
10648 	}
10649 
10650 	if (!rdev->ops->testmode_dump) {
10651 		err = -EOPNOTSUPP;
10652 		goto out_err;
10653 	}
10654 
10655 	while (1) {
10656 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
10657 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
10658 					   NL80211_CMD_TESTMODE);
10659 		struct nlattr *tmdata;
10660 
10661 		if (!hdr)
10662 			break;
10663 
10664 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
10665 			genlmsg_cancel(skb, hdr);
10666 			break;
10667 		}
10668 
10669 		tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
10670 		if (!tmdata) {
10671 			genlmsg_cancel(skb, hdr);
10672 			break;
10673 		}
10674 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
10675 		nla_nest_end(skb, tmdata);
10676 
10677 		if (err == -ENOBUFS || err == -ENOENT) {
10678 			genlmsg_cancel(skb, hdr);
10679 			break;
10680 		} else if (err) {
10681 			genlmsg_cancel(skb, hdr);
10682 			goto out_err;
10683 		}
10684 
10685 		genlmsg_end(skb, hdr);
10686 	}
10687 
10688 	err = skb->len;
10689 	/* see above */
10690 	cb->args[0] = phy_idx + 1;
10691  out_err:
10692 	kfree(attrbuf);
10693 	rtnl_unlock();
10694 	return err;
10695 }
10696 #endif
10697 
10698 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
10699 {
10700 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10701 	struct net_device *dev = info->user_ptr[1];
10702 	struct cfg80211_connect_params connect;
10703 	struct wiphy *wiphy;
10704 	struct cfg80211_cached_keys *connkeys = NULL;
10705 	u32 freq = 0;
10706 	int err;
10707 
10708 	memset(&connect, 0, sizeof(connect));
10709 
10710 	if (!info->attrs[NL80211_ATTR_SSID] ||
10711 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
10712 		return -EINVAL;
10713 
10714 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
10715 		connect.auth_type =
10716 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10717 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
10718 					     NL80211_CMD_CONNECT))
10719 			return -EINVAL;
10720 	} else
10721 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
10722 
10723 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
10724 
10725 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
10726 	    !wiphy_ext_feature_isset(&rdev->wiphy,
10727 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
10728 		return -EINVAL;
10729 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
10730 
10731 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
10732 				      NL80211_MAX_NR_CIPHER_SUITES);
10733 	if (err)
10734 		return err;
10735 
10736 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10737 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10738 		return -EOPNOTSUPP;
10739 
10740 	wiphy = &rdev->wiphy;
10741 
10742 	connect.bg_scan_period = -1;
10743 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
10744 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
10745 		connect.bg_scan_period =
10746 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
10747 	}
10748 
10749 	if (info->attrs[NL80211_ATTR_MAC])
10750 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10751 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
10752 		connect.bssid_hint =
10753 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
10754 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10755 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10756 
10757 	if (info->attrs[NL80211_ATTR_IE]) {
10758 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10759 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10760 	}
10761 
10762 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
10763 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
10764 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
10765 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10766 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
10767 			return -EOPNOTSUPP;
10768 	} else {
10769 		connect.mfp = NL80211_MFP_NO;
10770 	}
10771 
10772 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
10773 		connect.prev_bssid =
10774 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
10775 
10776 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ])
10777 		freq = MHZ_TO_KHZ(nla_get_u32(
10778 					info->attrs[NL80211_ATTR_WIPHY_FREQ]));
10779 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10780 		freq +=
10781 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10782 
10783 	if (freq) {
10784 		connect.channel = nl80211_get_valid_chan(wiphy, freq);
10785 		if (!connect.channel)
10786 			return -EINVAL;
10787 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
10788 		freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
10789 		freq = MHZ_TO_KHZ(freq);
10790 		connect.channel_hint = nl80211_get_valid_chan(wiphy, freq);
10791 		if (!connect.channel_hint)
10792 			return -EINVAL;
10793 	}
10794 
10795 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
10796 		connect.edmg.channels =
10797 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
10798 
10799 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
10800 			connect.edmg.bw_config =
10801 				nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
10802 	}
10803 
10804 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
10805 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
10806 		if (IS_ERR(connkeys))
10807 			return PTR_ERR(connkeys);
10808 	}
10809 
10810 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
10811 		connect.flags |= ASSOC_REQ_DISABLE_HT;
10812 
10813 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10814 		memcpy(&connect.ht_capa_mask,
10815 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
10816 		       sizeof(connect.ht_capa_mask));
10817 
10818 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
10819 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
10820 			kfree_sensitive(connkeys);
10821 			return -EINVAL;
10822 		}
10823 		memcpy(&connect.ht_capa,
10824 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
10825 		       sizeof(connect.ht_capa));
10826 	}
10827 
10828 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
10829 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
10830 
10831 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
10832 		connect.flags |= ASSOC_REQ_DISABLE_HE;
10833 
10834 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10835 		memcpy(&connect.vht_capa_mask,
10836 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
10837 		       sizeof(connect.vht_capa_mask));
10838 
10839 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
10840 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
10841 			kfree_sensitive(connkeys);
10842 			return -EINVAL;
10843 		}
10844 		memcpy(&connect.vht_capa,
10845 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
10846 		       sizeof(connect.vht_capa));
10847 	}
10848 
10849 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
10850 		if (!((rdev->wiphy.features &
10851 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
10852 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
10853 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10854 					     NL80211_EXT_FEATURE_RRM)) {
10855 			kfree_sensitive(connkeys);
10856 			return -EINVAL;
10857 		}
10858 		connect.flags |= ASSOC_REQ_USE_RRM;
10859 	}
10860 
10861 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
10862 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
10863 		kfree_sensitive(connkeys);
10864 		return -EOPNOTSUPP;
10865 	}
10866 
10867 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
10868 		/* bss selection makes no sense if bssid is set */
10869 		if (connect.bssid) {
10870 			kfree_sensitive(connkeys);
10871 			return -EINVAL;
10872 		}
10873 
10874 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
10875 				       wiphy, &connect.bss_select);
10876 		if (err) {
10877 			kfree_sensitive(connkeys);
10878 			return err;
10879 		}
10880 	}
10881 
10882 	if (wiphy_ext_feature_isset(&rdev->wiphy,
10883 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
10884 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
10885 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
10886 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
10887 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10888 		connect.fils_erp_username =
10889 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10890 		connect.fils_erp_username_len =
10891 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10892 		connect.fils_erp_realm =
10893 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10894 		connect.fils_erp_realm_len =
10895 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10896 		connect.fils_erp_next_seq_num =
10897 			nla_get_u16(
10898 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
10899 		connect.fils_erp_rrk =
10900 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10901 		connect.fils_erp_rrk_len =
10902 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10903 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
10904 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
10905 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
10906 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10907 		kfree_sensitive(connkeys);
10908 		return -EINVAL;
10909 	}
10910 
10911 	if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
10912 		if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10913 			kfree_sensitive(connkeys);
10914 			GENL_SET_ERR_MSG(info,
10915 					 "external auth requires connection ownership");
10916 			return -EINVAL;
10917 		}
10918 		connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
10919 	}
10920 
10921 	wdev_lock(dev->ieee80211_ptr);
10922 
10923 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
10924 			       connect.prev_bssid);
10925 	if (err)
10926 		kfree_sensitive(connkeys);
10927 
10928 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10929 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
10930 		if (connect.bssid)
10931 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
10932 			       connect.bssid, ETH_ALEN);
10933 		else
10934 			eth_zero_addr(dev->ieee80211_ptr->disconnect_bssid);
10935 	}
10936 
10937 	wdev_unlock(dev->ieee80211_ptr);
10938 
10939 	return err;
10940 }
10941 
10942 static int nl80211_update_connect_params(struct sk_buff *skb,
10943 					 struct genl_info *info)
10944 {
10945 	struct cfg80211_connect_params connect = {};
10946 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10947 	struct net_device *dev = info->user_ptr[1];
10948 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10949 	bool fils_sk_offload;
10950 	u32 auth_type;
10951 	u32 changed = 0;
10952 	int ret;
10953 
10954 	if (!rdev->ops->update_connect_params)
10955 		return -EOPNOTSUPP;
10956 
10957 	if (info->attrs[NL80211_ATTR_IE]) {
10958 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10959 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10960 		changed |= UPDATE_ASSOC_IES;
10961 	}
10962 
10963 	fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
10964 						  NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
10965 
10966 	/*
10967 	 * when driver supports fils-sk offload all attributes must be
10968 	 * provided. So the else covers "fils-sk-not-all" and
10969 	 * "no-fils-sk-any".
10970 	 */
10971 	if (fils_sk_offload &&
10972 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
10973 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
10974 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
10975 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10976 		connect.fils_erp_username =
10977 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10978 		connect.fils_erp_username_len =
10979 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10980 		connect.fils_erp_realm =
10981 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10982 		connect.fils_erp_realm_len =
10983 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10984 		connect.fils_erp_next_seq_num =
10985 			nla_get_u16(
10986 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
10987 		connect.fils_erp_rrk =
10988 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10989 		connect.fils_erp_rrk_len =
10990 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10991 		changed |= UPDATE_FILS_ERP_INFO;
10992 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
10993 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
10994 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
10995 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10996 		return -EINVAL;
10997 	}
10998 
10999 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
11000 		auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
11001 		if (!nl80211_valid_auth_type(rdev, auth_type,
11002 					     NL80211_CMD_CONNECT))
11003 			return -EINVAL;
11004 
11005 		if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
11006 		    fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
11007 			return -EINVAL;
11008 
11009 		connect.auth_type = auth_type;
11010 		changed |= UPDATE_AUTH_TYPE;
11011 	}
11012 
11013 	wdev_lock(dev->ieee80211_ptr);
11014 	if (!wdev->current_bss)
11015 		ret = -ENOLINK;
11016 	else
11017 		ret = rdev_update_connect_params(rdev, dev, &connect, changed);
11018 	wdev_unlock(dev->ieee80211_ptr);
11019 
11020 	return ret;
11021 }
11022 
11023 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
11024 {
11025 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11026 	struct net_device *dev = info->user_ptr[1];
11027 	u16 reason;
11028 	int ret;
11029 
11030 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11031 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11032 		return -EPERM;
11033 
11034 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11035 		reason = WLAN_REASON_DEAUTH_LEAVING;
11036 	else
11037 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11038 
11039 	if (reason == 0)
11040 		return -EINVAL;
11041 
11042 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11043 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11044 		return -EOPNOTSUPP;
11045 
11046 	wdev_lock(dev->ieee80211_ptr);
11047 	ret = cfg80211_disconnect(rdev, dev, reason, true);
11048 	wdev_unlock(dev->ieee80211_ptr);
11049 	return ret;
11050 }
11051 
11052 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
11053 {
11054 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11055 	struct net *net;
11056 	int err;
11057 
11058 	if (info->attrs[NL80211_ATTR_PID]) {
11059 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
11060 
11061 		net = get_net_ns_by_pid(pid);
11062 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
11063 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
11064 
11065 		net = get_net_ns_by_fd(fd);
11066 	} else {
11067 		return -EINVAL;
11068 	}
11069 
11070 	if (IS_ERR(net))
11071 		return PTR_ERR(net);
11072 
11073 	err = 0;
11074 
11075 	/* check if anything to do */
11076 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
11077 		err = cfg80211_switch_netns(rdev, net);
11078 
11079 	put_net(net);
11080 	return err;
11081 }
11082 
11083 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
11084 {
11085 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11086 	int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
11087 			struct cfg80211_pmksa *pmksa) = NULL;
11088 	struct net_device *dev = info->user_ptr[1];
11089 	struct cfg80211_pmksa pmksa;
11090 
11091 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
11092 
11093 	if (!info->attrs[NL80211_ATTR_PMKID])
11094 		return -EINVAL;
11095 
11096 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
11097 
11098 	if (info->attrs[NL80211_ATTR_MAC]) {
11099 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11100 	} else if (info->attrs[NL80211_ATTR_SSID] &&
11101 		   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
11102 		   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
11103 		    info->attrs[NL80211_ATTR_PMK])) {
11104 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11105 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11106 		pmksa.cache_id =
11107 			nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
11108 	} else {
11109 		return -EINVAL;
11110 	}
11111 	if (info->attrs[NL80211_ATTR_PMK]) {
11112 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
11113 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
11114 	}
11115 
11116 	if (info->attrs[NL80211_ATTR_PMK_LIFETIME])
11117 		pmksa.pmk_lifetime =
11118 			nla_get_u32(info->attrs[NL80211_ATTR_PMK_LIFETIME]);
11119 
11120 	if (info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD])
11121 		pmksa.pmk_reauth_threshold =
11122 			nla_get_u8(
11123 				info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD]);
11124 
11125 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11126 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
11127 	    !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
11128 	      wiphy_ext_feature_isset(&rdev->wiphy,
11129 				      NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
11130 		return -EOPNOTSUPP;
11131 
11132 	switch (info->genlhdr->cmd) {
11133 	case NL80211_CMD_SET_PMKSA:
11134 		rdev_ops = rdev->ops->set_pmksa;
11135 		break;
11136 	case NL80211_CMD_DEL_PMKSA:
11137 		rdev_ops = rdev->ops->del_pmksa;
11138 		break;
11139 	default:
11140 		WARN_ON(1);
11141 		break;
11142 	}
11143 
11144 	if (!rdev_ops)
11145 		return -EOPNOTSUPP;
11146 
11147 	return rdev_ops(&rdev->wiphy, dev, &pmksa);
11148 }
11149 
11150 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
11151 {
11152 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11153 	struct net_device *dev = info->user_ptr[1];
11154 
11155 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11156 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11157 		return -EOPNOTSUPP;
11158 
11159 	if (!rdev->ops->flush_pmksa)
11160 		return -EOPNOTSUPP;
11161 
11162 	return rdev_flush_pmksa(rdev, dev);
11163 }
11164 
11165 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
11166 {
11167 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11168 	struct net_device *dev = info->user_ptr[1];
11169 	u8 action_code, dialog_token;
11170 	u32 peer_capability = 0;
11171 	u16 status_code;
11172 	u8 *peer;
11173 	bool initiator;
11174 
11175 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
11176 	    !rdev->ops->tdls_mgmt)
11177 		return -EOPNOTSUPP;
11178 
11179 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
11180 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
11181 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
11182 	    !info->attrs[NL80211_ATTR_IE] ||
11183 	    !info->attrs[NL80211_ATTR_MAC])
11184 		return -EINVAL;
11185 
11186 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
11187 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
11188 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
11189 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
11190 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
11191 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
11192 		peer_capability =
11193 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
11194 
11195 	return rdev_tdls_mgmt(rdev, dev, peer, action_code,
11196 			      dialog_token, status_code, peer_capability,
11197 			      initiator,
11198 			      nla_data(info->attrs[NL80211_ATTR_IE]),
11199 			      nla_len(info->attrs[NL80211_ATTR_IE]));
11200 }
11201 
11202 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
11203 {
11204 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11205 	struct net_device *dev = info->user_ptr[1];
11206 	enum nl80211_tdls_operation operation;
11207 	u8 *peer;
11208 
11209 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
11210 	    !rdev->ops->tdls_oper)
11211 		return -EOPNOTSUPP;
11212 
11213 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
11214 	    !info->attrs[NL80211_ATTR_MAC])
11215 		return -EINVAL;
11216 
11217 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
11218 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
11219 
11220 	return rdev_tdls_oper(rdev, dev, peer, operation);
11221 }
11222 
11223 static int nl80211_remain_on_channel(struct sk_buff *skb,
11224 				     struct genl_info *info)
11225 {
11226 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11227 	struct wireless_dev *wdev = info->user_ptr[1];
11228 	struct cfg80211_chan_def chandef;
11229 	const struct cfg80211_chan_def *compat_chandef;
11230 	struct sk_buff *msg;
11231 	void *hdr;
11232 	u64 cookie;
11233 	u32 duration;
11234 	int err;
11235 
11236 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
11237 	    !info->attrs[NL80211_ATTR_DURATION])
11238 		return -EINVAL;
11239 
11240 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
11241 
11242 	if (!rdev->ops->remain_on_channel ||
11243 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
11244 		return -EOPNOTSUPP;
11245 
11246 	/*
11247 	 * We should be on that channel for at least a minimum amount of
11248 	 * time (10ms) but no longer than the driver supports.
11249 	 */
11250 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
11251 	    duration > rdev->wiphy.max_remain_on_channel_duration)
11252 		return -EINVAL;
11253 
11254 	err = nl80211_parse_chandef(rdev, info, &chandef);
11255 	if (err)
11256 		return err;
11257 
11258 	wdev_lock(wdev);
11259 	if (!cfg80211_off_channel_oper_allowed(wdev) &&
11260 	    !cfg80211_chandef_identical(&wdev->chandef, &chandef)) {
11261 		compat_chandef = cfg80211_chandef_compatible(&wdev->chandef,
11262 							     &chandef);
11263 		if (compat_chandef != &chandef) {
11264 			wdev_unlock(wdev);
11265 			return -EBUSY;
11266 		}
11267 	}
11268 	wdev_unlock(wdev);
11269 
11270 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11271 	if (!msg)
11272 		return -ENOMEM;
11273 
11274 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11275 			     NL80211_CMD_REMAIN_ON_CHANNEL);
11276 	if (!hdr) {
11277 		err = -ENOBUFS;
11278 		goto free_msg;
11279 	}
11280 
11281 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
11282 				     duration, &cookie);
11283 
11284 	if (err)
11285 		goto free_msg;
11286 
11287 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
11288 			      NL80211_ATTR_PAD))
11289 		goto nla_put_failure;
11290 
11291 	genlmsg_end(msg, hdr);
11292 
11293 	return genlmsg_reply(msg, info);
11294 
11295  nla_put_failure:
11296 	err = -ENOBUFS;
11297  free_msg:
11298 	nlmsg_free(msg);
11299 	return err;
11300 }
11301 
11302 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
11303 					    struct genl_info *info)
11304 {
11305 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11306 	struct wireless_dev *wdev = info->user_ptr[1];
11307 	u64 cookie;
11308 
11309 	if (!info->attrs[NL80211_ATTR_COOKIE])
11310 		return -EINVAL;
11311 
11312 	if (!rdev->ops->cancel_remain_on_channel)
11313 		return -EOPNOTSUPP;
11314 
11315 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
11316 
11317 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
11318 }
11319 
11320 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
11321 				       struct genl_info *info)
11322 {
11323 	struct cfg80211_bitrate_mask mask;
11324 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11325 	struct net_device *dev = info->user_ptr[1];
11326 	int err;
11327 
11328 	if (!rdev->ops->set_bitrate_mask)
11329 		return -EOPNOTSUPP;
11330 
11331 	err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
11332 					    NL80211_ATTR_TX_RATES, &mask,
11333 					    dev, true);
11334 	if (err)
11335 		return err;
11336 
11337 	return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
11338 }
11339 
11340 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
11341 {
11342 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11343 	struct wireless_dev *wdev = info->user_ptr[1];
11344 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
11345 
11346 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
11347 		return -EINVAL;
11348 
11349 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
11350 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
11351 
11352 	switch (wdev->iftype) {
11353 	case NL80211_IFTYPE_STATION:
11354 	case NL80211_IFTYPE_ADHOC:
11355 	case NL80211_IFTYPE_P2P_CLIENT:
11356 	case NL80211_IFTYPE_AP:
11357 	case NL80211_IFTYPE_AP_VLAN:
11358 	case NL80211_IFTYPE_MESH_POINT:
11359 	case NL80211_IFTYPE_P2P_GO:
11360 	case NL80211_IFTYPE_P2P_DEVICE:
11361 		break;
11362 	case NL80211_IFTYPE_NAN:
11363 	default:
11364 		return -EOPNOTSUPP;
11365 	}
11366 
11367 	/* not much point in registering if we can't reply */
11368 	if (!rdev->ops->mgmt_tx)
11369 		return -EOPNOTSUPP;
11370 
11371 	if (info->attrs[NL80211_ATTR_RECEIVE_MULTICAST] &&
11372 	    !wiphy_ext_feature_isset(&rdev->wiphy,
11373 				     NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS)) {
11374 		GENL_SET_ERR_MSG(info,
11375 				 "multicast RX registrations are not supported");
11376 		return -EOPNOTSUPP;
11377 	}
11378 
11379 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
11380 					   nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
11381 					   nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]),
11382 					   info->attrs[NL80211_ATTR_RECEIVE_MULTICAST],
11383 					   info->extack);
11384 }
11385 
11386 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
11387 {
11388 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11389 	struct wireless_dev *wdev = info->user_ptr[1];
11390 	struct cfg80211_chan_def chandef;
11391 	int err;
11392 	void *hdr = NULL;
11393 	u64 cookie;
11394 	struct sk_buff *msg = NULL;
11395 	struct cfg80211_mgmt_tx_params params = {
11396 		.dont_wait_for_ack =
11397 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
11398 	};
11399 
11400 	if (!info->attrs[NL80211_ATTR_FRAME])
11401 		return -EINVAL;
11402 
11403 	if (!rdev->ops->mgmt_tx)
11404 		return -EOPNOTSUPP;
11405 
11406 	switch (wdev->iftype) {
11407 	case NL80211_IFTYPE_P2P_DEVICE:
11408 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
11409 			return -EINVAL;
11410 		break;
11411 	case NL80211_IFTYPE_STATION:
11412 	case NL80211_IFTYPE_ADHOC:
11413 	case NL80211_IFTYPE_P2P_CLIENT:
11414 	case NL80211_IFTYPE_AP:
11415 	case NL80211_IFTYPE_AP_VLAN:
11416 	case NL80211_IFTYPE_MESH_POINT:
11417 	case NL80211_IFTYPE_P2P_GO:
11418 		break;
11419 	case NL80211_IFTYPE_NAN:
11420 	default:
11421 		return -EOPNOTSUPP;
11422 	}
11423 
11424 	if (info->attrs[NL80211_ATTR_DURATION]) {
11425 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
11426 			return -EINVAL;
11427 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
11428 
11429 		/*
11430 		 * We should wait on the channel for at least a minimum amount
11431 		 * of time (10ms) but no longer than the driver supports.
11432 		 */
11433 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
11434 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
11435 			return -EINVAL;
11436 	}
11437 
11438 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
11439 
11440 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
11441 		return -EINVAL;
11442 
11443 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
11444 
11445 	/* get the channel if any has been specified, otherwise pass NULL to
11446 	 * the driver. The latter will use the current one
11447 	 */
11448 	chandef.chan = NULL;
11449 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
11450 		err = nl80211_parse_chandef(rdev, info, &chandef);
11451 		if (err)
11452 			return err;
11453 	}
11454 
11455 	if (!chandef.chan && params.offchan)
11456 		return -EINVAL;
11457 
11458 	wdev_lock(wdev);
11459 	if (params.offchan && !cfg80211_off_channel_oper_allowed(wdev)) {
11460 		wdev_unlock(wdev);
11461 		return -EBUSY;
11462 	}
11463 	wdev_unlock(wdev);
11464 
11465 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
11466 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
11467 
11468 	if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
11469 		int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
11470 		int i;
11471 
11472 		if (len % sizeof(u16))
11473 			return -EINVAL;
11474 
11475 		params.n_csa_offsets = len / sizeof(u16);
11476 		params.csa_offsets =
11477 			nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
11478 
11479 		/* check that all the offsets fit the frame */
11480 		for (i = 0; i < params.n_csa_offsets; i++) {
11481 			if (params.csa_offsets[i] >= params.len)
11482 				return -EINVAL;
11483 		}
11484 	}
11485 
11486 	if (!params.dont_wait_for_ack) {
11487 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11488 		if (!msg)
11489 			return -ENOMEM;
11490 
11491 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11492 				     NL80211_CMD_FRAME);
11493 		if (!hdr) {
11494 			err = -ENOBUFS;
11495 			goto free_msg;
11496 		}
11497 	}
11498 
11499 	params.chan = chandef.chan;
11500 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
11501 	if (err)
11502 		goto free_msg;
11503 
11504 	if (msg) {
11505 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
11506 				      NL80211_ATTR_PAD))
11507 			goto nla_put_failure;
11508 
11509 		genlmsg_end(msg, hdr);
11510 		return genlmsg_reply(msg, info);
11511 	}
11512 
11513 	return 0;
11514 
11515  nla_put_failure:
11516 	err = -ENOBUFS;
11517  free_msg:
11518 	nlmsg_free(msg);
11519 	return err;
11520 }
11521 
11522 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
11523 {
11524 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11525 	struct wireless_dev *wdev = info->user_ptr[1];
11526 	u64 cookie;
11527 
11528 	if (!info->attrs[NL80211_ATTR_COOKIE])
11529 		return -EINVAL;
11530 
11531 	if (!rdev->ops->mgmt_tx_cancel_wait)
11532 		return -EOPNOTSUPP;
11533 
11534 	switch (wdev->iftype) {
11535 	case NL80211_IFTYPE_STATION:
11536 	case NL80211_IFTYPE_ADHOC:
11537 	case NL80211_IFTYPE_P2P_CLIENT:
11538 	case NL80211_IFTYPE_AP:
11539 	case NL80211_IFTYPE_AP_VLAN:
11540 	case NL80211_IFTYPE_P2P_GO:
11541 	case NL80211_IFTYPE_P2P_DEVICE:
11542 		break;
11543 	case NL80211_IFTYPE_NAN:
11544 	default:
11545 		return -EOPNOTSUPP;
11546 	}
11547 
11548 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
11549 
11550 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
11551 }
11552 
11553 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
11554 {
11555 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11556 	struct wireless_dev *wdev;
11557 	struct net_device *dev = info->user_ptr[1];
11558 	u8 ps_state;
11559 	bool state;
11560 	int err;
11561 
11562 	if (!info->attrs[NL80211_ATTR_PS_STATE])
11563 		return -EINVAL;
11564 
11565 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
11566 
11567 	wdev = dev->ieee80211_ptr;
11568 
11569 	if (!rdev->ops->set_power_mgmt)
11570 		return -EOPNOTSUPP;
11571 
11572 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
11573 
11574 	if (state == wdev->ps)
11575 		return 0;
11576 
11577 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
11578 	if (!err)
11579 		wdev->ps = state;
11580 	return err;
11581 }
11582 
11583 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
11584 {
11585 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11586 	enum nl80211_ps_state ps_state;
11587 	struct wireless_dev *wdev;
11588 	struct net_device *dev = info->user_ptr[1];
11589 	struct sk_buff *msg;
11590 	void *hdr;
11591 	int err;
11592 
11593 	wdev = dev->ieee80211_ptr;
11594 
11595 	if (!rdev->ops->set_power_mgmt)
11596 		return -EOPNOTSUPP;
11597 
11598 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11599 	if (!msg)
11600 		return -ENOMEM;
11601 
11602 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11603 			     NL80211_CMD_GET_POWER_SAVE);
11604 	if (!hdr) {
11605 		err = -ENOBUFS;
11606 		goto free_msg;
11607 	}
11608 
11609 	if (wdev->ps)
11610 		ps_state = NL80211_PS_ENABLED;
11611 	else
11612 		ps_state = NL80211_PS_DISABLED;
11613 
11614 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
11615 		goto nla_put_failure;
11616 
11617 	genlmsg_end(msg, hdr);
11618 	return genlmsg_reply(msg, info);
11619 
11620  nla_put_failure:
11621 	err = -ENOBUFS;
11622  free_msg:
11623 	nlmsg_free(msg);
11624 	return err;
11625 }
11626 
11627 static const struct nla_policy
11628 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
11629 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
11630 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
11631 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
11632 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
11633 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
11634 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
11635 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
11636 };
11637 
11638 static int nl80211_set_cqm_txe(struct genl_info *info,
11639 			       u32 rate, u32 pkts, u32 intvl)
11640 {
11641 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11642 	struct net_device *dev = info->user_ptr[1];
11643 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11644 
11645 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
11646 		return -EINVAL;
11647 
11648 	if (!rdev->ops->set_cqm_txe_config)
11649 		return -EOPNOTSUPP;
11650 
11651 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
11652 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
11653 		return -EOPNOTSUPP;
11654 
11655 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
11656 }
11657 
11658 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
11659 				    struct net_device *dev)
11660 {
11661 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11662 	s32 last, low, high;
11663 	u32 hyst;
11664 	int i, n, low_index;
11665 	int err;
11666 
11667 	/* RSSI reporting disabled? */
11668 	if (!wdev->cqm_config)
11669 		return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
11670 
11671 	/*
11672 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
11673 	 * event has been received yet, we should receive an event after a
11674 	 * connection is established and enough beacons received to calculate
11675 	 * the average.
11676 	 */
11677 	if (!wdev->cqm_config->last_rssi_event_value && wdev->current_bss &&
11678 	    rdev->ops->get_station) {
11679 		struct station_info sinfo = {};
11680 		u8 *mac_addr;
11681 
11682 		mac_addr = wdev->current_bss->pub.bssid;
11683 
11684 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
11685 		if (err)
11686 			return err;
11687 
11688 		cfg80211_sinfo_release_content(&sinfo);
11689 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
11690 			wdev->cqm_config->last_rssi_event_value =
11691 				(s8) sinfo.rx_beacon_signal_avg;
11692 	}
11693 
11694 	last = wdev->cqm_config->last_rssi_event_value;
11695 	hyst = wdev->cqm_config->rssi_hyst;
11696 	n = wdev->cqm_config->n_rssi_thresholds;
11697 
11698 	for (i = 0; i < n; i++) {
11699 		i = array_index_nospec(i, n);
11700 		if (last < wdev->cqm_config->rssi_thresholds[i])
11701 			break;
11702 	}
11703 
11704 	low_index = i - 1;
11705 	if (low_index >= 0) {
11706 		low_index = array_index_nospec(low_index, n);
11707 		low = wdev->cqm_config->rssi_thresholds[low_index] - hyst;
11708 	} else {
11709 		low = S32_MIN;
11710 	}
11711 	if (i < n) {
11712 		i = array_index_nospec(i, n);
11713 		high = wdev->cqm_config->rssi_thresholds[i] + hyst - 1;
11714 	} else {
11715 		high = S32_MAX;
11716 	}
11717 
11718 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
11719 }
11720 
11721 static int nl80211_set_cqm_rssi(struct genl_info *info,
11722 				const s32 *thresholds, int n_thresholds,
11723 				u32 hysteresis)
11724 {
11725 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11726 	struct net_device *dev = info->user_ptr[1];
11727 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11728 	int i, err;
11729 	s32 prev = S32_MIN;
11730 
11731 	/* Check all values negative and sorted */
11732 	for (i = 0; i < n_thresholds; i++) {
11733 		if (thresholds[i] > 0 || thresholds[i] <= prev)
11734 			return -EINVAL;
11735 
11736 		prev = thresholds[i];
11737 	}
11738 
11739 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
11740 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
11741 		return -EOPNOTSUPP;
11742 
11743 	wdev_lock(wdev);
11744 	cfg80211_cqm_config_free(wdev);
11745 	wdev_unlock(wdev);
11746 
11747 	if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
11748 		if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
11749 			return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
11750 
11751 		return rdev_set_cqm_rssi_config(rdev, dev,
11752 						thresholds[0], hysteresis);
11753 	}
11754 
11755 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
11756 				     NL80211_EXT_FEATURE_CQM_RSSI_LIST))
11757 		return -EOPNOTSUPP;
11758 
11759 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
11760 		n_thresholds = 0;
11761 
11762 	wdev_lock(wdev);
11763 	if (n_thresholds) {
11764 		struct cfg80211_cqm_config *cqm_config;
11765 
11766 		cqm_config = kzalloc(sizeof(struct cfg80211_cqm_config) +
11767 				     n_thresholds * sizeof(s32), GFP_KERNEL);
11768 		if (!cqm_config) {
11769 			err = -ENOMEM;
11770 			goto unlock;
11771 		}
11772 
11773 		cqm_config->rssi_hyst = hysteresis;
11774 		cqm_config->n_rssi_thresholds = n_thresholds;
11775 		memcpy(cqm_config->rssi_thresholds, thresholds,
11776 		       n_thresholds * sizeof(s32));
11777 
11778 		wdev->cqm_config = cqm_config;
11779 	}
11780 
11781 	err = cfg80211_cqm_rssi_update(rdev, dev);
11782 
11783 unlock:
11784 	wdev_unlock(wdev);
11785 
11786 	return err;
11787 }
11788 
11789 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
11790 {
11791 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
11792 	struct nlattr *cqm;
11793 	int err;
11794 
11795 	cqm = info->attrs[NL80211_ATTR_CQM];
11796 	if (!cqm)
11797 		return -EINVAL;
11798 
11799 	err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
11800 					  nl80211_attr_cqm_policy,
11801 					  info->extack);
11802 	if (err)
11803 		return err;
11804 
11805 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
11806 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
11807 		const s32 *thresholds =
11808 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
11809 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
11810 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
11811 
11812 		if (len % 4)
11813 			return -EINVAL;
11814 
11815 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
11816 					    hysteresis);
11817 	}
11818 
11819 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
11820 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
11821 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
11822 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
11823 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
11824 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
11825 
11826 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
11827 	}
11828 
11829 	return -EINVAL;
11830 }
11831 
11832 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
11833 {
11834 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11835 	struct net_device *dev = info->user_ptr[1];
11836 	struct ocb_setup setup = {};
11837 	int err;
11838 
11839 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
11840 	if (err)
11841 		return err;
11842 
11843 	return cfg80211_join_ocb(rdev, dev, &setup);
11844 }
11845 
11846 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
11847 {
11848 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11849 	struct net_device *dev = info->user_ptr[1];
11850 
11851 	return cfg80211_leave_ocb(rdev, dev);
11852 }
11853 
11854 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
11855 {
11856 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11857 	struct net_device *dev = info->user_ptr[1];
11858 	struct mesh_config cfg;
11859 	struct mesh_setup setup;
11860 	int err;
11861 
11862 	/* start with default */
11863 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
11864 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
11865 
11866 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
11867 		/* and parse parameters if given */
11868 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
11869 		if (err)
11870 			return err;
11871 	}
11872 
11873 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
11874 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
11875 		return -EINVAL;
11876 
11877 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
11878 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
11879 
11880 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11881 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
11882 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11883 			return -EINVAL;
11884 
11885 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
11886 		setup.beacon_interval =
11887 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11888 
11889 		err = cfg80211_validate_beacon_int(rdev,
11890 						   NL80211_IFTYPE_MESH_POINT,
11891 						   setup.beacon_interval);
11892 		if (err)
11893 			return err;
11894 	}
11895 
11896 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
11897 		setup.dtim_period =
11898 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
11899 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
11900 			return -EINVAL;
11901 	}
11902 
11903 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
11904 		/* parse additional setup parameters if given */
11905 		err = nl80211_parse_mesh_setup(info, &setup);
11906 		if (err)
11907 			return err;
11908 	}
11909 
11910 	if (setup.user_mpm)
11911 		cfg.auto_open_plinks = false;
11912 
11913 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
11914 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
11915 		if (err)
11916 			return err;
11917 	} else {
11918 		/* __cfg80211_join_mesh() will sort it out */
11919 		setup.chandef.chan = NULL;
11920 	}
11921 
11922 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11923 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11924 		int n_rates =
11925 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11926 		struct ieee80211_supported_band *sband;
11927 
11928 		if (!setup.chandef.chan)
11929 			return -EINVAL;
11930 
11931 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
11932 
11933 		err = ieee80211_get_ratemask(sband, rates, n_rates,
11934 					     &setup.basic_rates);
11935 		if (err)
11936 			return err;
11937 	}
11938 
11939 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
11940 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
11941 						    NL80211_ATTR_TX_RATES,
11942 						    &setup.beacon_rate,
11943 						    dev, false);
11944 		if (err)
11945 			return err;
11946 
11947 		if (!setup.chandef.chan)
11948 			return -EINVAL;
11949 
11950 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
11951 					      &setup.beacon_rate);
11952 		if (err)
11953 			return err;
11954 	}
11955 
11956 	setup.userspace_handles_dfs =
11957 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11958 
11959 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11960 		int r = validate_pae_over_nl80211(rdev, info);
11961 
11962 		if (r < 0)
11963 			return r;
11964 
11965 		setup.control_port_over_nl80211 = true;
11966 	}
11967 
11968 	wdev_lock(dev->ieee80211_ptr);
11969 	err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
11970 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
11971 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11972 	wdev_unlock(dev->ieee80211_ptr);
11973 
11974 	return err;
11975 }
11976 
11977 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
11978 {
11979 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11980 	struct net_device *dev = info->user_ptr[1];
11981 
11982 	return cfg80211_leave_mesh(rdev, dev);
11983 }
11984 
11985 #ifdef CONFIG_PM
11986 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
11987 					struct cfg80211_registered_device *rdev)
11988 {
11989 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
11990 	struct nlattr *nl_pats, *nl_pat;
11991 	int i, pat_len;
11992 
11993 	if (!wowlan->n_patterns)
11994 		return 0;
11995 
11996 	nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
11997 	if (!nl_pats)
11998 		return -ENOBUFS;
11999 
12000 	for (i = 0; i < wowlan->n_patterns; i++) {
12001 		nl_pat = nla_nest_start_noflag(msg, i + 1);
12002 		if (!nl_pat)
12003 			return -ENOBUFS;
12004 		pat_len = wowlan->patterns[i].pattern_len;
12005 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
12006 			    wowlan->patterns[i].mask) ||
12007 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
12008 			    wowlan->patterns[i].pattern) ||
12009 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
12010 				wowlan->patterns[i].pkt_offset))
12011 			return -ENOBUFS;
12012 		nla_nest_end(msg, nl_pat);
12013 	}
12014 	nla_nest_end(msg, nl_pats);
12015 
12016 	return 0;
12017 }
12018 
12019 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
12020 				   struct cfg80211_wowlan_tcp *tcp)
12021 {
12022 	struct nlattr *nl_tcp;
12023 
12024 	if (!tcp)
12025 		return 0;
12026 
12027 	nl_tcp = nla_nest_start_noflag(msg,
12028 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
12029 	if (!nl_tcp)
12030 		return -ENOBUFS;
12031 
12032 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
12033 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
12034 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
12035 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
12036 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
12037 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
12038 		    tcp->payload_len, tcp->payload) ||
12039 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
12040 			tcp->data_interval) ||
12041 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
12042 		    tcp->wake_len, tcp->wake_data) ||
12043 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
12044 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
12045 		return -ENOBUFS;
12046 
12047 	if (tcp->payload_seq.len &&
12048 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
12049 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
12050 		return -ENOBUFS;
12051 
12052 	if (tcp->payload_tok.len &&
12053 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
12054 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
12055 		    &tcp->payload_tok))
12056 		return -ENOBUFS;
12057 
12058 	nla_nest_end(msg, nl_tcp);
12059 
12060 	return 0;
12061 }
12062 
12063 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
12064 				  struct cfg80211_sched_scan_request *req)
12065 {
12066 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
12067 	int i;
12068 
12069 	if (!req)
12070 		return 0;
12071 
12072 	nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
12073 	if (!nd)
12074 		return -ENOBUFS;
12075 
12076 	if (req->n_scan_plans == 1 &&
12077 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
12078 			req->scan_plans[0].interval * 1000))
12079 		return -ENOBUFS;
12080 
12081 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
12082 		return -ENOBUFS;
12083 
12084 	if (req->relative_rssi_set) {
12085 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
12086 
12087 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
12088 			       req->relative_rssi))
12089 			return -ENOBUFS;
12090 
12091 		rssi_adjust.band = req->rssi_adjust.band;
12092 		rssi_adjust.delta = req->rssi_adjust.delta;
12093 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
12094 			    sizeof(rssi_adjust), &rssi_adjust))
12095 			return -ENOBUFS;
12096 	}
12097 
12098 	freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
12099 	if (!freqs)
12100 		return -ENOBUFS;
12101 
12102 	for (i = 0; i < req->n_channels; i++) {
12103 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
12104 			return -ENOBUFS;
12105 	}
12106 
12107 	nla_nest_end(msg, freqs);
12108 
12109 	if (req->n_match_sets) {
12110 		matches = nla_nest_start_noflag(msg,
12111 						NL80211_ATTR_SCHED_SCAN_MATCH);
12112 		if (!matches)
12113 			return -ENOBUFS;
12114 
12115 		for (i = 0; i < req->n_match_sets; i++) {
12116 			match = nla_nest_start_noflag(msg, i);
12117 			if (!match)
12118 				return -ENOBUFS;
12119 
12120 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
12121 				    req->match_sets[i].ssid.ssid_len,
12122 				    req->match_sets[i].ssid.ssid))
12123 				return -ENOBUFS;
12124 			nla_nest_end(msg, match);
12125 		}
12126 		nla_nest_end(msg, matches);
12127 	}
12128 
12129 	scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
12130 	if (!scan_plans)
12131 		return -ENOBUFS;
12132 
12133 	for (i = 0; i < req->n_scan_plans; i++) {
12134 		scan_plan = nla_nest_start_noflag(msg, i + 1);
12135 		if (!scan_plan)
12136 			return -ENOBUFS;
12137 
12138 		if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
12139 				req->scan_plans[i].interval) ||
12140 		    (req->scan_plans[i].iterations &&
12141 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
12142 				 req->scan_plans[i].iterations)))
12143 			return -ENOBUFS;
12144 		nla_nest_end(msg, scan_plan);
12145 	}
12146 	nla_nest_end(msg, scan_plans);
12147 
12148 	nla_nest_end(msg, nd);
12149 
12150 	return 0;
12151 }
12152 
12153 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
12154 {
12155 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12156 	struct sk_buff *msg;
12157 	void *hdr;
12158 	u32 size = NLMSG_DEFAULT_SIZE;
12159 
12160 	if (!rdev->wiphy.wowlan)
12161 		return -EOPNOTSUPP;
12162 
12163 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
12164 		/* adjust size to have room for all the data */
12165 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
12166 			rdev->wiphy.wowlan_config->tcp->payload_len +
12167 			rdev->wiphy.wowlan_config->tcp->wake_len +
12168 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
12169 	}
12170 
12171 	msg = nlmsg_new(size, GFP_KERNEL);
12172 	if (!msg)
12173 		return -ENOMEM;
12174 
12175 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12176 			     NL80211_CMD_GET_WOWLAN);
12177 	if (!hdr)
12178 		goto nla_put_failure;
12179 
12180 	if (rdev->wiphy.wowlan_config) {
12181 		struct nlattr *nl_wowlan;
12182 
12183 		nl_wowlan = nla_nest_start_noflag(msg,
12184 						  NL80211_ATTR_WOWLAN_TRIGGERS);
12185 		if (!nl_wowlan)
12186 			goto nla_put_failure;
12187 
12188 		if ((rdev->wiphy.wowlan_config->any &&
12189 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
12190 		    (rdev->wiphy.wowlan_config->disconnect &&
12191 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
12192 		    (rdev->wiphy.wowlan_config->magic_pkt &&
12193 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
12194 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
12195 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
12196 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
12197 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
12198 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
12199 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
12200 		    (rdev->wiphy.wowlan_config->rfkill_release &&
12201 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
12202 			goto nla_put_failure;
12203 
12204 		if (nl80211_send_wowlan_patterns(msg, rdev))
12205 			goto nla_put_failure;
12206 
12207 		if (nl80211_send_wowlan_tcp(msg,
12208 					    rdev->wiphy.wowlan_config->tcp))
12209 			goto nla_put_failure;
12210 
12211 		if (nl80211_send_wowlan_nd(
12212 			    msg,
12213 			    rdev->wiphy.wowlan_config->nd_config))
12214 			goto nla_put_failure;
12215 
12216 		nla_nest_end(msg, nl_wowlan);
12217 	}
12218 
12219 	genlmsg_end(msg, hdr);
12220 	return genlmsg_reply(msg, info);
12221 
12222 nla_put_failure:
12223 	nlmsg_free(msg);
12224 	return -ENOBUFS;
12225 }
12226 
12227 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
12228 				    struct nlattr *attr,
12229 				    struct cfg80211_wowlan *trig)
12230 {
12231 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
12232 	struct cfg80211_wowlan_tcp *cfg;
12233 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
12234 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
12235 	u32 size;
12236 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
12237 	int err, port;
12238 
12239 	if (!rdev->wiphy.wowlan->tcp)
12240 		return -EINVAL;
12241 
12242 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
12243 					  nl80211_wowlan_tcp_policy, NULL);
12244 	if (err)
12245 		return err;
12246 
12247 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
12248 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
12249 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
12250 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
12251 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
12252 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
12253 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
12254 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
12255 		return -EINVAL;
12256 
12257 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
12258 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
12259 		return -EINVAL;
12260 
12261 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
12262 			rdev->wiphy.wowlan->tcp->data_interval_max ||
12263 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
12264 		return -EINVAL;
12265 
12266 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
12267 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
12268 		return -EINVAL;
12269 
12270 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
12271 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
12272 		return -EINVAL;
12273 
12274 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
12275 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
12276 
12277 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
12278 		tokens_size = tokln - sizeof(*tok);
12279 
12280 		if (!tok->len || tokens_size % tok->len)
12281 			return -EINVAL;
12282 		if (!rdev->wiphy.wowlan->tcp->tok)
12283 			return -EINVAL;
12284 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
12285 			return -EINVAL;
12286 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
12287 			return -EINVAL;
12288 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
12289 			return -EINVAL;
12290 		if (tok->offset + tok->len > data_size)
12291 			return -EINVAL;
12292 	}
12293 
12294 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
12295 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
12296 		if (!rdev->wiphy.wowlan->tcp->seq)
12297 			return -EINVAL;
12298 		if (seq->len == 0 || seq->len > 4)
12299 			return -EINVAL;
12300 		if (seq->len + seq->offset > data_size)
12301 			return -EINVAL;
12302 	}
12303 
12304 	size = sizeof(*cfg);
12305 	size += data_size;
12306 	size += wake_size + wake_mask_size;
12307 	size += tokens_size;
12308 
12309 	cfg = kzalloc(size, GFP_KERNEL);
12310 	if (!cfg)
12311 		return -ENOMEM;
12312 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
12313 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
12314 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
12315 	       ETH_ALEN);
12316 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
12317 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
12318 	else
12319 		port = 0;
12320 #ifdef CONFIG_INET
12321 	/* allocate a socket and port for it and use it */
12322 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
12323 			    IPPROTO_TCP, &cfg->sock, 1);
12324 	if (err) {
12325 		kfree(cfg);
12326 		return err;
12327 	}
12328 	if (inet_csk_get_port(cfg->sock->sk, port)) {
12329 		sock_release(cfg->sock);
12330 		kfree(cfg);
12331 		return -EADDRINUSE;
12332 	}
12333 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
12334 #else
12335 	if (!port) {
12336 		kfree(cfg);
12337 		return -EINVAL;
12338 	}
12339 	cfg->src_port = port;
12340 #endif
12341 
12342 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
12343 	cfg->payload_len = data_size;
12344 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
12345 	memcpy((void *)cfg->payload,
12346 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
12347 	       data_size);
12348 	if (seq)
12349 		cfg->payload_seq = *seq;
12350 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
12351 	cfg->wake_len = wake_size;
12352 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
12353 	memcpy((void *)cfg->wake_data,
12354 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
12355 	       wake_size);
12356 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
12357 			 data_size + wake_size;
12358 	memcpy((void *)cfg->wake_mask,
12359 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
12360 	       wake_mask_size);
12361 	if (tok) {
12362 		cfg->tokens_size = tokens_size;
12363 		memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
12364 	}
12365 
12366 	trig->tcp = cfg;
12367 
12368 	return 0;
12369 }
12370 
12371 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
12372 				   const struct wiphy_wowlan_support *wowlan,
12373 				   struct nlattr *attr,
12374 				   struct cfg80211_wowlan *trig)
12375 {
12376 	struct nlattr **tb;
12377 	int err;
12378 
12379 	tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
12380 	if (!tb)
12381 		return -ENOMEM;
12382 
12383 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
12384 		err = -EOPNOTSUPP;
12385 		goto out;
12386 	}
12387 
12388 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
12389 					  nl80211_policy, NULL);
12390 	if (err)
12391 		goto out;
12392 
12393 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
12394 						   wowlan->max_nd_match_sets);
12395 	err = PTR_ERR_OR_ZERO(trig->nd_config);
12396 	if (err)
12397 		trig->nd_config = NULL;
12398 
12399 out:
12400 	kfree(tb);
12401 	return err;
12402 }
12403 
12404 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
12405 {
12406 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12407 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
12408 	struct cfg80211_wowlan new_triggers = {};
12409 	struct cfg80211_wowlan *ntrig;
12410 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
12411 	int err, i;
12412 	bool prev_enabled = rdev->wiphy.wowlan_config;
12413 	bool regular = false;
12414 
12415 	if (!wowlan)
12416 		return -EOPNOTSUPP;
12417 
12418 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
12419 		cfg80211_rdev_free_wowlan(rdev);
12420 		rdev->wiphy.wowlan_config = NULL;
12421 		goto set_wakeup;
12422 	}
12423 
12424 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
12425 					  info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
12426 					  nl80211_wowlan_policy, info->extack);
12427 	if (err)
12428 		return err;
12429 
12430 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
12431 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
12432 			return -EINVAL;
12433 		new_triggers.any = true;
12434 	}
12435 
12436 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
12437 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
12438 			return -EINVAL;
12439 		new_triggers.disconnect = true;
12440 		regular = true;
12441 	}
12442 
12443 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
12444 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
12445 			return -EINVAL;
12446 		new_triggers.magic_pkt = true;
12447 		regular = true;
12448 	}
12449 
12450 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
12451 		return -EINVAL;
12452 
12453 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
12454 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
12455 			return -EINVAL;
12456 		new_triggers.gtk_rekey_failure = true;
12457 		regular = true;
12458 	}
12459 
12460 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
12461 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
12462 			return -EINVAL;
12463 		new_triggers.eap_identity_req = true;
12464 		regular = true;
12465 	}
12466 
12467 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
12468 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
12469 			return -EINVAL;
12470 		new_triggers.four_way_handshake = true;
12471 		regular = true;
12472 	}
12473 
12474 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
12475 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
12476 			return -EINVAL;
12477 		new_triggers.rfkill_release = true;
12478 		regular = true;
12479 	}
12480 
12481 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
12482 		struct nlattr *pat;
12483 		int n_patterns = 0;
12484 		int rem, pat_len, mask_len, pkt_offset;
12485 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
12486 
12487 		regular = true;
12488 
12489 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
12490 				    rem)
12491 			n_patterns++;
12492 		if (n_patterns > wowlan->n_patterns)
12493 			return -EINVAL;
12494 
12495 		new_triggers.patterns = kcalloc(n_patterns,
12496 						sizeof(new_triggers.patterns[0]),
12497 						GFP_KERNEL);
12498 		if (!new_triggers.patterns)
12499 			return -ENOMEM;
12500 
12501 		new_triggers.n_patterns = n_patterns;
12502 		i = 0;
12503 
12504 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
12505 				    rem) {
12506 			u8 *mask_pat;
12507 
12508 			err = nla_parse_nested_deprecated(pat_tb,
12509 							  MAX_NL80211_PKTPAT,
12510 							  pat,
12511 							  nl80211_packet_pattern_policy,
12512 							  info->extack);
12513 			if (err)
12514 				goto error;
12515 
12516 			err = -EINVAL;
12517 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
12518 			    !pat_tb[NL80211_PKTPAT_PATTERN])
12519 				goto error;
12520 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
12521 			mask_len = DIV_ROUND_UP(pat_len, 8);
12522 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
12523 				goto error;
12524 			if (pat_len > wowlan->pattern_max_len ||
12525 			    pat_len < wowlan->pattern_min_len)
12526 				goto error;
12527 
12528 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
12529 				pkt_offset = 0;
12530 			else
12531 				pkt_offset = nla_get_u32(
12532 					pat_tb[NL80211_PKTPAT_OFFSET]);
12533 			if (pkt_offset > wowlan->max_pkt_offset)
12534 				goto error;
12535 			new_triggers.patterns[i].pkt_offset = pkt_offset;
12536 
12537 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
12538 			if (!mask_pat) {
12539 				err = -ENOMEM;
12540 				goto error;
12541 			}
12542 			new_triggers.patterns[i].mask = mask_pat;
12543 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
12544 			       mask_len);
12545 			mask_pat += mask_len;
12546 			new_triggers.patterns[i].pattern = mask_pat;
12547 			new_triggers.patterns[i].pattern_len = pat_len;
12548 			memcpy(mask_pat,
12549 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
12550 			       pat_len);
12551 			i++;
12552 		}
12553 	}
12554 
12555 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
12556 		regular = true;
12557 		err = nl80211_parse_wowlan_tcp(
12558 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
12559 			&new_triggers);
12560 		if (err)
12561 			goto error;
12562 	}
12563 
12564 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
12565 		regular = true;
12566 		err = nl80211_parse_wowlan_nd(
12567 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
12568 			&new_triggers);
12569 		if (err)
12570 			goto error;
12571 	}
12572 
12573 	/* The 'any' trigger means the device continues operating more or less
12574 	 * as in its normal operation mode and wakes up the host on most of the
12575 	 * normal interrupts (like packet RX, ...)
12576 	 * It therefore makes little sense to combine with the more constrained
12577 	 * wakeup trigger modes.
12578 	 */
12579 	if (new_triggers.any && regular) {
12580 		err = -EINVAL;
12581 		goto error;
12582 	}
12583 
12584 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
12585 	if (!ntrig) {
12586 		err = -ENOMEM;
12587 		goto error;
12588 	}
12589 	cfg80211_rdev_free_wowlan(rdev);
12590 	rdev->wiphy.wowlan_config = ntrig;
12591 
12592  set_wakeup:
12593 	if (rdev->ops->set_wakeup &&
12594 	    prev_enabled != !!rdev->wiphy.wowlan_config)
12595 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
12596 
12597 	return 0;
12598  error:
12599 	for (i = 0; i < new_triggers.n_patterns; i++)
12600 		kfree(new_triggers.patterns[i].mask);
12601 	kfree(new_triggers.patterns);
12602 	if (new_triggers.tcp && new_triggers.tcp->sock)
12603 		sock_release(new_triggers.tcp->sock);
12604 	kfree(new_triggers.tcp);
12605 	kfree(new_triggers.nd_config);
12606 	return err;
12607 }
12608 #endif
12609 
12610 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
12611 				       struct cfg80211_registered_device *rdev)
12612 {
12613 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
12614 	int i, j, pat_len;
12615 	struct cfg80211_coalesce_rules *rule;
12616 
12617 	if (!rdev->coalesce->n_rules)
12618 		return 0;
12619 
12620 	nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
12621 	if (!nl_rules)
12622 		return -ENOBUFS;
12623 
12624 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
12625 		nl_rule = nla_nest_start_noflag(msg, i + 1);
12626 		if (!nl_rule)
12627 			return -ENOBUFS;
12628 
12629 		rule = &rdev->coalesce->rules[i];
12630 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
12631 				rule->delay))
12632 			return -ENOBUFS;
12633 
12634 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
12635 				rule->condition))
12636 			return -ENOBUFS;
12637 
12638 		nl_pats = nla_nest_start_noflag(msg,
12639 						NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
12640 		if (!nl_pats)
12641 			return -ENOBUFS;
12642 
12643 		for (j = 0; j < rule->n_patterns; j++) {
12644 			nl_pat = nla_nest_start_noflag(msg, j + 1);
12645 			if (!nl_pat)
12646 				return -ENOBUFS;
12647 			pat_len = rule->patterns[j].pattern_len;
12648 			if (nla_put(msg, NL80211_PKTPAT_MASK,
12649 				    DIV_ROUND_UP(pat_len, 8),
12650 				    rule->patterns[j].mask) ||
12651 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
12652 				    rule->patterns[j].pattern) ||
12653 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
12654 					rule->patterns[j].pkt_offset))
12655 				return -ENOBUFS;
12656 			nla_nest_end(msg, nl_pat);
12657 		}
12658 		nla_nest_end(msg, nl_pats);
12659 		nla_nest_end(msg, nl_rule);
12660 	}
12661 	nla_nest_end(msg, nl_rules);
12662 
12663 	return 0;
12664 }
12665 
12666 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
12667 {
12668 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12669 	struct sk_buff *msg;
12670 	void *hdr;
12671 
12672 	if (!rdev->wiphy.coalesce)
12673 		return -EOPNOTSUPP;
12674 
12675 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12676 	if (!msg)
12677 		return -ENOMEM;
12678 
12679 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12680 			     NL80211_CMD_GET_COALESCE);
12681 	if (!hdr)
12682 		goto nla_put_failure;
12683 
12684 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
12685 		goto nla_put_failure;
12686 
12687 	genlmsg_end(msg, hdr);
12688 	return genlmsg_reply(msg, info);
12689 
12690 nla_put_failure:
12691 	nlmsg_free(msg);
12692 	return -ENOBUFS;
12693 }
12694 
12695 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
12696 {
12697 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
12698 	int i, j;
12699 	struct cfg80211_coalesce_rules *rule;
12700 
12701 	if (!coalesce)
12702 		return;
12703 
12704 	for (i = 0; i < coalesce->n_rules; i++) {
12705 		rule = &coalesce->rules[i];
12706 		for (j = 0; j < rule->n_patterns; j++)
12707 			kfree(rule->patterns[j].mask);
12708 		kfree(rule->patterns);
12709 	}
12710 	kfree(coalesce->rules);
12711 	kfree(coalesce);
12712 	rdev->coalesce = NULL;
12713 }
12714 
12715 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
12716 				       struct nlattr *rule,
12717 				       struct cfg80211_coalesce_rules *new_rule)
12718 {
12719 	int err, i;
12720 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
12721 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
12722 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
12723 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
12724 
12725 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
12726 					  rule, nl80211_coalesce_policy, NULL);
12727 	if (err)
12728 		return err;
12729 
12730 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
12731 		new_rule->delay =
12732 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
12733 	if (new_rule->delay > coalesce->max_delay)
12734 		return -EINVAL;
12735 
12736 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
12737 		new_rule->condition =
12738 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
12739 
12740 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
12741 		return -EINVAL;
12742 
12743 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
12744 			    rem)
12745 		n_patterns++;
12746 	if (n_patterns > coalesce->n_patterns)
12747 		return -EINVAL;
12748 
12749 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
12750 				     GFP_KERNEL);
12751 	if (!new_rule->patterns)
12752 		return -ENOMEM;
12753 
12754 	new_rule->n_patterns = n_patterns;
12755 	i = 0;
12756 
12757 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
12758 			    rem) {
12759 		u8 *mask_pat;
12760 
12761 		err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
12762 						  pat,
12763 						  nl80211_packet_pattern_policy,
12764 						  NULL);
12765 		if (err)
12766 			return err;
12767 
12768 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
12769 		    !pat_tb[NL80211_PKTPAT_PATTERN])
12770 			return -EINVAL;
12771 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
12772 		mask_len = DIV_ROUND_UP(pat_len, 8);
12773 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
12774 			return -EINVAL;
12775 		if (pat_len > coalesce->pattern_max_len ||
12776 		    pat_len < coalesce->pattern_min_len)
12777 			return -EINVAL;
12778 
12779 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
12780 			pkt_offset = 0;
12781 		else
12782 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
12783 		if (pkt_offset > coalesce->max_pkt_offset)
12784 			return -EINVAL;
12785 		new_rule->patterns[i].pkt_offset = pkt_offset;
12786 
12787 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
12788 		if (!mask_pat)
12789 			return -ENOMEM;
12790 
12791 		new_rule->patterns[i].mask = mask_pat;
12792 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
12793 		       mask_len);
12794 
12795 		mask_pat += mask_len;
12796 		new_rule->patterns[i].pattern = mask_pat;
12797 		new_rule->patterns[i].pattern_len = pat_len;
12798 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
12799 		       pat_len);
12800 		i++;
12801 	}
12802 
12803 	return 0;
12804 }
12805 
12806 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
12807 {
12808 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12809 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
12810 	struct cfg80211_coalesce new_coalesce = {};
12811 	struct cfg80211_coalesce *n_coalesce;
12812 	int err, rem_rule, n_rules = 0, i, j;
12813 	struct nlattr *rule;
12814 	struct cfg80211_coalesce_rules *tmp_rule;
12815 
12816 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
12817 		return -EOPNOTSUPP;
12818 
12819 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
12820 		cfg80211_rdev_free_coalesce(rdev);
12821 		rdev_set_coalesce(rdev, NULL);
12822 		return 0;
12823 	}
12824 
12825 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
12826 			    rem_rule)
12827 		n_rules++;
12828 	if (n_rules > coalesce->n_rules)
12829 		return -EINVAL;
12830 
12831 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
12832 				     GFP_KERNEL);
12833 	if (!new_coalesce.rules)
12834 		return -ENOMEM;
12835 
12836 	new_coalesce.n_rules = n_rules;
12837 	i = 0;
12838 
12839 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
12840 			    rem_rule) {
12841 		err = nl80211_parse_coalesce_rule(rdev, rule,
12842 						  &new_coalesce.rules[i]);
12843 		if (err)
12844 			goto error;
12845 
12846 		i++;
12847 	}
12848 
12849 	err = rdev_set_coalesce(rdev, &new_coalesce);
12850 	if (err)
12851 		goto error;
12852 
12853 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
12854 	if (!n_coalesce) {
12855 		err = -ENOMEM;
12856 		goto error;
12857 	}
12858 	cfg80211_rdev_free_coalesce(rdev);
12859 	rdev->coalesce = n_coalesce;
12860 
12861 	return 0;
12862 error:
12863 	for (i = 0; i < new_coalesce.n_rules; i++) {
12864 		tmp_rule = &new_coalesce.rules[i];
12865 		for (j = 0; j < tmp_rule->n_patterns; j++)
12866 			kfree(tmp_rule->patterns[j].mask);
12867 		kfree(tmp_rule->patterns);
12868 	}
12869 	kfree(new_coalesce.rules);
12870 
12871 	return err;
12872 }
12873 
12874 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
12875 {
12876 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12877 	struct net_device *dev = info->user_ptr[1];
12878 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12879 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
12880 	struct cfg80211_gtk_rekey_data rekey_data = {};
12881 	int err;
12882 
12883 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
12884 		return -EINVAL;
12885 
12886 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
12887 					  info->attrs[NL80211_ATTR_REKEY_DATA],
12888 					  nl80211_rekey_policy, info->extack);
12889 	if (err)
12890 		return err;
12891 
12892 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
12893 	    !tb[NL80211_REKEY_DATA_KCK])
12894 		return -EINVAL;
12895 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN &&
12896 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
12897 	      nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KEK_EXT_LEN))
12898 		return -ERANGE;
12899 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN &&
12900 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
12901 	      nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KCK_EXT_LEN))
12902 		return -ERANGE;
12903 
12904 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
12905 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
12906 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
12907 	rekey_data.kek_len = nla_len(tb[NL80211_REKEY_DATA_KEK]);
12908 	rekey_data.kck_len = nla_len(tb[NL80211_REKEY_DATA_KCK]);
12909 	if (tb[NL80211_REKEY_DATA_AKM])
12910 		rekey_data.akm = nla_get_u32(tb[NL80211_REKEY_DATA_AKM]);
12911 
12912 	wdev_lock(wdev);
12913 	if (!wdev->current_bss) {
12914 		err = -ENOTCONN;
12915 		goto out;
12916 	}
12917 
12918 	if (!rdev->ops->set_rekey_data) {
12919 		err = -EOPNOTSUPP;
12920 		goto out;
12921 	}
12922 
12923 	err = rdev_set_rekey_data(rdev, dev, &rekey_data);
12924  out:
12925 	wdev_unlock(wdev);
12926 	return err;
12927 }
12928 
12929 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
12930 					     struct genl_info *info)
12931 {
12932 	struct net_device *dev = info->user_ptr[1];
12933 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12934 
12935 	if (wdev->iftype != NL80211_IFTYPE_AP &&
12936 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
12937 		return -EINVAL;
12938 
12939 	if (wdev->ap_unexpected_nlportid)
12940 		return -EBUSY;
12941 
12942 	wdev->ap_unexpected_nlportid = info->snd_portid;
12943 	return 0;
12944 }
12945 
12946 static int nl80211_probe_client(struct sk_buff *skb,
12947 				struct genl_info *info)
12948 {
12949 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12950 	struct net_device *dev = info->user_ptr[1];
12951 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12952 	struct sk_buff *msg;
12953 	void *hdr;
12954 	const u8 *addr;
12955 	u64 cookie;
12956 	int err;
12957 
12958 	if (wdev->iftype != NL80211_IFTYPE_AP &&
12959 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
12960 		return -EOPNOTSUPP;
12961 
12962 	if (!info->attrs[NL80211_ATTR_MAC])
12963 		return -EINVAL;
12964 
12965 	if (!rdev->ops->probe_client)
12966 		return -EOPNOTSUPP;
12967 
12968 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12969 	if (!msg)
12970 		return -ENOMEM;
12971 
12972 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12973 			     NL80211_CMD_PROBE_CLIENT);
12974 	if (!hdr) {
12975 		err = -ENOBUFS;
12976 		goto free_msg;
12977 	}
12978 
12979 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12980 
12981 	err = rdev_probe_client(rdev, dev, addr, &cookie);
12982 	if (err)
12983 		goto free_msg;
12984 
12985 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12986 			      NL80211_ATTR_PAD))
12987 		goto nla_put_failure;
12988 
12989 	genlmsg_end(msg, hdr);
12990 
12991 	return genlmsg_reply(msg, info);
12992 
12993  nla_put_failure:
12994 	err = -ENOBUFS;
12995  free_msg:
12996 	nlmsg_free(msg);
12997 	return err;
12998 }
12999 
13000 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
13001 {
13002 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13003 	struct cfg80211_beacon_registration *reg, *nreg;
13004 	int rv;
13005 
13006 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
13007 		return -EOPNOTSUPP;
13008 
13009 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
13010 	if (!nreg)
13011 		return -ENOMEM;
13012 
13013 	/* First, check if already registered. */
13014 	spin_lock_bh(&rdev->beacon_registrations_lock);
13015 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
13016 		if (reg->nlportid == info->snd_portid) {
13017 			rv = -EALREADY;
13018 			goto out_err;
13019 		}
13020 	}
13021 	/* Add it to the list */
13022 	nreg->nlportid = info->snd_portid;
13023 	list_add(&nreg->list, &rdev->beacon_registrations);
13024 
13025 	spin_unlock_bh(&rdev->beacon_registrations_lock);
13026 
13027 	return 0;
13028 out_err:
13029 	spin_unlock_bh(&rdev->beacon_registrations_lock);
13030 	kfree(nreg);
13031 	return rv;
13032 }
13033 
13034 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
13035 {
13036 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13037 	struct wireless_dev *wdev = info->user_ptr[1];
13038 	int err;
13039 
13040 	if (!rdev->ops->start_p2p_device)
13041 		return -EOPNOTSUPP;
13042 
13043 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
13044 		return -EOPNOTSUPP;
13045 
13046 	if (wdev_running(wdev))
13047 		return 0;
13048 
13049 	if (rfkill_blocked(rdev->wiphy.rfkill))
13050 		return -ERFKILL;
13051 
13052 	err = rdev_start_p2p_device(rdev, wdev);
13053 	if (err)
13054 		return err;
13055 
13056 	wdev->is_running = true;
13057 	rdev->opencount++;
13058 
13059 	return 0;
13060 }
13061 
13062 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
13063 {
13064 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13065 	struct wireless_dev *wdev = info->user_ptr[1];
13066 
13067 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
13068 		return -EOPNOTSUPP;
13069 
13070 	if (!rdev->ops->stop_p2p_device)
13071 		return -EOPNOTSUPP;
13072 
13073 	cfg80211_stop_p2p_device(rdev, wdev);
13074 
13075 	return 0;
13076 }
13077 
13078 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
13079 {
13080 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13081 	struct wireless_dev *wdev = info->user_ptr[1];
13082 	struct cfg80211_nan_conf conf = {};
13083 	int err;
13084 
13085 	if (wdev->iftype != NL80211_IFTYPE_NAN)
13086 		return -EOPNOTSUPP;
13087 
13088 	if (wdev_running(wdev))
13089 		return -EEXIST;
13090 
13091 	if (rfkill_blocked(rdev->wiphy.rfkill))
13092 		return -ERFKILL;
13093 
13094 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
13095 		return -EINVAL;
13096 
13097 	conf.master_pref =
13098 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
13099 
13100 	if (info->attrs[NL80211_ATTR_BANDS]) {
13101 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
13102 
13103 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
13104 			return -EOPNOTSUPP;
13105 
13106 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
13107 			return -EINVAL;
13108 
13109 		conf.bands = bands;
13110 	}
13111 
13112 	err = rdev_start_nan(rdev, wdev, &conf);
13113 	if (err)
13114 		return err;
13115 
13116 	wdev->is_running = true;
13117 	rdev->opencount++;
13118 
13119 	return 0;
13120 }
13121 
13122 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
13123 {
13124 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13125 	struct wireless_dev *wdev = info->user_ptr[1];
13126 
13127 	if (wdev->iftype != NL80211_IFTYPE_NAN)
13128 		return -EOPNOTSUPP;
13129 
13130 	cfg80211_stop_nan(rdev, wdev);
13131 
13132 	return 0;
13133 }
13134 
13135 static int validate_nan_filter(struct nlattr *filter_attr)
13136 {
13137 	struct nlattr *attr;
13138 	int len = 0, n_entries = 0, rem;
13139 
13140 	nla_for_each_nested(attr, filter_attr, rem) {
13141 		len += nla_len(attr);
13142 		n_entries++;
13143 	}
13144 
13145 	if (len >= U8_MAX)
13146 		return -EINVAL;
13147 
13148 	return n_entries;
13149 }
13150 
13151 static int handle_nan_filter(struct nlattr *attr_filter,
13152 			     struct cfg80211_nan_func *func,
13153 			     bool tx)
13154 {
13155 	struct nlattr *attr;
13156 	int n_entries, rem, i;
13157 	struct cfg80211_nan_func_filter *filter;
13158 
13159 	n_entries = validate_nan_filter(attr_filter);
13160 	if (n_entries < 0)
13161 		return n_entries;
13162 
13163 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
13164 
13165 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
13166 	if (!filter)
13167 		return -ENOMEM;
13168 
13169 	i = 0;
13170 	nla_for_each_nested(attr, attr_filter, rem) {
13171 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
13172 		filter[i].len = nla_len(attr);
13173 		i++;
13174 	}
13175 	if (tx) {
13176 		func->num_tx_filters = n_entries;
13177 		func->tx_filters = filter;
13178 	} else {
13179 		func->num_rx_filters = n_entries;
13180 		func->rx_filters = filter;
13181 	}
13182 
13183 	return 0;
13184 }
13185 
13186 static int nl80211_nan_add_func(struct sk_buff *skb,
13187 				struct genl_info *info)
13188 {
13189 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13190 	struct wireless_dev *wdev = info->user_ptr[1];
13191 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
13192 	struct cfg80211_nan_func *func;
13193 	struct sk_buff *msg = NULL;
13194 	void *hdr = NULL;
13195 	int err = 0;
13196 
13197 	if (wdev->iftype != NL80211_IFTYPE_NAN)
13198 		return -EOPNOTSUPP;
13199 
13200 	if (!wdev_running(wdev))
13201 		return -ENOTCONN;
13202 
13203 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
13204 		return -EINVAL;
13205 
13206 	err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
13207 					  info->attrs[NL80211_ATTR_NAN_FUNC],
13208 					  nl80211_nan_func_policy,
13209 					  info->extack);
13210 	if (err)
13211 		return err;
13212 
13213 	func = kzalloc(sizeof(*func), GFP_KERNEL);
13214 	if (!func)
13215 		return -ENOMEM;
13216 
13217 	func->cookie = cfg80211_assign_cookie(rdev);
13218 
13219 	if (!tb[NL80211_NAN_FUNC_TYPE]) {
13220 		err = -EINVAL;
13221 		goto out;
13222 	}
13223 
13224 
13225 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
13226 
13227 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
13228 		err = -EINVAL;
13229 		goto out;
13230 	}
13231 
13232 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
13233 	       sizeof(func->service_id));
13234 
13235 	func->close_range =
13236 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
13237 
13238 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
13239 		func->serv_spec_info_len =
13240 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
13241 		func->serv_spec_info =
13242 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
13243 				func->serv_spec_info_len,
13244 				GFP_KERNEL);
13245 		if (!func->serv_spec_info) {
13246 			err = -ENOMEM;
13247 			goto out;
13248 		}
13249 	}
13250 
13251 	if (tb[NL80211_NAN_FUNC_TTL])
13252 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
13253 
13254 	switch (func->type) {
13255 	case NL80211_NAN_FUNC_PUBLISH:
13256 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
13257 			err = -EINVAL;
13258 			goto out;
13259 		}
13260 
13261 		func->publish_type =
13262 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
13263 		func->publish_bcast =
13264 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
13265 
13266 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
13267 			func->publish_bcast) {
13268 			err = -EINVAL;
13269 			goto out;
13270 		}
13271 		break;
13272 	case NL80211_NAN_FUNC_SUBSCRIBE:
13273 		func->subscribe_active =
13274 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
13275 		break;
13276 	case NL80211_NAN_FUNC_FOLLOW_UP:
13277 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
13278 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
13279 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
13280 			err = -EINVAL;
13281 			goto out;
13282 		}
13283 
13284 		func->followup_id =
13285 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
13286 		func->followup_reqid =
13287 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
13288 		memcpy(func->followup_dest.addr,
13289 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
13290 		       sizeof(func->followup_dest.addr));
13291 		if (func->ttl) {
13292 			err = -EINVAL;
13293 			goto out;
13294 		}
13295 		break;
13296 	default:
13297 		err = -EINVAL;
13298 		goto out;
13299 	}
13300 
13301 	if (tb[NL80211_NAN_FUNC_SRF]) {
13302 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
13303 
13304 		err = nla_parse_nested_deprecated(srf_tb,
13305 						  NL80211_NAN_SRF_ATTR_MAX,
13306 						  tb[NL80211_NAN_FUNC_SRF],
13307 						  nl80211_nan_srf_policy,
13308 						  info->extack);
13309 		if (err)
13310 			goto out;
13311 
13312 		func->srf_include =
13313 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
13314 
13315 		if (srf_tb[NL80211_NAN_SRF_BF]) {
13316 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
13317 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
13318 				err = -EINVAL;
13319 				goto out;
13320 			}
13321 
13322 			func->srf_bf_len =
13323 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
13324 			func->srf_bf =
13325 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
13326 					func->srf_bf_len, GFP_KERNEL);
13327 			if (!func->srf_bf) {
13328 				err = -ENOMEM;
13329 				goto out;
13330 			}
13331 
13332 			func->srf_bf_idx =
13333 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
13334 		} else {
13335 			struct nlattr *attr, *mac_attr =
13336 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
13337 			int n_entries, rem, i = 0;
13338 
13339 			if (!mac_attr) {
13340 				err = -EINVAL;
13341 				goto out;
13342 			}
13343 
13344 			n_entries = validate_acl_mac_addrs(mac_attr);
13345 			if (n_entries <= 0) {
13346 				err = -EINVAL;
13347 				goto out;
13348 			}
13349 
13350 			func->srf_num_macs = n_entries;
13351 			func->srf_macs =
13352 				kcalloc(n_entries, sizeof(*func->srf_macs),
13353 					GFP_KERNEL);
13354 			if (!func->srf_macs) {
13355 				err = -ENOMEM;
13356 				goto out;
13357 			}
13358 
13359 			nla_for_each_nested(attr, mac_attr, rem)
13360 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
13361 				       sizeof(*func->srf_macs));
13362 		}
13363 	}
13364 
13365 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
13366 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
13367 					func, true);
13368 		if (err)
13369 			goto out;
13370 	}
13371 
13372 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
13373 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
13374 					func, false);
13375 		if (err)
13376 			goto out;
13377 	}
13378 
13379 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13380 	if (!msg) {
13381 		err = -ENOMEM;
13382 		goto out;
13383 	}
13384 
13385 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13386 			     NL80211_CMD_ADD_NAN_FUNCTION);
13387 	/* This can't really happen - we just allocated 4KB */
13388 	if (WARN_ON(!hdr)) {
13389 		err = -ENOMEM;
13390 		goto out;
13391 	}
13392 
13393 	err = rdev_add_nan_func(rdev, wdev, func);
13394 out:
13395 	if (err < 0) {
13396 		cfg80211_free_nan_func(func);
13397 		nlmsg_free(msg);
13398 		return err;
13399 	}
13400 
13401 	/* propagate the instance id and cookie to userspace  */
13402 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
13403 			      NL80211_ATTR_PAD))
13404 		goto nla_put_failure;
13405 
13406 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
13407 	if (!func_attr)
13408 		goto nla_put_failure;
13409 
13410 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
13411 		       func->instance_id))
13412 		goto nla_put_failure;
13413 
13414 	nla_nest_end(msg, func_attr);
13415 
13416 	genlmsg_end(msg, hdr);
13417 	return genlmsg_reply(msg, info);
13418 
13419 nla_put_failure:
13420 	nlmsg_free(msg);
13421 	return -ENOBUFS;
13422 }
13423 
13424 static int nl80211_nan_del_func(struct sk_buff *skb,
13425 			       struct genl_info *info)
13426 {
13427 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13428 	struct wireless_dev *wdev = info->user_ptr[1];
13429 	u64 cookie;
13430 
13431 	if (wdev->iftype != NL80211_IFTYPE_NAN)
13432 		return -EOPNOTSUPP;
13433 
13434 	if (!wdev_running(wdev))
13435 		return -ENOTCONN;
13436 
13437 	if (!info->attrs[NL80211_ATTR_COOKIE])
13438 		return -EINVAL;
13439 
13440 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
13441 
13442 	rdev_del_nan_func(rdev, wdev, cookie);
13443 
13444 	return 0;
13445 }
13446 
13447 static int nl80211_nan_change_config(struct sk_buff *skb,
13448 				     struct genl_info *info)
13449 {
13450 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13451 	struct wireless_dev *wdev = info->user_ptr[1];
13452 	struct cfg80211_nan_conf conf = {};
13453 	u32 changed = 0;
13454 
13455 	if (wdev->iftype != NL80211_IFTYPE_NAN)
13456 		return -EOPNOTSUPP;
13457 
13458 	if (!wdev_running(wdev))
13459 		return -ENOTCONN;
13460 
13461 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
13462 		conf.master_pref =
13463 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
13464 		if (conf.master_pref <= 1 || conf.master_pref == 255)
13465 			return -EINVAL;
13466 
13467 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
13468 	}
13469 
13470 	if (info->attrs[NL80211_ATTR_BANDS]) {
13471 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
13472 
13473 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
13474 			return -EOPNOTSUPP;
13475 
13476 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
13477 			return -EINVAL;
13478 
13479 		conf.bands = bands;
13480 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
13481 	}
13482 
13483 	if (!changed)
13484 		return -EINVAL;
13485 
13486 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
13487 }
13488 
13489 void cfg80211_nan_match(struct wireless_dev *wdev,
13490 			struct cfg80211_nan_match_params *match, gfp_t gfp)
13491 {
13492 	struct wiphy *wiphy = wdev->wiphy;
13493 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13494 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
13495 	struct sk_buff *msg;
13496 	void *hdr;
13497 
13498 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
13499 		return;
13500 
13501 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
13502 	if (!msg)
13503 		return;
13504 
13505 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
13506 	if (!hdr) {
13507 		nlmsg_free(msg);
13508 		return;
13509 	}
13510 
13511 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13512 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
13513 					 wdev->netdev->ifindex)) ||
13514 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
13515 			      NL80211_ATTR_PAD))
13516 		goto nla_put_failure;
13517 
13518 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
13519 			      NL80211_ATTR_PAD) ||
13520 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
13521 		goto nla_put_failure;
13522 
13523 	match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
13524 	if (!match_attr)
13525 		goto nla_put_failure;
13526 
13527 	local_func_attr = nla_nest_start_noflag(msg,
13528 						NL80211_NAN_MATCH_FUNC_LOCAL);
13529 	if (!local_func_attr)
13530 		goto nla_put_failure;
13531 
13532 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
13533 		goto nla_put_failure;
13534 
13535 	nla_nest_end(msg, local_func_attr);
13536 
13537 	peer_func_attr = nla_nest_start_noflag(msg,
13538 					       NL80211_NAN_MATCH_FUNC_PEER);
13539 	if (!peer_func_attr)
13540 		goto nla_put_failure;
13541 
13542 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
13543 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
13544 		goto nla_put_failure;
13545 
13546 	if (match->info && match->info_len &&
13547 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
13548 		    match->info))
13549 		goto nla_put_failure;
13550 
13551 	nla_nest_end(msg, peer_func_attr);
13552 	nla_nest_end(msg, match_attr);
13553 	genlmsg_end(msg, hdr);
13554 
13555 	if (!wdev->owner_nlportid)
13556 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
13557 					msg, 0, NL80211_MCGRP_NAN, gfp);
13558 	else
13559 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
13560 				wdev->owner_nlportid);
13561 
13562 	return;
13563 
13564 nla_put_failure:
13565 	nlmsg_free(msg);
13566 }
13567 EXPORT_SYMBOL(cfg80211_nan_match);
13568 
13569 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
13570 				  u8 inst_id,
13571 				  enum nl80211_nan_func_term_reason reason,
13572 				  u64 cookie, gfp_t gfp)
13573 {
13574 	struct wiphy *wiphy = wdev->wiphy;
13575 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13576 	struct sk_buff *msg;
13577 	struct nlattr *func_attr;
13578 	void *hdr;
13579 
13580 	if (WARN_ON(!inst_id))
13581 		return;
13582 
13583 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
13584 	if (!msg)
13585 		return;
13586 
13587 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
13588 	if (!hdr) {
13589 		nlmsg_free(msg);
13590 		return;
13591 	}
13592 
13593 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13594 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
13595 					 wdev->netdev->ifindex)) ||
13596 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
13597 			      NL80211_ATTR_PAD))
13598 		goto nla_put_failure;
13599 
13600 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
13601 			      NL80211_ATTR_PAD))
13602 		goto nla_put_failure;
13603 
13604 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
13605 	if (!func_attr)
13606 		goto nla_put_failure;
13607 
13608 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
13609 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
13610 		goto nla_put_failure;
13611 
13612 	nla_nest_end(msg, func_attr);
13613 	genlmsg_end(msg, hdr);
13614 
13615 	if (!wdev->owner_nlportid)
13616 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
13617 					msg, 0, NL80211_MCGRP_NAN, gfp);
13618 	else
13619 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
13620 				wdev->owner_nlportid);
13621 
13622 	return;
13623 
13624 nla_put_failure:
13625 	nlmsg_free(msg);
13626 }
13627 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
13628 
13629 static int nl80211_get_protocol_features(struct sk_buff *skb,
13630 					 struct genl_info *info)
13631 {
13632 	void *hdr;
13633 	struct sk_buff *msg;
13634 
13635 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13636 	if (!msg)
13637 		return -ENOMEM;
13638 
13639 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13640 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
13641 	if (!hdr)
13642 		goto nla_put_failure;
13643 
13644 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
13645 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
13646 		goto nla_put_failure;
13647 
13648 	genlmsg_end(msg, hdr);
13649 	return genlmsg_reply(msg, info);
13650 
13651  nla_put_failure:
13652 	kfree_skb(msg);
13653 	return -ENOBUFS;
13654 }
13655 
13656 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
13657 {
13658 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13659 	struct cfg80211_update_ft_ies_params ft_params;
13660 	struct net_device *dev = info->user_ptr[1];
13661 
13662 	if (!rdev->ops->update_ft_ies)
13663 		return -EOPNOTSUPP;
13664 
13665 	if (!info->attrs[NL80211_ATTR_MDID] ||
13666 	    !info->attrs[NL80211_ATTR_IE])
13667 		return -EINVAL;
13668 
13669 	memset(&ft_params, 0, sizeof(ft_params));
13670 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
13671 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
13672 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
13673 
13674 	return rdev_update_ft_ies(rdev, dev, &ft_params);
13675 }
13676 
13677 static int nl80211_crit_protocol_start(struct sk_buff *skb,
13678 				       struct genl_info *info)
13679 {
13680 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13681 	struct wireless_dev *wdev = info->user_ptr[1];
13682 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
13683 	u16 duration;
13684 	int ret;
13685 
13686 	if (!rdev->ops->crit_proto_start)
13687 		return -EOPNOTSUPP;
13688 
13689 	if (WARN_ON(!rdev->ops->crit_proto_stop))
13690 		return -EINVAL;
13691 
13692 	if (rdev->crit_proto_nlportid)
13693 		return -EBUSY;
13694 
13695 	/* determine protocol if provided */
13696 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
13697 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
13698 
13699 	if (proto >= NUM_NL80211_CRIT_PROTO)
13700 		return -EINVAL;
13701 
13702 	/* timeout must be provided */
13703 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
13704 		return -EINVAL;
13705 
13706 	duration =
13707 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
13708 
13709 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
13710 	if (!ret)
13711 		rdev->crit_proto_nlportid = info->snd_portid;
13712 
13713 	return ret;
13714 }
13715 
13716 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
13717 				      struct genl_info *info)
13718 {
13719 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13720 	struct wireless_dev *wdev = info->user_ptr[1];
13721 
13722 	if (!rdev->ops->crit_proto_stop)
13723 		return -EOPNOTSUPP;
13724 
13725 	if (rdev->crit_proto_nlportid) {
13726 		rdev->crit_proto_nlportid = 0;
13727 		rdev_crit_proto_stop(rdev, wdev);
13728 	}
13729 	return 0;
13730 }
13731 
13732 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
13733 				       struct nlattr *attr,
13734 				       struct netlink_ext_ack *extack)
13735 {
13736 	if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
13737 		if (attr->nla_type & NLA_F_NESTED) {
13738 			NL_SET_ERR_MSG_ATTR(extack, attr,
13739 					    "unexpected nested data");
13740 			return -EINVAL;
13741 		}
13742 
13743 		return 0;
13744 	}
13745 
13746 	if (!(attr->nla_type & NLA_F_NESTED)) {
13747 		NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
13748 		return -EINVAL;
13749 	}
13750 
13751 	return nla_validate_nested(attr, vcmd->maxattr, vcmd->policy, extack);
13752 }
13753 
13754 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
13755 {
13756 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13757 	struct wireless_dev *wdev =
13758 		__cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
13759 					   info->attrs);
13760 	int i, err;
13761 	u32 vid, subcmd;
13762 
13763 	if (!rdev->wiphy.vendor_commands)
13764 		return -EOPNOTSUPP;
13765 
13766 	if (IS_ERR(wdev)) {
13767 		err = PTR_ERR(wdev);
13768 		if (err != -EINVAL)
13769 			return err;
13770 		wdev = NULL;
13771 	} else if (wdev->wiphy != &rdev->wiphy) {
13772 		return -EINVAL;
13773 	}
13774 
13775 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
13776 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
13777 		return -EINVAL;
13778 
13779 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
13780 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
13781 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
13782 		const struct wiphy_vendor_command *vcmd;
13783 		void *data = NULL;
13784 		int len = 0;
13785 
13786 		vcmd = &rdev->wiphy.vendor_commands[i];
13787 
13788 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
13789 			continue;
13790 
13791 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
13792 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
13793 			if (!wdev)
13794 				return -EINVAL;
13795 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
13796 			    !wdev->netdev)
13797 				return -EINVAL;
13798 
13799 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
13800 				if (!wdev_running(wdev))
13801 					return -ENETDOWN;
13802 			}
13803 		} else {
13804 			wdev = NULL;
13805 		}
13806 
13807 		if (!vcmd->doit)
13808 			return -EOPNOTSUPP;
13809 
13810 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
13811 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
13812 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
13813 
13814 			err = nl80211_vendor_check_policy(vcmd,
13815 					info->attrs[NL80211_ATTR_VENDOR_DATA],
13816 					info->extack);
13817 			if (err)
13818 				return err;
13819 		}
13820 
13821 		rdev->cur_cmd_info = info;
13822 		err = vcmd->doit(&rdev->wiphy, wdev, data, len);
13823 		rdev->cur_cmd_info = NULL;
13824 		return err;
13825 	}
13826 
13827 	return -EOPNOTSUPP;
13828 }
13829 
13830 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
13831 				       struct netlink_callback *cb,
13832 				       struct cfg80211_registered_device **rdev,
13833 				       struct wireless_dev **wdev)
13834 {
13835 	struct nlattr **attrbuf;
13836 	u32 vid, subcmd;
13837 	unsigned int i;
13838 	int vcmd_idx = -1;
13839 	int err;
13840 	void *data = NULL;
13841 	unsigned int data_len = 0;
13842 
13843 	if (cb->args[0]) {
13844 		/* subtract the 1 again here */
13845 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
13846 		struct wireless_dev *tmp;
13847 
13848 		if (!wiphy)
13849 			return -ENODEV;
13850 		*rdev = wiphy_to_rdev(wiphy);
13851 		*wdev = NULL;
13852 
13853 		if (cb->args[1]) {
13854 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
13855 				if (tmp->identifier == cb->args[1] - 1) {
13856 					*wdev = tmp;
13857 					break;
13858 				}
13859 			}
13860 		}
13861 
13862 		/* keep rtnl locked in successful case */
13863 		return 0;
13864 	}
13865 
13866 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
13867 	if (!attrbuf)
13868 		return -ENOMEM;
13869 
13870 	err = nlmsg_parse_deprecated(cb->nlh,
13871 				     GENL_HDRLEN + nl80211_fam.hdrsize,
13872 				     attrbuf, nl80211_fam.maxattr,
13873 				     nl80211_policy, NULL);
13874 	if (err)
13875 		goto out;
13876 
13877 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
13878 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
13879 		err = -EINVAL;
13880 		goto out;
13881 	}
13882 
13883 	*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(skb->sk), attrbuf);
13884 	if (IS_ERR(*wdev))
13885 		*wdev = NULL;
13886 
13887 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
13888 	if (IS_ERR(*rdev)) {
13889 		err = PTR_ERR(*rdev);
13890 		goto out;
13891 	}
13892 
13893 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
13894 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
13895 
13896 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
13897 		const struct wiphy_vendor_command *vcmd;
13898 
13899 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
13900 
13901 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
13902 			continue;
13903 
13904 		if (!vcmd->dumpit) {
13905 			err = -EOPNOTSUPP;
13906 			goto out;
13907 		}
13908 
13909 		vcmd_idx = i;
13910 		break;
13911 	}
13912 
13913 	if (vcmd_idx < 0) {
13914 		err = -EOPNOTSUPP;
13915 		goto out;
13916 	}
13917 
13918 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
13919 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
13920 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
13921 
13922 		err = nl80211_vendor_check_policy(
13923 				&(*rdev)->wiphy.vendor_commands[vcmd_idx],
13924 				attrbuf[NL80211_ATTR_VENDOR_DATA],
13925 				cb->extack);
13926 		if (err)
13927 			goto out;
13928 	}
13929 
13930 	/* 0 is the first index - add 1 to parse only once */
13931 	cb->args[0] = (*rdev)->wiphy_idx + 1;
13932 	/* add 1 to know if it was NULL */
13933 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
13934 	cb->args[2] = vcmd_idx;
13935 	cb->args[3] = (unsigned long)data;
13936 	cb->args[4] = data_len;
13937 
13938 	/* keep rtnl locked in successful case */
13939 	err = 0;
13940 out:
13941 	kfree(attrbuf);
13942 	return err;
13943 }
13944 
13945 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
13946 				   struct netlink_callback *cb)
13947 {
13948 	struct cfg80211_registered_device *rdev;
13949 	struct wireless_dev *wdev;
13950 	unsigned int vcmd_idx;
13951 	const struct wiphy_vendor_command *vcmd;
13952 	void *data;
13953 	int data_len;
13954 	int err;
13955 	struct nlattr *vendor_data;
13956 
13957 	rtnl_lock();
13958 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
13959 	if (err)
13960 		goto out;
13961 
13962 	vcmd_idx = cb->args[2];
13963 	data = (void *)cb->args[3];
13964 	data_len = cb->args[4];
13965 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
13966 
13967 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
13968 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
13969 		if (!wdev) {
13970 			err = -EINVAL;
13971 			goto out;
13972 		}
13973 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
13974 		    !wdev->netdev) {
13975 			err = -EINVAL;
13976 			goto out;
13977 		}
13978 
13979 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
13980 			if (!wdev_running(wdev)) {
13981 				err = -ENETDOWN;
13982 				goto out;
13983 			}
13984 		}
13985 	}
13986 
13987 	while (1) {
13988 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
13989 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
13990 					   NL80211_CMD_VENDOR);
13991 		if (!hdr)
13992 			break;
13993 
13994 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13995 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
13996 					       wdev_id(wdev),
13997 					       NL80211_ATTR_PAD))) {
13998 			genlmsg_cancel(skb, hdr);
13999 			break;
14000 		}
14001 
14002 		vendor_data = nla_nest_start_noflag(skb,
14003 						    NL80211_ATTR_VENDOR_DATA);
14004 		if (!vendor_data) {
14005 			genlmsg_cancel(skb, hdr);
14006 			break;
14007 		}
14008 
14009 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
14010 				   (unsigned long *)&cb->args[5]);
14011 		nla_nest_end(skb, vendor_data);
14012 
14013 		if (err == -ENOBUFS || err == -ENOENT) {
14014 			genlmsg_cancel(skb, hdr);
14015 			break;
14016 		} else if (err <= 0) {
14017 			genlmsg_cancel(skb, hdr);
14018 			goto out;
14019 		}
14020 
14021 		genlmsg_end(skb, hdr);
14022 	}
14023 
14024 	err = skb->len;
14025  out:
14026 	rtnl_unlock();
14027 	return err;
14028 }
14029 
14030 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
14031 					   enum nl80211_commands cmd,
14032 					   enum nl80211_attrs attr,
14033 					   int approxlen)
14034 {
14035 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14036 
14037 	if (WARN_ON(!rdev->cur_cmd_info))
14038 		return NULL;
14039 
14040 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
14041 					   rdev->cur_cmd_info->snd_portid,
14042 					   rdev->cur_cmd_info->snd_seq,
14043 					   cmd, attr, NULL, GFP_KERNEL);
14044 }
14045 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
14046 
14047 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
14048 {
14049 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
14050 	void *hdr = ((void **)skb->cb)[1];
14051 	struct nlattr *data = ((void **)skb->cb)[2];
14052 
14053 	/* clear CB data for netlink core to own from now on */
14054 	memset(skb->cb, 0, sizeof(skb->cb));
14055 
14056 	if (WARN_ON(!rdev->cur_cmd_info)) {
14057 		kfree_skb(skb);
14058 		return -EINVAL;
14059 	}
14060 
14061 	nla_nest_end(skb, data);
14062 	genlmsg_end(skb, hdr);
14063 	return genlmsg_reply(skb, rdev->cur_cmd_info);
14064 }
14065 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
14066 
14067 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
14068 {
14069 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14070 
14071 	if (WARN_ON(!rdev->cur_cmd_info))
14072 		return 0;
14073 
14074 	return rdev->cur_cmd_info->snd_portid;
14075 }
14076 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
14077 
14078 static int nl80211_set_qos_map(struct sk_buff *skb,
14079 			       struct genl_info *info)
14080 {
14081 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14082 	struct cfg80211_qos_map *qos_map = NULL;
14083 	struct net_device *dev = info->user_ptr[1];
14084 	u8 *pos, len, num_des, des_len, des;
14085 	int ret;
14086 
14087 	if (!rdev->ops->set_qos_map)
14088 		return -EOPNOTSUPP;
14089 
14090 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
14091 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
14092 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
14093 
14094 		if (len % 2)
14095 			return -EINVAL;
14096 
14097 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
14098 		if (!qos_map)
14099 			return -ENOMEM;
14100 
14101 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
14102 		if (num_des) {
14103 			des_len = num_des *
14104 				sizeof(struct cfg80211_dscp_exception);
14105 			memcpy(qos_map->dscp_exception, pos, des_len);
14106 			qos_map->num_des = num_des;
14107 			for (des = 0; des < num_des; des++) {
14108 				if (qos_map->dscp_exception[des].up > 7) {
14109 					kfree(qos_map);
14110 					return -EINVAL;
14111 				}
14112 			}
14113 			pos += des_len;
14114 		}
14115 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
14116 	}
14117 
14118 	wdev_lock(dev->ieee80211_ptr);
14119 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
14120 	if (!ret)
14121 		ret = rdev_set_qos_map(rdev, dev, qos_map);
14122 	wdev_unlock(dev->ieee80211_ptr);
14123 
14124 	kfree(qos_map);
14125 	return ret;
14126 }
14127 
14128 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
14129 {
14130 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14131 	struct net_device *dev = info->user_ptr[1];
14132 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14133 	const u8 *peer;
14134 	u8 tsid, up;
14135 	u16 admitted_time = 0;
14136 	int err;
14137 
14138 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
14139 		return -EOPNOTSUPP;
14140 
14141 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
14142 	    !info->attrs[NL80211_ATTR_USER_PRIO])
14143 		return -EINVAL;
14144 
14145 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
14146 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
14147 
14148 	/* WMM uses TIDs 0-7 even for TSPEC */
14149 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
14150 		/* TODO: handle 802.11 TSPEC/admission control
14151 		 * need more attributes for that (e.g. BA session requirement);
14152 		 * change the WMM adminssion test above to allow both then
14153 		 */
14154 		return -EINVAL;
14155 	}
14156 
14157 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
14158 
14159 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
14160 		admitted_time =
14161 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
14162 		if (!admitted_time)
14163 			return -EINVAL;
14164 	}
14165 
14166 	wdev_lock(wdev);
14167 	switch (wdev->iftype) {
14168 	case NL80211_IFTYPE_STATION:
14169 	case NL80211_IFTYPE_P2P_CLIENT:
14170 		if (wdev->current_bss)
14171 			break;
14172 		err = -ENOTCONN;
14173 		goto out;
14174 	default:
14175 		err = -EOPNOTSUPP;
14176 		goto out;
14177 	}
14178 
14179 	err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
14180 
14181  out:
14182 	wdev_unlock(wdev);
14183 	return err;
14184 }
14185 
14186 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
14187 {
14188 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14189 	struct net_device *dev = info->user_ptr[1];
14190 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14191 	const u8 *peer;
14192 	u8 tsid;
14193 	int err;
14194 
14195 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
14196 		return -EINVAL;
14197 
14198 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
14199 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
14200 
14201 	wdev_lock(wdev);
14202 	err = rdev_del_tx_ts(rdev, dev, tsid, peer);
14203 	wdev_unlock(wdev);
14204 
14205 	return err;
14206 }
14207 
14208 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
14209 				       struct genl_info *info)
14210 {
14211 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14212 	struct net_device *dev = info->user_ptr[1];
14213 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14214 	struct cfg80211_chan_def chandef = {};
14215 	const u8 *addr;
14216 	u8 oper_class;
14217 	int err;
14218 
14219 	if (!rdev->ops->tdls_channel_switch ||
14220 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
14221 		return -EOPNOTSUPP;
14222 
14223 	switch (dev->ieee80211_ptr->iftype) {
14224 	case NL80211_IFTYPE_STATION:
14225 	case NL80211_IFTYPE_P2P_CLIENT:
14226 		break;
14227 	default:
14228 		return -EOPNOTSUPP;
14229 	}
14230 
14231 	if (!info->attrs[NL80211_ATTR_MAC] ||
14232 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
14233 		return -EINVAL;
14234 
14235 	err = nl80211_parse_chandef(rdev, info, &chandef);
14236 	if (err)
14237 		return err;
14238 
14239 	/*
14240 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
14241 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
14242 	 * specification is not defined for them.
14243 	 */
14244 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
14245 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
14246 	    chandef.width != NL80211_CHAN_WIDTH_20)
14247 		return -EINVAL;
14248 
14249 	/* we will be active on the TDLS link */
14250 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
14251 					   wdev->iftype))
14252 		return -EINVAL;
14253 
14254 	/* don't allow switching to DFS channels */
14255 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
14256 		return -EINVAL;
14257 
14258 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
14259 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
14260 
14261 	wdev_lock(wdev);
14262 	err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
14263 	wdev_unlock(wdev);
14264 
14265 	return err;
14266 }
14267 
14268 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
14269 					      struct genl_info *info)
14270 {
14271 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14272 	struct net_device *dev = info->user_ptr[1];
14273 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14274 	const u8 *addr;
14275 
14276 	if (!rdev->ops->tdls_channel_switch ||
14277 	    !rdev->ops->tdls_cancel_channel_switch ||
14278 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
14279 		return -EOPNOTSUPP;
14280 
14281 	switch (dev->ieee80211_ptr->iftype) {
14282 	case NL80211_IFTYPE_STATION:
14283 	case NL80211_IFTYPE_P2P_CLIENT:
14284 		break;
14285 	default:
14286 		return -EOPNOTSUPP;
14287 	}
14288 
14289 	if (!info->attrs[NL80211_ATTR_MAC])
14290 		return -EINVAL;
14291 
14292 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
14293 
14294 	wdev_lock(wdev);
14295 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
14296 	wdev_unlock(wdev);
14297 
14298 	return 0;
14299 }
14300 
14301 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
14302 					    struct genl_info *info)
14303 {
14304 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14305 	struct net_device *dev = info->user_ptr[1];
14306 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14307 	const struct nlattr *nla;
14308 	bool enabled;
14309 
14310 	if (!rdev->ops->set_multicast_to_unicast)
14311 		return -EOPNOTSUPP;
14312 
14313 	if (wdev->iftype != NL80211_IFTYPE_AP &&
14314 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
14315 		return -EOPNOTSUPP;
14316 
14317 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
14318 	enabled = nla_get_flag(nla);
14319 
14320 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
14321 }
14322 
14323 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
14324 {
14325 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14326 	struct net_device *dev = info->user_ptr[1];
14327 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14328 	struct cfg80211_pmk_conf pmk_conf = {};
14329 	int ret;
14330 
14331 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
14332 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
14333 		return -EOPNOTSUPP;
14334 
14335 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
14336 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
14337 		return -EOPNOTSUPP;
14338 
14339 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
14340 		return -EINVAL;
14341 
14342 	wdev_lock(wdev);
14343 	if (!wdev->current_bss) {
14344 		ret = -ENOTCONN;
14345 		goto out;
14346 	}
14347 
14348 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
14349 	if (memcmp(pmk_conf.aa, wdev->current_bss->pub.bssid, ETH_ALEN)) {
14350 		ret = -EINVAL;
14351 		goto out;
14352 	}
14353 
14354 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
14355 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
14356 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
14357 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
14358 		ret = -EINVAL;
14359 		goto out;
14360 	}
14361 
14362 	if (info->attrs[NL80211_ATTR_PMKR0_NAME])
14363 		pmk_conf.pmk_r0_name =
14364 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
14365 
14366 	ret = rdev_set_pmk(rdev, dev, &pmk_conf);
14367 out:
14368 	wdev_unlock(wdev);
14369 	return ret;
14370 }
14371 
14372 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
14373 {
14374 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14375 	struct net_device *dev = info->user_ptr[1];
14376 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14377 	const u8 *aa;
14378 	int ret;
14379 
14380 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
14381 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
14382 		return -EOPNOTSUPP;
14383 
14384 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
14385 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
14386 		return -EOPNOTSUPP;
14387 
14388 	if (!info->attrs[NL80211_ATTR_MAC])
14389 		return -EINVAL;
14390 
14391 	wdev_lock(wdev);
14392 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
14393 	ret = rdev_del_pmk(rdev, dev, aa);
14394 	wdev_unlock(wdev);
14395 
14396 	return ret;
14397 }
14398 
14399 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
14400 {
14401 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14402 	struct net_device *dev = info->user_ptr[1];
14403 	struct cfg80211_external_auth_params params;
14404 
14405 	if (!rdev->ops->external_auth)
14406 		return -EOPNOTSUPP;
14407 
14408 	if (!info->attrs[NL80211_ATTR_SSID] &&
14409 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
14410 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
14411 		return -EINVAL;
14412 
14413 	if (!info->attrs[NL80211_ATTR_BSSID])
14414 		return -EINVAL;
14415 
14416 	if (!info->attrs[NL80211_ATTR_STATUS_CODE])
14417 		return -EINVAL;
14418 
14419 	memset(&params, 0, sizeof(params));
14420 
14421 	if (info->attrs[NL80211_ATTR_SSID]) {
14422 		params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
14423 		if (params.ssid.ssid_len == 0)
14424 			return -EINVAL;
14425 		memcpy(params.ssid.ssid,
14426 		       nla_data(info->attrs[NL80211_ATTR_SSID]),
14427 		       params.ssid.ssid_len);
14428 	}
14429 
14430 	memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
14431 	       ETH_ALEN);
14432 
14433 	params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
14434 
14435 	if (info->attrs[NL80211_ATTR_PMKID])
14436 		params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
14437 
14438 	return rdev_external_auth(rdev, dev, &params);
14439 }
14440 
14441 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
14442 {
14443 	bool dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
14444 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14445 	struct net_device *dev = info->user_ptr[1];
14446 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14447 	const u8 *buf;
14448 	size_t len;
14449 	u8 *dest;
14450 	u16 proto;
14451 	bool noencrypt;
14452 	u64 cookie = 0;
14453 	int err;
14454 
14455 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
14456 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
14457 		return -EOPNOTSUPP;
14458 
14459 	if (!rdev->ops->tx_control_port)
14460 		return -EOPNOTSUPP;
14461 
14462 	if (!info->attrs[NL80211_ATTR_FRAME] ||
14463 	    !info->attrs[NL80211_ATTR_MAC] ||
14464 	    !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
14465 		GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
14466 		return -EINVAL;
14467 	}
14468 
14469 	wdev_lock(wdev);
14470 
14471 	switch (wdev->iftype) {
14472 	case NL80211_IFTYPE_AP:
14473 	case NL80211_IFTYPE_P2P_GO:
14474 	case NL80211_IFTYPE_MESH_POINT:
14475 		break;
14476 	case NL80211_IFTYPE_ADHOC:
14477 	case NL80211_IFTYPE_STATION:
14478 	case NL80211_IFTYPE_P2P_CLIENT:
14479 		if (wdev->current_bss)
14480 			break;
14481 		err = -ENOTCONN;
14482 		goto out;
14483 	default:
14484 		err = -EOPNOTSUPP;
14485 		goto out;
14486 	}
14487 
14488 	wdev_unlock(wdev);
14489 
14490 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
14491 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
14492 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
14493 	proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
14494 	noencrypt =
14495 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
14496 
14497 	err = rdev_tx_control_port(rdev, dev, buf, len,
14498 				   dest, cpu_to_be16(proto), noencrypt,
14499 				   dont_wait_for_ack ? NULL : &cookie);
14500 	if (!err && !dont_wait_for_ack)
14501 		nl_set_extack_cookie_u64(info->extack, cookie);
14502 	return err;
14503  out:
14504 	wdev_unlock(wdev);
14505 	return err;
14506 }
14507 
14508 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
14509 					   struct genl_info *info)
14510 {
14511 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14512 	struct net_device *dev = info->user_ptr[1];
14513 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14514 	struct cfg80211_ftm_responder_stats ftm_stats = {};
14515 	struct sk_buff *msg;
14516 	void *hdr;
14517 	struct nlattr *ftm_stats_attr;
14518 	int err;
14519 
14520 	if (wdev->iftype != NL80211_IFTYPE_AP || !wdev->beacon_interval)
14521 		return -EOPNOTSUPP;
14522 
14523 	err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
14524 	if (err)
14525 		return err;
14526 
14527 	if (!ftm_stats.filled)
14528 		return -ENODATA;
14529 
14530 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14531 	if (!msg)
14532 		return -ENOMEM;
14533 
14534 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14535 			     NL80211_CMD_GET_FTM_RESPONDER_STATS);
14536 	if (!hdr)
14537 		goto nla_put_failure;
14538 
14539 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
14540 		goto nla_put_failure;
14541 
14542 	ftm_stats_attr = nla_nest_start_noflag(msg,
14543 					       NL80211_ATTR_FTM_RESPONDER_STATS);
14544 	if (!ftm_stats_attr)
14545 		goto nla_put_failure;
14546 
14547 #define SET_FTM(field, name, type)					 \
14548 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
14549 	    nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,		 \
14550 			     ftm_stats.field))				 \
14551 		goto nla_put_failure; } while (0)
14552 #define SET_FTM_U64(field, name)					 \
14553 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
14554 	    nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,		 \
14555 			      ftm_stats.field, NL80211_FTM_STATS_PAD))	 \
14556 		goto nla_put_failure; } while (0)
14557 
14558 	SET_FTM(success_num, SUCCESS_NUM, u32);
14559 	SET_FTM(partial_num, PARTIAL_NUM, u32);
14560 	SET_FTM(failed_num, FAILED_NUM, u32);
14561 	SET_FTM(asap_num, ASAP_NUM, u32);
14562 	SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
14563 	SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
14564 	SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
14565 	SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
14566 	SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
14567 #undef SET_FTM
14568 
14569 	nla_nest_end(msg, ftm_stats_attr);
14570 
14571 	genlmsg_end(msg, hdr);
14572 	return genlmsg_reply(msg, info);
14573 
14574 nla_put_failure:
14575 	nlmsg_free(msg);
14576 	return -ENOBUFS;
14577 }
14578 
14579 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
14580 {
14581 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14582 	struct cfg80211_update_owe_info owe_info;
14583 	struct net_device *dev = info->user_ptr[1];
14584 
14585 	if (!rdev->ops->update_owe_info)
14586 		return -EOPNOTSUPP;
14587 
14588 	if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
14589 	    !info->attrs[NL80211_ATTR_MAC])
14590 		return -EINVAL;
14591 
14592 	memset(&owe_info, 0, sizeof(owe_info));
14593 	owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
14594 	nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
14595 
14596 	if (info->attrs[NL80211_ATTR_IE]) {
14597 		owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
14598 		owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
14599 	}
14600 
14601 	return rdev_update_owe_info(rdev, dev, &owe_info);
14602 }
14603 
14604 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
14605 {
14606 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14607 	struct net_device *dev = info->user_ptr[1];
14608 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14609 	struct station_info sinfo = {};
14610 	const u8 *buf;
14611 	size_t len;
14612 	u8 *dest;
14613 	int err;
14614 
14615 	if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
14616 		return -EOPNOTSUPP;
14617 
14618 	if (!info->attrs[NL80211_ATTR_MAC] ||
14619 	    !info->attrs[NL80211_ATTR_FRAME]) {
14620 		GENL_SET_ERR_MSG(info, "Frame or MAC missing");
14621 		return -EINVAL;
14622 	}
14623 
14624 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
14625 		return -EOPNOTSUPP;
14626 
14627 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
14628 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
14629 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
14630 
14631 	if (len < sizeof(struct ethhdr))
14632 		return -EINVAL;
14633 
14634 	if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
14635 	    !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
14636 		return -EINVAL;
14637 
14638 	err = rdev_get_station(rdev, dev, dest, &sinfo);
14639 	if (err)
14640 		return err;
14641 
14642 	cfg80211_sinfo_release_content(&sinfo);
14643 
14644 	return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
14645 }
14646 
14647 static int parse_tid_conf(struct cfg80211_registered_device *rdev,
14648 			  struct nlattr *attrs[], struct net_device *dev,
14649 			  struct cfg80211_tid_cfg *tid_conf,
14650 			  struct genl_info *info, const u8 *peer)
14651 {
14652 	struct netlink_ext_ack *extack = info->extack;
14653 	u64 mask;
14654 	int err;
14655 
14656 	if (!attrs[NL80211_TID_CONFIG_ATTR_TIDS])
14657 		return -EINVAL;
14658 
14659 	tid_conf->config_override =
14660 			nla_get_flag(attrs[NL80211_TID_CONFIG_ATTR_OVERRIDE]);
14661 	tid_conf->tids = nla_get_u16(attrs[NL80211_TID_CONFIG_ATTR_TIDS]);
14662 
14663 	if (tid_conf->config_override) {
14664 		if (rdev->ops->reset_tid_config) {
14665 			err = rdev_reset_tid_config(rdev, dev, peer,
14666 						    tid_conf->tids);
14667 			if (err)
14668 				return err;
14669 		} else {
14670 			return -EINVAL;
14671 		}
14672 	}
14673 
14674 	if (attrs[NL80211_TID_CONFIG_ATTR_NOACK]) {
14675 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_NOACK);
14676 		tid_conf->noack =
14677 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_NOACK]);
14678 	}
14679 
14680 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]) {
14681 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT);
14682 		tid_conf->retry_short =
14683 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]);
14684 
14685 		if (tid_conf->retry_short > rdev->wiphy.max_data_retry_count)
14686 			return -EINVAL;
14687 	}
14688 
14689 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]) {
14690 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
14691 		tid_conf->retry_long =
14692 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]);
14693 
14694 		if (tid_conf->retry_long > rdev->wiphy.max_data_retry_count)
14695 			return -EINVAL;
14696 	}
14697 
14698 	if (attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]) {
14699 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
14700 		tid_conf->ampdu =
14701 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]);
14702 	}
14703 
14704 	if (attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]) {
14705 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
14706 		tid_conf->rtscts =
14707 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]);
14708 	}
14709 
14710 	if (attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]) {
14711 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
14712 		tid_conf->amsdu =
14713 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]);
14714 	}
14715 
14716 	if (attrs[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE]) {
14717 		u32 idx = NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE, attr;
14718 
14719 		tid_conf->txrate_type = nla_get_u8(attrs[idx]);
14720 
14721 		if (tid_conf->txrate_type != NL80211_TX_RATE_AUTOMATIC) {
14722 			attr = NL80211_TID_CONFIG_ATTR_TX_RATE;
14723 			err = nl80211_parse_tx_bitrate_mask(info, attrs, attr,
14724 						    &tid_conf->txrate_mask, dev,
14725 						    true);
14726 			if (err)
14727 				return err;
14728 
14729 			tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE);
14730 		}
14731 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
14732 	}
14733 
14734 	if (peer)
14735 		mask = rdev->wiphy.tid_config_support.peer;
14736 	else
14737 		mask = rdev->wiphy.tid_config_support.vif;
14738 
14739 	if (tid_conf->mask & ~mask) {
14740 		NL_SET_ERR_MSG(extack, "unsupported TID configuration");
14741 		return -ENOTSUPP;
14742 	}
14743 
14744 	return 0;
14745 }
14746 
14747 static int nl80211_set_tid_config(struct sk_buff *skb,
14748 				  struct genl_info *info)
14749 {
14750 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14751 	struct nlattr *attrs[NL80211_TID_CONFIG_ATTR_MAX + 1];
14752 	struct net_device *dev = info->user_ptr[1];
14753 	struct cfg80211_tid_config *tid_config;
14754 	struct nlattr *tid;
14755 	int conf_idx = 0, rem_conf;
14756 	int ret = -EINVAL;
14757 	u32 num_conf = 0;
14758 
14759 	if (!info->attrs[NL80211_ATTR_TID_CONFIG])
14760 		return -EINVAL;
14761 
14762 	if (!rdev->ops->set_tid_config)
14763 		return -EOPNOTSUPP;
14764 
14765 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
14766 			    rem_conf)
14767 		num_conf++;
14768 
14769 	tid_config = kzalloc(struct_size(tid_config, tid_conf, num_conf),
14770 			     GFP_KERNEL);
14771 	if (!tid_config)
14772 		return -ENOMEM;
14773 
14774 	tid_config->n_tid_conf = num_conf;
14775 
14776 	if (info->attrs[NL80211_ATTR_MAC])
14777 		tid_config->peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
14778 
14779 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
14780 			    rem_conf) {
14781 		ret = nla_parse_nested(attrs, NL80211_TID_CONFIG_ATTR_MAX,
14782 				       tid, NULL, NULL);
14783 
14784 		if (ret)
14785 			goto bad_tid_conf;
14786 
14787 		ret = parse_tid_conf(rdev, attrs, dev,
14788 				     &tid_config->tid_conf[conf_idx],
14789 				     info, tid_config->peer);
14790 		if (ret)
14791 			goto bad_tid_conf;
14792 
14793 		conf_idx++;
14794 	}
14795 
14796 	ret = rdev_set_tid_config(rdev, dev, tid_config);
14797 
14798 bad_tid_conf:
14799 	kfree(tid_config);
14800 	return ret;
14801 }
14802 
14803 #define NL80211_FLAG_NEED_WIPHY		0x01
14804 #define NL80211_FLAG_NEED_NETDEV	0x02
14805 #define NL80211_FLAG_NEED_RTNL		0x04
14806 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
14807 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
14808 					 NL80211_FLAG_CHECK_NETDEV_UP)
14809 #define NL80211_FLAG_NEED_WDEV		0x10
14810 /* If a netdev is associated, it must be UP, P2P must be started */
14811 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
14812 					 NL80211_FLAG_CHECK_NETDEV_UP)
14813 #define NL80211_FLAG_CLEAR_SKB		0x20
14814 #define NL80211_FLAG_NO_WIPHY_MTX	0x40
14815 
14816 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
14817 			    struct genl_info *info)
14818 {
14819 	struct cfg80211_registered_device *rdev = NULL;
14820 	struct wireless_dev *wdev;
14821 	struct net_device *dev;
14822 
14823 	rtnl_lock();
14824 	if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
14825 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
14826 		if (IS_ERR(rdev)) {
14827 			rtnl_unlock();
14828 			return PTR_ERR(rdev);
14829 		}
14830 		info->user_ptr[0] = rdev;
14831 	} else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
14832 		   ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
14833 		wdev = __cfg80211_wdev_from_attrs(NULL, genl_info_net(info),
14834 						  info->attrs);
14835 		if (IS_ERR(wdev)) {
14836 			rtnl_unlock();
14837 			return PTR_ERR(wdev);
14838 		}
14839 
14840 		dev = wdev->netdev;
14841 		rdev = wiphy_to_rdev(wdev->wiphy);
14842 
14843 		if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
14844 			if (!dev) {
14845 				rtnl_unlock();
14846 				return -EINVAL;
14847 			}
14848 
14849 			info->user_ptr[1] = dev;
14850 		} else {
14851 			info->user_ptr[1] = wdev;
14852 		}
14853 
14854 		if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
14855 		    !wdev_running(wdev)) {
14856 			rtnl_unlock();
14857 			return -ENETDOWN;
14858 		}
14859 
14860 		if (dev)
14861 			dev_hold(dev);
14862 
14863 		info->user_ptr[0] = rdev;
14864 	}
14865 
14866 	if (rdev && !(ops->internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
14867 		wiphy_lock(&rdev->wiphy);
14868 		/* we keep the mutex locked until post_doit */
14869 		__release(&rdev->wiphy.mtx);
14870 	}
14871 	if (!(ops->internal_flags & NL80211_FLAG_NEED_RTNL))
14872 		rtnl_unlock();
14873 
14874 	return 0;
14875 }
14876 
14877 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
14878 			      struct genl_info *info)
14879 {
14880 	if (info->user_ptr[1]) {
14881 		if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
14882 			struct wireless_dev *wdev = info->user_ptr[1];
14883 
14884 			if (wdev->netdev)
14885 				dev_put(wdev->netdev);
14886 		} else {
14887 			dev_put(info->user_ptr[1]);
14888 		}
14889 	}
14890 
14891 	if (info->user_ptr[0] &&
14892 	    !(ops->internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
14893 		struct cfg80211_registered_device *rdev = info->user_ptr[0];
14894 
14895 		/* we kept the mutex locked since pre_doit */
14896 		__acquire(&rdev->wiphy.mtx);
14897 		wiphy_unlock(&rdev->wiphy);
14898 	}
14899 
14900 	if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
14901 		rtnl_unlock();
14902 
14903 	/* If needed, clear the netlink message payload from the SKB
14904 	 * as it might contain key data that shouldn't stick around on
14905 	 * the heap after the SKB is freed. The netlink message header
14906 	 * is still needed for further processing, so leave it intact.
14907 	 */
14908 	if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
14909 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
14910 
14911 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
14912 	}
14913 }
14914 
14915 static int nl80211_set_sar_sub_specs(struct cfg80211_registered_device *rdev,
14916 				     struct cfg80211_sar_specs *sar_specs,
14917 				     struct nlattr *spec[], int index)
14918 {
14919 	u32 range_index, i;
14920 
14921 	if (!sar_specs || !spec)
14922 		return -EINVAL;
14923 
14924 	if (!spec[NL80211_SAR_ATTR_SPECS_POWER] ||
14925 	    !spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX])
14926 		return -EINVAL;
14927 
14928 	range_index = nla_get_u32(spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX]);
14929 
14930 	/* check if range_index exceeds num_freq_ranges */
14931 	if (range_index >= rdev->wiphy.sar_capa->num_freq_ranges)
14932 		return -EINVAL;
14933 
14934 	/* check if range_index duplicates */
14935 	for (i = 0; i < index; i++) {
14936 		if (sar_specs->sub_specs[i].freq_range_index == range_index)
14937 			return -EINVAL;
14938 	}
14939 
14940 	sar_specs->sub_specs[index].power =
14941 		nla_get_s32(spec[NL80211_SAR_ATTR_SPECS_POWER]);
14942 
14943 	sar_specs->sub_specs[index].freq_range_index = range_index;
14944 
14945 	return 0;
14946 }
14947 
14948 static int nl80211_set_sar_specs(struct sk_buff *skb, struct genl_info *info)
14949 {
14950 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14951 	struct nlattr *spec[NL80211_SAR_ATTR_SPECS_MAX + 1];
14952 	struct nlattr *tb[NL80211_SAR_ATTR_MAX + 1];
14953 	struct cfg80211_sar_specs *sar_spec;
14954 	enum nl80211_sar_type type;
14955 	struct nlattr *spec_list;
14956 	u32 specs;
14957 	int rem, err;
14958 
14959 	if (!rdev->wiphy.sar_capa || !rdev->ops->set_sar_specs)
14960 		return -EOPNOTSUPP;
14961 
14962 	if (!info->attrs[NL80211_ATTR_SAR_SPEC])
14963 		return -EINVAL;
14964 
14965 	nla_parse_nested(tb, NL80211_SAR_ATTR_MAX,
14966 			 info->attrs[NL80211_ATTR_SAR_SPEC],
14967 			 NULL, NULL);
14968 
14969 	if (!tb[NL80211_SAR_ATTR_TYPE] || !tb[NL80211_SAR_ATTR_SPECS])
14970 		return -EINVAL;
14971 
14972 	type = nla_get_u32(tb[NL80211_SAR_ATTR_TYPE]);
14973 	if (type != rdev->wiphy.sar_capa->type)
14974 		return -EINVAL;
14975 
14976 	specs = 0;
14977 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem)
14978 		specs++;
14979 
14980 	if (specs > rdev->wiphy.sar_capa->num_freq_ranges)
14981 		return -EINVAL;
14982 
14983 	sar_spec = kzalloc(sizeof(*sar_spec) +
14984 			   specs * sizeof(struct cfg80211_sar_sub_specs),
14985 			   GFP_KERNEL);
14986 	if (!sar_spec)
14987 		return -ENOMEM;
14988 
14989 	sar_spec->type = type;
14990 	specs = 0;
14991 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem) {
14992 		nla_parse_nested(spec, NL80211_SAR_ATTR_SPECS_MAX,
14993 				 spec_list, NULL, NULL);
14994 
14995 		switch (type) {
14996 		case NL80211_SAR_TYPE_POWER:
14997 			if (nl80211_set_sar_sub_specs(rdev, sar_spec,
14998 						      spec, specs)) {
14999 				err = -EINVAL;
15000 				goto error;
15001 			}
15002 			break;
15003 		default:
15004 			err = -EINVAL;
15005 			goto error;
15006 		}
15007 		specs++;
15008 	}
15009 
15010 	sar_spec->num_sub_specs = specs;
15011 
15012 	rdev->cur_cmd_info = info;
15013 	err = rdev_set_sar_specs(rdev, sar_spec);
15014 	rdev->cur_cmd_info = NULL;
15015 error:
15016 	kfree(sar_spec);
15017 	return err;
15018 }
15019 
15020 static const struct genl_ops nl80211_ops[] = {
15021 	{
15022 		.cmd = NL80211_CMD_GET_WIPHY,
15023 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15024 		.doit = nl80211_get_wiphy,
15025 		.dumpit = nl80211_dump_wiphy,
15026 		.done = nl80211_dump_wiphy_done,
15027 		/* can be retrieved by unprivileged users */
15028 		.internal_flags = NL80211_FLAG_NEED_WIPHY,
15029 	},
15030 };
15031 
15032 static const struct genl_small_ops nl80211_small_ops[] = {
15033 	{
15034 		.cmd = NL80211_CMD_SET_WIPHY,
15035 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15036 		.doit = nl80211_set_wiphy,
15037 		.flags = GENL_UNS_ADMIN_PERM,
15038 	},
15039 	{
15040 		.cmd = NL80211_CMD_GET_INTERFACE,
15041 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15042 		.doit = nl80211_get_interface,
15043 		.dumpit = nl80211_dump_interface,
15044 		/* can be retrieved by unprivileged users */
15045 		.internal_flags = NL80211_FLAG_NEED_WDEV,
15046 	},
15047 	{
15048 		.cmd = NL80211_CMD_SET_INTERFACE,
15049 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15050 		.doit = nl80211_set_interface,
15051 		.flags = GENL_UNS_ADMIN_PERM,
15052 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15053 				  NL80211_FLAG_NEED_RTNL,
15054 	},
15055 	{
15056 		.cmd = NL80211_CMD_NEW_INTERFACE,
15057 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15058 		.doit = nl80211_new_interface,
15059 		.flags = GENL_UNS_ADMIN_PERM,
15060 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
15061 				  NL80211_FLAG_NEED_RTNL |
15062 				  /* we take the wiphy mutex later ourselves */
15063 				  NL80211_FLAG_NO_WIPHY_MTX,
15064 	},
15065 	{
15066 		.cmd = NL80211_CMD_DEL_INTERFACE,
15067 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15068 		.doit = nl80211_del_interface,
15069 		.flags = GENL_UNS_ADMIN_PERM,
15070 		.internal_flags = NL80211_FLAG_NEED_WDEV |
15071 				  NL80211_FLAG_NEED_RTNL,
15072 	},
15073 	{
15074 		.cmd = NL80211_CMD_GET_KEY,
15075 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15076 		.doit = nl80211_get_key,
15077 		.flags = GENL_UNS_ADMIN_PERM,
15078 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15079 	},
15080 	{
15081 		.cmd = NL80211_CMD_SET_KEY,
15082 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15083 		.doit = nl80211_set_key,
15084 		.flags = GENL_UNS_ADMIN_PERM,
15085 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15086 				  NL80211_FLAG_CLEAR_SKB,
15087 	},
15088 	{
15089 		.cmd = NL80211_CMD_NEW_KEY,
15090 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15091 		.doit = nl80211_new_key,
15092 		.flags = GENL_UNS_ADMIN_PERM,
15093 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15094 				  NL80211_FLAG_CLEAR_SKB,
15095 	},
15096 	{
15097 		.cmd = NL80211_CMD_DEL_KEY,
15098 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15099 		.doit = nl80211_del_key,
15100 		.flags = GENL_UNS_ADMIN_PERM,
15101 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15102 	},
15103 	{
15104 		.cmd = NL80211_CMD_SET_BEACON,
15105 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15106 		.flags = GENL_UNS_ADMIN_PERM,
15107 		.doit = nl80211_set_beacon,
15108 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15109 	},
15110 	{
15111 		.cmd = NL80211_CMD_START_AP,
15112 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15113 		.flags = GENL_UNS_ADMIN_PERM,
15114 		.doit = nl80211_start_ap,
15115 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15116 	},
15117 	{
15118 		.cmd = NL80211_CMD_STOP_AP,
15119 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15120 		.flags = GENL_UNS_ADMIN_PERM,
15121 		.doit = nl80211_stop_ap,
15122 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15123 	},
15124 	{
15125 		.cmd = NL80211_CMD_GET_STATION,
15126 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15127 		.doit = nl80211_get_station,
15128 		.dumpit = nl80211_dump_station,
15129 		.internal_flags = NL80211_FLAG_NEED_NETDEV,
15130 	},
15131 	{
15132 		.cmd = NL80211_CMD_SET_STATION,
15133 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15134 		.doit = nl80211_set_station,
15135 		.flags = GENL_UNS_ADMIN_PERM,
15136 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15137 	},
15138 	{
15139 		.cmd = NL80211_CMD_NEW_STATION,
15140 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15141 		.doit = nl80211_new_station,
15142 		.flags = GENL_UNS_ADMIN_PERM,
15143 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15144 	},
15145 	{
15146 		.cmd = NL80211_CMD_DEL_STATION,
15147 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15148 		.doit = nl80211_del_station,
15149 		.flags = GENL_UNS_ADMIN_PERM,
15150 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15151 	},
15152 	{
15153 		.cmd = NL80211_CMD_GET_MPATH,
15154 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15155 		.doit = nl80211_get_mpath,
15156 		.dumpit = nl80211_dump_mpath,
15157 		.flags = GENL_UNS_ADMIN_PERM,
15158 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15159 	},
15160 	{
15161 		.cmd = NL80211_CMD_GET_MPP,
15162 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15163 		.doit = nl80211_get_mpp,
15164 		.dumpit = nl80211_dump_mpp,
15165 		.flags = GENL_UNS_ADMIN_PERM,
15166 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15167 	},
15168 	{
15169 		.cmd = NL80211_CMD_SET_MPATH,
15170 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15171 		.doit = nl80211_set_mpath,
15172 		.flags = GENL_UNS_ADMIN_PERM,
15173 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15174 	},
15175 	{
15176 		.cmd = NL80211_CMD_NEW_MPATH,
15177 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15178 		.doit = nl80211_new_mpath,
15179 		.flags = GENL_UNS_ADMIN_PERM,
15180 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15181 	},
15182 	{
15183 		.cmd = NL80211_CMD_DEL_MPATH,
15184 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15185 		.doit = nl80211_del_mpath,
15186 		.flags = GENL_UNS_ADMIN_PERM,
15187 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15188 	},
15189 	{
15190 		.cmd = NL80211_CMD_SET_BSS,
15191 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15192 		.doit = nl80211_set_bss,
15193 		.flags = GENL_UNS_ADMIN_PERM,
15194 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15195 	},
15196 	{
15197 		.cmd = NL80211_CMD_GET_REG,
15198 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15199 		.doit = nl80211_get_reg_do,
15200 		.dumpit = nl80211_get_reg_dump,
15201 		.internal_flags = 0,
15202 		/* can be retrieved by unprivileged users */
15203 	},
15204 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
15205 	{
15206 		.cmd = NL80211_CMD_SET_REG,
15207 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15208 		.doit = nl80211_set_reg,
15209 		.flags = GENL_ADMIN_PERM,
15210 		.internal_flags = 0,
15211 	},
15212 #endif
15213 	{
15214 		.cmd = NL80211_CMD_REQ_SET_REG,
15215 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15216 		.doit = nl80211_req_set_reg,
15217 		.flags = GENL_ADMIN_PERM,
15218 	},
15219 	{
15220 		.cmd = NL80211_CMD_RELOAD_REGDB,
15221 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15222 		.doit = nl80211_reload_regdb,
15223 		.flags = GENL_ADMIN_PERM,
15224 	},
15225 	{
15226 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
15227 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15228 		.doit = nl80211_get_mesh_config,
15229 		/* can be retrieved by unprivileged users */
15230 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15231 	},
15232 	{
15233 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
15234 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15235 		.doit = nl80211_update_mesh_config,
15236 		.flags = GENL_UNS_ADMIN_PERM,
15237 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15238 	},
15239 	{
15240 		.cmd = NL80211_CMD_TRIGGER_SCAN,
15241 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15242 		.doit = nl80211_trigger_scan,
15243 		.flags = GENL_UNS_ADMIN_PERM,
15244 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP,
15245 	},
15246 	{
15247 		.cmd = NL80211_CMD_ABORT_SCAN,
15248 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15249 		.doit = nl80211_abort_scan,
15250 		.flags = GENL_UNS_ADMIN_PERM,
15251 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP,
15252 	},
15253 	{
15254 		.cmd = NL80211_CMD_GET_SCAN,
15255 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15256 		.dumpit = nl80211_dump_scan,
15257 	},
15258 	{
15259 		.cmd = NL80211_CMD_START_SCHED_SCAN,
15260 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15261 		.doit = nl80211_start_sched_scan,
15262 		.flags = GENL_UNS_ADMIN_PERM,
15263 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15264 	},
15265 	{
15266 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
15267 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15268 		.doit = nl80211_stop_sched_scan,
15269 		.flags = GENL_UNS_ADMIN_PERM,
15270 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15271 	},
15272 	{
15273 		.cmd = NL80211_CMD_AUTHENTICATE,
15274 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15275 		.doit = nl80211_authenticate,
15276 		.flags = GENL_UNS_ADMIN_PERM,
15277 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15278 				  0 |
15279 				  NL80211_FLAG_CLEAR_SKB,
15280 	},
15281 	{
15282 		.cmd = NL80211_CMD_ASSOCIATE,
15283 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15284 		.doit = nl80211_associate,
15285 		.flags = GENL_UNS_ADMIN_PERM,
15286 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15287 				  0 |
15288 				  NL80211_FLAG_CLEAR_SKB,
15289 	},
15290 	{
15291 		.cmd = NL80211_CMD_DEAUTHENTICATE,
15292 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15293 		.doit = nl80211_deauthenticate,
15294 		.flags = GENL_UNS_ADMIN_PERM,
15295 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15296 	},
15297 	{
15298 		.cmd = NL80211_CMD_DISASSOCIATE,
15299 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15300 		.doit = nl80211_disassociate,
15301 		.flags = GENL_UNS_ADMIN_PERM,
15302 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15303 	},
15304 	{
15305 		.cmd = NL80211_CMD_JOIN_IBSS,
15306 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15307 		.doit = nl80211_join_ibss,
15308 		.flags = GENL_UNS_ADMIN_PERM,
15309 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15310 	},
15311 	{
15312 		.cmd = NL80211_CMD_LEAVE_IBSS,
15313 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15314 		.doit = nl80211_leave_ibss,
15315 		.flags = GENL_UNS_ADMIN_PERM,
15316 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15317 	},
15318 #ifdef CONFIG_NL80211_TESTMODE
15319 	{
15320 		.cmd = NL80211_CMD_TESTMODE,
15321 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15322 		.doit = nl80211_testmode_do,
15323 		.dumpit = nl80211_testmode_dump,
15324 		.flags = GENL_UNS_ADMIN_PERM,
15325 		.internal_flags = NL80211_FLAG_NEED_WIPHY,
15326 	},
15327 #endif
15328 	{
15329 		.cmd = NL80211_CMD_CONNECT,
15330 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15331 		.doit = nl80211_connect,
15332 		.flags = GENL_UNS_ADMIN_PERM,
15333 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15334 				  0 |
15335 				  NL80211_FLAG_CLEAR_SKB,
15336 	},
15337 	{
15338 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
15339 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15340 		.doit = nl80211_update_connect_params,
15341 		.flags = GENL_ADMIN_PERM,
15342 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15343 				  0 |
15344 				  NL80211_FLAG_CLEAR_SKB,
15345 	},
15346 	{
15347 		.cmd = NL80211_CMD_DISCONNECT,
15348 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15349 		.doit = nl80211_disconnect,
15350 		.flags = GENL_UNS_ADMIN_PERM,
15351 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15352 	},
15353 	{
15354 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
15355 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15356 		.doit = nl80211_wiphy_netns,
15357 		.flags = GENL_UNS_ADMIN_PERM,
15358 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
15359 				  NL80211_FLAG_NEED_RTNL |
15360 				  NL80211_FLAG_NO_WIPHY_MTX,
15361 	},
15362 	{
15363 		.cmd = NL80211_CMD_GET_SURVEY,
15364 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15365 		.dumpit = nl80211_dump_survey,
15366 	},
15367 	{
15368 		.cmd = NL80211_CMD_SET_PMKSA,
15369 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15370 		.doit = nl80211_setdel_pmksa,
15371 		.flags = GENL_UNS_ADMIN_PERM,
15372 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15373 				  0 |
15374 				  NL80211_FLAG_CLEAR_SKB,
15375 	},
15376 	{
15377 		.cmd = NL80211_CMD_DEL_PMKSA,
15378 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15379 		.doit = nl80211_setdel_pmksa,
15380 		.flags = GENL_UNS_ADMIN_PERM,
15381 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15382 	},
15383 	{
15384 		.cmd = NL80211_CMD_FLUSH_PMKSA,
15385 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15386 		.doit = nl80211_flush_pmksa,
15387 		.flags = GENL_UNS_ADMIN_PERM,
15388 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15389 	},
15390 	{
15391 		.cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
15392 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15393 		.doit = nl80211_remain_on_channel,
15394 		.flags = GENL_UNS_ADMIN_PERM,
15395 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP,
15396 	},
15397 	{
15398 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
15399 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15400 		.doit = nl80211_cancel_remain_on_channel,
15401 		.flags = GENL_UNS_ADMIN_PERM,
15402 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP,
15403 	},
15404 	{
15405 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
15406 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15407 		.doit = nl80211_set_tx_bitrate_mask,
15408 		.flags = GENL_UNS_ADMIN_PERM,
15409 		.internal_flags = NL80211_FLAG_NEED_NETDEV,
15410 	},
15411 	{
15412 		.cmd = NL80211_CMD_REGISTER_FRAME,
15413 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15414 		.doit = nl80211_register_mgmt,
15415 		.flags = GENL_UNS_ADMIN_PERM,
15416 		.internal_flags = NL80211_FLAG_NEED_WDEV,
15417 	},
15418 	{
15419 		.cmd = NL80211_CMD_FRAME,
15420 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15421 		.doit = nl80211_tx_mgmt,
15422 		.flags = GENL_UNS_ADMIN_PERM,
15423 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP,
15424 	},
15425 	{
15426 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
15427 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15428 		.doit = nl80211_tx_mgmt_cancel_wait,
15429 		.flags = GENL_UNS_ADMIN_PERM,
15430 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP,
15431 	},
15432 	{
15433 		.cmd = NL80211_CMD_SET_POWER_SAVE,
15434 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15435 		.doit = nl80211_set_power_save,
15436 		.flags = GENL_UNS_ADMIN_PERM,
15437 		.internal_flags = NL80211_FLAG_NEED_NETDEV,
15438 	},
15439 	{
15440 		.cmd = NL80211_CMD_GET_POWER_SAVE,
15441 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15442 		.doit = nl80211_get_power_save,
15443 		/* can be retrieved by unprivileged users */
15444 		.internal_flags = NL80211_FLAG_NEED_NETDEV,
15445 	},
15446 	{
15447 		.cmd = NL80211_CMD_SET_CQM,
15448 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15449 		.doit = nl80211_set_cqm,
15450 		.flags = GENL_UNS_ADMIN_PERM,
15451 		.internal_flags = NL80211_FLAG_NEED_NETDEV,
15452 	},
15453 	{
15454 		.cmd = NL80211_CMD_SET_CHANNEL,
15455 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15456 		.doit = nl80211_set_channel,
15457 		.flags = GENL_UNS_ADMIN_PERM,
15458 		.internal_flags = NL80211_FLAG_NEED_NETDEV,
15459 	},
15460 	{
15461 		.cmd = NL80211_CMD_JOIN_MESH,
15462 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15463 		.doit = nl80211_join_mesh,
15464 		.flags = GENL_UNS_ADMIN_PERM,
15465 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15466 	},
15467 	{
15468 		.cmd = NL80211_CMD_LEAVE_MESH,
15469 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15470 		.doit = nl80211_leave_mesh,
15471 		.flags = GENL_UNS_ADMIN_PERM,
15472 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15473 	},
15474 	{
15475 		.cmd = NL80211_CMD_JOIN_OCB,
15476 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15477 		.doit = nl80211_join_ocb,
15478 		.flags = GENL_UNS_ADMIN_PERM,
15479 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15480 	},
15481 	{
15482 		.cmd = NL80211_CMD_LEAVE_OCB,
15483 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15484 		.doit = nl80211_leave_ocb,
15485 		.flags = GENL_UNS_ADMIN_PERM,
15486 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15487 	},
15488 #ifdef CONFIG_PM
15489 	{
15490 		.cmd = NL80211_CMD_GET_WOWLAN,
15491 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15492 		.doit = nl80211_get_wowlan,
15493 		/* can be retrieved by unprivileged users */
15494 		.internal_flags = NL80211_FLAG_NEED_WIPHY,
15495 	},
15496 	{
15497 		.cmd = NL80211_CMD_SET_WOWLAN,
15498 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15499 		.doit = nl80211_set_wowlan,
15500 		.flags = GENL_UNS_ADMIN_PERM,
15501 		.internal_flags = NL80211_FLAG_NEED_WIPHY,
15502 	},
15503 #endif
15504 	{
15505 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
15506 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15507 		.doit = nl80211_set_rekey_data,
15508 		.flags = GENL_UNS_ADMIN_PERM,
15509 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15510 				  0 |
15511 				  NL80211_FLAG_CLEAR_SKB,
15512 	},
15513 	{
15514 		.cmd = NL80211_CMD_TDLS_MGMT,
15515 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15516 		.doit = nl80211_tdls_mgmt,
15517 		.flags = GENL_UNS_ADMIN_PERM,
15518 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15519 	},
15520 	{
15521 		.cmd = NL80211_CMD_TDLS_OPER,
15522 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15523 		.doit = nl80211_tdls_oper,
15524 		.flags = GENL_UNS_ADMIN_PERM,
15525 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15526 	},
15527 	{
15528 		.cmd = NL80211_CMD_UNEXPECTED_FRAME,
15529 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15530 		.doit = nl80211_register_unexpected_frame,
15531 		.flags = GENL_UNS_ADMIN_PERM,
15532 		.internal_flags = NL80211_FLAG_NEED_NETDEV,
15533 	},
15534 	{
15535 		.cmd = NL80211_CMD_PROBE_CLIENT,
15536 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15537 		.doit = nl80211_probe_client,
15538 		.flags = GENL_UNS_ADMIN_PERM,
15539 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15540 	},
15541 	{
15542 		.cmd = NL80211_CMD_REGISTER_BEACONS,
15543 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15544 		.doit = nl80211_register_beacons,
15545 		.flags = GENL_UNS_ADMIN_PERM,
15546 		.internal_flags = NL80211_FLAG_NEED_WIPHY,
15547 	},
15548 	{
15549 		.cmd = NL80211_CMD_SET_NOACK_MAP,
15550 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15551 		.doit = nl80211_set_noack_map,
15552 		.flags = GENL_UNS_ADMIN_PERM,
15553 		.internal_flags = NL80211_FLAG_NEED_NETDEV,
15554 	},
15555 	{
15556 		.cmd = NL80211_CMD_START_P2P_DEVICE,
15557 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15558 		.doit = nl80211_start_p2p_device,
15559 		.flags = GENL_UNS_ADMIN_PERM,
15560 		.internal_flags = NL80211_FLAG_NEED_WDEV |
15561 				  NL80211_FLAG_NEED_RTNL,
15562 	},
15563 	{
15564 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
15565 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15566 		.doit = nl80211_stop_p2p_device,
15567 		.flags = GENL_UNS_ADMIN_PERM,
15568 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15569 				  NL80211_FLAG_NEED_RTNL,
15570 	},
15571 	{
15572 		.cmd = NL80211_CMD_START_NAN,
15573 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15574 		.doit = nl80211_start_nan,
15575 		.flags = GENL_ADMIN_PERM,
15576 		.internal_flags = NL80211_FLAG_NEED_WDEV |
15577 				  NL80211_FLAG_NEED_RTNL,
15578 	},
15579 	{
15580 		.cmd = NL80211_CMD_STOP_NAN,
15581 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15582 		.doit = nl80211_stop_nan,
15583 		.flags = GENL_ADMIN_PERM,
15584 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15585 				  NL80211_FLAG_NEED_RTNL,
15586 	},
15587 	{
15588 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
15589 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15590 		.doit = nl80211_nan_add_func,
15591 		.flags = GENL_ADMIN_PERM,
15592 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP,
15593 	},
15594 	{
15595 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
15596 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15597 		.doit = nl80211_nan_del_func,
15598 		.flags = GENL_ADMIN_PERM,
15599 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP,
15600 	},
15601 	{
15602 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
15603 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15604 		.doit = nl80211_nan_change_config,
15605 		.flags = GENL_ADMIN_PERM,
15606 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP,
15607 	},
15608 	{
15609 		.cmd = NL80211_CMD_SET_MCAST_RATE,
15610 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15611 		.doit = nl80211_set_mcast_rate,
15612 		.flags = GENL_UNS_ADMIN_PERM,
15613 		.internal_flags = NL80211_FLAG_NEED_NETDEV,
15614 	},
15615 	{
15616 		.cmd = NL80211_CMD_SET_MAC_ACL,
15617 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15618 		.doit = nl80211_set_mac_acl,
15619 		.flags = GENL_UNS_ADMIN_PERM,
15620 		.internal_flags = NL80211_FLAG_NEED_NETDEV,
15621 	},
15622 	{
15623 		.cmd = NL80211_CMD_RADAR_DETECT,
15624 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15625 		.doit = nl80211_start_radar_detection,
15626 		.flags = GENL_UNS_ADMIN_PERM,
15627 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15628 	},
15629 	{
15630 		.cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
15631 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15632 		.doit = nl80211_get_protocol_features,
15633 	},
15634 	{
15635 		.cmd = NL80211_CMD_UPDATE_FT_IES,
15636 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15637 		.doit = nl80211_update_ft_ies,
15638 		.flags = GENL_UNS_ADMIN_PERM,
15639 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15640 	},
15641 	{
15642 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
15643 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15644 		.doit = nl80211_crit_protocol_start,
15645 		.flags = GENL_UNS_ADMIN_PERM,
15646 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP,
15647 	},
15648 	{
15649 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
15650 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15651 		.doit = nl80211_crit_protocol_stop,
15652 		.flags = GENL_UNS_ADMIN_PERM,
15653 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP,
15654 	},
15655 	{
15656 		.cmd = NL80211_CMD_GET_COALESCE,
15657 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15658 		.doit = nl80211_get_coalesce,
15659 		.internal_flags = NL80211_FLAG_NEED_WIPHY,
15660 	},
15661 	{
15662 		.cmd = NL80211_CMD_SET_COALESCE,
15663 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15664 		.doit = nl80211_set_coalesce,
15665 		.flags = GENL_UNS_ADMIN_PERM,
15666 		.internal_flags = NL80211_FLAG_NEED_WIPHY,
15667 	},
15668 	{
15669 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
15670 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15671 		.doit = nl80211_channel_switch,
15672 		.flags = GENL_UNS_ADMIN_PERM,
15673 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15674 	},
15675 	{
15676 		.cmd = NL80211_CMD_VENDOR,
15677 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15678 		.doit = nl80211_vendor_cmd,
15679 		.dumpit = nl80211_vendor_cmd_dump,
15680 		.flags = GENL_UNS_ADMIN_PERM,
15681 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
15682 				  0 |
15683 				  NL80211_FLAG_CLEAR_SKB,
15684 	},
15685 	{
15686 		.cmd = NL80211_CMD_SET_QOS_MAP,
15687 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15688 		.doit = nl80211_set_qos_map,
15689 		.flags = GENL_UNS_ADMIN_PERM,
15690 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15691 	},
15692 	{
15693 		.cmd = NL80211_CMD_ADD_TX_TS,
15694 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15695 		.doit = nl80211_add_tx_ts,
15696 		.flags = GENL_UNS_ADMIN_PERM,
15697 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15698 	},
15699 	{
15700 		.cmd = NL80211_CMD_DEL_TX_TS,
15701 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15702 		.doit = nl80211_del_tx_ts,
15703 		.flags = GENL_UNS_ADMIN_PERM,
15704 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15705 	},
15706 	{
15707 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
15708 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15709 		.doit = nl80211_tdls_channel_switch,
15710 		.flags = GENL_UNS_ADMIN_PERM,
15711 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15712 	},
15713 	{
15714 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
15715 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15716 		.doit = nl80211_tdls_cancel_channel_switch,
15717 		.flags = GENL_UNS_ADMIN_PERM,
15718 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15719 	},
15720 	{
15721 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
15722 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15723 		.doit = nl80211_set_multicast_to_unicast,
15724 		.flags = GENL_UNS_ADMIN_PERM,
15725 		.internal_flags = NL80211_FLAG_NEED_NETDEV,
15726 	},
15727 	{
15728 		.cmd = NL80211_CMD_SET_PMK,
15729 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15730 		.doit = nl80211_set_pmk,
15731 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15732 				  0 |
15733 				  NL80211_FLAG_CLEAR_SKB,
15734 	},
15735 	{
15736 		.cmd = NL80211_CMD_DEL_PMK,
15737 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15738 		.doit = nl80211_del_pmk,
15739 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15740 	},
15741 	{
15742 		.cmd = NL80211_CMD_EXTERNAL_AUTH,
15743 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15744 		.doit = nl80211_external_auth,
15745 		.flags = GENL_ADMIN_PERM,
15746 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15747 	},
15748 	{
15749 		.cmd = NL80211_CMD_CONTROL_PORT_FRAME,
15750 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15751 		.doit = nl80211_tx_control_port,
15752 		.flags = GENL_UNS_ADMIN_PERM,
15753 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15754 	},
15755 	{
15756 		.cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
15757 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15758 		.doit = nl80211_get_ftm_responder_stats,
15759 		.internal_flags = NL80211_FLAG_NEED_NETDEV,
15760 	},
15761 	{
15762 		.cmd = NL80211_CMD_PEER_MEASUREMENT_START,
15763 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15764 		.doit = nl80211_pmsr_start,
15765 		.flags = GENL_UNS_ADMIN_PERM,
15766 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP,
15767 	},
15768 	{
15769 		.cmd = NL80211_CMD_NOTIFY_RADAR,
15770 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15771 		.doit = nl80211_notify_radar_detection,
15772 		.flags = GENL_UNS_ADMIN_PERM,
15773 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15774 	},
15775 	{
15776 		.cmd = NL80211_CMD_UPDATE_OWE_INFO,
15777 		.doit = nl80211_update_owe_info,
15778 		.flags = GENL_ADMIN_PERM,
15779 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15780 	},
15781 	{
15782 		.cmd = NL80211_CMD_PROBE_MESH_LINK,
15783 		.doit = nl80211_probe_mesh_link,
15784 		.flags = GENL_UNS_ADMIN_PERM,
15785 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP,
15786 	},
15787 	{
15788 		.cmd = NL80211_CMD_SET_TID_CONFIG,
15789 		.doit = nl80211_set_tid_config,
15790 		.flags = GENL_UNS_ADMIN_PERM,
15791 		.internal_flags = NL80211_FLAG_NEED_NETDEV,
15792 	},
15793 	{
15794 		.cmd = NL80211_CMD_SET_SAR_SPECS,
15795 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15796 		.doit = nl80211_set_sar_specs,
15797 		.flags = GENL_UNS_ADMIN_PERM,
15798 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
15799 				  NL80211_FLAG_NEED_RTNL,
15800 	},
15801 };
15802 
15803 static struct genl_family nl80211_fam __ro_after_init = {
15804 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
15805 	.hdrsize = 0,			/* no private header */
15806 	.version = 1,			/* no particular meaning now */
15807 	.maxattr = NL80211_ATTR_MAX,
15808 	.policy = nl80211_policy,
15809 	.netnsok = true,
15810 	.pre_doit = nl80211_pre_doit,
15811 	.post_doit = nl80211_post_doit,
15812 	.module = THIS_MODULE,
15813 	.ops = nl80211_ops,
15814 	.n_ops = ARRAY_SIZE(nl80211_ops),
15815 	.small_ops = nl80211_small_ops,
15816 	.n_small_ops = ARRAY_SIZE(nl80211_small_ops),
15817 	.mcgrps = nl80211_mcgrps,
15818 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
15819 	.parallel_ops = true,
15820 };
15821 
15822 /* notification functions */
15823 
15824 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
15825 			  enum nl80211_commands cmd)
15826 {
15827 	struct sk_buff *msg;
15828 	struct nl80211_dump_wiphy_state state = {};
15829 
15830 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
15831 		cmd != NL80211_CMD_DEL_WIPHY);
15832 
15833 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15834 	if (!msg)
15835 		return;
15836 
15837 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
15838 		nlmsg_free(msg);
15839 		return;
15840 	}
15841 
15842 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15843 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
15844 }
15845 
15846 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
15847 				struct wireless_dev *wdev,
15848 				enum nl80211_commands cmd)
15849 {
15850 	struct sk_buff *msg;
15851 
15852 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15853 	if (!msg)
15854 		return;
15855 
15856 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
15857 		nlmsg_free(msg);
15858 		return;
15859 	}
15860 
15861 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15862 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
15863 }
15864 
15865 static int nl80211_add_scan_req(struct sk_buff *msg,
15866 				struct cfg80211_registered_device *rdev)
15867 {
15868 	struct cfg80211_scan_request *req = rdev->scan_req;
15869 	struct nlattr *nest;
15870 	int i;
15871 	struct cfg80211_scan_info *info;
15872 
15873 	if (WARN_ON(!req))
15874 		return 0;
15875 
15876 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
15877 	if (!nest)
15878 		goto nla_put_failure;
15879 	for (i = 0; i < req->n_ssids; i++) {
15880 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
15881 			goto nla_put_failure;
15882 	}
15883 	nla_nest_end(msg, nest);
15884 
15885 	if (req->flags & NL80211_SCAN_FLAG_FREQ_KHZ) {
15886 		nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQ_KHZ);
15887 		if (!nest)
15888 			goto nla_put_failure;
15889 		for (i = 0; i < req->n_channels; i++) {
15890 			if (nla_put_u32(msg, i,
15891 				   ieee80211_channel_to_khz(req->channels[i])))
15892 				goto nla_put_failure;
15893 		}
15894 		nla_nest_end(msg, nest);
15895 	} else {
15896 		nest = nla_nest_start_noflag(msg,
15897 					     NL80211_ATTR_SCAN_FREQUENCIES);
15898 		if (!nest)
15899 			goto nla_put_failure;
15900 		for (i = 0; i < req->n_channels; i++) {
15901 			if (nla_put_u32(msg, i, req->channels[i]->center_freq))
15902 				goto nla_put_failure;
15903 		}
15904 		nla_nest_end(msg, nest);
15905 	}
15906 
15907 	if (req->ie &&
15908 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
15909 		goto nla_put_failure;
15910 
15911 	if (req->flags &&
15912 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
15913 		goto nla_put_failure;
15914 
15915 	info = rdev->int_scan_req ? &rdev->int_scan_req->info :
15916 		&rdev->scan_req->info;
15917 	if (info->scan_start_tsf &&
15918 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
15919 			       info->scan_start_tsf, NL80211_BSS_PAD) ||
15920 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
15921 		     info->tsf_bssid)))
15922 		goto nla_put_failure;
15923 
15924 	return 0;
15925  nla_put_failure:
15926 	return -ENOBUFS;
15927 }
15928 
15929 static int nl80211_prep_scan_msg(struct sk_buff *msg,
15930 				 struct cfg80211_registered_device *rdev,
15931 				 struct wireless_dev *wdev,
15932 				 u32 portid, u32 seq, int flags,
15933 				 u32 cmd)
15934 {
15935 	void *hdr;
15936 
15937 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
15938 	if (!hdr)
15939 		return -1;
15940 
15941 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15942 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15943 					 wdev->netdev->ifindex)) ||
15944 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15945 			      NL80211_ATTR_PAD))
15946 		goto nla_put_failure;
15947 
15948 	/* ignore errors and send incomplete event anyway */
15949 	nl80211_add_scan_req(msg, rdev);
15950 
15951 	genlmsg_end(msg, hdr);
15952 	return 0;
15953 
15954  nla_put_failure:
15955 	genlmsg_cancel(msg, hdr);
15956 	return -EMSGSIZE;
15957 }
15958 
15959 static int
15960 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
15961 			    struct cfg80211_sched_scan_request *req, u32 cmd)
15962 {
15963 	void *hdr;
15964 
15965 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15966 	if (!hdr)
15967 		return -1;
15968 
15969 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
15970 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
15971 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
15972 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
15973 			      NL80211_ATTR_PAD))
15974 		goto nla_put_failure;
15975 
15976 	genlmsg_end(msg, hdr);
15977 	return 0;
15978 
15979  nla_put_failure:
15980 	genlmsg_cancel(msg, hdr);
15981 	return -EMSGSIZE;
15982 }
15983 
15984 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
15985 			     struct wireless_dev *wdev)
15986 {
15987 	struct sk_buff *msg;
15988 
15989 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15990 	if (!msg)
15991 		return;
15992 
15993 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
15994 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
15995 		nlmsg_free(msg);
15996 		return;
15997 	}
15998 
15999 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16000 				NL80211_MCGRP_SCAN, GFP_KERNEL);
16001 }
16002 
16003 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
16004 				       struct wireless_dev *wdev, bool aborted)
16005 {
16006 	struct sk_buff *msg;
16007 
16008 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
16009 	if (!msg)
16010 		return NULL;
16011 
16012 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
16013 				  aborted ? NL80211_CMD_SCAN_ABORTED :
16014 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
16015 		nlmsg_free(msg);
16016 		return NULL;
16017 	}
16018 
16019 	return msg;
16020 }
16021 
16022 /* send message created by nl80211_build_scan_msg() */
16023 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
16024 			   struct sk_buff *msg)
16025 {
16026 	if (!msg)
16027 		return;
16028 
16029 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16030 				NL80211_MCGRP_SCAN, GFP_KERNEL);
16031 }
16032 
16033 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
16034 {
16035 	struct sk_buff *msg;
16036 
16037 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
16038 	if (!msg)
16039 		return;
16040 
16041 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
16042 		nlmsg_free(msg);
16043 		return;
16044 	}
16045 
16046 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
16047 				NL80211_MCGRP_SCAN, GFP_KERNEL);
16048 }
16049 
16050 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
16051 					  struct regulatory_request *request)
16052 {
16053 	/* Userspace can always count this one always being set */
16054 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
16055 		goto nla_put_failure;
16056 
16057 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
16058 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
16059 			       NL80211_REGDOM_TYPE_WORLD))
16060 			goto nla_put_failure;
16061 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
16062 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
16063 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
16064 			goto nla_put_failure;
16065 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
16066 		   request->intersect) {
16067 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
16068 			       NL80211_REGDOM_TYPE_INTERSECTION))
16069 			goto nla_put_failure;
16070 	} else {
16071 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
16072 			       NL80211_REGDOM_TYPE_COUNTRY) ||
16073 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
16074 				   request->alpha2))
16075 			goto nla_put_failure;
16076 	}
16077 
16078 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
16079 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
16080 
16081 		if (wiphy &&
16082 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
16083 			goto nla_put_failure;
16084 
16085 		if (wiphy &&
16086 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
16087 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
16088 			goto nla_put_failure;
16089 	}
16090 
16091 	return true;
16092 
16093 nla_put_failure:
16094 	return false;
16095 }
16096 
16097 /*
16098  * This can happen on global regulatory changes or device specific settings
16099  * based on custom regulatory domains.
16100  */
16101 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
16102 				     struct regulatory_request *request)
16103 {
16104 	struct sk_buff *msg;
16105 	void *hdr;
16106 
16107 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
16108 	if (!msg)
16109 		return;
16110 
16111 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
16112 	if (!hdr)
16113 		goto nla_put_failure;
16114 
16115 	if (!nl80211_reg_change_event_fill(msg, request))
16116 		goto nla_put_failure;
16117 
16118 	genlmsg_end(msg, hdr);
16119 
16120 	rcu_read_lock();
16121 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
16122 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
16123 	rcu_read_unlock();
16124 
16125 	return;
16126 
16127 nla_put_failure:
16128 	nlmsg_free(msg);
16129 }
16130 
16131 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
16132 				    struct net_device *netdev,
16133 				    const u8 *buf, size_t len,
16134 				    enum nl80211_commands cmd, gfp_t gfp,
16135 				    int uapsd_queues, const u8 *req_ies,
16136 				    size_t req_ies_len, bool reconnect)
16137 {
16138 	struct sk_buff *msg;
16139 	void *hdr;
16140 
16141 	msg = nlmsg_new(100 + len + req_ies_len, gfp);
16142 	if (!msg)
16143 		return;
16144 
16145 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
16146 	if (!hdr) {
16147 		nlmsg_free(msg);
16148 		return;
16149 	}
16150 
16151 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16152 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16153 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
16154 	    (req_ies &&
16155 	     nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
16156 		goto nla_put_failure;
16157 
16158 	if (reconnect && nla_put_flag(msg, NL80211_ATTR_RECONNECT_REQUESTED))
16159 		goto nla_put_failure;
16160 
16161 	if (uapsd_queues >= 0) {
16162 		struct nlattr *nla_wmm =
16163 			nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
16164 		if (!nla_wmm)
16165 			goto nla_put_failure;
16166 
16167 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
16168 			       uapsd_queues))
16169 			goto nla_put_failure;
16170 
16171 		nla_nest_end(msg, nla_wmm);
16172 	}
16173 
16174 	genlmsg_end(msg, hdr);
16175 
16176 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16177 				NL80211_MCGRP_MLME, gfp);
16178 	return;
16179 
16180  nla_put_failure:
16181 	nlmsg_free(msg);
16182 }
16183 
16184 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
16185 			  struct net_device *netdev, const u8 *buf,
16186 			  size_t len, gfp_t gfp)
16187 {
16188 	nl80211_send_mlme_event(rdev, netdev, buf, len,
16189 				NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0,
16190 				false);
16191 }
16192 
16193 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
16194 			   struct net_device *netdev, const u8 *buf,
16195 			   size_t len, gfp_t gfp, int uapsd_queues,
16196 			   const u8 *req_ies, size_t req_ies_len)
16197 {
16198 	nl80211_send_mlme_event(rdev, netdev, buf, len,
16199 				NL80211_CMD_ASSOCIATE, gfp, uapsd_queues,
16200 				req_ies, req_ies_len, false);
16201 }
16202 
16203 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
16204 			 struct net_device *netdev, const u8 *buf,
16205 			 size_t len, bool reconnect, gfp_t gfp)
16206 {
16207 	nl80211_send_mlme_event(rdev, netdev, buf, len,
16208 				NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0,
16209 				reconnect);
16210 }
16211 
16212 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
16213 			   struct net_device *netdev, const u8 *buf,
16214 			   size_t len, bool reconnect, gfp_t gfp)
16215 {
16216 	nl80211_send_mlme_event(rdev, netdev, buf, len,
16217 				NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0,
16218 				reconnect);
16219 }
16220 
16221 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
16222 				  size_t len)
16223 {
16224 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16225 	struct wiphy *wiphy = wdev->wiphy;
16226 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16227 	const struct ieee80211_mgmt *mgmt = (void *)buf;
16228 	u32 cmd;
16229 
16230 	if (WARN_ON(len < 2))
16231 		return;
16232 
16233 	if (ieee80211_is_deauth(mgmt->frame_control)) {
16234 		cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
16235 	} else if (ieee80211_is_disassoc(mgmt->frame_control)) {
16236 		cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
16237 	} else if (ieee80211_is_beacon(mgmt->frame_control)) {
16238 		if (wdev->unprot_beacon_reported &&
16239 		    elapsed_jiffies_msecs(wdev->unprot_beacon_reported) < 10000)
16240 			return;
16241 		cmd = NL80211_CMD_UNPROT_BEACON;
16242 		wdev->unprot_beacon_reported = jiffies;
16243 	} else {
16244 		return;
16245 	}
16246 
16247 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
16248 	nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
16249 				NULL, 0, false);
16250 }
16251 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
16252 
16253 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
16254 				      struct net_device *netdev, int cmd,
16255 				      const u8 *addr, gfp_t gfp)
16256 {
16257 	struct sk_buff *msg;
16258 	void *hdr;
16259 
16260 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16261 	if (!msg)
16262 		return;
16263 
16264 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
16265 	if (!hdr) {
16266 		nlmsg_free(msg);
16267 		return;
16268 	}
16269 
16270 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16271 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16272 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
16273 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
16274 		goto nla_put_failure;
16275 
16276 	genlmsg_end(msg, hdr);
16277 
16278 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16279 				NL80211_MCGRP_MLME, gfp);
16280 	return;
16281 
16282  nla_put_failure:
16283 	nlmsg_free(msg);
16284 }
16285 
16286 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
16287 			       struct net_device *netdev, const u8 *addr,
16288 			       gfp_t gfp)
16289 {
16290 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
16291 				  addr, gfp);
16292 }
16293 
16294 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
16295 				struct net_device *netdev, const u8 *addr,
16296 				gfp_t gfp)
16297 {
16298 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
16299 				  addr, gfp);
16300 }
16301 
16302 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
16303 				 struct net_device *netdev,
16304 				 struct cfg80211_connect_resp_params *cr,
16305 				 gfp_t gfp)
16306 {
16307 	struct sk_buff *msg;
16308 	void *hdr;
16309 
16310 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
16311 			cr->fils.kek_len + cr->fils.pmk_len +
16312 			(cr->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
16313 	if (!msg)
16314 		return;
16315 
16316 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
16317 	if (!hdr) {
16318 		nlmsg_free(msg);
16319 		return;
16320 	}
16321 
16322 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16323 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16324 	    (cr->bssid &&
16325 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, cr->bssid)) ||
16326 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
16327 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
16328 			cr->status) ||
16329 	    (cr->status < 0 &&
16330 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
16331 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
16332 			  cr->timeout_reason))) ||
16333 	    (cr->req_ie &&
16334 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
16335 	    (cr->resp_ie &&
16336 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
16337 		     cr->resp_ie)) ||
16338 	    (cr->fils.update_erp_next_seq_num &&
16339 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
16340 			 cr->fils.erp_next_seq_num)) ||
16341 	    (cr->status == WLAN_STATUS_SUCCESS &&
16342 	     ((cr->fils.kek &&
16343 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
16344 		       cr->fils.kek)) ||
16345 	      (cr->fils.pmk &&
16346 	       nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
16347 	      (cr->fils.pmkid &&
16348 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
16349 		goto nla_put_failure;
16350 
16351 	genlmsg_end(msg, hdr);
16352 
16353 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16354 				NL80211_MCGRP_MLME, gfp);
16355 	return;
16356 
16357  nla_put_failure:
16358 	nlmsg_free(msg);
16359 }
16360 
16361 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
16362 			 struct net_device *netdev,
16363 			 struct cfg80211_roam_info *info, gfp_t gfp)
16364 {
16365 	struct sk_buff *msg;
16366 	void *hdr;
16367 	const u8 *bssid = info->bss ? info->bss->bssid : info->bssid;
16368 
16369 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
16370 			info->fils.kek_len + info->fils.pmk_len +
16371 			(info->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
16372 	if (!msg)
16373 		return;
16374 
16375 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
16376 	if (!hdr) {
16377 		nlmsg_free(msg);
16378 		return;
16379 	}
16380 
16381 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16382 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16383 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
16384 	    (info->req_ie &&
16385 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
16386 		     info->req_ie)) ||
16387 	    (info->resp_ie &&
16388 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
16389 		     info->resp_ie)) ||
16390 	    (info->fils.update_erp_next_seq_num &&
16391 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
16392 			 info->fils.erp_next_seq_num)) ||
16393 	    (info->fils.kek &&
16394 	     nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
16395 		     info->fils.kek)) ||
16396 	    (info->fils.pmk &&
16397 	     nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
16398 	    (info->fils.pmkid &&
16399 	     nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
16400 		goto nla_put_failure;
16401 
16402 	genlmsg_end(msg, hdr);
16403 
16404 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16405 				NL80211_MCGRP_MLME, gfp);
16406 	return;
16407 
16408  nla_put_failure:
16409 	nlmsg_free(msg);
16410 }
16411 
16412 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
16413 				  struct net_device *netdev, const u8 *bssid)
16414 {
16415 	struct sk_buff *msg;
16416 	void *hdr;
16417 
16418 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
16419 	if (!msg)
16420 		return;
16421 
16422 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
16423 	if (!hdr) {
16424 		nlmsg_free(msg);
16425 		return;
16426 	}
16427 
16428 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16429 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16430 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
16431 		goto nla_put_failure;
16432 
16433 	genlmsg_end(msg, hdr);
16434 
16435 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16436 				NL80211_MCGRP_MLME, GFP_KERNEL);
16437 	return;
16438 
16439  nla_put_failure:
16440 	nlmsg_free(msg);
16441 }
16442 
16443 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
16444 			       struct net_device *netdev, u16 reason,
16445 			       const u8 *ie, size_t ie_len, bool from_ap)
16446 {
16447 	struct sk_buff *msg;
16448 	void *hdr;
16449 
16450 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
16451 	if (!msg)
16452 		return;
16453 
16454 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
16455 	if (!hdr) {
16456 		nlmsg_free(msg);
16457 		return;
16458 	}
16459 
16460 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16461 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16462 	    (reason &&
16463 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
16464 	    (from_ap &&
16465 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
16466 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
16467 		goto nla_put_failure;
16468 
16469 	genlmsg_end(msg, hdr);
16470 
16471 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16472 				NL80211_MCGRP_MLME, GFP_KERNEL);
16473 	return;
16474 
16475  nla_put_failure:
16476 	nlmsg_free(msg);
16477 }
16478 
16479 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
16480 			     struct net_device *netdev, const u8 *bssid,
16481 			     gfp_t gfp)
16482 {
16483 	struct sk_buff *msg;
16484 	void *hdr;
16485 
16486 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16487 	if (!msg)
16488 		return;
16489 
16490 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
16491 	if (!hdr) {
16492 		nlmsg_free(msg);
16493 		return;
16494 	}
16495 
16496 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16497 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16498 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
16499 		goto nla_put_failure;
16500 
16501 	genlmsg_end(msg, hdr);
16502 
16503 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16504 				NL80211_MCGRP_MLME, gfp);
16505 	return;
16506 
16507  nla_put_failure:
16508 	nlmsg_free(msg);
16509 }
16510 
16511 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
16512 					const u8 *ie, u8 ie_len,
16513 					int sig_dbm, gfp_t gfp)
16514 {
16515 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16516 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16517 	struct sk_buff *msg;
16518 	void *hdr;
16519 
16520 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
16521 		return;
16522 
16523 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
16524 
16525 	msg = nlmsg_new(100 + ie_len, gfp);
16526 	if (!msg)
16527 		return;
16528 
16529 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
16530 	if (!hdr) {
16531 		nlmsg_free(msg);
16532 		return;
16533 	}
16534 
16535 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16536 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16537 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
16538 	    (ie_len && ie &&
16539 	     nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
16540 	    (sig_dbm &&
16541 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
16542 		goto nla_put_failure;
16543 
16544 	genlmsg_end(msg, hdr);
16545 
16546 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16547 				NL80211_MCGRP_MLME, gfp);
16548 	return;
16549 
16550  nla_put_failure:
16551 	nlmsg_free(msg);
16552 }
16553 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
16554 
16555 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
16556 				 struct net_device *netdev, const u8 *addr,
16557 				 enum nl80211_key_type key_type, int key_id,
16558 				 const u8 *tsc, gfp_t gfp)
16559 {
16560 	struct sk_buff *msg;
16561 	void *hdr;
16562 
16563 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16564 	if (!msg)
16565 		return;
16566 
16567 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
16568 	if (!hdr) {
16569 		nlmsg_free(msg);
16570 		return;
16571 	}
16572 
16573 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16574 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16575 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
16576 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
16577 	    (key_id != -1 &&
16578 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
16579 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
16580 		goto nla_put_failure;
16581 
16582 	genlmsg_end(msg, hdr);
16583 
16584 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16585 				NL80211_MCGRP_MLME, gfp);
16586 	return;
16587 
16588  nla_put_failure:
16589 	nlmsg_free(msg);
16590 }
16591 
16592 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
16593 				    struct ieee80211_channel *channel_before,
16594 				    struct ieee80211_channel *channel_after)
16595 {
16596 	struct sk_buff *msg;
16597 	void *hdr;
16598 	struct nlattr *nl_freq;
16599 
16600 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
16601 	if (!msg)
16602 		return;
16603 
16604 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
16605 	if (!hdr) {
16606 		nlmsg_free(msg);
16607 		return;
16608 	}
16609 
16610 	/*
16611 	 * Since we are applying the beacon hint to a wiphy we know its
16612 	 * wiphy_idx is valid
16613 	 */
16614 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
16615 		goto nla_put_failure;
16616 
16617 	/* Before */
16618 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
16619 	if (!nl_freq)
16620 		goto nla_put_failure;
16621 
16622 	if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
16623 		goto nla_put_failure;
16624 	nla_nest_end(msg, nl_freq);
16625 
16626 	/* After */
16627 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
16628 	if (!nl_freq)
16629 		goto nla_put_failure;
16630 
16631 	if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
16632 		goto nla_put_failure;
16633 	nla_nest_end(msg, nl_freq);
16634 
16635 	genlmsg_end(msg, hdr);
16636 
16637 	rcu_read_lock();
16638 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
16639 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
16640 	rcu_read_unlock();
16641 
16642 	return;
16643 
16644 nla_put_failure:
16645 	nlmsg_free(msg);
16646 }
16647 
16648 static void nl80211_send_remain_on_chan_event(
16649 	int cmd, struct cfg80211_registered_device *rdev,
16650 	struct wireless_dev *wdev, u64 cookie,
16651 	struct ieee80211_channel *chan,
16652 	unsigned int duration, gfp_t gfp)
16653 {
16654 	struct sk_buff *msg;
16655 	void *hdr;
16656 
16657 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16658 	if (!msg)
16659 		return;
16660 
16661 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
16662 	if (!hdr) {
16663 		nlmsg_free(msg);
16664 		return;
16665 	}
16666 
16667 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16668 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
16669 					 wdev->netdev->ifindex)) ||
16670 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16671 			      NL80211_ATTR_PAD) ||
16672 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
16673 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
16674 			NL80211_CHAN_NO_HT) ||
16675 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
16676 			      NL80211_ATTR_PAD))
16677 		goto nla_put_failure;
16678 
16679 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
16680 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
16681 		goto nla_put_failure;
16682 
16683 	genlmsg_end(msg, hdr);
16684 
16685 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16686 				NL80211_MCGRP_MLME, gfp);
16687 	return;
16688 
16689  nla_put_failure:
16690 	nlmsg_free(msg);
16691 }
16692 
16693 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
16694 			       struct ieee80211_channel *chan,
16695 			       unsigned int duration, gfp_t gfp)
16696 {
16697 	struct wiphy *wiphy = wdev->wiphy;
16698 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16699 
16700 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
16701 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
16702 					  rdev, wdev, cookie, chan,
16703 					  duration, gfp);
16704 }
16705 EXPORT_SYMBOL(cfg80211_ready_on_channel);
16706 
16707 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
16708 					struct ieee80211_channel *chan,
16709 					gfp_t gfp)
16710 {
16711 	struct wiphy *wiphy = wdev->wiphy;
16712 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16713 
16714 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
16715 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
16716 					  rdev, wdev, cookie, chan, 0, gfp);
16717 }
16718 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
16719 
16720 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
16721 					struct ieee80211_channel *chan,
16722 					gfp_t gfp)
16723 {
16724 	struct wiphy *wiphy = wdev->wiphy;
16725 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16726 
16727 	trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
16728 	nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
16729 					  rdev, wdev, cookie, chan, 0, gfp);
16730 }
16731 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
16732 
16733 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
16734 		      struct station_info *sinfo, gfp_t gfp)
16735 {
16736 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
16737 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16738 	struct sk_buff *msg;
16739 
16740 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
16741 
16742 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16743 	if (!msg)
16744 		return;
16745 
16746 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
16747 				 rdev, dev, mac_addr, sinfo) < 0) {
16748 		nlmsg_free(msg);
16749 		return;
16750 	}
16751 
16752 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16753 				NL80211_MCGRP_MLME, gfp);
16754 }
16755 EXPORT_SYMBOL(cfg80211_new_sta);
16756 
16757 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
16758 			    struct station_info *sinfo, gfp_t gfp)
16759 {
16760 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
16761 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16762 	struct sk_buff *msg;
16763 	struct station_info empty_sinfo = {};
16764 
16765 	if (!sinfo)
16766 		sinfo = &empty_sinfo;
16767 
16768 	trace_cfg80211_del_sta(dev, mac_addr);
16769 
16770 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16771 	if (!msg) {
16772 		cfg80211_sinfo_release_content(sinfo);
16773 		return;
16774 	}
16775 
16776 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
16777 				 rdev, dev, mac_addr, sinfo) < 0) {
16778 		nlmsg_free(msg);
16779 		return;
16780 	}
16781 
16782 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16783 				NL80211_MCGRP_MLME, gfp);
16784 }
16785 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
16786 
16787 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
16788 			  enum nl80211_connect_failed_reason reason,
16789 			  gfp_t gfp)
16790 {
16791 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
16792 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16793 	struct sk_buff *msg;
16794 	void *hdr;
16795 
16796 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
16797 	if (!msg)
16798 		return;
16799 
16800 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
16801 	if (!hdr) {
16802 		nlmsg_free(msg);
16803 		return;
16804 	}
16805 
16806 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16807 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
16808 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
16809 		goto nla_put_failure;
16810 
16811 	genlmsg_end(msg, hdr);
16812 
16813 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16814 				NL80211_MCGRP_MLME, gfp);
16815 	return;
16816 
16817  nla_put_failure:
16818 	nlmsg_free(msg);
16819 }
16820 EXPORT_SYMBOL(cfg80211_conn_failed);
16821 
16822 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
16823 				       const u8 *addr, gfp_t gfp)
16824 {
16825 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16826 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16827 	struct sk_buff *msg;
16828 	void *hdr;
16829 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
16830 
16831 	if (!nlportid)
16832 		return false;
16833 
16834 	msg = nlmsg_new(100, gfp);
16835 	if (!msg)
16836 		return true;
16837 
16838 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
16839 	if (!hdr) {
16840 		nlmsg_free(msg);
16841 		return true;
16842 	}
16843 
16844 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16845 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16846 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
16847 		goto nla_put_failure;
16848 
16849 	genlmsg_end(msg, hdr);
16850 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
16851 	return true;
16852 
16853  nla_put_failure:
16854 	nlmsg_free(msg);
16855 	return true;
16856 }
16857 
16858 bool cfg80211_rx_spurious_frame(struct net_device *dev,
16859 				const u8 *addr, gfp_t gfp)
16860 {
16861 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16862 	bool ret;
16863 
16864 	trace_cfg80211_rx_spurious_frame(dev, addr);
16865 
16866 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
16867 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
16868 		trace_cfg80211_return_bool(false);
16869 		return false;
16870 	}
16871 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
16872 					 addr, gfp);
16873 	trace_cfg80211_return_bool(ret);
16874 	return ret;
16875 }
16876 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
16877 
16878 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
16879 					const u8 *addr, gfp_t gfp)
16880 {
16881 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16882 	bool ret;
16883 
16884 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
16885 
16886 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
16887 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
16888 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
16889 		trace_cfg80211_return_bool(false);
16890 		return false;
16891 	}
16892 	ret = __nl80211_unexpected_frame(dev,
16893 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
16894 					 addr, gfp);
16895 	trace_cfg80211_return_bool(ret);
16896 	return ret;
16897 }
16898 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
16899 
16900 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
16901 		      struct wireless_dev *wdev, u32 nlportid,
16902 		      int freq, int sig_dbm,
16903 		      const u8 *buf, size_t len, u32 flags, gfp_t gfp)
16904 {
16905 	struct net_device *netdev = wdev->netdev;
16906 	struct sk_buff *msg;
16907 	void *hdr;
16908 
16909 	msg = nlmsg_new(100 + len, gfp);
16910 	if (!msg)
16911 		return -ENOMEM;
16912 
16913 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
16914 	if (!hdr) {
16915 		nlmsg_free(msg);
16916 		return -ENOMEM;
16917 	}
16918 
16919 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16920 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
16921 					netdev->ifindex)) ||
16922 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16923 			      NL80211_ATTR_PAD) ||
16924 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, KHZ_TO_MHZ(freq)) ||
16925 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET, freq % 1000) ||
16926 	    (sig_dbm &&
16927 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
16928 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
16929 	    (flags &&
16930 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
16931 		goto nla_put_failure;
16932 
16933 	genlmsg_end(msg, hdr);
16934 
16935 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
16936 
16937  nla_put_failure:
16938 	nlmsg_free(msg);
16939 	return -ENOBUFS;
16940 }
16941 
16942 static void nl80211_frame_tx_status(struct wireless_dev *wdev, u64 cookie,
16943 				    const u8 *buf, size_t len, bool ack,
16944 				    gfp_t gfp, enum nl80211_commands command)
16945 {
16946 	struct wiphy *wiphy = wdev->wiphy;
16947 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16948 	struct net_device *netdev = wdev->netdev;
16949 	struct sk_buff *msg;
16950 	void *hdr;
16951 
16952 	if (command == NL80211_CMD_FRAME_TX_STATUS)
16953 		trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
16954 	else
16955 		trace_cfg80211_control_port_tx_status(wdev, cookie, ack);
16956 
16957 	msg = nlmsg_new(100 + len, gfp);
16958 	if (!msg)
16959 		return;
16960 
16961 	hdr = nl80211hdr_put(msg, 0, 0, 0, command);
16962 	if (!hdr) {
16963 		nlmsg_free(msg);
16964 		return;
16965 	}
16966 
16967 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16968 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
16969 				   netdev->ifindex)) ||
16970 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16971 			      NL80211_ATTR_PAD) ||
16972 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
16973 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
16974 			      NL80211_ATTR_PAD) ||
16975 	    (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
16976 		goto nla_put_failure;
16977 
16978 	genlmsg_end(msg, hdr);
16979 
16980 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16981 				NL80211_MCGRP_MLME, gfp);
16982 	return;
16983 
16984 nla_put_failure:
16985 	nlmsg_free(msg);
16986 }
16987 
16988 void cfg80211_control_port_tx_status(struct wireless_dev *wdev, u64 cookie,
16989 				     const u8 *buf, size_t len, bool ack,
16990 				     gfp_t gfp)
16991 {
16992 	nl80211_frame_tx_status(wdev, cookie, buf, len, ack, gfp,
16993 				NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS);
16994 }
16995 EXPORT_SYMBOL(cfg80211_control_port_tx_status);
16996 
16997 void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
16998 			     const u8 *buf, size_t len, bool ack, gfp_t gfp)
16999 {
17000 	nl80211_frame_tx_status(wdev, cookie, buf, len, ack, gfp,
17001 				NL80211_CMD_FRAME_TX_STATUS);
17002 }
17003 EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
17004 
17005 static int __nl80211_rx_control_port(struct net_device *dev,
17006 				     struct sk_buff *skb,
17007 				     bool unencrypted, gfp_t gfp)
17008 {
17009 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17010 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
17011 	struct ethhdr *ehdr = eth_hdr(skb);
17012 	const u8 *addr = ehdr->h_source;
17013 	u16 proto = be16_to_cpu(skb->protocol);
17014 	struct sk_buff *msg;
17015 	void *hdr;
17016 	struct nlattr *frame;
17017 
17018 	u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
17019 
17020 	if (!nlportid)
17021 		return -ENOENT;
17022 
17023 	msg = nlmsg_new(100 + skb->len, gfp);
17024 	if (!msg)
17025 		return -ENOMEM;
17026 
17027 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
17028 	if (!hdr) {
17029 		nlmsg_free(msg);
17030 		return -ENOBUFS;
17031 	}
17032 
17033 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17034 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
17035 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17036 			      NL80211_ATTR_PAD) ||
17037 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
17038 	    nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
17039 	    (unencrypted && nla_put_flag(msg,
17040 					 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
17041 		goto nla_put_failure;
17042 
17043 	frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
17044 	if (!frame)
17045 		goto nla_put_failure;
17046 
17047 	skb_copy_bits(skb, 0, nla_data(frame), skb->len);
17048 	genlmsg_end(msg, hdr);
17049 
17050 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
17051 
17052  nla_put_failure:
17053 	nlmsg_free(msg);
17054 	return -ENOBUFS;
17055 }
17056 
17057 bool cfg80211_rx_control_port(struct net_device *dev,
17058 			      struct sk_buff *skb, bool unencrypted)
17059 {
17060 	int ret;
17061 
17062 	trace_cfg80211_rx_control_port(dev, skb, unencrypted);
17063 	ret = __nl80211_rx_control_port(dev, skb, unencrypted, GFP_ATOMIC);
17064 	trace_cfg80211_return_bool(ret == 0);
17065 	return ret == 0;
17066 }
17067 EXPORT_SYMBOL(cfg80211_rx_control_port);
17068 
17069 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
17070 					    const char *mac, gfp_t gfp)
17071 {
17072 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17073 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
17074 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17075 	void **cb;
17076 
17077 	if (!msg)
17078 		return NULL;
17079 
17080 	cb = (void **)msg->cb;
17081 
17082 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
17083 	if (!cb[0]) {
17084 		nlmsg_free(msg);
17085 		return NULL;
17086 	}
17087 
17088 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17089 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
17090 		goto nla_put_failure;
17091 
17092 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
17093 		goto nla_put_failure;
17094 
17095 	cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
17096 	if (!cb[1])
17097 		goto nla_put_failure;
17098 
17099 	cb[2] = rdev;
17100 
17101 	return msg;
17102  nla_put_failure:
17103 	nlmsg_free(msg);
17104 	return NULL;
17105 }
17106 
17107 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
17108 {
17109 	void **cb = (void **)msg->cb;
17110 	struct cfg80211_registered_device *rdev = cb[2];
17111 
17112 	nla_nest_end(msg, cb[1]);
17113 	genlmsg_end(msg, cb[0]);
17114 
17115 	memset(msg->cb, 0, sizeof(msg->cb));
17116 
17117 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17118 				NL80211_MCGRP_MLME, gfp);
17119 }
17120 
17121 void cfg80211_cqm_rssi_notify(struct net_device *dev,
17122 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
17123 			      s32 rssi_level, gfp_t gfp)
17124 {
17125 	struct sk_buff *msg;
17126 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17127 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
17128 
17129 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
17130 
17131 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
17132 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
17133 		return;
17134 
17135 	if (wdev->cqm_config) {
17136 		wdev->cqm_config->last_rssi_event_value = rssi_level;
17137 
17138 		cfg80211_cqm_rssi_update(rdev, dev);
17139 
17140 		if (rssi_level == 0)
17141 			rssi_level = wdev->cqm_config->last_rssi_event_value;
17142 	}
17143 
17144 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
17145 	if (!msg)
17146 		return;
17147 
17148 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
17149 			rssi_event))
17150 		goto nla_put_failure;
17151 
17152 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
17153 				      rssi_level))
17154 		goto nla_put_failure;
17155 
17156 	cfg80211_send_cqm(msg, gfp);
17157 
17158 	return;
17159 
17160  nla_put_failure:
17161 	nlmsg_free(msg);
17162 }
17163 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
17164 
17165 void cfg80211_cqm_txe_notify(struct net_device *dev,
17166 			     const u8 *peer, u32 num_packets,
17167 			     u32 rate, u32 intvl, gfp_t gfp)
17168 {
17169 	struct sk_buff *msg;
17170 
17171 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
17172 	if (!msg)
17173 		return;
17174 
17175 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
17176 		goto nla_put_failure;
17177 
17178 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
17179 		goto nla_put_failure;
17180 
17181 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
17182 		goto nla_put_failure;
17183 
17184 	cfg80211_send_cqm(msg, gfp);
17185 	return;
17186 
17187  nla_put_failure:
17188 	nlmsg_free(msg);
17189 }
17190 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
17191 
17192 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
17193 				 const u8 *peer, u32 num_packets, gfp_t gfp)
17194 {
17195 	struct sk_buff *msg;
17196 
17197 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
17198 
17199 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
17200 	if (!msg)
17201 		return;
17202 
17203 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
17204 		goto nla_put_failure;
17205 
17206 	cfg80211_send_cqm(msg, gfp);
17207 	return;
17208 
17209  nla_put_failure:
17210 	nlmsg_free(msg);
17211 }
17212 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
17213 
17214 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
17215 {
17216 	struct sk_buff *msg;
17217 
17218 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
17219 	if (!msg)
17220 		return;
17221 
17222 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
17223 		goto nla_put_failure;
17224 
17225 	cfg80211_send_cqm(msg, gfp);
17226 	return;
17227 
17228  nla_put_failure:
17229 	nlmsg_free(msg);
17230 }
17231 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
17232 
17233 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
17234 				     struct net_device *netdev, const u8 *bssid,
17235 				     const u8 *replay_ctr, gfp_t gfp)
17236 {
17237 	struct sk_buff *msg;
17238 	struct nlattr *rekey_attr;
17239 	void *hdr;
17240 
17241 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17242 	if (!msg)
17243 		return;
17244 
17245 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
17246 	if (!hdr) {
17247 		nlmsg_free(msg);
17248 		return;
17249 	}
17250 
17251 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17252 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17253 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
17254 		goto nla_put_failure;
17255 
17256 	rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
17257 	if (!rekey_attr)
17258 		goto nla_put_failure;
17259 
17260 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
17261 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
17262 		goto nla_put_failure;
17263 
17264 	nla_nest_end(msg, rekey_attr);
17265 
17266 	genlmsg_end(msg, hdr);
17267 
17268 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17269 				NL80211_MCGRP_MLME, gfp);
17270 	return;
17271 
17272  nla_put_failure:
17273 	nlmsg_free(msg);
17274 }
17275 
17276 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
17277 			       const u8 *replay_ctr, gfp_t gfp)
17278 {
17279 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17280 	struct wiphy *wiphy = wdev->wiphy;
17281 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17282 
17283 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
17284 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
17285 }
17286 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
17287 
17288 static void
17289 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
17290 			       struct net_device *netdev, int index,
17291 			       const u8 *bssid, bool preauth, gfp_t gfp)
17292 {
17293 	struct sk_buff *msg;
17294 	struct nlattr *attr;
17295 	void *hdr;
17296 
17297 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17298 	if (!msg)
17299 		return;
17300 
17301 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
17302 	if (!hdr) {
17303 		nlmsg_free(msg);
17304 		return;
17305 	}
17306 
17307 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17308 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
17309 		goto nla_put_failure;
17310 
17311 	attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
17312 	if (!attr)
17313 		goto nla_put_failure;
17314 
17315 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
17316 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
17317 	    (preauth &&
17318 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
17319 		goto nla_put_failure;
17320 
17321 	nla_nest_end(msg, attr);
17322 
17323 	genlmsg_end(msg, hdr);
17324 
17325 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17326 				NL80211_MCGRP_MLME, gfp);
17327 	return;
17328 
17329  nla_put_failure:
17330 	nlmsg_free(msg);
17331 }
17332 
17333 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
17334 				     const u8 *bssid, bool preauth, gfp_t gfp)
17335 {
17336 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17337 	struct wiphy *wiphy = wdev->wiphy;
17338 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17339 
17340 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
17341 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
17342 }
17343 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
17344 
17345 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
17346 				     struct net_device *netdev,
17347 				     struct cfg80211_chan_def *chandef,
17348 				     gfp_t gfp,
17349 				     enum nl80211_commands notif,
17350 				     u8 count, bool quiet)
17351 {
17352 	struct sk_buff *msg;
17353 	void *hdr;
17354 
17355 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17356 	if (!msg)
17357 		return;
17358 
17359 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
17360 	if (!hdr) {
17361 		nlmsg_free(msg);
17362 		return;
17363 	}
17364 
17365 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
17366 		goto nla_put_failure;
17367 
17368 	if (nl80211_send_chandef(msg, chandef))
17369 		goto nla_put_failure;
17370 
17371 	if (notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) {
17372 		if (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count))
17373 			goto nla_put_failure;
17374 		if (quiet &&
17375 		    nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX))
17376 			goto nla_put_failure;
17377 	}
17378 
17379 	genlmsg_end(msg, hdr);
17380 
17381 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17382 				NL80211_MCGRP_MLME, gfp);
17383 	return;
17384 
17385  nla_put_failure:
17386 	nlmsg_free(msg);
17387 }
17388 
17389 void cfg80211_ch_switch_notify(struct net_device *dev,
17390 			       struct cfg80211_chan_def *chandef)
17391 {
17392 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17393 	struct wiphy *wiphy = wdev->wiphy;
17394 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17395 
17396 	ASSERT_WDEV_LOCK(wdev);
17397 
17398 	trace_cfg80211_ch_switch_notify(dev, chandef);
17399 
17400 	wdev->chandef = *chandef;
17401 	wdev->preset_chandef = *chandef;
17402 
17403 	if (wdev->iftype == NL80211_IFTYPE_STATION &&
17404 	    !WARN_ON(!wdev->current_bss))
17405 		cfg80211_update_assoc_bss_entry(wdev, chandef->chan);
17406 
17407 	cfg80211_sched_dfs_chan_update(rdev);
17408 
17409 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
17410 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0, false);
17411 }
17412 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
17413 
17414 void cfg80211_ch_switch_started_notify(struct net_device *dev,
17415 				       struct cfg80211_chan_def *chandef,
17416 				       u8 count, bool quiet)
17417 {
17418 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17419 	struct wiphy *wiphy = wdev->wiphy;
17420 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17421 
17422 	trace_cfg80211_ch_switch_started_notify(dev, chandef);
17423 
17424 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
17425 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY,
17426 				 count, quiet);
17427 }
17428 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
17429 
17430 void
17431 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
17432 		     const struct cfg80211_chan_def *chandef,
17433 		     enum nl80211_radar_event event,
17434 		     struct net_device *netdev, gfp_t gfp)
17435 {
17436 	struct sk_buff *msg;
17437 	void *hdr;
17438 
17439 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17440 	if (!msg)
17441 		return;
17442 
17443 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
17444 	if (!hdr) {
17445 		nlmsg_free(msg);
17446 		return;
17447 	}
17448 
17449 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
17450 		goto nla_put_failure;
17451 
17452 	/* NOP and radar events don't need a netdev parameter */
17453 	if (netdev) {
17454 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
17455 
17456 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17457 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17458 				      NL80211_ATTR_PAD))
17459 			goto nla_put_failure;
17460 	}
17461 
17462 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
17463 		goto nla_put_failure;
17464 
17465 	if (nl80211_send_chandef(msg, chandef))
17466 		goto nla_put_failure;
17467 
17468 	genlmsg_end(msg, hdr);
17469 
17470 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17471 				NL80211_MCGRP_MLME, gfp);
17472 	return;
17473 
17474  nla_put_failure:
17475 	nlmsg_free(msg);
17476 }
17477 
17478 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
17479 				       struct sta_opmode_info *sta_opmode,
17480 				       gfp_t gfp)
17481 {
17482 	struct sk_buff *msg;
17483 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17484 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
17485 	void *hdr;
17486 
17487 	if (WARN_ON(!mac))
17488 		return;
17489 
17490 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17491 	if (!msg)
17492 		return;
17493 
17494 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
17495 	if (!hdr) {
17496 		nlmsg_free(msg);
17497 		return;
17498 	}
17499 
17500 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
17501 		goto nla_put_failure;
17502 
17503 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
17504 		goto nla_put_failure;
17505 
17506 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
17507 		goto nla_put_failure;
17508 
17509 	if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
17510 	    nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
17511 		goto nla_put_failure;
17512 
17513 	if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
17514 	    nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
17515 		goto nla_put_failure;
17516 
17517 	if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
17518 	    nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
17519 		goto nla_put_failure;
17520 
17521 	genlmsg_end(msg, hdr);
17522 
17523 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17524 				NL80211_MCGRP_MLME, gfp);
17525 
17526 	return;
17527 
17528 nla_put_failure:
17529 	nlmsg_free(msg);
17530 }
17531 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
17532 
17533 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
17534 			   u64 cookie, bool acked, s32 ack_signal,
17535 			   bool is_valid_ack_signal, gfp_t gfp)
17536 {
17537 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17538 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
17539 	struct sk_buff *msg;
17540 	void *hdr;
17541 
17542 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
17543 
17544 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17545 
17546 	if (!msg)
17547 		return;
17548 
17549 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
17550 	if (!hdr) {
17551 		nlmsg_free(msg);
17552 		return;
17553 	}
17554 
17555 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17556 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
17557 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
17558 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
17559 			      NL80211_ATTR_PAD) ||
17560 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
17561 	    (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
17562 						ack_signal)))
17563 		goto nla_put_failure;
17564 
17565 	genlmsg_end(msg, hdr);
17566 
17567 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17568 				NL80211_MCGRP_MLME, gfp);
17569 	return;
17570 
17571  nla_put_failure:
17572 	nlmsg_free(msg);
17573 }
17574 EXPORT_SYMBOL(cfg80211_probe_status);
17575 
17576 void cfg80211_report_obss_beacon_khz(struct wiphy *wiphy, const u8 *frame,
17577 				     size_t len, int freq, int sig_dbm)
17578 {
17579 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17580 	struct sk_buff *msg;
17581 	void *hdr;
17582 	struct cfg80211_beacon_registration *reg;
17583 
17584 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
17585 
17586 	spin_lock_bh(&rdev->beacon_registrations_lock);
17587 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
17588 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
17589 		if (!msg) {
17590 			spin_unlock_bh(&rdev->beacon_registrations_lock);
17591 			return;
17592 		}
17593 
17594 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
17595 		if (!hdr)
17596 			goto nla_put_failure;
17597 
17598 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17599 		    (freq &&
17600 		     (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
17601 				  KHZ_TO_MHZ(freq)) ||
17602 		      nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
17603 				  freq % 1000))) ||
17604 		    (sig_dbm &&
17605 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
17606 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
17607 			goto nla_put_failure;
17608 
17609 		genlmsg_end(msg, hdr);
17610 
17611 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
17612 	}
17613 	spin_unlock_bh(&rdev->beacon_registrations_lock);
17614 	return;
17615 
17616  nla_put_failure:
17617 	spin_unlock_bh(&rdev->beacon_registrations_lock);
17618 	nlmsg_free(msg);
17619 }
17620 EXPORT_SYMBOL(cfg80211_report_obss_beacon_khz);
17621 
17622 #ifdef CONFIG_PM
17623 static int cfg80211_net_detect_results(struct sk_buff *msg,
17624 				       struct cfg80211_wowlan_wakeup *wakeup)
17625 {
17626 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
17627 	struct nlattr *nl_results, *nl_match, *nl_freqs;
17628 	int i, j;
17629 
17630 	nl_results = nla_nest_start_noflag(msg,
17631 					   NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
17632 	if (!nl_results)
17633 		return -EMSGSIZE;
17634 
17635 	for (i = 0; i < nd->n_matches; i++) {
17636 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
17637 
17638 		nl_match = nla_nest_start_noflag(msg, i);
17639 		if (!nl_match)
17640 			break;
17641 
17642 		/* The SSID attribute is optional in nl80211, but for
17643 		 * simplicity reasons it's always present in the
17644 		 * cfg80211 structure.  If a driver can't pass the
17645 		 * SSID, that needs to be changed.  A zero length SSID
17646 		 * is still a valid SSID (wildcard), so it cannot be
17647 		 * used for this purpose.
17648 		 */
17649 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
17650 			    match->ssid.ssid)) {
17651 			nla_nest_cancel(msg, nl_match);
17652 			goto out;
17653 		}
17654 
17655 		if (match->n_channels) {
17656 			nl_freqs = nla_nest_start_noflag(msg,
17657 							 NL80211_ATTR_SCAN_FREQUENCIES);
17658 			if (!nl_freqs) {
17659 				nla_nest_cancel(msg, nl_match);
17660 				goto out;
17661 			}
17662 
17663 			for (j = 0; j < match->n_channels; j++) {
17664 				if (nla_put_u32(msg, j, match->channels[j])) {
17665 					nla_nest_cancel(msg, nl_freqs);
17666 					nla_nest_cancel(msg, nl_match);
17667 					goto out;
17668 				}
17669 			}
17670 
17671 			nla_nest_end(msg, nl_freqs);
17672 		}
17673 
17674 		nla_nest_end(msg, nl_match);
17675 	}
17676 
17677 out:
17678 	nla_nest_end(msg, nl_results);
17679 	return 0;
17680 }
17681 
17682 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
17683 				   struct cfg80211_wowlan_wakeup *wakeup,
17684 				   gfp_t gfp)
17685 {
17686 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
17687 	struct sk_buff *msg;
17688 	void *hdr;
17689 	int size = 200;
17690 
17691 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
17692 
17693 	if (wakeup)
17694 		size += wakeup->packet_present_len;
17695 
17696 	msg = nlmsg_new(size, gfp);
17697 	if (!msg)
17698 		return;
17699 
17700 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
17701 	if (!hdr)
17702 		goto free_msg;
17703 
17704 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17705 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17706 			      NL80211_ATTR_PAD))
17707 		goto free_msg;
17708 
17709 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
17710 					wdev->netdev->ifindex))
17711 		goto free_msg;
17712 
17713 	if (wakeup) {
17714 		struct nlattr *reasons;
17715 
17716 		reasons = nla_nest_start_noflag(msg,
17717 						NL80211_ATTR_WOWLAN_TRIGGERS);
17718 		if (!reasons)
17719 			goto free_msg;
17720 
17721 		if (wakeup->disconnect &&
17722 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
17723 			goto free_msg;
17724 		if (wakeup->magic_pkt &&
17725 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
17726 			goto free_msg;
17727 		if (wakeup->gtk_rekey_failure &&
17728 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
17729 			goto free_msg;
17730 		if (wakeup->eap_identity_req &&
17731 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
17732 			goto free_msg;
17733 		if (wakeup->four_way_handshake &&
17734 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
17735 			goto free_msg;
17736 		if (wakeup->rfkill_release &&
17737 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
17738 			goto free_msg;
17739 
17740 		if (wakeup->pattern_idx >= 0 &&
17741 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
17742 				wakeup->pattern_idx))
17743 			goto free_msg;
17744 
17745 		if (wakeup->tcp_match &&
17746 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
17747 			goto free_msg;
17748 
17749 		if (wakeup->tcp_connlost &&
17750 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
17751 			goto free_msg;
17752 
17753 		if (wakeup->tcp_nomoretokens &&
17754 		    nla_put_flag(msg,
17755 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
17756 			goto free_msg;
17757 
17758 		if (wakeup->packet) {
17759 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
17760 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
17761 
17762 			if (!wakeup->packet_80211) {
17763 				pkt_attr =
17764 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
17765 				len_attr =
17766 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
17767 			}
17768 
17769 			if (wakeup->packet_len &&
17770 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
17771 				goto free_msg;
17772 
17773 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
17774 				    wakeup->packet))
17775 				goto free_msg;
17776 		}
17777 
17778 		if (wakeup->net_detect &&
17779 		    cfg80211_net_detect_results(msg, wakeup))
17780 				goto free_msg;
17781 
17782 		nla_nest_end(msg, reasons);
17783 	}
17784 
17785 	genlmsg_end(msg, hdr);
17786 
17787 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17788 				NL80211_MCGRP_MLME, gfp);
17789 	return;
17790 
17791  free_msg:
17792 	nlmsg_free(msg);
17793 }
17794 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
17795 #endif
17796 
17797 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
17798 				enum nl80211_tdls_operation oper,
17799 				u16 reason_code, gfp_t gfp)
17800 {
17801 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17802 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
17803 	struct sk_buff *msg;
17804 	void *hdr;
17805 
17806 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
17807 					 reason_code);
17808 
17809 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17810 	if (!msg)
17811 		return;
17812 
17813 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
17814 	if (!hdr) {
17815 		nlmsg_free(msg);
17816 		return;
17817 	}
17818 
17819 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17820 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
17821 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
17822 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
17823 	    (reason_code > 0 &&
17824 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
17825 		goto nla_put_failure;
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 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
17837 
17838 static int nl80211_netlink_notify(struct notifier_block * nb,
17839 				  unsigned long state,
17840 				  void *_notify)
17841 {
17842 	struct netlink_notify *notify = _notify;
17843 	struct cfg80211_registered_device *rdev;
17844 	struct wireless_dev *wdev;
17845 	struct cfg80211_beacon_registration *reg, *tmp;
17846 
17847 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
17848 		return NOTIFY_DONE;
17849 
17850 	rcu_read_lock();
17851 
17852 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
17853 		struct cfg80211_sched_scan_request *sched_scan_req;
17854 
17855 		list_for_each_entry_rcu(sched_scan_req,
17856 					&rdev->sched_scan_req_list,
17857 					list) {
17858 			if (sched_scan_req->owner_nlportid == notify->portid) {
17859 				sched_scan_req->nl_owner_dead = true;
17860 				schedule_work(&rdev->sched_scan_stop_wk);
17861 			}
17862 		}
17863 
17864 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
17865 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
17866 
17867 			if (wdev->owner_nlportid == notify->portid) {
17868 				wdev->nl_owner_dead = true;
17869 				schedule_work(&rdev->destroy_work);
17870 			} else if (wdev->conn_owner_nlportid == notify->portid) {
17871 				schedule_work(&wdev->disconnect_wk);
17872 			}
17873 
17874 			cfg80211_release_pmsr(wdev, notify->portid);
17875 		}
17876 
17877 		spin_lock_bh(&rdev->beacon_registrations_lock);
17878 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
17879 					 list) {
17880 			if (reg->nlportid == notify->portid) {
17881 				list_del(&reg->list);
17882 				kfree(reg);
17883 				break;
17884 			}
17885 		}
17886 		spin_unlock_bh(&rdev->beacon_registrations_lock);
17887 	}
17888 
17889 	rcu_read_unlock();
17890 
17891 	/*
17892 	 * It is possible that the user space process that is controlling the
17893 	 * indoor setting disappeared, so notify the regulatory core.
17894 	 */
17895 	regulatory_netlink_notify(notify->portid);
17896 	return NOTIFY_OK;
17897 }
17898 
17899 static struct notifier_block nl80211_netlink_notifier = {
17900 	.notifier_call = nl80211_netlink_notify,
17901 };
17902 
17903 void cfg80211_ft_event(struct net_device *netdev,
17904 		       struct cfg80211_ft_event_params *ft_event)
17905 {
17906 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
17907 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17908 	struct sk_buff *msg;
17909 	void *hdr;
17910 
17911 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
17912 
17913 	if (!ft_event->target_ap)
17914 		return;
17915 
17916 	msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
17917 			GFP_KERNEL);
17918 	if (!msg)
17919 		return;
17920 
17921 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
17922 	if (!hdr)
17923 		goto out;
17924 
17925 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17926 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17927 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
17928 		goto out;
17929 
17930 	if (ft_event->ies &&
17931 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
17932 		goto out;
17933 	if (ft_event->ric_ies &&
17934 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
17935 		    ft_event->ric_ies))
17936 		goto out;
17937 
17938 	genlmsg_end(msg, hdr);
17939 
17940 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17941 				NL80211_MCGRP_MLME, GFP_KERNEL);
17942 	return;
17943  out:
17944 	nlmsg_free(msg);
17945 }
17946 EXPORT_SYMBOL(cfg80211_ft_event);
17947 
17948 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
17949 {
17950 	struct cfg80211_registered_device *rdev;
17951 	struct sk_buff *msg;
17952 	void *hdr;
17953 	u32 nlportid;
17954 
17955 	rdev = wiphy_to_rdev(wdev->wiphy);
17956 	if (!rdev->crit_proto_nlportid)
17957 		return;
17958 
17959 	nlportid = rdev->crit_proto_nlportid;
17960 	rdev->crit_proto_nlportid = 0;
17961 
17962 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17963 	if (!msg)
17964 		return;
17965 
17966 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
17967 	if (!hdr)
17968 		goto nla_put_failure;
17969 
17970 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17971 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17972 			      NL80211_ATTR_PAD))
17973 		goto nla_put_failure;
17974 
17975 	genlmsg_end(msg, hdr);
17976 
17977 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
17978 	return;
17979 
17980  nla_put_failure:
17981 	nlmsg_free(msg);
17982 }
17983 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
17984 
17985 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
17986 {
17987 	struct wiphy *wiphy = wdev->wiphy;
17988 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17989 	struct sk_buff *msg;
17990 	void *hdr;
17991 
17992 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17993 	if (!msg)
17994 		return;
17995 
17996 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
17997 	if (!hdr)
17998 		goto out;
17999 
18000 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18001 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
18002 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18003 			      NL80211_ATTR_PAD))
18004 		goto out;
18005 
18006 	genlmsg_end(msg, hdr);
18007 
18008 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
18009 				NL80211_MCGRP_MLME, GFP_KERNEL);
18010 	return;
18011  out:
18012 	nlmsg_free(msg);
18013 }
18014 
18015 int cfg80211_external_auth_request(struct net_device *dev,
18016 				   struct cfg80211_external_auth_params *params,
18017 				   gfp_t gfp)
18018 {
18019 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18020 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18021 	struct sk_buff *msg;
18022 	void *hdr;
18023 
18024 	if (!wdev->conn_owner_nlportid)
18025 		return -EINVAL;
18026 
18027 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18028 	if (!msg)
18029 		return -ENOMEM;
18030 
18031 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
18032 	if (!hdr)
18033 		goto nla_put_failure;
18034 
18035 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18036 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18037 	    nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
18038 	    nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
18039 			params->action) ||
18040 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
18041 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
18042 		    params->ssid.ssid))
18043 		goto nla_put_failure;
18044 
18045 	genlmsg_end(msg, hdr);
18046 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
18047 			wdev->conn_owner_nlportid);
18048 	return 0;
18049 
18050  nla_put_failure:
18051 	nlmsg_free(msg);
18052 	return -ENOBUFS;
18053 }
18054 EXPORT_SYMBOL(cfg80211_external_auth_request);
18055 
18056 void cfg80211_update_owe_info_event(struct net_device *netdev,
18057 				    struct cfg80211_update_owe_info *owe_info,
18058 				    gfp_t gfp)
18059 {
18060 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
18061 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18062 	struct sk_buff *msg;
18063 	void *hdr;
18064 
18065 	trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
18066 
18067 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18068 	if (!msg)
18069 		return;
18070 
18071 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
18072 	if (!hdr)
18073 		goto nla_put_failure;
18074 
18075 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18076 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18077 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
18078 		goto nla_put_failure;
18079 
18080 	if (!owe_info->ie_len ||
18081 	    nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
18082 		goto nla_put_failure;
18083 
18084 	genlmsg_end(msg, hdr);
18085 
18086 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18087 				NL80211_MCGRP_MLME, gfp);
18088 	return;
18089 
18090 nla_put_failure:
18091 	genlmsg_cancel(msg, hdr);
18092 	nlmsg_free(msg);
18093 }
18094 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
18095 
18096 /* initialisation/exit functions */
18097 
18098 int __init nl80211_init(void)
18099 {
18100 	int err;
18101 
18102 	err = genl_register_family(&nl80211_fam);
18103 	if (err)
18104 		return err;
18105 
18106 	err = netlink_register_notifier(&nl80211_netlink_notifier);
18107 	if (err)
18108 		goto err_out;
18109 
18110 	return 0;
18111  err_out:
18112 	genl_unregister_family(&nl80211_fam);
18113 	return err;
18114 }
18115 
18116 void nl80211_exit(void)
18117 {
18118 	netlink_unregister_notifier(&nl80211_netlink_notifier);
18119 	genl_unregister_family(&nl80211_fam);
18120 }
18121