xref: /openbmc/linux/net/wireless/nl80211.c (revision c8ed9fc9)
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-2020 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 net *netns, struct nlattr **attrs)
68 {
69 	struct cfg80211_registered_device *rdev;
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;
74 	int wiphy_idx = -1;
75 	int ifidx = -1;
76 
77 	ASSERT_RTNL();
78 
79 	if (!have_ifidx && !have_wdev_id)
80 		return ERR_PTR(-EINVAL);
81 
82 	if (have_ifidx)
83 		ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
84 	if (have_wdev_id) {
85 		wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
86 		wiphy_idx = wdev_id >> 32;
87 	}
88 
89 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
90 		struct wireless_dev *wdev;
91 
92 		if (wiphy_net(&rdev->wiphy) != netns)
93 			continue;
94 
95 		if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
96 			continue;
97 
98 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
99 			if (have_ifidx && wdev->netdev &&
100 			    wdev->netdev->ifindex == ifidx) {
101 				result = wdev;
102 				break;
103 			}
104 			if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
105 				result = wdev;
106 				break;
107 			}
108 		}
109 
110 		if (result)
111 			break;
112 	}
113 
114 	if (result)
115 		return result;
116 	return ERR_PTR(-ENODEV);
117 }
118 
119 static struct cfg80211_registered_device *
120 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
121 {
122 	struct cfg80211_registered_device *rdev = NULL, *tmp;
123 	struct net_device *netdev;
124 
125 	ASSERT_RTNL();
126 
127 	if (!attrs[NL80211_ATTR_WIPHY] &&
128 	    !attrs[NL80211_ATTR_IFINDEX] &&
129 	    !attrs[NL80211_ATTR_WDEV])
130 		return ERR_PTR(-EINVAL);
131 
132 	if (attrs[NL80211_ATTR_WIPHY])
133 		rdev = cfg80211_rdev_by_wiphy_idx(
134 				nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
135 
136 	if (attrs[NL80211_ATTR_WDEV]) {
137 		u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
138 		struct wireless_dev *wdev;
139 		bool found = false;
140 
141 		tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
142 		if (tmp) {
143 			/* make sure wdev exists */
144 			list_for_each_entry(wdev, &tmp->wiphy.wdev_list, list) {
145 				if (wdev->identifier != (u32)wdev_id)
146 					continue;
147 				found = true;
148 				break;
149 			}
150 
151 			if (!found)
152 				tmp = NULL;
153 
154 			if (rdev && tmp != rdev)
155 				return ERR_PTR(-EINVAL);
156 			rdev = tmp;
157 		}
158 	}
159 
160 	if (attrs[NL80211_ATTR_IFINDEX]) {
161 		int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
162 
163 		netdev = __dev_get_by_index(netns, ifindex);
164 		if (netdev) {
165 			if (netdev->ieee80211_ptr)
166 				tmp = wiphy_to_rdev(
167 					netdev->ieee80211_ptr->wiphy);
168 			else
169 				tmp = NULL;
170 
171 			/* not wireless device -- return error */
172 			if (!tmp)
173 				return ERR_PTR(-EINVAL);
174 
175 			/* mismatch -- return error */
176 			if (rdev && tmp != rdev)
177 				return ERR_PTR(-EINVAL);
178 
179 			rdev = tmp;
180 		}
181 	}
182 
183 	if (!rdev)
184 		return ERR_PTR(-ENODEV);
185 
186 	if (netns != wiphy_net(&rdev->wiphy))
187 		return ERR_PTR(-ENODEV);
188 
189 	return rdev;
190 }
191 
192 /*
193  * This function returns a pointer to the driver
194  * that the genl_info item that is passed refers to.
195  *
196  * The result of this can be a PTR_ERR and hence must
197  * be checked with IS_ERR() for errors.
198  */
199 static struct cfg80211_registered_device *
200 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
201 {
202 	return __cfg80211_rdev_from_attrs(netns, info->attrs);
203 }
204 
205 static int validate_beacon_head(const struct nlattr *attr,
206 				struct netlink_ext_ack *extack)
207 {
208 	const u8 *data = nla_data(attr);
209 	unsigned int len = nla_len(attr);
210 	const struct element *elem;
211 	const struct ieee80211_mgmt *mgmt = (void *)data;
212 	unsigned int fixedlen = offsetof(struct ieee80211_mgmt,
213 					 u.beacon.variable);
214 
215 	if (len < fixedlen)
216 		goto err;
217 
218 	if (ieee80211_hdrlen(mgmt->frame_control) !=
219 	    offsetof(struct ieee80211_mgmt, u.beacon))
220 		goto err;
221 
222 	data += fixedlen;
223 	len -= fixedlen;
224 
225 	for_each_element(elem, data, len) {
226 		/* nothing */
227 	}
228 
229 	if (for_each_element_completed(elem, data, len))
230 		return 0;
231 
232 err:
233 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed beacon head");
234 	return -EINVAL;
235 }
236 
237 static int validate_ie_attr(const struct nlattr *attr,
238 			    struct netlink_ext_ack *extack)
239 {
240 	const u8 *data = nla_data(attr);
241 	unsigned int len = nla_len(attr);
242 	const struct element *elem;
243 
244 	for_each_element(elem, data, len) {
245 		/* nothing */
246 	}
247 
248 	if (for_each_element_completed(elem, data, len))
249 		return 0;
250 
251 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed information elements");
252 	return -EINVAL;
253 }
254 
255 /* policy for the attributes */
256 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR];
257 
258 static const struct nla_policy
259 nl80211_ftm_responder_policy[NL80211_FTM_RESP_ATTR_MAX + 1] = {
260 	[NL80211_FTM_RESP_ATTR_ENABLED] = { .type = NLA_FLAG, },
261 	[NL80211_FTM_RESP_ATTR_LCI] = { .type = NLA_BINARY,
262 					.len = U8_MAX },
263 	[NL80211_FTM_RESP_ATTR_CIVICLOC] = { .type = NLA_BINARY,
264 					     .len = U8_MAX },
265 };
266 
267 static const struct nla_policy
268 nl80211_pmsr_ftm_req_attr_policy[NL80211_PMSR_FTM_REQ_ATTR_MAX + 1] = {
269 	[NL80211_PMSR_FTM_REQ_ATTR_ASAP] = { .type = NLA_FLAG },
270 	[NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE] = { .type = NLA_U32 },
271 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP] =
272 		NLA_POLICY_MAX(NLA_U8, 15),
273 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD] = { .type = NLA_U16 },
274 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION] =
275 		NLA_POLICY_MAX(NLA_U8, 15),
276 	[NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST] =
277 		NLA_POLICY_MAX(NLA_U8, 31),
278 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES] = { .type = NLA_U8 },
279 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI] = { .type = NLA_FLAG },
280 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC] = { .type = NLA_FLAG },
281 	[NL80211_PMSR_FTM_REQ_ATTR_TRIGGER_BASED] = { .type = NLA_FLAG },
282 	[NL80211_PMSR_FTM_REQ_ATTR_NON_TRIGGER_BASED] = { .type = NLA_FLAG },
283 };
284 
285 static const struct nla_policy
286 nl80211_pmsr_req_data_policy[NL80211_PMSR_TYPE_MAX + 1] = {
287 	[NL80211_PMSR_TYPE_FTM] =
288 		NLA_POLICY_NESTED(nl80211_pmsr_ftm_req_attr_policy),
289 };
290 
291 static const struct nla_policy
292 nl80211_pmsr_req_attr_policy[NL80211_PMSR_REQ_ATTR_MAX + 1] = {
293 	[NL80211_PMSR_REQ_ATTR_DATA] =
294 		NLA_POLICY_NESTED(nl80211_pmsr_req_data_policy),
295 	[NL80211_PMSR_REQ_ATTR_GET_AP_TSF] = { .type = NLA_FLAG },
296 };
297 
298 static const struct nla_policy
299 nl80211_psmr_peer_attr_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
300 	[NL80211_PMSR_PEER_ATTR_ADDR] = NLA_POLICY_ETH_ADDR,
301 	[NL80211_PMSR_PEER_ATTR_CHAN] = NLA_POLICY_NESTED(nl80211_policy),
302 	[NL80211_PMSR_PEER_ATTR_REQ] =
303 		NLA_POLICY_NESTED(nl80211_pmsr_req_attr_policy),
304 	[NL80211_PMSR_PEER_ATTR_RESP] = { .type = NLA_REJECT },
305 };
306 
307 static const struct nla_policy
308 nl80211_pmsr_attr_policy[NL80211_PMSR_ATTR_MAX + 1] = {
309 	[NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_REJECT },
310 	[NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_REJECT },
311 	[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
312 	[NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
313 	[NL80211_PMSR_ATTR_PEERS] =
314 		NLA_POLICY_NESTED_ARRAY(nl80211_psmr_peer_attr_policy),
315 };
316 
317 static const struct nla_policy
318 he_obss_pd_policy[NL80211_HE_OBSS_PD_ATTR_MAX + 1] = {
319 	[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] =
320 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
321 	[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET] =
322 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
323 };
324 
325 static const struct nla_policy
326 he_bss_color_policy[NL80211_HE_BSS_COLOR_ATTR_MAX + 1] = {
327 	[NL80211_HE_BSS_COLOR_ATTR_COLOR] = NLA_POLICY_RANGE(NLA_U8, 1, 63),
328 	[NL80211_HE_BSS_COLOR_ATTR_DISABLED] = { .type = NLA_FLAG },
329 	[NL80211_HE_BSS_COLOR_ATTR_PARTIAL] = { .type = NLA_FLAG },
330 };
331 
332 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
333 	[NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
334 				    .len = NL80211_MAX_SUPP_RATES },
335 	[NL80211_TXRATE_HT] = { .type = NLA_BINARY,
336 				.len = NL80211_MAX_SUPP_HT_RATES },
337 	[NL80211_TXRATE_VHT] = NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_txrate_vht)),
338 	[NL80211_TXRATE_GI] = { .type = NLA_U8 },
339 };
340 
341 static const struct nla_policy
342 nl80211_tid_config_attr_policy[NL80211_TID_CONFIG_ATTR_MAX + 1] = {
343 	[NL80211_TID_CONFIG_ATTR_VIF_SUPP] = { .type = NLA_U64 },
344 	[NL80211_TID_CONFIG_ATTR_PEER_SUPP] = { .type = NLA_U64 },
345 	[NL80211_TID_CONFIG_ATTR_OVERRIDE] = { .type = NLA_FLAG },
346 	[NL80211_TID_CONFIG_ATTR_TIDS] = NLA_POLICY_RANGE(NLA_U16, 1, 0xff),
347 	[NL80211_TID_CONFIG_ATTR_NOACK] =
348 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
349 	[NL80211_TID_CONFIG_ATTR_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
350 	[NL80211_TID_CONFIG_ATTR_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
351 	[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL] =
352 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
353 	[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL] =
354 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
355 	[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL] =
356 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
357 	[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE] =
358 			NLA_POLICY_MAX(NLA_U8, NL80211_TX_RATE_FIXED),
359 	[NL80211_TID_CONFIG_ATTR_TX_RATE] =
360 			NLA_POLICY_NESTED(nl80211_txattr_policy),
361 };
362 
363 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
364 	[0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
365 	[NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
366 	[NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
367 				      .len = 20-1 },
368 	[NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
369 
370 	[NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
371 	[NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
372 	[NL80211_ATTR_WIPHY_EDMG_CHANNELS] = NLA_POLICY_RANGE(NLA_U8,
373 						NL80211_EDMG_CHANNELS_MIN,
374 						NL80211_EDMG_CHANNELS_MAX),
375 	[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG] = NLA_POLICY_RANGE(NLA_U8,
376 						NL80211_EDMG_BW_CONFIG_MIN,
377 						NL80211_EDMG_BW_CONFIG_MAX),
378 
379 	[NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
380 	[NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
381 	[NL80211_ATTR_CENTER_FREQ1_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
382 	[NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
383 
384 	[NL80211_ATTR_WIPHY_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
385 	[NL80211_ATTR_WIPHY_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
386 	[NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
387 	[NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
388 	[NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
389 	[NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
390 
391 	[NL80211_ATTR_IFTYPE] = NLA_POLICY_MAX(NLA_U32, NL80211_IFTYPE_MAX),
392 	[NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
393 	[NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
394 
395 	[NL80211_ATTR_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
396 	[NL80211_ATTR_PREV_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
397 
398 	[NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
399 	[NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
400 				    .len = WLAN_MAX_KEY_LEN },
401 	[NL80211_ATTR_KEY_IDX] = NLA_POLICY_MAX(NLA_U8, 7),
402 	[NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
403 	[NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
404 	[NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
405 	[NL80211_ATTR_KEY_TYPE] =
406 		NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES),
407 
408 	[NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
409 	[NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
410 	[NL80211_ATTR_BEACON_HEAD] =
411 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_beacon_head,
412 				       IEEE80211_MAX_DATA_LEN),
413 	[NL80211_ATTR_BEACON_TAIL] =
414 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
415 				       IEEE80211_MAX_DATA_LEN),
416 	[NL80211_ATTR_STA_AID] =
417 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
418 	[NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
419 	[NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
420 	[NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
421 					       .len = NL80211_MAX_SUPP_RATES },
422 	[NL80211_ATTR_STA_PLINK_ACTION] =
423 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_ACTIONS - 1),
424 	[NL80211_ATTR_STA_TX_POWER_SETTING] =
425 		NLA_POLICY_RANGE(NLA_U8,
426 				 NL80211_TX_POWER_AUTOMATIC,
427 				 NL80211_TX_POWER_FIXED),
428 	[NL80211_ATTR_STA_TX_POWER] = { .type = NLA_S16 },
429 	[NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
430 	[NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
431 	[NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
432 				   .len = IEEE80211_MAX_MESH_ID_LEN },
433 	[NL80211_ATTR_MPATH_NEXT_HOP] = NLA_POLICY_ETH_ADDR_COMPAT,
434 
435 	[NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
436 	[NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
437 
438 	[NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
439 	[NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
440 	[NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
441 	[NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
442 					   .len = NL80211_MAX_SUPP_RATES },
443 	[NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
444 
445 	[NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
446 	[NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
447 
448 	[NL80211_ATTR_HT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_HT_CAPABILITY_LEN),
449 
450 	[NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
451 	[NL80211_ATTR_IE] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
452 						   validate_ie_attr,
453 						   IEEE80211_MAX_DATA_LEN),
454 	[NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
455 	[NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
456 
457 	[NL80211_ATTR_SSID] = { .type = NLA_BINARY,
458 				.len = IEEE80211_MAX_SSID_LEN },
459 	[NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
460 	[NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
461 	[NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
462 	[NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
463 	[NL80211_ATTR_USE_MFP] = NLA_POLICY_RANGE(NLA_U32,
464 						  NL80211_MFP_NO,
465 						  NL80211_MFP_OPTIONAL),
466 	[NL80211_ATTR_STA_FLAGS2] = {
467 		.len = sizeof(struct nl80211_sta_flag_update),
468 	},
469 	[NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
470 	[NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
471 	[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
472 	[NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
473 	[NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
474 	[NL80211_ATTR_STATUS_CODE] = { .type = NLA_U16 },
475 	[NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
476 	[NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
477 	[NL80211_ATTR_PID] = { .type = NLA_U32 },
478 	[NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
479 	[NL80211_ATTR_PMKID] = NLA_POLICY_EXACT_LEN_WARN(WLAN_PMKID_LEN),
480 	[NL80211_ATTR_DURATION] = { .type = NLA_U32 },
481 	[NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
482 	[NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
483 	[NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
484 				 .len = IEEE80211_MAX_DATA_LEN },
485 	[NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
486 	[NL80211_ATTR_PS_STATE] = NLA_POLICY_RANGE(NLA_U32,
487 						   NL80211_PS_DISABLED,
488 						   NL80211_PS_ENABLED),
489 	[NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
490 	[NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
491 	[NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
492 	[NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
493 	[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
494 	[NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
495 	[NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
496 	[NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
497 	[NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
498 	[NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
499 	[NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
500 	[NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
501 	[NL80211_ATTR_STA_PLINK_STATE] =
502 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_STATES - 1),
503 	[NL80211_ATTR_MEASUREMENT_DURATION] = { .type = NLA_U16 },
504 	[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY] = { .type = NLA_FLAG },
505 	[NL80211_ATTR_MESH_PEER_AID] =
506 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
507 	[NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
508 	[NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
509 	[NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
510 	[NL80211_ATTR_HIDDEN_SSID] =
511 		NLA_POLICY_RANGE(NLA_U32,
512 				 NL80211_HIDDEN_SSID_NOT_IN_USE,
513 				 NL80211_HIDDEN_SSID_ZERO_CONTENTS),
514 	[NL80211_ATTR_IE_PROBE_RESP] =
515 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
516 				       IEEE80211_MAX_DATA_LEN),
517 	[NL80211_ATTR_IE_ASSOC_RESP] =
518 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
519 				       IEEE80211_MAX_DATA_LEN),
520 	[NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
521 	[NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
522 	[NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
523 	[NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
524 	[NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
525 	[NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
526 	[NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
527 	[NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
528 	[NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
529 	[NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
530 	[NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
531 				      .len = IEEE80211_MAX_DATA_LEN },
532 	[NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
533 	[NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
534 	[NL80211_ATTR_HT_CAPABILITY_MASK] = {
535 		.len = NL80211_HT_CAPABILITY_LEN
536 	},
537 	[NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
538 	[NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
539 	[NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
540 	[NL80211_ATTR_WDEV] = { .type = NLA_U64 },
541 	[NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
542 	[NL80211_ATTR_AUTH_DATA] = { .type = NLA_BINARY, },
543 	[NL80211_ATTR_VHT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_VHT_CAPABILITY_LEN),
544 	[NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
545 	[NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
546 	[NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
547 	[NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
548 		NLA_POLICY_RANGE(NLA_U32,
549 				 NL80211_MESH_POWER_UNKNOWN + 1,
550 				 NL80211_MESH_POWER_MAX),
551 	[NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
552 	[NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
553 	[NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
554 	[NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
555 	[NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
556 	[NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
557 	[NL80211_ATTR_VHT_CAPABILITY_MASK] = {
558 		.len = NL80211_VHT_CAPABILITY_LEN,
559 	},
560 	[NL80211_ATTR_MDID] = { .type = NLA_U16 },
561 	[NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
562 				  .len = IEEE80211_MAX_DATA_LEN },
563 	[NL80211_ATTR_CRIT_PROT_ID] = { .type = NLA_U16 },
564 	[NL80211_ATTR_MAX_CRIT_PROT_DURATION] = { .type = NLA_U16 },
565 	[NL80211_ATTR_PEER_AID] =
566 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
567 	[NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
568 	[NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
569 	[NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
570 	[NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
571 	[NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
572 	[NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
573 	[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
574 	[NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
575 	[NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
576 	[NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
577 	[NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
578 	[NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
579 	[NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
580 				   .len = IEEE80211_QOS_MAP_LEN_MAX },
581 	[NL80211_ATTR_MAC_HINT] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
582 	[NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
583 	[NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
584 	[NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
585 	[NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
586 	[NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
587 	[NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
588 	[NL80211_ATTR_USER_PRIO] =
589 		NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
590 	[NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
591 	[NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
592 	[NL80211_ATTR_OPER_CLASS] = { .type = NLA_U8 },
593 	[NL80211_ATTR_MAC_MASK] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
594 	[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
595 	[NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
596 	[NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
597 	[NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
598 	[NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
599 	[NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
600 	[NL80211_ATTR_STA_SUPPORT_P2P_PS] =
601 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
602 	[NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
603 		.len = VHT_MUMIMO_GROUPS_DATA_LEN
604 	},
605 	[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
606 	[NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
607 	[NL80211_ATTR_BANDS] = { .type = NLA_U32 },
608 	[NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
609 	[NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
610 				    .len = FILS_MAX_KEK_LEN },
611 	[NL80211_ATTR_FILS_NONCES] = NLA_POLICY_EXACT_LEN_WARN(2 * FILS_NONCE_LEN),
612 	[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
613 	[NL80211_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
614 	[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
615 	[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
616 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
617 	},
618 	[NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
619 	[NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
620 					     .len = FILS_ERP_MAX_USERNAME_LEN },
621 	[NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
622 					  .len = FILS_ERP_MAX_REALM_LEN },
623 	[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
624 	[NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
625 					.len = FILS_ERP_MAX_RRK_LEN },
626 	[NL80211_ATTR_FILS_CACHE_ID] = NLA_POLICY_EXACT_LEN_WARN(2),
627 	[NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
628 	[NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
629 	[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
630 
631 	[NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
632 	[NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
633 	[NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
634 	[NL80211_ATTR_HE_CAPABILITY] = { .type = NLA_BINARY,
635 					 .len = NL80211_HE_MAX_CAPABILITY_LEN },
636 
637 	[NL80211_ATTR_FTM_RESPONDER] =
638 		NLA_POLICY_NESTED(nl80211_ftm_responder_policy),
639 	[NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
640 	[NL80211_ATTR_PEER_MEASUREMENTS] =
641 		NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
642 	[NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
643 	[NL80211_ATTR_SAE_PASSWORD] = { .type = NLA_BINARY,
644 					.len = SAE_PASSWORD_MAX_LEN },
645 	[NL80211_ATTR_TWT_RESPONDER] = { .type = NLA_FLAG },
646 	[NL80211_ATTR_HE_OBSS_PD] = NLA_POLICY_NESTED(he_obss_pd_policy),
647 	[NL80211_ATTR_VLAN_ID] = NLA_POLICY_RANGE(NLA_U16, 1, VLAN_N_VID - 2),
648 	[NL80211_ATTR_HE_BSS_COLOR] = NLA_POLICY_NESTED(he_bss_color_policy),
649 	[NL80211_ATTR_TID_CONFIG] =
650 		NLA_POLICY_NESTED_ARRAY(nl80211_tid_config_attr_policy),
651 	[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH] = { .type = NLA_FLAG },
652 	[NL80211_ATTR_PMK_LIFETIME] = NLA_POLICY_MIN(NLA_U32, 1),
653 	[NL80211_ATTR_PMK_REAUTH_THRESHOLD] = NLA_POLICY_RANGE(NLA_U8, 1, 100),
654 	[NL80211_ATTR_RECEIVE_MULTICAST] = { .type = NLA_FLAG },
655 	[NL80211_ATTR_WIPHY_FREQ_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
656 	[NL80211_ATTR_SCAN_FREQ_KHZ] = { .type = NLA_NESTED },
657 	[NL80211_ATTR_HE_6GHZ_CAPABILITY] = {
658 		.type = NLA_EXACT_LEN,
659 		.len = sizeof(struct ieee80211_he_6ghz_capa),
660 	},
661 };
662 
663 /* policy for the key attributes */
664 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
665 	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
666 	[NL80211_KEY_IDX] = { .type = NLA_U8 },
667 	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
668 	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
669 	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
670 	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
671 	[NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
672 	[NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
673 	[NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
674 };
675 
676 /* policy for the key default flags */
677 static const struct nla_policy
678 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
679 	[NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
680 	[NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
681 };
682 
683 #ifdef CONFIG_PM
684 /* policy for WoWLAN attributes */
685 static const struct nla_policy
686 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
687 	[NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
688 	[NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
689 	[NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
690 	[NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
691 	[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
692 	[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
693 	[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
694 	[NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
695 	[NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
696 	[NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
697 };
698 
699 static const struct nla_policy
700 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
701 	[NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
702 	[NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
703 	[NL80211_WOWLAN_TCP_DST_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
704 	[NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
705 	[NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
706 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .type = NLA_MIN_LEN, .len = 1 },
707 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
708 		.len = sizeof(struct nl80211_wowlan_tcp_data_seq)
709 	},
710 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
711 		.len = sizeof(struct nl80211_wowlan_tcp_data_token)
712 	},
713 	[NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
714 	[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .type = NLA_MIN_LEN, .len = 1 },
715 	[NL80211_WOWLAN_TCP_WAKE_MASK] = { .type = NLA_MIN_LEN, .len = 1 },
716 };
717 #endif /* CONFIG_PM */
718 
719 /* policy for coalesce rule attributes */
720 static const struct nla_policy
721 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
722 	[NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
723 	[NL80211_ATTR_COALESCE_RULE_CONDITION] =
724 		NLA_POLICY_RANGE(NLA_U32,
725 				 NL80211_COALESCE_CONDITION_MATCH,
726 				 NL80211_COALESCE_CONDITION_NO_MATCH),
727 	[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
728 };
729 
730 /* policy for GTK rekey offload attributes */
731 static const struct nla_policy
732 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
733 	[NL80211_REKEY_DATA_KEK] = {
734 		.type = NLA_BINARY,
735 		.len = NL80211_KEK_EXT_LEN
736 	},
737 	[NL80211_REKEY_DATA_KCK] = {
738 		.type = NLA_BINARY,
739 		.len = NL80211_KCK_EXT_LEN
740 	},
741 	[NL80211_REKEY_DATA_REPLAY_CTR] = NLA_POLICY_EXACT_LEN_WARN(NL80211_REPLAY_CTR_LEN),
742 	[NL80211_REKEY_DATA_AKM] = { .type = NLA_U32 },
743 };
744 
745 static const struct nla_policy
746 nl80211_match_band_rssi_policy[NUM_NL80211_BANDS] = {
747 	[NL80211_BAND_2GHZ] = { .type = NLA_S32 },
748 	[NL80211_BAND_5GHZ] = { .type = NLA_S32 },
749 	[NL80211_BAND_6GHZ] = { .type = NLA_S32 },
750 	[NL80211_BAND_60GHZ] = { .type = NLA_S32 },
751 };
752 
753 static const struct nla_policy
754 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
755 	[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
756 						 .len = IEEE80211_MAX_SSID_LEN },
757 	[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
758 	[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
759 	[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI] =
760 		NLA_POLICY_NESTED(nl80211_match_band_rssi_policy),
761 };
762 
763 static const struct nla_policy
764 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
765 	[NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
766 	[NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
767 };
768 
769 static const struct nla_policy
770 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
771 	[NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
772 	[NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
773 	[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
774 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
775 	},
776 };
777 
778 /* policy for NAN function attributes */
779 static const struct nla_policy
780 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
781 	[NL80211_NAN_FUNC_TYPE] = { .type = NLA_U8 },
782 	[NL80211_NAN_FUNC_SERVICE_ID] = {
783 				    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
784 	[NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
785 	[NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
786 	[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
787 	[NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
788 	[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
789 	[NL80211_NAN_FUNC_FOLLOW_UP_DEST] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
790 	[NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
791 	[NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
792 	[NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
793 			.len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
794 	[NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
795 	[NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
796 	[NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
797 	[NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
798 	[NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
799 };
800 
801 /* policy for Service Response Filter attributes */
802 static const struct nla_policy
803 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
804 	[NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
805 	[NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
806 				 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
807 	[NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
808 	[NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
809 };
810 
811 /* policy for packet pattern attributes */
812 static const struct nla_policy
813 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
814 	[NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
815 	[NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
816 	[NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
817 };
818 
819 int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
820 			      struct cfg80211_registered_device **rdev,
821 			      struct wireless_dev **wdev)
822 {
823 	int err;
824 
825 	if (!cb->args[0]) {
826 		struct nlattr **attrbuf;
827 
828 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
829 				  GFP_KERNEL);
830 		if (!attrbuf)
831 			return -ENOMEM;
832 
833 		err = nlmsg_parse_deprecated(cb->nlh,
834 					     GENL_HDRLEN + nl80211_fam.hdrsize,
835 					     attrbuf, nl80211_fam.maxattr,
836 					     nl80211_policy, NULL);
837 		if (err) {
838 			kfree(attrbuf);
839 			return err;
840 		}
841 
842 		*wdev = __cfg80211_wdev_from_attrs(sock_net(cb->skb->sk),
843 						   attrbuf);
844 		kfree(attrbuf);
845 		if (IS_ERR(*wdev))
846 			return PTR_ERR(*wdev);
847 		*rdev = wiphy_to_rdev((*wdev)->wiphy);
848 		/* 0 is the first index - add 1 to parse only once */
849 		cb->args[0] = (*rdev)->wiphy_idx + 1;
850 		cb->args[1] = (*wdev)->identifier;
851 	} else {
852 		/* subtract the 1 again here */
853 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
854 		struct wireless_dev *tmp;
855 
856 		if (!wiphy)
857 			return -ENODEV;
858 		*rdev = wiphy_to_rdev(wiphy);
859 		*wdev = NULL;
860 
861 		list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
862 			if (tmp->identifier == cb->args[1]) {
863 				*wdev = tmp;
864 				break;
865 			}
866 		}
867 
868 		if (!*wdev)
869 			return -ENODEV;
870 	}
871 
872 	return 0;
873 }
874 
875 /* message building helper */
876 void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
877 		     int flags, u8 cmd)
878 {
879 	/* since there is no private header just add the generic one */
880 	return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
881 }
882 
883 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
884 				     const struct ieee80211_reg_rule *rule)
885 {
886 	int j;
887 	struct nlattr *nl_wmm_rules =
888 		nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
889 
890 	if (!nl_wmm_rules)
891 		goto nla_put_failure;
892 
893 	for (j = 0; j < IEEE80211_NUM_ACS; j++) {
894 		struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
895 
896 		if (!nl_wmm_rule)
897 			goto nla_put_failure;
898 
899 		if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
900 				rule->wmm_rule.client[j].cw_min) ||
901 		    nla_put_u16(msg, NL80211_WMMR_CW_MAX,
902 				rule->wmm_rule.client[j].cw_max) ||
903 		    nla_put_u8(msg, NL80211_WMMR_AIFSN,
904 			       rule->wmm_rule.client[j].aifsn) ||
905 		    nla_put_u16(msg, NL80211_WMMR_TXOP,
906 			        rule->wmm_rule.client[j].cot))
907 			goto nla_put_failure;
908 
909 		nla_nest_end(msg, nl_wmm_rule);
910 	}
911 	nla_nest_end(msg, nl_wmm_rules);
912 
913 	return 0;
914 
915 nla_put_failure:
916 	return -ENOBUFS;
917 }
918 
919 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
920 				   struct ieee80211_channel *chan,
921 				   bool large)
922 {
923 	/* Some channels must be completely excluded from the
924 	 * list to protect old user-space tools from breaking
925 	 */
926 	if (!large && chan->flags &
927 	    (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
928 		return 0;
929 
930 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
931 			chan->center_freq))
932 		goto nla_put_failure;
933 
934 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_OFFSET, chan->freq_offset))
935 		goto nla_put_failure;
936 
937 	if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
938 	    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
939 		goto nla_put_failure;
940 	if (chan->flags & IEEE80211_CHAN_NO_IR) {
941 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
942 			goto nla_put_failure;
943 		if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
944 			goto nla_put_failure;
945 	}
946 	if (chan->flags & IEEE80211_CHAN_RADAR) {
947 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
948 			goto nla_put_failure;
949 		if (large) {
950 			u32 time;
951 
952 			time = elapsed_jiffies_msecs(chan->dfs_state_entered);
953 
954 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
955 					chan->dfs_state))
956 				goto nla_put_failure;
957 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
958 					time))
959 				goto nla_put_failure;
960 			if (nla_put_u32(msg,
961 					NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
962 					chan->dfs_cac_ms))
963 				goto nla_put_failure;
964 		}
965 	}
966 
967 	if (large) {
968 		if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
969 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
970 			goto nla_put_failure;
971 		if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
972 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
973 			goto nla_put_failure;
974 		if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
975 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
976 			goto nla_put_failure;
977 		if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
978 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
979 			goto nla_put_failure;
980 		if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
981 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
982 			goto nla_put_failure;
983 		if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
984 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
985 			goto nla_put_failure;
986 		if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
987 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
988 			goto nla_put_failure;
989 		if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
990 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
991 			goto nla_put_failure;
992 		if ((chan->flags & IEEE80211_CHAN_NO_HE) &&
993 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HE))
994 			goto nla_put_failure;
995 	}
996 
997 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
998 			DBM_TO_MBM(chan->max_power)))
999 		goto nla_put_failure;
1000 
1001 	if (large) {
1002 		const struct ieee80211_reg_rule *rule =
1003 			freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1004 
1005 		if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
1006 			if (nl80211_msg_put_wmm_rules(msg, rule))
1007 				goto nla_put_failure;
1008 		}
1009 	}
1010 
1011 	return 0;
1012 
1013  nla_put_failure:
1014 	return -ENOBUFS;
1015 }
1016 
1017 static bool nl80211_put_txq_stats(struct sk_buff *msg,
1018 				  struct cfg80211_txq_stats *txqstats,
1019 				  int attrtype)
1020 {
1021 	struct nlattr *txqattr;
1022 
1023 #define PUT_TXQVAL_U32(attr, memb) do {					  \
1024 	if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&	  \
1025 	    nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
1026 		return false;						  \
1027 	} while (0)
1028 
1029 	txqattr = nla_nest_start_noflag(msg, attrtype);
1030 	if (!txqattr)
1031 		return false;
1032 
1033 	PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
1034 	PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
1035 	PUT_TXQVAL_U32(FLOWS, flows);
1036 	PUT_TXQVAL_U32(DROPS, drops);
1037 	PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
1038 	PUT_TXQVAL_U32(OVERLIMIT, overlimit);
1039 	PUT_TXQVAL_U32(OVERMEMORY, overmemory);
1040 	PUT_TXQVAL_U32(COLLISIONS, collisions);
1041 	PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
1042 	PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
1043 	PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
1044 	nla_nest_end(msg, txqattr);
1045 
1046 #undef PUT_TXQVAL_U32
1047 	return true;
1048 }
1049 
1050 /* netlink command implementations */
1051 
1052 struct key_parse {
1053 	struct key_params p;
1054 	int idx;
1055 	int type;
1056 	bool def, defmgmt, defbeacon;
1057 	bool def_uni, def_multi;
1058 };
1059 
1060 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
1061 				 struct key_parse *k)
1062 {
1063 	struct nlattr *tb[NL80211_KEY_MAX + 1];
1064 	int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
1065 					      nl80211_key_policy,
1066 					      info->extack);
1067 	if (err)
1068 		return err;
1069 
1070 	k->def = !!tb[NL80211_KEY_DEFAULT];
1071 	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1072 	k->defbeacon = !!tb[NL80211_KEY_DEFAULT_BEACON];
1073 
1074 	if (k->def) {
1075 		k->def_uni = true;
1076 		k->def_multi = true;
1077 	}
1078 	if (k->defmgmt || k->defbeacon)
1079 		k->def_multi = true;
1080 
1081 	if (tb[NL80211_KEY_IDX])
1082 		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1083 
1084 	if (tb[NL80211_KEY_DATA]) {
1085 		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1086 		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1087 	}
1088 
1089 	if (tb[NL80211_KEY_SEQ]) {
1090 		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1091 		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1092 	}
1093 
1094 	if (tb[NL80211_KEY_CIPHER])
1095 		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1096 
1097 	if (tb[NL80211_KEY_TYPE])
1098 		k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1099 
1100 	if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1101 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1102 
1103 		err = nla_parse_nested_deprecated(kdt,
1104 						  NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1105 						  tb[NL80211_KEY_DEFAULT_TYPES],
1106 						  nl80211_key_default_policy,
1107 						  info->extack);
1108 		if (err)
1109 			return err;
1110 
1111 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1112 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1113 	}
1114 
1115 	if (tb[NL80211_KEY_MODE])
1116 		k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1117 
1118 	return 0;
1119 }
1120 
1121 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1122 {
1123 	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1124 		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1125 		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1126 	}
1127 
1128 	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1129 		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1130 		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1131 	}
1132 
1133 	if (info->attrs[NL80211_ATTR_KEY_IDX])
1134 		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1135 
1136 	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1137 		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1138 
1139 	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1140 	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1141 
1142 	if (k->def) {
1143 		k->def_uni = true;
1144 		k->def_multi = true;
1145 	}
1146 	if (k->defmgmt)
1147 		k->def_multi = true;
1148 
1149 	if (info->attrs[NL80211_ATTR_KEY_TYPE])
1150 		k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1151 
1152 	if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1153 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1154 		int err = nla_parse_nested_deprecated(kdt,
1155 						      NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1156 						      info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1157 						      nl80211_key_default_policy,
1158 						      info->extack);
1159 		if (err)
1160 			return err;
1161 
1162 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1163 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1164 	}
1165 
1166 	return 0;
1167 }
1168 
1169 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1170 {
1171 	int err;
1172 
1173 	memset(k, 0, sizeof(*k));
1174 	k->idx = -1;
1175 	k->type = -1;
1176 
1177 	if (info->attrs[NL80211_ATTR_KEY])
1178 		err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1179 	else
1180 		err = nl80211_parse_key_old(info, k);
1181 
1182 	if (err)
1183 		return err;
1184 
1185 	if ((k->def ? 1 : 0) + (k->defmgmt ? 1 : 0) +
1186 	    (k->defbeacon ? 1 : 0) > 1) {
1187 		GENL_SET_ERR_MSG(info,
1188 				 "key with multiple default flags is invalid");
1189 		return -EINVAL;
1190 	}
1191 
1192 	if (k->defmgmt || k->defbeacon) {
1193 		if (k->def_uni || !k->def_multi) {
1194 			GENL_SET_ERR_MSG(info,
1195 					 "defmgmt/defbeacon key must be mcast");
1196 			return -EINVAL;
1197 		}
1198 	}
1199 
1200 	if (k->idx != -1) {
1201 		if (k->defmgmt) {
1202 			if (k->idx < 4 || k->idx > 5) {
1203 				GENL_SET_ERR_MSG(info,
1204 						 "defmgmt key idx not 4 or 5");
1205 				return -EINVAL;
1206 			}
1207 		} else if (k->defbeacon) {
1208 			if (k->idx < 6 || k->idx > 7) {
1209 				GENL_SET_ERR_MSG(info,
1210 						 "defbeacon key idx not 6 or 7");
1211 				return -EINVAL;
1212 			}
1213 		} else if (k->def) {
1214 			if (k->idx < 0 || k->idx > 3) {
1215 				GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1216 				return -EINVAL;
1217 			}
1218 		} else {
1219 			if (k->idx < 0 || k->idx > 7) {
1220 				GENL_SET_ERR_MSG(info, "key idx not 0-7");
1221 				return -EINVAL;
1222 			}
1223 		}
1224 	}
1225 
1226 	return 0;
1227 }
1228 
1229 static struct cfg80211_cached_keys *
1230 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1231 		       struct genl_info *info, bool *no_ht)
1232 {
1233 	struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1234 	struct key_parse parse;
1235 	struct nlattr *key;
1236 	struct cfg80211_cached_keys *result;
1237 	int rem, err, def = 0;
1238 	bool have_key = false;
1239 
1240 	nla_for_each_nested(key, keys, rem) {
1241 		have_key = true;
1242 		break;
1243 	}
1244 
1245 	if (!have_key)
1246 		return NULL;
1247 
1248 	result = kzalloc(sizeof(*result), GFP_KERNEL);
1249 	if (!result)
1250 		return ERR_PTR(-ENOMEM);
1251 
1252 	result->def = -1;
1253 
1254 	nla_for_each_nested(key, keys, rem) {
1255 		memset(&parse, 0, sizeof(parse));
1256 		parse.idx = -1;
1257 
1258 		err = nl80211_parse_key_new(info, key, &parse);
1259 		if (err)
1260 			goto error;
1261 		err = -EINVAL;
1262 		if (!parse.p.key)
1263 			goto error;
1264 		if (parse.idx < 0 || parse.idx > 3) {
1265 			GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1266 			goto error;
1267 		}
1268 		if (parse.def) {
1269 			if (def) {
1270 				GENL_SET_ERR_MSG(info,
1271 						 "only one key can be default");
1272 				goto error;
1273 			}
1274 			def = 1;
1275 			result->def = parse.idx;
1276 			if (!parse.def_uni || !parse.def_multi)
1277 				goto error;
1278 		} else if (parse.defmgmt)
1279 			goto error;
1280 		err = cfg80211_validate_key_settings(rdev, &parse.p,
1281 						     parse.idx, false, NULL);
1282 		if (err)
1283 			goto error;
1284 		if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1285 		    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1286 			GENL_SET_ERR_MSG(info, "connect key must be WEP");
1287 			err = -EINVAL;
1288 			goto error;
1289 		}
1290 		result->params[parse.idx].cipher = parse.p.cipher;
1291 		result->params[parse.idx].key_len = parse.p.key_len;
1292 		result->params[parse.idx].key = result->data[parse.idx];
1293 		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1294 
1295 		/* must be WEP key if we got here */
1296 		if (no_ht)
1297 			*no_ht = true;
1298 	}
1299 
1300 	if (result->def < 0) {
1301 		err = -EINVAL;
1302 		GENL_SET_ERR_MSG(info, "need a default/TX key");
1303 		goto error;
1304 	}
1305 
1306 	return result;
1307  error:
1308 	kfree(result);
1309 	return ERR_PTR(err);
1310 }
1311 
1312 static int nl80211_key_allowed(struct wireless_dev *wdev)
1313 {
1314 	ASSERT_WDEV_LOCK(wdev);
1315 
1316 	switch (wdev->iftype) {
1317 	case NL80211_IFTYPE_AP:
1318 	case NL80211_IFTYPE_AP_VLAN:
1319 	case NL80211_IFTYPE_P2P_GO:
1320 	case NL80211_IFTYPE_MESH_POINT:
1321 		break;
1322 	case NL80211_IFTYPE_ADHOC:
1323 	case NL80211_IFTYPE_STATION:
1324 	case NL80211_IFTYPE_P2P_CLIENT:
1325 		if (!wdev->current_bss)
1326 			return -ENOLINK;
1327 		break;
1328 	case NL80211_IFTYPE_UNSPECIFIED:
1329 	case NL80211_IFTYPE_OCB:
1330 	case NL80211_IFTYPE_MONITOR:
1331 	case NL80211_IFTYPE_NAN:
1332 	case NL80211_IFTYPE_P2P_DEVICE:
1333 	case NL80211_IFTYPE_WDS:
1334 	case NUM_NL80211_IFTYPES:
1335 		return -EINVAL;
1336 	}
1337 
1338 	return 0;
1339 }
1340 
1341 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1342 							u32 freq)
1343 {
1344 	struct ieee80211_channel *chan;
1345 
1346 	chan = ieee80211_get_channel_khz(wiphy, freq);
1347 	if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1348 		return NULL;
1349 	return chan;
1350 }
1351 
1352 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1353 {
1354 	struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1355 	int i;
1356 
1357 	if (!nl_modes)
1358 		goto nla_put_failure;
1359 
1360 	i = 0;
1361 	while (ifmodes) {
1362 		if ((ifmodes & 1) && nla_put_flag(msg, i))
1363 			goto nla_put_failure;
1364 		ifmodes >>= 1;
1365 		i++;
1366 	}
1367 
1368 	nla_nest_end(msg, nl_modes);
1369 	return 0;
1370 
1371 nla_put_failure:
1372 	return -ENOBUFS;
1373 }
1374 
1375 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1376 					  struct sk_buff *msg,
1377 					  bool large)
1378 {
1379 	struct nlattr *nl_combis;
1380 	int i, j;
1381 
1382 	nl_combis = nla_nest_start_noflag(msg,
1383 					  NL80211_ATTR_INTERFACE_COMBINATIONS);
1384 	if (!nl_combis)
1385 		goto nla_put_failure;
1386 
1387 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1388 		const struct ieee80211_iface_combination *c;
1389 		struct nlattr *nl_combi, *nl_limits;
1390 
1391 		c = &wiphy->iface_combinations[i];
1392 
1393 		nl_combi = nla_nest_start_noflag(msg, i + 1);
1394 		if (!nl_combi)
1395 			goto nla_put_failure;
1396 
1397 		nl_limits = nla_nest_start_noflag(msg,
1398 						  NL80211_IFACE_COMB_LIMITS);
1399 		if (!nl_limits)
1400 			goto nla_put_failure;
1401 
1402 		for (j = 0; j < c->n_limits; j++) {
1403 			struct nlattr *nl_limit;
1404 
1405 			nl_limit = nla_nest_start_noflag(msg, j + 1);
1406 			if (!nl_limit)
1407 				goto nla_put_failure;
1408 			if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1409 					c->limits[j].max))
1410 				goto nla_put_failure;
1411 			if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1412 						c->limits[j].types))
1413 				goto nla_put_failure;
1414 			nla_nest_end(msg, nl_limit);
1415 		}
1416 
1417 		nla_nest_end(msg, nl_limits);
1418 
1419 		if (c->beacon_int_infra_match &&
1420 		    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1421 			goto nla_put_failure;
1422 		if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1423 				c->num_different_channels) ||
1424 		    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1425 				c->max_interfaces))
1426 			goto nla_put_failure;
1427 		if (large &&
1428 		    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1429 				c->radar_detect_widths) ||
1430 		     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1431 				c->radar_detect_regions)))
1432 			goto nla_put_failure;
1433 		if (c->beacon_int_min_gcd &&
1434 		    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1435 				c->beacon_int_min_gcd))
1436 			goto nla_put_failure;
1437 
1438 		nla_nest_end(msg, nl_combi);
1439 	}
1440 
1441 	nla_nest_end(msg, nl_combis);
1442 
1443 	return 0;
1444 nla_put_failure:
1445 	return -ENOBUFS;
1446 }
1447 
1448 #ifdef CONFIG_PM
1449 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1450 					struct sk_buff *msg)
1451 {
1452 	const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1453 	struct nlattr *nl_tcp;
1454 
1455 	if (!tcp)
1456 		return 0;
1457 
1458 	nl_tcp = nla_nest_start_noflag(msg,
1459 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1460 	if (!nl_tcp)
1461 		return -ENOBUFS;
1462 
1463 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1464 			tcp->data_payload_max))
1465 		return -ENOBUFS;
1466 
1467 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1468 			tcp->data_payload_max))
1469 		return -ENOBUFS;
1470 
1471 	if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1472 		return -ENOBUFS;
1473 
1474 	if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1475 				sizeof(*tcp->tok), tcp->tok))
1476 		return -ENOBUFS;
1477 
1478 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1479 			tcp->data_interval_max))
1480 		return -ENOBUFS;
1481 
1482 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1483 			tcp->wake_payload_max))
1484 		return -ENOBUFS;
1485 
1486 	nla_nest_end(msg, nl_tcp);
1487 	return 0;
1488 }
1489 
1490 static int nl80211_send_wowlan(struct sk_buff *msg,
1491 			       struct cfg80211_registered_device *rdev,
1492 			       bool large)
1493 {
1494 	struct nlattr *nl_wowlan;
1495 
1496 	if (!rdev->wiphy.wowlan)
1497 		return 0;
1498 
1499 	nl_wowlan = nla_nest_start_noflag(msg,
1500 					  NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1501 	if (!nl_wowlan)
1502 		return -ENOBUFS;
1503 
1504 	if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1505 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1506 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1507 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1508 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1509 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1510 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1511 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1512 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1513 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1514 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1515 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1516 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1517 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1518 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1519 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1520 		return -ENOBUFS;
1521 
1522 	if (rdev->wiphy.wowlan->n_patterns) {
1523 		struct nl80211_pattern_support pat = {
1524 			.max_patterns = rdev->wiphy.wowlan->n_patterns,
1525 			.min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1526 			.max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1527 			.max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1528 		};
1529 
1530 		if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1531 			    sizeof(pat), &pat))
1532 			return -ENOBUFS;
1533 	}
1534 
1535 	if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1536 	    nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1537 			rdev->wiphy.wowlan->max_nd_match_sets))
1538 		return -ENOBUFS;
1539 
1540 	if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1541 		return -ENOBUFS;
1542 
1543 	nla_nest_end(msg, nl_wowlan);
1544 
1545 	return 0;
1546 }
1547 #endif
1548 
1549 static int nl80211_send_coalesce(struct sk_buff *msg,
1550 				 struct cfg80211_registered_device *rdev)
1551 {
1552 	struct nl80211_coalesce_rule_support rule;
1553 
1554 	if (!rdev->wiphy.coalesce)
1555 		return 0;
1556 
1557 	rule.max_rules = rdev->wiphy.coalesce->n_rules;
1558 	rule.max_delay = rdev->wiphy.coalesce->max_delay;
1559 	rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1560 	rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1561 	rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1562 	rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1563 
1564 	if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1565 		return -ENOBUFS;
1566 
1567 	return 0;
1568 }
1569 
1570 static int
1571 nl80211_send_iftype_data(struct sk_buff *msg,
1572 			 const struct ieee80211_supported_band *sband,
1573 			 const struct ieee80211_sband_iftype_data *iftdata)
1574 {
1575 	const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1576 
1577 	if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1578 				iftdata->types_mask))
1579 		return -ENOBUFS;
1580 
1581 	if (he_cap->has_he) {
1582 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1583 			    sizeof(he_cap->he_cap_elem.mac_cap_info),
1584 			    he_cap->he_cap_elem.mac_cap_info) ||
1585 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1586 			    sizeof(he_cap->he_cap_elem.phy_cap_info),
1587 			    he_cap->he_cap_elem.phy_cap_info) ||
1588 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1589 			    sizeof(he_cap->he_mcs_nss_supp),
1590 			    &he_cap->he_mcs_nss_supp) ||
1591 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1592 			    sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1593 			return -ENOBUFS;
1594 	}
1595 
1596 	if (sband->band == NL80211_BAND_6GHZ &&
1597 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_6GHZ_CAPA,
1598 		    sizeof(iftdata->he_6ghz_capa),
1599 		    &iftdata->he_6ghz_capa))
1600 		return -ENOBUFS;
1601 
1602 	return 0;
1603 }
1604 
1605 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1606 				      struct ieee80211_supported_band *sband)
1607 {
1608 	struct nlattr *nl_rates, *nl_rate;
1609 	struct ieee80211_rate *rate;
1610 	int i;
1611 
1612 	/* add HT info */
1613 	if (sband->ht_cap.ht_supported &&
1614 	    (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1615 		     sizeof(sband->ht_cap.mcs),
1616 		     &sband->ht_cap.mcs) ||
1617 	     nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1618 			 sband->ht_cap.cap) ||
1619 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1620 			sband->ht_cap.ampdu_factor) ||
1621 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1622 			sband->ht_cap.ampdu_density)))
1623 		return -ENOBUFS;
1624 
1625 	/* add VHT info */
1626 	if (sband->vht_cap.vht_supported &&
1627 	    (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1628 		     sizeof(sband->vht_cap.vht_mcs),
1629 		     &sband->vht_cap.vht_mcs) ||
1630 	     nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1631 			 sband->vht_cap.cap)))
1632 		return -ENOBUFS;
1633 
1634 	if (sband->n_iftype_data) {
1635 		struct nlattr *nl_iftype_data =
1636 			nla_nest_start_noflag(msg,
1637 					      NL80211_BAND_ATTR_IFTYPE_DATA);
1638 		int err;
1639 
1640 		if (!nl_iftype_data)
1641 			return -ENOBUFS;
1642 
1643 		for (i = 0; i < sband->n_iftype_data; i++) {
1644 			struct nlattr *iftdata;
1645 
1646 			iftdata = nla_nest_start_noflag(msg, i + 1);
1647 			if (!iftdata)
1648 				return -ENOBUFS;
1649 
1650 			err = nl80211_send_iftype_data(msg, sband,
1651 						       &sband->iftype_data[i]);
1652 			if (err)
1653 				return err;
1654 
1655 			nla_nest_end(msg, iftdata);
1656 		}
1657 
1658 		nla_nest_end(msg, nl_iftype_data);
1659 	}
1660 
1661 	/* add EDMG info */
1662 	if (sband->edmg_cap.channels &&
1663 	    (nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_CHANNELS,
1664 		       sband->edmg_cap.channels) ||
1665 	    nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_BW_CONFIG,
1666 		       sband->edmg_cap.bw_config)))
1667 
1668 		return -ENOBUFS;
1669 
1670 	/* add bitrates */
1671 	nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1672 	if (!nl_rates)
1673 		return -ENOBUFS;
1674 
1675 	for (i = 0; i < sband->n_bitrates; i++) {
1676 		nl_rate = nla_nest_start_noflag(msg, i);
1677 		if (!nl_rate)
1678 			return -ENOBUFS;
1679 
1680 		rate = &sband->bitrates[i];
1681 		if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1682 				rate->bitrate))
1683 			return -ENOBUFS;
1684 		if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1685 		    nla_put_flag(msg,
1686 				 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1687 			return -ENOBUFS;
1688 
1689 		nla_nest_end(msg, nl_rate);
1690 	}
1691 
1692 	nla_nest_end(msg, nl_rates);
1693 
1694 	return 0;
1695 }
1696 
1697 static int
1698 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1699 			 const struct ieee80211_txrx_stypes *mgmt_stypes)
1700 {
1701 	u16 stypes;
1702 	struct nlattr *nl_ftypes, *nl_ifs;
1703 	enum nl80211_iftype ift;
1704 	int i;
1705 
1706 	if (!mgmt_stypes)
1707 		return 0;
1708 
1709 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
1710 	if (!nl_ifs)
1711 		return -ENOBUFS;
1712 
1713 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1714 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1715 		if (!nl_ftypes)
1716 			return -ENOBUFS;
1717 		i = 0;
1718 		stypes = mgmt_stypes[ift].tx;
1719 		while (stypes) {
1720 			if ((stypes & 1) &&
1721 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1722 					(i << 4) | IEEE80211_FTYPE_MGMT))
1723 				return -ENOBUFS;
1724 			stypes >>= 1;
1725 			i++;
1726 		}
1727 		nla_nest_end(msg, nl_ftypes);
1728 	}
1729 
1730 	nla_nest_end(msg, nl_ifs);
1731 
1732 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
1733 	if (!nl_ifs)
1734 		return -ENOBUFS;
1735 
1736 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1737 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1738 		if (!nl_ftypes)
1739 			return -ENOBUFS;
1740 		i = 0;
1741 		stypes = mgmt_stypes[ift].rx;
1742 		while (stypes) {
1743 			if ((stypes & 1) &&
1744 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1745 					(i << 4) | IEEE80211_FTYPE_MGMT))
1746 				return -ENOBUFS;
1747 			stypes >>= 1;
1748 			i++;
1749 		}
1750 		nla_nest_end(msg, nl_ftypes);
1751 	}
1752 	nla_nest_end(msg, nl_ifs);
1753 
1754 	return 0;
1755 }
1756 
1757 #define CMD(op, n)							\
1758 	 do {								\
1759 		if (rdev->ops->op) {					\
1760 			i++;						\
1761 			if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) 	\
1762 				goto nla_put_failure;			\
1763 		}							\
1764 	} while (0)
1765 
1766 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
1767 					struct sk_buff *msg)
1768 {
1769 	int i = 0;
1770 
1771 	/*
1772 	 * do *NOT* add anything into this function, new things need to be
1773 	 * advertised only to new versions of userspace that can deal with
1774 	 * the split (and they can't possibly care about new features...
1775 	 */
1776 	CMD(add_virtual_intf, NEW_INTERFACE);
1777 	CMD(change_virtual_intf, SET_INTERFACE);
1778 	CMD(add_key, NEW_KEY);
1779 	CMD(start_ap, START_AP);
1780 	CMD(add_station, NEW_STATION);
1781 	CMD(add_mpath, NEW_MPATH);
1782 	CMD(update_mesh_config, SET_MESH_CONFIG);
1783 	CMD(change_bss, SET_BSS);
1784 	CMD(auth, AUTHENTICATE);
1785 	CMD(assoc, ASSOCIATE);
1786 	CMD(deauth, DEAUTHENTICATE);
1787 	CMD(disassoc, DISASSOCIATE);
1788 	CMD(join_ibss, JOIN_IBSS);
1789 	CMD(join_mesh, JOIN_MESH);
1790 	CMD(set_pmksa, SET_PMKSA);
1791 	CMD(del_pmksa, DEL_PMKSA);
1792 	CMD(flush_pmksa, FLUSH_PMKSA);
1793 	if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1794 		CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1795 	CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1796 	CMD(mgmt_tx, FRAME);
1797 	CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1798 	if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1799 		i++;
1800 		if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1801 			goto nla_put_failure;
1802 	}
1803 	if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1804 	    rdev->ops->join_mesh) {
1805 		i++;
1806 		if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1807 			goto nla_put_failure;
1808 	}
1809 	CMD(set_wds_peer, SET_WDS_PEER);
1810 	if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1811 		CMD(tdls_mgmt, TDLS_MGMT);
1812 		CMD(tdls_oper, TDLS_OPER);
1813 	}
1814 	if (rdev->wiphy.max_sched_scan_reqs)
1815 		CMD(sched_scan_start, START_SCHED_SCAN);
1816 	CMD(probe_client, PROBE_CLIENT);
1817 	CMD(set_noack_map, SET_NOACK_MAP);
1818 	if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1819 		i++;
1820 		if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1821 			goto nla_put_failure;
1822 	}
1823 	CMD(start_p2p_device, START_P2P_DEVICE);
1824 	CMD(set_mcast_rate, SET_MCAST_RATE);
1825 #ifdef CONFIG_NL80211_TESTMODE
1826 	CMD(testmode_cmd, TESTMODE);
1827 #endif
1828 
1829 	if (rdev->ops->connect || rdev->ops->auth) {
1830 		i++;
1831 		if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1832 			goto nla_put_failure;
1833 	}
1834 
1835 	if (rdev->ops->disconnect || rdev->ops->deauth) {
1836 		i++;
1837 		if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1838 			goto nla_put_failure;
1839 	}
1840 
1841 	return i;
1842  nla_put_failure:
1843 	return -ENOBUFS;
1844 }
1845 
1846 static int
1847 nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
1848 			   struct sk_buff *msg)
1849 {
1850 	struct nlattr *ftm;
1851 
1852 	if (!cap->ftm.supported)
1853 		return 0;
1854 
1855 	ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
1856 	if (!ftm)
1857 		return -ENOBUFS;
1858 
1859 	if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
1860 		return -ENOBUFS;
1861 	if (cap->ftm.non_asap &&
1862 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
1863 		return -ENOBUFS;
1864 	if (cap->ftm.request_lci &&
1865 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
1866 		return -ENOBUFS;
1867 	if (cap->ftm.request_civicloc &&
1868 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
1869 		return -ENOBUFS;
1870 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
1871 			cap->ftm.preambles))
1872 		return -ENOBUFS;
1873 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
1874 			cap->ftm.bandwidths))
1875 		return -ENOBUFS;
1876 	if (cap->ftm.max_bursts_exponent >= 0 &&
1877 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
1878 			cap->ftm.max_bursts_exponent))
1879 		return -ENOBUFS;
1880 	if (cap->ftm.max_ftms_per_burst &&
1881 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
1882 			cap->ftm.max_ftms_per_burst))
1883 		return -ENOBUFS;
1884 	if (cap->ftm.trigger_based &&
1885 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_TRIGGER_BASED))
1886 		return -ENOBUFS;
1887 	if (cap->ftm.non_trigger_based &&
1888 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_TRIGGER_BASED))
1889 		return -ENOBUFS;
1890 
1891 	nla_nest_end(msg, ftm);
1892 	return 0;
1893 }
1894 
1895 static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
1896 				  struct sk_buff *msg)
1897 {
1898 	const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
1899 	struct nlattr *pmsr, *caps;
1900 
1901 	if (!cap)
1902 		return 0;
1903 
1904 	/*
1905 	 * we don't need to clean up anything here since the caller
1906 	 * will genlmsg_cancel() if we fail
1907 	 */
1908 
1909 	pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
1910 	if (!pmsr)
1911 		return -ENOBUFS;
1912 
1913 	if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
1914 		return -ENOBUFS;
1915 
1916 	if (cap->report_ap_tsf &&
1917 	    nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
1918 		return -ENOBUFS;
1919 
1920 	if (cap->randomize_mac_addr &&
1921 	    nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
1922 		return -ENOBUFS;
1923 
1924 	caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
1925 	if (!caps)
1926 		return -ENOBUFS;
1927 
1928 	if (nl80211_send_pmsr_ftm_capa(cap, msg))
1929 		return -ENOBUFS;
1930 
1931 	nla_nest_end(msg, caps);
1932 	nla_nest_end(msg, pmsr);
1933 
1934 	return 0;
1935 }
1936 
1937 static int
1938 nl80211_put_iftype_akm_suites(struct cfg80211_registered_device *rdev,
1939 			      struct sk_buff *msg)
1940 {
1941 	int i;
1942 	struct nlattr *nested, *nested_akms;
1943 	const struct wiphy_iftype_akm_suites *iftype_akms;
1944 
1945 	if (!rdev->wiphy.num_iftype_akm_suites ||
1946 	    !rdev->wiphy.iftype_akm_suites)
1947 		return 0;
1948 
1949 	nested = nla_nest_start(msg, NL80211_ATTR_IFTYPE_AKM_SUITES);
1950 	if (!nested)
1951 		return -ENOBUFS;
1952 
1953 	for (i = 0; i < rdev->wiphy.num_iftype_akm_suites; i++) {
1954 		nested_akms = nla_nest_start(msg, i + 1);
1955 		if (!nested_akms)
1956 			return -ENOBUFS;
1957 
1958 		iftype_akms = &rdev->wiphy.iftype_akm_suites[i];
1959 
1960 		if (nl80211_put_iftypes(msg, NL80211_IFTYPE_AKM_ATTR_IFTYPES,
1961 					iftype_akms->iftypes_mask))
1962 			return -ENOBUFS;
1963 
1964 		if (nla_put(msg, NL80211_IFTYPE_AKM_ATTR_SUITES,
1965 			    sizeof(u32) * iftype_akms->n_akm_suites,
1966 			    iftype_akms->akm_suites)) {
1967 			return -ENOBUFS;
1968 		}
1969 		nla_nest_end(msg, nested_akms);
1970 	}
1971 
1972 	nla_nest_end(msg, nested);
1973 
1974 	return 0;
1975 }
1976 
1977 static int
1978 nl80211_put_tid_config_support(struct cfg80211_registered_device *rdev,
1979 			       struct sk_buff *msg)
1980 {
1981 	struct nlattr *supp;
1982 
1983 	if (!rdev->wiphy.tid_config_support.vif &&
1984 	    !rdev->wiphy.tid_config_support.peer)
1985 		return 0;
1986 
1987 	supp = nla_nest_start(msg, NL80211_ATTR_TID_CONFIG);
1988 	if (!supp)
1989 		return -ENOSPC;
1990 
1991 	if (rdev->wiphy.tid_config_support.vif &&
1992 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_VIF_SUPP,
1993 			      rdev->wiphy.tid_config_support.vif,
1994 			      NL80211_TID_CONFIG_ATTR_PAD))
1995 		goto fail;
1996 
1997 	if (rdev->wiphy.tid_config_support.peer &&
1998 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_PEER_SUPP,
1999 			      rdev->wiphy.tid_config_support.peer,
2000 			      NL80211_TID_CONFIG_ATTR_PAD))
2001 		goto fail;
2002 
2003 	/* for now we just use the same value ... makes more sense */
2004 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_SHORT,
2005 		       rdev->wiphy.tid_config_support.max_retry))
2006 		goto fail;
2007 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_LONG,
2008 		       rdev->wiphy.tid_config_support.max_retry))
2009 		goto fail;
2010 
2011 	nla_nest_end(msg, supp);
2012 
2013 	return 0;
2014 fail:
2015 	nla_nest_cancel(msg, supp);
2016 	return -ENOBUFS;
2017 }
2018 
2019 struct nl80211_dump_wiphy_state {
2020 	s64 filter_wiphy;
2021 	long start;
2022 	long split_start, band_start, chan_start, capa_start;
2023 	bool split;
2024 };
2025 
2026 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
2027 			      enum nl80211_commands cmd,
2028 			      struct sk_buff *msg, u32 portid, u32 seq,
2029 			      int flags, struct nl80211_dump_wiphy_state *state)
2030 {
2031 	void *hdr;
2032 	struct nlattr *nl_bands, *nl_band;
2033 	struct nlattr *nl_freqs, *nl_freq;
2034 	struct nlattr *nl_cmds;
2035 	enum nl80211_band band;
2036 	struct ieee80211_channel *chan;
2037 	int i;
2038 	const struct ieee80211_txrx_stypes *mgmt_stypes =
2039 				rdev->wiphy.mgmt_stypes;
2040 	u32 features;
2041 
2042 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2043 	if (!hdr)
2044 		return -ENOBUFS;
2045 
2046 	if (WARN_ON(!state))
2047 		return -EINVAL;
2048 
2049 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2050 	    nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
2051 			   wiphy_name(&rdev->wiphy)) ||
2052 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
2053 			cfg80211_rdev_list_generation))
2054 		goto nla_put_failure;
2055 
2056 	if (cmd != NL80211_CMD_NEW_WIPHY)
2057 		goto finish;
2058 
2059 	switch (state->split_start) {
2060 	case 0:
2061 		if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
2062 			       rdev->wiphy.retry_short) ||
2063 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
2064 			       rdev->wiphy.retry_long) ||
2065 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
2066 				rdev->wiphy.frag_threshold) ||
2067 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
2068 				rdev->wiphy.rts_threshold) ||
2069 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
2070 			       rdev->wiphy.coverage_class) ||
2071 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
2072 			       rdev->wiphy.max_scan_ssids) ||
2073 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
2074 			       rdev->wiphy.max_sched_scan_ssids) ||
2075 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
2076 				rdev->wiphy.max_scan_ie_len) ||
2077 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
2078 				rdev->wiphy.max_sched_scan_ie_len) ||
2079 		    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
2080 			       rdev->wiphy.max_match_sets) ||
2081 		    nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
2082 				rdev->wiphy.max_sched_scan_plans) ||
2083 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
2084 				rdev->wiphy.max_sched_scan_plan_interval) ||
2085 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
2086 				rdev->wiphy.max_sched_scan_plan_iterations))
2087 			goto nla_put_failure;
2088 
2089 		if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
2090 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
2091 			goto nla_put_failure;
2092 		if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
2093 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
2094 			goto nla_put_failure;
2095 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2096 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
2097 			goto nla_put_failure;
2098 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
2099 		    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
2100 			goto nla_put_failure;
2101 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2102 		    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
2103 			goto nla_put_failure;
2104 		if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
2105 		    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
2106 			goto nla_put_failure;
2107 		state->split_start++;
2108 		if (state->split)
2109 			break;
2110 		/* fall through */
2111 	case 1:
2112 		if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
2113 			    sizeof(u32) * rdev->wiphy.n_cipher_suites,
2114 			    rdev->wiphy.cipher_suites))
2115 			goto nla_put_failure;
2116 
2117 		if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
2118 			       rdev->wiphy.max_num_pmkids))
2119 			goto nla_put_failure;
2120 
2121 		if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
2122 		    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
2123 			goto nla_put_failure;
2124 
2125 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
2126 				rdev->wiphy.available_antennas_tx) ||
2127 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
2128 				rdev->wiphy.available_antennas_rx))
2129 			goto nla_put_failure;
2130 
2131 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
2132 		    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
2133 				rdev->wiphy.probe_resp_offload))
2134 			goto nla_put_failure;
2135 
2136 		if ((rdev->wiphy.available_antennas_tx ||
2137 		     rdev->wiphy.available_antennas_rx) &&
2138 		    rdev->ops->get_antenna) {
2139 			u32 tx_ant = 0, rx_ant = 0;
2140 			int res;
2141 
2142 			res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
2143 			if (!res) {
2144 				if (nla_put_u32(msg,
2145 						NL80211_ATTR_WIPHY_ANTENNA_TX,
2146 						tx_ant) ||
2147 				    nla_put_u32(msg,
2148 						NL80211_ATTR_WIPHY_ANTENNA_RX,
2149 						rx_ant))
2150 					goto nla_put_failure;
2151 			}
2152 		}
2153 
2154 		state->split_start++;
2155 		if (state->split)
2156 			break;
2157 		/* fall through */
2158 	case 2:
2159 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
2160 					rdev->wiphy.interface_modes))
2161 				goto nla_put_failure;
2162 		state->split_start++;
2163 		if (state->split)
2164 			break;
2165 		/* fall through */
2166 	case 3:
2167 		nl_bands = nla_nest_start_noflag(msg,
2168 						 NL80211_ATTR_WIPHY_BANDS);
2169 		if (!nl_bands)
2170 			goto nla_put_failure;
2171 
2172 		for (band = state->band_start;
2173 		     band < NUM_NL80211_BANDS; band++) {
2174 			struct ieee80211_supported_band *sband;
2175 
2176 			sband = rdev->wiphy.bands[band];
2177 
2178 			if (!sband)
2179 				continue;
2180 
2181 			nl_band = nla_nest_start_noflag(msg, band);
2182 			if (!nl_band)
2183 				goto nla_put_failure;
2184 
2185 			switch (state->chan_start) {
2186 			case 0:
2187 				if (nl80211_send_band_rateinfo(msg, sband))
2188 					goto nla_put_failure;
2189 				state->chan_start++;
2190 				if (state->split)
2191 					break;
2192 				/* fall through */
2193 			default:
2194 				/* add frequencies */
2195 				nl_freqs = nla_nest_start_noflag(msg,
2196 								 NL80211_BAND_ATTR_FREQS);
2197 				if (!nl_freqs)
2198 					goto nla_put_failure;
2199 
2200 				for (i = state->chan_start - 1;
2201 				     i < sband->n_channels;
2202 				     i++) {
2203 					nl_freq = nla_nest_start_noflag(msg,
2204 									i);
2205 					if (!nl_freq)
2206 						goto nla_put_failure;
2207 
2208 					chan = &sband->channels[i];
2209 
2210 					if (nl80211_msg_put_channel(
2211 							msg, &rdev->wiphy, chan,
2212 							state->split))
2213 						goto nla_put_failure;
2214 
2215 					nla_nest_end(msg, nl_freq);
2216 					if (state->split)
2217 						break;
2218 				}
2219 				if (i < sband->n_channels)
2220 					state->chan_start = i + 2;
2221 				else
2222 					state->chan_start = 0;
2223 				nla_nest_end(msg, nl_freqs);
2224 			}
2225 
2226 			nla_nest_end(msg, nl_band);
2227 
2228 			if (state->split) {
2229 				/* start again here */
2230 				if (state->chan_start)
2231 					band--;
2232 				break;
2233 			}
2234 		}
2235 		nla_nest_end(msg, nl_bands);
2236 
2237 		if (band < NUM_NL80211_BANDS)
2238 			state->band_start = band + 1;
2239 		else
2240 			state->band_start = 0;
2241 
2242 		/* if bands & channels are done, continue outside */
2243 		if (state->band_start == 0 && state->chan_start == 0)
2244 			state->split_start++;
2245 		if (state->split)
2246 			break;
2247 		/* fall through */
2248 	case 4:
2249 		nl_cmds = nla_nest_start_noflag(msg,
2250 						NL80211_ATTR_SUPPORTED_COMMANDS);
2251 		if (!nl_cmds)
2252 			goto nla_put_failure;
2253 
2254 		i = nl80211_add_commands_unsplit(rdev, msg);
2255 		if (i < 0)
2256 			goto nla_put_failure;
2257 		if (state->split) {
2258 			CMD(crit_proto_start, CRIT_PROTOCOL_START);
2259 			CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2260 			if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2261 				CMD(channel_switch, CHANNEL_SWITCH);
2262 			CMD(set_qos_map, SET_QOS_MAP);
2263 			if (rdev->wiphy.features &
2264 					NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2265 				CMD(add_tx_ts, ADD_TX_TS);
2266 			CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2267 			CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2268 			CMD(update_ft_ies, UPDATE_FT_IES);
2269 		}
2270 #undef CMD
2271 
2272 		nla_nest_end(msg, nl_cmds);
2273 		state->split_start++;
2274 		if (state->split)
2275 			break;
2276 		/* fall through */
2277 	case 5:
2278 		if (rdev->ops->remain_on_channel &&
2279 		    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2280 		    nla_put_u32(msg,
2281 				NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2282 				rdev->wiphy.max_remain_on_channel_duration))
2283 			goto nla_put_failure;
2284 
2285 		if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2286 		    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2287 			goto nla_put_failure;
2288 
2289 		if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2290 			goto nla_put_failure;
2291 		state->split_start++;
2292 		if (state->split)
2293 			break;
2294 		/* fall through */
2295 	case 6:
2296 #ifdef CONFIG_PM
2297 		if (nl80211_send_wowlan(msg, rdev, state->split))
2298 			goto nla_put_failure;
2299 		state->split_start++;
2300 		if (state->split)
2301 			break;
2302 #else
2303 		state->split_start++;
2304 #endif
2305 		/* fall through */
2306 	case 7:
2307 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2308 					rdev->wiphy.software_iftypes))
2309 			goto nla_put_failure;
2310 
2311 		if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2312 						   state->split))
2313 			goto nla_put_failure;
2314 
2315 		state->split_start++;
2316 		if (state->split)
2317 			break;
2318 		/* fall through */
2319 	case 8:
2320 		if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2321 		    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2322 				rdev->wiphy.ap_sme_capa))
2323 			goto nla_put_failure;
2324 
2325 		features = rdev->wiphy.features;
2326 		/*
2327 		 * We can only add the per-channel limit information if the
2328 		 * dump is split, otherwise it makes it too big. Therefore
2329 		 * only advertise it in that case.
2330 		 */
2331 		if (state->split)
2332 			features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2333 		if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2334 			goto nla_put_failure;
2335 
2336 		if (rdev->wiphy.ht_capa_mod_mask &&
2337 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2338 			    sizeof(*rdev->wiphy.ht_capa_mod_mask),
2339 			    rdev->wiphy.ht_capa_mod_mask))
2340 			goto nla_put_failure;
2341 
2342 		if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2343 		    rdev->wiphy.max_acl_mac_addrs &&
2344 		    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2345 				rdev->wiphy.max_acl_mac_addrs))
2346 			goto nla_put_failure;
2347 
2348 		/*
2349 		 * Any information below this point is only available to
2350 		 * applications that can deal with it being split. This
2351 		 * helps ensure that newly added capabilities don't break
2352 		 * older tools by overrunning their buffers.
2353 		 *
2354 		 * We still increment split_start so that in the split
2355 		 * case we'll continue with more data in the next round,
2356 		 * but break unconditionally so unsplit data stops here.
2357 		 */
2358 		state->split_start++;
2359 		break;
2360 	case 9:
2361 		if (rdev->wiphy.extended_capabilities &&
2362 		    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2363 			     rdev->wiphy.extended_capabilities_len,
2364 			     rdev->wiphy.extended_capabilities) ||
2365 		     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2366 			     rdev->wiphy.extended_capabilities_len,
2367 			     rdev->wiphy.extended_capabilities_mask)))
2368 			goto nla_put_failure;
2369 
2370 		if (rdev->wiphy.vht_capa_mod_mask &&
2371 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2372 			    sizeof(*rdev->wiphy.vht_capa_mod_mask),
2373 			    rdev->wiphy.vht_capa_mod_mask))
2374 			goto nla_put_failure;
2375 
2376 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2377 			    rdev->wiphy.perm_addr))
2378 			goto nla_put_failure;
2379 
2380 		if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2381 		    nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2382 			    rdev->wiphy.addr_mask))
2383 			goto nla_put_failure;
2384 
2385 		if (rdev->wiphy.n_addresses > 1) {
2386 			void *attr;
2387 
2388 			attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2389 			if (!attr)
2390 				goto nla_put_failure;
2391 
2392 			for (i = 0; i < rdev->wiphy.n_addresses; i++)
2393 				if (nla_put(msg, i + 1, ETH_ALEN,
2394 					    rdev->wiphy.addresses[i].addr))
2395 					goto nla_put_failure;
2396 
2397 			nla_nest_end(msg, attr);
2398 		}
2399 
2400 		state->split_start++;
2401 		break;
2402 	case 10:
2403 		if (nl80211_send_coalesce(msg, rdev))
2404 			goto nla_put_failure;
2405 
2406 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2407 		    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2408 		     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2409 			goto nla_put_failure;
2410 
2411 		if (rdev->wiphy.max_ap_assoc_sta &&
2412 		    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2413 				rdev->wiphy.max_ap_assoc_sta))
2414 			goto nla_put_failure;
2415 
2416 		state->split_start++;
2417 		break;
2418 	case 11:
2419 		if (rdev->wiphy.n_vendor_commands) {
2420 			const struct nl80211_vendor_cmd_info *info;
2421 			struct nlattr *nested;
2422 
2423 			nested = nla_nest_start_noflag(msg,
2424 						       NL80211_ATTR_VENDOR_DATA);
2425 			if (!nested)
2426 				goto nla_put_failure;
2427 
2428 			for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2429 				info = &rdev->wiphy.vendor_commands[i].info;
2430 				if (nla_put(msg, i + 1, sizeof(*info), info))
2431 					goto nla_put_failure;
2432 			}
2433 			nla_nest_end(msg, nested);
2434 		}
2435 
2436 		if (rdev->wiphy.n_vendor_events) {
2437 			const struct nl80211_vendor_cmd_info *info;
2438 			struct nlattr *nested;
2439 
2440 			nested = nla_nest_start_noflag(msg,
2441 						       NL80211_ATTR_VENDOR_EVENTS);
2442 			if (!nested)
2443 				goto nla_put_failure;
2444 
2445 			for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2446 				info = &rdev->wiphy.vendor_events[i];
2447 				if (nla_put(msg, i + 1, sizeof(*info), info))
2448 					goto nla_put_failure;
2449 			}
2450 			nla_nest_end(msg, nested);
2451 		}
2452 		state->split_start++;
2453 		break;
2454 	case 12:
2455 		if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2456 		    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2457 			       rdev->wiphy.max_num_csa_counters))
2458 			goto nla_put_failure;
2459 
2460 		if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2461 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2462 			goto nla_put_failure;
2463 
2464 		if (rdev->wiphy.max_sched_scan_reqs &&
2465 		    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2466 				rdev->wiphy.max_sched_scan_reqs))
2467 			goto nla_put_failure;
2468 
2469 		if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2470 			    sizeof(rdev->wiphy.ext_features),
2471 			    rdev->wiphy.ext_features))
2472 			goto nla_put_failure;
2473 
2474 		if (rdev->wiphy.bss_select_support) {
2475 			struct nlattr *nested;
2476 			u32 bss_select_support = rdev->wiphy.bss_select_support;
2477 
2478 			nested = nla_nest_start_noflag(msg,
2479 						       NL80211_ATTR_BSS_SELECT);
2480 			if (!nested)
2481 				goto nla_put_failure;
2482 
2483 			i = 0;
2484 			while (bss_select_support) {
2485 				if ((bss_select_support & 1) &&
2486 				    nla_put_flag(msg, i))
2487 					goto nla_put_failure;
2488 				i++;
2489 				bss_select_support >>= 1;
2490 			}
2491 			nla_nest_end(msg, nested);
2492 		}
2493 
2494 		state->split_start++;
2495 		break;
2496 	case 13:
2497 		if (rdev->wiphy.num_iftype_ext_capab &&
2498 		    rdev->wiphy.iftype_ext_capab) {
2499 			struct nlattr *nested_ext_capab, *nested;
2500 
2501 			nested = nla_nest_start_noflag(msg,
2502 						       NL80211_ATTR_IFTYPE_EXT_CAPA);
2503 			if (!nested)
2504 				goto nla_put_failure;
2505 
2506 			for (i = state->capa_start;
2507 			     i < rdev->wiphy.num_iftype_ext_capab; i++) {
2508 				const struct wiphy_iftype_ext_capab *capab;
2509 
2510 				capab = &rdev->wiphy.iftype_ext_capab[i];
2511 
2512 				nested_ext_capab = nla_nest_start_noflag(msg,
2513 									 i);
2514 				if (!nested_ext_capab ||
2515 				    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2516 						capab->iftype) ||
2517 				    nla_put(msg, NL80211_ATTR_EXT_CAPA,
2518 					    capab->extended_capabilities_len,
2519 					    capab->extended_capabilities) ||
2520 				    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2521 					    capab->extended_capabilities_len,
2522 					    capab->extended_capabilities_mask))
2523 					goto nla_put_failure;
2524 
2525 				nla_nest_end(msg, nested_ext_capab);
2526 				if (state->split)
2527 					break;
2528 			}
2529 			nla_nest_end(msg, nested);
2530 			if (i < rdev->wiphy.num_iftype_ext_capab) {
2531 				state->capa_start = i + 1;
2532 				break;
2533 			}
2534 		}
2535 
2536 		if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2537 				rdev->wiphy.nan_supported_bands))
2538 			goto nla_put_failure;
2539 
2540 		if (wiphy_ext_feature_isset(&rdev->wiphy,
2541 					    NL80211_EXT_FEATURE_TXQS)) {
2542 			struct cfg80211_txq_stats txqstats = {};
2543 			int res;
2544 
2545 			res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2546 			if (!res &&
2547 			    !nl80211_put_txq_stats(msg, &txqstats,
2548 						   NL80211_ATTR_TXQ_STATS))
2549 				goto nla_put_failure;
2550 
2551 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2552 					rdev->wiphy.txq_limit))
2553 				goto nla_put_failure;
2554 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2555 					rdev->wiphy.txq_memory_limit))
2556 				goto nla_put_failure;
2557 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2558 					rdev->wiphy.txq_quantum))
2559 				goto nla_put_failure;
2560 		}
2561 
2562 		state->split_start++;
2563 		break;
2564 	case 14:
2565 		if (nl80211_send_pmsr_capa(rdev, msg))
2566 			goto nla_put_failure;
2567 
2568 		state->split_start++;
2569 		break;
2570 	case 15:
2571 		if (rdev->wiphy.akm_suites &&
2572 		    nla_put(msg, NL80211_ATTR_AKM_SUITES,
2573 			    sizeof(u32) * rdev->wiphy.n_akm_suites,
2574 			    rdev->wiphy.akm_suites))
2575 			goto nla_put_failure;
2576 
2577 		if (nl80211_put_iftype_akm_suites(rdev, msg))
2578 			goto nla_put_failure;
2579 
2580 		if (nl80211_put_tid_config_support(rdev, msg))
2581 			goto nla_put_failure;
2582 
2583 		/* done */
2584 		state->split_start = 0;
2585 		break;
2586 	}
2587  finish:
2588 	genlmsg_end(msg, hdr);
2589 	return 0;
2590 
2591  nla_put_failure:
2592 	genlmsg_cancel(msg, hdr);
2593 	return -EMSGSIZE;
2594 }
2595 
2596 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
2597 				    struct netlink_callback *cb,
2598 				    struct nl80211_dump_wiphy_state *state)
2599 {
2600 	struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
2601 	int ret;
2602 
2603 	if (!tb)
2604 		return -ENOMEM;
2605 
2606 	ret = nlmsg_parse_deprecated(cb->nlh,
2607 				     GENL_HDRLEN + nl80211_fam.hdrsize,
2608 				     tb, nl80211_fam.maxattr,
2609 				     nl80211_policy, NULL);
2610 	/* ignore parse errors for backward compatibility */
2611 	if (ret) {
2612 		ret = 0;
2613 		goto out;
2614 	}
2615 
2616 	state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
2617 	if (tb[NL80211_ATTR_WIPHY])
2618 		state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2619 	if (tb[NL80211_ATTR_WDEV])
2620 		state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
2621 	if (tb[NL80211_ATTR_IFINDEX]) {
2622 		struct net_device *netdev;
2623 		struct cfg80211_registered_device *rdev;
2624 		int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2625 
2626 		netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
2627 		if (!netdev) {
2628 			ret = -ENODEV;
2629 			goto out;
2630 		}
2631 		if (netdev->ieee80211_ptr) {
2632 			rdev = wiphy_to_rdev(
2633 				netdev->ieee80211_ptr->wiphy);
2634 			state->filter_wiphy = rdev->wiphy_idx;
2635 		}
2636 	}
2637 
2638 	ret = 0;
2639 out:
2640 	kfree(tb);
2641 	return ret;
2642 }
2643 
2644 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
2645 {
2646 	int idx = 0, ret;
2647 	struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
2648 	struct cfg80211_registered_device *rdev;
2649 
2650 	rtnl_lock();
2651 	if (!state) {
2652 		state = kzalloc(sizeof(*state), GFP_KERNEL);
2653 		if (!state) {
2654 			rtnl_unlock();
2655 			return -ENOMEM;
2656 		}
2657 		state->filter_wiphy = -1;
2658 		ret = nl80211_dump_wiphy_parse(skb, cb, state);
2659 		if (ret) {
2660 			kfree(state);
2661 			rtnl_unlock();
2662 			return ret;
2663 		}
2664 		cb->args[0] = (long)state;
2665 	}
2666 
2667 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2668 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2669 			continue;
2670 		if (++idx <= state->start)
2671 			continue;
2672 		if (state->filter_wiphy != -1 &&
2673 		    state->filter_wiphy != rdev->wiphy_idx)
2674 			continue;
2675 		/* attempt to fit multiple wiphy data chunks into the skb */
2676 		do {
2677 			ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
2678 						 skb,
2679 						 NETLINK_CB(cb->skb).portid,
2680 						 cb->nlh->nlmsg_seq,
2681 						 NLM_F_MULTI, state);
2682 			if (ret < 0) {
2683 				/*
2684 				 * If sending the wiphy data didn't fit (ENOBUFS
2685 				 * or EMSGSIZE returned), this SKB is still
2686 				 * empty (so it's not too big because another
2687 				 * wiphy dataset is already in the skb) and
2688 				 * we've not tried to adjust the dump allocation
2689 				 * yet ... then adjust the alloc size to be
2690 				 * bigger, and return 1 but with the empty skb.
2691 				 * This results in an empty message being RX'ed
2692 				 * in userspace, but that is ignored.
2693 				 *
2694 				 * We can then retry with the larger buffer.
2695 				 */
2696 				if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
2697 				    !skb->len && !state->split &&
2698 				    cb->min_dump_alloc < 4096) {
2699 					cb->min_dump_alloc = 4096;
2700 					state->split_start = 0;
2701 					rtnl_unlock();
2702 					return 1;
2703 				}
2704 				idx--;
2705 				break;
2706 			}
2707 		} while (state->split_start > 0);
2708 		break;
2709 	}
2710 	rtnl_unlock();
2711 
2712 	state->start = idx;
2713 
2714 	return skb->len;
2715 }
2716 
2717 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
2718 {
2719 	kfree((void *)cb->args[0]);
2720 	return 0;
2721 }
2722 
2723 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
2724 {
2725 	struct sk_buff *msg;
2726 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2727 	struct nl80211_dump_wiphy_state state = {};
2728 
2729 	msg = nlmsg_new(4096, GFP_KERNEL);
2730 	if (!msg)
2731 		return -ENOMEM;
2732 
2733 	if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
2734 			       info->snd_portid, info->snd_seq, 0,
2735 			       &state) < 0) {
2736 		nlmsg_free(msg);
2737 		return -ENOBUFS;
2738 	}
2739 
2740 	return genlmsg_reply(msg, info);
2741 }
2742 
2743 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
2744 	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
2745 	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
2746 	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
2747 	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
2748 	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
2749 };
2750 
2751 static int parse_txq_params(struct nlattr *tb[],
2752 			    struct ieee80211_txq_params *txq_params)
2753 {
2754 	u8 ac;
2755 
2756 	if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
2757 	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
2758 	    !tb[NL80211_TXQ_ATTR_AIFS])
2759 		return -EINVAL;
2760 
2761 	ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
2762 	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
2763 	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
2764 	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
2765 	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
2766 
2767 	if (ac >= NL80211_NUM_ACS)
2768 		return -EINVAL;
2769 	txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
2770 	return 0;
2771 }
2772 
2773 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
2774 {
2775 	/*
2776 	 * You can only set the channel explicitly for WDS interfaces,
2777 	 * all others have their channel managed via their respective
2778 	 * "establish a connection" command (connect, join, ...)
2779 	 *
2780 	 * For AP/GO and mesh mode, the channel can be set with the
2781 	 * channel userspace API, but is only stored and passed to the
2782 	 * low-level driver when the AP starts or the mesh is joined.
2783 	 * This is for backward compatibility, userspace can also give
2784 	 * the channel in the start-ap or join-mesh commands instead.
2785 	 *
2786 	 * Monitors are special as they are normally slaved to
2787 	 * whatever else is going on, so they have their own special
2788 	 * operation to set the monitor channel if possible.
2789 	 */
2790 	return !wdev ||
2791 		wdev->iftype == NL80211_IFTYPE_AP ||
2792 		wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
2793 		wdev->iftype == NL80211_IFTYPE_MONITOR ||
2794 		wdev->iftype == NL80211_IFTYPE_P2P_GO;
2795 }
2796 
2797 int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
2798 			  struct genl_info *info,
2799 			  struct cfg80211_chan_def *chandef)
2800 {
2801 	struct netlink_ext_ack *extack = info->extack;
2802 	struct nlattr **attrs = info->attrs;
2803 	u32 control_freq;
2804 
2805 	if (!attrs[NL80211_ATTR_WIPHY_FREQ])
2806 		return -EINVAL;
2807 
2808 	control_freq = MHZ_TO_KHZ(
2809 			nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
2810 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
2811 		control_freq +=
2812 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
2813 
2814 	memset(chandef, 0, sizeof(*chandef));
2815 	chandef->chan = ieee80211_get_channel_khz(&rdev->wiphy, control_freq);
2816 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
2817 	chandef->center_freq1 = KHZ_TO_MHZ(control_freq);
2818 	chandef->freq1_offset = control_freq % 1000;
2819 	chandef->center_freq2 = 0;
2820 
2821 	/* Primary channel not allowed */
2822 	if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED) {
2823 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
2824 				    "Channel is disabled");
2825 		return -EINVAL;
2826 	}
2827 
2828 	if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2829 		enum nl80211_channel_type chantype;
2830 
2831 		chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2832 
2833 		switch (chantype) {
2834 		case NL80211_CHAN_NO_HT:
2835 		case NL80211_CHAN_HT20:
2836 		case NL80211_CHAN_HT40PLUS:
2837 		case NL80211_CHAN_HT40MINUS:
2838 			cfg80211_chandef_create(chandef, chandef->chan,
2839 						chantype);
2840 			/* user input for center_freq is incorrect */
2841 			if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
2842 			    chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
2843 				NL_SET_ERR_MSG_ATTR(extack,
2844 						    attrs[NL80211_ATTR_CENTER_FREQ1],
2845 						    "bad center frequency 1");
2846 				return -EINVAL;
2847 			}
2848 			/* center_freq2 must be zero */
2849 			if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
2850 			    nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
2851 				NL_SET_ERR_MSG_ATTR(extack,
2852 						    attrs[NL80211_ATTR_CENTER_FREQ2],
2853 						    "center frequency 2 can't be used");
2854 				return -EINVAL;
2855 			}
2856 			break;
2857 		default:
2858 			NL_SET_ERR_MSG_ATTR(extack,
2859 					    attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
2860 					    "invalid channel type");
2861 			return -EINVAL;
2862 		}
2863 	} else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
2864 		chandef->width =
2865 			nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
2866 		if (attrs[NL80211_ATTR_CENTER_FREQ1]) {
2867 			chandef->center_freq1 =
2868 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
2869 			if (attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET])
2870 				chandef->freq1_offset = nla_get_u32(
2871 				      attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET]);
2872 			else
2873 				chandef->freq1_offset = 0;
2874 		}
2875 		if (attrs[NL80211_ATTR_CENTER_FREQ2])
2876 			chandef->center_freq2 =
2877 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
2878 	}
2879 
2880 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
2881 		chandef->edmg.channels =
2882 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
2883 
2884 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
2885 			chandef->edmg.bw_config =
2886 		     nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
2887 	} else {
2888 		chandef->edmg.bw_config = 0;
2889 		chandef->edmg.channels = 0;
2890 	}
2891 
2892 	if (!cfg80211_chandef_valid(chandef)) {
2893 		NL_SET_ERR_MSG(extack, "invalid channel definition");
2894 		return -EINVAL;
2895 	}
2896 
2897 	if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
2898 				     IEEE80211_CHAN_DISABLED)) {
2899 		NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
2900 		return -EINVAL;
2901 	}
2902 
2903 	if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
2904 	     chandef->width == NL80211_CHAN_WIDTH_10) &&
2905 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
2906 		NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
2907 		return -EINVAL;
2908 	}
2909 
2910 	return 0;
2911 }
2912 
2913 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
2914 				 struct net_device *dev,
2915 				 struct genl_info *info)
2916 {
2917 	struct cfg80211_chan_def chandef;
2918 	int result;
2919 	enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
2920 	struct wireless_dev *wdev = NULL;
2921 
2922 	if (dev)
2923 		wdev = dev->ieee80211_ptr;
2924 	if (!nl80211_can_set_dev_channel(wdev))
2925 		return -EOPNOTSUPP;
2926 	if (wdev)
2927 		iftype = wdev->iftype;
2928 
2929 	result = nl80211_parse_chandef(rdev, info, &chandef);
2930 	if (result)
2931 		return result;
2932 
2933 	switch (iftype) {
2934 	case NL80211_IFTYPE_AP:
2935 	case NL80211_IFTYPE_P2P_GO:
2936 		if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
2937 						   iftype)) {
2938 			result = -EINVAL;
2939 			break;
2940 		}
2941 		if (wdev->beacon_interval) {
2942 			if (!dev || !rdev->ops->set_ap_chanwidth ||
2943 			    !(rdev->wiphy.features &
2944 			      NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
2945 				result = -EBUSY;
2946 				break;
2947 			}
2948 
2949 			/* Only allow dynamic channel width changes */
2950 			if (chandef.chan != wdev->preset_chandef.chan) {
2951 				result = -EBUSY;
2952 				break;
2953 			}
2954 			result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
2955 			if (result)
2956 				break;
2957 		}
2958 		wdev->preset_chandef = chandef;
2959 		result = 0;
2960 		break;
2961 	case NL80211_IFTYPE_MESH_POINT:
2962 		result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
2963 		break;
2964 	case NL80211_IFTYPE_MONITOR:
2965 		result = cfg80211_set_monitor_channel(rdev, &chandef);
2966 		break;
2967 	default:
2968 		result = -EINVAL;
2969 	}
2970 
2971 	return result;
2972 }
2973 
2974 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2975 {
2976 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2977 	struct net_device *netdev = info->user_ptr[1];
2978 
2979 	return __nl80211_set_channel(rdev, netdev, info);
2980 }
2981 
2982 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2983 {
2984 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2985 	struct net_device *dev = info->user_ptr[1];
2986 	struct wireless_dev *wdev = dev->ieee80211_ptr;
2987 	const u8 *bssid;
2988 
2989 	if (!info->attrs[NL80211_ATTR_MAC])
2990 		return -EINVAL;
2991 
2992 	if (netif_running(dev))
2993 		return -EBUSY;
2994 
2995 	if (!rdev->ops->set_wds_peer)
2996 		return -EOPNOTSUPP;
2997 
2998 	if (wdev->iftype != NL80211_IFTYPE_WDS)
2999 		return -EOPNOTSUPP;
3000 
3001 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3002 	return rdev_set_wds_peer(rdev, dev, bssid);
3003 }
3004 
3005 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
3006 {
3007 	struct cfg80211_registered_device *rdev;
3008 	struct net_device *netdev = NULL;
3009 	struct wireless_dev *wdev;
3010 	int result = 0, rem_txq_params = 0;
3011 	struct nlattr *nl_txq_params;
3012 	u32 changed;
3013 	u8 retry_short = 0, retry_long = 0;
3014 	u32 frag_threshold = 0, rts_threshold = 0;
3015 	u8 coverage_class = 0;
3016 	u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
3017 
3018 	ASSERT_RTNL();
3019 
3020 	/*
3021 	 * Try to find the wiphy and netdev. Normally this
3022 	 * function shouldn't need the netdev, but this is
3023 	 * done for backward compatibility -- previously
3024 	 * setting the channel was done per wiphy, but now
3025 	 * it is per netdev. Previous userland like hostapd
3026 	 * also passed a netdev to set_wiphy, so that it is
3027 	 * possible to let that go to the right netdev!
3028 	 */
3029 
3030 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
3031 		int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
3032 
3033 		netdev = __dev_get_by_index(genl_info_net(info), ifindex);
3034 		if (netdev && netdev->ieee80211_ptr)
3035 			rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
3036 		else
3037 			netdev = NULL;
3038 	}
3039 
3040 	if (!netdev) {
3041 		rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
3042 						  info->attrs);
3043 		if (IS_ERR(rdev))
3044 			return PTR_ERR(rdev);
3045 		wdev = NULL;
3046 		netdev = NULL;
3047 		result = 0;
3048 	} else
3049 		wdev = netdev->ieee80211_ptr;
3050 
3051 	/*
3052 	 * end workaround code, by now the rdev is available
3053 	 * and locked, and wdev may or may not be NULL.
3054 	 */
3055 
3056 	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
3057 		result = cfg80211_dev_rename(
3058 			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
3059 
3060 	if (result)
3061 		return result;
3062 
3063 	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
3064 		struct ieee80211_txq_params txq_params;
3065 		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
3066 
3067 		if (!rdev->ops->set_txq_params)
3068 			return -EOPNOTSUPP;
3069 
3070 		if (!netdev)
3071 			return -EINVAL;
3072 
3073 		if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3074 		    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3075 			return -EINVAL;
3076 
3077 		if (!netif_running(netdev))
3078 			return -ENETDOWN;
3079 
3080 		nla_for_each_nested(nl_txq_params,
3081 				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
3082 				    rem_txq_params) {
3083 			result = nla_parse_nested_deprecated(tb,
3084 							     NL80211_TXQ_ATTR_MAX,
3085 							     nl_txq_params,
3086 							     txq_params_policy,
3087 							     info->extack);
3088 			if (result)
3089 				return result;
3090 			result = parse_txq_params(tb, &txq_params);
3091 			if (result)
3092 				return result;
3093 
3094 			result = rdev_set_txq_params(rdev, netdev,
3095 						     &txq_params);
3096 			if (result)
3097 				return result;
3098 		}
3099 	}
3100 
3101 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3102 		result = __nl80211_set_channel(
3103 			rdev,
3104 			nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
3105 			info);
3106 		if (result)
3107 			return result;
3108 	}
3109 
3110 	if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
3111 		struct wireless_dev *txp_wdev = wdev;
3112 		enum nl80211_tx_power_setting type;
3113 		int idx, mbm = 0;
3114 
3115 		if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
3116 			txp_wdev = NULL;
3117 
3118 		if (!rdev->ops->set_tx_power)
3119 			return -EOPNOTSUPP;
3120 
3121 		idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
3122 		type = nla_get_u32(info->attrs[idx]);
3123 
3124 		if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
3125 		    (type != NL80211_TX_POWER_AUTOMATIC))
3126 			return -EINVAL;
3127 
3128 		if (type != NL80211_TX_POWER_AUTOMATIC) {
3129 			idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
3130 			mbm = nla_get_u32(info->attrs[idx]);
3131 		}
3132 
3133 		result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
3134 		if (result)
3135 			return result;
3136 	}
3137 
3138 	if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
3139 	    info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
3140 		u32 tx_ant, rx_ant;
3141 
3142 		if ((!rdev->wiphy.available_antennas_tx &&
3143 		     !rdev->wiphy.available_antennas_rx) ||
3144 		    !rdev->ops->set_antenna)
3145 			return -EOPNOTSUPP;
3146 
3147 		tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
3148 		rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
3149 
3150 		/* reject antenna configurations which don't match the
3151 		 * available antenna masks, except for the "all" mask */
3152 		if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
3153 		    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
3154 			return -EINVAL;
3155 
3156 		tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
3157 		rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
3158 
3159 		result = rdev_set_antenna(rdev, tx_ant, rx_ant);
3160 		if (result)
3161 			return result;
3162 	}
3163 
3164 	changed = 0;
3165 
3166 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
3167 		retry_short = nla_get_u8(
3168 			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
3169 
3170 		changed |= WIPHY_PARAM_RETRY_SHORT;
3171 	}
3172 
3173 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
3174 		retry_long = nla_get_u8(
3175 			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
3176 
3177 		changed |= WIPHY_PARAM_RETRY_LONG;
3178 	}
3179 
3180 	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
3181 		frag_threshold = nla_get_u32(
3182 			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
3183 		if (frag_threshold < 256)
3184 			return -EINVAL;
3185 
3186 		if (frag_threshold != (u32) -1) {
3187 			/*
3188 			 * Fragments (apart from the last one) are required to
3189 			 * have even length. Make the fragmentation code
3190 			 * simpler by stripping LSB should someone try to use
3191 			 * odd threshold value.
3192 			 */
3193 			frag_threshold &= ~0x1;
3194 		}
3195 		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
3196 	}
3197 
3198 	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
3199 		rts_threshold = nla_get_u32(
3200 			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
3201 		changed |= WIPHY_PARAM_RTS_THRESHOLD;
3202 	}
3203 
3204 	if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
3205 		if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
3206 			return -EINVAL;
3207 
3208 		coverage_class = nla_get_u8(
3209 			info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
3210 		changed |= WIPHY_PARAM_COVERAGE_CLASS;
3211 	}
3212 
3213 	if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3214 		if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
3215 			return -EOPNOTSUPP;
3216 
3217 		changed |= WIPHY_PARAM_DYN_ACK;
3218 	}
3219 
3220 	if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3221 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3222 					     NL80211_EXT_FEATURE_TXQS))
3223 			return -EOPNOTSUPP;
3224 		txq_limit = nla_get_u32(
3225 			info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3226 		changed |= WIPHY_PARAM_TXQ_LIMIT;
3227 	}
3228 
3229 	if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3230 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3231 					     NL80211_EXT_FEATURE_TXQS))
3232 			return -EOPNOTSUPP;
3233 		txq_memory_limit = nla_get_u32(
3234 			info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3235 		changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3236 	}
3237 
3238 	if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3239 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3240 					     NL80211_EXT_FEATURE_TXQS))
3241 			return -EOPNOTSUPP;
3242 		txq_quantum = nla_get_u32(
3243 			info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3244 		changed |= WIPHY_PARAM_TXQ_QUANTUM;
3245 	}
3246 
3247 	if (changed) {
3248 		u8 old_retry_short, old_retry_long;
3249 		u32 old_frag_threshold, old_rts_threshold;
3250 		u8 old_coverage_class;
3251 		u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3252 
3253 		if (!rdev->ops->set_wiphy_params)
3254 			return -EOPNOTSUPP;
3255 
3256 		old_retry_short = rdev->wiphy.retry_short;
3257 		old_retry_long = rdev->wiphy.retry_long;
3258 		old_frag_threshold = rdev->wiphy.frag_threshold;
3259 		old_rts_threshold = rdev->wiphy.rts_threshold;
3260 		old_coverage_class = rdev->wiphy.coverage_class;
3261 		old_txq_limit = rdev->wiphy.txq_limit;
3262 		old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3263 		old_txq_quantum = rdev->wiphy.txq_quantum;
3264 
3265 		if (changed & WIPHY_PARAM_RETRY_SHORT)
3266 			rdev->wiphy.retry_short = retry_short;
3267 		if (changed & WIPHY_PARAM_RETRY_LONG)
3268 			rdev->wiphy.retry_long = retry_long;
3269 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3270 			rdev->wiphy.frag_threshold = frag_threshold;
3271 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3272 			rdev->wiphy.rts_threshold = rts_threshold;
3273 		if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3274 			rdev->wiphy.coverage_class = coverage_class;
3275 		if (changed & WIPHY_PARAM_TXQ_LIMIT)
3276 			rdev->wiphy.txq_limit = txq_limit;
3277 		if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3278 			rdev->wiphy.txq_memory_limit = txq_memory_limit;
3279 		if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3280 			rdev->wiphy.txq_quantum = txq_quantum;
3281 
3282 		result = rdev_set_wiphy_params(rdev, changed);
3283 		if (result) {
3284 			rdev->wiphy.retry_short = old_retry_short;
3285 			rdev->wiphy.retry_long = old_retry_long;
3286 			rdev->wiphy.frag_threshold = old_frag_threshold;
3287 			rdev->wiphy.rts_threshold = old_rts_threshold;
3288 			rdev->wiphy.coverage_class = old_coverage_class;
3289 			rdev->wiphy.txq_limit = old_txq_limit;
3290 			rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3291 			rdev->wiphy.txq_quantum = old_txq_quantum;
3292 			return result;
3293 		}
3294 	}
3295 	return 0;
3296 }
3297 
3298 static int nl80211_send_chandef(struct sk_buff *msg,
3299 				const struct cfg80211_chan_def *chandef)
3300 {
3301 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3302 		return -EINVAL;
3303 
3304 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3305 			chandef->chan->center_freq))
3306 		return -ENOBUFS;
3307 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
3308 			chandef->chan->freq_offset))
3309 		return -ENOBUFS;
3310 	switch (chandef->width) {
3311 	case NL80211_CHAN_WIDTH_20_NOHT:
3312 	case NL80211_CHAN_WIDTH_20:
3313 	case NL80211_CHAN_WIDTH_40:
3314 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3315 				cfg80211_get_chandef_type(chandef)))
3316 			return -ENOBUFS;
3317 		break;
3318 	default:
3319 		break;
3320 	}
3321 	if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3322 		return -ENOBUFS;
3323 	if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3324 		return -ENOBUFS;
3325 	if (chandef->center_freq2 &&
3326 	    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3327 		return -ENOBUFS;
3328 	return 0;
3329 }
3330 
3331 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3332 			      struct cfg80211_registered_device *rdev,
3333 			      struct wireless_dev *wdev,
3334 			      enum nl80211_commands cmd)
3335 {
3336 	struct net_device *dev = wdev->netdev;
3337 	void *hdr;
3338 
3339 	WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3340 		cmd != NL80211_CMD_DEL_INTERFACE &&
3341 		cmd != NL80211_CMD_SET_INTERFACE);
3342 
3343 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3344 	if (!hdr)
3345 		return -1;
3346 
3347 	if (dev &&
3348 	    (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3349 	     nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3350 		goto nla_put_failure;
3351 
3352 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3353 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3354 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3355 			      NL80211_ATTR_PAD) ||
3356 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3357 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
3358 			rdev->devlist_generation ^
3359 			(cfg80211_rdev_list_generation << 2)) ||
3360 	    nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3361 		goto nla_put_failure;
3362 
3363 	if (rdev->ops->get_channel) {
3364 		int ret;
3365 		struct cfg80211_chan_def chandef = {};
3366 
3367 		ret = rdev_get_channel(rdev, wdev, &chandef);
3368 		if (ret == 0) {
3369 			if (nl80211_send_chandef(msg, &chandef))
3370 				goto nla_put_failure;
3371 		}
3372 	}
3373 
3374 	if (rdev->ops->get_tx_power) {
3375 		int dbm, ret;
3376 
3377 		ret = rdev_get_tx_power(rdev, wdev, &dbm);
3378 		if (ret == 0 &&
3379 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3380 				DBM_TO_MBM(dbm)))
3381 			goto nla_put_failure;
3382 	}
3383 
3384 	wdev_lock(wdev);
3385 	switch (wdev->iftype) {
3386 	case NL80211_IFTYPE_AP:
3387 		if (wdev->ssid_len &&
3388 		    nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
3389 			goto nla_put_failure_locked;
3390 		break;
3391 	case NL80211_IFTYPE_STATION:
3392 	case NL80211_IFTYPE_P2P_CLIENT:
3393 	case NL80211_IFTYPE_ADHOC: {
3394 		const u8 *ssid_ie;
3395 		if (!wdev->current_bss)
3396 			break;
3397 		rcu_read_lock();
3398 		ssid_ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
3399 					       WLAN_EID_SSID);
3400 		if (ssid_ie &&
3401 		    nla_put(msg, NL80211_ATTR_SSID, ssid_ie[1], ssid_ie + 2))
3402 			goto nla_put_failure_rcu_locked;
3403 		rcu_read_unlock();
3404 		break;
3405 		}
3406 	default:
3407 		/* nothing */
3408 		break;
3409 	}
3410 	wdev_unlock(wdev);
3411 
3412 	if (rdev->ops->get_txq_stats) {
3413 		struct cfg80211_txq_stats txqstats = {};
3414 		int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3415 
3416 		if (ret == 0 &&
3417 		    !nl80211_put_txq_stats(msg, &txqstats,
3418 					   NL80211_ATTR_TXQ_STATS))
3419 			goto nla_put_failure;
3420 	}
3421 
3422 	genlmsg_end(msg, hdr);
3423 	return 0;
3424 
3425  nla_put_failure_rcu_locked:
3426 	rcu_read_unlock();
3427  nla_put_failure_locked:
3428 	wdev_unlock(wdev);
3429  nla_put_failure:
3430 	genlmsg_cancel(msg, hdr);
3431 	return -EMSGSIZE;
3432 }
3433 
3434 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3435 {
3436 	int wp_idx = 0;
3437 	int if_idx = 0;
3438 	int wp_start = cb->args[0];
3439 	int if_start = cb->args[1];
3440 	int filter_wiphy = -1;
3441 	struct cfg80211_registered_device *rdev;
3442 	struct wireless_dev *wdev;
3443 	int ret;
3444 
3445 	rtnl_lock();
3446 	if (!cb->args[2]) {
3447 		struct nl80211_dump_wiphy_state state = {
3448 			.filter_wiphy = -1,
3449 		};
3450 
3451 		ret = nl80211_dump_wiphy_parse(skb, cb, &state);
3452 		if (ret)
3453 			goto out_unlock;
3454 
3455 		filter_wiphy = state.filter_wiphy;
3456 
3457 		/*
3458 		 * if filtering, set cb->args[2] to +1 since 0 is the default
3459 		 * value needed to determine that parsing is necessary.
3460 		 */
3461 		if (filter_wiphy >= 0)
3462 			cb->args[2] = filter_wiphy + 1;
3463 		else
3464 			cb->args[2] = -1;
3465 	} else if (cb->args[2] > 0) {
3466 		filter_wiphy = cb->args[2] - 1;
3467 	}
3468 
3469 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3470 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3471 			continue;
3472 		if (wp_idx < wp_start) {
3473 			wp_idx++;
3474 			continue;
3475 		}
3476 
3477 		if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
3478 			continue;
3479 
3480 		if_idx = 0;
3481 
3482 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3483 			if (if_idx < if_start) {
3484 				if_idx++;
3485 				continue;
3486 			}
3487 			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
3488 					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
3489 					       rdev, wdev,
3490 					       NL80211_CMD_NEW_INTERFACE) < 0) {
3491 				goto out;
3492 			}
3493 			if_idx++;
3494 		}
3495 
3496 		wp_idx++;
3497 	}
3498  out:
3499 	cb->args[0] = wp_idx;
3500 	cb->args[1] = if_idx;
3501 
3502 	ret = skb->len;
3503  out_unlock:
3504 	rtnl_unlock();
3505 
3506 	return ret;
3507 }
3508 
3509 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
3510 {
3511 	struct sk_buff *msg;
3512 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3513 	struct wireless_dev *wdev = info->user_ptr[1];
3514 
3515 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3516 	if (!msg)
3517 		return -ENOMEM;
3518 
3519 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3520 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3521 		nlmsg_free(msg);
3522 		return -ENOBUFS;
3523 	}
3524 
3525 	return genlmsg_reply(msg, info);
3526 }
3527 
3528 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
3529 	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
3530 	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
3531 	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
3532 	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
3533 	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
3534 	[NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
3535 };
3536 
3537 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
3538 {
3539 	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
3540 	int flag;
3541 
3542 	*mntrflags = 0;
3543 
3544 	if (!nla)
3545 		return -EINVAL;
3546 
3547 	if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
3548 		return -EINVAL;
3549 
3550 	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
3551 		if (flags[flag])
3552 			*mntrflags |= (1<<flag);
3553 
3554 	*mntrflags |= MONITOR_FLAG_CHANGED;
3555 
3556 	return 0;
3557 }
3558 
3559 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
3560 				     enum nl80211_iftype type,
3561 				     struct genl_info *info,
3562 				     struct vif_params *params)
3563 {
3564 	bool change = false;
3565 	int err;
3566 
3567 	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
3568 		if (type != NL80211_IFTYPE_MONITOR)
3569 			return -EINVAL;
3570 
3571 		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
3572 					  &params->flags);
3573 		if (err)
3574 			return err;
3575 
3576 		change = true;
3577 	}
3578 
3579 	if (params->flags & MONITOR_FLAG_ACTIVE &&
3580 	    !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
3581 		return -EOPNOTSUPP;
3582 
3583 	if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
3584 		const u8 *mumimo_groups;
3585 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3586 
3587 		if (type != NL80211_IFTYPE_MONITOR)
3588 			return -EINVAL;
3589 
3590 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3591 			return -EOPNOTSUPP;
3592 
3593 		mumimo_groups =
3594 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
3595 
3596 		/* bits 0 and 63 are reserved and must be zero */
3597 		if ((mumimo_groups[0] & BIT(0)) ||
3598 		    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
3599 			return -EINVAL;
3600 
3601 		params->vht_mumimo_groups = mumimo_groups;
3602 		change = true;
3603 	}
3604 
3605 	if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
3606 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3607 
3608 		if (type != NL80211_IFTYPE_MONITOR)
3609 			return -EINVAL;
3610 
3611 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3612 			return -EOPNOTSUPP;
3613 
3614 		params->vht_mumimo_follow_addr =
3615 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
3616 		change = true;
3617 	}
3618 
3619 	return change ? 1 : 0;
3620 }
3621 
3622 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
3623 			       struct net_device *netdev, u8 use_4addr,
3624 			       enum nl80211_iftype iftype)
3625 {
3626 	if (!use_4addr) {
3627 		if (netdev && netif_is_bridge_port(netdev))
3628 			return -EBUSY;
3629 		return 0;
3630 	}
3631 
3632 	switch (iftype) {
3633 	case NL80211_IFTYPE_AP_VLAN:
3634 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
3635 			return 0;
3636 		break;
3637 	case NL80211_IFTYPE_STATION:
3638 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
3639 			return 0;
3640 		break;
3641 	default:
3642 		break;
3643 	}
3644 
3645 	return -EOPNOTSUPP;
3646 }
3647 
3648 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
3649 {
3650 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3651 	struct vif_params params;
3652 	int err;
3653 	enum nl80211_iftype otype, ntype;
3654 	struct net_device *dev = info->user_ptr[1];
3655 	bool change = false;
3656 
3657 	memset(&params, 0, sizeof(params));
3658 
3659 	otype = ntype = dev->ieee80211_ptr->iftype;
3660 
3661 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
3662 		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3663 		if (otype != ntype)
3664 			change = true;
3665 	}
3666 
3667 	if (info->attrs[NL80211_ATTR_MESH_ID]) {
3668 		struct wireless_dev *wdev = dev->ieee80211_ptr;
3669 
3670 		if (ntype != NL80211_IFTYPE_MESH_POINT)
3671 			return -EINVAL;
3672 		if (netif_running(dev))
3673 			return -EBUSY;
3674 
3675 		wdev_lock(wdev);
3676 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3677 			     IEEE80211_MAX_MESH_ID_LEN);
3678 		wdev->mesh_id_up_len =
3679 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3680 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3681 		       wdev->mesh_id_up_len);
3682 		wdev_unlock(wdev);
3683 	}
3684 
3685 	if (info->attrs[NL80211_ATTR_4ADDR]) {
3686 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3687 		change = true;
3688 		err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
3689 		if (err)
3690 			return err;
3691 	} else {
3692 		params.use_4addr = -1;
3693 	}
3694 
3695 	err = nl80211_parse_mon_options(rdev, ntype, info, &params);
3696 	if (err < 0)
3697 		return err;
3698 	if (err > 0)
3699 		change = true;
3700 
3701 	if (change)
3702 		err = cfg80211_change_iface(rdev, dev, ntype, &params);
3703 	else
3704 		err = 0;
3705 
3706 	if (!err && params.use_4addr != -1)
3707 		dev->ieee80211_ptr->use_4addr = params.use_4addr;
3708 
3709 	if (change && !err) {
3710 		struct wireless_dev *wdev = dev->ieee80211_ptr;
3711 
3712 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
3713 	}
3714 
3715 	return err;
3716 }
3717 
3718 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
3719 {
3720 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3721 	struct vif_params params;
3722 	struct wireless_dev *wdev;
3723 	struct sk_buff *msg;
3724 	int err;
3725 	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
3726 
3727 	/* to avoid failing a new interface creation due to pending removal */
3728 	cfg80211_destroy_ifaces(rdev);
3729 
3730 	memset(&params, 0, sizeof(params));
3731 
3732 	if (!info->attrs[NL80211_ATTR_IFNAME])
3733 		return -EINVAL;
3734 
3735 	if (info->attrs[NL80211_ATTR_IFTYPE])
3736 		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3737 
3738 	if (!rdev->ops->add_virtual_intf)
3739 		return -EOPNOTSUPP;
3740 
3741 	if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
3742 	     rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
3743 	    info->attrs[NL80211_ATTR_MAC]) {
3744 		nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
3745 			   ETH_ALEN);
3746 		if (!is_valid_ether_addr(params.macaddr))
3747 			return -EADDRNOTAVAIL;
3748 	}
3749 
3750 	if (info->attrs[NL80211_ATTR_4ADDR]) {
3751 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3752 		err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
3753 		if (err)
3754 			return err;
3755 	}
3756 
3757 	if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
3758 		return -EOPNOTSUPP;
3759 
3760 	err = nl80211_parse_mon_options(rdev, type, info, &params);
3761 	if (err < 0)
3762 		return err;
3763 
3764 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3765 	if (!msg)
3766 		return -ENOMEM;
3767 
3768 	wdev = rdev_add_virtual_intf(rdev,
3769 				nla_data(info->attrs[NL80211_ATTR_IFNAME]),
3770 				NET_NAME_USER, type, &params);
3771 	if (WARN_ON(!wdev)) {
3772 		nlmsg_free(msg);
3773 		return -EPROTO;
3774 	} else if (IS_ERR(wdev)) {
3775 		nlmsg_free(msg);
3776 		return PTR_ERR(wdev);
3777 	}
3778 
3779 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
3780 		wdev->owner_nlportid = info->snd_portid;
3781 
3782 	switch (type) {
3783 	case NL80211_IFTYPE_MESH_POINT:
3784 		if (!info->attrs[NL80211_ATTR_MESH_ID])
3785 			break;
3786 		wdev_lock(wdev);
3787 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3788 			     IEEE80211_MAX_MESH_ID_LEN);
3789 		wdev->mesh_id_up_len =
3790 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3791 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3792 		       wdev->mesh_id_up_len);
3793 		wdev_unlock(wdev);
3794 		break;
3795 	case NL80211_IFTYPE_NAN:
3796 	case NL80211_IFTYPE_P2P_DEVICE:
3797 		/*
3798 		 * P2P Device and NAN do not have a netdev, so don't go
3799 		 * through the netdev notifier and must be added here
3800 		 */
3801 		cfg80211_init_wdev(rdev, wdev);
3802 		break;
3803 	default:
3804 		break;
3805 	}
3806 
3807 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3808 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3809 		nlmsg_free(msg);
3810 		return -ENOBUFS;
3811 	}
3812 
3813 	return genlmsg_reply(msg, info);
3814 }
3815 
3816 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
3817 {
3818 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3819 	struct wireless_dev *wdev = info->user_ptr[1];
3820 
3821 	if (!rdev->ops->del_virtual_intf)
3822 		return -EOPNOTSUPP;
3823 
3824 	/*
3825 	 * If we remove a wireless device without a netdev then clear
3826 	 * user_ptr[1] so that nl80211_post_doit won't dereference it
3827 	 * to check if it needs to do dev_put(). Otherwise it crashes
3828 	 * since the wdev has been freed, unlike with a netdev where
3829 	 * we need the dev_put() for the netdev to really be freed.
3830 	 */
3831 	if (!wdev->netdev)
3832 		info->user_ptr[1] = NULL;
3833 
3834 	return rdev_del_virtual_intf(rdev, wdev);
3835 }
3836 
3837 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
3838 {
3839 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3840 	struct net_device *dev = info->user_ptr[1];
3841 	u16 noack_map;
3842 
3843 	if (!info->attrs[NL80211_ATTR_NOACK_MAP])
3844 		return -EINVAL;
3845 
3846 	if (!rdev->ops->set_noack_map)
3847 		return -EOPNOTSUPP;
3848 
3849 	noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
3850 
3851 	return rdev_set_noack_map(rdev, dev, noack_map);
3852 }
3853 
3854 struct get_key_cookie {
3855 	struct sk_buff *msg;
3856 	int error;
3857 	int idx;
3858 };
3859 
3860 static void get_key_callback(void *c, struct key_params *params)
3861 {
3862 	struct nlattr *key;
3863 	struct get_key_cookie *cookie = c;
3864 
3865 	if ((params->key &&
3866 	     nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
3867 		     params->key_len, params->key)) ||
3868 	    (params->seq &&
3869 	     nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
3870 		     params->seq_len, params->seq)) ||
3871 	    (params->cipher &&
3872 	     nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
3873 			 params->cipher)))
3874 		goto nla_put_failure;
3875 
3876 	key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
3877 	if (!key)
3878 		goto nla_put_failure;
3879 
3880 	if ((params->key &&
3881 	     nla_put(cookie->msg, NL80211_KEY_DATA,
3882 		     params->key_len, params->key)) ||
3883 	    (params->seq &&
3884 	     nla_put(cookie->msg, NL80211_KEY_SEQ,
3885 		     params->seq_len, params->seq)) ||
3886 	    (params->cipher &&
3887 	     nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
3888 			 params->cipher)))
3889 		goto nla_put_failure;
3890 
3891 	if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
3892 		goto nla_put_failure;
3893 
3894 	nla_nest_end(cookie->msg, key);
3895 
3896 	return;
3897  nla_put_failure:
3898 	cookie->error = 1;
3899 }
3900 
3901 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
3902 {
3903 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3904 	int err;
3905 	struct net_device *dev = info->user_ptr[1];
3906 	u8 key_idx = 0;
3907 	const u8 *mac_addr = NULL;
3908 	bool pairwise;
3909 	struct get_key_cookie cookie = {
3910 		.error = 0,
3911 	};
3912 	void *hdr;
3913 	struct sk_buff *msg;
3914 	bool bigtk_support = false;
3915 
3916 	if (wiphy_ext_feature_isset(&rdev->wiphy,
3917 				    NL80211_EXT_FEATURE_BEACON_PROTECTION))
3918 		bigtk_support = true;
3919 
3920 	if ((dev->ieee80211_ptr->iftype == NL80211_IFTYPE_STATION ||
3921 	     dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
3922 	    wiphy_ext_feature_isset(&rdev->wiphy,
3923 				    NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
3924 		bigtk_support = true;
3925 
3926 	if (info->attrs[NL80211_ATTR_KEY_IDX]) {
3927 		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
3928 
3929 		if (key_idx >= 6 && key_idx <= 7 && !bigtk_support) {
3930 			GENL_SET_ERR_MSG(info, "BIGTK not supported");
3931 			return -EINVAL;
3932 		}
3933 	}
3934 
3935 	if (info->attrs[NL80211_ATTR_MAC])
3936 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3937 
3938 	pairwise = !!mac_addr;
3939 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
3940 		u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
3941 
3942 		if (kt != NL80211_KEYTYPE_GROUP &&
3943 		    kt != NL80211_KEYTYPE_PAIRWISE)
3944 			return -EINVAL;
3945 		pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
3946 	}
3947 
3948 	if (!rdev->ops->get_key)
3949 		return -EOPNOTSUPP;
3950 
3951 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3952 		return -ENOENT;
3953 
3954 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3955 	if (!msg)
3956 		return -ENOMEM;
3957 
3958 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
3959 			     NL80211_CMD_NEW_KEY);
3960 	if (!hdr)
3961 		goto nla_put_failure;
3962 
3963 	cookie.msg = msg;
3964 	cookie.idx = key_idx;
3965 
3966 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3967 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
3968 		goto nla_put_failure;
3969 	if (mac_addr &&
3970 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
3971 		goto nla_put_failure;
3972 
3973 	err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
3974 			   get_key_callback);
3975 
3976 	if (err)
3977 		goto free_msg;
3978 
3979 	if (cookie.error)
3980 		goto nla_put_failure;
3981 
3982 	genlmsg_end(msg, hdr);
3983 	return genlmsg_reply(msg, info);
3984 
3985  nla_put_failure:
3986 	err = -ENOBUFS;
3987  free_msg:
3988 	nlmsg_free(msg);
3989 	return err;
3990 }
3991 
3992 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
3993 {
3994 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3995 	struct key_parse key;
3996 	int err;
3997 	struct net_device *dev = info->user_ptr[1];
3998 
3999 	err = nl80211_parse_key(info, &key);
4000 	if (err)
4001 		return err;
4002 
4003 	if (key.idx < 0)
4004 		return -EINVAL;
4005 
4006 	/* Only support setting default key and
4007 	 * Extended Key ID action NL80211_KEY_SET_TX.
4008 	 */
4009 	if (!key.def && !key.defmgmt && !key.defbeacon &&
4010 	    !(key.p.mode == NL80211_KEY_SET_TX))
4011 		return -EINVAL;
4012 
4013 	wdev_lock(dev->ieee80211_ptr);
4014 
4015 	if (key.def) {
4016 		if (!rdev->ops->set_default_key) {
4017 			err = -EOPNOTSUPP;
4018 			goto out;
4019 		}
4020 
4021 		err = nl80211_key_allowed(dev->ieee80211_ptr);
4022 		if (err)
4023 			goto out;
4024 
4025 		err = rdev_set_default_key(rdev, dev, key.idx,
4026 						 key.def_uni, key.def_multi);
4027 
4028 		if (err)
4029 			goto out;
4030 
4031 #ifdef CONFIG_CFG80211_WEXT
4032 		dev->ieee80211_ptr->wext.default_key = key.idx;
4033 #endif
4034 	} else if (key.defmgmt) {
4035 		if (key.def_uni || !key.def_multi) {
4036 			err = -EINVAL;
4037 			goto out;
4038 		}
4039 
4040 		if (!rdev->ops->set_default_mgmt_key) {
4041 			err = -EOPNOTSUPP;
4042 			goto out;
4043 		}
4044 
4045 		err = nl80211_key_allowed(dev->ieee80211_ptr);
4046 		if (err)
4047 			goto out;
4048 
4049 		err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
4050 		if (err)
4051 			goto out;
4052 
4053 #ifdef CONFIG_CFG80211_WEXT
4054 		dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
4055 #endif
4056 	} else if (key.defbeacon) {
4057 		if (key.def_uni || !key.def_multi) {
4058 			err = -EINVAL;
4059 			goto out;
4060 		}
4061 
4062 		if (!rdev->ops->set_default_beacon_key) {
4063 			err = -EOPNOTSUPP;
4064 			goto out;
4065 		}
4066 
4067 		err = nl80211_key_allowed(dev->ieee80211_ptr);
4068 		if (err)
4069 			goto out;
4070 
4071 		err = rdev_set_default_beacon_key(rdev, dev, key.idx);
4072 		if (err)
4073 			goto out;
4074 	} else if (key.p.mode == NL80211_KEY_SET_TX &&
4075 		   wiphy_ext_feature_isset(&rdev->wiphy,
4076 					   NL80211_EXT_FEATURE_EXT_KEY_ID)) {
4077 		u8 *mac_addr = NULL;
4078 
4079 		if (info->attrs[NL80211_ATTR_MAC])
4080 			mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4081 
4082 		if (!mac_addr || key.idx < 0 || key.idx > 1) {
4083 			err = -EINVAL;
4084 			goto out;
4085 		}
4086 
4087 		err = rdev_add_key(rdev, dev, key.idx,
4088 				   NL80211_KEYTYPE_PAIRWISE,
4089 				   mac_addr, &key.p);
4090 	} else {
4091 		err = -EINVAL;
4092 	}
4093  out:
4094 	wdev_unlock(dev->ieee80211_ptr);
4095 
4096 	return err;
4097 }
4098 
4099 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
4100 {
4101 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4102 	int err;
4103 	struct net_device *dev = info->user_ptr[1];
4104 	struct key_parse key;
4105 	const u8 *mac_addr = NULL;
4106 
4107 	err = nl80211_parse_key(info, &key);
4108 	if (err)
4109 		return err;
4110 
4111 	if (!key.p.key) {
4112 		GENL_SET_ERR_MSG(info, "no key");
4113 		return -EINVAL;
4114 	}
4115 
4116 	if (info->attrs[NL80211_ATTR_MAC])
4117 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4118 
4119 	if (key.type == -1) {
4120 		if (mac_addr)
4121 			key.type = NL80211_KEYTYPE_PAIRWISE;
4122 		else
4123 			key.type = NL80211_KEYTYPE_GROUP;
4124 	}
4125 
4126 	/* for now */
4127 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4128 	    key.type != NL80211_KEYTYPE_GROUP) {
4129 		GENL_SET_ERR_MSG(info, "key type not pairwise or group");
4130 		return -EINVAL;
4131 	}
4132 
4133 	if (key.type == NL80211_KEYTYPE_GROUP &&
4134 	    info->attrs[NL80211_ATTR_VLAN_ID])
4135 		key.p.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
4136 
4137 	if (!rdev->ops->add_key)
4138 		return -EOPNOTSUPP;
4139 
4140 	if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
4141 					   key.type == NL80211_KEYTYPE_PAIRWISE,
4142 					   mac_addr)) {
4143 		GENL_SET_ERR_MSG(info, "key setting validation failed");
4144 		return -EINVAL;
4145 	}
4146 
4147 	wdev_lock(dev->ieee80211_ptr);
4148 	err = nl80211_key_allowed(dev->ieee80211_ptr);
4149 	if (err)
4150 		GENL_SET_ERR_MSG(info, "key not allowed");
4151 	if (!err) {
4152 		err = rdev_add_key(rdev, dev, key.idx,
4153 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4154 				    mac_addr, &key.p);
4155 		if (err)
4156 			GENL_SET_ERR_MSG(info, "key addition failed");
4157 	}
4158 	wdev_unlock(dev->ieee80211_ptr);
4159 
4160 	return err;
4161 }
4162 
4163 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
4164 {
4165 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4166 	int err;
4167 	struct net_device *dev = info->user_ptr[1];
4168 	u8 *mac_addr = NULL;
4169 	struct key_parse key;
4170 
4171 	err = nl80211_parse_key(info, &key);
4172 	if (err)
4173 		return err;
4174 
4175 	if (info->attrs[NL80211_ATTR_MAC])
4176 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4177 
4178 	if (key.type == -1) {
4179 		if (mac_addr)
4180 			key.type = NL80211_KEYTYPE_PAIRWISE;
4181 		else
4182 			key.type = NL80211_KEYTYPE_GROUP;
4183 	}
4184 
4185 	/* for now */
4186 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4187 	    key.type != NL80211_KEYTYPE_GROUP)
4188 		return -EINVAL;
4189 
4190 	if (!rdev->ops->del_key)
4191 		return -EOPNOTSUPP;
4192 
4193 	wdev_lock(dev->ieee80211_ptr);
4194 	err = nl80211_key_allowed(dev->ieee80211_ptr);
4195 
4196 	if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
4197 	    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4198 		err = -ENOENT;
4199 
4200 	if (!err)
4201 		err = rdev_del_key(rdev, dev, key.idx,
4202 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4203 				   mac_addr);
4204 
4205 #ifdef CONFIG_CFG80211_WEXT
4206 	if (!err) {
4207 		if (key.idx == dev->ieee80211_ptr->wext.default_key)
4208 			dev->ieee80211_ptr->wext.default_key = -1;
4209 		else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
4210 			dev->ieee80211_ptr->wext.default_mgmt_key = -1;
4211 	}
4212 #endif
4213 	wdev_unlock(dev->ieee80211_ptr);
4214 
4215 	return err;
4216 }
4217 
4218 /* This function returns an error or the number of nested attributes */
4219 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
4220 {
4221 	struct nlattr *attr;
4222 	int n_entries = 0, tmp;
4223 
4224 	nla_for_each_nested(attr, nl_attr, tmp) {
4225 		if (nla_len(attr) != ETH_ALEN)
4226 			return -EINVAL;
4227 
4228 		n_entries++;
4229 	}
4230 
4231 	return n_entries;
4232 }
4233 
4234 /*
4235  * This function parses ACL information and allocates memory for ACL data.
4236  * On successful return, the calling function is responsible to free the
4237  * ACL buffer returned by this function.
4238  */
4239 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
4240 						struct genl_info *info)
4241 {
4242 	enum nl80211_acl_policy acl_policy;
4243 	struct nlattr *attr;
4244 	struct cfg80211_acl_data *acl;
4245 	int i = 0, n_entries, tmp;
4246 
4247 	if (!wiphy->max_acl_mac_addrs)
4248 		return ERR_PTR(-EOPNOTSUPP);
4249 
4250 	if (!info->attrs[NL80211_ATTR_ACL_POLICY])
4251 		return ERR_PTR(-EINVAL);
4252 
4253 	acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
4254 	if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
4255 	    acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
4256 		return ERR_PTR(-EINVAL);
4257 
4258 	if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
4259 		return ERR_PTR(-EINVAL);
4260 
4261 	n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
4262 	if (n_entries < 0)
4263 		return ERR_PTR(n_entries);
4264 
4265 	if (n_entries > wiphy->max_acl_mac_addrs)
4266 		return ERR_PTR(-ENOTSUPP);
4267 
4268 	acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4269 	if (!acl)
4270 		return ERR_PTR(-ENOMEM);
4271 
4272 	nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4273 		memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4274 		i++;
4275 	}
4276 
4277 	acl->n_acl_entries = n_entries;
4278 	acl->acl_policy = acl_policy;
4279 
4280 	return acl;
4281 }
4282 
4283 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4284 {
4285 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4286 	struct net_device *dev = info->user_ptr[1];
4287 	struct cfg80211_acl_data *acl;
4288 	int err;
4289 
4290 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4291 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4292 		return -EOPNOTSUPP;
4293 
4294 	if (!dev->ieee80211_ptr->beacon_interval)
4295 		return -EINVAL;
4296 
4297 	acl = parse_acl_data(&rdev->wiphy, info);
4298 	if (IS_ERR(acl))
4299 		return PTR_ERR(acl);
4300 
4301 	err = rdev_set_mac_acl(rdev, dev, acl);
4302 
4303 	kfree(acl);
4304 
4305 	return err;
4306 }
4307 
4308 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4309 			   u8 *rates, u8 rates_len)
4310 {
4311 	u8 i;
4312 	u32 mask = 0;
4313 
4314 	for (i = 0; i < rates_len; i++) {
4315 		int rate = (rates[i] & 0x7f) * 5;
4316 		int ridx;
4317 
4318 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4319 			struct ieee80211_rate *srate =
4320 				&sband->bitrates[ridx];
4321 			if (rate == srate->bitrate) {
4322 				mask |= 1 << ridx;
4323 				break;
4324 			}
4325 		}
4326 		if (ridx == sband->n_bitrates)
4327 			return 0; /* rate not found */
4328 	}
4329 
4330 	return mask;
4331 }
4332 
4333 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4334 			       u8 *rates, u8 rates_len,
4335 			       u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4336 {
4337 	u8 i;
4338 
4339 	memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4340 
4341 	for (i = 0; i < rates_len; i++) {
4342 		int ridx, rbit;
4343 
4344 		ridx = rates[i] / 8;
4345 		rbit = BIT(rates[i] % 8);
4346 
4347 		/* check validity */
4348 		if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4349 			return false;
4350 
4351 		/* check availability */
4352 		ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
4353 		if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
4354 			mcs[ridx] |= rbit;
4355 		else
4356 			return false;
4357 	}
4358 
4359 	return true;
4360 }
4361 
4362 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
4363 {
4364 	u16 mcs_mask = 0;
4365 
4366 	switch (vht_mcs_map) {
4367 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
4368 		break;
4369 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
4370 		mcs_mask = 0x00FF;
4371 		break;
4372 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
4373 		mcs_mask = 0x01FF;
4374 		break;
4375 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
4376 		mcs_mask = 0x03FF;
4377 		break;
4378 	default:
4379 		break;
4380 	}
4381 
4382 	return mcs_mask;
4383 }
4384 
4385 static void vht_build_mcs_mask(u16 vht_mcs_map,
4386 			       u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
4387 {
4388 	u8 nss;
4389 
4390 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
4391 		vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
4392 		vht_mcs_map >>= 2;
4393 	}
4394 }
4395 
4396 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
4397 			     struct nl80211_txrate_vht *txrate,
4398 			     u16 mcs[NL80211_VHT_NSS_MAX])
4399 {
4400 	u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4401 	u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
4402 	u8 i;
4403 
4404 	if (!sband->vht_cap.vht_supported)
4405 		return false;
4406 
4407 	memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
4408 
4409 	/* Build vht_mcs_mask from VHT capabilities */
4410 	vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
4411 
4412 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4413 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
4414 			mcs[i] = txrate->mcs[i];
4415 		else
4416 			return false;
4417 	}
4418 
4419 	return true;
4420 }
4421 
4422 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
4423 					 struct nlattr *attrs[],
4424 					 enum nl80211_attrs attr,
4425 					 struct cfg80211_bitrate_mask *mask)
4426 {
4427 	struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4428 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4429 	int rem, i;
4430 	struct nlattr *tx_rates;
4431 	struct ieee80211_supported_band *sband;
4432 	u16 vht_tx_mcs_map;
4433 
4434 	memset(mask, 0, sizeof(*mask));
4435 	/* Default to all rates enabled */
4436 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
4437 		sband = rdev->wiphy.bands[i];
4438 
4439 		if (!sband)
4440 			continue;
4441 
4442 		mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
4443 		memcpy(mask->control[i].ht_mcs,
4444 		       sband->ht_cap.mcs.rx_mask,
4445 		       sizeof(mask->control[i].ht_mcs));
4446 
4447 		if (!sband->vht_cap.vht_supported)
4448 			continue;
4449 
4450 		vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4451 		vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
4452 	}
4453 
4454 	/* if no rates are given set it back to the defaults */
4455 	if (!attrs[attr])
4456 		goto out;
4457 
4458 	/* The nested attribute uses enum nl80211_band as the index. This maps
4459 	 * directly to the enum nl80211_band values used in cfg80211.
4460 	 */
4461 	BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
4462 	nla_for_each_nested(tx_rates, attrs[attr], rem) {
4463 		enum nl80211_band band = nla_type(tx_rates);
4464 		int err;
4465 
4466 		if (band < 0 || band >= NUM_NL80211_BANDS)
4467 			return -EINVAL;
4468 		sband = rdev->wiphy.bands[band];
4469 		if (sband == NULL)
4470 			return -EINVAL;
4471 		err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
4472 						  tx_rates,
4473 						  nl80211_txattr_policy,
4474 						  info->extack);
4475 		if (err)
4476 			return err;
4477 		if (tb[NL80211_TXRATE_LEGACY]) {
4478 			mask->control[band].legacy = rateset_to_mask(
4479 				sband,
4480 				nla_data(tb[NL80211_TXRATE_LEGACY]),
4481 				nla_len(tb[NL80211_TXRATE_LEGACY]));
4482 			if ((mask->control[band].legacy == 0) &&
4483 			    nla_len(tb[NL80211_TXRATE_LEGACY]))
4484 				return -EINVAL;
4485 		}
4486 		if (tb[NL80211_TXRATE_HT]) {
4487 			if (!ht_rateset_to_mask(
4488 					sband,
4489 					nla_data(tb[NL80211_TXRATE_HT]),
4490 					nla_len(tb[NL80211_TXRATE_HT]),
4491 					mask->control[band].ht_mcs))
4492 				return -EINVAL;
4493 		}
4494 		if (tb[NL80211_TXRATE_VHT]) {
4495 			if (!vht_set_mcs_mask(
4496 					sband,
4497 					nla_data(tb[NL80211_TXRATE_VHT]),
4498 					mask->control[band].vht_mcs))
4499 				return -EINVAL;
4500 		}
4501 		if (tb[NL80211_TXRATE_GI]) {
4502 			mask->control[band].gi =
4503 				nla_get_u8(tb[NL80211_TXRATE_GI]);
4504 			if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
4505 				return -EINVAL;
4506 		}
4507 
4508 		if (mask->control[band].legacy == 0) {
4509 			/* don't allow empty legacy rates if HT or VHT
4510 			 * are not even supported.
4511 			 */
4512 			if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
4513 			      rdev->wiphy.bands[band]->vht_cap.vht_supported))
4514 				return -EINVAL;
4515 
4516 			for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4517 				if (mask->control[band].ht_mcs[i])
4518 					goto out;
4519 
4520 			for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4521 				if (mask->control[band].vht_mcs[i])
4522 					goto out;
4523 
4524 			/* legacy and mcs rates may not be both empty */
4525 			return -EINVAL;
4526 		}
4527 	}
4528 
4529 out:
4530 	return 0;
4531 }
4532 
4533 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
4534 				   enum nl80211_band band,
4535 				   struct cfg80211_bitrate_mask *beacon_rate)
4536 {
4537 	u32 count_ht, count_vht, i;
4538 	u32 rate = beacon_rate->control[band].legacy;
4539 
4540 	/* Allow only one rate */
4541 	if (hweight32(rate) > 1)
4542 		return -EINVAL;
4543 
4544 	count_ht = 0;
4545 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4546 		if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
4547 			return -EINVAL;
4548 		} else if (beacon_rate->control[band].ht_mcs[i]) {
4549 			count_ht++;
4550 			if (count_ht > 1)
4551 				return -EINVAL;
4552 		}
4553 		if (count_ht && rate)
4554 			return -EINVAL;
4555 	}
4556 
4557 	count_vht = 0;
4558 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4559 		if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
4560 			return -EINVAL;
4561 		} else if (beacon_rate->control[band].vht_mcs[i]) {
4562 			count_vht++;
4563 			if (count_vht > 1)
4564 				return -EINVAL;
4565 		}
4566 		if (count_vht && rate)
4567 			return -EINVAL;
4568 	}
4569 
4570 	if ((count_ht && count_vht) || (!rate && !count_ht && !count_vht))
4571 		return -EINVAL;
4572 
4573 	if (rate &&
4574 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4575 				     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
4576 		return -EINVAL;
4577 	if (count_ht &&
4578 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4579 				     NL80211_EXT_FEATURE_BEACON_RATE_HT))
4580 		return -EINVAL;
4581 	if (count_vht &&
4582 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4583 				     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
4584 		return -EINVAL;
4585 
4586 	return 0;
4587 }
4588 
4589 static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
4590 				struct nlattr *attrs[],
4591 				struct cfg80211_beacon_data *bcn)
4592 {
4593 	bool haveinfo = false;
4594 	int err;
4595 
4596 	memset(bcn, 0, sizeof(*bcn));
4597 
4598 	if (attrs[NL80211_ATTR_BEACON_HEAD]) {
4599 		bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
4600 		bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
4601 		if (!bcn->head_len)
4602 			return -EINVAL;
4603 		haveinfo = true;
4604 	}
4605 
4606 	if (attrs[NL80211_ATTR_BEACON_TAIL]) {
4607 		bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
4608 		bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
4609 		haveinfo = true;
4610 	}
4611 
4612 	if (!haveinfo)
4613 		return -EINVAL;
4614 
4615 	if (attrs[NL80211_ATTR_IE]) {
4616 		bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
4617 		bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
4618 	}
4619 
4620 	if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
4621 		bcn->proberesp_ies =
4622 			nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4623 		bcn->proberesp_ies_len =
4624 			nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4625 	}
4626 
4627 	if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
4628 		bcn->assocresp_ies =
4629 			nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4630 		bcn->assocresp_ies_len =
4631 			nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4632 	}
4633 
4634 	if (attrs[NL80211_ATTR_PROBE_RESP]) {
4635 		bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
4636 		bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
4637 	}
4638 
4639 	if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
4640 		struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
4641 
4642 		err = nla_parse_nested_deprecated(tb,
4643 						  NL80211_FTM_RESP_ATTR_MAX,
4644 						  attrs[NL80211_ATTR_FTM_RESPONDER],
4645 						  NULL, NULL);
4646 		if (err)
4647 			return err;
4648 
4649 		if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
4650 		    wiphy_ext_feature_isset(&rdev->wiphy,
4651 					    NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
4652 			bcn->ftm_responder = 1;
4653 		else
4654 			return -EOPNOTSUPP;
4655 
4656 		if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
4657 			bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
4658 			bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
4659 		}
4660 
4661 		if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
4662 			bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
4663 			bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
4664 		}
4665 	} else {
4666 		bcn->ftm_responder = -1;
4667 	}
4668 
4669 	return 0;
4670 }
4671 
4672 static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
4673 				    struct ieee80211_he_obss_pd *he_obss_pd)
4674 {
4675 	struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
4676 	int err;
4677 
4678 	err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
4679 			       he_obss_pd_policy, NULL);
4680 	if (err)
4681 		return err;
4682 
4683 	if (!tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] ||
4684 	    !tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
4685 		return -EINVAL;
4686 
4687 	he_obss_pd->min_offset =
4688 		nla_get_u32(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
4689 	he_obss_pd->max_offset =
4690 		nla_get_u32(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
4691 
4692 	if (he_obss_pd->min_offset >= he_obss_pd->max_offset)
4693 		return -EINVAL;
4694 
4695 	he_obss_pd->enable = true;
4696 
4697 	return 0;
4698 }
4699 
4700 static int nl80211_parse_he_bss_color(struct nlattr *attrs,
4701 				      struct cfg80211_he_bss_color *he_bss_color)
4702 {
4703 	struct nlattr *tb[NL80211_HE_BSS_COLOR_ATTR_MAX + 1];
4704 	int err;
4705 
4706 	err = nla_parse_nested(tb, NL80211_HE_BSS_COLOR_ATTR_MAX, attrs,
4707 			       he_bss_color_policy, NULL);
4708 	if (err)
4709 		return err;
4710 
4711 	if (!tb[NL80211_HE_BSS_COLOR_ATTR_COLOR])
4712 		return -EINVAL;
4713 
4714 	he_bss_color->color =
4715 		nla_get_u8(tb[NL80211_HE_BSS_COLOR_ATTR_COLOR]);
4716 	he_bss_color->disabled =
4717 		nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_DISABLED]);
4718 	he_bss_color->partial =
4719 		nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_PARTIAL]);
4720 
4721 	return 0;
4722 }
4723 
4724 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
4725 					    const u8 *rates)
4726 {
4727 	int i;
4728 
4729 	if (!rates)
4730 		return;
4731 
4732 	for (i = 0; i < rates[1]; i++) {
4733 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
4734 			params->ht_required = true;
4735 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
4736 			params->vht_required = true;
4737 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HE_PHY)
4738 			params->he_required = true;
4739 	}
4740 }
4741 
4742 /*
4743  * Since the nl80211 API didn't include, from the beginning, attributes about
4744  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
4745  * benefit of drivers that rebuild IEs in the firmware.
4746  */
4747 static void nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
4748 {
4749 	const struct cfg80211_beacon_data *bcn = &params->beacon;
4750 	size_t ies_len = bcn->tail_len;
4751 	const u8 *ies = bcn->tail;
4752 	const u8 *rates;
4753 	const u8 *cap;
4754 
4755 	rates = cfg80211_find_ie(WLAN_EID_SUPP_RATES, ies, ies_len);
4756 	nl80211_check_ap_rate_selectors(params, rates);
4757 
4758 	rates = cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
4759 	nl80211_check_ap_rate_selectors(params, rates);
4760 
4761 	cap = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies, ies_len);
4762 	if (cap && cap[1] >= sizeof(*params->ht_cap))
4763 		params->ht_cap = (void *)(cap + 2);
4764 	cap = cfg80211_find_ie(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
4765 	if (cap && cap[1] >= sizeof(*params->vht_cap))
4766 		params->vht_cap = (void *)(cap + 2);
4767 	cap = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
4768 	if (cap && cap[1] >= sizeof(*params->he_cap) + 1)
4769 		params->he_cap = (void *)(cap + 3);
4770 	cap = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_OPERATION, ies, ies_len);
4771 	if (cap && cap[1] >= sizeof(*params->he_oper) + 1)
4772 		params->he_oper = (void *)(cap + 3);
4773 }
4774 
4775 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
4776 				   struct cfg80211_ap_settings *params)
4777 {
4778 	struct wireless_dev *wdev;
4779 	bool ret = false;
4780 
4781 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
4782 		if (wdev->iftype != NL80211_IFTYPE_AP &&
4783 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)
4784 			continue;
4785 
4786 		if (!wdev->preset_chandef.chan)
4787 			continue;
4788 
4789 		params->chandef = wdev->preset_chandef;
4790 		ret = true;
4791 		break;
4792 	}
4793 
4794 	return ret;
4795 }
4796 
4797 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
4798 				    enum nl80211_auth_type auth_type,
4799 				    enum nl80211_commands cmd)
4800 {
4801 	if (auth_type > NL80211_AUTHTYPE_MAX)
4802 		return false;
4803 
4804 	switch (cmd) {
4805 	case NL80211_CMD_AUTHENTICATE:
4806 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
4807 		    auth_type == NL80211_AUTHTYPE_SAE)
4808 			return false;
4809 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
4810 					     NL80211_EXT_FEATURE_FILS_STA) &&
4811 		    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
4812 		     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4813 		     auth_type == NL80211_AUTHTYPE_FILS_PK))
4814 			return false;
4815 		return true;
4816 	case NL80211_CMD_CONNECT:
4817 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
4818 		    !wiphy_ext_feature_isset(&rdev->wiphy,
4819 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
4820 		    auth_type == NL80211_AUTHTYPE_SAE)
4821 			return false;
4822 
4823 		/* FILS with SK PFS or PK not supported yet */
4824 		if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4825 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
4826 			return false;
4827 		if (!wiphy_ext_feature_isset(
4828 			    &rdev->wiphy,
4829 			    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
4830 		    auth_type == NL80211_AUTHTYPE_FILS_SK)
4831 			return false;
4832 		return true;
4833 	case NL80211_CMD_START_AP:
4834 		/* SAE not supported yet */
4835 		if (auth_type == NL80211_AUTHTYPE_SAE)
4836 			return false;
4837 		/* FILS not supported yet */
4838 		if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
4839 		    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4840 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
4841 			return false;
4842 		return true;
4843 	default:
4844 		return false;
4845 	}
4846 }
4847 
4848 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
4849 {
4850 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4851 	struct net_device *dev = info->user_ptr[1];
4852 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4853 	struct cfg80211_ap_settings params;
4854 	int err;
4855 
4856 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4857 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4858 		return -EOPNOTSUPP;
4859 
4860 	if (!rdev->ops->start_ap)
4861 		return -EOPNOTSUPP;
4862 
4863 	if (wdev->beacon_interval)
4864 		return -EALREADY;
4865 
4866 	memset(&params, 0, sizeof(params));
4867 
4868 	/* these are required for START_AP */
4869 	if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
4870 	    !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
4871 	    !info->attrs[NL80211_ATTR_BEACON_HEAD])
4872 		return -EINVAL;
4873 
4874 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon);
4875 	if (err)
4876 		return err;
4877 
4878 	params.beacon_interval =
4879 		nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4880 	params.dtim_period =
4881 		nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
4882 
4883 	err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
4884 					   params.beacon_interval);
4885 	if (err)
4886 		return err;
4887 
4888 	/*
4889 	 * In theory, some of these attributes should be required here
4890 	 * but since they were not used when the command was originally
4891 	 * added, keep them optional for old user space programs to let
4892 	 * them continue to work with drivers that do not need the
4893 	 * additional information -- drivers must check!
4894 	 */
4895 	if (info->attrs[NL80211_ATTR_SSID]) {
4896 		params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4897 		params.ssid_len =
4898 			nla_len(info->attrs[NL80211_ATTR_SSID]);
4899 		if (params.ssid_len == 0 ||
4900 		    params.ssid_len > IEEE80211_MAX_SSID_LEN)
4901 			return -EINVAL;
4902 	}
4903 
4904 	if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
4905 		params.hidden_ssid = nla_get_u32(
4906 			info->attrs[NL80211_ATTR_HIDDEN_SSID]);
4907 
4908 	params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
4909 
4910 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4911 		params.auth_type = nla_get_u32(
4912 			info->attrs[NL80211_ATTR_AUTH_TYPE]);
4913 		if (!nl80211_valid_auth_type(rdev, params.auth_type,
4914 					     NL80211_CMD_START_AP))
4915 			return -EINVAL;
4916 	} else
4917 		params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4918 
4919 	err = nl80211_crypto_settings(rdev, info, &params.crypto,
4920 				      NL80211_MAX_NR_CIPHER_SUITES);
4921 	if (err)
4922 		return err;
4923 
4924 	if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
4925 		if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
4926 			return -EOPNOTSUPP;
4927 		params.inactivity_timeout = nla_get_u16(
4928 			info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
4929 	}
4930 
4931 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4932 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4933 			return -EINVAL;
4934 		params.p2p_ctwindow =
4935 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4936 		if (params.p2p_ctwindow != 0 &&
4937 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4938 			return -EINVAL;
4939 	}
4940 
4941 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4942 		u8 tmp;
4943 
4944 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4945 			return -EINVAL;
4946 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4947 		params.p2p_opp_ps = tmp;
4948 		if (params.p2p_opp_ps != 0 &&
4949 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4950 			return -EINVAL;
4951 	}
4952 
4953 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4954 		err = nl80211_parse_chandef(rdev, info, &params.chandef);
4955 		if (err)
4956 			return err;
4957 	} else if (wdev->preset_chandef.chan) {
4958 		params.chandef = wdev->preset_chandef;
4959 	} else if (!nl80211_get_ap_channel(rdev, &params))
4960 		return -EINVAL;
4961 
4962 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
4963 					   wdev->iftype))
4964 		return -EINVAL;
4965 
4966 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
4967 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
4968 						    NL80211_ATTR_TX_RATES,
4969 						    &params.beacon_rate);
4970 		if (err)
4971 			return err;
4972 
4973 		err = validate_beacon_tx_rate(rdev, params.chandef.chan->band,
4974 					      &params.beacon_rate);
4975 		if (err)
4976 			return err;
4977 	}
4978 
4979 	if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
4980 		params.smps_mode =
4981 			nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
4982 		switch (params.smps_mode) {
4983 		case NL80211_SMPS_OFF:
4984 			break;
4985 		case NL80211_SMPS_STATIC:
4986 			if (!(rdev->wiphy.features &
4987 			      NL80211_FEATURE_STATIC_SMPS))
4988 				return -EINVAL;
4989 			break;
4990 		case NL80211_SMPS_DYNAMIC:
4991 			if (!(rdev->wiphy.features &
4992 			      NL80211_FEATURE_DYNAMIC_SMPS))
4993 				return -EINVAL;
4994 			break;
4995 		default:
4996 			return -EINVAL;
4997 		}
4998 	} else {
4999 		params.smps_mode = NL80211_SMPS_OFF;
5000 	}
5001 
5002 	params.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
5003 	if (params.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ])
5004 		return -EOPNOTSUPP;
5005 
5006 	if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
5007 		params.acl = parse_acl_data(&rdev->wiphy, info);
5008 		if (IS_ERR(params.acl))
5009 			return PTR_ERR(params.acl);
5010 	}
5011 
5012 	params.twt_responder =
5013 		    nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
5014 
5015 	if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
5016 		err = nl80211_parse_he_obss_pd(
5017 					info->attrs[NL80211_ATTR_HE_OBSS_PD],
5018 					&params.he_obss_pd);
5019 		goto out;
5020 	}
5021 
5022 	if (info->attrs[NL80211_ATTR_HE_BSS_COLOR]) {
5023 		err = nl80211_parse_he_bss_color(
5024 					info->attrs[NL80211_ATTR_HE_BSS_COLOR],
5025 					&params.he_bss_color);
5026 		if (err)
5027 			return err;
5028 	}
5029 
5030 	nl80211_calculate_ap_params(&params);
5031 
5032 	if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
5033 		params.flags |= AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
5034 
5035 	wdev_lock(wdev);
5036 	err = rdev_start_ap(rdev, dev, &params);
5037 	if (!err) {
5038 		wdev->preset_chandef = params.chandef;
5039 		wdev->beacon_interval = params.beacon_interval;
5040 		wdev->chandef = params.chandef;
5041 		wdev->ssid_len = params.ssid_len;
5042 		memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
5043 
5044 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
5045 			wdev->conn_owner_nlportid = info->snd_portid;
5046 	}
5047 	wdev_unlock(wdev);
5048 
5049 out:
5050 	kfree(params.acl);
5051 
5052 	return err;
5053 }
5054 
5055 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
5056 {
5057 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5058 	struct net_device *dev = info->user_ptr[1];
5059 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5060 	struct cfg80211_beacon_data params;
5061 	int err;
5062 
5063 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5064 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5065 		return -EOPNOTSUPP;
5066 
5067 	if (!rdev->ops->change_beacon)
5068 		return -EOPNOTSUPP;
5069 
5070 	if (!wdev->beacon_interval)
5071 		return -EINVAL;
5072 
5073 	err = nl80211_parse_beacon(rdev, info->attrs, &params);
5074 	if (err)
5075 		return err;
5076 
5077 	wdev_lock(wdev);
5078 	err = rdev_change_beacon(rdev, dev, &params);
5079 	wdev_unlock(wdev);
5080 
5081 	return err;
5082 }
5083 
5084 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
5085 {
5086 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5087 	struct net_device *dev = info->user_ptr[1];
5088 
5089 	return cfg80211_stop_ap(rdev, dev, false);
5090 }
5091 
5092 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
5093 	[NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
5094 	[NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
5095 	[NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
5096 	[NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
5097 	[NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
5098 	[NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
5099 };
5100 
5101 static int parse_station_flags(struct genl_info *info,
5102 			       enum nl80211_iftype iftype,
5103 			       struct station_parameters *params)
5104 {
5105 	struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
5106 	struct nlattr *nla;
5107 	int flag;
5108 
5109 	/*
5110 	 * Try parsing the new attribute first so userspace
5111 	 * can specify both for older kernels.
5112 	 */
5113 	nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
5114 	if (nla) {
5115 		struct nl80211_sta_flag_update *sta_flags;
5116 
5117 		sta_flags = nla_data(nla);
5118 		params->sta_flags_mask = sta_flags->mask;
5119 		params->sta_flags_set = sta_flags->set;
5120 		params->sta_flags_set &= params->sta_flags_mask;
5121 		if ((params->sta_flags_mask |
5122 		     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
5123 			return -EINVAL;
5124 		return 0;
5125 	}
5126 
5127 	/* if present, parse the old attribute */
5128 
5129 	nla = info->attrs[NL80211_ATTR_STA_FLAGS];
5130 	if (!nla)
5131 		return 0;
5132 
5133 	if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
5134 		return -EINVAL;
5135 
5136 	/*
5137 	 * Only allow certain flags for interface types so that
5138 	 * other attributes are silently ignored. Remember that
5139 	 * this is backward compatibility code with old userspace
5140 	 * and shouldn't be hit in other cases anyway.
5141 	 */
5142 	switch (iftype) {
5143 	case NL80211_IFTYPE_AP:
5144 	case NL80211_IFTYPE_AP_VLAN:
5145 	case NL80211_IFTYPE_P2P_GO:
5146 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
5147 					 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
5148 					 BIT(NL80211_STA_FLAG_WME) |
5149 					 BIT(NL80211_STA_FLAG_MFP);
5150 		break;
5151 	case NL80211_IFTYPE_P2P_CLIENT:
5152 	case NL80211_IFTYPE_STATION:
5153 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
5154 					 BIT(NL80211_STA_FLAG_TDLS_PEER);
5155 		break;
5156 	case NL80211_IFTYPE_MESH_POINT:
5157 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5158 					 BIT(NL80211_STA_FLAG_MFP) |
5159 					 BIT(NL80211_STA_FLAG_AUTHORIZED);
5160 		break;
5161 	default:
5162 		return -EINVAL;
5163 	}
5164 
5165 	for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
5166 		if (flags[flag]) {
5167 			params->sta_flags_set |= (1<<flag);
5168 
5169 			/* no longer support new API additions in old API */
5170 			if (flag > NL80211_STA_FLAG_MAX_OLD_API)
5171 				return -EINVAL;
5172 		}
5173 	}
5174 
5175 	return 0;
5176 }
5177 
5178 bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
5179 {
5180 	struct nlattr *rate;
5181 	u32 bitrate;
5182 	u16 bitrate_compat;
5183 	enum nl80211_rate_info rate_flg;
5184 
5185 	rate = nla_nest_start_noflag(msg, attr);
5186 	if (!rate)
5187 		return false;
5188 
5189 	/* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
5190 	bitrate = cfg80211_calculate_bitrate(info);
5191 	/* report 16-bit bitrate only if we can */
5192 	bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
5193 	if (bitrate > 0 &&
5194 	    nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
5195 		return false;
5196 	if (bitrate_compat > 0 &&
5197 	    nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
5198 		return false;
5199 
5200 	switch (info->bw) {
5201 	case RATE_INFO_BW_5:
5202 		rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
5203 		break;
5204 	case RATE_INFO_BW_10:
5205 		rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
5206 		break;
5207 	default:
5208 		WARN_ON(1);
5209 		/* fall through */
5210 	case RATE_INFO_BW_20:
5211 		rate_flg = 0;
5212 		break;
5213 	case RATE_INFO_BW_40:
5214 		rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
5215 		break;
5216 	case RATE_INFO_BW_80:
5217 		rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
5218 		break;
5219 	case RATE_INFO_BW_160:
5220 		rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
5221 		break;
5222 	case RATE_INFO_BW_HE_RU:
5223 		rate_flg = 0;
5224 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
5225 	}
5226 
5227 	if (rate_flg && nla_put_flag(msg, rate_flg))
5228 		return false;
5229 
5230 	if (info->flags & RATE_INFO_FLAGS_MCS) {
5231 		if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
5232 			return false;
5233 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
5234 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
5235 			return false;
5236 	} else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
5237 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
5238 			return false;
5239 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
5240 			return false;
5241 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
5242 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
5243 			return false;
5244 	} else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
5245 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
5246 			return false;
5247 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
5248 			return false;
5249 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
5250 			return false;
5251 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
5252 			return false;
5253 		if (info->bw == RATE_INFO_BW_HE_RU &&
5254 		    nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
5255 			       info->he_ru_alloc))
5256 			return false;
5257 	}
5258 
5259 	nla_nest_end(msg, rate);
5260 	return true;
5261 }
5262 
5263 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
5264 			       int id)
5265 {
5266 	void *attr;
5267 	int i = 0;
5268 
5269 	if (!mask)
5270 		return true;
5271 
5272 	attr = nla_nest_start_noflag(msg, id);
5273 	if (!attr)
5274 		return false;
5275 
5276 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
5277 		if (!(mask & BIT(i)))
5278 			continue;
5279 
5280 		if (nla_put_u8(msg, i, signal[i]))
5281 			return false;
5282 	}
5283 
5284 	nla_nest_end(msg, attr);
5285 
5286 	return true;
5287 }
5288 
5289 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
5290 				u32 seq, int flags,
5291 				struct cfg80211_registered_device *rdev,
5292 				struct net_device *dev,
5293 				const u8 *mac_addr, struct station_info *sinfo)
5294 {
5295 	void *hdr;
5296 	struct nlattr *sinfoattr, *bss_param;
5297 
5298 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
5299 	if (!hdr) {
5300 		cfg80211_sinfo_release_content(sinfo);
5301 		return -1;
5302 	}
5303 
5304 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5305 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
5306 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
5307 		goto nla_put_failure;
5308 
5309 	sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
5310 	if (!sinfoattr)
5311 		goto nla_put_failure;
5312 
5313 #define PUT_SINFO(attr, memb, type) do {				\
5314 	BUILD_BUG_ON(sizeof(type) == sizeof(u64));			\
5315 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
5316 	    nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,		\
5317 			     sinfo->memb))				\
5318 		goto nla_put_failure;					\
5319 	} while (0)
5320 #define PUT_SINFO_U64(attr, memb) do {					\
5321 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
5322 	    nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,		\
5323 			      sinfo->memb, NL80211_STA_INFO_PAD))	\
5324 		goto nla_put_failure;					\
5325 	} while (0)
5326 
5327 	PUT_SINFO(CONNECTED_TIME, connected_time, u32);
5328 	PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
5329 	PUT_SINFO_U64(ASSOC_AT_BOOTTIME, assoc_at);
5330 
5331 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
5332 			     BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
5333 	    nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
5334 			(u32)sinfo->rx_bytes))
5335 		goto nla_put_failure;
5336 
5337 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
5338 			     BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
5339 	    nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
5340 			(u32)sinfo->tx_bytes))
5341 		goto nla_put_failure;
5342 
5343 	PUT_SINFO_U64(RX_BYTES64, rx_bytes);
5344 	PUT_SINFO_U64(TX_BYTES64, tx_bytes);
5345 	PUT_SINFO(LLID, llid, u16);
5346 	PUT_SINFO(PLID, plid, u16);
5347 	PUT_SINFO(PLINK_STATE, plink_state, u8);
5348 	PUT_SINFO_U64(RX_DURATION, rx_duration);
5349 	PUT_SINFO_U64(TX_DURATION, tx_duration);
5350 
5351 	if (wiphy_ext_feature_isset(&rdev->wiphy,
5352 				    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5353 		PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
5354 
5355 	switch (rdev->wiphy.signal_type) {
5356 	case CFG80211_SIGNAL_TYPE_MBM:
5357 		PUT_SINFO(SIGNAL, signal, u8);
5358 		PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
5359 		break;
5360 	default:
5361 		break;
5362 	}
5363 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
5364 		if (!nl80211_put_signal(msg, sinfo->chains,
5365 					sinfo->chain_signal,
5366 					NL80211_STA_INFO_CHAIN_SIGNAL))
5367 			goto nla_put_failure;
5368 	}
5369 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
5370 		if (!nl80211_put_signal(msg, sinfo->chains,
5371 					sinfo->chain_signal_avg,
5372 					NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
5373 			goto nla_put_failure;
5374 	}
5375 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
5376 		if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
5377 					  NL80211_STA_INFO_TX_BITRATE))
5378 			goto nla_put_failure;
5379 	}
5380 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
5381 		if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
5382 					  NL80211_STA_INFO_RX_BITRATE))
5383 			goto nla_put_failure;
5384 	}
5385 
5386 	PUT_SINFO(RX_PACKETS, rx_packets, u32);
5387 	PUT_SINFO(TX_PACKETS, tx_packets, u32);
5388 	PUT_SINFO(TX_RETRIES, tx_retries, u32);
5389 	PUT_SINFO(TX_FAILED, tx_failed, u32);
5390 	PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
5391 	PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
5392 	PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
5393 	PUT_SINFO(LOCAL_PM, local_pm, u32);
5394 	PUT_SINFO(PEER_PM, peer_pm, u32);
5395 	PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
5396 	PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
5397 
5398 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
5399 		bss_param = nla_nest_start_noflag(msg,
5400 						  NL80211_STA_INFO_BSS_PARAM);
5401 		if (!bss_param)
5402 			goto nla_put_failure;
5403 
5404 		if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
5405 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
5406 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
5407 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
5408 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
5409 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
5410 		    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
5411 			       sinfo->bss_param.dtim_period) ||
5412 		    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
5413 				sinfo->bss_param.beacon_interval))
5414 			goto nla_put_failure;
5415 
5416 		nla_nest_end(msg, bss_param);
5417 	}
5418 	if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
5419 	    nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
5420 		    sizeof(struct nl80211_sta_flag_update),
5421 		    &sinfo->sta_flags))
5422 		goto nla_put_failure;
5423 
5424 	PUT_SINFO_U64(T_OFFSET, t_offset);
5425 	PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
5426 	PUT_SINFO_U64(BEACON_RX, rx_beacon);
5427 	PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
5428 	PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
5429 	PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
5430 	if (wiphy_ext_feature_isset(&rdev->wiphy,
5431 				    NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
5432 		PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
5433 		PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
5434 	}
5435 
5436 #undef PUT_SINFO
5437 #undef PUT_SINFO_U64
5438 
5439 	if (sinfo->pertid) {
5440 		struct nlattr *tidsattr;
5441 		int tid;
5442 
5443 		tidsattr = nla_nest_start_noflag(msg,
5444 						 NL80211_STA_INFO_TID_STATS);
5445 		if (!tidsattr)
5446 			goto nla_put_failure;
5447 
5448 		for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
5449 			struct cfg80211_tid_stats *tidstats;
5450 			struct nlattr *tidattr;
5451 
5452 			tidstats = &sinfo->pertid[tid];
5453 
5454 			if (!tidstats->filled)
5455 				continue;
5456 
5457 			tidattr = nla_nest_start_noflag(msg, tid + 1);
5458 			if (!tidattr)
5459 				goto nla_put_failure;
5460 
5461 #define PUT_TIDVAL_U64(attr, memb) do {					\
5462 	if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&	\
5463 	    nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,		\
5464 			      tidstats->memb, NL80211_TID_STATS_PAD))	\
5465 		goto nla_put_failure;					\
5466 	} while (0)
5467 
5468 			PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
5469 			PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
5470 			PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
5471 			PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
5472 
5473 #undef PUT_TIDVAL_U64
5474 			if ((tidstats->filled &
5475 			     BIT(NL80211_TID_STATS_TXQ_STATS)) &&
5476 			    !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
5477 						   NL80211_TID_STATS_TXQ_STATS))
5478 				goto nla_put_failure;
5479 
5480 			nla_nest_end(msg, tidattr);
5481 		}
5482 
5483 		nla_nest_end(msg, tidsattr);
5484 	}
5485 
5486 	nla_nest_end(msg, sinfoattr);
5487 
5488 	if (sinfo->assoc_req_ies_len &&
5489 	    nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
5490 		    sinfo->assoc_req_ies))
5491 		goto nla_put_failure;
5492 
5493 	cfg80211_sinfo_release_content(sinfo);
5494 	genlmsg_end(msg, hdr);
5495 	return 0;
5496 
5497  nla_put_failure:
5498 	cfg80211_sinfo_release_content(sinfo);
5499 	genlmsg_cancel(msg, hdr);
5500 	return -EMSGSIZE;
5501 }
5502 
5503 static int nl80211_dump_station(struct sk_buff *skb,
5504 				struct netlink_callback *cb)
5505 {
5506 	struct station_info sinfo;
5507 	struct cfg80211_registered_device *rdev;
5508 	struct wireless_dev *wdev;
5509 	u8 mac_addr[ETH_ALEN];
5510 	int sta_idx = cb->args[2];
5511 	int err;
5512 
5513 	rtnl_lock();
5514 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
5515 	if (err)
5516 		goto out_err;
5517 
5518 	if (!wdev->netdev) {
5519 		err = -EINVAL;
5520 		goto out_err;
5521 	}
5522 
5523 	if (!rdev->ops->dump_station) {
5524 		err = -EOPNOTSUPP;
5525 		goto out_err;
5526 	}
5527 
5528 	while (1) {
5529 		memset(&sinfo, 0, sizeof(sinfo));
5530 		err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
5531 					mac_addr, &sinfo);
5532 		if (err == -ENOENT)
5533 			break;
5534 		if (err)
5535 			goto out_err;
5536 
5537 		if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
5538 				NETLINK_CB(cb->skb).portid,
5539 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
5540 				rdev, wdev->netdev, mac_addr,
5541 				&sinfo) < 0)
5542 			goto out;
5543 
5544 		sta_idx++;
5545 	}
5546 
5547  out:
5548 	cb->args[2] = sta_idx;
5549 	err = skb->len;
5550  out_err:
5551 	rtnl_unlock();
5552 
5553 	return err;
5554 }
5555 
5556 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
5557 {
5558 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5559 	struct net_device *dev = info->user_ptr[1];
5560 	struct station_info sinfo;
5561 	struct sk_buff *msg;
5562 	u8 *mac_addr = NULL;
5563 	int err;
5564 
5565 	memset(&sinfo, 0, sizeof(sinfo));
5566 
5567 	if (!info->attrs[NL80211_ATTR_MAC])
5568 		return -EINVAL;
5569 
5570 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5571 
5572 	if (!rdev->ops->get_station)
5573 		return -EOPNOTSUPP;
5574 
5575 	err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
5576 	if (err)
5577 		return err;
5578 
5579 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5580 	if (!msg) {
5581 		cfg80211_sinfo_release_content(&sinfo);
5582 		return -ENOMEM;
5583 	}
5584 
5585 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
5586 				 info->snd_portid, info->snd_seq, 0,
5587 				 rdev, dev, mac_addr, &sinfo) < 0) {
5588 		nlmsg_free(msg);
5589 		return -ENOBUFS;
5590 	}
5591 
5592 	return genlmsg_reply(msg, info);
5593 }
5594 
5595 int cfg80211_check_station_change(struct wiphy *wiphy,
5596 				  struct station_parameters *params,
5597 				  enum cfg80211_station_type statype)
5598 {
5599 	if (params->listen_interval != -1 &&
5600 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5601 		return -EINVAL;
5602 
5603 	if (params->support_p2p_ps != -1 &&
5604 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5605 		return -EINVAL;
5606 
5607 	if (params->aid &&
5608 	    !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
5609 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5610 		return -EINVAL;
5611 
5612 	/* When you run into this, adjust the code below for the new flag */
5613 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5614 
5615 	switch (statype) {
5616 	case CFG80211_STA_MESH_PEER_KERNEL:
5617 	case CFG80211_STA_MESH_PEER_USER:
5618 		/*
5619 		 * No ignoring the TDLS flag here -- the userspace mesh
5620 		 * code doesn't have the bug of including TDLS in the
5621 		 * mask everywhere.
5622 		 */
5623 		if (params->sta_flags_mask &
5624 				~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5625 				  BIT(NL80211_STA_FLAG_MFP) |
5626 				  BIT(NL80211_STA_FLAG_AUTHORIZED)))
5627 			return -EINVAL;
5628 		break;
5629 	case CFG80211_STA_TDLS_PEER_SETUP:
5630 	case CFG80211_STA_TDLS_PEER_ACTIVE:
5631 		if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
5632 			return -EINVAL;
5633 		/* ignore since it can't change */
5634 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5635 		break;
5636 	default:
5637 		/* disallow mesh-specific things */
5638 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
5639 			return -EINVAL;
5640 		if (params->local_pm)
5641 			return -EINVAL;
5642 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
5643 			return -EINVAL;
5644 	}
5645 
5646 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
5647 	    statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
5648 		/* TDLS can't be set, ... */
5649 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
5650 			return -EINVAL;
5651 		/*
5652 		 * ... but don't bother the driver with it. This works around
5653 		 * a hostapd/wpa_supplicant issue -- it always includes the
5654 		 * TLDS_PEER flag in the mask even for AP mode.
5655 		 */
5656 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5657 	}
5658 
5659 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
5660 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5661 		/* reject other things that can't change */
5662 		if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
5663 			return -EINVAL;
5664 		if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
5665 			return -EINVAL;
5666 		if (params->supported_rates)
5667 			return -EINVAL;
5668 		if (params->ext_capab || params->ht_capa || params->vht_capa ||
5669 		    params->he_capa)
5670 			return -EINVAL;
5671 	}
5672 
5673 	if (statype != CFG80211_STA_AP_CLIENT &&
5674 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5675 		if (params->vlan)
5676 			return -EINVAL;
5677 	}
5678 
5679 	switch (statype) {
5680 	case CFG80211_STA_AP_MLME_CLIENT:
5681 		/* Use this only for authorizing/unauthorizing a station */
5682 		if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
5683 			return -EOPNOTSUPP;
5684 		break;
5685 	case CFG80211_STA_AP_CLIENT:
5686 	case CFG80211_STA_AP_CLIENT_UNASSOC:
5687 		/* accept only the listed bits */
5688 		if (params->sta_flags_mask &
5689 				~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
5690 				  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5691 				  BIT(NL80211_STA_FLAG_ASSOCIATED) |
5692 				  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
5693 				  BIT(NL80211_STA_FLAG_WME) |
5694 				  BIT(NL80211_STA_FLAG_MFP)))
5695 			return -EINVAL;
5696 
5697 		/* but authenticated/associated only if driver handles it */
5698 		if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5699 		    params->sta_flags_mask &
5700 				(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5701 				 BIT(NL80211_STA_FLAG_ASSOCIATED)))
5702 			return -EINVAL;
5703 		break;
5704 	case CFG80211_STA_IBSS:
5705 	case CFG80211_STA_AP_STA:
5706 		/* reject any changes other than AUTHORIZED */
5707 		if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
5708 			return -EINVAL;
5709 		break;
5710 	case CFG80211_STA_TDLS_PEER_SETUP:
5711 		/* reject any changes other than AUTHORIZED or WME */
5712 		if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
5713 					       BIT(NL80211_STA_FLAG_WME)))
5714 			return -EINVAL;
5715 		/* force (at least) rates when authorizing */
5716 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
5717 		    !params->supported_rates)
5718 			return -EINVAL;
5719 		break;
5720 	case CFG80211_STA_TDLS_PEER_ACTIVE:
5721 		/* reject any changes */
5722 		return -EINVAL;
5723 	case CFG80211_STA_MESH_PEER_KERNEL:
5724 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
5725 			return -EINVAL;
5726 		break;
5727 	case CFG80211_STA_MESH_PEER_USER:
5728 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
5729 		    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
5730 			return -EINVAL;
5731 		break;
5732 	}
5733 
5734 	/*
5735 	 * Older kernel versions ignored this attribute entirely, so don't
5736 	 * reject attempts to update it but mark it as unused instead so the
5737 	 * driver won't look at the data.
5738 	 */
5739 	if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
5740 	    statype != CFG80211_STA_TDLS_PEER_SETUP)
5741 		params->opmode_notif_used = false;
5742 
5743 	return 0;
5744 }
5745 EXPORT_SYMBOL(cfg80211_check_station_change);
5746 
5747 /*
5748  * Get vlan interface making sure it is running and on the right wiphy.
5749  */
5750 static struct net_device *get_vlan(struct genl_info *info,
5751 				   struct cfg80211_registered_device *rdev)
5752 {
5753 	struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
5754 	struct net_device *v;
5755 	int ret;
5756 
5757 	if (!vlanattr)
5758 		return NULL;
5759 
5760 	v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
5761 	if (!v)
5762 		return ERR_PTR(-ENODEV);
5763 
5764 	if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
5765 		ret = -EINVAL;
5766 		goto error;
5767 	}
5768 
5769 	if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5770 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5771 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
5772 		ret = -EINVAL;
5773 		goto error;
5774 	}
5775 
5776 	if (!netif_running(v)) {
5777 		ret = -ENETDOWN;
5778 		goto error;
5779 	}
5780 
5781 	return v;
5782  error:
5783 	dev_put(v);
5784 	return ERR_PTR(ret);
5785 }
5786 
5787 static const struct nla_policy
5788 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
5789 	[NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
5790 	[NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
5791 };
5792 
5793 static int nl80211_parse_sta_wme(struct genl_info *info,
5794 				 struct station_parameters *params)
5795 {
5796 	struct nlattr *tb[NL80211_STA_WME_MAX + 1];
5797 	struct nlattr *nla;
5798 	int err;
5799 
5800 	/* parse WME attributes if present */
5801 	if (!info->attrs[NL80211_ATTR_STA_WME])
5802 		return 0;
5803 
5804 	nla = info->attrs[NL80211_ATTR_STA_WME];
5805 	err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
5806 					  nl80211_sta_wme_policy,
5807 					  info->extack);
5808 	if (err)
5809 		return err;
5810 
5811 	if (tb[NL80211_STA_WME_UAPSD_QUEUES])
5812 		params->uapsd_queues = nla_get_u8(
5813 			tb[NL80211_STA_WME_UAPSD_QUEUES]);
5814 	if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
5815 		return -EINVAL;
5816 
5817 	if (tb[NL80211_STA_WME_MAX_SP])
5818 		params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
5819 
5820 	if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
5821 		return -EINVAL;
5822 
5823 	params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
5824 
5825 	return 0;
5826 }
5827 
5828 static int nl80211_parse_sta_channel_info(struct genl_info *info,
5829 				      struct station_parameters *params)
5830 {
5831 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
5832 		params->supported_channels =
5833 		     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
5834 		params->supported_channels_len =
5835 		     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
5836 		/*
5837 		 * Need to include at least one (first channel, number of
5838 		 * channels) tuple for each subband, and must have proper
5839 		 * tuples for the rest of the data as well.
5840 		 */
5841 		if (params->supported_channels_len < 2)
5842 			return -EINVAL;
5843 		if (params->supported_channels_len % 2)
5844 			return -EINVAL;
5845 	}
5846 
5847 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
5848 		params->supported_oper_classes =
5849 		 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
5850 		params->supported_oper_classes_len =
5851 		  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
5852 		/*
5853 		 * The value of the Length field of the Supported Operating
5854 		 * Classes element is between 2 and 253.
5855 		 */
5856 		if (params->supported_oper_classes_len < 2 ||
5857 		    params->supported_oper_classes_len > 253)
5858 			return -EINVAL;
5859 	}
5860 	return 0;
5861 }
5862 
5863 static int nl80211_set_station_tdls(struct genl_info *info,
5864 				    struct station_parameters *params)
5865 {
5866 	int err;
5867 	/* Dummy STA entry gets updated once the peer capabilities are known */
5868 	if (info->attrs[NL80211_ATTR_PEER_AID])
5869 		params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5870 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5871 		params->ht_capa =
5872 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5873 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5874 		params->vht_capa =
5875 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5876 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
5877 		params->he_capa =
5878 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5879 		params->he_capa_len =
5880 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5881 
5882 		if (params->he_capa_len < NL80211_HE_MIN_CAPABILITY_LEN)
5883 			return -EINVAL;
5884 	}
5885 
5886 	err = nl80211_parse_sta_channel_info(info, params);
5887 	if (err)
5888 		return err;
5889 
5890 	return nl80211_parse_sta_wme(info, params);
5891 }
5892 
5893 static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
5894 					     struct station_parameters *params)
5895 {
5896 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5897 	int idx;
5898 
5899 	if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
5900 		if (!rdev->ops->set_tx_power ||
5901 		    !wiphy_ext_feature_isset(&rdev->wiphy,
5902 					 NL80211_EXT_FEATURE_STA_TX_PWR))
5903 			return -EOPNOTSUPP;
5904 
5905 		idx = NL80211_ATTR_STA_TX_POWER_SETTING;
5906 		params->txpwr.type = nla_get_u8(info->attrs[idx]);
5907 
5908 		if (params->txpwr.type == NL80211_TX_POWER_LIMITED) {
5909 			idx = NL80211_ATTR_STA_TX_POWER;
5910 
5911 			if (info->attrs[idx])
5912 				params->txpwr.power =
5913 					nla_get_s16(info->attrs[idx]);
5914 			else
5915 				return -EINVAL;
5916 		}
5917 		params->sta_modify_mask |= STATION_PARAM_APPLY_STA_TXPOWER;
5918 	}
5919 
5920 	return 0;
5921 }
5922 
5923 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
5924 {
5925 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5926 	struct net_device *dev = info->user_ptr[1];
5927 	struct station_parameters params;
5928 	u8 *mac_addr;
5929 	int err;
5930 
5931 	memset(&params, 0, sizeof(params));
5932 
5933 	if (!rdev->ops->change_station)
5934 		return -EOPNOTSUPP;
5935 
5936 	/*
5937 	 * AID and listen_interval properties can be set only for unassociated
5938 	 * station. Include these parameters here and will check them in
5939 	 * cfg80211_check_station_change().
5940 	 */
5941 	if (info->attrs[NL80211_ATTR_STA_AID])
5942 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5943 
5944 	if (info->attrs[NL80211_ATTR_VLAN_ID])
5945 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
5946 
5947 	if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5948 		params.listen_interval =
5949 		     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5950 	else
5951 		params.listen_interval = -1;
5952 
5953 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
5954 		params.support_p2p_ps =
5955 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5956 	else
5957 		params.support_p2p_ps = -1;
5958 
5959 	if (!info->attrs[NL80211_ATTR_MAC])
5960 		return -EINVAL;
5961 
5962 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5963 
5964 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
5965 		params.supported_rates =
5966 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5967 		params.supported_rates_len =
5968 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5969 	}
5970 
5971 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5972 		params.capability =
5973 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5974 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5975 	}
5976 
5977 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5978 		params.ext_capab =
5979 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5980 		params.ext_capab_len =
5981 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5982 	}
5983 
5984 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5985 		return -EINVAL;
5986 
5987 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
5988 		params.plink_action =
5989 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5990 
5991 	if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
5992 		params.plink_state =
5993 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
5994 		if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
5995 			params.peer_aid = nla_get_u16(
5996 				info->attrs[NL80211_ATTR_MESH_PEER_AID]);
5997 		params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
5998 	}
5999 
6000 	if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
6001 		params.local_pm = nla_get_u32(
6002 			info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
6003 
6004 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
6005 		params.opmode_notif_used = true;
6006 		params.opmode_notif =
6007 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
6008 	}
6009 
6010 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
6011 		params.he_6ghz_capa =
6012 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6013 
6014 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
6015 		params.airtime_weight =
6016 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
6017 
6018 	if (params.airtime_weight &&
6019 	    !wiphy_ext_feature_isset(&rdev->wiphy,
6020 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
6021 		return -EOPNOTSUPP;
6022 
6023 	err = nl80211_parse_sta_txpower_setting(info, &params);
6024 	if (err)
6025 		return err;
6026 
6027 	/* Include parameters for TDLS peer (will check later) */
6028 	err = nl80211_set_station_tdls(info, &params);
6029 	if (err)
6030 		return err;
6031 
6032 	params.vlan = get_vlan(info, rdev);
6033 	if (IS_ERR(params.vlan))
6034 		return PTR_ERR(params.vlan);
6035 
6036 	switch (dev->ieee80211_ptr->iftype) {
6037 	case NL80211_IFTYPE_AP:
6038 	case NL80211_IFTYPE_AP_VLAN:
6039 	case NL80211_IFTYPE_P2P_GO:
6040 	case NL80211_IFTYPE_P2P_CLIENT:
6041 	case NL80211_IFTYPE_STATION:
6042 	case NL80211_IFTYPE_ADHOC:
6043 	case NL80211_IFTYPE_MESH_POINT:
6044 		break;
6045 	default:
6046 		err = -EOPNOTSUPP;
6047 		goto out_put_vlan;
6048 	}
6049 
6050 	/* driver will call cfg80211_check_station_change() */
6051 	err = rdev_change_station(rdev, dev, mac_addr, &params);
6052 
6053  out_put_vlan:
6054 	if (params.vlan)
6055 		dev_put(params.vlan);
6056 
6057 	return err;
6058 }
6059 
6060 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
6061 {
6062 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6063 	int err;
6064 	struct net_device *dev = info->user_ptr[1];
6065 	struct station_parameters params;
6066 	u8 *mac_addr = NULL;
6067 	u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6068 			 BIT(NL80211_STA_FLAG_ASSOCIATED);
6069 
6070 	memset(&params, 0, sizeof(params));
6071 
6072 	if (!rdev->ops->add_station)
6073 		return -EOPNOTSUPP;
6074 
6075 	if (!info->attrs[NL80211_ATTR_MAC])
6076 		return -EINVAL;
6077 
6078 	if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
6079 		return -EINVAL;
6080 
6081 	if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
6082 		return -EINVAL;
6083 
6084 	if (!info->attrs[NL80211_ATTR_STA_AID] &&
6085 	    !info->attrs[NL80211_ATTR_PEER_AID])
6086 		return -EINVAL;
6087 
6088 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6089 	params.supported_rates =
6090 		nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
6091 	params.supported_rates_len =
6092 		nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
6093 	params.listen_interval =
6094 		nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
6095 
6096 	if (info->attrs[NL80211_ATTR_VLAN_ID])
6097 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
6098 
6099 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
6100 		params.support_p2p_ps =
6101 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
6102 	} else {
6103 		/*
6104 		 * if not specified, assume it's supported for P2P GO interface,
6105 		 * and is NOT supported for AP interface
6106 		 */
6107 		params.support_p2p_ps =
6108 			dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
6109 	}
6110 
6111 	if (info->attrs[NL80211_ATTR_PEER_AID])
6112 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
6113 	else
6114 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
6115 
6116 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
6117 		params.capability =
6118 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
6119 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
6120 	}
6121 
6122 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
6123 		params.ext_capab =
6124 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
6125 		params.ext_capab_len =
6126 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
6127 	}
6128 
6129 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
6130 		params.ht_capa =
6131 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
6132 
6133 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
6134 		params.vht_capa =
6135 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
6136 
6137 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
6138 		params.he_capa =
6139 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6140 		params.he_capa_len =
6141 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6142 
6143 		/* max len is validated in nla policy */
6144 		if (params.he_capa_len < NL80211_HE_MIN_CAPABILITY_LEN)
6145 			return -EINVAL;
6146 	}
6147 
6148 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
6149 		params.he_6ghz_capa =
6150 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
6151 
6152 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
6153 		params.opmode_notif_used = true;
6154 		params.opmode_notif =
6155 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
6156 	}
6157 
6158 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
6159 		params.plink_action =
6160 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
6161 
6162 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
6163 		params.airtime_weight =
6164 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
6165 
6166 	if (params.airtime_weight &&
6167 	    !wiphy_ext_feature_isset(&rdev->wiphy,
6168 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
6169 		return -EOPNOTSUPP;
6170 
6171 	err = nl80211_parse_sta_txpower_setting(info, &params);
6172 	if (err)
6173 		return err;
6174 
6175 	err = nl80211_parse_sta_channel_info(info, &params);
6176 	if (err)
6177 		return err;
6178 
6179 	err = nl80211_parse_sta_wme(info, &params);
6180 	if (err)
6181 		return err;
6182 
6183 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
6184 		return -EINVAL;
6185 
6186 	/* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
6187 	 * as userspace might just pass through the capabilities from the IEs
6188 	 * directly, rather than enforcing this restriction and returning an
6189 	 * error in this case.
6190 	 */
6191 	if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
6192 		params.ht_capa = NULL;
6193 		params.vht_capa = NULL;
6194 
6195 		/* HE requires WME */
6196 		if (params.he_capa_len || params.he_6ghz_capa)
6197 			return -EINVAL;
6198 	}
6199 
6200 	/* Ensure that HT/VHT capabilities are not set for 6 GHz HE STA */
6201 	if (params.he_6ghz_capa && (params.ht_capa || params.vht_capa))
6202 		return -EINVAL;
6203 
6204 	/* When you run into this, adjust the code below for the new flag */
6205 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
6206 
6207 	switch (dev->ieee80211_ptr->iftype) {
6208 	case NL80211_IFTYPE_AP:
6209 	case NL80211_IFTYPE_AP_VLAN:
6210 	case NL80211_IFTYPE_P2P_GO:
6211 		/* ignore WME attributes if iface/sta is not capable */
6212 		if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
6213 		    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
6214 			params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6215 
6216 		/* TDLS peers cannot be added */
6217 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
6218 		    info->attrs[NL80211_ATTR_PEER_AID])
6219 			return -EINVAL;
6220 		/* but don't bother the driver with it */
6221 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6222 
6223 		/* allow authenticated/associated only if driver handles it */
6224 		if (!(rdev->wiphy.features &
6225 				NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
6226 		    params.sta_flags_mask & auth_assoc)
6227 			return -EINVAL;
6228 
6229 		/* Older userspace, or userspace wanting to be compatible with
6230 		 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
6231 		 * and assoc flags in the mask, but assumes the station will be
6232 		 * added as associated anyway since this was the required driver
6233 		 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
6234 		 * introduced.
6235 		 * In order to not bother drivers with this quirk in the API
6236 		 * set the flags in both the mask and set for new stations in
6237 		 * this case.
6238 		 */
6239 		if (!(params.sta_flags_mask & auth_assoc)) {
6240 			params.sta_flags_mask |= auth_assoc;
6241 			params.sta_flags_set |= auth_assoc;
6242 		}
6243 
6244 		/* must be last in here for error handling */
6245 		params.vlan = get_vlan(info, rdev);
6246 		if (IS_ERR(params.vlan))
6247 			return PTR_ERR(params.vlan);
6248 		break;
6249 	case NL80211_IFTYPE_MESH_POINT:
6250 		/* ignore uAPSD data */
6251 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6252 
6253 		/* associated is disallowed */
6254 		if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
6255 			return -EINVAL;
6256 		/* TDLS peers cannot be added */
6257 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
6258 		    info->attrs[NL80211_ATTR_PEER_AID])
6259 			return -EINVAL;
6260 		break;
6261 	case NL80211_IFTYPE_STATION:
6262 	case NL80211_IFTYPE_P2P_CLIENT:
6263 		/* ignore uAPSD data */
6264 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6265 
6266 		/* these are disallowed */
6267 		if (params.sta_flags_mask &
6268 				(BIT(NL80211_STA_FLAG_ASSOCIATED) |
6269 				 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
6270 			return -EINVAL;
6271 		/* Only TDLS peers can be added */
6272 		if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6273 			return -EINVAL;
6274 		/* Can only add if TDLS ... */
6275 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
6276 			return -EOPNOTSUPP;
6277 		/* ... with external setup is supported */
6278 		if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
6279 			return -EOPNOTSUPP;
6280 		/*
6281 		 * Older wpa_supplicant versions always mark the TDLS peer
6282 		 * as authorized, but it shouldn't yet be.
6283 		 */
6284 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
6285 		break;
6286 	default:
6287 		return -EOPNOTSUPP;
6288 	}
6289 
6290 	/* be aware of params.vlan when changing code here */
6291 
6292 	err = rdev_add_station(rdev, dev, mac_addr, &params);
6293 
6294 	if (params.vlan)
6295 		dev_put(params.vlan);
6296 	return err;
6297 }
6298 
6299 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
6300 {
6301 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6302 	struct net_device *dev = info->user_ptr[1];
6303 	struct station_del_parameters params;
6304 
6305 	memset(&params, 0, sizeof(params));
6306 
6307 	if (info->attrs[NL80211_ATTR_MAC])
6308 		params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
6309 
6310 	switch (dev->ieee80211_ptr->iftype) {
6311 	case NL80211_IFTYPE_AP:
6312 	case NL80211_IFTYPE_AP_VLAN:
6313 	case NL80211_IFTYPE_MESH_POINT:
6314 	case NL80211_IFTYPE_P2P_GO:
6315 		/* always accept these */
6316 		break;
6317 	case NL80211_IFTYPE_ADHOC:
6318 		/* conditionally accept */
6319 		if (wiphy_ext_feature_isset(&rdev->wiphy,
6320 					    NL80211_EXT_FEATURE_DEL_IBSS_STA))
6321 			break;
6322 		return -EINVAL;
6323 	default:
6324 		return -EINVAL;
6325 	}
6326 
6327 	if (!rdev->ops->del_station)
6328 		return -EOPNOTSUPP;
6329 
6330 	if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
6331 		params.subtype =
6332 			nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
6333 		if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
6334 		    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
6335 			return -EINVAL;
6336 	} else {
6337 		/* Default to Deauthentication frame */
6338 		params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
6339 	}
6340 
6341 	if (info->attrs[NL80211_ATTR_REASON_CODE]) {
6342 		params.reason_code =
6343 			nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6344 		if (params.reason_code == 0)
6345 			return -EINVAL; /* 0 is reserved */
6346 	} else {
6347 		/* Default to reason code 2 */
6348 		params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
6349 	}
6350 
6351 	return rdev_del_station(rdev, dev, &params);
6352 }
6353 
6354 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
6355 				int flags, struct net_device *dev,
6356 				u8 *dst, u8 *next_hop,
6357 				struct mpath_info *pinfo)
6358 {
6359 	void *hdr;
6360 	struct nlattr *pinfoattr;
6361 
6362 	hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
6363 	if (!hdr)
6364 		return -1;
6365 
6366 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6367 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
6368 	    nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
6369 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
6370 		goto nla_put_failure;
6371 
6372 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
6373 	if (!pinfoattr)
6374 		goto nla_put_failure;
6375 	if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
6376 	    nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
6377 			pinfo->frame_qlen))
6378 		goto nla_put_failure;
6379 	if (((pinfo->filled & MPATH_INFO_SN) &&
6380 	     nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
6381 	    ((pinfo->filled & MPATH_INFO_METRIC) &&
6382 	     nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
6383 			 pinfo->metric)) ||
6384 	    ((pinfo->filled & MPATH_INFO_EXPTIME) &&
6385 	     nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
6386 			 pinfo->exptime)) ||
6387 	    ((pinfo->filled & MPATH_INFO_FLAGS) &&
6388 	     nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
6389 			pinfo->flags)) ||
6390 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
6391 	     nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
6392 			 pinfo->discovery_timeout)) ||
6393 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
6394 	     nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
6395 			pinfo->discovery_retries)) ||
6396 	    ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
6397 	     nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
6398 			pinfo->hop_count)) ||
6399 	    ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
6400 	     nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
6401 			 pinfo->path_change_count)))
6402 		goto nla_put_failure;
6403 
6404 	nla_nest_end(msg, pinfoattr);
6405 
6406 	genlmsg_end(msg, hdr);
6407 	return 0;
6408 
6409  nla_put_failure:
6410 	genlmsg_cancel(msg, hdr);
6411 	return -EMSGSIZE;
6412 }
6413 
6414 static int nl80211_dump_mpath(struct sk_buff *skb,
6415 			      struct netlink_callback *cb)
6416 {
6417 	struct mpath_info pinfo;
6418 	struct cfg80211_registered_device *rdev;
6419 	struct wireless_dev *wdev;
6420 	u8 dst[ETH_ALEN];
6421 	u8 next_hop[ETH_ALEN];
6422 	int path_idx = cb->args[2];
6423 	int err;
6424 
6425 	rtnl_lock();
6426 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
6427 	if (err)
6428 		goto out_err;
6429 
6430 	if (!rdev->ops->dump_mpath) {
6431 		err = -EOPNOTSUPP;
6432 		goto out_err;
6433 	}
6434 
6435 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
6436 		err = -EOPNOTSUPP;
6437 		goto out_err;
6438 	}
6439 
6440 	while (1) {
6441 		err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
6442 				      next_hop, &pinfo);
6443 		if (err == -ENOENT)
6444 			break;
6445 		if (err)
6446 			goto out_err;
6447 
6448 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
6449 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
6450 				       wdev->netdev, dst, next_hop,
6451 				       &pinfo) < 0)
6452 			goto out;
6453 
6454 		path_idx++;
6455 	}
6456 
6457  out:
6458 	cb->args[2] = path_idx;
6459 	err = skb->len;
6460  out_err:
6461 	rtnl_unlock();
6462 	return err;
6463 }
6464 
6465 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
6466 {
6467 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6468 	int err;
6469 	struct net_device *dev = info->user_ptr[1];
6470 	struct mpath_info pinfo;
6471 	struct sk_buff *msg;
6472 	u8 *dst = NULL;
6473 	u8 next_hop[ETH_ALEN];
6474 
6475 	memset(&pinfo, 0, sizeof(pinfo));
6476 
6477 	if (!info->attrs[NL80211_ATTR_MAC])
6478 		return -EINVAL;
6479 
6480 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6481 
6482 	if (!rdev->ops->get_mpath)
6483 		return -EOPNOTSUPP;
6484 
6485 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6486 		return -EOPNOTSUPP;
6487 
6488 	err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
6489 	if (err)
6490 		return err;
6491 
6492 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6493 	if (!msg)
6494 		return -ENOMEM;
6495 
6496 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
6497 				 dev, dst, next_hop, &pinfo) < 0) {
6498 		nlmsg_free(msg);
6499 		return -ENOBUFS;
6500 	}
6501 
6502 	return genlmsg_reply(msg, info);
6503 }
6504 
6505 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
6506 {
6507 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6508 	struct net_device *dev = info->user_ptr[1];
6509 	u8 *dst = NULL;
6510 	u8 *next_hop = NULL;
6511 
6512 	if (!info->attrs[NL80211_ATTR_MAC])
6513 		return -EINVAL;
6514 
6515 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
6516 		return -EINVAL;
6517 
6518 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6519 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
6520 
6521 	if (!rdev->ops->change_mpath)
6522 		return -EOPNOTSUPP;
6523 
6524 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6525 		return -EOPNOTSUPP;
6526 
6527 	return rdev_change_mpath(rdev, dev, dst, next_hop);
6528 }
6529 
6530 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
6531 {
6532 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6533 	struct net_device *dev = info->user_ptr[1];
6534 	u8 *dst = NULL;
6535 	u8 *next_hop = NULL;
6536 
6537 	if (!info->attrs[NL80211_ATTR_MAC])
6538 		return -EINVAL;
6539 
6540 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
6541 		return -EINVAL;
6542 
6543 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6544 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
6545 
6546 	if (!rdev->ops->add_mpath)
6547 		return -EOPNOTSUPP;
6548 
6549 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6550 		return -EOPNOTSUPP;
6551 
6552 	return rdev_add_mpath(rdev, dev, dst, next_hop);
6553 }
6554 
6555 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
6556 {
6557 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6558 	struct net_device *dev = info->user_ptr[1];
6559 	u8 *dst = NULL;
6560 
6561 	if (info->attrs[NL80211_ATTR_MAC])
6562 		dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6563 
6564 	if (!rdev->ops->del_mpath)
6565 		return -EOPNOTSUPP;
6566 
6567 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6568 		return -EOPNOTSUPP;
6569 
6570 	return rdev_del_mpath(rdev, dev, dst);
6571 }
6572 
6573 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
6574 {
6575 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6576 	int err;
6577 	struct net_device *dev = info->user_ptr[1];
6578 	struct mpath_info pinfo;
6579 	struct sk_buff *msg;
6580 	u8 *dst = NULL;
6581 	u8 mpp[ETH_ALEN];
6582 
6583 	memset(&pinfo, 0, sizeof(pinfo));
6584 
6585 	if (!info->attrs[NL80211_ATTR_MAC])
6586 		return -EINVAL;
6587 
6588 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6589 
6590 	if (!rdev->ops->get_mpp)
6591 		return -EOPNOTSUPP;
6592 
6593 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6594 		return -EOPNOTSUPP;
6595 
6596 	err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
6597 	if (err)
6598 		return err;
6599 
6600 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6601 	if (!msg)
6602 		return -ENOMEM;
6603 
6604 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
6605 			       dev, dst, mpp, &pinfo) < 0) {
6606 		nlmsg_free(msg);
6607 		return -ENOBUFS;
6608 	}
6609 
6610 	return genlmsg_reply(msg, info);
6611 }
6612 
6613 static int nl80211_dump_mpp(struct sk_buff *skb,
6614 			    struct netlink_callback *cb)
6615 {
6616 	struct mpath_info pinfo;
6617 	struct cfg80211_registered_device *rdev;
6618 	struct wireless_dev *wdev;
6619 	u8 dst[ETH_ALEN];
6620 	u8 mpp[ETH_ALEN];
6621 	int path_idx = cb->args[2];
6622 	int err;
6623 
6624 	rtnl_lock();
6625 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
6626 	if (err)
6627 		goto out_err;
6628 
6629 	if (!rdev->ops->dump_mpp) {
6630 		err = -EOPNOTSUPP;
6631 		goto out_err;
6632 	}
6633 
6634 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
6635 		err = -EOPNOTSUPP;
6636 		goto out_err;
6637 	}
6638 
6639 	while (1) {
6640 		err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
6641 				    mpp, &pinfo);
6642 		if (err == -ENOENT)
6643 			break;
6644 		if (err)
6645 			goto out_err;
6646 
6647 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
6648 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
6649 				       wdev->netdev, dst, mpp,
6650 				       &pinfo) < 0)
6651 			goto out;
6652 
6653 		path_idx++;
6654 	}
6655 
6656  out:
6657 	cb->args[2] = path_idx;
6658 	err = skb->len;
6659  out_err:
6660 	rtnl_unlock();
6661 	return err;
6662 }
6663 
6664 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
6665 {
6666 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6667 	struct net_device *dev = info->user_ptr[1];
6668 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6669 	struct bss_parameters params;
6670 	int err;
6671 
6672 	memset(&params, 0, sizeof(params));
6673 	/* default to not changing parameters */
6674 	params.use_cts_prot = -1;
6675 	params.use_short_preamble = -1;
6676 	params.use_short_slot_time = -1;
6677 	params.ap_isolate = -1;
6678 	params.ht_opmode = -1;
6679 	params.p2p_ctwindow = -1;
6680 	params.p2p_opp_ps = -1;
6681 
6682 	if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
6683 		params.use_cts_prot =
6684 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
6685 	if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
6686 		params.use_short_preamble =
6687 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
6688 	if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
6689 		params.use_short_slot_time =
6690 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
6691 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6692 		params.basic_rates =
6693 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6694 		params.basic_rates_len =
6695 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6696 	}
6697 	if (info->attrs[NL80211_ATTR_AP_ISOLATE])
6698 		params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
6699 	if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
6700 		params.ht_opmode =
6701 			nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
6702 
6703 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
6704 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6705 			return -EINVAL;
6706 		params.p2p_ctwindow =
6707 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
6708 		if (params.p2p_ctwindow != 0 &&
6709 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
6710 			return -EINVAL;
6711 	}
6712 
6713 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
6714 		u8 tmp;
6715 
6716 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6717 			return -EINVAL;
6718 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
6719 		params.p2p_opp_ps = tmp;
6720 		if (params.p2p_opp_ps &&
6721 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
6722 			return -EINVAL;
6723 	}
6724 
6725 	if (!rdev->ops->change_bss)
6726 		return -EOPNOTSUPP;
6727 
6728 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6729 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6730 		return -EOPNOTSUPP;
6731 
6732 	wdev_lock(wdev);
6733 	err = rdev_change_bss(rdev, dev, &params);
6734 	wdev_unlock(wdev);
6735 
6736 	return err;
6737 }
6738 
6739 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
6740 {
6741 	char *data = NULL;
6742 	bool is_indoor;
6743 	enum nl80211_user_reg_hint_type user_reg_hint_type;
6744 	u32 owner_nlportid;
6745 
6746 	/*
6747 	 * You should only get this when cfg80211 hasn't yet initialized
6748 	 * completely when built-in to the kernel right between the time
6749 	 * window between nl80211_init() and regulatory_init(), if that is
6750 	 * even possible.
6751 	 */
6752 	if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
6753 		return -EINPROGRESS;
6754 
6755 	if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
6756 		user_reg_hint_type =
6757 		  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
6758 	else
6759 		user_reg_hint_type = NL80211_USER_REG_HINT_USER;
6760 
6761 	switch (user_reg_hint_type) {
6762 	case NL80211_USER_REG_HINT_USER:
6763 	case NL80211_USER_REG_HINT_CELL_BASE:
6764 		if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
6765 			return -EINVAL;
6766 
6767 		data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
6768 		return regulatory_hint_user(data, user_reg_hint_type);
6769 	case NL80211_USER_REG_HINT_INDOOR:
6770 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
6771 			owner_nlportid = info->snd_portid;
6772 			is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
6773 		} else {
6774 			owner_nlportid = 0;
6775 			is_indoor = true;
6776 		}
6777 
6778 		return regulatory_hint_indoor(is_indoor, owner_nlportid);
6779 	default:
6780 		return -EINVAL;
6781 	}
6782 }
6783 
6784 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
6785 {
6786 	return reg_reload_regdb();
6787 }
6788 
6789 static int nl80211_get_mesh_config(struct sk_buff *skb,
6790 				   struct genl_info *info)
6791 {
6792 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6793 	struct net_device *dev = info->user_ptr[1];
6794 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6795 	struct mesh_config cur_params;
6796 	int err = 0;
6797 	void *hdr;
6798 	struct nlattr *pinfoattr;
6799 	struct sk_buff *msg;
6800 
6801 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6802 		return -EOPNOTSUPP;
6803 
6804 	if (!rdev->ops->get_mesh_config)
6805 		return -EOPNOTSUPP;
6806 
6807 	wdev_lock(wdev);
6808 	/* If not connected, get default parameters */
6809 	if (!wdev->mesh_id_len)
6810 		memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
6811 	else
6812 		err = rdev_get_mesh_config(rdev, dev, &cur_params);
6813 	wdev_unlock(wdev);
6814 
6815 	if (err)
6816 		return err;
6817 
6818 	/* Draw up a netlink message to send back */
6819 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6820 	if (!msg)
6821 		return -ENOMEM;
6822 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
6823 			     NL80211_CMD_GET_MESH_CONFIG);
6824 	if (!hdr)
6825 		goto out;
6826 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
6827 	if (!pinfoattr)
6828 		goto nla_put_failure;
6829 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6830 	    nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
6831 			cur_params.dot11MeshRetryTimeout) ||
6832 	    nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
6833 			cur_params.dot11MeshConfirmTimeout) ||
6834 	    nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
6835 			cur_params.dot11MeshHoldingTimeout) ||
6836 	    nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
6837 			cur_params.dot11MeshMaxPeerLinks) ||
6838 	    nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
6839 		       cur_params.dot11MeshMaxRetries) ||
6840 	    nla_put_u8(msg, NL80211_MESHCONF_TTL,
6841 		       cur_params.dot11MeshTTL) ||
6842 	    nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
6843 		       cur_params.element_ttl) ||
6844 	    nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6845 		       cur_params.auto_open_plinks) ||
6846 	    nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6847 			cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
6848 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6849 		       cur_params.dot11MeshHWMPmaxPREQretries) ||
6850 	    nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
6851 			cur_params.path_refresh_time) ||
6852 	    nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6853 			cur_params.min_discovery_timeout) ||
6854 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6855 			cur_params.dot11MeshHWMPactivePathTimeout) ||
6856 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6857 			cur_params.dot11MeshHWMPpreqMinInterval) ||
6858 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6859 			cur_params.dot11MeshHWMPperrMinInterval) ||
6860 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6861 			cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
6862 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
6863 		       cur_params.dot11MeshHWMPRootMode) ||
6864 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6865 			cur_params.dot11MeshHWMPRannInterval) ||
6866 	    nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6867 		       cur_params.dot11MeshGateAnnouncementProtocol) ||
6868 	    nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
6869 		       cur_params.dot11MeshForwarding) ||
6870 	    nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
6871 			cur_params.rssi_threshold) ||
6872 	    nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
6873 			cur_params.ht_opmode) ||
6874 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6875 			cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
6876 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6877 			cur_params.dot11MeshHWMProotInterval) ||
6878 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6879 			cur_params.dot11MeshHWMPconfirmationInterval) ||
6880 	    nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
6881 			cur_params.power_mode) ||
6882 	    nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
6883 			cur_params.dot11MeshAwakeWindowDuration) ||
6884 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
6885 			cur_params.plink_timeout) ||
6886 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
6887 		       cur_params.dot11MeshConnectedToMeshGate))
6888 		goto nla_put_failure;
6889 	nla_nest_end(msg, pinfoattr);
6890 	genlmsg_end(msg, hdr);
6891 	return genlmsg_reply(msg, info);
6892 
6893  nla_put_failure:
6894  out:
6895 	nlmsg_free(msg);
6896 	return -ENOBUFS;
6897 }
6898 
6899 static const struct nla_policy
6900 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
6901 	[NL80211_MESHCONF_RETRY_TIMEOUT] =
6902 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
6903 	[NL80211_MESHCONF_CONFIRM_TIMEOUT] =
6904 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
6905 	[NL80211_MESHCONF_HOLDING_TIMEOUT] =
6906 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
6907 	[NL80211_MESHCONF_MAX_PEER_LINKS] =
6908 		NLA_POLICY_RANGE(NLA_U16, 0, 255),
6909 	[NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
6910 	[NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
6911 	[NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
6912 	[NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
6913 	[NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
6914 		NLA_POLICY_RANGE(NLA_U32, 1, 255),
6915 	[NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
6916 	[NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
6917 	[NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
6918 	[NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
6919 	[NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
6920 		NLA_POLICY_MIN(NLA_U16, 1),
6921 	[NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
6922 		NLA_POLICY_MIN(NLA_U16, 1),
6923 	[NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
6924 		NLA_POLICY_MIN(NLA_U16, 1),
6925 	[NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
6926 	[NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
6927 		NLA_POLICY_MIN(NLA_U16, 1),
6928 	[NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
6929 	[NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
6930 	[NL80211_MESHCONF_RSSI_THRESHOLD] =
6931 		NLA_POLICY_RANGE(NLA_S32, -255, 0),
6932 	[NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
6933 	[NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
6934 	[NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
6935 		NLA_POLICY_MIN(NLA_U16, 1),
6936 	[NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
6937 		NLA_POLICY_MIN(NLA_U16, 1),
6938 	[NL80211_MESHCONF_POWER_MODE] =
6939 		NLA_POLICY_RANGE(NLA_U32,
6940 				 NL80211_MESH_POWER_ACTIVE,
6941 				 NL80211_MESH_POWER_MAX),
6942 	[NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
6943 	[NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
6944 	[NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
6945 };
6946 
6947 static const struct nla_policy
6948 	nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
6949 	[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
6950 	[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
6951 	[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
6952 	[NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
6953 	[NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
6954 	[NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
6955 	[NL80211_MESH_SETUP_IE] =
6956 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
6957 				       IEEE80211_MAX_DATA_LEN),
6958 	[NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
6959 };
6960 
6961 static int nl80211_parse_mesh_config(struct genl_info *info,
6962 				     struct mesh_config *cfg,
6963 				     u32 *mask_out)
6964 {
6965 	struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
6966 	u32 mask = 0;
6967 	u16 ht_opmode;
6968 
6969 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)	\
6970 do {									\
6971 	if (tb[attr]) {							\
6972 		cfg->param = fn(tb[attr]);				\
6973 		mask |= BIT((attr) - 1);				\
6974 	}								\
6975 } while (0)
6976 
6977 	if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
6978 		return -EINVAL;
6979 	if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
6980 		return -EINVAL;
6981 
6982 	/* This makes sure that there aren't more than 32 mesh config
6983 	 * parameters (otherwise our bitfield scheme would not work.) */
6984 	BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
6985 
6986 	/* Fill in the params struct */
6987 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
6988 				  NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
6989 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
6990 				  NL80211_MESHCONF_CONFIRM_TIMEOUT,
6991 				  nla_get_u16);
6992 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
6993 				  NL80211_MESHCONF_HOLDING_TIMEOUT,
6994 				  nla_get_u16);
6995 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
6996 				  NL80211_MESHCONF_MAX_PEER_LINKS,
6997 				  nla_get_u16);
6998 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
6999 				  NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
7000 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
7001 				  NL80211_MESHCONF_TTL, nla_get_u8);
7002 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
7003 				  NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
7004 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
7005 				  NL80211_MESHCONF_AUTO_OPEN_PLINKS,
7006 				  nla_get_u8);
7007 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
7008 				  mask,
7009 				  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
7010 				  nla_get_u32);
7011 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
7012 				  NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
7013 				  nla_get_u8);
7014 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
7015 				  NL80211_MESHCONF_PATH_REFRESH_TIME,
7016 				  nla_get_u32);
7017 	if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
7018 	    (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
7019 		return -EINVAL;
7020 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
7021 				  NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
7022 				  nla_get_u16);
7023 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
7024 				  mask,
7025 				  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
7026 				  nla_get_u32);
7027 	if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
7028 	    (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
7029 	     cfg->dot11MeshHWMPactivePathTimeout > 65535))
7030 		return -EINVAL;
7031 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
7032 				  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
7033 				  nla_get_u16);
7034 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
7035 				  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
7036 				  nla_get_u16);
7037 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
7038 				  dot11MeshHWMPnetDiameterTraversalTime, mask,
7039 				  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
7040 				  nla_get_u16);
7041 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
7042 				  NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
7043 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
7044 				  NL80211_MESHCONF_HWMP_RANN_INTERVAL,
7045 				  nla_get_u16);
7046 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
7047 				  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
7048 				  nla_get_u8);
7049 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
7050 				  NL80211_MESHCONF_FORWARDING, nla_get_u8);
7051 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
7052 				  NL80211_MESHCONF_RSSI_THRESHOLD,
7053 				  nla_get_s32);
7054 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
7055 				  NL80211_MESHCONF_CONNECTED_TO_GATE,
7056 				  nla_get_u8);
7057 	/*
7058 	 * Check HT operation mode based on
7059 	 * IEEE 802.11-2016 9.4.2.57 HT Operation element.
7060 	 */
7061 	if (tb[NL80211_MESHCONF_HT_OPMODE]) {
7062 		ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
7063 
7064 		if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
7065 				  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
7066 				  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
7067 			return -EINVAL;
7068 
7069 		/* NON_HT_STA bit is reserved, but some programs set it */
7070 		ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
7071 
7072 		cfg->ht_opmode = ht_opmode;
7073 		mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
7074 	}
7075 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
7076 				  dot11MeshHWMPactivePathToRootTimeout, mask,
7077 				  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
7078 				  nla_get_u32);
7079 	if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
7080 	    (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
7081 	     cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
7082 		return -EINVAL;
7083 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
7084 				  NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
7085 				  nla_get_u16);
7086 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
7087 				  mask,
7088 				  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
7089 				  nla_get_u16);
7090 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
7091 				  NL80211_MESHCONF_POWER_MODE, nla_get_u32);
7092 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
7093 				  NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
7094 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
7095 				  NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
7096 	if (mask_out)
7097 		*mask_out = mask;
7098 
7099 	return 0;
7100 
7101 #undef FILL_IN_MESH_PARAM_IF_SET
7102 }
7103 
7104 static int nl80211_parse_mesh_setup(struct genl_info *info,
7105 				     struct mesh_setup *setup)
7106 {
7107 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7108 	struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
7109 
7110 	if (!info->attrs[NL80211_ATTR_MESH_SETUP])
7111 		return -EINVAL;
7112 	if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
7113 		return -EINVAL;
7114 
7115 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
7116 		setup->sync_method =
7117 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
7118 		 IEEE80211_SYNC_METHOD_VENDOR :
7119 		 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
7120 
7121 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
7122 		setup->path_sel_proto =
7123 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
7124 		 IEEE80211_PATH_PROTOCOL_VENDOR :
7125 		 IEEE80211_PATH_PROTOCOL_HWMP;
7126 
7127 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
7128 		setup->path_metric =
7129 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
7130 		 IEEE80211_PATH_METRIC_VENDOR :
7131 		 IEEE80211_PATH_METRIC_AIRTIME;
7132 
7133 	if (tb[NL80211_MESH_SETUP_IE]) {
7134 		struct nlattr *ieattr =
7135 			tb[NL80211_MESH_SETUP_IE];
7136 		setup->ie = nla_data(ieattr);
7137 		setup->ie_len = nla_len(ieattr);
7138 	}
7139 	if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
7140 	    !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
7141 		return -EINVAL;
7142 	setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
7143 	setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
7144 	setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
7145 	if (setup->is_secure)
7146 		setup->user_mpm = true;
7147 
7148 	if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
7149 		if (!setup->user_mpm)
7150 			return -EINVAL;
7151 		setup->auth_id =
7152 			nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
7153 	}
7154 
7155 	return 0;
7156 }
7157 
7158 static int nl80211_update_mesh_config(struct sk_buff *skb,
7159 				      struct genl_info *info)
7160 {
7161 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7162 	struct net_device *dev = info->user_ptr[1];
7163 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7164 	struct mesh_config cfg;
7165 	u32 mask;
7166 	int err;
7167 
7168 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
7169 		return -EOPNOTSUPP;
7170 
7171 	if (!rdev->ops->update_mesh_config)
7172 		return -EOPNOTSUPP;
7173 
7174 	err = nl80211_parse_mesh_config(info, &cfg, &mask);
7175 	if (err)
7176 		return err;
7177 
7178 	wdev_lock(wdev);
7179 	if (!wdev->mesh_id_len)
7180 		err = -ENOLINK;
7181 
7182 	if (!err)
7183 		err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
7184 
7185 	wdev_unlock(wdev);
7186 
7187 	return err;
7188 }
7189 
7190 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
7191 			      struct sk_buff *msg)
7192 {
7193 	struct nlattr *nl_reg_rules;
7194 	unsigned int i;
7195 
7196 	if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
7197 	    (regdom->dfs_region &&
7198 	     nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
7199 		goto nla_put_failure;
7200 
7201 	nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
7202 	if (!nl_reg_rules)
7203 		goto nla_put_failure;
7204 
7205 	for (i = 0; i < regdom->n_reg_rules; i++) {
7206 		struct nlattr *nl_reg_rule;
7207 		const struct ieee80211_reg_rule *reg_rule;
7208 		const struct ieee80211_freq_range *freq_range;
7209 		const struct ieee80211_power_rule *power_rule;
7210 		unsigned int max_bandwidth_khz;
7211 
7212 		reg_rule = &regdom->reg_rules[i];
7213 		freq_range = &reg_rule->freq_range;
7214 		power_rule = &reg_rule->power_rule;
7215 
7216 		nl_reg_rule = nla_nest_start_noflag(msg, i);
7217 		if (!nl_reg_rule)
7218 			goto nla_put_failure;
7219 
7220 		max_bandwidth_khz = freq_range->max_bandwidth_khz;
7221 		if (!max_bandwidth_khz)
7222 			max_bandwidth_khz = reg_get_max_bandwidth(regdom,
7223 								  reg_rule);
7224 
7225 		if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
7226 				reg_rule->flags) ||
7227 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
7228 				freq_range->start_freq_khz) ||
7229 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
7230 				freq_range->end_freq_khz) ||
7231 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
7232 				max_bandwidth_khz) ||
7233 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
7234 				power_rule->max_antenna_gain) ||
7235 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
7236 				power_rule->max_eirp) ||
7237 		    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
7238 				reg_rule->dfs_cac_ms))
7239 			goto nla_put_failure;
7240 
7241 		nla_nest_end(msg, nl_reg_rule);
7242 	}
7243 
7244 	nla_nest_end(msg, nl_reg_rules);
7245 	return 0;
7246 
7247 nla_put_failure:
7248 	return -EMSGSIZE;
7249 }
7250 
7251 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
7252 {
7253 	const struct ieee80211_regdomain *regdom = NULL;
7254 	struct cfg80211_registered_device *rdev;
7255 	struct wiphy *wiphy = NULL;
7256 	struct sk_buff *msg;
7257 	void *hdr;
7258 
7259 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7260 	if (!msg)
7261 		return -ENOBUFS;
7262 
7263 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7264 			     NL80211_CMD_GET_REG);
7265 	if (!hdr)
7266 		goto put_failure;
7267 
7268 	if (info->attrs[NL80211_ATTR_WIPHY]) {
7269 		bool self_managed;
7270 
7271 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
7272 		if (IS_ERR(rdev)) {
7273 			nlmsg_free(msg);
7274 			return PTR_ERR(rdev);
7275 		}
7276 
7277 		wiphy = &rdev->wiphy;
7278 		self_managed = wiphy->regulatory_flags &
7279 			       REGULATORY_WIPHY_SELF_MANAGED;
7280 		regdom = get_wiphy_regdom(wiphy);
7281 
7282 		/* a self-managed-reg device must have a private regdom */
7283 		if (WARN_ON(!regdom && self_managed)) {
7284 			nlmsg_free(msg);
7285 			return -EINVAL;
7286 		}
7287 
7288 		if (regdom &&
7289 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7290 			goto nla_put_failure;
7291 	}
7292 
7293 	if (!wiphy && reg_last_request_cell_base() &&
7294 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
7295 			NL80211_USER_REG_HINT_CELL_BASE))
7296 		goto nla_put_failure;
7297 
7298 	rcu_read_lock();
7299 
7300 	if (!regdom)
7301 		regdom = rcu_dereference(cfg80211_regdomain);
7302 
7303 	if (nl80211_put_regdom(regdom, msg))
7304 		goto nla_put_failure_rcu;
7305 
7306 	rcu_read_unlock();
7307 
7308 	genlmsg_end(msg, hdr);
7309 	return genlmsg_reply(msg, info);
7310 
7311 nla_put_failure_rcu:
7312 	rcu_read_unlock();
7313 nla_put_failure:
7314 put_failure:
7315 	nlmsg_free(msg);
7316 	return -EMSGSIZE;
7317 }
7318 
7319 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
7320 			       u32 seq, int flags, struct wiphy *wiphy,
7321 			       const struct ieee80211_regdomain *regdom)
7322 {
7323 	void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
7324 				   NL80211_CMD_GET_REG);
7325 
7326 	if (!hdr)
7327 		return -1;
7328 
7329 	genl_dump_check_consistent(cb, hdr);
7330 
7331 	if (nl80211_put_regdom(regdom, msg))
7332 		goto nla_put_failure;
7333 
7334 	if (!wiphy && reg_last_request_cell_base() &&
7335 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
7336 			NL80211_USER_REG_HINT_CELL_BASE))
7337 		goto nla_put_failure;
7338 
7339 	if (wiphy &&
7340 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7341 		goto nla_put_failure;
7342 
7343 	if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
7344 	    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
7345 		goto nla_put_failure;
7346 
7347 	genlmsg_end(msg, hdr);
7348 	return 0;
7349 
7350 nla_put_failure:
7351 	genlmsg_cancel(msg, hdr);
7352 	return -EMSGSIZE;
7353 }
7354 
7355 static int nl80211_get_reg_dump(struct sk_buff *skb,
7356 				struct netlink_callback *cb)
7357 {
7358 	const struct ieee80211_regdomain *regdom = NULL;
7359 	struct cfg80211_registered_device *rdev;
7360 	int err, reg_idx, start = cb->args[2];
7361 
7362 	rtnl_lock();
7363 
7364 	if (cfg80211_regdomain && start == 0) {
7365 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7366 					  NLM_F_MULTI, NULL,
7367 					  rtnl_dereference(cfg80211_regdomain));
7368 		if (err < 0)
7369 			goto out_err;
7370 	}
7371 
7372 	/* the global regdom is idx 0 */
7373 	reg_idx = 1;
7374 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
7375 		regdom = get_wiphy_regdom(&rdev->wiphy);
7376 		if (!regdom)
7377 			continue;
7378 
7379 		if (++reg_idx <= start)
7380 			continue;
7381 
7382 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7383 					  NLM_F_MULTI, &rdev->wiphy, regdom);
7384 		if (err < 0) {
7385 			reg_idx--;
7386 			break;
7387 		}
7388 	}
7389 
7390 	cb->args[2] = reg_idx;
7391 	err = skb->len;
7392 out_err:
7393 	rtnl_unlock();
7394 	return err;
7395 }
7396 
7397 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
7398 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
7399 	[NL80211_ATTR_REG_RULE_FLAGS]		= { .type = NLA_U32 },
7400 	[NL80211_ATTR_FREQ_RANGE_START]		= { .type = NLA_U32 },
7401 	[NL80211_ATTR_FREQ_RANGE_END]		= { .type = NLA_U32 },
7402 	[NL80211_ATTR_FREQ_RANGE_MAX_BW]	= { .type = NLA_U32 },
7403 	[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]	= { .type = NLA_U32 },
7404 	[NL80211_ATTR_POWER_RULE_MAX_EIRP]	= { .type = NLA_U32 },
7405 	[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
7406 };
7407 
7408 static int parse_reg_rule(struct nlattr *tb[],
7409 	struct ieee80211_reg_rule *reg_rule)
7410 {
7411 	struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
7412 	struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
7413 
7414 	if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
7415 		return -EINVAL;
7416 	if (!tb[NL80211_ATTR_FREQ_RANGE_START])
7417 		return -EINVAL;
7418 	if (!tb[NL80211_ATTR_FREQ_RANGE_END])
7419 		return -EINVAL;
7420 	if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
7421 		return -EINVAL;
7422 	if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
7423 		return -EINVAL;
7424 
7425 	reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
7426 
7427 	freq_range->start_freq_khz =
7428 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
7429 	freq_range->end_freq_khz =
7430 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
7431 	freq_range->max_bandwidth_khz =
7432 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
7433 
7434 	power_rule->max_eirp =
7435 		nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
7436 
7437 	if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
7438 		power_rule->max_antenna_gain =
7439 			nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
7440 
7441 	if (tb[NL80211_ATTR_DFS_CAC_TIME])
7442 		reg_rule->dfs_cac_ms =
7443 			nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
7444 
7445 	return 0;
7446 }
7447 
7448 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
7449 {
7450 	struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
7451 	struct nlattr *nl_reg_rule;
7452 	char *alpha2;
7453 	int rem_reg_rules, r;
7454 	u32 num_rules = 0, rule_idx = 0;
7455 	enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
7456 	struct ieee80211_regdomain *rd;
7457 
7458 	if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
7459 		return -EINVAL;
7460 
7461 	if (!info->attrs[NL80211_ATTR_REG_RULES])
7462 		return -EINVAL;
7463 
7464 	alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
7465 
7466 	if (info->attrs[NL80211_ATTR_DFS_REGION])
7467 		dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
7468 
7469 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7470 			    rem_reg_rules) {
7471 		num_rules++;
7472 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
7473 			return -EINVAL;
7474 	}
7475 
7476 	if (!reg_is_valid_request(alpha2))
7477 		return -EINVAL;
7478 
7479 	rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
7480 	if (!rd)
7481 		return -ENOMEM;
7482 
7483 	rd->n_reg_rules = num_rules;
7484 	rd->alpha2[0] = alpha2[0];
7485 	rd->alpha2[1] = alpha2[1];
7486 
7487 	/*
7488 	 * Disable DFS master mode if the DFS region was
7489 	 * not supported or known on this kernel.
7490 	 */
7491 	if (reg_supported_dfs_region(dfs_region))
7492 		rd->dfs_region = dfs_region;
7493 
7494 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7495 			    rem_reg_rules) {
7496 		r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
7497 						nl_reg_rule, reg_rule_policy,
7498 						info->extack);
7499 		if (r)
7500 			goto bad_reg;
7501 		r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
7502 		if (r)
7503 			goto bad_reg;
7504 
7505 		rule_idx++;
7506 
7507 		if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
7508 			r = -EINVAL;
7509 			goto bad_reg;
7510 		}
7511 	}
7512 
7513 	/* set_regdom takes ownership of rd */
7514 	return set_regdom(rd, REGD_SOURCE_CRDA);
7515  bad_reg:
7516 	kfree(rd);
7517 	return r;
7518 }
7519 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
7520 
7521 static int validate_scan_freqs(struct nlattr *freqs)
7522 {
7523 	struct nlattr *attr1, *attr2;
7524 	int n_channels = 0, tmp1, tmp2;
7525 
7526 	nla_for_each_nested(attr1, freqs, tmp1)
7527 		if (nla_len(attr1) != sizeof(u32))
7528 			return 0;
7529 
7530 	nla_for_each_nested(attr1, freqs, tmp1) {
7531 		n_channels++;
7532 		/*
7533 		 * Some hardware has a limited channel list for
7534 		 * scanning, and it is pretty much nonsensical
7535 		 * to scan for a channel twice, so disallow that
7536 		 * and don't require drivers to check that the
7537 		 * channel list they get isn't longer than what
7538 		 * they can scan, as long as they can scan all
7539 		 * the channels they registered at once.
7540 		 */
7541 		nla_for_each_nested(attr2, freqs, tmp2)
7542 			if (attr1 != attr2 &&
7543 			    nla_get_u32(attr1) == nla_get_u32(attr2))
7544 				return 0;
7545 	}
7546 
7547 	return n_channels;
7548 }
7549 
7550 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
7551 {
7552 	return b < NUM_NL80211_BANDS && wiphy->bands[b];
7553 }
7554 
7555 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
7556 			    struct cfg80211_bss_selection *bss_select)
7557 {
7558 	struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
7559 	struct nlattr *nest;
7560 	int err;
7561 	bool found = false;
7562 	int i;
7563 
7564 	/* only process one nested attribute */
7565 	nest = nla_data(nla);
7566 	if (!nla_ok(nest, nla_len(nest)))
7567 		return -EINVAL;
7568 
7569 	err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
7570 					  nest, nl80211_bss_select_policy,
7571 					  NULL);
7572 	if (err)
7573 		return err;
7574 
7575 	/* only one attribute may be given */
7576 	for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
7577 		if (attr[i]) {
7578 			if (found)
7579 				return -EINVAL;
7580 			found = true;
7581 		}
7582 	}
7583 
7584 	bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
7585 
7586 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
7587 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
7588 
7589 	if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
7590 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
7591 		bss_select->param.band_pref =
7592 			nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
7593 		if (!is_band_valid(wiphy, bss_select->param.band_pref))
7594 			return -EINVAL;
7595 	}
7596 
7597 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
7598 		struct nl80211_bss_select_rssi_adjust *adj_param;
7599 
7600 		adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
7601 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
7602 		bss_select->param.adjust.band = adj_param->band;
7603 		bss_select->param.adjust.delta = adj_param->delta;
7604 		if (!is_band_valid(wiphy, bss_select->param.adjust.band))
7605 			return -EINVAL;
7606 	}
7607 
7608 	/* user-space did not provide behaviour attribute */
7609 	if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
7610 		return -EINVAL;
7611 
7612 	if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
7613 		return -EINVAL;
7614 
7615 	return 0;
7616 }
7617 
7618 int nl80211_parse_random_mac(struct nlattr **attrs,
7619 			     u8 *mac_addr, u8 *mac_addr_mask)
7620 {
7621 	int i;
7622 
7623 	if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
7624 		eth_zero_addr(mac_addr);
7625 		eth_zero_addr(mac_addr_mask);
7626 		mac_addr[0] = 0x2;
7627 		mac_addr_mask[0] = 0x3;
7628 
7629 		return 0;
7630 	}
7631 
7632 	/* need both or none */
7633 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
7634 		return -EINVAL;
7635 
7636 	memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
7637 	memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
7638 
7639 	/* don't allow or configure an mcast address */
7640 	if (!is_multicast_ether_addr(mac_addr_mask) ||
7641 	    is_multicast_ether_addr(mac_addr))
7642 		return -EINVAL;
7643 
7644 	/*
7645 	 * allow users to pass a MAC address that has bits set outside
7646 	 * of the mask, but don't bother drivers with having to deal
7647 	 * with such bits
7648 	 */
7649 	for (i = 0; i < ETH_ALEN; i++)
7650 		mac_addr[i] &= mac_addr_mask[i];
7651 
7652 	return 0;
7653 }
7654 
7655 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev)
7656 {
7657 	ASSERT_WDEV_LOCK(wdev);
7658 
7659 	if (!cfg80211_beaconing_iface_active(wdev))
7660 		return true;
7661 
7662 	if (!(wdev->chandef.chan->flags & IEEE80211_CHAN_RADAR))
7663 		return true;
7664 
7665 	return regulatory_pre_cac_allowed(wdev->wiphy);
7666 }
7667 
7668 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
7669 				    enum nl80211_ext_feature_index feat)
7670 {
7671 	if (!(flags & flag))
7672 		return true;
7673 	if (wiphy_ext_feature_isset(wiphy, feat))
7674 		return true;
7675 	return false;
7676 }
7677 
7678 static int
7679 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
7680 			 void *request, struct nlattr **attrs,
7681 			 bool is_sched_scan)
7682 {
7683 	u8 *mac_addr, *mac_addr_mask;
7684 	u32 *flags;
7685 	enum nl80211_feature_flags randomness_flag;
7686 
7687 	if (!attrs[NL80211_ATTR_SCAN_FLAGS])
7688 		return 0;
7689 
7690 	if (is_sched_scan) {
7691 		struct cfg80211_sched_scan_request *req = request;
7692 
7693 		randomness_flag = wdev ?
7694 				  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
7695 				  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
7696 		flags = &req->flags;
7697 		mac_addr = req->mac_addr;
7698 		mac_addr_mask = req->mac_addr_mask;
7699 	} else {
7700 		struct cfg80211_scan_request *req = request;
7701 
7702 		randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
7703 		flags = &req->flags;
7704 		mac_addr = req->mac_addr;
7705 		mac_addr_mask = req->mac_addr_mask;
7706 	}
7707 
7708 	*flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
7709 
7710 	if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
7711 	     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
7712 	    !nl80211_check_scan_feat(wiphy, *flags,
7713 				     NL80211_SCAN_FLAG_LOW_SPAN,
7714 				     NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
7715 	    !nl80211_check_scan_feat(wiphy, *flags,
7716 				     NL80211_SCAN_FLAG_LOW_POWER,
7717 				     NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
7718 	    !nl80211_check_scan_feat(wiphy, *flags,
7719 				     NL80211_SCAN_FLAG_HIGH_ACCURACY,
7720 				     NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
7721 	    !nl80211_check_scan_feat(wiphy, *flags,
7722 				     NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
7723 				     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
7724 	    !nl80211_check_scan_feat(wiphy, *flags,
7725 				     NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
7726 				     NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
7727 	    !nl80211_check_scan_feat(wiphy, *flags,
7728 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
7729 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
7730 	    !nl80211_check_scan_feat(wiphy, *flags,
7731 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
7732 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
7733 	    !nl80211_check_scan_feat(wiphy, *flags,
7734 				     NL80211_SCAN_FLAG_RANDOM_SN,
7735 				     NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
7736 	    !nl80211_check_scan_feat(wiphy, *flags,
7737 				     NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
7738 				     NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
7739 		return -EOPNOTSUPP;
7740 
7741 	if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
7742 		int err;
7743 
7744 		if (!(wiphy->features & randomness_flag) ||
7745 		    (wdev && wdev->current_bss))
7746 			return -EOPNOTSUPP;
7747 
7748 		err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
7749 		if (err)
7750 			return err;
7751 	}
7752 
7753 	return 0;
7754 }
7755 
7756 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
7757 {
7758 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7759 	struct wireless_dev *wdev = info->user_ptr[1];
7760 	struct cfg80211_scan_request *request;
7761 	struct nlattr *scan_freqs = NULL;
7762 	bool scan_freqs_khz = false;
7763 	struct nlattr *attr;
7764 	struct wiphy *wiphy;
7765 	int err, tmp, n_ssids = 0, n_channels, i;
7766 	size_t ie_len;
7767 
7768 	wiphy = &rdev->wiphy;
7769 
7770 	if (wdev->iftype == NL80211_IFTYPE_NAN)
7771 		return -EOPNOTSUPP;
7772 
7773 	if (!rdev->ops->scan)
7774 		return -EOPNOTSUPP;
7775 
7776 	if (rdev->scan_req || rdev->scan_msg) {
7777 		err = -EBUSY;
7778 		goto unlock;
7779 	}
7780 
7781 	if (info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ]) {
7782 		if (!wiphy_ext_feature_isset(wiphy,
7783 					     NL80211_EXT_FEATURE_SCAN_FREQ_KHZ))
7784 			return -EOPNOTSUPP;
7785 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ];
7786 		scan_freqs_khz = true;
7787 	} else if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES])
7788 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQUENCIES];
7789 
7790 	if (scan_freqs) {
7791 		n_channels = validate_scan_freqs(scan_freqs);
7792 		if (!n_channels) {
7793 			err = -EINVAL;
7794 			goto unlock;
7795 		}
7796 	} else {
7797 		n_channels = ieee80211_get_num_supported_channels(wiphy);
7798 	}
7799 
7800 	if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
7801 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
7802 			n_ssids++;
7803 
7804 	if (n_ssids > wiphy->max_scan_ssids) {
7805 		err = -EINVAL;
7806 		goto unlock;
7807 	}
7808 
7809 	if (info->attrs[NL80211_ATTR_IE])
7810 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7811 	else
7812 		ie_len = 0;
7813 
7814 	if (ie_len > wiphy->max_scan_ie_len) {
7815 		err = -EINVAL;
7816 		goto unlock;
7817 	}
7818 
7819 	request = kzalloc(sizeof(*request)
7820 			+ sizeof(*request->ssids) * n_ssids
7821 			+ sizeof(*request->channels) * n_channels
7822 			+ ie_len, GFP_KERNEL);
7823 	if (!request) {
7824 		err = -ENOMEM;
7825 		goto unlock;
7826 	}
7827 
7828 	if (n_ssids)
7829 		request->ssids = (void *)&request->channels[n_channels];
7830 	request->n_ssids = n_ssids;
7831 	if (ie_len) {
7832 		if (n_ssids)
7833 			request->ie = (void *)(request->ssids + n_ssids);
7834 		else
7835 			request->ie = (void *)(request->channels + n_channels);
7836 	}
7837 
7838 	i = 0;
7839 	if (scan_freqs) {
7840 		/* user specified, bail out if channel not found */
7841 		nla_for_each_nested(attr, scan_freqs, tmp) {
7842 			struct ieee80211_channel *chan;
7843 			int freq = nla_get_u32(attr);
7844 
7845 			if (!scan_freqs_khz)
7846 				freq = MHZ_TO_KHZ(freq);
7847 
7848 			chan = ieee80211_get_channel_khz(wiphy, freq);
7849 			if (!chan) {
7850 				err = -EINVAL;
7851 				goto out_free;
7852 			}
7853 
7854 			/* ignore disabled channels */
7855 			if (chan->flags & IEEE80211_CHAN_DISABLED)
7856 				continue;
7857 
7858 			request->channels[i] = chan;
7859 			i++;
7860 		}
7861 	} else {
7862 		enum nl80211_band band;
7863 
7864 		/* all channels */
7865 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
7866 			int j;
7867 
7868 			if (!wiphy->bands[band])
7869 				continue;
7870 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
7871 				struct ieee80211_channel *chan;
7872 
7873 				chan = &wiphy->bands[band]->channels[j];
7874 
7875 				if (chan->flags & IEEE80211_CHAN_DISABLED)
7876 					continue;
7877 
7878 				request->channels[i] = chan;
7879 				i++;
7880 			}
7881 		}
7882 	}
7883 
7884 	if (!i) {
7885 		err = -EINVAL;
7886 		goto out_free;
7887 	}
7888 
7889 	request->n_channels = i;
7890 
7891 	wdev_lock(wdev);
7892 	if (!cfg80211_off_channel_oper_allowed(wdev)) {
7893 		struct ieee80211_channel *chan;
7894 
7895 		if (request->n_channels != 1) {
7896 			wdev_unlock(wdev);
7897 			err = -EBUSY;
7898 			goto out_free;
7899 		}
7900 
7901 		chan = request->channels[0];
7902 		if (chan->center_freq != wdev->chandef.chan->center_freq) {
7903 			wdev_unlock(wdev);
7904 			err = -EBUSY;
7905 			goto out_free;
7906 		}
7907 	}
7908 	wdev_unlock(wdev);
7909 
7910 	i = 0;
7911 	if (n_ssids) {
7912 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
7913 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
7914 				err = -EINVAL;
7915 				goto out_free;
7916 			}
7917 			request->ssids[i].ssid_len = nla_len(attr);
7918 			memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
7919 			i++;
7920 		}
7921 	}
7922 
7923 	if (info->attrs[NL80211_ATTR_IE]) {
7924 		request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7925 		memcpy((void *)request->ie,
7926 		       nla_data(info->attrs[NL80211_ATTR_IE]),
7927 		       request->ie_len);
7928 	}
7929 
7930 	for (i = 0; i < NUM_NL80211_BANDS; i++)
7931 		if (wiphy->bands[i])
7932 			request->rates[i] =
7933 				(1 << wiphy->bands[i]->n_bitrates) - 1;
7934 
7935 	if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
7936 		nla_for_each_nested(attr,
7937 				    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
7938 				    tmp) {
7939 			enum nl80211_band band = nla_type(attr);
7940 
7941 			if (band < 0 || band >= NUM_NL80211_BANDS) {
7942 				err = -EINVAL;
7943 				goto out_free;
7944 			}
7945 
7946 			if (!wiphy->bands[band])
7947 				continue;
7948 
7949 			err = ieee80211_get_ratemask(wiphy->bands[band],
7950 						     nla_data(attr),
7951 						     nla_len(attr),
7952 						     &request->rates[band]);
7953 			if (err)
7954 				goto out_free;
7955 		}
7956 	}
7957 
7958 	if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
7959 		if (!wiphy_ext_feature_isset(wiphy,
7960 					NL80211_EXT_FEATURE_SET_SCAN_DWELL)) {
7961 			err = -EOPNOTSUPP;
7962 			goto out_free;
7963 		}
7964 
7965 		request->duration =
7966 			nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
7967 		request->duration_mandatory =
7968 			nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
7969 	}
7970 
7971 	err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
7972 				       false);
7973 	if (err)
7974 		goto out_free;
7975 
7976 	request->no_cck =
7977 		nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7978 
7979 	/* Initial implementation used NL80211_ATTR_MAC to set the specific
7980 	 * BSSID to scan for. This was problematic because that same attribute
7981 	 * was already used for another purpose (local random MAC address). The
7982 	 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
7983 	 * compatibility with older userspace components, also use the
7984 	 * NL80211_ATTR_MAC value here if it can be determined to be used for
7985 	 * the specific BSSID use case instead of the random MAC address
7986 	 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
7987 	 */
7988 	if (info->attrs[NL80211_ATTR_BSSID])
7989 		memcpy(request->bssid,
7990 		       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
7991 	else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
7992 		 info->attrs[NL80211_ATTR_MAC])
7993 		memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
7994 		       ETH_ALEN);
7995 	else
7996 		eth_broadcast_addr(request->bssid);
7997 
7998 	request->wdev = wdev;
7999 	request->wiphy = &rdev->wiphy;
8000 	request->scan_start = jiffies;
8001 
8002 	rdev->scan_req = request;
8003 	err = rdev_scan(rdev, request);
8004 
8005 	if (!err) {
8006 		nl80211_send_scan_start(rdev, wdev);
8007 		if (wdev->netdev)
8008 			dev_hold(wdev->netdev);
8009 	} else {
8010  out_free:
8011 		rdev->scan_req = NULL;
8012 		kfree(request);
8013 	}
8014 
8015  unlock:
8016 	return err;
8017 }
8018 
8019 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
8020 {
8021 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8022 	struct wireless_dev *wdev = info->user_ptr[1];
8023 
8024 	if (!rdev->ops->abort_scan)
8025 		return -EOPNOTSUPP;
8026 
8027 	if (rdev->scan_msg)
8028 		return 0;
8029 
8030 	if (!rdev->scan_req)
8031 		return -ENOENT;
8032 
8033 	rdev_abort_scan(rdev, wdev);
8034 	return 0;
8035 }
8036 
8037 static int
8038 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
8039 			       struct cfg80211_sched_scan_request *request,
8040 			       struct nlattr **attrs)
8041 {
8042 	int tmp, err, i = 0;
8043 	struct nlattr *attr;
8044 
8045 	if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
8046 		u32 interval;
8047 
8048 		/*
8049 		 * If scan plans are not specified,
8050 		 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
8051 		 * case one scan plan will be set with the specified scan
8052 		 * interval and infinite number of iterations.
8053 		 */
8054 		interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
8055 		if (!interval)
8056 			return -EINVAL;
8057 
8058 		request->scan_plans[0].interval =
8059 			DIV_ROUND_UP(interval, MSEC_PER_SEC);
8060 		if (!request->scan_plans[0].interval)
8061 			return -EINVAL;
8062 
8063 		if (request->scan_plans[0].interval >
8064 		    wiphy->max_sched_scan_plan_interval)
8065 			request->scan_plans[0].interval =
8066 				wiphy->max_sched_scan_plan_interval;
8067 
8068 		return 0;
8069 	}
8070 
8071 	nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
8072 		struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
8073 
8074 		if (WARN_ON(i >= n_plans))
8075 			return -EINVAL;
8076 
8077 		err = nla_parse_nested_deprecated(plan,
8078 						  NL80211_SCHED_SCAN_PLAN_MAX,
8079 						  attr, nl80211_plan_policy,
8080 						  NULL);
8081 		if (err)
8082 			return err;
8083 
8084 		if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
8085 			return -EINVAL;
8086 
8087 		request->scan_plans[i].interval =
8088 			nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
8089 		if (!request->scan_plans[i].interval ||
8090 		    request->scan_plans[i].interval >
8091 		    wiphy->max_sched_scan_plan_interval)
8092 			return -EINVAL;
8093 
8094 		if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
8095 			request->scan_plans[i].iterations =
8096 				nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
8097 			if (!request->scan_plans[i].iterations ||
8098 			    (request->scan_plans[i].iterations >
8099 			     wiphy->max_sched_scan_plan_iterations))
8100 				return -EINVAL;
8101 		} else if (i < n_plans - 1) {
8102 			/*
8103 			 * All scan plans but the last one must specify
8104 			 * a finite number of iterations
8105 			 */
8106 			return -EINVAL;
8107 		}
8108 
8109 		i++;
8110 	}
8111 
8112 	/*
8113 	 * The last scan plan must not specify the number of
8114 	 * iterations, it is supposed to run infinitely
8115 	 */
8116 	if (request->scan_plans[n_plans - 1].iterations)
8117 		return  -EINVAL;
8118 
8119 	return 0;
8120 }
8121 
8122 static int
8123 nl80211_parse_sched_scan_per_band_rssi(struct wiphy *wiphy,
8124 				       struct cfg80211_match_set *match_sets,
8125 				       struct nlattr *tb_band_rssi,
8126 				       s32 rssi_thold)
8127 {
8128 	struct nlattr *attr;
8129 	int i, tmp, ret = 0;
8130 
8131 	if (!wiphy_ext_feature_isset(wiphy,
8132 		    NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD)) {
8133 		if (tb_band_rssi)
8134 			ret = -EOPNOTSUPP;
8135 		else
8136 			for (i = 0; i < NUM_NL80211_BANDS; i++)
8137 				match_sets->per_band_rssi_thold[i] =
8138 					NL80211_SCAN_RSSI_THOLD_OFF;
8139 		return ret;
8140 	}
8141 
8142 	for (i = 0; i < NUM_NL80211_BANDS; i++)
8143 		match_sets->per_band_rssi_thold[i] = rssi_thold;
8144 
8145 	nla_for_each_nested(attr, tb_band_rssi, tmp) {
8146 		enum nl80211_band band = nla_type(attr);
8147 
8148 		if (band < 0 || band >= NUM_NL80211_BANDS)
8149 			return -EINVAL;
8150 
8151 		match_sets->per_band_rssi_thold[band] =	nla_get_s32(attr);
8152 	}
8153 
8154 	return 0;
8155 }
8156 
8157 static struct cfg80211_sched_scan_request *
8158 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
8159 			 struct nlattr **attrs, int max_match_sets)
8160 {
8161 	struct cfg80211_sched_scan_request *request;
8162 	struct nlattr *attr;
8163 	int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
8164 	enum nl80211_band band;
8165 	size_t ie_len;
8166 	struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
8167 	s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
8168 
8169 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
8170 		n_channels = validate_scan_freqs(
8171 				attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
8172 		if (!n_channels)
8173 			return ERR_PTR(-EINVAL);
8174 	} else {
8175 		n_channels = ieee80211_get_num_supported_channels(wiphy);
8176 	}
8177 
8178 	if (attrs[NL80211_ATTR_SCAN_SSIDS])
8179 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
8180 				    tmp)
8181 			n_ssids++;
8182 
8183 	if (n_ssids > wiphy->max_sched_scan_ssids)
8184 		return ERR_PTR(-EINVAL);
8185 
8186 	/*
8187 	 * First, count the number of 'real' matchsets. Due to an issue with
8188 	 * the old implementation, matchsets containing only the RSSI attribute
8189 	 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
8190 	 * RSSI for all matchsets, rather than their own matchset for reporting
8191 	 * all APs with a strong RSSI. This is needed to be compatible with
8192 	 * older userspace that treated a matchset with only the RSSI as the
8193 	 * global RSSI for all other matchsets - if there are other matchsets.
8194 	 */
8195 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
8196 		nla_for_each_nested(attr,
8197 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
8198 				    tmp) {
8199 			struct nlattr *rssi;
8200 
8201 			err = nla_parse_nested_deprecated(tb,
8202 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
8203 							  attr,
8204 							  nl80211_match_policy,
8205 							  NULL);
8206 			if (err)
8207 				return ERR_PTR(err);
8208 
8209 			/* SSID and BSSID are mutually exclusive */
8210 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
8211 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
8212 				return ERR_PTR(-EINVAL);
8213 
8214 			/* add other standalone attributes here */
8215 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
8216 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
8217 				n_match_sets++;
8218 				continue;
8219 			}
8220 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
8221 			if (rssi)
8222 				default_match_rssi = nla_get_s32(rssi);
8223 		}
8224 	}
8225 
8226 	/* However, if there's no other matchset, add the RSSI one */
8227 	if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
8228 		n_match_sets = 1;
8229 
8230 	if (n_match_sets > max_match_sets)
8231 		return ERR_PTR(-EINVAL);
8232 
8233 	if (attrs[NL80211_ATTR_IE])
8234 		ie_len = nla_len(attrs[NL80211_ATTR_IE]);
8235 	else
8236 		ie_len = 0;
8237 
8238 	if (ie_len > wiphy->max_sched_scan_ie_len)
8239 		return ERR_PTR(-EINVAL);
8240 
8241 	if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
8242 		/*
8243 		 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
8244 		 * each scan plan already specifies its own interval
8245 		 */
8246 		if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
8247 			return ERR_PTR(-EINVAL);
8248 
8249 		nla_for_each_nested(attr,
8250 				    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
8251 			n_plans++;
8252 	} else {
8253 		/*
8254 		 * The scan interval attribute is kept for backward
8255 		 * compatibility. If no scan plans are specified and sched scan
8256 		 * interval is specified, one scan plan will be set with this
8257 		 * scan interval and infinite number of iterations.
8258 		 */
8259 		if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
8260 			return ERR_PTR(-EINVAL);
8261 
8262 		n_plans = 1;
8263 	}
8264 
8265 	if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
8266 		return ERR_PTR(-EINVAL);
8267 
8268 	if (!wiphy_ext_feature_isset(
8269 		    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
8270 	    (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
8271 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
8272 		return ERR_PTR(-EINVAL);
8273 
8274 	request = kzalloc(sizeof(*request)
8275 			+ sizeof(*request->ssids) * n_ssids
8276 			+ sizeof(*request->match_sets) * n_match_sets
8277 			+ sizeof(*request->scan_plans) * n_plans
8278 			+ sizeof(*request->channels) * n_channels
8279 			+ ie_len, GFP_KERNEL);
8280 	if (!request)
8281 		return ERR_PTR(-ENOMEM);
8282 
8283 	if (n_ssids)
8284 		request->ssids = (void *)&request->channels[n_channels];
8285 	request->n_ssids = n_ssids;
8286 	if (ie_len) {
8287 		if (n_ssids)
8288 			request->ie = (void *)(request->ssids + n_ssids);
8289 		else
8290 			request->ie = (void *)(request->channels + n_channels);
8291 	}
8292 
8293 	if (n_match_sets) {
8294 		if (request->ie)
8295 			request->match_sets = (void *)(request->ie + ie_len);
8296 		else if (n_ssids)
8297 			request->match_sets =
8298 				(void *)(request->ssids + n_ssids);
8299 		else
8300 			request->match_sets =
8301 				(void *)(request->channels + n_channels);
8302 	}
8303 	request->n_match_sets = n_match_sets;
8304 
8305 	if (n_match_sets)
8306 		request->scan_plans = (void *)(request->match_sets +
8307 					       n_match_sets);
8308 	else if (request->ie)
8309 		request->scan_plans = (void *)(request->ie + ie_len);
8310 	else if (n_ssids)
8311 		request->scan_plans = (void *)(request->ssids + n_ssids);
8312 	else
8313 		request->scan_plans = (void *)(request->channels + n_channels);
8314 
8315 	request->n_scan_plans = n_plans;
8316 
8317 	i = 0;
8318 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
8319 		/* user specified, bail out if channel not found */
8320 		nla_for_each_nested(attr,
8321 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
8322 				    tmp) {
8323 			struct ieee80211_channel *chan;
8324 
8325 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
8326 
8327 			if (!chan) {
8328 				err = -EINVAL;
8329 				goto out_free;
8330 			}
8331 
8332 			/* ignore disabled channels */
8333 			if (chan->flags & IEEE80211_CHAN_DISABLED)
8334 				continue;
8335 
8336 			request->channels[i] = chan;
8337 			i++;
8338 		}
8339 	} else {
8340 		/* all channels */
8341 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
8342 			int j;
8343 
8344 			if (!wiphy->bands[band])
8345 				continue;
8346 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
8347 				struct ieee80211_channel *chan;
8348 
8349 				chan = &wiphy->bands[band]->channels[j];
8350 
8351 				if (chan->flags & IEEE80211_CHAN_DISABLED)
8352 					continue;
8353 
8354 				request->channels[i] = chan;
8355 				i++;
8356 			}
8357 		}
8358 	}
8359 
8360 	if (!i) {
8361 		err = -EINVAL;
8362 		goto out_free;
8363 	}
8364 
8365 	request->n_channels = i;
8366 
8367 	i = 0;
8368 	if (n_ssids) {
8369 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
8370 				    tmp) {
8371 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
8372 				err = -EINVAL;
8373 				goto out_free;
8374 			}
8375 			request->ssids[i].ssid_len = nla_len(attr);
8376 			memcpy(request->ssids[i].ssid, nla_data(attr),
8377 			       nla_len(attr));
8378 			i++;
8379 		}
8380 	}
8381 
8382 	i = 0;
8383 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
8384 		nla_for_each_nested(attr,
8385 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
8386 				    tmp) {
8387 			struct nlattr *ssid, *bssid, *rssi;
8388 
8389 			err = nla_parse_nested_deprecated(tb,
8390 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
8391 							  attr,
8392 							  nl80211_match_policy,
8393 							  NULL);
8394 			if (err)
8395 				goto out_free;
8396 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
8397 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
8398 
8399 			if (!ssid && !bssid) {
8400 				i++;
8401 				continue;
8402 			}
8403 
8404 			if (WARN_ON(i >= n_match_sets)) {
8405 				/* this indicates a programming error,
8406 				 * the loop above should have verified
8407 				 * things properly
8408 				 */
8409 				err = -EINVAL;
8410 				goto out_free;
8411 			}
8412 
8413 			if (ssid) {
8414 				if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
8415 					err = -EINVAL;
8416 					goto out_free;
8417 				}
8418 				memcpy(request->match_sets[i].ssid.ssid,
8419 				       nla_data(ssid), nla_len(ssid));
8420 				request->match_sets[i].ssid.ssid_len =
8421 					nla_len(ssid);
8422 			}
8423 			if (bssid) {
8424 				if (nla_len(bssid) != ETH_ALEN) {
8425 					err = -EINVAL;
8426 					goto out_free;
8427 				}
8428 				memcpy(request->match_sets[i].bssid,
8429 				       nla_data(bssid), ETH_ALEN);
8430 			}
8431 
8432 			/* special attribute - old implementation w/a */
8433 			request->match_sets[i].rssi_thold = default_match_rssi;
8434 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
8435 			if (rssi)
8436 				request->match_sets[i].rssi_thold =
8437 					nla_get_s32(rssi);
8438 
8439 			/* Parse per band RSSI attribute */
8440 			err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
8441 				&request->match_sets[i],
8442 				tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
8443 				request->match_sets[i].rssi_thold);
8444 			if (err)
8445 				goto out_free;
8446 
8447 			i++;
8448 		}
8449 
8450 		/* there was no other matchset, so the RSSI one is alone */
8451 		if (i == 0 && n_match_sets)
8452 			request->match_sets[0].rssi_thold = default_match_rssi;
8453 
8454 		request->min_rssi_thold = INT_MAX;
8455 		for (i = 0; i < n_match_sets; i++)
8456 			request->min_rssi_thold =
8457 				min(request->match_sets[i].rssi_thold,
8458 				    request->min_rssi_thold);
8459 	} else {
8460 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
8461 	}
8462 
8463 	if (ie_len) {
8464 		request->ie_len = ie_len;
8465 		memcpy((void *)request->ie,
8466 		       nla_data(attrs[NL80211_ATTR_IE]),
8467 		       request->ie_len);
8468 	}
8469 
8470 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
8471 	if (err)
8472 		goto out_free;
8473 
8474 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
8475 		request->delay =
8476 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
8477 
8478 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
8479 		request->relative_rssi = nla_get_s8(
8480 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
8481 		request->relative_rssi_set = true;
8482 	}
8483 
8484 	if (request->relative_rssi_set &&
8485 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
8486 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
8487 
8488 		rssi_adjust = nla_data(
8489 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
8490 		request->rssi_adjust.band = rssi_adjust->band;
8491 		request->rssi_adjust.delta = rssi_adjust->delta;
8492 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
8493 			err = -EINVAL;
8494 			goto out_free;
8495 		}
8496 	}
8497 
8498 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
8499 	if (err)
8500 		goto out_free;
8501 
8502 	request->scan_start = jiffies;
8503 
8504 	return request;
8505 
8506 out_free:
8507 	kfree(request);
8508 	return ERR_PTR(err);
8509 }
8510 
8511 static int nl80211_start_sched_scan(struct sk_buff *skb,
8512 				    struct genl_info *info)
8513 {
8514 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8515 	struct net_device *dev = info->user_ptr[1];
8516 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8517 	struct cfg80211_sched_scan_request *sched_scan_req;
8518 	bool want_multi;
8519 	int err;
8520 
8521 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
8522 		return -EOPNOTSUPP;
8523 
8524 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
8525 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
8526 	if (err)
8527 		return err;
8528 
8529 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
8530 						  info->attrs,
8531 						  rdev->wiphy.max_match_sets);
8532 
8533 	err = PTR_ERR_OR_ZERO(sched_scan_req);
8534 	if (err)
8535 		goto out_err;
8536 
8537 	/* leave request id zero for legacy request
8538 	 * or if driver does not support multi-scheduled scan
8539 	 */
8540 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1)
8541 		sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
8542 
8543 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
8544 	if (err)
8545 		goto out_free;
8546 
8547 	sched_scan_req->dev = dev;
8548 	sched_scan_req->wiphy = &rdev->wiphy;
8549 
8550 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
8551 		sched_scan_req->owner_nlportid = info->snd_portid;
8552 
8553 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
8554 
8555 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
8556 	return 0;
8557 
8558 out_free:
8559 	kfree(sched_scan_req);
8560 out_err:
8561 	return err;
8562 }
8563 
8564 static int nl80211_stop_sched_scan(struct sk_buff *skb,
8565 				   struct genl_info *info)
8566 {
8567 	struct cfg80211_sched_scan_request *req;
8568 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8569 	u64 cookie;
8570 
8571 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
8572 		return -EOPNOTSUPP;
8573 
8574 	if (info->attrs[NL80211_ATTR_COOKIE]) {
8575 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
8576 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
8577 	}
8578 
8579 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
8580 				     struct cfg80211_sched_scan_request,
8581 				     list);
8582 	if (!req || req->reqid ||
8583 	    (req->owner_nlportid &&
8584 	     req->owner_nlportid != info->snd_portid))
8585 		return -ENOENT;
8586 
8587 	return cfg80211_stop_sched_scan_req(rdev, req, false);
8588 }
8589 
8590 static int nl80211_start_radar_detection(struct sk_buff *skb,
8591 					 struct genl_info *info)
8592 {
8593 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8594 	struct net_device *dev = info->user_ptr[1];
8595 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8596 	struct wiphy *wiphy = wdev->wiphy;
8597 	struct cfg80211_chan_def chandef;
8598 	enum nl80211_dfs_regions dfs_region;
8599 	unsigned int cac_time_ms;
8600 	int err;
8601 
8602 	dfs_region = reg_get_dfs_region(wiphy);
8603 	if (dfs_region == NL80211_DFS_UNSET)
8604 		return -EINVAL;
8605 
8606 	err = nl80211_parse_chandef(rdev, info, &chandef);
8607 	if (err)
8608 		return err;
8609 
8610 	if (netif_carrier_ok(dev))
8611 		return -EBUSY;
8612 
8613 	if (wdev->cac_started)
8614 		return -EBUSY;
8615 
8616 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
8617 	if (err < 0)
8618 		return err;
8619 
8620 	if (err == 0)
8621 		return -EINVAL;
8622 
8623 	if (!cfg80211_chandef_dfs_usable(wiphy, &chandef))
8624 		return -EINVAL;
8625 
8626 	/* CAC start is offloaded to HW and can't be started manually */
8627 	if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD))
8628 		return -EOPNOTSUPP;
8629 
8630 	if (!rdev->ops->start_radar_detection)
8631 		return -EOPNOTSUPP;
8632 
8633 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
8634 	if (WARN_ON(!cac_time_ms))
8635 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
8636 
8637 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
8638 	if (!err) {
8639 		wdev->chandef = chandef;
8640 		wdev->cac_started = true;
8641 		wdev->cac_start_time = jiffies;
8642 		wdev->cac_time_ms = cac_time_ms;
8643 	}
8644 	return err;
8645 }
8646 
8647 static int nl80211_notify_radar_detection(struct sk_buff *skb,
8648 					  struct genl_info *info)
8649 {
8650 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8651 	struct net_device *dev = info->user_ptr[1];
8652 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8653 	struct wiphy *wiphy = wdev->wiphy;
8654 	struct cfg80211_chan_def chandef;
8655 	enum nl80211_dfs_regions dfs_region;
8656 	int err;
8657 
8658 	dfs_region = reg_get_dfs_region(wiphy);
8659 	if (dfs_region == NL80211_DFS_UNSET) {
8660 		GENL_SET_ERR_MSG(info,
8661 				 "DFS Region is not set. Unexpected Radar indication");
8662 		return -EINVAL;
8663 	}
8664 
8665 	err = nl80211_parse_chandef(rdev, info, &chandef);
8666 	if (err) {
8667 		GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
8668 		return err;
8669 	}
8670 
8671 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
8672 	if (err < 0) {
8673 		GENL_SET_ERR_MSG(info, "chandef is invalid");
8674 		return err;
8675 	}
8676 
8677 	if (err == 0) {
8678 		GENL_SET_ERR_MSG(info,
8679 				 "Unexpected Radar indication for chandef/iftype");
8680 		return -EINVAL;
8681 	}
8682 
8683 	/* Do not process this notification if radar is already detected
8684 	 * by kernel on this channel, and return success.
8685 	 */
8686 	if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
8687 		return 0;
8688 
8689 	cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
8690 
8691 	cfg80211_sched_dfs_chan_update(rdev);
8692 
8693 	rdev->radar_chandef = chandef;
8694 
8695 	/* Propagate this notification to other radios as well */
8696 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
8697 
8698 	return 0;
8699 }
8700 
8701 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
8702 {
8703 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8704 	struct net_device *dev = info->user_ptr[1];
8705 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8706 	struct cfg80211_csa_settings params;
8707 	/* csa_attrs is defined static to avoid waste of stack size - this
8708 	 * function is called under RTNL lock, so this should not be a problem.
8709 	 */
8710 	static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
8711 	int err;
8712 	bool need_new_beacon = false;
8713 	bool need_handle_dfs_flag = true;
8714 	int len, i;
8715 	u32 cs_count;
8716 
8717 	if (!rdev->ops->channel_switch ||
8718 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
8719 		return -EOPNOTSUPP;
8720 
8721 	switch (dev->ieee80211_ptr->iftype) {
8722 	case NL80211_IFTYPE_AP:
8723 	case NL80211_IFTYPE_P2P_GO:
8724 		need_new_beacon = true;
8725 		/* For all modes except AP the handle_dfs flag needs to be
8726 		 * supplied to tell the kernel that userspace will handle radar
8727 		 * events when they happen. Otherwise a switch to a channel
8728 		 * requiring DFS will be rejected.
8729 		 */
8730 		need_handle_dfs_flag = false;
8731 
8732 		/* useless if AP is not running */
8733 		if (!wdev->beacon_interval)
8734 			return -ENOTCONN;
8735 		break;
8736 	case NL80211_IFTYPE_ADHOC:
8737 		if (!wdev->ssid_len)
8738 			return -ENOTCONN;
8739 		break;
8740 	case NL80211_IFTYPE_MESH_POINT:
8741 		if (!wdev->mesh_id_len)
8742 			return -ENOTCONN;
8743 		break;
8744 	default:
8745 		return -EOPNOTSUPP;
8746 	}
8747 
8748 	memset(&params, 0, sizeof(params));
8749 	params.beacon_csa.ftm_responder = -1;
8750 
8751 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
8752 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
8753 		return -EINVAL;
8754 
8755 	/* only important for AP, IBSS and mesh create IEs internally */
8756 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
8757 		return -EINVAL;
8758 
8759 	/* Even though the attribute is u32, the specification says
8760 	 * u8, so let's make sure we don't overflow.
8761 	 */
8762 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
8763 	if (cs_count > 255)
8764 		return -EINVAL;
8765 
8766 	params.count = cs_count;
8767 
8768 	if (!need_new_beacon)
8769 		goto skip_beacons;
8770 
8771 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after);
8772 	if (err)
8773 		return err;
8774 
8775 	err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
8776 					  info->attrs[NL80211_ATTR_CSA_IES],
8777 					  nl80211_policy, info->extack);
8778 	if (err)
8779 		return err;
8780 
8781 	err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa);
8782 	if (err)
8783 		return err;
8784 
8785 	if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
8786 		return -EINVAL;
8787 
8788 	len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
8789 	if (!len || (len % sizeof(u16)))
8790 		return -EINVAL;
8791 
8792 	params.n_counter_offsets_beacon = len / sizeof(u16);
8793 	if (rdev->wiphy.max_num_csa_counters &&
8794 	    (params.n_counter_offsets_beacon >
8795 	     rdev->wiphy.max_num_csa_counters))
8796 		return -EINVAL;
8797 
8798 	params.counter_offsets_beacon =
8799 		nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
8800 
8801 	/* sanity checks - counters should fit and be the same */
8802 	for (i = 0; i < params.n_counter_offsets_beacon; i++) {
8803 		u16 offset = params.counter_offsets_beacon[i];
8804 
8805 		if (offset >= params.beacon_csa.tail_len)
8806 			return -EINVAL;
8807 
8808 		if (params.beacon_csa.tail[offset] != params.count)
8809 			return -EINVAL;
8810 	}
8811 
8812 	if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
8813 		len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
8814 		if (!len || (len % sizeof(u16)))
8815 			return -EINVAL;
8816 
8817 		params.n_counter_offsets_presp = len / sizeof(u16);
8818 		if (rdev->wiphy.max_num_csa_counters &&
8819 		    (params.n_counter_offsets_presp >
8820 		     rdev->wiphy.max_num_csa_counters))
8821 			return -EINVAL;
8822 
8823 		params.counter_offsets_presp =
8824 			nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
8825 
8826 		/* sanity checks - counters should fit and be the same */
8827 		for (i = 0; i < params.n_counter_offsets_presp; i++) {
8828 			u16 offset = params.counter_offsets_presp[i];
8829 
8830 			if (offset >= params.beacon_csa.probe_resp_len)
8831 				return -EINVAL;
8832 
8833 			if (params.beacon_csa.probe_resp[offset] !=
8834 			    params.count)
8835 				return -EINVAL;
8836 		}
8837 	}
8838 
8839 skip_beacons:
8840 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
8841 	if (err)
8842 		return err;
8843 
8844 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
8845 					   wdev->iftype))
8846 		return -EINVAL;
8847 
8848 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
8849 					    &params.chandef,
8850 					    wdev->iftype);
8851 	if (err < 0)
8852 		return err;
8853 
8854 	if (err > 0) {
8855 		params.radar_required = true;
8856 		if (need_handle_dfs_flag &&
8857 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
8858 			return -EINVAL;
8859 		}
8860 	}
8861 
8862 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
8863 		params.block_tx = true;
8864 
8865 	wdev_lock(wdev);
8866 	err = rdev_channel_switch(rdev, dev, &params);
8867 	wdev_unlock(wdev);
8868 
8869 	return err;
8870 }
8871 
8872 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
8873 			    u32 seq, int flags,
8874 			    struct cfg80211_registered_device *rdev,
8875 			    struct wireless_dev *wdev,
8876 			    struct cfg80211_internal_bss *intbss)
8877 {
8878 	struct cfg80211_bss *res = &intbss->pub;
8879 	const struct cfg80211_bss_ies *ies;
8880 	void *hdr;
8881 	struct nlattr *bss;
8882 
8883 	ASSERT_WDEV_LOCK(wdev);
8884 
8885 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
8886 			     NL80211_CMD_NEW_SCAN_RESULTS);
8887 	if (!hdr)
8888 		return -1;
8889 
8890 	genl_dump_check_consistent(cb, hdr);
8891 
8892 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
8893 		goto nla_put_failure;
8894 	if (wdev->netdev &&
8895 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
8896 		goto nla_put_failure;
8897 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
8898 			      NL80211_ATTR_PAD))
8899 		goto nla_put_failure;
8900 
8901 	bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
8902 	if (!bss)
8903 		goto nla_put_failure;
8904 	if ((!is_zero_ether_addr(res->bssid) &&
8905 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
8906 		goto nla_put_failure;
8907 
8908 	rcu_read_lock();
8909 	/* indicate whether we have probe response data or not */
8910 	if (rcu_access_pointer(res->proberesp_ies) &&
8911 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
8912 		goto fail_unlock_rcu;
8913 
8914 	/* this pointer prefers to be pointed to probe response data
8915 	 * but is always valid
8916 	 */
8917 	ies = rcu_dereference(res->ies);
8918 	if (ies) {
8919 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
8920 				      NL80211_BSS_PAD))
8921 			goto fail_unlock_rcu;
8922 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
8923 					ies->len, ies->data))
8924 			goto fail_unlock_rcu;
8925 	}
8926 
8927 	/* and this pointer is always (unless driver didn't know) beacon data */
8928 	ies = rcu_dereference(res->beacon_ies);
8929 	if (ies && ies->from_beacon) {
8930 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
8931 				      NL80211_BSS_PAD))
8932 			goto fail_unlock_rcu;
8933 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
8934 					ies->len, ies->data))
8935 			goto fail_unlock_rcu;
8936 	}
8937 	rcu_read_unlock();
8938 
8939 	if (res->beacon_interval &&
8940 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
8941 		goto nla_put_failure;
8942 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
8943 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
8944 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY_OFFSET,
8945 			res->channel->freq_offset) ||
8946 	    nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
8947 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
8948 			jiffies_to_msecs(jiffies - intbss->ts)))
8949 		goto nla_put_failure;
8950 
8951 	if (intbss->parent_tsf &&
8952 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
8953 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
8954 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
8955 		     intbss->parent_bssid)))
8956 		goto nla_put_failure;
8957 
8958 	if (intbss->ts_boottime &&
8959 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
8960 			      intbss->ts_boottime, NL80211_BSS_PAD))
8961 		goto nla_put_failure;
8962 
8963 	if (!nl80211_put_signal(msg, intbss->pub.chains,
8964 				intbss->pub.chain_signal,
8965 				NL80211_BSS_CHAIN_SIGNAL))
8966 		goto nla_put_failure;
8967 
8968 	switch (rdev->wiphy.signal_type) {
8969 	case CFG80211_SIGNAL_TYPE_MBM:
8970 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
8971 			goto nla_put_failure;
8972 		break;
8973 	case CFG80211_SIGNAL_TYPE_UNSPEC:
8974 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
8975 			goto nla_put_failure;
8976 		break;
8977 	default:
8978 		break;
8979 	}
8980 
8981 	switch (wdev->iftype) {
8982 	case NL80211_IFTYPE_P2P_CLIENT:
8983 	case NL80211_IFTYPE_STATION:
8984 		if (intbss == wdev->current_bss &&
8985 		    nla_put_u32(msg, NL80211_BSS_STATUS,
8986 				NL80211_BSS_STATUS_ASSOCIATED))
8987 			goto nla_put_failure;
8988 		break;
8989 	case NL80211_IFTYPE_ADHOC:
8990 		if (intbss == wdev->current_bss &&
8991 		    nla_put_u32(msg, NL80211_BSS_STATUS,
8992 				NL80211_BSS_STATUS_IBSS_JOINED))
8993 			goto nla_put_failure;
8994 		break;
8995 	default:
8996 		break;
8997 	}
8998 
8999 	nla_nest_end(msg, bss);
9000 
9001 	genlmsg_end(msg, hdr);
9002 	return 0;
9003 
9004  fail_unlock_rcu:
9005 	rcu_read_unlock();
9006  nla_put_failure:
9007 	genlmsg_cancel(msg, hdr);
9008 	return -EMSGSIZE;
9009 }
9010 
9011 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
9012 {
9013 	struct cfg80211_registered_device *rdev;
9014 	struct cfg80211_internal_bss *scan;
9015 	struct wireless_dev *wdev;
9016 	int start = cb->args[2], idx = 0;
9017 	int err;
9018 
9019 	rtnl_lock();
9020 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
9021 	if (err) {
9022 		rtnl_unlock();
9023 		return err;
9024 	}
9025 
9026 	wdev_lock(wdev);
9027 	spin_lock_bh(&rdev->bss_lock);
9028 
9029 	/*
9030 	 * dump_scan will be called multiple times to break up the scan results
9031 	 * into multiple messages.  It is unlikely that any more bss-es will be
9032 	 * expired after the first call, so only call only call this on the
9033 	 * first dump_scan invocation.
9034 	 */
9035 	if (start == 0)
9036 		cfg80211_bss_expire(rdev);
9037 
9038 	cb->seq = rdev->bss_generation;
9039 
9040 	list_for_each_entry(scan, &rdev->bss_list, list) {
9041 		if (++idx <= start)
9042 			continue;
9043 		if (nl80211_send_bss(skb, cb,
9044 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
9045 				rdev, wdev, scan) < 0) {
9046 			idx--;
9047 			break;
9048 		}
9049 	}
9050 
9051 	spin_unlock_bh(&rdev->bss_lock);
9052 	wdev_unlock(wdev);
9053 
9054 	cb->args[2] = idx;
9055 	rtnl_unlock();
9056 
9057 	return skb->len;
9058 }
9059 
9060 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
9061 			       int flags, struct net_device *dev,
9062 			       bool allow_radio_stats,
9063 			       struct survey_info *survey)
9064 {
9065 	void *hdr;
9066 	struct nlattr *infoattr;
9067 
9068 	/* skip radio stats if userspace didn't request them */
9069 	if (!survey->channel && !allow_radio_stats)
9070 		return 0;
9071 
9072 	hdr = nl80211hdr_put(msg, portid, seq, flags,
9073 			     NL80211_CMD_NEW_SURVEY_RESULTS);
9074 	if (!hdr)
9075 		return -ENOMEM;
9076 
9077 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
9078 		goto nla_put_failure;
9079 
9080 	infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
9081 	if (!infoattr)
9082 		goto nla_put_failure;
9083 
9084 	if (survey->channel &&
9085 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
9086 			survey->channel->center_freq))
9087 		goto nla_put_failure;
9088 
9089 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
9090 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
9091 		goto nla_put_failure;
9092 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
9093 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
9094 		goto nla_put_failure;
9095 	if ((survey->filled & SURVEY_INFO_TIME) &&
9096 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
9097 			survey->time, NL80211_SURVEY_INFO_PAD))
9098 		goto nla_put_failure;
9099 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
9100 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
9101 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
9102 		goto nla_put_failure;
9103 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
9104 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
9105 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
9106 		goto nla_put_failure;
9107 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
9108 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
9109 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
9110 		goto nla_put_failure;
9111 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
9112 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
9113 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
9114 		goto nla_put_failure;
9115 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
9116 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
9117 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
9118 		goto nla_put_failure;
9119 	if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
9120 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
9121 			      survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
9122 		goto nla_put_failure;
9123 
9124 	nla_nest_end(msg, infoattr);
9125 
9126 	genlmsg_end(msg, hdr);
9127 	return 0;
9128 
9129  nla_put_failure:
9130 	genlmsg_cancel(msg, hdr);
9131 	return -EMSGSIZE;
9132 }
9133 
9134 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
9135 {
9136 	struct nlattr **attrbuf;
9137 	struct survey_info survey;
9138 	struct cfg80211_registered_device *rdev;
9139 	struct wireless_dev *wdev;
9140 	int survey_idx = cb->args[2];
9141 	int res;
9142 	bool radio_stats;
9143 
9144 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
9145 	if (!attrbuf)
9146 		return -ENOMEM;
9147 
9148 	rtnl_lock();
9149 	res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
9150 	if (res)
9151 		goto out_err;
9152 
9153 	/* prepare_wdev_dump parsed the attributes */
9154 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
9155 
9156 	if (!wdev->netdev) {
9157 		res = -EINVAL;
9158 		goto out_err;
9159 	}
9160 
9161 	if (!rdev->ops->dump_survey) {
9162 		res = -EOPNOTSUPP;
9163 		goto out_err;
9164 	}
9165 
9166 	while (1) {
9167 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
9168 		if (res == -ENOENT)
9169 			break;
9170 		if (res)
9171 			goto out_err;
9172 
9173 		/* don't send disabled channels, but do send non-channel data */
9174 		if (survey.channel &&
9175 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
9176 			survey_idx++;
9177 			continue;
9178 		}
9179 
9180 		if (nl80211_send_survey(skb,
9181 				NETLINK_CB(cb->skb).portid,
9182 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
9183 				wdev->netdev, radio_stats, &survey) < 0)
9184 			goto out;
9185 		survey_idx++;
9186 	}
9187 
9188  out:
9189 	cb->args[2] = survey_idx;
9190 	res = skb->len;
9191  out_err:
9192 	kfree(attrbuf);
9193 	rtnl_unlock();
9194 	return res;
9195 }
9196 
9197 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
9198 {
9199 	return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
9200 				  NL80211_WPA_VERSION_2 |
9201 				  NL80211_WPA_VERSION_3));
9202 }
9203 
9204 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
9205 {
9206 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9207 	struct net_device *dev = info->user_ptr[1];
9208 	struct ieee80211_channel *chan;
9209 	const u8 *bssid, *ssid, *ie = NULL, *auth_data = NULL;
9210 	int err, ssid_len, ie_len = 0, auth_data_len = 0;
9211 	enum nl80211_auth_type auth_type;
9212 	struct key_parse key;
9213 	bool local_state_change;
9214 	u32 freq;
9215 
9216 	if (!info->attrs[NL80211_ATTR_MAC])
9217 		return -EINVAL;
9218 
9219 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
9220 		return -EINVAL;
9221 
9222 	if (!info->attrs[NL80211_ATTR_SSID])
9223 		return -EINVAL;
9224 
9225 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
9226 		return -EINVAL;
9227 
9228 	err = nl80211_parse_key(info, &key);
9229 	if (err)
9230 		return err;
9231 
9232 	if (key.idx >= 0) {
9233 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
9234 			return -EINVAL;
9235 		if (!key.p.key || !key.p.key_len)
9236 			return -EINVAL;
9237 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
9238 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
9239 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
9240 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
9241 			return -EINVAL;
9242 		if (key.idx > 3)
9243 			return -EINVAL;
9244 	} else {
9245 		key.p.key_len = 0;
9246 		key.p.key = NULL;
9247 	}
9248 
9249 	if (key.idx >= 0) {
9250 		int i;
9251 		bool ok = false;
9252 
9253 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
9254 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
9255 				ok = true;
9256 				break;
9257 			}
9258 		}
9259 		if (!ok)
9260 			return -EINVAL;
9261 	}
9262 
9263 	if (!rdev->ops->auth)
9264 		return -EOPNOTSUPP;
9265 
9266 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9267 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9268 		return -EOPNOTSUPP;
9269 
9270 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9271 	freq = MHZ_TO_KHZ(nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
9272 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
9273 		freq +=
9274 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
9275 
9276 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
9277 	if (!chan)
9278 		return -EINVAL;
9279 
9280 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9281 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9282 
9283 	if (info->attrs[NL80211_ATTR_IE]) {
9284 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9285 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9286 	}
9287 
9288 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
9289 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
9290 		return -EINVAL;
9291 
9292 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
9293 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
9294 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
9295 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
9296 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
9297 		return -EINVAL;
9298 
9299 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
9300 		if (auth_type != NL80211_AUTHTYPE_SAE &&
9301 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
9302 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
9303 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
9304 			return -EINVAL;
9305 		auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
9306 		auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
9307 		/* need to include at least Auth Transaction and Status Code */
9308 		if (auth_data_len < 4)
9309 			return -EINVAL;
9310 	}
9311 
9312 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9313 
9314 	/*
9315 	 * Since we no longer track auth state, ignore
9316 	 * requests to only change local state.
9317 	 */
9318 	if (local_state_change)
9319 		return 0;
9320 
9321 	wdev_lock(dev->ieee80211_ptr);
9322 	err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
9323 				 ssid, ssid_len, ie, ie_len,
9324 				 key.p.key, key.p.key_len, key.idx,
9325 				 auth_data, auth_data_len);
9326 	wdev_unlock(dev->ieee80211_ptr);
9327 	return err;
9328 }
9329 
9330 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
9331 				     struct genl_info *info)
9332 {
9333 	if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9334 		GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
9335 		return -EINVAL;
9336 	}
9337 
9338 	if (!rdev->ops->tx_control_port ||
9339 	    !wiphy_ext_feature_isset(&rdev->wiphy,
9340 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
9341 		return -EOPNOTSUPP;
9342 
9343 	return 0;
9344 }
9345 
9346 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
9347 				   struct genl_info *info,
9348 				   struct cfg80211_crypto_settings *settings,
9349 				   int cipher_limit)
9350 {
9351 	memset(settings, 0, sizeof(*settings));
9352 
9353 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
9354 
9355 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
9356 		u16 proto;
9357 
9358 		proto = nla_get_u16(
9359 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
9360 		settings->control_port_ethertype = cpu_to_be16(proto);
9361 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
9362 		    proto != ETH_P_PAE)
9363 			return -EINVAL;
9364 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
9365 			settings->control_port_no_encrypt = true;
9366 	} else
9367 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
9368 
9369 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
9370 		int r = validate_pae_over_nl80211(rdev, info);
9371 
9372 		if (r < 0)
9373 			return r;
9374 
9375 		settings->control_port_over_nl80211 = true;
9376 
9377 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH])
9378 			settings->control_port_no_preauth = true;
9379 	}
9380 
9381 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
9382 		void *data;
9383 		int len, i;
9384 
9385 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9386 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9387 		settings->n_ciphers_pairwise = len / sizeof(u32);
9388 
9389 		if (len % sizeof(u32))
9390 			return -EINVAL;
9391 
9392 		if (settings->n_ciphers_pairwise > cipher_limit)
9393 			return -EINVAL;
9394 
9395 		memcpy(settings->ciphers_pairwise, data, len);
9396 
9397 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
9398 			if (!cfg80211_supported_cipher_suite(
9399 					&rdev->wiphy,
9400 					settings->ciphers_pairwise[i]))
9401 				return -EINVAL;
9402 	}
9403 
9404 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
9405 		settings->cipher_group =
9406 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
9407 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
9408 						     settings->cipher_group))
9409 			return -EINVAL;
9410 	}
9411 
9412 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
9413 		settings->wpa_versions =
9414 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
9415 		if (!nl80211_valid_wpa_versions(settings->wpa_versions))
9416 			return -EINVAL;
9417 	}
9418 
9419 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
9420 		void *data;
9421 		int len;
9422 
9423 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
9424 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
9425 		settings->n_akm_suites = len / sizeof(u32);
9426 
9427 		if (len % sizeof(u32))
9428 			return -EINVAL;
9429 
9430 		if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
9431 			return -EINVAL;
9432 
9433 		memcpy(settings->akm_suites, data, len);
9434 	}
9435 
9436 	if (info->attrs[NL80211_ATTR_PMK]) {
9437 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
9438 			return -EINVAL;
9439 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9440 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK))
9441 			return -EINVAL;
9442 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
9443 	}
9444 
9445 	if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
9446 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9447 					     NL80211_EXT_FEATURE_SAE_OFFLOAD))
9448 			return -EINVAL;
9449 		settings->sae_pwd =
9450 			nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9451 		settings->sae_pwd_len =
9452 			nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9453 	}
9454 
9455 	return 0;
9456 }
9457 
9458 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
9459 {
9460 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9461 	struct net_device *dev = info->user_ptr[1];
9462 	struct ieee80211_channel *chan;
9463 	struct cfg80211_assoc_request req = {};
9464 	const u8 *bssid, *ssid;
9465 	int err, ssid_len = 0;
9466 	u32 freq;
9467 
9468 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9469 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9470 		return -EPERM;
9471 
9472 	if (!info->attrs[NL80211_ATTR_MAC] ||
9473 	    !info->attrs[NL80211_ATTR_SSID] ||
9474 	    !info->attrs[NL80211_ATTR_WIPHY_FREQ])
9475 		return -EINVAL;
9476 
9477 	if (!rdev->ops->assoc)
9478 		return -EOPNOTSUPP;
9479 
9480 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9481 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9482 		return -EOPNOTSUPP;
9483 
9484 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9485 
9486 	freq = MHZ_TO_KHZ(nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
9487 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
9488 		freq +=
9489 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
9490 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
9491 	if (!chan)
9492 		return -EINVAL;
9493 
9494 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9495 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9496 
9497 	if (info->attrs[NL80211_ATTR_IE]) {
9498 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9499 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9500 	}
9501 
9502 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
9503 		enum nl80211_mfp mfp =
9504 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
9505 		if (mfp == NL80211_MFP_REQUIRED)
9506 			req.use_mfp = true;
9507 		else if (mfp != NL80211_MFP_NO)
9508 			return -EINVAL;
9509 	}
9510 
9511 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
9512 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
9513 
9514 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9515 		req.flags |= ASSOC_REQ_DISABLE_HT;
9516 
9517 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9518 		memcpy(&req.ht_capa_mask,
9519 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9520 		       sizeof(req.ht_capa_mask));
9521 
9522 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9523 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9524 			return -EINVAL;
9525 		memcpy(&req.ht_capa,
9526 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9527 		       sizeof(req.ht_capa));
9528 	}
9529 
9530 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
9531 		req.flags |= ASSOC_REQ_DISABLE_VHT;
9532 
9533 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9534 		memcpy(&req.vht_capa_mask,
9535 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
9536 		       sizeof(req.vht_capa_mask));
9537 
9538 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
9539 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9540 			return -EINVAL;
9541 		memcpy(&req.vht_capa,
9542 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
9543 		       sizeof(req.vht_capa));
9544 	}
9545 
9546 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
9547 		if (!((rdev->wiphy.features &
9548 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
9549 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
9550 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9551 					     NL80211_EXT_FEATURE_RRM))
9552 			return -EINVAL;
9553 		req.flags |= ASSOC_REQ_USE_RRM;
9554 	}
9555 
9556 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
9557 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
9558 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
9559 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
9560 			return -EINVAL;
9561 		req.fils_nonces =
9562 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
9563 	}
9564 
9565 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
9566 	if (!err) {
9567 		wdev_lock(dev->ieee80211_ptr);
9568 
9569 		err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
9570 					  ssid, ssid_len, &req);
9571 
9572 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9573 			dev->ieee80211_ptr->conn_owner_nlportid =
9574 				info->snd_portid;
9575 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
9576 			       bssid, ETH_ALEN);
9577 		}
9578 
9579 		wdev_unlock(dev->ieee80211_ptr);
9580 	}
9581 
9582 	return err;
9583 }
9584 
9585 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
9586 {
9587 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9588 	struct net_device *dev = info->user_ptr[1];
9589 	const u8 *ie = NULL, *bssid;
9590 	int ie_len = 0, err;
9591 	u16 reason_code;
9592 	bool local_state_change;
9593 
9594 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9595 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9596 		return -EPERM;
9597 
9598 	if (!info->attrs[NL80211_ATTR_MAC])
9599 		return -EINVAL;
9600 
9601 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
9602 		return -EINVAL;
9603 
9604 	if (!rdev->ops->deauth)
9605 		return -EOPNOTSUPP;
9606 
9607 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9608 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9609 		return -EOPNOTSUPP;
9610 
9611 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9612 
9613 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9614 	if (reason_code == 0) {
9615 		/* Reason Code 0 is reserved */
9616 		return -EINVAL;
9617 	}
9618 
9619 	if (info->attrs[NL80211_ATTR_IE]) {
9620 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9621 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9622 	}
9623 
9624 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9625 
9626 	wdev_lock(dev->ieee80211_ptr);
9627 	err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
9628 				   local_state_change);
9629 	wdev_unlock(dev->ieee80211_ptr);
9630 	return err;
9631 }
9632 
9633 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
9634 {
9635 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9636 	struct net_device *dev = info->user_ptr[1];
9637 	const u8 *ie = NULL, *bssid;
9638 	int ie_len = 0, err;
9639 	u16 reason_code;
9640 	bool local_state_change;
9641 
9642 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9643 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9644 		return -EPERM;
9645 
9646 	if (!info->attrs[NL80211_ATTR_MAC])
9647 		return -EINVAL;
9648 
9649 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
9650 		return -EINVAL;
9651 
9652 	if (!rdev->ops->disassoc)
9653 		return -EOPNOTSUPP;
9654 
9655 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9656 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9657 		return -EOPNOTSUPP;
9658 
9659 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9660 
9661 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9662 	if (reason_code == 0) {
9663 		/* Reason Code 0 is reserved */
9664 		return -EINVAL;
9665 	}
9666 
9667 	if (info->attrs[NL80211_ATTR_IE]) {
9668 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9669 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9670 	}
9671 
9672 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9673 
9674 	wdev_lock(dev->ieee80211_ptr);
9675 	err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
9676 				     local_state_change);
9677 	wdev_unlock(dev->ieee80211_ptr);
9678 	return err;
9679 }
9680 
9681 static bool
9682 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
9683 			 int mcast_rate[NUM_NL80211_BANDS],
9684 			 int rateval)
9685 {
9686 	struct wiphy *wiphy = &rdev->wiphy;
9687 	bool found = false;
9688 	int band, i;
9689 
9690 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
9691 		struct ieee80211_supported_band *sband;
9692 
9693 		sband = wiphy->bands[band];
9694 		if (!sband)
9695 			continue;
9696 
9697 		for (i = 0; i < sband->n_bitrates; i++) {
9698 			if (sband->bitrates[i].bitrate == rateval) {
9699 				mcast_rate[band] = i + 1;
9700 				found = true;
9701 				break;
9702 			}
9703 		}
9704 	}
9705 
9706 	return found;
9707 }
9708 
9709 static int nl80211_join_ibss(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 cfg80211_ibss_params ibss;
9714 	struct wiphy *wiphy;
9715 	struct cfg80211_cached_keys *connkeys = NULL;
9716 	int err;
9717 
9718 	memset(&ibss, 0, sizeof(ibss));
9719 
9720 	if (!info->attrs[NL80211_ATTR_SSID] ||
9721 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
9722 		return -EINVAL;
9723 
9724 	ibss.beacon_interval = 100;
9725 
9726 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
9727 		ibss.beacon_interval =
9728 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
9729 
9730 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
9731 					   ibss.beacon_interval);
9732 	if (err)
9733 		return err;
9734 
9735 	if (!rdev->ops->join_ibss)
9736 		return -EOPNOTSUPP;
9737 
9738 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
9739 		return -EOPNOTSUPP;
9740 
9741 	wiphy = &rdev->wiphy;
9742 
9743 	if (info->attrs[NL80211_ATTR_MAC]) {
9744 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9745 
9746 		if (!is_valid_ether_addr(ibss.bssid))
9747 			return -EINVAL;
9748 	}
9749 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9750 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9751 
9752 	if (info->attrs[NL80211_ATTR_IE]) {
9753 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9754 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9755 	}
9756 
9757 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
9758 	if (err)
9759 		return err;
9760 
9761 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
9762 				     NL80211_IFTYPE_ADHOC))
9763 		return -EINVAL;
9764 
9765 	switch (ibss.chandef.width) {
9766 	case NL80211_CHAN_WIDTH_5:
9767 	case NL80211_CHAN_WIDTH_10:
9768 	case NL80211_CHAN_WIDTH_20_NOHT:
9769 		break;
9770 	case NL80211_CHAN_WIDTH_20:
9771 	case NL80211_CHAN_WIDTH_40:
9772 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
9773 			return -EINVAL;
9774 		break;
9775 	case NL80211_CHAN_WIDTH_80:
9776 	case NL80211_CHAN_WIDTH_80P80:
9777 	case NL80211_CHAN_WIDTH_160:
9778 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
9779 			return -EINVAL;
9780 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9781 					     NL80211_EXT_FEATURE_VHT_IBSS))
9782 			return -EINVAL;
9783 		break;
9784 	default:
9785 		return -EINVAL;
9786 	}
9787 
9788 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
9789 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
9790 
9791 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
9792 		u8 *rates =
9793 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9794 		int n_rates =
9795 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9796 		struct ieee80211_supported_band *sband =
9797 			wiphy->bands[ibss.chandef.chan->band];
9798 
9799 		err = ieee80211_get_ratemask(sband, rates, n_rates,
9800 					     &ibss.basic_rates);
9801 		if (err)
9802 			return err;
9803 	}
9804 
9805 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9806 		memcpy(&ibss.ht_capa_mask,
9807 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9808 		       sizeof(ibss.ht_capa_mask));
9809 
9810 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9811 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9812 			return -EINVAL;
9813 		memcpy(&ibss.ht_capa,
9814 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9815 		       sizeof(ibss.ht_capa));
9816 	}
9817 
9818 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
9819 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
9820 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
9821 		return -EINVAL;
9822 
9823 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9824 		bool no_ht = false;
9825 
9826 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
9827 		if (IS_ERR(connkeys))
9828 			return PTR_ERR(connkeys);
9829 
9830 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
9831 		    no_ht) {
9832 			kzfree(connkeys);
9833 			return -EINVAL;
9834 		}
9835 	}
9836 
9837 	ibss.control_port =
9838 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
9839 
9840 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
9841 		int r = validate_pae_over_nl80211(rdev, info);
9842 
9843 		if (r < 0) {
9844 			kzfree(connkeys);
9845 			return r;
9846 		}
9847 
9848 		ibss.control_port_over_nl80211 = true;
9849 	}
9850 
9851 	ibss.userspace_handles_dfs =
9852 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
9853 
9854 	wdev_lock(dev->ieee80211_ptr);
9855 	err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
9856 	if (err)
9857 		kzfree(connkeys);
9858 	else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9859 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
9860 	wdev_unlock(dev->ieee80211_ptr);
9861 
9862 	return err;
9863 }
9864 
9865 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
9866 {
9867 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9868 	struct net_device *dev = info->user_ptr[1];
9869 
9870 	if (!rdev->ops->leave_ibss)
9871 		return -EOPNOTSUPP;
9872 
9873 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
9874 		return -EOPNOTSUPP;
9875 
9876 	return cfg80211_leave_ibss(rdev, dev, false);
9877 }
9878 
9879 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
9880 {
9881 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9882 	struct net_device *dev = info->user_ptr[1];
9883 	int mcast_rate[NUM_NL80211_BANDS];
9884 	u32 nla_rate;
9885 	int err;
9886 
9887 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
9888 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
9889 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
9890 		return -EOPNOTSUPP;
9891 
9892 	if (!rdev->ops->set_mcast_rate)
9893 		return -EOPNOTSUPP;
9894 
9895 	memset(mcast_rate, 0, sizeof(mcast_rate));
9896 
9897 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
9898 		return -EINVAL;
9899 
9900 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
9901 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
9902 		return -EINVAL;
9903 
9904 	err = rdev_set_mcast_rate(rdev, dev, mcast_rate);
9905 
9906 	return err;
9907 }
9908 
9909 static struct sk_buff *
9910 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
9911 			    struct wireless_dev *wdev, int approxlen,
9912 			    u32 portid, u32 seq, enum nl80211_commands cmd,
9913 			    enum nl80211_attrs attr,
9914 			    const struct nl80211_vendor_cmd_info *info,
9915 			    gfp_t gfp)
9916 {
9917 	struct sk_buff *skb;
9918 	void *hdr;
9919 	struct nlattr *data;
9920 
9921 	skb = nlmsg_new(approxlen + 100, gfp);
9922 	if (!skb)
9923 		return NULL;
9924 
9925 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
9926 	if (!hdr) {
9927 		kfree_skb(skb);
9928 		return NULL;
9929 	}
9930 
9931 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9932 		goto nla_put_failure;
9933 
9934 	if (info) {
9935 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
9936 				info->vendor_id))
9937 			goto nla_put_failure;
9938 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
9939 				info->subcmd))
9940 			goto nla_put_failure;
9941 	}
9942 
9943 	if (wdev) {
9944 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
9945 				      wdev_id(wdev), NL80211_ATTR_PAD))
9946 			goto nla_put_failure;
9947 		if (wdev->netdev &&
9948 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
9949 				wdev->netdev->ifindex))
9950 			goto nla_put_failure;
9951 	}
9952 
9953 	data = nla_nest_start_noflag(skb, attr);
9954 	if (!data)
9955 		goto nla_put_failure;
9956 
9957 	((void **)skb->cb)[0] = rdev;
9958 	((void **)skb->cb)[1] = hdr;
9959 	((void **)skb->cb)[2] = data;
9960 
9961 	return skb;
9962 
9963  nla_put_failure:
9964 	kfree_skb(skb);
9965 	return NULL;
9966 }
9967 
9968 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
9969 					   struct wireless_dev *wdev,
9970 					   enum nl80211_commands cmd,
9971 					   enum nl80211_attrs attr,
9972 					   unsigned int portid,
9973 					   int vendor_event_idx,
9974 					   int approxlen, gfp_t gfp)
9975 {
9976 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
9977 	const struct nl80211_vendor_cmd_info *info;
9978 
9979 	switch (cmd) {
9980 	case NL80211_CMD_TESTMODE:
9981 		if (WARN_ON(vendor_event_idx != -1))
9982 			return NULL;
9983 		info = NULL;
9984 		break;
9985 	case NL80211_CMD_VENDOR:
9986 		if (WARN_ON(vendor_event_idx < 0 ||
9987 			    vendor_event_idx >= wiphy->n_vendor_events))
9988 			return NULL;
9989 		info = &wiphy->vendor_events[vendor_event_idx];
9990 		break;
9991 	default:
9992 		WARN_ON(1);
9993 		return NULL;
9994 	}
9995 
9996 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
9997 					   cmd, attr, info, gfp);
9998 }
9999 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
10000 
10001 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
10002 {
10003 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
10004 	void *hdr = ((void **)skb->cb)[1];
10005 	struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
10006 	struct nlattr *data = ((void **)skb->cb)[2];
10007 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
10008 
10009 	/* clear CB data for netlink core to own from now on */
10010 	memset(skb->cb, 0, sizeof(skb->cb));
10011 
10012 	nla_nest_end(skb, data);
10013 	genlmsg_end(skb, hdr);
10014 
10015 	if (nlhdr->nlmsg_pid) {
10016 		genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
10017 				nlhdr->nlmsg_pid);
10018 	} else {
10019 		if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
10020 			mcgrp = NL80211_MCGRP_VENDOR;
10021 
10022 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
10023 					skb, 0, mcgrp, gfp);
10024 	}
10025 }
10026 EXPORT_SYMBOL(__cfg80211_send_event_skb);
10027 
10028 #ifdef CONFIG_NL80211_TESTMODE
10029 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
10030 {
10031 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10032 	struct wireless_dev *wdev =
10033 		__cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
10034 	int err;
10035 
10036 	if (!rdev->ops->testmode_cmd)
10037 		return -EOPNOTSUPP;
10038 
10039 	if (IS_ERR(wdev)) {
10040 		err = PTR_ERR(wdev);
10041 		if (err != -EINVAL)
10042 			return err;
10043 		wdev = NULL;
10044 	} else if (wdev->wiphy != &rdev->wiphy) {
10045 		return -EINVAL;
10046 	}
10047 
10048 	if (!info->attrs[NL80211_ATTR_TESTDATA])
10049 		return -EINVAL;
10050 
10051 	rdev->cur_cmd_info = info;
10052 	err = rdev_testmode_cmd(rdev, wdev,
10053 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
10054 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
10055 	rdev->cur_cmd_info = NULL;
10056 
10057 	return err;
10058 }
10059 
10060 static int nl80211_testmode_dump(struct sk_buff *skb,
10061 				 struct netlink_callback *cb)
10062 {
10063 	struct cfg80211_registered_device *rdev;
10064 	struct nlattr **attrbuf = NULL;
10065 	int err;
10066 	long phy_idx;
10067 	void *data = NULL;
10068 	int data_len = 0;
10069 
10070 	rtnl_lock();
10071 
10072 	if (cb->args[0]) {
10073 		/*
10074 		 * 0 is a valid index, but not valid for args[0],
10075 		 * so we need to offset by 1.
10076 		 */
10077 		phy_idx = cb->args[0] - 1;
10078 
10079 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
10080 		if (!rdev) {
10081 			err = -ENOENT;
10082 			goto out_err;
10083 		}
10084 	} else {
10085 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
10086 				  GFP_KERNEL);
10087 		if (!attrbuf) {
10088 			err = -ENOMEM;
10089 			goto out_err;
10090 		}
10091 
10092 		err = nlmsg_parse_deprecated(cb->nlh,
10093 					     GENL_HDRLEN + nl80211_fam.hdrsize,
10094 					     attrbuf, nl80211_fam.maxattr,
10095 					     nl80211_policy, NULL);
10096 		if (err)
10097 			goto out_err;
10098 
10099 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
10100 		if (IS_ERR(rdev)) {
10101 			err = PTR_ERR(rdev);
10102 			goto out_err;
10103 		}
10104 		phy_idx = rdev->wiphy_idx;
10105 
10106 		if (attrbuf[NL80211_ATTR_TESTDATA])
10107 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
10108 	}
10109 
10110 	if (cb->args[1]) {
10111 		data = nla_data((void *)cb->args[1]);
10112 		data_len = nla_len((void *)cb->args[1]);
10113 	}
10114 
10115 	if (!rdev->ops->testmode_dump) {
10116 		err = -EOPNOTSUPP;
10117 		goto out_err;
10118 	}
10119 
10120 	while (1) {
10121 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
10122 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
10123 					   NL80211_CMD_TESTMODE);
10124 		struct nlattr *tmdata;
10125 
10126 		if (!hdr)
10127 			break;
10128 
10129 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
10130 			genlmsg_cancel(skb, hdr);
10131 			break;
10132 		}
10133 
10134 		tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
10135 		if (!tmdata) {
10136 			genlmsg_cancel(skb, hdr);
10137 			break;
10138 		}
10139 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
10140 		nla_nest_end(skb, tmdata);
10141 
10142 		if (err == -ENOBUFS || err == -ENOENT) {
10143 			genlmsg_cancel(skb, hdr);
10144 			break;
10145 		} else if (err) {
10146 			genlmsg_cancel(skb, hdr);
10147 			goto out_err;
10148 		}
10149 
10150 		genlmsg_end(skb, hdr);
10151 	}
10152 
10153 	err = skb->len;
10154 	/* see above */
10155 	cb->args[0] = phy_idx + 1;
10156  out_err:
10157 	kfree(attrbuf);
10158 	rtnl_unlock();
10159 	return err;
10160 }
10161 #endif
10162 
10163 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
10164 {
10165 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10166 	struct net_device *dev = info->user_ptr[1];
10167 	struct cfg80211_connect_params connect;
10168 	struct wiphy *wiphy;
10169 	struct cfg80211_cached_keys *connkeys = NULL;
10170 	u32 freq = 0;
10171 	int err;
10172 
10173 	memset(&connect, 0, sizeof(connect));
10174 
10175 	if (!info->attrs[NL80211_ATTR_SSID] ||
10176 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
10177 		return -EINVAL;
10178 
10179 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
10180 		connect.auth_type =
10181 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10182 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
10183 					     NL80211_CMD_CONNECT))
10184 			return -EINVAL;
10185 	} else
10186 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
10187 
10188 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
10189 
10190 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
10191 	    !wiphy_ext_feature_isset(&rdev->wiphy,
10192 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
10193 		return -EINVAL;
10194 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
10195 
10196 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
10197 				      NL80211_MAX_NR_CIPHER_SUITES);
10198 	if (err)
10199 		return err;
10200 
10201 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10202 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10203 		return -EOPNOTSUPP;
10204 
10205 	wiphy = &rdev->wiphy;
10206 
10207 	connect.bg_scan_period = -1;
10208 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
10209 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
10210 		connect.bg_scan_period =
10211 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
10212 	}
10213 
10214 	if (info->attrs[NL80211_ATTR_MAC])
10215 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10216 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
10217 		connect.bssid_hint =
10218 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
10219 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10220 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10221 
10222 	if (info->attrs[NL80211_ATTR_IE]) {
10223 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10224 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10225 	}
10226 
10227 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
10228 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
10229 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
10230 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10231 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
10232 			return -EOPNOTSUPP;
10233 	} else {
10234 		connect.mfp = NL80211_MFP_NO;
10235 	}
10236 
10237 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
10238 		connect.prev_bssid =
10239 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
10240 
10241 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ])
10242 		freq = MHZ_TO_KHZ(nla_get_u32(
10243 					info->attrs[NL80211_ATTR_WIPHY_FREQ]));
10244 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10245 		freq +=
10246 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10247 
10248 	if (freq) {
10249 		connect.channel = nl80211_get_valid_chan(wiphy, freq);
10250 		if (!connect.channel)
10251 			return -EINVAL;
10252 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
10253 		freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
10254 		freq = MHZ_TO_KHZ(freq);
10255 		connect.channel_hint = nl80211_get_valid_chan(wiphy, freq);
10256 		if (!connect.channel_hint)
10257 			return -EINVAL;
10258 	}
10259 
10260 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
10261 		connect.edmg.channels =
10262 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
10263 
10264 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
10265 			connect.edmg.bw_config =
10266 				nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
10267 	}
10268 
10269 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
10270 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
10271 		if (IS_ERR(connkeys))
10272 			return PTR_ERR(connkeys);
10273 	}
10274 
10275 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
10276 		connect.flags |= ASSOC_REQ_DISABLE_HT;
10277 
10278 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10279 		memcpy(&connect.ht_capa_mask,
10280 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
10281 		       sizeof(connect.ht_capa_mask));
10282 
10283 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
10284 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
10285 			kzfree(connkeys);
10286 			return -EINVAL;
10287 		}
10288 		memcpy(&connect.ht_capa,
10289 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
10290 		       sizeof(connect.ht_capa));
10291 	}
10292 
10293 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
10294 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
10295 
10296 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10297 		memcpy(&connect.vht_capa_mask,
10298 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
10299 		       sizeof(connect.vht_capa_mask));
10300 
10301 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
10302 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
10303 			kzfree(connkeys);
10304 			return -EINVAL;
10305 		}
10306 		memcpy(&connect.vht_capa,
10307 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
10308 		       sizeof(connect.vht_capa));
10309 	}
10310 
10311 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
10312 		if (!((rdev->wiphy.features &
10313 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
10314 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
10315 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10316 					     NL80211_EXT_FEATURE_RRM)) {
10317 			kzfree(connkeys);
10318 			return -EINVAL;
10319 		}
10320 		connect.flags |= ASSOC_REQ_USE_RRM;
10321 	}
10322 
10323 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
10324 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
10325 		kzfree(connkeys);
10326 		return -EOPNOTSUPP;
10327 	}
10328 
10329 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
10330 		/* bss selection makes no sense if bssid is set */
10331 		if (connect.bssid) {
10332 			kzfree(connkeys);
10333 			return -EINVAL;
10334 		}
10335 
10336 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
10337 				       wiphy, &connect.bss_select);
10338 		if (err) {
10339 			kzfree(connkeys);
10340 			return err;
10341 		}
10342 	}
10343 
10344 	if (wiphy_ext_feature_isset(&rdev->wiphy,
10345 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
10346 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
10347 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
10348 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
10349 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10350 		connect.fils_erp_username =
10351 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10352 		connect.fils_erp_username_len =
10353 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10354 		connect.fils_erp_realm =
10355 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10356 		connect.fils_erp_realm_len =
10357 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10358 		connect.fils_erp_next_seq_num =
10359 			nla_get_u16(
10360 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
10361 		connect.fils_erp_rrk =
10362 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10363 		connect.fils_erp_rrk_len =
10364 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10365 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
10366 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
10367 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
10368 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10369 		kzfree(connkeys);
10370 		return -EINVAL;
10371 	}
10372 
10373 	if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
10374 		if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10375 			kzfree(connkeys);
10376 			GENL_SET_ERR_MSG(info,
10377 					 "external auth requires connection ownership");
10378 			return -EINVAL;
10379 		}
10380 		connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
10381 	}
10382 
10383 	wdev_lock(dev->ieee80211_ptr);
10384 
10385 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
10386 			       connect.prev_bssid);
10387 	if (err)
10388 		kzfree(connkeys);
10389 
10390 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10391 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
10392 		if (connect.bssid)
10393 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
10394 			       connect.bssid, ETH_ALEN);
10395 		else
10396 			memset(dev->ieee80211_ptr->disconnect_bssid,
10397 			       0, ETH_ALEN);
10398 	}
10399 
10400 	wdev_unlock(dev->ieee80211_ptr);
10401 
10402 	return err;
10403 }
10404 
10405 static int nl80211_update_connect_params(struct sk_buff *skb,
10406 					 struct genl_info *info)
10407 {
10408 	struct cfg80211_connect_params connect = {};
10409 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10410 	struct net_device *dev = info->user_ptr[1];
10411 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10412 	bool fils_sk_offload;
10413 	u32 auth_type;
10414 	u32 changed = 0;
10415 	int ret;
10416 
10417 	if (!rdev->ops->update_connect_params)
10418 		return -EOPNOTSUPP;
10419 
10420 	if (info->attrs[NL80211_ATTR_IE]) {
10421 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10422 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10423 		changed |= UPDATE_ASSOC_IES;
10424 	}
10425 
10426 	fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
10427 						  NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
10428 
10429 	/*
10430 	 * when driver supports fils-sk offload all attributes must be
10431 	 * provided. So the else covers "fils-sk-not-all" and
10432 	 * "no-fils-sk-any".
10433 	 */
10434 	if (fils_sk_offload &&
10435 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
10436 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
10437 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
10438 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10439 		connect.fils_erp_username =
10440 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10441 		connect.fils_erp_username_len =
10442 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10443 		connect.fils_erp_realm =
10444 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10445 		connect.fils_erp_realm_len =
10446 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10447 		connect.fils_erp_next_seq_num =
10448 			nla_get_u16(
10449 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
10450 		connect.fils_erp_rrk =
10451 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10452 		connect.fils_erp_rrk_len =
10453 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10454 		changed |= UPDATE_FILS_ERP_INFO;
10455 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
10456 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
10457 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
10458 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10459 		return -EINVAL;
10460 	}
10461 
10462 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
10463 		auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10464 		if (!nl80211_valid_auth_type(rdev, auth_type,
10465 					     NL80211_CMD_CONNECT))
10466 			return -EINVAL;
10467 
10468 		if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
10469 		    fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
10470 			return -EINVAL;
10471 
10472 		connect.auth_type = auth_type;
10473 		changed |= UPDATE_AUTH_TYPE;
10474 	}
10475 
10476 	wdev_lock(dev->ieee80211_ptr);
10477 	if (!wdev->current_bss)
10478 		ret = -ENOLINK;
10479 	else
10480 		ret = rdev_update_connect_params(rdev, dev, &connect, changed);
10481 	wdev_unlock(dev->ieee80211_ptr);
10482 
10483 	return ret;
10484 }
10485 
10486 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
10487 {
10488 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10489 	struct net_device *dev = info->user_ptr[1];
10490 	u16 reason;
10491 	int ret;
10492 
10493 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
10494 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10495 		return -EPERM;
10496 
10497 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
10498 		reason = WLAN_REASON_DEAUTH_LEAVING;
10499 	else
10500 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
10501 
10502 	if (reason == 0)
10503 		return -EINVAL;
10504 
10505 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10506 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10507 		return -EOPNOTSUPP;
10508 
10509 	wdev_lock(dev->ieee80211_ptr);
10510 	ret = cfg80211_disconnect(rdev, dev, reason, true);
10511 	wdev_unlock(dev->ieee80211_ptr);
10512 	return ret;
10513 }
10514 
10515 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
10516 {
10517 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10518 	struct net *net;
10519 	int err;
10520 
10521 	if (info->attrs[NL80211_ATTR_PID]) {
10522 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
10523 
10524 		net = get_net_ns_by_pid(pid);
10525 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
10526 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
10527 
10528 		net = get_net_ns_by_fd(fd);
10529 	} else {
10530 		return -EINVAL;
10531 	}
10532 
10533 	if (IS_ERR(net))
10534 		return PTR_ERR(net);
10535 
10536 	err = 0;
10537 
10538 	/* check if anything to do */
10539 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
10540 		err = cfg80211_switch_netns(rdev, net);
10541 
10542 	put_net(net);
10543 	return err;
10544 }
10545 
10546 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
10547 {
10548 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10549 	int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
10550 			struct cfg80211_pmksa *pmksa) = NULL;
10551 	struct net_device *dev = info->user_ptr[1];
10552 	struct cfg80211_pmksa pmksa;
10553 
10554 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
10555 
10556 	if (!info->attrs[NL80211_ATTR_PMKID])
10557 		return -EINVAL;
10558 
10559 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
10560 
10561 	if (info->attrs[NL80211_ATTR_MAC]) {
10562 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10563 	} else if (info->attrs[NL80211_ATTR_SSID] &&
10564 		   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
10565 		   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
10566 		    info->attrs[NL80211_ATTR_PMK])) {
10567 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10568 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10569 		pmksa.cache_id =
10570 			nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
10571 	} else {
10572 		return -EINVAL;
10573 	}
10574 	if (info->attrs[NL80211_ATTR_PMK]) {
10575 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10576 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
10577 	}
10578 
10579 	if (info->attrs[NL80211_ATTR_PMK_LIFETIME])
10580 		pmksa.pmk_lifetime =
10581 			nla_get_u32(info->attrs[NL80211_ATTR_PMK_LIFETIME]);
10582 
10583 	if (info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD])
10584 		pmksa.pmk_reauth_threshold =
10585 			nla_get_u8(
10586 				info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD]);
10587 
10588 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10589 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
10590 	    !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
10591 	      wiphy_ext_feature_isset(&rdev->wiphy,
10592 				      NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
10593 		return -EOPNOTSUPP;
10594 
10595 	switch (info->genlhdr->cmd) {
10596 	case NL80211_CMD_SET_PMKSA:
10597 		rdev_ops = rdev->ops->set_pmksa;
10598 		break;
10599 	case NL80211_CMD_DEL_PMKSA:
10600 		rdev_ops = rdev->ops->del_pmksa;
10601 		break;
10602 	default:
10603 		WARN_ON(1);
10604 		break;
10605 	}
10606 
10607 	if (!rdev_ops)
10608 		return -EOPNOTSUPP;
10609 
10610 	return rdev_ops(&rdev->wiphy, dev, &pmksa);
10611 }
10612 
10613 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
10614 {
10615 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10616 	struct net_device *dev = info->user_ptr[1];
10617 
10618 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10619 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10620 		return -EOPNOTSUPP;
10621 
10622 	if (!rdev->ops->flush_pmksa)
10623 		return -EOPNOTSUPP;
10624 
10625 	return rdev_flush_pmksa(rdev, dev);
10626 }
10627 
10628 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
10629 {
10630 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10631 	struct net_device *dev = info->user_ptr[1];
10632 	u8 action_code, dialog_token;
10633 	u32 peer_capability = 0;
10634 	u16 status_code;
10635 	u8 *peer;
10636 	bool initiator;
10637 
10638 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
10639 	    !rdev->ops->tdls_mgmt)
10640 		return -EOPNOTSUPP;
10641 
10642 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
10643 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
10644 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
10645 	    !info->attrs[NL80211_ATTR_IE] ||
10646 	    !info->attrs[NL80211_ATTR_MAC])
10647 		return -EINVAL;
10648 
10649 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10650 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
10651 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
10652 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
10653 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
10654 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
10655 		peer_capability =
10656 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
10657 
10658 	return rdev_tdls_mgmt(rdev, dev, peer, action_code,
10659 			      dialog_token, status_code, peer_capability,
10660 			      initiator,
10661 			      nla_data(info->attrs[NL80211_ATTR_IE]),
10662 			      nla_len(info->attrs[NL80211_ATTR_IE]));
10663 }
10664 
10665 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
10666 {
10667 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10668 	struct net_device *dev = info->user_ptr[1];
10669 	enum nl80211_tdls_operation operation;
10670 	u8 *peer;
10671 
10672 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
10673 	    !rdev->ops->tdls_oper)
10674 		return -EOPNOTSUPP;
10675 
10676 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
10677 	    !info->attrs[NL80211_ATTR_MAC])
10678 		return -EINVAL;
10679 
10680 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
10681 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10682 
10683 	return rdev_tdls_oper(rdev, dev, peer, operation);
10684 }
10685 
10686 static int nl80211_remain_on_channel(struct sk_buff *skb,
10687 				     struct genl_info *info)
10688 {
10689 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10690 	struct wireless_dev *wdev = info->user_ptr[1];
10691 	struct cfg80211_chan_def chandef;
10692 	const struct cfg80211_chan_def *compat_chandef;
10693 	struct sk_buff *msg;
10694 	void *hdr;
10695 	u64 cookie;
10696 	u32 duration;
10697 	int err;
10698 
10699 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
10700 	    !info->attrs[NL80211_ATTR_DURATION])
10701 		return -EINVAL;
10702 
10703 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
10704 
10705 	if (!rdev->ops->remain_on_channel ||
10706 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
10707 		return -EOPNOTSUPP;
10708 
10709 	/*
10710 	 * We should be on that channel for at least a minimum amount of
10711 	 * time (10ms) but no longer than the driver supports.
10712 	 */
10713 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
10714 	    duration > rdev->wiphy.max_remain_on_channel_duration)
10715 		return -EINVAL;
10716 
10717 	err = nl80211_parse_chandef(rdev, info, &chandef);
10718 	if (err)
10719 		return err;
10720 
10721 	wdev_lock(wdev);
10722 	if (!cfg80211_off_channel_oper_allowed(wdev) &&
10723 	    !cfg80211_chandef_identical(&wdev->chandef, &chandef)) {
10724 		compat_chandef = cfg80211_chandef_compatible(&wdev->chandef,
10725 							     &chandef);
10726 		if (compat_chandef != &chandef) {
10727 			wdev_unlock(wdev);
10728 			return -EBUSY;
10729 		}
10730 	}
10731 	wdev_unlock(wdev);
10732 
10733 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10734 	if (!msg)
10735 		return -ENOMEM;
10736 
10737 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10738 			     NL80211_CMD_REMAIN_ON_CHANNEL);
10739 	if (!hdr) {
10740 		err = -ENOBUFS;
10741 		goto free_msg;
10742 	}
10743 
10744 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
10745 				     duration, &cookie);
10746 
10747 	if (err)
10748 		goto free_msg;
10749 
10750 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
10751 			      NL80211_ATTR_PAD))
10752 		goto nla_put_failure;
10753 
10754 	genlmsg_end(msg, hdr);
10755 
10756 	return genlmsg_reply(msg, info);
10757 
10758  nla_put_failure:
10759 	err = -ENOBUFS;
10760  free_msg:
10761 	nlmsg_free(msg);
10762 	return err;
10763 }
10764 
10765 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
10766 					    struct genl_info *info)
10767 {
10768 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10769 	struct wireless_dev *wdev = info->user_ptr[1];
10770 	u64 cookie;
10771 
10772 	if (!info->attrs[NL80211_ATTR_COOKIE])
10773 		return -EINVAL;
10774 
10775 	if (!rdev->ops->cancel_remain_on_channel)
10776 		return -EOPNOTSUPP;
10777 
10778 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
10779 
10780 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
10781 }
10782 
10783 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
10784 				       struct genl_info *info)
10785 {
10786 	struct cfg80211_bitrate_mask mask;
10787 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10788 	struct net_device *dev = info->user_ptr[1];
10789 	int err;
10790 
10791 	if (!rdev->ops->set_bitrate_mask)
10792 		return -EOPNOTSUPP;
10793 
10794 	err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
10795 					    NL80211_ATTR_TX_RATES, &mask);
10796 	if (err)
10797 		return err;
10798 
10799 	return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
10800 }
10801 
10802 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
10803 {
10804 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10805 	struct wireless_dev *wdev = info->user_ptr[1];
10806 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
10807 
10808 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
10809 		return -EINVAL;
10810 
10811 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
10812 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
10813 
10814 	switch (wdev->iftype) {
10815 	case NL80211_IFTYPE_STATION:
10816 	case NL80211_IFTYPE_ADHOC:
10817 	case NL80211_IFTYPE_P2P_CLIENT:
10818 	case NL80211_IFTYPE_AP:
10819 	case NL80211_IFTYPE_AP_VLAN:
10820 	case NL80211_IFTYPE_MESH_POINT:
10821 	case NL80211_IFTYPE_P2P_GO:
10822 	case NL80211_IFTYPE_P2P_DEVICE:
10823 		break;
10824 	case NL80211_IFTYPE_NAN:
10825 	default:
10826 		return -EOPNOTSUPP;
10827 	}
10828 
10829 	/* not much point in registering if we can't reply */
10830 	if (!rdev->ops->mgmt_tx)
10831 		return -EOPNOTSUPP;
10832 
10833 	if (info->attrs[NL80211_ATTR_RECEIVE_MULTICAST] &&
10834 	    !wiphy_ext_feature_isset(&rdev->wiphy,
10835 				     NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS)) {
10836 		GENL_SET_ERR_MSG(info,
10837 				 "multicast RX registrations are not supported");
10838 		return -EOPNOTSUPP;
10839 	}
10840 
10841 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
10842 					   nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
10843 					   nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]),
10844 					   info->attrs[NL80211_ATTR_RECEIVE_MULTICAST],
10845 					   info->extack);
10846 }
10847 
10848 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
10849 {
10850 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10851 	struct wireless_dev *wdev = info->user_ptr[1];
10852 	struct cfg80211_chan_def chandef;
10853 	int err;
10854 	void *hdr = NULL;
10855 	u64 cookie;
10856 	struct sk_buff *msg = NULL;
10857 	struct cfg80211_mgmt_tx_params params = {
10858 		.dont_wait_for_ack =
10859 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
10860 	};
10861 
10862 	if (!info->attrs[NL80211_ATTR_FRAME])
10863 		return -EINVAL;
10864 
10865 	if (!rdev->ops->mgmt_tx)
10866 		return -EOPNOTSUPP;
10867 
10868 	switch (wdev->iftype) {
10869 	case NL80211_IFTYPE_P2P_DEVICE:
10870 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10871 			return -EINVAL;
10872 	case NL80211_IFTYPE_STATION:
10873 	case NL80211_IFTYPE_ADHOC:
10874 	case NL80211_IFTYPE_P2P_CLIENT:
10875 	case NL80211_IFTYPE_AP:
10876 	case NL80211_IFTYPE_AP_VLAN:
10877 	case NL80211_IFTYPE_MESH_POINT:
10878 	case NL80211_IFTYPE_P2P_GO:
10879 		break;
10880 	case NL80211_IFTYPE_NAN:
10881 	default:
10882 		return -EOPNOTSUPP;
10883 	}
10884 
10885 	if (info->attrs[NL80211_ATTR_DURATION]) {
10886 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
10887 			return -EINVAL;
10888 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
10889 
10890 		/*
10891 		 * We should wait on the channel for at least a minimum amount
10892 		 * of time (10ms) but no longer than the driver supports.
10893 		 */
10894 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
10895 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
10896 			return -EINVAL;
10897 	}
10898 
10899 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
10900 
10901 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
10902 		return -EINVAL;
10903 
10904 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
10905 
10906 	/* get the channel if any has been specified, otherwise pass NULL to
10907 	 * the driver. The latter will use the current one
10908 	 */
10909 	chandef.chan = NULL;
10910 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
10911 		err = nl80211_parse_chandef(rdev, info, &chandef);
10912 		if (err)
10913 			return err;
10914 	}
10915 
10916 	if (!chandef.chan && params.offchan)
10917 		return -EINVAL;
10918 
10919 	wdev_lock(wdev);
10920 	if (params.offchan && !cfg80211_off_channel_oper_allowed(wdev)) {
10921 		wdev_unlock(wdev);
10922 		return -EBUSY;
10923 	}
10924 	wdev_unlock(wdev);
10925 
10926 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
10927 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
10928 
10929 	if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
10930 		int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
10931 		int i;
10932 
10933 		if (len % sizeof(u16))
10934 			return -EINVAL;
10935 
10936 		params.n_csa_offsets = len / sizeof(u16);
10937 		params.csa_offsets =
10938 			nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
10939 
10940 		/* check that all the offsets fit the frame */
10941 		for (i = 0; i < params.n_csa_offsets; i++) {
10942 			if (params.csa_offsets[i] >= params.len)
10943 				return -EINVAL;
10944 		}
10945 	}
10946 
10947 	if (!params.dont_wait_for_ack) {
10948 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10949 		if (!msg)
10950 			return -ENOMEM;
10951 
10952 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10953 				     NL80211_CMD_FRAME);
10954 		if (!hdr) {
10955 			err = -ENOBUFS;
10956 			goto free_msg;
10957 		}
10958 	}
10959 
10960 	params.chan = chandef.chan;
10961 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
10962 	if (err)
10963 		goto free_msg;
10964 
10965 	if (msg) {
10966 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
10967 				      NL80211_ATTR_PAD))
10968 			goto nla_put_failure;
10969 
10970 		genlmsg_end(msg, hdr);
10971 		return genlmsg_reply(msg, info);
10972 	}
10973 
10974 	return 0;
10975 
10976  nla_put_failure:
10977 	err = -ENOBUFS;
10978  free_msg:
10979 	nlmsg_free(msg);
10980 	return err;
10981 }
10982 
10983 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
10984 {
10985 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10986 	struct wireless_dev *wdev = info->user_ptr[1];
10987 	u64 cookie;
10988 
10989 	if (!info->attrs[NL80211_ATTR_COOKIE])
10990 		return -EINVAL;
10991 
10992 	if (!rdev->ops->mgmt_tx_cancel_wait)
10993 		return -EOPNOTSUPP;
10994 
10995 	switch (wdev->iftype) {
10996 	case NL80211_IFTYPE_STATION:
10997 	case NL80211_IFTYPE_ADHOC:
10998 	case NL80211_IFTYPE_P2P_CLIENT:
10999 	case NL80211_IFTYPE_AP:
11000 	case NL80211_IFTYPE_AP_VLAN:
11001 	case NL80211_IFTYPE_P2P_GO:
11002 	case NL80211_IFTYPE_P2P_DEVICE:
11003 		break;
11004 	case NL80211_IFTYPE_NAN:
11005 	default:
11006 		return -EOPNOTSUPP;
11007 	}
11008 
11009 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
11010 
11011 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
11012 }
11013 
11014 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
11015 {
11016 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11017 	struct wireless_dev *wdev;
11018 	struct net_device *dev = info->user_ptr[1];
11019 	u8 ps_state;
11020 	bool state;
11021 	int err;
11022 
11023 	if (!info->attrs[NL80211_ATTR_PS_STATE])
11024 		return -EINVAL;
11025 
11026 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
11027 
11028 	wdev = dev->ieee80211_ptr;
11029 
11030 	if (!rdev->ops->set_power_mgmt)
11031 		return -EOPNOTSUPP;
11032 
11033 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
11034 
11035 	if (state == wdev->ps)
11036 		return 0;
11037 
11038 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
11039 	if (!err)
11040 		wdev->ps = state;
11041 	return err;
11042 }
11043 
11044 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
11045 {
11046 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11047 	enum nl80211_ps_state ps_state;
11048 	struct wireless_dev *wdev;
11049 	struct net_device *dev = info->user_ptr[1];
11050 	struct sk_buff *msg;
11051 	void *hdr;
11052 	int err;
11053 
11054 	wdev = dev->ieee80211_ptr;
11055 
11056 	if (!rdev->ops->set_power_mgmt)
11057 		return -EOPNOTSUPP;
11058 
11059 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11060 	if (!msg)
11061 		return -ENOMEM;
11062 
11063 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11064 			     NL80211_CMD_GET_POWER_SAVE);
11065 	if (!hdr) {
11066 		err = -ENOBUFS;
11067 		goto free_msg;
11068 	}
11069 
11070 	if (wdev->ps)
11071 		ps_state = NL80211_PS_ENABLED;
11072 	else
11073 		ps_state = NL80211_PS_DISABLED;
11074 
11075 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
11076 		goto nla_put_failure;
11077 
11078 	genlmsg_end(msg, hdr);
11079 	return genlmsg_reply(msg, info);
11080 
11081  nla_put_failure:
11082 	err = -ENOBUFS;
11083  free_msg:
11084 	nlmsg_free(msg);
11085 	return err;
11086 }
11087 
11088 static const struct nla_policy
11089 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
11090 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
11091 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
11092 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
11093 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
11094 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
11095 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
11096 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
11097 };
11098 
11099 static int nl80211_set_cqm_txe(struct genl_info *info,
11100 			       u32 rate, u32 pkts, u32 intvl)
11101 {
11102 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11103 	struct net_device *dev = info->user_ptr[1];
11104 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11105 
11106 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
11107 		return -EINVAL;
11108 
11109 	if (!rdev->ops->set_cqm_txe_config)
11110 		return -EOPNOTSUPP;
11111 
11112 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
11113 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
11114 		return -EOPNOTSUPP;
11115 
11116 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
11117 }
11118 
11119 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
11120 				    struct net_device *dev)
11121 {
11122 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11123 	s32 last, low, high;
11124 	u32 hyst;
11125 	int i, n, low_index;
11126 	int err;
11127 
11128 	/* RSSI reporting disabled? */
11129 	if (!wdev->cqm_config)
11130 		return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
11131 
11132 	/*
11133 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
11134 	 * event has been received yet, we should receive an event after a
11135 	 * connection is established and enough beacons received to calculate
11136 	 * the average.
11137 	 */
11138 	if (!wdev->cqm_config->last_rssi_event_value && wdev->current_bss &&
11139 	    rdev->ops->get_station) {
11140 		struct station_info sinfo = {};
11141 		u8 *mac_addr;
11142 
11143 		mac_addr = wdev->current_bss->pub.bssid;
11144 
11145 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
11146 		if (err)
11147 			return err;
11148 
11149 		cfg80211_sinfo_release_content(&sinfo);
11150 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
11151 			wdev->cqm_config->last_rssi_event_value =
11152 				(s8) sinfo.rx_beacon_signal_avg;
11153 	}
11154 
11155 	last = wdev->cqm_config->last_rssi_event_value;
11156 	hyst = wdev->cqm_config->rssi_hyst;
11157 	n = wdev->cqm_config->n_rssi_thresholds;
11158 
11159 	for (i = 0; i < n; i++) {
11160 		i = array_index_nospec(i, n);
11161 		if (last < wdev->cqm_config->rssi_thresholds[i])
11162 			break;
11163 	}
11164 
11165 	low_index = i - 1;
11166 	if (low_index >= 0) {
11167 		low_index = array_index_nospec(low_index, n);
11168 		low = wdev->cqm_config->rssi_thresholds[low_index] - hyst;
11169 	} else {
11170 		low = S32_MIN;
11171 	}
11172 	if (i < n) {
11173 		i = array_index_nospec(i, n);
11174 		high = wdev->cqm_config->rssi_thresholds[i] + hyst - 1;
11175 	} else {
11176 		high = S32_MAX;
11177 	}
11178 
11179 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
11180 }
11181 
11182 static int nl80211_set_cqm_rssi(struct genl_info *info,
11183 				const s32 *thresholds, int n_thresholds,
11184 				u32 hysteresis)
11185 {
11186 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11187 	struct net_device *dev = info->user_ptr[1];
11188 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11189 	int i, err;
11190 	s32 prev = S32_MIN;
11191 
11192 	/* Check all values negative and sorted */
11193 	for (i = 0; i < n_thresholds; i++) {
11194 		if (thresholds[i] > 0 || thresholds[i] <= prev)
11195 			return -EINVAL;
11196 
11197 		prev = thresholds[i];
11198 	}
11199 
11200 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
11201 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
11202 		return -EOPNOTSUPP;
11203 
11204 	wdev_lock(wdev);
11205 	cfg80211_cqm_config_free(wdev);
11206 	wdev_unlock(wdev);
11207 
11208 	if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
11209 		if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
11210 			return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
11211 
11212 		return rdev_set_cqm_rssi_config(rdev, dev,
11213 						thresholds[0], hysteresis);
11214 	}
11215 
11216 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
11217 				     NL80211_EXT_FEATURE_CQM_RSSI_LIST))
11218 		return -EOPNOTSUPP;
11219 
11220 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
11221 		n_thresholds = 0;
11222 
11223 	wdev_lock(wdev);
11224 	if (n_thresholds) {
11225 		struct cfg80211_cqm_config *cqm_config;
11226 
11227 		cqm_config = kzalloc(sizeof(struct cfg80211_cqm_config) +
11228 				     n_thresholds * sizeof(s32), GFP_KERNEL);
11229 		if (!cqm_config) {
11230 			err = -ENOMEM;
11231 			goto unlock;
11232 		}
11233 
11234 		cqm_config->rssi_hyst = hysteresis;
11235 		cqm_config->n_rssi_thresholds = n_thresholds;
11236 		memcpy(cqm_config->rssi_thresholds, thresholds,
11237 		       n_thresholds * sizeof(s32));
11238 
11239 		wdev->cqm_config = cqm_config;
11240 	}
11241 
11242 	err = cfg80211_cqm_rssi_update(rdev, dev);
11243 
11244 unlock:
11245 	wdev_unlock(wdev);
11246 
11247 	return err;
11248 }
11249 
11250 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
11251 {
11252 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
11253 	struct nlattr *cqm;
11254 	int err;
11255 
11256 	cqm = info->attrs[NL80211_ATTR_CQM];
11257 	if (!cqm)
11258 		return -EINVAL;
11259 
11260 	err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
11261 					  nl80211_attr_cqm_policy,
11262 					  info->extack);
11263 	if (err)
11264 		return err;
11265 
11266 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
11267 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
11268 		const s32 *thresholds =
11269 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
11270 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
11271 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
11272 
11273 		if (len % 4)
11274 			return -EINVAL;
11275 
11276 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
11277 					    hysteresis);
11278 	}
11279 
11280 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
11281 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
11282 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
11283 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
11284 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
11285 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
11286 
11287 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
11288 	}
11289 
11290 	return -EINVAL;
11291 }
11292 
11293 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
11294 {
11295 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11296 	struct net_device *dev = info->user_ptr[1];
11297 	struct ocb_setup setup = {};
11298 	int err;
11299 
11300 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
11301 	if (err)
11302 		return err;
11303 
11304 	return cfg80211_join_ocb(rdev, dev, &setup);
11305 }
11306 
11307 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
11308 {
11309 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11310 	struct net_device *dev = info->user_ptr[1];
11311 
11312 	return cfg80211_leave_ocb(rdev, dev);
11313 }
11314 
11315 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
11316 {
11317 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11318 	struct net_device *dev = info->user_ptr[1];
11319 	struct mesh_config cfg;
11320 	struct mesh_setup setup;
11321 	int err;
11322 
11323 	/* start with default */
11324 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
11325 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
11326 
11327 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
11328 		/* and parse parameters if given */
11329 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
11330 		if (err)
11331 			return err;
11332 	}
11333 
11334 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
11335 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
11336 		return -EINVAL;
11337 
11338 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
11339 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
11340 
11341 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11342 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
11343 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11344 			return -EINVAL;
11345 
11346 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
11347 		setup.beacon_interval =
11348 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11349 
11350 		err = cfg80211_validate_beacon_int(rdev,
11351 						   NL80211_IFTYPE_MESH_POINT,
11352 						   setup.beacon_interval);
11353 		if (err)
11354 			return err;
11355 	}
11356 
11357 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
11358 		setup.dtim_period =
11359 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
11360 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
11361 			return -EINVAL;
11362 	}
11363 
11364 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
11365 		/* parse additional setup parameters if given */
11366 		err = nl80211_parse_mesh_setup(info, &setup);
11367 		if (err)
11368 			return err;
11369 	}
11370 
11371 	if (setup.user_mpm)
11372 		cfg.auto_open_plinks = false;
11373 
11374 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
11375 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
11376 		if (err)
11377 			return err;
11378 	} else {
11379 		/* __cfg80211_join_mesh() will sort it out */
11380 		setup.chandef.chan = NULL;
11381 	}
11382 
11383 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11384 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11385 		int n_rates =
11386 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11387 		struct ieee80211_supported_band *sband;
11388 
11389 		if (!setup.chandef.chan)
11390 			return -EINVAL;
11391 
11392 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
11393 
11394 		err = ieee80211_get_ratemask(sband, rates, n_rates,
11395 					     &setup.basic_rates);
11396 		if (err)
11397 			return err;
11398 	}
11399 
11400 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
11401 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
11402 						    NL80211_ATTR_TX_RATES,
11403 						    &setup.beacon_rate);
11404 		if (err)
11405 			return err;
11406 
11407 		if (!setup.chandef.chan)
11408 			return -EINVAL;
11409 
11410 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
11411 					      &setup.beacon_rate);
11412 		if (err)
11413 			return err;
11414 	}
11415 
11416 	setup.userspace_handles_dfs =
11417 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11418 
11419 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11420 		int r = validate_pae_over_nl80211(rdev, info);
11421 
11422 		if (r < 0)
11423 			return r;
11424 
11425 		setup.control_port_over_nl80211 = true;
11426 	}
11427 
11428 	wdev_lock(dev->ieee80211_ptr);
11429 	err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
11430 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
11431 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11432 	wdev_unlock(dev->ieee80211_ptr);
11433 
11434 	return err;
11435 }
11436 
11437 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
11438 {
11439 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11440 	struct net_device *dev = info->user_ptr[1];
11441 
11442 	return cfg80211_leave_mesh(rdev, dev);
11443 }
11444 
11445 #ifdef CONFIG_PM
11446 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
11447 					struct cfg80211_registered_device *rdev)
11448 {
11449 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
11450 	struct nlattr *nl_pats, *nl_pat;
11451 	int i, pat_len;
11452 
11453 	if (!wowlan->n_patterns)
11454 		return 0;
11455 
11456 	nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
11457 	if (!nl_pats)
11458 		return -ENOBUFS;
11459 
11460 	for (i = 0; i < wowlan->n_patterns; i++) {
11461 		nl_pat = nla_nest_start_noflag(msg, i + 1);
11462 		if (!nl_pat)
11463 			return -ENOBUFS;
11464 		pat_len = wowlan->patterns[i].pattern_len;
11465 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
11466 			    wowlan->patterns[i].mask) ||
11467 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
11468 			    wowlan->patterns[i].pattern) ||
11469 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
11470 				wowlan->patterns[i].pkt_offset))
11471 			return -ENOBUFS;
11472 		nla_nest_end(msg, nl_pat);
11473 	}
11474 	nla_nest_end(msg, nl_pats);
11475 
11476 	return 0;
11477 }
11478 
11479 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
11480 				   struct cfg80211_wowlan_tcp *tcp)
11481 {
11482 	struct nlattr *nl_tcp;
11483 
11484 	if (!tcp)
11485 		return 0;
11486 
11487 	nl_tcp = nla_nest_start_noflag(msg,
11488 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
11489 	if (!nl_tcp)
11490 		return -ENOBUFS;
11491 
11492 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
11493 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
11494 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
11495 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
11496 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
11497 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
11498 		    tcp->payload_len, tcp->payload) ||
11499 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
11500 			tcp->data_interval) ||
11501 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
11502 		    tcp->wake_len, tcp->wake_data) ||
11503 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
11504 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
11505 		return -ENOBUFS;
11506 
11507 	if (tcp->payload_seq.len &&
11508 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
11509 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
11510 		return -ENOBUFS;
11511 
11512 	if (tcp->payload_tok.len &&
11513 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
11514 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
11515 		    &tcp->payload_tok))
11516 		return -ENOBUFS;
11517 
11518 	nla_nest_end(msg, nl_tcp);
11519 
11520 	return 0;
11521 }
11522 
11523 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
11524 				  struct cfg80211_sched_scan_request *req)
11525 {
11526 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
11527 	int i;
11528 
11529 	if (!req)
11530 		return 0;
11531 
11532 	nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
11533 	if (!nd)
11534 		return -ENOBUFS;
11535 
11536 	if (req->n_scan_plans == 1 &&
11537 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
11538 			req->scan_plans[0].interval * 1000))
11539 		return -ENOBUFS;
11540 
11541 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
11542 		return -ENOBUFS;
11543 
11544 	if (req->relative_rssi_set) {
11545 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
11546 
11547 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
11548 			       req->relative_rssi))
11549 			return -ENOBUFS;
11550 
11551 		rssi_adjust.band = req->rssi_adjust.band;
11552 		rssi_adjust.delta = req->rssi_adjust.delta;
11553 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
11554 			    sizeof(rssi_adjust), &rssi_adjust))
11555 			return -ENOBUFS;
11556 	}
11557 
11558 	freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
11559 	if (!freqs)
11560 		return -ENOBUFS;
11561 
11562 	for (i = 0; i < req->n_channels; i++) {
11563 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
11564 			return -ENOBUFS;
11565 	}
11566 
11567 	nla_nest_end(msg, freqs);
11568 
11569 	if (req->n_match_sets) {
11570 		matches = nla_nest_start_noflag(msg,
11571 						NL80211_ATTR_SCHED_SCAN_MATCH);
11572 		if (!matches)
11573 			return -ENOBUFS;
11574 
11575 		for (i = 0; i < req->n_match_sets; i++) {
11576 			match = nla_nest_start_noflag(msg, i);
11577 			if (!match)
11578 				return -ENOBUFS;
11579 
11580 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
11581 				    req->match_sets[i].ssid.ssid_len,
11582 				    req->match_sets[i].ssid.ssid))
11583 				return -ENOBUFS;
11584 			nla_nest_end(msg, match);
11585 		}
11586 		nla_nest_end(msg, matches);
11587 	}
11588 
11589 	scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
11590 	if (!scan_plans)
11591 		return -ENOBUFS;
11592 
11593 	for (i = 0; i < req->n_scan_plans; i++) {
11594 		scan_plan = nla_nest_start_noflag(msg, i + 1);
11595 		if (!scan_plan)
11596 			return -ENOBUFS;
11597 
11598 		if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
11599 				req->scan_plans[i].interval) ||
11600 		    (req->scan_plans[i].iterations &&
11601 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
11602 				 req->scan_plans[i].iterations)))
11603 			return -ENOBUFS;
11604 		nla_nest_end(msg, scan_plan);
11605 	}
11606 	nla_nest_end(msg, scan_plans);
11607 
11608 	nla_nest_end(msg, nd);
11609 
11610 	return 0;
11611 }
11612 
11613 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
11614 {
11615 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11616 	struct sk_buff *msg;
11617 	void *hdr;
11618 	u32 size = NLMSG_DEFAULT_SIZE;
11619 
11620 	if (!rdev->wiphy.wowlan)
11621 		return -EOPNOTSUPP;
11622 
11623 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
11624 		/* adjust size to have room for all the data */
11625 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
11626 			rdev->wiphy.wowlan_config->tcp->payload_len +
11627 			rdev->wiphy.wowlan_config->tcp->wake_len +
11628 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
11629 	}
11630 
11631 	msg = nlmsg_new(size, GFP_KERNEL);
11632 	if (!msg)
11633 		return -ENOMEM;
11634 
11635 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11636 			     NL80211_CMD_GET_WOWLAN);
11637 	if (!hdr)
11638 		goto nla_put_failure;
11639 
11640 	if (rdev->wiphy.wowlan_config) {
11641 		struct nlattr *nl_wowlan;
11642 
11643 		nl_wowlan = nla_nest_start_noflag(msg,
11644 						  NL80211_ATTR_WOWLAN_TRIGGERS);
11645 		if (!nl_wowlan)
11646 			goto nla_put_failure;
11647 
11648 		if ((rdev->wiphy.wowlan_config->any &&
11649 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
11650 		    (rdev->wiphy.wowlan_config->disconnect &&
11651 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
11652 		    (rdev->wiphy.wowlan_config->magic_pkt &&
11653 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
11654 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
11655 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
11656 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
11657 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
11658 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
11659 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
11660 		    (rdev->wiphy.wowlan_config->rfkill_release &&
11661 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
11662 			goto nla_put_failure;
11663 
11664 		if (nl80211_send_wowlan_patterns(msg, rdev))
11665 			goto nla_put_failure;
11666 
11667 		if (nl80211_send_wowlan_tcp(msg,
11668 					    rdev->wiphy.wowlan_config->tcp))
11669 			goto nla_put_failure;
11670 
11671 		if (nl80211_send_wowlan_nd(
11672 			    msg,
11673 			    rdev->wiphy.wowlan_config->nd_config))
11674 			goto nla_put_failure;
11675 
11676 		nla_nest_end(msg, nl_wowlan);
11677 	}
11678 
11679 	genlmsg_end(msg, hdr);
11680 	return genlmsg_reply(msg, info);
11681 
11682 nla_put_failure:
11683 	nlmsg_free(msg);
11684 	return -ENOBUFS;
11685 }
11686 
11687 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
11688 				    struct nlattr *attr,
11689 				    struct cfg80211_wowlan *trig)
11690 {
11691 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
11692 	struct cfg80211_wowlan_tcp *cfg;
11693 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
11694 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
11695 	u32 size;
11696 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
11697 	int err, port;
11698 
11699 	if (!rdev->wiphy.wowlan->tcp)
11700 		return -EINVAL;
11701 
11702 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
11703 					  nl80211_wowlan_tcp_policy, NULL);
11704 	if (err)
11705 		return err;
11706 
11707 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
11708 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
11709 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
11710 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
11711 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
11712 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
11713 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
11714 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
11715 		return -EINVAL;
11716 
11717 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
11718 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
11719 		return -EINVAL;
11720 
11721 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
11722 			rdev->wiphy.wowlan->tcp->data_interval_max ||
11723 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
11724 		return -EINVAL;
11725 
11726 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
11727 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
11728 		return -EINVAL;
11729 
11730 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
11731 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
11732 		return -EINVAL;
11733 
11734 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
11735 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
11736 
11737 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
11738 		tokens_size = tokln - sizeof(*tok);
11739 
11740 		if (!tok->len || tokens_size % tok->len)
11741 			return -EINVAL;
11742 		if (!rdev->wiphy.wowlan->tcp->tok)
11743 			return -EINVAL;
11744 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
11745 			return -EINVAL;
11746 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
11747 			return -EINVAL;
11748 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
11749 			return -EINVAL;
11750 		if (tok->offset + tok->len > data_size)
11751 			return -EINVAL;
11752 	}
11753 
11754 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
11755 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
11756 		if (!rdev->wiphy.wowlan->tcp->seq)
11757 			return -EINVAL;
11758 		if (seq->len == 0 || seq->len > 4)
11759 			return -EINVAL;
11760 		if (seq->len + seq->offset > data_size)
11761 			return -EINVAL;
11762 	}
11763 
11764 	size = sizeof(*cfg);
11765 	size += data_size;
11766 	size += wake_size + wake_mask_size;
11767 	size += tokens_size;
11768 
11769 	cfg = kzalloc(size, GFP_KERNEL);
11770 	if (!cfg)
11771 		return -ENOMEM;
11772 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
11773 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
11774 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
11775 	       ETH_ALEN);
11776 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
11777 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
11778 	else
11779 		port = 0;
11780 #ifdef CONFIG_INET
11781 	/* allocate a socket and port for it and use it */
11782 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
11783 			    IPPROTO_TCP, &cfg->sock, 1);
11784 	if (err) {
11785 		kfree(cfg);
11786 		return err;
11787 	}
11788 	if (inet_csk_get_port(cfg->sock->sk, port)) {
11789 		sock_release(cfg->sock);
11790 		kfree(cfg);
11791 		return -EADDRINUSE;
11792 	}
11793 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
11794 #else
11795 	if (!port) {
11796 		kfree(cfg);
11797 		return -EINVAL;
11798 	}
11799 	cfg->src_port = port;
11800 #endif
11801 
11802 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
11803 	cfg->payload_len = data_size;
11804 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
11805 	memcpy((void *)cfg->payload,
11806 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
11807 	       data_size);
11808 	if (seq)
11809 		cfg->payload_seq = *seq;
11810 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
11811 	cfg->wake_len = wake_size;
11812 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
11813 	memcpy((void *)cfg->wake_data,
11814 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
11815 	       wake_size);
11816 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
11817 			 data_size + wake_size;
11818 	memcpy((void *)cfg->wake_mask,
11819 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
11820 	       wake_mask_size);
11821 	if (tok) {
11822 		cfg->tokens_size = tokens_size;
11823 		memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
11824 	}
11825 
11826 	trig->tcp = cfg;
11827 
11828 	return 0;
11829 }
11830 
11831 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
11832 				   const struct wiphy_wowlan_support *wowlan,
11833 				   struct nlattr *attr,
11834 				   struct cfg80211_wowlan *trig)
11835 {
11836 	struct nlattr **tb;
11837 	int err;
11838 
11839 	tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
11840 	if (!tb)
11841 		return -ENOMEM;
11842 
11843 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
11844 		err = -EOPNOTSUPP;
11845 		goto out;
11846 	}
11847 
11848 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
11849 					  nl80211_policy, NULL);
11850 	if (err)
11851 		goto out;
11852 
11853 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
11854 						   wowlan->max_nd_match_sets);
11855 	err = PTR_ERR_OR_ZERO(trig->nd_config);
11856 	if (err)
11857 		trig->nd_config = NULL;
11858 
11859 out:
11860 	kfree(tb);
11861 	return err;
11862 }
11863 
11864 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
11865 {
11866 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11867 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
11868 	struct cfg80211_wowlan new_triggers = {};
11869 	struct cfg80211_wowlan *ntrig;
11870 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
11871 	int err, i;
11872 	bool prev_enabled = rdev->wiphy.wowlan_config;
11873 	bool regular = false;
11874 
11875 	if (!wowlan)
11876 		return -EOPNOTSUPP;
11877 
11878 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
11879 		cfg80211_rdev_free_wowlan(rdev);
11880 		rdev->wiphy.wowlan_config = NULL;
11881 		goto set_wakeup;
11882 	}
11883 
11884 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
11885 					  info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
11886 					  nl80211_wowlan_policy, info->extack);
11887 	if (err)
11888 		return err;
11889 
11890 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
11891 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
11892 			return -EINVAL;
11893 		new_triggers.any = true;
11894 	}
11895 
11896 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
11897 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
11898 			return -EINVAL;
11899 		new_triggers.disconnect = true;
11900 		regular = true;
11901 	}
11902 
11903 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
11904 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
11905 			return -EINVAL;
11906 		new_triggers.magic_pkt = true;
11907 		regular = true;
11908 	}
11909 
11910 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
11911 		return -EINVAL;
11912 
11913 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
11914 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
11915 			return -EINVAL;
11916 		new_triggers.gtk_rekey_failure = true;
11917 		regular = true;
11918 	}
11919 
11920 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
11921 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
11922 			return -EINVAL;
11923 		new_triggers.eap_identity_req = true;
11924 		regular = true;
11925 	}
11926 
11927 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
11928 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
11929 			return -EINVAL;
11930 		new_triggers.four_way_handshake = true;
11931 		regular = true;
11932 	}
11933 
11934 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
11935 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
11936 			return -EINVAL;
11937 		new_triggers.rfkill_release = true;
11938 		regular = true;
11939 	}
11940 
11941 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
11942 		struct nlattr *pat;
11943 		int n_patterns = 0;
11944 		int rem, pat_len, mask_len, pkt_offset;
11945 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
11946 
11947 		regular = true;
11948 
11949 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
11950 				    rem)
11951 			n_patterns++;
11952 		if (n_patterns > wowlan->n_patterns)
11953 			return -EINVAL;
11954 
11955 		new_triggers.patterns = kcalloc(n_patterns,
11956 						sizeof(new_triggers.patterns[0]),
11957 						GFP_KERNEL);
11958 		if (!new_triggers.patterns)
11959 			return -ENOMEM;
11960 
11961 		new_triggers.n_patterns = n_patterns;
11962 		i = 0;
11963 
11964 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
11965 				    rem) {
11966 			u8 *mask_pat;
11967 
11968 			err = nla_parse_nested_deprecated(pat_tb,
11969 							  MAX_NL80211_PKTPAT,
11970 							  pat,
11971 							  nl80211_packet_pattern_policy,
11972 							  info->extack);
11973 			if (err)
11974 				goto error;
11975 
11976 			err = -EINVAL;
11977 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
11978 			    !pat_tb[NL80211_PKTPAT_PATTERN])
11979 				goto error;
11980 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
11981 			mask_len = DIV_ROUND_UP(pat_len, 8);
11982 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
11983 				goto error;
11984 			if (pat_len > wowlan->pattern_max_len ||
11985 			    pat_len < wowlan->pattern_min_len)
11986 				goto error;
11987 
11988 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
11989 				pkt_offset = 0;
11990 			else
11991 				pkt_offset = nla_get_u32(
11992 					pat_tb[NL80211_PKTPAT_OFFSET]);
11993 			if (pkt_offset > wowlan->max_pkt_offset)
11994 				goto error;
11995 			new_triggers.patterns[i].pkt_offset = pkt_offset;
11996 
11997 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
11998 			if (!mask_pat) {
11999 				err = -ENOMEM;
12000 				goto error;
12001 			}
12002 			new_triggers.patterns[i].mask = mask_pat;
12003 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
12004 			       mask_len);
12005 			mask_pat += mask_len;
12006 			new_triggers.patterns[i].pattern = mask_pat;
12007 			new_triggers.patterns[i].pattern_len = pat_len;
12008 			memcpy(mask_pat,
12009 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
12010 			       pat_len);
12011 			i++;
12012 		}
12013 	}
12014 
12015 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
12016 		regular = true;
12017 		err = nl80211_parse_wowlan_tcp(
12018 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
12019 			&new_triggers);
12020 		if (err)
12021 			goto error;
12022 	}
12023 
12024 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
12025 		regular = true;
12026 		err = nl80211_parse_wowlan_nd(
12027 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
12028 			&new_triggers);
12029 		if (err)
12030 			goto error;
12031 	}
12032 
12033 	/* The 'any' trigger means the device continues operating more or less
12034 	 * as in its normal operation mode and wakes up the host on most of the
12035 	 * normal interrupts (like packet RX, ...)
12036 	 * It therefore makes little sense to combine with the more constrained
12037 	 * wakeup trigger modes.
12038 	 */
12039 	if (new_triggers.any && regular) {
12040 		err = -EINVAL;
12041 		goto error;
12042 	}
12043 
12044 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
12045 	if (!ntrig) {
12046 		err = -ENOMEM;
12047 		goto error;
12048 	}
12049 	cfg80211_rdev_free_wowlan(rdev);
12050 	rdev->wiphy.wowlan_config = ntrig;
12051 
12052  set_wakeup:
12053 	if (rdev->ops->set_wakeup &&
12054 	    prev_enabled != !!rdev->wiphy.wowlan_config)
12055 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
12056 
12057 	return 0;
12058  error:
12059 	for (i = 0; i < new_triggers.n_patterns; i++)
12060 		kfree(new_triggers.patterns[i].mask);
12061 	kfree(new_triggers.patterns);
12062 	if (new_triggers.tcp && new_triggers.tcp->sock)
12063 		sock_release(new_triggers.tcp->sock);
12064 	kfree(new_triggers.tcp);
12065 	kfree(new_triggers.nd_config);
12066 	return err;
12067 }
12068 #endif
12069 
12070 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
12071 				       struct cfg80211_registered_device *rdev)
12072 {
12073 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
12074 	int i, j, pat_len;
12075 	struct cfg80211_coalesce_rules *rule;
12076 
12077 	if (!rdev->coalesce->n_rules)
12078 		return 0;
12079 
12080 	nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
12081 	if (!nl_rules)
12082 		return -ENOBUFS;
12083 
12084 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
12085 		nl_rule = nla_nest_start_noflag(msg, i + 1);
12086 		if (!nl_rule)
12087 			return -ENOBUFS;
12088 
12089 		rule = &rdev->coalesce->rules[i];
12090 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
12091 				rule->delay))
12092 			return -ENOBUFS;
12093 
12094 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
12095 				rule->condition))
12096 			return -ENOBUFS;
12097 
12098 		nl_pats = nla_nest_start_noflag(msg,
12099 						NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
12100 		if (!nl_pats)
12101 			return -ENOBUFS;
12102 
12103 		for (j = 0; j < rule->n_patterns; j++) {
12104 			nl_pat = nla_nest_start_noflag(msg, j + 1);
12105 			if (!nl_pat)
12106 				return -ENOBUFS;
12107 			pat_len = rule->patterns[j].pattern_len;
12108 			if (nla_put(msg, NL80211_PKTPAT_MASK,
12109 				    DIV_ROUND_UP(pat_len, 8),
12110 				    rule->patterns[j].mask) ||
12111 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
12112 				    rule->patterns[j].pattern) ||
12113 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
12114 					rule->patterns[j].pkt_offset))
12115 				return -ENOBUFS;
12116 			nla_nest_end(msg, nl_pat);
12117 		}
12118 		nla_nest_end(msg, nl_pats);
12119 		nla_nest_end(msg, nl_rule);
12120 	}
12121 	nla_nest_end(msg, nl_rules);
12122 
12123 	return 0;
12124 }
12125 
12126 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
12127 {
12128 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12129 	struct sk_buff *msg;
12130 	void *hdr;
12131 
12132 	if (!rdev->wiphy.coalesce)
12133 		return -EOPNOTSUPP;
12134 
12135 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12136 	if (!msg)
12137 		return -ENOMEM;
12138 
12139 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12140 			     NL80211_CMD_GET_COALESCE);
12141 	if (!hdr)
12142 		goto nla_put_failure;
12143 
12144 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
12145 		goto nla_put_failure;
12146 
12147 	genlmsg_end(msg, hdr);
12148 	return genlmsg_reply(msg, info);
12149 
12150 nla_put_failure:
12151 	nlmsg_free(msg);
12152 	return -ENOBUFS;
12153 }
12154 
12155 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
12156 {
12157 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
12158 	int i, j;
12159 	struct cfg80211_coalesce_rules *rule;
12160 
12161 	if (!coalesce)
12162 		return;
12163 
12164 	for (i = 0; i < coalesce->n_rules; i++) {
12165 		rule = &coalesce->rules[i];
12166 		for (j = 0; j < rule->n_patterns; j++)
12167 			kfree(rule->patterns[j].mask);
12168 		kfree(rule->patterns);
12169 	}
12170 	kfree(coalesce->rules);
12171 	kfree(coalesce);
12172 	rdev->coalesce = NULL;
12173 }
12174 
12175 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
12176 				       struct nlattr *rule,
12177 				       struct cfg80211_coalesce_rules *new_rule)
12178 {
12179 	int err, i;
12180 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
12181 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
12182 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
12183 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
12184 
12185 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
12186 					  rule, nl80211_coalesce_policy, NULL);
12187 	if (err)
12188 		return err;
12189 
12190 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
12191 		new_rule->delay =
12192 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
12193 	if (new_rule->delay > coalesce->max_delay)
12194 		return -EINVAL;
12195 
12196 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
12197 		new_rule->condition =
12198 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
12199 
12200 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
12201 		return -EINVAL;
12202 
12203 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
12204 			    rem)
12205 		n_patterns++;
12206 	if (n_patterns > coalesce->n_patterns)
12207 		return -EINVAL;
12208 
12209 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
12210 				     GFP_KERNEL);
12211 	if (!new_rule->patterns)
12212 		return -ENOMEM;
12213 
12214 	new_rule->n_patterns = n_patterns;
12215 	i = 0;
12216 
12217 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
12218 			    rem) {
12219 		u8 *mask_pat;
12220 
12221 		err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
12222 						  pat,
12223 						  nl80211_packet_pattern_policy,
12224 						  NULL);
12225 		if (err)
12226 			return err;
12227 
12228 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
12229 		    !pat_tb[NL80211_PKTPAT_PATTERN])
12230 			return -EINVAL;
12231 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
12232 		mask_len = DIV_ROUND_UP(pat_len, 8);
12233 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
12234 			return -EINVAL;
12235 		if (pat_len > coalesce->pattern_max_len ||
12236 		    pat_len < coalesce->pattern_min_len)
12237 			return -EINVAL;
12238 
12239 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
12240 			pkt_offset = 0;
12241 		else
12242 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
12243 		if (pkt_offset > coalesce->max_pkt_offset)
12244 			return -EINVAL;
12245 		new_rule->patterns[i].pkt_offset = pkt_offset;
12246 
12247 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
12248 		if (!mask_pat)
12249 			return -ENOMEM;
12250 
12251 		new_rule->patterns[i].mask = mask_pat;
12252 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
12253 		       mask_len);
12254 
12255 		mask_pat += mask_len;
12256 		new_rule->patterns[i].pattern = mask_pat;
12257 		new_rule->patterns[i].pattern_len = pat_len;
12258 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
12259 		       pat_len);
12260 		i++;
12261 	}
12262 
12263 	return 0;
12264 }
12265 
12266 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
12267 {
12268 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12269 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
12270 	struct cfg80211_coalesce new_coalesce = {};
12271 	struct cfg80211_coalesce *n_coalesce;
12272 	int err, rem_rule, n_rules = 0, i, j;
12273 	struct nlattr *rule;
12274 	struct cfg80211_coalesce_rules *tmp_rule;
12275 
12276 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
12277 		return -EOPNOTSUPP;
12278 
12279 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
12280 		cfg80211_rdev_free_coalesce(rdev);
12281 		rdev_set_coalesce(rdev, NULL);
12282 		return 0;
12283 	}
12284 
12285 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
12286 			    rem_rule)
12287 		n_rules++;
12288 	if (n_rules > coalesce->n_rules)
12289 		return -EINVAL;
12290 
12291 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
12292 				     GFP_KERNEL);
12293 	if (!new_coalesce.rules)
12294 		return -ENOMEM;
12295 
12296 	new_coalesce.n_rules = n_rules;
12297 	i = 0;
12298 
12299 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
12300 			    rem_rule) {
12301 		err = nl80211_parse_coalesce_rule(rdev, rule,
12302 						  &new_coalesce.rules[i]);
12303 		if (err)
12304 			goto error;
12305 
12306 		i++;
12307 	}
12308 
12309 	err = rdev_set_coalesce(rdev, &new_coalesce);
12310 	if (err)
12311 		goto error;
12312 
12313 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
12314 	if (!n_coalesce) {
12315 		err = -ENOMEM;
12316 		goto error;
12317 	}
12318 	cfg80211_rdev_free_coalesce(rdev);
12319 	rdev->coalesce = n_coalesce;
12320 
12321 	return 0;
12322 error:
12323 	for (i = 0; i < new_coalesce.n_rules; i++) {
12324 		tmp_rule = &new_coalesce.rules[i];
12325 		for (j = 0; j < tmp_rule->n_patterns; j++)
12326 			kfree(tmp_rule->patterns[j].mask);
12327 		kfree(tmp_rule->patterns);
12328 	}
12329 	kfree(new_coalesce.rules);
12330 
12331 	return err;
12332 }
12333 
12334 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
12335 {
12336 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12337 	struct net_device *dev = info->user_ptr[1];
12338 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12339 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
12340 	struct cfg80211_gtk_rekey_data rekey_data;
12341 	int err;
12342 
12343 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
12344 		return -EINVAL;
12345 
12346 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
12347 					  info->attrs[NL80211_ATTR_REKEY_DATA],
12348 					  nl80211_rekey_policy, info->extack);
12349 	if (err)
12350 		return err;
12351 
12352 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
12353 	    !tb[NL80211_REKEY_DATA_KCK])
12354 		return -EINVAL;
12355 	if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
12356 		return -ERANGE;
12357 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN &&
12358 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
12359 	      nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KEK_EXT_LEN))
12360 		return -ERANGE;
12361 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN &&
12362 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
12363 	      nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KCK_EXT_LEN))
12364 		return -ERANGE;
12365 
12366 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
12367 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
12368 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
12369 	rekey_data.kek_len = nla_len(tb[NL80211_REKEY_DATA_KEK]);
12370 	rekey_data.kck_len = nla_len(tb[NL80211_REKEY_DATA_KCK]);
12371 	if (tb[NL80211_REKEY_DATA_AKM])
12372 		rekey_data.akm = nla_get_u32(tb[NL80211_REKEY_DATA_AKM]);
12373 
12374 	wdev_lock(wdev);
12375 	if (!wdev->current_bss) {
12376 		err = -ENOTCONN;
12377 		goto out;
12378 	}
12379 
12380 	if (!rdev->ops->set_rekey_data) {
12381 		err = -EOPNOTSUPP;
12382 		goto out;
12383 	}
12384 
12385 	err = rdev_set_rekey_data(rdev, dev, &rekey_data);
12386  out:
12387 	wdev_unlock(wdev);
12388 	return err;
12389 }
12390 
12391 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
12392 					     struct genl_info *info)
12393 {
12394 	struct net_device *dev = info->user_ptr[1];
12395 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12396 
12397 	if (wdev->iftype != NL80211_IFTYPE_AP &&
12398 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
12399 		return -EINVAL;
12400 
12401 	if (wdev->ap_unexpected_nlportid)
12402 		return -EBUSY;
12403 
12404 	wdev->ap_unexpected_nlportid = info->snd_portid;
12405 	return 0;
12406 }
12407 
12408 static int nl80211_probe_client(struct sk_buff *skb,
12409 				struct genl_info *info)
12410 {
12411 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12412 	struct net_device *dev = info->user_ptr[1];
12413 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12414 	struct sk_buff *msg;
12415 	void *hdr;
12416 	const u8 *addr;
12417 	u64 cookie;
12418 	int err;
12419 
12420 	if (wdev->iftype != NL80211_IFTYPE_AP &&
12421 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
12422 		return -EOPNOTSUPP;
12423 
12424 	if (!info->attrs[NL80211_ATTR_MAC])
12425 		return -EINVAL;
12426 
12427 	if (!rdev->ops->probe_client)
12428 		return -EOPNOTSUPP;
12429 
12430 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12431 	if (!msg)
12432 		return -ENOMEM;
12433 
12434 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12435 			     NL80211_CMD_PROBE_CLIENT);
12436 	if (!hdr) {
12437 		err = -ENOBUFS;
12438 		goto free_msg;
12439 	}
12440 
12441 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12442 
12443 	err = rdev_probe_client(rdev, dev, addr, &cookie);
12444 	if (err)
12445 		goto free_msg;
12446 
12447 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12448 			      NL80211_ATTR_PAD))
12449 		goto nla_put_failure;
12450 
12451 	genlmsg_end(msg, hdr);
12452 
12453 	return genlmsg_reply(msg, info);
12454 
12455  nla_put_failure:
12456 	err = -ENOBUFS;
12457  free_msg:
12458 	nlmsg_free(msg);
12459 	return err;
12460 }
12461 
12462 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
12463 {
12464 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12465 	struct cfg80211_beacon_registration *reg, *nreg;
12466 	int rv;
12467 
12468 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
12469 		return -EOPNOTSUPP;
12470 
12471 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
12472 	if (!nreg)
12473 		return -ENOMEM;
12474 
12475 	/* First, check if already registered. */
12476 	spin_lock_bh(&rdev->beacon_registrations_lock);
12477 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
12478 		if (reg->nlportid == info->snd_portid) {
12479 			rv = -EALREADY;
12480 			goto out_err;
12481 		}
12482 	}
12483 	/* Add it to the list */
12484 	nreg->nlportid = info->snd_portid;
12485 	list_add(&nreg->list, &rdev->beacon_registrations);
12486 
12487 	spin_unlock_bh(&rdev->beacon_registrations_lock);
12488 
12489 	return 0;
12490 out_err:
12491 	spin_unlock_bh(&rdev->beacon_registrations_lock);
12492 	kfree(nreg);
12493 	return rv;
12494 }
12495 
12496 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
12497 {
12498 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12499 	struct wireless_dev *wdev = info->user_ptr[1];
12500 	int err;
12501 
12502 	if (!rdev->ops->start_p2p_device)
12503 		return -EOPNOTSUPP;
12504 
12505 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
12506 		return -EOPNOTSUPP;
12507 
12508 	if (wdev_running(wdev))
12509 		return 0;
12510 
12511 	if (rfkill_blocked(rdev->rfkill))
12512 		return -ERFKILL;
12513 
12514 	err = rdev_start_p2p_device(rdev, wdev);
12515 	if (err)
12516 		return err;
12517 
12518 	wdev->is_running = true;
12519 	rdev->opencount++;
12520 
12521 	return 0;
12522 }
12523 
12524 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
12525 {
12526 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12527 	struct wireless_dev *wdev = info->user_ptr[1];
12528 
12529 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
12530 		return -EOPNOTSUPP;
12531 
12532 	if (!rdev->ops->stop_p2p_device)
12533 		return -EOPNOTSUPP;
12534 
12535 	cfg80211_stop_p2p_device(rdev, wdev);
12536 
12537 	return 0;
12538 }
12539 
12540 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
12541 {
12542 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12543 	struct wireless_dev *wdev = info->user_ptr[1];
12544 	struct cfg80211_nan_conf conf = {};
12545 	int err;
12546 
12547 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12548 		return -EOPNOTSUPP;
12549 
12550 	if (wdev_running(wdev))
12551 		return -EEXIST;
12552 
12553 	if (rfkill_blocked(rdev->rfkill))
12554 		return -ERFKILL;
12555 
12556 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
12557 		return -EINVAL;
12558 
12559 	conf.master_pref =
12560 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
12561 
12562 	if (info->attrs[NL80211_ATTR_BANDS]) {
12563 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
12564 
12565 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
12566 			return -EOPNOTSUPP;
12567 
12568 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
12569 			return -EINVAL;
12570 
12571 		conf.bands = bands;
12572 	}
12573 
12574 	err = rdev_start_nan(rdev, wdev, &conf);
12575 	if (err)
12576 		return err;
12577 
12578 	wdev->is_running = true;
12579 	rdev->opencount++;
12580 
12581 	return 0;
12582 }
12583 
12584 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
12585 {
12586 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12587 	struct wireless_dev *wdev = info->user_ptr[1];
12588 
12589 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12590 		return -EOPNOTSUPP;
12591 
12592 	cfg80211_stop_nan(rdev, wdev);
12593 
12594 	return 0;
12595 }
12596 
12597 static int validate_nan_filter(struct nlattr *filter_attr)
12598 {
12599 	struct nlattr *attr;
12600 	int len = 0, n_entries = 0, rem;
12601 
12602 	nla_for_each_nested(attr, filter_attr, rem) {
12603 		len += nla_len(attr);
12604 		n_entries++;
12605 	}
12606 
12607 	if (len >= U8_MAX)
12608 		return -EINVAL;
12609 
12610 	return n_entries;
12611 }
12612 
12613 static int handle_nan_filter(struct nlattr *attr_filter,
12614 			     struct cfg80211_nan_func *func,
12615 			     bool tx)
12616 {
12617 	struct nlattr *attr;
12618 	int n_entries, rem, i;
12619 	struct cfg80211_nan_func_filter *filter;
12620 
12621 	n_entries = validate_nan_filter(attr_filter);
12622 	if (n_entries < 0)
12623 		return n_entries;
12624 
12625 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
12626 
12627 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
12628 	if (!filter)
12629 		return -ENOMEM;
12630 
12631 	i = 0;
12632 	nla_for_each_nested(attr, attr_filter, rem) {
12633 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
12634 		filter[i].len = nla_len(attr);
12635 		i++;
12636 	}
12637 	if (tx) {
12638 		func->num_tx_filters = n_entries;
12639 		func->tx_filters = filter;
12640 	} else {
12641 		func->num_rx_filters = n_entries;
12642 		func->rx_filters = filter;
12643 	}
12644 
12645 	return 0;
12646 }
12647 
12648 static int nl80211_nan_add_func(struct sk_buff *skb,
12649 				struct genl_info *info)
12650 {
12651 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12652 	struct wireless_dev *wdev = info->user_ptr[1];
12653 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
12654 	struct cfg80211_nan_func *func;
12655 	struct sk_buff *msg = NULL;
12656 	void *hdr = NULL;
12657 	int err = 0;
12658 
12659 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12660 		return -EOPNOTSUPP;
12661 
12662 	if (!wdev_running(wdev))
12663 		return -ENOTCONN;
12664 
12665 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
12666 		return -EINVAL;
12667 
12668 	err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
12669 					  info->attrs[NL80211_ATTR_NAN_FUNC],
12670 					  nl80211_nan_func_policy,
12671 					  info->extack);
12672 	if (err)
12673 		return err;
12674 
12675 	func = kzalloc(sizeof(*func), GFP_KERNEL);
12676 	if (!func)
12677 		return -ENOMEM;
12678 
12679 	func->cookie = cfg80211_assign_cookie(rdev);
12680 
12681 	if (!tb[NL80211_NAN_FUNC_TYPE] ||
12682 	    nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]) > NL80211_NAN_FUNC_MAX_TYPE) {
12683 		err = -EINVAL;
12684 		goto out;
12685 	}
12686 
12687 
12688 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
12689 
12690 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
12691 		err = -EINVAL;
12692 		goto out;
12693 	}
12694 
12695 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
12696 	       sizeof(func->service_id));
12697 
12698 	func->close_range =
12699 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
12700 
12701 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
12702 		func->serv_spec_info_len =
12703 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
12704 		func->serv_spec_info =
12705 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
12706 				func->serv_spec_info_len,
12707 				GFP_KERNEL);
12708 		if (!func->serv_spec_info) {
12709 			err = -ENOMEM;
12710 			goto out;
12711 		}
12712 	}
12713 
12714 	if (tb[NL80211_NAN_FUNC_TTL])
12715 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
12716 
12717 	switch (func->type) {
12718 	case NL80211_NAN_FUNC_PUBLISH:
12719 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
12720 			err = -EINVAL;
12721 			goto out;
12722 		}
12723 
12724 		func->publish_type =
12725 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
12726 		func->publish_bcast =
12727 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
12728 
12729 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
12730 			func->publish_bcast) {
12731 			err = -EINVAL;
12732 			goto out;
12733 		}
12734 		break;
12735 	case NL80211_NAN_FUNC_SUBSCRIBE:
12736 		func->subscribe_active =
12737 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
12738 		break;
12739 	case NL80211_NAN_FUNC_FOLLOW_UP:
12740 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
12741 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
12742 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
12743 			err = -EINVAL;
12744 			goto out;
12745 		}
12746 
12747 		func->followup_id =
12748 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
12749 		func->followup_reqid =
12750 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
12751 		memcpy(func->followup_dest.addr,
12752 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
12753 		       sizeof(func->followup_dest.addr));
12754 		if (func->ttl) {
12755 			err = -EINVAL;
12756 			goto out;
12757 		}
12758 		break;
12759 	default:
12760 		err = -EINVAL;
12761 		goto out;
12762 	}
12763 
12764 	if (tb[NL80211_NAN_FUNC_SRF]) {
12765 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
12766 
12767 		err = nla_parse_nested_deprecated(srf_tb,
12768 						  NL80211_NAN_SRF_ATTR_MAX,
12769 						  tb[NL80211_NAN_FUNC_SRF],
12770 						  nl80211_nan_srf_policy,
12771 						  info->extack);
12772 		if (err)
12773 			goto out;
12774 
12775 		func->srf_include =
12776 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
12777 
12778 		if (srf_tb[NL80211_NAN_SRF_BF]) {
12779 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
12780 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
12781 				err = -EINVAL;
12782 				goto out;
12783 			}
12784 
12785 			func->srf_bf_len =
12786 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
12787 			func->srf_bf =
12788 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
12789 					func->srf_bf_len, GFP_KERNEL);
12790 			if (!func->srf_bf) {
12791 				err = -ENOMEM;
12792 				goto out;
12793 			}
12794 
12795 			func->srf_bf_idx =
12796 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
12797 		} else {
12798 			struct nlattr *attr, *mac_attr =
12799 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
12800 			int n_entries, rem, i = 0;
12801 
12802 			if (!mac_attr) {
12803 				err = -EINVAL;
12804 				goto out;
12805 			}
12806 
12807 			n_entries = validate_acl_mac_addrs(mac_attr);
12808 			if (n_entries <= 0) {
12809 				err = -EINVAL;
12810 				goto out;
12811 			}
12812 
12813 			func->srf_num_macs = n_entries;
12814 			func->srf_macs =
12815 				kcalloc(n_entries, sizeof(*func->srf_macs),
12816 					GFP_KERNEL);
12817 			if (!func->srf_macs) {
12818 				err = -ENOMEM;
12819 				goto out;
12820 			}
12821 
12822 			nla_for_each_nested(attr, mac_attr, rem)
12823 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
12824 				       sizeof(*func->srf_macs));
12825 		}
12826 	}
12827 
12828 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
12829 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
12830 					func, true);
12831 		if (err)
12832 			goto out;
12833 	}
12834 
12835 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
12836 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
12837 					func, false);
12838 		if (err)
12839 			goto out;
12840 	}
12841 
12842 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12843 	if (!msg) {
12844 		err = -ENOMEM;
12845 		goto out;
12846 	}
12847 
12848 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12849 			     NL80211_CMD_ADD_NAN_FUNCTION);
12850 	/* This can't really happen - we just allocated 4KB */
12851 	if (WARN_ON(!hdr)) {
12852 		err = -ENOMEM;
12853 		goto out;
12854 	}
12855 
12856 	err = rdev_add_nan_func(rdev, wdev, func);
12857 out:
12858 	if (err < 0) {
12859 		cfg80211_free_nan_func(func);
12860 		nlmsg_free(msg);
12861 		return err;
12862 	}
12863 
12864 	/* propagate the instance id and cookie to userspace  */
12865 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
12866 			      NL80211_ATTR_PAD))
12867 		goto nla_put_failure;
12868 
12869 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
12870 	if (!func_attr)
12871 		goto nla_put_failure;
12872 
12873 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
12874 		       func->instance_id))
12875 		goto nla_put_failure;
12876 
12877 	nla_nest_end(msg, func_attr);
12878 
12879 	genlmsg_end(msg, hdr);
12880 	return genlmsg_reply(msg, info);
12881 
12882 nla_put_failure:
12883 	nlmsg_free(msg);
12884 	return -ENOBUFS;
12885 }
12886 
12887 static int nl80211_nan_del_func(struct sk_buff *skb,
12888 			       struct genl_info *info)
12889 {
12890 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12891 	struct wireless_dev *wdev = info->user_ptr[1];
12892 	u64 cookie;
12893 
12894 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12895 		return -EOPNOTSUPP;
12896 
12897 	if (!wdev_running(wdev))
12898 		return -ENOTCONN;
12899 
12900 	if (!info->attrs[NL80211_ATTR_COOKIE])
12901 		return -EINVAL;
12902 
12903 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12904 
12905 	rdev_del_nan_func(rdev, wdev, cookie);
12906 
12907 	return 0;
12908 }
12909 
12910 static int nl80211_nan_change_config(struct sk_buff *skb,
12911 				     struct genl_info *info)
12912 {
12913 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12914 	struct wireless_dev *wdev = info->user_ptr[1];
12915 	struct cfg80211_nan_conf conf = {};
12916 	u32 changed = 0;
12917 
12918 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12919 		return -EOPNOTSUPP;
12920 
12921 	if (!wdev_running(wdev))
12922 		return -ENOTCONN;
12923 
12924 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
12925 		conf.master_pref =
12926 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
12927 		if (conf.master_pref <= 1 || conf.master_pref == 255)
12928 			return -EINVAL;
12929 
12930 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
12931 	}
12932 
12933 	if (info->attrs[NL80211_ATTR_BANDS]) {
12934 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
12935 
12936 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
12937 			return -EOPNOTSUPP;
12938 
12939 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
12940 			return -EINVAL;
12941 
12942 		conf.bands = bands;
12943 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
12944 	}
12945 
12946 	if (!changed)
12947 		return -EINVAL;
12948 
12949 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
12950 }
12951 
12952 void cfg80211_nan_match(struct wireless_dev *wdev,
12953 			struct cfg80211_nan_match_params *match, gfp_t gfp)
12954 {
12955 	struct wiphy *wiphy = wdev->wiphy;
12956 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12957 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
12958 	struct sk_buff *msg;
12959 	void *hdr;
12960 
12961 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
12962 		return;
12963 
12964 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12965 	if (!msg)
12966 		return;
12967 
12968 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
12969 	if (!hdr) {
12970 		nlmsg_free(msg);
12971 		return;
12972 	}
12973 
12974 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12975 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12976 					 wdev->netdev->ifindex)) ||
12977 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
12978 			      NL80211_ATTR_PAD))
12979 		goto nla_put_failure;
12980 
12981 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
12982 			      NL80211_ATTR_PAD) ||
12983 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
12984 		goto nla_put_failure;
12985 
12986 	match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
12987 	if (!match_attr)
12988 		goto nla_put_failure;
12989 
12990 	local_func_attr = nla_nest_start_noflag(msg,
12991 						NL80211_NAN_MATCH_FUNC_LOCAL);
12992 	if (!local_func_attr)
12993 		goto nla_put_failure;
12994 
12995 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
12996 		goto nla_put_failure;
12997 
12998 	nla_nest_end(msg, local_func_attr);
12999 
13000 	peer_func_attr = nla_nest_start_noflag(msg,
13001 					       NL80211_NAN_MATCH_FUNC_PEER);
13002 	if (!peer_func_attr)
13003 		goto nla_put_failure;
13004 
13005 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
13006 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
13007 		goto nla_put_failure;
13008 
13009 	if (match->info && match->info_len &&
13010 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
13011 		    match->info))
13012 		goto nla_put_failure;
13013 
13014 	nla_nest_end(msg, peer_func_attr);
13015 	nla_nest_end(msg, match_attr);
13016 	genlmsg_end(msg, hdr);
13017 
13018 	if (!wdev->owner_nlportid)
13019 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
13020 					msg, 0, NL80211_MCGRP_NAN, gfp);
13021 	else
13022 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
13023 				wdev->owner_nlportid);
13024 
13025 	return;
13026 
13027 nla_put_failure:
13028 	nlmsg_free(msg);
13029 }
13030 EXPORT_SYMBOL(cfg80211_nan_match);
13031 
13032 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
13033 				  u8 inst_id,
13034 				  enum nl80211_nan_func_term_reason reason,
13035 				  u64 cookie, gfp_t gfp)
13036 {
13037 	struct wiphy *wiphy = wdev->wiphy;
13038 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13039 	struct sk_buff *msg;
13040 	struct nlattr *func_attr;
13041 	void *hdr;
13042 
13043 	if (WARN_ON(!inst_id))
13044 		return;
13045 
13046 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
13047 	if (!msg)
13048 		return;
13049 
13050 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
13051 	if (!hdr) {
13052 		nlmsg_free(msg);
13053 		return;
13054 	}
13055 
13056 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13057 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
13058 					 wdev->netdev->ifindex)) ||
13059 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
13060 			      NL80211_ATTR_PAD))
13061 		goto nla_put_failure;
13062 
13063 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
13064 			      NL80211_ATTR_PAD))
13065 		goto nla_put_failure;
13066 
13067 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
13068 	if (!func_attr)
13069 		goto nla_put_failure;
13070 
13071 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
13072 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
13073 		goto nla_put_failure;
13074 
13075 	nla_nest_end(msg, func_attr);
13076 	genlmsg_end(msg, hdr);
13077 
13078 	if (!wdev->owner_nlportid)
13079 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
13080 					msg, 0, NL80211_MCGRP_NAN, gfp);
13081 	else
13082 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
13083 				wdev->owner_nlportid);
13084 
13085 	return;
13086 
13087 nla_put_failure:
13088 	nlmsg_free(msg);
13089 }
13090 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
13091 
13092 static int nl80211_get_protocol_features(struct sk_buff *skb,
13093 					 struct genl_info *info)
13094 {
13095 	void *hdr;
13096 	struct sk_buff *msg;
13097 
13098 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13099 	if (!msg)
13100 		return -ENOMEM;
13101 
13102 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13103 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
13104 	if (!hdr)
13105 		goto nla_put_failure;
13106 
13107 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
13108 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
13109 		goto nla_put_failure;
13110 
13111 	genlmsg_end(msg, hdr);
13112 	return genlmsg_reply(msg, info);
13113 
13114  nla_put_failure:
13115 	kfree_skb(msg);
13116 	return -ENOBUFS;
13117 }
13118 
13119 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
13120 {
13121 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13122 	struct cfg80211_update_ft_ies_params ft_params;
13123 	struct net_device *dev = info->user_ptr[1];
13124 
13125 	if (!rdev->ops->update_ft_ies)
13126 		return -EOPNOTSUPP;
13127 
13128 	if (!info->attrs[NL80211_ATTR_MDID] ||
13129 	    !info->attrs[NL80211_ATTR_IE])
13130 		return -EINVAL;
13131 
13132 	memset(&ft_params, 0, sizeof(ft_params));
13133 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
13134 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
13135 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
13136 
13137 	return rdev_update_ft_ies(rdev, dev, &ft_params);
13138 }
13139 
13140 static int nl80211_crit_protocol_start(struct sk_buff *skb,
13141 				       struct genl_info *info)
13142 {
13143 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13144 	struct wireless_dev *wdev = info->user_ptr[1];
13145 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
13146 	u16 duration;
13147 	int ret;
13148 
13149 	if (!rdev->ops->crit_proto_start)
13150 		return -EOPNOTSUPP;
13151 
13152 	if (WARN_ON(!rdev->ops->crit_proto_stop))
13153 		return -EINVAL;
13154 
13155 	if (rdev->crit_proto_nlportid)
13156 		return -EBUSY;
13157 
13158 	/* determine protocol if provided */
13159 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
13160 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
13161 
13162 	if (proto >= NUM_NL80211_CRIT_PROTO)
13163 		return -EINVAL;
13164 
13165 	/* timeout must be provided */
13166 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
13167 		return -EINVAL;
13168 
13169 	duration =
13170 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
13171 
13172 	if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
13173 		return -ERANGE;
13174 
13175 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
13176 	if (!ret)
13177 		rdev->crit_proto_nlportid = info->snd_portid;
13178 
13179 	return ret;
13180 }
13181 
13182 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
13183 				      struct genl_info *info)
13184 {
13185 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13186 	struct wireless_dev *wdev = info->user_ptr[1];
13187 
13188 	if (!rdev->ops->crit_proto_stop)
13189 		return -EOPNOTSUPP;
13190 
13191 	if (rdev->crit_proto_nlportid) {
13192 		rdev->crit_proto_nlportid = 0;
13193 		rdev_crit_proto_stop(rdev, wdev);
13194 	}
13195 	return 0;
13196 }
13197 
13198 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
13199 				       struct nlattr *attr,
13200 				       struct netlink_ext_ack *extack)
13201 {
13202 	if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
13203 		if (attr->nla_type & NLA_F_NESTED) {
13204 			NL_SET_ERR_MSG_ATTR(extack, attr,
13205 					    "unexpected nested data");
13206 			return -EINVAL;
13207 		}
13208 
13209 		return 0;
13210 	}
13211 
13212 	if (!(attr->nla_type & NLA_F_NESTED)) {
13213 		NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
13214 		return -EINVAL;
13215 	}
13216 
13217 	return nla_validate_nested(attr, vcmd->maxattr, vcmd->policy, extack);
13218 }
13219 
13220 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
13221 {
13222 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13223 	struct wireless_dev *wdev =
13224 		__cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
13225 	int i, err;
13226 	u32 vid, subcmd;
13227 
13228 	if (!rdev->wiphy.vendor_commands)
13229 		return -EOPNOTSUPP;
13230 
13231 	if (IS_ERR(wdev)) {
13232 		err = PTR_ERR(wdev);
13233 		if (err != -EINVAL)
13234 			return err;
13235 		wdev = NULL;
13236 	} else if (wdev->wiphy != &rdev->wiphy) {
13237 		return -EINVAL;
13238 	}
13239 
13240 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
13241 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
13242 		return -EINVAL;
13243 
13244 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
13245 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
13246 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
13247 		const struct wiphy_vendor_command *vcmd;
13248 		void *data = NULL;
13249 		int len = 0;
13250 
13251 		vcmd = &rdev->wiphy.vendor_commands[i];
13252 
13253 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
13254 			continue;
13255 
13256 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
13257 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
13258 			if (!wdev)
13259 				return -EINVAL;
13260 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
13261 			    !wdev->netdev)
13262 				return -EINVAL;
13263 
13264 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
13265 				if (!wdev_running(wdev))
13266 					return -ENETDOWN;
13267 			}
13268 
13269 			if (!vcmd->doit)
13270 				return -EOPNOTSUPP;
13271 		} else {
13272 			wdev = NULL;
13273 		}
13274 
13275 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
13276 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
13277 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
13278 
13279 			err = nl80211_vendor_check_policy(vcmd,
13280 					info->attrs[NL80211_ATTR_VENDOR_DATA],
13281 					info->extack);
13282 			if (err)
13283 				return err;
13284 		}
13285 
13286 		rdev->cur_cmd_info = info;
13287 		err = vcmd->doit(&rdev->wiphy, wdev, data, len);
13288 		rdev->cur_cmd_info = NULL;
13289 		return err;
13290 	}
13291 
13292 	return -EOPNOTSUPP;
13293 }
13294 
13295 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
13296 				       struct netlink_callback *cb,
13297 				       struct cfg80211_registered_device **rdev,
13298 				       struct wireless_dev **wdev)
13299 {
13300 	struct nlattr **attrbuf;
13301 	u32 vid, subcmd;
13302 	unsigned int i;
13303 	int vcmd_idx = -1;
13304 	int err;
13305 	void *data = NULL;
13306 	unsigned int data_len = 0;
13307 
13308 	if (cb->args[0]) {
13309 		/* subtract the 1 again here */
13310 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
13311 		struct wireless_dev *tmp;
13312 
13313 		if (!wiphy)
13314 			return -ENODEV;
13315 		*rdev = wiphy_to_rdev(wiphy);
13316 		*wdev = NULL;
13317 
13318 		if (cb->args[1]) {
13319 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
13320 				if (tmp->identifier == cb->args[1] - 1) {
13321 					*wdev = tmp;
13322 					break;
13323 				}
13324 			}
13325 		}
13326 
13327 		/* keep rtnl locked in successful case */
13328 		return 0;
13329 	}
13330 
13331 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
13332 	if (!attrbuf)
13333 		return -ENOMEM;
13334 
13335 	err = nlmsg_parse_deprecated(cb->nlh,
13336 				     GENL_HDRLEN + nl80211_fam.hdrsize,
13337 				     attrbuf, nl80211_fam.maxattr,
13338 				     nl80211_policy, NULL);
13339 	if (err)
13340 		goto out;
13341 
13342 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
13343 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
13344 		err = -EINVAL;
13345 		goto out;
13346 	}
13347 
13348 	*wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk), attrbuf);
13349 	if (IS_ERR(*wdev))
13350 		*wdev = NULL;
13351 
13352 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
13353 	if (IS_ERR(*rdev)) {
13354 		err = PTR_ERR(*rdev);
13355 		goto out;
13356 	}
13357 
13358 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
13359 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
13360 
13361 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
13362 		const struct wiphy_vendor_command *vcmd;
13363 
13364 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
13365 
13366 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
13367 			continue;
13368 
13369 		if (!vcmd->dumpit) {
13370 			err = -EOPNOTSUPP;
13371 			goto out;
13372 		}
13373 
13374 		vcmd_idx = i;
13375 		break;
13376 	}
13377 
13378 	if (vcmd_idx < 0) {
13379 		err = -EOPNOTSUPP;
13380 		goto out;
13381 	}
13382 
13383 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
13384 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
13385 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
13386 
13387 		err = nl80211_vendor_check_policy(
13388 				&(*rdev)->wiphy.vendor_commands[vcmd_idx],
13389 				attrbuf[NL80211_ATTR_VENDOR_DATA],
13390 				cb->extack);
13391 		if (err)
13392 			goto out;
13393 	}
13394 
13395 	/* 0 is the first index - add 1 to parse only once */
13396 	cb->args[0] = (*rdev)->wiphy_idx + 1;
13397 	/* add 1 to know if it was NULL */
13398 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
13399 	cb->args[2] = vcmd_idx;
13400 	cb->args[3] = (unsigned long)data;
13401 	cb->args[4] = data_len;
13402 
13403 	/* keep rtnl locked in successful case */
13404 	err = 0;
13405 out:
13406 	kfree(attrbuf);
13407 	return err;
13408 }
13409 
13410 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
13411 				   struct netlink_callback *cb)
13412 {
13413 	struct cfg80211_registered_device *rdev;
13414 	struct wireless_dev *wdev;
13415 	unsigned int vcmd_idx;
13416 	const struct wiphy_vendor_command *vcmd;
13417 	void *data;
13418 	int data_len;
13419 	int err;
13420 	struct nlattr *vendor_data;
13421 
13422 	rtnl_lock();
13423 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
13424 	if (err)
13425 		goto out;
13426 
13427 	vcmd_idx = cb->args[2];
13428 	data = (void *)cb->args[3];
13429 	data_len = cb->args[4];
13430 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
13431 
13432 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
13433 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
13434 		if (!wdev) {
13435 			err = -EINVAL;
13436 			goto out;
13437 		}
13438 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
13439 		    !wdev->netdev) {
13440 			err = -EINVAL;
13441 			goto out;
13442 		}
13443 
13444 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
13445 			if (!wdev_running(wdev)) {
13446 				err = -ENETDOWN;
13447 				goto out;
13448 			}
13449 		}
13450 	}
13451 
13452 	while (1) {
13453 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
13454 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
13455 					   NL80211_CMD_VENDOR);
13456 		if (!hdr)
13457 			break;
13458 
13459 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13460 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
13461 					       wdev_id(wdev),
13462 					       NL80211_ATTR_PAD))) {
13463 			genlmsg_cancel(skb, hdr);
13464 			break;
13465 		}
13466 
13467 		vendor_data = nla_nest_start_noflag(skb,
13468 						    NL80211_ATTR_VENDOR_DATA);
13469 		if (!vendor_data) {
13470 			genlmsg_cancel(skb, hdr);
13471 			break;
13472 		}
13473 
13474 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
13475 				   (unsigned long *)&cb->args[5]);
13476 		nla_nest_end(skb, vendor_data);
13477 
13478 		if (err == -ENOBUFS || err == -ENOENT) {
13479 			genlmsg_cancel(skb, hdr);
13480 			break;
13481 		} else if (err) {
13482 			genlmsg_cancel(skb, hdr);
13483 			goto out;
13484 		}
13485 
13486 		genlmsg_end(skb, hdr);
13487 	}
13488 
13489 	err = skb->len;
13490  out:
13491 	rtnl_unlock();
13492 	return err;
13493 }
13494 
13495 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
13496 					   enum nl80211_commands cmd,
13497 					   enum nl80211_attrs attr,
13498 					   int approxlen)
13499 {
13500 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13501 
13502 	if (WARN_ON(!rdev->cur_cmd_info))
13503 		return NULL;
13504 
13505 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
13506 					   rdev->cur_cmd_info->snd_portid,
13507 					   rdev->cur_cmd_info->snd_seq,
13508 					   cmd, attr, NULL, GFP_KERNEL);
13509 }
13510 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
13511 
13512 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
13513 {
13514 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
13515 	void *hdr = ((void **)skb->cb)[1];
13516 	struct nlattr *data = ((void **)skb->cb)[2];
13517 
13518 	/* clear CB data for netlink core to own from now on */
13519 	memset(skb->cb, 0, sizeof(skb->cb));
13520 
13521 	if (WARN_ON(!rdev->cur_cmd_info)) {
13522 		kfree_skb(skb);
13523 		return -EINVAL;
13524 	}
13525 
13526 	nla_nest_end(skb, data);
13527 	genlmsg_end(skb, hdr);
13528 	return genlmsg_reply(skb, rdev->cur_cmd_info);
13529 }
13530 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
13531 
13532 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
13533 {
13534 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13535 
13536 	if (WARN_ON(!rdev->cur_cmd_info))
13537 		return 0;
13538 
13539 	return rdev->cur_cmd_info->snd_portid;
13540 }
13541 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
13542 
13543 static int nl80211_set_qos_map(struct sk_buff *skb,
13544 			       struct genl_info *info)
13545 {
13546 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13547 	struct cfg80211_qos_map *qos_map = NULL;
13548 	struct net_device *dev = info->user_ptr[1];
13549 	u8 *pos, len, num_des, des_len, des;
13550 	int ret;
13551 
13552 	if (!rdev->ops->set_qos_map)
13553 		return -EOPNOTSUPP;
13554 
13555 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
13556 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
13557 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
13558 
13559 		if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
13560 		    len > IEEE80211_QOS_MAP_LEN_MAX)
13561 			return -EINVAL;
13562 
13563 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
13564 		if (!qos_map)
13565 			return -ENOMEM;
13566 
13567 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
13568 		if (num_des) {
13569 			des_len = num_des *
13570 				sizeof(struct cfg80211_dscp_exception);
13571 			memcpy(qos_map->dscp_exception, pos, des_len);
13572 			qos_map->num_des = num_des;
13573 			for (des = 0; des < num_des; des++) {
13574 				if (qos_map->dscp_exception[des].up > 7) {
13575 					kfree(qos_map);
13576 					return -EINVAL;
13577 				}
13578 			}
13579 			pos += des_len;
13580 		}
13581 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
13582 	}
13583 
13584 	wdev_lock(dev->ieee80211_ptr);
13585 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
13586 	if (!ret)
13587 		ret = rdev_set_qos_map(rdev, dev, qos_map);
13588 	wdev_unlock(dev->ieee80211_ptr);
13589 
13590 	kfree(qos_map);
13591 	return ret;
13592 }
13593 
13594 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
13595 {
13596 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13597 	struct net_device *dev = info->user_ptr[1];
13598 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13599 	const u8 *peer;
13600 	u8 tsid, up;
13601 	u16 admitted_time = 0;
13602 	int err;
13603 
13604 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
13605 		return -EOPNOTSUPP;
13606 
13607 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
13608 	    !info->attrs[NL80211_ATTR_USER_PRIO])
13609 		return -EINVAL;
13610 
13611 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
13612 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
13613 
13614 	/* WMM uses TIDs 0-7 even for TSPEC */
13615 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
13616 		/* TODO: handle 802.11 TSPEC/admission control
13617 		 * need more attributes for that (e.g. BA session requirement);
13618 		 * change the WMM adminssion test above to allow both then
13619 		 */
13620 		return -EINVAL;
13621 	}
13622 
13623 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
13624 
13625 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
13626 		admitted_time =
13627 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
13628 		if (!admitted_time)
13629 			return -EINVAL;
13630 	}
13631 
13632 	wdev_lock(wdev);
13633 	switch (wdev->iftype) {
13634 	case NL80211_IFTYPE_STATION:
13635 	case NL80211_IFTYPE_P2P_CLIENT:
13636 		if (wdev->current_bss)
13637 			break;
13638 		err = -ENOTCONN;
13639 		goto out;
13640 	default:
13641 		err = -EOPNOTSUPP;
13642 		goto out;
13643 	}
13644 
13645 	err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
13646 
13647  out:
13648 	wdev_unlock(wdev);
13649 	return err;
13650 }
13651 
13652 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
13653 {
13654 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13655 	struct net_device *dev = info->user_ptr[1];
13656 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13657 	const u8 *peer;
13658 	u8 tsid;
13659 	int err;
13660 
13661 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
13662 		return -EINVAL;
13663 
13664 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
13665 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
13666 
13667 	wdev_lock(wdev);
13668 	err = rdev_del_tx_ts(rdev, dev, tsid, peer);
13669 	wdev_unlock(wdev);
13670 
13671 	return err;
13672 }
13673 
13674 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
13675 				       struct genl_info *info)
13676 {
13677 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13678 	struct net_device *dev = info->user_ptr[1];
13679 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13680 	struct cfg80211_chan_def chandef = {};
13681 	const u8 *addr;
13682 	u8 oper_class;
13683 	int err;
13684 
13685 	if (!rdev->ops->tdls_channel_switch ||
13686 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
13687 		return -EOPNOTSUPP;
13688 
13689 	switch (dev->ieee80211_ptr->iftype) {
13690 	case NL80211_IFTYPE_STATION:
13691 	case NL80211_IFTYPE_P2P_CLIENT:
13692 		break;
13693 	default:
13694 		return -EOPNOTSUPP;
13695 	}
13696 
13697 	if (!info->attrs[NL80211_ATTR_MAC] ||
13698 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
13699 		return -EINVAL;
13700 
13701 	err = nl80211_parse_chandef(rdev, info, &chandef);
13702 	if (err)
13703 		return err;
13704 
13705 	/*
13706 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
13707 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
13708 	 * specification is not defined for them.
13709 	 */
13710 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
13711 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
13712 	    chandef.width != NL80211_CHAN_WIDTH_20)
13713 		return -EINVAL;
13714 
13715 	/* we will be active on the TDLS link */
13716 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
13717 					   wdev->iftype))
13718 		return -EINVAL;
13719 
13720 	/* don't allow switching to DFS channels */
13721 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
13722 		return -EINVAL;
13723 
13724 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
13725 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
13726 
13727 	wdev_lock(wdev);
13728 	err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
13729 	wdev_unlock(wdev);
13730 
13731 	return err;
13732 }
13733 
13734 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
13735 					      struct genl_info *info)
13736 {
13737 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13738 	struct net_device *dev = info->user_ptr[1];
13739 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13740 	const u8 *addr;
13741 
13742 	if (!rdev->ops->tdls_channel_switch ||
13743 	    !rdev->ops->tdls_cancel_channel_switch ||
13744 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
13745 		return -EOPNOTSUPP;
13746 
13747 	switch (dev->ieee80211_ptr->iftype) {
13748 	case NL80211_IFTYPE_STATION:
13749 	case NL80211_IFTYPE_P2P_CLIENT:
13750 		break;
13751 	default:
13752 		return -EOPNOTSUPP;
13753 	}
13754 
13755 	if (!info->attrs[NL80211_ATTR_MAC])
13756 		return -EINVAL;
13757 
13758 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
13759 
13760 	wdev_lock(wdev);
13761 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
13762 	wdev_unlock(wdev);
13763 
13764 	return 0;
13765 }
13766 
13767 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
13768 					    struct genl_info *info)
13769 {
13770 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13771 	struct net_device *dev = info->user_ptr[1];
13772 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13773 	const struct nlattr *nla;
13774 	bool enabled;
13775 
13776 	if (!rdev->ops->set_multicast_to_unicast)
13777 		return -EOPNOTSUPP;
13778 
13779 	if (wdev->iftype != NL80211_IFTYPE_AP &&
13780 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
13781 		return -EOPNOTSUPP;
13782 
13783 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
13784 	enabled = nla_get_flag(nla);
13785 
13786 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
13787 }
13788 
13789 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
13790 {
13791 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13792 	struct net_device *dev = info->user_ptr[1];
13793 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13794 	struct cfg80211_pmk_conf pmk_conf = {};
13795 	int ret;
13796 
13797 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
13798 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
13799 		return -EOPNOTSUPP;
13800 
13801 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
13802 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
13803 		return -EOPNOTSUPP;
13804 
13805 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
13806 		return -EINVAL;
13807 
13808 	wdev_lock(wdev);
13809 	if (!wdev->current_bss) {
13810 		ret = -ENOTCONN;
13811 		goto out;
13812 	}
13813 
13814 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
13815 	if (memcmp(pmk_conf.aa, wdev->current_bss->pub.bssid, ETH_ALEN)) {
13816 		ret = -EINVAL;
13817 		goto out;
13818 	}
13819 
13820 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
13821 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
13822 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
13823 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
13824 		ret = -EINVAL;
13825 		goto out;
13826 	}
13827 
13828 	if (info->attrs[NL80211_ATTR_PMKR0_NAME]) {
13829 		int r0_name_len = nla_len(info->attrs[NL80211_ATTR_PMKR0_NAME]);
13830 
13831 		if (r0_name_len != WLAN_PMK_NAME_LEN) {
13832 			ret = -EINVAL;
13833 			goto out;
13834 		}
13835 
13836 		pmk_conf.pmk_r0_name =
13837 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
13838 	}
13839 
13840 	ret = rdev_set_pmk(rdev, dev, &pmk_conf);
13841 out:
13842 	wdev_unlock(wdev);
13843 	return ret;
13844 }
13845 
13846 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
13847 {
13848 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13849 	struct net_device *dev = info->user_ptr[1];
13850 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13851 	const u8 *aa;
13852 	int ret;
13853 
13854 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
13855 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
13856 		return -EOPNOTSUPP;
13857 
13858 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
13859 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
13860 		return -EOPNOTSUPP;
13861 
13862 	if (!info->attrs[NL80211_ATTR_MAC])
13863 		return -EINVAL;
13864 
13865 	wdev_lock(wdev);
13866 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
13867 	ret = rdev_del_pmk(rdev, dev, aa);
13868 	wdev_unlock(wdev);
13869 
13870 	return ret;
13871 }
13872 
13873 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
13874 {
13875 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13876 	struct net_device *dev = info->user_ptr[1];
13877 	struct cfg80211_external_auth_params params;
13878 
13879 	if (!rdev->ops->external_auth)
13880 		return -EOPNOTSUPP;
13881 
13882 	if (!info->attrs[NL80211_ATTR_SSID] &&
13883 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
13884 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
13885 		return -EINVAL;
13886 
13887 	if (!info->attrs[NL80211_ATTR_BSSID])
13888 		return -EINVAL;
13889 
13890 	if (!info->attrs[NL80211_ATTR_STATUS_CODE])
13891 		return -EINVAL;
13892 
13893 	memset(&params, 0, sizeof(params));
13894 
13895 	if (info->attrs[NL80211_ATTR_SSID]) {
13896 		params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
13897 		if (params.ssid.ssid_len == 0 ||
13898 		    params.ssid.ssid_len > IEEE80211_MAX_SSID_LEN)
13899 			return -EINVAL;
13900 		memcpy(params.ssid.ssid,
13901 		       nla_data(info->attrs[NL80211_ATTR_SSID]),
13902 		       params.ssid.ssid_len);
13903 	}
13904 
13905 	memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
13906 	       ETH_ALEN);
13907 
13908 	params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
13909 
13910 	if (info->attrs[NL80211_ATTR_PMKID])
13911 		params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
13912 
13913 	return rdev_external_auth(rdev, dev, &params);
13914 }
13915 
13916 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
13917 {
13918 	bool dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
13919 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13920 	struct net_device *dev = info->user_ptr[1];
13921 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13922 	const u8 *buf;
13923 	size_t len;
13924 	u8 *dest;
13925 	u16 proto;
13926 	bool noencrypt;
13927 	u64 cookie = 0;
13928 	int err;
13929 
13930 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
13931 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
13932 		return -EOPNOTSUPP;
13933 
13934 	if (!rdev->ops->tx_control_port)
13935 		return -EOPNOTSUPP;
13936 
13937 	if (!info->attrs[NL80211_ATTR_FRAME] ||
13938 	    !info->attrs[NL80211_ATTR_MAC] ||
13939 	    !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
13940 		GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
13941 		return -EINVAL;
13942 	}
13943 
13944 	wdev_lock(wdev);
13945 
13946 	switch (wdev->iftype) {
13947 	case NL80211_IFTYPE_AP:
13948 	case NL80211_IFTYPE_P2P_GO:
13949 	case NL80211_IFTYPE_MESH_POINT:
13950 		break;
13951 	case NL80211_IFTYPE_ADHOC:
13952 	case NL80211_IFTYPE_STATION:
13953 	case NL80211_IFTYPE_P2P_CLIENT:
13954 		if (wdev->current_bss)
13955 			break;
13956 		err = -ENOTCONN;
13957 		goto out;
13958 	default:
13959 		err = -EOPNOTSUPP;
13960 		goto out;
13961 	}
13962 
13963 	wdev_unlock(wdev);
13964 
13965 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
13966 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
13967 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
13968 	proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
13969 	noencrypt =
13970 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
13971 
13972 	err = rdev_tx_control_port(rdev, dev, buf, len,
13973 				   dest, cpu_to_be16(proto), noencrypt,
13974 				   dont_wait_for_ack ? NULL : &cookie);
13975 	if (!err && !dont_wait_for_ack)
13976 		nl_set_extack_cookie_u64(info->extack, cookie);
13977 	return err;
13978  out:
13979 	wdev_unlock(wdev);
13980 	return err;
13981 }
13982 
13983 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
13984 					   struct genl_info *info)
13985 {
13986 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13987 	struct net_device *dev = info->user_ptr[1];
13988 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13989 	struct cfg80211_ftm_responder_stats ftm_stats = {};
13990 	struct sk_buff *msg;
13991 	void *hdr;
13992 	struct nlattr *ftm_stats_attr;
13993 	int err;
13994 
13995 	if (wdev->iftype != NL80211_IFTYPE_AP || !wdev->beacon_interval)
13996 		return -EOPNOTSUPP;
13997 
13998 	err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
13999 	if (err)
14000 		return err;
14001 
14002 	if (!ftm_stats.filled)
14003 		return -ENODATA;
14004 
14005 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14006 	if (!msg)
14007 		return -ENOMEM;
14008 
14009 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14010 			     NL80211_CMD_GET_FTM_RESPONDER_STATS);
14011 	if (!hdr)
14012 		goto nla_put_failure;
14013 
14014 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
14015 		goto nla_put_failure;
14016 
14017 	ftm_stats_attr = nla_nest_start_noflag(msg,
14018 					       NL80211_ATTR_FTM_RESPONDER_STATS);
14019 	if (!ftm_stats_attr)
14020 		goto nla_put_failure;
14021 
14022 #define SET_FTM(field, name, type)					 \
14023 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
14024 	    nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,		 \
14025 			     ftm_stats.field))				 \
14026 		goto nla_put_failure; } while (0)
14027 #define SET_FTM_U64(field, name)					 \
14028 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
14029 	    nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,		 \
14030 			      ftm_stats.field, NL80211_FTM_STATS_PAD))	 \
14031 		goto nla_put_failure; } while (0)
14032 
14033 	SET_FTM(success_num, SUCCESS_NUM, u32);
14034 	SET_FTM(partial_num, PARTIAL_NUM, u32);
14035 	SET_FTM(failed_num, FAILED_NUM, u32);
14036 	SET_FTM(asap_num, ASAP_NUM, u32);
14037 	SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
14038 	SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
14039 	SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
14040 	SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
14041 	SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
14042 #undef SET_FTM
14043 
14044 	nla_nest_end(msg, ftm_stats_attr);
14045 
14046 	genlmsg_end(msg, hdr);
14047 	return genlmsg_reply(msg, info);
14048 
14049 nla_put_failure:
14050 	nlmsg_free(msg);
14051 	return -ENOBUFS;
14052 }
14053 
14054 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
14055 {
14056 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14057 	struct cfg80211_update_owe_info owe_info;
14058 	struct net_device *dev = info->user_ptr[1];
14059 
14060 	if (!rdev->ops->update_owe_info)
14061 		return -EOPNOTSUPP;
14062 
14063 	if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
14064 	    !info->attrs[NL80211_ATTR_MAC])
14065 		return -EINVAL;
14066 
14067 	memset(&owe_info, 0, sizeof(owe_info));
14068 	owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
14069 	nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
14070 
14071 	if (info->attrs[NL80211_ATTR_IE]) {
14072 		owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
14073 		owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
14074 	}
14075 
14076 	return rdev_update_owe_info(rdev, dev, &owe_info);
14077 }
14078 
14079 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
14080 {
14081 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14082 	struct net_device *dev = info->user_ptr[1];
14083 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14084 	struct station_info sinfo = {};
14085 	const u8 *buf;
14086 	size_t len;
14087 	u8 *dest;
14088 	int err;
14089 
14090 	if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
14091 		return -EOPNOTSUPP;
14092 
14093 	if (!info->attrs[NL80211_ATTR_MAC] ||
14094 	    !info->attrs[NL80211_ATTR_FRAME]) {
14095 		GENL_SET_ERR_MSG(info, "Frame or MAC missing");
14096 		return -EINVAL;
14097 	}
14098 
14099 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
14100 		return -EOPNOTSUPP;
14101 
14102 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
14103 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
14104 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
14105 
14106 	if (len < sizeof(struct ethhdr))
14107 		return -EINVAL;
14108 
14109 	if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
14110 	    !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
14111 		return -EINVAL;
14112 
14113 	err = rdev_get_station(rdev, dev, dest, &sinfo);
14114 	if (err)
14115 		return err;
14116 
14117 	cfg80211_sinfo_release_content(&sinfo);
14118 
14119 	return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
14120 }
14121 
14122 static int parse_tid_conf(struct cfg80211_registered_device *rdev,
14123 			  struct nlattr *attrs[], struct net_device *dev,
14124 			  struct cfg80211_tid_cfg *tid_conf,
14125 			  struct genl_info *info, const u8 *peer)
14126 {
14127 	struct netlink_ext_ack *extack = info->extack;
14128 	u64 mask;
14129 	int err;
14130 
14131 	if (!attrs[NL80211_TID_CONFIG_ATTR_TIDS])
14132 		return -EINVAL;
14133 
14134 	tid_conf->config_override =
14135 			nla_get_flag(attrs[NL80211_TID_CONFIG_ATTR_OVERRIDE]);
14136 	tid_conf->tids = nla_get_u16(attrs[NL80211_TID_CONFIG_ATTR_TIDS]);
14137 
14138 	if (tid_conf->config_override) {
14139 		if (rdev->ops->reset_tid_config) {
14140 			err = rdev_reset_tid_config(rdev, dev, peer,
14141 						    tid_conf->tids);
14142 			if (err)
14143 				return err;
14144 		} else {
14145 			return -EINVAL;
14146 		}
14147 	}
14148 
14149 	if (attrs[NL80211_TID_CONFIG_ATTR_NOACK]) {
14150 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_NOACK);
14151 		tid_conf->noack =
14152 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_NOACK]);
14153 	}
14154 
14155 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]) {
14156 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT);
14157 		tid_conf->retry_short =
14158 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]);
14159 
14160 		if (tid_conf->retry_short > rdev->wiphy.max_data_retry_count)
14161 			return -EINVAL;
14162 	}
14163 
14164 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]) {
14165 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
14166 		tid_conf->retry_long =
14167 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]);
14168 
14169 		if (tid_conf->retry_long > rdev->wiphy.max_data_retry_count)
14170 			return -EINVAL;
14171 	}
14172 
14173 	if (attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]) {
14174 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
14175 		tid_conf->ampdu =
14176 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]);
14177 	}
14178 
14179 	if (attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]) {
14180 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
14181 		tid_conf->rtscts =
14182 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]);
14183 	}
14184 
14185 	if (attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]) {
14186 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
14187 		tid_conf->amsdu =
14188 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]);
14189 	}
14190 
14191 	if (attrs[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE]) {
14192 		u32 idx = NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE, attr;
14193 
14194 		tid_conf->txrate_type = nla_get_u8(attrs[idx]);
14195 
14196 		if (tid_conf->txrate_type != NL80211_TX_RATE_AUTOMATIC) {
14197 			attr = NL80211_TID_CONFIG_ATTR_TX_RATE;
14198 			err = nl80211_parse_tx_bitrate_mask(info, attrs, attr,
14199 						    &tid_conf->txrate_mask);
14200 			if (err)
14201 				return err;
14202 
14203 			tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE);
14204 		}
14205 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
14206 	}
14207 
14208 	if (peer)
14209 		mask = rdev->wiphy.tid_config_support.peer;
14210 	else
14211 		mask = rdev->wiphy.tid_config_support.vif;
14212 
14213 	if (tid_conf->mask & ~mask) {
14214 		NL_SET_ERR_MSG(extack, "unsupported TID configuration");
14215 		return -ENOTSUPP;
14216 	}
14217 
14218 	return 0;
14219 }
14220 
14221 static int nl80211_set_tid_config(struct sk_buff *skb,
14222 				  struct genl_info *info)
14223 {
14224 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14225 	struct nlattr *attrs[NL80211_TID_CONFIG_ATTR_MAX + 1];
14226 	struct net_device *dev = info->user_ptr[1];
14227 	struct cfg80211_tid_config *tid_config;
14228 	struct nlattr *tid;
14229 	int conf_idx = 0, rem_conf;
14230 	int ret = -EINVAL;
14231 	u32 num_conf = 0;
14232 
14233 	if (!info->attrs[NL80211_ATTR_TID_CONFIG])
14234 		return -EINVAL;
14235 
14236 	if (!rdev->ops->set_tid_config)
14237 		return -EOPNOTSUPP;
14238 
14239 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
14240 			    rem_conf)
14241 		num_conf++;
14242 
14243 	tid_config = kzalloc(struct_size(tid_config, tid_conf, num_conf),
14244 			     GFP_KERNEL);
14245 	if (!tid_config)
14246 		return -ENOMEM;
14247 
14248 	tid_config->n_tid_conf = num_conf;
14249 
14250 	if (info->attrs[NL80211_ATTR_MAC])
14251 		tid_config->peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
14252 
14253 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
14254 			    rem_conf) {
14255 		ret = nla_parse_nested(attrs, NL80211_TID_CONFIG_ATTR_MAX,
14256 				       tid, NULL, NULL);
14257 
14258 		if (ret)
14259 			goto bad_tid_conf;
14260 
14261 		ret = parse_tid_conf(rdev, attrs, dev,
14262 				     &tid_config->tid_conf[conf_idx],
14263 				     info, tid_config->peer);
14264 		if (ret)
14265 			goto bad_tid_conf;
14266 
14267 		conf_idx++;
14268 	}
14269 
14270 	ret = rdev_set_tid_config(rdev, dev, tid_config);
14271 
14272 bad_tid_conf:
14273 	kfree(tid_config);
14274 	return ret;
14275 }
14276 
14277 #define NL80211_FLAG_NEED_WIPHY		0x01
14278 #define NL80211_FLAG_NEED_NETDEV	0x02
14279 #define NL80211_FLAG_NEED_RTNL		0x04
14280 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
14281 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
14282 					 NL80211_FLAG_CHECK_NETDEV_UP)
14283 #define NL80211_FLAG_NEED_WDEV		0x10
14284 /* If a netdev is associated, it must be UP, P2P must be started */
14285 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
14286 					 NL80211_FLAG_CHECK_NETDEV_UP)
14287 #define NL80211_FLAG_CLEAR_SKB		0x20
14288 
14289 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
14290 			    struct genl_info *info)
14291 {
14292 	struct cfg80211_registered_device *rdev;
14293 	struct wireless_dev *wdev;
14294 	struct net_device *dev;
14295 	bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
14296 
14297 	if (rtnl)
14298 		rtnl_lock();
14299 
14300 	if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
14301 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
14302 		if (IS_ERR(rdev)) {
14303 			if (rtnl)
14304 				rtnl_unlock();
14305 			return PTR_ERR(rdev);
14306 		}
14307 		info->user_ptr[0] = rdev;
14308 	} else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
14309 		   ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
14310 		ASSERT_RTNL();
14311 
14312 		wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
14313 						  info->attrs);
14314 		if (IS_ERR(wdev)) {
14315 			if (rtnl)
14316 				rtnl_unlock();
14317 			return PTR_ERR(wdev);
14318 		}
14319 
14320 		dev = wdev->netdev;
14321 		rdev = wiphy_to_rdev(wdev->wiphy);
14322 
14323 		if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
14324 			if (!dev) {
14325 				if (rtnl)
14326 					rtnl_unlock();
14327 				return -EINVAL;
14328 			}
14329 
14330 			info->user_ptr[1] = dev;
14331 		} else {
14332 			info->user_ptr[1] = wdev;
14333 		}
14334 
14335 		if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
14336 		    !wdev_running(wdev)) {
14337 			if (rtnl)
14338 				rtnl_unlock();
14339 			return -ENETDOWN;
14340 		}
14341 
14342 		if (dev)
14343 			dev_hold(dev);
14344 
14345 		info->user_ptr[0] = rdev;
14346 	}
14347 
14348 	return 0;
14349 }
14350 
14351 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
14352 			      struct genl_info *info)
14353 {
14354 	if (info->user_ptr[1]) {
14355 		if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
14356 			struct wireless_dev *wdev = info->user_ptr[1];
14357 
14358 			if (wdev->netdev)
14359 				dev_put(wdev->netdev);
14360 		} else {
14361 			dev_put(info->user_ptr[1]);
14362 		}
14363 	}
14364 
14365 	if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
14366 		rtnl_unlock();
14367 
14368 	/* If needed, clear the netlink message payload from the SKB
14369 	 * as it might contain key data that shouldn't stick around on
14370 	 * the heap after the SKB is freed. The netlink message header
14371 	 * is still needed for further processing, so leave it intact.
14372 	 */
14373 	if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
14374 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
14375 
14376 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
14377 	}
14378 }
14379 
14380 static const struct genl_ops nl80211_ops[] = {
14381 	{
14382 		.cmd = NL80211_CMD_GET_WIPHY,
14383 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14384 		.doit = nl80211_get_wiphy,
14385 		.dumpit = nl80211_dump_wiphy,
14386 		.done = nl80211_dump_wiphy_done,
14387 		/* can be retrieved by unprivileged users */
14388 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14389 				  NL80211_FLAG_NEED_RTNL,
14390 	},
14391 	{
14392 		.cmd = NL80211_CMD_SET_WIPHY,
14393 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14394 		.doit = nl80211_set_wiphy,
14395 		.flags = GENL_UNS_ADMIN_PERM,
14396 		.internal_flags = NL80211_FLAG_NEED_RTNL,
14397 	},
14398 	{
14399 		.cmd = NL80211_CMD_GET_INTERFACE,
14400 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14401 		.doit = nl80211_get_interface,
14402 		.dumpit = nl80211_dump_interface,
14403 		/* can be retrieved by unprivileged users */
14404 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14405 				  NL80211_FLAG_NEED_RTNL,
14406 	},
14407 	{
14408 		.cmd = NL80211_CMD_SET_INTERFACE,
14409 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14410 		.doit = nl80211_set_interface,
14411 		.flags = GENL_UNS_ADMIN_PERM,
14412 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14413 				  NL80211_FLAG_NEED_RTNL,
14414 	},
14415 	{
14416 		.cmd = NL80211_CMD_NEW_INTERFACE,
14417 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14418 		.doit = nl80211_new_interface,
14419 		.flags = GENL_UNS_ADMIN_PERM,
14420 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14421 				  NL80211_FLAG_NEED_RTNL,
14422 	},
14423 	{
14424 		.cmd = NL80211_CMD_DEL_INTERFACE,
14425 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14426 		.doit = nl80211_del_interface,
14427 		.flags = GENL_UNS_ADMIN_PERM,
14428 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14429 				  NL80211_FLAG_NEED_RTNL,
14430 	},
14431 	{
14432 		.cmd = NL80211_CMD_GET_KEY,
14433 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14434 		.doit = nl80211_get_key,
14435 		.flags = GENL_UNS_ADMIN_PERM,
14436 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14437 				  NL80211_FLAG_NEED_RTNL,
14438 	},
14439 	{
14440 		.cmd = NL80211_CMD_SET_KEY,
14441 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14442 		.doit = nl80211_set_key,
14443 		.flags = GENL_UNS_ADMIN_PERM,
14444 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14445 				  NL80211_FLAG_NEED_RTNL |
14446 				  NL80211_FLAG_CLEAR_SKB,
14447 	},
14448 	{
14449 		.cmd = NL80211_CMD_NEW_KEY,
14450 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14451 		.doit = nl80211_new_key,
14452 		.flags = GENL_UNS_ADMIN_PERM,
14453 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14454 				  NL80211_FLAG_NEED_RTNL |
14455 				  NL80211_FLAG_CLEAR_SKB,
14456 	},
14457 	{
14458 		.cmd = NL80211_CMD_DEL_KEY,
14459 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14460 		.doit = nl80211_del_key,
14461 		.flags = GENL_UNS_ADMIN_PERM,
14462 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14463 				  NL80211_FLAG_NEED_RTNL,
14464 	},
14465 	{
14466 		.cmd = NL80211_CMD_SET_BEACON,
14467 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14468 		.flags = GENL_UNS_ADMIN_PERM,
14469 		.doit = nl80211_set_beacon,
14470 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14471 				  NL80211_FLAG_NEED_RTNL,
14472 	},
14473 	{
14474 		.cmd = NL80211_CMD_START_AP,
14475 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14476 		.flags = GENL_UNS_ADMIN_PERM,
14477 		.doit = nl80211_start_ap,
14478 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14479 				  NL80211_FLAG_NEED_RTNL,
14480 	},
14481 	{
14482 		.cmd = NL80211_CMD_STOP_AP,
14483 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14484 		.flags = GENL_UNS_ADMIN_PERM,
14485 		.doit = nl80211_stop_ap,
14486 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14487 				  NL80211_FLAG_NEED_RTNL,
14488 	},
14489 	{
14490 		.cmd = NL80211_CMD_GET_STATION,
14491 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14492 		.doit = nl80211_get_station,
14493 		.dumpit = nl80211_dump_station,
14494 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14495 				  NL80211_FLAG_NEED_RTNL,
14496 	},
14497 	{
14498 		.cmd = NL80211_CMD_SET_STATION,
14499 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14500 		.doit = nl80211_set_station,
14501 		.flags = GENL_UNS_ADMIN_PERM,
14502 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14503 				  NL80211_FLAG_NEED_RTNL,
14504 	},
14505 	{
14506 		.cmd = NL80211_CMD_NEW_STATION,
14507 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14508 		.doit = nl80211_new_station,
14509 		.flags = GENL_UNS_ADMIN_PERM,
14510 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14511 				  NL80211_FLAG_NEED_RTNL,
14512 	},
14513 	{
14514 		.cmd = NL80211_CMD_DEL_STATION,
14515 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14516 		.doit = nl80211_del_station,
14517 		.flags = GENL_UNS_ADMIN_PERM,
14518 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14519 				  NL80211_FLAG_NEED_RTNL,
14520 	},
14521 	{
14522 		.cmd = NL80211_CMD_GET_MPATH,
14523 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14524 		.doit = nl80211_get_mpath,
14525 		.dumpit = nl80211_dump_mpath,
14526 		.flags = GENL_UNS_ADMIN_PERM,
14527 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14528 				  NL80211_FLAG_NEED_RTNL,
14529 	},
14530 	{
14531 		.cmd = NL80211_CMD_GET_MPP,
14532 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14533 		.doit = nl80211_get_mpp,
14534 		.dumpit = nl80211_dump_mpp,
14535 		.flags = GENL_UNS_ADMIN_PERM,
14536 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14537 				  NL80211_FLAG_NEED_RTNL,
14538 	},
14539 	{
14540 		.cmd = NL80211_CMD_SET_MPATH,
14541 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14542 		.doit = nl80211_set_mpath,
14543 		.flags = GENL_UNS_ADMIN_PERM,
14544 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14545 				  NL80211_FLAG_NEED_RTNL,
14546 	},
14547 	{
14548 		.cmd = NL80211_CMD_NEW_MPATH,
14549 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14550 		.doit = nl80211_new_mpath,
14551 		.flags = GENL_UNS_ADMIN_PERM,
14552 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14553 				  NL80211_FLAG_NEED_RTNL,
14554 	},
14555 	{
14556 		.cmd = NL80211_CMD_DEL_MPATH,
14557 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14558 		.doit = nl80211_del_mpath,
14559 		.flags = GENL_UNS_ADMIN_PERM,
14560 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14561 				  NL80211_FLAG_NEED_RTNL,
14562 	},
14563 	{
14564 		.cmd = NL80211_CMD_SET_BSS,
14565 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14566 		.doit = nl80211_set_bss,
14567 		.flags = GENL_UNS_ADMIN_PERM,
14568 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14569 				  NL80211_FLAG_NEED_RTNL,
14570 	},
14571 	{
14572 		.cmd = NL80211_CMD_GET_REG,
14573 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14574 		.doit = nl80211_get_reg_do,
14575 		.dumpit = nl80211_get_reg_dump,
14576 		.internal_flags = NL80211_FLAG_NEED_RTNL,
14577 		/* can be retrieved by unprivileged users */
14578 	},
14579 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
14580 	{
14581 		.cmd = NL80211_CMD_SET_REG,
14582 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14583 		.doit = nl80211_set_reg,
14584 		.flags = GENL_ADMIN_PERM,
14585 		.internal_flags = NL80211_FLAG_NEED_RTNL,
14586 	},
14587 #endif
14588 	{
14589 		.cmd = NL80211_CMD_REQ_SET_REG,
14590 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14591 		.doit = nl80211_req_set_reg,
14592 		.flags = GENL_ADMIN_PERM,
14593 	},
14594 	{
14595 		.cmd = NL80211_CMD_RELOAD_REGDB,
14596 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14597 		.doit = nl80211_reload_regdb,
14598 		.flags = GENL_ADMIN_PERM,
14599 	},
14600 	{
14601 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
14602 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14603 		.doit = nl80211_get_mesh_config,
14604 		/* can be retrieved by unprivileged users */
14605 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14606 				  NL80211_FLAG_NEED_RTNL,
14607 	},
14608 	{
14609 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
14610 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14611 		.doit = nl80211_update_mesh_config,
14612 		.flags = GENL_UNS_ADMIN_PERM,
14613 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14614 				  NL80211_FLAG_NEED_RTNL,
14615 	},
14616 	{
14617 		.cmd = NL80211_CMD_TRIGGER_SCAN,
14618 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14619 		.doit = nl80211_trigger_scan,
14620 		.flags = GENL_UNS_ADMIN_PERM,
14621 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14622 				  NL80211_FLAG_NEED_RTNL,
14623 	},
14624 	{
14625 		.cmd = NL80211_CMD_ABORT_SCAN,
14626 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14627 		.doit = nl80211_abort_scan,
14628 		.flags = GENL_UNS_ADMIN_PERM,
14629 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14630 				  NL80211_FLAG_NEED_RTNL,
14631 	},
14632 	{
14633 		.cmd = NL80211_CMD_GET_SCAN,
14634 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14635 		.dumpit = nl80211_dump_scan,
14636 	},
14637 	{
14638 		.cmd = NL80211_CMD_START_SCHED_SCAN,
14639 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14640 		.doit = nl80211_start_sched_scan,
14641 		.flags = GENL_UNS_ADMIN_PERM,
14642 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14643 				  NL80211_FLAG_NEED_RTNL,
14644 	},
14645 	{
14646 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
14647 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14648 		.doit = nl80211_stop_sched_scan,
14649 		.flags = GENL_UNS_ADMIN_PERM,
14650 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14651 				  NL80211_FLAG_NEED_RTNL,
14652 	},
14653 	{
14654 		.cmd = NL80211_CMD_AUTHENTICATE,
14655 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14656 		.doit = nl80211_authenticate,
14657 		.flags = GENL_UNS_ADMIN_PERM,
14658 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14659 				  NL80211_FLAG_NEED_RTNL |
14660 				  NL80211_FLAG_CLEAR_SKB,
14661 	},
14662 	{
14663 		.cmd = NL80211_CMD_ASSOCIATE,
14664 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14665 		.doit = nl80211_associate,
14666 		.flags = GENL_UNS_ADMIN_PERM,
14667 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14668 				  NL80211_FLAG_NEED_RTNL |
14669 				  NL80211_FLAG_CLEAR_SKB,
14670 	},
14671 	{
14672 		.cmd = NL80211_CMD_DEAUTHENTICATE,
14673 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14674 		.doit = nl80211_deauthenticate,
14675 		.flags = GENL_UNS_ADMIN_PERM,
14676 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14677 				  NL80211_FLAG_NEED_RTNL,
14678 	},
14679 	{
14680 		.cmd = NL80211_CMD_DISASSOCIATE,
14681 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14682 		.doit = nl80211_disassociate,
14683 		.flags = GENL_UNS_ADMIN_PERM,
14684 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14685 				  NL80211_FLAG_NEED_RTNL,
14686 	},
14687 	{
14688 		.cmd = NL80211_CMD_JOIN_IBSS,
14689 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14690 		.doit = nl80211_join_ibss,
14691 		.flags = GENL_UNS_ADMIN_PERM,
14692 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14693 				  NL80211_FLAG_NEED_RTNL,
14694 	},
14695 	{
14696 		.cmd = NL80211_CMD_LEAVE_IBSS,
14697 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14698 		.doit = nl80211_leave_ibss,
14699 		.flags = GENL_UNS_ADMIN_PERM,
14700 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14701 				  NL80211_FLAG_NEED_RTNL,
14702 	},
14703 #ifdef CONFIG_NL80211_TESTMODE
14704 	{
14705 		.cmd = NL80211_CMD_TESTMODE,
14706 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14707 		.doit = nl80211_testmode_do,
14708 		.dumpit = nl80211_testmode_dump,
14709 		.flags = GENL_UNS_ADMIN_PERM,
14710 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14711 				  NL80211_FLAG_NEED_RTNL,
14712 	},
14713 #endif
14714 	{
14715 		.cmd = NL80211_CMD_CONNECT,
14716 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14717 		.doit = nl80211_connect,
14718 		.flags = GENL_UNS_ADMIN_PERM,
14719 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14720 				  NL80211_FLAG_NEED_RTNL |
14721 				  NL80211_FLAG_CLEAR_SKB,
14722 	},
14723 	{
14724 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
14725 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14726 		.doit = nl80211_update_connect_params,
14727 		.flags = GENL_ADMIN_PERM,
14728 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14729 				  NL80211_FLAG_NEED_RTNL |
14730 				  NL80211_FLAG_CLEAR_SKB,
14731 	},
14732 	{
14733 		.cmd = NL80211_CMD_DISCONNECT,
14734 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14735 		.doit = nl80211_disconnect,
14736 		.flags = GENL_UNS_ADMIN_PERM,
14737 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14738 				  NL80211_FLAG_NEED_RTNL,
14739 	},
14740 	{
14741 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
14742 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14743 		.doit = nl80211_wiphy_netns,
14744 		.flags = GENL_UNS_ADMIN_PERM,
14745 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14746 				  NL80211_FLAG_NEED_RTNL,
14747 	},
14748 	{
14749 		.cmd = NL80211_CMD_GET_SURVEY,
14750 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14751 		.dumpit = nl80211_dump_survey,
14752 	},
14753 	{
14754 		.cmd = NL80211_CMD_SET_PMKSA,
14755 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14756 		.doit = nl80211_setdel_pmksa,
14757 		.flags = GENL_UNS_ADMIN_PERM,
14758 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14759 				  NL80211_FLAG_NEED_RTNL |
14760 				  NL80211_FLAG_CLEAR_SKB,
14761 	},
14762 	{
14763 		.cmd = NL80211_CMD_DEL_PMKSA,
14764 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14765 		.doit = nl80211_setdel_pmksa,
14766 		.flags = GENL_UNS_ADMIN_PERM,
14767 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14768 				  NL80211_FLAG_NEED_RTNL,
14769 	},
14770 	{
14771 		.cmd = NL80211_CMD_FLUSH_PMKSA,
14772 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14773 		.doit = nl80211_flush_pmksa,
14774 		.flags = GENL_UNS_ADMIN_PERM,
14775 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14776 				  NL80211_FLAG_NEED_RTNL,
14777 	},
14778 	{
14779 		.cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
14780 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14781 		.doit = nl80211_remain_on_channel,
14782 		.flags = GENL_UNS_ADMIN_PERM,
14783 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14784 				  NL80211_FLAG_NEED_RTNL,
14785 	},
14786 	{
14787 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
14788 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14789 		.doit = nl80211_cancel_remain_on_channel,
14790 		.flags = GENL_UNS_ADMIN_PERM,
14791 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14792 				  NL80211_FLAG_NEED_RTNL,
14793 	},
14794 	{
14795 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
14796 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14797 		.doit = nl80211_set_tx_bitrate_mask,
14798 		.flags = GENL_UNS_ADMIN_PERM,
14799 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14800 				  NL80211_FLAG_NEED_RTNL,
14801 	},
14802 	{
14803 		.cmd = NL80211_CMD_REGISTER_FRAME,
14804 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14805 		.doit = nl80211_register_mgmt,
14806 		.flags = GENL_UNS_ADMIN_PERM,
14807 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14808 				  NL80211_FLAG_NEED_RTNL,
14809 	},
14810 	{
14811 		.cmd = NL80211_CMD_FRAME,
14812 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14813 		.doit = nl80211_tx_mgmt,
14814 		.flags = GENL_UNS_ADMIN_PERM,
14815 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14816 				  NL80211_FLAG_NEED_RTNL,
14817 	},
14818 	{
14819 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
14820 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14821 		.doit = nl80211_tx_mgmt_cancel_wait,
14822 		.flags = GENL_UNS_ADMIN_PERM,
14823 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14824 				  NL80211_FLAG_NEED_RTNL,
14825 	},
14826 	{
14827 		.cmd = NL80211_CMD_SET_POWER_SAVE,
14828 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14829 		.doit = nl80211_set_power_save,
14830 		.flags = GENL_UNS_ADMIN_PERM,
14831 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14832 				  NL80211_FLAG_NEED_RTNL,
14833 	},
14834 	{
14835 		.cmd = NL80211_CMD_GET_POWER_SAVE,
14836 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14837 		.doit = nl80211_get_power_save,
14838 		/* can be retrieved by unprivileged users */
14839 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14840 				  NL80211_FLAG_NEED_RTNL,
14841 	},
14842 	{
14843 		.cmd = NL80211_CMD_SET_CQM,
14844 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14845 		.doit = nl80211_set_cqm,
14846 		.flags = GENL_UNS_ADMIN_PERM,
14847 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14848 				  NL80211_FLAG_NEED_RTNL,
14849 	},
14850 	{
14851 		.cmd = NL80211_CMD_SET_CHANNEL,
14852 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14853 		.doit = nl80211_set_channel,
14854 		.flags = GENL_UNS_ADMIN_PERM,
14855 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14856 				  NL80211_FLAG_NEED_RTNL,
14857 	},
14858 	{
14859 		.cmd = NL80211_CMD_SET_WDS_PEER,
14860 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14861 		.doit = nl80211_set_wds_peer,
14862 		.flags = GENL_UNS_ADMIN_PERM,
14863 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14864 				  NL80211_FLAG_NEED_RTNL,
14865 	},
14866 	{
14867 		.cmd = NL80211_CMD_JOIN_MESH,
14868 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14869 		.doit = nl80211_join_mesh,
14870 		.flags = GENL_UNS_ADMIN_PERM,
14871 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14872 				  NL80211_FLAG_NEED_RTNL,
14873 	},
14874 	{
14875 		.cmd = NL80211_CMD_LEAVE_MESH,
14876 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14877 		.doit = nl80211_leave_mesh,
14878 		.flags = GENL_UNS_ADMIN_PERM,
14879 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14880 				  NL80211_FLAG_NEED_RTNL,
14881 	},
14882 	{
14883 		.cmd = NL80211_CMD_JOIN_OCB,
14884 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14885 		.doit = nl80211_join_ocb,
14886 		.flags = GENL_UNS_ADMIN_PERM,
14887 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14888 				  NL80211_FLAG_NEED_RTNL,
14889 	},
14890 	{
14891 		.cmd = NL80211_CMD_LEAVE_OCB,
14892 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14893 		.doit = nl80211_leave_ocb,
14894 		.flags = GENL_UNS_ADMIN_PERM,
14895 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14896 				  NL80211_FLAG_NEED_RTNL,
14897 	},
14898 #ifdef CONFIG_PM
14899 	{
14900 		.cmd = NL80211_CMD_GET_WOWLAN,
14901 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14902 		.doit = nl80211_get_wowlan,
14903 		/* can be retrieved by unprivileged users */
14904 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14905 				  NL80211_FLAG_NEED_RTNL,
14906 	},
14907 	{
14908 		.cmd = NL80211_CMD_SET_WOWLAN,
14909 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14910 		.doit = nl80211_set_wowlan,
14911 		.flags = GENL_UNS_ADMIN_PERM,
14912 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14913 				  NL80211_FLAG_NEED_RTNL,
14914 	},
14915 #endif
14916 	{
14917 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
14918 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14919 		.doit = nl80211_set_rekey_data,
14920 		.flags = GENL_UNS_ADMIN_PERM,
14921 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14922 				  NL80211_FLAG_NEED_RTNL |
14923 				  NL80211_FLAG_CLEAR_SKB,
14924 	},
14925 	{
14926 		.cmd = NL80211_CMD_TDLS_MGMT,
14927 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14928 		.doit = nl80211_tdls_mgmt,
14929 		.flags = GENL_UNS_ADMIN_PERM,
14930 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14931 				  NL80211_FLAG_NEED_RTNL,
14932 	},
14933 	{
14934 		.cmd = NL80211_CMD_TDLS_OPER,
14935 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14936 		.doit = nl80211_tdls_oper,
14937 		.flags = GENL_UNS_ADMIN_PERM,
14938 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14939 				  NL80211_FLAG_NEED_RTNL,
14940 	},
14941 	{
14942 		.cmd = NL80211_CMD_UNEXPECTED_FRAME,
14943 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14944 		.doit = nl80211_register_unexpected_frame,
14945 		.flags = GENL_UNS_ADMIN_PERM,
14946 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14947 				  NL80211_FLAG_NEED_RTNL,
14948 	},
14949 	{
14950 		.cmd = NL80211_CMD_PROBE_CLIENT,
14951 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14952 		.doit = nl80211_probe_client,
14953 		.flags = GENL_UNS_ADMIN_PERM,
14954 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14955 				  NL80211_FLAG_NEED_RTNL,
14956 	},
14957 	{
14958 		.cmd = NL80211_CMD_REGISTER_BEACONS,
14959 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14960 		.doit = nl80211_register_beacons,
14961 		.flags = GENL_UNS_ADMIN_PERM,
14962 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14963 				  NL80211_FLAG_NEED_RTNL,
14964 	},
14965 	{
14966 		.cmd = NL80211_CMD_SET_NOACK_MAP,
14967 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14968 		.doit = nl80211_set_noack_map,
14969 		.flags = GENL_UNS_ADMIN_PERM,
14970 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14971 				  NL80211_FLAG_NEED_RTNL,
14972 	},
14973 	{
14974 		.cmd = NL80211_CMD_START_P2P_DEVICE,
14975 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14976 		.doit = nl80211_start_p2p_device,
14977 		.flags = GENL_UNS_ADMIN_PERM,
14978 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14979 				  NL80211_FLAG_NEED_RTNL,
14980 	},
14981 	{
14982 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
14983 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14984 		.doit = nl80211_stop_p2p_device,
14985 		.flags = GENL_UNS_ADMIN_PERM,
14986 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14987 				  NL80211_FLAG_NEED_RTNL,
14988 	},
14989 	{
14990 		.cmd = NL80211_CMD_START_NAN,
14991 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14992 		.doit = nl80211_start_nan,
14993 		.flags = GENL_ADMIN_PERM,
14994 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14995 				  NL80211_FLAG_NEED_RTNL,
14996 	},
14997 	{
14998 		.cmd = NL80211_CMD_STOP_NAN,
14999 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15000 		.doit = nl80211_stop_nan,
15001 		.flags = GENL_ADMIN_PERM,
15002 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15003 				  NL80211_FLAG_NEED_RTNL,
15004 	},
15005 	{
15006 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
15007 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15008 		.doit = nl80211_nan_add_func,
15009 		.flags = GENL_ADMIN_PERM,
15010 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15011 				  NL80211_FLAG_NEED_RTNL,
15012 	},
15013 	{
15014 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
15015 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15016 		.doit = nl80211_nan_del_func,
15017 		.flags = GENL_ADMIN_PERM,
15018 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15019 				  NL80211_FLAG_NEED_RTNL,
15020 	},
15021 	{
15022 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
15023 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15024 		.doit = nl80211_nan_change_config,
15025 		.flags = GENL_ADMIN_PERM,
15026 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15027 				  NL80211_FLAG_NEED_RTNL,
15028 	},
15029 	{
15030 		.cmd = NL80211_CMD_SET_MCAST_RATE,
15031 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15032 		.doit = nl80211_set_mcast_rate,
15033 		.flags = GENL_UNS_ADMIN_PERM,
15034 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15035 				  NL80211_FLAG_NEED_RTNL,
15036 	},
15037 	{
15038 		.cmd = NL80211_CMD_SET_MAC_ACL,
15039 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15040 		.doit = nl80211_set_mac_acl,
15041 		.flags = GENL_UNS_ADMIN_PERM,
15042 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15043 				  NL80211_FLAG_NEED_RTNL,
15044 	},
15045 	{
15046 		.cmd = NL80211_CMD_RADAR_DETECT,
15047 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15048 		.doit = nl80211_start_radar_detection,
15049 		.flags = GENL_UNS_ADMIN_PERM,
15050 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15051 				  NL80211_FLAG_NEED_RTNL,
15052 	},
15053 	{
15054 		.cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
15055 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15056 		.doit = nl80211_get_protocol_features,
15057 	},
15058 	{
15059 		.cmd = NL80211_CMD_UPDATE_FT_IES,
15060 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15061 		.doit = nl80211_update_ft_ies,
15062 		.flags = GENL_UNS_ADMIN_PERM,
15063 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15064 				  NL80211_FLAG_NEED_RTNL,
15065 	},
15066 	{
15067 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
15068 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15069 		.doit = nl80211_crit_protocol_start,
15070 		.flags = GENL_UNS_ADMIN_PERM,
15071 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15072 				  NL80211_FLAG_NEED_RTNL,
15073 	},
15074 	{
15075 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
15076 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15077 		.doit = nl80211_crit_protocol_stop,
15078 		.flags = GENL_UNS_ADMIN_PERM,
15079 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15080 				  NL80211_FLAG_NEED_RTNL,
15081 	},
15082 	{
15083 		.cmd = NL80211_CMD_GET_COALESCE,
15084 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15085 		.doit = nl80211_get_coalesce,
15086 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
15087 				  NL80211_FLAG_NEED_RTNL,
15088 	},
15089 	{
15090 		.cmd = NL80211_CMD_SET_COALESCE,
15091 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15092 		.doit = nl80211_set_coalesce,
15093 		.flags = GENL_UNS_ADMIN_PERM,
15094 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
15095 				  NL80211_FLAG_NEED_RTNL,
15096 	},
15097 	{
15098 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
15099 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15100 		.doit = nl80211_channel_switch,
15101 		.flags = GENL_UNS_ADMIN_PERM,
15102 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15103 				  NL80211_FLAG_NEED_RTNL,
15104 	},
15105 	{
15106 		.cmd = NL80211_CMD_VENDOR,
15107 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15108 		.doit = nl80211_vendor_cmd,
15109 		.dumpit = nl80211_vendor_cmd_dump,
15110 		.flags = GENL_UNS_ADMIN_PERM,
15111 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
15112 				  NL80211_FLAG_NEED_RTNL |
15113 				  NL80211_FLAG_CLEAR_SKB,
15114 	},
15115 	{
15116 		.cmd = NL80211_CMD_SET_QOS_MAP,
15117 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15118 		.doit = nl80211_set_qos_map,
15119 		.flags = GENL_UNS_ADMIN_PERM,
15120 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15121 				  NL80211_FLAG_NEED_RTNL,
15122 	},
15123 	{
15124 		.cmd = NL80211_CMD_ADD_TX_TS,
15125 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15126 		.doit = nl80211_add_tx_ts,
15127 		.flags = GENL_UNS_ADMIN_PERM,
15128 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15129 				  NL80211_FLAG_NEED_RTNL,
15130 	},
15131 	{
15132 		.cmd = NL80211_CMD_DEL_TX_TS,
15133 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15134 		.doit = nl80211_del_tx_ts,
15135 		.flags = GENL_UNS_ADMIN_PERM,
15136 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15137 				  NL80211_FLAG_NEED_RTNL,
15138 	},
15139 	{
15140 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
15141 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15142 		.doit = nl80211_tdls_channel_switch,
15143 		.flags = GENL_UNS_ADMIN_PERM,
15144 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15145 				  NL80211_FLAG_NEED_RTNL,
15146 	},
15147 	{
15148 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
15149 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15150 		.doit = nl80211_tdls_cancel_channel_switch,
15151 		.flags = GENL_UNS_ADMIN_PERM,
15152 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15153 				  NL80211_FLAG_NEED_RTNL,
15154 	},
15155 	{
15156 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
15157 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15158 		.doit = nl80211_set_multicast_to_unicast,
15159 		.flags = GENL_UNS_ADMIN_PERM,
15160 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15161 				  NL80211_FLAG_NEED_RTNL,
15162 	},
15163 	{
15164 		.cmd = NL80211_CMD_SET_PMK,
15165 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15166 		.doit = nl80211_set_pmk,
15167 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15168 				  NL80211_FLAG_NEED_RTNL |
15169 				  NL80211_FLAG_CLEAR_SKB,
15170 	},
15171 	{
15172 		.cmd = NL80211_CMD_DEL_PMK,
15173 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15174 		.doit = nl80211_del_pmk,
15175 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15176 				  NL80211_FLAG_NEED_RTNL,
15177 	},
15178 	{
15179 		.cmd = NL80211_CMD_EXTERNAL_AUTH,
15180 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15181 		.doit = nl80211_external_auth,
15182 		.flags = GENL_ADMIN_PERM,
15183 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15184 				  NL80211_FLAG_NEED_RTNL,
15185 	},
15186 	{
15187 		.cmd = NL80211_CMD_CONTROL_PORT_FRAME,
15188 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15189 		.doit = nl80211_tx_control_port,
15190 		.flags = GENL_UNS_ADMIN_PERM,
15191 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15192 				  NL80211_FLAG_NEED_RTNL,
15193 	},
15194 	{
15195 		.cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
15196 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15197 		.doit = nl80211_get_ftm_responder_stats,
15198 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15199 				  NL80211_FLAG_NEED_RTNL,
15200 	},
15201 	{
15202 		.cmd = NL80211_CMD_PEER_MEASUREMENT_START,
15203 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15204 		.doit = nl80211_pmsr_start,
15205 		.flags = GENL_UNS_ADMIN_PERM,
15206 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15207 				  NL80211_FLAG_NEED_RTNL,
15208 	},
15209 	{
15210 		.cmd = NL80211_CMD_NOTIFY_RADAR,
15211 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15212 		.doit = nl80211_notify_radar_detection,
15213 		.flags = GENL_UNS_ADMIN_PERM,
15214 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15215 				  NL80211_FLAG_NEED_RTNL,
15216 	},
15217 	{
15218 		.cmd = NL80211_CMD_UPDATE_OWE_INFO,
15219 		.doit = nl80211_update_owe_info,
15220 		.flags = GENL_ADMIN_PERM,
15221 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15222 				  NL80211_FLAG_NEED_RTNL,
15223 	},
15224 	{
15225 		.cmd = NL80211_CMD_PROBE_MESH_LINK,
15226 		.doit = nl80211_probe_mesh_link,
15227 		.flags = GENL_UNS_ADMIN_PERM,
15228 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15229 				  NL80211_FLAG_NEED_RTNL,
15230 	},
15231 	{
15232 		.cmd = NL80211_CMD_SET_TID_CONFIG,
15233 		.doit = nl80211_set_tid_config,
15234 		.flags = GENL_UNS_ADMIN_PERM,
15235 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15236 				  NL80211_FLAG_NEED_RTNL,
15237 	},
15238 };
15239 
15240 static struct genl_family nl80211_fam __ro_after_init = {
15241 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
15242 	.hdrsize = 0,			/* no private header */
15243 	.version = 1,			/* no particular meaning now */
15244 	.maxattr = NL80211_ATTR_MAX,
15245 	.policy = nl80211_policy,
15246 	.netnsok = true,
15247 	.pre_doit = nl80211_pre_doit,
15248 	.post_doit = nl80211_post_doit,
15249 	.module = THIS_MODULE,
15250 	.ops = nl80211_ops,
15251 	.n_ops = ARRAY_SIZE(nl80211_ops),
15252 	.mcgrps = nl80211_mcgrps,
15253 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
15254 	.parallel_ops = true,
15255 };
15256 
15257 /* notification functions */
15258 
15259 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
15260 			  enum nl80211_commands cmd)
15261 {
15262 	struct sk_buff *msg;
15263 	struct nl80211_dump_wiphy_state state = {};
15264 
15265 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
15266 		cmd != NL80211_CMD_DEL_WIPHY);
15267 
15268 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15269 	if (!msg)
15270 		return;
15271 
15272 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
15273 		nlmsg_free(msg);
15274 		return;
15275 	}
15276 
15277 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15278 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
15279 }
15280 
15281 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
15282 				struct wireless_dev *wdev,
15283 				enum nl80211_commands cmd)
15284 {
15285 	struct sk_buff *msg;
15286 
15287 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15288 	if (!msg)
15289 		return;
15290 
15291 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
15292 		nlmsg_free(msg);
15293 		return;
15294 	}
15295 
15296 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15297 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
15298 }
15299 
15300 static int nl80211_add_scan_req(struct sk_buff *msg,
15301 				struct cfg80211_registered_device *rdev)
15302 {
15303 	struct cfg80211_scan_request *req = rdev->scan_req;
15304 	struct nlattr *nest;
15305 	int i;
15306 
15307 	if (WARN_ON(!req))
15308 		return 0;
15309 
15310 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
15311 	if (!nest)
15312 		goto nla_put_failure;
15313 	for (i = 0; i < req->n_ssids; i++) {
15314 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
15315 			goto nla_put_failure;
15316 	}
15317 	nla_nest_end(msg, nest);
15318 
15319 	if (req->flags & NL80211_SCAN_FLAG_FREQ_KHZ) {
15320 		nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQ_KHZ);
15321 		if (!nest)
15322 			goto nla_put_failure;
15323 		for (i = 0; i < req->n_channels; i++) {
15324 			if (nla_put_u32(msg, i,
15325 				   ieee80211_channel_to_khz(req->channels[i])))
15326 				goto nla_put_failure;
15327 		}
15328 		nla_nest_end(msg, nest);
15329 	} else {
15330 		nest = nla_nest_start_noflag(msg,
15331 					     NL80211_ATTR_SCAN_FREQUENCIES);
15332 		if (!nest)
15333 			goto nla_put_failure;
15334 		for (i = 0; i < req->n_channels; i++) {
15335 			if (nla_put_u32(msg, i, req->channels[i]->center_freq))
15336 				goto nla_put_failure;
15337 		}
15338 		nla_nest_end(msg, nest);
15339 	}
15340 
15341 	if (req->ie &&
15342 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
15343 		goto nla_put_failure;
15344 
15345 	if (req->flags &&
15346 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
15347 		goto nla_put_failure;
15348 
15349 	if (req->info.scan_start_tsf &&
15350 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
15351 			       req->info.scan_start_tsf, NL80211_BSS_PAD) ||
15352 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
15353 		     req->info.tsf_bssid)))
15354 		goto nla_put_failure;
15355 
15356 	return 0;
15357  nla_put_failure:
15358 	return -ENOBUFS;
15359 }
15360 
15361 static int nl80211_prep_scan_msg(struct sk_buff *msg,
15362 				 struct cfg80211_registered_device *rdev,
15363 				 struct wireless_dev *wdev,
15364 				 u32 portid, u32 seq, int flags,
15365 				 u32 cmd)
15366 {
15367 	void *hdr;
15368 
15369 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
15370 	if (!hdr)
15371 		return -1;
15372 
15373 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15374 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15375 					 wdev->netdev->ifindex)) ||
15376 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15377 			      NL80211_ATTR_PAD))
15378 		goto nla_put_failure;
15379 
15380 	/* ignore errors and send incomplete event anyway */
15381 	nl80211_add_scan_req(msg, rdev);
15382 
15383 	genlmsg_end(msg, hdr);
15384 	return 0;
15385 
15386  nla_put_failure:
15387 	genlmsg_cancel(msg, hdr);
15388 	return -EMSGSIZE;
15389 }
15390 
15391 static int
15392 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
15393 			    struct cfg80211_sched_scan_request *req, u32 cmd)
15394 {
15395 	void *hdr;
15396 
15397 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15398 	if (!hdr)
15399 		return -1;
15400 
15401 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
15402 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
15403 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
15404 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
15405 			      NL80211_ATTR_PAD))
15406 		goto nla_put_failure;
15407 
15408 	genlmsg_end(msg, hdr);
15409 	return 0;
15410 
15411  nla_put_failure:
15412 	genlmsg_cancel(msg, hdr);
15413 	return -EMSGSIZE;
15414 }
15415 
15416 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
15417 			     struct wireless_dev *wdev)
15418 {
15419 	struct sk_buff *msg;
15420 
15421 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15422 	if (!msg)
15423 		return;
15424 
15425 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
15426 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
15427 		nlmsg_free(msg);
15428 		return;
15429 	}
15430 
15431 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15432 				NL80211_MCGRP_SCAN, GFP_KERNEL);
15433 }
15434 
15435 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
15436 				       struct wireless_dev *wdev, bool aborted)
15437 {
15438 	struct sk_buff *msg;
15439 
15440 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15441 	if (!msg)
15442 		return NULL;
15443 
15444 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
15445 				  aborted ? NL80211_CMD_SCAN_ABORTED :
15446 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
15447 		nlmsg_free(msg);
15448 		return NULL;
15449 	}
15450 
15451 	return msg;
15452 }
15453 
15454 /* send message created by nl80211_build_scan_msg() */
15455 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
15456 			   struct sk_buff *msg)
15457 {
15458 	if (!msg)
15459 		return;
15460 
15461 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15462 				NL80211_MCGRP_SCAN, GFP_KERNEL);
15463 }
15464 
15465 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
15466 {
15467 	struct sk_buff *msg;
15468 
15469 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15470 	if (!msg)
15471 		return;
15472 
15473 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
15474 		nlmsg_free(msg);
15475 		return;
15476 	}
15477 
15478 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
15479 				NL80211_MCGRP_SCAN, GFP_KERNEL);
15480 }
15481 
15482 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
15483 					  struct regulatory_request *request)
15484 {
15485 	/* Userspace can always count this one always being set */
15486 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
15487 		goto nla_put_failure;
15488 
15489 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
15490 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15491 			       NL80211_REGDOM_TYPE_WORLD))
15492 			goto nla_put_failure;
15493 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
15494 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15495 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
15496 			goto nla_put_failure;
15497 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
15498 		   request->intersect) {
15499 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15500 			       NL80211_REGDOM_TYPE_INTERSECTION))
15501 			goto nla_put_failure;
15502 	} else {
15503 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15504 			       NL80211_REGDOM_TYPE_COUNTRY) ||
15505 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
15506 				   request->alpha2))
15507 			goto nla_put_failure;
15508 	}
15509 
15510 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
15511 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
15512 
15513 		if (wiphy &&
15514 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
15515 			goto nla_put_failure;
15516 
15517 		if (wiphy &&
15518 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
15519 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
15520 			goto nla_put_failure;
15521 	}
15522 
15523 	return true;
15524 
15525 nla_put_failure:
15526 	return false;
15527 }
15528 
15529 /*
15530  * This can happen on global regulatory changes or device specific settings
15531  * based on custom regulatory domains.
15532  */
15533 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
15534 				     struct regulatory_request *request)
15535 {
15536 	struct sk_buff *msg;
15537 	void *hdr;
15538 
15539 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15540 	if (!msg)
15541 		return;
15542 
15543 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
15544 	if (!hdr)
15545 		goto nla_put_failure;
15546 
15547 	if (!nl80211_reg_change_event_fill(msg, request))
15548 		goto nla_put_failure;
15549 
15550 	genlmsg_end(msg, hdr);
15551 
15552 	rcu_read_lock();
15553 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
15554 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
15555 	rcu_read_unlock();
15556 
15557 	return;
15558 
15559 nla_put_failure:
15560 	nlmsg_free(msg);
15561 }
15562 
15563 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
15564 				    struct net_device *netdev,
15565 				    const u8 *buf, size_t len,
15566 				    enum nl80211_commands cmd, gfp_t gfp,
15567 				    int uapsd_queues, const u8 *req_ies,
15568 				    size_t req_ies_len)
15569 {
15570 	struct sk_buff *msg;
15571 	void *hdr;
15572 
15573 	msg = nlmsg_new(100 + len + req_ies_len, gfp);
15574 	if (!msg)
15575 		return;
15576 
15577 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15578 	if (!hdr) {
15579 		nlmsg_free(msg);
15580 		return;
15581 	}
15582 
15583 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15584 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15585 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15586 	    (req_ies &&
15587 	     nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
15588 		goto nla_put_failure;
15589 
15590 	if (uapsd_queues >= 0) {
15591 		struct nlattr *nla_wmm =
15592 			nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
15593 		if (!nla_wmm)
15594 			goto nla_put_failure;
15595 
15596 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
15597 			       uapsd_queues))
15598 			goto nla_put_failure;
15599 
15600 		nla_nest_end(msg, nla_wmm);
15601 	}
15602 
15603 	genlmsg_end(msg, hdr);
15604 
15605 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15606 				NL80211_MCGRP_MLME, gfp);
15607 	return;
15608 
15609  nla_put_failure:
15610 	nlmsg_free(msg);
15611 }
15612 
15613 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
15614 			  struct net_device *netdev, const u8 *buf,
15615 			  size_t len, gfp_t gfp)
15616 {
15617 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15618 				NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0);
15619 }
15620 
15621 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
15622 			   struct net_device *netdev, const u8 *buf,
15623 			   size_t len, gfp_t gfp, int uapsd_queues,
15624 			   const u8 *req_ies, size_t req_ies_len)
15625 {
15626 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15627 				NL80211_CMD_ASSOCIATE, gfp, uapsd_queues,
15628 				req_ies, req_ies_len);
15629 }
15630 
15631 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
15632 			 struct net_device *netdev, const u8 *buf,
15633 			 size_t len, gfp_t gfp)
15634 {
15635 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15636 				NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0);
15637 }
15638 
15639 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
15640 			   struct net_device *netdev, const u8 *buf,
15641 			   size_t len, gfp_t gfp)
15642 {
15643 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15644 				NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0);
15645 }
15646 
15647 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
15648 				  size_t len)
15649 {
15650 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15651 	struct wiphy *wiphy = wdev->wiphy;
15652 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15653 	const struct ieee80211_mgmt *mgmt = (void *)buf;
15654 	u32 cmd;
15655 
15656 	if (WARN_ON(len < 2))
15657 		return;
15658 
15659 	if (ieee80211_is_deauth(mgmt->frame_control)) {
15660 		cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
15661 	} else if (ieee80211_is_disassoc(mgmt->frame_control)) {
15662 		cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
15663 	} else if (ieee80211_is_beacon(mgmt->frame_control)) {
15664 		if (wdev->unprot_beacon_reported &&
15665 		    elapsed_jiffies_msecs(wdev->unprot_beacon_reported) < 10000)
15666 			return;
15667 		cmd = NL80211_CMD_UNPROT_BEACON;
15668 		wdev->unprot_beacon_reported = jiffies;
15669 	} else {
15670 		return;
15671 	}
15672 
15673 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
15674 	nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
15675 				NULL, 0);
15676 }
15677 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
15678 
15679 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
15680 				      struct net_device *netdev, int cmd,
15681 				      const u8 *addr, gfp_t gfp)
15682 {
15683 	struct sk_buff *msg;
15684 	void *hdr;
15685 
15686 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15687 	if (!msg)
15688 		return;
15689 
15690 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15691 	if (!hdr) {
15692 		nlmsg_free(msg);
15693 		return;
15694 	}
15695 
15696 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15697 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15698 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
15699 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
15700 		goto nla_put_failure;
15701 
15702 	genlmsg_end(msg, hdr);
15703 
15704 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15705 				NL80211_MCGRP_MLME, gfp);
15706 	return;
15707 
15708  nla_put_failure:
15709 	nlmsg_free(msg);
15710 }
15711 
15712 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
15713 			       struct net_device *netdev, const u8 *addr,
15714 			       gfp_t gfp)
15715 {
15716 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
15717 				  addr, gfp);
15718 }
15719 
15720 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
15721 				struct net_device *netdev, const u8 *addr,
15722 				gfp_t gfp)
15723 {
15724 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
15725 				  addr, gfp);
15726 }
15727 
15728 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
15729 				 struct net_device *netdev,
15730 				 struct cfg80211_connect_resp_params *cr,
15731 				 gfp_t gfp)
15732 {
15733 	struct sk_buff *msg;
15734 	void *hdr;
15735 
15736 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
15737 			cr->fils.kek_len + cr->fils.pmk_len +
15738 			(cr->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
15739 	if (!msg)
15740 		return;
15741 
15742 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
15743 	if (!hdr) {
15744 		nlmsg_free(msg);
15745 		return;
15746 	}
15747 
15748 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15749 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15750 	    (cr->bssid &&
15751 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, cr->bssid)) ||
15752 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
15753 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
15754 			cr->status) ||
15755 	    (cr->status < 0 &&
15756 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
15757 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
15758 			  cr->timeout_reason))) ||
15759 	    (cr->req_ie &&
15760 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
15761 	    (cr->resp_ie &&
15762 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
15763 		     cr->resp_ie)) ||
15764 	    (cr->fils.update_erp_next_seq_num &&
15765 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
15766 			 cr->fils.erp_next_seq_num)) ||
15767 	    (cr->status == WLAN_STATUS_SUCCESS &&
15768 	     ((cr->fils.kek &&
15769 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
15770 		       cr->fils.kek)) ||
15771 	      (cr->fils.pmk &&
15772 	       nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
15773 	      (cr->fils.pmkid &&
15774 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
15775 		goto nla_put_failure;
15776 
15777 	genlmsg_end(msg, hdr);
15778 
15779 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15780 				NL80211_MCGRP_MLME, gfp);
15781 	return;
15782 
15783  nla_put_failure:
15784 	nlmsg_free(msg);
15785 }
15786 
15787 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
15788 			 struct net_device *netdev,
15789 			 struct cfg80211_roam_info *info, gfp_t gfp)
15790 {
15791 	struct sk_buff *msg;
15792 	void *hdr;
15793 	const u8 *bssid = info->bss ? info->bss->bssid : info->bssid;
15794 
15795 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
15796 			info->fils.kek_len + info->fils.pmk_len +
15797 			(info->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
15798 	if (!msg)
15799 		return;
15800 
15801 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
15802 	if (!hdr) {
15803 		nlmsg_free(msg);
15804 		return;
15805 	}
15806 
15807 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15808 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15809 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
15810 	    (info->req_ie &&
15811 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
15812 		     info->req_ie)) ||
15813 	    (info->resp_ie &&
15814 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
15815 		     info->resp_ie)) ||
15816 	    (info->fils.update_erp_next_seq_num &&
15817 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
15818 			 info->fils.erp_next_seq_num)) ||
15819 	    (info->fils.kek &&
15820 	     nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
15821 		     info->fils.kek)) ||
15822 	    (info->fils.pmk &&
15823 	     nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
15824 	    (info->fils.pmkid &&
15825 	     nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
15826 		goto nla_put_failure;
15827 
15828 	genlmsg_end(msg, hdr);
15829 
15830 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15831 				NL80211_MCGRP_MLME, gfp);
15832 	return;
15833 
15834  nla_put_failure:
15835 	nlmsg_free(msg);
15836 }
15837 
15838 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
15839 				  struct net_device *netdev, const u8 *bssid)
15840 {
15841 	struct sk_buff *msg;
15842 	void *hdr;
15843 
15844 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15845 	if (!msg)
15846 		return;
15847 
15848 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
15849 	if (!hdr) {
15850 		nlmsg_free(msg);
15851 		return;
15852 	}
15853 
15854 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15855 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15856 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
15857 		goto nla_put_failure;
15858 
15859 	genlmsg_end(msg, hdr);
15860 
15861 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15862 				NL80211_MCGRP_MLME, GFP_KERNEL);
15863 	return;
15864 
15865  nla_put_failure:
15866 	nlmsg_free(msg);
15867 }
15868 
15869 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
15870 			       struct net_device *netdev, u16 reason,
15871 			       const u8 *ie, size_t ie_len, bool from_ap)
15872 {
15873 	struct sk_buff *msg;
15874 	void *hdr;
15875 
15876 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
15877 	if (!msg)
15878 		return;
15879 
15880 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
15881 	if (!hdr) {
15882 		nlmsg_free(msg);
15883 		return;
15884 	}
15885 
15886 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15887 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15888 	    (reason &&
15889 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
15890 	    (from_ap &&
15891 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
15892 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
15893 		goto nla_put_failure;
15894 
15895 	genlmsg_end(msg, hdr);
15896 
15897 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15898 				NL80211_MCGRP_MLME, GFP_KERNEL);
15899 	return;
15900 
15901  nla_put_failure:
15902 	nlmsg_free(msg);
15903 }
15904 
15905 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
15906 			     struct net_device *netdev, const u8 *bssid,
15907 			     gfp_t gfp)
15908 {
15909 	struct sk_buff *msg;
15910 	void *hdr;
15911 
15912 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15913 	if (!msg)
15914 		return;
15915 
15916 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
15917 	if (!hdr) {
15918 		nlmsg_free(msg);
15919 		return;
15920 	}
15921 
15922 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15923 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15924 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
15925 		goto nla_put_failure;
15926 
15927 	genlmsg_end(msg, hdr);
15928 
15929 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15930 				NL80211_MCGRP_MLME, gfp);
15931 	return;
15932 
15933  nla_put_failure:
15934 	nlmsg_free(msg);
15935 }
15936 
15937 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
15938 					const u8 *ie, u8 ie_len,
15939 					int sig_dbm, gfp_t gfp)
15940 {
15941 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15942 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15943 	struct sk_buff *msg;
15944 	void *hdr;
15945 
15946 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
15947 		return;
15948 
15949 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
15950 
15951 	msg = nlmsg_new(100 + ie_len, gfp);
15952 	if (!msg)
15953 		return;
15954 
15955 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
15956 	if (!hdr) {
15957 		nlmsg_free(msg);
15958 		return;
15959 	}
15960 
15961 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15962 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15963 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
15964 	    (ie_len && ie &&
15965 	     nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
15966 	    (sig_dbm &&
15967 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
15968 		goto nla_put_failure;
15969 
15970 	genlmsg_end(msg, hdr);
15971 
15972 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15973 				NL80211_MCGRP_MLME, gfp);
15974 	return;
15975 
15976  nla_put_failure:
15977 	nlmsg_free(msg);
15978 }
15979 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
15980 
15981 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
15982 				 struct net_device *netdev, const u8 *addr,
15983 				 enum nl80211_key_type key_type, int key_id,
15984 				 const u8 *tsc, gfp_t gfp)
15985 {
15986 	struct sk_buff *msg;
15987 	void *hdr;
15988 
15989 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15990 	if (!msg)
15991 		return;
15992 
15993 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
15994 	if (!hdr) {
15995 		nlmsg_free(msg);
15996 		return;
15997 	}
15998 
15999 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16000 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16001 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
16002 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
16003 	    (key_id != -1 &&
16004 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
16005 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
16006 		goto nla_put_failure;
16007 
16008 	genlmsg_end(msg, hdr);
16009 
16010 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16011 				NL80211_MCGRP_MLME, gfp);
16012 	return;
16013 
16014  nla_put_failure:
16015 	nlmsg_free(msg);
16016 }
16017 
16018 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
16019 				    struct ieee80211_channel *channel_before,
16020 				    struct ieee80211_channel *channel_after)
16021 {
16022 	struct sk_buff *msg;
16023 	void *hdr;
16024 	struct nlattr *nl_freq;
16025 
16026 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
16027 	if (!msg)
16028 		return;
16029 
16030 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
16031 	if (!hdr) {
16032 		nlmsg_free(msg);
16033 		return;
16034 	}
16035 
16036 	/*
16037 	 * Since we are applying the beacon hint to a wiphy we know its
16038 	 * wiphy_idx is valid
16039 	 */
16040 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
16041 		goto nla_put_failure;
16042 
16043 	/* Before */
16044 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
16045 	if (!nl_freq)
16046 		goto nla_put_failure;
16047 
16048 	if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
16049 		goto nla_put_failure;
16050 	nla_nest_end(msg, nl_freq);
16051 
16052 	/* After */
16053 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
16054 	if (!nl_freq)
16055 		goto nla_put_failure;
16056 
16057 	if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
16058 		goto nla_put_failure;
16059 	nla_nest_end(msg, nl_freq);
16060 
16061 	genlmsg_end(msg, hdr);
16062 
16063 	rcu_read_lock();
16064 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
16065 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
16066 	rcu_read_unlock();
16067 
16068 	return;
16069 
16070 nla_put_failure:
16071 	nlmsg_free(msg);
16072 }
16073 
16074 static void nl80211_send_remain_on_chan_event(
16075 	int cmd, struct cfg80211_registered_device *rdev,
16076 	struct wireless_dev *wdev, u64 cookie,
16077 	struct ieee80211_channel *chan,
16078 	unsigned int duration, gfp_t gfp)
16079 {
16080 	struct sk_buff *msg;
16081 	void *hdr;
16082 
16083 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16084 	if (!msg)
16085 		return;
16086 
16087 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
16088 	if (!hdr) {
16089 		nlmsg_free(msg);
16090 		return;
16091 	}
16092 
16093 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16094 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
16095 					 wdev->netdev->ifindex)) ||
16096 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16097 			      NL80211_ATTR_PAD) ||
16098 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
16099 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
16100 			NL80211_CHAN_NO_HT) ||
16101 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
16102 			      NL80211_ATTR_PAD))
16103 		goto nla_put_failure;
16104 
16105 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
16106 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
16107 		goto nla_put_failure;
16108 
16109 	genlmsg_end(msg, hdr);
16110 
16111 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16112 				NL80211_MCGRP_MLME, gfp);
16113 	return;
16114 
16115  nla_put_failure:
16116 	nlmsg_free(msg);
16117 }
16118 
16119 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
16120 			       struct ieee80211_channel *chan,
16121 			       unsigned int duration, gfp_t gfp)
16122 {
16123 	struct wiphy *wiphy = wdev->wiphy;
16124 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16125 
16126 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
16127 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
16128 					  rdev, wdev, cookie, chan,
16129 					  duration, gfp);
16130 }
16131 EXPORT_SYMBOL(cfg80211_ready_on_channel);
16132 
16133 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
16134 					struct ieee80211_channel *chan,
16135 					gfp_t gfp)
16136 {
16137 	struct wiphy *wiphy = wdev->wiphy;
16138 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16139 
16140 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
16141 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
16142 					  rdev, wdev, cookie, chan, 0, gfp);
16143 }
16144 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
16145 
16146 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
16147 					struct ieee80211_channel *chan,
16148 					gfp_t gfp)
16149 {
16150 	struct wiphy *wiphy = wdev->wiphy;
16151 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16152 
16153 	trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
16154 	nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
16155 					  rdev, wdev, cookie, chan, 0, gfp);
16156 }
16157 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
16158 
16159 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
16160 		      struct station_info *sinfo, gfp_t gfp)
16161 {
16162 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
16163 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16164 	struct sk_buff *msg;
16165 
16166 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
16167 
16168 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16169 	if (!msg)
16170 		return;
16171 
16172 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
16173 				 rdev, dev, mac_addr, sinfo) < 0) {
16174 		nlmsg_free(msg);
16175 		return;
16176 	}
16177 
16178 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16179 				NL80211_MCGRP_MLME, gfp);
16180 }
16181 EXPORT_SYMBOL(cfg80211_new_sta);
16182 
16183 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
16184 			    struct station_info *sinfo, gfp_t gfp)
16185 {
16186 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
16187 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16188 	struct sk_buff *msg;
16189 	struct station_info empty_sinfo = {};
16190 
16191 	if (!sinfo)
16192 		sinfo = &empty_sinfo;
16193 
16194 	trace_cfg80211_del_sta(dev, mac_addr);
16195 
16196 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16197 	if (!msg) {
16198 		cfg80211_sinfo_release_content(sinfo);
16199 		return;
16200 	}
16201 
16202 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
16203 				 rdev, dev, mac_addr, sinfo) < 0) {
16204 		nlmsg_free(msg);
16205 		return;
16206 	}
16207 
16208 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16209 				NL80211_MCGRP_MLME, gfp);
16210 }
16211 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
16212 
16213 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
16214 			  enum nl80211_connect_failed_reason reason,
16215 			  gfp_t gfp)
16216 {
16217 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
16218 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16219 	struct sk_buff *msg;
16220 	void *hdr;
16221 
16222 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
16223 	if (!msg)
16224 		return;
16225 
16226 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
16227 	if (!hdr) {
16228 		nlmsg_free(msg);
16229 		return;
16230 	}
16231 
16232 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16233 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
16234 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
16235 		goto nla_put_failure;
16236 
16237 	genlmsg_end(msg, hdr);
16238 
16239 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16240 				NL80211_MCGRP_MLME, gfp);
16241 	return;
16242 
16243  nla_put_failure:
16244 	nlmsg_free(msg);
16245 }
16246 EXPORT_SYMBOL(cfg80211_conn_failed);
16247 
16248 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
16249 				       const u8 *addr, gfp_t gfp)
16250 {
16251 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16252 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16253 	struct sk_buff *msg;
16254 	void *hdr;
16255 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
16256 
16257 	if (!nlportid)
16258 		return false;
16259 
16260 	msg = nlmsg_new(100, gfp);
16261 	if (!msg)
16262 		return true;
16263 
16264 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
16265 	if (!hdr) {
16266 		nlmsg_free(msg);
16267 		return true;
16268 	}
16269 
16270 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16271 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16272 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
16273 		goto nla_put_failure;
16274 
16275 	genlmsg_end(msg, hdr);
16276 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
16277 	return true;
16278 
16279  nla_put_failure:
16280 	nlmsg_free(msg);
16281 	return true;
16282 }
16283 
16284 bool cfg80211_rx_spurious_frame(struct net_device *dev,
16285 				const u8 *addr, gfp_t gfp)
16286 {
16287 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16288 	bool ret;
16289 
16290 	trace_cfg80211_rx_spurious_frame(dev, addr);
16291 
16292 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
16293 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
16294 		trace_cfg80211_return_bool(false);
16295 		return false;
16296 	}
16297 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
16298 					 addr, gfp);
16299 	trace_cfg80211_return_bool(ret);
16300 	return ret;
16301 }
16302 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
16303 
16304 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
16305 					const u8 *addr, gfp_t gfp)
16306 {
16307 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16308 	bool ret;
16309 
16310 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
16311 
16312 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
16313 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
16314 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
16315 		trace_cfg80211_return_bool(false);
16316 		return false;
16317 	}
16318 	ret = __nl80211_unexpected_frame(dev,
16319 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
16320 					 addr, gfp);
16321 	trace_cfg80211_return_bool(ret);
16322 	return ret;
16323 }
16324 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
16325 
16326 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
16327 		      struct wireless_dev *wdev, u32 nlportid,
16328 		      int freq, int sig_dbm,
16329 		      const u8 *buf, size_t len, u32 flags, gfp_t gfp)
16330 {
16331 	struct net_device *netdev = wdev->netdev;
16332 	struct sk_buff *msg;
16333 	void *hdr;
16334 
16335 	msg = nlmsg_new(100 + len, gfp);
16336 	if (!msg)
16337 		return -ENOMEM;
16338 
16339 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
16340 	if (!hdr) {
16341 		nlmsg_free(msg);
16342 		return -ENOMEM;
16343 	}
16344 
16345 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16346 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
16347 					netdev->ifindex)) ||
16348 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16349 			      NL80211_ATTR_PAD) ||
16350 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, KHZ_TO_MHZ(freq)) ||
16351 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET, freq % 1000) ||
16352 	    (sig_dbm &&
16353 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
16354 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
16355 	    (flags &&
16356 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
16357 		goto nla_put_failure;
16358 
16359 	genlmsg_end(msg, hdr);
16360 
16361 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
16362 
16363  nla_put_failure:
16364 	nlmsg_free(msg);
16365 	return -ENOBUFS;
16366 }
16367 
16368 static void nl80211_frame_tx_status(struct wireless_dev *wdev, u64 cookie,
16369 				    const u8 *buf, size_t len, bool ack,
16370 				    gfp_t gfp, enum nl80211_commands command)
16371 {
16372 	struct wiphy *wiphy = wdev->wiphy;
16373 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16374 	struct net_device *netdev = wdev->netdev;
16375 	struct sk_buff *msg;
16376 	void *hdr;
16377 
16378 	if (command == NL80211_CMD_FRAME_TX_STATUS)
16379 		trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
16380 	else
16381 		trace_cfg80211_control_port_tx_status(wdev, cookie, ack);
16382 
16383 	msg = nlmsg_new(100 + len, gfp);
16384 	if (!msg)
16385 		return;
16386 
16387 	hdr = nl80211hdr_put(msg, 0, 0, 0, command);
16388 	if (!hdr) {
16389 		nlmsg_free(msg);
16390 		return;
16391 	}
16392 
16393 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16394 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
16395 				   netdev->ifindex)) ||
16396 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16397 			      NL80211_ATTR_PAD) ||
16398 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
16399 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
16400 			      NL80211_ATTR_PAD) ||
16401 	    (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
16402 		goto nla_put_failure;
16403 
16404 	genlmsg_end(msg, hdr);
16405 
16406 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16407 				NL80211_MCGRP_MLME, gfp);
16408 	return;
16409 
16410 nla_put_failure:
16411 	nlmsg_free(msg);
16412 }
16413 
16414 void cfg80211_control_port_tx_status(struct wireless_dev *wdev, u64 cookie,
16415 				     const u8 *buf, size_t len, bool ack,
16416 				     gfp_t gfp)
16417 {
16418 	nl80211_frame_tx_status(wdev, cookie, buf, len, ack, gfp,
16419 				NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS);
16420 }
16421 EXPORT_SYMBOL(cfg80211_control_port_tx_status);
16422 
16423 void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
16424 			     const u8 *buf, size_t len, bool ack, gfp_t gfp)
16425 {
16426 	nl80211_frame_tx_status(wdev, cookie, buf, len, ack, gfp,
16427 				NL80211_CMD_FRAME_TX_STATUS);
16428 }
16429 EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
16430 
16431 static int __nl80211_rx_control_port(struct net_device *dev,
16432 				     struct sk_buff *skb,
16433 				     bool unencrypted, gfp_t gfp)
16434 {
16435 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16436 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16437 	struct ethhdr *ehdr = eth_hdr(skb);
16438 	const u8 *addr = ehdr->h_source;
16439 	u16 proto = be16_to_cpu(skb->protocol);
16440 	struct sk_buff *msg;
16441 	void *hdr;
16442 	struct nlattr *frame;
16443 
16444 	u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
16445 
16446 	if (!nlportid)
16447 		return -ENOENT;
16448 
16449 	msg = nlmsg_new(100 + skb->len, gfp);
16450 	if (!msg)
16451 		return -ENOMEM;
16452 
16453 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
16454 	if (!hdr) {
16455 		nlmsg_free(msg);
16456 		return -ENOBUFS;
16457 	}
16458 
16459 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16460 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16461 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16462 			      NL80211_ATTR_PAD) ||
16463 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
16464 	    nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
16465 	    (unencrypted && nla_put_flag(msg,
16466 					 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
16467 		goto nla_put_failure;
16468 
16469 	frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
16470 	if (!frame)
16471 		goto nla_put_failure;
16472 
16473 	skb_copy_bits(skb, 0, nla_data(frame), skb->len);
16474 	genlmsg_end(msg, hdr);
16475 
16476 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
16477 
16478  nla_put_failure:
16479 	nlmsg_free(msg);
16480 	return -ENOBUFS;
16481 }
16482 
16483 bool cfg80211_rx_control_port(struct net_device *dev,
16484 			      struct sk_buff *skb, bool unencrypted)
16485 {
16486 	int ret;
16487 
16488 	trace_cfg80211_rx_control_port(dev, skb, unencrypted);
16489 	ret = __nl80211_rx_control_port(dev, skb, unencrypted, GFP_ATOMIC);
16490 	trace_cfg80211_return_bool(ret == 0);
16491 	return ret == 0;
16492 }
16493 EXPORT_SYMBOL(cfg80211_rx_control_port);
16494 
16495 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
16496 					    const char *mac, gfp_t gfp)
16497 {
16498 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16499 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16500 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16501 	void **cb;
16502 
16503 	if (!msg)
16504 		return NULL;
16505 
16506 	cb = (void **)msg->cb;
16507 
16508 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
16509 	if (!cb[0]) {
16510 		nlmsg_free(msg);
16511 		return NULL;
16512 	}
16513 
16514 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16515 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
16516 		goto nla_put_failure;
16517 
16518 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
16519 		goto nla_put_failure;
16520 
16521 	cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
16522 	if (!cb[1])
16523 		goto nla_put_failure;
16524 
16525 	cb[2] = rdev;
16526 
16527 	return msg;
16528  nla_put_failure:
16529 	nlmsg_free(msg);
16530 	return NULL;
16531 }
16532 
16533 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
16534 {
16535 	void **cb = (void **)msg->cb;
16536 	struct cfg80211_registered_device *rdev = cb[2];
16537 
16538 	nla_nest_end(msg, cb[1]);
16539 	genlmsg_end(msg, cb[0]);
16540 
16541 	memset(msg->cb, 0, sizeof(msg->cb));
16542 
16543 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16544 				NL80211_MCGRP_MLME, gfp);
16545 }
16546 
16547 void cfg80211_cqm_rssi_notify(struct net_device *dev,
16548 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
16549 			      s32 rssi_level, gfp_t gfp)
16550 {
16551 	struct sk_buff *msg;
16552 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16553 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16554 
16555 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
16556 
16557 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
16558 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
16559 		return;
16560 
16561 	if (wdev->cqm_config) {
16562 		wdev->cqm_config->last_rssi_event_value = rssi_level;
16563 
16564 		cfg80211_cqm_rssi_update(rdev, dev);
16565 
16566 		if (rssi_level == 0)
16567 			rssi_level = wdev->cqm_config->last_rssi_event_value;
16568 	}
16569 
16570 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
16571 	if (!msg)
16572 		return;
16573 
16574 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
16575 			rssi_event))
16576 		goto nla_put_failure;
16577 
16578 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
16579 				      rssi_level))
16580 		goto nla_put_failure;
16581 
16582 	cfg80211_send_cqm(msg, gfp);
16583 
16584 	return;
16585 
16586  nla_put_failure:
16587 	nlmsg_free(msg);
16588 }
16589 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
16590 
16591 void cfg80211_cqm_txe_notify(struct net_device *dev,
16592 			     const u8 *peer, u32 num_packets,
16593 			     u32 rate, u32 intvl, gfp_t gfp)
16594 {
16595 	struct sk_buff *msg;
16596 
16597 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
16598 	if (!msg)
16599 		return;
16600 
16601 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
16602 		goto nla_put_failure;
16603 
16604 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
16605 		goto nla_put_failure;
16606 
16607 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
16608 		goto nla_put_failure;
16609 
16610 	cfg80211_send_cqm(msg, gfp);
16611 	return;
16612 
16613  nla_put_failure:
16614 	nlmsg_free(msg);
16615 }
16616 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
16617 
16618 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
16619 				 const u8 *peer, u32 num_packets, gfp_t gfp)
16620 {
16621 	struct sk_buff *msg;
16622 
16623 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
16624 
16625 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
16626 	if (!msg)
16627 		return;
16628 
16629 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
16630 		goto nla_put_failure;
16631 
16632 	cfg80211_send_cqm(msg, gfp);
16633 	return;
16634 
16635  nla_put_failure:
16636 	nlmsg_free(msg);
16637 }
16638 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
16639 
16640 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
16641 {
16642 	struct sk_buff *msg;
16643 
16644 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
16645 	if (!msg)
16646 		return;
16647 
16648 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
16649 		goto nla_put_failure;
16650 
16651 	cfg80211_send_cqm(msg, gfp);
16652 	return;
16653 
16654  nla_put_failure:
16655 	nlmsg_free(msg);
16656 }
16657 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
16658 
16659 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
16660 				     struct net_device *netdev, const u8 *bssid,
16661 				     const u8 *replay_ctr, gfp_t gfp)
16662 {
16663 	struct sk_buff *msg;
16664 	struct nlattr *rekey_attr;
16665 	void *hdr;
16666 
16667 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16668 	if (!msg)
16669 		return;
16670 
16671 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
16672 	if (!hdr) {
16673 		nlmsg_free(msg);
16674 		return;
16675 	}
16676 
16677 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16678 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16679 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
16680 		goto nla_put_failure;
16681 
16682 	rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
16683 	if (!rekey_attr)
16684 		goto nla_put_failure;
16685 
16686 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
16687 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
16688 		goto nla_put_failure;
16689 
16690 	nla_nest_end(msg, rekey_attr);
16691 
16692 	genlmsg_end(msg, hdr);
16693 
16694 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16695 				NL80211_MCGRP_MLME, gfp);
16696 	return;
16697 
16698  nla_put_failure:
16699 	nlmsg_free(msg);
16700 }
16701 
16702 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
16703 			       const u8 *replay_ctr, gfp_t gfp)
16704 {
16705 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16706 	struct wiphy *wiphy = wdev->wiphy;
16707 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16708 
16709 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
16710 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
16711 }
16712 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
16713 
16714 static void
16715 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
16716 			       struct net_device *netdev, int index,
16717 			       const u8 *bssid, bool preauth, gfp_t gfp)
16718 {
16719 	struct sk_buff *msg;
16720 	struct nlattr *attr;
16721 	void *hdr;
16722 
16723 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16724 	if (!msg)
16725 		return;
16726 
16727 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
16728 	if (!hdr) {
16729 		nlmsg_free(msg);
16730 		return;
16731 	}
16732 
16733 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16734 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
16735 		goto nla_put_failure;
16736 
16737 	attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
16738 	if (!attr)
16739 		goto nla_put_failure;
16740 
16741 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
16742 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
16743 	    (preauth &&
16744 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
16745 		goto nla_put_failure;
16746 
16747 	nla_nest_end(msg, attr);
16748 
16749 	genlmsg_end(msg, hdr);
16750 
16751 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16752 				NL80211_MCGRP_MLME, gfp);
16753 	return;
16754 
16755  nla_put_failure:
16756 	nlmsg_free(msg);
16757 }
16758 
16759 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
16760 				     const u8 *bssid, bool preauth, gfp_t gfp)
16761 {
16762 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16763 	struct wiphy *wiphy = wdev->wiphy;
16764 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16765 
16766 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
16767 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
16768 }
16769 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
16770 
16771 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
16772 				     struct net_device *netdev,
16773 				     struct cfg80211_chan_def *chandef,
16774 				     gfp_t gfp,
16775 				     enum nl80211_commands notif,
16776 				     u8 count)
16777 {
16778 	struct sk_buff *msg;
16779 	void *hdr;
16780 
16781 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16782 	if (!msg)
16783 		return;
16784 
16785 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
16786 	if (!hdr) {
16787 		nlmsg_free(msg);
16788 		return;
16789 	}
16790 
16791 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
16792 		goto nla_put_failure;
16793 
16794 	if (nl80211_send_chandef(msg, chandef))
16795 		goto nla_put_failure;
16796 
16797 	if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
16798 	    (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
16799 			goto nla_put_failure;
16800 
16801 	genlmsg_end(msg, hdr);
16802 
16803 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16804 				NL80211_MCGRP_MLME, gfp);
16805 	return;
16806 
16807  nla_put_failure:
16808 	nlmsg_free(msg);
16809 }
16810 
16811 void cfg80211_ch_switch_notify(struct net_device *dev,
16812 			       struct cfg80211_chan_def *chandef)
16813 {
16814 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16815 	struct wiphy *wiphy = wdev->wiphy;
16816 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16817 
16818 	ASSERT_WDEV_LOCK(wdev);
16819 
16820 	trace_cfg80211_ch_switch_notify(dev, chandef);
16821 
16822 	wdev->chandef = *chandef;
16823 	wdev->preset_chandef = *chandef;
16824 
16825 	if (wdev->iftype == NL80211_IFTYPE_STATION &&
16826 	    !WARN_ON(!wdev->current_bss))
16827 		cfg80211_update_assoc_bss_entry(wdev, chandef->chan);
16828 
16829 	cfg80211_sched_dfs_chan_update(rdev);
16830 
16831 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
16832 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0);
16833 }
16834 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
16835 
16836 void cfg80211_ch_switch_started_notify(struct net_device *dev,
16837 				       struct cfg80211_chan_def *chandef,
16838 				       u8 count)
16839 {
16840 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16841 	struct wiphy *wiphy = wdev->wiphy;
16842 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16843 
16844 	trace_cfg80211_ch_switch_started_notify(dev, chandef);
16845 
16846 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
16847 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
16848 }
16849 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
16850 
16851 void
16852 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
16853 		     const struct cfg80211_chan_def *chandef,
16854 		     enum nl80211_radar_event event,
16855 		     struct net_device *netdev, gfp_t gfp)
16856 {
16857 	struct sk_buff *msg;
16858 	void *hdr;
16859 
16860 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16861 	if (!msg)
16862 		return;
16863 
16864 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
16865 	if (!hdr) {
16866 		nlmsg_free(msg);
16867 		return;
16868 	}
16869 
16870 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
16871 		goto nla_put_failure;
16872 
16873 	/* NOP and radar events don't need a netdev parameter */
16874 	if (netdev) {
16875 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
16876 
16877 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16878 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16879 				      NL80211_ATTR_PAD))
16880 			goto nla_put_failure;
16881 	}
16882 
16883 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
16884 		goto nla_put_failure;
16885 
16886 	if (nl80211_send_chandef(msg, chandef))
16887 		goto nla_put_failure;
16888 
16889 	genlmsg_end(msg, hdr);
16890 
16891 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16892 				NL80211_MCGRP_MLME, gfp);
16893 	return;
16894 
16895  nla_put_failure:
16896 	nlmsg_free(msg);
16897 }
16898 
16899 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
16900 				       struct sta_opmode_info *sta_opmode,
16901 				       gfp_t gfp)
16902 {
16903 	struct sk_buff *msg;
16904 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16905 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16906 	void *hdr;
16907 
16908 	if (WARN_ON(!mac))
16909 		return;
16910 
16911 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16912 	if (!msg)
16913 		return;
16914 
16915 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
16916 	if (!hdr) {
16917 		nlmsg_free(msg);
16918 		return;
16919 	}
16920 
16921 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
16922 		goto nla_put_failure;
16923 
16924 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
16925 		goto nla_put_failure;
16926 
16927 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
16928 		goto nla_put_failure;
16929 
16930 	if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
16931 	    nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
16932 		goto nla_put_failure;
16933 
16934 	if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
16935 	    nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
16936 		goto nla_put_failure;
16937 
16938 	if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
16939 	    nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
16940 		goto nla_put_failure;
16941 
16942 	genlmsg_end(msg, hdr);
16943 
16944 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16945 				NL80211_MCGRP_MLME, gfp);
16946 
16947 	return;
16948 
16949 nla_put_failure:
16950 	nlmsg_free(msg);
16951 }
16952 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
16953 
16954 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
16955 			   u64 cookie, bool acked, s32 ack_signal,
16956 			   bool is_valid_ack_signal, gfp_t gfp)
16957 {
16958 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16959 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16960 	struct sk_buff *msg;
16961 	void *hdr;
16962 
16963 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
16964 
16965 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16966 
16967 	if (!msg)
16968 		return;
16969 
16970 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
16971 	if (!hdr) {
16972 		nlmsg_free(msg);
16973 		return;
16974 	}
16975 
16976 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16977 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16978 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
16979 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
16980 			      NL80211_ATTR_PAD) ||
16981 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
16982 	    (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
16983 						ack_signal)))
16984 		goto nla_put_failure;
16985 
16986 	genlmsg_end(msg, hdr);
16987 
16988 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16989 				NL80211_MCGRP_MLME, gfp);
16990 	return;
16991 
16992  nla_put_failure:
16993 	nlmsg_free(msg);
16994 }
16995 EXPORT_SYMBOL(cfg80211_probe_status);
16996 
16997 void cfg80211_report_obss_beacon_khz(struct wiphy *wiphy, const u8 *frame,
16998 				     size_t len, int freq, int sig_dbm)
16999 {
17000 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17001 	struct sk_buff *msg;
17002 	void *hdr;
17003 	struct cfg80211_beacon_registration *reg;
17004 
17005 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
17006 
17007 	spin_lock_bh(&rdev->beacon_registrations_lock);
17008 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
17009 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
17010 		if (!msg) {
17011 			spin_unlock_bh(&rdev->beacon_registrations_lock);
17012 			return;
17013 		}
17014 
17015 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
17016 		if (!hdr)
17017 			goto nla_put_failure;
17018 
17019 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17020 		    (freq &&
17021 		     (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
17022 				  KHZ_TO_MHZ(freq)) ||
17023 		      nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
17024 				  freq % 1000))) ||
17025 		    (sig_dbm &&
17026 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
17027 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
17028 			goto nla_put_failure;
17029 
17030 		genlmsg_end(msg, hdr);
17031 
17032 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
17033 	}
17034 	spin_unlock_bh(&rdev->beacon_registrations_lock);
17035 	return;
17036 
17037  nla_put_failure:
17038 	spin_unlock_bh(&rdev->beacon_registrations_lock);
17039 	nlmsg_free(msg);
17040 }
17041 EXPORT_SYMBOL(cfg80211_report_obss_beacon_khz);
17042 
17043 #ifdef CONFIG_PM
17044 static int cfg80211_net_detect_results(struct sk_buff *msg,
17045 				       struct cfg80211_wowlan_wakeup *wakeup)
17046 {
17047 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
17048 	struct nlattr *nl_results, *nl_match, *nl_freqs;
17049 	int i, j;
17050 
17051 	nl_results = nla_nest_start_noflag(msg,
17052 					   NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
17053 	if (!nl_results)
17054 		return -EMSGSIZE;
17055 
17056 	for (i = 0; i < nd->n_matches; i++) {
17057 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
17058 
17059 		nl_match = nla_nest_start_noflag(msg, i);
17060 		if (!nl_match)
17061 			break;
17062 
17063 		/* The SSID attribute is optional in nl80211, but for
17064 		 * simplicity reasons it's always present in the
17065 		 * cfg80211 structure.  If a driver can't pass the
17066 		 * SSID, that needs to be changed.  A zero length SSID
17067 		 * is still a valid SSID (wildcard), so it cannot be
17068 		 * used for this purpose.
17069 		 */
17070 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
17071 			    match->ssid.ssid)) {
17072 			nla_nest_cancel(msg, nl_match);
17073 			goto out;
17074 		}
17075 
17076 		if (match->n_channels) {
17077 			nl_freqs = nla_nest_start_noflag(msg,
17078 							 NL80211_ATTR_SCAN_FREQUENCIES);
17079 			if (!nl_freqs) {
17080 				nla_nest_cancel(msg, nl_match);
17081 				goto out;
17082 			}
17083 
17084 			for (j = 0; j < match->n_channels; j++) {
17085 				if (nla_put_u32(msg, j, match->channels[j])) {
17086 					nla_nest_cancel(msg, nl_freqs);
17087 					nla_nest_cancel(msg, nl_match);
17088 					goto out;
17089 				}
17090 			}
17091 
17092 			nla_nest_end(msg, nl_freqs);
17093 		}
17094 
17095 		nla_nest_end(msg, nl_match);
17096 	}
17097 
17098 out:
17099 	nla_nest_end(msg, nl_results);
17100 	return 0;
17101 }
17102 
17103 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
17104 				   struct cfg80211_wowlan_wakeup *wakeup,
17105 				   gfp_t gfp)
17106 {
17107 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
17108 	struct sk_buff *msg;
17109 	void *hdr;
17110 	int size = 200;
17111 
17112 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
17113 
17114 	if (wakeup)
17115 		size += wakeup->packet_present_len;
17116 
17117 	msg = nlmsg_new(size, gfp);
17118 	if (!msg)
17119 		return;
17120 
17121 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
17122 	if (!hdr)
17123 		goto free_msg;
17124 
17125 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17126 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17127 			      NL80211_ATTR_PAD))
17128 		goto free_msg;
17129 
17130 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
17131 					wdev->netdev->ifindex))
17132 		goto free_msg;
17133 
17134 	if (wakeup) {
17135 		struct nlattr *reasons;
17136 
17137 		reasons = nla_nest_start_noflag(msg,
17138 						NL80211_ATTR_WOWLAN_TRIGGERS);
17139 		if (!reasons)
17140 			goto free_msg;
17141 
17142 		if (wakeup->disconnect &&
17143 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
17144 			goto free_msg;
17145 		if (wakeup->magic_pkt &&
17146 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
17147 			goto free_msg;
17148 		if (wakeup->gtk_rekey_failure &&
17149 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
17150 			goto free_msg;
17151 		if (wakeup->eap_identity_req &&
17152 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
17153 			goto free_msg;
17154 		if (wakeup->four_way_handshake &&
17155 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
17156 			goto free_msg;
17157 		if (wakeup->rfkill_release &&
17158 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
17159 			goto free_msg;
17160 
17161 		if (wakeup->pattern_idx >= 0 &&
17162 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
17163 				wakeup->pattern_idx))
17164 			goto free_msg;
17165 
17166 		if (wakeup->tcp_match &&
17167 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
17168 			goto free_msg;
17169 
17170 		if (wakeup->tcp_connlost &&
17171 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
17172 			goto free_msg;
17173 
17174 		if (wakeup->tcp_nomoretokens &&
17175 		    nla_put_flag(msg,
17176 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
17177 			goto free_msg;
17178 
17179 		if (wakeup->packet) {
17180 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
17181 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
17182 
17183 			if (!wakeup->packet_80211) {
17184 				pkt_attr =
17185 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
17186 				len_attr =
17187 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
17188 			}
17189 
17190 			if (wakeup->packet_len &&
17191 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
17192 				goto free_msg;
17193 
17194 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
17195 				    wakeup->packet))
17196 				goto free_msg;
17197 		}
17198 
17199 		if (wakeup->net_detect &&
17200 		    cfg80211_net_detect_results(msg, wakeup))
17201 				goto free_msg;
17202 
17203 		nla_nest_end(msg, reasons);
17204 	}
17205 
17206 	genlmsg_end(msg, hdr);
17207 
17208 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17209 				NL80211_MCGRP_MLME, gfp);
17210 	return;
17211 
17212  free_msg:
17213 	nlmsg_free(msg);
17214 }
17215 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
17216 #endif
17217 
17218 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
17219 				enum nl80211_tdls_operation oper,
17220 				u16 reason_code, gfp_t gfp)
17221 {
17222 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17223 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
17224 	struct sk_buff *msg;
17225 	void *hdr;
17226 
17227 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
17228 					 reason_code);
17229 
17230 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17231 	if (!msg)
17232 		return;
17233 
17234 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
17235 	if (!hdr) {
17236 		nlmsg_free(msg);
17237 		return;
17238 	}
17239 
17240 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17241 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
17242 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
17243 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
17244 	    (reason_code > 0 &&
17245 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
17246 		goto nla_put_failure;
17247 
17248 	genlmsg_end(msg, hdr);
17249 
17250 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17251 				NL80211_MCGRP_MLME, gfp);
17252 	return;
17253 
17254  nla_put_failure:
17255 	nlmsg_free(msg);
17256 }
17257 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
17258 
17259 static int nl80211_netlink_notify(struct notifier_block * nb,
17260 				  unsigned long state,
17261 				  void *_notify)
17262 {
17263 	struct netlink_notify *notify = _notify;
17264 	struct cfg80211_registered_device *rdev;
17265 	struct wireless_dev *wdev;
17266 	struct cfg80211_beacon_registration *reg, *tmp;
17267 
17268 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
17269 		return NOTIFY_DONE;
17270 
17271 	rcu_read_lock();
17272 
17273 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
17274 		struct cfg80211_sched_scan_request *sched_scan_req;
17275 
17276 		list_for_each_entry_rcu(sched_scan_req,
17277 					&rdev->sched_scan_req_list,
17278 					list) {
17279 			if (sched_scan_req->owner_nlportid == notify->portid) {
17280 				sched_scan_req->nl_owner_dead = true;
17281 				schedule_work(&rdev->sched_scan_stop_wk);
17282 			}
17283 		}
17284 
17285 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
17286 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
17287 
17288 			if (wdev->owner_nlportid == notify->portid) {
17289 				wdev->nl_owner_dead = true;
17290 				schedule_work(&rdev->destroy_work);
17291 			} else if (wdev->conn_owner_nlportid == notify->portid) {
17292 				schedule_work(&wdev->disconnect_wk);
17293 			}
17294 
17295 			cfg80211_release_pmsr(wdev, notify->portid);
17296 		}
17297 
17298 		spin_lock_bh(&rdev->beacon_registrations_lock);
17299 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
17300 					 list) {
17301 			if (reg->nlportid == notify->portid) {
17302 				list_del(&reg->list);
17303 				kfree(reg);
17304 				break;
17305 			}
17306 		}
17307 		spin_unlock_bh(&rdev->beacon_registrations_lock);
17308 	}
17309 
17310 	rcu_read_unlock();
17311 
17312 	/*
17313 	 * It is possible that the user space process that is controlling the
17314 	 * indoor setting disappeared, so notify the regulatory core.
17315 	 */
17316 	regulatory_netlink_notify(notify->portid);
17317 	return NOTIFY_OK;
17318 }
17319 
17320 static struct notifier_block nl80211_netlink_notifier = {
17321 	.notifier_call = nl80211_netlink_notify,
17322 };
17323 
17324 void cfg80211_ft_event(struct net_device *netdev,
17325 		       struct cfg80211_ft_event_params *ft_event)
17326 {
17327 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
17328 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17329 	struct sk_buff *msg;
17330 	void *hdr;
17331 
17332 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
17333 
17334 	if (!ft_event->target_ap)
17335 		return;
17336 
17337 	msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
17338 			GFP_KERNEL);
17339 	if (!msg)
17340 		return;
17341 
17342 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
17343 	if (!hdr)
17344 		goto out;
17345 
17346 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17347 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17348 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
17349 		goto out;
17350 
17351 	if (ft_event->ies &&
17352 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
17353 		goto out;
17354 	if (ft_event->ric_ies &&
17355 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
17356 		    ft_event->ric_ies))
17357 		goto out;
17358 
17359 	genlmsg_end(msg, hdr);
17360 
17361 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17362 				NL80211_MCGRP_MLME, GFP_KERNEL);
17363 	return;
17364  out:
17365 	nlmsg_free(msg);
17366 }
17367 EXPORT_SYMBOL(cfg80211_ft_event);
17368 
17369 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
17370 {
17371 	struct cfg80211_registered_device *rdev;
17372 	struct sk_buff *msg;
17373 	void *hdr;
17374 	u32 nlportid;
17375 
17376 	rdev = wiphy_to_rdev(wdev->wiphy);
17377 	if (!rdev->crit_proto_nlportid)
17378 		return;
17379 
17380 	nlportid = rdev->crit_proto_nlportid;
17381 	rdev->crit_proto_nlportid = 0;
17382 
17383 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17384 	if (!msg)
17385 		return;
17386 
17387 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
17388 	if (!hdr)
17389 		goto nla_put_failure;
17390 
17391 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17392 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17393 			      NL80211_ATTR_PAD))
17394 		goto nla_put_failure;
17395 
17396 	genlmsg_end(msg, hdr);
17397 
17398 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
17399 	return;
17400 
17401  nla_put_failure:
17402 	nlmsg_free(msg);
17403 }
17404 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
17405 
17406 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
17407 {
17408 	struct wiphy *wiphy = wdev->wiphy;
17409 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17410 	struct sk_buff *msg;
17411 	void *hdr;
17412 
17413 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17414 	if (!msg)
17415 		return;
17416 
17417 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
17418 	if (!hdr)
17419 		goto out;
17420 
17421 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17422 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
17423 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17424 			      NL80211_ATTR_PAD))
17425 		goto out;
17426 
17427 	genlmsg_end(msg, hdr);
17428 
17429 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
17430 				NL80211_MCGRP_MLME, GFP_KERNEL);
17431 	return;
17432  out:
17433 	nlmsg_free(msg);
17434 }
17435 
17436 int cfg80211_external_auth_request(struct net_device *dev,
17437 				   struct cfg80211_external_auth_params *params,
17438 				   gfp_t gfp)
17439 {
17440 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17441 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
17442 	struct sk_buff *msg;
17443 	void *hdr;
17444 
17445 	if (!wdev->conn_owner_nlportid)
17446 		return -EINVAL;
17447 
17448 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17449 	if (!msg)
17450 		return -ENOMEM;
17451 
17452 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
17453 	if (!hdr)
17454 		goto nla_put_failure;
17455 
17456 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17457 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
17458 	    nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
17459 	    nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
17460 			params->action) ||
17461 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
17462 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
17463 		    params->ssid.ssid))
17464 		goto nla_put_failure;
17465 
17466 	genlmsg_end(msg, hdr);
17467 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
17468 			wdev->conn_owner_nlportid);
17469 	return 0;
17470 
17471  nla_put_failure:
17472 	nlmsg_free(msg);
17473 	return -ENOBUFS;
17474 }
17475 EXPORT_SYMBOL(cfg80211_external_auth_request);
17476 
17477 void cfg80211_update_owe_info_event(struct net_device *netdev,
17478 				    struct cfg80211_update_owe_info *owe_info,
17479 				    gfp_t gfp)
17480 {
17481 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
17482 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17483 	struct sk_buff *msg;
17484 	void *hdr;
17485 
17486 	trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
17487 
17488 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17489 	if (!msg)
17490 		return;
17491 
17492 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
17493 	if (!hdr)
17494 		goto nla_put_failure;
17495 
17496 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17497 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17498 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
17499 		goto nla_put_failure;
17500 
17501 	if (!owe_info->ie_len ||
17502 	    nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
17503 		goto nla_put_failure;
17504 
17505 	genlmsg_end(msg, hdr);
17506 
17507 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17508 				NL80211_MCGRP_MLME, gfp);
17509 	return;
17510 
17511 nla_put_failure:
17512 	genlmsg_cancel(msg, hdr);
17513 	nlmsg_free(msg);
17514 }
17515 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
17516 
17517 /* initialisation/exit functions */
17518 
17519 int __init nl80211_init(void)
17520 {
17521 	int err;
17522 
17523 	err = genl_register_family(&nl80211_fam);
17524 	if (err)
17525 		return err;
17526 
17527 	err = netlink_register_notifier(&nl80211_netlink_notifier);
17528 	if (err)
17529 		goto err_out;
17530 
17531 	return 0;
17532  err_out:
17533 	genl_unregister_family(&nl80211_fam);
17534 	return err;
17535 }
17536 
17537 void nl80211_exit(void)
17538 {
17539 	netlink_unregister_notifier(&nl80211_netlink_notifier);
17540 	genl_unregister_family(&nl80211_fam);
17541 }
17542