xref: /openbmc/linux/net/wireless/nl80211.c (revision 7b73a9c8)
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-2019 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 <net/net_namespace.h>
24 #include <net/genetlink.h>
25 #include <net/cfg80211.h>
26 #include <net/sock.h>
27 #include <net/inet_connection_sock.h>
28 #include "core.h"
29 #include "nl80211.h"
30 #include "reg.h"
31 #include "rdev-ops.h"
32 
33 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
34 				   struct genl_info *info,
35 				   struct cfg80211_crypto_settings *settings,
36 				   int cipher_limit);
37 
38 /* the netlink family */
39 static struct genl_family nl80211_fam;
40 
41 /* multicast groups */
42 enum nl80211_multicast_groups {
43 	NL80211_MCGRP_CONFIG,
44 	NL80211_MCGRP_SCAN,
45 	NL80211_MCGRP_REGULATORY,
46 	NL80211_MCGRP_MLME,
47 	NL80211_MCGRP_VENDOR,
48 	NL80211_MCGRP_NAN,
49 	NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
50 };
51 
52 static const struct genl_multicast_group nl80211_mcgrps[] = {
53 	[NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
54 	[NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
55 	[NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
56 	[NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
57 	[NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
58 	[NL80211_MCGRP_NAN] = { .name = NL80211_MULTICAST_GROUP_NAN },
59 #ifdef CONFIG_NL80211_TESTMODE
60 	[NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
61 #endif
62 };
63 
64 /* returns ERR_PTR values */
65 static struct wireless_dev *
66 __cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
67 {
68 	struct cfg80211_registered_device *rdev;
69 	struct wireless_dev *result = NULL;
70 	bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
71 	bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
72 	u64 wdev_id;
73 	int wiphy_idx = -1;
74 	int ifidx = -1;
75 
76 	ASSERT_RTNL();
77 
78 	if (!have_ifidx && !have_wdev_id)
79 		return ERR_PTR(-EINVAL);
80 
81 	if (have_ifidx)
82 		ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
83 	if (have_wdev_id) {
84 		wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
85 		wiphy_idx = wdev_id >> 32;
86 	}
87 
88 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
89 		struct wireless_dev *wdev;
90 
91 		if (wiphy_net(&rdev->wiphy) != netns)
92 			continue;
93 
94 		if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
95 			continue;
96 
97 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
98 			if (have_ifidx && wdev->netdev &&
99 			    wdev->netdev->ifindex == ifidx) {
100 				result = wdev;
101 				break;
102 			}
103 			if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
104 				result = wdev;
105 				break;
106 			}
107 		}
108 
109 		if (result)
110 			break;
111 	}
112 
113 	if (result)
114 		return result;
115 	return ERR_PTR(-ENODEV);
116 }
117 
118 static struct cfg80211_registered_device *
119 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
120 {
121 	struct cfg80211_registered_device *rdev = NULL, *tmp;
122 	struct net_device *netdev;
123 
124 	ASSERT_RTNL();
125 
126 	if (!attrs[NL80211_ATTR_WIPHY] &&
127 	    !attrs[NL80211_ATTR_IFINDEX] &&
128 	    !attrs[NL80211_ATTR_WDEV])
129 		return ERR_PTR(-EINVAL);
130 
131 	if (attrs[NL80211_ATTR_WIPHY])
132 		rdev = cfg80211_rdev_by_wiphy_idx(
133 				nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
134 
135 	if (attrs[NL80211_ATTR_WDEV]) {
136 		u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
137 		struct wireless_dev *wdev;
138 		bool found = false;
139 
140 		tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
141 		if (tmp) {
142 			/* make sure wdev exists */
143 			list_for_each_entry(wdev, &tmp->wiphy.wdev_list, list) {
144 				if (wdev->identifier != (u32)wdev_id)
145 					continue;
146 				found = true;
147 				break;
148 			}
149 
150 			if (!found)
151 				tmp = NULL;
152 
153 			if (rdev && tmp != rdev)
154 				return ERR_PTR(-EINVAL);
155 			rdev = tmp;
156 		}
157 	}
158 
159 	if (attrs[NL80211_ATTR_IFINDEX]) {
160 		int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
161 
162 		netdev = __dev_get_by_index(netns, ifindex);
163 		if (netdev) {
164 			if (netdev->ieee80211_ptr)
165 				tmp = wiphy_to_rdev(
166 					netdev->ieee80211_ptr->wiphy);
167 			else
168 				tmp = NULL;
169 
170 			/* not wireless device -- return error */
171 			if (!tmp)
172 				return ERR_PTR(-EINVAL);
173 
174 			/* mismatch -- return error */
175 			if (rdev && tmp != rdev)
176 				return ERR_PTR(-EINVAL);
177 
178 			rdev = tmp;
179 		}
180 	}
181 
182 	if (!rdev)
183 		return ERR_PTR(-ENODEV);
184 
185 	if (netns != wiphy_net(&rdev->wiphy))
186 		return ERR_PTR(-ENODEV);
187 
188 	return rdev;
189 }
190 
191 /*
192  * This function returns a pointer to the driver
193  * that the genl_info item that is passed refers to.
194  *
195  * The result of this can be a PTR_ERR and hence must
196  * be checked with IS_ERR() for errors.
197  */
198 static struct cfg80211_registered_device *
199 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
200 {
201 	return __cfg80211_rdev_from_attrs(netns, info->attrs);
202 }
203 
204 static int validate_beacon_head(const struct nlattr *attr,
205 				struct netlink_ext_ack *extack)
206 {
207 	const u8 *data = nla_data(attr);
208 	unsigned int len = nla_len(attr);
209 	const struct element *elem;
210 	const struct ieee80211_mgmt *mgmt = (void *)data;
211 	unsigned int fixedlen = offsetof(struct ieee80211_mgmt,
212 					 u.beacon.variable);
213 
214 	if (len < fixedlen)
215 		goto err;
216 
217 	if (ieee80211_hdrlen(mgmt->frame_control) !=
218 	    offsetof(struct ieee80211_mgmt, u.beacon))
219 		goto err;
220 
221 	data += fixedlen;
222 	len -= fixedlen;
223 
224 	for_each_element(elem, data, len) {
225 		/* nothing */
226 	}
227 
228 	if (for_each_element_completed(elem, data, len))
229 		return 0;
230 
231 err:
232 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed beacon head");
233 	return -EINVAL;
234 }
235 
236 static int validate_ie_attr(const struct nlattr *attr,
237 			    struct netlink_ext_ack *extack)
238 {
239 	const u8 *data = nla_data(attr);
240 	unsigned int len = nla_len(attr);
241 	const struct element *elem;
242 
243 	for_each_element(elem, data, len) {
244 		/* nothing */
245 	}
246 
247 	if (for_each_element_completed(elem, data, len))
248 		return 0;
249 
250 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed information elements");
251 	return -EINVAL;
252 }
253 
254 /* policy for the attributes */
255 static const struct nla_policy
256 nl80211_ftm_responder_policy[NL80211_FTM_RESP_ATTR_MAX + 1] = {
257 	[NL80211_FTM_RESP_ATTR_ENABLED] = { .type = NLA_FLAG, },
258 	[NL80211_FTM_RESP_ATTR_LCI] = { .type = NLA_BINARY,
259 					.len = U8_MAX },
260 	[NL80211_FTM_RESP_ATTR_CIVICLOC] = { .type = NLA_BINARY,
261 					     .len = U8_MAX },
262 };
263 
264 static const struct nla_policy
265 nl80211_pmsr_ftm_req_attr_policy[NL80211_PMSR_FTM_REQ_ATTR_MAX + 1] = {
266 	[NL80211_PMSR_FTM_REQ_ATTR_ASAP] = { .type = NLA_FLAG },
267 	[NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE] = { .type = NLA_U32 },
268 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP] =
269 		NLA_POLICY_MAX(NLA_U8, 15),
270 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD] = { .type = NLA_U16 },
271 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION] =
272 		NLA_POLICY_MAX(NLA_U8, 15),
273 	[NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST] =
274 		NLA_POLICY_MAX(NLA_U8, 31),
275 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES] = { .type = NLA_U8 },
276 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI] = { .type = NLA_FLAG },
277 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC] = { .type = NLA_FLAG },
278 };
279 
280 static const struct nla_policy
281 nl80211_pmsr_req_data_policy[NL80211_PMSR_TYPE_MAX + 1] = {
282 	[NL80211_PMSR_TYPE_FTM] =
283 		NLA_POLICY_NESTED(nl80211_pmsr_ftm_req_attr_policy),
284 };
285 
286 static const struct nla_policy
287 nl80211_pmsr_req_attr_policy[NL80211_PMSR_REQ_ATTR_MAX + 1] = {
288 	[NL80211_PMSR_REQ_ATTR_DATA] =
289 		NLA_POLICY_NESTED(nl80211_pmsr_req_data_policy),
290 	[NL80211_PMSR_REQ_ATTR_GET_AP_TSF] = { .type = NLA_FLAG },
291 };
292 
293 static const struct nla_policy
294 nl80211_psmr_peer_attr_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
295 	[NL80211_PMSR_PEER_ATTR_ADDR] = NLA_POLICY_ETH_ADDR,
296 	/*
297 	 * we could specify this again to be the top-level policy,
298 	 * but that would open us up to recursion problems ...
299 	 */
300 	[NL80211_PMSR_PEER_ATTR_CHAN] = { .type = NLA_NESTED },
301 	[NL80211_PMSR_PEER_ATTR_REQ] =
302 		NLA_POLICY_NESTED(nl80211_pmsr_req_attr_policy),
303 	[NL80211_PMSR_PEER_ATTR_RESP] = { .type = NLA_REJECT },
304 };
305 
306 static const struct nla_policy
307 nl80211_pmsr_attr_policy[NL80211_PMSR_ATTR_MAX + 1] = {
308 	[NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_REJECT },
309 	[NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_REJECT },
310 	[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
311 	[NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
312 	[NL80211_PMSR_ATTR_PEERS] =
313 		NLA_POLICY_NESTED_ARRAY(nl80211_psmr_peer_attr_policy),
314 };
315 
316 static const struct nla_policy
317 he_obss_pd_policy[NL80211_HE_OBSS_PD_ATTR_MAX + 1] = {
318 	[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] =
319 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
320 	[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET] =
321 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
322 };
323 
324 const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
325 	[0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
326 	[NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
327 	[NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
328 				      .len = 20-1 },
329 	[NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
330 
331 	[NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
332 	[NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
333 	[NL80211_ATTR_WIPHY_EDMG_CHANNELS] = NLA_POLICY_RANGE(NLA_U8,
334 						NL80211_EDMG_CHANNELS_MIN,
335 						NL80211_EDMG_CHANNELS_MAX),
336 	[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG] = NLA_POLICY_RANGE(NLA_U8,
337 						NL80211_EDMG_BW_CONFIG_MIN,
338 						NL80211_EDMG_BW_CONFIG_MAX),
339 
340 	[NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
341 	[NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
342 	[NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
343 
344 	[NL80211_ATTR_WIPHY_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
345 	[NL80211_ATTR_WIPHY_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
346 	[NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
347 	[NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
348 	[NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
349 	[NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
350 
351 	[NL80211_ATTR_IFTYPE] = NLA_POLICY_MAX(NLA_U32, NL80211_IFTYPE_MAX),
352 	[NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
353 	[NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
354 
355 	[NL80211_ATTR_MAC] = { .type = NLA_EXACT_LEN_WARN, .len = ETH_ALEN },
356 	[NL80211_ATTR_PREV_BSSID] = {
357 		.type = NLA_EXACT_LEN_WARN,
358 		.len = ETH_ALEN
359 	},
360 
361 	[NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
362 	[NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
363 				    .len = WLAN_MAX_KEY_LEN },
364 	[NL80211_ATTR_KEY_IDX] = NLA_POLICY_MAX(NLA_U8, 5),
365 	[NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
366 	[NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
367 	[NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
368 	[NL80211_ATTR_KEY_TYPE] =
369 		NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES),
370 
371 	[NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
372 	[NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
373 	[NL80211_ATTR_BEACON_HEAD] =
374 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_beacon_head,
375 				       IEEE80211_MAX_DATA_LEN),
376 	[NL80211_ATTR_BEACON_TAIL] =
377 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
378 				       IEEE80211_MAX_DATA_LEN),
379 	[NL80211_ATTR_STA_AID] =
380 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
381 	[NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
382 	[NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
383 	[NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
384 					       .len = NL80211_MAX_SUPP_RATES },
385 	[NL80211_ATTR_STA_PLINK_ACTION] =
386 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_ACTIONS - 1),
387 	[NL80211_ATTR_STA_TX_POWER_SETTING] =
388 		NLA_POLICY_RANGE(NLA_U8,
389 				 NL80211_TX_POWER_AUTOMATIC,
390 				 NL80211_TX_POWER_FIXED),
391 	[NL80211_ATTR_STA_TX_POWER] = { .type = NLA_S16 },
392 	[NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
393 	[NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
394 	[NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
395 				   .len = IEEE80211_MAX_MESH_ID_LEN },
396 	[NL80211_ATTR_MPATH_NEXT_HOP] = NLA_POLICY_ETH_ADDR_COMPAT,
397 
398 	[NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
399 	[NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
400 
401 	[NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
402 	[NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
403 	[NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
404 	[NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
405 					   .len = NL80211_MAX_SUPP_RATES },
406 	[NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
407 
408 	[NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
409 	[NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
410 
411 	[NL80211_ATTR_HT_CAPABILITY] = {
412 		.type = NLA_EXACT_LEN_WARN,
413 		.len = NL80211_HT_CAPABILITY_LEN
414 	},
415 
416 	[NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
417 	[NL80211_ATTR_IE] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
418 						   validate_ie_attr,
419 						   IEEE80211_MAX_DATA_LEN),
420 	[NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
421 	[NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
422 
423 	[NL80211_ATTR_SSID] = { .type = NLA_BINARY,
424 				.len = IEEE80211_MAX_SSID_LEN },
425 	[NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
426 	[NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
427 	[NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
428 	[NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
429 	[NL80211_ATTR_USE_MFP] = NLA_POLICY_RANGE(NLA_U32,
430 						  NL80211_MFP_NO,
431 						  NL80211_MFP_OPTIONAL),
432 	[NL80211_ATTR_STA_FLAGS2] = {
433 		.len = sizeof(struct nl80211_sta_flag_update),
434 	},
435 	[NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
436 	[NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
437 	[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
438 	[NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
439 	[NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
440 	[NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
441 	[NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
442 	[NL80211_ATTR_PID] = { .type = NLA_U32 },
443 	[NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
444 	[NL80211_ATTR_PMKID] = {
445 		.type = NLA_EXACT_LEN_WARN,
446 		.len = WLAN_PMKID_LEN
447 	},
448 	[NL80211_ATTR_DURATION] = { .type = NLA_U32 },
449 	[NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
450 	[NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
451 	[NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
452 				 .len = IEEE80211_MAX_DATA_LEN },
453 	[NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
454 	[NL80211_ATTR_PS_STATE] = NLA_POLICY_RANGE(NLA_U32,
455 						   NL80211_PS_DISABLED,
456 						   NL80211_PS_ENABLED),
457 	[NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
458 	[NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
459 	[NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
460 	[NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
461 	[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
462 	[NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
463 	[NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
464 	[NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
465 	[NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
466 	[NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
467 	[NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
468 	[NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
469 	[NL80211_ATTR_STA_PLINK_STATE] =
470 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_STATES - 1),
471 	[NL80211_ATTR_MESH_PEER_AID] =
472 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
473 	[NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
474 	[NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
475 	[NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
476 	[NL80211_ATTR_HIDDEN_SSID] =
477 		NLA_POLICY_RANGE(NLA_U32,
478 				 NL80211_HIDDEN_SSID_NOT_IN_USE,
479 				 NL80211_HIDDEN_SSID_ZERO_CONTENTS),
480 	[NL80211_ATTR_IE_PROBE_RESP] =
481 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
482 				       IEEE80211_MAX_DATA_LEN),
483 	[NL80211_ATTR_IE_ASSOC_RESP] =
484 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
485 				       IEEE80211_MAX_DATA_LEN),
486 	[NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
487 	[NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
488 	[NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
489 	[NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
490 	[NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
491 	[NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
492 	[NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
493 	[NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
494 	[NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
495 	[NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
496 	[NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
497 				      .len = IEEE80211_MAX_DATA_LEN },
498 	[NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
499 	[NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
500 	[NL80211_ATTR_HT_CAPABILITY_MASK] = {
501 		.len = NL80211_HT_CAPABILITY_LEN
502 	},
503 	[NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
504 	[NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
505 	[NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
506 	[NL80211_ATTR_WDEV] = { .type = NLA_U64 },
507 	[NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
508 	[NL80211_ATTR_AUTH_DATA] = { .type = NLA_BINARY, },
509 	[NL80211_ATTR_VHT_CAPABILITY] = {
510 		.type = NLA_EXACT_LEN_WARN,
511 		.len = NL80211_VHT_CAPABILITY_LEN
512 	},
513 	[NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
514 	[NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
515 	[NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
516 	[NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
517 		NLA_POLICY_RANGE(NLA_U32,
518 				 NL80211_MESH_POWER_UNKNOWN + 1,
519 				 NL80211_MESH_POWER_MAX),
520 	[NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
521 	[NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
522 	[NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
523 	[NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
524 	[NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
525 	[NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
526 	[NL80211_ATTR_VHT_CAPABILITY_MASK] = {
527 		.len = NL80211_VHT_CAPABILITY_LEN,
528 	},
529 	[NL80211_ATTR_MDID] = { .type = NLA_U16 },
530 	[NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
531 				  .len = IEEE80211_MAX_DATA_LEN },
532 	[NL80211_ATTR_PEER_AID] =
533 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
534 	[NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
535 	[NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
536 	[NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
537 	[NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
538 	[NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
539 	[NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
540 	[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
541 	[NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
542 	[NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
543 	[NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
544 	[NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
545 	[NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
546 	[NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
547 				   .len = IEEE80211_QOS_MAP_LEN_MAX },
548 	[NL80211_ATTR_MAC_HINT] = {
549 		.type = NLA_EXACT_LEN_WARN,
550 		.len = ETH_ALEN
551 	},
552 	[NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
553 	[NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
554 	[NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
555 	[NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
556 	[NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
557 	[NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
558 	[NL80211_ATTR_USER_PRIO] =
559 		NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
560 	[NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
561 	[NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
562 	[NL80211_ATTR_MAC_MASK] = {
563 		.type = NLA_EXACT_LEN_WARN,
564 		.len = ETH_ALEN
565 	},
566 	[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
567 	[NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
568 	[NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
569 	[NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
570 	[NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
571 	[NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
572 	[NL80211_ATTR_STA_SUPPORT_P2P_PS] =
573 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
574 	[NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
575 		.len = VHT_MUMIMO_GROUPS_DATA_LEN
576 	},
577 	[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = {
578 		.type = NLA_EXACT_LEN_WARN,
579 		.len = ETH_ALEN
580 	},
581 	[NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
582 	[NL80211_ATTR_BANDS] = { .type = NLA_U32 },
583 	[NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
584 	[NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
585 				    .len = FILS_MAX_KEK_LEN },
586 	[NL80211_ATTR_FILS_NONCES] = {
587 		.type = NLA_EXACT_LEN_WARN,
588 		.len = 2 * FILS_NONCE_LEN
589 	},
590 	[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
591 	[NL80211_ATTR_BSSID] = { .type = NLA_EXACT_LEN_WARN, .len = ETH_ALEN },
592 	[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
593 	[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
594 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
595 	},
596 	[NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
597 	[NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
598 					     .len = FILS_ERP_MAX_USERNAME_LEN },
599 	[NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
600 					  .len = FILS_ERP_MAX_REALM_LEN },
601 	[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
602 	[NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
603 					.len = FILS_ERP_MAX_RRK_LEN },
604 	[NL80211_ATTR_FILS_CACHE_ID] = { .type = NLA_EXACT_LEN_WARN, .len = 2 },
605 	[NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
606 	[NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
607 	[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
608 
609 	[NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
610 	[NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
611 	[NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
612 	[NL80211_ATTR_HE_CAPABILITY] = { .type = NLA_BINARY,
613 					 .len = NL80211_HE_MAX_CAPABILITY_LEN },
614 
615 	[NL80211_ATTR_FTM_RESPONDER] = {
616 		.type = NLA_NESTED,
617 		.validation_data = nl80211_ftm_responder_policy,
618 	},
619 	[NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
620 	[NL80211_ATTR_PEER_MEASUREMENTS] =
621 		NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
622 	[NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
623 	[NL80211_ATTR_SAE_PASSWORD] = { .type = NLA_BINARY,
624 					.len = SAE_PASSWORD_MAX_LEN },
625 	[NL80211_ATTR_TWT_RESPONDER] = { .type = NLA_FLAG },
626 	[NL80211_ATTR_HE_OBSS_PD] = NLA_POLICY_NESTED(he_obss_pd_policy),
627 	[NL80211_ATTR_VLAN_ID] = NLA_POLICY_RANGE(NLA_U16, 1, VLAN_N_VID - 2),
628 };
629 
630 /* policy for the key attributes */
631 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
632 	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
633 	[NL80211_KEY_IDX] = { .type = NLA_U8 },
634 	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
635 	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
636 	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
637 	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
638 	[NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
639 	[NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
640 	[NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
641 };
642 
643 /* policy for the key default flags */
644 static const struct nla_policy
645 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
646 	[NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
647 	[NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
648 };
649 
650 #ifdef CONFIG_PM
651 /* policy for WoWLAN attributes */
652 static const struct nla_policy
653 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
654 	[NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
655 	[NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
656 	[NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
657 	[NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
658 	[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
659 	[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
660 	[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
661 	[NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
662 	[NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
663 	[NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
664 };
665 
666 static const struct nla_policy
667 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
668 	[NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
669 	[NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
670 	[NL80211_WOWLAN_TCP_DST_MAC] = {
671 		.type = NLA_EXACT_LEN_WARN,
672 		.len = ETH_ALEN
673 	},
674 	[NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
675 	[NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
676 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .type = NLA_MIN_LEN, .len = 1 },
677 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
678 		.len = sizeof(struct nl80211_wowlan_tcp_data_seq)
679 	},
680 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
681 		.len = sizeof(struct nl80211_wowlan_tcp_data_token)
682 	},
683 	[NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
684 	[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .type = NLA_MIN_LEN, .len = 1 },
685 	[NL80211_WOWLAN_TCP_WAKE_MASK] = { .type = NLA_MIN_LEN, .len = 1 },
686 };
687 #endif /* CONFIG_PM */
688 
689 /* policy for coalesce rule attributes */
690 static const struct nla_policy
691 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
692 	[NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
693 	[NL80211_ATTR_COALESCE_RULE_CONDITION] =
694 		NLA_POLICY_RANGE(NLA_U32,
695 				 NL80211_COALESCE_CONDITION_MATCH,
696 				 NL80211_COALESCE_CONDITION_NO_MATCH),
697 	[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
698 };
699 
700 /* policy for GTK rekey offload attributes */
701 static const struct nla_policy
702 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
703 	[NL80211_REKEY_DATA_KEK] = {
704 		.type = NLA_EXACT_LEN_WARN,
705 		.len = NL80211_KEK_LEN,
706 	},
707 	[NL80211_REKEY_DATA_KCK] = {
708 		.type = NLA_EXACT_LEN_WARN,
709 		.len = NL80211_KCK_LEN,
710 	},
711 	[NL80211_REKEY_DATA_REPLAY_CTR] = {
712 		.type = NLA_EXACT_LEN_WARN,
713 		.len = NL80211_REPLAY_CTR_LEN
714 	},
715 };
716 
717 static const struct nla_policy
718 nl80211_match_band_rssi_policy[NUM_NL80211_BANDS] = {
719 	[NL80211_BAND_2GHZ] = { .type = NLA_S32 },
720 	[NL80211_BAND_5GHZ] = { .type = NLA_S32 },
721 	[NL80211_BAND_6GHZ] = { .type = NLA_S32 },
722 	[NL80211_BAND_60GHZ] = { .type = NLA_S32 },
723 };
724 
725 static const struct nla_policy
726 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
727 	[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
728 						 .len = IEEE80211_MAX_SSID_LEN },
729 	[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = {
730 		.type = NLA_EXACT_LEN_WARN,
731 		.len = ETH_ALEN
732 	},
733 	[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
734 	[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI] =
735 		NLA_POLICY_NESTED(nl80211_match_band_rssi_policy),
736 };
737 
738 static const struct nla_policy
739 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
740 	[NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
741 	[NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
742 };
743 
744 static const struct nla_policy
745 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
746 	[NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
747 	[NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
748 	[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
749 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
750 	},
751 };
752 
753 /* policy for NAN function attributes */
754 static const struct nla_policy
755 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
756 	[NL80211_NAN_FUNC_TYPE] = { .type = NLA_U8 },
757 	[NL80211_NAN_FUNC_SERVICE_ID] = {
758 				    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
759 	[NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
760 	[NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
761 	[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
762 	[NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
763 	[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
764 	[NL80211_NAN_FUNC_FOLLOW_UP_DEST] = {
765 		.type = NLA_EXACT_LEN_WARN,
766 		.len = ETH_ALEN
767 	},
768 	[NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
769 	[NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
770 	[NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
771 			.len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
772 	[NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
773 	[NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
774 	[NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
775 	[NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
776 	[NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
777 };
778 
779 /* policy for Service Response Filter attributes */
780 static const struct nla_policy
781 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
782 	[NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
783 	[NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
784 				 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
785 	[NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
786 	[NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
787 };
788 
789 /* policy for packet pattern attributes */
790 static const struct nla_policy
791 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
792 	[NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
793 	[NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
794 	[NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
795 };
796 
797 int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
798 			      struct cfg80211_registered_device **rdev,
799 			      struct wireless_dev **wdev)
800 {
801 	int err;
802 
803 	if (!cb->args[0]) {
804 		struct nlattr **attrbuf;
805 
806 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
807 				  GFP_KERNEL);
808 		if (!attrbuf)
809 			return -ENOMEM;
810 
811 		err = nlmsg_parse_deprecated(cb->nlh,
812 					     GENL_HDRLEN + nl80211_fam.hdrsize,
813 					     attrbuf, nl80211_fam.maxattr,
814 					     nl80211_policy, NULL);
815 		if (err) {
816 			kfree(attrbuf);
817 			return err;
818 		}
819 
820 		*wdev = __cfg80211_wdev_from_attrs(sock_net(cb->skb->sk),
821 						   attrbuf);
822 		kfree(attrbuf);
823 		if (IS_ERR(*wdev))
824 			return PTR_ERR(*wdev);
825 		*rdev = wiphy_to_rdev((*wdev)->wiphy);
826 		/* 0 is the first index - add 1 to parse only once */
827 		cb->args[0] = (*rdev)->wiphy_idx + 1;
828 		cb->args[1] = (*wdev)->identifier;
829 	} else {
830 		/* subtract the 1 again here */
831 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
832 		struct wireless_dev *tmp;
833 
834 		if (!wiphy)
835 			return -ENODEV;
836 		*rdev = wiphy_to_rdev(wiphy);
837 		*wdev = NULL;
838 
839 		list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
840 			if (tmp->identifier == cb->args[1]) {
841 				*wdev = tmp;
842 				break;
843 			}
844 		}
845 
846 		if (!*wdev)
847 			return -ENODEV;
848 	}
849 
850 	return 0;
851 }
852 
853 /* message building helper */
854 void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
855 		     int flags, u8 cmd)
856 {
857 	/* since there is no private header just add the generic one */
858 	return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
859 }
860 
861 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
862 				     const struct ieee80211_reg_rule *rule)
863 {
864 	int j;
865 	struct nlattr *nl_wmm_rules =
866 		nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
867 
868 	if (!nl_wmm_rules)
869 		goto nla_put_failure;
870 
871 	for (j = 0; j < IEEE80211_NUM_ACS; j++) {
872 		struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
873 
874 		if (!nl_wmm_rule)
875 			goto nla_put_failure;
876 
877 		if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
878 				rule->wmm_rule.client[j].cw_min) ||
879 		    nla_put_u16(msg, NL80211_WMMR_CW_MAX,
880 				rule->wmm_rule.client[j].cw_max) ||
881 		    nla_put_u8(msg, NL80211_WMMR_AIFSN,
882 			       rule->wmm_rule.client[j].aifsn) ||
883 		    nla_put_u16(msg, NL80211_WMMR_TXOP,
884 			        rule->wmm_rule.client[j].cot))
885 			goto nla_put_failure;
886 
887 		nla_nest_end(msg, nl_wmm_rule);
888 	}
889 	nla_nest_end(msg, nl_wmm_rules);
890 
891 	return 0;
892 
893 nla_put_failure:
894 	return -ENOBUFS;
895 }
896 
897 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
898 				   struct ieee80211_channel *chan,
899 				   bool large)
900 {
901 	/* Some channels must be completely excluded from the
902 	 * list to protect old user-space tools from breaking
903 	 */
904 	if (!large && chan->flags &
905 	    (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
906 		return 0;
907 
908 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
909 			chan->center_freq))
910 		goto nla_put_failure;
911 
912 	if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
913 	    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
914 		goto nla_put_failure;
915 	if (chan->flags & IEEE80211_CHAN_NO_IR) {
916 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
917 			goto nla_put_failure;
918 		if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
919 			goto nla_put_failure;
920 	}
921 	if (chan->flags & IEEE80211_CHAN_RADAR) {
922 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
923 			goto nla_put_failure;
924 		if (large) {
925 			u32 time;
926 
927 			time = elapsed_jiffies_msecs(chan->dfs_state_entered);
928 
929 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
930 					chan->dfs_state))
931 				goto nla_put_failure;
932 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
933 					time))
934 				goto nla_put_failure;
935 			if (nla_put_u32(msg,
936 					NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
937 					chan->dfs_cac_ms))
938 				goto nla_put_failure;
939 		}
940 	}
941 
942 	if (large) {
943 		if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
944 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
945 			goto nla_put_failure;
946 		if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
947 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
948 			goto nla_put_failure;
949 		if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
950 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
951 			goto nla_put_failure;
952 		if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
953 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
954 			goto nla_put_failure;
955 		if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
956 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
957 			goto nla_put_failure;
958 		if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
959 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
960 			goto nla_put_failure;
961 		if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
962 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
963 			goto nla_put_failure;
964 		if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
965 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
966 			goto nla_put_failure;
967 	}
968 
969 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
970 			DBM_TO_MBM(chan->max_power)))
971 		goto nla_put_failure;
972 
973 	if (large) {
974 		const struct ieee80211_reg_rule *rule =
975 			freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
976 
977 		if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
978 			if (nl80211_msg_put_wmm_rules(msg, rule))
979 				goto nla_put_failure;
980 		}
981 	}
982 
983 	return 0;
984 
985  nla_put_failure:
986 	return -ENOBUFS;
987 }
988 
989 static bool nl80211_put_txq_stats(struct sk_buff *msg,
990 				  struct cfg80211_txq_stats *txqstats,
991 				  int attrtype)
992 {
993 	struct nlattr *txqattr;
994 
995 #define PUT_TXQVAL_U32(attr, memb) do {					  \
996 	if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&	  \
997 	    nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
998 		return false;						  \
999 	} while (0)
1000 
1001 	txqattr = nla_nest_start_noflag(msg, attrtype);
1002 	if (!txqattr)
1003 		return false;
1004 
1005 	PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
1006 	PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
1007 	PUT_TXQVAL_U32(FLOWS, flows);
1008 	PUT_TXQVAL_U32(DROPS, drops);
1009 	PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
1010 	PUT_TXQVAL_U32(OVERLIMIT, overlimit);
1011 	PUT_TXQVAL_U32(OVERMEMORY, overmemory);
1012 	PUT_TXQVAL_U32(COLLISIONS, collisions);
1013 	PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
1014 	PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
1015 	PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
1016 	nla_nest_end(msg, txqattr);
1017 
1018 #undef PUT_TXQVAL_U32
1019 	return true;
1020 }
1021 
1022 /* netlink command implementations */
1023 
1024 struct key_parse {
1025 	struct key_params p;
1026 	int idx;
1027 	int type;
1028 	bool def, defmgmt;
1029 	bool def_uni, def_multi;
1030 };
1031 
1032 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
1033 				 struct key_parse *k)
1034 {
1035 	struct nlattr *tb[NL80211_KEY_MAX + 1];
1036 	int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
1037 					      nl80211_key_policy,
1038 					      info->extack);
1039 	if (err)
1040 		return err;
1041 
1042 	k->def = !!tb[NL80211_KEY_DEFAULT];
1043 	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1044 
1045 	if (k->def) {
1046 		k->def_uni = true;
1047 		k->def_multi = true;
1048 	}
1049 	if (k->defmgmt)
1050 		k->def_multi = true;
1051 
1052 	if (tb[NL80211_KEY_IDX])
1053 		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1054 
1055 	if (tb[NL80211_KEY_DATA]) {
1056 		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1057 		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1058 	}
1059 
1060 	if (tb[NL80211_KEY_SEQ]) {
1061 		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1062 		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1063 	}
1064 
1065 	if (tb[NL80211_KEY_CIPHER])
1066 		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1067 
1068 	if (tb[NL80211_KEY_TYPE])
1069 		k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1070 
1071 	if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1072 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1073 
1074 		err = nla_parse_nested_deprecated(kdt,
1075 						  NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1076 						  tb[NL80211_KEY_DEFAULT_TYPES],
1077 						  nl80211_key_default_policy,
1078 						  info->extack);
1079 		if (err)
1080 			return err;
1081 
1082 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1083 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1084 	}
1085 
1086 	if (tb[NL80211_KEY_MODE])
1087 		k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1088 
1089 	return 0;
1090 }
1091 
1092 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1093 {
1094 	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1095 		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1096 		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1097 	}
1098 
1099 	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1100 		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1101 		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1102 	}
1103 
1104 	if (info->attrs[NL80211_ATTR_KEY_IDX])
1105 		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1106 
1107 	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1108 		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1109 
1110 	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1111 	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1112 
1113 	if (k->def) {
1114 		k->def_uni = true;
1115 		k->def_multi = true;
1116 	}
1117 	if (k->defmgmt)
1118 		k->def_multi = true;
1119 
1120 	if (info->attrs[NL80211_ATTR_KEY_TYPE])
1121 		k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1122 
1123 	if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1124 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1125 		int err = nla_parse_nested_deprecated(kdt,
1126 						      NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1127 						      info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1128 						      nl80211_key_default_policy,
1129 						      info->extack);
1130 		if (err)
1131 			return err;
1132 
1133 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1134 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1135 	}
1136 
1137 	return 0;
1138 }
1139 
1140 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1141 {
1142 	int err;
1143 
1144 	memset(k, 0, sizeof(*k));
1145 	k->idx = -1;
1146 	k->type = -1;
1147 
1148 	if (info->attrs[NL80211_ATTR_KEY])
1149 		err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1150 	else
1151 		err = nl80211_parse_key_old(info, k);
1152 
1153 	if (err)
1154 		return err;
1155 
1156 	if (k->def && k->defmgmt) {
1157 		GENL_SET_ERR_MSG(info, "key with def && defmgmt is invalid");
1158 		return -EINVAL;
1159 	}
1160 
1161 	if (k->defmgmt) {
1162 		if (k->def_uni || !k->def_multi) {
1163 			GENL_SET_ERR_MSG(info, "defmgmt key must be mcast");
1164 			return -EINVAL;
1165 		}
1166 	}
1167 
1168 	if (k->idx != -1) {
1169 		if (k->defmgmt) {
1170 			if (k->idx < 4 || k->idx > 5) {
1171 				GENL_SET_ERR_MSG(info,
1172 						 "defmgmt key idx not 4 or 5");
1173 				return -EINVAL;
1174 			}
1175 		} else if (k->def) {
1176 			if (k->idx < 0 || k->idx > 3) {
1177 				GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1178 				return -EINVAL;
1179 			}
1180 		} else {
1181 			if (k->idx < 0 || k->idx > 5) {
1182 				GENL_SET_ERR_MSG(info, "key idx not 0-5");
1183 				return -EINVAL;
1184 			}
1185 		}
1186 	}
1187 
1188 	return 0;
1189 }
1190 
1191 static struct cfg80211_cached_keys *
1192 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1193 		       struct genl_info *info, bool *no_ht)
1194 {
1195 	struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1196 	struct key_parse parse;
1197 	struct nlattr *key;
1198 	struct cfg80211_cached_keys *result;
1199 	int rem, err, def = 0;
1200 	bool have_key = false;
1201 
1202 	nla_for_each_nested(key, keys, rem) {
1203 		have_key = true;
1204 		break;
1205 	}
1206 
1207 	if (!have_key)
1208 		return NULL;
1209 
1210 	result = kzalloc(sizeof(*result), GFP_KERNEL);
1211 	if (!result)
1212 		return ERR_PTR(-ENOMEM);
1213 
1214 	result->def = -1;
1215 
1216 	nla_for_each_nested(key, keys, rem) {
1217 		memset(&parse, 0, sizeof(parse));
1218 		parse.idx = -1;
1219 
1220 		err = nl80211_parse_key_new(info, key, &parse);
1221 		if (err)
1222 			goto error;
1223 		err = -EINVAL;
1224 		if (!parse.p.key)
1225 			goto error;
1226 		if (parse.idx < 0 || parse.idx > 3) {
1227 			GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1228 			goto error;
1229 		}
1230 		if (parse.def) {
1231 			if (def) {
1232 				GENL_SET_ERR_MSG(info,
1233 						 "only one key can be default");
1234 				goto error;
1235 			}
1236 			def = 1;
1237 			result->def = parse.idx;
1238 			if (!parse.def_uni || !parse.def_multi)
1239 				goto error;
1240 		} else if (parse.defmgmt)
1241 			goto error;
1242 		err = cfg80211_validate_key_settings(rdev, &parse.p,
1243 						     parse.idx, false, NULL);
1244 		if (err)
1245 			goto error;
1246 		if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1247 		    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1248 			GENL_SET_ERR_MSG(info, "connect key must be WEP");
1249 			err = -EINVAL;
1250 			goto error;
1251 		}
1252 		result->params[parse.idx].cipher = parse.p.cipher;
1253 		result->params[parse.idx].key_len = parse.p.key_len;
1254 		result->params[parse.idx].key = result->data[parse.idx];
1255 		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1256 
1257 		/* must be WEP key if we got here */
1258 		if (no_ht)
1259 			*no_ht = true;
1260 	}
1261 
1262 	if (result->def < 0) {
1263 		err = -EINVAL;
1264 		GENL_SET_ERR_MSG(info, "need a default/TX key");
1265 		goto error;
1266 	}
1267 
1268 	return result;
1269  error:
1270 	kfree(result);
1271 	return ERR_PTR(err);
1272 }
1273 
1274 static int nl80211_key_allowed(struct wireless_dev *wdev)
1275 {
1276 	ASSERT_WDEV_LOCK(wdev);
1277 
1278 	switch (wdev->iftype) {
1279 	case NL80211_IFTYPE_AP:
1280 	case NL80211_IFTYPE_AP_VLAN:
1281 	case NL80211_IFTYPE_P2P_GO:
1282 	case NL80211_IFTYPE_MESH_POINT:
1283 		break;
1284 	case NL80211_IFTYPE_ADHOC:
1285 	case NL80211_IFTYPE_STATION:
1286 	case NL80211_IFTYPE_P2P_CLIENT:
1287 		if (!wdev->current_bss)
1288 			return -ENOLINK;
1289 		break;
1290 	case NL80211_IFTYPE_UNSPECIFIED:
1291 	case NL80211_IFTYPE_OCB:
1292 	case NL80211_IFTYPE_MONITOR:
1293 	case NL80211_IFTYPE_NAN:
1294 	case NL80211_IFTYPE_P2P_DEVICE:
1295 	case NL80211_IFTYPE_WDS:
1296 	case NUM_NL80211_IFTYPES:
1297 		return -EINVAL;
1298 	}
1299 
1300 	return 0;
1301 }
1302 
1303 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1304 							struct nlattr *tb)
1305 {
1306 	struct ieee80211_channel *chan;
1307 
1308 	if (tb == NULL)
1309 		return NULL;
1310 	chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
1311 	if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1312 		return NULL;
1313 	return chan;
1314 }
1315 
1316 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1317 {
1318 	struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1319 	int i;
1320 
1321 	if (!nl_modes)
1322 		goto nla_put_failure;
1323 
1324 	i = 0;
1325 	while (ifmodes) {
1326 		if ((ifmodes & 1) && nla_put_flag(msg, i))
1327 			goto nla_put_failure;
1328 		ifmodes >>= 1;
1329 		i++;
1330 	}
1331 
1332 	nla_nest_end(msg, nl_modes);
1333 	return 0;
1334 
1335 nla_put_failure:
1336 	return -ENOBUFS;
1337 }
1338 
1339 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1340 					  struct sk_buff *msg,
1341 					  bool large)
1342 {
1343 	struct nlattr *nl_combis;
1344 	int i, j;
1345 
1346 	nl_combis = nla_nest_start_noflag(msg,
1347 					  NL80211_ATTR_INTERFACE_COMBINATIONS);
1348 	if (!nl_combis)
1349 		goto nla_put_failure;
1350 
1351 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1352 		const struct ieee80211_iface_combination *c;
1353 		struct nlattr *nl_combi, *nl_limits;
1354 
1355 		c = &wiphy->iface_combinations[i];
1356 
1357 		nl_combi = nla_nest_start_noflag(msg, i + 1);
1358 		if (!nl_combi)
1359 			goto nla_put_failure;
1360 
1361 		nl_limits = nla_nest_start_noflag(msg,
1362 						  NL80211_IFACE_COMB_LIMITS);
1363 		if (!nl_limits)
1364 			goto nla_put_failure;
1365 
1366 		for (j = 0; j < c->n_limits; j++) {
1367 			struct nlattr *nl_limit;
1368 
1369 			nl_limit = nla_nest_start_noflag(msg, j + 1);
1370 			if (!nl_limit)
1371 				goto nla_put_failure;
1372 			if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1373 					c->limits[j].max))
1374 				goto nla_put_failure;
1375 			if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1376 						c->limits[j].types))
1377 				goto nla_put_failure;
1378 			nla_nest_end(msg, nl_limit);
1379 		}
1380 
1381 		nla_nest_end(msg, nl_limits);
1382 
1383 		if (c->beacon_int_infra_match &&
1384 		    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1385 			goto nla_put_failure;
1386 		if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1387 				c->num_different_channels) ||
1388 		    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1389 				c->max_interfaces))
1390 			goto nla_put_failure;
1391 		if (large &&
1392 		    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1393 				c->radar_detect_widths) ||
1394 		     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1395 				c->radar_detect_regions)))
1396 			goto nla_put_failure;
1397 		if (c->beacon_int_min_gcd &&
1398 		    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1399 				c->beacon_int_min_gcd))
1400 			goto nla_put_failure;
1401 
1402 		nla_nest_end(msg, nl_combi);
1403 	}
1404 
1405 	nla_nest_end(msg, nl_combis);
1406 
1407 	return 0;
1408 nla_put_failure:
1409 	return -ENOBUFS;
1410 }
1411 
1412 #ifdef CONFIG_PM
1413 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1414 					struct sk_buff *msg)
1415 {
1416 	const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1417 	struct nlattr *nl_tcp;
1418 
1419 	if (!tcp)
1420 		return 0;
1421 
1422 	nl_tcp = nla_nest_start_noflag(msg,
1423 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1424 	if (!nl_tcp)
1425 		return -ENOBUFS;
1426 
1427 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1428 			tcp->data_payload_max))
1429 		return -ENOBUFS;
1430 
1431 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1432 			tcp->data_payload_max))
1433 		return -ENOBUFS;
1434 
1435 	if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1436 		return -ENOBUFS;
1437 
1438 	if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1439 				sizeof(*tcp->tok), tcp->tok))
1440 		return -ENOBUFS;
1441 
1442 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1443 			tcp->data_interval_max))
1444 		return -ENOBUFS;
1445 
1446 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1447 			tcp->wake_payload_max))
1448 		return -ENOBUFS;
1449 
1450 	nla_nest_end(msg, nl_tcp);
1451 	return 0;
1452 }
1453 
1454 static int nl80211_send_wowlan(struct sk_buff *msg,
1455 			       struct cfg80211_registered_device *rdev,
1456 			       bool large)
1457 {
1458 	struct nlattr *nl_wowlan;
1459 
1460 	if (!rdev->wiphy.wowlan)
1461 		return 0;
1462 
1463 	nl_wowlan = nla_nest_start_noflag(msg,
1464 					  NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1465 	if (!nl_wowlan)
1466 		return -ENOBUFS;
1467 
1468 	if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1469 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1470 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1471 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1472 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1473 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1474 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1475 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1476 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1477 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1478 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1479 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1480 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1481 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1482 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1483 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1484 		return -ENOBUFS;
1485 
1486 	if (rdev->wiphy.wowlan->n_patterns) {
1487 		struct nl80211_pattern_support pat = {
1488 			.max_patterns = rdev->wiphy.wowlan->n_patterns,
1489 			.min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1490 			.max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1491 			.max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1492 		};
1493 
1494 		if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1495 			    sizeof(pat), &pat))
1496 			return -ENOBUFS;
1497 	}
1498 
1499 	if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1500 	    nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1501 			rdev->wiphy.wowlan->max_nd_match_sets))
1502 		return -ENOBUFS;
1503 
1504 	if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1505 		return -ENOBUFS;
1506 
1507 	nla_nest_end(msg, nl_wowlan);
1508 
1509 	return 0;
1510 }
1511 #endif
1512 
1513 static int nl80211_send_coalesce(struct sk_buff *msg,
1514 				 struct cfg80211_registered_device *rdev)
1515 {
1516 	struct nl80211_coalesce_rule_support rule;
1517 
1518 	if (!rdev->wiphy.coalesce)
1519 		return 0;
1520 
1521 	rule.max_rules = rdev->wiphy.coalesce->n_rules;
1522 	rule.max_delay = rdev->wiphy.coalesce->max_delay;
1523 	rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1524 	rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1525 	rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1526 	rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1527 
1528 	if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1529 		return -ENOBUFS;
1530 
1531 	return 0;
1532 }
1533 
1534 static int
1535 nl80211_send_iftype_data(struct sk_buff *msg,
1536 			 const struct ieee80211_sband_iftype_data *iftdata)
1537 {
1538 	const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1539 
1540 	if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1541 				iftdata->types_mask))
1542 		return -ENOBUFS;
1543 
1544 	if (he_cap->has_he) {
1545 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1546 			    sizeof(he_cap->he_cap_elem.mac_cap_info),
1547 			    he_cap->he_cap_elem.mac_cap_info) ||
1548 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1549 			    sizeof(he_cap->he_cap_elem.phy_cap_info),
1550 			    he_cap->he_cap_elem.phy_cap_info) ||
1551 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1552 			    sizeof(he_cap->he_mcs_nss_supp),
1553 			    &he_cap->he_mcs_nss_supp) ||
1554 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1555 			    sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1556 			return -ENOBUFS;
1557 	}
1558 
1559 	return 0;
1560 }
1561 
1562 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1563 				      struct ieee80211_supported_band *sband)
1564 {
1565 	struct nlattr *nl_rates, *nl_rate;
1566 	struct ieee80211_rate *rate;
1567 	int i;
1568 
1569 	/* add HT info */
1570 	if (sband->ht_cap.ht_supported &&
1571 	    (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1572 		     sizeof(sband->ht_cap.mcs),
1573 		     &sband->ht_cap.mcs) ||
1574 	     nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1575 			 sband->ht_cap.cap) ||
1576 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1577 			sband->ht_cap.ampdu_factor) ||
1578 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1579 			sband->ht_cap.ampdu_density)))
1580 		return -ENOBUFS;
1581 
1582 	/* add VHT info */
1583 	if (sband->vht_cap.vht_supported &&
1584 	    (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1585 		     sizeof(sband->vht_cap.vht_mcs),
1586 		     &sband->vht_cap.vht_mcs) ||
1587 	     nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1588 			 sband->vht_cap.cap)))
1589 		return -ENOBUFS;
1590 
1591 	if (sband->n_iftype_data) {
1592 		struct nlattr *nl_iftype_data =
1593 			nla_nest_start_noflag(msg,
1594 					      NL80211_BAND_ATTR_IFTYPE_DATA);
1595 		int err;
1596 
1597 		if (!nl_iftype_data)
1598 			return -ENOBUFS;
1599 
1600 		for (i = 0; i < sband->n_iftype_data; i++) {
1601 			struct nlattr *iftdata;
1602 
1603 			iftdata = nla_nest_start_noflag(msg, i + 1);
1604 			if (!iftdata)
1605 				return -ENOBUFS;
1606 
1607 			err = nl80211_send_iftype_data(msg,
1608 						       &sband->iftype_data[i]);
1609 			if (err)
1610 				return err;
1611 
1612 			nla_nest_end(msg, iftdata);
1613 		}
1614 
1615 		nla_nest_end(msg, nl_iftype_data);
1616 	}
1617 
1618 	/* add EDMG info */
1619 	if (sband->edmg_cap.channels &&
1620 	    (nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_CHANNELS,
1621 		       sband->edmg_cap.channels) ||
1622 	    nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_BW_CONFIG,
1623 		       sband->edmg_cap.bw_config)))
1624 
1625 		return -ENOBUFS;
1626 
1627 	/* add bitrates */
1628 	nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1629 	if (!nl_rates)
1630 		return -ENOBUFS;
1631 
1632 	for (i = 0; i < sband->n_bitrates; i++) {
1633 		nl_rate = nla_nest_start_noflag(msg, i);
1634 		if (!nl_rate)
1635 			return -ENOBUFS;
1636 
1637 		rate = &sband->bitrates[i];
1638 		if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1639 				rate->bitrate))
1640 			return -ENOBUFS;
1641 		if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1642 		    nla_put_flag(msg,
1643 				 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1644 			return -ENOBUFS;
1645 
1646 		nla_nest_end(msg, nl_rate);
1647 	}
1648 
1649 	nla_nest_end(msg, nl_rates);
1650 
1651 	return 0;
1652 }
1653 
1654 static int
1655 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1656 			 const struct ieee80211_txrx_stypes *mgmt_stypes)
1657 {
1658 	u16 stypes;
1659 	struct nlattr *nl_ftypes, *nl_ifs;
1660 	enum nl80211_iftype ift;
1661 	int i;
1662 
1663 	if (!mgmt_stypes)
1664 		return 0;
1665 
1666 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
1667 	if (!nl_ifs)
1668 		return -ENOBUFS;
1669 
1670 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1671 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1672 		if (!nl_ftypes)
1673 			return -ENOBUFS;
1674 		i = 0;
1675 		stypes = mgmt_stypes[ift].tx;
1676 		while (stypes) {
1677 			if ((stypes & 1) &&
1678 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1679 					(i << 4) | IEEE80211_FTYPE_MGMT))
1680 				return -ENOBUFS;
1681 			stypes >>= 1;
1682 			i++;
1683 		}
1684 		nla_nest_end(msg, nl_ftypes);
1685 	}
1686 
1687 	nla_nest_end(msg, nl_ifs);
1688 
1689 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
1690 	if (!nl_ifs)
1691 		return -ENOBUFS;
1692 
1693 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1694 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1695 		if (!nl_ftypes)
1696 			return -ENOBUFS;
1697 		i = 0;
1698 		stypes = mgmt_stypes[ift].rx;
1699 		while (stypes) {
1700 			if ((stypes & 1) &&
1701 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1702 					(i << 4) | IEEE80211_FTYPE_MGMT))
1703 				return -ENOBUFS;
1704 			stypes >>= 1;
1705 			i++;
1706 		}
1707 		nla_nest_end(msg, nl_ftypes);
1708 	}
1709 	nla_nest_end(msg, nl_ifs);
1710 
1711 	return 0;
1712 }
1713 
1714 #define CMD(op, n)							\
1715 	 do {								\
1716 		if (rdev->ops->op) {					\
1717 			i++;						\
1718 			if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) 	\
1719 				goto nla_put_failure;			\
1720 		}							\
1721 	} while (0)
1722 
1723 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
1724 					struct sk_buff *msg)
1725 {
1726 	int i = 0;
1727 
1728 	/*
1729 	 * do *NOT* add anything into this function, new things need to be
1730 	 * advertised only to new versions of userspace that can deal with
1731 	 * the split (and they can't possibly care about new features...
1732 	 */
1733 	CMD(add_virtual_intf, NEW_INTERFACE);
1734 	CMD(change_virtual_intf, SET_INTERFACE);
1735 	CMD(add_key, NEW_KEY);
1736 	CMD(start_ap, START_AP);
1737 	CMD(add_station, NEW_STATION);
1738 	CMD(add_mpath, NEW_MPATH);
1739 	CMD(update_mesh_config, SET_MESH_CONFIG);
1740 	CMD(change_bss, SET_BSS);
1741 	CMD(auth, AUTHENTICATE);
1742 	CMD(assoc, ASSOCIATE);
1743 	CMD(deauth, DEAUTHENTICATE);
1744 	CMD(disassoc, DISASSOCIATE);
1745 	CMD(join_ibss, JOIN_IBSS);
1746 	CMD(join_mesh, JOIN_MESH);
1747 	CMD(set_pmksa, SET_PMKSA);
1748 	CMD(del_pmksa, DEL_PMKSA);
1749 	CMD(flush_pmksa, FLUSH_PMKSA);
1750 	if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1751 		CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1752 	CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1753 	CMD(mgmt_tx, FRAME);
1754 	CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1755 	if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1756 		i++;
1757 		if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1758 			goto nla_put_failure;
1759 	}
1760 	if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1761 	    rdev->ops->join_mesh) {
1762 		i++;
1763 		if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1764 			goto nla_put_failure;
1765 	}
1766 	CMD(set_wds_peer, SET_WDS_PEER);
1767 	if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1768 		CMD(tdls_mgmt, TDLS_MGMT);
1769 		CMD(tdls_oper, TDLS_OPER);
1770 	}
1771 	if (rdev->wiphy.max_sched_scan_reqs)
1772 		CMD(sched_scan_start, START_SCHED_SCAN);
1773 	CMD(probe_client, PROBE_CLIENT);
1774 	CMD(set_noack_map, SET_NOACK_MAP);
1775 	if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1776 		i++;
1777 		if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1778 			goto nla_put_failure;
1779 	}
1780 	CMD(start_p2p_device, START_P2P_DEVICE);
1781 	CMD(set_mcast_rate, SET_MCAST_RATE);
1782 #ifdef CONFIG_NL80211_TESTMODE
1783 	CMD(testmode_cmd, TESTMODE);
1784 #endif
1785 
1786 	if (rdev->ops->connect || rdev->ops->auth) {
1787 		i++;
1788 		if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1789 			goto nla_put_failure;
1790 	}
1791 
1792 	if (rdev->ops->disconnect || rdev->ops->deauth) {
1793 		i++;
1794 		if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1795 			goto nla_put_failure;
1796 	}
1797 
1798 	return i;
1799  nla_put_failure:
1800 	return -ENOBUFS;
1801 }
1802 
1803 static int
1804 nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
1805 			   struct sk_buff *msg)
1806 {
1807 	struct nlattr *ftm;
1808 
1809 	if (!cap->ftm.supported)
1810 		return 0;
1811 
1812 	ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
1813 	if (!ftm)
1814 		return -ENOBUFS;
1815 
1816 	if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
1817 		return -ENOBUFS;
1818 	if (cap->ftm.non_asap &&
1819 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
1820 		return -ENOBUFS;
1821 	if (cap->ftm.request_lci &&
1822 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
1823 		return -ENOBUFS;
1824 	if (cap->ftm.request_civicloc &&
1825 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
1826 		return -ENOBUFS;
1827 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
1828 			cap->ftm.preambles))
1829 		return -ENOBUFS;
1830 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
1831 			cap->ftm.bandwidths))
1832 		return -ENOBUFS;
1833 	if (cap->ftm.max_bursts_exponent >= 0 &&
1834 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
1835 			cap->ftm.max_bursts_exponent))
1836 		return -ENOBUFS;
1837 	if (cap->ftm.max_ftms_per_burst &&
1838 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
1839 			cap->ftm.max_ftms_per_burst))
1840 		return -ENOBUFS;
1841 
1842 	nla_nest_end(msg, ftm);
1843 	return 0;
1844 }
1845 
1846 static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
1847 				  struct sk_buff *msg)
1848 {
1849 	const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
1850 	struct nlattr *pmsr, *caps;
1851 
1852 	if (!cap)
1853 		return 0;
1854 
1855 	/*
1856 	 * we don't need to clean up anything here since the caller
1857 	 * will genlmsg_cancel() if we fail
1858 	 */
1859 
1860 	pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
1861 	if (!pmsr)
1862 		return -ENOBUFS;
1863 
1864 	if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
1865 		return -ENOBUFS;
1866 
1867 	if (cap->report_ap_tsf &&
1868 	    nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
1869 		return -ENOBUFS;
1870 
1871 	if (cap->randomize_mac_addr &&
1872 	    nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
1873 		return -ENOBUFS;
1874 
1875 	caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
1876 	if (!caps)
1877 		return -ENOBUFS;
1878 
1879 	if (nl80211_send_pmsr_ftm_capa(cap, msg))
1880 		return -ENOBUFS;
1881 
1882 	nla_nest_end(msg, caps);
1883 	nla_nest_end(msg, pmsr);
1884 
1885 	return 0;
1886 }
1887 
1888 struct nl80211_dump_wiphy_state {
1889 	s64 filter_wiphy;
1890 	long start;
1891 	long split_start, band_start, chan_start, capa_start;
1892 	bool split;
1893 };
1894 
1895 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
1896 			      enum nl80211_commands cmd,
1897 			      struct sk_buff *msg, u32 portid, u32 seq,
1898 			      int flags, struct nl80211_dump_wiphy_state *state)
1899 {
1900 	void *hdr;
1901 	struct nlattr *nl_bands, *nl_band;
1902 	struct nlattr *nl_freqs, *nl_freq;
1903 	struct nlattr *nl_cmds;
1904 	enum nl80211_band band;
1905 	struct ieee80211_channel *chan;
1906 	int i;
1907 	const struct ieee80211_txrx_stypes *mgmt_stypes =
1908 				rdev->wiphy.mgmt_stypes;
1909 	u32 features;
1910 
1911 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
1912 	if (!hdr)
1913 		return -ENOBUFS;
1914 
1915 	if (WARN_ON(!state))
1916 		return -EINVAL;
1917 
1918 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1919 	    nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1920 			   wiphy_name(&rdev->wiphy)) ||
1921 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
1922 			cfg80211_rdev_list_generation))
1923 		goto nla_put_failure;
1924 
1925 	if (cmd != NL80211_CMD_NEW_WIPHY)
1926 		goto finish;
1927 
1928 	switch (state->split_start) {
1929 	case 0:
1930 		if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1931 			       rdev->wiphy.retry_short) ||
1932 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1933 			       rdev->wiphy.retry_long) ||
1934 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1935 				rdev->wiphy.frag_threshold) ||
1936 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1937 				rdev->wiphy.rts_threshold) ||
1938 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1939 			       rdev->wiphy.coverage_class) ||
1940 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1941 			       rdev->wiphy.max_scan_ssids) ||
1942 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1943 			       rdev->wiphy.max_sched_scan_ssids) ||
1944 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1945 				rdev->wiphy.max_scan_ie_len) ||
1946 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1947 				rdev->wiphy.max_sched_scan_ie_len) ||
1948 		    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1949 			       rdev->wiphy.max_match_sets) ||
1950 		    nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
1951 				rdev->wiphy.max_sched_scan_plans) ||
1952 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
1953 				rdev->wiphy.max_sched_scan_plan_interval) ||
1954 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
1955 				rdev->wiphy.max_sched_scan_plan_iterations))
1956 			goto nla_put_failure;
1957 
1958 		if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1959 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1960 			goto nla_put_failure;
1961 		if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1962 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1963 			goto nla_put_failure;
1964 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1965 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1966 			goto nla_put_failure;
1967 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1968 		    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1969 			goto nla_put_failure;
1970 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1971 		    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1972 			goto nla_put_failure;
1973 		if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1974 		    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1975 			goto nla_put_failure;
1976 		state->split_start++;
1977 		if (state->split)
1978 			break;
1979 		/* fall through */
1980 	case 1:
1981 		if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1982 			    sizeof(u32) * rdev->wiphy.n_cipher_suites,
1983 			    rdev->wiphy.cipher_suites))
1984 			goto nla_put_failure;
1985 
1986 		if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1987 			       rdev->wiphy.max_num_pmkids))
1988 			goto nla_put_failure;
1989 
1990 		if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1991 		    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1992 			goto nla_put_failure;
1993 
1994 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1995 				rdev->wiphy.available_antennas_tx) ||
1996 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1997 				rdev->wiphy.available_antennas_rx))
1998 			goto nla_put_failure;
1999 
2000 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
2001 		    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
2002 				rdev->wiphy.probe_resp_offload))
2003 			goto nla_put_failure;
2004 
2005 		if ((rdev->wiphy.available_antennas_tx ||
2006 		     rdev->wiphy.available_antennas_rx) &&
2007 		    rdev->ops->get_antenna) {
2008 			u32 tx_ant = 0, rx_ant = 0;
2009 			int res;
2010 
2011 			res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
2012 			if (!res) {
2013 				if (nla_put_u32(msg,
2014 						NL80211_ATTR_WIPHY_ANTENNA_TX,
2015 						tx_ant) ||
2016 				    nla_put_u32(msg,
2017 						NL80211_ATTR_WIPHY_ANTENNA_RX,
2018 						rx_ant))
2019 					goto nla_put_failure;
2020 			}
2021 		}
2022 
2023 		state->split_start++;
2024 		if (state->split)
2025 			break;
2026 		/* fall through */
2027 	case 2:
2028 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
2029 					rdev->wiphy.interface_modes))
2030 				goto nla_put_failure;
2031 		state->split_start++;
2032 		if (state->split)
2033 			break;
2034 		/* fall through */
2035 	case 3:
2036 		nl_bands = nla_nest_start_noflag(msg,
2037 						 NL80211_ATTR_WIPHY_BANDS);
2038 		if (!nl_bands)
2039 			goto nla_put_failure;
2040 
2041 		for (band = state->band_start;
2042 		     band < NUM_NL80211_BANDS; band++) {
2043 			struct ieee80211_supported_band *sband;
2044 
2045 			sband = rdev->wiphy.bands[band];
2046 
2047 			if (!sband)
2048 				continue;
2049 
2050 			nl_band = nla_nest_start_noflag(msg, band);
2051 			if (!nl_band)
2052 				goto nla_put_failure;
2053 
2054 			switch (state->chan_start) {
2055 			case 0:
2056 				if (nl80211_send_band_rateinfo(msg, sband))
2057 					goto nla_put_failure;
2058 				state->chan_start++;
2059 				if (state->split)
2060 					break;
2061 				/* fall through */
2062 			default:
2063 				/* add frequencies */
2064 				nl_freqs = nla_nest_start_noflag(msg,
2065 								 NL80211_BAND_ATTR_FREQS);
2066 				if (!nl_freqs)
2067 					goto nla_put_failure;
2068 
2069 				for (i = state->chan_start - 1;
2070 				     i < sband->n_channels;
2071 				     i++) {
2072 					nl_freq = nla_nest_start_noflag(msg,
2073 									i);
2074 					if (!nl_freq)
2075 						goto nla_put_failure;
2076 
2077 					chan = &sband->channels[i];
2078 
2079 					if (nl80211_msg_put_channel(
2080 							msg, &rdev->wiphy, chan,
2081 							state->split))
2082 						goto nla_put_failure;
2083 
2084 					nla_nest_end(msg, nl_freq);
2085 					if (state->split)
2086 						break;
2087 				}
2088 				if (i < sband->n_channels)
2089 					state->chan_start = i + 2;
2090 				else
2091 					state->chan_start = 0;
2092 				nla_nest_end(msg, nl_freqs);
2093 			}
2094 
2095 			nla_nest_end(msg, nl_band);
2096 
2097 			if (state->split) {
2098 				/* start again here */
2099 				if (state->chan_start)
2100 					band--;
2101 				break;
2102 			}
2103 		}
2104 		nla_nest_end(msg, nl_bands);
2105 
2106 		if (band < NUM_NL80211_BANDS)
2107 			state->band_start = band + 1;
2108 		else
2109 			state->band_start = 0;
2110 
2111 		/* if bands & channels are done, continue outside */
2112 		if (state->band_start == 0 && state->chan_start == 0)
2113 			state->split_start++;
2114 		if (state->split)
2115 			break;
2116 		/* fall through */
2117 	case 4:
2118 		nl_cmds = nla_nest_start_noflag(msg,
2119 						NL80211_ATTR_SUPPORTED_COMMANDS);
2120 		if (!nl_cmds)
2121 			goto nla_put_failure;
2122 
2123 		i = nl80211_add_commands_unsplit(rdev, msg);
2124 		if (i < 0)
2125 			goto nla_put_failure;
2126 		if (state->split) {
2127 			CMD(crit_proto_start, CRIT_PROTOCOL_START);
2128 			CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2129 			if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2130 				CMD(channel_switch, CHANNEL_SWITCH);
2131 			CMD(set_qos_map, SET_QOS_MAP);
2132 			if (rdev->wiphy.features &
2133 					NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2134 				CMD(add_tx_ts, ADD_TX_TS);
2135 			CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2136 			CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2137 			CMD(update_ft_ies, UPDATE_FT_IES);
2138 		}
2139 #undef CMD
2140 
2141 		nla_nest_end(msg, nl_cmds);
2142 		state->split_start++;
2143 		if (state->split)
2144 			break;
2145 		/* fall through */
2146 	case 5:
2147 		if (rdev->ops->remain_on_channel &&
2148 		    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2149 		    nla_put_u32(msg,
2150 				NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2151 				rdev->wiphy.max_remain_on_channel_duration))
2152 			goto nla_put_failure;
2153 
2154 		if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2155 		    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2156 			goto nla_put_failure;
2157 
2158 		if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2159 			goto nla_put_failure;
2160 		state->split_start++;
2161 		if (state->split)
2162 			break;
2163 		/* fall through */
2164 	case 6:
2165 #ifdef CONFIG_PM
2166 		if (nl80211_send_wowlan(msg, rdev, state->split))
2167 			goto nla_put_failure;
2168 		state->split_start++;
2169 		if (state->split)
2170 			break;
2171 #else
2172 		state->split_start++;
2173 #endif
2174 		/* fall through */
2175 	case 7:
2176 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2177 					rdev->wiphy.software_iftypes))
2178 			goto nla_put_failure;
2179 
2180 		if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2181 						   state->split))
2182 			goto nla_put_failure;
2183 
2184 		state->split_start++;
2185 		if (state->split)
2186 			break;
2187 		/* fall through */
2188 	case 8:
2189 		if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2190 		    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2191 				rdev->wiphy.ap_sme_capa))
2192 			goto nla_put_failure;
2193 
2194 		features = rdev->wiphy.features;
2195 		/*
2196 		 * We can only add the per-channel limit information if the
2197 		 * dump is split, otherwise it makes it too big. Therefore
2198 		 * only advertise it in that case.
2199 		 */
2200 		if (state->split)
2201 			features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2202 		if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2203 			goto nla_put_failure;
2204 
2205 		if (rdev->wiphy.ht_capa_mod_mask &&
2206 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2207 			    sizeof(*rdev->wiphy.ht_capa_mod_mask),
2208 			    rdev->wiphy.ht_capa_mod_mask))
2209 			goto nla_put_failure;
2210 
2211 		if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2212 		    rdev->wiphy.max_acl_mac_addrs &&
2213 		    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2214 				rdev->wiphy.max_acl_mac_addrs))
2215 			goto nla_put_failure;
2216 
2217 		/*
2218 		 * Any information below this point is only available to
2219 		 * applications that can deal with it being split. This
2220 		 * helps ensure that newly added capabilities don't break
2221 		 * older tools by overrunning their buffers.
2222 		 *
2223 		 * We still increment split_start so that in the split
2224 		 * case we'll continue with more data in the next round,
2225 		 * but break unconditionally so unsplit data stops here.
2226 		 */
2227 		state->split_start++;
2228 		break;
2229 	case 9:
2230 		if (rdev->wiphy.extended_capabilities &&
2231 		    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2232 			     rdev->wiphy.extended_capabilities_len,
2233 			     rdev->wiphy.extended_capabilities) ||
2234 		     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2235 			     rdev->wiphy.extended_capabilities_len,
2236 			     rdev->wiphy.extended_capabilities_mask)))
2237 			goto nla_put_failure;
2238 
2239 		if (rdev->wiphy.vht_capa_mod_mask &&
2240 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2241 			    sizeof(*rdev->wiphy.vht_capa_mod_mask),
2242 			    rdev->wiphy.vht_capa_mod_mask))
2243 			goto nla_put_failure;
2244 
2245 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2246 			    rdev->wiphy.perm_addr))
2247 			goto nla_put_failure;
2248 
2249 		if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2250 		    nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2251 			    rdev->wiphy.addr_mask))
2252 			goto nla_put_failure;
2253 
2254 		if (rdev->wiphy.n_addresses > 1) {
2255 			void *attr;
2256 
2257 			attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2258 			if (!attr)
2259 				goto nla_put_failure;
2260 
2261 			for (i = 0; i < rdev->wiphy.n_addresses; i++)
2262 				if (nla_put(msg, i + 1, ETH_ALEN,
2263 					    rdev->wiphy.addresses[i].addr))
2264 					goto nla_put_failure;
2265 
2266 			nla_nest_end(msg, attr);
2267 		}
2268 
2269 		state->split_start++;
2270 		break;
2271 	case 10:
2272 		if (nl80211_send_coalesce(msg, rdev))
2273 			goto nla_put_failure;
2274 
2275 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2276 		    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2277 		     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2278 			goto nla_put_failure;
2279 
2280 		if (rdev->wiphy.max_ap_assoc_sta &&
2281 		    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2282 				rdev->wiphy.max_ap_assoc_sta))
2283 			goto nla_put_failure;
2284 
2285 		state->split_start++;
2286 		break;
2287 	case 11:
2288 		if (rdev->wiphy.n_vendor_commands) {
2289 			const struct nl80211_vendor_cmd_info *info;
2290 			struct nlattr *nested;
2291 
2292 			nested = nla_nest_start_noflag(msg,
2293 						       NL80211_ATTR_VENDOR_DATA);
2294 			if (!nested)
2295 				goto nla_put_failure;
2296 
2297 			for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2298 				info = &rdev->wiphy.vendor_commands[i].info;
2299 				if (nla_put(msg, i + 1, sizeof(*info), info))
2300 					goto nla_put_failure;
2301 			}
2302 			nla_nest_end(msg, nested);
2303 		}
2304 
2305 		if (rdev->wiphy.n_vendor_events) {
2306 			const struct nl80211_vendor_cmd_info *info;
2307 			struct nlattr *nested;
2308 
2309 			nested = nla_nest_start_noflag(msg,
2310 						       NL80211_ATTR_VENDOR_EVENTS);
2311 			if (!nested)
2312 				goto nla_put_failure;
2313 
2314 			for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2315 				info = &rdev->wiphy.vendor_events[i];
2316 				if (nla_put(msg, i + 1, sizeof(*info), info))
2317 					goto nla_put_failure;
2318 			}
2319 			nla_nest_end(msg, nested);
2320 		}
2321 		state->split_start++;
2322 		break;
2323 	case 12:
2324 		if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2325 		    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2326 			       rdev->wiphy.max_num_csa_counters))
2327 			goto nla_put_failure;
2328 
2329 		if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2330 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2331 			goto nla_put_failure;
2332 
2333 		if (rdev->wiphy.max_sched_scan_reqs &&
2334 		    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2335 				rdev->wiphy.max_sched_scan_reqs))
2336 			goto nla_put_failure;
2337 
2338 		if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2339 			    sizeof(rdev->wiphy.ext_features),
2340 			    rdev->wiphy.ext_features))
2341 			goto nla_put_failure;
2342 
2343 		if (rdev->wiphy.bss_select_support) {
2344 			struct nlattr *nested;
2345 			u32 bss_select_support = rdev->wiphy.bss_select_support;
2346 
2347 			nested = nla_nest_start_noflag(msg,
2348 						       NL80211_ATTR_BSS_SELECT);
2349 			if (!nested)
2350 				goto nla_put_failure;
2351 
2352 			i = 0;
2353 			while (bss_select_support) {
2354 				if ((bss_select_support & 1) &&
2355 				    nla_put_flag(msg, i))
2356 					goto nla_put_failure;
2357 				i++;
2358 				bss_select_support >>= 1;
2359 			}
2360 			nla_nest_end(msg, nested);
2361 		}
2362 
2363 		state->split_start++;
2364 		break;
2365 	case 13:
2366 		if (rdev->wiphy.num_iftype_ext_capab &&
2367 		    rdev->wiphy.iftype_ext_capab) {
2368 			struct nlattr *nested_ext_capab, *nested;
2369 
2370 			nested = nla_nest_start_noflag(msg,
2371 						       NL80211_ATTR_IFTYPE_EXT_CAPA);
2372 			if (!nested)
2373 				goto nla_put_failure;
2374 
2375 			for (i = state->capa_start;
2376 			     i < rdev->wiphy.num_iftype_ext_capab; i++) {
2377 				const struct wiphy_iftype_ext_capab *capab;
2378 
2379 				capab = &rdev->wiphy.iftype_ext_capab[i];
2380 
2381 				nested_ext_capab = nla_nest_start_noflag(msg,
2382 									 i);
2383 				if (!nested_ext_capab ||
2384 				    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2385 						capab->iftype) ||
2386 				    nla_put(msg, NL80211_ATTR_EXT_CAPA,
2387 					    capab->extended_capabilities_len,
2388 					    capab->extended_capabilities) ||
2389 				    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2390 					    capab->extended_capabilities_len,
2391 					    capab->extended_capabilities_mask))
2392 					goto nla_put_failure;
2393 
2394 				nla_nest_end(msg, nested_ext_capab);
2395 				if (state->split)
2396 					break;
2397 			}
2398 			nla_nest_end(msg, nested);
2399 			if (i < rdev->wiphy.num_iftype_ext_capab) {
2400 				state->capa_start = i + 1;
2401 				break;
2402 			}
2403 		}
2404 
2405 		if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2406 				rdev->wiphy.nan_supported_bands))
2407 			goto nla_put_failure;
2408 
2409 		if (wiphy_ext_feature_isset(&rdev->wiphy,
2410 					    NL80211_EXT_FEATURE_TXQS)) {
2411 			struct cfg80211_txq_stats txqstats = {};
2412 			int res;
2413 
2414 			res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2415 			if (!res &&
2416 			    !nl80211_put_txq_stats(msg, &txqstats,
2417 						   NL80211_ATTR_TXQ_STATS))
2418 				goto nla_put_failure;
2419 
2420 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2421 					rdev->wiphy.txq_limit))
2422 				goto nla_put_failure;
2423 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2424 					rdev->wiphy.txq_memory_limit))
2425 				goto nla_put_failure;
2426 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2427 					rdev->wiphy.txq_quantum))
2428 				goto nla_put_failure;
2429 		}
2430 
2431 		state->split_start++;
2432 		break;
2433 	case 14:
2434 		if (nl80211_send_pmsr_capa(rdev, msg))
2435 			goto nla_put_failure;
2436 
2437 		state->split_start++;
2438 		break;
2439 	case 15:
2440 		if (rdev->wiphy.akm_suites &&
2441 		    nla_put(msg, NL80211_ATTR_AKM_SUITES,
2442 			    sizeof(u32) * rdev->wiphy.n_akm_suites,
2443 			    rdev->wiphy.akm_suites))
2444 			goto nla_put_failure;
2445 
2446 		/* done */
2447 		state->split_start = 0;
2448 		break;
2449 	}
2450  finish:
2451 	genlmsg_end(msg, hdr);
2452 	return 0;
2453 
2454  nla_put_failure:
2455 	genlmsg_cancel(msg, hdr);
2456 	return -EMSGSIZE;
2457 }
2458 
2459 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
2460 				    struct netlink_callback *cb,
2461 				    struct nl80211_dump_wiphy_state *state)
2462 {
2463 	struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
2464 	int ret;
2465 
2466 	if (!tb)
2467 		return -ENOMEM;
2468 
2469 	ret = nlmsg_parse_deprecated(cb->nlh,
2470 				     GENL_HDRLEN + nl80211_fam.hdrsize,
2471 				     tb, nl80211_fam.maxattr,
2472 				     nl80211_policy, NULL);
2473 	/* ignore parse errors for backward compatibility */
2474 	if (ret) {
2475 		ret = 0;
2476 		goto out;
2477 	}
2478 
2479 	state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
2480 	if (tb[NL80211_ATTR_WIPHY])
2481 		state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2482 	if (tb[NL80211_ATTR_WDEV])
2483 		state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
2484 	if (tb[NL80211_ATTR_IFINDEX]) {
2485 		struct net_device *netdev;
2486 		struct cfg80211_registered_device *rdev;
2487 		int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2488 
2489 		netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
2490 		if (!netdev) {
2491 			ret = -ENODEV;
2492 			goto out;
2493 		}
2494 		if (netdev->ieee80211_ptr) {
2495 			rdev = wiphy_to_rdev(
2496 				netdev->ieee80211_ptr->wiphy);
2497 			state->filter_wiphy = rdev->wiphy_idx;
2498 		}
2499 	}
2500 
2501 	ret = 0;
2502 out:
2503 	kfree(tb);
2504 	return ret;
2505 }
2506 
2507 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
2508 {
2509 	int idx = 0, ret;
2510 	struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
2511 	struct cfg80211_registered_device *rdev;
2512 
2513 	rtnl_lock();
2514 	if (!state) {
2515 		state = kzalloc(sizeof(*state), GFP_KERNEL);
2516 		if (!state) {
2517 			rtnl_unlock();
2518 			return -ENOMEM;
2519 		}
2520 		state->filter_wiphy = -1;
2521 		ret = nl80211_dump_wiphy_parse(skb, cb, state);
2522 		if (ret) {
2523 			kfree(state);
2524 			rtnl_unlock();
2525 			return ret;
2526 		}
2527 		cb->args[0] = (long)state;
2528 	}
2529 
2530 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2531 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2532 			continue;
2533 		if (++idx <= state->start)
2534 			continue;
2535 		if (state->filter_wiphy != -1 &&
2536 		    state->filter_wiphy != rdev->wiphy_idx)
2537 			continue;
2538 		/* attempt to fit multiple wiphy data chunks into the skb */
2539 		do {
2540 			ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
2541 						 skb,
2542 						 NETLINK_CB(cb->skb).portid,
2543 						 cb->nlh->nlmsg_seq,
2544 						 NLM_F_MULTI, state);
2545 			if (ret < 0) {
2546 				/*
2547 				 * If sending the wiphy data didn't fit (ENOBUFS
2548 				 * or EMSGSIZE returned), this SKB is still
2549 				 * empty (so it's not too big because another
2550 				 * wiphy dataset is already in the skb) and
2551 				 * we've not tried to adjust the dump allocation
2552 				 * yet ... then adjust the alloc size to be
2553 				 * bigger, and return 1 but with the empty skb.
2554 				 * This results in an empty message being RX'ed
2555 				 * in userspace, but that is ignored.
2556 				 *
2557 				 * We can then retry with the larger buffer.
2558 				 */
2559 				if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
2560 				    !skb->len && !state->split &&
2561 				    cb->min_dump_alloc < 4096) {
2562 					cb->min_dump_alloc = 4096;
2563 					state->split_start = 0;
2564 					rtnl_unlock();
2565 					return 1;
2566 				}
2567 				idx--;
2568 				break;
2569 			}
2570 		} while (state->split_start > 0);
2571 		break;
2572 	}
2573 	rtnl_unlock();
2574 
2575 	state->start = idx;
2576 
2577 	return skb->len;
2578 }
2579 
2580 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
2581 {
2582 	kfree((void *)cb->args[0]);
2583 	return 0;
2584 }
2585 
2586 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
2587 {
2588 	struct sk_buff *msg;
2589 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2590 	struct nl80211_dump_wiphy_state state = {};
2591 
2592 	msg = nlmsg_new(4096, GFP_KERNEL);
2593 	if (!msg)
2594 		return -ENOMEM;
2595 
2596 	if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
2597 			       info->snd_portid, info->snd_seq, 0,
2598 			       &state) < 0) {
2599 		nlmsg_free(msg);
2600 		return -ENOBUFS;
2601 	}
2602 
2603 	return genlmsg_reply(msg, info);
2604 }
2605 
2606 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
2607 	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
2608 	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
2609 	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
2610 	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
2611 	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
2612 };
2613 
2614 static int parse_txq_params(struct nlattr *tb[],
2615 			    struct ieee80211_txq_params *txq_params)
2616 {
2617 	u8 ac;
2618 
2619 	if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
2620 	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
2621 	    !tb[NL80211_TXQ_ATTR_AIFS])
2622 		return -EINVAL;
2623 
2624 	ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
2625 	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
2626 	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
2627 	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
2628 	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
2629 
2630 	if (ac >= NL80211_NUM_ACS)
2631 		return -EINVAL;
2632 	txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
2633 	return 0;
2634 }
2635 
2636 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
2637 {
2638 	/*
2639 	 * You can only set the channel explicitly for WDS interfaces,
2640 	 * all others have their channel managed via their respective
2641 	 * "establish a connection" command (connect, join, ...)
2642 	 *
2643 	 * For AP/GO and mesh mode, the channel can be set with the
2644 	 * channel userspace API, but is only stored and passed to the
2645 	 * low-level driver when the AP starts or the mesh is joined.
2646 	 * This is for backward compatibility, userspace can also give
2647 	 * the channel in the start-ap or join-mesh commands instead.
2648 	 *
2649 	 * Monitors are special as they are normally slaved to
2650 	 * whatever else is going on, so they have their own special
2651 	 * operation to set the monitor channel if possible.
2652 	 */
2653 	return !wdev ||
2654 		wdev->iftype == NL80211_IFTYPE_AP ||
2655 		wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
2656 		wdev->iftype == NL80211_IFTYPE_MONITOR ||
2657 		wdev->iftype == NL80211_IFTYPE_P2P_GO;
2658 }
2659 
2660 int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
2661 			  struct genl_info *info,
2662 			  struct cfg80211_chan_def *chandef)
2663 {
2664 	struct netlink_ext_ack *extack = info->extack;
2665 	struct nlattr **attrs = info->attrs;
2666 	u32 control_freq;
2667 
2668 	if (!attrs[NL80211_ATTR_WIPHY_FREQ])
2669 		return -EINVAL;
2670 
2671 	control_freq = nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ]);
2672 
2673 	memset(chandef, 0, sizeof(*chandef));
2674 
2675 	chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
2676 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
2677 	chandef->center_freq1 = control_freq;
2678 	chandef->center_freq2 = 0;
2679 
2680 	/* Primary channel not allowed */
2681 	if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED) {
2682 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
2683 				    "Channel is disabled");
2684 		return -EINVAL;
2685 	}
2686 
2687 	if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2688 		enum nl80211_channel_type chantype;
2689 
2690 		chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2691 
2692 		switch (chantype) {
2693 		case NL80211_CHAN_NO_HT:
2694 		case NL80211_CHAN_HT20:
2695 		case NL80211_CHAN_HT40PLUS:
2696 		case NL80211_CHAN_HT40MINUS:
2697 			cfg80211_chandef_create(chandef, chandef->chan,
2698 						chantype);
2699 			/* user input for center_freq is incorrect */
2700 			if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
2701 			    chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
2702 				NL_SET_ERR_MSG_ATTR(extack,
2703 						    attrs[NL80211_ATTR_CENTER_FREQ1],
2704 						    "bad center frequency 1");
2705 				return -EINVAL;
2706 			}
2707 			/* center_freq2 must be zero */
2708 			if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
2709 			    nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
2710 				NL_SET_ERR_MSG_ATTR(extack,
2711 						    attrs[NL80211_ATTR_CENTER_FREQ2],
2712 						    "center frequency 2 can't be used");
2713 				return -EINVAL;
2714 			}
2715 			break;
2716 		default:
2717 			NL_SET_ERR_MSG_ATTR(extack,
2718 					    attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
2719 					    "invalid channel type");
2720 			return -EINVAL;
2721 		}
2722 	} else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
2723 		chandef->width =
2724 			nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
2725 		if (attrs[NL80211_ATTR_CENTER_FREQ1])
2726 			chandef->center_freq1 =
2727 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
2728 		if (attrs[NL80211_ATTR_CENTER_FREQ2])
2729 			chandef->center_freq2 =
2730 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
2731 	}
2732 
2733 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
2734 		chandef->edmg.channels =
2735 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
2736 
2737 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
2738 			chandef->edmg.bw_config =
2739 		     nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
2740 	} else {
2741 		chandef->edmg.bw_config = 0;
2742 		chandef->edmg.channels = 0;
2743 	}
2744 
2745 	if (!cfg80211_chandef_valid(chandef)) {
2746 		NL_SET_ERR_MSG(extack, "invalid channel definition");
2747 		return -EINVAL;
2748 	}
2749 
2750 	if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
2751 				     IEEE80211_CHAN_DISABLED)) {
2752 		NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
2753 		return -EINVAL;
2754 	}
2755 
2756 	if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
2757 	     chandef->width == NL80211_CHAN_WIDTH_10) &&
2758 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
2759 		NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
2760 		return -EINVAL;
2761 	}
2762 
2763 	return 0;
2764 }
2765 
2766 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
2767 				 struct net_device *dev,
2768 				 struct genl_info *info)
2769 {
2770 	struct cfg80211_chan_def chandef;
2771 	int result;
2772 	enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
2773 	struct wireless_dev *wdev = NULL;
2774 
2775 	if (dev)
2776 		wdev = dev->ieee80211_ptr;
2777 	if (!nl80211_can_set_dev_channel(wdev))
2778 		return -EOPNOTSUPP;
2779 	if (wdev)
2780 		iftype = wdev->iftype;
2781 
2782 	result = nl80211_parse_chandef(rdev, info, &chandef);
2783 	if (result)
2784 		return result;
2785 
2786 	switch (iftype) {
2787 	case NL80211_IFTYPE_AP:
2788 	case NL80211_IFTYPE_P2P_GO:
2789 		if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
2790 						   iftype)) {
2791 			result = -EINVAL;
2792 			break;
2793 		}
2794 		if (wdev->beacon_interval) {
2795 			if (!dev || !rdev->ops->set_ap_chanwidth ||
2796 			    !(rdev->wiphy.features &
2797 			      NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
2798 				result = -EBUSY;
2799 				break;
2800 			}
2801 
2802 			/* Only allow dynamic channel width changes */
2803 			if (chandef.chan != wdev->preset_chandef.chan) {
2804 				result = -EBUSY;
2805 				break;
2806 			}
2807 			result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
2808 			if (result)
2809 				break;
2810 		}
2811 		wdev->preset_chandef = chandef;
2812 		result = 0;
2813 		break;
2814 	case NL80211_IFTYPE_MESH_POINT:
2815 		result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
2816 		break;
2817 	case NL80211_IFTYPE_MONITOR:
2818 		result = cfg80211_set_monitor_channel(rdev, &chandef);
2819 		break;
2820 	default:
2821 		result = -EINVAL;
2822 	}
2823 
2824 	return result;
2825 }
2826 
2827 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2828 {
2829 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2830 	struct net_device *netdev = info->user_ptr[1];
2831 
2832 	return __nl80211_set_channel(rdev, netdev, info);
2833 }
2834 
2835 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2836 {
2837 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2838 	struct net_device *dev = info->user_ptr[1];
2839 	struct wireless_dev *wdev = dev->ieee80211_ptr;
2840 	const u8 *bssid;
2841 
2842 	if (!info->attrs[NL80211_ATTR_MAC])
2843 		return -EINVAL;
2844 
2845 	if (netif_running(dev))
2846 		return -EBUSY;
2847 
2848 	if (!rdev->ops->set_wds_peer)
2849 		return -EOPNOTSUPP;
2850 
2851 	if (wdev->iftype != NL80211_IFTYPE_WDS)
2852 		return -EOPNOTSUPP;
2853 
2854 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
2855 	return rdev_set_wds_peer(rdev, dev, bssid);
2856 }
2857 
2858 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
2859 {
2860 	struct cfg80211_registered_device *rdev;
2861 	struct net_device *netdev = NULL;
2862 	struct wireless_dev *wdev;
2863 	int result = 0, rem_txq_params = 0;
2864 	struct nlattr *nl_txq_params;
2865 	u32 changed;
2866 	u8 retry_short = 0, retry_long = 0;
2867 	u32 frag_threshold = 0, rts_threshold = 0;
2868 	u8 coverage_class = 0;
2869 	u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
2870 
2871 	ASSERT_RTNL();
2872 
2873 	/*
2874 	 * Try to find the wiphy and netdev. Normally this
2875 	 * function shouldn't need the netdev, but this is
2876 	 * done for backward compatibility -- previously
2877 	 * setting the channel was done per wiphy, but now
2878 	 * it is per netdev. Previous userland like hostapd
2879 	 * also passed a netdev to set_wiphy, so that it is
2880 	 * possible to let that go to the right netdev!
2881 	 */
2882 
2883 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
2884 		int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2885 
2886 		netdev = __dev_get_by_index(genl_info_net(info), ifindex);
2887 		if (netdev && netdev->ieee80211_ptr)
2888 			rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
2889 		else
2890 			netdev = NULL;
2891 	}
2892 
2893 	if (!netdev) {
2894 		rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2895 						  info->attrs);
2896 		if (IS_ERR(rdev))
2897 			return PTR_ERR(rdev);
2898 		wdev = NULL;
2899 		netdev = NULL;
2900 		result = 0;
2901 	} else
2902 		wdev = netdev->ieee80211_ptr;
2903 
2904 	/*
2905 	 * end workaround code, by now the rdev is available
2906 	 * and locked, and wdev may or may not be NULL.
2907 	 */
2908 
2909 	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
2910 		result = cfg80211_dev_rename(
2911 			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
2912 
2913 	if (result)
2914 		return result;
2915 
2916 	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2917 		struct ieee80211_txq_params txq_params;
2918 		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2919 
2920 		if (!rdev->ops->set_txq_params)
2921 			return -EOPNOTSUPP;
2922 
2923 		if (!netdev)
2924 			return -EINVAL;
2925 
2926 		if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2927 		    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2928 			return -EINVAL;
2929 
2930 		if (!netif_running(netdev))
2931 			return -ENETDOWN;
2932 
2933 		nla_for_each_nested(nl_txq_params,
2934 				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2935 				    rem_txq_params) {
2936 			result = nla_parse_nested_deprecated(tb,
2937 							     NL80211_TXQ_ATTR_MAX,
2938 							     nl_txq_params,
2939 							     txq_params_policy,
2940 							     info->extack);
2941 			if (result)
2942 				return result;
2943 			result = parse_txq_params(tb, &txq_params);
2944 			if (result)
2945 				return result;
2946 
2947 			result = rdev_set_txq_params(rdev, netdev,
2948 						     &txq_params);
2949 			if (result)
2950 				return result;
2951 		}
2952 	}
2953 
2954 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2955 		result = __nl80211_set_channel(
2956 			rdev,
2957 			nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
2958 			info);
2959 		if (result)
2960 			return result;
2961 	}
2962 
2963 	if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
2964 		struct wireless_dev *txp_wdev = wdev;
2965 		enum nl80211_tx_power_setting type;
2966 		int idx, mbm = 0;
2967 
2968 		if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2969 			txp_wdev = NULL;
2970 
2971 		if (!rdev->ops->set_tx_power)
2972 			return -EOPNOTSUPP;
2973 
2974 		idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2975 		type = nla_get_u32(info->attrs[idx]);
2976 
2977 		if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
2978 		    (type != NL80211_TX_POWER_AUTOMATIC))
2979 			return -EINVAL;
2980 
2981 		if (type != NL80211_TX_POWER_AUTOMATIC) {
2982 			idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2983 			mbm = nla_get_u32(info->attrs[idx]);
2984 		}
2985 
2986 		result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
2987 		if (result)
2988 			return result;
2989 	}
2990 
2991 	if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2992 	    info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2993 		u32 tx_ant, rx_ant;
2994 
2995 		if ((!rdev->wiphy.available_antennas_tx &&
2996 		     !rdev->wiphy.available_antennas_rx) ||
2997 		    !rdev->ops->set_antenna)
2998 			return -EOPNOTSUPP;
2999 
3000 		tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
3001 		rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
3002 
3003 		/* reject antenna configurations which don't match the
3004 		 * available antenna masks, except for the "all" mask */
3005 		if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
3006 		    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
3007 			return -EINVAL;
3008 
3009 		tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
3010 		rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
3011 
3012 		result = rdev_set_antenna(rdev, tx_ant, rx_ant);
3013 		if (result)
3014 			return result;
3015 	}
3016 
3017 	changed = 0;
3018 
3019 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
3020 		retry_short = nla_get_u8(
3021 			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
3022 
3023 		changed |= WIPHY_PARAM_RETRY_SHORT;
3024 	}
3025 
3026 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
3027 		retry_long = nla_get_u8(
3028 			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
3029 
3030 		changed |= WIPHY_PARAM_RETRY_LONG;
3031 	}
3032 
3033 	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
3034 		frag_threshold = nla_get_u32(
3035 			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
3036 		if (frag_threshold < 256)
3037 			return -EINVAL;
3038 
3039 		if (frag_threshold != (u32) -1) {
3040 			/*
3041 			 * Fragments (apart from the last one) are required to
3042 			 * have even length. Make the fragmentation code
3043 			 * simpler by stripping LSB should someone try to use
3044 			 * odd threshold value.
3045 			 */
3046 			frag_threshold &= ~0x1;
3047 		}
3048 		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
3049 	}
3050 
3051 	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
3052 		rts_threshold = nla_get_u32(
3053 			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
3054 		changed |= WIPHY_PARAM_RTS_THRESHOLD;
3055 	}
3056 
3057 	if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
3058 		if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
3059 			return -EINVAL;
3060 
3061 		coverage_class = nla_get_u8(
3062 			info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
3063 		changed |= WIPHY_PARAM_COVERAGE_CLASS;
3064 	}
3065 
3066 	if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3067 		if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
3068 			return -EOPNOTSUPP;
3069 
3070 		changed |= WIPHY_PARAM_DYN_ACK;
3071 	}
3072 
3073 	if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3074 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3075 					     NL80211_EXT_FEATURE_TXQS))
3076 			return -EOPNOTSUPP;
3077 		txq_limit = nla_get_u32(
3078 			info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3079 		changed |= WIPHY_PARAM_TXQ_LIMIT;
3080 	}
3081 
3082 	if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3083 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3084 					     NL80211_EXT_FEATURE_TXQS))
3085 			return -EOPNOTSUPP;
3086 		txq_memory_limit = nla_get_u32(
3087 			info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3088 		changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3089 	}
3090 
3091 	if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3092 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3093 					     NL80211_EXT_FEATURE_TXQS))
3094 			return -EOPNOTSUPP;
3095 		txq_quantum = nla_get_u32(
3096 			info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3097 		changed |= WIPHY_PARAM_TXQ_QUANTUM;
3098 	}
3099 
3100 	if (changed) {
3101 		u8 old_retry_short, old_retry_long;
3102 		u32 old_frag_threshold, old_rts_threshold;
3103 		u8 old_coverage_class;
3104 		u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3105 
3106 		if (!rdev->ops->set_wiphy_params)
3107 			return -EOPNOTSUPP;
3108 
3109 		old_retry_short = rdev->wiphy.retry_short;
3110 		old_retry_long = rdev->wiphy.retry_long;
3111 		old_frag_threshold = rdev->wiphy.frag_threshold;
3112 		old_rts_threshold = rdev->wiphy.rts_threshold;
3113 		old_coverage_class = rdev->wiphy.coverage_class;
3114 		old_txq_limit = rdev->wiphy.txq_limit;
3115 		old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3116 		old_txq_quantum = rdev->wiphy.txq_quantum;
3117 
3118 		if (changed & WIPHY_PARAM_RETRY_SHORT)
3119 			rdev->wiphy.retry_short = retry_short;
3120 		if (changed & WIPHY_PARAM_RETRY_LONG)
3121 			rdev->wiphy.retry_long = retry_long;
3122 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3123 			rdev->wiphy.frag_threshold = frag_threshold;
3124 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3125 			rdev->wiphy.rts_threshold = rts_threshold;
3126 		if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3127 			rdev->wiphy.coverage_class = coverage_class;
3128 		if (changed & WIPHY_PARAM_TXQ_LIMIT)
3129 			rdev->wiphy.txq_limit = txq_limit;
3130 		if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3131 			rdev->wiphy.txq_memory_limit = txq_memory_limit;
3132 		if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3133 			rdev->wiphy.txq_quantum = txq_quantum;
3134 
3135 		result = rdev_set_wiphy_params(rdev, changed);
3136 		if (result) {
3137 			rdev->wiphy.retry_short = old_retry_short;
3138 			rdev->wiphy.retry_long = old_retry_long;
3139 			rdev->wiphy.frag_threshold = old_frag_threshold;
3140 			rdev->wiphy.rts_threshold = old_rts_threshold;
3141 			rdev->wiphy.coverage_class = old_coverage_class;
3142 			rdev->wiphy.txq_limit = old_txq_limit;
3143 			rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3144 			rdev->wiphy.txq_quantum = old_txq_quantum;
3145 			return result;
3146 		}
3147 	}
3148 	return 0;
3149 }
3150 
3151 static int nl80211_send_chandef(struct sk_buff *msg,
3152 				const struct cfg80211_chan_def *chandef)
3153 {
3154 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3155 		return -EINVAL;
3156 
3157 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3158 			chandef->chan->center_freq))
3159 		return -ENOBUFS;
3160 	switch (chandef->width) {
3161 	case NL80211_CHAN_WIDTH_20_NOHT:
3162 	case NL80211_CHAN_WIDTH_20:
3163 	case NL80211_CHAN_WIDTH_40:
3164 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3165 				cfg80211_get_chandef_type(chandef)))
3166 			return -ENOBUFS;
3167 		break;
3168 	default:
3169 		break;
3170 	}
3171 	if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3172 		return -ENOBUFS;
3173 	if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3174 		return -ENOBUFS;
3175 	if (chandef->center_freq2 &&
3176 	    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3177 		return -ENOBUFS;
3178 	return 0;
3179 }
3180 
3181 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3182 			      struct cfg80211_registered_device *rdev,
3183 			      struct wireless_dev *wdev,
3184 			      enum nl80211_commands cmd)
3185 {
3186 	struct net_device *dev = wdev->netdev;
3187 	void *hdr;
3188 
3189 	WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3190 		cmd != NL80211_CMD_DEL_INTERFACE &&
3191 		cmd != NL80211_CMD_SET_INTERFACE);
3192 
3193 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3194 	if (!hdr)
3195 		return -1;
3196 
3197 	if (dev &&
3198 	    (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3199 	     nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3200 		goto nla_put_failure;
3201 
3202 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3203 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3204 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3205 			      NL80211_ATTR_PAD) ||
3206 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3207 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
3208 			rdev->devlist_generation ^
3209 			(cfg80211_rdev_list_generation << 2)) ||
3210 	    nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3211 		goto nla_put_failure;
3212 
3213 	if (rdev->ops->get_channel) {
3214 		int ret;
3215 		struct cfg80211_chan_def chandef = {};
3216 
3217 		ret = rdev_get_channel(rdev, wdev, &chandef);
3218 		if (ret == 0) {
3219 			if (nl80211_send_chandef(msg, &chandef))
3220 				goto nla_put_failure;
3221 		}
3222 	}
3223 
3224 	if (rdev->ops->get_tx_power) {
3225 		int dbm, ret;
3226 
3227 		ret = rdev_get_tx_power(rdev, wdev, &dbm);
3228 		if (ret == 0 &&
3229 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3230 				DBM_TO_MBM(dbm)))
3231 			goto nla_put_failure;
3232 	}
3233 
3234 	wdev_lock(wdev);
3235 	switch (wdev->iftype) {
3236 	case NL80211_IFTYPE_AP:
3237 		if (wdev->ssid_len &&
3238 		    nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
3239 			goto nla_put_failure_locked;
3240 		break;
3241 	case NL80211_IFTYPE_STATION:
3242 	case NL80211_IFTYPE_P2P_CLIENT:
3243 	case NL80211_IFTYPE_ADHOC: {
3244 		const u8 *ssid_ie;
3245 		if (!wdev->current_bss)
3246 			break;
3247 		rcu_read_lock();
3248 		ssid_ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
3249 					       WLAN_EID_SSID);
3250 		if (ssid_ie &&
3251 		    nla_put(msg, NL80211_ATTR_SSID, ssid_ie[1], ssid_ie + 2))
3252 			goto nla_put_failure_rcu_locked;
3253 		rcu_read_unlock();
3254 		break;
3255 		}
3256 	default:
3257 		/* nothing */
3258 		break;
3259 	}
3260 	wdev_unlock(wdev);
3261 
3262 	if (rdev->ops->get_txq_stats) {
3263 		struct cfg80211_txq_stats txqstats = {};
3264 		int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3265 
3266 		if (ret == 0 &&
3267 		    !nl80211_put_txq_stats(msg, &txqstats,
3268 					   NL80211_ATTR_TXQ_STATS))
3269 			goto nla_put_failure;
3270 	}
3271 
3272 	genlmsg_end(msg, hdr);
3273 	return 0;
3274 
3275  nla_put_failure_rcu_locked:
3276 	rcu_read_unlock();
3277  nla_put_failure_locked:
3278 	wdev_unlock(wdev);
3279  nla_put_failure:
3280 	genlmsg_cancel(msg, hdr);
3281 	return -EMSGSIZE;
3282 }
3283 
3284 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3285 {
3286 	int wp_idx = 0;
3287 	int if_idx = 0;
3288 	int wp_start = cb->args[0];
3289 	int if_start = cb->args[1];
3290 	int filter_wiphy = -1;
3291 	struct cfg80211_registered_device *rdev;
3292 	struct wireless_dev *wdev;
3293 	int ret;
3294 
3295 	rtnl_lock();
3296 	if (!cb->args[2]) {
3297 		struct nl80211_dump_wiphy_state state = {
3298 			.filter_wiphy = -1,
3299 		};
3300 
3301 		ret = nl80211_dump_wiphy_parse(skb, cb, &state);
3302 		if (ret)
3303 			goto out_unlock;
3304 
3305 		filter_wiphy = state.filter_wiphy;
3306 
3307 		/*
3308 		 * if filtering, set cb->args[2] to +1 since 0 is the default
3309 		 * value needed to determine that parsing is necessary.
3310 		 */
3311 		if (filter_wiphy >= 0)
3312 			cb->args[2] = filter_wiphy + 1;
3313 		else
3314 			cb->args[2] = -1;
3315 	} else if (cb->args[2] > 0) {
3316 		filter_wiphy = cb->args[2] - 1;
3317 	}
3318 
3319 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3320 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3321 			continue;
3322 		if (wp_idx < wp_start) {
3323 			wp_idx++;
3324 			continue;
3325 		}
3326 
3327 		if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
3328 			continue;
3329 
3330 		if_idx = 0;
3331 
3332 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3333 			if (if_idx < if_start) {
3334 				if_idx++;
3335 				continue;
3336 			}
3337 			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
3338 					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
3339 					       rdev, wdev,
3340 					       NL80211_CMD_NEW_INTERFACE) < 0) {
3341 				goto out;
3342 			}
3343 			if_idx++;
3344 		}
3345 
3346 		wp_idx++;
3347 	}
3348  out:
3349 	cb->args[0] = wp_idx;
3350 	cb->args[1] = if_idx;
3351 
3352 	ret = skb->len;
3353  out_unlock:
3354 	rtnl_unlock();
3355 
3356 	return ret;
3357 }
3358 
3359 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
3360 {
3361 	struct sk_buff *msg;
3362 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3363 	struct wireless_dev *wdev = info->user_ptr[1];
3364 
3365 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3366 	if (!msg)
3367 		return -ENOMEM;
3368 
3369 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3370 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3371 		nlmsg_free(msg);
3372 		return -ENOBUFS;
3373 	}
3374 
3375 	return genlmsg_reply(msg, info);
3376 }
3377 
3378 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
3379 	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
3380 	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
3381 	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
3382 	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
3383 	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
3384 	[NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
3385 };
3386 
3387 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
3388 {
3389 	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
3390 	int flag;
3391 
3392 	*mntrflags = 0;
3393 
3394 	if (!nla)
3395 		return -EINVAL;
3396 
3397 	if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
3398 		return -EINVAL;
3399 
3400 	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
3401 		if (flags[flag])
3402 			*mntrflags |= (1<<flag);
3403 
3404 	*mntrflags |= MONITOR_FLAG_CHANGED;
3405 
3406 	return 0;
3407 }
3408 
3409 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
3410 				     enum nl80211_iftype type,
3411 				     struct genl_info *info,
3412 				     struct vif_params *params)
3413 {
3414 	bool change = false;
3415 	int err;
3416 
3417 	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
3418 		if (type != NL80211_IFTYPE_MONITOR)
3419 			return -EINVAL;
3420 
3421 		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
3422 					  &params->flags);
3423 		if (err)
3424 			return err;
3425 
3426 		change = true;
3427 	}
3428 
3429 	if (params->flags & MONITOR_FLAG_ACTIVE &&
3430 	    !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
3431 		return -EOPNOTSUPP;
3432 
3433 	if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
3434 		const u8 *mumimo_groups;
3435 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3436 
3437 		if (type != NL80211_IFTYPE_MONITOR)
3438 			return -EINVAL;
3439 
3440 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3441 			return -EOPNOTSUPP;
3442 
3443 		mumimo_groups =
3444 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
3445 
3446 		/* bits 0 and 63 are reserved and must be zero */
3447 		if ((mumimo_groups[0] & BIT(0)) ||
3448 		    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
3449 			return -EINVAL;
3450 
3451 		params->vht_mumimo_groups = mumimo_groups;
3452 		change = true;
3453 	}
3454 
3455 	if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
3456 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3457 
3458 		if (type != NL80211_IFTYPE_MONITOR)
3459 			return -EINVAL;
3460 
3461 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3462 			return -EOPNOTSUPP;
3463 
3464 		params->vht_mumimo_follow_addr =
3465 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
3466 		change = true;
3467 	}
3468 
3469 	return change ? 1 : 0;
3470 }
3471 
3472 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
3473 			       struct net_device *netdev, u8 use_4addr,
3474 			       enum nl80211_iftype iftype)
3475 {
3476 	if (!use_4addr) {
3477 		if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
3478 			return -EBUSY;
3479 		return 0;
3480 	}
3481 
3482 	switch (iftype) {
3483 	case NL80211_IFTYPE_AP_VLAN:
3484 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
3485 			return 0;
3486 		break;
3487 	case NL80211_IFTYPE_STATION:
3488 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
3489 			return 0;
3490 		break;
3491 	default:
3492 		break;
3493 	}
3494 
3495 	return -EOPNOTSUPP;
3496 }
3497 
3498 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
3499 {
3500 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3501 	struct vif_params params;
3502 	int err;
3503 	enum nl80211_iftype otype, ntype;
3504 	struct net_device *dev = info->user_ptr[1];
3505 	bool change = false;
3506 
3507 	memset(&params, 0, sizeof(params));
3508 
3509 	otype = ntype = dev->ieee80211_ptr->iftype;
3510 
3511 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
3512 		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3513 		if (otype != ntype)
3514 			change = true;
3515 	}
3516 
3517 	if (info->attrs[NL80211_ATTR_MESH_ID]) {
3518 		struct wireless_dev *wdev = dev->ieee80211_ptr;
3519 
3520 		if (ntype != NL80211_IFTYPE_MESH_POINT)
3521 			return -EINVAL;
3522 		if (netif_running(dev))
3523 			return -EBUSY;
3524 
3525 		wdev_lock(wdev);
3526 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3527 			     IEEE80211_MAX_MESH_ID_LEN);
3528 		wdev->mesh_id_up_len =
3529 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3530 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3531 		       wdev->mesh_id_up_len);
3532 		wdev_unlock(wdev);
3533 	}
3534 
3535 	if (info->attrs[NL80211_ATTR_4ADDR]) {
3536 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3537 		change = true;
3538 		err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
3539 		if (err)
3540 			return err;
3541 	} else {
3542 		params.use_4addr = -1;
3543 	}
3544 
3545 	err = nl80211_parse_mon_options(rdev, ntype, info, &params);
3546 	if (err < 0)
3547 		return err;
3548 	if (err > 0)
3549 		change = true;
3550 
3551 	if (change)
3552 		err = cfg80211_change_iface(rdev, dev, ntype, &params);
3553 	else
3554 		err = 0;
3555 
3556 	if (!err && params.use_4addr != -1)
3557 		dev->ieee80211_ptr->use_4addr = params.use_4addr;
3558 
3559 	if (change && !err) {
3560 		struct wireless_dev *wdev = dev->ieee80211_ptr;
3561 
3562 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
3563 	}
3564 
3565 	return err;
3566 }
3567 
3568 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
3569 {
3570 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3571 	struct vif_params params;
3572 	struct wireless_dev *wdev;
3573 	struct sk_buff *msg;
3574 	int err;
3575 	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
3576 
3577 	/* to avoid failing a new interface creation due to pending removal */
3578 	cfg80211_destroy_ifaces(rdev);
3579 
3580 	memset(&params, 0, sizeof(params));
3581 
3582 	if (!info->attrs[NL80211_ATTR_IFNAME])
3583 		return -EINVAL;
3584 
3585 	if (info->attrs[NL80211_ATTR_IFTYPE])
3586 		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3587 
3588 	if (!rdev->ops->add_virtual_intf)
3589 		return -EOPNOTSUPP;
3590 
3591 	if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
3592 	     rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
3593 	    info->attrs[NL80211_ATTR_MAC]) {
3594 		nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
3595 			   ETH_ALEN);
3596 		if (!is_valid_ether_addr(params.macaddr))
3597 			return -EADDRNOTAVAIL;
3598 	}
3599 
3600 	if (info->attrs[NL80211_ATTR_4ADDR]) {
3601 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3602 		err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
3603 		if (err)
3604 			return err;
3605 	}
3606 
3607 	if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
3608 		return -EOPNOTSUPP;
3609 
3610 	err = nl80211_parse_mon_options(rdev, type, info, &params);
3611 	if (err < 0)
3612 		return err;
3613 
3614 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3615 	if (!msg)
3616 		return -ENOMEM;
3617 
3618 	wdev = rdev_add_virtual_intf(rdev,
3619 				nla_data(info->attrs[NL80211_ATTR_IFNAME]),
3620 				NET_NAME_USER, type, &params);
3621 	if (WARN_ON(!wdev)) {
3622 		nlmsg_free(msg);
3623 		return -EPROTO;
3624 	} else if (IS_ERR(wdev)) {
3625 		nlmsg_free(msg);
3626 		return PTR_ERR(wdev);
3627 	}
3628 
3629 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
3630 		wdev->owner_nlportid = info->snd_portid;
3631 
3632 	switch (type) {
3633 	case NL80211_IFTYPE_MESH_POINT:
3634 		if (!info->attrs[NL80211_ATTR_MESH_ID])
3635 			break;
3636 		wdev_lock(wdev);
3637 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3638 			     IEEE80211_MAX_MESH_ID_LEN);
3639 		wdev->mesh_id_up_len =
3640 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3641 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3642 		       wdev->mesh_id_up_len);
3643 		wdev_unlock(wdev);
3644 		break;
3645 	case NL80211_IFTYPE_NAN:
3646 	case NL80211_IFTYPE_P2P_DEVICE:
3647 		/*
3648 		 * P2P Device and NAN do not have a netdev, so don't go
3649 		 * through the netdev notifier and must be added here
3650 		 */
3651 		cfg80211_init_wdev(rdev, wdev);
3652 		break;
3653 	default:
3654 		break;
3655 	}
3656 
3657 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3658 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3659 		nlmsg_free(msg);
3660 		return -ENOBUFS;
3661 	}
3662 
3663 	return genlmsg_reply(msg, info);
3664 }
3665 
3666 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
3667 {
3668 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3669 	struct wireless_dev *wdev = info->user_ptr[1];
3670 
3671 	if (!rdev->ops->del_virtual_intf)
3672 		return -EOPNOTSUPP;
3673 
3674 	/*
3675 	 * If we remove a wireless device without a netdev then clear
3676 	 * user_ptr[1] so that nl80211_post_doit won't dereference it
3677 	 * to check if it needs to do dev_put(). Otherwise it crashes
3678 	 * since the wdev has been freed, unlike with a netdev where
3679 	 * we need the dev_put() for the netdev to really be freed.
3680 	 */
3681 	if (!wdev->netdev)
3682 		info->user_ptr[1] = NULL;
3683 
3684 	return rdev_del_virtual_intf(rdev, wdev);
3685 }
3686 
3687 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
3688 {
3689 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3690 	struct net_device *dev = info->user_ptr[1];
3691 	u16 noack_map;
3692 
3693 	if (!info->attrs[NL80211_ATTR_NOACK_MAP])
3694 		return -EINVAL;
3695 
3696 	if (!rdev->ops->set_noack_map)
3697 		return -EOPNOTSUPP;
3698 
3699 	noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
3700 
3701 	return rdev_set_noack_map(rdev, dev, noack_map);
3702 }
3703 
3704 struct get_key_cookie {
3705 	struct sk_buff *msg;
3706 	int error;
3707 	int idx;
3708 };
3709 
3710 static void get_key_callback(void *c, struct key_params *params)
3711 {
3712 	struct nlattr *key;
3713 	struct get_key_cookie *cookie = c;
3714 
3715 	if ((params->key &&
3716 	     nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
3717 		     params->key_len, params->key)) ||
3718 	    (params->seq &&
3719 	     nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
3720 		     params->seq_len, params->seq)) ||
3721 	    (params->cipher &&
3722 	     nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
3723 			 params->cipher)))
3724 		goto nla_put_failure;
3725 
3726 	key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
3727 	if (!key)
3728 		goto nla_put_failure;
3729 
3730 	if ((params->key &&
3731 	     nla_put(cookie->msg, NL80211_KEY_DATA,
3732 		     params->key_len, params->key)) ||
3733 	    (params->seq &&
3734 	     nla_put(cookie->msg, NL80211_KEY_SEQ,
3735 		     params->seq_len, params->seq)) ||
3736 	    (params->cipher &&
3737 	     nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
3738 			 params->cipher)))
3739 		goto nla_put_failure;
3740 
3741 	if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
3742 		goto nla_put_failure;
3743 
3744 	nla_nest_end(cookie->msg, key);
3745 
3746 	return;
3747  nla_put_failure:
3748 	cookie->error = 1;
3749 }
3750 
3751 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
3752 {
3753 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3754 	int err;
3755 	struct net_device *dev = info->user_ptr[1];
3756 	u8 key_idx = 0;
3757 	const u8 *mac_addr = NULL;
3758 	bool pairwise;
3759 	struct get_key_cookie cookie = {
3760 		.error = 0,
3761 	};
3762 	void *hdr;
3763 	struct sk_buff *msg;
3764 
3765 	if (info->attrs[NL80211_ATTR_KEY_IDX])
3766 		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
3767 
3768 	if (info->attrs[NL80211_ATTR_MAC])
3769 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3770 
3771 	pairwise = !!mac_addr;
3772 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
3773 		u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
3774 
3775 		if (kt != NL80211_KEYTYPE_GROUP &&
3776 		    kt != NL80211_KEYTYPE_PAIRWISE)
3777 			return -EINVAL;
3778 		pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
3779 	}
3780 
3781 	if (!rdev->ops->get_key)
3782 		return -EOPNOTSUPP;
3783 
3784 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3785 		return -ENOENT;
3786 
3787 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3788 	if (!msg)
3789 		return -ENOMEM;
3790 
3791 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
3792 			     NL80211_CMD_NEW_KEY);
3793 	if (!hdr)
3794 		goto nla_put_failure;
3795 
3796 	cookie.msg = msg;
3797 	cookie.idx = key_idx;
3798 
3799 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3800 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
3801 		goto nla_put_failure;
3802 	if (mac_addr &&
3803 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
3804 		goto nla_put_failure;
3805 
3806 	err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
3807 			   get_key_callback);
3808 
3809 	if (err)
3810 		goto free_msg;
3811 
3812 	if (cookie.error)
3813 		goto nla_put_failure;
3814 
3815 	genlmsg_end(msg, hdr);
3816 	return genlmsg_reply(msg, info);
3817 
3818  nla_put_failure:
3819 	err = -ENOBUFS;
3820  free_msg:
3821 	nlmsg_free(msg);
3822 	return err;
3823 }
3824 
3825 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
3826 {
3827 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3828 	struct key_parse key;
3829 	int err;
3830 	struct net_device *dev = info->user_ptr[1];
3831 
3832 	err = nl80211_parse_key(info, &key);
3833 	if (err)
3834 		return err;
3835 
3836 	if (key.idx < 0)
3837 		return -EINVAL;
3838 
3839 	/* Only support setting default key and
3840 	 * Extended Key ID action NL80211_KEY_SET_TX.
3841 	 */
3842 	if (!key.def && !key.defmgmt &&
3843 	    !(key.p.mode == NL80211_KEY_SET_TX))
3844 		return -EINVAL;
3845 
3846 	wdev_lock(dev->ieee80211_ptr);
3847 
3848 	if (key.def) {
3849 		if (!rdev->ops->set_default_key) {
3850 			err = -EOPNOTSUPP;
3851 			goto out;
3852 		}
3853 
3854 		err = nl80211_key_allowed(dev->ieee80211_ptr);
3855 		if (err)
3856 			goto out;
3857 
3858 		err = rdev_set_default_key(rdev, dev, key.idx,
3859 						 key.def_uni, key.def_multi);
3860 
3861 		if (err)
3862 			goto out;
3863 
3864 #ifdef CONFIG_CFG80211_WEXT
3865 		dev->ieee80211_ptr->wext.default_key = key.idx;
3866 #endif
3867 	} else if (key.defmgmt) {
3868 		if (key.def_uni || !key.def_multi) {
3869 			err = -EINVAL;
3870 			goto out;
3871 		}
3872 
3873 		if (!rdev->ops->set_default_mgmt_key) {
3874 			err = -EOPNOTSUPP;
3875 			goto out;
3876 		}
3877 
3878 		err = nl80211_key_allowed(dev->ieee80211_ptr);
3879 		if (err)
3880 			goto out;
3881 
3882 		err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
3883 		if (err)
3884 			goto out;
3885 
3886 #ifdef CONFIG_CFG80211_WEXT
3887 		dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
3888 #endif
3889 	} else if (key.p.mode == NL80211_KEY_SET_TX &&
3890 		   wiphy_ext_feature_isset(&rdev->wiphy,
3891 					   NL80211_EXT_FEATURE_EXT_KEY_ID)) {
3892 		u8 *mac_addr = NULL;
3893 
3894 		if (info->attrs[NL80211_ATTR_MAC])
3895 			mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3896 
3897 		if (!mac_addr || key.idx < 0 || key.idx > 1) {
3898 			err = -EINVAL;
3899 			goto out;
3900 		}
3901 
3902 		err = rdev_add_key(rdev, dev, key.idx,
3903 				   NL80211_KEYTYPE_PAIRWISE,
3904 				   mac_addr, &key.p);
3905 	} else {
3906 		err = -EINVAL;
3907 	}
3908  out:
3909 	wdev_unlock(dev->ieee80211_ptr);
3910 
3911 	return err;
3912 }
3913 
3914 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
3915 {
3916 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3917 	int err;
3918 	struct net_device *dev = info->user_ptr[1];
3919 	struct key_parse key;
3920 	const u8 *mac_addr = NULL;
3921 
3922 	err = nl80211_parse_key(info, &key);
3923 	if (err)
3924 		return err;
3925 
3926 	if (!key.p.key)
3927 		return -EINVAL;
3928 
3929 	if (info->attrs[NL80211_ATTR_MAC])
3930 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3931 
3932 	if (key.type == -1) {
3933 		if (mac_addr)
3934 			key.type = NL80211_KEYTYPE_PAIRWISE;
3935 		else
3936 			key.type = NL80211_KEYTYPE_GROUP;
3937 	}
3938 
3939 	/* for now */
3940 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3941 	    key.type != NL80211_KEYTYPE_GROUP)
3942 		return -EINVAL;
3943 
3944 	if (key.type == NL80211_KEYTYPE_GROUP &&
3945 	    info->attrs[NL80211_ATTR_VLAN_ID])
3946 		key.p.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
3947 
3948 	if (!rdev->ops->add_key)
3949 		return -EOPNOTSUPP;
3950 
3951 	if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
3952 					   key.type == NL80211_KEYTYPE_PAIRWISE,
3953 					   mac_addr))
3954 		return -EINVAL;
3955 
3956 	wdev_lock(dev->ieee80211_ptr);
3957 	err = nl80211_key_allowed(dev->ieee80211_ptr);
3958 	if (!err)
3959 		err = rdev_add_key(rdev, dev, key.idx,
3960 				   key.type == NL80211_KEYTYPE_PAIRWISE,
3961 				    mac_addr, &key.p);
3962 	wdev_unlock(dev->ieee80211_ptr);
3963 
3964 	return err;
3965 }
3966 
3967 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
3968 {
3969 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3970 	int err;
3971 	struct net_device *dev = info->user_ptr[1];
3972 	u8 *mac_addr = NULL;
3973 	struct key_parse key;
3974 
3975 	err = nl80211_parse_key(info, &key);
3976 	if (err)
3977 		return err;
3978 
3979 	if (info->attrs[NL80211_ATTR_MAC])
3980 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3981 
3982 	if (key.type == -1) {
3983 		if (mac_addr)
3984 			key.type = NL80211_KEYTYPE_PAIRWISE;
3985 		else
3986 			key.type = NL80211_KEYTYPE_GROUP;
3987 	}
3988 
3989 	/* for now */
3990 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3991 	    key.type != NL80211_KEYTYPE_GROUP)
3992 		return -EINVAL;
3993 
3994 	if (!rdev->ops->del_key)
3995 		return -EOPNOTSUPP;
3996 
3997 	wdev_lock(dev->ieee80211_ptr);
3998 	err = nl80211_key_allowed(dev->ieee80211_ptr);
3999 
4000 	if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
4001 	    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4002 		err = -ENOENT;
4003 
4004 	if (!err)
4005 		err = rdev_del_key(rdev, dev, key.idx,
4006 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4007 				   mac_addr);
4008 
4009 #ifdef CONFIG_CFG80211_WEXT
4010 	if (!err) {
4011 		if (key.idx == dev->ieee80211_ptr->wext.default_key)
4012 			dev->ieee80211_ptr->wext.default_key = -1;
4013 		else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
4014 			dev->ieee80211_ptr->wext.default_mgmt_key = -1;
4015 	}
4016 #endif
4017 	wdev_unlock(dev->ieee80211_ptr);
4018 
4019 	return err;
4020 }
4021 
4022 /* This function returns an error or the number of nested attributes */
4023 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
4024 {
4025 	struct nlattr *attr;
4026 	int n_entries = 0, tmp;
4027 
4028 	nla_for_each_nested(attr, nl_attr, tmp) {
4029 		if (nla_len(attr) != ETH_ALEN)
4030 			return -EINVAL;
4031 
4032 		n_entries++;
4033 	}
4034 
4035 	return n_entries;
4036 }
4037 
4038 /*
4039  * This function parses ACL information and allocates memory for ACL data.
4040  * On successful return, the calling function is responsible to free the
4041  * ACL buffer returned by this function.
4042  */
4043 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
4044 						struct genl_info *info)
4045 {
4046 	enum nl80211_acl_policy acl_policy;
4047 	struct nlattr *attr;
4048 	struct cfg80211_acl_data *acl;
4049 	int i = 0, n_entries, tmp;
4050 
4051 	if (!wiphy->max_acl_mac_addrs)
4052 		return ERR_PTR(-EOPNOTSUPP);
4053 
4054 	if (!info->attrs[NL80211_ATTR_ACL_POLICY])
4055 		return ERR_PTR(-EINVAL);
4056 
4057 	acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
4058 	if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
4059 	    acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
4060 		return ERR_PTR(-EINVAL);
4061 
4062 	if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
4063 		return ERR_PTR(-EINVAL);
4064 
4065 	n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
4066 	if (n_entries < 0)
4067 		return ERR_PTR(n_entries);
4068 
4069 	if (n_entries > wiphy->max_acl_mac_addrs)
4070 		return ERR_PTR(-ENOTSUPP);
4071 
4072 	acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4073 	if (!acl)
4074 		return ERR_PTR(-ENOMEM);
4075 
4076 	nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4077 		memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4078 		i++;
4079 	}
4080 
4081 	acl->n_acl_entries = n_entries;
4082 	acl->acl_policy = acl_policy;
4083 
4084 	return acl;
4085 }
4086 
4087 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4088 {
4089 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4090 	struct net_device *dev = info->user_ptr[1];
4091 	struct cfg80211_acl_data *acl;
4092 	int err;
4093 
4094 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4095 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4096 		return -EOPNOTSUPP;
4097 
4098 	if (!dev->ieee80211_ptr->beacon_interval)
4099 		return -EINVAL;
4100 
4101 	acl = parse_acl_data(&rdev->wiphy, info);
4102 	if (IS_ERR(acl))
4103 		return PTR_ERR(acl);
4104 
4105 	err = rdev_set_mac_acl(rdev, dev, acl);
4106 
4107 	kfree(acl);
4108 
4109 	return err;
4110 }
4111 
4112 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4113 			   u8 *rates, u8 rates_len)
4114 {
4115 	u8 i;
4116 	u32 mask = 0;
4117 
4118 	for (i = 0; i < rates_len; i++) {
4119 		int rate = (rates[i] & 0x7f) * 5;
4120 		int ridx;
4121 
4122 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4123 			struct ieee80211_rate *srate =
4124 				&sband->bitrates[ridx];
4125 			if (rate == srate->bitrate) {
4126 				mask |= 1 << ridx;
4127 				break;
4128 			}
4129 		}
4130 		if (ridx == sband->n_bitrates)
4131 			return 0; /* rate not found */
4132 	}
4133 
4134 	return mask;
4135 }
4136 
4137 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4138 			       u8 *rates, u8 rates_len,
4139 			       u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4140 {
4141 	u8 i;
4142 
4143 	memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4144 
4145 	for (i = 0; i < rates_len; i++) {
4146 		int ridx, rbit;
4147 
4148 		ridx = rates[i] / 8;
4149 		rbit = BIT(rates[i] % 8);
4150 
4151 		/* check validity */
4152 		if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4153 			return false;
4154 
4155 		/* check availability */
4156 		ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
4157 		if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
4158 			mcs[ridx] |= rbit;
4159 		else
4160 			return false;
4161 	}
4162 
4163 	return true;
4164 }
4165 
4166 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
4167 {
4168 	u16 mcs_mask = 0;
4169 
4170 	switch (vht_mcs_map) {
4171 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
4172 		break;
4173 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
4174 		mcs_mask = 0x00FF;
4175 		break;
4176 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
4177 		mcs_mask = 0x01FF;
4178 		break;
4179 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
4180 		mcs_mask = 0x03FF;
4181 		break;
4182 	default:
4183 		break;
4184 	}
4185 
4186 	return mcs_mask;
4187 }
4188 
4189 static void vht_build_mcs_mask(u16 vht_mcs_map,
4190 			       u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
4191 {
4192 	u8 nss;
4193 
4194 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
4195 		vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
4196 		vht_mcs_map >>= 2;
4197 	}
4198 }
4199 
4200 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
4201 			     struct nl80211_txrate_vht *txrate,
4202 			     u16 mcs[NL80211_VHT_NSS_MAX])
4203 {
4204 	u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4205 	u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
4206 	u8 i;
4207 
4208 	if (!sband->vht_cap.vht_supported)
4209 		return false;
4210 
4211 	memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
4212 
4213 	/* Build vht_mcs_mask from VHT capabilities */
4214 	vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
4215 
4216 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4217 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
4218 			mcs[i] = txrate->mcs[i];
4219 		else
4220 			return false;
4221 	}
4222 
4223 	return true;
4224 }
4225 
4226 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
4227 	[NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4228 				    .len = NL80211_MAX_SUPP_RATES },
4229 	[NL80211_TXRATE_HT] = { .type = NLA_BINARY,
4230 				.len = NL80211_MAX_SUPP_HT_RATES },
4231 	[NL80211_TXRATE_VHT] = {
4232 		.type = NLA_EXACT_LEN_WARN,
4233 		.len = sizeof(struct nl80211_txrate_vht),
4234 	},
4235 	[NL80211_TXRATE_GI] = { .type = NLA_U8 },
4236 };
4237 
4238 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
4239 					 struct cfg80211_bitrate_mask *mask)
4240 {
4241 	struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4242 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4243 	int rem, i;
4244 	struct nlattr *tx_rates;
4245 	struct ieee80211_supported_band *sband;
4246 	u16 vht_tx_mcs_map;
4247 
4248 	memset(mask, 0, sizeof(*mask));
4249 	/* Default to all rates enabled */
4250 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
4251 		sband = rdev->wiphy.bands[i];
4252 
4253 		if (!sband)
4254 			continue;
4255 
4256 		mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
4257 		memcpy(mask->control[i].ht_mcs,
4258 		       sband->ht_cap.mcs.rx_mask,
4259 		       sizeof(mask->control[i].ht_mcs));
4260 
4261 		if (!sband->vht_cap.vht_supported)
4262 			continue;
4263 
4264 		vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4265 		vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
4266 	}
4267 
4268 	/* if no rates are given set it back to the defaults */
4269 	if (!info->attrs[NL80211_ATTR_TX_RATES])
4270 		goto out;
4271 
4272 	/* The nested attribute uses enum nl80211_band as the index. This maps
4273 	 * directly to the enum nl80211_band values used in cfg80211.
4274 	 */
4275 	BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
4276 	nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
4277 		enum nl80211_band band = nla_type(tx_rates);
4278 		int err;
4279 
4280 		if (band < 0 || band >= NUM_NL80211_BANDS)
4281 			return -EINVAL;
4282 		sband = rdev->wiphy.bands[band];
4283 		if (sband == NULL)
4284 			return -EINVAL;
4285 		err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
4286 						  tx_rates,
4287 						  nl80211_txattr_policy,
4288 						  info->extack);
4289 		if (err)
4290 			return err;
4291 		if (tb[NL80211_TXRATE_LEGACY]) {
4292 			mask->control[band].legacy = rateset_to_mask(
4293 				sband,
4294 				nla_data(tb[NL80211_TXRATE_LEGACY]),
4295 				nla_len(tb[NL80211_TXRATE_LEGACY]));
4296 			if ((mask->control[band].legacy == 0) &&
4297 			    nla_len(tb[NL80211_TXRATE_LEGACY]))
4298 				return -EINVAL;
4299 		}
4300 		if (tb[NL80211_TXRATE_HT]) {
4301 			if (!ht_rateset_to_mask(
4302 					sband,
4303 					nla_data(tb[NL80211_TXRATE_HT]),
4304 					nla_len(tb[NL80211_TXRATE_HT]),
4305 					mask->control[band].ht_mcs))
4306 				return -EINVAL;
4307 		}
4308 		if (tb[NL80211_TXRATE_VHT]) {
4309 			if (!vht_set_mcs_mask(
4310 					sband,
4311 					nla_data(tb[NL80211_TXRATE_VHT]),
4312 					mask->control[band].vht_mcs))
4313 				return -EINVAL;
4314 		}
4315 		if (tb[NL80211_TXRATE_GI]) {
4316 			mask->control[band].gi =
4317 				nla_get_u8(tb[NL80211_TXRATE_GI]);
4318 			if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
4319 				return -EINVAL;
4320 		}
4321 
4322 		if (mask->control[band].legacy == 0) {
4323 			/* don't allow empty legacy rates if HT or VHT
4324 			 * are not even supported.
4325 			 */
4326 			if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
4327 			      rdev->wiphy.bands[band]->vht_cap.vht_supported))
4328 				return -EINVAL;
4329 
4330 			for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4331 				if (mask->control[band].ht_mcs[i])
4332 					goto out;
4333 
4334 			for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4335 				if (mask->control[band].vht_mcs[i])
4336 					goto out;
4337 
4338 			/* legacy and mcs rates may not be both empty */
4339 			return -EINVAL;
4340 		}
4341 	}
4342 
4343 out:
4344 	return 0;
4345 }
4346 
4347 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
4348 				   enum nl80211_band band,
4349 				   struct cfg80211_bitrate_mask *beacon_rate)
4350 {
4351 	u32 count_ht, count_vht, i;
4352 	u32 rate = beacon_rate->control[band].legacy;
4353 
4354 	/* Allow only one rate */
4355 	if (hweight32(rate) > 1)
4356 		return -EINVAL;
4357 
4358 	count_ht = 0;
4359 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4360 		if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
4361 			return -EINVAL;
4362 		} else if (beacon_rate->control[band].ht_mcs[i]) {
4363 			count_ht++;
4364 			if (count_ht > 1)
4365 				return -EINVAL;
4366 		}
4367 		if (count_ht && rate)
4368 			return -EINVAL;
4369 	}
4370 
4371 	count_vht = 0;
4372 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4373 		if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
4374 			return -EINVAL;
4375 		} else if (beacon_rate->control[band].vht_mcs[i]) {
4376 			count_vht++;
4377 			if (count_vht > 1)
4378 				return -EINVAL;
4379 		}
4380 		if (count_vht && rate)
4381 			return -EINVAL;
4382 	}
4383 
4384 	if ((count_ht && count_vht) || (!rate && !count_ht && !count_vht))
4385 		return -EINVAL;
4386 
4387 	if (rate &&
4388 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4389 				     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
4390 		return -EINVAL;
4391 	if (count_ht &&
4392 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4393 				     NL80211_EXT_FEATURE_BEACON_RATE_HT))
4394 		return -EINVAL;
4395 	if (count_vht &&
4396 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4397 				     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
4398 		return -EINVAL;
4399 
4400 	return 0;
4401 }
4402 
4403 static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
4404 				struct nlattr *attrs[],
4405 				struct cfg80211_beacon_data *bcn)
4406 {
4407 	bool haveinfo = false;
4408 	int err;
4409 
4410 	memset(bcn, 0, sizeof(*bcn));
4411 
4412 	if (attrs[NL80211_ATTR_BEACON_HEAD]) {
4413 		bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
4414 		bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
4415 		if (!bcn->head_len)
4416 			return -EINVAL;
4417 		haveinfo = true;
4418 	}
4419 
4420 	if (attrs[NL80211_ATTR_BEACON_TAIL]) {
4421 		bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
4422 		bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
4423 		haveinfo = true;
4424 	}
4425 
4426 	if (!haveinfo)
4427 		return -EINVAL;
4428 
4429 	if (attrs[NL80211_ATTR_IE]) {
4430 		bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
4431 		bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
4432 	}
4433 
4434 	if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
4435 		bcn->proberesp_ies =
4436 			nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4437 		bcn->proberesp_ies_len =
4438 			nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4439 	}
4440 
4441 	if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
4442 		bcn->assocresp_ies =
4443 			nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4444 		bcn->assocresp_ies_len =
4445 			nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4446 	}
4447 
4448 	if (attrs[NL80211_ATTR_PROBE_RESP]) {
4449 		bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
4450 		bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
4451 	}
4452 
4453 	if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
4454 		struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
4455 
4456 		err = nla_parse_nested_deprecated(tb,
4457 						  NL80211_FTM_RESP_ATTR_MAX,
4458 						  attrs[NL80211_ATTR_FTM_RESPONDER],
4459 						  NULL, NULL);
4460 		if (err)
4461 			return err;
4462 
4463 		if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
4464 		    wiphy_ext_feature_isset(&rdev->wiphy,
4465 					    NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
4466 			bcn->ftm_responder = 1;
4467 		else
4468 			return -EOPNOTSUPP;
4469 
4470 		if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
4471 			bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
4472 			bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
4473 		}
4474 
4475 		if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
4476 			bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
4477 			bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
4478 		}
4479 	} else {
4480 		bcn->ftm_responder = -1;
4481 	}
4482 
4483 	return 0;
4484 }
4485 
4486 static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
4487 				    struct ieee80211_he_obss_pd *he_obss_pd)
4488 {
4489 	struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
4490 	int err;
4491 
4492 	err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
4493 			       he_obss_pd_policy, NULL);
4494 	if (err)
4495 		return err;
4496 
4497 	if (!tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] ||
4498 	    !tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
4499 		return -EINVAL;
4500 
4501 	he_obss_pd->min_offset =
4502 		nla_get_u32(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
4503 	he_obss_pd->max_offset =
4504 		nla_get_u32(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
4505 
4506 	if (he_obss_pd->min_offset >= he_obss_pd->max_offset)
4507 		return -EINVAL;
4508 
4509 	he_obss_pd->enable = true;
4510 
4511 	return 0;
4512 }
4513 
4514 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
4515 					    const u8 *rates)
4516 {
4517 	int i;
4518 
4519 	if (!rates)
4520 		return;
4521 
4522 	for (i = 0; i < rates[1]; i++) {
4523 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
4524 			params->ht_required = true;
4525 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
4526 			params->vht_required = true;
4527 	}
4528 }
4529 
4530 /*
4531  * Since the nl80211 API didn't include, from the beginning, attributes about
4532  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
4533  * benefit of drivers that rebuild IEs in the firmware.
4534  */
4535 static void nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
4536 {
4537 	const struct cfg80211_beacon_data *bcn = &params->beacon;
4538 	size_t ies_len = bcn->tail_len;
4539 	const u8 *ies = bcn->tail;
4540 	const u8 *rates;
4541 	const u8 *cap;
4542 
4543 	rates = cfg80211_find_ie(WLAN_EID_SUPP_RATES, ies, ies_len);
4544 	nl80211_check_ap_rate_selectors(params, rates);
4545 
4546 	rates = cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
4547 	nl80211_check_ap_rate_selectors(params, rates);
4548 
4549 	cap = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies, ies_len);
4550 	if (cap && cap[1] >= sizeof(*params->ht_cap))
4551 		params->ht_cap = (void *)(cap + 2);
4552 	cap = cfg80211_find_ie(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
4553 	if (cap && cap[1] >= sizeof(*params->vht_cap))
4554 		params->vht_cap = (void *)(cap + 2);
4555 	cap = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
4556 	if (cap && cap[1] >= sizeof(*params->he_cap) + 1)
4557 		params->he_cap = (void *)(cap + 3);
4558 }
4559 
4560 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
4561 				   struct cfg80211_ap_settings *params)
4562 {
4563 	struct wireless_dev *wdev;
4564 	bool ret = false;
4565 
4566 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
4567 		if (wdev->iftype != NL80211_IFTYPE_AP &&
4568 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)
4569 			continue;
4570 
4571 		if (!wdev->preset_chandef.chan)
4572 			continue;
4573 
4574 		params->chandef = wdev->preset_chandef;
4575 		ret = true;
4576 		break;
4577 	}
4578 
4579 	return ret;
4580 }
4581 
4582 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
4583 				    enum nl80211_auth_type auth_type,
4584 				    enum nl80211_commands cmd)
4585 {
4586 	if (auth_type > NL80211_AUTHTYPE_MAX)
4587 		return false;
4588 
4589 	switch (cmd) {
4590 	case NL80211_CMD_AUTHENTICATE:
4591 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
4592 		    auth_type == NL80211_AUTHTYPE_SAE)
4593 			return false;
4594 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
4595 					     NL80211_EXT_FEATURE_FILS_STA) &&
4596 		    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
4597 		     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4598 		     auth_type == NL80211_AUTHTYPE_FILS_PK))
4599 			return false;
4600 		return true;
4601 	case NL80211_CMD_CONNECT:
4602 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
4603 		    !wiphy_ext_feature_isset(&rdev->wiphy,
4604 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
4605 		    auth_type == NL80211_AUTHTYPE_SAE)
4606 			return false;
4607 
4608 		/* FILS with SK PFS or PK not supported yet */
4609 		if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4610 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
4611 			return false;
4612 		if (!wiphy_ext_feature_isset(
4613 			    &rdev->wiphy,
4614 			    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
4615 		    auth_type == NL80211_AUTHTYPE_FILS_SK)
4616 			return false;
4617 		return true;
4618 	case NL80211_CMD_START_AP:
4619 		/* SAE not supported yet */
4620 		if (auth_type == NL80211_AUTHTYPE_SAE)
4621 			return false;
4622 		/* FILS not supported yet */
4623 		if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
4624 		    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4625 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
4626 			return false;
4627 		return true;
4628 	default:
4629 		return false;
4630 	}
4631 }
4632 
4633 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
4634 {
4635 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4636 	struct net_device *dev = info->user_ptr[1];
4637 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4638 	struct cfg80211_ap_settings params;
4639 	int err;
4640 
4641 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4642 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4643 		return -EOPNOTSUPP;
4644 
4645 	if (!rdev->ops->start_ap)
4646 		return -EOPNOTSUPP;
4647 
4648 	if (wdev->beacon_interval)
4649 		return -EALREADY;
4650 
4651 	memset(&params, 0, sizeof(params));
4652 
4653 	/* these are required for START_AP */
4654 	if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
4655 	    !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
4656 	    !info->attrs[NL80211_ATTR_BEACON_HEAD])
4657 		return -EINVAL;
4658 
4659 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon);
4660 	if (err)
4661 		return err;
4662 
4663 	params.beacon_interval =
4664 		nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4665 	params.dtim_period =
4666 		nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
4667 
4668 	err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
4669 					   params.beacon_interval);
4670 	if (err)
4671 		return err;
4672 
4673 	/*
4674 	 * In theory, some of these attributes should be required here
4675 	 * but since they were not used when the command was originally
4676 	 * added, keep them optional for old user space programs to let
4677 	 * them continue to work with drivers that do not need the
4678 	 * additional information -- drivers must check!
4679 	 */
4680 	if (info->attrs[NL80211_ATTR_SSID]) {
4681 		params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4682 		params.ssid_len =
4683 			nla_len(info->attrs[NL80211_ATTR_SSID]);
4684 		if (params.ssid_len == 0 ||
4685 		    params.ssid_len > IEEE80211_MAX_SSID_LEN)
4686 			return -EINVAL;
4687 	}
4688 
4689 	if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
4690 		params.hidden_ssid = nla_get_u32(
4691 			info->attrs[NL80211_ATTR_HIDDEN_SSID]);
4692 
4693 	params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
4694 
4695 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4696 		params.auth_type = nla_get_u32(
4697 			info->attrs[NL80211_ATTR_AUTH_TYPE]);
4698 		if (!nl80211_valid_auth_type(rdev, params.auth_type,
4699 					     NL80211_CMD_START_AP))
4700 			return -EINVAL;
4701 	} else
4702 		params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4703 
4704 	err = nl80211_crypto_settings(rdev, info, &params.crypto,
4705 				      NL80211_MAX_NR_CIPHER_SUITES);
4706 	if (err)
4707 		return err;
4708 
4709 	if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
4710 		if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
4711 			return -EOPNOTSUPP;
4712 		params.inactivity_timeout = nla_get_u16(
4713 			info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
4714 	}
4715 
4716 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4717 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4718 			return -EINVAL;
4719 		params.p2p_ctwindow =
4720 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4721 		if (params.p2p_ctwindow != 0 &&
4722 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4723 			return -EINVAL;
4724 	}
4725 
4726 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4727 		u8 tmp;
4728 
4729 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4730 			return -EINVAL;
4731 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4732 		params.p2p_opp_ps = tmp;
4733 		if (params.p2p_opp_ps != 0 &&
4734 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4735 			return -EINVAL;
4736 	}
4737 
4738 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4739 		err = nl80211_parse_chandef(rdev, info, &params.chandef);
4740 		if (err)
4741 			return err;
4742 	} else if (wdev->preset_chandef.chan) {
4743 		params.chandef = wdev->preset_chandef;
4744 	} else if (!nl80211_get_ap_channel(rdev, &params))
4745 		return -EINVAL;
4746 
4747 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
4748 					   wdev->iftype))
4749 		return -EINVAL;
4750 
4751 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
4752 		err = nl80211_parse_tx_bitrate_mask(info, &params.beacon_rate);
4753 		if (err)
4754 			return err;
4755 
4756 		err = validate_beacon_tx_rate(rdev, params.chandef.chan->band,
4757 					      &params.beacon_rate);
4758 		if (err)
4759 			return err;
4760 	}
4761 
4762 	if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
4763 		params.smps_mode =
4764 			nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
4765 		switch (params.smps_mode) {
4766 		case NL80211_SMPS_OFF:
4767 			break;
4768 		case NL80211_SMPS_STATIC:
4769 			if (!(rdev->wiphy.features &
4770 			      NL80211_FEATURE_STATIC_SMPS))
4771 				return -EINVAL;
4772 			break;
4773 		case NL80211_SMPS_DYNAMIC:
4774 			if (!(rdev->wiphy.features &
4775 			      NL80211_FEATURE_DYNAMIC_SMPS))
4776 				return -EINVAL;
4777 			break;
4778 		default:
4779 			return -EINVAL;
4780 		}
4781 	} else {
4782 		params.smps_mode = NL80211_SMPS_OFF;
4783 	}
4784 
4785 	params.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
4786 	if (params.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ])
4787 		return -EOPNOTSUPP;
4788 
4789 	if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
4790 		params.acl = parse_acl_data(&rdev->wiphy, info);
4791 		if (IS_ERR(params.acl))
4792 			return PTR_ERR(params.acl);
4793 	}
4794 
4795 	params.twt_responder =
4796 		    nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
4797 
4798 	if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
4799 		err = nl80211_parse_he_obss_pd(
4800 					info->attrs[NL80211_ATTR_HE_OBSS_PD],
4801 					&params.he_obss_pd);
4802 		if (err)
4803 			return err;
4804 	}
4805 
4806 	nl80211_calculate_ap_params(&params);
4807 
4808 	if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
4809 		params.flags |= AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
4810 
4811 	wdev_lock(wdev);
4812 	err = rdev_start_ap(rdev, dev, &params);
4813 	if (!err) {
4814 		wdev->preset_chandef = params.chandef;
4815 		wdev->beacon_interval = params.beacon_interval;
4816 		wdev->chandef = params.chandef;
4817 		wdev->ssid_len = params.ssid_len;
4818 		memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
4819 
4820 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
4821 			wdev->conn_owner_nlportid = info->snd_portid;
4822 	}
4823 	wdev_unlock(wdev);
4824 
4825 	kfree(params.acl);
4826 
4827 	return err;
4828 }
4829 
4830 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
4831 {
4832 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4833 	struct net_device *dev = info->user_ptr[1];
4834 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4835 	struct cfg80211_beacon_data params;
4836 	int err;
4837 
4838 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4839 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4840 		return -EOPNOTSUPP;
4841 
4842 	if (!rdev->ops->change_beacon)
4843 		return -EOPNOTSUPP;
4844 
4845 	if (!wdev->beacon_interval)
4846 		return -EINVAL;
4847 
4848 	err = nl80211_parse_beacon(rdev, info->attrs, &params);
4849 	if (err)
4850 		return err;
4851 
4852 	wdev_lock(wdev);
4853 	err = rdev_change_beacon(rdev, dev, &params);
4854 	wdev_unlock(wdev);
4855 
4856 	return err;
4857 }
4858 
4859 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
4860 {
4861 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4862 	struct net_device *dev = info->user_ptr[1];
4863 
4864 	return cfg80211_stop_ap(rdev, dev, false);
4865 }
4866 
4867 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
4868 	[NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
4869 	[NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
4870 	[NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
4871 	[NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
4872 	[NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
4873 	[NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
4874 };
4875 
4876 static int parse_station_flags(struct genl_info *info,
4877 			       enum nl80211_iftype iftype,
4878 			       struct station_parameters *params)
4879 {
4880 	struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
4881 	struct nlattr *nla;
4882 	int flag;
4883 
4884 	/*
4885 	 * Try parsing the new attribute first so userspace
4886 	 * can specify both for older kernels.
4887 	 */
4888 	nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
4889 	if (nla) {
4890 		struct nl80211_sta_flag_update *sta_flags;
4891 
4892 		sta_flags = nla_data(nla);
4893 		params->sta_flags_mask = sta_flags->mask;
4894 		params->sta_flags_set = sta_flags->set;
4895 		params->sta_flags_set &= params->sta_flags_mask;
4896 		if ((params->sta_flags_mask |
4897 		     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
4898 			return -EINVAL;
4899 		return 0;
4900 	}
4901 
4902 	/* if present, parse the old attribute */
4903 
4904 	nla = info->attrs[NL80211_ATTR_STA_FLAGS];
4905 	if (!nla)
4906 		return 0;
4907 
4908 	if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
4909 		return -EINVAL;
4910 
4911 	/*
4912 	 * Only allow certain flags for interface types so that
4913 	 * other attributes are silently ignored. Remember that
4914 	 * this is backward compatibility code with old userspace
4915 	 * and shouldn't be hit in other cases anyway.
4916 	 */
4917 	switch (iftype) {
4918 	case NL80211_IFTYPE_AP:
4919 	case NL80211_IFTYPE_AP_VLAN:
4920 	case NL80211_IFTYPE_P2P_GO:
4921 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4922 					 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4923 					 BIT(NL80211_STA_FLAG_WME) |
4924 					 BIT(NL80211_STA_FLAG_MFP);
4925 		break;
4926 	case NL80211_IFTYPE_P2P_CLIENT:
4927 	case NL80211_IFTYPE_STATION:
4928 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4929 					 BIT(NL80211_STA_FLAG_TDLS_PEER);
4930 		break;
4931 	case NL80211_IFTYPE_MESH_POINT:
4932 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4933 					 BIT(NL80211_STA_FLAG_MFP) |
4934 					 BIT(NL80211_STA_FLAG_AUTHORIZED);
4935 		break;
4936 	default:
4937 		return -EINVAL;
4938 	}
4939 
4940 	for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
4941 		if (flags[flag]) {
4942 			params->sta_flags_set |= (1<<flag);
4943 
4944 			/* no longer support new API additions in old API */
4945 			if (flag > NL80211_STA_FLAG_MAX_OLD_API)
4946 				return -EINVAL;
4947 		}
4948 	}
4949 
4950 	return 0;
4951 }
4952 
4953 bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
4954 {
4955 	struct nlattr *rate;
4956 	u32 bitrate;
4957 	u16 bitrate_compat;
4958 	enum nl80211_rate_info rate_flg;
4959 
4960 	rate = nla_nest_start_noflag(msg, attr);
4961 	if (!rate)
4962 		return false;
4963 
4964 	/* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
4965 	bitrate = cfg80211_calculate_bitrate(info);
4966 	/* report 16-bit bitrate only if we can */
4967 	bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
4968 	if (bitrate > 0 &&
4969 	    nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
4970 		return false;
4971 	if (bitrate_compat > 0 &&
4972 	    nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
4973 		return false;
4974 
4975 	switch (info->bw) {
4976 	case RATE_INFO_BW_5:
4977 		rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
4978 		break;
4979 	case RATE_INFO_BW_10:
4980 		rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
4981 		break;
4982 	default:
4983 		WARN_ON(1);
4984 		/* fall through */
4985 	case RATE_INFO_BW_20:
4986 		rate_flg = 0;
4987 		break;
4988 	case RATE_INFO_BW_40:
4989 		rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
4990 		break;
4991 	case RATE_INFO_BW_80:
4992 		rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
4993 		break;
4994 	case RATE_INFO_BW_160:
4995 		rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
4996 		break;
4997 	case RATE_INFO_BW_HE_RU:
4998 		rate_flg = 0;
4999 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
5000 	}
5001 
5002 	if (rate_flg && nla_put_flag(msg, rate_flg))
5003 		return false;
5004 
5005 	if (info->flags & RATE_INFO_FLAGS_MCS) {
5006 		if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
5007 			return false;
5008 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
5009 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
5010 			return false;
5011 	} else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
5012 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
5013 			return false;
5014 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
5015 			return false;
5016 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
5017 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
5018 			return false;
5019 	} else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
5020 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
5021 			return false;
5022 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
5023 			return false;
5024 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
5025 			return false;
5026 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
5027 			return false;
5028 		if (info->bw == RATE_INFO_BW_HE_RU &&
5029 		    nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
5030 			       info->he_ru_alloc))
5031 			return false;
5032 	}
5033 
5034 	nla_nest_end(msg, rate);
5035 	return true;
5036 }
5037 
5038 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
5039 			       int id)
5040 {
5041 	void *attr;
5042 	int i = 0;
5043 
5044 	if (!mask)
5045 		return true;
5046 
5047 	attr = nla_nest_start_noflag(msg, id);
5048 	if (!attr)
5049 		return false;
5050 
5051 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
5052 		if (!(mask & BIT(i)))
5053 			continue;
5054 
5055 		if (nla_put_u8(msg, i, signal[i]))
5056 			return false;
5057 	}
5058 
5059 	nla_nest_end(msg, attr);
5060 
5061 	return true;
5062 }
5063 
5064 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
5065 				u32 seq, int flags,
5066 				struct cfg80211_registered_device *rdev,
5067 				struct net_device *dev,
5068 				const u8 *mac_addr, struct station_info *sinfo)
5069 {
5070 	void *hdr;
5071 	struct nlattr *sinfoattr, *bss_param;
5072 
5073 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
5074 	if (!hdr) {
5075 		cfg80211_sinfo_release_content(sinfo);
5076 		return -1;
5077 	}
5078 
5079 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5080 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
5081 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
5082 		goto nla_put_failure;
5083 
5084 	sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
5085 	if (!sinfoattr)
5086 		goto nla_put_failure;
5087 
5088 #define PUT_SINFO(attr, memb, type) do {				\
5089 	BUILD_BUG_ON(sizeof(type) == sizeof(u64));			\
5090 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
5091 	    nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,		\
5092 			     sinfo->memb))				\
5093 		goto nla_put_failure;					\
5094 	} while (0)
5095 #define PUT_SINFO_U64(attr, memb) do {					\
5096 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
5097 	    nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,		\
5098 			      sinfo->memb, NL80211_STA_INFO_PAD))	\
5099 		goto nla_put_failure;					\
5100 	} while (0)
5101 
5102 	PUT_SINFO(CONNECTED_TIME, connected_time, u32);
5103 	PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
5104 	PUT_SINFO_U64(ASSOC_AT_BOOTTIME, assoc_at);
5105 
5106 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
5107 			     BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
5108 	    nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
5109 			(u32)sinfo->rx_bytes))
5110 		goto nla_put_failure;
5111 
5112 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
5113 			     BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
5114 	    nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
5115 			(u32)sinfo->tx_bytes))
5116 		goto nla_put_failure;
5117 
5118 	PUT_SINFO_U64(RX_BYTES64, rx_bytes);
5119 	PUT_SINFO_U64(TX_BYTES64, tx_bytes);
5120 	PUT_SINFO(LLID, llid, u16);
5121 	PUT_SINFO(PLID, plid, u16);
5122 	PUT_SINFO(PLINK_STATE, plink_state, u8);
5123 	PUT_SINFO_U64(RX_DURATION, rx_duration);
5124 	PUT_SINFO_U64(TX_DURATION, tx_duration);
5125 
5126 	if (wiphy_ext_feature_isset(&rdev->wiphy,
5127 				    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5128 		PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
5129 
5130 	switch (rdev->wiphy.signal_type) {
5131 	case CFG80211_SIGNAL_TYPE_MBM:
5132 		PUT_SINFO(SIGNAL, signal, u8);
5133 		PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
5134 		break;
5135 	default:
5136 		break;
5137 	}
5138 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
5139 		if (!nl80211_put_signal(msg, sinfo->chains,
5140 					sinfo->chain_signal,
5141 					NL80211_STA_INFO_CHAIN_SIGNAL))
5142 			goto nla_put_failure;
5143 	}
5144 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
5145 		if (!nl80211_put_signal(msg, sinfo->chains,
5146 					sinfo->chain_signal_avg,
5147 					NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
5148 			goto nla_put_failure;
5149 	}
5150 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
5151 		if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
5152 					  NL80211_STA_INFO_TX_BITRATE))
5153 			goto nla_put_failure;
5154 	}
5155 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
5156 		if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
5157 					  NL80211_STA_INFO_RX_BITRATE))
5158 			goto nla_put_failure;
5159 	}
5160 
5161 	PUT_SINFO(RX_PACKETS, rx_packets, u32);
5162 	PUT_SINFO(TX_PACKETS, tx_packets, u32);
5163 	PUT_SINFO(TX_RETRIES, tx_retries, u32);
5164 	PUT_SINFO(TX_FAILED, tx_failed, u32);
5165 	PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
5166 	PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
5167 	PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
5168 	PUT_SINFO(LOCAL_PM, local_pm, u32);
5169 	PUT_SINFO(PEER_PM, peer_pm, u32);
5170 	PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
5171 	PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
5172 
5173 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
5174 		bss_param = nla_nest_start_noflag(msg,
5175 						  NL80211_STA_INFO_BSS_PARAM);
5176 		if (!bss_param)
5177 			goto nla_put_failure;
5178 
5179 		if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
5180 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
5181 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
5182 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
5183 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
5184 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
5185 		    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
5186 			       sinfo->bss_param.dtim_period) ||
5187 		    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
5188 				sinfo->bss_param.beacon_interval))
5189 			goto nla_put_failure;
5190 
5191 		nla_nest_end(msg, bss_param);
5192 	}
5193 	if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
5194 	    nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
5195 		    sizeof(struct nl80211_sta_flag_update),
5196 		    &sinfo->sta_flags))
5197 		goto nla_put_failure;
5198 
5199 	PUT_SINFO_U64(T_OFFSET, t_offset);
5200 	PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
5201 	PUT_SINFO_U64(BEACON_RX, rx_beacon);
5202 	PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
5203 	PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
5204 	PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
5205 	if (wiphy_ext_feature_isset(&rdev->wiphy,
5206 				    NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
5207 		PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
5208 		PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
5209 	}
5210 
5211 #undef PUT_SINFO
5212 #undef PUT_SINFO_U64
5213 
5214 	if (sinfo->pertid) {
5215 		struct nlattr *tidsattr;
5216 		int tid;
5217 
5218 		tidsattr = nla_nest_start_noflag(msg,
5219 						 NL80211_STA_INFO_TID_STATS);
5220 		if (!tidsattr)
5221 			goto nla_put_failure;
5222 
5223 		for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
5224 			struct cfg80211_tid_stats *tidstats;
5225 			struct nlattr *tidattr;
5226 
5227 			tidstats = &sinfo->pertid[tid];
5228 
5229 			if (!tidstats->filled)
5230 				continue;
5231 
5232 			tidattr = nla_nest_start_noflag(msg, tid + 1);
5233 			if (!tidattr)
5234 				goto nla_put_failure;
5235 
5236 #define PUT_TIDVAL_U64(attr, memb) do {					\
5237 	if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&	\
5238 	    nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,		\
5239 			      tidstats->memb, NL80211_TID_STATS_PAD))	\
5240 		goto nla_put_failure;					\
5241 	} while (0)
5242 
5243 			PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
5244 			PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
5245 			PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
5246 			PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
5247 
5248 #undef PUT_TIDVAL_U64
5249 			if ((tidstats->filled &
5250 			     BIT(NL80211_TID_STATS_TXQ_STATS)) &&
5251 			    !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
5252 						   NL80211_TID_STATS_TXQ_STATS))
5253 				goto nla_put_failure;
5254 
5255 			nla_nest_end(msg, tidattr);
5256 		}
5257 
5258 		nla_nest_end(msg, tidsattr);
5259 	}
5260 
5261 	nla_nest_end(msg, sinfoattr);
5262 
5263 	if (sinfo->assoc_req_ies_len &&
5264 	    nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
5265 		    sinfo->assoc_req_ies))
5266 		goto nla_put_failure;
5267 
5268 	cfg80211_sinfo_release_content(sinfo);
5269 	genlmsg_end(msg, hdr);
5270 	return 0;
5271 
5272  nla_put_failure:
5273 	cfg80211_sinfo_release_content(sinfo);
5274 	genlmsg_cancel(msg, hdr);
5275 	return -EMSGSIZE;
5276 }
5277 
5278 static int nl80211_dump_station(struct sk_buff *skb,
5279 				struct netlink_callback *cb)
5280 {
5281 	struct station_info sinfo;
5282 	struct cfg80211_registered_device *rdev;
5283 	struct wireless_dev *wdev;
5284 	u8 mac_addr[ETH_ALEN];
5285 	int sta_idx = cb->args[2];
5286 	int err;
5287 
5288 	rtnl_lock();
5289 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
5290 	if (err)
5291 		goto out_err;
5292 
5293 	if (!wdev->netdev) {
5294 		err = -EINVAL;
5295 		goto out_err;
5296 	}
5297 
5298 	if (!rdev->ops->dump_station) {
5299 		err = -EOPNOTSUPP;
5300 		goto out_err;
5301 	}
5302 
5303 	while (1) {
5304 		memset(&sinfo, 0, sizeof(sinfo));
5305 		err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
5306 					mac_addr, &sinfo);
5307 		if (err == -ENOENT)
5308 			break;
5309 		if (err)
5310 			goto out_err;
5311 
5312 		if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
5313 				NETLINK_CB(cb->skb).portid,
5314 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
5315 				rdev, wdev->netdev, mac_addr,
5316 				&sinfo) < 0)
5317 			goto out;
5318 
5319 		sta_idx++;
5320 	}
5321 
5322  out:
5323 	cb->args[2] = sta_idx;
5324 	err = skb->len;
5325  out_err:
5326 	rtnl_unlock();
5327 
5328 	return err;
5329 }
5330 
5331 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
5332 {
5333 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5334 	struct net_device *dev = info->user_ptr[1];
5335 	struct station_info sinfo;
5336 	struct sk_buff *msg;
5337 	u8 *mac_addr = NULL;
5338 	int err;
5339 
5340 	memset(&sinfo, 0, sizeof(sinfo));
5341 
5342 	if (!info->attrs[NL80211_ATTR_MAC])
5343 		return -EINVAL;
5344 
5345 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5346 
5347 	if (!rdev->ops->get_station)
5348 		return -EOPNOTSUPP;
5349 
5350 	err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
5351 	if (err)
5352 		return err;
5353 
5354 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5355 	if (!msg) {
5356 		cfg80211_sinfo_release_content(&sinfo);
5357 		return -ENOMEM;
5358 	}
5359 
5360 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
5361 				 info->snd_portid, info->snd_seq, 0,
5362 				 rdev, dev, mac_addr, &sinfo) < 0) {
5363 		nlmsg_free(msg);
5364 		return -ENOBUFS;
5365 	}
5366 
5367 	return genlmsg_reply(msg, info);
5368 }
5369 
5370 int cfg80211_check_station_change(struct wiphy *wiphy,
5371 				  struct station_parameters *params,
5372 				  enum cfg80211_station_type statype)
5373 {
5374 	if (params->listen_interval != -1 &&
5375 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5376 		return -EINVAL;
5377 
5378 	if (params->support_p2p_ps != -1 &&
5379 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5380 		return -EINVAL;
5381 
5382 	if (params->aid &&
5383 	    !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
5384 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5385 		return -EINVAL;
5386 
5387 	/* When you run into this, adjust the code below for the new flag */
5388 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5389 
5390 	switch (statype) {
5391 	case CFG80211_STA_MESH_PEER_KERNEL:
5392 	case CFG80211_STA_MESH_PEER_USER:
5393 		/*
5394 		 * No ignoring the TDLS flag here -- the userspace mesh
5395 		 * code doesn't have the bug of including TDLS in the
5396 		 * mask everywhere.
5397 		 */
5398 		if (params->sta_flags_mask &
5399 				~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5400 				  BIT(NL80211_STA_FLAG_MFP) |
5401 				  BIT(NL80211_STA_FLAG_AUTHORIZED)))
5402 			return -EINVAL;
5403 		break;
5404 	case CFG80211_STA_TDLS_PEER_SETUP:
5405 	case CFG80211_STA_TDLS_PEER_ACTIVE:
5406 		if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
5407 			return -EINVAL;
5408 		/* ignore since it can't change */
5409 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5410 		break;
5411 	default:
5412 		/* disallow mesh-specific things */
5413 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
5414 			return -EINVAL;
5415 		if (params->local_pm)
5416 			return -EINVAL;
5417 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
5418 			return -EINVAL;
5419 	}
5420 
5421 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
5422 	    statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
5423 		/* TDLS can't be set, ... */
5424 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
5425 			return -EINVAL;
5426 		/*
5427 		 * ... but don't bother the driver with it. This works around
5428 		 * a hostapd/wpa_supplicant issue -- it always includes the
5429 		 * TLDS_PEER flag in the mask even for AP mode.
5430 		 */
5431 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5432 	}
5433 
5434 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
5435 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5436 		/* reject other things that can't change */
5437 		if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
5438 			return -EINVAL;
5439 		if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
5440 			return -EINVAL;
5441 		if (params->supported_rates)
5442 			return -EINVAL;
5443 		if (params->ext_capab || params->ht_capa || params->vht_capa ||
5444 		    params->he_capa)
5445 			return -EINVAL;
5446 	}
5447 
5448 	if (statype != CFG80211_STA_AP_CLIENT &&
5449 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5450 		if (params->vlan)
5451 			return -EINVAL;
5452 	}
5453 
5454 	switch (statype) {
5455 	case CFG80211_STA_AP_MLME_CLIENT:
5456 		/* Use this only for authorizing/unauthorizing a station */
5457 		if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
5458 			return -EOPNOTSUPP;
5459 		break;
5460 	case CFG80211_STA_AP_CLIENT:
5461 	case CFG80211_STA_AP_CLIENT_UNASSOC:
5462 		/* accept only the listed bits */
5463 		if (params->sta_flags_mask &
5464 				~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
5465 				  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5466 				  BIT(NL80211_STA_FLAG_ASSOCIATED) |
5467 				  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
5468 				  BIT(NL80211_STA_FLAG_WME) |
5469 				  BIT(NL80211_STA_FLAG_MFP)))
5470 			return -EINVAL;
5471 
5472 		/* but authenticated/associated only if driver handles it */
5473 		if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5474 		    params->sta_flags_mask &
5475 				(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5476 				 BIT(NL80211_STA_FLAG_ASSOCIATED)))
5477 			return -EINVAL;
5478 		break;
5479 	case CFG80211_STA_IBSS:
5480 	case CFG80211_STA_AP_STA:
5481 		/* reject any changes other than AUTHORIZED */
5482 		if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
5483 			return -EINVAL;
5484 		break;
5485 	case CFG80211_STA_TDLS_PEER_SETUP:
5486 		/* reject any changes other than AUTHORIZED or WME */
5487 		if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
5488 					       BIT(NL80211_STA_FLAG_WME)))
5489 			return -EINVAL;
5490 		/* force (at least) rates when authorizing */
5491 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
5492 		    !params->supported_rates)
5493 			return -EINVAL;
5494 		break;
5495 	case CFG80211_STA_TDLS_PEER_ACTIVE:
5496 		/* reject any changes */
5497 		return -EINVAL;
5498 	case CFG80211_STA_MESH_PEER_KERNEL:
5499 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
5500 			return -EINVAL;
5501 		break;
5502 	case CFG80211_STA_MESH_PEER_USER:
5503 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
5504 		    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
5505 			return -EINVAL;
5506 		break;
5507 	}
5508 
5509 	/*
5510 	 * Older kernel versions ignored this attribute entirely, so don't
5511 	 * reject attempts to update it but mark it as unused instead so the
5512 	 * driver won't look at the data.
5513 	 */
5514 	if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
5515 	    statype != CFG80211_STA_TDLS_PEER_SETUP)
5516 		params->opmode_notif_used = false;
5517 
5518 	return 0;
5519 }
5520 EXPORT_SYMBOL(cfg80211_check_station_change);
5521 
5522 /*
5523  * Get vlan interface making sure it is running and on the right wiphy.
5524  */
5525 static struct net_device *get_vlan(struct genl_info *info,
5526 				   struct cfg80211_registered_device *rdev)
5527 {
5528 	struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
5529 	struct net_device *v;
5530 	int ret;
5531 
5532 	if (!vlanattr)
5533 		return NULL;
5534 
5535 	v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
5536 	if (!v)
5537 		return ERR_PTR(-ENODEV);
5538 
5539 	if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
5540 		ret = -EINVAL;
5541 		goto error;
5542 	}
5543 
5544 	if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5545 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5546 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
5547 		ret = -EINVAL;
5548 		goto error;
5549 	}
5550 
5551 	if (!netif_running(v)) {
5552 		ret = -ENETDOWN;
5553 		goto error;
5554 	}
5555 
5556 	return v;
5557  error:
5558 	dev_put(v);
5559 	return ERR_PTR(ret);
5560 }
5561 
5562 static const struct nla_policy
5563 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
5564 	[NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
5565 	[NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
5566 };
5567 
5568 static int nl80211_parse_sta_wme(struct genl_info *info,
5569 				 struct station_parameters *params)
5570 {
5571 	struct nlattr *tb[NL80211_STA_WME_MAX + 1];
5572 	struct nlattr *nla;
5573 	int err;
5574 
5575 	/* parse WME attributes if present */
5576 	if (!info->attrs[NL80211_ATTR_STA_WME])
5577 		return 0;
5578 
5579 	nla = info->attrs[NL80211_ATTR_STA_WME];
5580 	err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
5581 					  nl80211_sta_wme_policy,
5582 					  info->extack);
5583 	if (err)
5584 		return err;
5585 
5586 	if (tb[NL80211_STA_WME_UAPSD_QUEUES])
5587 		params->uapsd_queues = nla_get_u8(
5588 			tb[NL80211_STA_WME_UAPSD_QUEUES]);
5589 	if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
5590 		return -EINVAL;
5591 
5592 	if (tb[NL80211_STA_WME_MAX_SP])
5593 		params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
5594 
5595 	if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
5596 		return -EINVAL;
5597 
5598 	params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
5599 
5600 	return 0;
5601 }
5602 
5603 static int nl80211_parse_sta_channel_info(struct genl_info *info,
5604 				      struct station_parameters *params)
5605 {
5606 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
5607 		params->supported_channels =
5608 		     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
5609 		params->supported_channels_len =
5610 		     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
5611 		/*
5612 		 * Need to include at least one (first channel, number of
5613 		 * channels) tuple for each subband, and must have proper
5614 		 * tuples for the rest of the data as well.
5615 		 */
5616 		if (params->supported_channels_len < 2)
5617 			return -EINVAL;
5618 		if (params->supported_channels_len % 2)
5619 			return -EINVAL;
5620 	}
5621 
5622 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
5623 		params->supported_oper_classes =
5624 		 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
5625 		params->supported_oper_classes_len =
5626 		  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
5627 		/*
5628 		 * The value of the Length field of the Supported Operating
5629 		 * Classes element is between 2 and 253.
5630 		 */
5631 		if (params->supported_oper_classes_len < 2 ||
5632 		    params->supported_oper_classes_len > 253)
5633 			return -EINVAL;
5634 	}
5635 	return 0;
5636 }
5637 
5638 static int nl80211_set_station_tdls(struct genl_info *info,
5639 				    struct station_parameters *params)
5640 {
5641 	int err;
5642 	/* Dummy STA entry gets updated once the peer capabilities are known */
5643 	if (info->attrs[NL80211_ATTR_PEER_AID])
5644 		params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5645 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5646 		params->ht_capa =
5647 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5648 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5649 		params->vht_capa =
5650 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5651 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
5652 		params->he_capa =
5653 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5654 		params->he_capa_len =
5655 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5656 
5657 		if (params->he_capa_len < NL80211_HE_MIN_CAPABILITY_LEN)
5658 			return -EINVAL;
5659 	}
5660 
5661 	err = nl80211_parse_sta_channel_info(info, params);
5662 	if (err)
5663 		return err;
5664 
5665 	return nl80211_parse_sta_wme(info, params);
5666 }
5667 
5668 static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
5669 					     struct station_parameters *params)
5670 {
5671 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5672 	int idx;
5673 
5674 	if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
5675 		if (!rdev->ops->set_tx_power ||
5676 		    !wiphy_ext_feature_isset(&rdev->wiphy,
5677 					 NL80211_EXT_FEATURE_STA_TX_PWR))
5678 			return -EOPNOTSUPP;
5679 
5680 		idx = NL80211_ATTR_STA_TX_POWER_SETTING;
5681 		params->txpwr.type = nla_get_u8(info->attrs[idx]);
5682 
5683 		if (params->txpwr.type == NL80211_TX_POWER_LIMITED) {
5684 			idx = NL80211_ATTR_STA_TX_POWER;
5685 
5686 			if (info->attrs[idx])
5687 				params->txpwr.power =
5688 					nla_get_s16(info->attrs[idx]);
5689 			else
5690 				return -EINVAL;
5691 		}
5692 		params->sta_modify_mask |= STATION_PARAM_APPLY_STA_TXPOWER;
5693 	}
5694 
5695 	return 0;
5696 }
5697 
5698 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
5699 {
5700 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5701 	struct net_device *dev = info->user_ptr[1];
5702 	struct station_parameters params;
5703 	u8 *mac_addr;
5704 	int err;
5705 
5706 	memset(&params, 0, sizeof(params));
5707 
5708 	if (!rdev->ops->change_station)
5709 		return -EOPNOTSUPP;
5710 
5711 	/*
5712 	 * AID and listen_interval properties can be set only for unassociated
5713 	 * station. Include these parameters here and will check them in
5714 	 * cfg80211_check_station_change().
5715 	 */
5716 	if (info->attrs[NL80211_ATTR_STA_AID])
5717 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5718 
5719 	if (info->attrs[NL80211_ATTR_VLAN_ID])
5720 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
5721 
5722 	if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5723 		params.listen_interval =
5724 		     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5725 	else
5726 		params.listen_interval = -1;
5727 
5728 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
5729 		params.support_p2p_ps =
5730 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5731 	else
5732 		params.support_p2p_ps = -1;
5733 
5734 	if (!info->attrs[NL80211_ATTR_MAC])
5735 		return -EINVAL;
5736 
5737 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5738 
5739 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
5740 		params.supported_rates =
5741 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5742 		params.supported_rates_len =
5743 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5744 	}
5745 
5746 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5747 		params.capability =
5748 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5749 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5750 	}
5751 
5752 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5753 		params.ext_capab =
5754 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5755 		params.ext_capab_len =
5756 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5757 	}
5758 
5759 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5760 		return -EINVAL;
5761 
5762 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
5763 		params.plink_action =
5764 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5765 
5766 	if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
5767 		params.plink_state =
5768 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
5769 		if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
5770 			params.peer_aid = nla_get_u16(
5771 				info->attrs[NL80211_ATTR_MESH_PEER_AID]);
5772 		params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
5773 	}
5774 
5775 	if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
5776 		params.local_pm = nla_get_u32(
5777 			info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
5778 
5779 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5780 		params.opmode_notif_used = true;
5781 		params.opmode_notif =
5782 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5783 	}
5784 
5785 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
5786 		params.airtime_weight =
5787 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
5788 
5789 	if (params.airtime_weight &&
5790 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5791 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5792 		return -EOPNOTSUPP;
5793 
5794 	err = nl80211_parse_sta_txpower_setting(info, &params);
5795 	if (err)
5796 		return err;
5797 
5798 	/* Include parameters for TDLS peer (will check later) */
5799 	err = nl80211_set_station_tdls(info, &params);
5800 	if (err)
5801 		return err;
5802 
5803 	params.vlan = get_vlan(info, rdev);
5804 	if (IS_ERR(params.vlan))
5805 		return PTR_ERR(params.vlan);
5806 
5807 	switch (dev->ieee80211_ptr->iftype) {
5808 	case NL80211_IFTYPE_AP:
5809 	case NL80211_IFTYPE_AP_VLAN:
5810 	case NL80211_IFTYPE_P2P_GO:
5811 	case NL80211_IFTYPE_P2P_CLIENT:
5812 	case NL80211_IFTYPE_STATION:
5813 	case NL80211_IFTYPE_ADHOC:
5814 	case NL80211_IFTYPE_MESH_POINT:
5815 		break;
5816 	default:
5817 		err = -EOPNOTSUPP;
5818 		goto out_put_vlan;
5819 	}
5820 
5821 	/* driver will call cfg80211_check_station_change() */
5822 	err = rdev_change_station(rdev, dev, mac_addr, &params);
5823 
5824  out_put_vlan:
5825 	if (params.vlan)
5826 		dev_put(params.vlan);
5827 
5828 	return err;
5829 }
5830 
5831 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
5832 {
5833 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5834 	int err;
5835 	struct net_device *dev = info->user_ptr[1];
5836 	struct station_parameters params;
5837 	u8 *mac_addr = NULL;
5838 	u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5839 			 BIT(NL80211_STA_FLAG_ASSOCIATED);
5840 
5841 	memset(&params, 0, sizeof(params));
5842 
5843 	if (!rdev->ops->add_station)
5844 		return -EOPNOTSUPP;
5845 
5846 	if (!info->attrs[NL80211_ATTR_MAC])
5847 		return -EINVAL;
5848 
5849 	if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5850 		return -EINVAL;
5851 
5852 	if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
5853 		return -EINVAL;
5854 
5855 	if (!info->attrs[NL80211_ATTR_STA_AID] &&
5856 	    !info->attrs[NL80211_ATTR_PEER_AID])
5857 		return -EINVAL;
5858 
5859 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5860 	params.supported_rates =
5861 		nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5862 	params.supported_rates_len =
5863 		nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5864 	params.listen_interval =
5865 		nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5866 
5867 	if (info->attrs[NL80211_ATTR_VLAN_ID])
5868 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
5869 
5870 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
5871 		params.support_p2p_ps =
5872 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5873 	} else {
5874 		/*
5875 		 * if not specified, assume it's supported for P2P GO interface,
5876 		 * and is NOT supported for AP interface
5877 		 */
5878 		params.support_p2p_ps =
5879 			dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
5880 	}
5881 
5882 	if (info->attrs[NL80211_ATTR_PEER_AID])
5883 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5884 	else
5885 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5886 
5887 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5888 		params.capability =
5889 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5890 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5891 	}
5892 
5893 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5894 		params.ext_capab =
5895 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5896 		params.ext_capab_len =
5897 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5898 	}
5899 
5900 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5901 		params.ht_capa =
5902 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5903 
5904 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5905 		params.vht_capa =
5906 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5907 
5908 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
5909 		params.he_capa =
5910 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5911 		params.he_capa_len =
5912 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5913 
5914 		/* max len is validated in nla policy */
5915 		if (params.he_capa_len < NL80211_HE_MIN_CAPABILITY_LEN)
5916 			return -EINVAL;
5917 	}
5918 
5919 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5920 		params.opmode_notif_used = true;
5921 		params.opmode_notif =
5922 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5923 	}
5924 
5925 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
5926 		params.plink_action =
5927 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5928 
5929 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
5930 		params.airtime_weight =
5931 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
5932 
5933 	if (params.airtime_weight &&
5934 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5935 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5936 		return -EOPNOTSUPP;
5937 
5938 	err = nl80211_parse_sta_txpower_setting(info, &params);
5939 	if (err)
5940 		return err;
5941 
5942 	err = nl80211_parse_sta_channel_info(info, &params);
5943 	if (err)
5944 		return err;
5945 
5946 	err = nl80211_parse_sta_wme(info, &params);
5947 	if (err)
5948 		return err;
5949 
5950 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5951 		return -EINVAL;
5952 
5953 	/* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
5954 	 * as userspace might just pass through the capabilities from the IEs
5955 	 * directly, rather than enforcing this restriction and returning an
5956 	 * error in this case.
5957 	 */
5958 	if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
5959 		params.ht_capa = NULL;
5960 		params.vht_capa = NULL;
5961 
5962 		/* HE requires WME */
5963 		if (params.he_capa_len)
5964 			return -EINVAL;
5965 	}
5966 
5967 	/* When you run into this, adjust the code below for the new flag */
5968 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5969 
5970 	switch (dev->ieee80211_ptr->iftype) {
5971 	case NL80211_IFTYPE_AP:
5972 	case NL80211_IFTYPE_AP_VLAN:
5973 	case NL80211_IFTYPE_P2P_GO:
5974 		/* ignore WME attributes if iface/sta is not capable */
5975 		if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
5976 		    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
5977 			params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5978 
5979 		/* TDLS peers cannot be added */
5980 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5981 		    info->attrs[NL80211_ATTR_PEER_AID])
5982 			return -EINVAL;
5983 		/* but don't bother the driver with it */
5984 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5985 
5986 		/* allow authenticated/associated only if driver handles it */
5987 		if (!(rdev->wiphy.features &
5988 				NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5989 		    params.sta_flags_mask & auth_assoc)
5990 			return -EINVAL;
5991 
5992 		/* Older userspace, or userspace wanting to be compatible with
5993 		 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
5994 		 * and assoc flags in the mask, but assumes the station will be
5995 		 * added as associated anyway since this was the required driver
5996 		 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
5997 		 * introduced.
5998 		 * In order to not bother drivers with this quirk in the API
5999 		 * set the flags in both the mask and set for new stations in
6000 		 * this case.
6001 		 */
6002 		if (!(params.sta_flags_mask & auth_assoc)) {
6003 			params.sta_flags_mask |= auth_assoc;
6004 			params.sta_flags_set |= auth_assoc;
6005 		}
6006 
6007 		/* must be last in here for error handling */
6008 		params.vlan = get_vlan(info, rdev);
6009 		if (IS_ERR(params.vlan))
6010 			return PTR_ERR(params.vlan);
6011 		break;
6012 	case NL80211_IFTYPE_MESH_POINT:
6013 		/* ignore uAPSD data */
6014 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6015 
6016 		/* associated is disallowed */
6017 		if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
6018 			return -EINVAL;
6019 		/* TDLS peers cannot be added */
6020 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
6021 		    info->attrs[NL80211_ATTR_PEER_AID])
6022 			return -EINVAL;
6023 		break;
6024 	case NL80211_IFTYPE_STATION:
6025 	case NL80211_IFTYPE_P2P_CLIENT:
6026 		/* ignore uAPSD data */
6027 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6028 
6029 		/* these are disallowed */
6030 		if (params.sta_flags_mask &
6031 				(BIT(NL80211_STA_FLAG_ASSOCIATED) |
6032 				 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
6033 			return -EINVAL;
6034 		/* Only TDLS peers can be added */
6035 		if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6036 			return -EINVAL;
6037 		/* Can only add if TDLS ... */
6038 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
6039 			return -EOPNOTSUPP;
6040 		/* ... with external setup is supported */
6041 		if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
6042 			return -EOPNOTSUPP;
6043 		/*
6044 		 * Older wpa_supplicant versions always mark the TDLS peer
6045 		 * as authorized, but it shouldn't yet be.
6046 		 */
6047 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
6048 		break;
6049 	default:
6050 		return -EOPNOTSUPP;
6051 	}
6052 
6053 	/* be aware of params.vlan when changing code here */
6054 
6055 	err = rdev_add_station(rdev, dev, mac_addr, &params);
6056 
6057 	if (params.vlan)
6058 		dev_put(params.vlan);
6059 	return err;
6060 }
6061 
6062 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
6063 {
6064 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6065 	struct net_device *dev = info->user_ptr[1];
6066 	struct station_del_parameters params;
6067 
6068 	memset(&params, 0, sizeof(params));
6069 
6070 	if (info->attrs[NL80211_ATTR_MAC])
6071 		params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
6072 
6073 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6074 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
6075 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
6076 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6077 		return -EINVAL;
6078 
6079 	if (!rdev->ops->del_station)
6080 		return -EOPNOTSUPP;
6081 
6082 	if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
6083 		params.subtype =
6084 			nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
6085 		if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
6086 		    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
6087 			return -EINVAL;
6088 	} else {
6089 		/* Default to Deauthentication frame */
6090 		params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
6091 	}
6092 
6093 	if (info->attrs[NL80211_ATTR_REASON_CODE]) {
6094 		params.reason_code =
6095 			nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6096 		if (params.reason_code == 0)
6097 			return -EINVAL; /* 0 is reserved */
6098 	} else {
6099 		/* Default to reason code 2 */
6100 		params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
6101 	}
6102 
6103 	return rdev_del_station(rdev, dev, &params);
6104 }
6105 
6106 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
6107 				int flags, struct net_device *dev,
6108 				u8 *dst, u8 *next_hop,
6109 				struct mpath_info *pinfo)
6110 {
6111 	void *hdr;
6112 	struct nlattr *pinfoattr;
6113 
6114 	hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
6115 	if (!hdr)
6116 		return -1;
6117 
6118 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6119 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
6120 	    nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
6121 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
6122 		goto nla_put_failure;
6123 
6124 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
6125 	if (!pinfoattr)
6126 		goto nla_put_failure;
6127 	if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
6128 	    nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
6129 			pinfo->frame_qlen))
6130 		goto nla_put_failure;
6131 	if (((pinfo->filled & MPATH_INFO_SN) &&
6132 	     nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
6133 	    ((pinfo->filled & MPATH_INFO_METRIC) &&
6134 	     nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
6135 			 pinfo->metric)) ||
6136 	    ((pinfo->filled & MPATH_INFO_EXPTIME) &&
6137 	     nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
6138 			 pinfo->exptime)) ||
6139 	    ((pinfo->filled & MPATH_INFO_FLAGS) &&
6140 	     nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
6141 			pinfo->flags)) ||
6142 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
6143 	     nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
6144 			 pinfo->discovery_timeout)) ||
6145 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
6146 	     nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
6147 			pinfo->discovery_retries)) ||
6148 	    ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
6149 	     nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
6150 			pinfo->hop_count)) ||
6151 	    ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
6152 	     nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
6153 			 pinfo->path_change_count)))
6154 		goto nla_put_failure;
6155 
6156 	nla_nest_end(msg, pinfoattr);
6157 
6158 	genlmsg_end(msg, hdr);
6159 	return 0;
6160 
6161  nla_put_failure:
6162 	genlmsg_cancel(msg, hdr);
6163 	return -EMSGSIZE;
6164 }
6165 
6166 static int nl80211_dump_mpath(struct sk_buff *skb,
6167 			      struct netlink_callback *cb)
6168 {
6169 	struct mpath_info pinfo;
6170 	struct cfg80211_registered_device *rdev;
6171 	struct wireless_dev *wdev;
6172 	u8 dst[ETH_ALEN];
6173 	u8 next_hop[ETH_ALEN];
6174 	int path_idx = cb->args[2];
6175 	int err;
6176 
6177 	rtnl_lock();
6178 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
6179 	if (err)
6180 		goto out_err;
6181 
6182 	if (!rdev->ops->dump_mpath) {
6183 		err = -EOPNOTSUPP;
6184 		goto out_err;
6185 	}
6186 
6187 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
6188 		err = -EOPNOTSUPP;
6189 		goto out_err;
6190 	}
6191 
6192 	while (1) {
6193 		err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
6194 				      next_hop, &pinfo);
6195 		if (err == -ENOENT)
6196 			break;
6197 		if (err)
6198 			goto out_err;
6199 
6200 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
6201 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
6202 				       wdev->netdev, dst, next_hop,
6203 				       &pinfo) < 0)
6204 			goto out;
6205 
6206 		path_idx++;
6207 	}
6208 
6209  out:
6210 	cb->args[2] = path_idx;
6211 	err = skb->len;
6212  out_err:
6213 	rtnl_unlock();
6214 	return err;
6215 }
6216 
6217 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
6218 {
6219 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6220 	int err;
6221 	struct net_device *dev = info->user_ptr[1];
6222 	struct mpath_info pinfo;
6223 	struct sk_buff *msg;
6224 	u8 *dst = NULL;
6225 	u8 next_hop[ETH_ALEN];
6226 
6227 	memset(&pinfo, 0, sizeof(pinfo));
6228 
6229 	if (!info->attrs[NL80211_ATTR_MAC])
6230 		return -EINVAL;
6231 
6232 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6233 
6234 	if (!rdev->ops->get_mpath)
6235 		return -EOPNOTSUPP;
6236 
6237 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6238 		return -EOPNOTSUPP;
6239 
6240 	err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
6241 	if (err)
6242 		return err;
6243 
6244 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6245 	if (!msg)
6246 		return -ENOMEM;
6247 
6248 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
6249 				 dev, dst, next_hop, &pinfo) < 0) {
6250 		nlmsg_free(msg);
6251 		return -ENOBUFS;
6252 	}
6253 
6254 	return genlmsg_reply(msg, info);
6255 }
6256 
6257 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
6258 {
6259 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6260 	struct net_device *dev = info->user_ptr[1];
6261 	u8 *dst = NULL;
6262 	u8 *next_hop = NULL;
6263 
6264 	if (!info->attrs[NL80211_ATTR_MAC])
6265 		return -EINVAL;
6266 
6267 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
6268 		return -EINVAL;
6269 
6270 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6271 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
6272 
6273 	if (!rdev->ops->change_mpath)
6274 		return -EOPNOTSUPP;
6275 
6276 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6277 		return -EOPNOTSUPP;
6278 
6279 	return rdev_change_mpath(rdev, dev, dst, next_hop);
6280 }
6281 
6282 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
6283 {
6284 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6285 	struct net_device *dev = info->user_ptr[1];
6286 	u8 *dst = NULL;
6287 	u8 *next_hop = NULL;
6288 
6289 	if (!info->attrs[NL80211_ATTR_MAC])
6290 		return -EINVAL;
6291 
6292 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
6293 		return -EINVAL;
6294 
6295 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6296 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
6297 
6298 	if (!rdev->ops->add_mpath)
6299 		return -EOPNOTSUPP;
6300 
6301 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6302 		return -EOPNOTSUPP;
6303 
6304 	return rdev_add_mpath(rdev, dev, dst, next_hop);
6305 }
6306 
6307 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
6308 {
6309 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6310 	struct net_device *dev = info->user_ptr[1];
6311 	u8 *dst = NULL;
6312 
6313 	if (info->attrs[NL80211_ATTR_MAC])
6314 		dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6315 
6316 	if (!rdev->ops->del_mpath)
6317 		return -EOPNOTSUPP;
6318 
6319 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6320 		return -EOPNOTSUPP;
6321 
6322 	return rdev_del_mpath(rdev, dev, dst);
6323 }
6324 
6325 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
6326 {
6327 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6328 	int err;
6329 	struct net_device *dev = info->user_ptr[1];
6330 	struct mpath_info pinfo;
6331 	struct sk_buff *msg;
6332 	u8 *dst = NULL;
6333 	u8 mpp[ETH_ALEN];
6334 
6335 	memset(&pinfo, 0, sizeof(pinfo));
6336 
6337 	if (!info->attrs[NL80211_ATTR_MAC])
6338 		return -EINVAL;
6339 
6340 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6341 
6342 	if (!rdev->ops->get_mpp)
6343 		return -EOPNOTSUPP;
6344 
6345 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6346 		return -EOPNOTSUPP;
6347 
6348 	err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
6349 	if (err)
6350 		return err;
6351 
6352 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6353 	if (!msg)
6354 		return -ENOMEM;
6355 
6356 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
6357 			       dev, dst, mpp, &pinfo) < 0) {
6358 		nlmsg_free(msg);
6359 		return -ENOBUFS;
6360 	}
6361 
6362 	return genlmsg_reply(msg, info);
6363 }
6364 
6365 static int nl80211_dump_mpp(struct sk_buff *skb,
6366 			    struct netlink_callback *cb)
6367 {
6368 	struct mpath_info pinfo;
6369 	struct cfg80211_registered_device *rdev;
6370 	struct wireless_dev *wdev;
6371 	u8 dst[ETH_ALEN];
6372 	u8 mpp[ETH_ALEN];
6373 	int path_idx = cb->args[2];
6374 	int err;
6375 
6376 	rtnl_lock();
6377 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
6378 	if (err)
6379 		goto out_err;
6380 
6381 	if (!rdev->ops->dump_mpp) {
6382 		err = -EOPNOTSUPP;
6383 		goto out_err;
6384 	}
6385 
6386 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
6387 		err = -EOPNOTSUPP;
6388 		goto out_err;
6389 	}
6390 
6391 	while (1) {
6392 		err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
6393 				    mpp, &pinfo);
6394 		if (err == -ENOENT)
6395 			break;
6396 		if (err)
6397 			goto out_err;
6398 
6399 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
6400 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
6401 				       wdev->netdev, dst, mpp,
6402 				       &pinfo) < 0)
6403 			goto out;
6404 
6405 		path_idx++;
6406 	}
6407 
6408  out:
6409 	cb->args[2] = path_idx;
6410 	err = skb->len;
6411  out_err:
6412 	rtnl_unlock();
6413 	return err;
6414 }
6415 
6416 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
6417 {
6418 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6419 	struct net_device *dev = info->user_ptr[1];
6420 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6421 	struct bss_parameters params;
6422 	int err;
6423 
6424 	memset(&params, 0, sizeof(params));
6425 	/* default to not changing parameters */
6426 	params.use_cts_prot = -1;
6427 	params.use_short_preamble = -1;
6428 	params.use_short_slot_time = -1;
6429 	params.ap_isolate = -1;
6430 	params.ht_opmode = -1;
6431 	params.p2p_ctwindow = -1;
6432 	params.p2p_opp_ps = -1;
6433 
6434 	if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
6435 		params.use_cts_prot =
6436 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
6437 	if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
6438 		params.use_short_preamble =
6439 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
6440 	if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
6441 		params.use_short_slot_time =
6442 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
6443 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6444 		params.basic_rates =
6445 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6446 		params.basic_rates_len =
6447 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6448 	}
6449 	if (info->attrs[NL80211_ATTR_AP_ISOLATE])
6450 		params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
6451 	if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
6452 		params.ht_opmode =
6453 			nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
6454 
6455 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
6456 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6457 			return -EINVAL;
6458 		params.p2p_ctwindow =
6459 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
6460 		if (params.p2p_ctwindow != 0 &&
6461 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
6462 			return -EINVAL;
6463 	}
6464 
6465 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
6466 		u8 tmp;
6467 
6468 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6469 			return -EINVAL;
6470 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
6471 		params.p2p_opp_ps = tmp;
6472 		if (params.p2p_opp_ps &&
6473 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
6474 			return -EINVAL;
6475 	}
6476 
6477 	if (!rdev->ops->change_bss)
6478 		return -EOPNOTSUPP;
6479 
6480 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6481 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6482 		return -EOPNOTSUPP;
6483 
6484 	wdev_lock(wdev);
6485 	err = rdev_change_bss(rdev, dev, &params);
6486 	wdev_unlock(wdev);
6487 
6488 	return err;
6489 }
6490 
6491 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
6492 {
6493 	char *data = NULL;
6494 	bool is_indoor;
6495 	enum nl80211_user_reg_hint_type user_reg_hint_type;
6496 	u32 owner_nlportid;
6497 
6498 	/*
6499 	 * You should only get this when cfg80211 hasn't yet initialized
6500 	 * completely when built-in to the kernel right between the time
6501 	 * window between nl80211_init() and regulatory_init(), if that is
6502 	 * even possible.
6503 	 */
6504 	if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
6505 		return -EINPROGRESS;
6506 
6507 	if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
6508 		user_reg_hint_type =
6509 		  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
6510 	else
6511 		user_reg_hint_type = NL80211_USER_REG_HINT_USER;
6512 
6513 	switch (user_reg_hint_type) {
6514 	case NL80211_USER_REG_HINT_USER:
6515 	case NL80211_USER_REG_HINT_CELL_BASE:
6516 		if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
6517 			return -EINVAL;
6518 
6519 		data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
6520 		return regulatory_hint_user(data, user_reg_hint_type);
6521 	case NL80211_USER_REG_HINT_INDOOR:
6522 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
6523 			owner_nlportid = info->snd_portid;
6524 			is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
6525 		} else {
6526 			owner_nlportid = 0;
6527 			is_indoor = true;
6528 		}
6529 
6530 		return regulatory_hint_indoor(is_indoor, owner_nlportid);
6531 	default:
6532 		return -EINVAL;
6533 	}
6534 }
6535 
6536 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
6537 {
6538 	return reg_reload_regdb();
6539 }
6540 
6541 static int nl80211_get_mesh_config(struct sk_buff *skb,
6542 				   struct genl_info *info)
6543 {
6544 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6545 	struct net_device *dev = info->user_ptr[1];
6546 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6547 	struct mesh_config cur_params;
6548 	int err = 0;
6549 	void *hdr;
6550 	struct nlattr *pinfoattr;
6551 	struct sk_buff *msg;
6552 
6553 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6554 		return -EOPNOTSUPP;
6555 
6556 	if (!rdev->ops->get_mesh_config)
6557 		return -EOPNOTSUPP;
6558 
6559 	wdev_lock(wdev);
6560 	/* If not connected, get default parameters */
6561 	if (!wdev->mesh_id_len)
6562 		memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
6563 	else
6564 		err = rdev_get_mesh_config(rdev, dev, &cur_params);
6565 	wdev_unlock(wdev);
6566 
6567 	if (err)
6568 		return err;
6569 
6570 	/* Draw up a netlink message to send back */
6571 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6572 	if (!msg)
6573 		return -ENOMEM;
6574 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
6575 			     NL80211_CMD_GET_MESH_CONFIG);
6576 	if (!hdr)
6577 		goto out;
6578 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
6579 	if (!pinfoattr)
6580 		goto nla_put_failure;
6581 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6582 	    nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
6583 			cur_params.dot11MeshRetryTimeout) ||
6584 	    nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
6585 			cur_params.dot11MeshConfirmTimeout) ||
6586 	    nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
6587 			cur_params.dot11MeshHoldingTimeout) ||
6588 	    nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
6589 			cur_params.dot11MeshMaxPeerLinks) ||
6590 	    nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
6591 		       cur_params.dot11MeshMaxRetries) ||
6592 	    nla_put_u8(msg, NL80211_MESHCONF_TTL,
6593 		       cur_params.dot11MeshTTL) ||
6594 	    nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
6595 		       cur_params.element_ttl) ||
6596 	    nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6597 		       cur_params.auto_open_plinks) ||
6598 	    nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6599 			cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
6600 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6601 		       cur_params.dot11MeshHWMPmaxPREQretries) ||
6602 	    nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
6603 			cur_params.path_refresh_time) ||
6604 	    nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6605 			cur_params.min_discovery_timeout) ||
6606 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6607 			cur_params.dot11MeshHWMPactivePathTimeout) ||
6608 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6609 			cur_params.dot11MeshHWMPpreqMinInterval) ||
6610 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6611 			cur_params.dot11MeshHWMPperrMinInterval) ||
6612 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6613 			cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
6614 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
6615 		       cur_params.dot11MeshHWMPRootMode) ||
6616 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6617 			cur_params.dot11MeshHWMPRannInterval) ||
6618 	    nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6619 		       cur_params.dot11MeshGateAnnouncementProtocol) ||
6620 	    nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
6621 		       cur_params.dot11MeshForwarding) ||
6622 	    nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
6623 			cur_params.rssi_threshold) ||
6624 	    nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
6625 			cur_params.ht_opmode) ||
6626 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6627 			cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
6628 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6629 			cur_params.dot11MeshHWMProotInterval) ||
6630 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6631 			cur_params.dot11MeshHWMPconfirmationInterval) ||
6632 	    nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
6633 			cur_params.power_mode) ||
6634 	    nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
6635 			cur_params.dot11MeshAwakeWindowDuration) ||
6636 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
6637 			cur_params.plink_timeout) ||
6638 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
6639 		       cur_params.dot11MeshConnectedToMeshGate))
6640 		goto nla_put_failure;
6641 	nla_nest_end(msg, pinfoattr);
6642 	genlmsg_end(msg, hdr);
6643 	return genlmsg_reply(msg, info);
6644 
6645  nla_put_failure:
6646  out:
6647 	nlmsg_free(msg);
6648 	return -ENOBUFS;
6649 }
6650 
6651 static const struct nla_policy
6652 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
6653 	[NL80211_MESHCONF_RETRY_TIMEOUT] =
6654 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
6655 	[NL80211_MESHCONF_CONFIRM_TIMEOUT] =
6656 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
6657 	[NL80211_MESHCONF_HOLDING_TIMEOUT] =
6658 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
6659 	[NL80211_MESHCONF_MAX_PEER_LINKS] =
6660 		NLA_POLICY_RANGE(NLA_U16, 0, 255),
6661 	[NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
6662 	[NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
6663 	[NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
6664 	[NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
6665 	[NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
6666 		NLA_POLICY_RANGE(NLA_U32, 1, 255),
6667 	[NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
6668 	[NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
6669 	[NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
6670 	[NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
6671 	[NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
6672 		NLA_POLICY_MIN(NLA_U16, 1),
6673 	[NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
6674 		NLA_POLICY_MIN(NLA_U16, 1),
6675 	[NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
6676 		NLA_POLICY_MIN(NLA_U16, 1),
6677 	[NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
6678 	[NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
6679 		NLA_POLICY_MIN(NLA_U16, 1),
6680 	[NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
6681 	[NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
6682 	[NL80211_MESHCONF_RSSI_THRESHOLD] =
6683 		NLA_POLICY_RANGE(NLA_S32, -255, 0),
6684 	[NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
6685 	[NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
6686 	[NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
6687 		NLA_POLICY_MIN(NLA_U16, 1),
6688 	[NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
6689 		NLA_POLICY_MIN(NLA_U16, 1),
6690 	[NL80211_MESHCONF_POWER_MODE] =
6691 		NLA_POLICY_RANGE(NLA_U32,
6692 				 NL80211_MESH_POWER_ACTIVE,
6693 				 NL80211_MESH_POWER_MAX),
6694 	[NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
6695 	[NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
6696 	[NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
6697 };
6698 
6699 static const struct nla_policy
6700 	nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
6701 	[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
6702 	[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
6703 	[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
6704 	[NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
6705 	[NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
6706 	[NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
6707 	[NL80211_MESH_SETUP_IE] =
6708 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
6709 				       IEEE80211_MAX_DATA_LEN),
6710 	[NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
6711 };
6712 
6713 static int nl80211_parse_mesh_config(struct genl_info *info,
6714 				     struct mesh_config *cfg,
6715 				     u32 *mask_out)
6716 {
6717 	struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
6718 	u32 mask = 0;
6719 	u16 ht_opmode;
6720 
6721 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)	\
6722 do {									\
6723 	if (tb[attr]) {							\
6724 		cfg->param = fn(tb[attr]);				\
6725 		mask |= BIT((attr) - 1);				\
6726 	}								\
6727 } while (0)
6728 
6729 	if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
6730 		return -EINVAL;
6731 	if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
6732 		return -EINVAL;
6733 
6734 	/* This makes sure that there aren't more than 32 mesh config
6735 	 * parameters (otherwise our bitfield scheme would not work.) */
6736 	BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
6737 
6738 	/* Fill in the params struct */
6739 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
6740 				  NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
6741 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
6742 				  NL80211_MESHCONF_CONFIRM_TIMEOUT,
6743 				  nla_get_u16);
6744 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
6745 				  NL80211_MESHCONF_HOLDING_TIMEOUT,
6746 				  nla_get_u16);
6747 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
6748 				  NL80211_MESHCONF_MAX_PEER_LINKS,
6749 				  nla_get_u16);
6750 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
6751 				  NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
6752 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
6753 				  NL80211_MESHCONF_TTL, nla_get_u8);
6754 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
6755 				  NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
6756 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
6757 				  NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6758 				  nla_get_u8);
6759 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
6760 				  mask,
6761 				  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6762 				  nla_get_u32);
6763 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
6764 				  NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6765 				  nla_get_u8);
6766 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
6767 				  NL80211_MESHCONF_PATH_REFRESH_TIME,
6768 				  nla_get_u32);
6769 	if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
6770 	    (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
6771 		return -EINVAL;
6772 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
6773 				  NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6774 				  nla_get_u16);
6775 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
6776 				  mask,
6777 				  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6778 				  nla_get_u32);
6779 	if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
6780 	    (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
6781 	     cfg->dot11MeshHWMPactivePathTimeout > 65535))
6782 		return -EINVAL;
6783 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
6784 				  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6785 				  nla_get_u16);
6786 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
6787 				  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6788 				  nla_get_u16);
6789 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6790 				  dot11MeshHWMPnetDiameterTraversalTime, mask,
6791 				  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6792 				  nla_get_u16);
6793 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
6794 				  NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
6795 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
6796 				  NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6797 				  nla_get_u16);
6798 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
6799 				  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6800 				  nla_get_u8);
6801 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
6802 				  NL80211_MESHCONF_FORWARDING, nla_get_u8);
6803 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
6804 				  NL80211_MESHCONF_RSSI_THRESHOLD,
6805 				  nla_get_s32);
6806 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
6807 				  NL80211_MESHCONF_CONNECTED_TO_GATE,
6808 				  nla_get_u8);
6809 	/*
6810 	 * Check HT operation mode based on
6811 	 * IEEE 802.11-2016 9.4.2.57 HT Operation element.
6812 	 */
6813 	if (tb[NL80211_MESHCONF_HT_OPMODE]) {
6814 		ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
6815 
6816 		if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
6817 				  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
6818 				  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
6819 			return -EINVAL;
6820 
6821 		/* NON_HT_STA bit is reserved, but some programs set it */
6822 		ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
6823 
6824 		cfg->ht_opmode = ht_opmode;
6825 		mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
6826 	}
6827 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6828 				  dot11MeshHWMPactivePathToRootTimeout, mask,
6829 				  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6830 				  nla_get_u32);
6831 	if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
6832 	    (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
6833 	     cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
6834 		return -EINVAL;
6835 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
6836 				  NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6837 				  nla_get_u16);
6838 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
6839 				  mask,
6840 				  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6841 				  nla_get_u16);
6842 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
6843 				  NL80211_MESHCONF_POWER_MODE, nla_get_u32);
6844 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
6845 				  NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
6846 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
6847 				  NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
6848 	if (mask_out)
6849 		*mask_out = mask;
6850 
6851 	return 0;
6852 
6853 #undef FILL_IN_MESH_PARAM_IF_SET
6854 }
6855 
6856 static int nl80211_parse_mesh_setup(struct genl_info *info,
6857 				     struct mesh_setup *setup)
6858 {
6859 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6860 	struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
6861 
6862 	if (!info->attrs[NL80211_ATTR_MESH_SETUP])
6863 		return -EINVAL;
6864 	if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
6865 		return -EINVAL;
6866 
6867 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
6868 		setup->sync_method =
6869 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
6870 		 IEEE80211_SYNC_METHOD_VENDOR :
6871 		 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
6872 
6873 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
6874 		setup->path_sel_proto =
6875 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
6876 		 IEEE80211_PATH_PROTOCOL_VENDOR :
6877 		 IEEE80211_PATH_PROTOCOL_HWMP;
6878 
6879 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
6880 		setup->path_metric =
6881 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
6882 		 IEEE80211_PATH_METRIC_VENDOR :
6883 		 IEEE80211_PATH_METRIC_AIRTIME;
6884 
6885 	if (tb[NL80211_MESH_SETUP_IE]) {
6886 		struct nlattr *ieattr =
6887 			tb[NL80211_MESH_SETUP_IE];
6888 		setup->ie = nla_data(ieattr);
6889 		setup->ie_len = nla_len(ieattr);
6890 	}
6891 	if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
6892 	    !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
6893 		return -EINVAL;
6894 	setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
6895 	setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
6896 	setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
6897 	if (setup->is_secure)
6898 		setup->user_mpm = true;
6899 
6900 	if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
6901 		if (!setup->user_mpm)
6902 			return -EINVAL;
6903 		setup->auth_id =
6904 			nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
6905 	}
6906 
6907 	return 0;
6908 }
6909 
6910 static int nl80211_update_mesh_config(struct sk_buff *skb,
6911 				      struct genl_info *info)
6912 {
6913 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6914 	struct net_device *dev = info->user_ptr[1];
6915 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6916 	struct mesh_config cfg;
6917 	u32 mask;
6918 	int err;
6919 
6920 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6921 		return -EOPNOTSUPP;
6922 
6923 	if (!rdev->ops->update_mesh_config)
6924 		return -EOPNOTSUPP;
6925 
6926 	err = nl80211_parse_mesh_config(info, &cfg, &mask);
6927 	if (err)
6928 		return err;
6929 
6930 	wdev_lock(wdev);
6931 	if (!wdev->mesh_id_len)
6932 		err = -ENOLINK;
6933 
6934 	if (!err)
6935 		err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
6936 
6937 	wdev_unlock(wdev);
6938 
6939 	return err;
6940 }
6941 
6942 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
6943 			      struct sk_buff *msg)
6944 {
6945 	struct nlattr *nl_reg_rules;
6946 	unsigned int i;
6947 
6948 	if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
6949 	    (regdom->dfs_region &&
6950 	     nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
6951 		goto nla_put_failure;
6952 
6953 	nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
6954 	if (!nl_reg_rules)
6955 		goto nla_put_failure;
6956 
6957 	for (i = 0; i < regdom->n_reg_rules; i++) {
6958 		struct nlattr *nl_reg_rule;
6959 		const struct ieee80211_reg_rule *reg_rule;
6960 		const struct ieee80211_freq_range *freq_range;
6961 		const struct ieee80211_power_rule *power_rule;
6962 		unsigned int max_bandwidth_khz;
6963 
6964 		reg_rule = &regdom->reg_rules[i];
6965 		freq_range = &reg_rule->freq_range;
6966 		power_rule = &reg_rule->power_rule;
6967 
6968 		nl_reg_rule = nla_nest_start_noflag(msg, i);
6969 		if (!nl_reg_rule)
6970 			goto nla_put_failure;
6971 
6972 		max_bandwidth_khz = freq_range->max_bandwidth_khz;
6973 		if (!max_bandwidth_khz)
6974 			max_bandwidth_khz = reg_get_max_bandwidth(regdom,
6975 								  reg_rule);
6976 
6977 		if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
6978 				reg_rule->flags) ||
6979 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
6980 				freq_range->start_freq_khz) ||
6981 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
6982 				freq_range->end_freq_khz) ||
6983 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
6984 				max_bandwidth_khz) ||
6985 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
6986 				power_rule->max_antenna_gain) ||
6987 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
6988 				power_rule->max_eirp) ||
6989 		    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
6990 				reg_rule->dfs_cac_ms))
6991 			goto nla_put_failure;
6992 
6993 		nla_nest_end(msg, nl_reg_rule);
6994 	}
6995 
6996 	nla_nest_end(msg, nl_reg_rules);
6997 	return 0;
6998 
6999 nla_put_failure:
7000 	return -EMSGSIZE;
7001 }
7002 
7003 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
7004 {
7005 	const struct ieee80211_regdomain *regdom = NULL;
7006 	struct cfg80211_registered_device *rdev;
7007 	struct wiphy *wiphy = NULL;
7008 	struct sk_buff *msg;
7009 	void *hdr;
7010 
7011 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7012 	if (!msg)
7013 		return -ENOBUFS;
7014 
7015 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7016 			     NL80211_CMD_GET_REG);
7017 	if (!hdr)
7018 		goto put_failure;
7019 
7020 	if (info->attrs[NL80211_ATTR_WIPHY]) {
7021 		bool self_managed;
7022 
7023 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
7024 		if (IS_ERR(rdev)) {
7025 			nlmsg_free(msg);
7026 			return PTR_ERR(rdev);
7027 		}
7028 
7029 		wiphy = &rdev->wiphy;
7030 		self_managed = wiphy->regulatory_flags &
7031 			       REGULATORY_WIPHY_SELF_MANAGED;
7032 		regdom = get_wiphy_regdom(wiphy);
7033 
7034 		/* a self-managed-reg device must have a private regdom */
7035 		if (WARN_ON(!regdom && self_managed)) {
7036 			nlmsg_free(msg);
7037 			return -EINVAL;
7038 		}
7039 
7040 		if (regdom &&
7041 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7042 			goto nla_put_failure;
7043 	}
7044 
7045 	if (!wiphy && reg_last_request_cell_base() &&
7046 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
7047 			NL80211_USER_REG_HINT_CELL_BASE))
7048 		goto nla_put_failure;
7049 
7050 	rcu_read_lock();
7051 
7052 	if (!regdom)
7053 		regdom = rcu_dereference(cfg80211_regdomain);
7054 
7055 	if (nl80211_put_regdom(regdom, msg))
7056 		goto nla_put_failure_rcu;
7057 
7058 	rcu_read_unlock();
7059 
7060 	genlmsg_end(msg, hdr);
7061 	return genlmsg_reply(msg, info);
7062 
7063 nla_put_failure_rcu:
7064 	rcu_read_unlock();
7065 nla_put_failure:
7066 put_failure:
7067 	nlmsg_free(msg);
7068 	return -EMSGSIZE;
7069 }
7070 
7071 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
7072 			       u32 seq, int flags, struct wiphy *wiphy,
7073 			       const struct ieee80211_regdomain *regdom)
7074 {
7075 	void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
7076 				   NL80211_CMD_GET_REG);
7077 
7078 	if (!hdr)
7079 		return -1;
7080 
7081 	genl_dump_check_consistent(cb, hdr);
7082 
7083 	if (nl80211_put_regdom(regdom, msg))
7084 		goto nla_put_failure;
7085 
7086 	if (!wiphy && reg_last_request_cell_base() &&
7087 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
7088 			NL80211_USER_REG_HINT_CELL_BASE))
7089 		goto nla_put_failure;
7090 
7091 	if (wiphy &&
7092 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7093 		goto nla_put_failure;
7094 
7095 	if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
7096 	    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
7097 		goto nla_put_failure;
7098 
7099 	genlmsg_end(msg, hdr);
7100 	return 0;
7101 
7102 nla_put_failure:
7103 	genlmsg_cancel(msg, hdr);
7104 	return -EMSGSIZE;
7105 }
7106 
7107 static int nl80211_get_reg_dump(struct sk_buff *skb,
7108 				struct netlink_callback *cb)
7109 {
7110 	const struct ieee80211_regdomain *regdom = NULL;
7111 	struct cfg80211_registered_device *rdev;
7112 	int err, reg_idx, start = cb->args[2];
7113 
7114 	rtnl_lock();
7115 
7116 	if (cfg80211_regdomain && start == 0) {
7117 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7118 					  NLM_F_MULTI, NULL,
7119 					  rtnl_dereference(cfg80211_regdomain));
7120 		if (err < 0)
7121 			goto out_err;
7122 	}
7123 
7124 	/* the global regdom is idx 0 */
7125 	reg_idx = 1;
7126 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
7127 		regdom = get_wiphy_regdom(&rdev->wiphy);
7128 		if (!regdom)
7129 			continue;
7130 
7131 		if (++reg_idx <= start)
7132 			continue;
7133 
7134 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7135 					  NLM_F_MULTI, &rdev->wiphy, regdom);
7136 		if (err < 0) {
7137 			reg_idx--;
7138 			break;
7139 		}
7140 	}
7141 
7142 	cb->args[2] = reg_idx;
7143 	err = skb->len;
7144 out_err:
7145 	rtnl_unlock();
7146 	return err;
7147 }
7148 
7149 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
7150 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
7151 	[NL80211_ATTR_REG_RULE_FLAGS]		= { .type = NLA_U32 },
7152 	[NL80211_ATTR_FREQ_RANGE_START]		= { .type = NLA_U32 },
7153 	[NL80211_ATTR_FREQ_RANGE_END]		= { .type = NLA_U32 },
7154 	[NL80211_ATTR_FREQ_RANGE_MAX_BW]	= { .type = NLA_U32 },
7155 	[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]	= { .type = NLA_U32 },
7156 	[NL80211_ATTR_POWER_RULE_MAX_EIRP]	= { .type = NLA_U32 },
7157 	[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
7158 };
7159 
7160 static int parse_reg_rule(struct nlattr *tb[],
7161 	struct ieee80211_reg_rule *reg_rule)
7162 {
7163 	struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
7164 	struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
7165 
7166 	if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
7167 		return -EINVAL;
7168 	if (!tb[NL80211_ATTR_FREQ_RANGE_START])
7169 		return -EINVAL;
7170 	if (!tb[NL80211_ATTR_FREQ_RANGE_END])
7171 		return -EINVAL;
7172 	if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
7173 		return -EINVAL;
7174 	if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
7175 		return -EINVAL;
7176 
7177 	reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
7178 
7179 	freq_range->start_freq_khz =
7180 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
7181 	freq_range->end_freq_khz =
7182 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
7183 	freq_range->max_bandwidth_khz =
7184 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
7185 
7186 	power_rule->max_eirp =
7187 		nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
7188 
7189 	if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
7190 		power_rule->max_antenna_gain =
7191 			nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
7192 
7193 	if (tb[NL80211_ATTR_DFS_CAC_TIME])
7194 		reg_rule->dfs_cac_ms =
7195 			nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
7196 
7197 	return 0;
7198 }
7199 
7200 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
7201 {
7202 	struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
7203 	struct nlattr *nl_reg_rule;
7204 	char *alpha2;
7205 	int rem_reg_rules, r;
7206 	u32 num_rules = 0, rule_idx = 0;
7207 	enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
7208 	struct ieee80211_regdomain *rd;
7209 
7210 	if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
7211 		return -EINVAL;
7212 
7213 	if (!info->attrs[NL80211_ATTR_REG_RULES])
7214 		return -EINVAL;
7215 
7216 	alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
7217 
7218 	if (info->attrs[NL80211_ATTR_DFS_REGION])
7219 		dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
7220 
7221 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7222 			    rem_reg_rules) {
7223 		num_rules++;
7224 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
7225 			return -EINVAL;
7226 	}
7227 
7228 	if (!reg_is_valid_request(alpha2))
7229 		return -EINVAL;
7230 
7231 	rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
7232 	if (!rd)
7233 		return -ENOMEM;
7234 
7235 	rd->n_reg_rules = num_rules;
7236 	rd->alpha2[0] = alpha2[0];
7237 	rd->alpha2[1] = alpha2[1];
7238 
7239 	/*
7240 	 * Disable DFS master mode if the DFS region was
7241 	 * not supported or known on this kernel.
7242 	 */
7243 	if (reg_supported_dfs_region(dfs_region))
7244 		rd->dfs_region = dfs_region;
7245 
7246 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7247 			    rem_reg_rules) {
7248 		r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
7249 						nl_reg_rule, reg_rule_policy,
7250 						info->extack);
7251 		if (r)
7252 			goto bad_reg;
7253 		r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
7254 		if (r)
7255 			goto bad_reg;
7256 
7257 		rule_idx++;
7258 
7259 		if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
7260 			r = -EINVAL;
7261 			goto bad_reg;
7262 		}
7263 	}
7264 
7265 	/* set_regdom takes ownership of rd */
7266 	return set_regdom(rd, REGD_SOURCE_CRDA);
7267  bad_reg:
7268 	kfree(rd);
7269 	return r;
7270 }
7271 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
7272 
7273 static int validate_scan_freqs(struct nlattr *freqs)
7274 {
7275 	struct nlattr *attr1, *attr2;
7276 	int n_channels = 0, tmp1, tmp2;
7277 
7278 	nla_for_each_nested(attr1, freqs, tmp1)
7279 		if (nla_len(attr1) != sizeof(u32))
7280 			return 0;
7281 
7282 	nla_for_each_nested(attr1, freqs, tmp1) {
7283 		n_channels++;
7284 		/*
7285 		 * Some hardware has a limited channel list for
7286 		 * scanning, and it is pretty much nonsensical
7287 		 * to scan for a channel twice, so disallow that
7288 		 * and don't require drivers to check that the
7289 		 * channel list they get isn't longer than what
7290 		 * they can scan, as long as they can scan all
7291 		 * the channels they registered at once.
7292 		 */
7293 		nla_for_each_nested(attr2, freqs, tmp2)
7294 			if (attr1 != attr2 &&
7295 			    nla_get_u32(attr1) == nla_get_u32(attr2))
7296 				return 0;
7297 	}
7298 
7299 	return n_channels;
7300 }
7301 
7302 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
7303 {
7304 	return b < NUM_NL80211_BANDS && wiphy->bands[b];
7305 }
7306 
7307 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
7308 			    struct cfg80211_bss_selection *bss_select)
7309 {
7310 	struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
7311 	struct nlattr *nest;
7312 	int err;
7313 	bool found = false;
7314 	int i;
7315 
7316 	/* only process one nested attribute */
7317 	nest = nla_data(nla);
7318 	if (!nla_ok(nest, nla_len(nest)))
7319 		return -EINVAL;
7320 
7321 	err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
7322 					  nest, nl80211_bss_select_policy,
7323 					  NULL);
7324 	if (err)
7325 		return err;
7326 
7327 	/* only one attribute may be given */
7328 	for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
7329 		if (attr[i]) {
7330 			if (found)
7331 				return -EINVAL;
7332 			found = true;
7333 		}
7334 	}
7335 
7336 	bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
7337 
7338 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
7339 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
7340 
7341 	if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
7342 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
7343 		bss_select->param.band_pref =
7344 			nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
7345 		if (!is_band_valid(wiphy, bss_select->param.band_pref))
7346 			return -EINVAL;
7347 	}
7348 
7349 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
7350 		struct nl80211_bss_select_rssi_adjust *adj_param;
7351 
7352 		adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
7353 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
7354 		bss_select->param.adjust.band = adj_param->band;
7355 		bss_select->param.adjust.delta = adj_param->delta;
7356 		if (!is_band_valid(wiphy, bss_select->param.adjust.band))
7357 			return -EINVAL;
7358 	}
7359 
7360 	/* user-space did not provide behaviour attribute */
7361 	if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
7362 		return -EINVAL;
7363 
7364 	if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
7365 		return -EINVAL;
7366 
7367 	return 0;
7368 }
7369 
7370 int nl80211_parse_random_mac(struct nlattr **attrs,
7371 			     u8 *mac_addr, u8 *mac_addr_mask)
7372 {
7373 	int i;
7374 
7375 	if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
7376 		eth_zero_addr(mac_addr);
7377 		eth_zero_addr(mac_addr_mask);
7378 		mac_addr[0] = 0x2;
7379 		mac_addr_mask[0] = 0x3;
7380 
7381 		return 0;
7382 	}
7383 
7384 	/* need both or none */
7385 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
7386 		return -EINVAL;
7387 
7388 	memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
7389 	memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
7390 
7391 	/* don't allow or configure an mcast address */
7392 	if (!is_multicast_ether_addr(mac_addr_mask) ||
7393 	    is_multicast_ether_addr(mac_addr))
7394 		return -EINVAL;
7395 
7396 	/*
7397 	 * allow users to pass a MAC address that has bits set outside
7398 	 * of the mask, but don't bother drivers with having to deal
7399 	 * with such bits
7400 	 */
7401 	for (i = 0; i < ETH_ALEN; i++)
7402 		mac_addr[i] &= mac_addr_mask[i];
7403 
7404 	return 0;
7405 }
7406 
7407 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev)
7408 {
7409 	ASSERT_WDEV_LOCK(wdev);
7410 
7411 	if (!cfg80211_beaconing_iface_active(wdev))
7412 		return true;
7413 
7414 	if (!(wdev->chandef.chan->flags & IEEE80211_CHAN_RADAR))
7415 		return true;
7416 
7417 	return regulatory_pre_cac_allowed(wdev->wiphy);
7418 }
7419 
7420 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
7421 				    enum nl80211_ext_feature_index feat)
7422 {
7423 	if (!(flags & flag))
7424 		return true;
7425 	if (wiphy_ext_feature_isset(wiphy, feat))
7426 		return true;
7427 	return false;
7428 }
7429 
7430 static int
7431 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
7432 			 void *request, struct nlattr **attrs,
7433 			 bool is_sched_scan)
7434 {
7435 	u8 *mac_addr, *mac_addr_mask;
7436 	u32 *flags;
7437 	enum nl80211_feature_flags randomness_flag;
7438 
7439 	if (!attrs[NL80211_ATTR_SCAN_FLAGS])
7440 		return 0;
7441 
7442 	if (is_sched_scan) {
7443 		struct cfg80211_sched_scan_request *req = request;
7444 
7445 		randomness_flag = wdev ?
7446 				  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
7447 				  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
7448 		flags = &req->flags;
7449 		mac_addr = req->mac_addr;
7450 		mac_addr_mask = req->mac_addr_mask;
7451 	} else {
7452 		struct cfg80211_scan_request *req = request;
7453 
7454 		randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
7455 		flags = &req->flags;
7456 		mac_addr = req->mac_addr;
7457 		mac_addr_mask = req->mac_addr_mask;
7458 	}
7459 
7460 	*flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
7461 
7462 	if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
7463 	     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
7464 	    !nl80211_check_scan_feat(wiphy, *flags,
7465 				     NL80211_SCAN_FLAG_LOW_SPAN,
7466 				     NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
7467 	    !nl80211_check_scan_feat(wiphy, *flags,
7468 				     NL80211_SCAN_FLAG_LOW_POWER,
7469 				     NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
7470 	    !nl80211_check_scan_feat(wiphy, *flags,
7471 				     NL80211_SCAN_FLAG_HIGH_ACCURACY,
7472 				     NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
7473 	    !nl80211_check_scan_feat(wiphy, *flags,
7474 				     NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
7475 				     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
7476 	    !nl80211_check_scan_feat(wiphy, *flags,
7477 				     NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
7478 				     NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
7479 	    !nl80211_check_scan_feat(wiphy, *flags,
7480 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
7481 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
7482 	    !nl80211_check_scan_feat(wiphy, *flags,
7483 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
7484 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
7485 	    !nl80211_check_scan_feat(wiphy, *flags,
7486 				     NL80211_SCAN_FLAG_RANDOM_SN,
7487 				     NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
7488 	    !nl80211_check_scan_feat(wiphy, *flags,
7489 				     NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
7490 				     NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
7491 		return -EOPNOTSUPP;
7492 
7493 	if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
7494 		int err;
7495 
7496 		if (!(wiphy->features & randomness_flag) ||
7497 		    (wdev && wdev->current_bss))
7498 			return -EOPNOTSUPP;
7499 
7500 		err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
7501 		if (err)
7502 			return err;
7503 	}
7504 
7505 	return 0;
7506 }
7507 
7508 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
7509 {
7510 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7511 	struct wireless_dev *wdev = info->user_ptr[1];
7512 	struct cfg80211_scan_request *request;
7513 	struct nlattr *attr;
7514 	struct wiphy *wiphy;
7515 	int err, tmp, n_ssids = 0, n_channels, i;
7516 	size_t ie_len;
7517 
7518 	wiphy = &rdev->wiphy;
7519 
7520 	if (wdev->iftype == NL80211_IFTYPE_NAN)
7521 		return -EOPNOTSUPP;
7522 
7523 	if (!rdev->ops->scan)
7524 		return -EOPNOTSUPP;
7525 
7526 	if (rdev->scan_req || rdev->scan_msg) {
7527 		err = -EBUSY;
7528 		goto unlock;
7529 	}
7530 
7531 	if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7532 		n_channels = validate_scan_freqs(
7533 				info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
7534 		if (!n_channels) {
7535 			err = -EINVAL;
7536 			goto unlock;
7537 		}
7538 	} else {
7539 		n_channels = ieee80211_get_num_supported_channels(wiphy);
7540 	}
7541 
7542 	if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
7543 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
7544 			n_ssids++;
7545 
7546 	if (n_ssids > wiphy->max_scan_ssids) {
7547 		err = -EINVAL;
7548 		goto unlock;
7549 	}
7550 
7551 	if (info->attrs[NL80211_ATTR_IE])
7552 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7553 	else
7554 		ie_len = 0;
7555 
7556 	if (ie_len > wiphy->max_scan_ie_len) {
7557 		err = -EINVAL;
7558 		goto unlock;
7559 	}
7560 
7561 	request = kzalloc(sizeof(*request)
7562 			+ sizeof(*request->ssids) * n_ssids
7563 			+ sizeof(*request->channels) * n_channels
7564 			+ ie_len, GFP_KERNEL);
7565 	if (!request) {
7566 		err = -ENOMEM;
7567 		goto unlock;
7568 	}
7569 
7570 	if (n_ssids)
7571 		request->ssids = (void *)&request->channels[n_channels];
7572 	request->n_ssids = n_ssids;
7573 	if (ie_len) {
7574 		if (n_ssids)
7575 			request->ie = (void *)(request->ssids + n_ssids);
7576 		else
7577 			request->ie = (void *)(request->channels + n_channels);
7578 	}
7579 
7580 	i = 0;
7581 	if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7582 		/* user specified, bail out if channel not found */
7583 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
7584 			struct ieee80211_channel *chan;
7585 
7586 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
7587 
7588 			if (!chan) {
7589 				err = -EINVAL;
7590 				goto out_free;
7591 			}
7592 
7593 			/* ignore disabled channels */
7594 			if (chan->flags & IEEE80211_CHAN_DISABLED)
7595 				continue;
7596 
7597 			request->channels[i] = chan;
7598 			i++;
7599 		}
7600 	} else {
7601 		enum nl80211_band band;
7602 
7603 		/* all channels */
7604 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
7605 			int j;
7606 
7607 			if (!wiphy->bands[band])
7608 				continue;
7609 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
7610 				struct ieee80211_channel *chan;
7611 
7612 				chan = &wiphy->bands[band]->channels[j];
7613 
7614 				if (chan->flags & IEEE80211_CHAN_DISABLED)
7615 					continue;
7616 
7617 				request->channels[i] = chan;
7618 				i++;
7619 			}
7620 		}
7621 	}
7622 
7623 	if (!i) {
7624 		err = -EINVAL;
7625 		goto out_free;
7626 	}
7627 
7628 	request->n_channels = i;
7629 
7630 	wdev_lock(wdev);
7631 	if (!cfg80211_off_channel_oper_allowed(wdev)) {
7632 		struct ieee80211_channel *chan;
7633 
7634 		if (request->n_channels != 1) {
7635 			wdev_unlock(wdev);
7636 			err = -EBUSY;
7637 			goto out_free;
7638 		}
7639 
7640 		chan = request->channels[0];
7641 		if (chan->center_freq != wdev->chandef.chan->center_freq) {
7642 			wdev_unlock(wdev);
7643 			err = -EBUSY;
7644 			goto out_free;
7645 		}
7646 	}
7647 	wdev_unlock(wdev);
7648 
7649 	i = 0;
7650 	if (n_ssids) {
7651 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
7652 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
7653 				err = -EINVAL;
7654 				goto out_free;
7655 			}
7656 			request->ssids[i].ssid_len = nla_len(attr);
7657 			memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
7658 			i++;
7659 		}
7660 	}
7661 
7662 	if (info->attrs[NL80211_ATTR_IE]) {
7663 		request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7664 		memcpy((void *)request->ie,
7665 		       nla_data(info->attrs[NL80211_ATTR_IE]),
7666 		       request->ie_len);
7667 	}
7668 
7669 	for (i = 0; i < NUM_NL80211_BANDS; i++)
7670 		if (wiphy->bands[i])
7671 			request->rates[i] =
7672 				(1 << wiphy->bands[i]->n_bitrates) - 1;
7673 
7674 	if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
7675 		nla_for_each_nested(attr,
7676 				    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
7677 				    tmp) {
7678 			enum nl80211_band band = nla_type(attr);
7679 
7680 			if (band < 0 || band >= NUM_NL80211_BANDS) {
7681 				err = -EINVAL;
7682 				goto out_free;
7683 			}
7684 
7685 			if (!wiphy->bands[band])
7686 				continue;
7687 
7688 			err = ieee80211_get_ratemask(wiphy->bands[band],
7689 						     nla_data(attr),
7690 						     nla_len(attr),
7691 						     &request->rates[band]);
7692 			if (err)
7693 				goto out_free;
7694 		}
7695 	}
7696 
7697 	if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
7698 		if (!wiphy_ext_feature_isset(wiphy,
7699 					NL80211_EXT_FEATURE_SET_SCAN_DWELL)) {
7700 			err = -EOPNOTSUPP;
7701 			goto out_free;
7702 		}
7703 
7704 		request->duration =
7705 			nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
7706 		request->duration_mandatory =
7707 			nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
7708 	}
7709 
7710 	err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
7711 				       false);
7712 	if (err)
7713 		goto out_free;
7714 
7715 	request->no_cck =
7716 		nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7717 
7718 	/* Initial implementation used NL80211_ATTR_MAC to set the specific
7719 	 * BSSID to scan for. This was problematic because that same attribute
7720 	 * was already used for another purpose (local random MAC address). The
7721 	 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
7722 	 * compatibility with older userspace components, also use the
7723 	 * NL80211_ATTR_MAC value here if it can be determined to be used for
7724 	 * the specific BSSID use case instead of the random MAC address
7725 	 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
7726 	 */
7727 	if (info->attrs[NL80211_ATTR_BSSID])
7728 		memcpy(request->bssid,
7729 		       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
7730 	else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
7731 		 info->attrs[NL80211_ATTR_MAC])
7732 		memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
7733 		       ETH_ALEN);
7734 	else
7735 		eth_broadcast_addr(request->bssid);
7736 
7737 	request->wdev = wdev;
7738 	request->wiphy = &rdev->wiphy;
7739 	request->scan_start = jiffies;
7740 
7741 	rdev->scan_req = request;
7742 	err = rdev_scan(rdev, request);
7743 
7744 	if (!err) {
7745 		nl80211_send_scan_start(rdev, wdev);
7746 		if (wdev->netdev)
7747 			dev_hold(wdev->netdev);
7748 	} else {
7749  out_free:
7750 		rdev->scan_req = NULL;
7751 		kfree(request);
7752 	}
7753 
7754  unlock:
7755 	return err;
7756 }
7757 
7758 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
7759 {
7760 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7761 	struct wireless_dev *wdev = info->user_ptr[1];
7762 
7763 	if (!rdev->ops->abort_scan)
7764 		return -EOPNOTSUPP;
7765 
7766 	if (rdev->scan_msg)
7767 		return 0;
7768 
7769 	if (!rdev->scan_req)
7770 		return -ENOENT;
7771 
7772 	rdev_abort_scan(rdev, wdev);
7773 	return 0;
7774 }
7775 
7776 static int
7777 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
7778 			       struct cfg80211_sched_scan_request *request,
7779 			       struct nlattr **attrs)
7780 {
7781 	int tmp, err, i = 0;
7782 	struct nlattr *attr;
7783 
7784 	if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7785 		u32 interval;
7786 
7787 		/*
7788 		 * If scan plans are not specified,
7789 		 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
7790 		 * case one scan plan will be set with the specified scan
7791 		 * interval and infinite number of iterations.
7792 		 */
7793 		interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
7794 		if (!interval)
7795 			return -EINVAL;
7796 
7797 		request->scan_plans[0].interval =
7798 			DIV_ROUND_UP(interval, MSEC_PER_SEC);
7799 		if (!request->scan_plans[0].interval)
7800 			return -EINVAL;
7801 
7802 		if (request->scan_plans[0].interval >
7803 		    wiphy->max_sched_scan_plan_interval)
7804 			request->scan_plans[0].interval =
7805 				wiphy->max_sched_scan_plan_interval;
7806 
7807 		return 0;
7808 	}
7809 
7810 	nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
7811 		struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
7812 
7813 		if (WARN_ON(i >= n_plans))
7814 			return -EINVAL;
7815 
7816 		err = nla_parse_nested_deprecated(plan,
7817 						  NL80211_SCHED_SCAN_PLAN_MAX,
7818 						  attr, nl80211_plan_policy,
7819 						  NULL);
7820 		if (err)
7821 			return err;
7822 
7823 		if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
7824 			return -EINVAL;
7825 
7826 		request->scan_plans[i].interval =
7827 			nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
7828 		if (!request->scan_plans[i].interval ||
7829 		    request->scan_plans[i].interval >
7830 		    wiphy->max_sched_scan_plan_interval)
7831 			return -EINVAL;
7832 
7833 		if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
7834 			request->scan_plans[i].iterations =
7835 				nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
7836 			if (!request->scan_plans[i].iterations ||
7837 			    (request->scan_plans[i].iterations >
7838 			     wiphy->max_sched_scan_plan_iterations))
7839 				return -EINVAL;
7840 		} else if (i < n_plans - 1) {
7841 			/*
7842 			 * All scan plans but the last one must specify
7843 			 * a finite number of iterations
7844 			 */
7845 			return -EINVAL;
7846 		}
7847 
7848 		i++;
7849 	}
7850 
7851 	/*
7852 	 * The last scan plan must not specify the number of
7853 	 * iterations, it is supposed to run infinitely
7854 	 */
7855 	if (request->scan_plans[n_plans - 1].iterations)
7856 		return  -EINVAL;
7857 
7858 	return 0;
7859 }
7860 
7861 static int
7862 nl80211_parse_sched_scan_per_band_rssi(struct wiphy *wiphy,
7863 				       struct cfg80211_match_set *match_sets,
7864 				       struct nlattr *tb_band_rssi,
7865 				       s32 rssi_thold)
7866 {
7867 	struct nlattr *attr;
7868 	int i, tmp, ret = 0;
7869 
7870 	if (!wiphy_ext_feature_isset(wiphy,
7871 		    NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD)) {
7872 		if (tb_band_rssi)
7873 			ret = -EOPNOTSUPP;
7874 		else
7875 			for (i = 0; i < NUM_NL80211_BANDS; i++)
7876 				match_sets->per_band_rssi_thold[i] =
7877 					NL80211_SCAN_RSSI_THOLD_OFF;
7878 		return ret;
7879 	}
7880 
7881 	for (i = 0; i < NUM_NL80211_BANDS; i++)
7882 		match_sets->per_band_rssi_thold[i] = rssi_thold;
7883 
7884 	nla_for_each_nested(attr, tb_band_rssi, tmp) {
7885 		enum nl80211_band band = nla_type(attr);
7886 
7887 		if (band < 0 || band >= NUM_NL80211_BANDS)
7888 			return -EINVAL;
7889 
7890 		match_sets->per_band_rssi_thold[band] =	nla_get_s32(attr);
7891 	}
7892 
7893 	return 0;
7894 }
7895 
7896 static struct cfg80211_sched_scan_request *
7897 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
7898 			 struct nlattr **attrs, int max_match_sets)
7899 {
7900 	struct cfg80211_sched_scan_request *request;
7901 	struct nlattr *attr;
7902 	int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
7903 	enum nl80211_band band;
7904 	size_t ie_len;
7905 	struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
7906 	s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
7907 
7908 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7909 		n_channels = validate_scan_freqs(
7910 				attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
7911 		if (!n_channels)
7912 			return ERR_PTR(-EINVAL);
7913 	} else {
7914 		n_channels = ieee80211_get_num_supported_channels(wiphy);
7915 	}
7916 
7917 	if (attrs[NL80211_ATTR_SCAN_SSIDS])
7918 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
7919 				    tmp)
7920 			n_ssids++;
7921 
7922 	if (n_ssids > wiphy->max_sched_scan_ssids)
7923 		return ERR_PTR(-EINVAL);
7924 
7925 	/*
7926 	 * First, count the number of 'real' matchsets. Due to an issue with
7927 	 * the old implementation, matchsets containing only the RSSI attribute
7928 	 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
7929 	 * RSSI for all matchsets, rather than their own matchset for reporting
7930 	 * all APs with a strong RSSI. This is needed to be compatible with
7931 	 * older userspace that treated a matchset with only the RSSI as the
7932 	 * global RSSI for all other matchsets - if there are other matchsets.
7933 	 */
7934 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
7935 		nla_for_each_nested(attr,
7936 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
7937 				    tmp) {
7938 			struct nlattr *rssi;
7939 
7940 			err = nla_parse_nested_deprecated(tb,
7941 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
7942 							  attr,
7943 							  nl80211_match_policy,
7944 							  NULL);
7945 			if (err)
7946 				return ERR_PTR(err);
7947 
7948 			/* SSID and BSSID are mutually exclusive */
7949 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
7950 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
7951 				return ERR_PTR(-EINVAL);
7952 
7953 			/* add other standalone attributes here */
7954 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
7955 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
7956 				n_match_sets++;
7957 				continue;
7958 			}
7959 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
7960 			if (rssi)
7961 				default_match_rssi = nla_get_s32(rssi);
7962 		}
7963 	}
7964 
7965 	/* However, if there's no other matchset, add the RSSI one */
7966 	if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
7967 		n_match_sets = 1;
7968 
7969 	if (n_match_sets > max_match_sets)
7970 		return ERR_PTR(-EINVAL);
7971 
7972 	if (attrs[NL80211_ATTR_IE])
7973 		ie_len = nla_len(attrs[NL80211_ATTR_IE]);
7974 	else
7975 		ie_len = 0;
7976 
7977 	if (ie_len > wiphy->max_sched_scan_ie_len)
7978 		return ERR_PTR(-EINVAL);
7979 
7980 	if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7981 		/*
7982 		 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
7983 		 * each scan plan already specifies its own interval
7984 		 */
7985 		if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7986 			return ERR_PTR(-EINVAL);
7987 
7988 		nla_for_each_nested(attr,
7989 				    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
7990 			n_plans++;
7991 	} else {
7992 		/*
7993 		 * The scan interval attribute is kept for backward
7994 		 * compatibility. If no scan plans are specified and sched scan
7995 		 * interval is specified, one scan plan will be set with this
7996 		 * scan interval and infinite number of iterations.
7997 		 */
7998 		if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7999 			return ERR_PTR(-EINVAL);
8000 
8001 		n_plans = 1;
8002 	}
8003 
8004 	if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
8005 		return ERR_PTR(-EINVAL);
8006 
8007 	if (!wiphy_ext_feature_isset(
8008 		    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
8009 	    (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
8010 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
8011 		return ERR_PTR(-EINVAL);
8012 
8013 	request = kzalloc(sizeof(*request)
8014 			+ sizeof(*request->ssids) * n_ssids
8015 			+ sizeof(*request->match_sets) * n_match_sets
8016 			+ sizeof(*request->scan_plans) * n_plans
8017 			+ sizeof(*request->channels) * n_channels
8018 			+ ie_len, GFP_KERNEL);
8019 	if (!request)
8020 		return ERR_PTR(-ENOMEM);
8021 
8022 	if (n_ssids)
8023 		request->ssids = (void *)&request->channels[n_channels];
8024 	request->n_ssids = n_ssids;
8025 	if (ie_len) {
8026 		if (n_ssids)
8027 			request->ie = (void *)(request->ssids + n_ssids);
8028 		else
8029 			request->ie = (void *)(request->channels + n_channels);
8030 	}
8031 
8032 	if (n_match_sets) {
8033 		if (request->ie)
8034 			request->match_sets = (void *)(request->ie + ie_len);
8035 		else if (n_ssids)
8036 			request->match_sets =
8037 				(void *)(request->ssids + n_ssids);
8038 		else
8039 			request->match_sets =
8040 				(void *)(request->channels + n_channels);
8041 	}
8042 	request->n_match_sets = n_match_sets;
8043 
8044 	if (n_match_sets)
8045 		request->scan_plans = (void *)(request->match_sets +
8046 					       n_match_sets);
8047 	else if (request->ie)
8048 		request->scan_plans = (void *)(request->ie + ie_len);
8049 	else if (n_ssids)
8050 		request->scan_plans = (void *)(request->ssids + n_ssids);
8051 	else
8052 		request->scan_plans = (void *)(request->channels + n_channels);
8053 
8054 	request->n_scan_plans = n_plans;
8055 
8056 	i = 0;
8057 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
8058 		/* user specified, bail out if channel not found */
8059 		nla_for_each_nested(attr,
8060 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
8061 				    tmp) {
8062 			struct ieee80211_channel *chan;
8063 
8064 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
8065 
8066 			if (!chan) {
8067 				err = -EINVAL;
8068 				goto out_free;
8069 			}
8070 
8071 			/* ignore disabled channels */
8072 			if (chan->flags & IEEE80211_CHAN_DISABLED)
8073 				continue;
8074 
8075 			request->channels[i] = chan;
8076 			i++;
8077 		}
8078 	} else {
8079 		/* all channels */
8080 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
8081 			int j;
8082 
8083 			if (!wiphy->bands[band])
8084 				continue;
8085 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
8086 				struct ieee80211_channel *chan;
8087 
8088 				chan = &wiphy->bands[band]->channels[j];
8089 
8090 				if (chan->flags & IEEE80211_CHAN_DISABLED)
8091 					continue;
8092 
8093 				request->channels[i] = chan;
8094 				i++;
8095 			}
8096 		}
8097 	}
8098 
8099 	if (!i) {
8100 		err = -EINVAL;
8101 		goto out_free;
8102 	}
8103 
8104 	request->n_channels = i;
8105 
8106 	i = 0;
8107 	if (n_ssids) {
8108 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
8109 				    tmp) {
8110 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
8111 				err = -EINVAL;
8112 				goto out_free;
8113 			}
8114 			request->ssids[i].ssid_len = nla_len(attr);
8115 			memcpy(request->ssids[i].ssid, nla_data(attr),
8116 			       nla_len(attr));
8117 			i++;
8118 		}
8119 	}
8120 
8121 	i = 0;
8122 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
8123 		nla_for_each_nested(attr,
8124 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
8125 				    tmp) {
8126 			struct nlattr *ssid, *bssid, *rssi;
8127 
8128 			err = nla_parse_nested_deprecated(tb,
8129 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
8130 							  attr,
8131 							  nl80211_match_policy,
8132 							  NULL);
8133 			if (err)
8134 				goto out_free;
8135 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
8136 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
8137 
8138 			if (!ssid && !bssid) {
8139 				i++;
8140 				continue;
8141 			}
8142 
8143 			if (WARN_ON(i >= n_match_sets)) {
8144 				/* this indicates a programming error,
8145 				 * the loop above should have verified
8146 				 * things properly
8147 				 */
8148 				err = -EINVAL;
8149 				goto out_free;
8150 			}
8151 
8152 			if (ssid) {
8153 				if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
8154 					err = -EINVAL;
8155 					goto out_free;
8156 				}
8157 				memcpy(request->match_sets[i].ssid.ssid,
8158 				       nla_data(ssid), nla_len(ssid));
8159 				request->match_sets[i].ssid.ssid_len =
8160 					nla_len(ssid);
8161 			}
8162 			if (bssid) {
8163 				if (nla_len(bssid) != ETH_ALEN) {
8164 					err = -EINVAL;
8165 					goto out_free;
8166 				}
8167 				memcpy(request->match_sets[i].bssid,
8168 				       nla_data(bssid), ETH_ALEN);
8169 			}
8170 
8171 			/* special attribute - old implementation w/a */
8172 			request->match_sets[i].rssi_thold = default_match_rssi;
8173 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
8174 			if (rssi)
8175 				request->match_sets[i].rssi_thold =
8176 					nla_get_s32(rssi);
8177 
8178 			/* Parse per band RSSI attribute */
8179 			err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
8180 				&request->match_sets[i],
8181 				tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
8182 				request->match_sets[i].rssi_thold);
8183 			if (err)
8184 				goto out_free;
8185 
8186 			i++;
8187 		}
8188 
8189 		/* there was no other matchset, so the RSSI one is alone */
8190 		if (i == 0 && n_match_sets)
8191 			request->match_sets[0].rssi_thold = default_match_rssi;
8192 
8193 		request->min_rssi_thold = INT_MAX;
8194 		for (i = 0; i < n_match_sets; i++)
8195 			request->min_rssi_thold =
8196 				min(request->match_sets[i].rssi_thold,
8197 				    request->min_rssi_thold);
8198 	} else {
8199 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
8200 	}
8201 
8202 	if (ie_len) {
8203 		request->ie_len = ie_len;
8204 		memcpy((void *)request->ie,
8205 		       nla_data(attrs[NL80211_ATTR_IE]),
8206 		       request->ie_len);
8207 	}
8208 
8209 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
8210 	if (err)
8211 		goto out_free;
8212 
8213 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
8214 		request->delay =
8215 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
8216 
8217 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
8218 		request->relative_rssi = nla_get_s8(
8219 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
8220 		request->relative_rssi_set = true;
8221 	}
8222 
8223 	if (request->relative_rssi_set &&
8224 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
8225 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
8226 
8227 		rssi_adjust = nla_data(
8228 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
8229 		request->rssi_adjust.band = rssi_adjust->band;
8230 		request->rssi_adjust.delta = rssi_adjust->delta;
8231 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
8232 			err = -EINVAL;
8233 			goto out_free;
8234 		}
8235 	}
8236 
8237 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
8238 	if (err)
8239 		goto out_free;
8240 
8241 	request->scan_start = jiffies;
8242 
8243 	return request;
8244 
8245 out_free:
8246 	kfree(request);
8247 	return ERR_PTR(err);
8248 }
8249 
8250 static int nl80211_start_sched_scan(struct sk_buff *skb,
8251 				    struct genl_info *info)
8252 {
8253 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8254 	struct net_device *dev = info->user_ptr[1];
8255 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8256 	struct cfg80211_sched_scan_request *sched_scan_req;
8257 	bool want_multi;
8258 	int err;
8259 
8260 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
8261 		return -EOPNOTSUPP;
8262 
8263 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
8264 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
8265 	if (err)
8266 		return err;
8267 
8268 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
8269 						  info->attrs,
8270 						  rdev->wiphy.max_match_sets);
8271 
8272 	err = PTR_ERR_OR_ZERO(sched_scan_req);
8273 	if (err)
8274 		goto out_err;
8275 
8276 	/* leave request id zero for legacy request
8277 	 * or if driver does not support multi-scheduled scan
8278 	 */
8279 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1)
8280 		sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
8281 
8282 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
8283 	if (err)
8284 		goto out_free;
8285 
8286 	sched_scan_req->dev = dev;
8287 	sched_scan_req->wiphy = &rdev->wiphy;
8288 
8289 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
8290 		sched_scan_req->owner_nlportid = info->snd_portid;
8291 
8292 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
8293 
8294 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
8295 	return 0;
8296 
8297 out_free:
8298 	kfree(sched_scan_req);
8299 out_err:
8300 	return err;
8301 }
8302 
8303 static int nl80211_stop_sched_scan(struct sk_buff *skb,
8304 				   struct genl_info *info)
8305 {
8306 	struct cfg80211_sched_scan_request *req;
8307 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8308 	u64 cookie;
8309 
8310 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
8311 		return -EOPNOTSUPP;
8312 
8313 	if (info->attrs[NL80211_ATTR_COOKIE]) {
8314 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
8315 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
8316 	}
8317 
8318 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
8319 				     struct cfg80211_sched_scan_request,
8320 				     list);
8321 	if (!req || req->reqid ||
8322 	    (req->owner_nlportid &&
8323 	     req->owner_nlportid != info->snd_portid))
8324 		return -ENOENT;
8325 
8326 	return cfg80211_stop_sched_scan_req(rdev, req, false);
8327 }
8328 
8329 static int nl80211_start_radar_detection(struct sk_buff *skb,
8330 					 struct genl_info *info)
8331 {
8332 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8333 	struct net_device *dev = info->user_ptr[1];
8334 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8335 	struct wiphy *wiphy = wdev->wiphy;
8336 	struct cfg80211_chan_def chandef;
8337 	enum nl80211_dfs_regions dfs_region;
8338 	unsigned int cac_time_ms;
8339 	int err;
8340 
8341 	dfs_region = reg_get_dfs_region(wiphy);
8342 	if (dfs_region == NL80211_DFS_UNSET)
8343 		return -EINVAL;
8344 
8345 	err = nl80211_parse_chandef(rdev, info, &chandef);
8346 	if (err)
8347 		return err;
8348 
8349 	if (netif_carrier_ok(dev))
8350 		return -EBUSY;
8351 
8352 	if (wdev->cac_started)
8353 		return -EBUSY;
8354 
8355 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
8356 	if (err < 0)
8357 		return err;
8358 
8359 	if (err == 0)
8360 		return -EINVAL;
8361 
8362 	if (!cfg80211_chandef_dfs_usable(wiphy, &chandef))
8363 		return -EINVAL;
8364 
8365 	/* CAC start is offloaded to HW and can't be started manually */
8366 	if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD))
8367 		return -EOPNOTSUPP;
8368 
8369 	if (!rdev->ops->start_radar_detection)
8370 		return -EOPNOTSUPP;
8371 
8372 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
8373 	if (WARN_ON(!cac_time_ms))
8374 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
8375 
8376 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
8377 	if (!err) {
8378 		wdev->chandef = chandef;
8379 		wdev->cac_started = true;
8380 		wdev->cac_start_time = jiffies;
8381 		wdev->cac_time_ms = cac_time_ms;
8382 	}
8383 	return err;
8384 }
8385 
8386 static int nl80211_notify_radar_detection(struct sk_buff *skb,
8387 					  struct genl_info *info)
8388 {
8389 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8390 	struct net_device *dev = info->user_ptr[1];
8391 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8392 	struct wiphy *wiphy = wdev->wiphy;
8393 	struct cfg80211_chan_def chandef;
8394 	enum nl80211_dfs_regions dfs_region;
8395 	int err;
8396 
8397 	dfs_region = reg_get_dfs_region(wiphy);
8398 	if (dfs_region == NL80211_DFS_UNSET) {
8399 		GENL_SET_ERR_MSG(info,
8400 				 "DFS Region is not set. Unexpected Radar indication");
8401 		return -EINVAL;
8402 	}
8403 
8404 	err = nl80211_parse_chandef(rdev, info, &chandef);
8405 	if (err) {
8406 		GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
8407 		return err;
8408 	}
8409 
8410 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
8411 	if (err < 0) {
8412 		GENL_SET_ERR_MSG(info, "chandef is invalid");
8413 		return err;
8414 	}
8415 
8416 	if (err == 0) {
8417 		GENL_SET_ERR_MSG(info,
8418 				 "Unexpected Radar indication for chandef/iftype");
8419 		return -EINVAL;
8420 	}
8421 
8422 	/* Do not process this notification if radar is already detected
8423 	 * by kernel on this channel, and return success.
8424 	 */
8425 	if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
8426 		return 0;
8427 
8428 	cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
8429 
8430 	cfg80211_sched_dfs_chan_update(rdev);
8431 
8432 	rdev->radar_chandef = chandef;
8433 
8434 	/* Propagate this notification to other radios as well */
8435 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
8436 
8437 	return 0;
8438 }
8439 
8440 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
8441 {
8442 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8443 	struct net_device *dev = info->user_ptr[1];
8444 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8445 	struct cfg80211_csa_settings params;
8446 	/* csa_attrs is defined static to avoid waste of stack size - this
8447 	 * function is called under RTNL lock, so this should not be a problem.
8448 	 */
8449 	static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
8450 	int err;
8451 	bool need_new_beacon = false;
8452 	bool need_handle_dfs_flag = true;
8453 	int len, i;
8454 	u32 cs_count;
8455 
8456 	if (!rdev->ops->channel_switch ||
8457 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
8458 		return -EOPNOTSUPP;
8459 
8460 	switch (dev->ieee80211_ptr->iftype) {
8461 	case NL80211_IFTYPE_AP:
8462 	case NL80211_IFTYPE_P2P_GO:
8463 		need_new_beacon = true;
8464 		/* For all modes except AP the handle_dfs flag needs to be
8465 		 * supplied to tell the kernel that userspace will handle radar
8466 		 * events when they happen. Otherwise a switch to a channel
8467 		 * requiring DFS will be rejected.
8468 		 */
8469 		need_handle_dfs_flag = false;
8470 
8471 		/* useless if AP is not running */
8472 		if (!wdev->beacon_interval)
8473 			return -ENOTCONN;
8474 		break;
8475 	case NL80211_IFTYPE_ADHOC:
8476 		if (!wdev->ssid_len)
8477 			return -ENOTCONN;
8478 		break;
8479 	case NL80211_IFTYPE_MESH_POINT:
8480 		if (!wdev->mesh_id_len)
8481 			return -ENOTCONN;
8482 		break;
8483 	default:
8484 		return -EOPNOTSUPP;
8485 	}
8486 
8487 	memset(&params, 0, sizeof(params));
8488 	params.beacon_csa.ftm_responder = -1;
8489 
8490 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
8491 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
8492 		return -EINVAL;
8493 
8494 	/* only important for AP, IBSS and mesh create IEs internally */
8495 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
8496 		return -EINVAL;
8497 
8498 	/* Even though the attribute is u32, the specification says
8499 	 * u8, so let's make sure we don't overflow.
8500 	 */
8501 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
8502 	if (cs_count > 255)
8503 		return -EINVAL;
8504 
8505 	params.count = cs_count;
8506 
8507 	if (!need_new_beacon)
8508 		goto skip_beacons;
8509 
8510 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after);
8511 	if (err)
8512 		return err;
8513 
8514 	err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
8515 					  info->attrs[NL80211_ATTR_CSA_IES],
8516 					  nl80211_policy, info->extack);
8517 	if (err)
8518 		return err;
8519 
8520 	err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa);
8521 	if (err)
8522 		return err;
8523 
8524 	if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
8525 		return -EINVAL;
8526 
8527 	len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
8528 	if (!len || (len % sizeof(u16)))
8529 		return -EINVAL;
8530 
8531 	params.n_counter_offsets_beacon = len / sizeof(u16);
8532 	if (rdev->wiphy.max_num_csa_counters &&
8533 	    (params.n_counter_offsets_beacon >
8534 	     rdev->wiphy.max_num_csa_counters))
8535 		return -EINVAL;
8536 
8537 	params.counter_offsets_beacon =
8538 		nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
8539 
8540 	/* sanity checks - counters should fit and be the same */
8541 	for (i = 0; i < params.n_counter_offsets_beacon; i++) {
8542 		u16 offset = params.counter_offsets_beacon[i];
8543 
8544 		if (offset >= params.beacon_csa.tail_len)
8545 			return -EINVAL;
8546 
8547 		if (params.beacon_csa.tail[offset] != params.count)
8548 			return -EINVAL;
8549 	}
8550 
8551 	if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
8552 		len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
8553 		if (!len || (len % sizeof(u16)))
8554 			return -EINVAL;
8555 
8556 		params.n_counter_offsets_presp = len / sizeof(u16);
8557 		if (rdev->wiphy.max_num_csa_counters &&
8558 		    (params.n_counter_offsets_presp >
8559 		     rdev->wiphy.max_num_csa_counters))
8560 			return -EINVAL;
8561 
8562 		params.counter_offsets_presp =
8563 			nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
8564 
8565 		/* sanity checks - counters should fit and be the same */
8566 		for (i = 0; i < params.n_counter_offsets_presp; i++) {
8567 			u16 offset = params.counter_offsets_presp[i];
8568 
8569 			if (offset >= params.beacon_csa.probe_resp_len)
8570 				return -EINVAL;
8571 
8572 			if (params.beacon_csa.probe_resp[offset] !=
8573 			    params.count)
8574 				return -EINVAL;
8575 		}
8576 	}
8577 
8578 skip_beacons:
8579 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
8580 	if (err)
8581 		return err;
8582 
8583 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
8584 					   wdev->iftype))
8585 		return -EINVAL;
8586 
8587 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
8588 					    &params.chandef,
8589 					    wdev->iftype);
8590 	if (err < 0)
8591 		return err;
8592 
8593 	if (err > 0) {
8594 		params.radar_required = true;
8595 		if (need_handle_dfs_flag &&
8596 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
8597 			return -EINVAL;
8598 		}
8599 	}
8600 
8601 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
8602 		params.block_tx = true;
8603 
8604 	wdev_lock(wdev);
8605 	err = rdev_channel_switch(rdev, dev, &params);
8606 	wdev_unlock(wdev);
8607 
8608 	return err;
8609 }
8610 
8611 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
8612 			    u32 seq, int flags,
8613 			    struct cfg80211_registered_device *rdev,
8614 			    struct wireless_dev *wdev,
8615 			    struct cfg80211_internal_bss *intbss)
8616 {
8617 	struct cfg80211_bss *res = &intbss->pub;
8618 	const struct cfg80211_bss_ies *ies;
8619 	void *hdr;
8620 	struct nlattr *bss;
8621 
8622 	ASSERT_WDEV_LOCK(wdev);
8623 
8624 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
8625 			     NL80211_CMD_NEW_SCAN_RESULTS);
8626 	if (!hdr)
8627 		return -1;
8628 
8629 	genl_dump_check_consistent(cb, hdr);
8630 
8631 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
8632 		goto nla_put_failure;
8633 	if (wdev->netdev &&
8634 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
8635 		goto nla_put_failure;
8636 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
8637 			      NL80211_ATTR_PAD))
8638 		goto nla_put_failure;
8639 
8640 	bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
8641 	if (!bss)
8642 		goto nla_put_failure;
8643 	if ((!is_zero_ether_addr(res->bssid) &&
8644 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
8645 		goto nla_put_failure;
8646 
8647 	rcu_read_lock();
8648 	/* indicate whether we have probe response data or not */
8649 	if (rcu_access_pointer(res->proberesp_ies) &&
8650 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
8651 		goto fail_unlock_rcu;
8652 
8653 	/* this pointer prefers to be pointed to probe response data
8654 	 * but is always valid
8655 	 */
8656 	ies = rcu_dereference(res->ies);
8657 	if (ies) {
8658 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
8659 				      NL80211_BSS_PAD))
8660 			goto fail_unlock_rcu;
8661 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
8662 					ies->len, ies->data))
8663 			goto fail_unlock_rcu;
8664 	}
8665 
8666 	/* and this pointer is always (unless driver didn't know) beacon data */
8667 	ies = rcu_dereference(res->beacon_ies);
8668 	if (ies && ies->from_beacon) {
8669 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
8670 				      NL80211_BSS_PAD))
8671 			goto fail_unlock_rcu;
8672 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
8673 					ies->len, ies->data))
8674 			goto fail_unlock_rcu;
8675 	}
8676 	rcu_read_unlock();
8677 
8678 	if (res->beacon_interval &&
8679 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
8680 		goto nla_put_failure;
8681 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
8682 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
8683 	    nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
8684 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
8685 			jiffies_to_msecs(jiffies - intbss->ts)))
8686 		goto nla_put_failure;
8687 
8688 	if (intbss->parent_tsf &&
8689 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
8690 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
8691 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
8692 		     intbss->parent_bssid)))
8693 		goto nla_put_failure;
8694 
8695 	if (intbss->ts_boottime &&
8696 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
8697 			      intbss->ts_boottime, NL80211_BSS_PAD))
8698 		goto nla_put_failure;
8699 
8700 	if (!nl80211_put_signal(msg, intbss->pub.chains,
8701 				intbss->pub.chain_signal,
8702 				NL80211_BSS_CHAIN_SIGNAL))
8703 		goto nla_put_failure;
8704 
8705 	switch (rdev->wiphy.signal_type) {
8706 	case CFG80211_SIGNAL_TYPE_MBM:
8707 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
8708 			goto nla_put_failure;
8709 		break;
8710 	case CFG80211_SIGNAL_TYPE_UNSPEC:
8711 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
8712 			goto nla_put_failure;
8713 		break;
8714 	default:
8715 		break;
8716 	}
8717 
8718 	switch (wdev->iftype) {
8719 	case NL80211_IFTYPE_P2P_CLIENT:
8720 	case NL80211_IFTYPE_STATION:
8721 		if (intbss == wdev->current_bss &&
8722 		    nla_put_u32(msg, NL80211_BSS_STATUS,
8723 				NL80211_BSS_STATUS_ASSOCIATED))
8724 			goto nla_put_failure;
8725 		break;
8726 	case NL80211_IFTYPE_ADHOC:
8727 		if (intbss == wdev->current_bss &&
8728 		    nla_put_u32(msg, NL80211_BSS_STATUS,
8729 				NL80211_BSS_STATUS_IBSS_JOINED))
8730 			goto nla_put_failure;
8731 		break;
8732 	default:
8733 		break;
8734 	}
8735 
8736 	nla_nest_end(msg, bss);
8737 
8738 	genlmsg_end(msg, hdr);
8739 	return 0;
8740 
8741  fail_unlock_rcu:
8742 	rcu_read_unlock();
8743  nla_put_failure:
8744 	genlmsg_cancel(msg, hdr);
8745 	return -EMSGSIZE;
8746 }
8747 
8748 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
8749 {
8750 	struct cfg80211_registered_device *rdev;
8751 	struct cfg80211_internal_bss *scan;
8752 	struct wireless_dev *wdev;
8753 	int start = cb->args[2], idx = 0;
8754 	int err;
8755 
8756 	rtnl_lock();
8757 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
8758 	if (err) {
8759 		rtnl_unlock();
8760 		return err;
8761 	}
8762 
8763 	wdev_lock(wdev);
8764 	spin_lock_bh(&rdev->bss_lock);
8765 
8766 	/*
8767 	 * dump_scan will be called multiple times to break up the scan results
8768 	 * into multiple messages.  It is unlikely that any more bss-es will be
8769 	 * expired after the first call, so only call only call this on the
8770 	 * first dump_scan invocation.
8771 	 */
8772 	if (start == 0)
8773 		cfg80211_bss_expire(rdev);
8774 
8775 	cb->seq = rdev->bss_generation;
8776 
8777 	list_for_each_entry(scan, &rdev->bss_list, list) {
8778 		if (++idx <= start)
8779 			continue;
8780 		if (nl80211_send_bss(skb, cb,
8781 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
8782 				rdev, wdev, scan) < 0) {
8783 			idx--;
8784 			break;
8785 		}
8786 	}
8787 
8788 	spin_unlock_bh(&rdev->bss_lock);
8789 	wdev_unlock(wdev);
8790 
8791 	cb->args[2] = idx;
8792 	rtnl_unlock();
8793 
8794 	return skb->len;
8795 }
8796 
8797 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
8798 			       int flags, struct net_device *dev,
8799 			       bool allow_radio_stats,
8800 			       struct survey_info *survey)
8801 {
8802 	void *hdr;
8803 	struct nlattr *infoattr;
8804 
8805 	/* skip radio stats if userspace didn't request them */
8806 	if (!survey->channel && !allow_radio_stats)
8807 		return 0;
8808 
8809 	hdr = nl80211hdr_put(msg, portid, seq, flags,
8810 			     NL80211_CMD_NEW_SURVEY_RESULTS);
8811 	if (!hdr)
8812 		return -ENOMEM;
8813 
8814 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
8815 		goto nla_put_failure;
8816 
8817 	infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
8818 	if (!infoattr)
8819 		goto nla_put_failure;
8820 
8821 	if (survey->channel &&
8822 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
8823 			survey->channel->center_freq))
8824 		goto nla_put_failure;
8825 
8826 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
8827 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
8828 		goto nla_put_failure;
8829 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
8830 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
8831 		goto nla_put_failure;
8832 	if ((survey->filled & SURVEY_INFO_TIME) &&
8833 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
8834 			survey->time, NL80211_SURVEY_INFO_PAD))
8835 		goto nla_put_failure;
8836 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
8837 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
8838 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
8839 		goto nla_put_failure;
8840 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
8841 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
8842 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
8843 		goto nla_put_failure;
8844 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
8845 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
8846 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
8847 		goto nla_put_failure;
8848 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
8849 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
8850 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
8851 		goto nla_put_failure;
8852 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
8853 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
8854 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
8855 		goto nla_put_failure;
8856 	if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
8857 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
8858 			      survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
8859 		goto nla_put_failure;
8860 
8861 	nla_nest_end(msg, infoattr);
8862 
8863 	genlmsg_end(msg, hdr);
8864 	return 0;
8865 
8866  nla_put_failure:
8867 	genlmsg_cancel(msg, hdr);
8868 	return -EMSGSIZE;
8869 }
8870 
8871 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
8872 {
8873 	struct nlattr **attrbuf;
8874 	struct survey_info survey;
8875 	struct cfg80211_registered_device *rdev;
8876 	struct wireless_dev *wdev;
8877 	int survey_idx = cb->args[2];
8878 	int res;
8879 	bool radio_stats;
8880 
8881 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
8882 	if (!attrbuf)
8883 		return -ENOMEM;
8884 
8885 	rtnl_lock();
8886 	res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
8887 	if (res)
8888 		goto out_err;
8889 
8890 	/* prepare_wdev_dump parsed the attributes */
8891 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
8892 
8893 	if (!wdev->netdev) {
8894 		res = -EINVAL;
8895 		goto out_err;
8896 	}
8897 
8898 	if (!rdev->ops->dump_survey) {
8899 		res = -EOPNOTSUPP;
8900 		goto out_err;
8901 	}
8902 
8903 	while (1) {
8904 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
8905 		if (res == -ENOENT)
8906 			break;
8907 		if (res)
8908 			goto out_err;
8909 
8910 		/* don't send disabled channels, but do send non-channel data */
8911 		if (survey.channel &&
8912 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
8913 			survey_idx++;
8914 			continue;
8915 		}
8916 
8917 		if (nl80211_send_survey(skb,
8918 				NETLINK_CB(cb->skb).portid,
8919 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
8920 				wdev->netdev, radio_stats, &survey) < 0)
8921 			goto out;
8922 		survey_idx++;
8923 	}
8924 
8925  out:
8926 	cb->args[2] = survey_idx;
8927 	res = skb->len;
8928  out_err:
8929 	kfree(attrbuf);
8930 	rtnl_unlock();
8931 	return res;
8932 }
8933 
8934 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
8935 {
8936 	return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
8937 				  NL80211_WPA_VERSION_2 |
8938 				  NL80211_WPA_VERSION_3));
8939 }
8940 
8941 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
8942 {
8943 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8944 	struct net_device *dev = info->user_ptr[1];
8945 	struct ieee80211_channel *chan;
8946 	const u8 *bssid, *ssid, *ie = NULL, *auth_data = NULL;
8947 	int err, ssid_len, ie_len = 0, auth_data_len = 0;
8948 	enum nl80211_auth_type auth_type;
8949 	struct key_parse key;
8950 	bool local_state_change;
8951 
8952 	if (!info->attrs[NL80211_ATTR_MAC])
8953 		return -EINVAL;
8954 
8955 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
8956 		return -EINVAL;
8957 
8958 	if (!info->attrs[NL80211_ATTR_SSID])
8959 		return -EINVAL;
8960 
8961 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
8962 		return -EINVAL;
8963 
8964 	err = nl80211_parse_key(info, &key);
8965 	if (err)
8966 		return err;
8967 
8968 	if (key.idx >= 0) {
8969 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
8970 			return -EINVAL;
8971 		if (!key.p.key || !key.p.key_len)
8972 			return -EINVAL;
8973 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
8974 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
8975 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
8976 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
8977 			return -EINVAL;
8978 		if (key.idx > 3)
8979 			return -EINVAL;
8980 	} else {
8981 		key.p.key_len = 0;
8982 		key.p.key = NULL;
8983 	}
8984 
8985 	if (key.idx >= 0) {
8986 		int i;
8987 		bool ok = false;
8988 
8989 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
8990 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
8991 				ok = true;
8992 				break;
8993 			}
8994 		}
8995 		if (!ok)
8996 			return -EINVAL;
8997 	}
8998 
8999 	if (!rdev->ops->auth)
9000 		return -EOPNOTSUPP;
9001 
9002 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9003 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9004 		return -EOPNOTSUPP;
9005 
9006 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9007 	chan = nl80211_get_valid_chan(&rdev->wiphy,
9008 				      info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9009 	if (!chan)
9010 		return -EINVAL;
9011 
9012 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9013 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9014 
9015 	if (info->attrs[NL80211_ATTR_IE]) {
9016 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9017 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9018 	}
9019 
9020 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
9021 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
9022 		return -EINVAL;
9023 
9024 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
9025 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
9026 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
9027 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
9028 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
9029 		return -EINVAL;
9030 
9031 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
9032 		if (auth_type != NL80211_AUTHTYPE_SAE &&
9033 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
9034 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
9035 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
9036 			return -EINVAL;
9037 		auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
9038 		auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
9039 		/* need to include at least Auth Transaction and Status Code */
9040 		if (auth_data_len < 4)
9041 			return -EINVAL;
9042 	}
9043 
9044 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9045 
9046 	/*
9047 	 * Since we no longer track auth state, ignore
9048 	 * requests to only change local state.
9049 	 */
9050 	if (local_state_change)
9051 		return 0;
9052 
9053 	wdev_lock(dev->ieee80211_ptr);
9054 	err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
9055 				 ssid, ssid_len, ie, ie_len,
9056 				 key.p.key, key.p.key_len, key.idx,
9057 				 auth_data, auth_data_len);
9058 	wdev_unlock(dev->ieee80211_ptr);
9059 	return err;
9060 }
9061 
9062 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
9063 				     struct genl_info *info)
9064 {
9065 	if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9066 		GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
9067 		return -EINVAL;
9068 	}
9069 
9070 	if (!rdev->ops->tx_control_port ||
9071 	    !wiphy_ext_feature_isset(&rdev->wiphy,
9072 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
9073 		return -EOPNOTSUPP;
9074 
9075 	return 0;
9076 }
9077 
9078 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
9079 				   struct genl_info *info,
9080 				   struct cfg80211_crypto_settings *settings,
9081 				   int cipher_limit)
9082 {
9083 	memset(settings, 0, sizeof(*settings));
9084 
9085 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
9086 
9087 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
9088 		u16 proto;
9089 
9090 		proto = nla_get_u16(
9091 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
9092 		settings->control_port_ethertype = cpu_to_be16(proto);
9093 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
9094 		    proto != ETH_P_PAE)
9095 			return -EINVAL;
9096 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
9097 			settings->control_port_no_encrypt = true;
9098 	} else
9099 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
9100 
9101 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
9102 		int r = validate_pae_over_nl80211(rdev, info);
9103 
9104 		if (r < 0)
9105 			return r;
9106 
9107 		settings->control_port_over_nl80211 = true;
9108 	}
9109 
9110 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
9111 		void *data;
9112 		int len, i;
9113 
9114 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9115 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9116 		settings->n_ciphers_pairwise = len / sizeof(u32);
9117 
9118 		if (len % sizeof(u32))
9119 			return -EINVAL;
9120 
9121 		if (settings->n_ciphers_pairwise > cipher_limit)
9122 			return -EINVAL;
9123 
9124 		memcpy(settings->ciphers_pairwise, data, len);
9125 
9126 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
9127 			if (!cfg80211_supported_cipher_suite(
9128 					&rdev->wiphy,
9129 					settings->ciphers_pairwise[i]))
9130 				return -EINVAL;
9131 	}
9132 
9133 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
9134 		settings->cipher_group =
9135 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
9136 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
9137 						     settings->cipher_group))
9138 			return -EINVAL;
9139 	}
9140 
9141 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
9142 		settings->wpa_versions =
9143 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
9144 		if (!nl80211_valid_wpa_versions(settings->wpa_versions))
9145 			return -EINVAL;
9146 	}
9147 
9148 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
9149 		void *data;
9150 		int len;
9151 
9152 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
9153 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
9154 		settings->n_akm_suites = len / sizeof(u32);
9155 
9156 		if (len % sizeof(u32))
9157 			return -EINVAL;
9158 
9159 		if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
9160 			return -EINVAL;
9161 
9162 		memcpy(settings->akm_suites, data, len);
9163 	}
9164 
9165 	if (info->attrs[NL80211_ATTR_PMK]) {
9166 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
9167 			return -EINVAL;
9168 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9169 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK))
9170 			return -EINVAL;
9171 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
9172 	}
9173 
9174 	if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
9175 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9176 					     NL80211_EXT_FEATURE_SAE_OFFLOAD))
9177 			return -EINVAL;
9178 		settings->sae_pwd =
9179 			nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9180 		settings->sae_pwd_len =
9181 			nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9182 	}
9183 
9184 	return 0;
9185 }
9186 
9187 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
9188 {
9189 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9190 	struct net_device *dev = info->user_ptr[1];
9191 	struct ieee80211_channel *chan;
9192 	struct cfg80211_assoc_request req = {};
9193 	const u8 *bssid, *ssid;
9194 	int err, ssid_len = 0;
9195 
9196 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9197 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9198 		return -EPERM;
9199 
9200 	if (!info->attrs[NL80211_ATTR_MAC] ||
9201 	    !info->attrs[NL80211_ATTR_SSID] ||
9202 	    !info->attrs[NL80211_ATTR_WIPHY_FREQ])
9203 		return -EINVAL;
9204 
9205 	if (!rdev->ops->assoc)
9206 		return -EOPNOTSUPP;
9207 
9208 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9209 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9210 		return -EOPNOTSUPP;
9211 
9212 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9213 
9214 	chan = nl80211_get_valid_chan(&rdev->wiphy,
9215 				      info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9216 	if (!chan)
9217 		return -EINVAL;
9218 
9219 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9220 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9221 
9222 	if (info->attrs[NL80211_ATTR_IE]) {
9223 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9224 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9225 	}
9226 
9227 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
9228 		enum nl80211_mfp mfp =
9229 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
9230 		if (mfp == NL80211_MFP_REQUIRED)
9231 			req.use_mfp = true;
9232 		else if (mfp != NL80211_MFP_NO)
9233 			return -EINVAL;
9234 	}
9235 
9236 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
9237 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
9238 
9239 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9240 		req.flags |= ASSOC_REQ_DISABLE_HT;
9241 
9242 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9243 		memcpy(&req.ht_capa_mask,
9244 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9245 		       sizeof(req.ht_capa_mask));
9246 
9247 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9248 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9249 			return -EINVAL;
9250 		memcpy(&req.ht_capa,
9251 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9252 		       sizeof(req.ht_capa));
9253 	}
9254 
9255 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
9256 		req.flags |= ASSOC_REQ_DISABLE_VHT;
9257 
9258 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9259 		memcpy(&req.vht_capa_mask,
9260 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
9261 		       sizeof(req.vht_capa_mask));
9262 
9263 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
9264 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9265 			return -EINVAL;
9266 		memcpy(&req.vht_capa,
9267 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
9268 		       sizeof(req.vht_capa));
9269 	}
9270 
9271 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
9272 		if (!((rdev->wiphy.features &
9273 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
9274 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
9275 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9276 					     NL80211_EXT_FEATURE_RRM))
9277 			return -EINVAL;
9278 		req.flags |= ASSOC_REQ_USE_RRM;
9279 	}
9280 
9281 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
9282 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
9283 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
9284 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
9285 			return -EINVAL;
9286 		req.fils_nonces =
9287 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
9288 	}
9289 
9290 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
9291 	if (!err) {
9292 		wdev_lock(dev->ieee80211_ptr);
9293 
9294 		err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
9295 					  ssid, ssid_len, &req);
9296 
9297 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9298 			dev->ieee80211_ptr->conn_owner_nlportid =
9299 				info->snd_portid;
9300 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
9301 			       bssid, ETH_ALEN);
9302 		}
9303 
9304 		wdev_unlock(dev->ieee80211_ptr);
9305 	}
9306 
9307 	return err;
9308 }
9309 
9310 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
9311 {
9312 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9313 	struct net_device *dev = info->user_ptr[1];
9314 	const u8 *ie = NULL, *bssid;
9315 	int ie_len = 0, err;
9316 	u16 reason_code;
9317 	bool local_state_change;
9318 
9319 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9320 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9321 		return -EPERM;
9322 
9323 	if (!info->attrs[NL80211_ATTR_MAC])
9324 		return -EINVAL;
9325 
9326 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
9327 		return -EINVAL;
9328 
9329 	if (!rdev->ops->deauth)
9330 		return -EOPNOTSUPP;
9331 
9332 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9333 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9334 		return -EOPNOTSUPP;
9335 
9336 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9337 
9338 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9339 	if (reason_code == 0) {
9340 		/* Reason Code 0 is reserved */
9341 		return -EINVAL;
9342 	}
9343 
9344 	if (info->attrs[NL80211_ATTR_IE]) {
9345 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9346 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9347 	}
9348 
9349 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9350 
9351 	wdev_lock(dev->ieee80211_ptr);
9352 	err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
9353 				   local_state_change);
9354 	wdev_unlock(dev->ieee80211_ptr);
9355 	return err;
9356 }
9357 
9358 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
9359 {
9360 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9361 	struct net_device *dev = info->user_ptr[1];
9362 	const u8 *ie = NULL, *bssid;
9363 	int ie_len = 0, err;
9364 	u16 reason_code;
9365 	bool local_state_change;
9366 
9367 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9368 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9369 		return -EPERM;
9370 
9371 	if (!info->attrs[NL80211_ATTR_MAC])
9372 		return -EINVAL;
9373 
9374 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
9375 		return -EINVAL;
9376 
9377 	if (!rdev->ops->disassoc)
9378 		return -EOPNOTSUPP;
9379 
9380 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9381 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9382 		return -EOPNOTSUPP;
9383 
9384 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9385 
9386 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9387 	if (reason_code == 0) {
9388 		/* Reason Code 0 is reserved */
9389 		return -EINVAL;
9390 	}
9391 
9392 	if (info->attrs[NL80211_ATTR_IE]) {
9393 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9394 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9395 	}
9396 
9397 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9398 
9399 	wdev_lock(dev->ieee80211_ptr);
9400 	err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
9401 				     local_state_change);
9402 	wdev_unlock(dev->ieee80211_ptr);
9403 	return err;
9404 }
9405 
9406 static bool
9407 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
9408 			 int mcast_rate[NUM_NL80211_BANDS],
9409 			 int rateval)
9410 {
9411 	struct wiphy *wiphy = &rdev->wiphy;
9412 	bool found = false;
9413 	int band, i;
9414 
9415 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
9416 		struct ieee80211_supported_band *sband;
9417 
9418 		sband = wiphy->bands[band];
9419 		if (!sband)
9420 			continue;
9421 
9422 		for (i = 0; i < sband->n_bitrates; i++) {
9423 			if (sband->bitrates[i].bitrate == rateval) {
9424 				mcast_rate[band] = i + 1;
9425 				found = true;
9426 				break;
9427 			}
9428 		}
9429 	}
9430 
9431 	return found;
9432 }
9433 
9434 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
9435 {
9436 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9437 	struct net_device *dev = info->user_ptr[1];
9438 	struct cfg80211_ibss_params ibss;
9439 	struct wiphy *wiphy;
9440 	struct cfg80211_cached_keys *connkeys = NULL;
9441 	int err;
9442 
9443 	memset(&ibss, 0, sizeof(ibss));
9444 
9445 	if (!info->attrs[NL80211_ATTR_SSID] ||
9446 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
9447 		return -EINVAL;
9448 
9449 	ibss.beacon_interval = 100;
9450 
9451 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
9452 		ibss.beacon_interval =
9453 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
9454 
9455 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
9456 					   ibss.beacon_interval);
9457 	if (err)
9458 		return err;
9459 
9460 	if (!rdev->ops->join_ibss)
9461 		return -EOPNOTSUPP;
9462 
9463 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
9464 		return -EOPNOTSUPP;
9465 
9466 	wiphy = &rdev->wiphy;
9467 
9468 	if (info->attrs[NL80211_ATTR_MAC]) {
9469 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9470 
9471 		if (!is_valid_ether_addr(ibss.bssid))
9472 			return -EINVAL;
9473 	}
9474 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9475 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9476 
9477 	if (info->attrs[NL80211_ATTR_IE]) {
9478 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9479 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9480 	}
9481 
9482 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
9483 	if (err)
9484 		return err;
9485 
9486 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
9487 				     NL80211_IFTYPE_ADHOC))
9488 		return -EINVAL;
9489 
9490 	switch (ibss.chandef.width) {
9491 	case NL80211_CHAN_WIDTH_5:
9492 	case NL80211_CHAN_WIDTH_10:
9493 	case NL80211_CHAN_WIDTH_20_NOHT:
9494 		break;
9495 	case NL80211_CHAN_WIDTH_20:
9496 	case NL80211_CHAN_WIDTH_40:
9497 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
9498 			return -EINVAL;
9499 		break;
9500 	case NL80211_CHAN_WIDTH_80:
9501 	case NL80211_CHAN_WIDTH_80P80:
9502 	case NL80211_CHAN_WIDTH_160:
9503 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
9504 			return -EINVAL;
9505 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9506 					     NL80211_EXT_FEATURE_VHT_IBSS))
9507 			return -EINVAL;
9508 		break;
9509 	default:
9510 		return -EINVAL;
9511 	}
9512 
9513 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
9514 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
9515 
9516 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
9517 		u8 *rates =
9518 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9519 		int n_rates =
9520 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9521 		struct ieee80211_supported_band *sband =
9522 			wiphy->bands[ibss.chandef.chan->band];
9523 
9524 		err = ieee80211_get_ratemask(sband, rates, n_rates,
9525 					     &ibss.basic_rates);
9526 		if (err)
9527 			return err;
9528 	}
9529 
9530 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9531 		memcpy(&ibss.ht_capa_mask,
9532 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9533 		       sizeof(ibss.ht_capa_mask));
9534 
9535 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9536 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9537 			return -EINVAL;
9538 		memcpy(&ibss.ht_capa,
9539 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9540 		       sizeof(ibss.ht_capa));
9541 	}
9542 
9543 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
9544 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
9545 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
9546 		return -EINVAL;
9547 
9548 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9549 		bool no_ht = false;
9550 
9551 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
9552 		if (IS_ERR(connkeys))
9553 			return PTR_ERR(connkeys);
9554 
9555 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
9556 		    no_ht) {
9557 			kzfree(connkeys);
9558 			return -EINVAL;
9559 		}
9560 	}
9561 
9562 	ibss.control_port =
9563 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
9564 
9565 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
9566 		int r = validate_pae_over_nl80211(rdev, info);
9567 
9568 		if (r < 0) {
9569 			kzfree(connkeys);
9570 			return r;
9571 		}
9572 
9573 		ibss.control_port_over_nl80211 = true;
9574 	}
9575 
9576 	ibss.userspace_handles_dfs =
9577 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
9578 
9579 	wdev_lock(dev->ieee80211_ptr);
9580 	err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
9581 	if (err)
9582 		kzfree(connkeys);
9583 	else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9584 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
9585 	wdev_unlock(dev->ieee80211_ptr);
9586 
9587 	return err;
9588 }
9589 
9590 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
9591 {
9592 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9593 	struct net_device *dev = info->user_ptr[1];
9594 
9595 	if (!rdev->ops->leave_ibss)
9596 		return -EOPNOTSUPP;
9597 
9598 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
9599 		return -EOPNOTSUPP;
9600 
9601 	return cfg80211_leave_ibss(rdev, dev, false);
9602 }
9603 
9604 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
9605 {
9606 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9607 	struct net_device *dev = info->user_ptr[1];
9608 	int mcast_rate[NUM_NL80211_BANDS];
9609 	u32 nla_rate;
9610 	int err;
9611 
9612 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
9613 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
9614 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
9615 		return -EOPNOTSUPP;
9616 
9617 	if (!rdev->ops->set_mcast_rate)
9618 		return -EOPNOTSUPP;
9619 
9620 	memset(mcast_rate, 0, sizeof(mcast_rate));
9621 
9622 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
9623 		return -EINVAL;
9624 
9625 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
9626 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
9627 		return -EINVAL;
9628 
9629 	err = rdev_set_mcast_rate(rdev, dev, mcast_rate);
9630 
9631 	return err;
9632 }
9633 
9634 static struct sk_buff *
9635 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
9636 			    struct wireless_dev *wdev, int approxlen,
9637 			    u32 portid, u32 seq, enum nl80211_commands cmd,
9638 			    enum nl80211_attrs attr,
9639 			    const struct nl80211_vendor_cmd_info *info,
9640 			    gfp_t gfp)
9641 {
9642 	struct sk_buff *skb;
9643 	void *hdr;
9644 	struct nlattr *data;
9645 
9646 	skb = nlmsg_new(approxlen + 100, gfp);
9647 	if (!skb)
9648 		return NULL;
9649 
9650 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
9651 	if (!hdr) {
9652 		kfree_skb(skb);
9653 		return NULL;
9654 	}
9655 
9656 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9657 		goto nla_put_failure;
9658 
9659 	if (info) {
9660 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
9661 				info->vendor_id))
9662 			goto nla_put_failure;
9663 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
9664 				info->subcmd))
9665 			goto nla_put_failure;
9666 	}
9667 
9668 	if (wdev) {
9669 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
9670 				      wdev_id(wdev), NL80211_ATTR_PAD))
9671 			goto nla_put_failure;
9672 		if (wdev->netdev &&
9673 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
9674 				wdev->netdev->ifindex))
9675 			goto nla_put_failure;
9676 	}
9677 
9678 	data = nla_nest_start_noflag(skb, attr);
9679 	if (!data)
9680 		goto nla_put_failure;
9681 
9682 	((void **)skb->cb)[0] = rdev;
9683 	((void **)skb->cb)[1] = hdr;
9684 	((void **)skb->cb)[2] = data;
9685 
9686 	return skb;
9687 
9688  nla_put_failure:
9689 	kfree_skb(skb);
9690 	return NULL;
9691 }
9692 
9693 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
9694 					   struct wireless_dev *wdev,
9695 					   enum nl80211_commands cmd,
9696 					   enum nl80211_attrs attr,
9697 					   unsigned int portid,
9698 					   int vendor_event_idx,
9699 					   int approxlen, gfp_t gfp)
9700 {
9701 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
9702 	const struct nl80211_vendor_cmd_info *info;
9703 
9704 	switch (cmd) {
9705 	case NL80211_CMD_TESTMODE:
9706 		if (WARN_ON(vendor_event_idx != -1))
9707 			return NULL;
9708 		info = NULL;
9709 		break;
9710 	case NL80211_CMD_VENDOR:
9711 		if (WARN_ON(vendor_event_idx < 0 ||
9712 			    vendor_event_idx >= wiphy->n_vendor_events))
9713 			return NULL;
9714 		info = &wiphy->vendor_events[vendor_event_idx];
9715 		break;
9716 	default:
9717 		WARN_ON(1);
9718 		return NULL;
9719 	}
9720 
9721 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
9722 					   cmd, attr, info, gfp);
9723 }
9724 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
9725 
9726 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
9727 {
9728 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9729 	void *hdr = ((void **)skb->cb)[1];
9730 	struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
9731 	struct nlattr *data = ((void **)skb->cb)[2];
9732 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
9733 
9734 	/* clear CB data for netlink core to own from now on */
9735 	memset(skb->cb, 0, sizeof(skb->cb));
9736 
9737 	nla_nest_end(skb, data);
9738 	genlmsg_end(skb, hdr);
9739 
9740 	if (nlhdr->nlmsg_pid) {
9741 		genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
9742 				nlhdr->nlmsg_pid);
9743 	} else {
9744 		if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
9745 			mcgrp = NL80211_MCGRP_VENDOR;
9746 
9747 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
9748 					skb, 0, mcgrp, gfp);
9749 	}
9750 }
9751 EXPORT_SYMBOL(__cfg80211_send_event_skb);
9752 
9753 #ifdef CONFIG_NL80211_TESTMODE
9754 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
9755 {
9756 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9757 	struct wireless_dev *wdev =
9758 		__cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9759 	int err;
9760 
9761 	if (!rdev->ops->testmode_cmd)
9762 		return -EOPNOTSUPP;
9763 
9764 	if (IS_ERR(wdev)) {
9765 		err = PTR_ERR(wdev);
9766 		if (err != -EINVAL)
9767 			return err;
9768 		wdev = NULL;
9769 	} else if (wdev->wiphy != &rdev->wiphy) {
9770 		return -EINVAL;
9771 	}
9772 
9773 	if (!info->attrs[NL80211_ATTR_TESTDATA])
9774 		return -EINVAL;
9775 
9776 	rdev->cur_cmd_info = info;
9777 	err = rdev_testmode_cmd(rdev, wdev,
9778 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
9779 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
9780 	rdev->cur_cmd_info = NULL;
9781 
9782 	return err;
9783 }
9784 
9785 static int nl80211_testmode_dump(struct sk_buff *skb,
9786 				 struct netlink_callback *cb)
9787 {
9788 	struct cfg80211_registered_device *rdev;
9789 	struct nlattr **attrbuf = NULL;
9790 	int err;
9791 	long phy_idx;
9792 	void *data = NULL;
9793 	int data_len = 0;
9794 
9795 	rtnl_lock();
9796 
9797 	if (cb->args[0]) {
9798 		/*
9799 		 * 0 is a valid index, but not valid for args[0],
9800 		 * so we need to offset by 1.
9801 		 */
9802 		phy_idx = cb->args[0] - 1;
9803 
9804 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
9805 		if (!rdev) {
9806 			err = -ENOENT;
9807 			goto out_err;
9808 		}
9809 	} else {
9810 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
9811 				  GFP_KERNEL);
9812 		if (!attrbuf) {
9813 			err = -ENOMEM;
9814 			goto out_err;
9815 		}
9816 
9817 		err = nlmsg_parse_deprecated(cb->nlh,
9818 					     GENL_HDRLEN + nl80211_fam.hdrsize,
9819 					     attrbuf, nl80211_fam.maxattr,
9820 					     nl80211_policy, NULL);
9821 		if (err)
9822 			goto out_err;
9823 
9824 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
9825 		if (IS_ERR(rdev)) {
9826 			err = PTR_ERR(rdev);
9827 			goto out_err;
9828 		}
9829 		phy_idx = rdev->wiphy_idx;
9830 
9831 		if (attrbuf[NL80211_ATTR_TESTDATA])
9832 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
9833 	}
9834 
9835 	if (cb->args[1]) {
9836 		data = nla_data((void *)cb->args[1]);
9837 		data_len = nla_len((void *)cb->args[1]);
9838 	}
9839 
9840 	if (!rdev->ops->testmode_dump) {
9841 		err = -EOPNOTSUPP;
9842 		goto out_err;
9843 	}
9844 
9845 	while (1) {
9846 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
9847 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
9848 					   NL80211_CMD_TESTMODE);
9849 		struct nlattr *tmdata;
9850 
9851 		if (!hdr)
9852 			break;
9853 
9854 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
9855 			genlmsg_cancel(skb, hdr);
9856 			break;
9857 		}
9858 
9859 		tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
9860 		if (!tmdata) {
9861 			genlmsg_cancel(skb, hdr);
9862 			break;
9863 		}
9864 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
9865 		nla_nest_end(skb, tmdata);
9866 
9867 		if (err == -ENOBUFS || err == -ENOENT) {
9868 			genlmsg_cancel(skb, hdr);
9869 			break;
9870 		} else if (err) {
9871 			genlmsg_cancel(skb, hdr);
9872 			goto out_err;
9873 		}
9874 
9875 		genlmsg_end(skb, hdr);
9876 	}
9877 
9878 	err = skb->len;
9879 	/* see above */
9880 	cb->args[0] = phy_idx + 1;
9881  out_err:
9882 	kfree(attrbuf);
9883 	rtnl_unlock();
9884 	return err;
9885 }
9886 #endif
9887 
9888 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
9889 {
9890 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9891 	struct net_device *dev = info->user_ptr[1];
9892 	struct cfg80211_connect_params connect;
9893 	struct wiphy *wiphy;
9894 	struct cfg80211_cached_keys *connkeys = NULL;
9895 	int err;
9896 
9897 	memset(&connect, 0, sizeof(connect));
9898 
9899 	if (!info->attrs[NL80211_ATTR_SSID] ||
9900 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
9901 		return -EINVAL;
9902 
9903 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
9904 		connect.auth_type =
9905 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
9906 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
9907 					     NL80211_CMD_CONNECT))
9908 			return -EINVAL;
9909 	} else
9910 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
9911 
9912 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
9913 
9914 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
9915 	    !wiphy_ext_feature_isset(&rdev->wiphy,
9916 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
9917 		return -EINVAL;
9918 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
9919 
9920 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
9921 				      NL80211_MAX_NR_CIPHER_SUITES);
9922 	if (err)
9923 		return err;
9924 
9925 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9926 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9927 		return -EOPNOTSUPP;
9928 
9929 	wiphy = &rdev->wiphy;
9930 
9931 	connect.bg_scan_period = -1;
9932 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
9933 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
9934 		connect.bg_scan_period =
9935 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
9936 	}
9937 
9938 	if (info->attrs[NL80211_ATTR_MAC])
9939 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9940 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
9941 		connect.bssid_hint =
9942 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
9943 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9944 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9945 
9946 	if (info->attrs[NL80211_ATTR_IE]) {
9947 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9948 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9949 	}
9950 
9951 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
9952 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
9953 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
9954 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9955 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
9956 			return -EOPNOTSUPP;
9957 	} else {
9958 		connect.mfp = NL80211_MFP_NO;
9959 	}
9960 
9961 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
9962 		connect.prev_bssid =
9963 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
9964 
9965 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
9966 		connect.channel = nl80211_get_valid_chan(
9967 			wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9968 		if (!connect.channel)
9969 			return -EINVAL;
9970 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
9971 		connect.channel_hint = nl80211_get_valid_chan(
9972 			wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
9973 		if (!connect.channel_hint)
9974 			return -EINVAL;
9975 	}
9976 
9977 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
9978 		connect.edmg.channels =
9979 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
9980 
9981 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
9982 			connect.edmg.bw_config =
9983 				nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
9984 	}
9985 
9986 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9987 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
9988 		if (IS_ERR(connkeys))
9989 			return PTR_ERR(connkeys);
9990 	}
9991 
9992 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9993 		connect.flags |= ASSOC_REQ_DISABLE_HT;
9994 
9995 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9996 		memcpy(&connect.ht_capa_mask,
9997 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9998 		       sizeof(connect.ht_capa_mask));
9999 
10000 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
10001 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
10002 			kzfree(connkeys);
10003 			return -EINVAL;
10004 		}
10005 		memcpy(&connect.ht_capa,
10006 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
10007 		       sizeof(connect.ht_capa));
10008 	}
10009 
10010 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
10011 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
10012 
10013 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10014 		memcpy(&connect.vht_capa_mask,
10015 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
10016 		       sizeof(connect.vht_capa_mask));
10017 
10018 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
10019 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
10020 			kzfree(connkeys);
10021 			return -EINVAL;
10022 		}
10023 		memcpy(&connect.vht_capa,
10024 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
10025 		       sizeof(connect.vht_capa));
10026 	}
10027 
10028 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
10029 		if (!((rdev->wiphy.features &
10030 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
10031 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
10032 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10033 					     NL80211_EXT_FEATURE_RRM)) {
10034 			kzfree(connkeys);
10035 			return -EINVAL;
10036 		}
10037 		connect.flags |= ASSOC_REQ_USE_RRM;
10038 	}
10039 
10040 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
10041 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
10042 		kzfree(connkeys);
10043 		return -EOPNOTSUPP;
10044 	}
10045 
10046 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
10047 		/* bss selection makes no sense if bssid is set */
10048 		if (connect.bssid) {
10049 			kzfree(connkeys);
10050 			return -EINVAL;
10051 		}
10052 
10053 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
10054 				       wiphy, &connect.bss_select);
10055 		if (err) {
10056 			kzfree(connkeys);
10057 			return err;
10058 		}
10059 	}
10060 
10061 	if (wiphy_ext_feature_isset(&rdev->wiphy,
10062 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
10063 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
10064 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
10065 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
10066 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10067 		connect.fils_erp_username =
10068 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10069 		connect.fils_erp_username_len =
10070 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10071 		connect.fils_erp_realm =
10072 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10073 		connect.fils_erp_realm_len =
10074 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10075 		connect.fils_erp_next_seq_num =
10076 			nla_get_u16(
10077 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
10078 		connect.fils_erp_rrk =
10079 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10080 		connect.fils_erp_rrk_len =
10081 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10082 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
10083 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
10084 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
10085 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10086 		kzfree(connkeys);
10087 		return -EINVAL;
10088 	}
10089 
10090 	if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
10091 		if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10092 			kzfree(connkeys);
10093 			GENL_SET_ERR_MSG(info,
10094 					 "external auth requires connection ownership");
10095 			return -EINVAL;
10096 		}
10097 		connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
10098 	}
10099 
10100 	wdev_lock(dev->ieee80211_ptr);
10101 
10102 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
10103 			       connect.prev_bssid);
10104 	if (err)
10105 		kzfree(connkeys);
10106 
10107 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10108 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
10109 		if (connect.bssid)
10110 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
10111 			       connect.bssid, ETH_ALEN);
10112 		else
10113 			memset(dev->ieee80211_ptr->disconnect_bssid,
10114 			       0, ETH_ALEN);
10115 	}
10116 
10117 	wdev_unlock(dev->ieee80211_ptr);
10118 
10119 	return err;
10120 }
10121 
10122 static int nl80211_update_connect_params(struct sk_buff *skb,
10123 					 struct genl_info *info)
10124 {
10125 	struct cfg80211_connect_params connect = {};
10126 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10127 	struct net_device *dev = info->user_ptr[1];
10128 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10129 	bool fils_sk_offload;
10130 	u32 auth_type;
10131 	u32 changed = 0;
10132 	int ret;
10133 
10134 	if (!rdev->ops->update_connect_params)
10135 		return -EOPNOTSUPP;
10136 
10137 	if (info->attrs[NL80211_ATTR_IE]) {
10138 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10139 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10140 		changed |= UPDATE_ASSOC_IES;
10141 	}
10142 
10143 	fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
10144 						  NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
10145 
10146 	/*
10147 	 * when driver supports fils-sk offload all attributes must be
10148 	 * provided. So the else covers "fils-sk-not-all" and
10149 	 * "no-fils-sk-any".
10150 	 */
10151 	if (fils_sk_offload &&
10152 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
10153 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
10154 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
10155 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10156 		connect.fils_erp_username =
10157 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10158 		connect.fils_erp_username_len =
10159 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10160 		connect.fils_erp_realm =
10161 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10162 		connect.fils_erp_realm_len =
10163 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10164 		connect.fils_erp_next_seq_num =
10165 			nla_get_u16(
10166 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
10167 		connect.fils_erp_rrk =
10168 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10169 		connect.fils_erp_rrk_len =
10170 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10171 		changed |= UPDATE_FILS_ERP_INFO;
10172 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
10173 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
10174 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
10175 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10176 		return -EINVAL;
10177 	}
10178 
10179 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
10180 		auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10181 		if (!nl80211_valid_auth_type(rdev, auth_type,
10182 					     NL80211_CMD_CONNECT))
10183 			return -EINVAL;
10184 
10185 		if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
10186 		    fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
10187 			return -EINVAL;
10188 
10189 		connect.auth_type = auth_type;
10190 		changed |= UPDATE_AUTH_TYPE;
10191 	}
10192 
10193 	wdev_lock(dev->ieee80211_ptr);
10194 	if (!wdev->current_bss)
10195 		ret = -ENOLINK;
10196 	else
10197 		ret = rdev_update_connect_params(rdev, dev, &connect, changed);
10198 	wdev_unlock(dev->ieee80211_ptr);
10199 
10200 	return ret;
10201 }
10202 
10203 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
10204 {
10205 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10206 	struct net_device *dev = info->user_ptr[1];
10207 	u16 reason;
10208 	int ret;
10209 
10210 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
10211 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10212 		return -EPERM;
10213 
10214 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
10215 		reason = WLAN_REASON_DEAUTH_LEAVING;
10216 	else
10217 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
10218 
10219 	if (reason == 0)
10220 		return -EINVAL;
10221 
10222 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10223 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10224 		return -EOPNOTSUPP;
10225 
10226 	wdev_lock(dev->ieee80211_ptr);
10227 	ret = cfg80211_disconnect(rdev, dev, reason, true);
10228 	wdev_unlock(dev->ieee80211_ptr);
10229 	return ret;
10230 }
10231 
10232 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
10233 {
10234 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10235 	struct net *net;
10236 	int err;
10237 
10238 	if (info->attrs[NL80211_ATTR_PID]) {
10239 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
10240 
10241 		net = get_net_ns_by_pid(pid);
10242 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
10243 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
10244 
10245 		net = get_net_ns_by_fd(fd);
10246 	} else {
10247 		return -EINVAL;
10248 	}
10249 
10250 	if (IS_ERR(net))
10251 		return PTR_ERR(net);
10252 
10253 	err = 0;
10254 
10255 	/* check if anything to do */
10256 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
10257 		err = cfg80211_switch_netns(rdev, net);
10258 
10259 	put_net(net);
10260 	return err;
10261 }
10262 
10263 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
10264 {
10265 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10266 	int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
10267 			struct cfg80211_pmksa *pmksa) = NULL;
10268 	struct net_device *dev = info->user_ptr[1];
10269 	struct cfg80211_pmksa pmksa;
10270 
10271 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
10272 
10273 	if (!info->attrs[NL80211_ATTR_PMKID])
10274 		return -EINVAL;
10275 
10276 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
10277 
10278 	if (info->attrs[NL80211_ATTR_MAC]) {
10279 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10280 	} else if (info->attrs[NL80211_ATTR_SSID] &&
10281 		   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
10282 		   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
10283 		    info->attrs[NL80211_ATTR_PMK])) {
10284 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10285 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10286 		pmksa.cache_id =
10287 			nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
10288 	} else {
10289 		return -EINVAL;
10290 	}
10291 	if (info->attrs[NL80211_ATTR_PMK]) {
10292 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10293 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
10294 	}
10295 
10296 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10297 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
10298 	    !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
10299 	      wiphy_ext_feature_isset(&rdev->wiphy,
10300 				      NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
10301 		return -EOPNOTSUPP;
10302 
10303 	switch (info->genlhdr->cmd) {
10304 	case NL80211_CMD_SET_PMKSA:
10305 		rdev_ops = rdev->ops->set_pmksa;
10306 		break;
10307 	case NL80211_CMD_DEL_PMKSA:
10308 		rdev_ops = rdev->ops->del_pmksa;
10309 		break;
10310 	default:
10311 		WARN_ON(1);
10312 		break;
10313 	}
10314 
10315 	if (!rdev_ops)
10316 		return -EOPNOTSUPP;
10317 
10318 	return rdev_ops(&rdev->wiphy, dev, &pmksa);
10319 }
10320 
10321 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
10322 {
10323 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10324 	struct net_device *dev = info->user_ptr[1];
10325 
10326 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10327 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10328 		return -EOPNOTSUPP;
10329 
10330 	if (!rdev->ops->flush_pmksa)
10331 		return -EOPNOTSUPP;
10332 
10333 	return rdev_flush_pmksa(rdev, dev);
10334 }
10335 
10336 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
10337 {
10338 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10339 	struct net_device *dev = info->user_ptr[1];
10340 	u8 action_code, dialog_token;
10341 	u32 peer_capability = 0;
10342 	u16 status_code;
10343 	u8 *peer;
10344 	bool initiator;
10345 
10346 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
10347 	    !rdev->ops->tdls_mgmt)
10348 		return -EOPNOTSUPP;
10349 
10350 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
10351 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
10352 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
10353 	    !info->attrs[NL80211_ATTR_IE] ||
10354 	    !info->attrs[NL80211_ATTR_MAC])
10355 		return -EINVAL;
10356 
10357 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10358 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
10359 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
10360 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
10361 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
10362 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
10363 		peer_capability =
10364 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
10365 
10366 	return rdev_tdls_mgmt(rdev, dev, peer, action_code,
10367 			      dialog_token, status_code, peer_capability,
10368 			      initiator,
10369 			      nla_data(info->attrs[NL80211_ATTR_IE]),
10370 			      nla_len(info->attrs[NL80211_ATTR_IE]));
10371 }
10372 
10373 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
10374 {
10375 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10376 	struct net_device *dev = info->user_ptr[1];
10377 	enum nl80211_tdls_operation operation;
10378 	u8 *peer;
10379 
10380 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
10381 	    !rdev->ops->tdls_oper)
10382 		return -EOPNOTSUPP;
10383 
10384 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
10385 	    !info->attrs[NL80211_ATTR_MAC])
10386 		return -EINVAL;
10387 
10388 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
10389 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10390 
10391 	return rdev_tdls_oper(rdev, dev, peer, operation);
10392 }
10393 
10394 static int nl80211_remain_on_channel(struct sk_buff *skb,
10395 				     struct genl_info *info)
10396 {
10397 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10398 	struct wireless_dev *wdev = info->user_ptr[1];
10399 	struct cfg80211_chan_def chandef;
10400 	const struct cfg80211_chan_def *compat_chandef;
10401 	struct sk_buff *msg;
10402 	void *hdr;
10403 	u64 cookie;
10404 	u32 duration;
10405 	int err;
10406 
10407 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
10408 	    !info->attrs[NL80211_ATTR_DURATION])
10409 		return -EINVAL;
10410 
10411 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
10412 
10413 	if (!rdev->ops->remain_on_channel ||
10414 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
10415 		return -EOPNOTSUPP;
10416 
10417 	/*
10418 	 * We should be on that channel for at least a minimum amount of
10419 	 * time (10ms) but no longer than the driver supports.
10420 	 */
10421 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
10422 	    duration > rdev->wiphy.max_remain_on_channel_duration)
10423 		return -EINVAL;
10424 
10425 	err = nl80211_parse_chandef(rdev, info, &chandef);
10426 	if (err)
10427 		return err;
10428 
10429 	wdev_lock(wdev);
10430 	if (!cfg80211_off_channel_oper_allowed(wdev) &&
10431 	    !cfg80211_chandef_identical(&wdev->chandef, &chandef)) {
10432 		compat_chandef = cfg80211_chandef_compatible(&wdev->chandef,
10433 							     &chandef);
10434 		if (compat_chandef != &chandef) {
10435 			wdev_unlock(wdev);
10436 			return -EBUSY;
10437 		}
10438 	}
10439 	wdev_unlock(wdev);
10440 
10441 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10442 	if (!msg)
10443 		return -ENOMEM;
10444 
10445 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10446 			     NL80211_CMD_REMAIN_ON_CHANNEL);
10447 	if (!hdr) {
10448 		err = -ENOBUFS;
10449 		goto free_msg;
10450 	}
10451 
10452 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
10453 				     duration, &cookie);
10454 
10455 	if (err)
10456 		goto free_msg;
10457 
10458 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
10459 			      NL80211_ATTR_PAD))
10460 		goto nla_put_failure;
10461 
10462 	genlmsg_end(msg, hdr);
10463 
10464 	return genlmsg_reply(msg, info);
10465 
10466  nla_put_failure:
10467 	err = -ENOBUFS;
10468  free_msg:
10469 	nlmsg_free(msg);
10470 	return err;
10471 }
10472 
10473 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
10474 					    struct genl_info *info)
10475 {
10476 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10477 	struct wireless_dev *wdev = info->user_ptr[1];
10478 	u64 cookie;
10479 
10480 	if (!info->attrs[NL80211_ATTR_COOKIE])
10481 		return -EINVAL;
10482 
10483 	if (!rdev->ops->cancel_remain_on_channel)
10484 		return -EOPNOTSUPP;
10485 
10486 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
10487 
10488 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
10489 }
10490 
10491 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
10492 				       struct genl_info *info)
10493 {
10494 	struct cfg80211_bitrate_mask mask;
10495 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10496 	struct net_device *dev = info->user_ptr[1];
10497 	int err;
10498 
10499 	if (!rdev->ops->set_bitrate_mask)
10500 		return -EOPNOTSUPP;
10501 
10502 	err = nl80211_parse_tx_bitrate_mask(info, &mask);
10503 	if (err)
10504 		return err;
10505 
10506 	return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
10507 }
10508 
10509 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
10510 {
10511 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10512 	struct wireless_dev *wdev = info->user_ptr[1];
10513 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
10514 
10515 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
10516 		return -EINVAL;
10517 
10518 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
10519 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
10520 
10521 	switch (wdev->iftype) {
10522 	case NL80211_IFTYPE_STATION:
10523 	case NL80211_IFTYPE_ADHOC:
10524 	case NL80211_IFTYPE_P2P_CLIENT:
10525 	case NL80211_IFTYPE_AP:
10526 	case NL80211_IFTYPE_AP_VLAN:
10527 	case NL80211_IFTYPE_MESH_POINT:
10528 	case NL80211_IFTYPE_P2P_GO:
10529 	case NL80211_IFTYPE_P2P_DEVICE:
10530 		break;
10531 	case NL80211_IFTYPE_NAN:
10532 	default:
10533 		return -EOPNOTSUPP;
10534 	}
10535 
10536 	/* not much point in registering if we can't reply */
10537 	if (!rdev->ops->mgmt_tx)
10538 		return -EOPNOTSUPP;
10539 
10540 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
10541 			nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
10542 			nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
10543 }
10544 
10545 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
10546 {
10547 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10548 	struct wireless_dev *wdev = info->user_ptr[1];
10549 	struct cfg80211_chan_def chandef;
10550 	int err;
10551 	void *hdr = NULL;
10552 	u64 cookie;
10553 	struct sk_buff *msg = NULL;
10554 	struct cfg80211_mgmt_tx_params params = {
10555 		.dont_wait_for_ack =
10556 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
10557 	};
10558 
10559 	if (!info->attrs[NL80211_ATTR_FRAME])
10560 		return -EINVAL;
10561 
10562 	if (!rdev->ops->mgmt_tx)
10563 		return -EOPNOTSUPP;
10564 
10565 	switch (wdev->iftype) {
10566 	case NL80211_IFTYPE_P2P_DEVICE:
10567 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10568 			return -EINVAL;
10569 	case NL80211_IFTYPE_STATION:
10570 	case NL80211_IFTYPE_ADHOC:
10571 	case NL80211_IFTYPE_P2P_CLIENT:
10572 	case NL80211_IFTYPE_AP:
10573 	case NL80211_IFTYPE_AP_VLAN:
10574 	case NL80211_IFTYPE_MESH_POINT:
10575 	case NL80211_IFTYPE_P2P_GO:
10576 		break;
10577 	case NL80211_IFTYPE_NAN:
10578 	default:
10579 		return -EOPNOTSUPP;
10580 	}
10581 
10582 	if (info->attrs[NL80211_ATTR_DURATION]) {
10583 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
10584 			return -EINVAL;
10585 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
10586 
10587 		/*
10588 		 * We should wait on the channel for at least a minimum amount
10589 		 * of time (10ms) but no longer than the driver supports.
10590 		 */
10591 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
10592 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
10593 			return -EINVAL;
10594 	}
10595 
10596 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
10597 
10598 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
10599 		return -EINVAL;
10600 
10601 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
10602 
10603 	/* get the channel if any has been specified, otherwise pass NULL to
10604 	 * the driver. The latter will use the current one
10605 	 */
10606 	chandef.chan = NULL;
10607 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
10608 		err = nl80211_parse_chandef(rdev, info, &chandef);
10609 		if (err)
10610 			return err;
10611 	}
10612 
10613 	if (!chandef.chan && params.offchan)
10614 		return -EINVAL;
10615 
10616 	wdev_lock(wdev);
10617 	if (params.offchan && !cfg80211_off_channel_oper_allowed(wdev)) {
10618 		wdev_unlock(wdev);
10619 		return -EBUSY;
10620 	}
10621 	wdev_unlock(wdev);
10622 
10623 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
10624 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
10625 
10626 	if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
10627 		int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
10628 		int i;
10629 
10630 		if (len % sizeof(u16))
10631 			return -EINVAL;
10632 
10633 		params.n_csa_offsets = len / sizeof(u16);
10634 		params.csa_offsets =
10635 			nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
10636 
10637 		/* check that all the offsets fit the frame */
10638 		for (i = 0; i < params.n_csa_offsets; i++) {
10639 			if (params.csa_offsets[i] >= params.len)
10640 				return -EINVAL;
10641 		}
10642 	}
10643 
10644 	if (!params.dont_wait_for_ack) {
10645 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10646 		if (!msg)
10647 			return -ENOMEM;
10648 
10649 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10650 				     NL80211_CMD_FRAME);
10651 		if (!hdr) {
10652 			err = -ENOBUFS;
10653 			goto free_msg;
10654 		}
10655 	}
10656 
10657 	params.chan = chandef.chan;
10658 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
10659 	if (err)
10660 		goto free_msg;
10661 
10662 	if (msg) {
10663 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
10664 				      NL80211_ATTR_PAD))
10665 			goto nla_put_failure;
10666 
10667 		genlmsg_end(msg, hdr);
10668 		return genlmsg_reply(msg, info);
10669 	}
10670 
10671 	return 0;
10672 
10673  nla_put_failure:
10674 	err = -ENOBUFS;
10675  free_msg:
10676 	nlmsg_free(msg);
10677 	return err;
10678 }
10679 
10680 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
10681 {
10682 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10683 	struct wireless_dev *wdev = info->user_ptr[1];
10684 	u64 cookie;
10685 
10686 	if (!info->attrs[NL80211_ATTR_COOKIE])
10687 		return -EINVAL;
10688 
10689 	if (!rdev->ops->mgmt_tx_cancel_wait)
10690 		return -EOPNOTSUPP;
10691 
10692 	switch (wdev->iftype) {
10693 	case NL80211_IFTYPE_STATION:
10694 	case NL80211_IFTYPE_ADHOC:
10695 	case NL80211_IFTYPE_P2P_CLIENT:
10696 	case NL80211_IFTYPE_AP:
10697 	case NL80211_IFTYPE_AP_VLAN:
10698 	case NL80211_IFTYPE_P2P_GO:
10699 	case NL80211_IFTYPE_P2P_DEVICE:
10700 		break;
10701 	case NL80211_IFTYPE_NAN:
10702 	default:
10703 		return -EOPNOTSUPP;
10704 	}
10705 
10706 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
10707 
10708 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
10709 }
10710 
10711 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
10712 {
10713 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10714 	struct wireless_dev *wdev;
10715 	struct net_device *dev = info->user_ptr[1];
10716 	u8 ps_state;
10717 	bool state;
10718 	int err;
10719 
10720 	if (!info->attrs[NL80211_ATTR_PS_STATE])
10721 		return -EINVAL;
10722 
10723 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
10724 
10725 	wdev = dev->ieee80211_ptr;
10726 
10727 	if (!rdev->ops->set_power_mgmt)
10728 		return -EOPNOTSUPP;
10729 
10730 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
10731 
10732 	if (state == wdev->ps)
10733 		return 0;
10734 
10735 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
10736 	if (!err)
10737 		wdev->ps = state;
10738 	return err;
10739 }
10740 
10741 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
10742 {
10743 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10744 	enum nl80211_ps_state ps_state;
10745 	struct wireless_dev *wdev;
10746 	struct net_device *dev = info->user_ptr[1];
10747 	struct sk_buff *msg;
10748 	void *hdr;
10749 	int err;
10750 
10751 	wdev = dev->ieee80211_ptr;
10752 
10753 	if (!rdev->ops->set_power_mgmt)
10754 		return -EOPNOTSUPP;
10755 
10756 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10757 	if (!msg)
10758 		return -ENOMEM;
10759 
10760 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10761 			     NL80211_CMD_GET_POWER_SAVE);
10762 	if (!hdr) {
10763 		err = -ENOBUFS;
10764 		goto free_msg;
10765 	}
10766 
10767 	if (wdev->ps)
10768 		ps_state = NL80211_PS_ENABLED;
10769 	else
10770 		ps_state = NL80211_PS_DISABLED;
10771 
10772 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
10773 		goto nla_put_failure;
10774 
10775 	genlmsg_end(msg, hdr);
10776 	return genlmsg_reply(msg, info);
10777 
10778  nla_put_failure:
10779 	err = -ENOBUFS;
10780  free_msg:
10781 	nlmsg_free(msg);
10782 	return err;
10783 }
10784 
10785 static const struct nla_policy
10786 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
10787 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
10788 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
10789 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
10790 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
10791 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
10792 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
10793 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
10794 };
10795 
10796 static int nl80211_set_cqm_txe(struct genl_info *info,
10797 			       u32 rate, u32 pkts, u32 intvl)
10798 {
10799 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10800 	struct net_device *dev = info->user_ptr[1];
10801 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10802 
10803 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
10804 		return -EINVAL;
10805 
10806 	if (!rdev->ops->set_cqm_txe_config)
10807 		return -EOPNOTSUPP;
10808 
10809 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
10810 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
10811 		return -EOPNOTSUPP;
10812 
10813 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
10814 }
10815 
10816 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
10817 				    struct net_device *dev)
10818 {
10819 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10820 	s32 last, low, high;
10821 	u32 hyst;
10822 	int i, n, low_index;
10823 	int err;
10824 
10825 	/* RSSI reporting disabled? */
10826 	if (!wdev->cqm_config)
10827 		return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
10828 
10829 	/*
10830 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
10831 	 * event has been received yet, we should receive an event after a
10832 	 * connection is established and enough beacons received to calculate
10833 	 * the average.
10834 	 */
10835 	if (!wdev->cqm_config->last_rssi_event_value && wdev->current_bss &&
10836 	    rdev->ops->get_station) {
10837 		struct station_info sinfo = {};
10838 		u8 *mac_addr;
10839 
10840 		mac_addr = wdev->current_bss->pub.bssid;
10841 
10842 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
10843 		if (err)
10844 			return err;
10845 
10846 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
10847 			wdev->cqm_config->last_rssi_event_value =
10848 				(s8) sinfo.rx_beacon_signal_avg;
10849 	}
10850 
10851 	last = wdev->cqm_config->last_rssi_event_value;
10852 	hyst = wdev->cqm_config->rssi_hyst;
10853 	n = wdev->cqm_config->n_rssi_thresholds;
10854 
10855 	for (i = 0; i < n; i++) {
10856 		i = array_index_nospec(i, n);
10857 		if (last < wdev->cqm_config->rssi_thresholds[i])
10858 			break;
10859 	}
10860 
10861 	low_index = i - 1;
10862 	if (low_index >= 0) {
10863 		low_index = array_index_nospec(low_index, n);
10864 		low = wdev->cqm_config->rssi_thresholds[low_index] - hyst;
10865 	} else {
10866 		low = S32_MIN;
10867 	}
10868 	if (i < n) {
10869 		i = array_index_nospec(i, n);
10870 		high = wdev->cqm_config->rssi_thresholds[i] + hyst - 1;
10871 	} else {
10872 		high = S32_MAX;
10873 	}
10874 
10875 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
10876 }
10877 
10878 static int nl80211_set_cqm_rssi(struct genl_info *info,
10879 				const s32 *thresholds, int n_thresholds,
10880 				u32 hysteresis)
10881 {
10882 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10883 	struct net_device *dev = info->user_ptr[1];
10884 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10885 	int i, err;
10886 	s32 prev = S32_MIN;
10887 
10888 	/* Check all values negative and sorted */
10889 	for (i = 0; i < n_thresholds; i++) {
10890 		if (thresholds[i] > 0 || thresholds[i] <= prev)
10891 			return -EINVAL;
10892 
10893 		prev = thresholds[i];
10894 	}
10895 
10896 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
10897 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
10898 		return -EOPNOTSUPP;
10899 
10900 	wdev_lock(wdev);
10901 	cfg80211_cqm_config_free(wdev);
10902 	wdev_unlock(wdev);
10903 
10904 	if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
10905 		if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
10906 			return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
10907 
10908 		return rdev_set_cqm_rssi_config(rdev, dev,
10909 						thresholds[0], hysteresis);
10910 	}
10911 
10912 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
10913 				     NL80211_EXT_FEATURE_CQM_RSSI_LIST))
10914 		return -EOPNOTSUPP;
10915 
10916 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
10917 		n_thresholds = 0;
10918 
10919 	wdev_lock(wdev);
10920 	if (n_thresholds) {
10921 		struct cfg80211_cqm_config *cqm_config;
10922 
10923 		cqm_config = kzalloc(sizeof(struct cfg80211_cqm_config) +
10924 				     n_thresholds * sizeof(s32), GFP_KERNEL);
10925 		if (!cqm_config) {
10926 			err = -ENOMEM;
10927 			goto unlock;
10928 		}
10929 
10930 		cqm_config->rssi_hyst = hysteresis;
10931 		cqm_config->n_rssi_thresholds = n_thresholds;
10932 		memcpy(cqm_config->rssi_thresholds, thresholds,
10933 		       n_thresholds * sizeof(s32));
10934 
10935 		wdev->cqm_config = cqm_config;
10936 	}
10937 
10938 	err = cfg80211_cqm_rssi_update(rdev, dev);
10939 
10940 unlock:
10941 	wdev_unlock(wdev);
10942 
10943 	return err;
10944 }
10945 
10946 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
10947 {
10948 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
10949 	struct nlattr *cqm;
10950 	int err;
10951 
10952 	cqm = info->attrs[NL80211_ATTR_CQM];
10953 	if (!cqm)
10954 		return -EINVAL;
10955 
10956 	err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
10957 					  nl80211_attr_cqm_policy,
10958 					  info->extack);
10959 	if (err)
10960 		return err;
10961 
10962 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
10963 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
10964 		const s32 *thresholds =
10965 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
10966 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
10967 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
10968 
10969 		if (len % 4)
10970 			return -EINVAL;
10971 
10972 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
10973 					    hysteresis);
10974 	}
10975 
10976 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
10977 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
10978 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
10979 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
10980 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
10981 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
10982 
10983 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
10984 	}
10985 
10986 	return -EINVAL;
10987 }
10988 
10989 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
10990 {
10991 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10992 	struct net_device *dev = info->user_ptr[1];
10993 	struct ocb_setup setup = {};
10994 	int err;
10995 
10996 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
10997 	if (err)
10998 		return err;
10999 
11000 	return cfg80211_join_ocb(rdev, dev, &setup);
11001 }
11002 
11003 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
11004 {
11005 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11006 	struct net_device *dev = info->user_ptr[1];
11007 
11008 	return cfg80211_leave_ocb(rdev, dev);
11009 }
11010 
11011 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
11012 {
11013 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11014 	struct net_device *dev = info->user_ptr[1];
11015 	struct mesh_config cfg;
11016 	struct mesh_setup setup;
11017 	int err;
11018 
11019 	/* start with default */
11020 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
11021 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
11022 
11023 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
11024 		/* and parse parameters if given */
11025 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
11026 		if (err)
11027 			return err;
11028 	}
11029 
11030 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
11031 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
11032 		return -EINVAL;
11033 
11034 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
11035 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
11036 
11037 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11038 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
11039 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11040 			return -EINVAL;
11041 
11042 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
11043 		setup.beacon_interval =
11044 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11045 
11046 		err = cfg80211_validate_beacon_int(rdev,
11047 						   NL80211_IFTYPE_MESH_POINT,
11048 						   setup.beacon_interval);
11049 		if (err)
11050 			return err;
11051 	}
11052 
11053 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
11054 		setup.dtim_period =
11055 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
11056 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
11057 			return -EINVAL;
11058 	}
11059 
11060 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
11061 		/* parse additional setup parameters if given */
11062 		err = nl80211_parse_mesh_setup(info, &setup);
11063 		if (err)
11064 			return err;
11065 	}
11066 
11067 	if (setup.user_mpm)
11068 		cfg.auto_open_plinks = false;
11069 
11070 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
11071 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
11072 		if (err)
11073 			return err;
11074 	} else {
11075 		/* __cfg80211_join_mesh() will sort it out */
11076 		setup.chandef.chan = NULL;
11077 	}
11078 
11079 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11080 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11081 		int n_rates =
11082 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11083 		struct ieee80211_supported_band *sband;
11084 
11085 		if (!setup.chandef.chan)
11086 			return -EINVAL;
11087 
11088 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
11089 
11090 		err = ieee80211_get_ratemask(sband, rates, n_rates,
11091 					     &setup.basic_rates);
11092 		if (err)
11093 			return err;
11094 	}
11095 
11096 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
11097 		err = nl80211_parse_tx_bitrate_mask(info, &setup.beacon_rate);
11098 		if (err)
11099 			return err;
11100 
11101 		if (!setup.chandef.chan)
11102 			return -EINVAL;
11103 
11104 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
11105 					      &setup.beacon_rate);
11106 		if (err)
11107 			return err;
11108 	}
11109 
11110 	setup.userspace_handles_dfs =
11111 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11112 
11113 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11114 		int r = validate_pae_over_nl80211(rdev, info);
11115 
11116 		if (r < 0)
11117 			return r;
11118 
11119 		setup.control_port_over_nl80211 = true;
11120 	}
11121 
11122 	wdev_lock(dev->ieee80211_ptr);
11123 	err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
11124 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
11125 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11126 	wdev_unlock(dev->ieee80211_ptr);
11127 
11128 	return err;
11129 }
11130 
11131 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
11132 {
11133 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11134 	struct net_device *dev = info->user_ptr[1];
11135 
11136 	return cfg80211_leave_mesh(rdev, dev);
11137 }
11138 
11139 #ifdef CONFIG_PM
11140 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
11141 					struct cfg80211_registered_device *rdev)
11142 {
11143 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
11144 	struct nlattr *nl_pats, *nl_pat;
11145 	int i, pat_len;
11146 
11147 	if (!wowlan->n_patterns)
11148 		return 0;
11149 
11150 	nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
11151 	if (!nl_pats)
11152 		return -ENOBUFS;
11153 
11154 	for (i = 0; i < wowlan->n_patterns; i++) {
11155 		nl_pat = nla_nest_start_noflag(msg, i + 1);
11156 		if (!nl_pat)
11157 			return -ENOBUFS;
11158 		pat_len = wowlan->patterns[i].pattern_len;
11159 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
11160 			    wowlan->patterns[i].mask) ||
11161 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
11162 			    wowlan->patterns[i].pattern) ||
11163 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
11164 				wowlan->patterns[i].pkt_offset))
11165 			return -ENOBUFS;
11166 		nla_nest_end(msg, nl_pat);
11167 	}
11168 	nla_nest_end(msg, nl_pats);
11169 
11170 	return 0;
11171 }
11172 
11173 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
11174 				   struct cfg80211_wowlan_tcp *tcp)
11175 {
11176 	struct nlattr *nl_tcp;
11177 
11178 	if (!tcp)
11179 		return 0;
11180 
11181 	nl_tcp = nla_nest_start_noflag(msg,
11182 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
11183 	if (!nl_tcp)
11184 		return -ENOBUFS;
11185 
11186 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
11187 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
11188 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
11189 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
11190 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
11191 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
11192 		    tcp->payload_len, tcp->payload) ||
11193 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
11194 			tcp->data_interval) ||
11195 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
11196 		    tcp->wake_len, tcp->wake_data) ||
11197 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
11198 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
11199 		return -ENOBUFS;
11200 
11201 	if (tcp->payload_seq.len &&
11202 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
11203 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
11204 		return -ENOBUFS;
11205 
11206 	if (tcp->payload_tok.len &&
11207 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
11208 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
11209 		    &tcp->payload_tok))
11210 		return -ENOBUFS;
11211 
11212 	nla_nest_end(msg, nl_tcp);
11213 
11214 	return 0;
11215 }
11216 
11217 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
11218 				  struct cfg80211_sched_scan_request *req)
11219 {
11220 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
11221 	int i;
11222 
11223 	if (!req)
11224 		return 0;
11225 
11226 	nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
11227 	if (!nd)
11228 		return -ENOBUFS;
11229 
11230 	if (req->n_scan_plans == 1 &&
11231 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
11232 			req->scan_plans[0].interval * 1000))
11233 		return -ENOBUFS;
11234 
11235 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
11236 		return -ENOBUFS;
11237 
11238 	if (req->relative_rssi_set) {
11239 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
11240 
11241 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
11242 			       req->relative_rssi))
11243 			return -ENOBUFS;
11244 
11245 		rssi_adjust.band = req->rssi_adjust.band;
11246 		rssi_adjust.delta = req->rssi_adjust.delta;
11247 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
11248 			    sizeof(rssi_adjust), &rssi_adjust))
11249 			return -ENOBUFS;
11250 	}
11251 
11252 	freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
11253 	if (!freqs)
11254 		return -ENOBUFS;
11255 
11256 	for (i = 0; i < req->n_channels; i++) {
11257 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
11258 			return -ENOBUFS;
11259 	}
11260 
11261 	nla_nest_end(msg, freqs);
11262 
11263 	if (req->n_match_sets) {
11264 		matches = nla_nest_start_noflag(msg,
11265 						NL80211_ATTR_SCHED_SCAN_MATCH);
11266 		if (!matches)
11267 			return -ENOBUFS;
11268 
11269 		for (i = 0; i < req->n_match_sets; i++) {
11270 			match = nla_nest_start_noflag(msg, i);
11271 			if (!match)
11272 				return -ENOBUFS;
11273 
11274 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
11275 				    req->match_sets[i].ssid.ssid_len,
11276 				    req->match_sets[i].ssid.ssid))
11277 				return -ENOBUFS;
11278 			nla_nest_end(msg, match);
11279 		}
11280 		nla_nest_end(msg, matches);
11281 	}
11282 
11283 	scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
11284 	if (!scan_plans)
11285 		return -ENOBUFS;
11286 
11287 	for (i = 0; i < req->n_scan_plans; i++) {
11288 		scan_plan = nla_nest_start_noflag(msg, i + 1);
11289 		if (!scan_plan)
11290 			return -ENOBUFS;
11291 
11292 		if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
11293 				req->scan_plans[i].interval) ||
11294 		    (req->scan_plans[i].iterations &&
11295 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
11296 				 req->scan_plans[i].iterations)))
11297 			return -ENOBUFS;
11298 		nla_nest_end(msg, scan_plan);
11299 	}
11300 	nla_nest_end(msg, scan_plans);
11301 
11302 	nla_nest_end(msg, nd);
11303 
11304 	return 0;
11305 }
11306 
11307 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
11308 {
11309 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11310 	struct sk_buff *msg;
11311 	void *hdr;
11312 	u32 size = NLMSG_DEFAULT_SIZE;
11313 
11314 	if (!rdev->wiphy.wowlan)
11315 		return -EOPNOTSUPP;
11316 
11317 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
11318 		/* adjust size to have room for all the data */
11319 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
11320 			rdev->wiphy.wowlan_config->tcp->payload_len +
11321 			rdev->wiphy.wowlan_config->tcp->wake_len +
11322 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
11323 	}
11324 
11325 	msg = nlmsg_new(size, GFP_KERNEL);
11326 	if (!msg)
11327 		return -ENOMEM;
11328 
11329 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11330 			     NL80211_CMD_GET_WOWLAN);
11331 	if (!hdr)
11332 		goto nla_put_failure;
11333 
11334 	if (rdev->wiphy.wowlan_config) {
11335 		struct nlattr *nl_wowlan;
11336 
11337 		nl_wowlan = nla_nest_start_noflag(msg,
11338 						  NL80211_ATTR_WOWLAN_TRIGGERS);
11339 		if (!nl_wowlan)
11340 			goto nla_put_failure;
11341 
11342 		if ((rdev->wiphy.wowlan_config->any &&
11343 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
11344 		    (rdev->wiphy.wowlan_config->disconnect &&
11345 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
11346 		    (rdev->wiphy.wowlan_config->magic_pkt &&
11347 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
11348 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
11349 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
11350 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
11351 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
11352 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
11353 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
11354 		    (rdev->wiphy.wowlan_config->rfkill_release &&
11355 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
11356 			goto nla_put_failure;
11357 
11358 		if (nl80211_send_wowlan_patterns(msg, rdev))
11359 			goto nla_put_failure;
11360 
11361 		if (nl80211_send_wowlan_tcp(msg,
11362 					    rdev->wiphy.wowlan_config->tcp))
11363 			goto nla_put_failure;
11364 
11365 		if (nl80211_send_wowlan_nd(
11366 			    msg,
11367 			    rdev->wiphy.wowlan_config->nd_config))
11368 			goto nla_put_failure;
11369 
11370 		nla_nest_end(msg, nl_wowlan);
11371 	}
11372 
11373 	genlmsg_end(msg, hdr);
11374 	return genlmsg_reply(msg, info);
11375 
11376 nla_put_failure:
11377 	nlmsg_free(msg);
11378 	return -ENOBUFS;
11379 }
11380 
11381 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
11382 				    struct nlattr *attr,
11383 				    struct cfg80211_wowlan *trig)
11384 {
11385 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
11386 	struct cfg80211_wowlan_tcp *cfg;
11387 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
11388 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
11389 	u32 size;
11390 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
11391 	int err, port;
11392 
11393 	if (!rdev->wiphy.wowlan->tcp)
11394 		return -EINVAL;
11395 
11396 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
11397 					  nl80211_wowlan_tcp_policy, NULL);
11398 	if (err)
11399 		return err;
11400 
11401 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
11402 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
11403 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
11404 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
11405 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
11406 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
11407 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
11408 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
11409 		return -EINVAL;
11410 
11411 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
11412 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
11413 		return -EINVAL;
11414 
11415 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
11416 			rdev->wiphy.wowlan->tcp->data_interval_max ||
11417 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
11418 		return -EINVAL;
11419 
11420 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
11421 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
11422 		return -EINVAL;
11423 
11424 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
11425 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
11426 		return -EINVAL;
11427 
11428 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
11429 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
11430 
11431 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
11432 		tokens_size = tokln - sizeof(*tok);
11433 
11434 		if (!tok->len || tokens_size % tok->len)
11435 			return -EINVAL;
11436 		if (!rdev->wiphy.wowlan->tcp->tok)
11437 			return -EINVAL;
11438 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
11439 			return -EINVAL;
11440 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
11441 			return -EINVAL;
11442 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
11443 			return -EINVAL;
11444 		if (tok->offset + tok->len > data_size)
11445 			return -EINVAL;
11446 	}
11447 
11448 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
11449 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
11450 		if (!rdev->wiphy.wowlan->tcp->seq)
11451 			return -EINVAL;
11452 		if (seq->len == 0 || seq->len > 4)
11453 			return -EINVAL;
11454 		if (seq->len + seq->offset > data_size)
11455 			return -EINVAL;
11456 	}
11457 
11458 	size = sizeof(*cfg);
11459 	size += data_size;
11460 	size += wake_size + wake_mask_size;
11461 	size += tokens_size;
11462 
11463 	cfg = kzalloc(size, GFP_KERNEL);
11464 	if (!cfg)
11465 		return -ENOMEM;
11466 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
11467 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
11468 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
11469 	       ETH_ALEN);
11470 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
11471 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
11472 	else
11473 		port = 0;
11474 #ifdef CONFIG_INET
11475 	/* allocate a socket and port for it and use it */
11476 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
11477 			    IPPROTO_TCP, &cfg->sock, 1);
11478 	if (err) {
11479 		kfree(cfg);
11480 		return err;
11481 	}
11482 	if (inet_csk_get_port(cfg->sock->sk, port)) {
11483 		sock_release(cfg->sock);
11484 		kfree(cfg);
11485 		return -EADDRINUSE;
11486 	}
11487 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
11488 #else
11489 	if (!port) {
11490 		kfree(cfg);
11491 		return -EINVAL;
11492 	}
11493 	cfg->src_port = port;
11494 #endif
11495 
11496 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
11497 	cfg->payload_len = data_size;
11498 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
11499 	memcpy((void *)cfg->payload,
11500 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
11501 	       data_size);
11502 	if (seq)
11503 		cfg->payload_seq = *seq;
11504 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
11505 	cfg->wake_len = wake_size;
11506 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
11507 	memcpy((void *)cfg->wake_data,
11508 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
11509 	       wake_size);
11510 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
11511 			 data_size + wake_size;
11512 	memcpy((void *)cfg->wake_mask,
11513 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
11514 	       wake_mask_size);
11515 	if (tok) {
11516 		cfg->tokens_size = tokens_size;
11517 		memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
11518 	}
11519 
11520 	trig->tcp = cfg;
11521 
11522 	return 0;
11523 }
11524 
11525 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
11526 				   const struct wiphy_wowlan_support *wowlan,
11527 				   struct nlattr *attr,
11528 				   struct cfg80211_wowlan *trig)
11529 {
11530 	struct nlattr **tb;
11531 	int err;
11532 
11533 	tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
11534 	if (!tb)
11535 		return -ENOMEM;
11536 
11537 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
11538 		err = -EOPNOTSUPP;
11539 		goto out;
11540 	}
11541 
11542 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
11543 					  nl80211_policy, NULL);
11544 	if (err)
11545 		goto out;
11546 
11547 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
11548 						   wowlan->max_nd_match_sets);
11549 	err = PTR_ERR_OR_ZERO(trig->nd_config);
11550 	if (err)
11551 		trig->nd_config = NULL;
11552 
11553 out:
11554 	kfree(tb);
11555 	return err;
11556 }
11557 
11558 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
11559 {
11560 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11561 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
11562 	struct cfg80211_wowlan new_triggers = {};
11563 	struct cfg80211_wowlan *ntrig;
11564 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
11565 	int err, i;
11566 	bool prev_enabled = rdev->wiphy.wowlan_config;
11567 	bool regular = false;
11568 
11569 	if (!wowlan)
11570 		return -EOPNOTSUPP;
11571 
11572 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
11573 		cfg80211_rdev_free_wowlan(rdev);
11574 		rdev->wiphy.wowlan_config = NULL;
11575 		goto set_wakeup;
11576 	}
11577 
11578 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
11579 					  info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
11580 					  nl80211_wowlan_policy, info->extack);
11581 	if (err)
11582 		return err;
11583 
11584 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
11585 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
11586 			return -EINVAL;
11587 		new_triggers.any = true;
11588 	}
11589 
11590 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
11591 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
11592 			return -EINVAL;
11593 		new_triggers.disconnect = true;
11594 		regular = true;
11595 	}
11596 
11597 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
11598 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
11599 			return -EINVAL;
11600 		new_triggers.magic_pkt = true;
11601 		regular = true;
11602 	}
11603 
11604 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
11605 		return -EINVAL;
11606 
11607 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
11608 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
11609 			return -EINVAL;
11610 		new_triggers.gtk_rekey_failure = true;
11611 		regular = true;
11612 	}
11613 
11614 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
11615 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
11616 			return -EINVAL;
11617 		new_triggers.eap_identity_req = true;
11618 		regular = true;
11619 	}
11620 
11621 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
11622 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
11623 			return -EINVAL;
11624 		new_triggers.four_way_handshake = true;
11625 		regular = true;
11626 	}
11627 
11628 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
11629 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
11630 			return -EINVAL;
11631 		new_triggers.rfkill_release = true;
11632 		regular = true;
11633 	}
11634 
11635 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
11636 		struct nlattr *pat;
11637 		int n_patterns = 0;
11638 		int rem, pat_len, mask_len, pkt_offset;
11639 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
11640 
11641 		regular = true;
11642 
11643 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
11644 				    rem)
11645 			n_patterns++;
11646 		if (n_patterns > wowlan->n_patterns)
11647 			return -EINVAL;
11648 
11649 		new_triggers.patterns = kcalloc(n_patterns,
11650 						sizeof(new_triggers.patterns[0]),
11651 						GFP_KERNEL);
11652 		if (!new_triggers.patterns)
11653 			return -ENOMEM;
11654 
11655 		new_triggers.n_patterns = n_patterns;
11656 		i = 0;
11657 
11658 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
11659 				    rem) {
11660 			u8 *mask_pat;
11661 
11662 			err = nla_parse_nested_deprecated(pat_tb,
11663 							  MAX_NL80211_PKTPAT,
11664 							  pat,
11665 							  nl80211_packet_pattern_policy,
11666 							  info->extack);
11667 			if (err)
11668 				goto error;
11669 
11670 			err = -EINVAL;
11671 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
11672 			    !pat_tb[NL80211_PKTPAT_PATTERN])
11673 				goto error;
11674 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
11675 			mask_len = DIV_ROUND_UP(pat_len, 8);
11676 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
11677 				goto error;
11678 			if (pat_len > wowlan->pattern_max_len ||
11679 			    pat_len < wowlan->pattern_min_len)
11680 				goto error;
11681 
11682 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
11683 				pkt_offset = 0;
11684 			else
11685 				pkt_offset = nla_get_u32(
11686 					pat_tb[NL80211_PKTPAT_OFFSET]);
11687 			if (pkt_offset > wowlan->max_pkt_offset)
11688 				goto error;
11689 			new_triggers.patterns[i].pkt_offset = pkt_offset;
11690 
11691 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
11692 			if (!mask_pat) {
11693 				err = -ENOMEM;
11694 				goto error;
11695 			}
11696 			new_triggers.patterns[i].mask = mask_pat;
11697 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
11698 			       mask_len);
11699 			mask_pat += mask_len;
11700 			new_triggers.patterns[i].pattern = mask_pat;
11701 			new_triggers.patterns[i].pattern_len = pat_len;
11702 			memcpy(mask_pat,
11703 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
11704 			       pat_len);
11705 			i++;
11706 		}
11707 	}
11708 
11709 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
11710 		regular = true;
11711 		err = nl80211_parse_wowlan_tcp(
11712 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
11713 			&new_triggers);
11714 		if (err)
11715 			goto error;
11716 	}
11717 
11718 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
11719 		regular = true;
11720 		err = nl80211_parse_wowlan_nd(
11721 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
11722 			&new_triggers);
11723 		if (err)
11724 			goto error;
11725 	}
11726 
11727 	/* The 'any' trigger means the device continues operating more or less
11728 	 * as in its normal operation mode and wakes up the host on most of the
11729 	 * normal interrupts (like packet RX, ...)
11730 	 * It therefore makes little sense to combine with the more constrained
11731 	 * wakeup trigger modes.
11732 	 */
11733 	if (new_triggers.any && regular) {
11734 		err = -EINVAL;
11735 		goto error;
11736 	}
11737 
11738 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
11739 	if (!ntrig) {
11740 		err = -ENOMEM;
11741 		goto error;
11742 	}
11743 	cfg80211_rdev_free_wowlan(rdev);
11744 	rdev->wiphy.wowlan_config = ntrig;
11745 
11746  set_wakeup:
11747 	if (rdev->ops->set_wakeup &&
11748 	    prev_enabled != !!rdev->wiphy.wowlan_config)
11749 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
11750 
11751 	return 0;
11752  error:
11753 	for (i = 0; i < new_triggers.n_patterns; i++)
11754 		kfree(new_triggers.patterns[i].mask);
11755 	kfree(new_triggers.patterns);
11756 	if (new_triggers.tcp && new_triggers.tcp->sock)
11757 		sock_release(new_triggers.tcp->sock);
11758 	kfree(new_triggers.tcp);
11759 	kfree(new_triggers.nd_config);
11760 	return err;
11761 }
11762 #endif
11763 
11764 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
11765 				       struct cfg80211_registered_device *rdev)
11766 {
11767 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
11768 	int i, j, pat_len;
11769 	struct cfg80211_coalesce_rules *rule;
11770 
11771 	if (!rdev->coalesce->n_rules)
11772 		return 0;
11773 
11774 	nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
11775 	if (!nl_rules)
11776 		return -ENOBUFS;
11777 
11778 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
11779 		nl_rule = nla_nest_start_noflag(msg, i + 1);
11780 		if (!nl_rule)
11781 			return -ENOBUFS;
11782 
11783 		rule = &rdev->coalesce->rules[i];
11784 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
11785 				rule->delay))
11786 			return -ENOBUFS;
11787 
11788 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
11789 				rule->condition))
11790 			return -ENOBUFS;
11791 
11792 		nl_pats = nla_nest_start_noflag(msg,
11793 						NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
11794 		if (!nl_pats)
11795 			return -ENOBUFS;
11796 
11797 		for (j = 0; j < rule->n_patterns; j++) {
11798 			nl_pat = nla_nest_start_noflag(msg, j + 1);
11799 			if (!nl_pat)
11800 				return -ENOBUFS;
11801 			pat_len = rule->patterns[j].pattern_len;
11802 			if (nla_put(msg, NL80211_PKTPAT_MASK,
11803 				    DIV_ROUND_UP(pat_len, 8),
11804 				    rule->patterns[j].mask) ||
11805 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
11806 				    rule->patterns[j].pattern) ||
11807 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
11808 					rule->patterns[j].pkt_offset))
11809 				return -ENOBUFS;
11810 			nla_nest_end(msg, nl_pat);
11811 		}
11812 		nla_nest_end(msg, nl_pats);
11813 		nla_nest_end(msg, nl_rule);
11814 	}
11815 	nla_nest_end(msg, nl_rules);
11816 
11817 	return 0;
11818 }
11819 
11820 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
11821 {
11822 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11823 	struct sk_buff *msg;
11824 	void *hdr;
11825 
11826 	if (!rdev->wiphy.coalesce)
11827 		return -EOPNOTSUPP;
11828 
11829 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11830 	if (!msg)
11831 		return -ENOMEM;
11832 
11833 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11834 			     NL80211_CMD_GET_COALESCE);
11835 	if (!hdr)
11836 		goto nla_put_failure;
11837 
11838 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
11839 		goto nla_put_failure;
11840 
11841 	genlmsg_end(msg, hdr);
11842 	return genlmsg_reply(msg, info);
11843 
11844 nla_put_failure:
11845 	nlmsg_free(msg);
11846 	return -ENOBUFS;
11847 }
11848 
11849 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
11850 {
11851 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
11852 	int i, j;
11853 	struct cfg80211_coalesce_rules *rule;
11854 
11855 	if (!coalesce)
11856 		return;
11857 
11858 	for (i = 0; i < coalesce->n_rules; i++) {
11859 		rule = &coalesce->rules[i];
11860 		for (j = 0; j < rule->n_patterns; j++)
11861 			kfree(rule->patterns[j].mask);
11862 		kfree(rule->patterns);
11863 	}
11864 	kfree(coalesce->rules);
11865 	kfree(coalesce);
11866 	rdev->coalesce = NULL;
11867 }
11868 
11869 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
11870 				       struct nlattr *rule,
11871 				       struct cfg80211_coalesce_rules *new_rule)
11872 {
11873 	int err, i;
11874 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
11875 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
11876 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
11877 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
11878 
11879 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
11880 					  rule, nl80211_coalesce_policy, NULL);
11881 	if (err)
11882 		return err;
11883 
11884 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
11885 		new_rule->delay =
11886 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
11887 	if (new_rule->delay > coalesce->max_delay)
11888 		return -EINVAL;
11889 
11890 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
11891 		new_rule->condition =
11892 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
11893 
11894 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
11895 		return -EINVAL;
11896 
11897 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
11898 			    rem)
11899 		n_patterns++;
11900 	if (n_patterns > coalesce->n_patterns)
11901 		return -EINVAL;
11902 
11903 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
11904 				     GFP_KERNEL);
11905 	if (!new_rule->patterns)
11906 		return -ENOMEM;
11907 
11908 	new_rule->n_patterns = n_patterns;
11909 	i = 0;
11910 
11911 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
11912 			    rem) {
11913 		u8 *mask_pat;
11914 
11915 		err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
11916 						  pat,
11917 						  nl80211_packet_pattern_policy,
11918 						  NULL);
11919 		if (err)
11920 			return err;
11921 
11922 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
11923 		    !pat_tb[NL80211_PKTPAT_PATTERN])
11924 			return -EINVAL;
11925 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
11926 		mask_len = DIV_ROUND_UP(pat_len, 8);
11927 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
11928 			return -EINVAL;
11929 		if (pat_len > coalesce->pattern_max_len ||
11930 		    pat_len < coalesce->pattern_min_len)
11931 			return -EINVAL;
11932 
11933 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
11934 			pkt_offset = 0;
11935 		else
11936 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
11937 		if (pkt_offset > coalesce->max_pkt_offset)
11938 			return -EINVAL;
11939 		new_rule->patterns[i].pkt_offset = pkt_offset;
11940 
11941 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
11942 		if (!mask_pat)
11943 			return -ENOMEM;
11944 
11945 		new_rule->patterns[i].mask = mask_pat;
11946 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
11947 		       mask_len);
11948 
11949 		mask_pat += mask_len;
11950 		new_rule->patterns[i].pattern = mask_pat;
11951 		new_rule->patterns[i].pattern_len = pat_len;
11952 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
11953 		       pat_len);
11954 		i++;
11955 	}
11956 
11957 	return 0;
11958 }
11959 
11960 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
11961 {
11962 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11963 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
11964 	struct cfg80211_coalesce new_coalesce = {};
11965 	struct cfg80211_coalesce *n_coalesce;
11966 	int err, rem_rule, n_rules = 0, i, j;
11967 	struct nlattr *rule;
11968 	struct cfg80211_coalesce_rules *tmp_rule;
11969 
11970 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
11971 		return -EOPNOTSUPP;
11972 
11973 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
11974 		cfg80211_rdev_free_coalesce(rdev);
11975 		rdev_set_coalesce(rdev, NULL);
11976 		return 0;
11977 	}
11978 
11979 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
11980 			    rem_rule)
11981 		n_rules++;
11982 	if (n_rules > coalesce->n_rules)
11983 		return -EINVAL;
11984 
11985 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
11986 				     GFP_KERNEL);
11987 	if (!new_coalesce.rules)
11988 		return -ENOMEM;
11989 
11990 	new_coalesce.n_rules = n_rules;
11991 	i = 0;
11992 
11993 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
11994 			    rem_rule) {
11995 		err = nl80211_parse_coalesce_rule(rdev, rule,
11996 						  &new_coalesce.rules[i]);
11997 		if (err)
11998 			goto error;
11999 
12000 		i++;
12001 	}
12002 
12003 	err = rdev_set_coalesce(rdev, &new_coalesce);
12004 	if (err)
12005 		goto error;
12006 
12007 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
12008 	if (!n_coalesce) {
12009 		err = -ENOMEM;
12010 		goto error;
12011 	}
12012 	cfg80211_rdev_free_coalesce(rdev);
12013 	rdev->coalesce = n_coalesce;
12014 
12015 	return 0;
12016 error:
12017 	for (i = 0; i < new_coalesce.n_rules; i++) {
12018 		tmp_rule = &new_coalesce.rules[i];
12019 		for (j = 0; j < tmp_rule->n_patterns; j++)
12020 			kfree(tmp_rule->patterns[j].mask);
12021 		kfree(tmp_rule->patterns);
12022 	}
12023 	kfree(new_coalesce.rules);
12024 
12025 	return err;
12026 }
12027 
12028 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
12029 {
12030 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12031 	struct net_device *dev = info->user_ptr[1];
12032 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12033 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
12034 	struct cfg80211_gtk_rekey_data rekey_data;
12035 	int err;
12036 
12037 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
12038 		return -EINVAL;
12039 
12040 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
12041 					  info->attrs[NL80211_ATTR_REKEY_DATA],
12042 					  nl80211_rekey_policy, info->extack);
12043 	if (err)
12044 		return err;
12045 
12046 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
12047 	    !tb[NL80211_REKEY_DATA_KCK])
12048 		return -EINVAL;
12049 	if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
12050 		return -ERANGE;
12051 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
12052 		return -ERANGE;
12053 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
12054 		return -ERANGE;
12055 
12056 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
12057 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
12058 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
12059 
12060 	wdev_lock(wdev);
12061 	if (!wdev->current_bss) {
12062 		err = -ENOTCONN;
12063 		goto out;
12064 	}
12065 
12066 	if (!rdev->ops->set_rekey_data) {
12067 		err = -EOPNOTSUPP;
12068 		goto out;
12069 	}
12070 
12071 	err = rdev_set_rekey_data(rdev, dev, &rekey_data);
12072  out:
12073 	wdev_unlock(wdev);
12074 	return err;
12075 }
12076 
12077 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
12078 					     struct genl_info *info)
12079 {
12080 	struct net_device *dev = info->user_ptr[1];
12081 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12082 
12083 	if (wdev->iftype != NL80211_IFTYPE_AP &&
12084 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
12085 		return -EINVAL;
12086 
12087 	if (wdev->ap_unexpected_nlportid)
12088 		return -EBUSY;
12089 
12090 	wdev->ap_unexpected_nlportid = info->snd_portid;
12091 	return 0;
12092 }
12093 
12094 static int nl80211_probe_client(struct sk_buff *skb,
12095 				struct genl_info *info)
12096 {
12097 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12098 	struct net_device *dev = info->user_ptr[1];
12099 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12100 	struct sk_buff *msg;
12101 	void *hdr;
12102 	const u8 *addr;
12103 	u64 cookie;
12104 	int err;
12105 
12106 	if (wdev->iftype != NL80211_IFTYPE_AP &&
12107 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
12108 		return -EOPNOTSUPP;
12109 
12110 	if (!info->attrs[NL80211_ATTR_MAC])
12111 		return -EINVAL;
12112 
12113 	if (!rdev->ops->probe_client)
12114 		return -EOPNOTSUPP;
12115 
12116 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12117 	if (!msg)
12118 		return -ENOMEM;
12119 
12120 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12121 			     NL80211_CMD_PROBE_CLIENT);
12122 	if (!hdr) {
12123 		err = -ENOBUFS;
12124 		goto free_msg;
12125 	}
12126 
12127 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12128 
12129 	err = rdev_probe_client(rdev, dev, addr, &cookie);
12130 	if (err)
12131 		goto free_msg;
12132 
12133 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12134 			      NL80211_ATTR_PAD))
12135 		goto nla_put_failure;
12136 
12137 	genlmsg_end(msg, hdr);
12138 
12139 	return genlmsg_reply(msg, info);
12140 
12141  nla_put_failure:
12142 	err = -ENOBUFS;
12143  free_msg:
12144 	nlmsg_free(msg);
12145 	return err;
12146 }
12147 
12148 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
12149 {
12150 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12151 	struct cfg80211_beacon_registration *reg, *nreg;
12152 	int rv;
12153 
12154 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
12155 		return -EOPNOTSUPP;
12156 
12157 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
12158 	if (!nreg)
12159 		return -ENOMEM;
12160 
12161 	/* First, check if already registered. */
12162 	spin_lock_bh(&rdev->beacon_registrations_lock);
12163 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
12164 		if (reg->nlportid == info->snd_portid) {
12165 			rv = -EALREADY;
12166 			goto out_err;
12167 		}
12168 	}
12169 	/* Add it to the list */
12170 	nreg->nlportid = info->snd_portid;
12171 	list_add(&nreg->list, &rdev->beacon_registrations);
12172 
12173 	spin_unlock_bh(&rdev->beacon_registrations_lock);
12174 
12175 	return 0;
12176 out_err:
12177 	spin_unlock_bh(&rdev->beacon_registrations_lock);
12178 	kfree(nreg);
12179 	return rv;
12180 }
12181 
12182 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
12183 {
12184 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12185 	struct wireless_dev *wdev = info->user_ptr[1];
12186 	int err;
12187 
12188 	if (!rdev->ops->start_p2p_device)
12189 		return -EOPNOTSUPP;
12190 
12191 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
12192 		return -EOPNOTSUPP;
12193 
12194 	if (wdev_running(wdev))
12195 		return 0;
12196 
12197 	if (rfkill_blocked(rdev->rfkill))
12198 		return -ERFKILL;
12199 
12200 	err = rdev_start_p2p_device(rdev, wdev);
12201 	if (err)
12202 		return err;
12203 
12204 	wdev->is_running = true;
12205 	rdev->opencount++;
12206 
12207 	return 0;
12208 }
12209 
12210 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
12211 {
12212 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12213 	struct wireless_dev *wdev = info->user_ptr[1];
12214 
12215 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
12216 		return -EOPNOTSUPP;
12217 
12218 	if (!rdev->ops->stop_p2p_device)
12219 		return -EOPNOTSUPP;
12220 
12221 	cfg80211_stop_p2p_device(rdev, wdev);
12222 
12223 	return 0;
12224 }
12225 
12226 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
12227 {
12228 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12229 	struct wireless_dev *wdev = info->user_ptr[1];
12230 	struct cfg80211_nan_conf conf = {};
12231 	int err;
12232 
12233 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12234 		return -EOPNOTSUPP;
12235 
12236 	if (wdev_running(wdev))
12237 		return -EEXIST;
12238 
12239 	if (rfkill_blocked(rdev->rfkill))
12240 		return -ERFKILL;
12241 
12242 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
12243 		return -EINVAL;
12244 
12245 	conf.master_pref =
12246 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
12247 
12248 	if (info->attrs[NL80211_ATTR_BANDS]) {
12249 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
12250 
12251 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
12252 			return -EOPNOTSUPP;
12253 
12254 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
12255 			return -EINVAL;
12256 
12257 		conf.bands = bands;
12258 	}
12259 
12260 	err = rdev_start_nan(rdev, wdev, &conf);
12261 	if (err)
12262 		return err;
12263 
12264 	wdev->is_running = true;
12265 	rdev->opencount++;
12266 
12267 	return 0;
12268 }
12269 
12270 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
12271 {
12272 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12273 	struct wireless_dev *wdev = info->user_ptr[1];
12274 
12275 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12276 		return -EOPNOTSUPP;
12277 
12278 	cfg80211_stop_nan(rdev, wdev);
12279 
12280 	return 0;
12281 }
12282 
12283 static int validate_nan_filter(struct nlattr *filter_attr)
12284 {
12285 	struct nlattr *attr;
12286 	int len = 0, n_entries = 0, rem;
12287 
12288 	nla_for_each_nested(attr, filter_attr, rem) {
12289 		len += nla_len(attr);
12290 		n_entries++;
12291 	}
12292 
12293 	if (len >= U8_MAX)
12294 		return -EINVAL;
12295 
12296 	return n_entries;
12297 }
12298 
12299 static int handle_nan_filter(struct nlattr *attr_filter,
12300 			     struct cfg80211_nan_func *func,
12301 			     bool tx)
12302 {
12303 	struct nlattr *attr;
12304 	int n_entries, rem, i;
12305 	struct cfg80211_nan_func_filter *filter;
12306 
12307 	n_entries = validate_nan_filter(attr_filter);
12308 	if (n_entries < 0)
12309 		return n_entries;
12310 
12311 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
12312 
12313 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
12314 	if (!filter)
12315 		return -ENOMEM;
12316 
12317 	i = 0;
12318 	nla_for_each_nested(attr, attr_filter, rem) {
12319 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
12320 		filter[i].len = nla_len(attr);
12321 		i++;
12322 	}
12323 	if (tx) {
12324 		func->num_tx_filters = n_entries;
12325 		func->tx_filters = filter;
12326 	} else {
12327 		func->num_rx_filters = n_entries;
12328 		func->rx_filters = filter;
12329 	}
12330 
12331 	return 0;
12332 }
12333 
12334 static int nl80211_nan_add_func(struct sk_buff *skb,
12335 				struct genl_info *info)
12336 {
12337 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12338 	struct wireless_dev *wdev = info->user_ptr[1];
12339 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
12340 	struct cfg80211_nan_func *func;
12341 	struct sk_buff *msg = NULL;
12342 	void *hdr = NULL;
12343 	int err = 0;
12344 
12345 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12346 		return -EOPNOTSUPP;
12347 
12348 	if (!wdev_running(wdev))
12349 		return -ENOTCONN;
12350 
12351 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
12352 		return -EINVAL;
12353 
12354 	err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
12355 					  info->attrs[NL80211_ATTR_NAN_FUNC],
12356 					  nl80211_nan_func_policy,
12357 					  info->extack);
12358 	if (err)
12359 		return err;
12360 
12361 	func = kzalloc(sizeof(*func), GFP_KERNEL);
12362 	if (!func)
12363 		return -ENOMEM;
12364 
12365 	func->cookie = cfg80211_assign_cookie(rdev);
12366 
12367 	if (!tb[NL80211_NAN_FUNC_TYPE] ||
12368 	    nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]) > NL80211_NAN_FUNC_MAX_TYPE) {
12369 		err = -EINVAL;
12370 		goto out;
12371 	}
12372 
12373 
12374 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
12375 
12376 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
12377 		err = -EINVAL;
12378 		goto out;
12379 	}
12380 
12381 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
12382 	       sizeof(func->service_id));
12383 
12384 	func->close_range =
12385 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
12386 
12387 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
12388 		func->serv_spec_info_len =
12389 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
12390 		func->serv_spec_info =
12391 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
12392 				func->serv_spec_info_len,
12393 				GFP_KERNEL);
12394 		if (!func->serv_spec_info) {
12395 			err = -ENOMEM;
12396 			goto out;
12397 		}
12398 	}
12399 
12400 	if (tb[NL80211_NAN_FUNC_TTL])
12401 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
12402 
12403 	switch (func->type) {
12404 	case NL80211_NAN_FUNC_PUBLISH:
12405 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
12406 			err = -EINVAL;
12407 			goto out;
12408 		}
12409 
12410 		func->publish_type =
12411 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
12412 		func->publish_bcast =
12413 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
12414 
12415 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
12416 			func->publish_bcast) {
12417 			err = -EINVAL;
12418 			goto out;
12419 		}
12420 		break;
12421 	case NL80211_NAN_FUNC_SUBSCRIBE:
12422 		func->subscribe_active =
12423 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
12424 		break;
12425 	case NL80211_NAN_FUNC_FOLLOW_UP:
12426 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
12427 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
12428 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
12429 			err = -EINVAL;
12430 			goto out;
12431 		}
12432 
12433 		func->followup_id =
12434 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
12435 		func->followup_reqid =
12436 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
12437 		memcpy(func->followup_dest.addr,
12438 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
12439 		       sizeof(func->followup_dest.addr));
12440 		if (func->ttl) {
12441 			err = -EINVAL;
12442 			goto out;
12443 		}
12444 		break;
12445 	default:
12446 		err = -EINVAL;
12447 		goto out;
12448 	}
12449 
12450 	if (tb[NL80211_NAN_FUNC_SRF]) {
12451 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
12452 
12453 		err = nla_parse_nested_deprecated(srf_tb,
12454 						  NL80211_NAN_SRF_ATTR_MAX,
12455 						  tb[NL80211_NAN_FUNC_SRF],
12456 						  nl80211_nan_srf_policy,
12457 						  info->extack);
12458 		if (err)
12459 			goto out;
12460 
12461 		func->srf_include =
12462 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
12463 
12464 		if (srf_tb[NL80211_NAN_SRF_BF]) {
12465 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
12466 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
12467 				err = -EINVAL;
12468 				goto out;
12469 			}
12470 
12471 			func->srf_bf_len =
12472 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
12473 			func->srf_bf =
12474 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
12475 					func->srf_bf_len, GFP_KERNEL);
12476 			if (!func->srf_bf) {
12477 				err = -ENOMEM;
12478 				goto out;
12479 			}
12480 
12481 			func->srf_bf_idx =
12482 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
12483 		} else {
12484 			struct nlattr *attr, *mac_attr =
12485 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
12486 			int n_entries, rem, i = 0;
12487 
12488 			if (!mac_attr) {
12489 				err = -EINVAL;
12490 				goto out;
12491 			}
12492 
12493 			n_entries = validate_acl_mac_addrs(mac_attr);
12494 			if (n_entries <= 0) {
12495 				err = -EINVAL;
12496 				goto out;
12497 			}
12498 
12499 			func->srf_num_macs = n_entries;
12500 			func->srf_macs =
12501 				kcalloc(n_entries, sizeof(*func->srf_macs),
12502 					GFP_KERNEL);
12503 			if (!func->srf_macs) {
12504 				err = -ENOMEM;
12505 				goto out;
12506 			}
12507 
12508 			nla_for_each_nested(attr, mac_attr, rem)
12509 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
12510 				       sizeof(*func->srf_macs));
12511 		}
12512 	}
12513 
12514 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
12515 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
12516 					func, true);
12517 		if (err)
12518 			goto out;
12519 	}
12520 
12521 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
12522 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
12523 					func, false);
12524 		if (err)
12525 			goto out;
12526 	}
12527 
12528 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12529 	if (!msg) {
12530 		err = -ENOMEM;
12531 		goto out;
12532 	}
12533 
12534 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12535 			     NL80211_CMD_ADD_NAN_FUNCTION);
12536 	/* This can't really happen - we just allocated 4KB */
12537 	if (WARN_ON(!hdr)) {
12538 		err = -ENOMEM;
12539 		goto out;
12540 	}
12541 
12542 	err = rdev_add_nan_func(rdev, wdev, func);
12543 out:
12544 	if (err < 0) {
12545 		cfg80211_free_nan_func(func);
12546 		nlmsg_free(msg);
12547 		return err;
12548 	}
12549 
12550 	/* propagate the instance id and cookie to userspace  */
12551 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
12552 			      NL80211_ATTR_PAD))
12553 		goto nla_put_failure;
12554 
12555 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
12556 	if (!func_attr)
12557 		goto nla_put_failure;
12558 
12559 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
12560 		       func->instance_id))
12561 		goto nla_put_failure;
12562 
12563 	nla_nest_end(msg, func_attr);
12564 
12565 	genlmsg_end(msg, hdr);
12566 	return genlmsg_reply(msg, info);
12567 
12568 nla_put_failure:
12569 	nlmsg_free(msg);
12570 	return -ENOBUFS;
12571 }
12572 
12573 static int nl80211_nan_del_func(struct sk_buff *skb,
12574 			       struct genl_info *info)
12575 {
12576 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12577 	struct wireless_dev *wdev = info->user_ptr[1];
12578 	u64 cookie;
12579 
12580 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12581 		return -EOPNOTSUPP;
12582 
12583 	if (!wdev_running(wdev))
12584 		return -ENOTCONN;
12585 
12586 	if (!info->attrs[NL80211_ATTR_COOKIE])
12587 		return -EINVAL;
12588 
12589 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12590 
12591 	rdev_del_nan_func(rdev, wdev, cookie);
12592 
12593 	return 0;
12594 }
12595 
12596 static int nl80211_nan_change_config(struct sk_buff *skb,
12597 				     struct genl_info *info)
12598 {
12599 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12600 	struct wireless_dev *wdev = info->user_ptr[1];
12601 	struct cfg80211_nan_conf conf = {};
12602 	u32 changed = 0;
12603 
12604 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12605 		return -EOPNOTSUPP;
12606 
12607 	if (!wdev_running(wdev))
12608 		return -ENOTCONN;
12609 
12610 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
12611 		conf.master_pref =
12612 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
12613 		if (conf.master_pref <= 1 || conf.master_pref == 255)
12614 			return -EINVAL;
12615 
12616 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
12617 	}
12618 
12619 	if (info->attrs[NL80211_ATTR_BANDS]) {
12620 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
12621 
12622 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
12623 			return -EOPNOTSUPP;
12624 
12625 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
12626 			return -EINVAL;
12627 
12628 		conf.bands = bands;
12629 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
12630 	}
12631 
12632 	if (!changed)
12633 		return -EINVAL;
12634 
12635 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
12636 }
12637 
12638 void cfg80211_nan_match(struct wireless_dev *wdev,
12639 			struct cfg80211_nan_match_params *match, gfp_t gfp)
12640 {
12641 	struct wiphy *wiphy = wdev->wiphy;
12642 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12643 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
12644 	struct sk_buff *msg;
12645 	void *hdr;
12646 
12647 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
12648 		return;
12649 
12650 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12651 	if (!msg)
12652 		return;
12653 
12654 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
12655 	if (!hdr) {
12656 		nlmsg_free(msg);
12657 		return;
12658 	}
12659 
12660 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12661 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12662 					 wdev->netdev->ifindex)) ||
12663 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
12664 			      NL80211_ATTR_PAD))
12665 		goto nla_put_failure;
12666 
12667 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
12668 			      NL80211_ATTR_PAD) ||
12669 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
12670 		goto nla_put_failure;
12671 
12672 	match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
12673 	if (!match_attr)
12674 		goto nla_put_failure;
12675 
12676 	local_func_attr = nla_nest_start_noflag(msg,
12677 						NL80211_NAN_MATCH_FUNC_LOCAL);
12678 	if (!local_func_attr)
12679 		goto nla_put_failure;
12680 
12681 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
12682 		goto nla_put_failure;
12683 
12684 	nla_nest_end(msg, local_func_attr);
12685 
12686 	peer_func_attr = nla_nest_start_noflag(msg,
12687 					       NL80211_NAN_MATCH_FUNC_PEER);
12688 	if (!peer_func_attr)
12689 		goto nla_put_failure;
12690 
12691 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
12692 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
12693 		goto nla_put_failure;
12694 
12695 	if (match->info && match->info_len &&
12696 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
12697 		    match->info))
12698 		goto nla_put_failure;
12699 
12700 	nla_nest_end(msg, peer_func_attr);
12701 	nla_nest_end(msg, match_attr);
12702 	genlmsg_end(msg, hdr);
12703 
12704 	if (!wdev->owner_nlportid)
12705 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
12706 					msg, 0, NL80211_MCGRP_NAN, gfp);
12707 	else
12708 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
12709 				wdev->owner_nlportid);
12710 
12711 	return;
12712 
12713 nla_put_failure:
12714 	nlmsg_free(msg);
12715 }
12716 EXPORT_SYMBOL(cfg80211_nan_match);
12717 
12718 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
12719 				  u8 inst_id,
12720 				  enum nl80211_nan_func_term_reason reason,
12721 				  u64 cookie, gfp_t gfp)
12722 {
12723 	struct wiphy *wiphy = wdev->wiphy;
12724 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12725 	struct sk_buff *msg;
12726 	struct nlattr *func_attr;
12727 	void *hdr;
12728 
12729 	if (WARN_ON(!inst_id))
12730 		return;
12731 
12732 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12733 	if (!msg)
12734 		return;
12735 
12736 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
12737 	if (!hdr) {
12738 		nlmsg_free(msg);
12739 		return;
12740 	}
12741 
12742 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12743 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12744 					 wdev->netdev->ifindex)) ||
12745 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
12746 			      NL80211_ATTR_PAD))
12747 		goto nla_put_failure;
12748 
12749 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12750 			      NL80211_ATTR_PAD))
12751 		goto nla_put_failure;
12752 
12753 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
12754 	if (!func_attr)
12755 		goto nla_put_failure;
12756 
12757 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
12758 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
12759 		goto nla_put_failure;
12760 
12761 	nla_nest_end(msg, func_attr);
12762 	genlmsg_end(msg, hdr);
12763 
12764 	if (!wdev->owner_nlportid)
12765 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
12766 					msg, 0, NL80211_MCGRP_NAN, gfp);
12767 	else
12768 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
12769 				wdev->owner_nlportid);
12770 
12771 	return;
12772 
12773 nla_put_failure:
12774 	nlmsg_free(msg);
12775 }
12776 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
12777 
12778 static int nl80211_get_protocol_features(struct sk_buff *skb,
12779 					 struct genl_info *info)
12780 {
12781 	void *hdr;
12782 	struct sk_buff *msg;
12783 
12784 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12785 	if (!msg)
12786 		return -ENOMEM;
12787 
12788 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12789 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
12790 	if (!hdr)
12791 		goto nla_put_failure;
12792 
12793 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
12794 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
12795 		goto nla_put_failure;
12796 
12797 	genlmsg_end(msg, hdr);
12798 	return genlmsg_reply(msg, info);
12799 
12800  nla_put_failure:
12801 	kfree_skb(msg);
12802 	return -ENOBUFS;
12803 }
12804 
12805 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
12806 {
12807 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12808 	struct cfg80211_update_ft_ies_params ft_params;
12809 	struct net_device *dev = info->user_ptr[1];
12810 
12811 	if (!rdev->ops->update_ft_ies)
12812 		return -EOPNOTSUPP;
12813 
12814 	if (!info->attrs[NL80211_ATTR_MDID] ||
12815 	    !info->attrs[NL80211_ATTR_IE])
12816 		return -EINVAL;
12817 
12818 	memset(&ft_params, 0, sizeof(ft_params));
12819 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
12820 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
12821 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
12822 
12823 	return rdev_update_ft_ies(rdev, dev, &ft_params);
12824 }
12825 
12826 static int nl80211_crit_protocol_start(struct sk_buff *skb,
12827 				       struct genl_info *info)
12828 {
12829 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12830 	struct wireless_dev *wdev = info->user_ptr[1];
12831 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
12832 	u16 duration;
12833 	int ret;
12834 
12835 	if (!rdev->ops->crit_proto_start)
12836 		return -EOPNOTSUPP;
12837 
12838 	if (WARN_ON(!rdev->ops->crit_proto_stop))
12839 		return -EINVAL;
12840 
12841 	if (rdev->crit_proto_nlportid)
12842 		return -EBUSY;
12843 
12844 	/* determine protocol if provided */
12845 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
12846 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
12847 
12848 	if (proto >= NUM_NL80211_CRIT_PROTO)
12849 		return -EINVAL;
12850 
12851 	/* timeout must be provided */
12852 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
12853 		return -EINVAL;
12854 
12855 	duration =
12856 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
12857 
12858 	if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
12859 		return -ERANGE;
12860 
12861 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
12862 	if (!ret)
12863 		rdev->crit_proto_nlportid = info->snd_portid;
12864 
12865 	return ret;
12866 }
12867 
12868 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
12869 				      struct genl_info *info)
12870 {
12871 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12872 	struct wireless_dev *wdev = info->user_ptr[1];
12873 
12874 	if (!rdev->ops->crit_proto_stop)
12875 		return -EOPNOTSUPP;
12876 
12877 	if (rdev->crit_proto_nlportid) {
12878 		rdev->crit_proto_nlportid = 0;
12879 		rdev_crit_proto_stop(rdev, wdev);
12880 	}
12881 	return 0;
12882 }
12883 
12884 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
12885 				       struct nlattr *attr,
12886 				       struct netlink_ext_ack *extack)
12887 {
12888 	if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
12889 		if (attr->nla_type & NLA_F_NESTED) {
12890 			NL_SET_ERR_MSG_ATTR(extack, attr,
12891 					    "unexpected nested data");
12892 			return -EINVAL;
12893 		}
12894 
12895 		return 0;
12896 	}
12897 
12898 	if (!(attr->nla_type & NLA_F_NESTED)) {
12899 		NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
12900 		return -EINVAL;
12901 	}
12902 
12903 	return nl80211_validate_nested(attr, vcmd->maxattr, vcmd->policy,
12904 				       extack);
12905 }
12906 
12907 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
12908 {
12909 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12910 	struct wireless_dev *wdev =
12911 		__cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
12912 	int i, err;
12913 	u32 vid, subcmd;
12914 
12915 	if (!rdev->wiphy.vendor_commands)
12916 		return -EOPNOTSUPP;
12917 
12918 	if (IS_ERR(wdev)) {
12919 		err = PTR_ERR(wdev);
12920 		if (err != -EINVAL)
12921 			return err;
12922 		wdev = NULL;
12923 	} else if (wdev->wiphy != &rdev->wiphy) {
12924 		return -EINVAL;
12925 	}
12926 
12927 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
12928 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
12929 		return -EINVAL;
12930 
12931 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
12932 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
12933 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
12934 		const struct wiphy_vendor_command *vcmd;
12935 		void *data = NULL;
12936 		int len = 0;
12937 
12938 		vcmd = &rdev->wiphy.vendor_commands[i];
12939 
12940 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
12941 			continue;
12942 
12943 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
12944 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
12945 			if (!wdev)
12946 				return -EINVAL;
12947 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
12948 			    !wdev->netdev)
12949 				return -EINVAL;
12950 
12951 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
12952 				if (!wdev_running(wdev))
12953 					return -ENETDOWN;
12954 			}
12955 
12956 			if (!vcmd->doit)
12957 				return -EOPNOTSUPP;
12958 		} else {
12959 			wdev = NULL;
12960 		}
12961 
12962 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
12963 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
12964 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
12965 
12966 			err = nl80211_vendor_check_policy(vcmd,
12967 					info->attrs[NL80211_ATTR_VENDOR_DATA],
12968 					info->extack);
12969 			if (err)
12970 				return err;
12971 		}
12972 
12973 		rdev->cur_cmd_info = info;
12974 		err = vcmd->doit(&rdev->wiphy, wdev, data, len);
12975 		rdev->cur_cmd_info = NULL;
12976 		return err;
12977 	}
12978 
12979 	return -EOPNOTSUPP;
12980 }
12981 
12982 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
12983 				       struct netlink_callback *cb,
12984 				       struct cfg80211_registered_device **rdev,
12985 				       struct wireless_dev **wdev)
12986 {
12987 	struct nlattr **attrbuf;
12988 	u32 vid, subcmd;
12989 	unsigned int i;
12990 	int vcmd_idx = -1;
12991 	int err;
12992 	void *data = NULL;
12993 	unsigned int data_len = 0;
12994 
12995 	if (cb->args[0]) {
12996 		/* subtract the 1 again here */
12997 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
12998 		struct wireless_dev *tmp;
12999 
13000 		if (!wiphy)
13001 			return -ENODEV;
13002 		*rdev = wiphy_to_rdev(wiphy);
13003 		*wdev = NULL;
13004 
13005 		if (cb->args[1]) {
13006 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
13007 				if (tmp->identifier == cb->args[1] - 1) {
13008 					*wdev = tmp;
13009 					break;
13010 				}
13011 			}
13012 		}
13013 
13014 		/* keep rtnl locked in successful case */
13015 		return 0;
13016 	}
13017 
13018 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
13019 	if (!attrbuf)
13020 		return -ENOMEM;
13021 
13022 	err = nlmsg_parse_deprecated(cb->nlh,
13023 				     GENL_HDRLEN + nl80211_fam.hdrsize,
13024 				     attrbuf, nl80211_fam.maxattr,
13025 				     nl80211_policy, NULL);
13026 	if (err)
13027 		goto out;
13028 
13029 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
13030 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
13031 		err = -EINVAL;
13032 		goto out;
13033 	}
13034 
13035 	*wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk), attrbuf);
13036 	if (IS_ERR(*wdev))
13037 		*wdev = NULL;
13038 
13039 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
13040 	if (IS_ERR(*rdev)) {
13041 		err = PTR_ERR(*rdev);
13042 		goto out;
13043 	}
13044 
13045 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
13046 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
13047 
13048 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
13049 		const struct wiphy_vendor_command *vcmd;
13050 
13051 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
13052 
13053 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
13054 			continue;
13055 
13056 		if (!vcmd->dumpit) {
13057 			err = -EOPNOTSUPP;
13058 			goto out;
13059 		}
13060 
13061 		vcmd_idx = i;
13062 		break;
13063 	}
13064 
13065 	if (vcmd_idx < 0) {
13066 		err = -EOPNOTSUPP;
13067 		goto out;
13068 	}
13069 
13070 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
13071 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
13072 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
13073 
13074 		err = nl80211_vendor_check_policy(
13075 				&(*rdev)->wiphy.vendor_commands[vcmd_idx],
13076 				attrbuf[NL80211_ATTR_VENDOR_DATA],
13077 				cb->extack);
13078 		if (err)
13079 			goto out;
13080 	}
13081 
13082 	/* 0 is the first index - add 1 to parse only once */
13083 	cb->args[0] = (*rdev)->wiphy_idx + 1;
13084 	/* add 1 to know if it was NULL */
13085 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
13086 	cb->args[2] = vcmd_idx;
13087 	cb->args[3] = (unsigned long)data;
13088 	cb->args[4] = data_len;
13089 
13090 	/* keep rtnl locked in successful case */
13091 	err = 0;
13092 out:
13093 	kfree(attrbuf);
13094 	return err;
13095 }
13096 
13097 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
13098 				   struct netlink_callback *cb)
13099 {
13100 	struct cfg80211_registered_device *rdev;
13101 	struct wireless_dev *wdev;
13102 	unsigned int vcmd_idx;
13103 	const struct wiphy_vendor_command *vcmd;
13104 	void *data;
13105 	int data_len;
13106 	int err;
13107 	struct nlattr *vendor_data;
13108 
13109 	rtnl_lock();
13110 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
13111 	if (err)
13112 		goto out;
13113 
13114 	vcmd_idx = cb->args[2];
13115 	data = (void *)cb->args[3];
13116 	data_len = cb->args[4];
13117 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
13118 
13119 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
13120 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
13121 		if (!wdev) {
13122 			err = -EINVAL;
13123 			goto out;
13124 		}
13125 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
13126 		    !wdev->netdev) {
13127 			err = -EINVAL;
13128 			goto out;
13129 		}
13130 
13131 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
13132 			if (!wdev_running(wdev)) {
13133 				err = -ENETDOWN;
13134 				goto out;
13135 			}
13136 		}
13137 	}
13138 
13139 	while (1) {
13140 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
13141 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
13142 					   NL80211_CMD_VENDOR);
13143 		if (!hdr)
13144 			break;
13145 
13146 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13147 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
13148 					       wdev_id(wdev),
13149 					       NL80211_ATTR_PAD))) {
13150 			genlmsg_cancel(skb, hdr);
13151 			break;
13152 		}
13153 
13154 		vendor_data = nla_nest_start_noflag(skb,
13155 						    NL80211_ATTR_VENDOR_DATA);
13156 		if (!vendor_data) {
13157 			genlmsg_cancel(skb, hdr);
13158 			break;
13159 		}
13160 
13161 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
13162 				   (unsigned long *)&cb->args[5]);
13163 		nla_nest_end(skb, vendor_data);
13164 
13165 		if (err == -ENOBUFS || err == -ENOENT) {
13166 			genlmsg_cancel(skb, hdr);
13167 			break;
13168 		} else if (err) {
13169 			genlmsg_cancel(skb, hdr);
13170 			goto out;
13171 		}
13172 
13173 		genlmsg_end(skb, hdr);
13174 	}
13175 
13176 	err = skb->len;
13177  out:
13178 	rtnl_unlock();
13179 	return err;
13180 }
13181 
13182 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
13183 					   enum nl80211_commands cmd,
13184 					   enum nl80211_attrs attr,
13185 					   int approxlen)
13186 {
13187 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13188 
13189 	if (WARN_ON(!rdev->cur_cmd_info))
13190 		return NULL;
13191 
13192 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
13193 					   rdev->cur_cmd_info->snd_portid,
13194 					   rdev->cur_cmd_info->snd_seq,
13195 					   cmd, attr, NULL, GFP_KERNEL);
13196 }
13197 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
13198 
13199 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
13200 {
13201 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
13202 	void *hdr = ((void **)skb->cb)[1];
13203 	struct nlattr *data = ((void **)skb->cb)[2];
13204 
13205 	/* clear CB data for netlink core to own from now on */
13206 	memset(skb->cb, 0, sizeof(skb->cb));
13207 
13208 	if (WARN_ON(!rdev->cur_cmd_info)) {
13209 		kfree_skb(skb);
13210 		return -EINVAL;
13211 	}
13212 
13213 	nla_nest_end(skb, data);
13214 	genlmsg_end(skb, hdr);
13215 	return genlmsg_reply(skb, rdev->cur_cmd_info);
13216 }
13217 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
13218 
13219 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
13220 {
13221 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13222 
13223 	if (WARN_ON(!rdev->cur_cmd_info))
13224 		return 0;
13225 
13226 	return rdev->cur_cmd_info->snd_portid;
13227 }
13228 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
13229 
13230 static int nl80211_set_qos_map(struct sk_buff *skb,
13231 			       struct genl_info *info)
13232 {
13233 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13234 	struct cfg80211_qos_map *qos_map = NULL;
13235 	struct net_device *dev = info->user_ptr[1];
13236 	u8 *pos, len, num_des, des_len, des;
13237 	int ret;
13238 
13239 	if (!rdev->ops->set_qos_map)
13240 		return -EOPNOTSUPP;
13241 
13242 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
13243 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
13244 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
13245 
13246 		if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
13247 		    len > IEEE80211_QOS_MAP_LEN_MAX)
13248 			return -EINVAL;
13249 
13250 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
13251 		if (!qos_map)
13252 			return -ENOMEM;
13253 
13254 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
13255 		if (num_des) {
13256 			des_len = num_des *
13257 				sizeof(struct cfg80211_dscp_exception);
13258 			memcpy(qos_map->dscp_exception, pos, des_len);
13259 			qos_map->num_des = num_des;
13260 			for (des = 0; des < num_des; des++) {
13261 				if (qos_map->dscp_exception[des].up > 7) {
13262 					kfree(qos_map);
13263 					return -EINVAL;
13264 				}
13265 			}
13266 			pos += des_len;
13267 		}
13268 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
13269 	}
13270 
13271 	wdev_lock(dev->ieee80211_ptr);
13272 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
13273 	if (!ret)
13274 		ret = rdev_set_qos_map(rdev, dev, qos_map);
13275 	wdev_unlock(dev->ieee80211_ptr);
13276 
13277 	kfree(qos_map);
13278 	return ret;
13279 }
13280 
13281 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
13282 {
13283 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13284 	struct net_device *dev = info->user_ptr[1];
13285 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13286 	const u8 *peer;
13287 	u8 tsid, up;
13288 	u16 admitted_time = 0;
13289 	int err;
13290 
13291 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
13292 		return -EOPNOTSUPP;
13293 
13294 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
13295 	    !info->attrs[NL80211_ATTR_USER_PRIO])
13296 		return -EINVAL;
13297 
13298 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
13299 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
13300 
13301 	/* WMM uses TIDs 0-7 even for TSPEC */
13302 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
13303 		/* TODO: handle 802.11 TSPEC/admission control
13304 		 * need more attributes for that (e.g. BA session requirement);
13305 		 * change the WMM adminssion test above to allow both then
13306 		 */
13307 		return -EINVAL;
13308 	}
13309 
13310 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
13311 
13312 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
13313 		admitted_time =
13314 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
13315 		if (!admitted_time)
13316 			return -EINVAL;
13317 	}
13318 
13319 	wdev_lock(wdev);
13320 	switch (wdev->iftype) {
13321 	case NL80211_IFTYPE_STATION:
13322 	case NL80211_IFTYPE_P2P_CLIENT:
13323 		if (wdev->current_bss)
13324 			break;
13325 		err = -ENOTCONN;
13326 		goto out;
13327 	default:
13328 		err = -EOPNOTSUPP;
13329 		goto out;
13330 	}
13331 
13332 	err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
13333 
13334  out:
13335 	wdev_unlock(wdev);
13336 	return err;
13337 }
13338 
13339 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
13340 {
13341 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13342 	struct net_device *dev = info->user_ptr[1];
13343 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13344 	const u8 *peer;
13345 	u8 tsid;
13346 	int err;
13347 
13348 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
13349 		return -EINVAL;
13350 
13351 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
13352 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
13353 
13354 	wdev_lock(wdev);
13355 	err = rdev_del_tx_ts(rdev, dev, tsid, peer);
13356 	wdev_unlock(wdev);
13357 
13358 	return err;
13359 }
13360 
13361 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
13362 				       struct genl_info *info)
13363 {
13364 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13365 	struct net_device *dev = info->user_ptr[1];
13366 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13367 	struct cfg80211_chan_def chandef = {};
13368 	const u8 *addr;
13369 	u8 oper_class;
13370 	int err;
13371 
13372 	if (!rdev->ops->tdls_channel_switch ||
13373 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
13374 		return -EOPNOTSUPP;
13375 
13376 	switch (dev->ieee80211_ptr->iftype) {
13377 	case NL80211_IFTYPE_STATION:
13378 	case NL80211_IFTYPE_P2P_CLIENT:
13379 		break;
13380 	default:
13381 		return -EOPNOTSUPP;
13382 	}
13383 
13384 	if (!info->attrs[NL80211_ATTR_MAC] ||
13385 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
13386 		return -EINVAL;
13387 
13388 	err = nl80211_parse_chandef(rdev, info, &chandef);
13389 	if (err)
13390 		return err;
13391 
13392 	/*
13393 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
13394 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
13395 	 * specification is not defined for them.
13396 	 */
13397 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
13398 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
13399 	    chandef.width != NL80211_CHAN_WIDTH_20)
13400 		return -EINVAL;
13401 
13402 	/* we will be active on the TDLS link */
13403 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
13404 					   wdev->iftype))
13405 		return -EINVAL;
13406 
13407 	/* don't allow switching to DFS channels */
13408 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
13409 		return -EINVAL;
13410 
13411 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
13412 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
13413 
13414 	wdev_lock(wdev);
13415 	err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
13416 	wdev_unlock(wdev);
13417 
13418 	return err;
13419 }
13420 
13421 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
13422 					      struct genl_info *info)
13423 {
13424 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13425 	struct net_device *dev = info->user_ptr[1];
13426 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13427 	const u8 *addr;
13428 
13429 	if (!rdev->ops->tdls_channel_switch ||
13430 	    !rdev->ops->tdls_cancel_channel_switch ||
13431 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
13432 		return -EOPNOTSUPP;
13433 
13434 	switch (dev->ieee80211_ptr->iftype) {
13435 	case NL80211_IFTYPE_STATION:
13436 	case NL80211_IFTYPE_P2P_CLIENT:
13437 		break;
13438 	default:
13439 		return -EOPNOTSUPP;
13440 	}
13441 
13442 	if (!info->attrs[NL80211_ATTR_MAC])
13443 		return -EINVAL;
13444 
13445 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
13446 
13447 	wdev_lock(wdev);
13448 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
13449 	wdev_unlock(wdev);
13450 
13451 	return 0;
13452 }
13453 
13454 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
13455 					    struct genl_info *info)
13456 {
13457 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13458 	struct net_device *dev = info->user_ptr[1];
13459 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13460 	const struct nlattr *nla;
13461 	bool enabled;
13462 
13463 	if (!rdev->ops->set_multicast_to_unicast)
13464 		return -EOPNOTSUPP;
13465 
13466 	if (wdev->iftype != NL80211_IFTYPE_AP &&
13467 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
13468 		return -EOPNOTSUPP;
13469 
13470 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
13471 	enabled = nla_get_flag(nla);
13472 
13473 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
13474 }
13475 
13476 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
13477 {
13478 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13479 	struct net_device *dev = info->user_ptr[1];
13480 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13481 	struct cfg80211_pmk_conf pmk_conf = {};
13482 	int ret;
13483 
13484 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
13485 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
13486 		return -EOPNOTSUPP;
13487 
13488 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
13489 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
13490 		return -EOPNOTSUPP;
13491 
13492 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
13493 		return -EINVAL;
13494 
13495 	wdev_lock(wdev);
13496 	if (!wdev->current_bss) {
13497 		ret = -ENOTCONN;
13498 		goto out;
13499 	}
13500 
13501 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
13502 	if (memcmp(pmk_conf.aa, wdev->current_bss->pub.bssid, ETH_ALEN)) {
13503 		ret = -EINVAL;
13504 		goto out;
13505 	}
13506 
13507 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
13508 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
13509 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
13510 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
13511 		ret = -EINVAL;
13512 		goto out;
13513 	}
13514 
13515 	if (info->attrs[NL80211_ATTR_PMKR0_NAME]) {
13516 		int r0_name_len = nla_len(info->attrs[NL80211_ATTR_PMKR0_NAME]);
13517 
13518 		if (r0_name_len != WLAN_PMK_NAME_LEN) {
13519 			ret = -EINVAL;
13520 			goto out;
13521 		}
13522 
13523 		pmk_conf.pmk_r0_name =
13524 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
13525 	}
13526 
13527 	ret = rdev_set_pmk(rdev, dev, &pmk_conf);
13528 out:
13529 	wdev_unlock(wdev);
13530 	return ret;
13531 }
13532 
13533 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
13534 {
13535 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13536 	struct net_device *dev = info->user_ptr[1];
13537 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13538 	const u8 *aa;
13539 	int ret;
13540 
13541 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
13542 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
13543 		return -EOPNOTSUPP;
13544 
13545 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
13546 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
13547 		return -EOPNOTSUPP;
13548 
13549 	if (!info->attrs[NL80211_ATTR_MAC])
13550 		return -EINVAL;
13551 
13552 	wdev_lock(wdev);
13553 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
13554 	ret = rdev_del_pmk(rdev, dev, aa);
13555 	wdev_unlock(wdev);
13556 
13557 	return ret;
13558 }
13559 
13560 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
13561 {
13562 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13563 	struct net_device *dev = info->user_ptr[1];
13564 	struct cfg80211_external_auth_params params;
13565 
13566 	if (!rdev->ops->external_auth)
13567 		return -EOPNOTSUPP;
13568 
13569 	if (!info->attrs[NL80211_ATTR_SSID] &&
13570 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
13571 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
13572 		return -EINVAL;
13573 
13574 	if (!info->attrs[NL80211_ATTR_BSSID])
13575 		return -EINVAL;
13576 
13577 	if (!info->attrs[NL80211_ATTR_STATUS_CODE])
13578 		return -EINVAL;
13579 
13580 	memset(&params, 0, sizeof(params));
13581 
13582 	if (info->attrs[NL80211_ATTR_SSID]) {
13583 		params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
13584 		if (params.ssid.ssid_len == 0 ||
13585 		    params.ssid.ssid_len > IEEE80211_MAX_SSID_LEN)
13586 			return -EINVAL;
13587 		memcpy(params.ssid.ssid,
13588 		       nla_data(info->attrs[NL80211_ATTR_SSID]),
13589 		       params.ssid.ssid_len);
13590 	}
13591 
13592 	memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
13593 	       ETH_ALEN);
13594 
13595 	params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
13596 
13597 	if (info->attrs[NL80211_ATTR_PMKID])
13598 		params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
13599 
13600 	return rdev_external_auth(rdev, dev, &params);
13601 }
13602 
13603 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
13604 {
13605 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13606 	struct net_device *dev = info->user_ptr[1];
13607 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13608 	const u8 *buf;
13609 	size_t len;
13610 	u8 *dest;
13611 	u16 proto;
13612 	bool noencrypt;
13613 	int err;
13614 
13615 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
13616 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
13617 		return -EOPNOTSUPP;
13618 
13619 	if (!rdev->ops->tx_control_port)
13620 		return -EOPNOTSUPP;
13621 
13622 	if (!info->attrs[NL80211_ATTR_FRAME] ||
13623 	    !info->attrs[NL80211_ATTR_MAC] ||
13624 	    !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
13625 		GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
13626 		return -EINVAL;
13627 	}
13628 
13629 	wdev_lock(wdev);
13630 
13631 	switch (wdev->iftype) {
13632 	case NL80211_IFTYPE_AP:
13633 	case NL80211_IFTYPE_P2P_GO:
13634 	case NL80211_IFTYPE_MESH_POINT:
13635 		break;
13636 	case NL80211_IFTYPE_ADHOC:
13637 	case NL80211_IFTYPE_STATION:
13638 	case NL80211_IFTYPE_P2P_CLIENT:
13639 		if (wdev->current_bss)
13640 			break;
13641 		err = -ENOTCONN;
13642 		goto out;
13643 	default:
13644 		err = -EOPNOTSUPP;
13645 		goto out;
13646 	}
13647 
13648 	wdev_unlock(wdev);
13649 
13650 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
13651 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
13652 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
13653 	proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
13654 	noencrypt =
13655 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
13656 
13657 	return rdev_tx_control_port(rdev, dev, buf, len,
13658 				    dest, cpu_to_be16(proto), noencrypt);
13659 
13660  out:
13661 	wdev_unlock(wdev);
13662 	return err;
13663 }
13664 
13665 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
13666 					   struct genl_info *info)
13667 {
13668 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13669 	struct net_device *dev = info->user_ptr[1];
13670 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13671 	struct cfg80211_ftm_responder_stats ftm_stats = {};
13672 	struct sk_buff *msg;
13673 	void *hdr;
13674 	struct nlattr *ftm_stats_attr;
13675 	int err;
13676 
13677 	if (wdev->iftype != NL80211_IFTYPE_AP || !wdev->beacon_interval)
13678 		return -EOPNOTSUPP;
13679 
13680 	err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
13681 	if (err)
13682 		return err;
13683 
13684 	if (!ftm_stats.filled)
13685 		return -ENODATA;
13686 
13687 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13688 	if (!msg)
13689 		return -ENOMEM;
13690 
13691 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13692 			     NL80211_CMD_GET_FTM_RESPONDER_STATS);
13693 	if (!hdr)
13694 		goto nla_put_failure;
13695 
13696 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
13697 		goto nla_put_failure;
13698 
13699 	ftm_stats_attr = nla_nest_start_noflag(msg,
13700 					       NL80211_ATTR_FTM_RESPONDER_STATS);
13701 	if (!ftm_stats_attr)
13702 		goto nla_put_failure;
13703 
13704 #define SET_FTM(field, name, type)					 \
13705 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
13706 	    nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,		 \
13707 			     ftm_stats.field))				 \
13708 		goto nla_put_failure; } while (0)
13709 #define SET_FTM_U64(field, name)					 \
13710 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
13711 	    nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,		 \
13712 			      ftm_stats.field, NL80211_FTM_STATS_PAD))	 \
13713 		goto nla_put_failure; } while (0)
13714 
13715 	SET_FTM(success_num, SUCCESS_NUM, u32);
13716 	SET_FTM(partial_num, PARTIAL_NUM, u32);
13717 	SET_FTM(failed_num, FAILED_NUM, u32);
13718 	SET_FTM(asap_num, ASAP_NUM, u32);
13719 	SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
13720 	SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
13721 	SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
13722 	SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
13723 	SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
13724 #undef SET_FTM
13725 
13726 	nla_nest_end(msg, ftm_stats_attr);
13727 
13728 	genlmsg_end(msg, hdr);
13729 	return genlmsg_reply(msg, info);
13730 
13731 nla_put_failure:
13732 	nlmsg_free(msg);
13733 	return -ENOBUFS;
13734 }
13735 
13736 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
13737 {
13738 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13739 	struct cfg80211_update_owe_info owe_info;
13740 	struct net_device *dev = info->user_ptr[1];
13741 
13742 	if (!rdev->ops->update_owe_info)
13743 		return -EOPNOTSUPP;
13744 
13745 	if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
13746 	    !info->attrs[NL80211_ATTR_MAC])
13747 		return -EINVAL;
13748 
13749 	memset(&owe_info, 0, sizeof(owe_info));
13750 	owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
13751 	nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
13752 
13753 	if (info->attrs[NL80211_ATTR_IE]) {
13754 		owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
13755 		owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
13756 	}
13757 
13758 	return rdev_update_owe_info(rdev, dev, &owe_info);
13759 }
13760 
13761 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
13762 {
13763 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13764 	struct net_device *dev = info->user_ptr[1];
13765 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13766 	struct station_info sinfo = {};
13767 	const u8 *buf;
13768 	size_t len;
13769 	u8 *dest;
13770 	int err;
13771 
13772 	if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
13773 		return -EOPNOTSUPP;
13774 
13775 	if (!info->attrs[NL80211_ATTR_MAC] ||
13776 	    !info->attrs[NL80211_ATTR_FRAME]) {
13777 		GENL_SET_ERR_MSG(info, "Frame or MAC missing");
13778 		return -EINVAL;
13779 	}
13780 
13781 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
13782 		return -EOPNOTSUPP;
13783 
13784 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
13785 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
13786 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
13787 
13788 	if (len < sizeof(struct ethhdr))
13789 		return -EINVAL;
13790 
13791 	if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
13792 	    !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
13793 		return -EINVAL;
13794 
13795 	err = rdev_get_station(rdev, dev, dest, &sinfo);
13796 	if (err)
13797 		return err;
13798 
13799 	return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
13800 }
13801 
13802 #define NL80211_FLAG_NEED_WIPHY		0x01
13803 #define NL80211_FLAG_NEED_NETDEV	0x02
13804 #define NL80211_FLAG_NEED_RTNL		0x04
13805 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
13806 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
13807 					 NL80211_FLAG_CHECK_NETDEV_UP)
13808 #define NL80211_FLAG_NEED_WDEV		0x10
13809 /* If a netdev is associated, it must be UP, P2P must be started */
13810 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
13811 					 NL80211_FLAG_CHECK_NETDEV_UP)
13812 #define NL80211_FLAG_CLEAR_SKB		0x20
13813 
13814 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
13815 			    struct genl_info *info)
13816 {
13817 	struct cfg80211_registered_device *rdev;
13818 	struct wireless_dev *wdev;
13819 	struct net_device *dev;
13820 	bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
13821 
13822 	if (rtnl)
13823 		rtnl_lock();
13824 
13825 	if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
13826 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
13827 		if (IS_ERR(rdev)) {
13828 			if (rtnl)
13829 				rtnl_unlock();
13830 			return PTR_ERR(rdev);
13831 		}
13832 		info->user_ptr[0] = rdev;
13833 	} else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
13834 		   ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
13835 		ASSERT_RTNL();
13836 
13837 		wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
13838 						  info->attrs);
13839 		if (IS_ERR(wdev)) {
13840 			if (rtnl)
13841 				rtnl_unlock();
13842 			return PTR_ERR(wdev);
13843 		}
13844 
13845 		dev = wdev->netdev;
13846 		rdev = wiphy_to_rdev(wdev->wiphy);
13847 
13848 		if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
13849 			if (!dev) {
13850 				if (rtnl)
13851 					rtnl_unlock();
13852 				return -EINVAL;
13853 			}
13854 
13855 			info->user_ptr[1] = dev;
13856 		} else {
13857 			info->user_ptr[1] = wdev;
13858 		}
13859 
13860 		if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
13861 		    !wdev_running(wdev)) {
13862 			if (rtnl)
13863 				rtnl_unlock();
13864 			return -ENETDOWN;
13865 		}
13866 
13867 		if (dev)
13868 			dev_hold(dev);
13869 
13870 		info->user_ptr[0] = rdev;
13871 	}
13872 
13873 	return 0;
13874 }
13875 
13876 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
13877 			      struct genl_info *info)
13878 {
13879 	if (info->user_ptr[1]) {
13880 		if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
13881 			struct wireless_dev *wdev = info->user_ptr[1];
13882 
13883 			if (wdev->netdev)
13884 				dev_put(wdev->netdev);
13885 		} else {
13886 			dev_put(info->user_ptr[1]);
13887 		}
13888 	}
13889 
13890 	if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
13891 		rtnl_unlock();
13892 
13893 	/* If needed, clear the netlink message payload from the SKB
13894 	 * as it might contain key data that shouldn't stick around on
13895 	 * the heap after the SKB is freed. The netlink message header
13896 	 * is still needed for further processing, so leave it intact.
13897 	 */
13898 	if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
13899 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
13900 
13901 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
13902 	}
13903 }
13904 
13905 static const struct genl_ops nl80211_ops[] = {
13906 	{
13907 		.cmd = NL80211_CMD_GET_WIPHY,
13908 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13909 		.doit = nl80211_get_wiphy,
13910 		.dumpit = nl80211_dump_wiphy,
13911 		.done = nl80211_dump_wiphy_done,
13912 		/* can be retrieved by unprivileged users */
13913 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13914 				  NL80211_FLAG_NEED_RTNL,
13915 	},
13916 	{
13917 		.cmd = NL80211_CMD_SET_WIPHY,
13918 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13919 		.doit = nl80211_set_wiphy,
13920 		.flags = GENL_UNS_ADMIN_PERM,
13921 		.internal_flags = NL80211_FLAG_NEED_RTNL,
13922 	},
13923 	{
13924 		.cmd = NL80211_CMD_GET_INTERFACE,
13925 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13926 		.doit = nl80211_get_interface,
13927 		.dumpit = nl80211_dump_interface,
13928 		/* can be retrieved by unprivileged users */
13929 		.internal_flags = NL80211_FLAG_NEED_WDEV |
13930 				  NL80211_FLAG_NEED_RTNL,
13931 	},
13932 	{
13933 		.cmd = NL80211_CMD_SET_INTERFACE,
13934 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13935 		.doit = nl80211_set_interface,
13936 		.flags = GENL_UNS_ADMIN_PERM,
13937 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13938 				  NL80211_FLAG_NEED_RTNL,
13939 	},
13940 	{
13941 		.cmd = NL80211_CMD_NEW_INTERFACE,
13942 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13943 		.doit = nl80211_new_interface,
13944 		.flags = GENL_UNS_ADMIN_PERM,
13945 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13946 				  NL80211_FLAG_NEED_RTNL,
13947 	},
13948 	{
13949 		.cmd = NL80211_CMD_DEL_INTERFACE,
13950 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13951 		.doit = nl80211_del_interface,
13952 		.flags = GENL_UNS_ADMIN_PERM,
13953 		.internal_flags = NL80211_FLAG_NEED_WDEV |
13954 				  NL80211_FLAG_NEED_RTNL,
13955 	},
13956 	{
13957 		.cmd = NL80211_CMD_GET_KEY,
13958 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13959 		.doit = nl80211_get_key,
13960 		.flags = GENL_UNS_ADMIN_PERM,
13961 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13962 				  NL80211_FLAG_NEED_RTNL,
13963 	},
13964 	{
13965 		.cmd = NL80211_CMD_SET_KEY,
13966 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13967 		.doit = nl80211_set_key,
13968 		.flags = GENL_UNS_ADMIN_PERM,
13969 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13970 				  NL80211_FLAG_NEED_RTNL |
13971 				  NL80211_FLAG_CLEAR_SKB,
13972 	},
13973 	{
13974 		.cmd = NL80211_CMD_NEW_KEY,
13975 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13976 		.doit = nl80211_new_key,
13977 		.flags = GENL_UNS_ADMIN_PERM,
13978 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13979 				  NL80211_FLAG_NEED_RTNL |
13980 				  NL80211_FLAG_CLEAR_SKB,
13981 	},
13982 	{
13983 		.cmd = NL80211_CMD_DEL_KEY,
13984 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13985 		.doit = nl80211_del_key,
13986 		.flags = GENL_UNS_ADMIN_PERM,
13987 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13988 				  NL80211_FLAG_NEED_RTNL,
13989 	},
13990 	{
13991 		.cmd = NL80211_CMD_SET_BEACON,
13992 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13993 		.flags = GENL_UNS_ADMIN_PERM,
13994 		.doit = nl80211_set_beacon,
13995 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13996 				  NL80211_FLAG_NEED_RTNL,
13997 	},
13998 	{
13999 		.cmd = NL80211_CMD_START_AP,
14000 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14001 		.flags = GENL_UNS_ADMIN_PERM,
14002 		.doit = nl80211_start_ap,
14003 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14004 				  NL80211_FLAG_NEED_RTNL,
14005 	},
14006 	{
14007 		.cmd = NL80211_CMD_STOP_AP,
14008 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14009 		.flags = GENL_UNS_ADMIN_PERM,
14010 		.doit = nl80211_stop_ap,
14011 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14012 				  NL80211_FLAG_NEED_RTNL,
14013 	},
14014 	{
14015 		.cmd = NL80211_CMD_GET_STATION,
14016 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14017 		.doit = nl80211_get_station,
14018 		.dumpit = nl80211_dump_station,
14019 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14020 				  NL80211_FLAG_NEED_RTNL,
14021 	},
14022 	{
14023 		.cmd = NL80211_CMD_SET_STATION,
14024 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14025 		.doit = nl80211_set_station,
14026 		.flags = GENL_UNS_ADMIN_PERM,
14027 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14028 				  NL80211_FLAG_NEED_RTNL,
14029 	},
14030 	{
14031 		.cmd = NL80211_CMD_NEW_STATION,
14032 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14033 		.doit = nl80211_new_station,
14034 		.flags = GENL_UNS_ADMIN_PERM,
14035 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14036 				  NL80211_FLAG_NEED_RTNL,
14037 	},
14038 	{
14039 		.cmd = NL80211_CMD_DEL_STATION,
14040 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14041 		.doit = nl80211_del_station,
14042 		.flags = GENL_UNS_ADMIN_PERM,
14043 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14044 				  NL80211_FLAG_NEED_RTNL,
14045 	},
14046 	{
14047 		.cmd = NL80211_CMD_GET_MPATH,
14048 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14049 		.doit = nl80211_get_mpath,
14050 		.dumpit = nl80211_dump_mpath,
14051 		.flags = GENL_UNS_ADMIN_PERM,
14052 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14053 				  NL80211_FLAG_NEED_RTNL,
14054 	},
14055 	{
14056 		.cmd = NL80211_CMD_GET_MPP,
14057 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14058 		.doit = nl80211_get_mpp,
14059 		.dumpit = nl80211_dump_mpp,
14060 		.flags = GENL_UNS_ADMIN_PERM,
14061 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14062 				  NL80211_FLAG_NEED_RTNL,
14063 	},
14064 	{
14065 		.cmd = NL80211_CMD_SET_MPATH,
14066 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14067 		.doit = nl80211_set_mpath,
14068 		.flags = GENL_UNS_ADMIN_PERM,
14069 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14070 				  NL80211_FLAG_NEED_RTNL,
14071 	},
14072 	{
14073 		.cmd = NL80211_CMD_NEW_MPATH,
14074 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14075 		.doit = nl80211_new_mpath,
14076 		.flags = GENL_UNS_ADMIN_PERM,
14077 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14078 				  NL80211_FLAG_NEED_RTNL,
14079 	},
14080 	{
14081 		.cmd = NL80211_CMD_DEL_MPATH,
14082 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14083 		.doit = nl80211_del_mpath,
14084 		.flags = GENL_UNS_ADMIN_PERM,
14085 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14086 				  NL80211_FLAG_NEED_RTNL,
14087 	},
14088 	{
14089 		.cmd = NL80211_CMD_SET_BSS,
14090 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14091 		.doit = nl80211_set_bss,
14092 		.flags = GENL_UNS_ADMIN_PERM,
14093 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14094 				  NL80211_FLAG_NEED_RTNL,
14095 	},
14096 	{
14097 		.cmd = NL80211_CMD_GET_REG,
14098 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14099 		.doit = nl80211_get_reg_do,
14100 		.dumpit = nl80211_get_reg_dump,
14101 		.internal_flags = NL80211_FLAG_NEED_RTNL,
14102 		/* can be retrieved by unprivileged users */
14103 	},
14104 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
14105 	{
14106 		.cmd = NL80211_CMD_SET_REG,
14107 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14108 		.doit = nl80211_set_reg,
14109 		.flags = GENL_ADMIN_PERM,
14110 		.internal_flags = NL80211_FLAG_NEED_RTNL,
14111 	},
14112 #endif
14113 	{
14114 		.cmd = NL80211_CMD_REQ_SET_REG,
14115 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14116 		.doit = nl80211_req_set_reg,
14117 		.flags = GENL_ADMIN_PERM,
14118 	},
14119 	{
14120 		.cmd = NL80211_CMD_RELOAD_REGDB,
14121 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14122 		.doit = nl80211_reload_regdb,
14123 		.flags = GENL_ADMIN_PERM,
14124 	},
14125 	{
14126 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
14127 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14128 		.doit = nl80211_get_mesh_config,
14129 		/* can be retrieved by unprivileged users */
14130 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14131 				  NL80211_FLAG_NEED_RTNL,
14132 	},
14133 	{
14134 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
14135 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14136 		.doit = nl80211_update_mesh_config,
14137 		.flags = GENL_UNS_ADMIN_PERM,
14138 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14139 				  NL80211_FLAG_NEED_RTNL,
14140 	},
14141 	{
14142 		.cmd = NL80211_CMD_TRIGGER_SCAN,
14143 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14144 		.doit = nl80211_trigger_scan,
14145 		.flags = GENL_UNS_ADMIN_PERM,
14146 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14147 				  NL80211_FLAG_NEED_RTNL,
14148 	},
14149 	{
14150 		.cmd = NL80211_CMD_ABORT_SCAN,
14151 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14152 		.doit = nl80211_abort_scan,
14153 		.flags = GENL_UNS_ADMIN_PERM,
14154 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14155 				  NL80211_FLAG_NEED_RTNL,
14156 	},
14157 	{
14158 		.cmd = NL80211_CMD_GET_SCAN,
14159 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14160 		.dumpit = nl80211_dump_scan,
14161 	},
14162 	{
14163 		.cmd = NL80211_CMD_START_SCHED_SCAN,
14164 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14165 		.doit = nl80211_start_sched_scan,
14166 		.flags = GENL_UNS_ADMIN_PERM,
14167 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14168 				  NL80211_FLAG_NEED_RTNL,
14169 	},
14170 	{
14171 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
14172 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14173 		.doit = nl80211_stop_sched_scan,
14174 		.flags = GENL_UNS_ADMIN_PERM,
14175 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14176 				  NL80211_FLAG_NEED_RTNL,
14177 	},
14178 	{
14179 		.cmd = NL80211_CMD_AUTHENTICATE,
14180 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14181 		.doit = nl80211_authenticate,
14182 		.flags = GENL_UNS_ADMIN_PERM,
14183 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14184 				  NL80211_FLAG_NEED_RTNL |
14185 				  NL80211_FLAG_CLEAR_SKB,
14186 	},
14187 	{
14188 		.cmd = NL80211_CMD_ASSOCIATE,
14189 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14190 		.doit = nl80211_associate,
14191 		.flags = GENL_UNS_ADMIN_PERM,
14192 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14193 				  NL80211_FLAG_NEED_RTNL |
14194 				  NL80211_FLAG_CLEAR_SKB,
14195 	},
14196 	{
14197 		.cmd = NL80211_CMD_DEAUTHENTICATE,
14198 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14199 		.doit = nl80211_deauthenticate,
14200 		.flags = GENL_UNS_ADMIN_PERM,
14201 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14202 				  NL80211_FLAG_NEED_RTNL,
14203 	},
14204 	{
14205 		.cmd = NL80211_CMD_DISASSOCIATE,
14206 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14207 		.doit = nl80211_disassociate,
14208 		.flags = GENL_UNS_ADMIN_PERM,
14209 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14210 				  NL80211_FLAG_NEED_RTNL,
14211 	},
14212 	{
14213 		.cmd = NL80211_CMD_JOIN_IBSS,
14214 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14215 		.doit = nl80211_join_ibss,
14216 		.flags = GENL_UNS_ADMIN_PERM,
14217 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14218 				  NL80211_FLAG_NEED_RTNL,
14219 	},
14220 	{
14221 		.cmd = NL80211_CMD_LEAVE_IBSS,
14222 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14223 		.doit = nl80211_leave_ibss,
14224 		.flags = GENL_UNS_ADMIN_PERM,
14225 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14226 				  NL80211_FLAG_NEED_RTNL,
14227 	},
14228 #ifdef CONFIG_NL80211_TESTMODE
14229 	{
14230 		.cmd = NL80211_CMD_TESTMODE,
14231 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14232 		.doit = nl80211_testmode_do,
14233 		.dumpit = nl80211_testmode_dump,
14234 		.flags = GENL_UNS_ADMIN_PERM,
14235 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14236 				  NL80211_FLAG_NEED_RTNL,
14237 	},
14238 #endif
14239 	{
14240 		.cmd = NL80211_CMD_CONNECT,
14241 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14242 		.doit = nl80211_connect,
14243 		.flags = GENL_UNS_ADMIN_PERM,
14244 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14245 				  NL80211_FLAG_NEED_RTNL |
14246 				  NL80211_FLAG_CLEAR_SKB,
14247 	},
14248 	{
14249 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
14250 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14251 		.doit = nl80211_update_connect_params,
14252 		.flags = GENL_ADMIN_PERM,
14253 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14254 				  NL80211_FLAG_NEED_RTNL |
14255 				  NL80211_FLAG_CLEAR_SKB,
14256 	},
14257 	{
14258 		.cmd = NL80211_CMD_DISCONNECT,
14259 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14260 		.doit = nl80211_disconnect,
14261 		.flags = GENL_UNS_ADMIN_PERM,
14262 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14263 				  NL80211_FLAG_NEED_RTNL,
14264 	},
14265 	{
14266 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
14267 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14268 		.doit = nl80211_wiphy_netns,
14269 		.flags = GENL_UNS_ADMIN_PERM,
14270 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14271 				  NL80211_FLAG_NEED_RTNL,
14272 	},
14273 	{
14274 		.cmd = NL80211_CMD_GET_SURVEY,
14275 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14276 		.dumpit = nl80211_dump_survey,
14277 	},
14278 	{
14279 		.cmd = NL80211_CMD_SET_PMKSA,
14280 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14281 		.doit = nl80211_setdel_pmksa,
14282 		.flags = GENL_UNS_ADMIN_PERM,
14283 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14284 				  NL80211_FLAG_NEED_RTNL |
14285 				  NL80211_FLAG_CLEAR_SKB,
14286 	},
14287 	{
14288 		.cmd = NL80211_CMD_DEL_PMKSA,
14289 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14290 		.doit = nl80211_setdel_pmksa,
14291 		.flags = GENL_UNS_ADMIN_PERM,
14292 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14293 				  NL80211_FLAG_NEED_RTNL,
14294 	},
14295 	{
14296 		.cmd = NL80211_CMD_FLUSH_PMKSA,
14297 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14298 		.doit = nl80211_flush_pmksa,
14299 		.flags = GENL_UNS_ADMIN_PERM,
14300 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14301 				  NL80211_FLAG_NEED_RTNL,
14302 	},
14303 	{
14304 		.cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
14305 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14306 		.doit = nl80211_remain_on_channel,
14307 		.flags = GENL_UNS_ADMIN_PERM,
14308 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14309 				  NL80211_FLAG_NEED_RTNL,
14310 	},
14311 	{
14312 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
14313 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14314 		.doit = nl80211_cancel_remain_on_channel,
14315 		.flags = GENL_UNS_ADMIN_PERM,
14316 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14317 				  NL80211_FLAG_NEED_RTNL,
14318 	},
14319 	{
14320 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
14321 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14322 		.doit = nl80211_set_tx_bitrate_mask,
14323 		.flags = GENL_UNS_ADMIN_PERM,
14324 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14325 				  NL80211_FLAG_NEED_RTNL,
14326 	},
14327 	{
14328 		.cmd = NL80211_CMD_REGISTER_FRAME,
14329 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14330 		.doit = nl80211_register_mgmt,
14331 		.flags = GENL_UNS_ADMIN_PERM,
14332 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14333 				  NL80211_FLAG_NEED_RTNL,
14334 	},
14335 	{
14336 		.cmd = NL80211_CMD_FRAME,
14337 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14338 		.doit = nl80211_tx_mgmt,
14339 		.flags = GENL_UNS_ADMIN_PERM,
14340 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14341 				  NL80211_FLAG_NEED_RTNL,
14342 	},
14343 	{
14344 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
14345 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14346 		.doit = nl80211_tx_mgmt_cancel_wait,
14347 		.flags = GENL_UNS_ADMIN_PERM,
14348 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14349 				  NL80211_FLAG_NEED_RTNL,
14350 	},
14351 	{
14352 		.cmd = NL80211_CMD_SET_POWER_SAVE,
14353 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14354 		.doit = nl80211_set_power_save,
14355 		.flags = GENL_UNS_ADMIN_PERM,
14356 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14357 				  NL80211_FLAG_NEED_RTNL,
14358 	},
14359 	{
14360 		.cmd = NL80211_CMD_GET_POWER_SAVE,
14361 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14362 		.doit = nl80211_get_power_save,
14363 		/* can be retrieved by unprivileged users */
14364 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14365 				  NL80211_FLAG_NEED_RTNL,
14366 	},
14367 	{
14368 		.cmd = NL80211_CMD_SET_CQM,
14369 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14370 		.doit = nl80211_set_cqm,
14371 		.flags = GENL_UNS_ADMIN_PERM,
14372 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14373 				  NL80211_FLAG_NEED_RTNL,
14374 	},
14375 	{
14376 		.cmd = NL80211_CMD_SET_CHANNEL,
14377 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14378 		.doit = nl80211_set_channel,
14379 		.flags = GENL_UNS_ADMIN_PERM,
14380 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14381 				  NL80211_FLAG_NEED_RTNL,
14382 	},
14383 	{
14384 		.cmd = NL80211_CMD_SET_WDS_PEER,
14385 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14386 		.doit = nl80211_set_wds_peer,
14387 		.flags = GENL_UNS_ADMIN_PERM,
14388 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14389 				  NL80211_FLAG_NEED_RTNL,
14390 	},
14391 	{
14392 		.cmd = NL80211_CMD_JOIN_MESH,
14393 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14394 		.doit = nl80211_join_mesh,
14395 		.flags = GENL_UNS_ADMIN_PERM,
14396 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14397 				  NL80211_FLAG_NEED_RTNL,
14398 	},
14399 	{
14400 		.cmd = NL80211_CMD_LEAVE_MESH,
14401 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14402 		.doit = nl80211_leave_mesh,
14403 		.flags = GENL_UNS_ADMIN_PERM,
14404 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14405 				  NL80211_FLAG_NEED_RTNL,
14406 	},
14407 	{
14408 		.cmd = NL80211_CMD_JOIN_OCB,
14409 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14410 		.doit = nl80211_join_ocb,
14411 		.flags = GENL_UNS_ADMIN_PERM,
14412 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14413 				  NL80211_FLAG_NEED_RTNL,
14414 	},
14415 	{
14416 		.cmd = NL80211_CMD_LEAVE_OCB,
14417 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14418 		.doit = nl80211_leave_ocb,
14419 		.flags = GENL_UNS_ADMIN_PERM,
14420 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14421 				  NL80211_FLAG_NEED_RTNL,
14422 	},
14423 #ifdef CONFIG_PM
14424 	{
14425 		.cmd = NL80211_CMD_GET_WOWLAN,
14426 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14427 		.doit = nl80211_get_wowlan,
14428 		/* can be retrieved by unprivileged users */
14429 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14430 				  NL80211_FLAG_NEED_RTNL,
14431 	},
14432 	{
14433 		.cmd = NL80211_CMD_SET_WOWLAN,
14434 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14435 		.doit = nl80211_set_wowlan,
14436 		.flags = GENL_UNS_ADMIN_PERM,
14437 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14438 				  NL80211_FLAG_NEED_RTNL,
14439 	},
14440 #endif
14441 	{
14442 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
14443 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14444 		.doit = nl80211_set_rekey_data,
14445 		.flags = GENL_UNS_ADMIN_PERM,
14446 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14447 				  NL80211_FLAG_NEED_RTNL |
14448 				  NL80211_FLAG_CLEAR_SKB,
14449 	},
14450 	{
14451 		.cmd = NL80211_CMD_TDLS_MGMT,
14452 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14453 		.doit = nl80211_tdls_mgmt,
14454 		.flags = GENL_UNS_ADMIN_PERM,
14455 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14456 				  NL80211_FLAG_NEED_RTNL,
14457 	},
14458 	{
14459 		.cmd = NL80211_CMD_TDLS_OPER,
14460 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14461 		.doit = nl80211_tdls_oper,
14462 		.flags = GENL_UNS_ADMIN_PERM,
14463 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14464 				  NL80211_FLAG_NEED_RTNL,
14465 	},
14466 	{
14467 		.cmd = NL80211_CMD_UNEXPECTED_FRAME,
14468 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14469 		.doit = nl80211_register_unexpected_frame,
14470 		.flags = GENL_UNS_ADMIN_PERM,
14471 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14472 				  NL80211_FLAG_NEED_RTNL,
14473 	},
14474 	{
14475 		.cmd = NL80211_CMD_PROBE_CLIENT,
14476 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14477 		.doit = nl80211_probe_client,
14478 		.flags = GENL_UNS_ADMIN_PERM,
14479 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14480 				  NL80211_FLAG_NEED_RTNL,
14481 	},
14482 	{
14483 		.cmd = NL80211_CMD_REGISTER_BEACONS,
14484 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14485 		.doit = nl80211_register_beacons,
14486 		.flags = GENL_UNS_ADMIN_PERM,
14487 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14488 				  NL80211_FLAG_NEED_RTNL,
14489 	},
14490 	{
14491 		.cmd = NL80211_CMD_SET_NOACK_MAP,
14492 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14493 		.doit = nl80211_set_noack_map,
14494 		.flags = GENL_UNS_ADMIN_PERM,
14495 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14496 				  NL80211_FLAG_NEED_RTNL,
14497 	},
14498 	{
14499 		.cmd = NL80211_CMD_START_P2P_DEVICE,
14500 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14501 		.doit = nl80211_start_p2p_device,
14502 		.flags = GENL_UNS_ADMIN_PERM,
14503 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14504 				  NL80211_FLAG_NEED_RTNL,
14505 	},
14506 	{
14507 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
14508 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14509 		.doit = nl80211_stop_p2p_device,
14510 		.flags = GENL_UNS_ADMIN_PERM,
14511 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14512 				  NL80211_FLAG_NEED_RTNL,
14513 	},
14514 	{
14515 		.cmd = NL80211_CMD_START_NAN,
14516 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14517 		.doit = nl80211_start_nan,
14518 		.flags = GENL_ADMIN_PERM,
14519 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14520 				  NL80211_FLAG_NEED_RTNL,
14521 	},
14522 	{
14523 		.cmd = NL80211_CMD_STOP_NAN,
14524 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14525 		.doit = nl80211_stop_nan,
14526 		.flags = GENL_ADMIN_PERM,
14527 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14528 				  NL80211_FLAG_NEED_RTNL,
14529 	},
14530 	{
14531 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
14532 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14533 		.doit = nl80211_nan_add_func,
14534 		.flags = GENL_ADMIN_PERM,
14535 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14536 				  NL80211_FLAG_NEED_RTNL,
14537 	},
14538 	{
14539 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
14540 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14541 		.doit = nl80211_nan_del_func,
14542 		.flags = GENL_ADMIN_PERM,
14543 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14544 				  NL80211_FLAG_NEED_RTNL,
14545 	},
14546 	{
14547 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
14548 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14549 		.doit = nl80211_nan_change_config,
14550 		.flags = GENL_ADMIN_PERM,
14551 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14552 				  NL80211_FLAG_NEED_RTNL,
14553 	},
14554 	{
14555 		.cmd = NL80211_CMD_SET_MCAST_RATE,
14556 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14557 		.doit = nl80211_set_mcast_rate,
14558 		.flags = GENL_UNS_ADMIN_PERM,
14559 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14560 				  NL80211_FLAG_NEED_RTNL,
14561 	},
14562 	{
14563 		.cmd = NL80211_CMD_SET_MAC_ACL,
14564 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14565 		.doit = nl80211_set_mac_acl,
14566 		.flags = GENL_UNS_ADMIN_PERM,
14567 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14568 				  NL80211_FLAG_NEED_RTNL,
14569 	},
14570 	{
14571 		.cmd = NL80211_CMD_RADAR_DETECT,
14572 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14573 		.doit = nl80211_start_radar_detection,
14574 		.flags = GENL_UNS_ADMIN_PERM,
14575 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14576 				  NL80211_FLAG_NEED_RTNL,
14577 	},
14578 	{
14579 		.cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
14580 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14581 		.doit = nl80211_get_protocol_features,
14582 	},
14583 	{
14584 		.cmd = NL80211_CMD_UPDATE_FT_IES,
14585 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14586 		.doit = nl80211_update_ft_ies,
14587 		.flags = GENL_UNS_ADMIN_PERM,
14588 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14589 				  NL80211_FLAG_NEED_RTNL,
14590 	},
14591 	{
14592 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
14593 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14594 		.doit = nl80211_crit_protocol_start,
14595 		.flags = GENL_UNS_ADMIN_PERM,
14596 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14597 				  NL80211_FLAG_NEED_RTNL,
14598 	},
14599 	{
14600 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
14601 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14602 		.doit = nl80211_crit_protocol_stop,
14603 		.flags = GENL_UNS_ADMIN_PERM,
14604 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14605 				  NL80211_FLAG_NEED_RTNL,
14606 	},
14607 	{
14608 		.cmd = NL80211_CMD_GET_COALESCE,
14609 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14610 		.doit = nl80211_get_coalesce,
14611 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14612 				  NL80211_FLAG_NEED_RTNL,
14613 	},
14614 	{
14615 		.cmd = NL80211_CMD_SET_COALESCE,
14616 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14617 		.doit = nl80211_set_coalesce,
14618 		.flags = GENL_UNS_ADMIN_PERM,
14619 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14620 				  NL80211_FLAG_NEED_RTNL,
14621 	},
14622 	{
14623 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
14624 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14625 		.doit = nl80211_channel_switch,
14626 		.flags = GENL_UNS_ADMIN_PERM,
14627 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14628 				  NL80211_FLAG_NEED_RTNL,
14629 	},
14630 	{
14631 		.cmd = NL80211_CMD_VENDOR,
14632 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14633 		.doit = nl80211_vendor_cmd,
14634 		.dumpit = nl80211_vendor_cmd_dump,
14635 		.flags = GENL_UNS_ADMIN_PERM,
14636 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14637 				  NL80211_FLAG_NEED_RTNL |
14638 				  NL80211_FLAG_CLEAR_SKB,
14639 	},
14640 	{
14641 		.cmd = NL80211_CMD_SET_QOS_MAP,
14642 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14643 		.doit = nl80211_set_qos_map,
14644 		.flags = GENL_UNS_ADMIN_PERM,
14645 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14646 				  NL80211_FLAG_NEED_RTNL,
14647 	},
14648 	{
14649 		.cmd = NL80211_CMD_ADD_TX_TS,
14650 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14651 		.doit = nl80211_add_tx_ts,
14652 		.flags = GENL_UNS_ADMIN_PERM,
14653 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14654 				  NL80211_FLAG_NEED_RTNL,
14655 	},
14656 	{
14657 		.cmd = NL80211_CMD_DEL_TX_TS,
14658 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14659 		.doit = nl80211_del_tx_ts,
14660 		.flags = GENL_UNS_ADMIN_PERM,
14661 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14662 				  NL80211_FLAG_NEED_RTNL,
14663 	},
14664 	{
14665 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
14666 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14667 		.doit = nl80211_tdls_channel_switch,
14668 		.flags = GENL_UNS_ADMIN_PERM,
14669 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14670 				  NL80211_FLAG_NEED_RTNL,
14671 	},
14672 	{
14673 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
14674 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14675 		.doit = nl80211_tdls_cancel_channel_switch,
14676 		.flags = GENL_UNS_ADMIN_PERM,
14677 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14678 				  NL80211_FLAG_NEED_RTNL,
14679 	},
14680 	{
14681 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
14682 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14683 		.doit = nl80211_set_multicast_to_unicast,
14684 		.flags = GENL_UNS_ADMIN_PERM,
14685 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14686 				  NL80211_FLAG_NEED_RTNL,
14687 	},
14688 	{
14689 		.cmd = NL80211_CMD_SET_PMK,
14690 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14691 		.doit = nl80211_set_pmk,
14692 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14693 				  NL80211_FLAG_NEED_RTNL |
14694 				  NL80211_FLAG_CLEAR_SKB,
14695 	},
14696 	{
14697 		.cmd = NL80211_CMD_DEL_PMK,
14698 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14699 		.doit = nl80211_del_pmk,
14700 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14701 				  NL80211_FLAG_NEED_RTNL,
14702 	},
14703 	{
14704 		.cmd = NL80211_CMD_EXTERNAL_AUTH,
14705 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14706 		.doit = nl80211_external_auth,
14707 		.flags = GENL_ADMIN_PERM,
14708 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14709 				  NL80211_FLAG_NEED_RTNL,
14710 	},
14711 	{
14712 		.cmd = NL80211_CMD_CONTROL_PORT_FRAME,
14713 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14714 		.doit = nl80211_tx_control_port,
14715 		.flags = GENL_UNS_ADMIN_PERM,
14716 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14717 				  NL80211_FLAG_NEED_RTNL,
14718 	},
14719 	{
14720 		.cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
14721 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14722 		.doit = nl80211_get_ftm_responder_stats,
14723 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14724 				  NL80211_FLAG_NEED_RTNL,
14725 	},
14726 	{
14727 		.cmd = NL80211_CMD_PEER_MEASUREMENT_START,
14728 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14729 		.doit = nl80211_pmsr_start,
14730 		.flags = GENL_UNS_ADMIN_PERM,
14731 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14732 				  NL80211_FLAG_NEED_RTNL,
14733 	},
14734 	{
14735 		.cmd = NL80211_CMD_NOTIFY_RADAR,
14736 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14737 		.doit = nl80211_notify_radar_detection,
14738 		.flags = GENL_UNS_ADMIN_PERM,
14739 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14740 				  NL80211_FLAG_NEED_RTNL,
14741 	},
14742 	{
14743 		.cmd = NL80211_CMD_UPDATE_OWE_INFO,
14744 		.doit = nl80211_update_owe_info,
14745 		.flags = GENL_ADMIN_PERM,
14746 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14747 				  NL80211_FLAG_NEED_RTNL,
14748 	},
14749 	{
14750 		.cmd = NL80211_CMD_PROBE_MESH_LINK,
14751 		.doit = nl80211_probe_mesh_link,
14752 		.flags = GENL_UNS_ADMIN_PERM,
14753 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14754 				  NL80211_FLAG_NEED_RTNL,
14755 	},
14756 };
14757 
14758 static struct genl_family nl80211_fam __ro_after_init = {
14759 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
14760 	.hdrsize = 0,			/* no private header */
14761 	.version = 1,			/* no particular meaning now */
14762 	.maxattr = NL80211_ATTR_MAX,
14763 	.policy = nl80211_policy,
14764 	.netnsok = true,
14765 	.pre_doit = nl80211_pre_doit,
14766 	.post_doit = nl80211_post_doit,
14767 	.module = THIS_MODULE,
14768 	.ops = nl80211_ops,
14769 	.n_ops = ARRAY_SIZE(nl80211_ops),
14770 	.mcgrps = nl80211_mcgrps,
14771 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
14772 	.parallel_ops = true,
14773 };
14774 
14775 /* notification functions */
14776 
14777 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
14778 			  enum nl80211_commands cmd)
14779 {
14780 	struct sk_buff *msg;
14781 	struct nl80211_dump_wiphy_state state = {};
14782 
14783 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
14784 		cmd != NL80211_CMD_DEL_WIPHY);
14785 
14786 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14787 	if (!msg)
14788 		return;
14789 
14790 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
14791 		nlmsg_free(msg);
14792 		return;
14793 	}
14794 
14795 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14796 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
14797 }
14798 
14799 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
14800 				struct wireless_dev *wdev,
14801 				enum nl80211_commands cmd)
14802 {
14803 	struct sk_buff *msg;
14804 
14805 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14806 	if (!msg)
14807 		return;
14808 
14809 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
14810 		nlmsg_free(msg);
14811 		return;
14812 	}
14813 
14814 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14815 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
14816 }
14817 
14818 static int nl80211_add_scan_req(struct sk_buff *msg,
14819 				struct cfg80211_registered_device *rdev)
14820 {
14821 	struct cfg80211_scan_request *req = rdev->scan_req;
14822 	struct nlattr *nest;
14823 	int i;
14824 
14825 	if (WARN_ON(!req))
14826 		return 0;
14827 
14828 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
14829 	if (!nest)
14830 		goto nla_put_failure;
14831 	for (i = 0; i < req->n_ssids; i++) {
14832 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
14833 			goto nla_put_failure;
14834 	}
14835 	nla_nest_end(msg, nest);
14836 
14837 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
14838 	if (!nest)
14839 		goto nla_put_failure;
14840 	for (i = 0; i < req->n_channels; i++) {
14841 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
14842 			goto nla_put_failure;
14843 	}
14844 	nla_nest_end(msg, nest);
14845 
14846 	if (req->ie &&
14847 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
14848 		goto nla_put_failure;
14849 
14850 	if (req->flags &&
14851 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
14852 		goto nla_put_failure;
14853 
14854 	if (req->info.scan_start_tsf &&
14855 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
14856 			       req->info.scan_start_tsf, NL80211_BSS_PAD) ||
14857 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
14858 		     req->info.tsf_bssid)))
14859 		goto nla_put_failure;
14860 
14861 	return 0;
14862  nla_put_failure:
14863 	return -ENOBUFS;
14864 }
14865 
14866 static int nl80211_prep_scan_msg(struct sk_buff *msg,
14867 				 struct cfg80211_registered_device *rdev,
14868 				 struct wireless_dev *wdev,
14869 				 u32 portid, u32 seq, int flags,
14870 				 u32 cmd)
14871 {
14872 	void *hdr;
14873 
14874 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
14875 	if (!hdr)
14876 		return -1;
14877 
14878 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14879 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14880 					 wdev->netdev->ifindex)) ||
14881 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14882 			      NL80211_ATTR_PAD))
14883 		goto nla_put_failure;
14884 
14885 	/* ignore errors and send incomplete event anyway */
14886 	nl80211_add_scan_req(msg, rdev);
14887 
14888 	genlmsg_end(msg, hdr);
14889 	return 0;
14890 
14891  nla_put_failure:
14892 	genlmsg_cancel(msg, hdr);
14893 	return -EMSGSIZE;
14894 }
14895 
14896 static int
14897 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
14898 			    struct cfg80211_sched_scan_request *req, u32 cmd)
14899 {
14900 	void *hdr;
14901 
14902 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14903 	if (!hdr)
14904 		return -1;
14905 
14906 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
14907 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
14908 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
14909 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
14910 			      NL80211_ATTR_PAD))
14911 		goto nla_put_failure;
14912 
14913 	genlmsg_end(msg, hdr);
14914 	return 0;
14915 
14916  nla_put_failure:
14917 	genlmsg_cancel(msg, hdr);
14918 	return -EMSGSIZE;
14919 }
14920 
14921 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
14922 			     struct wireless_dev *wdev)
14923 {
14924 	struct sk_buff *msg;
14925 
14926 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14927 	if (!msg)
14928 		return;
14929 
14930 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
14931 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
14932 		nlmsg_free(msg);
14933 		return;
14934 	}
14935 
14936 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14937 				NL80211_MCGRP_SCAN, GFP_KERNEL);
14938 }
14939 
14940 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
14941 				       struct wireless_dev *wdev, bool aborted)
14942 {
14943 	struct sk_buff *msg;
14944 
14945 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14946 	if (!msg)
14947 		return NULL;
14948 
14949 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
14950 				  aborted ? NL80211_CMD_SCAN_ABORTED :
14951 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
14952 		nlmsg_free(msg);
14953 		return NULL;
14954 	}
14955 
14956 	return msg;
14957 }
14958 
14959 /* send message created by nl80211_build_scan_msg() */
14960 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
14961 			   struct sk_buff *msg)
14962 {
14963 	if (!msg)
14964 		return;
14965 
14966 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14967 				NL80211_MCGRP_SCAN, GFP_KERNEL);
14968 }
14969 
14970 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
14971 {
14972 	struct sk_buff *msg;
14973 
14974 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14975 	if (!msg)
14976 		return;
14977 
14978 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
14979 		nlmsg_free(msg);
14980 		return;
14981 	}
14982 
14983 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
14984 				NL80211_MCGRP_SCAN, GFP_KERNEL);
14985 }
14986 
14987 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
14988 					  struct regulatory_request *request)
14989 {
14990 	/* Userspace can always count this one always being set */
14991 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
14992 		goto nla_put_failure;
14993 
14994 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
14995 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14996 			       NL80211_REGDOM_TYPE_WORLD))
14997 			goto nla_put_failure;
14998 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
14999 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15000 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
15001 			goto nla_put_failure;
15002 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
15003 		   request->intersect) {
15004 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15005 			       NL80211_REGDOM_TYPE_INTERSECTION))
15006 			goto nla_put_failure;
15007 	} else {
15008 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15009 			       NL80211_REGDOM_TYPE_COUNTRY) ||
15010 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
15011 				   request->alpha2))
15012 			goto nla_put_failure;
15013 	}
15014 
15015 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
15016 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
15017 
15018 		if (wiphy &&
15019 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
15020 			goto nla_put_failure;
15021 
15022 		if (wiphy &&
15023 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
15024 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
15025 			goto nla_put_failure;
15026 	}
15027 
15028 	return true;
15029 
15030 nla_put_failure:
15031 	return false;
15032 }
15033 
15034 /*
15035  * This can happen on global regulatory changes or device specific settings
15036  * based on custom regulatory domains.
15037  */
15038 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
15039 				     struct regulatory_request *request)
15040 {
15041 	struct sk_buff *msg;
15042 	void *hdr;
15043 
15044 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15045 	if (!msg)
15046 		return;
15047 
15048 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
15049 	if (!hdr)
15050 		goto nla_put_failure;
15051 
15052 	if (!nl80211_reg_change_event_fill(msg, request))
15053 		goto nla_put_failure;
15054 
15055 	genlmsg_end(msg, hdr);
15056 
15057 	rcu_read_lock();
15058 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
15059 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
15060 	rcu_read_unlock();
15061 
15062 	return;
15063 
15064 nla_put_failure:
15065 	nlmsg_free(msg);
15066 }
15067 
15068 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
15069 				    struct net_device *netdev,
15070 				    const u8 *buf, size_t len,
15071 				    enum nl80211_commands cmd, gfp_t gfp,
15072 				    int uapsd_queues, const u8 *req_ies,
15073 				    size_t req_ies_len)
15074 {
15075 	struct sk_buff *msg;
15076 	void *hdr;
15077 
15078 	msg = nlmsg_new(100 + len + req_ies_len, gfp);
15079 	if (!msg)
15080 		return;
15081 
15082 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15083 	if (!hdr) {
15084 		nlmsg_free(msg);
15085 		return;
15086 	}
15087 
15088 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15089 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15090 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15091 	    (req_ies &&
15092 	     nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
15093 		goto nla_put_failure;
15094 
15095 	if (uapsd_queues >= 0) {
15096 		struct nlattr *nla_wmm =
15097 			nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
15098 		if (!nla_wmm)
15099 			goto nla_put_failure;
15100 
15101 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
15102 			       uapsd_queues))
15103 			goto nla_put_failure;
15104 
15105 		nla_nest_end(msg, nla_wmm);
15106 	}
15107 
15108 	genlmsg_end(msg, hdr);
15109 
15110 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15111 				NL80211_MCGRP_MLME, gfp);
15112 	return;
15113 
15114  nla_put_failure:
15115 	nlmsg_free(msg);
15116 }
15117 
15118 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
15119 			  struct net_device *netdev, const u8 *buf,
15120 			  size_t len, gfp_t gfp)
15121 {
15122 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15123 				NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0);
15124 }
15125 
15126 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
15127 			   struct net_device *netdev, const u8 *buf,
15128 			   size_t len, gfp_t gfp, int uapsd_queues,
15129 			   const u8 *req_ies, size_t req_ies_len)
15130 {
15131 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15132 				NL80211_CMD_ASSOCIATE, gfp, uapsd_queues,
15133 				req_ies, req_ies_len);
15134 }
15135 
15136 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
15137 			 struct net_device *netdev, const u8 *buf,
15138 			 size_t len, gfp_t gfp)
15139 {
15140 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15141 				NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0);
15142 }
15143 
15144 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
15145 			   struct net_device *netdev, const u8 *buf,
15146 			   size_t len, gfp_t gfp)
15147 {
15148 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15149 				NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0);
15150 }
15151 
15152 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
15153 				  size_t len)
15154 {
15155 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15156 	struct wiphy *wiphy = wdev->wiphy;
15157 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15158 	const struct ieee80211_mgmt *mgmt = (void *)buf;
15159 	u32 cmd;
15160 
15161 	if (WARN_ON(len < 2))
15162 		return;
15163 
15164 	if (ieee80211_is_deauth(mgmt->frame_control))
15165 		cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
15166 	else
15167 		cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
15168 
15169 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
15170 	nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
15171 				NULL, 0);
15172 }
15173 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
15174 
15175 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
15176 				      struct net_device *netdev, int cmd,
15177 				      const u8 *addr, gfp_t gfp)
15178 {
15179 	struct sk_buff *msg;
15180 	void *hdr;
15181 
15182 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15183 	if (!msg)
15184 		return;
15185 
15186 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15187 	if (!hdr) {
15188 		nlmsg_free(msg);
15189 		return;
15190 	}
15191 
15192 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15193 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15194 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
15195 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
15196 		goto nla_put_failure;
15197 
15198 	genlmsg_end(msg, hdr);
15199 
15200 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15201 				NL80211_MCGRP_MLME, gfp);
15202 	return;
15203 
15204  nla_put_failure:
15205 	nlmsg_free(msg);
15206 }
15207 
15208 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
15209 			       struct net_device *netdev, const u8 *addr,
15210 			       gfp_t gfp)
15211 {
15212 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
15213 				  addr, gfp);
15214 }
15215 
15216 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
15217 				struct net_device *netdev, const u8 *addr,
15218 				gfp_t gfp)
15219 {
15220 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
15221 				  addr, gfp);
15222 }
15223 
15224 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
15225 				 struct net_device *netdev,
15226 				 struct cfg80211_connect_resp_params *cr,
15227 				 gfp_t gfp)
15228 {
15229 	struct sk_buff *msg;
15230 	void *hdr;
15231 
15232 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
15233 			cr->fils.kek_len + cr->fils.pmk_len +
15234 			(cr->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
15235 	if (!msg)
15236 		return;
15237 
15238 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
15239 	if (!hdr) {
15240 		nlmsg_free(msg);
15241 		return;
15242 	}
15243 
15244 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15245 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15246 	    (cr->bssid &&
15247 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, cr->bssid)) ||
15248 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
15249 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
15250 			cr->status) ||
15251 	    (cr->status < 0 &&
15252 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
15253 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
15254 			  cr->timeout_reason))) ||
15255 	    (cr->req_ie &&
15256 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
15257 	    (cr->resp_ie &&
15258 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
15259 		     cr->resp_ie)) ||
15260 	    (cr->fils.update_erp_next_seq_num &&
15261 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
15262 			 cr->fils.erp_next_seq_num)) ||
15263 	    (cr->status == WLAN_STATUS_SUCCESS &&
15264 	     ((cr->fils.kek &&
15265 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
15266 		       cr->fils.kek)) ||
15267 	      (cr->fils.pmk &&
15268 	       nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
15269 	      (cr->fils.pmkid &&
15270 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
15271 		goto nla_put_failure;
15272 
15273 	genlmsg_end(msg, hdr);
15274 
15275 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15276 				NL80211_MCGRP_MLME, gfp);
15277 	return;
15278 
15279  nla_put_failure:
15280 	nlmsg_free(msg);
15281 }
15282 
15283 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
15284 			 struct net_device *netdev,
15285 			 struct cfg80211_roam_info *info, gfp_t gfp)
15286 {
15287 	struct sk_buff *msg;
15288 	void *hdr;
15289 	const u8 *bssid = info->bss ? info->bss->bssid : info->bssid;
15290 
15291 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
15292 			info->fils.kek_len + info->fils.pmk_len +
15293 			(info->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
15294 	if (!msg)
15295 		return;
15296 
15297 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
15298 	if (!hdr) {
15299 		nlmsg_free(msg);
15300 		return;
15301 	}
15302 
15303 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15304 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15305 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
15306 	    (info->req_ie &&
15307 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
15308 		     info->req_ie)) ||
15309 	    (info->resp_ie &&
15310 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
15311 		     info->resp_ie)) ||
15312 	    (info->fils.update_erp_next_seq_num &&
15313 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
15314 			 info->fils.erp_next_seq_num)) ||
15315 	    (info->fils.kek &&
15316 	     nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
15317 		     info->fils.kek)) ||
15318 	    (info->fils.pmk &&
15319 	     nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
15320 	    (info->fils.pmkid &&
15321 	     nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
15322 		goto nla_put_failure;
15323 
15324 	genlmsg_end(msg, hdr);
15325 
15326 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15327 				NL80211_MCGRP_MLME, gfp);
15328 	return;
15329 
15330  nla_put_failure:
15331 	nlmsg_free(msg);
15332 }
15333 
15334 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
15335 				  struct net_device *netdev, const u8 *bssid)
15336 {
15337 	struct sk_buff *msg;
15338 	void *hdr;
15339 
15340 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15341 	if (!msg)
15342 		return;
15343 
15344 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
15345 	if (!hdr) {
15346 		nlmsg_free(msg);
15347 		return;
15348 	}
15349 
15350 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15351 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15352 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
15353 		goto nla_put_failure;
15354 
15355 	genlmsg_end(msg, hdr);
15356 
15357 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15358 				NL80211_MCGRP_MLME, GFP_KERNEL);
15359 	return;
15360 
15361  nla_put_failure:
15362 	nlmsg_free(msg);
15363 }
15364 
15365 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
15366 			       struct net_device *netdev, u16 reason,
15367 			       const u8 *ie, size_t ie_len, bool from_ap)
15368 {
15369 	struct sk_buff *msg;
15370 	void *hdr;
15371 
15372 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
15373 	if (!msg)
15374 		return;
15375 
15376 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
15377 	if (!hdr) {
15378 		nlmsg_free(msg);
15379 		return;
15380 	}
15381 
15382 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15383 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15384 	    (reason &&
15385 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
15386 	    (from_ap &&
15387 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
15388 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
15389 		goto nla_put_failure;
15390 
15391 	genlmsg_end(msg, hdr);
15392 
15393 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15394 				NL80211_MCGRP_MLME, GFP_KERNEL);
15395 	return;
15396 
15397  nla_put_failure:
15398 	nlmsg_free(msg);
15399 }
15400 
15401 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
15402 			     struct net_device *netdev, const u8 *bssid,
15403 			     gfp_t gfp)
15404 {
15405 	struct sk_buff *msg;
15406 	void *hdr;
15407 
15408 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15409 	if (!msg)
15410 		return;
15411 
15412 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
15413 	if (!hdr) {
15414 		nlmsg_free(msg);
15415 		return;
15416 	}
15417 
15418 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15419 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15420 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
15421 		goto nla_put_failure;
15422 
15423 	genlmsg_end(msg, hdr);
15424 
15425 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15426 				NL80211_MCGRP_MLME, gfp);
15427 	return;
15428 
15429  nla_put_failure:
15430 	nlmsg_free(msg);
15431 }
15432 
15433 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
15434 					const u8 *ie, u8 ie_len,
15435 					int sig_dbm, gfp_t gfp)
15436 {
15437 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15438 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15439 	struct sk_buff *msg;
15440 	void *hdr;
15441 
15442 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
15443 		return;
15444 
15445 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
15446 
15447 	msg = nlmsg_new(100 + ie_len, gfp);
15448 	if (!msg)
15449 		return;
15450 
15451 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
15452 	if (!hdr) {
15453 		nlmsg_free(msg);
15454 		return;
15455 	}
15456 
15457 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15458 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15459 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
15460 	    (ie_len && ie &&
15461 	     nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
15462 	    (sig_dbm &&
15463 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
15464 		goto nla_put_failure;
15465 
15466 	genlmsg_end(msg, hdr);
15467 
15468 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15469 				NL80211_MCGRP_MLME, gfp);
15470 	return;
15471 
15472  nla_put_failure:
15473 	nlmsg_free(msg);
15474 }
15475 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
15476 
15477 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
15478 				 struct net_device *netdev, const u8 *addr,
15479 				 enum nl80211_key_type key_type, int key_id,
15480 				 const u8 *tsc, gfp_t gfp)
15481 {
15482 	struct sk_buff *msg;
15483 	void *hdr;
15484 
15485 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15486 	if (!msg)
15487 		return;
15488 
15489 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
15490 	if (!hdr) {
15491 		nlmsg_free(msg);
15492 		return;
15493 	}
15494 
15495 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15496 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15497 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
15498 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
15499 	    (key_id != -1 &&
15500 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
15501 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
15502 		goto nla_put_failure;
15503 
15504 	genlmsg_end(msg, hdr);
15505 
15506 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15507 				NL80211_MCGRP_MLME, gfp);
15508 	return;
15509 
15510  nla_put_failure:
15511 	nlmsg_free(msg);
15512 }
15513 
15514 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
15515 				    struct ieee80211_channel *channel_before,
15516 				    struct ieee80211_channel *channel_after)
15517 {
15518 	struct sk_buff *msg;
15519 	void *hdr;
15520 	struct nlattr *nl_freq;
15521 
15522 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
15523 	if (!msg)
15524 		return;
15525 
15526 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
15527 	if (!hdr) {
15528 		nlmsg_free(msg);
15529 		return;
15530 	}
15531 
15532 	/*
15533 	 * Since we are applying the beacon hint to a wiphy we know its
15534 	 * wiphy_idx is valid
15535 	 */
15536 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
15537 		goto nla_put_failure;
15538 
15539 	/* Before */
15540 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
15541 	if (!nl_freq)
15542 		goto nla_put_failure;
15543 
15544 	if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
15545 		goto nla_put_failure;
15546 	nla_nest_end(msg, nl_freq);
15547 
15548 	/* After */
15549 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
15550 	if (!nl_freq)
15551 		goto nla_put_failure;
15552 
15553 	if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
15554 		goto nla_put_failure;
15555 	nla_nest_end(msg, nl_freq);
15556 
15557 	genlmsg_end(msg, hdr);
15558 
15559 	rcu_read_lock();
15560 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
15561 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
15562 	rcu_read_unlock();
15563 
15564 	return;
15565 
15566 nla_put_failure:
15567 	nlmsg_free(msg);
15568 }
15569 
15570 static void nl80211_send_remain_on_chan_event(
15571 	int cmd, struct cfg80211_registered_device *rdev,
15572 	struct wireless_dev *wdev, u64 cookie,
15573 	struct ieee80211_channel *chan,
15574 	unsigned int duration, gfp_t gfp)
15575 {
15576 	struct sk_buff *msg;
15577 	void *hdr;
15578 
15579 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15580 	if (!msg)
15581 		return;
15582 
15583 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15584 	if (!hdr) {
15585 		nlmsg_free(msg);
15586 		return;
15587 	}
15588 
15589 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15590 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15591 					 wdev->netdev->ifindex)) ||
15592 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15593 			      NL80211_ATTR_PAD) ||
15594 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
15595 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
15596 			NL80211_CHAN_NO_HT) ||
15597 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
15598 			      NL80211_ATTR_PAD))
15599 		goto nla_put_failure;
15600 
15601 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
15602 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
15603 		goto nla_put_failure;
15604 
15605 	genlmsg_end(msg, hdr);
15606 
15607 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15608 				NL80211_MCGRP_MLME, gfp);
15609 	return;
15610 
15611  nla_put_failure:
15612 	nlmsg_free(msg);
15613 }
15614 
15615 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
15616 			       struct ieee80211_channel *chan,
15617 			       unsigned int duration, gfp_t gfp)
15618 {
15619 	struct wiphy *wiphy = wdev->wiphy;
15620 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15621 
15622 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
15623 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
15624 					  rdev, wdev, cookie, chan,
15625 					  duration, gfp);
15626 }
15627 EXPORT_SYMBOL(cfg80211_ready_on_channel);
15628 
15629 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
15630 					struct ieee80211_channel *chan,
15631 					gfp_t gfp)
15632 {
15633 	struct wiphy *wiphy = wdev->wiphy;
15634 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15635 
15636 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
15637 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
15638 					  rdev, wdev, cookie, chan, 0, gfp);
15639 }
15640 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
15641 
15642 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
15643 					struct ieee80211_channel *chan,
15644 					gfp_t gfp)
15645 {
15646 	struct wiphy *wiphy = wdev->wiphy;
15647 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15648 
15649 	trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
15650 	nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
15651 					  rdev, wdev, cookie, chan, 0, gfp);
15652 }
15653 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
15654 
15655 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
15656 		      struct station_info *sinfo, gfp_t gfp)
15657 {
15658 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
15659 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15660 	struct sk_buff *msg;
15661 
15662 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
15663 
15664 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15665 	if (!msg)
15666 		return;
15667 
15668 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
15669 				 rdev, dev, mac_addr, sinfo) < 0) {
15670 		nlmsg_free(msg);
15671 		return;
15672 	}
15673 
15674 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15675 				NL80211_MCGRP_MLME, gfp);
15676 }
15677 EXPORT_SYMBOL(cfg80211_new_sta);
15678 
15679 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
15680 			    struct station_info *sinfo, gfp_t gfp)
15681 {
15682 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
15683 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15684 	struct sk_buff *msg;
15685 	struct station_info empty_sinfo = {};
15686 
15687 	if (!sinfo)
15688 		sinfo = &empty_sinfo;
15689 
15690 	trace_cfg80211_del_sta(dev, mac_addr);
15691 
15692 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15693 	if (!msg) {
15694 		cfg80211_sinfo_release_content(sinfo);
15695 		return;
15696 	}
15697 
15698 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
15699 				 rdev, dev, mac_addr, sinfo) < 0) {
15700 		nlmsg_free(msg);
15701 		return;
15702 	}
15703 
15704 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15705 				NL80211_MCGRP_MLME, gfp);
15706 }
15707 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
15708 
15709 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
15710 			  enum nl80211_connect_failed_reason reason,
15711 			  gfp_t gfp)
15712 {
15713 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
15714 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15715 	struct sk_buff *msg;
15716 	void *hdr;
15717 
15718 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
15719 	if (!msg)
15720 		return;
15721 
15722 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
15723 	if (!hdr) {
15724 		nlmsg_free(msg);
15725 		return;
15726 	}
15727 
15728 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15729 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
15730 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
15731 		goto nla_put_failure;
15732 
15733 	genlmsg_end(msg, hdr);
15734 
15735 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15736 				NL80211_MCGRP_MLME, gfp);
15737 	return;
15738 
15739  nla_put_failure:
15740 	nlmsg_free(msg);
15741 }
15742 EXPORT_SYMBOL(cfg80211_conn_failed);
15743 
15744 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
15745 				       const u8 *addr, gfp_t gfp)
15746 {
15747 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15748 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15749 	struct sk_buff *msg;
15750 	void *hdr;
15751 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
15752 
15753 	if (!nlportid)
15754 		return false;
15755 
15756 	msg = nlmsg_new(100, gfp);
15757 	if (!msg)
15758 		return true;
15759 
15760 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15761 	if (!hdr) {
15762 		nlmsg_free(msg);
15763 		return true;
15764 	}
15765 
15766 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15767 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15768 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
15769 		goto nla_put_failure;
15770 
15771 	genlmsg_end(msg, hdr);
15772 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15773 	return true;
15774 
15775  nla_put_failure:
15776 	nlmsg_free(msg);
15777 	return true;
15778 }
15779 
15780 bool cfg80211_rx_spurious_frame(struct net_device *dev,
15781 				const u8 *addr, gfp_t gfp)
15782 {
15783 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15784 	bool ret;
15785 
15786 	trace_cfg80211_rx_spurious_frame(dev, addr);
15787 
15788 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
15789 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
15790 		trace_cfg80211_return_bool(false);
15791 		return false;
15792 	}
15793 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
15794 					 addr, gfp);
15795 	trace_cfg80211_return_bool(ret);
15796 	return ret;
15797 }
15798 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
15799 
15800 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
15801 					const u8 *addr, gfp_t gfp)
15802 {
15803 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15804 	bool ret;
15805 
15806 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
15807 
15808 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
15809 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
15810 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
15811 		trace_cfg80211_return_bool(false);
15812 		return false;
15813 	}
15814 	ret = __nl80211_unexpected_frame(dev,
15815 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
15816 					 addr, gfp);
15817 	trace_cfg80211_return_bool(ret);
15818 	return ret;
15819 }
15820 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
15821 
15822 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
15823 		      struct wireless_dev *wdev, u32 nlportid,
15824 		      int freq, int sig_dbm,
15825 		      const u8 *buf, size_t len, u32 flags, gfp_t gfp)
15826 {
15827 	struct net_device *netdev = wdev->netdev;
15828 	struct sk_buff *msg;
15829 	void *hdr;
15830 
15831 	msg = nlmsg_new(100 + len, gfp);
15832 	if (!msg)
15833 		return -ENOMEM;
15834 
15835 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
15836 	if (!hdr) {
15837 		nlmsg_free(msg);
15838 		return -ENOMEM;
15839 	}
15840 
15841 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15842 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15843 					netdev->ifindex)) ||
15844 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15845 			      NL80211_ATTR_PAD) ||
15846 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
15847 	    (sig_dbm &&
15848 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
15849 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15850 	    (flags &&
15851 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
15852 		goto nla_put_failure;
15853 
15854 	genlmsg_end(msg, hdr);
15855 
15856 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15857 
15858  nla_put_failure:
15859 	nlmsg_free(msg);
15860 	return -ENOBUFS;
15861 }
15862 
15863 void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
15864 			     const u8 *buf, size_t len, bool ack, gfp_t gfp)
15865 {
15866 	struct wiphy *wiphy = wdev->wiphy;
15867 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15868 	struct net_device *netdev = wdev->netdev;
15869 	struct sk_buff *msg;
15870 	void *hdr;
15871 
15872 	trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
15873 
15874 	msg = nlmsg_new(100 + len, gfp);
15875 	if (!msg)
15876 		return;
15877 
15878 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
15879 	if (!hdr) {
15880 		nlmsg_free(msg);
15881 		return;
15882 	}
15883 
15884 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15885 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15886 				   netdev->ifindex)) ||
15887 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15888 			      NL80211_ATTR_PAD) ||
15889 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15890 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
15891 			      NL80211_ATTR_PAD) ||
15892 	    (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
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);
15899 	return;
15900 
15901  nla_put_failure:
15902 	nlmsg_free(msg);
15903 }
15904 EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
15905 
15906 static int __nl80211_rx_control_port(struct net_device *dev,
15907 				     struct sk_buff *skb,
15908 				     bool unencrypted, gfp_t gfp)
15909 {
15910 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15911 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15912 	struct ethhdr *ehdr = eth_hdr(skb);
15913 	const u8 *addr = ehdr->h_source;
15914 	u16 proto = be16_to_cpu(skb->protocol);
15915 	struct sk_buff *msg;
15916 	void *hdr;
15917 	struct nlattr *frame;
15918 
15919 	u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
15920 
15921 	if (!nlportid)
15922 		return -ENOENT;
15923 
15924 	msg = nlmsg_new(100 + skb->len, gfp);
15925 	if (!msg)
15926 		return -ENOMEM;
15927 
15928 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
15929 	if (!hdr) {
15930 		nlmsg_free(msg);
15931 		return -ENOBUFS;
15932 	}
15933 
15934 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15935 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15936 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15937 			      NL80211_ATTR_PAD) ||
15938 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
15939 	    nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
15940 	    (unencrypted && nla_put_flag(msg,
15941 					 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
15942 		goto nla_put_failure;
15943 
15944 	frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
15945 	if (!frame)
15946 		goto nla_put_failure;
15947 
15948 	skb_copy_bits(skb, 0, nla_data(frame), skb->len);
15949 	genlmsg_end(msg, hdr);
15950 
15951 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15952 
15953  nla_put_failure:
15954 	nlmsg_free(msg);
15955 	return -ENOBUFS;
15956 }
15957 
15958 bool cfg80211_rx_control_port(struct net_device *dev,
15959 			      struct sk_buff *skb, bool unencrypted)
15960 {
15961 	int ret;
15962 
15963 	trace_cfg80211_rx_control_port(dev, skb, unencrypted);
15964 	ret = __nl80211_rx_control_port(dev, skb, unencrypted, GFP_ATOMIC);
15965 	trace_cfg80211_return_bool(ret == 0);
15966 	return ret == 0;
15967 }
15968 EXPORT_SYMBOL(cfg80211_rx_control_port);
15969 
15970 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
15971 					    const char *mac, gfp_t gfp)
15972 {
15973 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15974 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15975 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15976 	void **cb;
15977 
15978 	if (!msg)
15979 		return NULL;
15980 
15981 	cb = (void **)msg->cb;
15982 
15983 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
15984 	if (!cb[0]) {
15985 		nlmsg_free(msg);
15986 		return NULL;
15987 	}
15988 
15989 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15990 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15991 		goto nla_put_failure;
15992 
15993 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
15994 		goto nla_put_failure;
15995 
15996 	cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
15997 	if (!cb[1])
15998 		goto nla_put_failure;
15999 
16000 	cb[2] = rdev;
16001 
16002 	return msg;
16003  nla_put_failure:
16004 	nlmsg_free(msg);
16005 	return NULL;
16006 }
16007 
16008 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
16009 {
16010 	void **cb = (void **)msg->cb;
16011 	struct cfg80211_registered_device *rdev = cb[2];
16012 
16013 	nla_nest_end(msg, cb[1]);
16014 	genlmsg_end(msg, cb[0]);
16015 
16016 	memset(msg->cb, 0, sizeof(msg->cb));
16017 
16018 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16019 				NL80211_MCGRP_MLME, gfp);
16020 }
16021 
16022 void cfg80211_cqm_rssi_notify(struct net_device *dev,
16023 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
16024 			      s32 rssi_level, gfp_t gfp)
16025 {
16026 	struct sk_buff *msg;
16027 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16028 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16029 
16030 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
16031 
16032 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
16033 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
16034 		return;
16035 
16036 	if (wdev->cqm_config) {
16037 		wdev->cqm_config->last_rssi_event_value = rssi_level;
16038 
16039 		cfg80211_cqm_rssi_update(rdev, dev);
16040 
16041 		if (rssi_level == 0)
16042 			rssi_level = wdev->cqm_config->last_rssi_event_value;
16043 	}
16044 
16045 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
16046 	if (!msg)
16047 		return;
16048 
16049 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
16050 			rssi_event))
16051 		goto nla_put_failure;
16052 
16053 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
16054 				      rssi_level))
16055 		goto nla_put_failure;
16056 
16057 	cfg80211_send_cqm(msg, gfp);
16058 
16059 	return;
16060 
16061  nla_put_failure:
16062 	nlmsg_free(msg);
16063 }
16064 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
16065 
16066 void cfg80211_cqm_txe_notify(struct net_device *dev,
16067 			     const u8 *peer, u32 num_packets,
16068 			     u32 rate, u32 intvl, gfp_t gfp)
16069 {
16070 	struct sk_buff *msg;
16071 
16072 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
16073 	if (!msg)
16074 		return;
16075 
16076 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
16077 		goto nla_put_failure;
16078 
16079 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
16080 		goto nla_put_failure;
16081 
16082 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
16083 		goto nla_put_failure;
16084 
16085 	cfg80211_send_cqm(msg, gfp);
16086 	return;
16087 
16088  nla_put_failure:
16089 	nlmsg_free(msg);
16090 }
16091 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
16092 
16093 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
16094 				 const u8 *peer, u32 num_packets, gfp_t gfp)
16095 {
16096 	struct sk_buff *msg;
16097 
16098 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
16099 
16100 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
16101 	if (!msg)
16102 		return;
16103 
16104 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
16105 		goto nla_put_failure;
16106 
16107 	cfg80211_send_cqm(msg, gfp);
16108 	return;
16109 
16110  nla_put_failure:
16111 	nlmsg_free(msg);
16112 }
16113 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
16114 
16115 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
16116 {
16117 	struct sk_buff *msg;
16118 
16119 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
16120 	if (!msg)
16121 		return;
16122 
16123 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
16124 		goto nla_put_failure;
16125 
16126 	cfg80211_send_cqm(msg, gfp);
16127 	return;
16128 
16129  nla_put_failure:
16130 	nlmsg_free(msg);
16131 }
16132 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
16133 
16134 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
16135 				     struct net_device *netdev, const u8 *bssid,
16136 				     const u8 *replay_ctr, gfp_t gfp)
16137 {
16138 	struct sk_buff *msg;
16139 	struct nlattr *rekey_attr;
16140 	void *hdr;
16141 
16142 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16143 	if (!msg)
16144 		return;
16145 
16146 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
16147 	if (!hdr) {
16148 		nlmsg_free(msg);
16149 		return;
16150 	}
16151 
16152 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16153 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16154 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
16155 		goto nla_put_failure;
16156 
16157 	rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
16158 	if (!rekey_attr)
16159 		goto nla_put_failure;
16160 
16161 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
16162 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
16163 		goto nla_put_failure;
16164 
16165 	nla_nest_end(msg, rekey_attr);
16166 
16167 	genlmsg_end(msg, hdr);
16168 
16169 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16170 				NL80211_MCGRP_MLME, gfp);
16171 	return;
16172 
16173  nla_put_failure:
16174 	nlmsg_free(msg);
16175 }
16176 
16177 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
16178 			       const u8 *replay_ctr, gfp_t gfp)
16179 {
16180 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16181 	struct wiphy *wiphy = wdev->wiphy;
16182 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16183 
16184 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
16185 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
16186 }
16187 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
16188 
16189 static void
16190 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
16191 			       struct net_device *netdev, int index,
16192 			       const u8 *bssid, bool preauth, gfp_t gfp)
16193 {
16194 	struct sk_buff *msg;
16195 	struct nlattr *attr;
16196 	void *hdr;
16197 
16198 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16199 	if (!msg)
16200 		return;
16201 
16202 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
16203 	if (!hdr) {
16204 		nlmsg_free(msg);
16205 		return;
16206 	}
16207 
16208 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16209 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
16210 		goto nla_put_failure;
16211 
16212 	attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
16213 	if (!attr)
16214 		goto nla_put_failure;
16215 
16216 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
16217 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
16218 	    (preauth &&
16219 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
16220 		goto nla_put_failure;
16221 
16222 	nla_nest_end(msg, attr);
16223 
16224 	genlmsg_end(msg, hdr);
16225 
16226 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16227 				NL80211_MCGRP_MLME, gfp);
16228 	return;
16229 
16230  nla_put_failure:
16231 	nlmsg_free(msg);
16232 }
16233 
16234 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
16235 				     const u8 *bssid, bool preauth, gfp_t gfp)
16236 {
16237 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16238 	struct wiphy *wiphy = wdev->wiphy;
16239 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16240 
16241 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
16242 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
16243 }
16244 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
16245 
16246 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
16247 				     struct net_device *netdev,
16248 				     struct cfg80211_chan_def *chandef,
16249 				     gfp_t gfp,
16250 				     enum nl80211_commands notif,
16251 				     u8 count)
16252 {
16253 	struct sk_buff *msg;
16254 	void *hdr;
16255 
16256 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16257 	if (!msg)
16258 		return;
16259 
16260 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
16261 	if (!hdr) {
16262 		nlmsg_free(msg);
16263 		return;
16264 	}
16265 
16266 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
16267 		goto nla_put_failure;
16268 
16269 	if (nl80211_send_chandef(msg, chandef))
16270 		goto nla_put_failure;
16271 
16272 	if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
16273 	    (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
16274 			goto nla_put_failure;
16275 
16276 	genlmsg_end(msg, hdr);
16277 
16278 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16279 				NL80211_MCGRP_MLME, gfp);
16280 	return;
16281 
16282  nla_put_failure:
16283 	nlmsg_free(msg);
16284 }
16285 
16286 void cfg80211_ch_switch_notify(struct net_device *dev,
16287 			       struct cfg80211_chan_def *chandef)
16288 {
16289 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16290 	struct wiphy *wiphy = wdev->wiphy;
16291 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16292 
16293 	ASSERT_WDEV_LOCK(wdev);
16294 
16295 	trace_cfg80211_ch_switch_notify(dev, chandef);
16296 
16297 	wdev->chandef = *chandef;
16298 	wdev->preset_chandef = *chandef;
16299 
16300 	if (wdev->iftype == NL80211_IFTYPE_STATION &&
16301 	    !WARN_ON(!wdev->current_bss))
16302 		cfg80211_update_assoc_bss_entry(wdev, chandef->chan);
16303 
16304 	cfg80211_sched_dfs_chan_update(rdev);
16305 
16306 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
16307 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0);
16308 }
16309 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
16310 
16311 void cfg80211_ch_switch_started_notify(struct net_device *dev,
16312 				       struct cfg80211_chan_def *chandef,
16313 				       u8 count)
16314 {
16315 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16316 	struct wiphy *wiphy = wdev->wiphy;
16317 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16318 
16319 	trace_cfg80211_ch_switch_started_notify(dev, chandef);
16320 
16321 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
16322 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
16323 }
16324 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
16325 
16326 void
16327 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
16328 		     const struct cfg80211_chan_def *chandef,
16329 		     enum nl80211_radar_event event,
16330 		     struct net_device *netdev, gfp_t gfp)
16331 {
16332 	struct sk_buff *msg;
16333 	void *hdr;
16334 
16335 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16336 	if (!msg)
16337 		return;
16338 
16339 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
16340 	if (!hdr) {
16341 		nlmsg_free(msg);
16342 		return;
16343 	}
16344 
16345 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
16346 		goto nla_put_failure;
16347 
16348 	/* NOP and radar events don't need a netdev parameter */
16349 	if (netdev) {
16350 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
16351 
16352 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16353 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16354 				      NL80211_ATTR_PAD))
16355 			goto nla_put_failure;
16356 	}
16357 
16358 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
16359 		goto nla_put_failure;
16360 
16361 	if (nl80211_send_chandef(msg, chandef))
16362 		goto nla_put_failure;
16363 
16364 	genlmsg_end(msg, hdr);
16365 
16366 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16367 				NL80211_MCGRP_MLME, gfp);
16368 	return;
16369 
16370  nla_put_failure:
16371 	nlmsg_free(msg);
16372 }
16373 
16374 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
16375 				       struct sta_opmode_info *sta_opmode,
16376 				       gfp_t gfp)
16377 {
16378 	struct sk_buff *msg;
16379 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16380 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16381 	void *hdr;
16382 
16383 	if (WARN_ON(!mac))
16384 		return;
16385 
16386 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16387 	if (!msg)
16388 		return;
16389 
16390 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
16391 	if (!hdr) {
16392 		nlmsg_free(msg);
16393 		return;
16394 	}
16395 
16396 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
16397 		goto nla_put_failure;
16398 
16399 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
16400 		goto nla_put_failure;
16401 
16402 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
16403 		goto nla_put_failure;
16404 
16405 	if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
16406 	    nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
16407 		goto nla_put_failure;
16408 
16409 	if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
16410 	    nla_put_u8(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
16411 		goto nla_put_failure;
16412 
16413 	if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
16414 	    nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
16415 		goto nla_put_failure;
16416 
16417 	genlmsg_end(msg, hdr);
16418 
16419 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16420 				NL80211_MCGRP_MLME, gfp);
16421 
16422 	return;
16423 
16424 nla_put_failure:
16425 	nlmsg_free(msg);
16426 }
16427 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
16428 
16429 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
16430 			   u64 cookie, bool acked, s32 ack_signal,
16431 			   bool is_valid_ack_signal, gfp_t gfp)
16432 {
16433 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16434 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16435 	struct sk_buff *msg;
16436 	void *hdr;
16437 
16438 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
16439 
16440 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16441 
16442 	if (!msg)
16443 		return;
16444 
16445 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
16446 	if (!hdr) {
16447 		nlmsg_free(msg);
16448 		return;
16449 	}
16450 
16451 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16452 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16453 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
16454 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
16455 			      NL80211_ATTR_PAD) ||
16456 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
16457 	    (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
16458 						ack_signal)))
16459 		goto nla_put_failure;
16460 
16461 	genlmsg_end(msg, hdr);
16462 
16463 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16464 				NL80211_MCGRP_MLME, gfp);
16465 	return;
16466 
16467  nla_put_failure:
16468 	nlmsg_free(msg);
16469 }
16470 EXPORT_SYMBOL(cfg80211_probe_status);
16471 
16472 void cfg80211_report_obss_beacon(struct wiphy *wiphy,
16473 				 const u8 *frame, size_t len,
16474 				 int freq, int sig_dbm)
16475 {
16476 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16477 	struct sk_buff *msg;
16478 	void *hdr;
16479 	struct cfg80211_beacon_registration *reg;
16480 
16481 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
16482 
16483 	spin_lock_bh(&rdev->beacon_registrations_lock);
16484 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
16485 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
16486 		if (!msg) {
16487 			spin_unlock_bh(&rdev->beacon_registrations_lock);
16488 			return;
16489 		}
16490 
16491 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
16492 		if (!hdr)
16493 			goto nla_put_failure;
16494 
16495 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16496 		    (freq &&
16497 		     nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
16498 		    (sig_dbm &&
16499 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
16500 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
16501 			goto nla_put_failure;
16502 
16503 		genlmsg_end(msg, hdr);
16504 
16505 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
16506 	}
16507 	spin_unlock_bh(&rdev->beacon_registrations_lock);
16508 	return;
16509 
16510  nla_put_failure:
16511 	spin_unlock_bh(&rdev->beacon_registrations_lock);
16512 	nlmsg_free(msg);
16513 }
16514 EXPORT_SYMBOL(cfg80211_report_obss_beacon);
16515 
16516 #ifdef CONFIG_PM
16517 static int cfg80211_net_detect_results(struct sk_buff *msg,
16518 				       struct cfg80211_wowlan_wakeup *wakeup)
16519 {
16520 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
16521 	struct nlattr *nl_results, *nl_match, *nl_freqs;
16522 	int i, j;
16523 
16524 	nl_results = nla_nest_start_noflag(msg,
16525 					   NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
16526 	if (!nl_results)
16527 		return -EMSGSIZE;
16528 
16529 	for (i = 0; i < nd->n_matches; i++) {
16530 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
16531 
16532 		nl_match = nla_nest_start_noflag(msg, i);
16533 		if (!nl_match)
16534 			break;
16535 
16536 		/* The SSID attribute is optional in nl80211, but for
16537 		 * simplicity reasons it's always present in the
16538 		 * cfg80211 structure.  If a driver can't pass the
16539 		 * SSID, that needs to be changed.  A zero length SSID
16540 		 * is still a valid SSID (wildcard), so it cannot be
16541 		 * used for this purpose.
16542 		 */
16543 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
16544 			    match->ssid.ssid)) {
16545 			nla_nest_cancel(msg, nl_match);
16546 			goto out;
16547 		}
16548 
16549 		if (match->n_channels) {
16550 			nl_freqs = nla_nest_start_noflag(msg,
16551 							 NL80211_ATTR_SCAN_FREQUENCIES);
16552 			if (!nl_freqs) {
16553 				nla_nest_cancel(msg, nl_match);
16554 				goto out;
16555 			}
16556 
16557 			for (j = 0; j < match->n_channels; j++) {
16558 				if (nla_put_u32(msg, j, match->channels[j])) {
16559 					nla_nest_cancel(msg, nl_freqs);
16560 					nla_nest_cancel(msg, nl_match);
16561 					goto out;
16562 				}
16563 			}
16564 
16565 			nla_nest_end(msg, nl_freqs);
16566 		}
16567 
16568 		nla_nest_end(msg, nl_match);
16569 	}
16570 
16571 out:
16572 	nla_nest_end(msg, nl_results);
16573 	return 0;
16574 }
16575 
16576 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
16577 				   struct cfg80211_wowlan_wakeup *wakeup,
16578 				   gfp_t gfp)
16579 {
16580 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16581 	struct sk_buff *msg;
16582 	void *hdr;
16583 	int size = 200;
16584 
16585 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
16586 
16587 	if (wakeup)
16588 		size += wakeup->packet_present_len;
16589 
16590 	msg = nlmsg_new(size, gfp);
16591 	if (!msg)
16592 		return;
16593 
16594 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
16595 	if (!hdr)
16596 		goto free_msg;
16597 
16598 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16599 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16600 			      NL80211_ATTR_PAD))
16601 		goto free_msg;
16602 
16603 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
16604 					wdev->netdev->ifindex))
16605 		goto free_msg;
16606 
16607 	if (wakeup) {
16608 		struct nlattr *reasons;
16609 
16610 		reasons = nla_nest_start_noflag(msg,
16611 						NL80211_ATTR_WOWLAN_TRIGGERS);
16612 		if (!reasons)
16613 			goto free_msg;
16614 
16615 		if (wakeup->disconnect &&
16616 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
16617 			goto free_msg;
16618 		if (wakeup->magic_pkt &&
16619 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
16620 			goto free_msg;
16621 		if (wakeup->gtk_rekey_failure &&
16622 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
16623 			goto free_msg;
16624 		if (wakeup->eap_identity_req &&
16625 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
16626 			goto free_msg;
16627 		if (wakeup->four_way_handshake &&
16628 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
16629 			goto free_msg;
16630 		if (wakeup->rfkill_release &&
16631 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
16632 			goto free_msg;
16633 
16634 		if (wakeup->pattern_idx >= 0 &&
16635 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
16636 				wakeup->pattern_idx))
16637 			goto free_msg;
16638 
16639 		if (wakeup->tcp_match &&
16640 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
16641 			goto free_msg;
16642 
16643 		if (wakeup->tcp_connlost &&
16644 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
16645 			goto free_msg;
16646 
16647 		if (wakeup->tcp_nomoretokens &&
16648 		    nla_put_flag(msg,
16649 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
16650 			goto free_msg;
16651 
16652 		if (wakeup->packet) {
16653 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
16654 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
16655 
16656 			if (!wakeup->packet_80211) {
16657 				pkt_attr =
16658 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
16659 				len_attr =
16660 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
16661 			}
16662 
16663 			if (wakeup->packet_len &&
16664 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
16665 				goto free_msg;
16666 
16667 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
16668 				    wakeup->packet))
16669 				goto free_msg;
16670 		}
16671 
16672 		if (wakeup->net_detect &&
16673 		    cfg80211_net_detect_results(msg, wakeup))
16674 				goto free_msg;
16675 
16676 		nla_nest_end(msg, reasons);
16677 	}
16678 
16679 	genlmsg_end(msg, hdr);
16680 
16681 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16682 				NL80211_MCGRP_MLME, gfp);
16683 	return;
16684 
16685  free_msg:
16686 	nlmsg_free(msg);
16687 }
16688 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
16689 #endif
16690 
16691 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
16692 				enum nl80211_tdls_operation oper,
16693 				u16 reason_code, gfp_t gfp)
16694 {
16695 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16696 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16697 	struct sk_buff *msg;
16698 	void *hdr;
16699 
16700 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
16701 					 reason_code);
16702 
16703 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16704 	if (!msg)
16705 		return;
16706 
16707 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
16708 	if (!hdr) {
16709 		nlmsg_free(msg);
16710 		return;
16711 	}
16712 
16713 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16714 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16715 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
16716 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
16717 	    (reason_code > 0 &&
16718 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
16719 		goto nla_put_failure;
16720 
16721 	genlmsg_end(msg, hdr);
16722 
16723 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16724 				NL80211_MCGRP_MLME, gfp);
16725 	return;
16726 
16727  nla_put_failure:
16728 	nlmsg_free(msg);
16729 }
16730 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
16731 
16732 static int nl80211_netlink_notify(struct notifier_block * nb,
16733 				  unsigned long state,
16734 				  void *_notify)
16735 {
16736 	struct netlink_notify *notify = _notify;
16737 	struct cfg80211_registered_device *rdev;
16738 	struct wireless_dev *wdev;
16739 	struct cfg80211_beacon_registration *reg, *tmp;
16740 
16741 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
16742 		return NOTIFY_DONE;
16743 
16744 	rcu_read_lock();
16745 
16746 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
16747 		struct cfg80211_sched_scan_request *sched_scan_req;
16748 
16749 		list_for_each_entry_rcu(sched_scan_req,
16750 					&rdev->sched_scan_req_list,
16751 					list) {
16752 			if (sched_scan_req->owner_nlportid == notify->portid) {
16753 				sched_scan_req->nl_owner_dead = true;
16754 				schedule_work(&rdev->sched_scan_stop_wk);
16755 			}
16756 		}
16757 
16758 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
16759 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
16760 
16761 			if (wdev->owner_nlportid == notify->portid) {
16762 				wdev->nl_owner_dead = true;
16763 				schedule_work(&rdev->destroy_work);
16764 			} else if (wdev->conn_owner_nlportid == notify->portid) {
16765 				schedule_work(&wdev->disconnect_wk);
16766 			}
16767 
16768 			cfg80211_release_pmsr(wdev, notify->portid);
16769 		}
16770 
16771 		spin_lock_bh(&rdev->beacon_registrations_lock);
16772 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
16773 					 list) {
16774 			if (reg->nlportid == notify->portid) {
16775 				list_del(&reg->list);
16776 				kfree(reg);
16777 				break;
16778 			}
16779 		}
16780 		spin_unlock_bh(&rdev->beacon_registrations_lock);
16781 	}
16782 
16783 	rcu_read_unlock();
16784 
16785 	/*
16786 	 * It is possible that the user space process that is controlling the
16787 	 * indoor setting disappeared, so notify the regulatory core.
16788 	 */
16789 	regulatory_netlink_notify(notify->portid);
16790 	return NOTIFY_OK;
16791 }
16792 
16793 static struct notifier_block nl80211_netlink_notifier = {
16794 	.notifier_call = nl80211_netlink_notify,
16795 };
16796 
16797 void cfg80211_ft_event(struct net_device *netdev,
16798 		       struct cfg80211_ft_event_params *ft_event)
16799 {
16800 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
16801 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16802 	struct sk_buff *msg;
16803 	void *hdr;
16804 
16805 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
16806 
16807 	if (!ft_event->target_ap)
16808 		return;
16809 
16810 	msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
16811 			GFP_KERNEL);
16812 	if (!msg)
16813 		return;
16814 
16815 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
16816 	if (!hdr)
16817 		goto out;
16818 
16819 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16820 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16821 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
16822 		goto out;
16823 
16824 	if (ft_event->ies &&
16825 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
16826 		goto out;
16827 	if (ft_event->ric_ies &&
16828 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
16829 		    ft_event->ric_ies))
16830 		goto out;
16831 
16832 	genlmsg_end(msg, hdr);
16833 
16834 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16835 				NL80211_MCGRP_MLME, GFP_KERNEL);
16836 	return;
16837  out:
16838 	nlmsg_free(msg);
16839 }
16840 EXPORT_SYMBOL(cfg80211_ft_event);
16841 
16842 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
16843 {
16844 	struct cfg80211_registered_device *rdev;
16845 	struct sk_buff *msg;
16846 	void *hdr;
16847 	u32 nlportid;
16848 
16849 	rdev = wiphy_to_rdev(wdev->wiphy);
16850 	if (!rdev->crit_proto_nlportid)
16851 		return;
16852 
16853 	nlportid = rdev->crit_proto_nlportid;
16854 	rdev->crit_proto_nlportid = 0;
16855 
16856 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16857 	if (!msg)
16858 		return;
16859 
16860 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
16861 	if (!hdr)
16862 		goto nla_put_failure;
16863 
16864 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16865 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16866 			      NL80211_ATTR_PAD))
16867 		goto nla_put_failure;
16868 
16869 	genlmsg_end(msg, hdr);
16870 
16871 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
16872 	return;
16873 
16874  nla_put_failure:
16875 	nlmsg_free(msg);
16876 }
16877 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
16878 
16879 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
16880 {
16881 	struct wiphy *wiphy = wdev->wiphy;
16882 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16883 	struct sk_buff *msg;
16884 	void *hdr;
16885 
16886 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
16887 	if (!msg)
16888 		return;
16889 
16890 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
16891 	if (!hdr)
16892 		goto out;
16893 
16894 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16895 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
16896 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16897 			      NL80211_ATTR_PAD))
16898 		goto out;
16899 
16900 	genlmsg_end(msg, hdr);
16901 
16902 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
16903 				NL80211_MCGRP_MLME, GFP_KERNEL);
16904 	return;
16905  out:
16906 	nlmsg_free(msg);
16907 }
16908 
16909 int cfg80211_external_auth_request(struct net_device *dev,
16910 				   struct cfg80211_external_auth_params *params,
16911 				   gfp_t gfp)
16912 {
16913 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16914 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16915 	struct sk_buff *msg;
16916 	void *hdr;
16917 
16918 	if (!wdev->conn_owner_nlportid)
16919 		return -EINVAL;
16920 
16921 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16922 	if (!msg)
16923 		return -ENOMEM;
16924 
16925 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
16926 	if (!hdr)
16927 		goto nla_put_failure;
16928 
16929 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16930 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16931 	    nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
16932 	    nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
16933 			params->action) ||
16934 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
16935 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
16936 		    params->ssid.ssid))
16937 		goto nla_put_failure;
16938 
16939 	genlmsg_end(msg, hdr);
16940 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
16941 			wdev->conn_owner_nlportid);
16942 	return 0;
16943 
16944  nla_put_failure:
16945 	nlmsg_free(msg);
16946 	return -ENOBUFS;
16947 }
16948 EXPORT_SYMBOL(cfg80211_external_auth_request);
16949 
16950 void cfg80211_update_owe_info_event(struct net_device *netdev,
16951 				    struct cfg80211_update_owe_info *owe_info,
16952 				    gfp_t gfp)
16953 {
16954 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
16955 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16956 	struct sk_buff *msg;
16957 	void *hdr;
16958 
16959 	trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
16960 
16961 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16962 	if (!msg)
16963 		return;
16964 
16965 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
16966 	if (!hdr)
16967 		goto nla_put_failure;
16968 
16969 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16970 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16971 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
16972 		goto nla_put_failure;
16973 
16974 	if (!owe_info->ie_len ||
16975 	    nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
16976 		goto nla_put_failure;
16977 
16978 	genlmsg_end(msg, hdr);
16979 
16980 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16981 				NL80211_MCGRP_MLME, gfp);
16982 	return;
16983 
16984 nla_put_failure:
16985 	genlmsg_cancel(msg, hdr);
16986 	nlmsg_free(msg);
16987 }
16988 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
16989 
16990 /* initialisation/exit functions */
16991 
16992 int __init nl80211_init(void)
16993 {
16994 	int err;
16995 
16996 	err = genl_register_family(&nl80211_fam);
16997 	if (err)
16998 		return err;
16999 
17000 	err = netlink_register_notifier(&nl80211_netlink_notifier);
17001 	if (err)
17002 		goto err_out;
17003 
17004 	return 0;
17005  err_out:
17006 	genl_unregister_family(&nl80211_fam);
17007 	return err;
17008 }
17009 
17010 void nl80211_exit(void)
17011 {
17012 	netlink_unregister_notifier(&nl80211_netlink_notifier);
17013 	genl_unregister_family(&nl80211_fam);
17014 }
17015